fix(libkernel): fail SO_ACCEPTFILTER with EINVAL on a socket that is not listening #11006

Merged
harisibrar1 merged 3 commits from upstream-pr/11006 into main 2026-10-26 21:59:08 +00:00
harisibrar1 commented 2026-10-25 14:04:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-25T14:04:20Z.


What

setsockopt and getsockopt of SO_ACCEPTFILTER (0x1000) on a socket that is not listening fail with ENOPROTOOPT (42), where the console fails them with EINVAL (22).

@orezraey measured this on a PS5 (firmware 13.60, payload process, non-listening TCP socket, pinet4.log):

pinet4: case so_0x1000_default ret=-1 errno=22 len=8 v=bbbbbbbbbbbbbbbb
pinet4: case so_0x1000_set_5 ret=-1 errno=22
pinet4: case so_0x1000_get_5 ret=-1 errno=22 len=8 v=bbbbbbbbbbbbbbbb
pinet4: case so_0x1000_set_0 ret=-1 errno=22

#10330 recorded it as a remaining difference ("which AnyPS5 still answers with ENOPROTOOPT"), and the getsockopt/setsockopt debt entry says the same. FreeBSD 11.4 agrees: do_getopt_accept_filter and do_setopt_accept_filter return EINVAL when SO_ACCEPTCONN is not set (uipc_accf.c, L206-L208, L264-L266).

In SocketOptions.cpp, SetSocketLevelOption and GetSocketLevelOption now check option 0x1000 first:

  • If the socket is not listening (the existing NativeListening), they return ErrorInvalid, and the output buffer is not written.
  • If it is listening, they throw NotImplemented_nid_no_patch. That case wasn't measured, and accept filters aren't modelled.

The debt entry is updated to match.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, full Release build on main 4017310678 with this change: no failed steps.

  • guest_socket_options, guest_socket_options_multicast, guest_socket_stream_options, guest_udp, guest_socket_descriptors and guest_sce_net pass, 3 runs of 6/6.
  • New case in GuestSocketOptions.cpp: on a non-listening TCP socket, setting 5 or 0 fails with EINVAL, and a get fails with EINVAL and leaves the 8-byte output untouched, as in the log.
  • Negative control: with main's SocketOptions.cpp, guest_socket_options fails (2 runs).
  • tools/check_conventions.py --base origin/main passes.
  • Not tested: Linux and macOS. NativeListening already has a path for each.

Overlap

pr_overlap only lists #7201 for SocketOptions.cpp; it conflicts with main and doesn't touch these functions. The debt-file rows are other entries.

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:5005435480 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11006). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-25T14:04:20Z. --- ### What `setsockopt` and `getsockopt` of `SO_ACCEPTFILTER` (0x1000) on a socket that is not listening fail with `ENOPROTOOPT` (42), where the console fails them with `EINVAL` (22). @orezraey measured this on a PS5 (firmware 13.60, payload process, non-listening TCP socket, [`pinet4.log`](https://gist.github.com/orezraey/d9efe0da49405f19d72cb35279c0db52)): ``` pinet4: case so_0x1000_default ret=-1 errno=22 len=8 v=bbbbbbbbbbbbbbbb pinet4: case so_0x1000_set_5 ret=-1 errno=22 pinet4: case so_0x1000_get_5 ret=-1 errno=22 len=8 v=bbbbbbbbbbbbbbbb pinet4: case so_0x1000_set_0 ret=-1 errno=22 ``` #10330 recorded it as a remaining difference ("which AnyPS5 still answers with `ENOPROTOOPT`"), and the getsockopt/setsockopt debt entry says the same. FreeBSD 11.4 agrees: `do_getopt_accept_filter` and `do_setopt_accept_filter` return `EINVAL` when `SO_ACCEPTCONN` is not set ([`uipc_accf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_accf.c#L174-L176), [L206-L208](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_accf.c#L206-L208), [L264-L266](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_accf.c#L264-L266)). In `SocketOptions.cpp`, `SetSocketLevelOption` and `GetSocketLevelOption` now check option 0x1000 first: - If the socket is not listening (the existing `NativeListening`), they return `ErrorInvalid`, and the output buffer is not written. - If it is listening, they throw `NotImplemented_nid_no_patch`. That case wasn't measured, and accept filters aren't modelled. The debt entry is updated to match. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, full Release build on main 4017310678 with this change: no failed steps. - `guest_socket_options`, `guest_socket_options_multicast`, `guest_socket_stream_options`, `guest_udp`, `guest_socket_descriptors` and `guest_sce_net` pass, 3 runs of 6/6. - New case in `GuestSocketOptions.cpp`: on a non-listening TCP socket, setting 5 or 0 fails with `EINVAL`, and a get fails with `EINVAL` and leaves the 8-byte output untouched, as in the log. - Negative control: with main's `SocketOptions.cpp`, `guest_socket_options` fails (2 runs). - `tools/check_conventions.py --base origin/main` passes. - Not tested: Linux and macOS. `NativeListening` already has a path for each. ### Overlap `pr_overlap` only lists #7201 for `SocketOptions.cpp`; it conflicts with main and doesn't touch these functions. The debt-file rows are other entries. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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)
MarcZX10 commented 2026-10-25 14:06:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T14:06:41Z.

