about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorTobias Bucher <tobiasbucher5991@gmail.com>2019-05-01 22:23:07 +0200
committerTobias Bucher <tobiasbucher5991@gmail.com>2019-05-01 22:23:07 +0200
commitbd8885d340beb20ac1728d0bd7e64321da641b91 (patch)
treeb92c0bc82c916ab33432619c60ef0677c3b4d6a9 /src/libstd
parent6cc24f26036b28fb3366de86efe3da6c4464057a (diff)
downloadrust-bd8885d340beb20ac1728d0bd7e64321da641b91.tar.gz
rust-bd8885d340beb20ac1728d0bd7e64321da641b91.zip
Fall back to `/dev/urandom` on `EPERM` for `getrandom`
This can happen because of seccomp or some VMs.

Fixes #52609.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/sys/unix/rand.rs7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/libstd/sys/unix/rand.rs b/src/libstd/sys/unix/rand.rs
index 77f1439e17b..71c62461ee9 100644
--- a/src/libstd/sys/unix/rand.rs
+++ b/src/libstd/sys/unix/rand.rs
@@ -47,7 +47,12 @@ mod imp {
                 let err = errno() as libc::c_int;
                 if err == libc::EINTR {
                     continue;
-                } else if err == libc::ENOSYS {
+                } else if err == libc::ENOSYS || err == libc::EPERM {
+                    // Fall back to reading /dev/urandom if `getrandom` is not
+                    // supported on the current kernel.
+                    //
+                    // Also fall back in case it is disabled by something like
+                    // seccomp or inside of virtual machines.
                     GETRANDOM_UNAVAILABLE.store(true, Ordering::Relaxed);
                     return false;
                 } else if err == libc::EAGAIN {