summaryrefslogtreecommitdiff
path: root/execute.c
diff options
context:
space:
mode:
authorStefan Esser2014-06-09 10:37:10 +0200
committerStefan Esser2014-06-09 10:37:10 +0200
commitfb0f51e922b597a46d1065437f716c3179e5506c (patch)
tree5958576a1aa087c7bfbdf76c6ef632d261905bb6 /execute.c
parent83bf21540d308a740c8835c4c3a104a5d2f761c5 (diff)
Added various improvements to rand()/mt_rand() protection
Diffstat (limited to 'execute.c')
-rw-r--r--execute.c23
1 files changed, 19 insertions, 4 deletions
diff --git a/execute.c b/execute.c
index 220c0ff..103a8bf 100644
--- a/execute.c
+++ b/execute.c
@@ -38,6 +38,13 @@
38 38
39#include "sha256.h" 39#include "sha256.h"
40 40
41#ifdef PHP_WIN32
42# include "win32/winutil.h"
43# include "win32/time.h"
44#else
45# include <sys/time.h>
46#endif
47
41#if PHP_VERSION_ID >= 50500 48#if PHP_VERSION_ID >= 50500
42static void (*old_execute_ex)(zend_execute_data *execute_data TSRMLS_DC); 49static void (*old_execute_ex)(zend_execute_data *execute_data TSRMLS_DC);
43static void suhosin_execute_ex(zend_execute_data *execute_data TSRMLS_DC); 50static void suhosin_execute_ex(zend_execute_data *execute_data TSRMLS_DC);
@@ -1325,8 +1332,9 @@ static php_uint32 suhosin_mt_rand(TSRMLS_D)
1325 1332
1326/* {{{ suhosin_gen_entropy 1333/* {{{ suhosin_gen_entropy
1327 */ 1334 */
1328static void suhosin_gen_entropy(php_uint32 *seedbuf TSRMLS_DC) 1335static void suhosin_gen_entropy(php_uint32 *entropybuf TSRMLS_DC)
1329{ 1336{
1337 php_uint32 seedbuf[20];
1330 /* On a modern OS code, stack and heap base are randomized */ 1338 /* On a modern OS code, stack and heap base are randomized */
1331 unsigned long code_value = (unsigned long)suhosin_gen_entropy; 1339 unsigned long code_value = (unsigned long)suhosin_gen_entropy;
1332 unsigned long stack_value = (unsigned long)&code_value; 1340 unsigned long stack_value = (unsigned long)&code_value;
@@ -1353,14 +1361,21 @@ static void suhosin_gen_entropy(php_uint32 *seedbuf TSRMLS_DC)
1353 fd = VCWD_OPEN("/dev/urandom", O_RDONLY); 1361 fd = VCWD_OPEN("/dev/urandom", O_RDONLY);
1354 if (fd >= 0) { 1362 if (fd >= 0) {
1355 /* ignore error case - if urandom doesn't give us any/enough random bytes */ 1363 /* ignore error case - if urandom doesn't give us any/enough random bytes */
1356 read(fd, &seedbuf[6], 2 * sizeof(php_uint32)); 1364 read(fd, &seedbuf[6], 8 * sizeof(php_uint32));
1357 close(fd); 1365 close(fd);
1358 } 1366 }
1367#else
1368 /* we have to live with the possibility that this call fails */
1369 php_win32_get_random_bytes(rbuf, 8 * sizeof(php_uint32));
1359#endif 1370#endif
1360 1371
1361 suhosin_SHA256Init(&context); 1372 suhosin_SHA256Init(&context);
1362 suhosin_SHA256Update(&context, (void *) seedbuf, sizeof(php_uint32) * 8); 1373 /* to our friends from Debian: yes this will add unitialized stack values to the entropy DO NOT REMOVE */
1363 suhosin_SHA256Final((void *)seedbuf, &context); 1374 suhosin_SHA256Update(&context, (void *) seedbuf, sizeof(seedbuf));
1375 if (SUHOSIN_G(seedingkey) != NULL && *SUHOSIN_G(seedingkey) != 0) {
1376 suhosin_SHA256Update(&context, (unsigned char*)SUHOSIN_G(seedingkey), strlen(SUHOSIN_G(seedingkey)));
1377 }
1378 suhosin_SHA256Final((void *)entropybuf, &context);
1364} 1379}
1365/* }}} */ 1380/* }}} */
1366 1381