rust/library/std/src/sys
Jason A. Donenfeld 18a9d58266 Use GRND_INSECURE instead of /dev/urandom when possible
From reading the source code, it appears like the desired semantic of
std::unix::rand is to always provide some bytes and never block. For
that reason GRND_NONBLOCK is checked before calling getrandom(0), so
that getrandom(0) won't block. If it would block, then the function
falls back to using /dev/urandom, which for the time being doesn't
block. There are some drawbacks to using /dev/urandom, however, and so
getrandom(GRND_INSECURE) was created as a replacement for this exact
circumstance.

getrandom(GRND_INSECURE) is the same as /dev/urandom, except:

- It won't leave a warning in dmesg if used at early boot time, which is
  a common occurance (and the reason why I found this issue);

- It won't introduce a tiny delay at early boot on newer kernels when
  /dev/urandom tries to opportunistically create jitter entropy;

- It only requires 1 syscall, rather than 3.

Other than that, it returns the same "quality" of randomness as
/dev/urandom, and never blocks.

It's only available on kernels ≥5.6, so we try to use it, cache the
result of that attempt, and fall back to to the previous code if it
didn't work.
2022-05-21 00:02:20 +02:00
..
common resolve the conflict in compiler/rustc_session/src/parse.rs 2022-03-16 20:12:30 +08:00
hermit Use Rust 2021 prelude in std itself. 2022-05-09 11:12:32 +02:00
itron Use Rust 2021 prelude in std itself. 2022-05-09 11:12:32 +02:00
sgx Use Rust 2021 prelude in std itself. 2022-05-09 11:12:32 +02:00
solid Use Rust 2021 prelude in std itself. 2022-05-09 11:12:32 +02:00
unix Use GRND_INSECURE instead of /dev/urandom when possible 2022-05-21 00:02:20 +02:00
unsupported Use Rust 2021 prelude in std itself. 2022-05-09 11:12:32 +02:00
wasi Use Rust 2021 prelude in std itself. 2022-05-09 11:12:32 +02:00
wasm Use Rust 2021 prelude in std itself. 2022-05-09 11:12:32 +02:00
windows Rollup merge of #97127 - Mark-Simulacrum:revert-96441, r=m-ou-se 2022-05-19 08:22:43 +09:00
mod.rs Use target_family = "wasm" 2021-11-10 08:35:42 -08:00