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/snuffleupagus.c | |
| parent | b3f67a16094168cc334f5da93a86f09476e01601 (diff) | |
Minor code cleanup
Diffstat (limited to 'src/snuffleupagus.c')
| -rw-r--r-- | src/snuffleupagus.c | 196 |
1 files changed, 98 insertions, 98 deletions
diff --git a/src/snuffleupagus.c b/src/snuffleupagus.c index 4fee81a..0126a37 100644 --- a/src/snuffleupagus.c +++ b/src/snuffleupagus.c | |||
| @@ -51,12 +51,6 @@ PHP_INI_ENTRY("sp.allow_broken_configuration", "0", PHP_INI_SYSTEM, | |||
| 51 | StrictMode) | 51 | StrictMode) |
| 52 | PHP_INI_END() | 52 | PHP_INI_END() |
| 53 | 53 | ||
| 54 | void free_disabled_functions_hashtable(HashTable *ht) { | ||
| 55 | void *ptr = NULL; | ||
| 56 | ZEND_HASH_FOREACH_PTR(ht, ptr) { sp_list_free(ptr); } | ||
| 57 | ZEND_HASH_FOREACH_END(); | ||
| 58 | } | ||
| 59 | |||
| 60 | ZEND_DLEXPORT zend_extension zend_extension_entry = { | 54 | ZEND_DLEXPORT zend_extension zend_extension_entry = { |
| 61 | PHP_SNUFFLEUPAGUS_EXTNAME, | 55 | PHP_SNUFFLEUPAGUS_EXTNAME, |
| 62 | PHP_SNUFFLEUPAGUS_VERSION, | 56 | PHP_SNUFFLEUPAGUS_VERSION, |
| @@ -79,47 +73,45 @@ ZEND_DLEXPORT zend_extension zend_extension_entry = { | |||
| 79 | PHP_GINIT_FUNCTION(snuffleupagus) { | 73 | PHP_GINIT_FUNCTION(snuffleupagus) { |
| 80 | snuffleupagus_globals->in_eval = 0; | 74 | snuffleupagus_globals->in_eval = 0; |
| 81 | 75 | ||
| 82 | #define SP_INIT(F) F = pecalloc(sizeof(*F), 1, 1); | 76 | #define SP_INIT_HT(F) snuffleupagus_globals->F = \ |
| 83 | #define SP_INIT_HT(F) \ | 77 | pemalloc(sizeof(*(snuffleupagus_globals->F)), 1); \ |
| 84 | F = pemalloc(sizeof(*F), 1); \ | 78 | zend_hash_init(snuffleupagus_globals->F, 10, NULL, NULL, 1); |
| 85 | zend_hash_init(F, 10, NULL, NULL, 1); | 79 | SP_INIT_HT(disabled_functions_hook); |
| 86 | 80 | SP_INIT_HT(sp_internal_functions_hook); | |
| 87 | SP_INIT_HT(snuffleupagus_globals->disabled_functions_hook); | 81 | SP_INIT_HT(sp_eval_blacklist_functions_hook); |
| 88 | SP_INIT_HT(snuffleupagus_globals->sp_internal_functions_hook); | 82 | SP_INIT_HT(config.config_disabled_functions); |
| 89 | SP_INIT_HT(snuffleupagus_globals->sp_eval_blacklist_functions_hook); | 83 | SP_INIT_HT(config.config_disabled_functions_hooked); |
| 90 | SP_INIT_HT(snuffleupagus_globals->config.config_disabled_functions); | 84 | SP_INIT_HT(config.config_disabled_functions_ret); |
| 91 | SP_INIT_HT(snuffleupagus_globals->config.config_disabled_functions_hooked); | 85 | SP_INIT_HT(config.config_disabled_functions_ret_hooked); |
| 92 | SP_INIT_HT(snuffleupagus_globals->config.config_disabled_functions_ret); | 86 | #undef SP_INIT_HT |
| 93 | SP_INIT_HT( | ||
| 94 | snuffleupagus_globals->config.config_disabled_functions_ret_hooked); | ||
| 95 | |||
| 96 | SP_INIT(snuffleupagus_globals->config.config_unserialize); | ||
| 97 | SP_INIT(snuffleupagus_globals->config.config_random); | ||
| 98 | SP_INIT(snuffleupagus_globals->config.config_sloppy); | ||
| 99 | SP_INIT(snuffleupagus_globals->config.config_readonly_exec); | ||
| 100 | SP_INIT(snuffleupagus_globals->config.config_global_strict); | ||
| 101 | SP_INIT(snuffleupagus_globals->config.config_auto_cookie_secure); | ||
| 102 | SP_INIT(snuffleupagus_globals->config.config_snuffleupagus); | ||
| 103 | SP_INIT(snuffleupagus_globals->config.config_disable_xxe); | ||
| 104 | SP_INIT(snuffleupagus_globals->config.config_upload_validation); | ||
| 105 | SP_INIT(snuffleupagus_globals->config.config_disabled_functions_reg); | ||
| 106 | SP_INIT(snuffleupagus_globals->config.config_disabled_functions_reg_ret); | ||
| 107 | SP_INIT(snuffleupagus_globals->config.config_cookie); | ||
| 108 | SP_INIT(snuffleupagus_globals->config.config_session); | ||
| 109 | SP_INIT(snuffleupagus_globals->config.config_eval); | ||
| 110 | SP_INIT(snuffleupagus_globals->config.config_wrapper); | ||
| 111 | |||
| 112 | snuffleupagus_globals->config.config_disabled_functions_reg | ||
| 113 | ->disabled_functions = NULL; | ||
| 114 | snuffleupagus_globals->config.config_disabled_functions_reg_ret | ||
| 115 | ->disabled_functions = NULL; | ||
| 116 | snuffleupagus_globals->config.config_cookie->cookies = NULL; | ||
| 117 | snuffleupagus_globals->config.config_eval->blacklist = NULL; | ||
| 118 | snuffleupagus_globals->config.config_eval->whitelist = NULL; | ||
| 119 | snuffleupagus_globals->config.config_wrapper->whitelist = NULL; | ||
| 120 | 87 | ||
| 88 | #define SP_INIT(F) snuffleupagus_globals->config.F = \ | ||
| 89 | pecalloc(sizeof(*(snuffleupagus_globals->config.F)), 1, 1); | ||
| 90 | SP_INIT(config_unserialize); | ||
| 91 | SP_INIT(config_random); | ||
| 92 | SP_INIT(config_sloppy); | ||
| 93 | SP_INIT(config_readonly_exec); | ||
| 94 | SP_INIT(config_global_strict); | ||
| 95 | SP_INIT(config_auto_cookie_secure); | ||
| 96 | SP_INIT(config_snuffleupagus); | ||
| 97 | SP_INIT(config_disable_xxe); | ||
| 98 | SP_INIT(config_upload_validation); | ||
| 99 | SP_INIT(config_disabled_functions_reg); | ||
| 100 | SP_INIT(config_disabled_functions_reg_ret); | ||
| 101 | SP_INIT(config_cookie); | ||
| 102 | SP_INIT(config_session); | ||
| 103 | SP_INIT(config_eval); | ||
| 104 | SP_INIT(config_wrapper); | ||
| 121 | #undef SP_INIT | 105 | #undef SP_INIT |
| 122 | #undef SP_INIT_HT | 106 | |
| 107 | #define SP_INIT_NULL(F) snuffleupagus_globals->config.F = NULL; | ||
| 108 | SP_INIT_NULL(config_disabled_functions_reg->disabled_functions); | ||
| 109 | SP_INIT_NULL(config_disabled_functions_reg_ret->disabled_functions); | ||
| 110 | SP_INIT_NULL(config_cookie->cookies); | ||
| 111 | SP_INIT_NULL(config_eval->blacklist); | ||
| 112 | SP_INIT_NULL(config_eval->whitelist); | ||
| 113 | SP_INIT_NULL(config_wrapper->whitelist); | ||
| 114 | #undef SP_INIT_NULL | ||
| 123 | } | 115 | } |
| 124 | 116 | ||
| 125 | PHP_MINIT_FUNCTION(snuffleupagus) { | 117 | PHP_MINIT_FUNCTION(snuffleupagus) { |
| @@ -128,60 +120,62 @@ PHP_MINIT_FUNCTION(snuffleupagus) { | |||
| 128 | return SUCCESS; | 120 | return SUCCESS; |
| 129 | } | 121 | } |
| 130 | 122 | ||
| 123 | static void free_disabled_functions_hashtable(HashTable *ht) { | ||
| 124 | void *ptr = NULL; | ||
| 125 | ZEND_HASH_FOREACH_PTR(ht, ptr) { sp_list_free(ptr); } | ||
| 126 | ZEND_HASH_FOREACH_END(); | ||
| 127 | } | ||
| 128 | |||
| 131 | PHP_MSHUTDOWN_FUNCTION(snuffleupagus) { | 129 | PHP_MSHUTDOWN_FUNCTION(snuffleupagus) { |
| 132 | free_disabled_functions_hashtable( | ||
| 133 | SNUFFLEUPAGUS_G(config).config_disabled_functions); | ||
| 134 | free_disabled_functions_hashtable( | ||
| 135 | SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked); | ||
| 136 | free_disabled_functions_hashtable( | ||
| 137 | SNUFFLEUPAGUS_G(config).config_disabled_functions_ret); | ||
| 138 | free_disabled_functions_hashtable( | ||
| 139 | SNUFFLEUPAGUS_G(config).config_disabled_functions_ret_hooked); | ||
| 140 | 130 | ||
| 141 | #define FREE_HT(F) \ | 131 | #define FREE_HT(F) \ |
| 142 | zend_hash_destroy(SNUFFLEUPAGUS_G(F)); \ | 132 | zend_hash_destroy(SNUFFLEUPAGUS_G(F)); \ |
| 143 | pefree(SNUFFLEUPAGUS_G(F), 1); | 133 | pefree(SNUFFLEUPAGUS_G(F), 1); |
| 144 | |||
| 145 | FREE_HT(disabled_functions_hook); | 134 | FREE_HT(disabled_functions_hook); |
| 146 | FREE_HT(sp_eval_blacklist_functions_hook); | 135 | FREE_HT(sp_eval_blacklist_functions_hook); |
| 147 | FREE_HT(config.config_disabled_functions); | ||
| 148 | FREE_HT(config.config_disabled_functions_hooked); | ||
| 149 | FREE_HT(config.config_disabled_functions_ret); | ||
| 150 | FREE_HT(config.config_disabled_functions_ret_hooked); | ||
| 151 | 136 | ||
| 137 | #define FREE_HT_LIST(F) \ | ||
| 138 | free_disabled_functions_hashtable(SNUFFLEUPAGUS_G(config).F); \ | ||
| 139 | FREE_HT(config.F); | ||
| 140 | FREE_HT_LIST(config_disabled_functions); | ||
| 141 | FREE_HT_LIST(config_disabled_functions_hooked); | ||
| 142 | FREE_HT_LIST(config_disabled_functions_ret); | ||
| 143 | FREE_HT_LIST(config_disabled_functions_ret_hooked); | ||
| 144 | #undef FREE_HT_LIST | ||
| 152 | #undef FREE_HT | 145 | #undef FREE_HT |
| 153 | 146 | ||
| 154 | pefree(SNUFFLEUPAGUS_G(config.config_unserialize), 1); | ||
| 155 | pefree(SNUFFLEUPAGUS_G(config.config_random), 1); | ||
| 156 | pefree(SNUFFLEUPAGUS_G(config.config_readonly_exec), 1); | ||
| 157 | pefree(SNUFFLEUPAGUS_G(config.config_global_strict), 1); | ||
| 158 | pefree(SNUFFLEUPAGUS_G(config.config_auto_cookie_secure), 1); | ||
| 159 | pefree(SNUFFLEUPAGUS_G(config.config_snuffleupagus), 1); | ||
| 160 | pefree(SNUFFLEUPAGUS_G(config.config_disable_xxe), 1); | ||
| 161 | pefree(SNUFFLEUPAGUS_G(config.config_upload_validation), 1); | ||
| 162 | pefree(SNUFFLEUPAGUS_G(config.config_session), 1); | ||
| 163 | |||
| 164 | #define FREE_LST_DISABLE(L) \ | 147 | #define FREE_LST_DISABLE(L) \ |
| 165 | do { \ | 148 | do { \ |
| 166 | sp_list_node *_n = SNUFFLEUPAGUS_G(L); \ | 149 | sp_list_node *_n = SNUFFLEUPAGUS_G(config).L; \ |
| 167 | sp_disabled_function_list_free(_n); \ | 150 | sp_disabled_function_list_free(_n); \ |
| 168 | sp_list_free(_n); \ | 151 | sp_list_free(_n); \ |
| 169 | } while (0) | 152 | } while (0) |
| 170 | 153 | FREE_LST_DISABLE(config_disabled_functions_reg->disabled_functions); | |
| 171 | FREE_LST_DISABLE(config.config_disabled_functions_reg->disabled_functions); | 154 | FREE_LST_DISABLE(config_disabled_functions_reg_ret->disabled_functions); |
| 172 | FREE_LST_DISABLE( | ||
| 173 | config.config_disabled_functions_reg_ret->disabled_functions); | ||
| 174 | sp_list_free(SNUFFLEUPAGUS_G(config).config_cookie->cookies); | ||
| 175 | sp_list_free(SNUFFLEUPAGUS_G(config).config_eval->blacklist); | ||
| 176 | sp_list_free(SNUFFLEUPAGUS_G(config).config_eval->whitelist); | ||
| 177 | sp_list_free(SNUFFLEUPAGUS_G(config).config_wrapper->whitelist); | ||
| 178 | |||
| 179 | #undef FREE_LST_DISABLE | 155 | #undef FREE_LST_DISABLE |
| 180 | 156 | ||
| 181 | pefree(SNUFFLEUPAGUS_G(config.config_disabled_functions_reg), 1); | 157 | #define FREE_LST(L) sp_list_free(SNUFFLEUPAGUS_G(config).L); |
| 182 | pefree(SNUFFLEUPAGUS_G(config.config_disabled_functions_reg_ret), 1); | 158 | FREE_LST(config_cookie->cookies); |
| 183 | pefree(SNUFFLEUPAGUS_G(config.config_cookie), 1); | 159 | FREE_LST(config_eval->blacklist); |
| 184 | pefree(SNUFFLEUPAGUS_G(config.config_wrapper), 1); | 160 | FREE_LST(config_eval->whitelist); |
| 161 | FREE_LST(config_wrapper->whitelist); | ||
| 162 | #undef FREE_LST | ||
| 163 | |||
| 164 | #define FREE_CFG(C) pefree(SNUFFLEUPAGUS_G(config).C, 1); | ||
| 165 | FREE_CFG(config_unserialize); | ||
| 166 | FREE_CFG(config_random); | ||
| 167 | FREE_CFG(config_readonly_exec); | ||
| 168 | FREE_CFG(config_global_strict); | ||
| 169 | FREE_CFG(config_auto_cookie_secure); | ||
| 170 | FREE_CFG(config_snuffleupagus); | ||
| 171 | FREE_CFG(config_disable_xxe); | ||
| 172 | FREE_CFG(config_upload_validation); | ||
| 173 | FREE_CFG(config_session); | ||
| 174 | FREE_CFG(config_disabled_functions_reg); | ||
| 175 | FREE_CFG(config_disabled_functions_reg_ret); | ||
| 176 | FREE_CFG(config_cookie); | ||
| 177 | FREE_CFG(config_wrapper); | ||
| 178 | #undef FREE_CFG | ||
| 185 | 179 | ||
| 186 | UNREGISTER_INI_ENTRIES(); | 180 | UNREGISTER_INI_ENTRIES(); |
| 187 | 181 | ||
| @@ -189,6 +183,8 @@ PHP_MSHUTDOWN_FUNCTION(snuffleupagus) { | |||
| 189 | } | 183 | } |
| 190 | 184 | ||
| 191 | PHP_RINIT_FUNCTION(snuffleupagus) { | 185 | PHP_RINIT_FUNCTION(snuffleupagus) { |
| 186 | const sp_config_wrapper* config_wrapper = | ||
| 187 | SNUFFLEUPAGUS_G(config).config_wrapper; | ||
| 192 | #if defined(COMPILE_DL_SNUFFLEUPAGUS) && defined(ZTS) | 188 | #if defined(COMPILE_DL_SNUFFLEUPAGUS) && defined(ZTS) |
| 193 | ZEND_TSRMLS_CACHE_UPDATE(); | 189 | ZEND_TSRMLS_CACHE_UPDATE(); |
| 194 | #endif | 190 | #endif |
| @@ -198,9 +194,9 @@ PHP_RINIT_FUNCTION(snuffleupagus) { | |||
| 198 | } | 194 | } |
| 199 | 195 | ||
| 200 | // We need to disable wrappers loaded by extensions loaded after SNUFFLEUPAGUS. | 196 | // We need to disable wrappers loaded by extensions loaded after SNUFFLEUPAGUS. |
| 201 | if (SNUFFLEUPAGUS_G(config).config_wrapper->enabled && | 197 | if (config_wrapper->enabled && |
| 202 | zend_hash_num_elements(php_stream_get_url_stream_wrappers_hash()) != | 198 | zend_hash_num_elements(php_stream_get_url_stream_wrappers_hash()) != |
| 203 | SNUFFLEUPAGUS_G(config).config_wrapper->num_wrapper) { | 199 | config_wrapper->num_wrapper) { |
| 204 | sp_disable_wrapper(); | 200 | sp_disable_wrapper(); |
| 205 | } | 201 | } |
| 206 | 202 | ||
| @@ -265,28 +261,32 @@ static PHP_INI_MH(OnUpdateConfiguration) { | |||
| 265 | if (SNUFFLEUPAGUS_G(config).config_random->enable) { | 261 | if (SNUFFLEUPAGUS_G(config).config_random->enable) { |
| 266 | hook_rand(); | 262 | hook_rand(); |
| 267 | } | 263 | } |
| 264 | |||
| 268 | if (SNUFFLEUPAGUS_G(config).config_upload_validation->enable) { | 265 | if (SNUFFLEUPAGUS_G(config).config_upload_validation->enable) { |
| 269 | hook_upload(); | 266 | hook_upload(); |
| 270 | } | 267 | } |
| 268 | |||
| 271 | if (SNUFFLEUPAGUS_G(config).config_disable_xxe->enable == 0) { | 269 | if (SNUFFLEUPAGUS_G(config).config_disable_xxe->enable == 0) { |
| 272 | hook_libxml_disable_entity_loader(); | 270 | hook_libxml_disable_entity_loader(); |
| 273 | } | 271 | } |
| 272 | |||
| 274 | if (SNUFFLEUPAGUS_G(config).config_wrapper->enabled) { | 273 | if (SNUFFLEUPAGUS_G(config).config_wrapper->enabled) { |
| 275 | hook_stream_wrappers(); | 274 | hook_stream_wrappers(); |
| 276 | } | 275 | } |
| 277 | hook_disabled_functions(); | 276 | |
| 278 | hook_execute(); | 277 | if (SNUFFLEUPAGUS_G(config).config_session->encrypt) { |
| 278 | hook_session(); | ||
| 279 | } | ||
| 279 | 280 | ||
| 280 | if (NULL != SNUFFLEUPAGUS_G(config).config_snuffleupagus->encryption_key) { | 281 | if (NULL != SNUFFLEUPAGUS_G(config).config_snuffleupagus->encryption_key) { |
| 281 | if (SNUFFLEUPAGUS_G(config).config_unserialize->enable) { | 282 | if (SNUFFLEUPAGUS_G(config).config_unserialize->enable) { |
| 282 | hook_serialize(); | 283 | hook_serialize(); |
| 283 | } | 284 | } |
| 284 | } | 285 | } |
| 285 | hook_cookies(); | ||
| 286 | 286 | ||
| 287 | if (SNUFFLEUPAGUS_G(config).config_session->encrypt) { | 287 | hook_disabled_functions(); |
| 288 | hook_session(); | 288 | hook_execute(); |
| 289 | } | 289 | hook_cookies(); |
| 290 | 290 | ||
| 291 | if (true == SNUFFLEUPAGUS_G(config).config_global_strict->enable) { | 291 | if (true == SNUFFLEUPAGUS_G(config).config_global_strict->enable) { |
| 292 | if (!zend_get_extension(PHP_SNUFFLEUPAGUS_EXTNAME)) { | 292 | if (!zend_get_extension(PHP_SNUFFLEUPAGUS_EXTNAME)) { |
| @@ -300,10 +300,10 @@ static PHP_INI_MH(OnUpdateConfiguration) { | |||
| 300 | // If `zend_write_default` is not NULL it is already hooked. | 300 | // If `zend_write_default` is not NULL it is already hooked. |
| 301 | if ((zend_hash_str_find( | 301 | if ((zend_hash_str_find( |
| 302 | SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked, "echo", | 302 | SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked, "echo", |
| 303 | strlen("echo")) || | 303 | sizeof("echo") - 1) || |
| 304 | zend_hash_str_find( | 304 | zend_hash_str_find( |
| 305 | SNUFFLEUPAGUS_G(config).config_disabled_functions_ret_hooked, "echo", | 305 | SNUFFLEUPAGUS_G(config).config_disabled_functions_ret_hooked, "echo", |
| 306 | strlen("echo"))) && NULL == zend_write_default) { | 306 | sizeof("echo") - 1)) && NULL == zend_write_default) { |
| 307 | zend_write_default = zend_write; | 307 | zend_write_default = zend_write; |
| 308 | zend_write = hook_echo; | 308 | zend_write = hook_echo; |
| 309 | } | 309 | } |
