Skip to content

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

wasi: return NotPermitted for stream open permission failures (#13816)

Sebastien Tardife59c8cfbce6f
diff --git a/crates/test-programs/src/bin/p2_file_stream_not_permitted.rs b/crates/test-programs/src/bin/p2_file_stream_not_permitted.rs
new file mode 100644
index 0000000000..88436f666d
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_file_stream_not_permitted.rs
@@ -0,0 +1,83 @@
+//! Stream open paths must return `not-permitted` when the preopen denies the
+//! access mode, matching non-stream `read`/`write` (not `bad-descriptor`).
+
+use test_programs::wasi::filesystem::preopens;
+use test_programs::wasi::filesystem::types::{DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
+
+fn main() {
+ let preopens = preopens::get_directories();
+
+ // --- readonly preopen: write/append streams denied, read stream allowed ---
+ let (readonly_dir, _) = preopens
+ .iter()
+ .find(|(_, path)| path == "readonly")
+ .expect("find preopen named readonly");
+
+ let file = readonly_dir
+ .open_at(
+ PathFlags::empty(),
+ "stream-perms.txt",
+ OpenFlags::empty(),
+ DescriptorFlags::READ,
+ )
+ .expect("open for reading");
+
+ let err = file
+ .write_via_stream(0)
+ .expect_err("write_via_stream without write permission");
+ assert_eq!(
+ err,
+ ErrorCode::NotPermitted,
+ "write_via_stream should return not-permitted, got {err:?}"
+ );
+
+ let err = file
+ .append_via_stream()
+ .expect_err("append_via_stream without write permission");
+ assert_eq!(
+ err,
+ ErrorCode::NotPermitted,
+ "append_via_stream should return not-permitted, got {err:?}"
+ );
+
+ let stream = file
+ .read_via_stream(0)
+ .expect("read_via_stream with read permission");
+ let contents = stream.blocking_read(100).expect("read file contents");
+ drop(stream);
+ drop(file);
+ assert_eq!(contents, b"stream permission test\n");
+
+ // --- writeonly preopen: read stream denied, write stream allowed ---
+ let (writeonly_dir, _) = preopens
+ .iter()
+ .find(|(_, path)| path == "writeonly")
+ .expect("find preopen named writeonly");
+
+ let file = writeonly_dir
+ .open_at(
+ PathFlags::empty(),
+ "stream-write.txt",
+ OpenFlags::empty(),
+ DescriptorFlags::WRITE,
+ )
+ .expect("open for writing");
+
+ let err = file
+ .read_via_stream(0)
+ .expect_err("read_via_stream without read permission");
+ assert_eq!(
+ err,
+ ErrorCode::NotPermitted,
+ "read_via_stream should return not-permitted, got {err:?}"
+ );
+
+ let stream = file
+ .write_via_stream(0)
+ .expect("write_via_stream with write permission");
+ stream
+ .blocking_write_and_flush(b"ok")
+ .expect("write stream contents");
+ drop(stream);
+ drop(file);
+}
diff --git a/crates/wasi/src/p2/host/filesystem.rs b/crates/wasi/src/p2/host/filesystem.rs
index 773b48191b..cc7addb893 100644
--- a/crates/wasi/src/p2/host/filesystem.rs
+++ b/crates/wasi/src/p2/host/filesystem.rs
@@ -378,7 +378,7 @@ impl HostDescriptor for WasiFilesystemCtxView<'_> {
let f = self.table.get(&fd)?.file()?;
if !f.perms.contains(FilePerms::READ) {
- Err(types::ErrorCode::BadDescriptor)?;
+ Err(types::ErrorCode::NotPermitted)?;
}
// Create a stream view for it.
@@ -399,7 +399,7 @@ impl HostDescriptor for WasiFilesystemCtxView<'_> {
let f = self.table.get(&fd)?.file()?;
if !f.perms.contains(FilePerms::WRITE) {
- Err(types::ErrorCode::BadDescriptor)?;
+ Err(types::ErrorCode::NotPermitted)?;
}
// Create a stream view for it.
@@ -420,7 +420,7 @@ impl HostDescriptor for WasiFilesystemCtxView<'_> {
let f = self.table.get(&fd)?.file()?;
if !f.perms.contains(FilePerms::WRITE) {
- Err(types::ErrorCode::BadDescriptor)?;
+ Err(types::ErrorCode::NotPermitted)?;
}
// Create a stream view for it.
diff --git a/crates/wasi/tests/all/p2/async_.rs b/crates/wasi/tests/all/p2/async_.rs
index 9b87382b73..fe43baa0cc 100644
--- a/crates/wasi/tests/all/p2/async_.rs
+++ b/crates/wasi/tests/all/p2/async_.rs
@@ -453,6 +453,49 @@ async fn p2_file_rename_across_perms() {
run_with_readonly_testfile(P2_FILE_RENAME_ACROSS_PERMS_COMPONENT).await
}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_file_stream_not_permitted() {
+ file_stream_not_permitted(P2_FILE_STREAM_NOT_PERMITTED_COMPONENT).await
+}
+
+async fn file_stream_not_permitted(component_path: &str) {
+ use wasmtime_wasi::{DirPerms, FilePerms};
+
+ let readonly = tempfile::Builder::new()
+ .prefix("wasi_components_stream_np_ro_")
+ .tempdir()
+ .expect("create readonly tempdir");
+ let writeonly = tempfile::Builder::new()
+ .prefix("wasi_components_stream_np_wo_")
+ .tempdir()
+ .expect("create writeonly tempdir");
+
+ const RO_CONTENTS: &[u8] = b"stream permission test\n";
+ std::fs::write(readonly.path().join("stream-perms.txt"), RO_CONTENTS)
+ .expect("write readonly test file");
+ std::fs::write(writeonly.path().join("stream-write.txt"), b"")
+ .expect("create writeonly test file");
+
+ run(component_path, |b| {
+ b.preopened_dir(
+ readonly.path(),
+ "readonly",
+ DirPerms::READ | DirPerms::MUTATE,
+ FilePerms::READ,
+ )
+ .unwrap();
+ b.preopened_dir(
+ writeonly.path(),
+ "writeonly",
+ DirPerms::READ | DirPerms::MUTATE,
+ FilePerms::WRITE,
+ )
+ .unwrap();
+ })
+ .await
+ .expect("run p2_file_stream_not_permitted guest");
+}
+
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_clocks_zero_wait() {
run(P2_CLOCKS_ZERO_WAIT_COMPONENT, |_| {}).await.unwrap()
diff --git a/crates/wasi/tests/all/p2/sync.rs b/crates/wasi/tests/all/p2/sync.rs
index 46cb1e4a43..0c1aaf6fcd 100644
--- a/crates/wasi/tests/all/p2/sync.rs
+++ b/crates/wasi/tests/all/p2/sync.rs
@@ -421,6 +421,48 @@ fn p2_file_rename_across_perms() {
run_with_readonly_testfile(P2_FILE_RENAME_ACROSS_PERMS_COMPONENT)
}
+#[test_log::test]
+fn p2_file_stream_not_permitted() {
+ file_stream_not_permitted(P2_FILE_STREAM_NOT_PERMITTED_COMPONENT)
+}
+
+fn file_stream_not_permitted(component_path: &str) {
+ use wasmtime_wasi::{DirPerms, FilePerms};
+
+ let readonly = tempfile::Builder::new()
+ .prefix("wasi_components_stream_np_ro_")
+ .tempdir()
+ .expect("create readonly tempdir");
+ let writeonly = tempfile::Builder::new()
+ .prefix("wasi_components_stream_np_wo_")
+ .tempdir()
+ .expect("create writeonly tempdir");
+
+ const RO_CONTENTS: &[u8] = b"stream permission test\n";
+ std::fs::write(readonly.path().join("stream-perms.txt"), RO_CONTENTS)
+ .expect("write readonly test file");
+ std::fs::write(writeonly.path().join("stream-write.txt"), b"")
+ .expect("create writeonly test file");
+
+ run(component_path, |b| {
+ b.preopened_dir(
+ readonly.path(),
+ "readonly",
+ DirPerms::READ | DirPerms::MUTATE,
+ FilePerms::READ,
+ )
+ .unwrap();
+ b.preopened_dir(
+ writeonly.path(),
+ "writeonly",
+ DirPerms::READ | DirPerms::MUTATE,
+ FilePerms::WRITE,
+ )
+ .unwrap();
+ })
+ .expect("run p2_file_stream_not_permitted guest");
+}
+
#[test_log::test]
fn p2_clocks_zero_wait() {
run(P2_CLOCKS_ZERO_WAIT_COMPONENT, |_| {}).unwrap()