Skip to content

Commit

Permalink
Minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
xXx-caillou-xXx authored and jvoisin committed Aug 30, 2018
1 parent b3f67a1 commit 206ffa3
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 189 deletions.
196 changes: 98 additions & 98 deletions src/snuffleupagus.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ PHP_INI_ENTRY("sp.allow_broken_configuration", "0", PHP_INI_SYSTEM,
StrictMode)
PHP_INI_END()

void free_disabled_functions_hashtable(HashTable *ht) {
void *ptr = NULL;
ZEND_HASH_FOREACH_PTR(ht, ptr) { sp_list_free(ptr); }
ZEND_HASH_FOREACH_END();
}

ZEND_DLEXPORT zend_extension zend_extension_entry = {
PHP_SNUFFLEUPAGUS_EXTNAME,
PHP_SNUFFLEUPAGUS_VERSION,
Expand All @@ -79,47 +73,45 @@ ZEND_DLEXPORT zend_extension zend_extension_entry = {
PHP_GINIT_FUNCTION(snuffleupagus) {
snuffleupagus_globals->in_eval = 0;

#define SP_INIT(F) F = pecalloc(sizeof(*F), 1, 1);
#define SP_INIT_HT(F) \
F = pemalloc(sizeof(*F), 1); \
zend_hash_init(F, 10, NULL, NULL, 1);

SP_INIT_HT(snuffleupagus_globals->disabled_functions_hook);
SP_INIT_HT(snuffleupagus_globals->sp_internal_functions_hook);
SP_INIT_HT(snuffleupagus_globals->sp_eval_blacklist_functions_hook);
SP_INIT_HT(snuffleupagus_globals->config.config_disabled_functions);
SP_INIT_HT(snuffleupagus_globals->config.config_disabled_functions_hooked);
SP_INIT_HT(snuffleupagus_globals->config.config_disabled_functions_ret);
SP_INIT_HT(
snuffleupagus_globals->config.config_disabled_functions_ret_hooked);

SP_INIT(snuffleupagus_globals->config.config_unserialize);
SP_INIT(snuffleupagus_globals->config.config_random);
SP_INIT(snuffleupagus_globals->config.config_sloppy);
SP_INIT(snuffleupagus_globals->config.config_readonly_exec);
SP_INIT(snuffleupagus_globals->config.config_global_strict);
SP_INIT(snuffleupagus_globals->config.config_auto_cookie_secure);
SP_INIT(snuffleupagus_globals->config.config_snuffleupagus);
SP_INIT(snuffleupagus_globals->config.config_disable_xxe);
SP_INIT(snuffleupagus_globals->config.config_upload_validation);
SP_INIT(snuffleupagus_globals->config.config_disabled_functions_reg);
SP_INIT(snuffleupagus_globals->config.config_disabled_functions_reg_ret);
SP_INIT(snuffleupagus_globals->config.config_cookie);
SP_INIT(snuffleupagus_globals->config.config_session);
SP_INIT(snuffleupagus_globals->config.config_eval);
SP_INIT(snuffleupagus_globals->config.config_wrapper);

snuffleupagus_globals->config.config_disabled_functions_reg
->disabled_functions = NULL;
snuffleupagus_globals->config.config_disabled_functions_reg_ret
->disabled_functions = NULL;
snuffleupagus_globals->config.config_cookie->cookies = NULL;
snuffleupagus_globals->config.config_eval->blacklist = NULL;
snuffleupagus_globals->config.config_eval->whitelist = NULL;
snuffleupagus_globals->config.config_wrapper->whitelist = NULL;
#define SP_INIT_HT(F) snuffleupagus_globals->F = \
pemalloc(sizeof(*(snuffleupagus_globals->F)), 1); \
zend_hash_init(snuffleupagus_globals->F, 10, NULL, NULL, 1);
SP_INIT_HT(disabled_functions_hook);
SP_INIT_HT(sp_internal_functions_hook);
SP_INIT_HT(sp_eval_blacklist_functions_hook);
SP_INIT_HT(config.config_disabled_functions);
SP_INIT_HT(config.config_disabled_functions_hooked);
SP_INIT_HT(config.config_disabled_functions_ret);
SP_INIT_HT(config.config_disabled_functions_ret_hooked);
#undef SP_INIT_HT

#define SP_INIT(F) snuffleupagus_globals->config.F = \
pecalloc(sizeof(*(snuffleupagus_globals->config.F)), 1, 1);
SP_INIT(config_unserialize);
SP_INIT(config_random);
SP_INIT(config_sloppy);
SP_INIT(config_readonly_exec);
SP_INIT(config_global_strict);
SP_INIT(config_auto_cookie_secure);
SP_INIT(config_snuffleupagus);
SP_INIT(config_disable_xxe);
SP_INIT(config_upload_validation);
SP_INIT(config_disabled_functions_reg);
SP_INIT(config_disabled_functions_reg_ret);
SP_INIT(config_cookie);
SP_INIT(config_session);
SP_INIT(config_eval);
SP_INIT(config_wrapper);
#undef SP_INIT
#undef SP_INIT_HT

#define SP_INIT_NULL(F) snuffleupagus_globals->config.F = NULL;
SP_INIT_NULL(config_disabled_functions_reg->disabled_functions);
SP_INIT_NULL(config_disabled_functions_reg_ret->disabled_functions);
SP_INIT_NULL(config_cookie->cookies);
SP_INIT_NULL(config_eval->blacklist);
SP_INIT_NULL(config_eval->whitelist);
SP_INIT_NULL(config_wrapper->whitelist);
#undef SP_INIT_NULL
}

PHP_MINIT_FUNCTION(snuffleupagus) {
Expand All @@ -128,67 +120,71 @@ PHP_MINIT_FUNCTION(snuffleupagus) {
return SUCCESS;
}

static void free_disabled_functions_hashtable(HashTable *ht) {
void *ptr = NULL;
ZEND_HASH_FOREACH_PTR(ht, ptr) { sp_list_free(ptr); }
ZEND_HASH_FOREACH_END();
}

PHP_MSHUTDOWN_FUNCTION(snuffleupagus) {
free_disabled_functions_hashtable(
SNUFFLEUPAGUS_G(config).config_disabled_functions);
free_disabled_functions_hashtable(
SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked);
free_disabled_functions_hashtable(
SNUFFLEUPAGUS_G(config).config_disabled_functions_ret);
free_disabled_functions_hashtable(
SNUFFLEUPAGUS_G(config).config_disabled_functions_ret_hooked);

#define FREE_HT(F) \
zend_hash_destroy(SNUFFLEUPAGUS_G(F)); \
pefree(SNUFFLEUPAGUS_G(F), 1);

zend_hash_destroy(SNUFFLEUPAGUS_G(F)); \
pefree(SNUFFLEUPAGUS_G(F), 1);
FREE_HT(disabled_functions_hook);
FREE_HT(sp_eval_blacklist_functions_hook);
FREE_HT(config.config_disabled_functions);
FREE_HT(config.config_disabled_functions_hooked);
FREE_HT(config.config_disabled_functions_ret);
FREE_HT(config.config_disabled_functions_ret_hooked);

#define FREE_HT_LIST(F) \
free_disabled_functions_hashtable(SNUFFLEUPAGUS_G(config).F); \
FREE_HT(config.F);
FREE_HT_LIST(config_disabled_functions);
FREE_HT_LIST(config_disabled_functions_hooked);
FREE_HT_LIST(config_disabled_functions_ret);
FREE_HT_LIST(config_disabled_functions_ret_hooked);
#undef FREE_HT_LIST
#undef FREE_HT

pefree(SNUFFLEUPAGUS_G(config.config_unserialize), 1);
pefree(SNUFFLEUPAGUS_G(config.config_random), 1);
pefree(SNUFFLEUPAGUS_G(config.config_readonly_exec), 1);
pefree(SNUFFLEUPAGUS_G(config.config_global_strict), 1);
pefree(SNUFFLEUPAGUS_G(config.config_auto_cookie_secure), 1);
pefree(SNUFFLEUPAGUS_G(config.config_snuffleupagus), 1);
pefree(SNUFFLEUPAGUS_G(config.config_disable_xxe), 1);
pefree(SNUFFLEUPAGUS_G(config.config_upload_validation), 1);
pefree(SNUFFLEUPAGUS_G(config.config_session), 1);

#define FREE_LST_DISABLE(L) \
do { \
sp_list_node *_n = SNUFFLEUPAGUS_G(L); \
sp_disabled_function_list_free(_n); \
sp_list_free(_n); \
} while (0)

FREE_LST_DISABLE(config.config_disabled_functions_reg->disabled_functions);
FREE_LST_DISABLE(
config.config_disabled_functions_reg_ret->disabled_functions);
sp_list_free(SNUFFLEUPAGUS_G(config).config_cookie->cookies);
sp_list_free(SNUFFLEUPAGUS_G(config).config_eval->blacklist);
sp_list_free(SNUFFLEUPAGUS_G(config).config_eval->whitelist);
sp_list_free(SNUFFLEUPAGUS_G(config).config_wrapper->whitelist);

do { \
sp_list_node *_n = SNUFFLEUPAGUS_G(config).L; \
sp_disabled_function_list_free(_n); \
sp_list_free(_n); \
} while (0)
FREE_LST_DISABLE(config_disabled_functions_reg->disabled_functions);
FREE_LST_DISABLE(config_disabled_functions_reg_ret->disabled_functions);
#undef FREE_LST_DISABLE

pefree(SNUFFLEUPAGUS_G(config.config_disabled_functions_reg), 1);
pefree(SNUFFLEUPAGUS_G(config.config_disabled_functions_reg_ret), 1);
pefree(SNUFFLEUPAGUS_G(config.config_cookie), 1);
pefree(SNUFFLEUPAGUS_G(config.config_wrapper), 1);
#define FREE_LST(L) sp_list_free(SNUFFLEUPAGUS_G(config).L);
FREE_LST(config_cookie->cookies);
FREE_LST(config_eval->blacklist);
FREE_LST(config_eval->whitelist);
FREE_LST(config_wrapper->whitelist);
#undef FREE_LST

#define FREE_CFG(C) pefree(SNUFFLEUPAGUS_G(config).C, 1);
FREE_CFG(config_unserialize);
FREE_CFG(config_random);
FREE_CFG(config_readonly_exec);
FREE_CFG(config_global_strict);
FREE_CFG(config_auto_cookie_secure);
FREE_CFG(config_snuffleupagus);
FREE_CFG(config_disable_xxe);
FREE_CFG(config_upload_validation);
FREE_CFG(config_session);
FREE_CFG(config_disabled_functions_reg);
FREE_CFG(config_disabled_functions_reg_ret);
FREE_CFG(config_cookie);
FREE_CFG(config_wrapper);
#undef FREE_CFG

UNREGISTER_INI_ENTRIES();

return SUCCESS;
}

PHP_RINIT_FUNCTION(snuffleupagus) {
const sp_config_wrapper* config_wrapper =
SNUFFLEUPAGUS_G(config).config_wrapper;
#if defined(COMPILE_DL_SNUFFLEUPAGUS) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
Expand All @@ -198,9 +194,9 @@ PHP_RINIT_FUNCTION(snuffleupagus) {
}

// We need to disable wrappers loaded by extensions loaded after SNUFFLEUPAGUS.
if (SNUFFLEUPAGUS_G(config).config_wrapper->enabled &&
if (config_wrapper->enabled &&
zend_hash_num_elements(php_stream_get_url_stream_wrappers_hash()) !=
SNUFFLEUPAGUS_G(config).config_wrapper->num_wrapper) {
config_wrapper->num_wrapper) {
sp_disable_wrapper();
}

Expand Down Expand Up @@ -265,28 +261,32 @@ static PHP_INI_MH(OnUpdateConfiguration) {
if (SNUFFLEUPAGUS_G(config).config_random->enable) {
hook_rand();
}

if (SNUFFLEUPAGUS_G(config).config_upload_validation->enable) {
hook_upload();
}

if (SNUFFLEUPAGUS_G(config).config_disable_xxe->enable == 0) {
hook_libxml_disable_entity_loader();
}

if (SNUFFLEUPAGUS_G(config).config_wrapper->enabled) {
hook_stream_wrappers();
}
hook_disabled_functions();
hook_execute();

if (SNUFFLEUPAGUS_G(config).config_session->encrypt) {
hook_session();
}

if (NULL != SNUFFLEUPAGUS_G(config).config_snuffleupagus->encryption_key) {
if (SNUFFLEUPAGUS_G(config).config_unserialize->enable) {
hook_serialize();
}
}
hook_cookies();

if (SNUFFLEUPAGUS_G(config).config_session->encrypt) {
hook_session();
}
hook_disabled_functions();
hook_execute();
hook_cookies();

if (true == SNUFFLEUPAGUS_G(config).config_global_strict->enable) {
if (!zend_get_extension(PHP_SNUFFLEUPAGUS_EXTNAME)) {
Expand All @@ -300,10 +300,10 @@ static PHP_INI_MH(OnUpdateConfiguration) {
// If `zend_write_default` is not NULL it is already hooked.
if ((zend_hash_str_find(
SNUFFLEUPAGUS_G(config).config_disabled_functions_hooked, "echo",
strlen("echo")) ||
sizeof("echo") - 1) ||
zend_hash_str_find(
SNUFFLEUPAGUS_G(config).config_disabled_functions_ret_hooked, "echo",
strlen("echo"))) && NULL == zend_write_default) {
sizeof("echo") - 1)) && NULL == zend_write_default) {
zend_write_default = zend_write;
zend_write = hook_echo;
}
Expand Down
3 changes: 1 addition & 2 deletions src/sp_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ int parse_php_type(char *restrict line, char *restrict keyword, void *retval) {
sp_log_err("error",
"%s) is expecting a valid php type ('false', 'true',"
" 'array'. 'object', 'long', 'double', 'null', 'resource', "
"'reference',"
" 'undef') on line %zu",
"'reference', 'undef') on line %zu",
keyword, sp_line_no);
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/sp_config_keywords.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ int parse_disabled_functions(char *line) {

if (df->function && zend_string_equals_literal(df->function, "print")) {
zend_string_release(df->function);
df->function = zend_string_init("echo", strlen("echo"), 1);
df->function = zend_string_init("echo", sizeof("echo") - 1, 1);
}

if (df->function && !df->functions_list) {
Expand Down
4 changes: 2 additions & 2 deletions src/sp_cookie_encryption.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ static zend_string *encrypt_data(zend_string *data) {
PHP_FUNCTION(sp_setcookie) {
zend_string *name = NULL, *value = NULL, *path = NULL, *domain = NULL, *value_enc = NULL,
#if PHP_VERSION_ID < 70300
*path_samesite = NULL;
*path_samesite = NULL;
#else
*samesite = NULL;
*samesite = NULL;
#endif

zend_long expires = 0;
Expand Down
3 changes: 1 addition & 2 deletions src/sp_crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ void generate_key(unsigned char *key) {
"cookie_encryption",
"The environment variable '%s' "
"is empty, cookies are weakly encrypted",
ZSTR_VAL(
SNUFFLEUPAGUS_G(config).config_snuffleupagus->cookies_env_var));
ZSTR_VAL(env_var_zend));
}

if (encryption_key) {
Expand Down
10 changes: 5 additions & 5 deletions src/sp_disabled_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,13 +580,13 @@ ZEND_FUNCTION(eval_blacklist_callback) {
if (SNUFFLEUPAGUS_G(in_eval) > 0) {
zend_string* filename = get_eval_filename(zend_get_executed_filename());
const int line_number = zend_get_executed_lineno(TSRMLS_C);
if (SNUFFLEUPAGUS_G(config).config_eval->dump) {
sp_log_request(
SNUFFLEUPAGUS_G(config).config_eval->dump,
SNUFFLEUPAGUS_G(config).config_eval->textual_representation,
const sp_config_eval* config_eval = SNUFFLEUPAGUS_G(config).config_eval;

if (config_eval->dump) {
sp_log_request(config_eval->dump, config_eval->textual_representation,
SP_TOKEN_EVAL_BLACKLIST);
}
if (SNUFFLEUPAGUS_G(config).config_eval->simulation) {
if (config_eval->simulation) {
sp_log_msg("eval", SP_LOG_SIMULATION,
"A call to %s was tried in eval, in %s:%d, logging it.",
current_function_name, ZSTR_VAL(filename), line_number);
Expand Down
Loading

0 comments on commit 206ffa3

Please sign in to comment.