summaryrefslogtreecommitdiff
path: root/ext/mcrypt/mcrypt.c
diff options
context:
space:
mode:
authorJulien Pauli <jpauli@php.net>2015-05-13 14:21:19 +0200
committerJulien Pauli <jpauli@php.net>2015-05-13 14:21:19 +0200
commit420961524d9a6482b01f90ebbb7a5d2622bcc176 (patch)
tree656fd2bee8cf5d8c5b5dc64382f8978e02d37a5f /ext/mcrypt/mcrypt.c
parent4f5ab6fca49843657e241eddf6e4b08fbdf226b4 (diff)
parentc09fad97d68d07d6b528d56d5cef1f8cbb9fc5ec (diff)
downloadphp-git-420961524d9a6482b01f90ebbb7a5d2622bcc176.tar.gz
Merge branch 'PHP-5.6'
* PHP-5.6: Close fd at the end, otherwise people complain Add file descriptor caching to mcrypt_create_iv() Conflicts: ext/mcrypt/mcrypt.c
Diffstat (limited to 'ext/mcrypt/mcrypt.c')
-rw-r--r--ext/mcrypt/mcrypt.c36
1 files changed, 28 insertions, 8 deletions
diff --git a/ext/mcrypt/mcrypt.c b/ext/mcrypt/mcrypt.c
index 1cdedd596a..f6d8c91234 100644
--- a/ext/mcrypt/mcrypt.c
+++ b/ext/mcrypt/mcrypt.c
@@ -346,6 +346,12 @@ static void php_mcrypt_module_dtor(zend_resource *rsrc) /* {{{ */
}
/* }}} */
+typedef enum {
+ RANDOM = 0,
+ URANDOM,
+ RAND
+} iv_source;
+
static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */
{
le_mcrypt = zend_register_list_destructors_ex(php_mcrypt_module_dtor, NULL, "mcrypt", module_number);
@@ -403,6 +409,9 @@ static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */
php_stream_filter_register_factory("mcrypt.*", &php_mcrypt_filter_factory);
php_stream_filter_register_factory("mdecrypt.*", &php_mcrypt_filter_factory);
+ MCG(fd[RANDOM]) = -1;
+ MCG(fd[URANDOM]) = -1;
+
return SUCCESS;
}
/* }}} */
@@ -412,6 +421,14 @@ static PHP_MSHUTDOWN_FUNCTION(mcrypt) /* {{{ */
php_stream_filter_unregister_factory("mcrypt.*");
php_stream_filter_unregister_factory("mdecrypt.*");
+ if (MCG(fd[RANDOM]) > 0) {
+ close(MCG(fd[RANDOM]));
+ }
+
+ if (MCG(fd[URANDOM]) > 0) {
+ close(MCG(fd[URANDOM]));
+ }
+
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
@@ -1337,24 +1354,27 @@ PHP_FUNCTION(mcrypt_create_iv)
}
n = (int)size;
#else
- int fd;
+ int *fd = &MCG(fd[source]);
size_t read_bytes = 0;
- fd = open(source == RANDOM ? "/dev/random" : "/dev/urandom", O_RDONLY);
- if (fd < 0) {
- efree(iv);
- php_error_docref(NULL, E_WARNING, "Cannot open source device");
- RETURN_FALSE;
+ if (*fd < 0) {
+ *fd = open(source == RANDOM ? "/dev/random" : "/dev/urandom", O_RDONLY);
+ if (*fd < 0) {
+ efree(iv);
+ php_error_docref(NULL, E_WARNING, "Cannot open source device");
+ RETURN_FALSE;
+ }
}
+
while (read_bytes < size) {
- n = read(fd, iv + read_bytes, size - read_bytes);
+ n = read(*fd, iv + read_bytes, size - read_bytes);
if (n < 0) {
break;
}
read_bytes += n;
}
n = read_bytes;
- close(fd);
+
if (n < size) {
efree(iv);
php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data");