From ff255e0991453e2867a03a7a8a4fd60d75420e28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 23 Jan 2023 19:02:09 +0100 Subject: [PATCH 1/4] random: Convert the urandom loop into a while() loop This allows us to more easily reduce the scope of `n` in a future commit and now matches the getrandom(2) loop. --- ext/random/random.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ext/random/random.c b/ext/random/random.c index cf2bf81e566fc..dbc0cbfd33863 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -605,7 +605,8 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) RANDOM_G(random_fd) = fd; } - for (read_bytes = 0; read_bytes < size; read_bytes += (size_t) n) { + read_bytes = 0; + while (read_bytes < size) { errno = 0; n = read(fd, bytes + read_bytes, size - read_bytes); @@ -619,6 +620,8 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) } return FAILURE; } + + read_bytes += (size_t) n; } } #endif From 80e06800eb1598bac39506385d70f632d87f728b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 23 Jan 2023 19:02:56 +0100 Subject: [PATCH 2/4] random: Move the errno reset immediately above the getrandom(2) call --- ext/random/random.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/random/random.c b/ext/random/random.c index dbc0cbfd33863..859ac170f4ffe 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -525,8 +525,6 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) * compared to the arc4random api albeit a fallback to /dev/urandom is considered. */ while (read_bytes < size) { - errno = 0; - /* Below, (bytes + read_bytes) is pointer arithmetic. bytes read_bytes size @@ -536,6 +534,8 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) amount_to_read */ size_t amount_to_read = size - read_bytes; + + errno = 0; # if defined(__linux__) n = syscall(SYS_getrandom, bytes + read_bytes, amount_to_read, 0); # else From 5b2529cb5972ef8e582e142cba735fcb32f8bdb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 23 Jan 2023 19:04:13 +0100 Subject: [PATCH 3/4] random: Reduce the scope of `n` in the CSPRNG --- ext/random/random.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ext/random/random.c b/ext/random/random.c index 859ac170f4ffe..917b01c538bb2 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -517,7 +517,6 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) arc4random_buf(bytes, size); #else size_t read_bytes = 0; - ssize_t n; # if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || (defined(__DragonFly__) && __DragonFly_version >= 500700) || \ defined(__sun) || (defined(__NetBSD__) && __NetBSD_Version__ >= 1000000000) /* Linux getrandom(2) syscall or FreeBSD/DragonFlyBSD/NetBSD getrandom(2) function @@ -537,9 +536,9 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) errno = 0; # if defined(__linux__) - n = syscall(SYS_getrandom, bytes + read_bytes, amount_to_read, 0); + ssize_t n = syscall(SYS_getrandom, bytes + read_bytes, amount_to_read, 0); # else - n = getrandom(bytes + read_bytes, amount_to_read, 0); + ssize_t n = getrandom(bytes + read_bytes, amount_to_read, 0); # endif if (n == -1) { @@ -608,7 +607,7 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) read_bytes = 0; while (read_bytes < size) { errno = 0; - n = read(fd, bytes + read_bytes, size - read_bytes); + ssize_t n = read(fd, bytes + read_bytes, size - read_bytes); if (n <= 0) { if (should_throw) { From bd2de6df0aa547466295e020caaa194ac64e7636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 23 Jan 2023 22:24:55 +0100 Subject: [PATCH 4/4] random: Declare `n` outside of preprocessor branch --- ext/random/random.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ext/random/random.c b/ext/random/random.c index 917b01c538bb2..83b9f2b698a6c 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -533,12 +533,13 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) amount_to_read */ size_t amount_to_read = size - read_bytes; + ssize_t n; errno = 0; # if defined(__linux__) - ssize_t n = syscall(SYS_getrandom, bytes + read_bytes, amount_to_read, 0); + n = syscall(SYS_getrandom, bytes + read_bytes, amount_to_read, 0); # else - ssize_t n = getrandom(bytes + read_bytes, amount_to_read, 0); + n = getrandom(bytes + read_bytes, amount_to_read, 0); # endif if (n == -1) {