diff options
| author | xXx-caillou-xXx | 2018-07-13 10:36:50 +0200 |
|---|---|---|
| committer | jvoisin | 2018-07-13 08:36:50 +0000 |
| commit | 7963580d72a358975133f86f01de2d2eab08ba38 (patch) | |
| tree | 4bec345d70f687a2a6002b36e2f2fc79318959f6 /src/snuffleupagus.c | |
| parent | 12b740bc7bb01ffe397cecc5b6fa25b136304911 (diff) | |
Massively optimize how rules are handled
This commit does a lot of things:
- Use hashtables instead of lists to store the rules
- Rules that can be applied at launch time won't be tried at runtime
- Improve feedback when writing nonsensical rules
- Make intensive use of `zend_string` instead of `char*`
Diffstat (limited to 'src/snuffleupagus.c')
| -rw-r--r-- | src/snuffleupagus.c | 53 |
1 files changed, 36 insertions, 17 deletions
diff --git a/src/snuffleupagus.c b/src/snuffleupagus.c index 08b2083..edca185 100644 --- a/src/snuffleupagus.c +++ b/src/snuffleupagus.c | |||
| @@ -39,6 +39,14 @@ PHP_INI_ENTRY("sp.configuration_file", "", PHP_INI_SYSTEM, | |||
| 39 | OnUpdateConfiguration) | 39 | OnUpdateConfiguration) |
| 40 | PHP_INI_END() | 40 | PHP_INI_END() |
| 41 | 41 | ||
| 42 | void free_disabled_functions_hashtable(HashTable *ht) { | ||
| 43 | void* ptr = NULL; | ||
| 44 | ZEND_HASH_FOREACH_PTR(ht, ptr) { | ||
| 45 | sp_list_free(ptr); | ||
| 46 | } | ||
| 47 | ZEND_HASH_FOREACH_END(); | ||
| 48 | } | ||
| 49 | |||
| 42 | ZEND_DLEXPORT zend_extension zend_extension_entry = { | 50 | ZEND_DLEXPORT zend_extension zend_extension_entry = { |
| 43 | PHP_SNUFFLEUPAGUS_EXTNAME, | 51 | PHP_SNUFFLEUPAGUS_EXTNAME, |
| 44 | PHP_SNUFFLEUPAGUS_VERSION, | 52 | PHP_SNUFFLEUPAGUS_VERSION, |
| @@ -69,6 +77,10 @@ PHP_GINIT_FUNCTION(snuffleupagus) { | |||
| 69 | SP_INIT_HT(snuffleupagus_globals->disabled_functions_hook); | 77 | SP_INIT_HT(snuffleupagus_globals->disabled_functions_hook); |
| 70 | SP_INIT_HT(snuffleupagus_globals->sp_internal_functions_hook); | 78 | SP_INIT_HT(snuffleupagus_globals->sp_internal_functions_hook); |
| 71 | SP_INIT_HT(snuffleupagus_globals->sp_eval_blacklist_functions_hook); | 79 | SP_INIT_HT(snuffleupagus_globals->sp_eval_blacklist_functions_hook); |
| 80 | SP_INIT_HT(snuffleupagus_globals->config.config_disabled_functions); | ||
| 81 | SP_INIT_HT(snuffleupagus_globals->config.config_disabled_functions_hooked); | ||
| 82 | SP_INIT_HT(snuffleupagus_globals->config.config_disabled_functions_ret); | ||
| 83 | SP_INIT_HT(snuffleupagus_globals->config.config_disabled_functions_ret_hooked); | ||
| 72 | 84 | ||
| 73 | SP_INIT(snuffleupagus_globals->config.config_unserialize); | 85 | SP_INIT(snuffleupagus_globals->config.config_unserialize); |
| 74 | SP_INIT(snuffleupagus_globals->config.config_random); | 86 | SP_INIT(snuffleupagus_globals->config.config_random); |
| @@ -79,20 +91,15 @@ PHP_GINIT_FUNCTION(snuffleupagus) { | |||
| 79 | SP_INIT(snuffleupagus_globals->config.config_snuffleupagus); | 91 | SP_INIT(snuffleupagus_globals->config.config_snuffleupagus); |
| 80 | SP_INIT(snuffleupagus_globals->config.config_disable_xxe); | 92 | SP_INIT(snuffleupagus_globals->config.config_disable_xxe); |
| 81 | SP_INIT(snuffleupagus_globals->config.config_upload_validation); | 93 | SP_INIT(snuffleupagus_globals->config.config_upload_validation); |
| 82 | SP_INIT(snuffleupagus_globals->config.config_disabled_functions); | 94 | SP_INIT(snuffleupagus_globals->config.config_disabled_functions_reg); |
| 83 | SP_INIT(snuffleupagus_globals->config.config_disabled_functions_ret); | 95 | SP_INIT(snuffleupagus_globals->config.config_disabled_functions_reg_ret); |
| 84 | SP_INIT(snuffleupagus_globals->config.config_cookie); | 96 | SP_INIT(snuffleupagus_globals->config.config_cookie); |
| 85 | SP_INIT(snuffleupagus_globals->config.config_session); | 97 | SP_INIT(snuffleupagus_globals->config.config_session); |
| 86 | SP_INIT(snuffleupagus_globals->config.config_disabled_constructs); | ||
| 87 | SP_INIT(snuffleupagus_globals->config.config_eval); | 98 | SP_INIT(snuffleupagus_globals->config.config_eval); |
| 88 | 99 | ||
| 89 | snuffleupagus_globals->config.config_disabled_constructs->construct_include = | 100 | snuffleupagus_globals->config.config_disabled_functions_reg |
| 90 | NULL; | 101 | ->disabled_functions = NULL; |
| 91 | snuffleupagus_globals->config.config_disabled_constructs->construct_eval = | 102 | snuffleupagus_globals->config.config_disabled_functions_reg_ret |
| 92 | NULL; | ||
| 93 | snuffleupagus_globals->config.config_disabled_functions->disabled_functions = | ||
| 94 | NULL; | ||
| 95 | snuffleupagus_globals->config.config_disabled_functions_ret | ||
| 96 | ->disabled_functions = NULL; | 103 | ->disabled_functions = NULL; |
| 97 | snuffleupagus_globals->config.config_cookie->cookies = NULL; | 104 | snuffleupagus_globals->config.config_cookie->cookies = NULL; |
| 98 | snuffleupagus_globals->config.config_eval->blacklist = NULL; | 105 | snuffleupagus_globals->config.config_eval->blacklist = NULL; |
| @@ -109,12 +116,21 @@ PHP_MINIT_FUNCTION(snuffleupagus) { | |||
| 109 | } | 116 | } |
| 110 | 117 | ||
| 111 | PHP_MSHUTDOWN_FUNCTION(snuffleupagus) { | 118 | PHP_MSHUTDOWN_FUNCTION(snuffleupagus) { |
| 119 | free_disabled_functions_hashtable(SNUFFLEUPAGUS_G(config).config_disabled_functions); | ||
| 120 | free_disabled_functions_hashtable(SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked); | ||
| 121 | free_disabled_functions_hashtable(SNUFFLEUPAGUS_G(config).config_disabled_functions_ret); | ||
| 122 | free_disabled_functions_hashtable(SNUFFLEUPAGUS_G(config).config_disabled_functions_ret_hooked); | ||
| 123 | |||
| 112 | #define FREE_HT(F) \ | 124 | #define FREE_HT(F) \ |
| 113 | zend_hash_destroy(SNUFFLEUPAGUS_G(F)); \ | 125 | zend_hash_destroy(SNUFFLEUPAGUS_G(F)); \ |
| 114 | pefree(SNUFFLEUPAGUS_G(F), 1); | 126 | pefree(SNUFFLEUPAGUS_G(F), 1); |
| 115 | 127 | ||
| 116 | FREE_HT(disabled_functions_hook); | 128 | FREE_HT(disabled_functions_hook); |
| 117 | FREE_HT(sp_eval_blacklist_functions_hook); | 129 | FREE_HT(sp_eval_blacklist_functions_hook); |
| 130 | FREE_HT(config.config_disabled_functions); | ||
| 131 | FREE_HT(config.config_disabled_functions_hooked); | ||
| 132 | FREE_HT(config.config_disabled_functions_ret); | ||
| 133 | FREE_HT(config.config_disabled_functions_ret_hooked); | ||
| 118 | 134 | ||
| 119 | #undef FREE_HT | 135 | #undef FREE_HT |
| 120 | 136 | ||
| @@ -135,19 +151,16 @@ PHP_MSHUTDOWN_FUNCTION(snuffleupagus) { | |||
| 135 | sp_list_free(_n); \ | 151 | sp_list_free(_n); \ |
| 136 | } while (0) | 152 | } while (0) |
| 137 | 153 | ||
| 138 | FREE_LST_DISABLE(config.config_disabled_functions->disabled_functions); | 154 | FREE_LST_DISABLE(config.config_disabled_functions_reg->disabled_functions); |
| 139 | FREE_LST_DISABLE(config.config_disabled_functions_ret->disabled_functions); | 155 | FREE_LST_DISABLE(config.config_disabled_functions_reg_ret->disabled_functions); |
| 140 | FREE_LST_DISABLE(config.config_disabled_constructs->construct_include); | ||
| 141 | FREE_LST_DISABLE(config.config_disabled_constructs->construct_eval); | ||
| 142 | sp_list_free(SNUFFLEUPAGUS_G(config).config_cookie->cookies); | 156 | sp_list_free(SNUFFLEUPAGUS_G(config).config_cookie->cookies); |
| 143 | sp_list_free(SNUFFLEUPAGUS_G(config).config_eval->blacklist); | 157 | sp_list_free(SNUFFLEUPAGUS_G(config).config_eval->blacklist); |
| 144 | sp_list_free(SNUFFLEUPAGUS_G(config).config_eval->whitelist); | 158 | sp_list_free(SNUFFLEUPAGUS_G(config).config_eval->whitelist); |
| 145 | 159 | ||
| 146 | #undef FREE_LST_DISABLE | 160 | #undef FREE_LST_DISABLE |
| 147 | 161 | ||
| 148 | pefree(SNUFFLEUPAGUS_G(config.config_disabled_functions), 1); | 162 | pefree(SNUFFLEUPAGUS_G(config.config_disabled_functions_reg), 1); |
| 149 | pefree(SNUFFLEUPAGUS_G(config.config_disabled_functions_ret), 1); | 163 | pefree(SNUFFLEUPAGUS_G(config.config_disabled_functions_reg_ret), 1); |
| 150 | pefree(SNUFFLEUPAGUS_G(config.config_disabled_constructs), 1); | ||
| 151 | pefree(SNUFFLEUPAGUS_G(config.config_cookie), 1); | 164 | pefree(SNUFFLEUPAGUS_G(config.config_cookie), 1); |
| 152 | 165 | ||
| 153 | UNREGISTER_INI_ENTRIES(); | 166 | UNREGISTER_INI_ENTRIES(); |
| @@ -249,6 +262,12 @@ static PHP_INI_MH(OnUpdateConfiguration) { | |||
| 249 | CG(compiler_options) |= ZEND_COMPILE_HANDLE_OP_ARRAY; | 262 | CG(compiler_options) |= ZEND_COMPILE_HANDLE_OP_ARRAY; |
| 250 | } | 263 | } |
| 251 | 264 | ||
| 265 | SNUFFLEUPAGUS_G(config).hook_execute = | ||
| 266 | SNUFFLEUPAGUS_G(config).config_disabled_functions_reg->disabled_functions || | ||
| 267 | SNUFFLEUPAGUS_G(config).config_disabled_functions_reg_ret->disabled_functions || | ||
| 268 | zend_hash_num_elements(SNUFFLEUPAGUS_G(config).config_disabled_functions) || | ||
| 269 | zend_hash_num_elements(SNUFFLEUPAGUS_G(config).config_disabled_functions_ret); | ||
| 270 | |||
| 252 | return SUCCESS; | 271 | return SUCCESS; |
| 253 | } | 272 | } |
| 254 | 273 | ||
