]> git.lizzy.rs Git - rust.git/commitdiff
Fall back to `/dev/urandom` on `EPERM` for `getrandom`
authorTobias Bucher <tobiasbucher5991@gmail.com>
Wed, 1 May 2019 20:23:07 +0000 (22:23 +0200)
committerTobias Bucher <tobiasbucher5991@gmail.com>
Wed, 1 May 2019 20:23:07 +0000 (22:23 +0200)
This can happen because of seccomp or some VMs.

Fixes #52609.

src/libstd/sys/unix/rand.rs

index 77f1439e17b10324c7d67d25ab036d7755a27922..71c62461ee9cb4aef5aa6e01652c37165842f25e 100644 (file)
@@ -47,7 +47,12 @@ fn getrandom_fill_bytes(v: &mut [u8]) -> bool {
                 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 {