forked from zenovich/runkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runkit_sandbox.c
2014 lines (1712 loc) · 58.9 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-2012 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Sara Golemon <[email protected]> |
| Props: Wez Furlong |
| Modified by Dmitry Zenovich <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "php_runkit.h"
#ifdef PHP_RUNKIT_SANDBOX
#include "SAPI.h"
#include "php_main.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 *
****************** */
/* 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
* 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
*/
inline void php_runkit_sandbox_ini_override(php_runkit_sandbox_object *objval, HashTable *options TSRMLS_DC)
{
zend_bool allow_url_fopen;
char open_basedir[MAXPATHLEN] = {0};
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || (PHP_MAJOR_VERSION < 5)
zend_bool safe_mode, safe_mode_gid;
char safe_mode_include_dir[MAXPATHLEN] = {0};
#endif
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(open_basedir) && *PG(open_basedir)) {
VCWD_REALPATH(PG(open_basedir), open_basedir);
}
if (PG(safe_mode_include_dir) && *PG(safe_mode_include_dir)) {
VCWD_REALPATH(PG(safe_mode_include_dir), safe_mode_include_dir);
}
#endif
allow_url_fopen = PG(allow_url_fopen);
}
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_dir && safe_mode) {
/* 2nd simplest case -- Full security already with safe_mode preexisting -- leave it alone */
} else if (!*safe_mode_include_dir) {
/* 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... */
int safe_mode_include_dir_len = strlen(safe_mode_include_dir);
char new_include_dir[MAXPATHLEN];
int new_include_dir_len;
VCWD_REALPATH(Z_STRVAL_PP(tmpzval), new_include_dir);
new_include_dir_len = strlen(new_include_dir);
if ((new_include_dir_len > safe_mode_include_dir_len) &&
(strncmp(safe_mode_include_dir, new_include_dir, safe_mode_include_dir_len) == 0) &&
(new_include_dir[safe_mode_include_dir_len] == '/')) {
/* 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);
}
}
}
#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) {
char new_basedir[MAXPATHLEN];
int open_basedir_len = strlen(open_basedir);
int new_basedir_len;
VCWD_REALPATH(Z_STRVAL_PP(tmpzval), new_basedir);
new_basedir_len = strlen(new_basedir);
if (open_basedir_len == 0) {
/* simplest case -- no open basedir existed yet */
zend_alter_ini_entry("open_basedir", sizeof("open_basedir"), new_basedir, new_basedir_len, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
} else if ((new_basedir_len > open_basedir_len) &&
(strncmp(open_basedir, new_basedir, open_basedir_len) == 0) &&
(new_basedir[open_basedir_len] == '/')) {
/* Tightening up of existing security level */
zend_alter_ini_entry("open_basedir", sizeof("open_basedir"), new_basedir, new_basedir_len, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
}
}
/* 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);
}
}
}
/* }}} */
/* {{{ 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;
}
/* }}} */
#if RUNKIT_ABOVE53
/* {{{ php_runkit_zend_object_store_get_obj */
static zend_object_store_bucket *php_runkit_zend_object_store_get_obj(const zval *zobject TSRMLS_DC)
{
zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
return &EG(objects_store).object_buckets[handle];
}
/* }}} */
#endif
/* {{{ 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, argc;
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();
}
argc = zend_hash_num_elements(Z_ARRVAL_P(args));
PHP_RUNKIT_SANDBOX_BEGIN(objval)
{
zval ***sandbox_args;
char *name = NULL;
int i;
zend_first_try {
HashPosition pos;
zval **tmpzval;
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;
}
sandbox_args = safe_emalloc(sizeof(zval**), argc, 0);
for(zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(args), &pos), i = 0;
(zend_hash_get_current_data_ex(Z_ARRVAL_P(args), (void**)&tmpzval, &pos) == SUCCESS) && (i < argc);
zend_hash_move_forward_ex(Z_ARRVAL_P(args), &pos), i++) {
sandbox_args[i] = emalloc(sizeof(zval*));
MAKE_STD_ZVAL(*sandbox_args[i]);
**sandbox_args[i] = **tmpzval;
#if RUNKIT_ABOVE53
if (Z_TYPE_P(*sandbox_args[i]) == IS_OBJECT && zend_get_class_entry(*sandbox_args[i], prior_context) == zend_ce_closure) {
zend_closure *closure;
zend_object_store_bucket *bucket;
bucket = php_runkit_zend_object_store_get_obj(*sandbox_args[i], prior_context);
closure = (zend_closure *) bucket->bucket.obj.object;
(*sandbox_args[i])->value.obj.handle = zend_objects_store_put(closure, NULL, NULL, bucket->bucket.obj.clone TSRMLS_CC);
} else
#endif
PHP_SANDBOX_CROSS_SCOPE_ZVAL_COPY_CTOR(*sandbox_args[i]);
}
/* Shouldn't be necessary */
argc = i;
/* Note: If this function is disabled by disable_functions or disable_classes,
* The user will get a confusing error message about (null)() being disabled for security reasons on line 0
* This will be fixable with a properly set EG(function_state_ptr)....just not yet
*/
if (call_user_function_ex(EG(function_table), NULL, func_name, &retval, argc, sandbox_args, 0, NULL TSRMLS_CC) == SUCCESS) {
if (retval) {
*return_value = *retval;
} else {
RETVAL_TRUE;
}
} else {
php_error_docref1(NULL TSRMLS_CC, name, E_WARNING, "Unable to call function");
RETVAL_FALSE;
}
if (name) {
efree(name);
}
for(i = 0; i < argc; i++) {
#if RUNKIT_ABOVE53
if (Z_TYPE_P(*sandbox_args[i]) == IS_OBJECT && zend_get_class_entry(*sandbox_args[i] TSRMLS_CC) == zend_ce_closure) {
zend_object_store_bucket *bucket = php_runkit_zend_object_store_get_obj(*sandbox_args[i] TSRMLS_CC);
zend_objects_store_del_ref(*sandbox_args[i] TSRMLS_CC);
zval_ptr_dtor(sandbox_args[i]);
bucket->bucket.obj.object = NULL;
}
#endif
zval_ptr_dtor(sandbox_args[i]);
efree(sandbox_args[i]);
}
efree(sandbox_args);
} 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;
if (type == ZEND_EVAL) {
/* eval() */
char *eval_desc = zend_make_compiled_string_description("Runkit_Sandbox Eval Code" TSRMLS_CC);
op_array = compile_string(zcode, eval_desc TSRMLS_CC);
efree(eval_desc);
} else if (!once) {
/* include() & requre() */
op_array = compile_filename(type, zcode TSRMLS_CC);
} else {
/* include_once() & require_once() */
int dummy = 1;
zend_file_handle file_handle;
if (SUCCESS == zend_stream_open(Z_STRVAL_P(zcode), &file_handle TSRMLS_CC)) {
if (!file_handle.opened_path) {
file_handle.opened_path = estrndup(Z_STRVAL_P(zcode), Z_STRLEN_P(zcode));
}
if (zend_hash_add(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1, (void *)&dummy, sizeof(int), NULL)==SUCCESS) {
op_array = zend_compile_file(&file_handle, type TSRMLS_CC);
zend_destroy_file_handle(&file_handle TSRMLS_CC);
} else {
RUNKIT_FILE_HANDLE_DTOR(&file_handle);
RETVAL_TRUE;
already_included = 1;
}
}
}
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();
if (zend_get_parameters_array_ex(argc, &argv) == FAILURE) {
/* 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) {
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_P(argv[i]),Z_STRLEN_P(argv[i]));
}
} zend_catch {
objval->active = 0;
} zend_end_try();
PHP_RUNKIT_SANDBOX_END(objval)
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 = NULL;
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);
}
if (Z_TYPE_P(member) != IS_STRING) {
ALLOC_ZVAL(tmp_member);
*tmp_member = *member;
INIT_PZVAL(tmp_member);
zval_copy_ctor(tmp_member);
convert_to_string(tmp_member);
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 (tmp_member) {
zval_ptr_dtor(&tmp_member);
}
if (prop_found) {
zval *return_value;
ALLOC_ZVAL(return_value);
*return_value = retval;
/* ZE expects refcount == 0 for unowned values */
INIT_PZVAL(return_value);
PHP_SANDBOX_CROSS_SCOPE_ZVAL_COPY_CTOR(return_value);
return_value->RUNKIT_REFCOUNT--;
return return_value;
} else {
return EG(uninitialized_zval_ptr);
}
}
/* }}} */
/* {{{ 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 = NULL;
if (!objval->active) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Current sandbox is no longer active");
return;
}
if (Z_TYPE_P(member) != IS_STRING) {
ALLOC_ZVAL(tmp_member);
*tmp_member = *member;
INIT_PZVAL(tmp_member);
zval_copy_ctor(tmp_member);
convert_to_string(tmp_member);
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 (tmp_member) {
zval_ptr_dtor(&tmp_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 > 6)
, const zend_literal *key
#endif
TSRMLS_DC)
{
php_runkit_sandbox_object* objval;
zval member_copy;
int result = 0;
#if PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 1
/* Map PHP 5.0 has_property flag to PHP 5.1+ flag */
has_set_exists = (has_set_exists == 0) ? 2 : 1;
#endif
objval = PHP_RUNKIT_SANDBOX_FETCHBOX(object);
if (!objval) {
return 0;
}
/* It's okay to read the symbol table post bailout */
if (Z_TYPE_P(member) != IS_STRING) {
member_copy = *member;
member = &member_copy;
zval_copy_ctor(member);
member->RUNKIT_REFCOUNT = 1;
convert_to_string(member);
}
PHP_RUNKIT_SANDBOX_BEGIN(objval)
{
zval **tmpzval;
if (zend_hash_find(&EG(symbol_table), Z_STRVAL_P(member), Z_STRLEN_P(member) + 1, (void**)&tmpzval) == SUCCESS) {
switch (has_set_exists) {
case 0:
result = (Z_TYPE_PP(tmpzval) != IS_NULL);
break;
case 1:
switch (Z_TYPE_PP(tmpzval)) {
case IS_BOOL: case IS_LONG: case IS_RESOURCE:
result = (Z_LVAL_PP(tmpzval) != 0);
break;
case IS_DOUBLE:
result = (Z_DVAL_PP(tmpzval) != 0);
break;
case IS_STRING:
result = (Z_STRLEN_PP(tmpzval) > 1 || (Z_STRLEN_PP(tmpzval) == 1 && Z_STRVAL_PP(tmpzval)[0] != '0'));
break;
case IS_ARRAY:
result = zend_hash_num_elements(Z_ARRVAL_PP(tmpzval)) > 0;
break;
case IS_OBJECT:
/* TODO: Use ZE2 logic for this rather than ZE1 logic */
result = zend_hash_num_elements(Z_OBJPROP_PP(tmpzval)) > 0;
break;
case IS_NULL:
default:
result = 0;
}
break;
case 2:
result = 1;
break;
}
} else {
result = 0;
}
}
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;
}
if (Z_TYPE_P(member) != IS_STRING) {
member_copy = *member;
member = &member_copy;
zval_copy_ctor(member);
member->RUNKIT_REFCOUNT = 1;
convert_to_string(member);
}
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... */