Skip to content

Commit

Permalink
implemented execution depth limit
Browse files Browse the repository at this point in the history
  • Loading branch information
bef committed Sep 15, 2021
1 parent 8e42064 commit 31d6a3c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/php_snuffleupagus.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ ZEND_BEGIN_MODULE_GLOBALS(snuffleupagus)
size_t in_eval;
sp_config config;
int is_config_valid; // 1 = valid, 0 = invalid, -1 = none
u_long execution_depth;
bool allow_broken_configuration;
HashTable *disabled_functions_hook;
HashTable *sp_internal_functions_hook;
Expand Down
2 changes: 2 additions & 0 deletions src/snuffleupagus.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ static PHP_GSHUTDOWN_FUNCTION(snuffleupagus) {
}

PHP_RINIT_FUNCTION(snuffleupagus) {
SNUFFLEUPAGUS_G(execution_depth) = 0;

const sp_config_wrapper *const config_wrapper =
SNUFFLEUPAGUS_G(config).config_wrapper;
#if defined(COMPILE_DL_SNUFFLEUPAGUS) && defined(ZTS)
Expand Down
2 changes: 2 additions & 0 deletions src/sp_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ typedef struct {
sp_config_ini *config_ini;
bool hook_execute;
char log_media;
u_long max_execution_depth;

HashTable *config_disabled_functions;
HashTable *config_disabled_functions_hooked;
Expand Down Expand Up @@ -286,6 +287,7 @@ typedef struct {
#define SP_TOKEN_ENCRYPTION_KEY "secret_key"
#define SP_TOKEN_ENV_VAR "cookie_env_var"
#define SP_TOKEN_LOG_MEDIA "log_media"
#define SP_TOKEN_MAX_EXECUTION_DEPTH "max_execution_depth"

// upload_validator
#define SP_TOKEN_UPLOAD_SCRIPT "script"
Expand Down
1 change: 1 addition & 0 deletions src/sp_config_keywords.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ SP_PARSE_FN(parse_global) {
{parse_str, SP_TOKEN_ENCRYPTION_KEY, &(SNUFFLEUPAGUS_G(config).config_snuffleupagus->encryption_key)},
{parse_str, SP_TOKEN_ENV_VAR, &(SNUFFLEUPAGUS_G(config).config_snuffleupagus->cookies_env_var)},
{parse_log_media, SP_TOKEN_LOG_MEDIA, &(SNUFFLEUPAGUS_G(config).log_media)},
{parse_ulong, SP_TOKEN_MAX_EXECUTION_DEPTH, &(SNUFFLEUPAGUS_G(config).max_execution_depth)},
{0, 0, 0}};

SP_PROCESS_CONFIG_KEYWORDS_ERR();
Expand Down
17 changes: 13 additions & 4 deletions src/sp_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ zend_string *get_eval_filename(const char *const filename) {
return clean_filename;
}

static inline void sp_orig_execute(zend_execute_data *execute_data) {
SNUFFLEUPAGUS_G(execution_depth)++;
if (SNUFFLEUPAGUS_G(execution_depth) > SNUFFLEUPAGUS_G(config).max_execution_depth && SNUFFLEUPAGUS_G(config).max_execution_depth > 0) {
sp_log_drop("execute", "Maximum recursion limit reached. Script terminated.");
}
orig_execute_ex(execute_data);
SNUFFLEUPAGUS_G(execution_depth)--;
}

static void sp_execute_ex(zend_execute_data *execute_data) {
is_in_eval_and_whitelisted(execute_data);
const HashTable *config_disabled_functions =
Expand All @@ -131,7 +140,7 @@ static void sp_execute_ex(zend_execute_data *execute_data) {
zend_string_release(filename);

SNUFFLEUPAGUS_G(in_eval)++;
orig_execute_ex(execute_data);
sp_orig_execute(execute_data);
SNUFFLEUPAGUS_G(in_eval)--;
return;
}
Expand All @@ -150,7 +159,7 @@ static void sp_execute_ex(zend_execute_data *execute_data) {
.config_disabled_functions_reg->disabled_functions;

if (!function_name) {
orig_execute_ex(execute_data);
sp_orig_execute(execute_data);
return;
}

Expand Down Expand Up @@ -184,7 +193,7 @@ static void sp_execute_ex(zend_execute_data *execute_data) {
EX(return_value) = &ret_val;
}

orig_execute_ex(execute_data);
sp_orig_execute(execute_data);

should_drop_on_ret_ht(
EX(return_value), function_name,
Expand All @@ -197,7 +206,7 @@ static void sp_execute_ex(zend_execute_data *execute_data) {
EX(return_value) = NULL;
}
} else {
orig_execute_ex(execute_data);
sp_orig_execute(execute_data);
}
}

Expand Down

0 comments on commit 31d6a3c

Please sign in to comment.