From ace9b373e33a32200bd30a01fb42f621235089a0 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 10 Nov 2022 07:01:02 -0800 Subject: [PATCH] Avoid unconditional `getrandom` syscall creating a `WasiCtx` This commit updates the default random context inserted into a `WasiCtxt` to be seeded from `thread_rng` rather than the system's entropy. This avoids an unconditional syscall on the creation of all `WasiCtx` structures shouldn't reduce the quality of the random numbers produced. --- crates/wasi-common/cap-std-sync/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/wasi-common/cap-std-sync/src/lib.rs b/crates/wasi-common/cap-std-sync/src/lib.rs index 4d8fd0cb9a36..12f27bcec885 100644 --- a/crates/wasi-common/cap-std-sync/src/lib.rs +++ b/crates/wasi-common/cap-std-sync/src/lib.rs @@ -47,7 +47,7 @@ pub use clocks::clocks_ctx; pub use sched::sched_ctx; use crate::net::Socket; -use cap_rand::RngCore; +use cap_rand::{Rng, RngCore, SeedableRng}; use std::path::Path; use wasi_common::{file::FileCaps, table::Table, Error, WasiCtx, WasiFile}; @@ -141,5 +141,6 @@ impl WasiCtxBuilder { } pub fn random_ctx() -> Box { - Box::new(cap_rand::std_rng_from_entropy(cap_rand::ambient_authority())) + let mut rng = cap_rand::thread_rng(cap_rand::ambient_authority()); + Box::new(cap_rand::rngs::StdRng::from_seed(rng.gen())) }