Skip to content

Commit c09fad9

Browse files
author
Julien Pauli
committed
Merge branch 'PHP-5.5' into PHP-5.6
* PHP-5.5: Close fd at the end, otherwise people complain Add file descriptor caching to mcrypt_create_iv()
2 parents 1b12982 + f7952b9 commit c09fad9

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

ext/mcrypt/mcrypt.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,13 @@ static void php_mcrypt_module_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{
380380
}
381381
}
382382
/* }}} */
383-
383+
384+
typedef enum {
385+
RANDOM = 0,
386+
URANDOM,
387+
RAND
388+
} iv_source;
389+
384390
static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */
385391
{
386392
le_mcrypt = zend_register_list_destructors_ex(php_mcrypt_module_dtor, NULL, "mcrypt", module_number);
@@ -438,6 +444,9 @@ static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */
438444
php_stream_filter_register_factory("mcrypt.*", &php_mcrypt_filter_factory TSRMLS_CC);
439445
php_stream_filter_register_factory("mdecrypt.*", &php_mcrypt_filter_factory TSRMLS_CC);
440446

447+
MCG(fd[RANDOM]) = -1;
448+
MCG(fd[URANDOM]) = -1;
449+
441450
return SUCCESS;
442451
}
443452
/* }}} */
@@ -447,6 +456,14 @@ static PHP_MSHUTDOWN_FUNCTION(mcrypt) /* {{{ */
447456
php_stream_filter_unregister_factory("mcrypt.*" TSRMLS_CC);
448457
php_stream_filter_unregister_factory("mdecrypt.*" TSRMLS_CC);
449458

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+
450467
UNREGISTER_INI_ENTRIES();
451468
return SUCCESS;
452469
}
@@ -1423,24 +1440,27 @@ PHP_FUNCTION(mcrypt_create_iv)
14231440
}
14241441
n = size;
14251442
#else
1426-
int fd;
1443+
int *fd = &MCG(fd[source]);
14271444
size_t read_bytes = 0;
14281445

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+
}
14341453
}
1454+
14351455
while (read_bytes < size) {
1436-
n = read(fd, iv + read_bytes, size - read_bytes);
1456+
n = read(*fd, iv + read_bytes, size - read_bytes);
14371457
if (n < 0) {
14381458
break;
14391459
}
14401460
read_bytes += n;
14411461
}
14421462
n = read_bytes;
1443-
close(fd);
1463+
14441464
if (n < size) {
14451465
efree(iv);
14461466
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not gather sufficient random data");

ext/mcrypt/php_mcrypt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ ZEND_BEGIN_MODULE_GLOBALS(mcrypt)
7777
int le_h;
7878
char *modes_dir;
7979
char *algorithms_dir;
80+
int fd[2]; // RANDOM = 0, URANDOM = 1
8081
ZEND_END_MODULE_GLOBALS(mcrypt)
8182

8283
#ifdef ZTS

0 commit comments

Comments
 (0)