fix(libkernel): validate the pointers of getsockopt and setsockopt like FreeBSD #7511

Closed
Bonjourcestmatt wants to merge 6 commits from upstream-pr/7511 into main
Bonjourcestmatt commented 2026-10-18 10:30:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Bonjourcestmatt on 2026-10-18T10:30:19Z.


What

setsockopt and getsockopt on a libkernel socket now validate their pointers in the order FreeBSD 11.4 does:

  • setsockopt with a null value and a non-zero length fails with EFAULT before the descriptor is checked, and a length above INT32_MAX with EINVAL (kern_setsockopt).
  • getsockopt with a value but no length pointer fails with EFAULT before the descriptor is checked (sys_getsockopt). With no value it reads a zero length, returns the option's own errors first (EBADF, ENOPROTOOPT), then fails with EFAULT on the final copyout. A null value with a length pointer succeeds and writes a zero length.
  • The existing debt sentence saying a null value gives EINVAL where FreeBSD gives EFAULT is updated.

Tested

Windows 11, WinLibs GCC 15.2.0 UCRT, Release. New NullPointersFollowFreeBsd in GuestSocketOptions.cpp (fails on main, passes here), the socket/udp/sce_net tests, and the full non-GPU suite (758/758, rebuilt on current main 9caecfd2d). python tools/check_conventions.py --base origin/main passes. Linux and macOS were not run here; the change uses no host call that differs per platform.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4908323071 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7511). Originally posted by [Bonjourcestmatt](https://github.com/Bonjourcestmatt) on 2026-10-18T10:30:19Z. --- ### What `setsockopt` and `getsockopt` on a libkernel socket now validate their pointers in the order FreeBSD 11.4 does: - `setsockopt` with a null value and a non-zero length fails with `EFAULT` before the descriptor is checked, and a length above `INT32_MAX` with `EINVAL` ([`kern_setsockopt`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L1414-L1417)). - `getsockopt` with a value but no length pointer fails with `EFAULT` before the descriptor is checked ([`sys_getsockopt`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L1448-L1472)). With no value it reads a zero length, returns the option's own errors first (`EBADF`, `ENOPROTOOPT`), then fails with `EFAULT` on the final copyout. A null value with a length pointer succeeds and writes a zero length. - The existing debt sentence saying a null value gives `EINVAL` where FreeBSD gives `EFAULT` is updated. ### Tested Windows 11, WinLibs GCC 15.2.0 UCRT, Release. New `NullPointersFollowFreeBsd` in `GuestSocketOptions.cpp` (fails on `main`, passes here), the socket/udp/sce_net tests, and the full non-GPU suite (758/758, rebuilt on current `main` 9caecfd2d). `python tools/check_conventions.py --base origin/main` passes. Linux and macOS were not run here; the change uses no host call that differs per platform. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
wachi35676 commented 2026-10-18 12:20:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-18T12:20:00Z.


Tested on Windows 11 (MinGW GCC 15.2) and Ubuntu 24.04 (GCC 13.3), both with this PR merged onto main 9f25d0bf5. guest_socket_options with the new NullPointersFollowFreeBsd fails on main's Udp.cpp and passes with this PR on both. The socket/sce_net/equeue set passes (Windows 38/38, Linux 26/26).

Two places still differ from the order the description cites (release/11.4.0). Probes at this PR's head:

  • kern_getsockopt rejects a negative *valsize with EINVAL (uipc_syscalls.c:1495-1498) before getsock_cap (L1517) and before the protocol sees the level. Here the check runs after Lookup and after the level check:

    • getsockopt(-1, SOL_SOCKET, SO_KEEPALIVE, &v, &len=0xffffffff) gives errno 9; FreeBSD gives 22.
    • getsockopt(tcp, 0 /*IPPROTO_IP*/, 1, &v, &len=0xffffffff) gives errno 42; FreeBSD gives 22.

    Moving available and its < 0 check above Lookup (value && length is already guaranteed there) would match.

  • The two new setsockopt checks sit below the KernelSetSocketOption_nid_no_patch bridge. So for a socket from sceNetSocket, setsockopt_nid_postfix(net, SOL_SOCKET, SO_KEEPALIVE, nullptr, 4) still returns -1 with errno 22, where kern_setsockopt returns EFAULT before the descriptor is looked up (L1414-L1415, getsock_cap at L1436). The updated debt sentence states the EFAULT-first order without that exception. Moving the two lines above the bridge call would cover it.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6249838741 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7511#issuecomment-6249838741). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-18T12:20:00Z. --- Tested on Windows 11 (MinGW GCC 15.2) and Ubuntu 24.04 (GCC 13.3), both with this PR merged onto `main` `9f25d0bf5`. `guest_socket_options` with the new `NullPointersFollowFreeBsd` fails on `main`'s `Udp.cpp` and passes with this PR on both. The socket/sce_net/equeue set passes (Windows 38/38, Linux 26/26). Two places still differ from the order the description cites (release/11.4.0). Probes at this PR's head: - `kern_getsockopt` rejects a negative `*valsize` with `EINVAL` ([uipc_syscalls.c:1495-1498](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L1495-L1498)) before `getsock_cap` (L1517) and before the protocol sees the level. Here the check runs after `Lookup` and after the level check: - `getsockopt(-1, SOL_SOCKET, SO_KEEPALIVE, &v, &len=0xffffffff)` gives errno 9; FreeBSD gives 22. - `getsockopt(tcp, 0 /*IPPROTO_IP*/, 1, &v, &len=0xffffffff)` gives errno 42; FreeBSD gives 22. Moving `available` and its `< 0` check above `Lookup` (`value && length` is already guaranteed there) would match. - The two new `setsockopt` checks sit below the `KernelSetSocketOption_nid_no_patch` bridge. So for a socket from `sceNetSocket`, `setsockopt_nid_postfix(net, SOL_SOCKET, SO_KEEPALIVE, nullptr, 4)` still returns -1 with errno 22, where `kern_setsockopt` returns `EFAULT` before the descriptor is looked up ([L1414-L1415](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L1414-L1415), `getsock_cap` at L1436). The updated debt sentence states the EFAULT-first order without that exception. Moving the two lines above the bridge call would cover it. AI-assisted: yes (Claude Code); results above were run on this machine.
Bonjourcestmatt commented 2026-10-19 06:38:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Bonjourcestmatt on 2026-10-19T06:38:30Z.


Thanks for the probes. Both differences are real and now fixed in 3ad71a4ed:

  • getsockopt rejects a negative *valsize with EINVAL before the descriptor lookup and before the level check (kern_getsockopt: if ((int)*valsize < 0) return (EINVAL); runs before getsock_cap), so the check moved above Lookup. getsockopt(-1, SOL_SOCKET, SO_KEEPALIVE, &v, &len=0xffffffff) and getsockopt(tcp, 0, 1, &v, &len=0xffffffff) now give 22.
  • setsockopt's two checks (val == NULL && valsize != 0 then (int)valsize < 0) moved above the KernelSetSocketOption_nid_no_patch bridge, so a sceNetSocket descriptor gets EFAULT and EINVAL first as well.

New checks: the two getsockopt cases in GuestSocketOptions.cpp, and the two setsockopt cases on a sceNetSocket descriptor in GuestSceNet.cpp (which already links libSceNet). With the old Udp.cpp both guest_socket_options and guest_sce_net abort; with the change they pass.

Windows 11, MinGW-w64 GCC 15.2.0 UCRT, head merged onto main b44d0127d: the full non-GPU suite builds and the socket, UDP, TCP, poll and select tests pass (31/31). Not run on Linux or macOS.

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6261282521 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7511#issuecomment-6261282521). Originally posted by [Bonjourcestmatt](https://github.com/Bonjourcestmatt) on 2026-10-19T06:38:30Z. --- Thanks for the probes. Both differences are real and now fixed in 3ad71a4ed: - `getsockopt` rejects a negative `*valsize` with `EINVAL` before the descriptor lookup and before the level check (`kern_getsockopt`: `if ((int)*valsize < 0) return (EINVAL);` runs before `getsock_cap`), so the check moved above `Lookup`. `getsockopt(-1, SOL_SOCKET, SO_KEEPALIVE, &v, &len=0xffffffff)` and `getsockopt(tcp, 0, 1, &v, &len=0xffffffff)` now give 22. - `setsockopt`'s two checks (`val == NULL && valsize != 0` then `(int)valsize < 0`) moved above the `KernelSetSocketOption_nid_no_patch` bridge, so a `sceNetSocket` descriptor gets `EFAULT` and `EINVAL` first as well. New checks: the two `getsockopt` cases in `GuestSocketOptions.cpp`, and the two `setsockopt` cases on a `sceNetSocket` descriptor in `GuestSceNet.cpp` (which already links `libSceNet`). With the old `Udp.cpp` both `guest_socket_options` and `guest_sce_net` abort; with the change they pass. Windows 11, MinGW-w64 GCC 15.2.0 UCRT, head merged onto `main` `b44d0127d`: the full non-GPU suite builds and the socket, UDP, TCP, poll and select tests pass (31/31). Not run on Linux or macOS. _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
wachi35676 commented 2026-10-19 07:44:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T07:44:46Z.


Re-tested 3ad71a4ed merged onto main c1ca83479 on Windows 11 (MinGW GCC 15.2) and Ubuntu 24.04 (GCC 13.3).

  • The three probes now give the FreeBSD 11.4 errnos on both platforms:

    • getsockopt(-1, SOL_SOCKET, SO_KEEPALIVE, &v, &len=0xffffffff) gives 22.
    • getsockopt(tcp, 0, 1, &v, &len=0xffffffff) gives 22.
    • setsockopt(<sceNetSocket fd>, SOL_SOCKET, SO_KEEPALIVE, nullptr, 4) gives 14.

    On main they give 9, 42 and 22. A plain socket still gets 14, and a valid setsockopt (SO_REUSEADDR, SO_RCVTIMEO) on a sceNetSocket descriptor still returns 0.

  • guest_socket_options and guest_sce_net abort with main's Udp.cpp and with the Udp.cpp from 58371fb8e, and pass with this one.

  • The socket/udp/tcp/poll/select/equeue/sce_net set passes: Windows 54/54, Linux 40/40.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6262273622 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7511#issuecomment-6262273622). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-19T07:44:46Z. --- Re-tested 3ad71a4ed merged onto `main` `c1ca83479` on Windows 11 (MinGW GCC 15.2) and Ubuntu 24.04 (GCC 13.3). - The three probes now give the FreeBSD 11.4 errnos on both platforms: - `getsockopt(-1, SOL_SOCKET, SO_KEEPALIVE, &v, &len=0xffffffff)` gives 22. - `getsockopt(tcp, 0, 1, &v, &len=0xffffffff)` gives 22. - `setsockopt(<sceNetSocket fd>, SOL_SOCKET, SO_KEEPALIVE, nullptr, 4)` gives 14. On `main` they give 9, 42 and 22. A plain socket still gets 14, and a valid `setsockopt` (`SO_REUSEADDR`, `SO_RCVTIMEO`) on a sceNetSocket descriptor still returns 0. - `guest_socket_options` and `guest_sce_net` abort with `main`'s `Udp.cpp` and with the `Udp.cpp` from 58371fb8e, and pass with this one. - The socket/udp/tcp/poll/select/equeue/sce_net set passes: Windows 54/54, Linux 40/40. AI-assisted: yes (Claude Code); results above were run on this machine.
Bonjourcestmatt commented 2026-10-19 09:04:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Bonjourcestmatt on 2026-10-19T09:04:21Z.


Merged current origin/main into the branch to resolve the conflict (no textual conflicts, only the merge). Rebuilt guest_socket_options_tests and guest_sce_net_tests on Windows (WinLibs GCC 15.2 UCRT): both pass; check_conventions.py --base origin/main passes. Linux/macOS were not run.

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6263617772 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7511#issuecomment-6263617772). Originally posted by [Bonjourcestmatt](https://github.com/Bonjourcestmatt) on 2026-10-19T09:04:21Z. --- Merged current `origin/main` into the branch to resolve the conflict (no textual conflicts, only the merge). Rebuilt `guest_socket_options_tests` and `guest_sce_net_tests` on Windows (WinLibs GCC 15.2 UCRT): both pass; `check_conventions.py --base origin/main` passes. Linux/macOS were not run. _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
Bonjourcestmatt commented 2026-10-19 09:24:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Bonjourcestmatt on 2026-10-19T09:24:15Z.


Merged origin/main again (conflict with the new IPV6_V6ONLY handling in getsockopt/setsockopt and the debt entry). Rebuilt on Windows (WinLibs GCC 15.2 UCRT): guest_socket_options and guest_sce_net pass, check_conventions passes. Linux and macOS did not run here.

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6263974284 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7511#issuecomment-6263974284). Originally posted by [Bonjourcestmatt](https://github.com/Bonjourcestmatt) on 2026-10-19T09:24:15Z. --- Merged `origin/main` again (conflict with the new `IPV6_V6ONLY` handling in `getsockopt`/`setsockopt` and the debt entry). Rebuilt on Windows (WinLibs GCC 15.2 UCRT): `guest_socket_options` and `guest_sce_net` pass, `check_conventions` passes. Linux and macOS did not run here. _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
blawesom commented 2026-10-19 09:41:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-19T09:41:19Z.


Rebase handoff for the 71-commit getsockopt-null-pointers series — rebased onto current main (all commits, authorship unchanged). This was a long series with main-merges; the notable resolutions:

  • your getsockopt pointer validation (the null-length and negative-available guards) merged with the IPV6_V6ONLY level-41 branch — main's available-length form kept with your null checks before it
  • the sample-location collision: your grid-fallback commit removes main's 2x2-grid Require — I initially kept main's Require (wrong direction) and corrected it: the fallback replaces the Require, with main's (state.samples > 1 || state.customSampleLocations) enabled condition preserved, and the grid rejection expectation replaced by your fallback acceptance
  • the FMASK numeric class: atomic || fmask → Uint merged ahead of main's nativeRepacked → Float clause
  • CMake registrations, symbol lists, and the heap-mirror/SetAddressCopyLimit declarations unioned throughout

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV): builds; conventions pass; full suite green apart from the four known main regressions (#5444 Mesa, #7584 BDA, #7766, #7784), none from this PR.

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/7511
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is 30afd0f91.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6264276781 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7511#issuecomment-6264276781). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-19T09:41:19Z. --- **Rebase handoff for the 71-commit getsockopt-null-pointers series** — rebased onto current `main` (all commits, authorship unchanged). This was a long series with main-merges; the notable resolutions: - your `getsockopt` pointer validation (the null-length and negative-available guards) merged with the `IPV6_V6ONLY` level-41 branch — main's available-length form kept with your null checks before it - the sample-location collision: your grid-fallback commit removes `main`'s 2x2-grid `Require` — I initially kept `main`'s Require (wrong direction) and corrected it: the fallback replaces the Require, with `main`'s `(state.samples > 1 || state.customSampleLocations)` enabled condition preserved, and the grid rejection expectation replaced by your fallback acceptance - the FMASK numeric class: `atomic || fmask → Uint` merged ahead of `main`'s `nativeRepacked → Float` clause - CMake registrations, symbol lists, and the heap-mirror/`SetAddressCopyLimit` declarations unioned throughout Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, AMD RX 9070 XT / RADV): builds; conventions pass; **full suite green apart from the four known `main` regressions** (#5444 Mesa, #7584 BDA, #7766, #7784), none from this PR. **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/7511 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `30afd0f91`.
EagleBearerYS commented 2026-10-22 00:40:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T00:40:20Z.


@Bonjourcestmatt a merge-order note: #9515 adds a call at the start of getsockopt_nid_postfix that hands a descriptor libkernel doesn't own to the option getter libSceNet registers, at the spot where this PR adds its pointer and length checks. This one goes first: #9515 lists it under Depends on and, when it rebases, puts its call after your two checks, so a libSceNet socket fails a bad pointer or length the same way.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6329827333 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7511#issuecomment-6329827333). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-22T00:40:20Z. --- @Bonjourcestmatt a merge-order note: #9515 adds a call at the start of `getsockopt_nid_postfix` that hands a descriptor libkernel doesn't own to the option getter libSceNet registers, at the spot where this PR adds its pointer and length checks. This one goes first: #9515 lists it under Depends on and, when it rebases, puts its call after your two checks, so a libSceNet socket fails a bad pointer or length the same way.

Pull request closed

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!7511
No description provided.