Repository navigation
Conversation
CompatMemory serves addresses from SCRATCH_BASE up out of a per-call
scratch buffer of `held` bytes. scratch_range accepts an empty range
anywhere in the window, but read then split the buffer at
`end.min(held) - start`, which underflows when the empty range starts
past `held`. The host panicked ("attempt to subtract with overflow" in
debug, an out-of-range split_at_mut in release), and the runtime killed
the guest with SIGSEGV.
A v1 guest reaches it with fd_write or fd_read given iovs_len = 0 and an
iovs pointer in the window, since the kernel's read_iovecs reads
`vlen * 16` bytes even when vlen is 0, or with an empty iovec whose base
is in the window. Linux never faults a zero-length user copy, and
writev(fd, iov, 0) returns 0 without looking at iov.
Return success without copying for an empty read in the window.
Authored and committed by Claude (AI agent).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
A v1 guest could make the host panic with an empty read in the compat layer's scratch window. This PR makes that read succeed without copying anything.
The bug
CompatMemory(lib/wasix/wasix1/src/memory.rs) serves addresses fromSCRATCH_BASE(0xFF00_0000) up out of a per-call scratch buffer ofheldbytes.scratch_rangetreats an empty range anywhere in the window as valid.readthen did this:When an empty read starts past
held,copied - startunderflows:split_at_mutan out-of-range index, and it panics instead.The runtime catches host panics (
host_panicked), so the host process survives. The guest, though, is killed with SIGSEGV for a call that should have returned success.How a guest reaches it
fd_writeorfd_readwithiovs_len = 0andiovspointing into the window. The shim passes the iovec pointer through. The kernel'sread_iovecs(wlk/crates/kernel/src/fs/io.rs) then readsvlen * 16bytes, which is an empty read whenvlen == 0.On Linux none of these can fault. A zero-length
copy_from_usernever faults, andwritev(fd, iov, 0)returns 0 without looking atiov.The fix
An empty read in the window now copies nothing and succeeds (one line). The doc comment on
scratch_rangenow says so.I fixed it in
CompatMemoryrather than having the kernel skip zero-length reads. An empty read is harmless against every otherUserMemoryimplementation, so the defect is in this one's arithmetic.writeis left unchanged. It still returnsFaultfor an empty write pastheld. That is inconsistent withread, but it can't crash, and I found no guest path that reaches it.Test
empty_iovec_array_in_scratch_windowis inlib/wasix/wasix1/tests/guest.rs, written as a WAT guest like its neighbourhello_world. It covers the three paths above, and checks that each call succeeds with 0 bytes transferred.memory.rs,attempt to subtract with overflow.Checks
cargo fmt --check -p wasixv2-wasix1: clean.cargo clippy -p wasixv2-wasix1 --all-targets --locked -- -D clippy::all, with thelint-packagesRUSTFLAGS: clean. I ran clippy on this crate only, not the full workspacemake lint.cargo test -p wasixv2-wasix1: all pass.RUST_MIN_STACK=67108864in a debug build on my machine. The existinghello_worldoverflows the default 2 MiB test-thread stack without it too, so this is not caused by this change.AI disclosure
Claude (an AI agent) found this bug, wrote the fix and the test, and made the commit. Russell Wallace reviewed and approved them, and is responsible for the change.
🤖 Generated with Claude Code