forked from laruence/taint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
taint.c
2828 lines (2498 loc) · 84.9 KB
/
taint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Xinchen Hui <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "SAPI.h"
#include "zend_compile.h"
#include "zend_execute.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_taint.h"
ZEND_DECLARE_MODULE_GLOBALS(taint)
/* {{{ TAINT_ARG_INFO
*/
ZEND_BEGIN_ARG_INFO_EX(taint_arginfo, 0, 0, 1)
ZEND_ARG_INFO(1, string)
ZEND_ARG_INFO(1, ...)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(untaint_arginfo, 0, 0, 1)
ZEND_ARG_INFO(1, string)
ZEND_ARG_INFO(1, ...)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(is_tainted_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, string)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ taint_functions[]
*/
zend_function_entry taint_functions[] = {
PHP_FE(taint, taint_arginfo)
PHP_FE(untaint, untaint_arginfo)
PHP_FE(is_tainted, is_tainted_arginfo)
{NULL, NULL, NULL}
};
/* }}} */
/** {{{ module depends
*/
#if ZEND_MODULE_API_NO >= 20050922
zend_module_dep taint_deps[] = {
ZEND_MOD_CONFLICTS("xdebug")
{NULL, NULL, NULL}
};
#endif
/* }}} */
/* {{{ taint_module_entry
*/
zend_module_entry taint_module_entry = {
#if ZEND_MODULE_API_NO >= 20050922
STANDARD_MODULE_HEADER_EX, NULL,
taint_deps,
#else
STANDARD_MODULE_HEADER,
#endif
"taint",
taint_functions,
PHP_MINIT(taint),
PHP_MSHUTDOWN(taint),
PHP_RINIT(taint), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(taint), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(taint),
#if ZEND_MODULE_API_NO >= 20010901
PHP_TAINT_VERSION, /* Replace with version number for your extension */
#endif
PHP_MODULE_GLOBALS(taint),
NULL,
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
static struct taint_overridden_fucs /* {{{ */ {
php_func strval;
php_func sprintf;
php_func vsprintf;
php_func explode;
php_func implode;
php_func trim;
php_func rtrim;
php_func ltrim;
php_func strstr;
php_func str_pad;
php_func str_replace;
php_func substr;
php_func strtolower;
php_func strtoupper;
} taint_origin_funcs;
#define TAINT_O_FUNC(m) (taint_origin_funcs.m)
/* }}} */
static void php_taint_mark_strings(zval *symbol_table TSRMLS_DC) /* {{{ */ {
zval **ppzval;
HashTable *ht = Z_ARRVAL_P(symbol_table);
HashPosition pos = {0};
for(zend_hash_internal_pointer_reset_ex(ht, &pos);
zend_hash_has_more_elements_ex(ht, &pos) == SUCCESS;
zend_hash_move_forward_ex(ht, &pos)) {
if (zend_hash_get_current_data_ex(ht, (void**)&ppzval, &pos) == FAILURE) {
continue;
}
if (Z_TYPE_PP(ppzval) == IS_ARRAY) {
php_taint_mark_strings(*ppzval TSRMLS_CC);
} else if (IS_STRING == Z_TYPE_PP(ppzval)) {
Z_STRVAL_PP(ppzval) = erealloc(Z_STRVAL_PP(ppzval), Z_STRLEN_PP(ppzval) + 1 + PHP_TAINT_MAGIC_LENGTH);
PHP_TAINT_MARK(*ppzval, PHP_TAINT_MAGIC_POSSIBLE);
}
}
} /* }}} */
static void taint_pzval_unlock_func(zval *z, taint_free_op *should_free, int unref) /* {{{ */ {
if (!Z_DELREF_P(z)) {
Z_SET_REFCOUNT_P(z, 1);
Z_UNSET_ISREF_P(z);
should_free->var = z;
} else {
should_free->var = 0;
if (unref && Z_ISREF_P(z) && Z_REFCOUNT_P(z) == 1) {
should_free->is_ref = 1;
Z_UNSET_ISREF_P(z);
}
}
} /* }}} */
static void taint_pzval_unlock_free_func(zval *z) /* {{{ */ {
if (!Z_DELREF_P(z)) {
zval_dtor(z);
safe_free_zval_ptr(z);
}
} /* }}} */
static void taint_pzval_lock_func(zval *z, taint_free_op *should_free) /* {{{ */ {
if (should_free->type == IS_VAR) {
Z_ADDREF_P(z);
if (should_free->var && should_free->is_ref) {
Z_SET_ISREF_P(z);
}
}
} /* }}} */
static void php_taint_get_cv_address(zend_compiled_variable *cv, zval ***ptr, temp_variable *Ts TSRMLS_DC) /* {{{ */ {
zval *new_zval = &EG(uninitialized_zval);
Z_ADDREF_P(new_zval);
zend_hash_quick_update(EG(active_symbol_table), cv->name, cv->name_len+1, cv->hash_value, &new_zval, sizeof(zval *), (void **)ptr);
}
/* }}} */
static zval **php_taint_get_obj_zval_ptr_ptr_unused(TSRMLS_D) /* {{{ */ {
if (EG(This)) {
return &EG(This);
} else {
zend_error(E_ERROR, "Using $this when not in object context");
return NULL;
}
} /* }}} */
static void make_real_object(zval **object_ptr TSRMLS_DC) /* {{{ */ {
if (Z_TYPE_PP(object_ptr) == IS_NULL
|| (Z_TYPE_PP(object_ptr) == IS_BOOL && Z_LVAL_PP(object_ptr) == 0)
|| (Z_TYPE_PP(object_ptr) == IS_STRING && Z_STRLEN_PP(object_ptr) == 0)
) {
zend_error(E_STRICT, "Creating default object from empty value");
SEPARATE_ZVAL_IF_NOT_REF(object_ptr);
zval_dtor(*object_ptr);
object_init(*object_ptr);
}
} /* }}} */
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
static zval * php_taint_get_zval_ptr_var(znode *node, temp_variable *Ts, taint_free_op *should_free TSRMLS_DC) /* {{{ */ {
zval *ptr = TAINT_TS(node->u.var).var.ptr;
if (ptr) {
TAINT_PZVAL_UNLOCK(ptr, should_free);
return ptr;
} else {
temp_variable *T = (temp_variable *)((char *)Ts + node->u.var);
zval *str = T->str_offset.str;
/* string offset */
ALLOC_ZVAL(ptr);
T->str_offset.ptr = ptr;
should_free->var = ptr;
if (T->str_offset.str->type != IS_STRING
|| ((int)T->str_offset.offset < 0)
|| (T->str_offset.str->value.str.len <= (int)T->str_offset.offset)) {
ptr->value.str.val = STR_EMPTY_ALLOC();
ptr->value.str.len = 0;
} else {
char c = str->value.str.val[T->str_offset.offset];
ptr->value.str.val = estrndup(&c, 1);
ptr->value.str.len = 1;
}
TAINT_PZVAL_UNLOCK_FREE(str);
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 3)
ptr->refcount = 1;
ptr->is_ref = 1;
#else
ptr->refcount__gc = 1;
ptr->is_ref__gc = 1;
#endif
ptr->type = IS_STRING;
return ptr;
}
} /* }}} */
static zval * php_taint_get_zval_ptr_cv(znode *node, temp_variable *Ts TSRMLS_DC) /* {{{ */ {
zval ***ptr = &TAINT_CV_OF(node->u.var);
if (!*ptr) {
zend_compiled_variable *cv = &TAINT_CV_DEF_OF(node->u.var);
if (!EG(active_symbol_table) || zend_hash_quick_find(EG(active_symbol_table), cv->name, cv->name_len + 1, cv->hash_value, (void **)ptr) == FAILURE) {
zend_error(E_NOTICE, "Undefined variable: %s", cv->name);
return &EG(uninitialized_zval);
}
}
return **ptr;
} /* }}} */
static zval * php_taint_get_zval_ptr_tmp(znode *node, temp_variable *Ts, taint_free_op *should_free TSRMLS_DC) /* {{{ */ {
return should_free->var = &TAINT_TS(node->u.var).tmp_var;
} /* }}} */
static zval ** php_taint_get_zval_ptr_ptr_var(znode *node, temp_variable *Ts, taint_free_op *should_free TSRMLS_DC) /* {{{ */ {
zval** ptr_ptr = TAINT_TS(node->u.var).var.ptr_ptr;
if (ptr_ptr) {
TAINT_PZVAL_UNLOCK(*ptr_ptr, should_free);
} else {
/* string offset */
TAINT_PZVAL_UNLOCK(TAINT_TS(node->u.var).str_offset.str, should_free);
}
return ptr_ptr;
} /* }}} */
static zval **php_taint_get_zval_ptr_ptr_cv(znode *node, temp_variable *Ts, int type TSRMLS_DC) /* {{{ */ {
zval ***ptr = &TAINT_CV_OF(node->u.var);
if (!*ptr) {
zend_compiled_variable *cv = &TAINT_CV_DEF_OF(node->u.var);
if (!EG(active_symbol_table)
|| zend_hash_quick_find(EG(active_symbol_table), cv->name, cv->name_len+1, cv->hash_value, (void **) ptr )==FAILURE) {
switch (type) {
case BP_VAR_R:
case BP_VAR_UNSET:
zend_error(E_NOTICE, "Undefined variable: %s", cv->name);
/* break missing intentionally */
case BP_VAR_IS:
return &EG(uninitialized_zval_ptr);
break;
case BP_VAR_RW:
zend_error(E_NOTICE, "Undefined variable: %s", cv->name);
/* break missing intentionally */
case BP_VAR_W:
php_taint_get_cv_address(cv, ptr, Ts TSRMLS_CC);
break;
}
}
}
return *ptr;
} /* }}} */
static zval **php_taint_get_zval_ptr_ptr(znode *node, temp_variable *Ts, taint_free_op *should_free, int type TSRMLS_DC) /* {{{ */ {
should_free->type = node->op_type;
if (node->op_type == IS_CV) {
should_free->var = 0;
return php_taint_get_zval_ptr_ptr_cv(node, Ts, type TSRMLS_CC);
} else if (node->op_type == IS_VAR) {
return php_taint_get_zval_ptr_ptr_var(node, Ts, should_free TSRMLS_CC);
} else {
should_free->var = 0;
return NULL;
}
} /* }}} */
static zval *php_taint_get_zval_ptr(znode *node, temp_variable *Ts, taint_free_op *should_free, int type TSRMLS_DC) /* {{{ */ {
/* should_free->is_var = 0; */
switch (node->op_type) {
case IS_CONST:
should_free->var = 0;
return &node->u.constant;
break;
case IS_TMP_VAR:
should_free->var = TAINT_TMP_FREE(&TAINT_TS(node->u.var).tmp_var);
return &TAINT_TS(node->u.var).tmp_var;
break;
case IS_VAR:
return php_taint_get_zval_ptr_var(node, Ts, should_free TSRMLS_CC);
break;
case IS_UNUSED:
should_free->var = 0;
return NULL;
break;
case IS_CV:
should_free->var = 0;
return php_taint_get_zval_ptr_cv(node, Ts TSRMLS_CC);
break;
EMPTY_SWITCH_DEFAULT_CASE()
}
return NULL;
} /* }}} */
static int php_taint_qm_assign_handler(ZEND_OPCODE_HANDLER_ARGS) /* {{{ */ {
zend_op *opline = execute_data->opline;
zval *op1 = NULL;
taint_free_op free_op1 = {0};
switch(TAINT_OP1_TYPE(opline)) {
case IS_TMP_VAR:
op1 = php_taint_get_zval_ptr_tmp(TAINT_OP1_NODE_PTR(opline), execute_data->Ts, &free_op1 TSRMLS_CC);
break;
case IS_VAR:
op1 = php_taint_get_zval_ptr_var(TAINT_OP1_NODE_PTR(opline), execute_data->Ts, &free_op1 TSRMLS_CC);
break;
case IS_CV:
op1 = php_taint_get_zval_ptr_cv(TAINT_OP1_NODE_PTR(opline), TAINT_GET_ZVAL_PTR_CV_2ND_ARG(BP_VAR_R) TSRMLS_CC);
break;
case IS_CONST:
op1 = TAINT_OP1_CONSTANT_PTR(opline);
break;
}
TAINT_T(TAINT_RESULT_VAR(opline)).tmp_var = *op1;
if (!((zend_uintptr_t)free_op1.var & 1L)) {
zval_copy_ctor(&TAINT_T(TAINT_RESULT_VAR(opline)).tmp_var);
if (op1 && IS_STRING == Z_TYPE_P(op1) && PHP_TAINT_POSSIBLE(op1)) {
zval *result = &TAINT_T(TAINT_RESULT_VAR(opline)).tmp_var;
Z_STRVAL_P(result) = erealloc(Z_STRVAL_P(result), Z_STRLEN_P(result) + 1 + PHP_TAINT_MAGIC_LENGTH);
PHP_TAINT_MARK(result, PHP_TAINT_MAGIC_POSSIBLE);
}
}
switch (TAINT_OP1_TYPE(opline)) {
case IS_TMP_VAR:
zval_dtor(free_op1.var);
break;
case IS_VAR:
if (free_op1.var) {
zval_ptr_dtor(&free_op1.var);
}
break;
}
execute_data->opline++;
return ZEND_USER_OPCODE_CONTINUE;
} /* }}} */
#else
static zval * php_taint_get_zval_ptr_var(zend_uint var, const temp_variable *Ts, taint_free_op *should_free TSRMLS_DC) /* {{{ */ {
zval *ptr = TAINT_TS(var).var.ptr;
TAINT_PZVAL_UNLOCK(ptr, should_free);
return ptr;
} /* }}} */
static zval * php_taint_get_zval_ptr_cv(zend_uint var, int type TSRMLS_DC) /* {{{ */ {
zval ***ptr = &TAINT_CV_OF(var);
if (UNEXPECTED(*ptr == NULL)) {
zend_compiled_variable *cv = &TAINT_CV_DEF_OF(var);
if (!EG(active_symbol_table) || zend_hash_quick_find(EG(active_symbol_table), cv->name, cv->name_len + 1, cv->hash_value, (void **)ptr) == FAILURE) {
zend_error(E_NOTICE, "Undefined variable: %s", cv->name);
return &EG(uninitialized_zval);
}
}
return **ptr;
} /* }}} */
static zval * php_taint_get_zval_ptr_tmp(zend_uint var, const temp_variable *Ts, taint_free_op *should_free TSRMLS_DC) /* {{{ */ {
return should_free->var = &TAINT_TS(var).tmp_var;
} /* }}} */
static zval ** php_taint_get_zval_ptr_ptr_var(zend_uint var, const temp_variable *Ts, taint_free_op *should_free TSRMLS_DC)/* {{{ */ {
zval** ptr_ptr = TAINT_TS(var).var.ptr_ptr;
if (EXPECTED(ptr_ptr != NULL)) {
TAINT_PZVAL_UNLOCK(*ptr_ptr, should_free);
} else {
/* string offset */
TAINT_PZVAL_UNLOCK(TAINT_TS(var).str_offset.str, should_free);
}
return ptr_ptr;
} /* }}} */
static zval ** php_taint_get_zval_ptr_ptr_cv(zend_uint var, int type TSRMLS_DC) /* {{{ */ {
zval ***ptr = &TAINT_CV_OF(var);
if (UNEXPECTED(*ptr == NULL)) {
zend_compiled_variable *cv = &TAINT_CV_DEF_OF(var);
if (!EG(active_symbol_table)
|| zend_hash_quick_find(EG(active_symbol_table), cv->name, cv->name_len+1, cv->hash_value, (void **) ptr )==FAILURE) {
switch (type) {
case BP_VAR_R:
case BP_VAR_UNSET:
zend_error(E_NOTICE, "Undefined variable: %s", cv->name);
/* break missing intentionally */
case BP_VAR_IS:
return &EG(uninitialized_zval_ptr);
break;
case BP_VAR_RW:
zend_error(E_NOTICE, "Undefined variable: %s", cv->name);
/* break missing intentionally */
case BP_VAR_W:
Z_ADDREF(EG(uninitialized_zval));
if (!EG(active_symbol_table)) {
*ptr = (zval**)EG(current_execute_data)->CVs + (EG(active_op_array)->last_var + var);
**ptr = &EG(uninitialized_zval);
} else {
zend_hash_quick_update(EG(active_symbol_table), cv->name, cv->name_len+1, cv->hash_value, &EG(uninitialized_zval_ptr), sizeof(zval *), (void **)ptr);
}
break;
}
}
}
return *ptr;
} /* }}} */
static zval ** php_taint_get_zval_ptr_ptr(int op_type, const znode_op *node, const temp_variable *Ts, taint_free_op *should_free, int type TSRMLS_DC) /* {{{ */ {
should_free->type = op_type;
if (op_type == IS_CV) {
should_free->var = 0;
return php_taint_get_zval_ptr_ptr_cv(node->var, type TSRMLS_CC);
} else if (op_type == IS_VAR) {
return php_taint_get_zval_ptr_ptr_var(node->var, Ts, should_free TSRMLS_CC);
} else {
should_free->var = 0;
return NULL;
}
} /* }}} */
static zval *php_taint_get_zval_ptr(int op_type, const znode_op *node, const temp_variable *Ts, taint_free_op *should_free, int type TSRMLS_DC) /* {{{ */ {
/* should_free->is_var = 0; */
switch (op_type) {
case IS_CONST:
should_free->var = 0;
return node->zv;
break;
case IS_TMP_VAR:
should_free->var = TAINT_TMP_FREE(&TAINT_TS(node->var).tmp_var);
return &TAINT_TS(node->var).tmp_var;
break;
case IS_VAR:
return php_taint_get_zval_ptr_var(node->var, Ts, should_free TSRMLS_CC);
break;
case IS_UNUSED:
should_free->var = 0;
return NULL;
break;
case IS_CV:
should_free->var = 0;
return php_taint_get_zval_ptr_cv(node->var, type TSRMLS_CC);
break;
EMPTY_SWITCH_DEFAULT_CASE()
}
return NULL;
} /* }}} */
#endif
static void php_taint_error(const char *docref TSRMLS_DC, const char *format, ...) /* {{{ */ {
va_list args;
va_start(args, format);
php_verror(docref, "", TAINT_G(error_level), format, args TSRMLS_CC);
va_end(args);
} /* }}} */
static int php_taint_echo_handler(ZEND_OPCODE_HANDLER_ARGS) /* {{{ */ {
zend_op *opline = execute_data->opline;
zval *op1 = NULL;
taint_free_op free_op1 = {0};
switch(TAINT_OP1_TYPE(opline)) {
case IS_TMP_VAR:
op1 = php_taint_get_zval_ptr_tmp(TAINT_OP1_NODE_PTR(opline), execute_data->Ts, &free_op1 TSRMLS_CC);
break;
case IS_VAR:
op1 = TAINT_T(TAINT_OP1_VAR(opline)).var.ptr;
break;
case IS_CV: {
zval **t = TAINT_CV_OF(TAINT_OP1_VAR(opline));
if (t && *t) {
op1 = *t;
} else if (EG(active_symbol_table)) {
zend_compiled_variable *cv = &TAINT_CV_DEF_OF(TAINT_OP1_VAR(opline));
if (zend_hash_quick_find(EG(active_symbol_table), cv->name, cv->name_len + 1, cv->hash_value, (void **)&t) == SUCCESS) {
op1 = *t;
}
}
}
break;
}
if (op1 && IS_STRING == Z_TYPE_P(op1) && PHP_TAINT_POSSIBLE(op1)) {
if (ZEND_ECHO == opline->opcode) {
php_taint_error("function.echo" TSRMLS_CC, "Attempt to echo a string that might be tainted");
} else {
php_taint_error("function.print" TSRMLS_CC, "Attempt to print a string that might be tainted");
}
}
return ZEND_USER_OPCODE_DISPATCH;
} /* }}} */
static int php_taint_include_or_eval_handler(ZEND_OPCODE_HANDLER_ARGS) /* {{{ */ {
zend_op *opline = execute_data->opline;
zval *op1 = NULL;
taint_free_op free_op1 = {0};
switch(TAINT_OP1_TYPE(opline)) {
case IS_TMP_VAR:
op1 = php_taint_get_zval_ptr_tmp(TAINT_OP1_NODE_PTR(opline), execute_data->Ts, &free_op1 TSRMLS_CC);
break;
case IS_VAR:
op1 = TAINT_T(TAINT_OP1_VAR(opline)).var.ptr;
break;
case IS_CV: {
zval **t = TAINT_CV_OF(TAINT_OP1_VAR(opline));
if (t && *t) {
op1 = *t;
} else if (EG(active_symbol_table)) {
zend_compiled_variable *cv = &TAINT_CV_DEF_OF(TAINT_OP1_VAR(opline));
if (zend_hash_quick_find(EG(active_symbol_table), cv->name, cv->name_len + 1, cv->hash_value, (void **)&t) == SUCCESS) {
op1 = *t;
}
}
}
break;
case IS_CONST:
op1 = TAINT_OP1_CONSTANT_PTR(opline);
break;
}
if ((op1 && IS_STRING == Z_TYPE_P(op1) && PHP_TAINT_POSSIBLE(op1)))
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
switch (Z_LVAL(opline->op2.u.constant)) {
#else
switch (opline->extended_value) {
#endif
case ZEND_INCLUDE_ONCE:
php_taint_error("function.include_once" TSRMLS_CC, "File path contains data that might be tainted");
break;
case ZEND_REQUIRE_ONCE:
php_taint_error("function.require_once" TSRMLS_CC, "File path contains data that might be tainted");
break;
case ZEND_INCLUDE:
php_taint_error("function.include" TSRMLS_CC, "File path contains data that might be tainted");
break;
case ZEND_REQUIRE:
php_taint_error("function.require" TSRMLS_CC, "File path contains data that might be tainted");
break;
case ZEND_EVAL:
php_taint_error("function.eval" TSRMLS_CC, "Eval code contains data that might be tainted");
break;
}
return ZEND_USER_OPCODE_DISPATCH;
} /* }}} */
static int php_taint_concat_handler(ZEND_OPCODE_HANDLER_ARGS) /* {{{ */ {
zend_op *opline = execute_data->opline;
zval *op1 = NULL, *op2 = NULL, *result;
taint_free_op free_op1 = {0}, free_op2 = {0};
uint tainted = 0;
result = &TAINT_T(TAINT_RESULT_VAR(opline)).tmp_var;
switch(TAINT_OP1_TYPE(opline)) {
case IS_TMP_VAR:
op1 = php_taint_get_zval_ptr_tmp(TAINT_OP1_NODE_PTR(opline), execute_data->Ts, &free_op1 TSRMLS_CC);
break;
case IS_VAR:
op1 = php_taint_get_zval_ptr_var(TAINT_OP1_NODE_PTR(opline), execute_data->Ts, &free_op1 TSRMLS_CC);
break;
case IS_CV:
op1 = php_taint_get_zval_ptr_cv(TAINT_OP1_NODE_PTR(opline), TAINT_GET_ZVAL_PTR_CV_2ND_ARG(BP_VAR_R) TSRMLS_CC);
break;
case IS_CONST:
op1 = TAINT_OP1_CONSTANT_PTR(opline);
break;
}
switch(TAINT_OP2_TYPE(opline)) {
case IS_TMP_VAR:
op2 = php_taint_get_zval_ptr_tmp(TAINT_OP2_NODE_PTR(opline), execute_data->Ts, &free_op2 TSRMLS_CC);
break;
case IS_VAR:
op2 = php_taint_get_zval_ptr_var(TAINT_OP2_NODE_PTR(opline), execute_data->Ts, &free_op2 TSRMLS_CC);
break;
case IS_CV:
op2 = php_taint_get_zval_ptr_cv(TAINT_OP2_NODE_PTR(opline), TAINT_GET_ZVAL_PTR_CV_2ND_ARG(BP_VAR_R) TSRMLS_CC);
break;
case IS_CONST:
op2 = TAINT_OP2_CONSTANT_PTR(opline);
break;
}
if ((op1 && IS_STRING == Z_TYPE_P(op1) && PHP_TAINT_POSSIBLE(op1))
|| (op2 && IS_STRING == Z_TYPE_P(op2) && PHP_TAINT_POSSIBLE(op2))) {
tainted = 1;
}
concat_function(result, op1, op2 TSRMLS_CC);
if (tainted && IS_STRING == Z_TYPE_P(result)) {
Z_STRVAL_P(result) = erealloc(Z_STRVAL_P(result), Z_STRLEN_P(result) + 1 + PHP_TAINT_MAGIC_LENGTH);
PHP_TAINT_MARK(result, PHP_TAINT_MAGIC_POSSIBLE);
}
switch(TAINT_OP1_TYPE(opline)) {
case IS_TMP_VAR:
zval_dtor(free_op1.var);
break;
case IS_VAR:
if (free_op1.var) {
zval_ptr_dtor(&free_op1.var);
}
break;
}
switch(TAINT_OP2_TYPE(opline)) {
case IS_TMP_VAR:
zval_dtor(free_op2.var);
break;
case IS_VAR:
if (free_op2.var) {
zval_ptr_dtor(&free_op2.var);
}
break;
}
execute_data->opline++;
return ZEND_USER_OPCODE_CONTINUE;
} /* }}} */
static zval **php_taint_fetch_dimension_address_inner(HashTable *ht, zval *dim, int dim_type, int type TSRMLS_DC) /* {{{ */ {
zval **retval;
char *offset_key;
int offset_key_length;
ulong hval;
switch (dim->type) {
case IS_NULL:
offset_key = "";
offset_key_length = 0;
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION > 3)
hval = zend_inline_hash_func("", 1);
#endif
goto fetch_string_dim;
case IS_STRING:
offset_key = dim->value.str.val;
offset_key_length = dim->value.str.len;
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION > 3)
if (dim_type == IS_CONST) {
hval = Z_HASH_P(dim);
} else {
ZEND_HANDLE_NUMERIC_EX(offset_key, offset_key_length+1, hval, goto num_index);
if (IS_INTERNED(offset_key)) {
hval = INTERNED_HASH(offset_key);
} else {
hval = zend_hash_func(offset_key, offset_key_length+1);
}
}
#endif
fetch_string_dim:
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
if (zend_symtable_find(ht, offset_key, offset_key_length+1, (void **) &retval) == FAILURE) {
#else
if (zend_hash_quick_find(ht, offset_key, offset_key_length+1, hval, (void **) &retval) == FAILURE) {
#endif
switch (type) {
case BP_VAR_R:
zend_error(E_NOTICE, "Undefined index: %s", offset_key);
/* break missing intentionally */
case BP_VAR_UNSET:
case BP_VAR_IS:
retval = &EG(uninitialized_zval_ptr);
break;
case BP_VAR_RW:
zend_error(E_NOTICE,"Undefined index: %s", offset_key);
/* break missing intentionally */
case BP_VAR_W: {
zval *new_zval = &EG(uninitialized_zval);
Z_ADDREF_P(new_zval);
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
zend_symtable_update(ht, offset_key, offset_key_length+1, &new_zval, sizeof(zval *), (void **) &retval);
#else
zend_hash_quick_update(ht, offset_key, offset_key_length+1, hval, &new_zval, sizeof(zval *), (void **) &retval);
#endif
}
break;
}
}
#if 0
}
#endif
break;
case IS_DOUBLE:
hval = zend_dval_to_lval(Z_DVAL_P(dim));
goto num_index;
case IS_RESOURCE:
zend_error(E_STRICT, "Resource ID#%ld used as offset, casting to integer (%ld)", Z_LVAL_P(dim), Z_LVAL_P(dim));
/* Fall Through */
case IS_BOOL:
case IS_LONG:
hval = Z_LVAL_P(dim);
num_index:
if (zend_hash_index_find(ht, hval, (void **) &retval) == FAILURE) {
switch (type) {
case BP_VAR_R:
zend_error(E_NOTICE,"Undefined offset: %ld", hval);
/* break missing intentionally */
case BP_VAR_UNSET:
case BP_VAR_IS:
retval = &EG(uninitialized_zval_ptr);
break;
case BP_VAR_RW:
zend_error(E_NOTICE,"Undefined offset: %ld", hval);
/* break missing intentionally */
case BP_VAR_W: {
zval *new_zval = &EG(uninitialized_zval);
Z_ADDREF_P(new_zval);
zend_hash_index_update(ht, hval, &new_zval, sizeof(zval *), (void **) &retval);
}
break;
}
}
break;
default:
zend_error(E_WARNING, "Illegal offset type");
return (type == BP_VAR_W || type == BP_VAR_RW) ?
&EG(error_zval_ptr) : &EG(uninitialized_zval_ptr);
}
return retval;
} /* }}} */
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
static void php_taint_fetch_dimension_address(temp_variable *result, zval **container_ptr, zval *dim, int dim_is_tmp_var, int type TSRMLS_DC)
#else
static void php_taint_fetch_dimension_address(temp_variable *result, zval **container_ptr, zval *dim, int dim_type, int type TSRMLS_DC)
#endif
{
zval *container = *container_ptr;
zval **retval;
switch (Z_TYPE_P(container)) {
case IS_ARRAY:
if (type != BP_VAR_UNSET && Z_REFCOUNT_P(container)>1 && !Z_ISREF_P(container)) {
SEPARATE_ZVAL(container_ptr);
container = *container_ptr;
}
fetch_from_array:
if (dim == NULL) {
zval *new_zval = &EG(uninitialized_zval);
Z_ADDREF_P(new_zval);
if (zend_hash_next_index_insert(Z_ARRVAL_P(container), &new_zval, sizeof(zval *), (void **) &retval) == FAILURE) {
zend_error(E_WARNING, "Cannot add element to the array as the next element is already occupied");
retval = &EG(error_zval_ptr);
Z_DELREF_P(new_zval);
}
} else {
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
retval = php_taint_fetch_dimension_address_inner(Z_ARRVAL_P(container), dim, 0, type TSRMLS_CC);
#else
retval = php_taint_fetch_dimension_address_inner(Z_ARRVAL_P(container), dim, dim_type, type TSRMLS_CC);
#endif
}
result->var.ptr_ptr = retval;
Z_ADDREF_P(*retval);
return;
break;
case IS_NULL:
if (container == &EG(error_zval)) {
result->var.ptr_ptr = &EG(error_zval_ptr);
Z_ADDREF_P(EG(error_zval_ptr));
} else if (type != BP_VAR_UNSET) {
convert_to_array:
if (!Z_ISREF_P(container)) {
SEPARATE_ZVAL(container_ptr);
container = *container_ptr;
}
zval_dtor(container);
array_init(container);
goto fetch_from_array;
} else {
/* for read-mode only */
result->var.ptr_ptr = &EG(uninitialized_zval_ptr);
Z_ADDREF_P(EG(uninitialized_zval_ptr));
}
return;
break;
case IS_STRING: {
zval tmp;
if (type != BP_VAR_UNSET && Z_STRLEN_P(container)==0) {
goto convert_to_array;
}
if (dim == NULL) {
zend_error(E_ERROR, "[] operator not supported for strings");
return;
}
if (Z_TYPE_P(dim) != IS_LONG) {
switch(Z_TYPE_P(dim)) {
/* case IS_LONG: */
case IS_STRING:
if (IS_LONG == is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), NULL, NULL, -1)) {
break;
}
if (type != BP_VAR_UNSET) {
zend_error(E_WARNING, "Illegal string offset '%s'", dim->value.str.val);
}
break;
case IS_DOUBLE:
case IS_NULL:
case IS_BOOL:
zend_error(E_NOTICE, "String offset cast occurred");
break;
default:
zend_error(E_WARNING, "Illegal offset type");
break;
}
tmp = *dim;
zval_copy_ctor(&tmp);
convert_to_long(&tmp);
dim = &tmp;
}
if (type != BP_VAR_UNSET) {
SEPARATE_ZVAL_IF_NOT_REF(container_ptr);
}
container = *container_ptr;
result->str_offset.str = container;
Z_ADDREF_P(container);
result->str_offset.offset = Z_LVAL_P(dim);
result->str_offset.ptr_ptr = NULL;
return;
}
break;
case IS_OBJECT:
if (!Z_OBJ_HT_P(container)->read_dimension) {
zend_error(E_ERROR, "Cannot use object as array");
return;
} else {
zval *overloaded_result;
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
if (dim_is_tmp_var) {
#else
if (dim_type == IS_TMP_VAR) {
#endif
zval *orig = dim;
MAKE_REAL_ZVAL_PTR(dim);
ZVAL_NULL(orig);
}
#if 0
}
#endif
overloaded_result = Z_OBJ_HT_P(container)->read_dimension(container, dim, type TSRMLS_CC);
if (overloaded_result) {
if (!Z_ISREF_P(overloaded_result)) {
if (Z_REFCOUNT_P(overloaded_result) > 0) {
zval *tmp = overloaded_result;
ALLOC_ZVAL(overloaded_result);
/* ZVAL_COPY_VALUE(overloaded_result, tmp); */
overloaded_result->value = tmp->value;
Z_TYPE_P(overloaded_result) = Z_TYPE_P(tmp);
zval_copy_ctor(overloaded_result);
Z_UNSET_ISREF_P(overloaded_result);
Z_SET_REFCOUNT_P(overloaded_result, 0);
}
if (Z_TYPE_P(overloaded_result) != IS_OBJECT) {
zend_class_entry *ce = Z_OBJCE_P(container);
zend_error(E_NOTICE, "Indirect modification of overloaded element of %s has no effect", ce->name);
}
}
retval = &overloaded_result;
} else {
retval = &EG(error_zval_ptr);
}
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
TAINT_AI_SET_PTR(result->var, *retval);
#else
TAINT_AI_SET_PTR(result, *retval);
#endif
Z_ADDREF_P(*retval);
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
if (dim_is_tmp_var) {
#else
if (dim_type == IS_TMP_VAR) {
#endif
zval_ptr_dtor(&dim);
}
#if 0
}
#endif
}
return;
break;
case IS_BOOL:
if (type != BP_VAR_UNSET && Z_LVAL_P(container)==0) {
goto convert_to_array;
}
/* break missing intentionally */
default:
if (type == BP_VAR_UNSET) {
zend_error(E_WARNING, "Cannot unset offset in a non-array variable");
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
TAINT_AI_SET_PTR(result->var, EG(uninitialized_zval_ptr));
#else
TAINT_AI_SET_PTR(result, &EG(uninitialized_zval));
#endif
Z_ADDREF_P(&EG(uninitialized_zval));
} else {
zend_error(E_WARNING, "Cannot use a scalar value as an array");
result->var.ptr_ptr = &EG(error_zval_ptr);
Z_ADDREF_P(EG(error_zval_ptr));
}
break;
}
#if 0
}
#endif
}
static int php_taint_binary_assign_op_obj_helper(int (*binary_op)(zval *result, zval *op1, zval *op2 TSRMLS_DC), ZEND_OPCODE_HANDLER_ARGS) /* {{{ */ {
zend_op *opline = execute_data->opline;
zend_op *op_data = opline+1;
taint_free_op free_op1 = {0}, free_op2 = {0}, free_op_data1 = {0};
zval **object_ptr = NULL, *object = NULL, *property = NULL;
int have_get_ptr = 0;
uint tainted = 0;
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
zval *value = php_taint_get_zval_ptr(&op_data->op1, execute_data->Ts, &free_op_data1, BP_VAR_R TSRMLS_CC);
#else
zval *value = php_taint_get_zval_ptr((opline+1)->op1_type, &(opline+1)->op1, execute_data->Ts, &free_op_data1, BP_VAR_R TSRMLS_CC);
#endif
zval **retval = &TAINT_T(TAINT_RESULT_VAR(opline)).var.ptr;
switch (TAINT_OP1_TYPE(opline)) {
case IS_VAR:
object_ptr = php_taint_get_zval_ptr_ptr_var(TAINT_OP1_NODE_PTR(opline), execute_data->Ts, &free_op1 TSRMLS_CC);
if (!object_ptr) {
zend_error(E_ERROR, "Cannot use string offset as an object");
return 0;
}
break;
case IS_CV:
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
object_ptr = php_taint_get_zval_ptr_ptr_cv(&opline->op1, execute_data->Ts, BP_VAR_W TSRMLS_CC);
#else
object_ptr = php_taint_get_zval_ptr_ptr_cv(opline->op1.var, BP_VAR_W TSRMLS_CC);
#endif
break;
case IS_UNUSED:
object_ptr = php_taint_get_obj_zval_ptr_ptr_unused(TSRMLS_C);