-
Notifications
You must be signed in to change notification settings - Fork 71
/
execute.c
1891 lines (1592 loc) · 52.7 KB
/
execute.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
/*
+----------------------------------------------------------------------+
| Suhosin Version 1 |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2007 The Hardened-PHP Project |
| Copyright (c) 2007-2015 SektionEins GmbH |
+----------------------------------------------------------------------+
| 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: Stefan Esser <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id: execute.c,v 1.1.1.1 2007-11-28 01:15:35 sesser Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <fcntl.h>
#include "php.h"
#include "php_ini.h"
#include "zend_hash.h"
#include "zend_extensions.h"
#include "ext/standard/info.h"
#include "ext/standard/php_rand.h"
#include "ext/standard/php_lcg.h"
#include "php_suhosin.h"
#include "zend_compile.h"
#include "zend_llist.h"
#include "SAPI.h"
#include "sha256.h"
#ifdef PHP_WIN32
# include "win32/fnmatch.h"
# include "win32/winutil.h"
# include "win32/time.h"
#else
# ifdef HAVE_FNMATCH
# include <fnmatch.h>
# endif
# include <sys/time.h>
#endif
#if PHP_VERSION_ID >= 50500
static void (*old_execute_ex)(zend_execute_data *execute_data TSRMLS_DC);
static void suhosin_execute_ex(zend_execute_data *execute_data TSRMLS_DC);
#endif
static void (*old_execute)(zend_op_array *op_array TSRMLS_DC);
static void suhosin_execute(zend_op_array *op_array TSRMLS_DC);
static void (*old_execute_ZO)(zend_op_array *op_array, long dummy TSRMLS_DC);
static void suhosin_execute_ZO(zend_op_array *op_array, long dummy TSRMLS_DC);
static void *(*zo_set_oe_ex)(void *ptr) = NULL;
/*STATIC zend_op_array* (*old_compile_file)(zend_file_handle* file_handle, int type TSRMLS_DC);
STATIC zend_op_array* suhosin_compile_file(zend_file_handle*, int TSRMLS_DC);*/
#if PHP_VERSION_ID >= 50500
static void suhosin_execute_internal(zend_execute_data *execute_data_ptr, zend_fcall_info *fci, int return_value_used TSRMLS_DC);
static void (*old_execute_internal)(zend_execute_data *execute_data_ptr, zend_fcall_info *fci, int return_value_used TSRMLS_DC);
#else
static void suhosin_execute_internal(zend_execute_data *execute_data_ptr, int return_value_used TSRMLS_DC);
static void (*old_execute_internal)(zend_execute_data *execute_data_ptr, int return_value_used TSRMLS_DC);
#endif
extern zend_extension suhosin_zend_extension_entry;
/* {{{ suhosin_strcasestr */
static char *suhosin_strcasestr(char *haystack, char *needle)
{
unsigned char *t, *h, *n;
h = (unsigned char *) haystack;
conts:
while (*h) {
n = (unsigned char *) needle;
if (toupper(*h++) == toupper(*n++)) {
for (t=h; *n; t++, n++) {
if (toupper(*t) != toupper(*n)) goto conts;
}
return ((char*)h-1);
}
}
return (NULL);
}
/* }}} */
#define SUHOSIN_CODE_TYPE_UNKNOWN 0
#define SUHOSIN_CODE_TYPE_COMMANDLINE 1
#define SUHOSIN_CODE_TYPE_EVAL 2
#define SUHOSIN_CODE_TYPE_REGEXP 3
#define SUHOSIN_CODE_TYPE_ASSERT 4
#define SUHOSIN_CODE_TYPE_CFUNC 5
#define SUHOSIN_CODE_TYPE_SUHOSIN 6
#define SUHOSIN_CODE_TYPE_UPLOADED 7
#define SUHOSIN_CODE_TYPE_0FILE 8
#define SUHOSIN_CODE_TYPE_BLACKURL 9
#define SUHOSIN_CODE_TYPE_BADURL 10
#define SUHOSIN_CODE_TYPE_GOODFILE 11
#define SUHOSIN_CODE_TYPE_BADFILE 12
#define SUHOSIN_CODE_TYPE_LONGNAME 13
#define SUHOSIN_CODE_TYPE_MANYDOTS 14
#define SUHOSIN_CODE_TYPE_WRITABLE 15
#define SUHOSIN_CODE_TYPE_MBREGEXP 16
static int suhosin_check_filename(char *s, int len TSRMLS_DC)
{
char fname[MAXPATHLEN+1];
char *t, *h, *h2, *index, *e;
int tlen, i, count=0;
uint indexlen;
ulong numindex;
zend_bool isOk;
/* check if filename is too long */
if (len > MAXPATHLEN) {
return SUHOSIN_CODE_TYPE_LONGNAME;
}
memcpy(fname, s, len);
fname[len] = 0;
s = (char *)&fname;
e = s + len;
/* check if ASCIIZ attack -> not working yet (and cannot work in PHP4 + ZO) */
if (len != strlen(s)) {
return SUHOSIN_CODE_TYPE_0FILE;
}
/* disallow uploaded files */
if (SG(rfc1867_uploaded_files)) {
if (zend_hash_exists(SG(rfc1867_uploaded_files), (char *) s, e-s+1)) {
return SUHOSIN_CODE_TYPE_UPLOADED;
}
}
/* count number of directory traversals */
for (i=0; i < len-3; i++) {
if (s[i] == '.' && s[i+1] == '.' && (s[i+2] == '/' || s[i+2] == '\\')) {
count++;
i+=2;
}
}
if (SUHOSIN_G(executor_include_max_traversal) && SUHOSIN_G(executor_include_max_traversal)<=count) {
return SUHOSIN_CODE_TYPE_MANYDOTS;
}
SDEBUG("xxx %p %p",SUHOSIN_G(include_whitelist),SUHOSIN_G(include_blacklist));
/* no black or whitelist then disallow all */
if (SUHOSIN_G(include_whitelist)==NULL && SUHOSIN_G(include_blacklist)==NULL) {
/* disallow all URLs */
if (strstr(s, "://") != NULL || suhosin_strcasestr(s, "data:") != NULL) {
return SUHOSIN_CODE_TYPE_BADURL;
}
} else
/* whitelist is stronger than blacklist */
if (SUHOSIN_G(include_whitelist)) {
do {
isOk = 0;
h = strstr(s, "://");
h2 = suhosin_strcasestr(s, "data:");
h2 = h2 == NULL ? NULL : h2 + 4;
t = h = (h == NULL) ? h2 : ( (h2 == NULL) ? h : ( (h < h2) ? h : h2 ) );
if (h == NULL) break;
while (t > s && (isalnum(t[-1]) || t[-1]=='_' || t[-1]=='.')) {
t--;
}
tlen = e-t;
zend_hash_internal_pointer_reset(SUHOSIN_G(include_whitelist));
do {
int r = zend_hash_get_current_key_ex(SUHOSIN_G(include_whitelist), &index, &indexlen, &numindex, 0, NULL);
if (r==HASH_KEY_NON_EXISTANT) {
break;
}
if (r==HASH_KEY_IS_STRING) {
if (h-t <= indexlen-1 && tlen>=indexlen-1) {
if (strncasecmp(t, index, indexlen-1)==0) {
isOk = 1;
break;
}
}
}
zend_hash_move_forward(SUHOSIN_G(include_whitelist));
} while (1);
/* not found in whitelist */
if (!isOk) {
return SUHOSIN_CODE_TYPE_BADURL;
}
s = h + 1;
} while (1);
} else {
do {
int tlen;
h = strstr(s, "://");
h2 = suhosin_strcasestr(s, "data:");
h2 = h2 == NULL ? NULL : h2 + 4;
t = h = (h == NULL) ? h2 : ( (h2 == NULL) ? h : ( (h < h2) ? h : h2 ) );
if (h == NULL) break;
while (t > s && (isalnum(t[-1]) || t[-1]=='_' || t[-1]=='.')) {
t--;
}
tlen = e-t;
zend_hash_internal_pointer_reset(SUHOSIN_G(include_blacklist));
do {
int r = zend_hash_get_current_key_ex(SUHOSIN_G(include_blacklist), &index, &indexlen, &numindex, 0, NULL);
if (r==HASH_KEY_NON_EXISTANT) {
break;
}
if (r==HASH_KEY_IS_STRING) {
if (h-t <= indexlen-1 && tlen>=indexlen-1) {
if (strncasecmp(t, index, indexlen-1)==0) {
return SUHOSIN_CODE_TYPE_BLACKURL;
}
}
}
zend_hash_move_forward(SUHOSIN_G(include_blacklist));
} while (1);
s = h + 1;
} while (1);
}
/* disallow writable files */
if (!SUHOSIN_G(executor_include_allow_writable_files)) {
/* protection against *REMOTE* attacks, potential
race condition of access() is irrelevant */
if (access(s, W_OK) == 0) {
return SUHOSIN_CODE_TYPE_WRITABLE;
}
}
return SUHOSIN_CODE_TYPE_GOODFILE;
}
static int (*old_zend_stream_open)(const char *filename, zend_file_handle *fh TSRMLS_DC);
static int suhosin_zend_stream_open(const char *filename, zend_file_handle *fh TSRMLS_DC)
{
zend_execute_data *exd;
exd=EG(current_execute_data);
if (EG(in_execution) && (exd!=NULL) && (exd->opline != NULL) && (exd->opline->opcode == ZEND_INCLUDE_OR_EVAL)) {
int filetype = suhosin_check_filename((char *)filename, strlen(filename) TSRMLS_CC);
switch (filetype) {
case SUHOSIN_CODE_TYPE_LONGNAME:
suhosin_log(S_INCLUDE, "Include filename ('%s') is too long", filename);
suhosin_bailout(TSRMLS_C);
break;
case SUHOSIN_CODE_TYPE_UPLOADED:
suhosin_log(S_INCLUDE, "Include filename is an uploaded file");
suhosin_bailout(TSRMLS_C);
break;
case SUHOSIN_CODE_TYPE_0FILE:
suhosin_log(S_INCLUDE, "Include filename contains an ASCIIZ character");
suhosin_bailout(TSRMLS_C);
break;
case SUHOSIN_CODE_TYPE_WRITABLE:
suhosin_log(S_INCLUDE, "Include filename ('%s') is writable by PHP process", filename);
suhosin_bailout(TSRMLS_C);
break;
case SUHOSIN_CODE_TYPE_BLACKURL:
suhosin_log(S_INCLUDE, "Include filename ('%s') is a URL that is forbidden by the blacklist", filename);
suhosin_bailout(TSRMLS_C);
break;
case SUHOSIN_CODE_TYPE_BADURL:
suhosin_log(S_INCLUDE, "Include filename ('%s') is a URL that is not allowed", filename);
suhosin_bailout(TSRMLS_C);
break;
case SUHOSIN_CODE_TYPE_MANYDOTS:
suhosin_log(S_INCLUDE, "Include filename ('%s') contains too many '../'", filename);
suhosin_bailout(TSRMLS_C);
break;
}
}
return old_zend_stream_open(filename, fh TSRMLS_CC);
}
static int suhosin_detect_codetype(zend_op_array *op_array TSRMLS_DC)
{
char *s;
int r;
s = (char *)op_array->filename;
/* eval, assert, create_function, preg_replace */
if (op_array->type == ZEND_EVAL_CODE) {
if (s == NULL) {
return SUHOSIN_CODE_TYPE_UNKNOWN;
}
if (strstr(s, "eval()'d code") != NULL) {
return SUHOSIN_CODE_TYPE_EVAL;
}
if (strstr(s, "regexp code") != NULL) {
return SUHOSIN_CODE_TYPE_REGEXP;
}
if (strstr(s, "mbregex replace") != NULL) {
return SUHOSIN_CODE_TYPE_MBREGEXP;
}
if (strstr(s, "assert code") != NULL) {
return SUHOSIN_CODE_TYPE_ASSERT;
}
if (strstr(s, "runtime-created function") != NULL) {
return SUHOSIN_CODE_TYPE_CFUNC;
}
if (strstr(s, "Command line code") != NULL) {
return SUHOSIN_CODE_TYPE_COMMANDLINE;
}
if (strstr(s, "Command line begin code") != NULL) {
return SUHOSIN_CODE_TYPE_COMMANDLINE;
}
if (strstr(s, "Command line run code") != NULL) {
return SUHOSIN_CODE_TYPE_COMMANDLINE;
}
if (strstr(s, "Command line end code") != NULL) {
return SUHOSIN_CODE_TYPE_COMMANDLINE;
}
if (strstr(s, "suhosin internal code") != NULL) {
return SUHOSIN_CODE_TYPE_SUHOSIN;
}
} else {
r = suhosin_check_filename(s, strlen(s) TSRMLS_CC);
return r;
}
return SUHOSIN_CODE_TYPE_UNKNOWN;
}
/* {{{ void suhosin_execute_ex(zend_op_array *op_array TSRMLS_DC)
* This function provides a hook for execution */
#if PHP_VERSION_ID >= 50500
static void suhosin_execute_ex(zend_execute_data *execute_data TSRMLS_DC)
{
zend_op_array *op_array = execute_data->op_array;
#else
static void suhosin_execute_ex(zend_op_array *op_array, int zo, long dummy TSRMLS_DC)
{
#endif
zend_op_array *new_op_array;
int op_array_type, len;
char *fn;
zval cs;
zend_uint orig_code_type;
unsigned long *suhosin_flags = NULL;
/* log variable dropping statistics */
if (SUHOSIN_G(abort_request)) {
SUHOSIN_G(abort_request) = 0; /* we only want this to happen the first time */
if (SUHOSIN_G(att_request_variables)-SUHOSIN_G(cur_request_variables) > 0) {
suhosin_log(S_VARS, "dropped %u request variables - (%u in GET, %u in POST, %u in COOKIE)",
SUHOSIN_G(att_request_variables)-SUHOSIN_G(cur_request_variables),
SUHOSIN_G(att_get_vars)-SUHOSIN_G(cur_get_vars),
SUHOSIN_G(att_post_vars)-SUHOSIN_G(cur_post_vars),
SUHOSIN_G(att_cookie_vars)-SUHOSIN_G(cur_cookie_vars));
}
if (!SUHOSIN_G(simulation) && SUHOSIN_G(filter_action)) {
char *action = SUHOSIN_G(filter_action);
long code = -1;
while (*action == ' ' || *action == '\t') action++;
if (*action >= '0' && *action <= '9') {
char *end = action;
while (*end && *end != ',' && *end != ';') end++;
code = zend_atoi(action, end-action);
action = end;
}
while (*action == ' ' || *action == '\t' || *action == ',' || *action == ';') action++;
if (*action) {
if (strncasecmp("http://", action, sizeof("http://")-1)==0
|| strncasecmp("https://", action, sizeof("https://")-1)==0) {
sapi_header_line ctr = {0};
if (code == -1) {
code = 302;
}
ctr.line_len = spprintf(&ctr.line, 0, "Location: %s", action);
ctr.response_code = code;
sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
efree(ctr.line);
} else {
zend_file_handle file_handle;
zend_op_array *new_op_array;
zval *result = NULL;
if (code == -1) {
code = 200;
}
if (zend_stream_open(action, &file_handle TSRMLS_CC) == SUCCESS) {
if (!file_handle.opened_path) {
file_handle.opened_path = estrndup(action, strlen(action));
}
new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE TSRMLS_CC);
zend_destroy_file_handle(&file_handle TSRMLS_CC);
if (new_op_array) {
EG(return_value_ptr_ptr) = &result;
EG(active_op_array) = new_op_array;
zend_execute(new_op_array TSRMLS_CC);
destroy_op_array(new_op_array TSRMLS_CC);
efree(new_op_array);
if (!EG(exception))
{
if (EG(return_value_ptr_ptr)) {
zval_ptr_dtor(EG(return_value_ptr_ptr));
EG(return_value_ptr_ptr) = NULL;
}
}
} else {
code = 500;
}
} else {
code = 500;
}
}
}
sapi_header_op(SAPI_HEADER_SET_STATUS, (void *)code TSRMLS_CC);
zend_bailout();
}
}
SDEBUG("%s %s", op_array->filename, op_array->function_name);
SUHOSIN_G(execution_depth)++;
if (SUHOSIN_G(max_execution_depth) && SUHOSIN_G(execution_depth) > SUHOSIN_G(max_execution_depth)) {
suhosin_log(S_EXECUTOR|S_GETCALLER, "maximum execution depth reached - script terminated");
suhosin_bailout(TSRMLS_C);
}
fn = (char *)op_array->filename;
len = strlen(fn);
orig_code_type = SUHOSIN_G(in_code_type);
if (op_array->type == ZEND_EVAL_CODE) {
SUHOSIN_G(in_code_type) = SUHOSIN_EVAL;
} else {
if (suhosin_zend_extension_entry.resource_number != -1) {
suhosin_flags = (unsigned long *) &op_array->reserved[suhosin_zend_extension_entry.resource_number];
SDEBUG("suhosin flags: %08lx", *suhosin_flags);
if (*suhosin_flags & SUHOSIN_FLAG_CREATED_BY_EVAL) {
SUHOSIN_G(in_code_type) = SUHOSIN_EVAL;
}
if (*suhosin_flags & SUHOSIN_FLAG_NOT_EVALED_CODE) {
goto not_evaled_code;
}
}
if (strstr(op_array->filename, "eval()'d code")) {
SUHOSIN_G(in_code_type) = SUHOSIN_EVAL;
} else {
if (suhosin_flags) {
*suhosin_flags |= SUHOSIN_FLAG_NOT_EVALED_CODE;
}
}
}
not_evaled_code:
SDEBUG("code type %u", SUHOSIN_G(in_code_type));
if (op_array->function_name) {
goto continue_execution;
}
/* if (SUHOSIN_G(deactivate)) {
goto continue_execution;
}
*/
op_array_type = suhosin_detect_codetype(op_array TSRMLS_CC);
switch (op_array_type) {
case SUHOSIN_CODE_TYPE_EVAL:
if (SUHOSIN_G(executor_disable_eval)) {
suhosin_log(S_EXECUTOR|S_GETCALLER, "use of eval is forbidden by configuration");
if (!SUHOSIN_G(simulation)) {
zend_error(E_ERROR, "SUHOSIN - Use of eval is forbidden by configuration");
}
}
break;
case SUHOSIN_CODE_TYPE_REGEXP:
if (SUHOSIN_G(executor_disable_emod)) {
suhosin_log(S_EXECUTOR|S_GETCALLER, "use of preg_replace() with /e modifier is forbidden by configuration");
if (!SUHOSIN_G(simulation)) {
zend_error(E_ERROR, "SUHOSIN - Use of preg_replace() with /e modifier is forbidden by configuration");
}
}
break;
case SUHOSIN_CODE_TYPE_MBREGEXP:
/* XXX TODO: Do we want to disallow this, too? */
break;
case SUHOSIN_CODE_TYPE_ASSERT:
break;
case SUHOSIN_CODE_TYPE_CFUNC:
break;
case SUHOSIN_CODE_TYPE_LONGNAME:
suhosin_log(S_INCLUDE|S_GETCALLER, "Include filename ('%s') is too long", op_array->filename);
suhosin_bailout(TSRMLS_C);
break;
case SUHOSIN_CODE_TYPE_MANYDOTS:
suhosin_log(S_INCLUDE|S_GETCALLER, "Include filename ('%s') contains too many '../'", op_array->filename);
suhosin_bailout(TSRMLS_C);
break;
case SUHOSIN_CODE_TYPE_UPLOADED:
suhosin_log(S_INCLUDE|S_GETCALLER, "Include filename is an uploaded file");
suhosin_bailout(TSRMLS_C);
break;
case SUHOSIN_CODE_TYPE_0FILE:
suhosin_log(S_INCLUDE|S_GETCALLER, "Include filename contains an ASCIIZ character");
suhosin_bailout(TSRMLS_C);
break;
case SUHOSIN_CODE_TYPE_WRITABLE:
suhosin_log(S_INCLUDE|S_GETCALLER, "Include filename ('%s') is writable by PHP process", op_array->filename);
suhosin_bailout(TSRMLS_C);
break;
case SUHOSIN_CODE_TYPE_BLACKURL:
suhosin_log(S_INCLUDE|S_GETCALLER, "Include filename ('%s') is a URL that is forbidden by the blacklist", op_array->filename);
suhosin_bailout(TSRMLS_C);
break;
case SUHOSIN_CODE_TYPE_BADURL:
suhosin_log(S_INCLUDE|S_GETCALLER, "Include filename ('%s') is a URL that is not allowed", op_array->filename);
suhosin_bailout(TSRMLS_C);
break;
case SUHOSIN_CODE_TYPE_BADFILE:
cs.type = IS_STRING;
#define DIE_WITH_MSG "die('disallowed_file'.chr(10).chr(10));"
cs.value.str.val = estrndup(DIE_WITH_MSG, sizeof(DIE_WITH_MSG)-1);
cs.value.str.len = sizeof(DIE_WITH_MSG)-1;
new_op_array = compile_string(&cs, "suhosin internal code" TSRMLS_CC);
if (new_op_array) {
op_array = new_op_array;
goto continue_execution;
}
suhosin_bailout(TSRMLS_C);
break;
case SUHOSIN_CODE_TYPE_COMMANDLINE:
case SUHOSIN_CODE_TYPE_SUHOSIN:
case SUHOSIN_CODE_TYPE_UNKNOWN:
case SUHOSIN_CODE_TYPE_GOODFILE:
goto continue_execution;
}
continue_execution:
#if PHP_VERSION_ID >= 50500
old_execute_ex (execute_data TSRMLS_CC);
#else
if (zo) {
old_execute_ZO (op_array, dummy TSRMLS_CC);
} else {
old_execute (op_array TSRMLS_CC);
}
#endif
/* nothing to do */
SUHOSIN_G(in_code_type) = orig_code_type;
SUHOSIN_G(execution_depth)--;
}
/* }}} */
#if PHP_VERSION_ID < 50500
/* {{{ void suhosin_execute(zend_op_array *op_array TSRMLS_DC)
* This function provides a hook for execution */
static void suhosin_execute(zend_op_array *op_array TSRMLS_DC)
{
suhosin_execute_ex(op_array, 0, 0 TSRMLS_CC);
}
/* {{{ void suhosin_execute(zend_op_array *op_array, long dummy TSRMLS_DC)
* This function provides a hook for execution */
static void suhosin_execute_ZO(zend_op_array *op_array, long dummy TSRMLS_DC)
{
suhosin_execute_ex(op_array, 1, dummy TSRMLS_CC);
}
/* }}} */
#endif
#if PHP_VERSION_ID >= 50500
#define IH_HANDLER_PARAMS_REST int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_value_used TSRMLS_DC
#define IH_HANDLER_PARAMS internal_function_handler *ih, IH_HANDLER_PARAMS_REST
#define IH_HANDLER_PARAM_PASSTHRU ih, ht, return_value, return_value_ptr, this_ptr, return_value_used TSRMLS_CC
#else
#define IH_HANDLER_PARAMS_REST zend_execute_data *execute_data_ptr, int return_value_used, int ht, zval *return_value TSRMLS_DC
#define IH_HANDLER_PARAMS internal_function_handler *ih, IH_HANDLER_PARAMS_REST
#define IH_HANDLER_PARAM_PASSTHRU ih, execute_data_ptr, return_value_used, ht, return_value TSRMLS_CC
#endif
HashTable ihandler_table;
typedef struct _internal_function_handler {
char *name;
int (*handler)(struct _internal_function_handler *ih, IH_HANDLER_PARAMS_REST);
void *arg1;
void *arg2;
void *arg3;
} internal_function_handler;
int ih_preg_replace(IH_HANDLER_PARAMS)
{
zval **regex,
**replace,
**subject,
**limit, **zcount;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ZZZ|ZZ", ®ex, &replace, &subject, &limit, &zcount) == FAILURE) {
return (1);
}
if (Z_TYPE_PP(regex) == IS_ARRAY) {
zval **regex_entry;
zend_hash_internal_pointer_reset(Z_ARRVAL_PP(regex));
/* For each entry in the regex array, get the entry */
while (zend_hash_get_current_data(Z_ARRVAL_PP(regex), (void **)®ex_entry) == SUCCESS) {
if (Z_TYPE_PP(regex_entry) == IS_STRING) {
if (strlen(Z_STRVAL_PP(regex_entry)) != Z_STRLEN_PP(regex_entry)) {
suhosin_log(S_EXECUTOR, "string termination attack on first preg_replace parameter detected");
if (!SUHOSIN_G(simulation)) {
RETVAL_FALSE;
return (1);
}
}
}
zend_hash_move_forward(Z_ARRVAL_PP(regex));
}
} else if (Z_TYPE_PP(regex) == IS_STRING) {
if (strlen(Z_STRVAL_PP(regex)) != Z_STRLEN_PP(regex)) {
suhosin_log(S_EXECUTOR, "string termination attack on first preg_replace parameter detected");
if (!SUHOSIN_G(simulation)) {
RETVAL_FALSE;
return (1);
}
}
}
return (0);
}
int ih_symlink(IH_HANDLER_PARAMS)
{
if (SUHOSIN_G(executor_allow_symlink)) {
return (0);
}
if (PG(open_basedir) && PG(open_basedir)[0]) {
suhosin_log(S_EXECUTOR, "symlink called during open_basedir");
if (!SUHOSIN_G(simulation)) {
RETVAL_FALSE;
return (1);
}
}
return (0);
}
int ih_mail(IH_HANDLER_PARAMS)
{
char *to=NULL, *message=NULL, *headers=NULL;
char *subject=NULL, *extra_cmd=NULL;
char *tmp;
int to_len, message_len, headers_len;
int subject_len, extra_cmd_len;
if (SUHOSIN_G(mailprotect) == 0) {
return (0);
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|ss",
&to, &to_len,
&subject, &subject_len,
&message, &message_len,
&headers, &headers_len,
&extra_cmd, &extra_cmd_len
) == FAILURE) {
RETVAL_FALSE;
return (1);
}
if (headers_len > 0 && headers &&
(strstr(headers, "\n\n") || strstr(headers, "\n\r\n") /* double newline */
|| *headers == '\n' || (headers[0] == '\r' && headers[1] == '\n') /* starts with newline */
)) {
suhosin_log(S_MAIL, "mail() - double newline in headers, possible injection, mail dropped");
if (!SUHOSIN_G(simulation)) {
RETVAL_FALSE;
return (1);
}
}
/* check for spam attempts with buggy webforms */
if (to_len > 0 && to) {
do {
if ((tmp = strchr(to, '\n')) == NULL)
tmp = strchr(to, '\r');
if (tmp == NULL) break;
to = tmp + 1;
if (!isspace(*to)) break;
} while (1);
if (tmp != NULL) {
suhosin_log(S_MAIL, "mail() - newline in To header, possible injection, mail dropped");
if (!SUHOSIN_G(simulation)) {
RETVAL_FALSE;
return (1);
}
}
}
if (subject_len > 0 && subject) {
do {
if ((tmp = strchr(subject, '\n')) == NULL)
tmp = strchr(subject, '\r');
if (tmp == NULL) break;
subject = tmp + 1;
if (!isspace(*subject)) break;
} while (1);
if (tmp != NULL) {
suhosin_log(S_MAIL, "mail() - newline in Subject header, possible injection, mail dropped");
if (!SUHOSIN_G(simulation)) {
RETVAL_FALSE;
return (1);
}
}
}
if (SUHOSIN_G(mailprotect) > 1) {
/* search for to, cc or bcc headers */
if (headers_len > 0 && headers != NULL) {
if (strncasecmp(headers, "to:", sizeof("to:") - 1) == 0 || suhosin_strcasestr(headers, "\nto:")) {
suhosin_log(S_MAIL, "mail() - To: headers aren't allowed in the headers parameter.");
if (!SUHOSIN_G(simulation)) {
RETVAL_FALSE;
return (1);
}
}
if (strncasecmp(headers, "cc:", sizeof("cc:") - 1) == 0 || suhosin_strcasestr(headers, "\ncc:")) {
suhosin_log(S_MAIL, "mail() - CC: headers aren't allowed in the headers parameter.");
if (!SUHOSIN_G(simulation)) {
RETVAL_FALSE;
return (1);
}
}
if (strncasecmp(headers, "bcc:", sizeof("bcc:") - 1) == 0 || suhosin_strcasestr(headers, "\nbcc:")) {
suhosin_log(S_MAIL, "mail() - BCC: headers aren't allowed in the headers parameter.");
if (!SUHOSIN_G(simulation)) {
RETVAL_FALSE;
return (1);
}
}
}
}
return (0);
}
#define SQLSTATE_SQL 0
#define SQLSTATE_IDENTIFIER 1
#define SQLSTATE_STRING 2
#define SQLSTATE_COMMENT 3
#define SQLSTATE_MLCOMMENT 4
int ih_querycheck(IH_HANDLER_PARAMS)
{
void **p = zend_vm_stack_top(TSRMLS_C) - 1;
unsigned long arg_count;
zval **arg;
char *query, *s, *e;
zval *backup;
int len;
char quote;
int state = SQLSTATE_SQL;
int cnt_union = 0, cnt_select = 0, cnt_comment = 0, cnt_opencomment = 0;
int mysql_extension = 0;
SDEBUG("function: %s", ih->name);
arg_count = (unsigned long) *p;
if (ht < (long) ih->arg1) {
return (0);
}
if ((long) ih->arg2) {
mysql_extension = 1;
}
arg = (zval **) p - (arg_count - (long) ih->arg1 + 1); /* count from 0 */
backup = *arg;
if (Z_TYPE_P(backup) != IS_STRING) {
return (0);
}
len = Z_STRLEN_P(backup);
query = Z_STRVAL_P(backup);
SDEBUG("SQL |%s|", query);
s = query;
e = s+len;
while (s < e) {
switch (state)
{
case SQLSTATE_SQL:
switch (s[0])
{
case '`':
state = SQLSTATE_IDENTIFIER;
quote = '`';
break;
case '\'':
case '"':
state = SQLSTATE_STRING;
quote = *s;
break;
case '/':
if (s[1]=='*') {
if (mysql_extension == 1 && s[2] == '!') {
s += 2;
break;
}
s++;
state = SQLSTATE_MLCOMMENT;
cnt_comment++;
}
break;
case '-':
if (s[1]=='-') {
s++;
state = SQLSTATE_COMMENT;
cnt_comment++;
}
break;
case '#':
state = SQLSTATE_COMMENT;
cnt_comment++;
break;
case 'u':
case 'U':
if (strncasecmp("union", s, 5)==0) {
s += 4;
cnt_union++;
}
break;
case 's':
case 'S':
if (strncasecmp("select", s, 6)==0) {
s += 5;
cnt_select++;
}
break;
}
break;
case SQLSTATE_STRING:
case SQLSTATE_IDENTIFIER:
if (s[0] == quote) {
if (s[1] == quote) {
s++;
} else {
state = SQLSTATE_SQL;
}
}
if (s[0] == '\\') {
s++;
}
break;
case SQLSTATE_COMMENT:
while (s[0] && s[0] != '\n') {
s++;
}
state = SQLSTATE_SQL;
break;
case SQLSTATE_MLCOMMENT:
while (s[0] && (s[0] != '*' || s[1] != '/')) {
s++;
}
if (s[0]) {
state = SQLSTATE_SQL;
}
break;
}
s++;
}
if (state == SQLSTATE_MLCOMMENT) {
cnt_opencomment = 1;
}
if (cnt_opencomment && SUHOSIN_G(sql_opencomment)>0) {
suhosin_log(S_SQL, "Open comment in SQL query: '%*s'", len, query);
if (SUHOSIN_G(sql_opencomment)>1) {
suhosin_bailout(TSRMLS_C);
}
}
if (cnt_comment && SUHOSIN_G(sql_comment)>0) {
suhosin_log(S_SQL, "Comment in SQL query: '%*s'", len, query);
if (SUHOSIN_G(sql_comment)>1) {
suhosin_bailout(TSRMLS_C);
}
}
if (cnt_union && SUHOSIN_G(sql_union)>0) {
suhosin_log(S_SQL, "UNION in SQL query: '%*s'", len, query);
if (SUHOSIN_G(sql_union)>1) {
suhosin_bailout(TSRMLS_C);
}
}
if (cnt_select>1 && SUHOSIN_G(sql_mselect)>0) {
suhosin_log(S_SQL, "Multiple SELECT in SQL query: '%*s'", len, query);
if (SUHOSIN_G(sql_mselect)>1) {
suhosin_bailout(TSRMLS_C);
}
}
return (0);
}
int ih_fixusername(IH_HANDLER_PARAMS)
{
void **p = zend_vm_stack_top(TSRMLS_C) - 1;
unsigned long arg_count;
zval **arg;
char *prefix, *postfix, *user, *user_match, *cp;
zval *backup, *my_user;
int prefix_len, postfix_len, len;
SDEBUG("function (fixusername): %s", ih->name);
prefix = SUHOSIN_G(sql_user_prefix);
postfix = SUHOSIN_G(sql_user_postfix);
user_match = SUHOSIN_G(sql_user_match);