diff options
Diffstat (limited to 'memory_limit.c')
| -rw-r--r-- | memory_limit.c | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/memory_limit.c b/memory_limit.c new file mode 100644 index 0000000..fa1683e --- /dev/null +++ b/memory_limit.c | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | /* | ||
| 2 | +----------------------------------------------------------------------+ | ||
| 3 | | Suhosin Version 1 | | ||
| 4 | +----------------------------------------------------------------------+ | ||
| 5 | | Copyright (c) 2006-2007 The Hardened-PHP Project | | ||
| 6 | | Copyright (c) 2007-2015 SektionEins GmbH | | ||
| 7 | +----------------------------------------------------------------------+ | ||
| 8 | | This source file is subject to version 3.01 of the PHP license, | | ||
| 9 | | that is bundled with this package in the file LICENSE, and is | | ||
| 10 | | available through the world-wide-web at the following url: | | ||
| 11 | | http://www.php.net/license/3_01.txt | | ||
| 12 | | If you did not receive a copy of the PHP license and are unable to | | ||
| 13 | | obtain it through the world-wide-web, please send a note to | | ||
| 14 | | license@php.net so we can mail you a copy immediately. | | ||
| 15 | +----------------------------------------------------------------------+ | ||
| 16 | | Author: Stefan Esser <sesser@sektioneins.de> | | ||
| 17 | +----------------------------------------------------------------------+ | ||
| 18 | */ | ||
| 19 | /* | ||
| 20 | $Id: memory_limit.c $ | ||
| 21 | */ | ||
| 22 | |||
| 23 | #ifdef HAVE_CONFIG_H | ||
| 24 | #include "config.h" | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #include "php.h" | ||
| 28 | #include "php_ini.h" | ||
| 29 | #include "ext/standard/info.h" | ||
| 30 | #include "php_suhosin7.h" | ||
| 31 | |||
| 32 | |||
| 33 | /* {{{ PHP_INI_MH | ||
| 34 | */ | ||
| 35 | static PHP_INI_MH(suhosin_OnChangeMemoryLimit) | ||
| 36 | { | ||
| 37 | #if SIZEOF_LONG==8 | ||
| 38 | long hard_memory_limit = 0x7fffffffffffffff; | ||
| 39 | #elif SIZEOF_LONG==4 | ||
| 40 | long hard_memory_limit = 0x7fffffff; | ||
| 41 | #endif /* will produce a compile error or SIZEOF_LONG is not 4 or 8 */ | ||
| 42 | if (stage == ZEND_INI_STAGE_RUNTIME) { | ||
| 43 | if (SUHOSIN7_G(memory_limit) > 0) { | ||
| 44 | SUHOSIN7_G(hard_memory_limit) = SUHOSIN7_G(memory_limit); | ||
| 45 | } else if (SUHOSIN7_G(hard_memory_limit) == 0) { | ||
| 46 | SUHOSIN7_G(hard_memory_limit) = PG(memory_limit); | ||
| 47 | } | ||
| 48 | hard_memory_limit = SUHOSIN7_G(hard_memory_limit); | ||
| 49 | } else { | ||
| 50 | SUHOSIN7_G(hard_memory_limit) = 0; | ||
| 51 | } | ||
| 52 | if (new_value) { | ||
| 53 | PG(memory_limit) = zend_atol(ZSTR_VAL(new_value), ZSTR_LEN(new_value)); | ||
| 54 | if (hard_memory_limit > 0) { | ||
| 55 | if (PG(memory_limit) > hard_memory_limit) { | ||
| 56 | suhosin_log(S_MISC, "script tried to increase memory_limit to " ZEND_LONG_FMT " bytes which is above the allowed value", PG(memory_limit)); | ||
| 57 | if (!SUHOSIN7_G(simulation)) { | ||
| 58 | PG(memory_limit) = hard_memory_limit; | ||
| 59 | return FAILURE; | ||
| 60 | } | ||
| 61 | } else if (PG(memory_limit) < 0) { | ||
| 62 | suhosin_log(S_MISC, "script tried to disable memory_limit by setting it to a negative value " ZEND_LONG_FMT " bytes which is not allowed", PG(memory_limit)); | ||
| 63 | if (!SUHOSIN7_G(simulation)) { | ||
| 64 | PG(memory_limit) = hard_memory_limit; | ||
| 65 | return FAILURE; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | } else { | ||
| 70 | PG(memory_limit) = hard_memory_limit; | ||
| 71 | } | ||
| 72 | return zend_set_memory_limit(PG(memory_limit)); | ||
| 73 | } | ||
| 74 | /* }}} */ | ||
| 75 | |||
| 76 | |||
| 77 | void suhosin_hook_memory_limit() | ||
| 78 | { | ||
| 79 | zend_ini_entry *ini_entry; | ||
| 80 | |||
| 81 | /* check if we are compiled against memory_limit */ | ||
| 82 | if ((ini_entry=zend_hash_str_find_ptr(EG(ini_directives), "memory_limit", sizeof("memory_limit")-1))) { | ||
| 83 | /* replace OnUpdateMemoryLimit handler */ | ||
| 84 | ini_entry->on_modify = suhosin_OnChangeMemoryLimit; | ||
| 85 | } | ||
| 86 | |||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | /* | ||
| 91 | * Local variables: | ||
| 92 | * tab-width: 4 | ||
| 93 | * c-basic-offset: 4 | ||
| 94 | * End: | ||
| 95 | * vim600: noet sw=4 ts=4 fdm=marker | ||
| 96 | * vim<600: noet sw=4 ts=4 | ||
| 97 | */ | ||
| 98 | |||
