mirror of
https://github.com/php/php-src.git
synced 2025-08-16 05:58:45 +02:00
Merge branch 'PHP-7.0' into PHP-7.1
This commit is contained in:
commit
c240feb7f4
2 changed files with 57 additions and 51 deletions
2
NEWS
2
NEWS
|
@ -49,6 +49,8 @@ PHP NEWS
|
||||||
. Fixed bug #74041 (substr_count with length=0 broken). (Nikita)
|
. Fixed bug #74041 (substr_count with length=0 broken). (Nikita)
|
||||||
. Fixed bug #73118 (is_callable callable name reports misleading value for
|
. Fixed bug #73118 (is_callable callable name reports misleading value for
|
||||||
anonymous classes). (Adam Saponara)
|
anonymous classes). (Adam Saponara)
|
||||||
|
. Fixed bug #74105 (PHP on Linux should use /dev/urandom when getrandom is
|
||||||
|
not available). (Benjamin Robin)
|
||||||
|
|
||||||
- Streams:
|
- Streams:
|
||||||
. Fixed bug #73496 (Invalid memory access in zend_inline_hash_func).
|
. Fixed bug #73496 (Invalid memory access in zend_inline_hash_func).
|
||||||
|
|
|
@ -93,14 +93,13 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, zend_bool should_throw)
|
||||||
}
|
}
|
||||||
#elif HAVE_DECL_ARC4RANDOM_BUF && ((defined(__OpenBSD__) && OpenBSD >= 201405) || (defined(__NetBSD__) && __NetBSD_Version__ >= 700000001))
|
#elif HAVE_DECL_ARC4RANDOM_BUF && ((defined(__OpenBSD__) && OpenBSD >= 201405) || (defined(__NetBSD__) && __NetBSD_Version__ >= 700000001))
|
||||||
arc4random_buf(bytes, size);
|
arc4random_buf(bytes, size);
|
||||||
#elif defined(__linux__) && defined(SYS_getrandom)
|
#else
|
||||||
/* Linux getrandom(2) syscall */
|
|
||||||
size_t read_bytes = 0;
|
size_t read_bytes = 0;
|
||||||
size_t amount_to_read = 0;
|
|
||||||
ssize_t n;
|
ssize_t n;
|
||||||
|
#if defined(__linux__) && defined(SYS_getrandom)
|
||||||
|
/* Linux getrandom(2) syscall */
|
||||||
/* Keep reading until we get enough entropy */
|
/* Keep reading until we get enough entropy */
|
||||||
do {
|
while (read_bytes < size) {
|
||||||
/* Below, (bytes + read_bytes) is pointer arithmetic.
|
/* Below, (bytes + read_bytes) is pointer arithmetic.
|
||||||
|
|
||||||
bytes read_bytes size
|
bytes read_bytes size
|
||||||
|
@ -110,11 +109,17 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, zend_bool should_throw)
|
||||||
amount_to_read
|
amount_to_read
|
||||||
|
|
||||||
*/
|
*/
|
||||||
amount_to_read = size - read_bytes;
|
size_t amount_to_read = size - read_bytes;
|
||||||
n = syscall(SYS_getrandom, bytes + read_bytes, amount_to_read, 0);
|
n = syscall(SYS_getrandom, bytes + read_bytes, amount_to_read, 0);
|
||||||
|
|
||||||
if (n == -1) {
|
if (n == -1) {
|
||||||
if (errno == EINTR || errno == EAGAIN) {
|
if (errno == ENOSYS) {
|
||||||
|
/* This can happen if PHP was compiled against a newer kernel where getrandom()
|
||||||
|
* is available, but then runs on an older kernel without getrandom(). If this
|
||||||
|
* happens we simply fall back to reading from /dev/urandom. */
|
||||||
|
ZEND_ASSERT(read_bytes == 0);
|
||||||
|
break;
|
||||||
|
} else if (errno == EINTR || errno == EAGAIN) {
|
||||||
/* Try again */
|
/* Try again */
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -130,53 +135,52 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, zend_bool should_throw)
|
||||||
}
|
}
|
||||||
|
|
||||||
read_bytes += (size_t) n;
|
read_bytes += (size_t) n;
|
||||||
} while (read_bytes < size);
|
}
|
||||||
#else
|
|
||||||
int fd = RANDOM_G(fd);
|
|
||||||
struct stat st;
|
|
||||||
size_t read_bytes = 0;
|
|
||||||
ssize_t n;
|
|
||||||
|
|
||||||
if (fd < 0) {
|
|
||||||
#if HAVE_DEV_URANDOM
|
|
||||||
fd = open("/dev/urandom", O_RDONLY);
|
|
||||||
#endif
|
#endif
|
||||||
if (fd < 0) {
|
|
||||||
if (should_throw) {
|
|
||||||
zend_throw_exception(zend_ce_exception, "Cannot open source device", 0);
|
|
||||||
}
|
|
||||||
return FAILURE;
|
|
||||||
}
|
|
||||||
/* Does the file exist and is it a character device? */
|
|
||||||
if (fstat(fd, &st) != 0 ||
|
|
||||||
# ifdef S_ISNAM
|
|
||||||
!(S_ISNAM(st.st_mode) || S_ISCHR(st.st_mode))
|
|
||||||
# else
|
|
||||||
!S_ISCHR(st.st_mode)
|
|
||||||
# endif
|
|
||||||
) {
|
|
||||||
close(fd);
|
|
||||||
if (should_throw) {
|
|
||||||
zend_throw_exception(zend_ce_exception, "Error reading from source device", 0);
|
|
||||||
}
|
|
||||||
return FAILURE;
|
|
||||||
}
|
|
||||||
RANDOM_G(fd) = fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (read_bytes < size) {
|
|
||||||
n = read(fd, bytes + read_bytes, size - read_bytes);
|
|
||||||
if (n <= 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
read_bytes += n;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (read_bytes < size) {
|
if (read_bytes < size) {
|
||||||
if (should_throw) {
|
int fd = RANDOM_G(fd);
|
||||||
zend_throw_exception(zend_ce_exception, "Could not gather sufficient random data", 0);
|
struct stat st;
|
||||||
|
|
||||||
|
if (fd < 0) {
|
||||||
|
#if HAVE_DEV_URANDOM
|
||||||
|
fd = open("/dev/urandom", O_RDONLY);
|
||||||
|
#endif
|
||||||
|
if (fd < 0) {
|
||||||
|
if (should_throw) {
|
||||||
|
zend_throw_exception(zend_ce_exception, "Cannot open source device", 0);
|
||||||
|
}
|
||||||
|
return FAILURE;
|
||||||
|
}
|
||||||
|
/* Does the file exist and is it a character device? */
|
||||||
|
if (fstat(fd, &st) != 0 ||
|
||||||
|
# ifdef S_ISNAM
|
||||||
|
!(S_ISNAM(st.st_mode) || S_ISCHR(st.st_mode))
|
||||||
|
# else
|
||||||
|
!S_ISCHR(st.st_mode)
|
||||||
|
# endif
|
||||||
|
) {
|
||||||
|
close(fd);
|
||||||
|
if (should_throw) {
|
||||||
|
zend_throw_exception(zend_ce_exception, "Error reading from source device", 0);
|
||||||
|
}
|
||||||
|
return FAILURE;
|
||||||
|
}
|
||||||
|
RANDOM_G(fd) = fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (read_bytes = 0; read_bytes < size; read_bytes += (size_t) n) {
|
||||||
|
n = read(fd, bytes + read_bytes, size - read_bytes);
|
||||||
|
if (n <= 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (read_bytes < size) {
|
||||||
|
if (should_throw) {
|
||||||
|
zend_throw_exception(zend_ce_exception, "Could not gather sufficient random data", 0);
|
||||||
|
}
|
||||||
|
return FAILURE;
|
||||||
}
|
}
|
||||||
return FAILURE;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue