[WasmFS] Enforce open file access mode on read/write#27249
Conversation
writeAtOffset and readAtOffset had a TODO to check the open file access mode but did not, so writing to an O_RDONLY fd (or reading from an O_WRONLY fd) silently succeeded instead of failing with EBADF as POSIX requires. Return __WASI_ERRNO_BADF in those cases, matching the existing check in the mmap path.
|
It looks like the failure of the Are there any other tests that maybe now pass under wasmfs that did not before? |
Root cause: Fixed by having
no, nothing newly passes |
tlively
left a comment
There was a problem hiding this comment.
Code looks good, but we need testing. If there are no existing tests that are now expected to pass, then we should write a new test.
Summary
writeAtOffsetandreadAtOffsetinsystem/lib/wasmfs/syscalls.cppeach carried a// TODO: Check open file access mode for ... permissions.and never actually checked it.As a result, WasmFS let you:
O_RDONLY, andO_WRONLY,both of which silently succeeded instead of failing. POSIX
write(2)/read(2)requireEBADFwhen the descriptor is not open for the requested operation.The change mirrors the check WasmFS already performs in the mmap path
(
__syscall_mmap2, which returns-EACCESwhen the mapping requires an access mode thedescriptor doesn't have).
Motivation
Higher-level runtimes rely on the kernel enforcing the descriptor's access mode. For example,
.NET's
System.IOmapsEBADFon a write toUnauthorizedAccessException, so tests such asRandomAccess.Writeto a read-only handle andFileStreamflushing a buffer on a read-onlyhandle expect the native write to fail. Under WasmFS these currently pass silently, diverging
from the legacy (MEMFS) JS filesystem and from every other POSIX platform.
Related dotnet/runtime#130141
Behavior change / risk
O_RDONLYdescriptors or read fromO_WRONLYdescriptors and ignored the result will now observe
EBADF.