Skip to content

WasmFS: enforce open file access mode on read/write#27249

Open
pavelsavara wants to merge 1 commit into
emscripten-core:mainfrom
pavelsavara:wasmfs-enforce-write-access
Open

WasmFS: enforce open file access mode on read/write#27249
pavelsavara wants to merge 1 commit into
emscripten-core:mainfrom
pavelsavara:wasmfs-enforce-write-access

Conversation

@pavelsavara

@pavelsavara pavelsavara commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

writeAtOffset and readAtOffset in system/lib/wasmfs/syscalls.cpp each carried a
// TODO: Check open file access mode for ... permissions. and never actually checked it.
As a result, WasmFS let you:

  • write to a file descriptor opened O_RDONLY, and
  • read from a file descriptor opened O_WRONLY,

both of which silently succeeded instead of failing. POSIX write(2)/read(2) require
EBADF when 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 -EACCES when the mapping requires an access mode the
descriptor doesn't have).

Motivation

Higher-level runtimes rely on the kernel enforcing the descriptor's access mode. For example,
.NET's System.IO maps EBADF on a write to UnauthorizedAccessException, so tests such as
RandomAccess.Write to a read-only handle and FileStream flushing a buffer on a read-only
handle 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

  • Programs that (incorrectly) wrote to O_RDONLY descriptors or read from O_WRONLY
    descriptors and ignored the result will now observe EBADF.

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.
@sbc100

sbc100 commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

It looks like the failure of the other.test_wasmfs_jsfile test is probably valid?

Are there any other tests that maybe now pass under wasmfs that did not before?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants