Skip to content

fix(net): don't trust cmsg_len past the end of a truncated control buffer - #1684

Open
xrl wants to merge 1 commit into
bytecodealliance:mainfrom
xrl:fix/truncated-cmsg-len
Open

xrl wants to merge 1 commit into
bytecodealliance:mainfrom
xrl:fix/truncated-cmsg-len

Conversation

@xrl

@xrl xrl commented Sep 19, 2026

Copy link
Copy Markdown

Fixes #1683.

On macOS, recvmsg truncates control data to fit the RecvAncillaryBuffer but leaves cmsg_len untruncated, so AncillaryDrain::advance underflows the remaining length, the drain in Drop panics again and the process aborts; with overflow checks off, cvt_msg instead slices past the buffer and yields OwnedFds read from uninitialized memory. Messages now yields the buffer space at each header alongside it, advance clamps cmsg_len to that, and cvt_msg rounds SCM_RIGHTS payloads down to whole descriptors.

  • Linux is unaffected: scm_detach_fds adjusts cmsg_len, so the clamp is a no-op there.
  • FreeBSD delivers none of a truncated message's payload, so the test checks that every descriptor it gets back is one of the sockets sent, not how many.
  • Not every environment CI runs in reports CTRUNC, so the test doesn't assert on it.

Disclosure: this change and its test were written by an AI coding agent (Claude, Anthropic) at the direction of @xrl, who reviewed the diff. Please review it with that in mind.

Run on macOS 15.6, aarch64, rustc 1.98.1: cargo test --features=all-apis --test net (45 pass; the new test aborts with SIGABRT against main), cargo test --release --features=all-apis --test net truncated, cargo clippy --features=all-apis --all-targets, cargo fmt --all --check.

…buffer

On macOS, `recvmsg` truncates control data to fit the buffer but leaves
`cmsg_len` untruncated, so `AncillaryDrain::advance` underflowed the
remaining length and the drain in `Drop` panicked again, aborting the
process; with overflow checks off, `cvt_msg` instead sliced past the buffer
and yielded `OwnedFd`s read from uninitialized memory. `Messages` now
yields the buffer space at each header, `advance` clamps `cmsg_len` to it,
and `cvt_msg` rounds `SCM_RIGHTS` payloads down to whole descriptors.

Fixes bytecodealliance#1683.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@xrl
xrl force-pushed the fix/truncated-cmsg-len branch from 1361c67 to 6a81010 Compare September 19, 2026 06:33
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.

recvmsg: truncated SCM_RIGHTS control data (MSG_CTRUNC) panics in AncillaryDrain and again in Drop on macOS

1 participant