fix(libkernel): fail getsockopt and setsockopt with EFAULT on an unusable pointer #13752

Merged
Gruzzberry merged 1 commit from upstream-pr/13752 into main 2026-11-02 17:31:53 +00:00
Gruzzberry commented 2026-10-31 04:11:07 +00:00 (Migrated from github.com)

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


What

getsockopt and setsockopt on a libkernel socket crashed the emulator when the guest passed an unmapped or unreadable optval or optlen pointer (for example 0x10 or a PROT_NONE page): the option code copied from value with a raw memcpy, read *length raw, and a NULL optlen returned EINVAL. FreeBSD 11.4 fails these with EFAULT (sys_setsockopt/kern_setsockopt/sys_getsockopt/kern_getsockopt, sooptcopyin/sooptcopyout in uipc_socket.c).

  • Every read of the guest optval in SocketOptions.cpp now goes through GuestCopyIn_nid_no_patch; an unreadable pointer gives EFAULT. The checks that come before the copy in sooptcopyin stay in front of it: bad descriptor EBADF, unknown level EINVAL, unknown option ENOPROTOOPT, a short length EINVAL. The IP_MULTICAST_IF ip_mreqn form is copied in as one 12-byte block instead of being indexed in place.
  • getsockopt copies the guest optlen in first when optval is not NULL (EFAULT, before the descriptor lookup, as sys_getsockopt does), works on a local length, writes the value and the length with GuestCopyOut_nid_no_patch, and stores the length only when the call succeeded. A NULL optval means length 0; an unwritable optlen then gives EFAULT only after the option itself succeeded. A NULL optlen with a non-NULL optval is now EFAULT (was EINVAL), as copyin(NULL) is in FreeBSD.
  • setsockopt also rejects a length above INT_MAX with EINVAL after the NULL-with-length EFAULT check, as kern_setsockopt does.
  • Descriptors owned by libSceNet are passed on unchanged: sceNetSetsockopt/sceNetGetsockopt still read the guest pointers themselves, so this PR covers libkernel's own sockets only.

Tested

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

  • guest_socket_options: new BadPointersFail block. setsockopt with 0x10 and a PROT_NONE page as optval fails with EFAULT for SO_KEEPALIVE, SO_LINGER, SO_RCVTIMEO, SO_NOSIGPIPE, SO_SNDBUF, TCP_NODELAY, IP_TTL, IP_ADD_MEMBERSHIP, IP_MULTICAST_IF (4 and 12 bytes) and IP_MULTICAST_TTL (1 and 4 bytes), and leaves the option unchanged; a short length, an unknown option, an unknown level and a bad descriptor are reported before the pointer is touched; a length of 0x80000000 gives EINVAL. getsockopt into 0x10, a PROT_NONE page and a read-only page fails with EFAULT and keeps the length; an unreadable optlen (0x10, PROT_NONE, NULL) fails with EFAULT before the descriptor and option checks; with a NULL optval an unwritable optlen fails with EFAULT only for an option that exists.
  • On main the new block crashes the test (SEGFAULT); on this branch guest_socket_options and guest_socket_options_multicast pass three runs out of three. net_ipv6only, which exercises the same getsockopt path on guest arena memory (read-only length page leaves the value untouched), passes 3/3.
  • Full suite: the failures are vulkan_validation_gate_reports and check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted (both are Python tests that fail on the Windows Store python.exe here and do not touch sockets). pr_overlap_encoding, agc_host_import_cut_lifetime and agc_driver_depth_plane_partial_store passed in this run.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux and macOS, a console, a title run.
  • Not changed: with optval non-NULL, an unwritable optlen is reported before a bad option (EFAULT instead of ENOPROTOOPT), because an existing test requires that the value is not modified when the length cannot be stored; a getsockopt length above INT_MAX is accepted (FreeBSD: EINVAL), pinned by the net_ipv6only test.

