From 87fc324eb63d533b35464f1dfca946795f2294fd Mon Sep 17 00:00:00 2001 From: "sammy.powers" Date: Wed, 18 Sep 2019 14:26:56 -0700 Subject: [PATCH] Ensure the request init hook is bound by the open_basedir INI directive Also sandbox the request init hook so that errors and exceptions are ignored --- package.xml | 1 + src/ext/request_hooks.c | 18 +++++++++++------- src/ext/sandbox.h | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 src/ext/sandbox.h diff --git a/package.xml b/package.xml index c311e02257..d242bcf99a 100644 --- a/package.xml +++ b/package.xml @@ -67,6 +67,7 @@ + diff --git a/src/ext/request_hooks.c b/src/ext/request_hooks.c index d740b0153d..e7e2d6f425 100644 --- a/src/ext/request_hooks.c +++ b/src/ext/request_hooks.c @@ -9,6 +9,7 @@ #include "ddtrace.h" #include "env_config.h" #include "logging.h" +#include "sandbox.h" ZEND_EXTERN_MODULE_GLOBALS(ddtrace); @@ -81,10 +82,10 @@ int dd_execute_php_file(const char *filename TSRMLS_DC) { int ret; BOOL_T rv = FALSE; - char *original_open_basedir = PG(open_basedir); - PG(open_basedir) = NULL; + DD_TRACE_SANDBOX_OPEN ret = php_stream_open_for_zend_ex(filename, &file_handle, USE_PATH | STREAM_OPEN_FOR_INCLUDE TSRMLS_CC); + DD_TRACE_SANDBOX_CLOSE if (ret == SUCCESS) { if (!file_handle.opened_path) { @@ -105,7 +106,10 @@ int dd_execute_php_file(const char *filename TSRMLS_DC) { zend_rebuild_symbol_table(TSRMLS_C); } - zend_execute(new_op_array TSRMLS_CC); + DD_TRACE_SANDBOX_OPEN + zend_try { zend_execute(new_op_array TSRMLS_CC); } + zend_end_try(); + DD_TRACE_SANDBOX_CLOSE destroy_op_array(new_op_array TSRMLS_CC); efree(new_op_array); @@ -118,7 +122,6 @@ int dd_execute_php_file(const char *filename TSRMLS_DC) { } } - PG(open_basedir) = original_open_basedir; return rv; } #else @@ -133,9 +136,9 @@ int dd_execute_php_file(const char *filename TSRMLS_DC) { zend_op_array *new_op_array; zval result; int ret, rv = FALSE; - char *original_open_basedir = PG(open_basedir); - PG(open_basedir) = NULL; + DD_TRACE_SANDBOX_OPEN ret = php_stream_open_for_zend_ex(filename, &file_handle, USE_PATH | STREAM_OPEN_FOR_INCLUDE); + DD_TRACE_SANDBOX_CLOSE if (ret == SUCCESS) { zend_string *opened_path; @@ -154,7 +157,9 @@ int dd_execute_php_file(const char *filename TSRMLS_DC) { zend_string_release(opened_path); if (new_op_array) { ZVAL_UNDEF(&result); + DD_TRACE_SANDBOX_OPEN zend_execute(new_op_array, &result); + DD_TRACE_SANDBOX_CLOSE destroy_op_array(new_op_array); efree(new_op_array); @@ -165,7 +170,6 @@ int dd_execute_php_file(const char *filename TSRMLS_DC) { } } - PG(open_basedir) = original_open_basedir; return rv; } #endif diff --git a/src/ext/sandbox.h b/src/ext/sandbox.h new file mode 100644 index 0000000000..97422a21b4 --- /dev/null +++ b/src/ext/sandbox.h @@ -0,0 +1,37 @@ +#ifndef DDTRACE_SANDBOX_H +#define DDTRACE_SANDBOX_H +#include +#include + +#if PHP_VERSION_ID < 70000 +#define DD_TRACE_SANDBOX_OPEN \ + zend_error_handling error_handling; \ + int orig_error_reporting = EG(error_reporting); \ + EG(error_reporting) = 0; \ + zend_replace_error_handling(EH_SUPPRESS, NULL, &error_handling TSRMLS_CC); \ + { +#define DD_TRACE_SANDBOX_CLOSE \ + } \ + zend_restore_error_handling(&error_handling TSRMLS_CC); \ + EG(error_reporting) = orig_error_reporting; \ + if (EG(exception)) { \ + if (!DDTRACE_G(strict_mode)) { \ + zend_clear_exception(TSRMLS_C); \ + } \ + } +#else +#define DD_TRACE_SANDBOX_OPEN \ + int orig_error_reporting = EG(error_reporting); \ + EG(error_reporting) = 0; \ + { +#define DD_TRACE_SANDBOX_CLOSE \ + } \ + EG(error_reporting) = orig_error_reporting; \ + if (EG(exception)) { \ + if (!DDTRACE_G(strict_mode)) { \ + zend_clear_exception(TSRMLS_C); \ + } \ + } +#endif + +#endif // DDTRACE_SANDBOX_H