Review: APPROVED


Reviewed the firmware 13.60 trace and the FreeBSD 11.4 behavior cited here. The non-listening set/get paths return EINVAL before mutating the output buffer, and the regression covers both accepted set values (0 and 5) plus the untouched get sentinel. The existing NativeListening helper uses getsockopt(SO_ACCEPTCONN) on Windows/Linux and proc_pidfdinfo on macOS, so the check follows the platform-specific listener detection already used by SO_ACCEPTCONN. The added code also preserves the explicit unimplemented path for listening sockets, which the trace does not cover. I verified the diff is clean and the changed translation units pass C++20 syntax checks locally. Windows runtime/build evidence is included in the PR; Linux and macOS product runtime tests remain unrun.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643596459 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11006#pullrequestreview-5643596459). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T14:06:41Z. **Review:** APPROVED --- Reviewed the firmware 13.60 trace and the FreeBSD 11.4 behavior cited here. The non-listening set/get paths return EINVAL before mutating the output buffer, and the regression covers both accepted set values (0 and 5) plus the untouched get sentinel. The existing NativeListening helper uses getsockopt(SO_ACCEPTCONN) on Windows/Linux and proc_pidfdinfo on macOS, so the check follows the platform-specific listener detection already used by SO_ACCEPTCONN. The added code also preserves the explicit unimplemented path for listening sockets, which the trace does not cover. I verified the diff is clean and the changed translation units pass C++20 syntax checks locally. Windows runtime/build evidence is included in the PR; Linux and macOS product runtime tests remain unrun.
ahmadmuj commented 2026-10-25 15:31:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T15:31:17Z.

Review: COMMENTED


Reviewed head e829b5c on base 4017310 (current main d2b3ee3 has no changes to these files). Linux: WSL2 Ubuntu 24.04, GCC 13.3, Release.

What I ran:

  • guest_socket_options aborts early on my box at the refused-connect SO_ERROR check (a known WSL networking issue here, it fails on main too), so it never reaches your new case. I ran a local copy of GuestSocketOptions.cpp with only ReportsAndClearsTheConnectError() removed: passes 3/3 on this head. With main's SocketOptions.cpp it fails at "SO_ACCEPTFILTER on a socket that is not listening must fail with EINVAL (22) (errno 42)".
  • A small probe of my own through setsockopt_nid_postfix / getsockopt_nid_postfix. On non-listening TCP and UDP sockets: int 5, int 0, NULL with length 0, a 256-byte accept_filter_arg, and 8- and 256-byte gets all give EINVAL, and the get buffers stay untouched. On a listening TCP socket every case throws your NotImplemented.

So the non-listening fix works on Linux for what the trace covers. One thing on the set path:

