[compiler-rt] [scudo] Only read urandom if getrandom syscall isn't available. (PR #161889)
Christopher Ferris via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 3 11:27:29 PDT 2025
https://github.com/cferris1000 created https://github.com/llvm/llvm-project/pull/161889
If the getrandom system call is available, but the call returns an error, it could mean that the system doesn't have enough randomness to respond yet. Trying to read /dev/urandom will likely block and cause initialization to be stalled. Therefore, return false in this case and use the backup random data.
>From 3d6405558b20af4c7bb2279bd92e2289ce2b0599 Mon Sep 17 00:00:00 2001
From: Christopher Ferris <cferris at google.com>
Date: Fri, 3 Oct 2025 11:22:35 -0700
Subject: [PATCH] [scudo] Only read urandom if getrandom syscall isn't
available.
If the getrandom system call is available, but the call returns
an error, it could mean that the system doesn't have enough
randomness to respond yet. Trying to read /dev/urandom will likely
block and cause initialization to be stalled. Therefore, return
false in this case and use the backup random data.
---
compiler-rt/lib/scudo/standalone/linux.cpp | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/compiler-rt/lib/scudo/standalone/linux.cpp b/compiler-rt/lib/scudo/standalone/linux.cpp
index 6cc8e0c786e06..57171edac1e9e 100644
--- a/compiler-rt/lib/scudo/standalone/linux.cpp
+++ b/compiler-rt/lib/scudo/standalone/linux.cpp
@@ -192,6 +192,12 @@ bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) {
syscall(SYS_getrandom, Buffer, Length, Blocking ? 0 : GRND_NONBLOCK);
if (ReadBytes == static_cast<ssize_t>(Length))
return true;
+ // If this system call is not implemented in the kernel, then we will try
+ // and use /dev/urandom. Otherwise, if the syscall fails, return false
+ // assuming that trying to read /dev/urandom will cause a delay waiting for
+ // the random data to be usable.
+ if (errno != ENOSYS)
+ return false;
#endif // defined(SYS_getrandom)
// Up to 256 bytes, a read off /dev/urandom will not be interrupted.
// Blocking is moot here, O_NONBLOCK has no effect when opening /dev/urandom.
More information about the llvm-commits
mailing list