From 3b6c6af3faa6a66e4f5337a769baed32f404b82b Mon Sep 17 00:00:00 2001 From: Stefan Esser Date: Sat, 14 Jan 2012 19:32:14 +0100 Subject: Use new suhosin_getenv() function in all places Add protection against mbstring Add detection of incompatible extensions that change POST handlers --- post_handler.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'post_handler.c') diff --git a/post_handler.c b/post_handler.c index c097a06..b405ae2 100644 --- a/post_handler.c +++ b/post_handler.c @@ -86,6 +86,40 @@ static void suhosin_post_handler_modification(sapi_post_entry *spe) efree(content_type); } +static int (*old_OnUpdate_mbstring_encoding_translation)(zend_ini_entry *entry, char *new_value, uint new_value_length, void *mh_arg1, void *mh_arg2, void *mh_arg3, int stage TSRMLS_DC) = NULL; + +/* {{{ static PHP_INI_MH(suhosin_OnUpdate_mbstring_encoding_translation) */ +static PHP_INI_MH(suhosin_OnUpdate_mbstring_encoding_translation) +{ + zend_bool *p; +#ifndef ZTS + char *base = (char *) mh_arg2; +#else + char *base; + + base = (char *) ts_resource(*((int *) mh_arg2)); +#endif + + p = (zend_bool *) (base+(size_t) mh_arg1); + + if (new_value_length == 2 && strcasecmp("on", new_value) == 0) { + *p = (zend_bool) 1; + } + else if (new_value_length == 3 && strcasecmp("yes", new_value) == 0) { + *p = (zend_bool) 1; + } + else if (new_value_length == 4 && strcasecmp("true", new_value) == 0) { + *p = (zend_bool) 1; + } + else { + *p = (zend_bool) atoi(new_value); + } + if (*p) { + suhosin_log(S_VARS, "Dynamic configuration (maybe a .htaccess file) tried to activate mbstring.encoding_translation which is incompatible with suhosin"); + } + return SUCCESS; +} +/* }}} */ /* {{{ php_post_entries[] */ @@ -99,6 +133,7 @@ static sapi_post_entry suhosin_post_entries[] = { void suhosin_hook_post_handlers(TSRMLS_D) { HashTable tempht; + zend_ini_entry *ini_entry; #if PHP_MAJOR_VERSION > 5 || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 0) sapi_unregister_post_entry(&suhosin_post_entries[0] TSRMLS_CC); @@ -117,12 +152,30 @@ void suhosin_hook_post_handlers(TSRMLS_D) zend_hash_destroy(&tempht); /* And now we can overwrite the destructor for post entries */ SG(known_post_content_types).pDestructor = suhosin_post_handler_modification; + + /* we have to stop mbstring from replacing our post handler */ + if (zend_hash_find(EG(ini_directives), "mbstring.encoding_translation", sizeof("mbstring.encoding_translation"), (void **) &ini_entry) == FAILURE) { + return; + } + /* replace OnUpdate_mbstring_encoding_translation handler */ + old_OnUpdate_mbstring_encoding_translation = ini_entry->on_modify; + ini_entry->on_modify = suhosin_OnUpdate_mbstring_encoding_translation; } void suhosin_unhook_post_handlers() { + zend_ini_entry *ini_entry; + /* Restore to an empty destructor */ SG(known_post_content_types).pDestructor = NULL; + + /* Now restore the ini entry handler */ + if (zend_hash_find(EG(ini_directives), "mbstring.encoding_translation", sizeof("mbstring.encoding_translation"), (void **) &ini_entry) == FAILURE) { + return; + } + /* replace OnUpdate_mbstring_encoding_translation handler */ + ini_entry->on_modify = old_OnUpdate_mbstring_encoding_translation; + old_OnUpdate_mbstring_encoding_translation = NULL; } /* -- cgit v1.3