Skip to content

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

filesystem: ErrorCode::IsDirectory when opening directory with WRITE (#12136)

Andy Wingo9fd594a357fe
diff --git a/crates/test-programs/src/bin/p1_path_open_preopen.rs b/crates/test-programs/src/bin/p1_path_open_preopen.rs
index eed1e80c7d..91d981855e 100644
--- a/crates/test-programs/src/bin/p1_path_open_preopen.rs
+++ b/crates/test-programs/src/bin/p1_path_open_preopen.rs
@@ -66,37 +66,23 @@ unsafe fn path_open_preopen() {
)
.expect("open with O_DIRECTORY and read right");
- if !test_programs::preview1::config().errno_expect_windows() {
- // Open OFLAGS_DIRECTORY and read/write rights should fail with isdir:
- let err = wasip1::path_open(
- FIRST_PREOPEN,
- 0,
- ".",
- wasip1::OFLAGS_DIRECTORY,
- wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE,
- 0,
- 0,
- )
- .err()
- .expect("open with O_DIRECTORY and read/write should fail");
- assert_eq!(
- err,
- wasip1::ERRNO_ISDIR,
- "opening directory read/write should fail with ISDIR"
- );
- } else {
- // Open OFLAGS_DIRECTORY and read/write rights will succeed, only on windows:
- let _ = wasip1::path_open(
- FIRST_PREOPEN,
- 0,
- ".",
- wasip1::OFLAGS_DIRECTORY,
- wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE,
- 0,
- 0,
- )
- .expect("open with O_DIRECTORY and read/write should succeed on windows");
- }
+ // Open OFLAGS_DIRECTORY and read/write rights should fail with isdir:
+ let err = wasip1::path_open(
+ FIRST_PREOPEN,
+ 0,
+ ".",
+ wasip1::OFLAGS_DIRECTORY,
+ wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE,
+ 0,
+ 0,
+ )
+ .err()
+ .expect("open with O_DIRECTORY and read/write should fail");
+ assert_eq!(
+ err,
+ wasip1::ERRNO_ISDIR,
+ "opening directory read/write should fail with ISDIR"
+ );
}
fn main() {
diff --git a/crates/wasi-common/src/snapshots/preview_1.rs b/crates/wasi-common/src/snapshots/preview_1.rs
index 2c57fd3068..feb87bb5d3 100644
--- a/crates/wasi-common/src/snapshots/preview_1.rs
+++ b/crates/wasi-common/src/snapshots/preview_1.rs
@@ -814,6 +814,13 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
drop(dir_entry);
let fd = match file {
+ // Paper over a divergence between Windows and POSIX, where
+ // POSIX returns EISDIR if you open a directory with the
+ // WRITE flag: https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html#:~:text=EISDIR
+ #[cfg(windows)]
+ OpenResult::Dir(_) if write => {
+ return Err(types::Errno::Isdir.into());
+ }
OpenResult::File(file) => table.push(Arc::new(FileEntry::new(file, access_mode)))?,
OpenResult::Dir(child_dir) => table.push(Arc::new(DirEntry::new(None, child_dir)))?,
};
diff --git a/crates/wasi/src/filesystem.rs b/crates/wasi/src/filesystem.rs
index c5e3b54b1a..0d640e40ab 100644
--- a/crates/wasi/src/filesystem.rs
+++ b/crates/wasi/src/filesystem.rs
@@ -1045,6 +1045,14 @@ impl Dir {
.await?;
match opened {
+ // Paper over a divergence between Windows and POSIX, where
+ // POSIX returns EISDIR if you open a directory with the
+ // WRITE flag: https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html#:~:text=EISDIR
+ #[cfg(windows)]
+ OpenResult::Dir(_) if flags.contains(DescriptorFlags::WRITE) => {
+ Err(ErrorCode::IsDirectory)
+ }
+
OpenResult::Dir(dir) => Ok(Descriptor::Dir(Dir::new(
dir,
self.perms,
diff --git a/tests/wasi.rs b/tests/wasi.rs
index 054e9c6749..0a467dcf5e 100644
--- a/tests/wasi.rs
+++ b/tests/wasi.rs
@@ -64,8 +64,6 @@ const KNOWN_FAILURES: &[&str] = &[
#[cfg(windows)]
"path_open_missing",
#[cfg(windows)]
- "path_open_preopen",
- #[cfg(windows)]
"path_open_read_write",
#[cfg(windows)]
"path_rename",