-
Notifications
You must be signed in to change notification settings - Fork 137
/
runkit_sandbox.c
1980 lines (1674 loc) · 58.2 KB
/
runkit_sandbox.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-2006 The PHP Group, (c) 2008-2015 Dmitry Zenovich |
+----------------------------------------------------------------------+
| This source file is subject to the new BSD 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.opensource.org/licenses/BSD-3-Clause |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| dzenovich@gmail.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Sara Golemon <pollita@php.net> |
| Props: Wez Furlong |
| Modified by Dmitry Zenovich <dzenovich@gmail.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "php_runkit.h"
#include "ext/standard/php_smart_str.h"
#ifdef PHP_RUNKIT_SANDBOX
#include "SAPI.h"
#include "php_main.h"
#include "php_runkit_sandbox.h"
#include "php_runkit_zval.h"
static zend_object_handlers php_runkit_sandbox_object_handlers;
static zend_class_entry *php_runkit_sandbox_class_entry;
#define PHP_RUNKIT_SANDBOX_BEGIN(objval) \
{ \
void *prior_context = tsrm_set_interpreter_context(objval->context); \
TSRMLS_FETCH();
#define PHP_RUNKIT_SANDBOX_ABORT(objval) \
{ \
tsrm_set_interpreter_context(prior_context); \
}
#define PHP_RUNKIT_SANDBOX_END(objval) \
PHP_RUNKIT_SANDBOX_ABORT(objval) \
if (objval->bailed_out_in_eval) { \
/* We're actually in bailout mode, but the child's bailout address had to resolve first */ \
zend_bailout(); \
} \
}
#define PHP_RUNKIT_SANDBOX_FETCHBOX(zval_p) (php_runkit_sandbox_object*)zend_objects_get_address(zval_p TSRMLS_CC)
int php_runkit_sandbox_array_deep_copy(RUNKIT_53_TSRMLS_ARG(zval **value), int num_args, va_list args, zend_hash_key *hash_key)
{
HashTable *target_hashtable = va_arg(args, HashTable*);
#if (RUNKIT_UNDER53)
TSRMLS_D = va_arg(args, void***);
#endif
zval *copyval;
MAKE_STD_ZVAL(copyval);
*copyval = **value;
PHP_SANDBOX_CROSS_SCOPE_ZVAL_COPY_CTOR(copyval);
if (hash_key->nKeyLength) {
#if PHP_MAJOR_VERSION >= 6
zend_u_hash_quick_update(target_hashtable, hash_key->type == HASH_KEY_IS_UNICODE ? IS_UNICODE : IS_STRING, hash_key->u.string, hash_key->nKeyLength, hash_key->h, ©val, sizeof(zval*), NULL);
#else
zend_hash_quick_update(target_hashtable, hash_key->arKey, hash_key->nKeyLength, hash_key->h, ©val, sizeof(zval*), NULL);
#endif
} else {
zend_hash_index_update(target_hashtable, hash_key->h, ©val, sizeof(zval*), NULL);
}
return ZEND_HASH_APPLY_KEEP;
}
/* ******************
* Runkit_Sandbox *
****************** */
static HashTable *php_runkit_sandbox_parse_multipath(const char *paths TSRMLS_DC) {
HashTable *ht;
int len = strlen(paths);
char *s, *colon, *pathcopy, tmppath[MAXPATHLEN] = {0};
if (!len) {
return NULL;
}
pathcopy = estrndup(paths, len);
ALLOC_HASHTABLE(ht);
zend_hash_init(ht, 4, NULL, NULL, 0);
for (s = pathcopy; (colon = strchr(s, ZEND_PATHS_SEPARATOR)); s = colon + 1) {
if (colon > s) {
*colon = 0;
VCWD_REALPATH(s, tmppath);
if (*tmppath) {
zend_hash_next_index_insert(ht, tmppath, strlen(tmppath) + 1, NULL);
} else {
/* s isn't real, leave it in the HashTable anyway
* to avoid making the old setting look like
* it had no values in it and thus allowing privilege elevation.
*/
zend_hash_next_index_insert(ht, s, (colon - s) + 1, NULL);
}
}
}
if (*s) {
VCWD_REALPATH(s, tmppath);
if (*tmppath) {
zend_hash_next_index_insert(ht, tmppath, strlen(tmppath) + 1, NULL);
} else {
/* See above */
zend_hash_next_index_insert(ht, s, strlen(s) + 1, NULL);
}
}
efree(pathcopy);
return ht;
}
static void php_runkit_sandbox_free_multipath(HashTable *ht TSRMLS_DC) {
if (ht) {
zend_hash_destroy(ht);
FREE_HASHTABLE(ht);
}
}
static char *php_runkit_sandbox_implode_stringht(HashTable *ht TSRMLS_DC) {
smart_str s = { 0 };
HashPosition pos;
char *str;
for(zend_hash_internal_pointer_reset_ex(ht, &pos);
SUCCESS == zend_hash_get_current_data_ex(ht, (void*)&str, &pos);
zend_hash_move_forward_ex(ht, &pos)) {
if (s.len) {
smart_str_appendc(&s, ZEND_PATHS_SEPARATOR);
}
smart_str_appends(&s, str);
}
smart_str_0(&s);
return s.c;
}
static char *php_runkit_sandbox_tighten_paths(HashTable *oldht, HashTable *newht TSRMLS_DC) {
HashPosition newpos;
char *newstr;
if (!oldht && !newht) {
return NULL;
}
if (!oldht) {
/* From nothing to something */
return php_runkit_sandbox_implode_stringht(newht TSRMLS_CC);
}
if (!newht) {
/* From something to nothing */
return NULL;
}
if (zend_hash_num_elements(oldht) == 0) {
/* Special edge case
* The parent's setting is ':' or similar.
* Despite looking similar to an empty setting,
* this actually has the opposite meaning.
* '' provides open access to the filesystem
* ':' provides access to none of it.
* THIS DISTINCTION MATTERS. :p
*/
return NULL;
}
for(zend_hash_internal_pointer_reset_ex(newht, &newpos);
SUCCESS == zend_hash_get_current_data_ex(newht, (void*)&newstr, &newpos);
zend_hash_move_forward_ex(newht, &newpos)) {
HashPosition oldpos;
char *oldstr;
int newstr_len = strlen(newstr);
for(zend_hash_internal_pointer_reset_ex(oldht, &oldpos);
SUCCESS == zend_hash_get_current_data_ex(oldht, (void*)&oldstr, &oldpos);
zend_hash_move_forward_ex(oldht, &oldpos)) {
int oldstr_len = strlen(oldstr);
if ((oldstr_len <= newstr_len) &&
!strncmp(oldstr, newstr, oldstr_len) &&
((oldstr_len == newstr_len) || (newstr[oldstr_len] == DEFAULT_SLASH))) {
goto newstr_ok;
}
}
/* Couldn't find an old path that we're constricting, so fail */
return NULL;
newstr_ok: ;
}
return php_runkit_sandbox_implode_stringht(newht TSRMLS_CC);
}
/* Special .ini options (normally PHP_INI_SYSTEM) which can be overridden within a sandbox in the direction of tighter security
*
* safe_mode = true
* safe_mode can only be turned on for a sandbox, not off. Doing so would tend to defeat safe_mode as applied to the calling script
* safe_mode_gid = false
* safe_mode_gid can only be turned off as it allows a partial bypass of safe_mode restrictions when on
* safe_mode_include_dir = /path/to/includables
* safe_mode_include_dir must be at or below the currently defined include_dir
* open_basedir = /path/to/basedir
* open_basedir must be at or below the currently defined basedir for the same reason that safe_mode can only be turned on
* allow_url_fopen = false
* allow_url_fopen may only be turned off for a sandbox, not on. Once again, don't castrate the existing restrictions
* allow_url_include = false
* allow_url_include may only be turned off for a sandbox, not on. Once again, don't castrate the existing restrictions
* disable_functions = coma_separated,list_of,additional_functions
* ADDITIONAL functions, on top of already disabled functions to disable
* disable_classes = coma_separated,list_of,additional_classes
* ADDITIONAL classes, on top of already disabled classes to disable
* runkit.superglobals = coma_separated,list_of,superglobals
* ADDITIONAL superglobals to define in the subinterpreter
* runkit.internal_override = false
* runkit.internal_override may be disabled (but not re-enabled) within sandboxes
*/
static inline void php_runkit_sandbox_ini_override(php_runkit_sandbox_object *objval, HashTable *options TSRMLS_DC)
{
zend_bool allow_url_fopen;
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || (PHP_MAJOR_VERSION < 5)
zend_bool safe_mode, safe_mode_gid;
HashTable *safe_mode_include_dirs = NULL;
#endif
#ifdef ZEND_ENGINE_2_2
zend_bool allow_url_include;
#endif
HashTable *open_basedirs = NULL;
zval **tmpzval;
/* Collect up parent values */
tsrm_set_interpreter_context(objval->parent_context);
{
/* Check current settings in parent context */
TSRMLS_FETCH_FROM_CTX(objval->parent_context);
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || (PHP_MAJOR_VERSION < 5)
safe_mode = PG(safe_mode);
safe_mode_gid = PG(safe_mode_gid);
if (PG(safe_mode_include_dir) && *PG(safe_mode_include_dir)) {
safe_mode_include_dirs = php_runkit_sandbox_parse_multipath(PG(safe_mode_include_dir) TSRMLS_CC);
}
#endif
if (PG(open_basedir) && *PG(open_basedir)) {
open_basedirs = php_runkit_sandbox_parse_multipath(PG(open_basedir) TSRMLS_CC);
}
allow_url_fopen = PG(allow_url_fopen);
#ifdef ZEND_ENGINE_2_2
allow_url_include = PG(allow_url_include);
#endif
}
tsrm_set_interpreter_context(objval->context);
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || (PHP_MAJOR_VERSION < 5)
/* safe_mode only goes on */
if (!safe_mode &&
zend_hash_find(options, "safe_mode", sizeof("safe_mode"), (void*)&tmpzval) == SUCCESS) {
zval copyval = **tmpzval;
zval_copy_ctor(©val);
convert_to_boolean(©val);
if (Z_BVAL(copyval)) {
zend_alter_ini_entry("safe_mode", sizeof("safe_mode"), "1", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
}
}
/* safe_mode_gid only goes off */
if (safe_mode_gid &&
zend_hash_find(options, "safe_mode_gid", sizeof("safe_mode_gid"), (void*)&tmpzval) == SUCCESS) {
zval copyval = **tmpzval;
zval_copy_ctor(©val);
convert_to_boolean(©val);
if (!Z_BVAL(copyval)) {
zend_alter_ini_entry("safe_mode_gid", sizeof("safe_mode_gid"), "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
}
}
/* safe_mode_include_dir can go deeper, or be set to blank (which means nowhere is includable) */
if (zend_hash_find(options, "safe_mode_include_dir", sizeof("safe_mode_include_dir"), (void*)&tmpzval) == SUCCESS &&
Z_TYPE_PP(tmpzval) == IS_STRING) {
if (Z_STRLEN_PP(tmpzval) == 0) {
/* simplest case -- clearing safe_mode_include_dir -- maximum restriction */
zend_alter_ini_entry("safe_mode_include_dir", sizeof("safe_mode_include_dir"), NULL, 0, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
} else if (!safe_mode_include_dirs && safe_mode) {
/* 2nd simplest case -- Full security already with safe_mode preexisting -- leave it alone */
} else if (!safe_mode_include_dirs) {
/* 3rd simplest case -- include_dir not yet set, but safe_mode not on yet, assume we're turning on */
zend_alter_ini_entry("safe_mode_include_dir", sizeof("safe_mode_include_dir"), Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
} else {
/* Otherwise... */
HashTable *new_include_dirs = php_runkit_sandbox_parse_multipath(Z_STRVAL_PP(tmpzval) TSRMLS_CC);
int new_include_dir_len = 0;
char *new_include_dir = php_runkit_sandbox_tighten_paths(safe_mode_include_dirs, new_include_dirs TSRMLS_CC);
if (new_include_dir) {
/* Tightening up of existing security level */
zend_alter_ini_entry("safe_mode_include_dir", sizeof("safe_mode_include_dir"), new_include_dir, new_include_dir_len, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
efree(new_include_dir);
}
php_runkit_sandbox_free_multipath(new_include_dirs TSRMLS_CC);
}
}
#endif
/* open_basedir goes deeper only */
if (zend_hash_find(options, "open_basedir", sizeof("open_basedir"), (void*)&tmpzval) == SUCCESS &&
Z_TYPE_PP(tmpzval) == IS_STRING) {
if (!open_basedirs) {
/* simplest case -- no open basedir existed yet */
zend_alter_ini_entry("open_basedir", sizeof("open_basedir"), Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
goto child_open_basedir_set;
} else {
HashTable *new_open_basedirs = php_runkit_sandbox_parse_multipath(Z_STRVAL_PP(tmpzval) TSRMLS_CC); char *new_open_basedir = php_runkit_sandbox_tighten_paths(open_basedirs, new_open_basedirs TSRMLS_CC);
php_runkit_sandbox_free_multipath(new_open_basedirs TSRMLS_CC);
if (new_open_basedir) {
/* Tightening up of existing security level */
zend_alter_ini_entry("open_basedir", sizeof("open_basedir"), new_open_basedir, strlen(new_open_basedir), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
efree(new_open_basedir);
goto child_open_basedir_set;
}
}
}
if (open_basedirs) {
/* Inherit parent's setting by default which may be PHP_INI_USER level setting */
char *parent_open_basedir = php_runkit_sandbox_implode_stringht(open_basedirs TSRMLS_CC);
zend_alter_ini_entry("open_basedir", sizeof("open_basedir"), parent_open_basedir, strlen(parent_open_basedir), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
efree(parent_open_basedir);
}
child_open_basedir_set:
/* allow_url_fopen goes off only */
if (allow_url_fopen &&
zend_hash_find(options, "allow_url_fopen", sizeof("allow_url_fopen"), (void*)&tmpzval) == SUCCESS) {
zval copyval = **tmpzval;
zval_copy_ctor(©val);
convert_to_boolean(©val);
if (!Z_BVAL(copyval)) {
zend_alter_ini_entry("allow_url_fopen", sizeof("allow_url_fopen"), "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
}
}
/* Can only disable additional functions */
if (zend_hash_find(options, "disable_functions", sizeof("disable_functions"), (void*)&tmpzval) == SUCCESS &&
Z_TYPE_PP(tmpzval) == IS_STRING) {
/* NOTE: disable_functions doesn't prevent $obj->function_name, it only blocks code inside $obj->eval() statements
* This could be brought into consistency, but I actually think it's okay to leave those functions available to calling script
*/
/* This buffer needs to be around when the error message occurs since the underlying implementation in Zend expects it to be */
int disable_functions_len = Z_STRLEN_PP(tmpzval);
char *p, *s;
objval->disable_functions = estrndup(Z_STRVAL_PP(tmpzval), disable_functions_len);
s = objval->disable_functions;
while ((p = strchr(s, ','))) {
*p = '\0';
zend_disable_function(s, p - s TSRMLS_CC);
s = p + 1;
}
zend_disable_function(s, strlen(s) TSRMLS_CC);
}
/* Can only disable additional classes */
if (zend_hash_find(options, "disable_classes", sizeof("disable_classes"), (void*)&tmpzval) == SUCCESS &&
Z_TYPE_PP(tmpzval) == IS_STRING) {
/* This buffer needs to be around when the error message occurs since the underlying implementation in Zend expects it to be */
int disable_classes_len = Z_STRLEN_PP(tmpzval);
char *p, *s;
objval->disable_classes = estrndup(Z_STRVAL_PP(tmpzval), disable_classes_len);
s = objval->disable_classes;
while ((p = strchr(s, ','))) {
*p = '\0';
zend_disable_class(s, p - s TSRMLS_CC);
s = p + 1;
}
zend_disable_class(s, strlen(s) TSRMLS_CC);
}
/* Additional superglobals to define */
if (zend_hash_find(options, "runkit.superglobal", sizeof("runkit.superglobal"), (void*)&tmpzval) == SUCCESS &&
Z_TYPE_PP(tmpzval) == IS_STRING) {
char *p, *s = Z_STRVAL_PP(tmpzval);
int len;
while ((p = strchr(s, ','))) {
if (p - s) {
*p = '\0';
zend_register_auto_global(s, p - s,
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION >= 6)
0,
#endif
NULL TSRMLS_CC);
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION >= 6)
zend_activate_auto_globals(TSRMLS_C);
#else
zend_auto_global_disable_jit(s, p - s TSRMLS_CC);
#endif
*p = ',';
}
s = p + 1;
}
len = strlen(s);
zend_register_auto_global(s, len,
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION >= 6)
0,
#endif
NULL TSRMLS_CC);
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || (PHP_MAJOR_VERSION < 5)
zend_auto_global_disable_jit(s, len TSRMLS_CC);
#endif
}
/* May only turn off */
if (zend_hash_find(options, "runkit.internal_override", sizeof("runkit.internal_override"), (void*)&tmpzval) == SUCCESS) {
zval copyval = **tmpzval;
zval_copy_ctor(©val);
convert_to_boolean(©val);
if (!Z_BVAL(copyval)) {
zend_alter_ini_entry("runkit.internal_override", sizeof("runkit.internal_override"), "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
}
}
#ifdef ZEND_ENGINE_2_2
/* May only turn off */
if (allow_url_include &&
(zend_hash_find(options, "allow_url_include", sizeof("allow_url_include"), (void*)&tmpzval) == SUCCESS) &&
!zend_is_true(*tmpzval)) {
zend_alter_ini_entry("allow_url_include", sizeof("allow_url_include"), "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
}
#endif
if (
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || (PHP_MAJOR_VERSION < 5)
safe_mode_include_dirs ||
#endif
open_basedirs) {
tsrm_set_interpreter_context(objval->parent_context);
{
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || (PHP_MAJOR_VERSION < 5)
php_runkit_sandbox_free_multipath(safe_mode_include_dirs TSRMLS_CC);
#endif
php_runkit_sandbox_free_multipath(open_basedirs TSRMLS_CC);
}
tsrm_set_interpreter_context(objval->context);
}
}
/* }}} */
/* {{{ proto void Runkit_Sandbox::__construct(array options)
* Options: see php_runkit_sandbox_ini_override()
*/
PHP_METHOD(Runkit_Sandbox,__construct)
{
php_runkit_sandbox_object *objval = PHP_RUNKIT_SANDBOX_FETCHBOX(this_ptr);
zval *options = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a", &options) == FAILURE) {
RETURN_NULL();
}
objval->context = tsrm_new_interpreter_context();
objval->disable_functions = NULL;
objval->disable_classes = NULL;
objval->output_handler = NULL;
PHP_RUNKIT_SANDBOX_BEGIN(objval)
objval->parent_context = prior_context;
zend_alter_ini_entry("implicit_flush", sizeof("implicit_flush") , "1", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
zend_alter_ini_entry("max_execution_time", sizeof("max_execution_time") , "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
if (options) {
/* Override a select subset of .ini options for increased restriction in the sandbox */
php_runkit_sandbox_ini_override(objval, Z_ARRVAL_P(options) TSRMLS_CC);
}
SG(headers_sent) = 1;
SG(request_info).no_headers = 1;
SG(options) = SAPI_OPTION_NO_CHDIR;
RUNKIT_G(current_sandbox) = objval; /* Needs to be set before RINIT */
php_request_startup(TSRMLS_C);
RUNKIT_G(current_sandbox) = objval; /* But gets reset during RINIT -- Bad design on my part */
PG(during_request_startup) = 0;
PHP_RUNKIT_SANDBOX_END(objval)
/* Prime the sandbox to be played in */
objval->active = 1;
RETURN_TRUE;
}
/* }}} */
/* {{{ proto Runkit_Sandbox::__call(mixed function_name, array args)
Call User Function */
PHP_METHOD(Runkit_Sandbox,__call)
{
zval *func_name, *args, *retval = NULL;
php_runkit_sandbox_object *objval;
int bailed_out = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "za", &func_name, &args) == FAILURE) {
RETURN_NULL();
}
objval = PHP_RUNKIT_SANDBOX_FETCHBOX(this_ptr);
if (!objval->active) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Current sandbox is no longer active");
RETURN_NULL();
}
PHP_RUNKIT_SANDBOX_BEGIN(objval)
{
char *name = NULL;
zend_first_try {
if (!RUNKIT_IS_CALLABLE(func_name, IS_CALLABLE_CHECK_NO_ACCESS, &name)) {
php_error_docref1(NULL TSRMLS_CC, name, E_WARNING, "Function not defined");
if (name) {
efree(name);
}
PHP_RUNKIT_SANDBOX_ABORT(objval)
RETURN_FALSE;
}
php_runkit_sandbox_call_int(func_name, &name, &retval, args, return_value, prior_context TSRMLS_CC);
} zend_catch {
bailed_out = 1;
objval->active = 0;
} zend_end_try();
}
PHP_RUNKIT_SANDBOX_END(objval)
if (bailed_out) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed calling sandbox function");
RETURN_FALSE;
}
PHP_SANDBOX_CROSS_SCOPE_ZVAL_COPY_CTOR(return_value);
if (retval) {
PHP_RUNKIT_SANDBOX_BEGIN(objval)
(void)(TSRMLS_C);
zval_ptr_dtor(&retval);
PHP_RUNKIT_SANDBOX_END(objval)
}
}
/* }}} */
/* {{{ php_runkit_sandbox_include_or_eval
*/
static void php_runkit_sandbox_include_or_eval(INTERNAL_FUNCTION_PARAMETERS, int type, int once)
{
php_runkit_sandbox_object *objval;
zval *zcode;
int bailed_out = 0;
zval *retval = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zcode) == FAILURE) {
RETURN_FALSE;
}
convert_to_string(zcode);
objval = PHP_RUNKIT_SANDBOX_FETCHBOX(this_ptr);
if (!objval->active) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Current sandbox is no longer active");
RETURN_NULL();
}
RETVAL_NULL();
PHP_RUNKIT_SANDBOX_BEGIN(objval)
zend_first_try {
zend_op_array *op_array = NULL;
int already_included = 0;
op_array = php_runkit_sandbox_include_or_eval_int(return_value, zcode, type, once, &already_included TSRMLS_CC);
if (op_array) {
EG(return_value_ptr_ptr) = &retval;
EG(active_op_array) = op_array;
zend_execute(op_array TSRMLS_CC);
if (retval) {
*return_value = *retval;
} else {
RETVAL_TRUE;
}
destroy_op_array(op_array TSRMLS_CC);
efree(op_array);
} else if ((type != ZEND_INCLUDE) && !already_included) {
/* include can fail to parse peacefully,
* require and eval should die on failure
*/
objval->active = 0;
bailed_out = 1;
}
} zend_catch {
/* It's impossible to know what caused the failure, just deactive the sandbox now */
objval->active = 0;
bailed_out = 1;
} zend_end_try();
PHP_RUNKIT_SANDBOX_END(objval)
if (bailed_out) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error executing sandbox code");
RETURN_FALSE;
}
PHP_SANDBOX_CROSS_SCOPE_ZVAL_COPY_CTOR(return_value);
/* Don't confuse the memory manager */
if (retval) {
PHP_RUNKIT_SANDBOX_BEGIN(objval)
(void)(TSRMLS_C);
zval_ptr_dtor(&retval);
PHP_RUNKIT_SANDBOX_END(objval)
}
}
/* }}} */
/* {{{ proto Runkit_Sandbox::eval(string phpcode)
Evaluate php code within the sandbox environment */
PHP_METHOD(Runkit_Sandbox,eval)
{
php_runkit_sandbox_include_or_eval(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_EVAL, 0);
}
/* }}} */
/* {{{ proto Runkit_Sandbox::include(string filename)
Evaluate php code from a file within the sandbox environment */
PHP_METHOD(Runkit_Sandbox,include)
{
php_runkit_sandbox_include_or_eval(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_INCLUDE, 0);
}
/* }}} */
/* {{{ proto Runkit_Sandbox::include_once(string filename)
Evaluate php code from a file within the sandbox environment */
PHP_METHOD(Runkit_Sandbox,include_once)
{
php_runkit_sandbox_include_or_eval(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_INCLUDE, 1);
}
/* }}} */
/* {{{ proto Runkit_Sandbox::require(string filename)
Evaluate php code from a file within the sandbox environment */
PHP_METHOD(Runkit_Sandbox,require)
{
php_runkit_sandbox_include_or_eval(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_REQUIRE, 0);
}
/* }}} */
/* {{{ proto Runkit_Sandbox::require_once(string filename)
Evaluate php code from a file within the sandbox environment */
PHP_METHOD(Runkit_Sandbox,require_once)
{
php_runkit_sandbox_include_or_eval(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_REQUIRE, 1);
}
/* }}} */
/* {{{ proto null Runkit_Sandbox::echo(mixed var[, mixed var[, ... ]])
Echo through the sandbox
The only thing which distinguished this from a non-sandboxed echo
is that content gets processed through the sandbox's output_handler (if present) */
PHP_METHOD(Runkit_Sandbox,echo)
{
php_runkit_sandbox_object *objval;
zval ***argv;
int i, argc = ZEND_NUM_ARGS();
argv = emalloc(sizeof(zval**) * argc);
if (zend_get_parameters_array_ex(argc, argv) == FAILURE) {
efree(argv);
/* Big problems... */
RETURN_NULL();
}
for(i = 0; i < argc; i++) {
/* Prepare for output */
convert_to_string(*(argv[i]));
}
objval = PHP_RUNKIT_SANDBOX_FETCHBOX(this_ptr);
if (!objval->active) {
efree(argv);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Current sandbox is no longer active");
RETURN_NULL();
}
PHP_RUNKIT_SANDBOX_BEGIN(objval)
zend_try {
for(i = 0; i < argc; i++) {
PHPWRITE(Z_STRVAL_PP(argv[i]),Z_STRLEN_PP(argv[i]));
}
} zend_catch {
objval->active = 0;
} zend_end_try();
PHP_RUNKIT_SANDBOX_END(objval)
efree(argv);
RETURN_NULL();
}
/* }}} */
/* {{{ proto bool Runkit_Sandbox::print(mixed var)
Echo through the sandbox
The only thing which distinguished this from a non-sandboxed echo
is that content gets processed through the sandbox's output_handler (if present) */
PHP_METHOD(Runkit_Sandbox,print)
{
php_runkit_sandbox_object *objval;
char *str;
int len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len) == FAILURE) {
RETURN_FALSE;
}
objval = PHP_RUNKIT_SANDBOX_FETCHBOX(this_ptr);
if (!objval->active) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Current sandbox is no longer active");
RETURN_NULL();
}
PHP_RUNKIT_SANDBOX_BEGIN(objval)
zend_try {
PHPWRITE(str,len);
} zend_catch {
objval->active = 0;
} zend_end_try();
PHP_RUNKIT_SANDBOX_END(objval)
RETURN_BOOL(len > 1 || (len == 1 && str[0] != '0'));
}
/* }}} */
/* {{{ proto void Runkit_Sandbox::die(mixed message)
MALIAS(exit)
Terminate a sandbox instance */
PHP_METHOD(Runkit_Sandbox,die)
{
php_runkit_sandbox_object *objval;
zval *message = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &message) == FAILURE) {
RETURN_FALSE;
}
RETVAL_NULL();
if (message && Z_TYPE_P(message) != IS_LONG) {
convert_to_string(message);
}
objval = PHP_RUNKIT_SANDBOX_FETCHBOX(this_ptr);
if (!objval->active) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Current sandbox is no longer active");
return;
}
PHP_RUNKIT_SANDBOX_BEGIN(objval)
zend_try {
if (message) {
if (Z_TYPE_P(message) == IS_LONG) {
EG(exit_status) = Z_LVAL_P(message);
} else {
PHPWRITE(Z_STRVAL_P(message), Z_STRLEN_P(message));
}
}
zend_bailout();
} zend_catch {
/* goes without saying doesn't it? */
objval->active = 0;
} zend_end_try();
PHP_RUNKIT_SANDBOX_END(objval)
}
/* }}} */
/* *********************
* Property Handlers *
********************* */
/* {{{ php_runkit_sandbox_read_property
read_property handler */
static zval *php_runkit_sandbox_read_property(zval *object, zval *member, int type
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION > 6)
, const zend_literal *key
#endif
TSRMLS_DC)
{
php_runkit_sandbox_object *objval = PHP_RUNKIT_SANDBOX_FETCHBOX(object);
zval tmp_member;
zval retval;
int prop_found = 0;
if (!objval->active) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Current sandbox is no longer active");
return EG(uninitialized_zval_ptr);
}
PHP_RUNKIT_ZVAL_CONVERT_TO_STRING_IF_NEEDED(member, tmp_member);
PHP_RUNKIT_SANDBOX_BEGIN(objval)
zend_try {
zval **value;
if (zend_hash_find(&EG(symbol_table), Z_STRVAL_P(member), Z_STRLEN_P(member) + 1, (void*)&value) == SUCCESS) {
retval = **value;
prop_found = 1;
}
} zend_catch {
/* Almost certainly impossible... */
objval->active = 0;
} zend_end_try();
PHP_RUNKIT_SANDBOX_END(objval)
if (member == &tmp_member) {
zval_dtor(member);
}
return php_runkit_sandbox_return_property_value(prop_found, &retval TSRMLS_CC);
}
/* }}} */
/* {{{ php_runkit_sandbox_write_property
write_property handler */
static void php_runkit_sandbox_write_property(zval *object, zval *member, zval *value
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION > 6)
, const zend_literal *key
#endif
TSRMLS_DC)
{
php_runkit_sandbox_object *objval = PHP_RUNKIT_SANDBOX_FETCHBOX(object);
zval tmp_member;
if (!objval->active) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Current sandbox is no longer active");
return;
}
PHP_RUNKIT_ZVAL_CONVERT_TO_STRING_IF_NEEDED(member, tmp_member);
PHP_RUNKIT_SANDBOX_BEGIN(objval)
zend_try {
zval *copyval;
MAKE_STD_ZVAL(copyval);
*copyval = *value;
PHP_SANDBOX_CROSS_SCOPE_ZVAL_COPY_CTOR(copyval);
ZEND_SET_SYMBOL(&EG(symbol_table), Z_STRVAL_P(member), copyval);
} zend_catch {
/* An emalloc() could bailout */
objval->active = 0;
} zend_end_try();
PHP_RUNKIT_SANDBOX_END(objval)
if (member == &tmp_member) {
zval_dtor(member);
}
}
/* }}} */
/* {{{ php_runkit_sandbox_has_property
has_property handler */
static int php_runkit_sandbox_has_property(zval *object, zval *member, int has_set_exists
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION > 5)
, const zend_literal *key
#endif
TSRMLS_DC)
{
php_runkit_sandbox_object* objval = PHP_RUNKIT_SANDBOX_FETCHBOX(object);
zval member_copy;
int result = 0;
if (!objval) {
return 0;
}
/* It's okay to read the symbol table post bailout */
PHP_RUNKIT_ZVAL_CONVERT_TO_STRING_IF_NEEDED(member, member_copy);
PHP_RUNKIT_SANDBOX_BEGIN(objval)
php_runkit_sandbox_has_property_int(has_set_exists, member TSRMLS_CC);
PHP_RUNKIT_SANDBOX_END(objval)
if (member == &member_copy) {
zval_dtor(member);
}
return result;
}
/* }}} */
/* {{{ php_runkit_sandbox_unset_property
unset_property handler */
static void php_runkit_sandbox_unset_property(zval *object, zval *member
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION > 6)
, const zend_literal *key
#endif
TSRMLS_DC)
{
php_runkit_sandbox_object *objval;
zval member_copy;
objval = PHP_RUNKIT_SANDBOX_FETCHBOX(object);
if (!objval) {
return;
}
if (!objval->active) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Current sandbox is no longer active");
return;
}
PHP_RUNKIT_ZVAL_CONVERT_TO_STRING_IF_NEEDED(member, member_copy);
PHP_RUNKIT_SANDBOX_BEGIN(objval)
zend_hash_del(&EG(symbol_table), Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
PHP_RUNKIT_SANDBOX_END(objval)
if (member == &member_copy) {
zval_dtor(member);
}
}
/* }}} */
/* **************
* SAPI Wedge *
************** */
static sapi_module_struct php_runkit_sandbox_original_sapi;
/* {{{ php_runkit_sandbox_sapi_ub_write
* Wrap the caller's output handler with a mechanism for switching contexts
*/
static int php_runkit_sandbox_sapi_ub_write(const char *str, uint str_length TSRMLS_DC)
{
php_runkit_sandbox_object *objval = RUNKIT_G(current_sandbox);
int bytes_written;
if (!str_length) {
return 0;
}
if (!objval) {
/* Not in a sandbox -- Use genuine sapi.ub_write handler */
if (php_runkit_sandbox_original_sapi.ub_write) {
return php_runkit_sandbox_original_sapi.ub_write(str, str_length TSRMLS_CC);
} else {
/* Ignore data, no real SAPI handler to pass it to */
return str_length;
}
}
tsrm_set_interpreter_context(objval->parent_context);
{
zval **output_buffer[1], *bufcopy, *retval = NULL;
TSRMLS_FETCH();
if (!objval->output_handler ||
!RUNKIT_IS_CALLABLE(objval->output_handler, IS_CALLABLE_CHECK_NO_ACCESS, NULL)) {
/* No hander, or invalid handler, pass up the line... */
bytes_written = PHPWRITE(str, str_length);
tsrm_set_interpreter_context(objval->context);
return bytes_written;
}
MAKE_STD_ZVAL(bufcopy);
ZVAL_STRINGL(bufcopy, (char*)str, str_length, 1);
output_buffer[0] = &bufcopy;
if (call_user_function_ex(EG(function_table), NULL, objval->output_handler, &retval, 1, output_buffer, 0, NULL TSRMLS_CC) == SUCCESS) {
if (retval) {
if (Z_TYPE_P(retval) != IS_NULL) {
convert_to_string(retval);
PHPWRITE(Z_STRVAL_P(retval), Z_STRLEN_P(retval));