fix(libkernel): fail sendto and recvfrom with EFAULT on an unusable pointer #13756

Merged
Gruzzberry merged 1 commit from upstream-pr/13756 into main 2026-10-31 23:54:11 +00:00
Gruzzberry commented 2026-10-31 04:21:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-31T04:21:53Z.


What

sendto and recvfrom on a guest socket crashed or returned a wrong errno when a guest pointer could not be used: a send buffer or destination address that is null, 0x10 or on an unreadable page, a receive buffer on an unwritable page, and an unreadable or unwritable address or address-length pointer. FreeBSD 11.4 fails these with EFAULT (sys_sendto/kern_sendit/getsockaddr, sys_recvfrom/kern_recvit/recvit). Native Windows recv with a bad pointer answered ECONNABORTED (53) here.

  • sendto copies the destination address into a local buffer (after the existing ENAMETOOLONG/EINVAL length checks, before the descriptor lookup, as getsockaddr does) and fails with EFAULT if it cannot be read. The send buffer is checked page by page right before the native send, after the existing EINVAL, address and port-zero checks.
  • recvfrom copies *addressLength in before the descriptor lookup (sys_recvfrom does the copyin before kern_recvit looks up the descriptor). An unwritable receive buffer is probed after the wait; if the socket would answer with its own error first (EAGAIN, ENOTCONN, a reset) that errno is returned, an empty stream or datagram returns 0, otherwise a datagram is dropped and the call fails with EFAULT, as soreceive does after dequeuing. The address and its length are written with the checked copy; as in recvit, the length is written back (the caller's value when no address buffer is given) and a failed write is EFAULT after the data is consumed.

Tested

Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, -DBUILD_TESTING=ON, on current main.

  • guest_udp: new CheckPointerFaults for UDP and TCP on loopback. sendto with a null, 0x10, unreadable and page-crossing buffer, and (UDP) a 0x10, unreadable and page-crossing destination; a read-only page is a valid send buffer. recvfrom with a 0x10, unreadable, read-only and page-crossing address-length pointer, a 0x10, unreadable and read-only receive buffer, and a 0x10, unreadable and page-crossing address buffer (data consumed); an empty nonblocking socket with a bad buffer answers EAGAIN; with no address buffer the length comes back unchanged. On main the test crashes (segfault); with this change it passes, three runs out of three, together with the 95 socket, datagram, TCP and net tests.
  • Full suite: the only failures are pr_overlap_encoding, agc_host_import_cut_lifetime and agc_driver_depth_plane_partial_store (fail on main here too), plus vulkan_validation_gate_reports and check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted, which fail because the WindowsApps python.exe cannot be opened by those scripts.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux and macOS (the new code uses the existing copy helpers; the peek path is not Windows-only), a console, a title run.
  • Not fixed, same on main: the early EFAULT for a null buffer with a non-zero length and for an address buffer without an address-length pointer (#7507 changes the second) still come before the descriptor state checks. The writability probe covers the whole requested length, not only the bytes that arrive, so a short message into a buffer whose tail is unmapped fails here where FreeBSD would succeed. A send with an unreadable buffer on an unconnected, shut-down or oversized-datagram socket answers EFAULT where FreeBSD answers EDESTADDRREQ, EPIPE, ENOTCONN or EMSGSIZE first, because a native send cannot be tried without side effects. For datagram sockets the buffer is checked before the destination address is validated, as sosend_dgram copies the data in before udp_output checks the address.

Overlaps

  • #7507 (recvfrom address buffer without a length pointer) edits the check next to my length copy-in in Udp.cpp (L1603, L1615-1621): it was opened first, so it goes first and this PR rebases.
  • #11896 (MSG_WAITALL stream receive) edits the receive call in recvfrom (L1608-1621): it was opened first, so it goes first and this PR rebases.
  • #12682 (cancel blocked reads) edits the same function heads (L1592-1593, L1608-1610): it was opened first, so it goes first and this PR rebases.
  • tools/pr_overlap.py could not compare Udp.cpp against #5021, #7088, #8605, #9713, #10645, #12688, #12700 and #12965, which conflict with main themselves; those touch other functions in the file and none implements sendto/recvfrom pointer checks.

Checklist

  • Based on current main; no other open PR implements the same function
  • 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); 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: #7507; merge order: after #7507, #11896, #12682
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5094470958 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13756). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-31T04:21:53Z. --- ### What `sendto` and `recvfrom` on a guest socket crashed or returned a wrong errno when a guest pointer could not be used: a send buffer or destination address that is null, `0x10` or on an unreadable page, a receive buffer on an unwritable page, and an unreadable or unwritable address or address-length pointer. FreeBSD 11.4 fails these with `EFAULT` ([`sys_sendto`/`kern_sendit`/`getsockaddr`, `sys_recvfrom`/`kern_recvit`/`recvit`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c)). Native Windows `recv` with a bad pointer answered `ECONNABORTED` (53) here. - `sendto` copies the destination address into a local buffer (after the existing `ENAMETOOLONG`/`EINVAL` length checks, before the descriptor lookup, as `getsockaddr` does) and fails with `EFAULT` if it cannot be read. The send buffer is checked page by page right before the native send, after the existing `EINVAL`, address and port-zero checks. - `recvfrom` copies `*addressLength` in before the descriptor lookup (`sys_recvfrom` does the `copyin` before `kern_recvit` looks up the descriptor). An unwritable receive buffer is probed after the wait; if the socket would answer with its own error first (`EAGAIN`, `ENOTCONN`, a reset) that errno is returned, an empty stream or datagram returns 0, otherwise a datagram is dropped and the call fails with `EFAULT`, as `soreceive` does after dequeuing. The address and its length are written with the checked copy; as in `recvit`, the length is written back (the caller's value when no address buffer is given) and a failed write is `EFAULT` after the data is consumed. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on current `main`. - `guest_udp`: new `CheckPointerFaults` for UDP and TCP on loopback. `sendto` with a null, `0x10`, unreadable and page-crossing buffer, and (UDP) a `0x10`, unreadable and page-crossing destination; a read-only page is a valid send buffer. `recvfrom` with a `0x10`, unreadable, read-only and page-crossing address-length pointer, a `0x10`, unreadable and read-only receive buffer, and a `0x10`, unreadable and page-crossing address buffer (data consumed); an empty nonblocking socket with a bad buffer answers `EAGAIN`; with no address buffer the length comes back unchanged. On `main` the test crashes (segfault); with this change it passes, three runs out of three, together with the 95 socket, datagram, TCP and net tests. - Full suite: the only failures are `pr_overlap_encoding`, `agc_host_import_cut_lifetime` and `agc_driver_depth_plane_partial_store` (fail on `main` here too), plus `vulkan_validation_gate_reports` and `check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted`, which fail because the WindowsApps `python.exe` cannot be opened by those scripts. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux and macOS (the new code uses the existing copy helpers; the peek path is not Windows-only), a console, a title run. - Not fixed, same on `main`: the early `EFAULT` for a null buffer with a non-zero length and for an address buffer without an address-length pointer (#7507 changes the second) still come before the descriptor state checks. The writability probe covers the whole requested length, not only the bytes that arrive, so a short message into a buffer whose tail is unmapped fails here where FreeBSD would succeed. A send with an unreadable buffer on an unconnected, shut-down or oversized-datagram socket answers `EFAULT` where FreeBSD answers `EDESTADDRREQ`, `EPIPE`, `ENOTCONN` or `EMSGSIZE` first, because a native send cannot be tried without side effects. For datagram sockets the buffer is checked before the destination address is validated, as `sosend_dgram` copies the data in before `udp_output` checks the address. ### Overlaps - #7507 (`recvfrom` address buffer without a length pointer) edits the check next to my length copy-in in `Udp.cpp` (L1603, L1615-1621): it was opened first, so it goes first and this PR rebases. - #11896 (`MSG_WAITALL` stream receive) edits the receive call in `recvfrom` (L1608-1621): it was opened first, so it goes first and this PR rebases. - #12682 (cancel blocked reads) edits the same function heads (L1592-1593, L1608-1610): it was opened first, so it goes first and this PR rebases. - `tools/pr_overlap.py` could not compare `Udp.cpp` against #5021, #7088, #8605, #9713, #10645, #12688, #12700 and #12965, which conflict with `main` themselves; those touch other functions in the file and none implements `sendto`/`recvfrom` pointer checks. ### Checklist - [x] Based on current `main`; no other open PR implements the same function - [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`); 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: #7507; merge order: after #7507, #11896, #12682 - [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)
jotitadev commented 2026-10-31 06:23:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-31T06:23:40Z.

