use cap_rand instead of getrandom
This commit is contained in:
11
Cargo.lock
generated
11
Cargo.lock
generated
@@ -241,6 +241,15 @@ dependencies = [
|
|||||||
"winx 0.21.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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]]
|
[[package]]
|
||||||
name = "cap-std"
|
name = "cap-std"
|
||||||
version = "0.7.0"
|
version = "0.7.0"
|
||||||
@@ -2437,11 +2446,11 @@ version = "0.21.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"cap-fs-ext",
|
"cap-fs-ext",
|
||||||
|
"cap-rand",
|
||||||
"cap-std 0.8.0",
|
"cap-std 0.8.0",
|
||||||
"cap-time-ext",
|
"cap-time-ext",
|
||||||
"cfg-if 1.0.0",
|
"cfg-if 1.0.0",
|
||||||
"fs-set-times",
|
"fs-set-times",
|
||||||
"getrandom 0.2.0",
|
|
||||||
"libc",
|
"libc",
|
||||||
"system-interface",
|
"system-interface",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
|
|||||||
@@ -22,13 +22,13 @@ links = "wasi-c2-19"
|
|||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
thiserror = "1.0"
|
thiserror = "1.0"
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
getrandom = { version = "0.2.0", features = ["std"] }
|
|
||||||
wiggle = { path = "../wiggle", default-features = false, version = "0.21.0" }
|
wiggle = { path = "../wiggle", default-features = false, version = "0.21.0" }
|
||||||
tracing = "0.1.19"
|
tracing = "0.1.19"
|
||||||
system-interface = "0.2"
|
system-interface = "0.2"
|
||||||
cap-std = "0.8"
|
cap-std = "0.8"
|
||||||
cap-fs-ext = "0.8"
|
cap-fs-ext = "0.8"
|
||||||
cap-time-ext = "0.8"
|
cap-time-ext = "0.8"
|
||||||
|
cap-rand = "0.8"
|
||||||
fs-set-times = "0.2.1"
|
fs-set-times = "0.2.1"
|
||||||
cfg-if = "1"
|
cfg-if = "1"
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
use crate::clocks::{WasiMonotonicClock, WasiSystemClock};
|
use crate::clocks::{WasiMonotonicClock, WasiSystemClock};
|
||||||
use crate::dir::{DirCaps, DirEntry, WasiDir};
|
use crate::dir::{DirCaps, DirEntry, WasiDir};
|
||||||
use crate::file::{FileCaps, FileEntry, WasiFile};
|
use crate::file::{FileCaps, FileEntry, WasiFile};
|
||||||
use crate::random::WasiRandom;
|
|
||||||
use crate::string_array::{StringArray, StringArrayError};
|
use crate::string_array::{StringArray, StringArrayError};
|
||||||
use crate::table::Table;
|
use crate::table::Table;
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
|
use cap_rand::RngCore;
|
||||||
use std::cell::{RefCell, RefMut};
|
use std::cell::{RefCell, RefMut};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
@@ -12,7 +12,7 @@ use std::rc::Rc;
|
|||||||
pub struct WasiCtx {
|
pub struct WasiCtx {
|
||||||
pub(crate) args: StringArray,
|
pub(crate) args: StringArray,
|
||||||
pub(crate) env: StringArray,
|
pub(crate) env: StringArray,
|
||||||
pub(crate) random: Box<dyn WasiRandom>,
|
pub(crate) random: RefCell<Box<dyn RngCore>>,
|
||||||
pub(crate) clocks: WasiCtxClocks,
|
pub(crate) clocks: WasiCtxClocks,
|
||||||
table: Rc<RefCell<Table>>,
|
table: Rc<RefCell<Table>>,
|
||||||
}
|
}
|
||||||
@@ -26,7 +26,7 @@ impl WasiCtx {
|
|||||||
WasiCtx {
|
WasiCtx {
|
||||||
args: StringArray::new(),
|
args: StringArray::new(),
|
||||||
env: 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(),
|
clocks: WasiCtxClocks::default(),
|
||||||
table: Rc::new(RefCell::new(Table::new())),
|
table: Rc::new(RefCell::new(Table::new())),
|
||||||
}
|
}
|
||||||
@@ -117,8 +117,8 @@ impl WasiCtxBuilder {
|
|||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn random(&mut self, random: Box<dyn WasiRandom>) -> &mut Self {
|
pub fn random(&mut self, random: Box<dyn RngCore>) -> &mut Self {
|
||||||
self.0.random = random;
|
self.0.random.replace(random);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ pub enum Error {
|
|||||||
TryFromInt(#[from] std::num::TryFromIntError),
|
TryFromInt(#[from] std::num::TryFromIntError),
|
||||||
#[error("Utf8Error: {0}")]
|
#[error("Utf8Error: {0}")]
|
||||||
Utf8(#[from] std::str::Utf8Error),
|
Utf8(#[from] std::str::Utf8Error),
|
||||||
#[error("GetRandom: {0}")]
|
#[error("cap_rand Error: {0}")]
|
||||||
GetRandom(#[from] getrandom::Error),
|
CapRand(#[from] cap_rand::Error),
|
||||||
|
|
||||||
/// Errno::Notcapable: Extension: Capabilities insufficient
|
/// Errno::Notcapable: Extension: Capabilities insufficient
|
||||||
#[error("File not capable: desired {desired:?}, has {has:?}")]
|
#[error("File not capable: desired {desired:?}, has {has:?}")]
|
||||||
|
|||||||
@@ -1,40 +1,32 @@
|
|||||||
use crate::Error;
|
use cap_rand::RngCore;
|
||||||
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(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Implement `WasiRandom` using a deterministic cycle of bytes.
|
/// Implement `WasiRandom` using a deterministic cycle of bytes.
|
||||||
pub struct Deterministic {
|
pub struct Deterministic {
|
||||||
sequence: RefCell<std::iter::Cycle<std::vec::IntoIter<u8>>>,
|
cycle: std::iter::Cycle<std::vec::IntoIter<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deterministic {
|
impl Deterministic {
|
||||||
pub fn new(bytes: Vec<u8>) -> Self {
|
pub fn new(bytes: Vec<u8>) -> Self {
|
||||||
Deterministic {
|
Deterministic {
|
||||||
sequence: RefCell::new(bytes.into_iter().cycle()),
|
cycle: bytes.into_iter().cycle(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WasiRandom for Deterministic {
|
impl RngCore for Deterministic {
|
||||||
fn get(&self, buf: &mut [u8]) -> Result<(), Error> {
|
fn next_u32(&mut self) -> u32 {
|
||||||
let mut s = self.sequence.borrow_mut();
|
todo!()
|
||||||
|
}
|
||||||
|
fn next_u64(&mut self) -> u64 {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
fn fill_bytes(&mut self, buf: &mut [u8]) {
|
||||||
for b in buf.iter_mut() {
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -44,9 +36,9 @@ mod test {
|
|||||||
use super::*;
|
use super::*;
|
||||||
#[test]
|
#[test]
|
||||||
fn deterministic() {
|
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];
|
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() {
|
for (ix, b) in buf.iter().enumerate() {
|
||||||
assert_eq!(*b, (ix % 4) as u8 + 1)
|
assert_eq!(*b, (ix % 4) as u8 + 1)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ impl From<Error> for types::Errno {
|
|||||||
| ErrorKind::UnexpectedEof
|
| ErrorKind::UnexpectedEof
|
||||||
| _ => Errno::Io,
|
| _ => Errno::Io,
|
||||||
},
|
},
|
||||||
Error::GetRandom(_) => Errno::Io,
|
Error::CapRand(_) => Errno::Io,
|
||||||
Error::TooBig => Errno::TooBig,
|
Error::TooBig => Errno::TooBig,
|
||||||
Error::Acces => Errno::Acces,
|
Error::Acces => Errno::Acces,
|
||||||
Error::Badf => Errno::Badf,
|
Error::Badf => Errno::Badf,
|
||||||
@@ -824,7 +824,7 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
|||||||
|
|
||||||
fn random_get(&self, buf: &GuestPtr<u8>, buf_len: types::Size) -> Result<(), Error> {
|
fn random_get(&self, buf: &GuestPtr<u8>, buf_len: types::Size) -> Result<(), Error> {
|
||||||
let mut buf = buf.as_array(buf_len).as_slice_mut()?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user