fix(libkernel): fail sendmsg control data in unreadable guest memory with EFAULT #14970

Merged
xXPBSXx merged 2 commits from upstream-pr/14970 into main 2026-11-17 20:56:57 +00:00
xXPBSXx commented 2026-11-02 06:24:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-11-02T06:24:41Z.


What

sendmsg on an AF_UNIX socket reads the control data (SCM_RIGHTS) in PassedDescriptors straight from guest memory with memcpy, so a msg_control pointer into unmapped or inaccessible memory crashed the host. SendPassing now checks the length and copies the buffer with the checked ReadGuest that recvmsg uses, and fails as FreeBSD 11.4 does in kern_sendit and sockargs: EINVAL when msg_controllen is below sizeof(struct cmsghdr) (12: three 4-byte members, socket.h L457-L462) or above MCLBYTES (2048, param.h L171-L174), and EFAULT when the copy fails. A controlLength above 2048 now fails with EINVAL, as sockargs does in FreeBSD 11.4 (MCLBYTES); before, this path accepted larger lengths. The existing guest_socket_rights test, which passes descriptors, still passes. The new guest_sendmsg_control test, registered outside Windows only, covers an unmapped pointer, an inaccessible page, a buffer that runs into one, and the lengths 8, 11 and 2049.

Depends on: #8463 (landing order only; same function). No console run has checked these results.

Tested

Linux (WSL, Ubuntu 24.04, GCC 13.3), 05:51 UTC to 05:52 UTC on 2026-11-02, on main 926f7dd492b0 plus this commit: the build exits 0 and guest_sendmsg_control, guest_socket_pair, guest_recvmsg_memory and guest_socket_rights pass in 3 of 3 runs. With Udp.cpp taken from main and the test kept (rebuilt, exit 0), guest_sendmsg_control crashes with a segmentation fault in 3 of 3 runs while the other three pass; with the file restored they pass again in 3 of 3 runs. python tools/check_conventions.py --base 926f7dd492b0 passes on this head, and the head merges with main 4569fe654a without a conflict (checked 2026-11-02 06:22 UTC). Not run: Windows (the test is not registered there, and sendmsg with control data throws there), macOS, a console, a title.

Overlap

