Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

wasip3: Limit randomness instead of trapping (#12794)

Alex Crichton75404ec9fd7a
diff --git a/crates/wasi/src/p3/random/host.rs b/crates/wasi/src/p3/random/host.rs
index 163248fcd5..0bb466b7ab 100644
--- a/crates/wasi/src/p3/random/host.rs
+++ b/crates/wasi/src/p3/random/host.rs
@@ -2,16 +2,12 @@ use crate::p3::bindings::random::{insecure, insecure_seed, random};
use crate::random::WasiRandomCtx;
use cap_rand::Rng;
use cap_rand::distributions::Standard;
-use wasmtime::bail;
impl random::Host for WasiRandomCtx {
fn get_random_bytes(&mut self, len: u64) -> wasmtime::Result<Vec<u8>> {
- if len > self.max_size {
- bail!("requested len {len:?} exceeds limit {}", self.max_size);
- }
Ok((&mut self.random)
.sample_iter(Standard)
- .take(len as usize)
+ .take(len.min(self.max_size) as usize)
.collect())
}
@@ -22,12 +18,9 @@ impl random::Host for WasiRandomCtx {
impl insecure::Host for WasiRandomCtx {
fn get_insecure_random_bytes(&mut self, len: u64) -> wasmtime::Result<Vec<u8>> {
- if len > self.max_size {
- bail!("requested len {len:?} exceeds limit {}", self.max_size);
- }
Ok((&mut self.insecure_random)
.sample_iter(Standard)
- .take(len as usize)
+ .take(len.min(self.max_size) as usize)
.collect())
}
diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs
index 682765c1ca..36995895e2 100644
--- a/tests/all/cli_tests.rs
+++ b/tests/all/cli_tests.rs
@@ -2844,18 +2844,15 @@ start a print 1234
for rand in ["random", "insecure"] {
run_wasmtime(&["run", "-Sp3", "-Wcomponent-model-async", c, rand, "256"])?;
- assert!(
- run_wasmtime(&[
- "run",
- "-Sp3",
- "-Wcomponent-model-async",
- "-Smax-random-size=255",
- c,
- rand,
- "256"
- ])
- .is_err()
- );
+ run_wasmtime(&[
+ "run",
+ "-Sp3",
+ "-Wcomponent-model-async",
+ "-Smax-random-size=255",
+ c,
+ rand,
+ "256",
+ ])?;
}
Ok(())