bionic: make NONBLOCK call to getrandom
By default getrandom() blocks if the entropy pool has not yet been initialized.
This will be an issue when init was first executed in some kernels.
This CL makes a check of getrandom readyness, by adding the GRND_NONBLOCK flag.
In such case, getrandom() does not block returns -1 with errno set to EAGAIN.
Test: on M/S devices
Bug: 33059407
Change-Id: I2a2ba8372a5e1c336852ba2ab77cdaac03c90389
diff --git a/libc/bionic/bionic_arc4random.cpp b/libc/bionic/bionic_arc4random.cpp
index 429ff7f..4ff18ab 100644
--- a/libc/bionic/bionic_arc4random.cpp
+++ b/libc/bionic/bionic_arc4random.cpp
@@ -38,11 +38,12 @@
#include "private/libc_logging.h"
void __libc_safe_arc4random_buf(void* buf, size_t n, KernelArgumentBlock& args) {
- static bool have_getrandom = syscall(SYS_getrandom, nullptr, 0, 0) != -1 || errno != ENOSYS;
static bool have_urandom = access("/dev/urandom", R_OK) == 0;
static size_t at_random_bytes_consumed = 0;
- if (have_getrandom || have_urandom) {
+ // Only call arc4random_buf once we `have_urandom', since in getentropy_getrandom we may fallback
+ // to use /dev/urandom, if the kernel entropy pool hasn't been initialized or not enough bytes
+ if (have_urandom) {
arc4random_buf(buf, n);
return;
}