diff options
| author | xXx-caillou-xXx | 2018-08-30 17:14:08 +0200 |
|---|---|---|
| committer | jvoisin | 2018-08-30 15:14:08 +0000 |
| commit | 206ffa3fb3fd72c6a2eb45194fb176535a91288c (patch) | |
| tree | 23a25d83c4ca878861f413a0d5df2ad77c7dadaf /src/sp_execute.c | |
| parent | b3f67a16094168cc334f5da93a86f09476e01601 (diff) | |
Minor code cleanup
Diffstat (limited to 'src/sp_execute.c')
| -rw-r--r-- | src/sp_execute.c | 81 |
1 files changed, 40 insertions, 41 deletions
diff --git a/src/sp_execute.c b/src/sp_execute.c index 5447ea1..60d63ab 100644 --- a/src/sp_execute.c +++ b/src/sp_execute.c | |||
| @@ -13,14 +13,15 @@ static int (*orig_zend_stream_open)(const char *filename, | |||
| 13 | 13 | ||
| 14 | // FIXME handle symlink | 14 | // FIXME handle symlink |
| 15 | ZEND_COLD static inline void terminate_if_writable(const char *filename) { | 15 | ZEND_COLD static inline void terminate_if_writable(const char *filename) { |
| 16 | const sp_config_readonly_exec* config_ro_exec = | ||
| 17 | SNUFFLEUPAGUS_G(config).config_readonly_exec; | ||
| 18 | |||
| 16 | if (0 == access(filename, W_OK)) { | 19 | if (0 == access(filename, W_OK)) { |
| 17 | if (SNUFFLEUPAGUS_G(config).config_readonly_exec->dump) { | 20 | if (config_ro_exec->dump) { |
| 18 | sp_log_request( | 21 | sp_log_request(config_ro_exec->dump, config_ro_exec->textual_representation, |
| 19 | SNUFFLEUPAGUS_G(config).config_readonly_exec->dump, | ||
| 20 | SNUFFLEUPAGUS_G(config).config_readonly_exec->textual_representation, | ||
| 21 | SP_TOKEN_READONLY_EXEC); | 22 | SP_TOKEN_READONLY_EXEC); |
| 22 | } | 23 | } |
| 23 | if (true == SNUFFLEUPAGUS_G(config).config_readonly_exec->simulation) { | 24 | if (true == config_ro_exec->simulation) { |
| 24 | sp_log_msg("readonly_exec", SP_LOG_SIMULATION, | 25 | sp_log_msg("readonly_exec", SP_LOG_SIMULATION, |
| 25 | "Attempted execution of a writable file (%s).", filename); | 26 | "Attempted execution of a writable file (%s).", filename); |
| 26 | } else { | 27 | } else { |
| @@ -57,7 +58,7 @@ inline static void is_builtin_matching( | |||
| 57 | 58 | ||
| 58 | static void ZEND_HOT | 59 | static void ZEND_HOT |
| 59 | is_in_eval_and_whitelisted(const zend_execute_data *execute_data) { | 60 | is_in_eval_and_whitelisted(const zend_execute_data *execute_data) { |
| 60 | sp_config_eval *eval = SNUFFLEUPAGUS_G(config).config_eval; | 61 | const sp_config_eval *config_eval = SNUFFLEUPAGUS_G(config).config_eval; |
| 61 | 62 | ||
| 62 | if (EXPECTED(0 == SNUFFLEUPAGUS_G(in_eval))) { | 63 | if (EXPECTED(0 == SNUFFLEUPAGUS_G(in_eval))) { |
| 63 | return; | 64 | return; |
| @@ -79,13 +80,11 @@ is_in_eval_and_whitelisted(const zend_execute_data *execute_data) { | |||
| 79 | 80 | ||
| 80 | if (EXPECTED(NULL != current_function)) { | 81 | if (EXPECTED(NULL != current_function)) { |
| 81 | if (UNEXPECTED(false == check_is_in_eval_whitelist(current_function))) { | 82 | if (UNEXPECTED(false == check_is_in_eval_whitelist(current_function))) { |
| 82 | if (eval->dump) { | 83 | if (config_eval->dump) { |
| 83 | sp_log_request( | 84 | sp_log_request(config_eval->dump, config_eval->textual_representation, |
| 84 | SNUFFLEUPAGUS_G(config).config_eval->dump, | ||
| 85 | SNUFFLEUPAGUS_G(config).config_eval->textual_representation, | ||
| 86 | SP_TOKEN_EVAL_WHITELIST); | 85 | SP_TOKEN_EVAL_WHITELIST); |
| 87 | } | 86 | } |
| 88 | if (eval->simulation) { | 87 | if (config_eval->simulation) { |
| 89 | sp_log_msg( | 88 | sp_log_msg( |
| 90 | "Eval_whitelist", SP_LOG_SIMULATION, | 89 | "Eval_whitelist", SP_LOG_SIMULATION, |
| 91 | "The function '%s' isn't in the eval whitelist, logging its call.", | 90 | "The function '%s' isn't in the eval whitelist, logging its call.", |
| @@ -124,17 +123,19 @@ zend_string *get_eval_filename(const char *const filename) { | |||
| 124 | 123 | ||
| 125 | static void sp_execute_ex(zend_execute_data *execute_data) { | 124 | static void sp_execute_ex(zend_execute_data *execute_data) { |
| 126 | is_in_eval_and_whitelisted(execute_data); | 125 | is_in_eval_and_whitelisted(execute_data); |
| 126 | const HashTable* config_disabled_functions = | ||
| 127 | SNUFFLEUPAGUS_G(config).config_disabled_functions; | ||
| 127 | 128 | ||
| 128 | if (!execute_data) { | 129 | if (!execute_data) { |
| 129 | return; | 130 | return; |
| 130 | } | 131 | } |
| 131 | 132 | ||
| 132 | if (UNEXPECTED(EX(func)->op_array.type == ZEND_EVAL_CODE)) { | 133 | if (UNEXPECTED(EX(func)->op_array.type == ZEND_EVAL_CODE)) { |
| 133 | const sp_list_node *config = zend_hash_str_find_ptr( | 134 | const sp_list_node * config = zend_hash_str_find_ptr( |
| 134 | SNUFFLEUPAGUS_G(config).config_disabled_functions, "eval", 4); | 135 | config_disabled_functions, "eval", sizeof("eval") - 1); |
| 136 | |||
| 135 | zend_string *filename = get_eval_filename(zend_get_executed_filename()); | 137 | zend_string *filename = get_eval_filename(zend_get_executed_filename()); |
| 136 | is_builtin_matching(filename, "eval", NULL, config, | 138 | is_builtin_matching(filename, "eval", NULL, config, config_disabled_functions); |
| 137 | SNUFFLEUPAGUS_G(config).config_disabled_functions); | ||
| 138 | zend_string_release(filename); | 139 | zend_string_release(filename); |
| 139 | 140 | ||
| 140 | SNUFFLEUPAGUS_G(in_eval)++; | 141 | SNUFFLEUPAGUS_G(in_eval)++; |
| @@ -152,6 +153,9 @@ static void sp_execute_ex(zend_execute_data *execute_data) { | |||
| 152 | if (SNUFFLEUPAGUS_G(config).hook_execute) { | 153 | if (SNUFFLEUPAGUS_G(config).hook_execute) { |
| 153 | char *function_name = get_complete_function_path(execute_data); | 154 | char *function_name = get_complete_function_path(execute_data); |
| 154 | zval ret_val; | 155 | zval ret_val; |
| 156 | const sp_list_node* config_disabled_functions_reg = | ||
| 157 | SNUFFLEUPAGUS_G(config).config_disabled_functions_reg | ||
| 158 | ->disabled_functions; | ||
| 155 | 159 | ||
| 156 | if (!function_name) { | 160 | if (!function_name) { |
| 157 | orig_execute_ex(execute_data); | 161 | orig_execute_ex(execute_data); |
| @@ -163,11 +167,9 @@ static void sp_execute_ex(zend_execute_data *execute_data) { | |||
| 163 | !ZEND_USER_CODE(execute_data->prev_execute_data->func->type) || | 167 | !ZEND_USER_CODE(execute_data->prev_execute_data->func->type) || |
| 164 | !execute_data->prev_execute_data->opline) { | 168 | !execute_data->prev_execute_data->opline) { |
| 165 | if (UNEXPECTED(true == | 169 | if (UNEXPECTED(true == |
| 166 | should_disable_ht( | 170 | should_disable_ht(execute_data, function_name, NULL, NULL, |
| 167 | execute_data, function_name, NULL, NULL, | 171 | config_disabled_functions_reg, |
| 168 | SNUFFLEUPAGUS_G(config) | 172 | config_disabled_functions))) { |
| 169 | .config_disabled_functions_reg->disabled_functions, | ||
| 170 | SNUFFLEUPAGUS_G(config).config_disabled_functions))) { | ||
| 171 | sp_terminate(); | 173 | sp_terminate(); |
| 172 | } | 174 | } |
| 173 | } else if ((execute_data->prev_execute_data->opline->opcode == | 175 | } else if ((execute_data->prev_execute_data->opline->opcode == |
| @@ -177,11 +179,9 @@ static void sp_execute_ex(zend_execute_data *execute_data) { | |||
| 177 | execute_data->prev_execute_data->opline->opcode == | 179 | execute_data->prev_execute_data->opline->opcode == |
| 178 | ZEND_DO_FCALL_BY_NAME)) { | 180 | ZEND_DO_FCALL_BY_NAME)) { |
| 179 | if (UNEXPECTED(true == | 181 | if (UNEXPECTED(true == |
| 180 | should_disable_ht( | 182 | should_disable_ht(execute_data, function_name, NULL, NULL, |
| 181 | execute_data, function_name, NULL, NULL, | 183 | config_disabled_functions_reg, |
| 182 | SNUFFLEUPAGUS_G(config) | 184 | config_disabled_functions))) { |
| 183 | .config_disabled_functions_reg->disabled_functions, | ||
| 184 | SNUFFLEUPAGUS_G(config).config_disabled_functions))) { | ||
| 185 | sp_terminate(); | 185 | sp_terminate(); |
| 186 | } | 186 | } |
| 187 | } | 187 | } |
| @@ -235,6 +235,9 @@ static int sp_stream_open(const char *filename, zend_file_handle *handle) { | |||
| 235 | } | 235 | } |
| 236 | 236 | ||
| 237 | zend_string *zend_filename = zend_string_init(filename, strlen(filename), 0); | 237 | zend_string *zend_filename = zend_string_init(filename, strlen(filename), 0); |
| 238 | const HashTable* disabled_functions_hooked = | ||
| 239 | SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked; | ||
| 240 | |||
| 238 | switch (data->opline->opcode) { | 241 | switch (data->opline->opcode) { |
| 239 | case ZEND_INCLUDE_OR_EVAL: | 242 | case ZEND_INCLUDE_OR_EVAL: |
| 240 | if (true == SNUFFLEUPAGUS_G(config).config_readonly_exec->enable) { | 243 | if (true == SNUFFLEUPAGUS_G(config).config_readonly_exec->enable) { |
| @@ -244,34 +247,30 @@ static int sp_stream_open(const char *filename, zend_file_handle *handle) { | |||
| 244 | case ZEND_INCLUDE: | 247 | case ZEND_INCLUDE: |
| 245 | is_builtin_matching( | 248 | is_builtin_matching( |
| 246 | zend_filename, "include", "inclusion path", | 249 | zend_filename, "include", "inclusion path", |
| 247 | zend_hash_str_find_ptr( | 250 | zend_hash_str_find_ptr(disabled_functions_hooked, |
| 248 | SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked, | 251 | "include", sizeof("include") - 1), |
| 249 | "include", 7), | 252 | disabled_functions_hooked); |
| 250 | SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked); | ||
| 251 | break; | 253 | break; |
| 252 | case ZEND_REQUIRE: | 254 | case ZEND_REQUIRE: |
| 253 | is_builtin_matching( | 255 | is_builtin_matching( |
| 254 | zend_filename, "require", "inclusion path", | 256 | zend_filename, "require", "inclusion path", |
| 255 | zend_hash_str_find_ptr( | 257 | zend_hash_str_find_ptr(disabled_functions_hooked, |
| 256 | SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked, | 258 | "require", sizeof("require") - 1), |
| 257 | "require", 7), | 259 | disabled_functions_hooked); |
| 258 | SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked); | ||
| 259 | break; | 260 | break; |
| 260 | case ZEND_REQUIRE_ONCE: | 261 | case ZEND_REQUIRE_ONCE: |
| 261 | is_builtin_matching( | 262 | is_builtin_matching( |
| 262 | zend_filename, "require_once", "inclusion path", | 263 | zend_filename, "require_once", "inclusion path", |
| 263 | zend_hash_str_find_ptr( | 264 | zend_hash_str_find_ptr(disabled_functions_hooked, |
| 264 | SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked, | 265 | "require_once", sizeof("require_once") - 1), |
| 265 | "require_once", 12), | 266 | disabled_functions_hooked); |
| 266 | SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked); | ||
| 267 | break; | 267 | break; |
| 268 | case ZEND_INCLUDE_ONCE: | 268 | case ZEND_INCLUDE_ONCE: |
| 269 | is_builtin_matching( | 269 | is_builtin_matching( |
| 270 | zend_filename, "include_once", "inclusion path", | 270 | zend_filename, "include_once", "inclusion path", |
| 271 | zend_hash_str_find_ptr( | 271 | zend_hash_str_find_ptr(disabled_functions_hooked, |
| 272 | SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked, | 272 | "include_once", sizeof("include_once") - 1), |
| 273 | "include_once", 12), | 273 | disabled_functions_hooked); |
| 274 | SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked); | ||
| 275 | break; | 274 | break; |
| 276 | EMPTY_SWITCH_DEFAULT_CASE(); | 275 | EMPTY_SWITCH_DEFAULT_CASE(); |
| 277 | } | 276 | } |
