bytecodealliance / bytecodealliance/wasmtime
Access flags are lost when using `openat` from `wasi-libc`
- Dominant language
- Rust
- Stars
- 18.6k
- Forks
- 1.8k
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 126
Description
### Test Case
[open-dir-tmpdir-rw-sdk-23.zip](https://github.com/user-attachments/files/16448323/open-dir-tmpdir-rw-sdk-23.zip)
### Steps to Reproduce
```c
#include
#include
#include
#include
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:
```shellsession
$ 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.wasm
```
Run 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 https://github.com/bytecodealliance/wasmtime/pull/6463. This bug is not present in wasmtime versions `v9.0.1`-`v14.0.4`
wasi-libc issue: https://github.com/WebAssembly/wasi-libc/issues/415
Contributor guide
Assessment
This issue has not been assessed yet.