Checked on 2026-11-02 against main 926f7dd492b0b432d9287f4d6faaaf1c9d3c86c5 (1898 open pull requests, listed at 04:56 UTC; the merge checks ran from 04:56 UTC to 05:38 UTC, and again for this head from 05:50 UTC to 05:53 UTC). I took the changed files of each from git diff --name-only against its merge base (one ref, #13454, could not be fetched; the GitHub file list shows it changes none of the three files). 86 of them change at least one of Socket/src/Udp.cpp, tests/GuestSocketPair.cpp and libkernel/CMakeLists.txt. git merge-tree of this head with each of the 86: 21 conflict, and 20 of those also conflict with main on their own. The remaining one is #8463, which changes the same function; hence the Depends on. I placed the new test function before CheckReceiveMessageFaults because directly after it, it conflicted with #14043. None of the 9 pull requests opened since that list (#14959 to #14969, listed at 06:23 UTC) changes one of the three files.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #8463 (landing order only; same function)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5115988582 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14970). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-11-02T06:24:41Z. --- ### What `sendmsg` on an `AF_UNIX` socket reads the control data (`SCM_RIGHTS`) in `PassedDescriptors` straight from guest memory with `memcpy`, so a `msg_control` pointer into unmapped or inaccessible memory crashed the host. `SendPassing` now checks the length and copies the buffer with the checked `ReadGuest` that `recvmsg` uses, and fails as FreeBSD 11.4 does in [`kern_sendit`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L776-L788) and [`sockargs`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L1733): `EINVAL` when `msg_controllen` is below `sizeof(struct cmsghdr)` (12: three 4-byte members, [`socket.h` L457-L462](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/sys/socket.h#L457-L462)) or above `MCLBYTES` (2048, [`param.h` L171-L174](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/sys/param.h#L171-L174)), and `EFAULT` when the copy fails. A `controlLength` above 2048 now fails with `EINVAL`, as `sockargs` does in FreeBSD 11.4 (`MCLBYTES`); before, this path accepted larger lengths. The existing `guest_socket_rights` test, which passes descriptors, still passes. The new `guest_sendmsg_control` test, registered outside Windows only, covers an unmapped pointer, an inaccessible page, a buffer that runs into one, and the lengths 8, 11 and 2049. Depends on: #8463 (landing order only; same function). No console run has checked these results. ### Tested Linux (WSL, Ubuntu 24.04, GCC 13.3), 05:51 UTC to 05:52 UTC on 2026-11-02, on `main` `926f7dd492b0` plus this commit: the build exits 0 and `guest_sendmsg_control`, `guest_socket_pair`, `guest_recvmsg_memory` and `guest_socket_rights` pass in 3 of 3 runs. With `Udp.cpp` taken from `main` and the test kept (rebuilt, exit 0), `guest_sendmsg_control` crashes with a segmentation fault in 3 of 3 runs while the other three pass; with the file restored they pass again in 3 of 3 runs. `python tools/check_conventions.py --base 926f7dd492b0` passes on this head, and the head merges with `main` `4569fe654a` without a conflict (checked 2026-11-02 06:22 UTC). Not run: Windows (the test is not registered there, and `sendmsg` with control data throws there), macOS, a console, a title. ### Overlap Checked on 2026-11-02 against `main` `926f7dd492b0b432d9287f4d6faaaf1c9d3c86c5` (1898 open pull requests, listed at 04:56 UTC; the merge checks ran from 04:56 UTC to 05:38 UTC, and again for this head from 05:50 UTC to 05:53 UTC). I took the changed files of each from `git diff --name-only` against its merge base (one ref, #13454, could not be fetched; the GitHub file list shows it changes none of the three files). 86 of them change at least one of `Socket/src/Udp.cpp`, `tests/GuestSocketPair.cpp` and `libkernel/CMakeLists.txt`. `git merge-tree` of this head with each of the 86: 21 conflict, and 20 of those also conflict with `main` on their own. The remaining one is #8463, which changes the same function; hence the `Depends on`. I placed the new test function before `CheckReceiveMessageFaults` because directly after it, it conflicted with #14043. None of the 9 pull requests opened since that list (#14959 to #14969, listed at 06:23 UTC) changes one of the three files. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #8463 (landing order only; same function) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
BunnyFl commented 2026-11-02 10:37:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T10:37:00Z.

Review: APPROVED


Looks right to me. I checked it against the cited FreeBSD 11.4 source.

  • The checks match FreeBSD 11.4. In sendit (uipc_syscalls.c at release/11.4.0), a non-null msg_control with msg_controllen < sizeof(struct cmsghdr) is EINVAL before any copy. sockargs then fails with EINVAL above MCLBYTES and with EFAULT when the copyin fails. SendPassing now does the same in that order, and copies the control buffer once with the checked ReadGuest before PassedDescriptors parses it. Only the copy is parsed, so the parser's raw memcpys no longer touch guest memory.
  • The test covers the boundaries. It checks an unmapped pointer, a PROT_NONE page and a buffer running into one for EFAULT, and lengths 8, 11 and 2049 for EINVAL. The 8-byte case with an unmapped pointer shows the length check comes before the copy, as in sendit.
  • ReadGuest builds on Linux. It is defined outside any platform guard further down, so the forward declaration links there, and SendPassing itself sits in the non-Windows block.

Non-blocking, and older than this PR: FreeBSD enters the control path whenever msg_control is non-null, so a non-null pointer with msg_controllen == 0 is EINVAL there. sendmsg_nid_postfix requires both to be set, so that call still goes through sendto here. It could be a follow-up or a debt line.

I couldn't run the new test, which is registered outside Windows only, and this machine has no Linux. This review is from reading the code and the FreeBSD source, plus your Linux runs and their negative control.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5735266730 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14970#pullrequestreview-5735266730). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T10:37:00Z. **Review:** APPROVED --- Looks right to me. I checked it against the cited FreeBSD 11.4 source. - **The checks match FreeBSD 11.4.** In `sendit` (`uipc_syscalls.c` at `release/11.4.0`), a non-null `msg_control` with `msg_controllen < sizeof(struct cmsghdr)` is `EINVAL` before any copy. `sockargs` then fails with `EINVAL` above `MCLBYTES` and with `EFAULT` when the `copyin` fails. `SendPassing` now does the same in that order, and copies the control buffer once with the checked `ReadGuest` before `PassedDescriptors` parses it. Only the copy is parsed, so the parser's raw `memcpy`s no longer touch guest memory. - **The test covers the boundaries.** It checks an unmapped pointer, a `PROT_NONE` page and a buffer running into one for `EFAULT`, and lengths 8, 11 and 2049 for `EINVAL`. The 8-byte case with an unmapped pointer shows the length check comes before the copy, as in `sendit`. - **`ReadGuest` builds on Linux.** It is defined outside any platform guard further down, so the forward declaration links there, and `SendPassing` itself sits in the non-Windows block. Non-blocking, and older than this PR: FreeBSD enters the control path whenever `msg_control` is non-null, so a non-null pointer with `msg_controllen == 0` is `EINVAL` there. `sendmsg_nid_postfix` requires both to be set, so that call still goes through `sendto` here. It could be a follow-up or a debt line. I couldn't run the new test, which is registered outside Windows only, and this machine has no Linux. This review is from reading the code and the FreeBSD source, plus your Linux runs and their negative control.
MarcZX10 commented 2026-11-16 19:51:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T19:51:43Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the stated behavior is implemented and the supplied Linux checks report passing results.

What I ran

Reviewed head b1b9e36f94ad29f81367636dac53ccbfd6e0e584 using the prepared diff and repository reader. No commands or tests were run in this review.

Results

Check Result Note
Diff and changed code passed All three changed files read
Author-reported checks not run Linux build and tests reported passing
Windows, macOS, console not run Not covered by the supplied report

Findings

Blocking

None.

Non-blocking

None.

Not verified

The full repository files and tests were not executed. files.json was unavailable; the manifest identifies three changed files, all included and read. No files were excluded by the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5926489324 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14970#pullrequestreview-5926489324). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-16T19:51:43Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the stated behavior is implemented and the supplied Linux checks report passing results. ## What I ran Reviewed head `b1b9e36f94ad29f81367636dac53ccbfd6e0e584` using the prepared diff and repository reader. No commands or tests were run in this review. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | All three changed files read | | Author-reported checks | not run | Linux build and tests reported passing | | Windows, macOS, console | not run | Not covered by the supplied report | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The full repository files and tests were not executed. `files.json` was unavailable; the manifest identifies three changed files, all included and read. No files were excluded by the manifest. ## To get this to a good state Nothing is required.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!14970
No description provided.