From add601fd6c639e395621c99c50888e311fdc63c9 Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Mon, 4 Jan 2021 16:21:23 -0800 Subject: [PATCH] use cap_rand instead of getrandom --- Cargo.lock | 11 +++++- crates/wasi-c2/Cargo.toml | 2 +- crates/wasi-c2/src/ctx.rs | 10 +++--- crates/wasi-c2/src/error.rs | 4 +-- crates/wasi-c2/src/random.rs | 42 +++++++++-------------- crates/wasi-c2/src/snapshots/preview_1.rs | 4 +-- 6 files changed, 37 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 81952eea6e..55d3ba7666 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -241,6 +241,15 @@ dependencies = [ "winx 0.21.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "cap-rand" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8289d98c0c748a22f1815bc68159255ca059cb4170371c8ab16b11b26886698c" +dependencies = [ + "rand", +] + [[package]] name = "cap-std" version = "0.7.0" @@ -2437,11 +2446,11 @@ version = "0.21.0" dependencies = [ "anyhow", "cap-fs-ext", + "cap-rand", "cap-std 0.8.0", "cap-time-ext", "cfg-if 1.0.0", "fs-set-times", - "getrandom 0.2.0", "libc", "system-interface", "thiserror", diff --git a/crates/wasi-c2/Cargo.toml b/crates/wasi-c2/Cargo.toml index db59f0ffae..e808da345c 100644 --- a/crates/wasi-c2/Cargo.toml +++ b/crates/wasi-c2/Cargo.toml @@ -22,13 +22,13 @@ links = "wasi-c2-19" anyhow = "1.0" thiserror = "1.0" libc = "0.2" -getrandom = { version = "0.2.0", features = ["std"] } wiggle = { path = "../wiggle", default-features = false, version = "0.21.0" } tracing = "0.1.19" system-interface = "0.2" cap-std = "0.8" cap-fs-ext = "0.8" cap-time-ext = "0.8" +cap-rand = "0.8" fs-set-times = "0.2.1" cfg-if = "1" diff --git a/crates/wasi-c2/src/ctx.rs b/crates/wasi-c2/src/ctx.rs index cb27328df1..25792c4d5a 100644 --- a/crates/wasi-c2/src/ctx.rs +++ b/crates/wasi-c2/src/ctx.rs @@ -1,10 +1,10 @@ use crate::clocks::{WasiMonotonicClock, WasiSystemClock}; use crate::dir::{DirCaps, DirEntry, WasiDir}; use crate::file::{FileCaps, FileEntry, WasiFile}; -use crate::random::WasiRandom; use crate::string_array::{StringArray, StringArrayError}; use crate::table::Table; use crate::Error; +use cap_rand::RngCore; use std::cell::{RefCell, RefMut}; use std::path::{Path, PathBuf}; use std::rc::Rc; @@ -12,7 +12,7 @@ use std::rc::Rc; pub struct WasiCtx { pub(crate) args: StringArray, pub(crate) env: StringArray, - pub(crate) random: Box, + pub(crate) random: RefCell>, pub(crate) clocks: WasiCtxClocks, table: Rc>, } @@ -26,7 +26,7 @@ impl WasiCtx { WasiCtx { args: StringArray::new(), env: StringArray::new(), - random: Box::new(crate::random::GetRandom), + random: RefCell::new(Box::new(unsafe { cap_rand::rngs::OsRng::default() })), clocks: WasiCtxClocks::default(), table: Rc::new(RefCell::new(Table::new())), } @@ -117,8 +117,8 @@ impl WasiCtxBuilder { Ok(self) } - pub fn random(&mut self, random: Box) -> &mut Self { - self.0.random = random; + pub fn random(&mut self, random: Box) -> &mut Self { + self.0.random.replace(random); self } } diff --git a/crates/wasi-c2/src/error.rs b/crates/wasi-c2/src/error.rs index cd896da1b5..7a592f08ce 100644 --- a/crates/wasi-c2/src/error.rs +++ b/crates/wasi-c2/src/error.rs @@ -13,8 +13,8 @@ pub enum Error { TryFromInt(#[from] std::num::TryFromIntError), #[error("Utf8Error: {0}")] Utf8(#[from] std::str::Utf8Error), - #[error("GetRandom: {0}")] - GetRandom(#[from] getrandom::Error), + #[error("cap_rand Error: {0}")] + CapRand(#[from] cap_rand::Error), /// Errno::Notcapable: Extension: Capabilities insufficient #[error("File not capable: desired {desired:?}, has {has:?}")] diff --git a/crates/wasi-c2/src/random.rs b/crates/wasi-c2/src/random.rs index 6412c3e0c5..45e4cb0ea9 100644 --- a/crates/wasi-c2/src/random.rs +++ b/crates/wasi-c2/src/random.rs @@ -1,40 +1,32 @@ -use crate::Error; -use std::cell::RefCell; - -pub trait WasiRandom { - fn get(&self, buf: &mut [u8]) -> Result<(), Error>; -} - -/// Implement `WasiRandom` using the `getrandom` crate, which selects your system's best entropy -/// source. -pub struct GetRandom; - -impl WasiRandom for GetRandom { - fn get(&self, buf: &mut [u8]) -> Result<(), Error> { - getrandom::getrandom(buf)?; - Ok(()) - } -} +use cap_rand::RngCore; /// Implement `WasiRandom` using a deterministic cycle of bytes. pub struct Deterministic { - sequence: RefCell>>, + cycle: std::iter::Cycle>, } impl Deterministic { pub fn new(bytes: Vec) -> Self { Deterministic { - sequence: RefCell::new(bytes.into_iter().cycle()), + cycle: bytes.into_iter().cycle(), } } } -impl WasiRandom for Deterministic { - fn get(&self, buf: &mut [u8]) -> Result<(), Error> { - let mut s = self.sequence.borrow_mut(); +impl RngCore for Deterministic { + fn next_u32(&mut self) -> u32 { + todo!() + } + fn next_u64(&mut self) -> u64 { + todo!() + } + fn fill_bytes(&mut self, buf: &mut [u8]) { for b in buf.iter_mut() { - *b = s.next().expect("infinite sequence"); + *b = self.cycle.next().expect("infinite sequence"); } + } + fn try_fill_bytes(&mut self, buf: &mut [u8]) -> Result<(), cap_rand::Error> { + self.fill_bytes(buf); Ok(()) } } @@ -44,9 +36,9 @@ mod test { use super::*; #[test] fn deterministic() { - let det = Deterministic::new(vec![1, 2, 3, 4]); + let mut det = Deterministic::new(vec![1, 2, 3, 4]); let mut buf = vec![0; 1024]; - det.get(&mut buf).expect("get randomness"); + det.try_fill_bytes(&mut buf).expect("get randomness"); for (ix, b) in buf.iter().enumerate() { assert_eq!(*b, (ix % 4) as u8 + 1) } diff --git a/crates/wasi-c2/src/snapshots/preview_1.rs b/crates/wasi-c2/src/snapshots/preview_1.rs index 3ede1f84df..a1de4d3de7 100644 --- a/crates/wasi-c2/src/snapshots/preview_1.rs +++ b/crates/wasi-c2/src/snapshots/preview_1.rs @@ -65,7 +65,7 @@ impl From for types::Errno { | ErrorKind::UnexpectedEof | _ => Errno::Io, }, - Error::GetRandom(_) => Errno::Io, + Error::CapRand(_) => Errno::Io, Error::TooBig => Errno::TooBig, Error::Acces => Errno::Acces, Error::Badf => Errno::Badf, @@ -824,7 +824,7 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { fn random_get(&self, buf: &GuestPtr, buf_len: types::Size) -> Result<(), Error> { let mut buf = buf.as_array(buf_len).as_slice_mut()?; - self.random.get(buf.deref_mut())?; + self.random.borrow_mut().try_fill_bytes(buf.deref_mut())?; Ok(()) }