From 14dbba275929f19c3426a05c28e53879e3dc6703 Mon Sep 17 00:00:00 2001 From: Yage Hu Date: Tue, 28 May 2024 10:51:13 -0700 Subject: [PATCH] Disallow empty path on path_open This commit makes `path_open` return `inval` when passed an empty path. This behavior is consistent with other Wasm runtimes (Wasmtime, Wasmer, Node, WAMR, WasmEdge). fixes #2222 Signed-off-by: Yage Hu --- imports/wasi_snapshot_preview1/fs.go | 4 ++++ imports/wasi_snapshot_preview1/fs_test.go | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/imports/wasi_snapshot_preview1/fs.go b/imports/wasi_snapshot_preview1/fs.go index 384036a275..a3559e5df2 100644 --- a/imports/wasi_snapshot_preview1/fs.go +++ b/imports/wasi_snapshot_preview1/fs.go @@ -1596,6 +1596,10 @@ func pathOpenFn(_ context.Context, mod api.Module, params []uint64) experimental return errno } + if pathLen == 0 { + return experimentalsys.EINVAL + } + fileOpenFlags := openFlags(dirflags, oflags, fdflags, rights) isDir := fileOpenFlags&experimentalsys.O_DIRECTORY != 0 diff --git a/imports/wasi_snapshot_preview1/fs_test.go b/imports/wasi_snapshot_preview1/fs_test.go index 9743d74865..037d34d61e 100644 --- a/imports/wasi_snapshot_preview1/fs_test.go +++ b/imports/wasi_snapshot_preview1/fs_test.go @@ -4095,6 +4095,17 @@ func Test_pathOpen_Errors(t *testing.T) { expectedLog: ` ==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=OOM(65536,4),oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=) <== (opened_fd=,errno=EFAULT) +`, + }, + { + name: "empty path", + fd: sys.FdPreopen, + path: 0, + pathLen: 0, + expectedErrno: wasip1.ErrnoInval, + expectedLog: ` +==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=) +<== (opened_fd=,errno=EINVAL) `, }, {