Avoid unconditional getrandom syscall creating a WasiCtx (#5244)

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.
This commit is contained in:
Alex Crichton
2022-11-10 13:58:11 -06:00
committed by GitHub
parent 92f6fe36cc
commit 1f09954fa4

View File

@@ -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<dyn RngCore + Send + Sync> {
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()))
}