Skip to content

Conversation

cferris1000
Copy link
Contributor

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.

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.
@llvmbot
Copy link
Member

llvmbot commented Oct 3, 2025

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Christopher Ferris (cferris1000)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/161889.diff

1 Files Affected:

  • (modified) compiler-rt/lib/scudo/standalone/linux.cpp (+6)
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.

@cferris1000 cferris1000 merged commit f642236 into llvm:main Oct 6, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants