@@ -380,7 +380,13 @@ static void php_mcrypt_module_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{
380
380
}
381
381
}
382
382
/* }}} */
383
-
383
+
384
+ typedef enum {
385
+ RANDOM = 0 ,
386
+ URANDOM ,
387
+ RAND
388
+ } iv_source ;
389
+
384
390
static PHP_MINIT_FUNCTION (mcrypt ) /* {{{ */
385
391
{
386
392
le_mcrypt = zend_register_list_destructors_ex (php_mcrypt_module_dtor , NULL , "mcrypt" , module_number );
@@ -438,6 +444,9 @@ static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */
438
444
php_stream_filter_register_factory ("mcrypt.*" , & php_mcrypt_filter_factory TSRMLS_CC );
439
445
php_stream_filter_register_factory ("mdecrypt.*" , & php_mcrypt_filter_factory TSRMLS_CC );
440
446
447
+ MCG (fd [RANDOM ]) = -1 ;
448
+ MCG (fd [URANDOM ]) = -1 ;
449
+
441
450
return SUCCESS ;
442
451
}
443
452
/* }}} */
@@ -447,6 +456,14 @@ static PHP_MSHUTDOWN_FUNCTION(mcrypt) /* {{{ */
447
456
php_stream_filter_unregister_factory ("mcrypt.*" TSRMLS_CC );
448
457
php_stream_filter_unregister_factory ("mdecrypt.*" TSRMLS_CC );
449
458
459
+ if (MCG (fd [RANDOM ]) > 0 ) {
460
+ close (MCG (fd [RANDOM ]));
461
+ }
462
+
463
+ if (MCG (fd [URANDOM ]) > 0 ) {
464
+ close (MCG (fd [URANDOM ]));
465
+ }
466
+
450
467
UNREGISTER_INI_ENTRIES ();
451
468
return SUCCESS ;
452
469
}
@@ -1423,24 +1440,27 @@ PHP_FUNCTION(mcrypt_create_iv)
1423
1440
}
1424
1441
n = size ;
1425
1442
#else
1426
- int fd ;
1443
+ int * fd = & MCG ( fd [ source ]) ;
1427
1444
size_t read_bytes = 0 ;
1428
1445
1429
- fd = open (source == RANDOM ? "/dev/random" : "/dev/urandom" , O_RDONLY );
1430
- if (fd < 0 ) {
1431
- efree (iv );
1432
- php_error_docref (NULL TSRMLS_CC , E_WARNING , "Cannot open source device" );
1433
- RETURN_FALSE ;
1446
+ if (* fd < 0 ) {
1447
+ * fd = open (source == RANDOM ? "/dev/random" : "/dev/urandom" , O_RDONLY );
1448
+ if (* fd < 0 ) {
1449
+ efree (iv );
1450
+ php_error_docref (NULL TSRMLS_CC , E_WARNING , "Cannot open source device" );
1451
+ RETURN_FALSE ;
1452
+ }
1434
1453
}
1454
+
1435
1455
while (read_bytes < size ) {
1436
- n = read (fd , iv + read_bytes , size - read_bytes );
1456
+ n = read (* fd , iv + read_bytes , size - read_bytes );
1437
1457
if (n < 0 ) {
1438
1458
break ;
1439
1459
}
1440
1460
read_bytes += n ;
1441
1461
}
1442
1462
n = read_bytes ;
1443
- close ( fd );
1463
+
1444
1464
if (n < size ) {
1445
1465
efree (iv );
1446
1466
php_error_docref (NULL TSRMLS_CC , E_WARNING , "Could not gather sufficient random data" );
0 commit comments