Skip to content

Commit bd8885d

Browse files
committed
Fall back to /dev/urandom on EPERM for getrandom
This can happen because of seccomp or some VMs. Fixes #52609.
1 parent 6cc24f2 commit bd8885d

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/libstd/sys/unix/rand.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ mod imp {
4747
let err = errno() as libc::c_int;
4848
if err == libc::EINTR {
4949
continue;
50-
} else if err == libc::ENOSYS {
50+
} else if err == libc::ENOSYS || err == libc::EPERM {
51+
// Fall back to reading /dev/urandom if `getrandom` is not
52+
// supported on the current kernel.
53+
//
54+
// Also fall back in case it is disabled by something like
55+
// seccomp or inside of virtual machines.
5156
GETRANDOM_UNAVAILABLE.store(true, Ordering::Relaxed);
5257
return false;
5358
} else if err == libc::EAGAIN {

0 commit comments

Comments
 (0)