Repository navigation
Open
Labels
bugIncorrect behavior in the current implementation that needs fixing
Description
Test Case
Steps to Reproduce
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
mkdirat(AT_FDCWD, "testdir", 0777);
{
// Opening with O_RDWR fails as expected if the dirfd is AT_FDCWD
int dir = openat(AT_FDCWD, "testdir", O_RDWR);
if (dir != -1) return 1;
}
// Open "testdir" for real now since we want to use it as the dirfd for openat
int dir = openat(AT_FDCWD, "testdir", O_RDONLY|O_DIRECTORY);
mkdirat(dir, "subdir", 0777);
// Try to open the subdir with O_RDWR, this should fail with EISDIR but it succeeds
// because the O_RDWR is lost and the openat2 syscall is called with O_RDONLY
// instead
int testdir = openat(dir, "subdir", O_RDWR);
if (testdir != -1) return 1;
return 0;
}Built via:
$ WASI_SDK=/home/ryan/Downloads/wasi-sdk-23.0-x86_64-linux
$ $WASI_SDK/bin/clang --sysroot=$WASI_SDK/share/wasi-sysroot open-dir-tmpdir-rw.c -o open-dir-tmpdir-rw-sdk-23.wasmRun with strace via:
$ strace -e trace=openat2 wasmtime --dir=. open-dir-tmpdir-rw-sdk-23.wasm
openat2(3, "testdir", {flags=O_RDWR|O_LARGEFILE|O_CLOEXEC, resolve=RESOLVE_NO_MAGICLINKS|RESOLVE_BENEATH}, 24) = -1 EISDIR (Is a directory)
openat2(3, "testdir", {flags=O_RDONLY|O_LARGEFILE|O_CLOEXEC, resolve=RESOLVE_NO_MAGICLINKS|RESOLVE_BENEATH}, 24) = 11
openat2(11, "subdir", {flags=O_RDONLY|O_LARGEFILE|O_CLOEXEC, resolve=RESOLVE_NO_MAGICLINKS|RESOLVE_BENEATH}, 24) = 12
+++ exited with 1 +++
Expected Results
The test program should exit with 0, and the last openat call should have O_RDWR set instead of O_RDONLY.
Actual Results
The test program exits with 1, since the last openat call loses the O_RDWR flag and erroneously sets O_RDONLY instead.
Versions and Environment
Wasmtime version or commit: wasi-sdk 23.0 and wasmtime 23.0.1
Operating system: Linux
Architecture: x86_64
Extra Info
This is a partial regression of a bug that was fixed by #6463. This bug is not present in wasmtime versions v9.0.1-v14.0.4
wasi-libc issue: WebAssembly/wasi-libc#415
Metadata
Metadata
Assignees
Labels
bugIncorrect behavior in the current implementation that needs fixing
Activity
alexcrichton commentedon Aug 1, 2024
Thanks for the report! It's not clear to me precisely where the fault lies when debugging this. Notes that follow are intended not as a fix for this issue but what I've found so far.
I've determined that this is "just a difference" between
-Spreview2={y,n}which iswasi-common(old) vswasmtime-wasi(new (ish)). The default,wasmtime-wasi, fails this test whilewasi-commonpasses it. It appears due to this line wherefd_fdstat_geton thedirvariable in the program above is returning different results. This does not look related topath_open.In
wasmtime-wasithefd_fdstat_getcall returnswhereas
wasi-commonreturns:notably there's a difference in
fs_rights_inheriting. This means that withwasi-commonthe request to write this directory is NOT masked out, hence the error. Withwasmtime-wasithe request to write is masked out becausefs_rights_inheritingdoesn't have the write bit set.@pchickey as the liberator of rights (hah!) are you able to recall the history around this? Is this something where we should update
wasmtime-wasito have the same fixed return value offs_rights_inheriting? Or should we update wasi-libc to do something different about masking here?