FreeBSD 11.4 doesn't check listening first when the value is non-NULL. do_setopt_accept_filter runs sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap) first (uipc_accf.c L232; sooptcopyin returns EINVAL when the length is under minlen, uipc_socket.c L2474-L2475). Then accept_filt_get returns ENOENT for a name that isn't registered (L239-L243), and only then comes the SO_ACCEPTCONN check (L264-L268). The trace sets 4-byte ints, so it fails at the size check and can't tell the two apart.

Example: a 256-byte {af_name = "bogus"} set on a non-listening socket is ENOENT on FreeBSD 11.4 and EINVAL here. Same for "dataready" if the console doesn't register accf_data, and we don't know which filters it has.

Suggestion: keep EINVAL for a NULL value and for a length under 256 (FreeBSD and the trace agree there), and throw for a full-size value until someone measures it. The debt line would match then too; right now it says the set behaves as do_setopt_accept_filter does, which only holds for those two cases.

Optional, since you chose to throw on listening sockets: FreeBSD has exact answers for some of those. A get with no filter attached is EINVAL (L178-L181), and nothing here can attach one. A NULL set is 0 (the delete path, L204-L223), and a short set is EINVAL from the size check. Fine as a follow-up.

Limits: Linux only, no console. The PS5 kernel could differ from FreeBSD here; all of the above comes from the source, not a measurement.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643873117 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11006#pullrequestreview-5643873117). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-25T15:31:17Z. **Review:** COMMENTED --- Reviewed head e829b5c on base 4017310 (current main d2b3ee3 has no changes to these files). Linux: WSL2 Ubuntu 24.04, GCC 13.3, Release. What I ran: - `guest_socket_options` aborts early on my box at the refused-connect `SO_ERROR` check (a known WSL networking issue here, it fails on main too), so it never reaches your new case. I ran a local copy of `GuestSocketOptions.cpp` with only `ReportsAndClearsTheConnectError()` removed: passes 3/3 on this head. With main's `SocketOptions.cpp` it fails at "SO_ACCEPTFILTER on a socket that is not listening must fail with EINVAL (22) (errno 42)". - A small probe of my own through `setsockopt_nid_postfix` / `getsockopt_nid_postfix`. On non-listening TCP and UDP sockets: int 5, int 0, NULL with length 0, a 256-byte `accept_filter_arg`, and 8- and 256-byte gets all give EINVAL, and the get buffers stay untouched. On a listening TCP socket every case throws your NotImplemented. So the non-listening fix works on Linux for what the trace covers. One thing on the set path: FreeBSD 11.4 doesn't check listening first when the value is non-NULL. `do_setopt_accept_filter` runs `sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap)` first ([uipc_accf.c L232](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_accf.c#L232); `sooptcopyin` returns EINVAL when the length is under minlen, [uipc_socket.c L2474-L2475](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L2474-L2475)). Then `accept_filt_get` returns ENOENT for a name that isn't registered ([L239-L243](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_accf.c#L239-L243)), and only then comes the `SO_ACCEPTCONN` check (L264-L268). The trace sets 4-byte ints, so it fails at the size check and can't tell the two apart. Example: a 256-byte `{af_name = "bogus"}` set on a non-listening socket is ENOENT on FreeBSD 11.4 and EINVAL here. Same for `"dataready"` if the console doesn't register accf_data, and we don't know which filters it has. Suggestion: keep EINVAL for a NULL value and for a length under 256 (FreeBSD and the trace agree there), and throw for a full-size value until someone measures it. The debt line would match then too; right now it says the set behaves as `do_setopt_accept_filter` does, which only holds for those two cases. Optional, since you chose to throw on listening sockets: FreeBSD has exact answers for some of those. A get with no filter attached is EINVAL ([L178-L181](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_accf.c#L178-L181)), and nothing here can attach one. A NULL set is 0 (the delete path, L204-L223), and a short set is EINVAL from the size check. Fine as a follow-up. Limits: Linux only, no console. The PS5 kernel could differ from FreeBSD here; all of the above comes from the source, not a measurement.
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!11006
No description provided.