Review: COMMENTED


Linux run of head f6681e1bc0, merged (--no-commit) onto main 6a691f69ee. Ubuntu 26.04 (kernel 7.0), GCC 15.2.0, CMake 4.2.3, Ninja, Release, BUILD_TESTING=ON, i7-8700.

  • guest_udp: passes 3/3.
  • Full build ok. Related subset guest_socket|guest_udp|guest_tcp|guest_poll|guest_net|guest_select|guest_kernel_fcntl: 35/35.
  • public_headers: 65/65.
  • Red: with Udp.cpp reverted to main and the new test kept, guest_udp crashes (SEGFAULT) 3/3.
  • Udp.cpp stack: main + #13695 + #13738 + #13742 + #13752 + #13756 merged in that order (all touch Udp.cpp) merges cleanly; full build ok, the five focal tests (guest_socket_pair, guest_socket_options, guest_udp, guest_poll, guest_kernel_fcntl) pass 3/3, the socket subset 35/35 and public_headers 65/65.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5721622105 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13756#pullrequestreview-5721622105). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-31T06:23:40Z. **Review:** COMMENTED --- Linux run of head `f6681e1bc0`, merged (`--no-commit`) onto `main` `6a691f69ee`. Ubuntu 26.04 (kernel 7.0), GCC 15.2.0, CMake 4.2.3, Ninja, Release, `BUILD_TESTING=ON`, i7-8700. - `guest_udp`: passes 3/3. - Full build ok. Related subset `guest_socket|guest_udp|guest_tcp|guest_poll|guest_net|guest_select|guest_kernel_fcntl`: 35/35. - `public_headers`: 65/65. - Red: with `Udp.cpp` reverted to `main` and the new test kept, `guest_udp` crashes (SEGFAULT) 3/3. - Udp.cpp stack: `main` + #13695 + #13738 + #13742 + #13752 + #13756 merged in that order (all touch `Udp.cpp`) merges cleanly; full build ok, the five focal tests (`guest_socket_pair`, `guest_socket_options`, `guest_udp`, `guest_poll`, `guest_kernel_fcntl`) pass 3/3, the socket subset 35/35 and `public_headers` 65/65. 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!13756
No description provided.