Skip to content

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

wasi:io: enforce the check-write permit in output streams that skipped it (#14429)

Xia Chao48285bf3e425
diff --git a/crates/test-programs/src/bin/p2_write_too_much.rs b/crates/test-programs/src/bin/p2_write_too_much.rs
new file mode 100644
index 0000000000..e2ecb40765
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_write_too_much.rs
@@ -0,0 +1,26 @@
+use test_programs::wasi::filesystem::preopens;
+use test_programs::wasi::filesystem::types::{DescriptorFlags, OpenFlags, PathFlags};
+
+/// An output stream should trap when it is asked to write more bytes than
+/// `check-write` permitted.
+fn main() {
+ let (dir, _) = preopens::get_directories().into_iter().next().unwrap();
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ "write-too-much",
+ OpenFlags::CREATE,
+ DescriptorFlags::READ | DescriptorFlags::WRITE,
+ )
+ .unwrap();
+ let out = file.write_via_stream(0).unwrap();
+
+ let permit = out.check_write().unwrap();
+ assert!(permit > 0);
+
+ // This should trap according to the `wasi:io/streams` spec since we're
+ // trying to write more bytes than `check-write` gave permission for:
+ let contents = vec![0; usize::try_from(permit + 1).unwrap()];
+ out.write(&contents).unwrap();
+ unreachable!("attempt to write more bytes than permitted should have trapped");
+}
diff --git a/crates/wasi/src/cli/stdout.rs b/crates/wasi/src/cli/stdout.rs
index 0ffe2ee976..6995c76882 100644
--- a/crates/wasi/src/cli/stdout.rs
+++ b/crates/wasi/src/cli/stdout.rs
@@ -72,8 +72,17 @@ enum StdioOutputStream {
Stderr,
}
+/// The number of bytes a single `write` is permitted to carry, as reported by
+/// `check_write`.
+const WRITE_BUDGET: usize = 1024 * 1024;
+
impl OutputStream for StdioOutputStream {
fn write(&mut self, bytes: Bytes) -> p2::StreamResult<()> {
+ if bytes.len() > WRITE_BUDGET {
+ return Err(p2::StreamError::Trap(wasmtime::format_err!(
+ "write exceeded budget"
+ )));
+ }
match self {
StdioOutputStream::Stdout => std::io::stdout().write_all(&bytes),
StdioOutputStream::Stderr => std::io::stderr().write_all(&bytes),
@@ -90,7 +99,23 @@ impl OutputStream for StdioOutputStream {
}
fn check_write(&mut self) -> p2::StreamResult<usize> {
- Ok(1024 * 1024)
+ Ok(WRITE_BUDGET)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ /// A write larger than the budget `check_write` reports has to trap instead
+ /// of being written out.
+ #[test]
+ fn write_larger_than_budget_traps() {
+ let mut stream = StdioOutputStream::Stdout;
+ let err = stream
+ .write(Bytes::from(vec![0; WRITE_BUDGET + 1]))
+ .unwrap_err();
+ assert!(matches!(err, p2::StreamError::Trap(_)), "{err:?}");
}
}
diff --git a/crates/wasi/src/p2/filesystem.rs b/crates/wasi/src/p2/filesystem.rs
index cad47f68d0..f0a1d0589d 100644
--- a/crates/wasi/src/p2/filesystem.rs
+++ b/crates/wasi/src/p2/filesystem.rs
@@ -280,7 +280,10 @@ const FILE_WRITE_CAPACITY: usize = 1024 * 1024;
impl OutputStream for FileOutputStream {
fn write(&mut self, buf: Bytes) -> Result<(), StreamError> {
match self.state {
- OutputState::Ready => {}
+ OutputState::Ready if buf.len() <= FILE_WRITE_CAPACITY => {}
+ OutputState::Ready => {
+ return Err(StreamError::Trap(format_err!("write exceeded budget")));
+ }
OutputState::Closed => return Err(StreamError::Closed),
OutputState::Waiting(_) | OutputState::Error(_) => {
// a write is pending - this call was not permitted
diff --git a/crates/wasi/tests/all/p2/async_.rs b/crates/wasi/tests/all/p2/async_.rs
index 81bb0955f1..cc8b9edf3c 100644
--- a/crates/wasi/tests/all/p2/async_.rs
+++ b/crates/wasi/tests/all/p2/async_.rs
@@ -391,6 +391,14 @@ async fn p2_udp_send_too_much() {
)
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_write_too_much() {
+ let e = run(P2_WRITE_TOO_MUCH_COMPONENT, |_| {}).await.unwrap_err();
+ assert_eq!(
+ format!("{}", e.source().expect("trap source")),
+ "write exceeded budget"
+ )
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_file_settime_overflow() {
run(P2_FILE_SETTIME_OVERFLOW_COMPONENT, |_| {})
.await
diff --git a/crates/wasi/tests/all/p2/sync.rs b/crates/wasi/tests/all/p2/sync.rs
index 32fbe17873..78cd1bbcea 100644
--- a/crates/wasi/tests/all/p2/sync.rs
+++ b/crates/wasi/tests/all/p2/sync.rs
@@ -367,6 +367,14 @@ fn p2_udp_send_too_much() {
)
}
#[test_log::test]
+fn p2_write_too_much() {
+ let e = run(P2_WRITE_TOO_MUCH_COMPONENT, |_| {}).unwrap_err();
+ assert_eq!(
+ format!("{}", e.source().expect("trap source")),
+ "write exceeded budget"
+ )
+}
+#[test_log::test]
fn p2_file_settime_overflow() {
run(P2_FILE_SETTIME_OVERFLOW_COMPONENT, |_| {}).unwrap()
}