fix(libkernel): fail sendmsg with EFAULT on guest memory it cannot read #8463

Merged
xXPBSXx merged 3 commits from upstream-pr/8463 into main 2026-11-14 03:32:38 +00:00
xXPBSXx commented 2026-10-20 13:03:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T13:03:46Z.


What

sendmsg_nid_postfix (Udp.cpp) read the message header, the iovec array, the buffers and the address from guest memory without a check, so a pointer into unmapped or inaccessible memory crashed the host. It now copies each of them with the checked ReadGuest that recvmsg uses (#7675) and fails with EFAULT, as copyin does in sys_sendmsg, copyiniov, getsockaddr and uiomove. The descriptor is looked up after the header, the iovec array and the address are read and before the buffers, which is the order of sendit and kern_sendit. The vector count and length checks of MessageLength run on the copy.
The branch is 3 commits: the fix 5e88a2bdcf33 and two merges of main (f4b87cbb57fb, a84f4c3dc4a5). The last merge brings in main 3aa0ac20df30 with one conflict, in GuestSocketPair.cpp, where main and this PR each added a function at the same place; both are kept and nothing of the PR changed. Against main the PR is still 3 files, +81 -9, the same added and removed lines as before the merge.
Fixes #8462
Overlap (P2, 2026-11-08, list of open pull requests from 04:42 UTC, main 62e7ca8676, checked 04:42-04:48 UTC, head a84f4c3dc4a5): of the 59 open pull requests that change core/libs/prx/libkernel/Socket/src/Udp.cpp, core/libs/prx/libkernel/CMakeLists.txt or core/libs/tests/GuestSocketPair.cpp, each was merged with and without this branch. Three conflict only with this branch: #14970 (ours, Depends on: #8463, same insertion point in GuestSocketPair.cpp), and #12965 and #12700 (accept and write cancellation, which also change Udp.cpp); whichever lands second rebases. 17 conflict with main without this branch as well; the other 39 merge cleanly with and without it. The head of #8448 (58a3e046e80f) merges cleanly with this branch, and both heads merge cleanly with main 8eb293447e.

Tested

Ubuntu 24.04 on WSL2 (Linux 6.18.54.1, AMD Ryzen 7 5800X3D), GCC 13.3.0, Release with BUILD_TESTING=ON, at head a84f4c3dc4a5 on 2026-11-08 at 03:13 UTC, one run of each:

  • guest_socket_pair, guest_recvmsg_memory and guest_sendmsg_memory pass.
  • Of 47 socket, UDP and sendmsg ctest cases, 44 pass and 3 (net_dontwait_blocking_send, _sendto, _sendmsg) did not run because their executable was not built; built and rerun at 03:14 UTC, all 11 net_dontwait cases pass.
  • python tools/check_conventions.py --base upstream/main at main 3aa0ac20df30: passes.
  • Earlier, before the merges (Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, main cf528e78c6 + the fix commit): guest_sendmsg_memory (new, registered in CMake) crashes with 0xc0000005 without the change to Udp.cpp (reverted to main, test kept, fresh build; 3 of 3 runs) and passes with it (3 of 3 runs); guest_socket_pair and guest_recvmsg_memory pass with and without it (3 of 3 runs each). The test covers a header, an iovec array, an iovec base (also as the second vector) and a name on an unmapped address and on an inaccessible page, EBADF for a bad descriptor with unreadable buffers, nothing queued after the failures, and a send from a read-only page.
  • Earlier, at head 64b5aa11f4 (the later heads only rename the iovec and msghdr types), others measured: Filip722 on Debian 13.7 (Linux 7.2.9, AMD Ryzen 5 7640HS, GCC 15.2.0): ten socket cases pass 3 of 3 each, and guest_sendmsg_memory segfaults with Udp.cpp of the merge base; jotitadev on macOS 27.2 (Apple M2 Max, x86_64 under Rosetta 2, on #8658): guest_sendmsg_memory segfaults without the Udp.cpp change and passes 3 of 3 with it, the same ten cases pass.
  • Not tested: the test failing without the change at the merged head, Windows and macOS at the merged head, a console, a title run, the other socket tests.

Checklist

  • Based on current main; no other open PR implements the same checks (#14970 adds the control-data check and depends on this PR)
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: #8462
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4936145411 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8463). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T13:03:46Z. --- ### What `sendmsg_nid_postfix` (Udp.cpp) read the message header, the iovec array, the buffers and the address from guest memory without a check, so a pointer into unmapped or inaccessible memory crashed the host. It now copies each of them with the checked `ReadGuest` that `recvmsg` uses (#7675) and fails with `EFAULT`, as `copyin` does in [`sys_sendmsg`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L1003-L1006), `copyiniov`, [`getsockaddr`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L766) and `uiomove`. The descriptor is looked up after the header, the iovec array and the address are read and before the buffers, which is the order of `sendit` and `kern_sendit`. The vector count and length checks of `MessageLength` run on the copy. The branch is 3 commits: the fix `5e88a2bdcf33` and two merges of `main` (`f4b87cbb57fb`, `a84f4c3dc4a5`). The last merge brings in `main` `3aa0ac20df30` with one conflict, in `GuestSocketPair.cpp`, where `main` and this PR each added a function at the same place; both are kept and nothing of the PR changed. Against `main` the PR is still 3 files, +81 -9, the same added and removed lines as before the merge. Fixes #8462 Overlap (P2, 2026-11-08, list of open pull requests from 04:42 UTC, `main` `62e7ca8676`, checked 04:42-04:48 UTC, head `a84f4c3dc4a5`): of the 59 open pull requests that change `core/libs/prx/libkernel/Socket/src/Udp.cpp`, `core/libs/prx/libkernel/CMakeLists.txt` or `core/libs/tests/GuestSocketPair.cpp`, each was merged with and without this branch. Three conflict only with this branch: #14970 (ours, `Depends on: #8463`, same insertion point in `GuestSocketPair.cpp`), and #12965 and #12700 (accept and write cancellation, which also change `Udp.cpp`); whichever lands second rebases. 17 conflict with `main` without this branch as well; the other 39 merge cleanly with and without it. The head of #8448 (`58a3e046e80f`) merges cleanly with this branch, and both heads merge cleanly with `main` `8eb293447e`. ### Tested Ubuntu 24.04 on WSL2 (Linux 6.18.54.1, AMD Ryzen 7 5800X3D), GCC 13.3.0, Release with `BUILD_TESTING=ON`, at head `a84f4c3dc4a5` on 2026-11-08 at 03:13 UTC, one run of each: - `guest_socket_pair`, `guest_recvmsg_memory` and `guest_sendmsg_memory` pass. - Of 47 socket, UDP and sendmsg ctest cases, 44 pass and 3 (`net_dontwait_blocking_send`, `_sendto`, `_sendmsg`) did not run because their executable was not built; built and rerun at 03:14 UTC, all 11 `net_dontwait` cases pass. - `python tools/check_conventions.py --base upstream/main` at `main` `3aa0ac20df30`: passes. - Earlier, before the merges (Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, `main` `cf528e78c6` + the fix commit): `guest_sendmsg_memory` (new, registered in CMake) crashes with 0xc0000005 without the change to `Udp.cpp` (reverted to `main`, test kept, fresh build; 3 of 3 runs) and passes with it (3 of 3 runs); `guest_socket_pair` and `guest_recvmsg_memory` pass with and without it (3 of 3 runs each). The test covers a header, an iovec array, an iovec base (also as the second vector) and a name on an unmapped address and on an inaccessible page, `EBADF` for a bad descriptor with unreadable buffers, nothing queued after the failures, and a send from a read-only page. - Earlier, at head `64b5aa11f4` (the later heads only rename the iovec and msghdr types), others measured: Filip722 on Debian 13.7 (Linux 7.2.9, AMD Ryzen 5 7640HS, GCC 15.2.0): ten socket cases pass 3 of 3 each, and `guest_sendmsg_memory` segfaults with `Udp.cpp` of the merge base; jotitadev on macOS 27.2 (Apple M2 Max, x86_64 under Rosetta 2, on #8658): `guest_sendmsg_memory` segfaults without the `Udp.cpp` change and passes 3 of 3 with it, the same ten cases pass. - Not tested: the test failing without the change at the merged head, Windows and macOS at the merged head, a console, a title run, the other socket tests. ### Checklist - [x] Based on current `main`; no other open PR implements the same checks (#14970 adds the control-data check and depends on this PR) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: #8462 - [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)
MarcZX10 commented 2026-10-21 06:34:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T06:34:26Z.

Review: APPROVED


The copied header, vector array, name and payload data now go through the same checked ReadGuest path as receive-side guest memory handling. I checked the length/error path and independently ran Linux C++ syntax compilation of Udp.cpp on this head; conventions and whitespace checks pass. The reported runtime regression covers unreadable ranges on Windows. I did not run the socket test binary on Linux in this workspace.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5595796032 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8463#pullrequestreview-5595796032). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T06:34:26Z. **Review:** APPROVED --- The copied header, vector array, name and payload data now go through the same checked `ReadGuest` path as receive-side guest memory handling. I checked the length/error path and independently ran Linux C++ syntax compilation of `Udp.cpp` on this head; conventions and whitespace checks pass. The reported runtime regression covers unreadable ranges on Windows. I did not run the socket test binary on Linux in this workspace.
Filip722 commented 2026-10-21 09:56:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Filip722 on 2026-10-21T09:56:09Z.


Full-project Linux runtime follow-up at exact head 64b5aa11f440addaff35ebb748907bae427b6666: the new regression prevents a reproducible Linux host segfault.

  • Ten focused cases each passed three consecutive executions (30 successes, no failures or skips): guest_sendmsg_memory, guest_recvmsg_memory, guest_socket_pair, guest_poll, guest_tcp, guest_udp, guest_select, guest_select_wait, guest_socket_error, and guest_socket_descriptors.
  • The unmodified send regression covers unmapped/inaccessible headers, iovec arrays, payloads and names, the bad-descriptor precedence case, no data queued after failures, and a successful send from read-only guest memory.
  • Added Linux boundary evidence using a temporary copy of the existing test: a header, iovec, payload and address each straddle a readable page and an inaccessible page. All four return EFAULT; the existing queue-empty and read-only-send checks still pass. This extension passed 3/3. A syscall trace also confirms real process_vm_readv partial reads on these cases, not only wholly unreadable ranges.
  • Negative control: kept the PR test executable, replaced only Udp.cpp with merge-base 44e3389c882c8ee3249e16933aad6479c3e00d08, and rebuilt libkernel. guest_sendmsg_memory fails with SEGFAULT (CTest exit 8). Restoring the PR source and rebuilding makes it pass again.

Environment: Debian 13.7 x86-64, Linux 7.2.9-x64v3-xanmod1, AMD Ryzen 5 7640HS, GCC 15.2.0, CMake 3.31.6. Full Release configuration with BUILD_TESTING=ON, ANYPS5_RELINKER_ONLY=OFF, bundled dependencies; built libkernel, selected test executables and dependencies with two jobs.

ctest --test-dir build \
  -R '^(guest_sendmsg_memory|guest_recvmsg_memory|guest_socket_pair|guest_poll|guest_tcp|guest_udp|guest_select|guest_select_wait|guest_socket_error|guest_socket_descriptors)$' \
  --repeat until-fail:3 -V

Tracked source restored to the named head. No new Windows/macOS, title, PS5 hardware, GPU or full repository test-suite run. This adds runtime coverage beyond the earlier Linux syntax check.

AI-assisted testing and report.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6313175979 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8463#issuecomment-6313175979). Originally posted by [Filip722](https://github.com/Filip722) on 2026-10-21T09:56:09Z. --- Full-project Linux runtime follow-up at exact head `64b5aa11f440addaff35ebb748907bae427b6666`: the new regression prevents a reproducible Linux host segfault. - Ten focused cases each passed three consecutive executions (30 successes, no failures or skips): `guest_sendmsg_memory`, `guest_recvmsg_memory`, `guest_socket_pair`, `guest_poll`, `guest_tcp`, `guest_udp`, `guest_select`, `guest_select_wait`, `guest_socket_error`, and `guest_socket_descriptors`. - The unmodified send regression covers unmapped/inaccessible headers, iovec arrays, payloads and names, the bad-descriptor precedence case, no data queued after failures, and a successful send from read-only guest memory. - Added Linux boundary evidence using a temporary copy of the existing test: a header, iovec, payload and address each straddle a readable page and an inaccessible page. All four return `EFAULT`; the existing queue-empty and read-only-send checks still pass. This extension passed 3/3. A syscall trace also confirms real `process_vm_readv` partial reads on these cases, not only wholly unreadable ranges. - Negative control: kept the PR test executable, replaced only `Udp.cpp` with merge-base `44e3389c882c8ee3249e16933aad6479c3e00d08`, and rebuilt libkernel. `guest_sendmsg_memory` fails with **SEGFAULT** (CTest exit 8). Restoring the PR source and rebuilding makes it pass again. Environment: Debian 13.7 x86-64, Linux `7.2.9-x64v3-xanmod1`, AMD Ryzen 5 7640HS, GCC 15.2.0, CMake 3.31.6. Full Release configuration with `BUILD_TESTING=ON`, `ANYPS5_RELINKER_ONLY=OFF`, bundled dependencies; built libkernel, selected test executables and dependencies with two jobs. ```sh ctest --test-dir build \ -R '^(guest_sendmsg_memory|guest_recvmsg_memory|guest_socket_pair|guest_poll|guest_tcp|guest_udp|guest_select|guest_select_wait|guest_socket_error|guest_socket_descriptors)$' \ --repeat until-fail:3 -V ``` Tracked source restored to the named head. No new Windows/macOS, title, PS5 hardware, GPU or full repository test-suite run. This adds runtime coverage beyond the earlier Linux syntax check. AI-assisted testing and report.
jotitadev commented 2026-10-23 04:27:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-23T04:27:28Z.

Review: COMMENTED


macOS run of the mach_vm_read_overwrite path, which the description lists as not tested.

  • Machine: Apple M2 Max, macOS 27.2 (26B5091g), Xcode 27 Apple clang, x86_64 build under Rosetta 2, Release.
  • Base: #8658 (feat/macos-port) @ 81490845d2 (main does not build the runtime on macOS yet), plus this PR's diff @ 64b5aa11f4 (applies cleanly).
guest_sendmsg_memory
test + CMake registration only, without the Udp.cpp change SegFault (host crash)
with the change 3/3 pass

With the change, the same ten cases Filip722 ran on Linux pass on macOS (guest_sendmsg_memory, guest_recvmsg_memory, guest_socket_pair, guest_poll, guest_tcp, guest_udp, guest_select, guest_select_wait, guest_socket_error, guest_socket_descriptors: 10/10). So the checked read gives EFAULT on all three hosts.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5623892482 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8463#pullrequestreview-5623892482). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-23T04:27:28Z. **Review:** COMMENTED --- macOS run of the `mach_vm_read_overwrite` path, which the description lists as not tested. - Machine: Apple M2 Max, macOS 27.2 (26B5091g), Xcode 27 Apple clang, x86_64 build under Rosetta 2, Release. - Base: #8658 (`feat/macos-port`) @ 81490845d2 (main does not build the runtime on macOS yet), plus this PR's diff @ 64b5aa11f4 (applies cleanly). | | `guest_sendmsg_memory` | |---|---| | test + CMake registration only, without the `Udp.cpp` change | `SegFault` (host crash) | | with the change | 3/3 pass | With the change, the same ten cases Filip722 ran on Linux pass on macOS (`guest_sendmsg_memory`, `guest_recvmsg_memory`, `guest_socket_pair`, `guest_poll`, `guest_tcp`, `guest_udp`, `guest_select`, `guest_select_wait`, `guest_socket_error`, `guest_socket_descriptors`: 10/10). So the checked read gives `EFAULT` on all three hosts. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
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!8463
No description provided.