diff options
Diffstat (limited to 'execute_rnd.c')
| -rw-r--r-- | execute_rnd.c | 152 |
1 files changed, 97 insertions, 55 deletions
diff --git a/execute_rnd.c b/execute_rnd.c index 9647b63..e2f6016 100644 --- a/execute_rnd.c +++ b/execute_rnd.c | |||
| @@ -1,5 +1,26 @@ | |||
| 1 | /* | ||
| 2 | +----------------------------------------------------------------------+ | ||
| 3 | | Suhosin Version 1 | | ||
| 4 | +----------------------------------------------------------------------+ | ||
| 5 | | Copyright (c) 2006-2007 The Hardened-PHP Project | | ||
| 6 | | Copyright (c) 2007-2016 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 | | Authors: Stefan Esser <sesser@sektioneins.de> | | ||
| 17 | | Ben Fuhrmannek <ben.fuhrmannek@sektioneins.de> | | ||
| 18 | +----------------------------------------------------------------------+ | ||
| 19 | */ | ||
| 20 | |||
| 1 | /* MT RAND FUNCTIONS */ | 21 | /* MT RAND FUNCTIONS */ |
| 2 | 22 | ||
| 23 | |||
| 3 | /* | 24 | /* |
| 4 | The following php_mt_...() functions are based on a C++ class MTRand by | 25 | The following php_mt_...() functions are based on a C++ class MTRand by |
| 5 | Richard J. Wagner. For more information see the web page at | 26 | Richard J. Wagner. For more information see the web page at |
| @@ -55,12 +76,27 @@ | |||
| 55 | The original code included the following notice: | 76 | The original code included the following notice: |
| 56 | 77 | ||
| 57 | When you use this, send an email to: matumoto@math.keio.ac.jp | 78 | When you use this, send an email to: matumoto@math.keio.ac.jp |
| 58 | with an appropriate reference to your work. | 79 | with an appropriate reference to your work. |
| 59 | 80 | ||
| 60 | It would be nice to CC: rjwagner@writeme.com and Cokus@math.washington.edu | 81 | It would be nice to CC: rjwagner@writeme.com and Cokus@math.washington.edu |
| 61 | when you write. | 82 | when you write. |
| 62 | */ | 83 | */ |
| 63 | 84 | ||
| 85 | #ifdef HAVE_CONFIG_H | ||
| 86 | #include "config.h" | ||
| 87 | #endif | ||
| 88 | |||
| 89 | #include "php.h" | ||
| 90 | #include "php_suhosin7.h" | ||
| 91 | #include "ext/hash/php_hash.h" | ||
| 92 | #include "ext/hash/php_hash_sha.h" | ||
| 93 | #include "ext/standard/php_lcg.h" | ||
| 94 | #include "ext/standard/php_rand.h" | ||
| 95 | #include "execute.h" | ||
| 96 | |||
| 97 | #include <fcntl.h> | ||
| 98 | |||
| 99 | |||
| 64 | #define N 624 /* length of state vector */ | 100 | #define N 624 /* length of state vector */ |
| 65 | #define M (397) /* a period parameter */ | 101 | #define M (397) /* a period parameter */ |
| 66 | #define hiBit(u) ((u) & 0x80000000U) /* mask all but highest bit of u */ | 102 | #define hiBit(u) ((u) & 0x80000000U) /* mask all but highest bit of u */ |
| @@ -93,21 +129,21 @@ static inline void suhosin_mt_initialize(php_uint32 seed, php_uint32 *state) | |||
| 93 | 129 | ||
| 94 | static inline void suhosin_mt_init_by_array(php_uint32 *key, int keylen, php_uint32 *state) | 130 | static inline void suhosin_mt_init_by_array(php_uint32 *key, int keylen, php_uint32 *state) |
| 95 | { | 131 | { |
| 96 | int i, j, k; | 132 | int i, j, k; |
| 97 | suhosin_mt_initialize(19650218U, state); | 133 | suhosin_mt_initialize(19650218U, state); |
| 98 | i = 1; j = 0; | 134 | i = 1; j = 0; |
| 99 | k = (N > keylen ? N : keylen); | 135 | k = (N > keylen ? N : keylen); |
| 100 | for (; k; k--) { | 136 | for (; k; k--) { |
| 101 | state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1664525U)) + key[j] + j; | 137 | state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1664525U)) + key[j] + j; |
| 102 | i++; j = (j+1) % keylen; | 138 | i++; j = (j+1) % keylen; |
| 103 | if (i >= N) { state[0] = state[N-1]; i=1; } | 139 | if (i >= N) { state[0] = state[N-1]; i=1; } |
| 104 | } | 140 | } |
| 105 | for (k=N-1; k; k--) { | 141 | for (k=N-1; k; k--) { |
| 106 | state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1566083941U)) - i; | 142 | state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1566083941U)) - i; |
| 107 | i++; | 143 | i++; |
| 108 | if (i >= N) { state[0] = state[N-1]; i=1; } | 144 | if (i >= N) { state[0] = state[N-1]; i=1; } |
| 109 | } | 145 | } |
| 110 | state[0] = 0x80000000U; | 146 | state[0] = 0x80000000U; |
| 111 | } | 147 | } |
| 112 | /* }}} */ | 148 | /* }}} */ |
| 113 | 149 | ||
| @@ -171,48 +207,54 @@ static php_uint32 suhosin_mt_rand() | |||
| 171 | */ | 207 | */ |
| 172 | static void SUHOSIN7_Gen_entropy(php_uint32 *entropybuf) | 208 | static void SUHOSIN7_Gen_entropy(php_uint32 *entropybuf) |
| 173 | { | 209 | { |
| 174 | php_uint32 seedbuf[20]; | 210 | php_uint32 seedbuf[20]; |
| 175 | /* On a modern OS code, stack and heap base are randomized */ | 211 | /* On a modern OS code, stack and heap base are randomized */ |
| 176 | unsigned long code_value = (unsigned long)SUHOSIN7_Gen_entropy; | 212 | unsigned long code_value = (unsigned long)SUHOSIN7_Gen_entropy; |
| 177 | unsigned long stack_value = (unsigned long)&code_value; | 213 | unsigned long stack_value = (unsigned long)&code_value; |
| 178 | unsigned long heap_value = (unsigned long)SUHOSIN7_G(r_state); | 214 | unsigned long heap_value = (unsigned long)SUHOSIN7_G(r_state); |
| 179 | suhosin_SHA256_CTX context; | 215 | PHP_SHA256_CTX context; |
| 180 | int fd; | 216 | int fd; |
| 181 | 217 | ||
| 182 | code_value ^= code_value >> 32; | 218 | code_value ^= code_value >> 32; |
| 183 | stack_value ^= stack_value >> 32; | 219 | stack_value ^= stack_value >> 32; |
| 184 | heap_value ^= heap_value >> 32; | 220 | heap_value ^= heap_value >> 32; |
| 185 | 221 | ||
| 186 | seedbuf[0] = code_value; | 222 | seedbuf[0] = code_value; |
| 187 | seedbuf[1] = stack_value; | 223 | seedbuf[1] = stack_value; |
| 188 | seedbuf[2] = heap_value; | 224 | seedbuf[2] = heap_value; |
| 189 | seedbuf[3] = time(0); | 225 | seedbuf[3] = time(0); |
| 190 | #ifdef PHP_WIN32 | 226 | #ifdef PHP_WIN32 |
| 191 | seedbuf[4] = GetCurrentProcessId(); | 227 | seedbuf[4] = GetCurrentProcessId(); |
| 192 | #else | 228 | #else |
| 193 | seedbuf[4] = getpid(); | 229 | seedbuf[4] = getpid(); |
| 194 | #endif | 230 | #endif |
| 195 | seedbuf[5] = (php_uint32) 0x7fffffff * php_combined_lcg(); | 231 | seedbuf[5] = (php_uint32) 0x7fffffff * php_combined_lcg(); |
| 196 | 232 | ||
| 197 | #ifndef PHP_WIN32 | 233 | #ifndef PHP_WIN32 |
| 198 | fd = VCWD_OPEN("/dev/urandom", O_RDONLY); | 234 | # if HAVE_DEV_URANDOM |
| 199 | if (fd >= 0) { | 235 | # ifdef VIRTUAL_DIR |
| 200 | /* ignore error case - if urandom doesn't give us any/enough random bytes */ | 236 | fd = VCWD_OPEN("/dev/urandom", O_RDONLY); |
| 201 | read(fd, &seedbuf[6], 8 * sizeof(php_uint32)); | 237 | # else |
| 202 | close(fd); | 238 | fd = open("/dev/urandom", O_RDONLY); |
| 203 | } | 239 | # endif |
| 240 | if (fd >= 0) { | ||
| 241 | /* ignore error case - if urandom doesn't give us any/enough random bytes */ | ||
| 242 | read(fd, &seedbuf[6], 8 * sizeof(php_uint32)); | ||
| 243 | close(fd); | ||
| 244 | } | ||
| 245 | # endif | ||
| 204 | #else | 246 | #else |
| 205 | /* we have to live with the possibility that this call fails */ | 247 | /* we have to live with the possibility that this call fails */ |
| 206 | php_win32_get_random_bytes((unsigned char*)&seedbuf[6], 8 * sizeof(php_uint32)); | 248 | php_win32_get_random_bytes((unsigned char*)&seedbuf[6], 8 * sizeof(php_uint32)); |
| 207 | #endif | 249 | #endif |
| 208 | 250 | ||
| 209 | suhosin_SHA256Init(&context); | 251 | PHP_SHA256Init(&context); |
| 210 | /* to our friends from Debian: yes this will add unitialized stack values to the entropy DO NOT REMOVE */ | 252 | /* to our friends from Debian: yes this will add unitialized stack values to the entropy DO NOT REMOVE */ |
| 211 | suhosin_SHA256Update(&context, (void *) seedbuf, sizeof(seedbuf)); | 253 | PHP_SHA256Update(&context, (void *) seedbuf, sizeof(seedbuf)); |
| 212 | if (SUHOSIN7_G(seedingkey) != NULL && *SUHOSIN7_G(seedingkey) != 0) { | 254 | if (SUHOSIN7_G(seedingkey) != NULL && *SUHOSIN7_G(seedingkey) != 0) { |
| 213 | suhosin_SHA256Update(&context, (unsigned char*)SUHOSIN7_G(seedingkey), strlen(SUHOSIN7_G(seedingkey))); | 255 | PHP_SHA256Update(&context, (unsigned char*)SUHOSIN7_G(seedingkey), strlen(SUHOSIN7_G(seedingkey))); |
| 214 | } | 256 | } |
| 215 | suhosin_SHA256Final((void *)entropybuf, &context); | 257 | PHP_SHA256Final((void *)entropybuf, &context); |
| 216 | } | 258 | } |
| 217 | /* }}} */ | 259 | /* }}} */ |
| 218 | 260 | ||
| @@ -283,7 +325,7 @@ static php_uint32 suhosin_rand() | |||
| 283 | } | 325 | } |
| 284 | /* }}} */ | 326 | /* }}} */ |
| 285 | 327 | ||
| 286 | static int ih_srand(IH_HANDLER_PARAMS) | 328 | S7_IH_FUNCTION(srand) |
| 287 | { | 329 | { |
| 288 | int argc = ZEND_NUM_ARGS(); | 330 | int argc = ZEND_NUM_ARGS(); |
| 289 | long seed; | 331 | long seed; |
| @@ -305,7 +347,7 @@ static int ih_srand(IH_HANDLER_PARAMS) | |||
| 305 | return (1); | 347 | return (1); |
| 306 | } | 348 | } |
| 307 | 349 | ||
| 308 | static int ih_mt_srand(IH_HANDLER_PARAMS) | 350 | S7_IH_FUNCTION(mt_srand) |
| 309 | { | 351 | { |
| 310 | int argc = ZEND_NUM_ARGS(); | 352 | int argc = ZEND_NUM_ARGS(); |
| 311 | long seed; | 353 | long seed; |
| @@ -327,7 +369,7 @@ static int ih_mt_srand(IH_HANDLER_PARAMS) | |||
| 327 | return 1; | 369 | return 1; |
| 328 | } | 370 | } |
| 329 | 371 | ||
| 330 | static int ih_mt_rand(IH_HANDLER_PARAMS) | 372 | S7_IH_FUNCTION(mt_rand) |
| 331 | { | 373 | { |
| 332 | int argc = ZEND_NUM_ARGS(); | 374 | int argc = ZEND_NUM_ARGS(); |
| 333 | long min; | 375 | long min; |
| @@ -351,7 +393,7 @@ static int ih_mt_rand(IH_HANDLER_PARAMS) | |||
| 351 | return (1); | 393 | return (1); |
| 352 | } | 394 | } |
| 353 | 395 | ||
| 354 | static int ih_rand(IH_HANDLER_PARAMS) | 396 | S7_IH_FUNCTION(rand) |
| 355 | { | 397 | { |
| 356 | int argc = ZEND_NUM_ARGS(); | 398 | int argc = ZEND_NUM_ARGS(); |
| 357 | long min; | 399 | long min; |
| @@ -375,7 +417,7 @@ static int ih_rand(IH_HANDLER_PARAMS) | |||
| 375 | return (1); | 417 | return (1); |
| 376 | } | 418 | } |
| 377 | 419 | ||
| 378 | static int ih_getrandmax(IH_HANDLER_PARAMS) | 420 | S7_IH_FUNCTION(getrandmax) |
| 379 | { | 421 | { |
| 380 | if (zend_parse_parameters_none() == FAILURE) { | 422 | if (zend_parse_parameters_none() == FAILURE) { |
| 381 | return(0); | 423 | return(0); |
