fix(libkernel): fail socketpair with EFAULT on an unusable result pointer #13742

Merged
Gruzzberry merged 2 commits from upstream-pr/13742 into main 2026-10-31 21:42:52 +00:00
Gruzzberry commented 2026-10-31 03:51:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-31T03:51:33Z.


What

socketpair with a result pointer the guest cannot write crashed the host: a pointer such as 0x10 was accepted by the guest-arena write guard and the final store to sv faulted. FreeBSD 11.4's sys_socketpair runs kern_socketpair first, so every argument error keeps its source order, then copyouts the two descriptors; if the copyout fails it closes both with kern_close and returns EFAULT.

The pair is now created and installed as before, and the two numbers are written with GuestCopyOut_nid_no_patch. If that fails, both descriptors are closed and the call fails with EFAULT. A null pointer takes the same path (it used to be rejected before anything was created), so the lowest free descriptor numbers are the same afterwards, as on FreeBSD. Close-on-exec registration happens only after the copyout succeeds. Argument errors (family, type, protocol) still win over a bad pointer.

Tested

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

  • guest_socket_pair: new checks for 0x10 with a stream and a datagram pair, a mapped page with no access, an sv that starts 4 bytes before such a page (second int unreadable), all giving -1 with EFAULT, and a check that a failed call with SOCK_CLOEXEC releases both numbers (the next pair gets the same numbers). On main the test crashes with a write fault at 0x10; with this change it passes, three runs out of three.
  • Full suite: the failures are pr_overlap_encoding, agc_host_import_cut_lifetime and agc_driver_depth_plane_partial_store, which fail on main in this environment too, plus vulkan_validation_gate_reports and check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted, which fail here because the Windows Store Python launcher path cannot be read (OSError on python.exe) and touch nothing in this change. guest_libc_cnd_timed_wait and guest_timed_wait_cpu failed in the parallel run under load and passed alone.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux and macOS, a console, a title run.

Overlaps

tools/pr_overlap.py found no confirmed overlap. It could not compare Udp.cpp and GuestSocketPair.cpp against #5021, #7088, #8605, #9713, #10645, #12682, #12688, #12700 and #12965 because those conflict with main themselves; I read their diffs and none changes socketpair_nid_postfix (#8605, #12682, #12688, #12700 and #12965 only call it in tests).

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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5094262863 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13742). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-31T03:51:33Z. --- ### What `socketpair` with a result pointer the guest cannot write crashed the host: a pointer such as `0x10` was accepted by the guest-arena write guard and the final store to `sv` faulted. FreeBSD 11.4's [`sys_socketpair`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L733) runs `kern_socketpair` first, so every argument error keeps its source order, then `copyout`s the two descriptors; if the copyout fails it closes both with `kern_close` and returns `EFAULT`. The pair is now created and installed as before, and the two numbers are written with `GuestCopyOut_nid_no_patch`. If that fails, both descriptors are closed and the call fails with `EFAULT`. A null pointer takes the same path (it used to be rejected before anything was created), so the lowest free descriptor numbers are the same afterwards, as on FreeBSD. Close-on-exec registration happens only after the copyout succeeds. Argument errors (family, type, protocol) still win over a bad pointer. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on current `main`. - `guest_socket_pair`: new checks for `0x10` with a stream and a datagram pair, a mapped page with no access, an `sv` that starts 4 bytes before such a page (second `int` unreadable), all giving `-1` with `EFAULT`, and a check that a failed call with `SOCK_CLOEXEC` releases both numbers (the next pair gets the same numbers). On `main` the test crashes with a write fault at `0x10`; with this change it passes, three runs out of three. - Full suite: the failures are `pr_overlap_encoding`, `agc_host_import_cut_lifetime` and `agc_driver_depth_plane_partial_store`, which fail on `main` in this environment too, plus `vulkan_validation_gate_reports` and `check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted`, which fail here because the Windows Store Python launcher path cannot be read (`OSError` on `python.exe`) and touch nothing in this change. `guest_libc_cnd_timed_wait` and `guest_timed_wait_cpu` failed in the parallel run under load and passed alone. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux and macOS, a console, a title run. ### Overlaps `tools/pr_overlap.py` found no confirmed overlap. It could not compare `Udp.cpp` and `GuestSocketPair.cpp` against #5021, #7088, #8605, #9713, #10645, #12682, #12688, #12700 and #12965 because those conflict with `main` themselves; I read their diffs and none changes `socketpair_nid_postfix` (#8605, #12682, #12688, #12700 and #12965 only call it in tests). ### 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: none - [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:34 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Linux run of head 53a9b2d3b9, 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_socket_pair: 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_socket_pair 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:5721621885 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13742#pullrequestreview-5721621885). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-31T06:23:34Z. **Review:** COMMENTED --- Linux run of head `53a9b2d3b9`, 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_socket_pair`: 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_socket_pair` 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!13742
No description provided.