Overlaps

  • #8448 changes the level check in the same two functions in Udp.cpp (L1249-1255); it was opened first, so it goes first and this PR rebases.
  • tools/pr_overlap.py could not compare Udp.cpp and SocketOptions.cpp against #5021, #7088, #8605, #9713, #10645, #12688, #12700 and #12965, which conflict with main themselves. The diffs of #12688, #12700 and #12965 do not touch the option functions; the older ones were not compared.

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: #8448; merge order: after #8448 (neighbouring lines in Udp.cpp)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5094399688 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13752). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-31T04:11:07Z. --- ### What `getsockopt` and `setsockopt` on a libkernel socket crashed the emulator when the guest passed an unmapped or unreadable `optval` or `optlen` pointer (for example `0x10` or a `PROT_NONE` page): the option code copied from `value` with a raw `memcpy`, read `*length` raw, and a `NULL` `optlen` returned `EINVAL`. FreeBSD 11.4 fails these with `EFAULT` ([`sys_setsockopt`/`kern_setsockopt`/`sys_getsockopt`/`kern_getsockopt`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L1380), `sooptcopyin`/`sooptcopyout` in `uipc_socket.c`). - Every read of the guest `optval` in `SocketOptions.cpp` now goes through `GuestCopyIn_nid_no_patch`; an unreadable pointer gives `EFAULT`. The checks that come before the copy in `sooptcopyin` stay in front of it: bad descriptor `EBADF`, unknown level `EINVAL`, unknown option `ENOPROTOOPT`, a short length `EINVAL`. The `IP_MULTICAST_IF` `ip_mreqn` form is copied in as one 12-byte block instead of being indexed in place. - `getsockopt` copies the guest `optlen` in first when `optval` is not `NULL` (`EFAULT`, before the descriptor lookup, as `sys_getsockopt` does), works on a local length, writes the value and the length with `GuestCopyOut_nid_no_patch`, and stores the length only when the call succeeded. A `NULL` `optval` means length 0; an unwritable `optlen` then gives `EFAULT` only after the option itself succeeded. A `NULL` `optlen` with a non-`NULL` `optval` is now `EFAULT` (was `EINVAL`), as `copyin(NULL)` is in FreeBSD. - `setsockopt` also rejects a length above `INT_MAX` with `EINVAL` after the `NULL`-with-length `EFAULT` check, as `kern_setsockopt` does. - Descriptors owned by libSceNet are passed on unchanged: `sceNetSetsockopt`/`sceNetGetsockopt` still read the guest pointers themselves, so this PR covers libkernel's own sockets only. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0, Ninja, Release, `-DBUILD_TESTING=ON`, on current `main`. - `guest_socket_options`: new `BadPointersFail` block. `setsockopt` with `0x10` and a `PROT_NONE` page as `optval` fails with `EFAULT` for `SO_KEEPALIVE`, `SO_LINGER`, `SO_RCVTIMEO`, `SO_NOSIGPIPE`, `SO_SNDBUF`, `TCP_NODELAY`, `IP_TTL`, `IP_ADD_MEMBERSHIP`, `IP_MULTICAST_IF` (4 and 12 bytes) and `IP_MULTICAST_TTL` (1 and 4 bytes), and leaves the option unchanged; a short length, an unknown option, an unknown level and a bad descriptor are reported before the pointer is touched; a length of `0x80000000` gives `EINVAL`. `getsockopt` into `0x10`, a `PROT_NONE` page and a read-only page fails with `EFAULT` and keeps the length; an unreadable `optlen` (`0x10`, `PROT_NONE`, `NULL`) fails with `EFAULT` before the descriptor and option checks; with a `NULL` `optval` an unwritable `optlen` fails with `EFAULT` only for an option that exists. - On `main` the new block crashes the test (`SEGFAULT`); on this branch `guest_socket_options` and `guest_socket_options_multicast` pass three runs out of three. `net_ipv6only`, which exercises the same `getsockopt` path on guest arena memory (read-only length page leaves the value untouched), passes 3/3. - Full suite: the failures are `vulkan_validation_gate_reports` and `check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted` (both are Python tests that fail on the Windows Store `python.exe` here and do not touch sockets). `pr_overlap_encoding`, `agc_host_import_cut_lifetime` and `agc_driver_depth_plane_partial_store` passed in this run. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux and macOS, a console, a title run. - Not changed: with `optval` non-`NULL`, an unwritable `optlen` is reported before a bad option (`EFAULT` instead of `ENOPROTOOPT`), because an existing test requires that the value is not modified when the length cannot be stored; a `getsockopt` length above `INT_MAX` is accepted (FreeBSD: `EINVAL`), pinned by the `net_ipv6only` test. ### Overlaps - #8448 changes the level check in the same two functions in `Udp.cpp` (L1249-1255); it was opened first, so it goes first and this PR rebases. - `tools/pr_overlap.py` could not compare `Udp.cpp` and `SocketOptions.cpp` against #5021, #7088, #8605, #9713, #10645, #12688, #12700 and #12965, which conflict with `main` themselves. The diffs of #12688, #12700 and #12965 do not touch the option functions; the older ones were not compared. ### 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: #8448; merge order: after #8448 (neighbouring lines in `Udp.cpp`) - [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:37 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Linux run of head 1f2365a5d1, 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_options: 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 SocketOptions.cpp and Udp.cpp reverted to main and the new test kept, guest_socket_options 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:5721621988 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13752#pullrequestreview-5721621988). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-31T06:23:37Z. **Review:** COMMENTED --- Linux run of head `1f2365a5d1`, 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_options`: 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 `SocketOptions.cpp` and `Udp.cpp` reverted to `main` and the new test kept, `guest_socket_options` 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!13752
No description provided.