fix(libkernel): fail an unknown setsockopt or getsockopt level with EINVAL like FreeBSD #8448

Merged
xXPBSXx merged 7 commits from upstream-pr/8448 into main 2026-11-14 03:29:23 +00:00
xXPBSXx commented 2026-10-20 12:47:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T12:47:01Z.


What

setsockopt_nid_postfix and getsockopt_nid_postfix (Udp.cpp) answered ENOPROTOOPT for every level except SOL_SOCKET, IPPROTO_TCP and IPPROTO_IPV6. FreeBSD 11.4 hands such a level to the protocol, and ip_ctloutput (ip_output.c#L943-L944), ip6_ctloutput (ip6_output.c#L1459) and uipc_ctloutput (uipc_usrreq.c#L1228-L1229) fail every level they do not own with EINVAL. Now an unknown level fails with EINVAL.

The levels a protocol owns keep ENOPROTOOPT for the options nothing here implements: IPPROTO_IP on an AF_INET socket and IPPROTO_UDP on a datagram socket (udp_ctloutput). Level 0 on an AF_UNIX socket fails with EOPNOTSUPP for the options uipc_ctloutput does not handle; LOCAL_PEERCRED (get), LOCAL_CREDS and LOCAL_CONNWAIT, which it handles, throw as unimplemented. The UDP level on an AF_INET6 datagram socket fails with EINVAL.

The test pins the unknown-option answer with option 0x7777 at level 0, the number the IPPROTO_IP getsockopt check in the same file already uses. Option 1 would be IP_OPTIONS, which FreeBSD 11.4's ip_ctloutput accepts (ip_output.c:983 and :1006, ip_options.c:600); wachi35676 pointed this out in a comment on this pull request.

The Technical debt entry is not touched: main has since split the getsockopt, setsockopt entry into measured per-option entries, and its unmeasured sentence on the IPPROTO_IP level is now only exact for AF_INET sockets. That is left for a separate note.

Merge with main 3aa0ac20df: one conflict, in Udp.cpp. This change and main (SendBeforeConnect) each added a function at the same place; both are kept and the content of this change is unchanged. The diff of this branch against main is the same before and after the merge: Udp.cpp and GuestSocketOptions.cpp, 47 insertions and 2 deletions. Overlap (P2, 2026-11-08, list of open pull requests from 04:42 UTC, main 62e7ca8676, checked 04:42-04:48 UTC, head 58a3e046e80f): of the 19 open pull requests that change core/libs/prx/libkernel/Socket/src/Udp.cpp or core/libs/tests/GuestSocketOptions.cpp, each was merged with and without this branch. None conflicts only with this branch (0 of 19); 5 conflict with main without it as well (#16585, #15785, #14756, #11896, #7088); the other 14 merge cleanly with and without it. The head of #8463 (a84f4c3dc4a5, also Udp.cpp) merges cleanly with this branch, and both heads merge cleanly with main 8eb293447e. #10320 (merged) changed the same two lines and routes level 0 to SetIpLevelOption/GetIpLevelOption; here level 0 reaches UnsupportedLevelError only on an AF_UNIX socket, so the PS5-measured AF_INET and AF_INET6 answers of #10320 are unchanged.

Fixes #8447

Tested

Head 58a3e046e8 (this branch merged with main 3aa0ac20df, 7 commits of which 2 are merges). Linux, WSL2 Ubuntu 24.04 (virtualised kernel, not bare metal), GCC 13.3.0, Release, fresh build directory, 3 runs of ctest -R '^guest_socket_(options|send_before_connect|stream_options|pair|error|descriptors)$':

  • All 6 tests pass in each of the 3 runs (100% tests passed, 0 tests failed out of 6), including guest_socket_options, which holds the new UnknownLevelsFollowFreeBsd check, and guest_socket_send_before_connect, which covers the function main added next to this change.
  • python tools/check_conventions.py --base upstream/main on this head: All conventions checks passed.

Earlier measurements, on bases that are no longer current:

  • On upstream/main 02b5a8e55f plus the first commit (Windows 10 19045, MinGW-w64 GCC 15.2.0, Release): guest_socket_options failed without the change to Udp.cpp (an unknown protocol level on a stream socket must fail with EINVAL (22), ip_ctloutput, exit 0xc0000409) and passed with it; 40 socket and network tests passed.
  • Rebased onto main b607bfdc8b (head 425ada08af), both arms on the same base: guest_socket_options and guest_socket_stream_options passed 4 of 4 runs; with Udp.cpp restored to main, guest_socket_options failed 4 of 4 and guest_socket_stream_options passed 4 of 4. Linux (WSL2) one run per arm gave the same split.
  • Measured by wachi35676 at head 77c7232681 merged onto main 0f34ebb85 on Windows 11 and WSL2, 3 runs: guest_socket_options, guest_socket_stream_options, guest_socket_error, guest_tcp, guest_udp and guest_socket_pair pass on both hosts; with Udp.cpp from main, guest_socket_options still fails on Windows.

Not tested by me on the merge head: Windows, macOS, bare-metal Linux, a console, a title run, and the without-the-change arm on main 3aa0ac20df. Not covered by the new test: any console behaviour of the throwing options.

Checklist

  • Based on current main; no other open PR implements the same functions (#8392, merged, touched the same two functions for pointer and length checks, not the level)
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; 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: #8447
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4935938510 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8448). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T12:47:01Z. --- ### What `setsockopt_nid_postfix` and `getsockopt_nid_postfix` (Udp.cpp) answered `ENOPROTOOPT` for every level except `SOL_SOCKET`, `IPPROTO_TCP` and `IPPROTO_IPV6`. FreeBSD 11.4 hands such a level to the protocol, and `ip_ctloutput` ([ip_output.c#L943-L944](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/netinet/ip_output.c#L943-L944)), `ip6_ctloutput` ([ip6_output.c#L1459](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/netinet6/ip6_output.c#L1459)) and `uipc_ctloutput` ([uipc_usrreq.c#L1228-L1229](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_usrreq.c#L1228-L1229)) fail every level they do not own with `EINVAL`. Now an unknown level fails with `EINVAL`. The levels a protocol owns keep `ENOPROTOOPT` for the options nothing here implements: `IPPROTO_IP` on an `AF_INET` socket and `IPPROTO_UDP` on a datagram socket ([udp_ctloutput](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/netinet/udp_usrreq.c#L982)). Level 0 on an `AF_UNIX` socket fails with `EOPNOTSUPP` for the options `uipc_ctloutput` does not handle; `LOCAL_PEERCRED` (get), `LOCAL_CREDS` and `LOCAL_CONNWAIT`, which it handles, throw as unimplemented. The UDP level on an `AF_INET6` datagram socket fails with `EINVAL`. The test pins the unknown-option answer with option `0x7777` at level 0, the number the `IPPROTO_IP` getsockopt check in the same file already uses. Option 1 would be `IP_OPTIONS`, which FreeBSD 11.4's `ip_ctloutput` accepts (`ip_output.c:983` and `:1006`, `ip_options.c:600`); wachi35676 pointed this out in a comment on this pull request. The Technical debt entry is not touched: `main` has since split the `getsockopt, setsockopt` entry into measured per-option entries, and its unmeasured sentence on the `IPPROTO_IP` level is now only exact for `AF_INET` sockets. That is left for a separate note. Merge with `main` `3aa0ac20df`: one conflict, in `Udp.cpp`. This change and `main` (`SendBeforeConnect`) each added a function at the same place; both are kept and the content of this change is unchanged. The diff of this branch against `main` is the same before and after the merge: `Udp.cpp` and `GuestSocketOptions.cpp`, 47 insertions and 2 deletions. Overlap (P2, 2026-11-08, list of open pull requests from 04:42 UTC, `main` `62e7ca8676`, checked 04:42-04:48 UTC, head `58a3e046e80f`): of the 19 open pull requests that change `core/libs/prx/libkernel/Socket/src/Udp.cpp` or `core/libs/tests/GuestSocketOptions.cpp`, each was merged with and without this branch. None conflicts only with this branch (0 of 19); 5 conflict with `main` without it as well (#16585, #15785, #14756, #11896, #7088); the other 14 merge cleanly with and without it. The head of #8463 (`a84f4c3dc4a5`, also `Udp.cpp`) merges cleanly with this branch, and both heads merge cleanly with `main` `8eb293447e`. #10320 (merged) changed the same two lines and routes level 0 to `SetIpLevelOption`/`GetIpLevelOption`; here level 0 reaches `UnsupportedLevelError` only on an `AF_UNIX` socket, so the PS5-measured `AF_INET` and `AF_INET6` answers of #10320 are unchanged. Fixes #8447 ### Tested Head `58a3e046e8` (this branch merged with `main` `3aa0ac20df`, 7 commits of which 2 are merges). Linux, WSL2 Ubuntu 24.04 (virtualised kernel, not bare metal), GCC 13.3.0, Release, fresh build directory, 3 runs of `ctest -R '^guest_socket_(options|send_before_connect|stream_options|pair|error|descriptors)$'`: - All 6 tests pass in each of the 3 runs (`100% tests passed, 0 tests failed out of 6`), including `guest_socket_options`, which holds the new `UnknownLevelsFollowFreeBsd` check, and `guest_socket_send_before_connect`, which covers the function `main` added next to this change. - `python tools/check_conventions.py --base upstream/main` on this head: `All conventions checks passed.` Earlier measurements, on bases that are no longer current: - On `upstream/main` `02b5a8e55f` plus the first commit (Windows 10 19045, MinGW-w64 GCC 15.2.0, Release): `guest_socket_options` failed without the change to `Udp.cpp` (`an unknown protocol level on a stream socket must fail with EINVAL (22), ip_ctloutput`, exit `0xc0000409`) and passed with it; 40 socket and network tests passed. - Rebased onto `main` `b607bfdc8b` (head `425ada08af`), both arms on the same base: `guest_socket_options` and `guest_socket_stream_options` passed 4 of 4 runs; with `Udp.cpp` restored to `main`, `guest_socket_options` failed 4 of 4 and `guest_socket_stream_options` passed 4 of 4. Linux (WSL2) one run per arm gave the same split. - Measured by wachi35676 at head `77c7232681` merged onto `main` `0f34ebb85` on Windows 11 and WSL2, 3 runs: `guest_socket_options`, `guest_socket_stream_options`, `guest_socket_error`, `guest_tcp`, `guest_udp` and `guest_socket_pair` pass on both hosts; with `Udp.cpp` from `main`, `guest_socket_options` still fails on Windows. Not tested by me on the merge head: Windows, macOS, bare-metal Linux, a console, a title run, and the without-the-change arm on `main` `3aa0ac20df`. Not covered by the new test: any console behaviour of the throwing options. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (#8392, merged, touched the same two functions for pointer and length checks, not the level) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; 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: #8447 - [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)
xXPBSXx commented 2026-10-21 19:32:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T19:32:26Z.


Rebased onto main 1896871ba7 (head 01b3b7e9a9, same commit). The conflict was only in Udp.cpp: main added ReceiveAnsweredHere next to where UnsupportedLevelError goes; both functions are kept. Windows 10 19045, WinLibs GCC 15.2.0 r7, Release: ctest -R '^guest_socket_options$' passes 3 of 3; the socket options, udp, tcp, socket_pair, poll and socket_address_length tests pass (6 of 6). check_conventions passes (one existing length warning on the long getsockopt debt entry); git merge-tree against main is clean. I did not re-run the test without the change on this head.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6324751932 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8448#issuecomment-6324751932). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T19:32:26Z. --- Rebased onto `main` `1896871ba7` (head `01b3b7e9a9`, same commit). The conflict was only in `Udp.cpp`: `main` added `ReceiveAnsweredHere` next to where `UnsupportedLevelError` goes; both functions are kept. Windows 10 19045, WinLibs GCC 15.2.0 r7, Release: `ctest -R '^guest_socket_options$'` passes 3 of 3; the socket options, udp, tcp, socket_pair, poll and socket_address_length tests pass (6 of 6). `check_conventions` passes (one existing length warning on the long getsockopt debt entry); `git merge-tree` against `main` is clean. I did not re-run the test without the change on this head. AI-assisted (Claude Code).
harisibrar1 commented 2026-10-24 13:51:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:51:14Z.

Review: COMMENTED


Checked against main 522e32e3b6 and FreeBSD 11.4 sys/kern/uipc_usrreq.c, sys/netinet/udp_usrreq.c and sys/netinet/ip_output.c:

  • Main still answers every level other than 0xffff/6/41 with ENOPROTOOPT (Udp.cpp:1148, 1160), and git merge-tree against main is clean. orezraey's #10320 changes the same two lines with PS5 13.60 measurements (gist d9efe0da…, pinet4): level 99 gives EINVAL, IPPROTO_IP on AF_INET6 gives EINVAL, and an unknown IP option gives ENOPROTOOPT. Those agree with this PR, so they may be worth citing. One difference: #10320's blanket EINVAL would also cover level 17 on a datagram socket, where udp_ctloutput (udp_usrreq.c:982) keeps ENOPROTOOPT as this PR does, so whichever lands second should keep that.
  • AF_UNIX, level 0: uipc_ctloutput (uipc_usrreq.c:1237-1309) supports LOCAL_PEERCRED (1, get), LOCAL_CREDS (2) and LOCAL_CONNWAIT (4); only other options get EOPNOTSUPP. With this change (UnsupportedLevelError, Udp.cpp:729), setsockopt(unixfd, 0, 2, &one, 4) fails with EOPNOTSUPP where 11.4 returns 0. Could those three options throw (NotImplemented_nid_no_patch) and the rest return EOPNOTSUPP?
  • Small test note: option 1 at IPPROTO_UDP is UDP_ENCAP (netinet/udp.h), which 11.4 handles on an IPSEC kernel (udp_usrreq.c:1004-1012). An option number with no case would test ENOPROTOOPT without depending on the kernel config.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638480325 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8448#pullrequestreview-5638480325). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:51:14Z. **Review:** COMMENTED --- Checked against main 522e32e3b6 and FreeBSD 11.4 sys/kern/uipc_usrreq.c, sys/netinet/udp_usrreq.c and sys/netinet/ip_output.c: - Main still answers every level other than 0xffff/6/41 with ENOPROTOOPT (Udp.cpp:1148, 1160), and `git merge-tree` against main is clean. orezraey's #10320 changes the same two lines with PS5 13.60 measurements (gist d9efe0da…, `pinet4`): level 99 gives EINVAL, IPPROTO_IP on AF_INET6 gives EINVAL, and an unknown IP option gives ENOPROTOOPT. Those agree with this PR, so they may be worth citing. One difference: #10320's blanket EINVAL would also cover level 17 on a datagram socket, where `udp_ctloutput` (udp_usrreq.c:982) keeps ENOPROTOOPT as this PR does, so whichever lands second should keep that. - AF_UNIX, level 0: `uipc_ctloutput` (uipc_usrreq.c:1237-1309) supports LOCAL_PEERCRED (1, get), LOCAL_CREDS (2) and LOCAL_CONNWAIT (4); only other options get EOPNOTSUPP. With this change (`UnsupportedLevelError`, Udp.cpp:729), `setsockopt(unixfd, 0, 2, &one, 4)` fails with EOPNOTSUPP where 11.4 returns 0. Could those three options throw (`NotImplemented_nid_no_patch`) and the rest return EOPNOTSUPP? - Small test note: option 1 at IPPROTO_UDP is `UDP_ENCAP` (netinet/udp.h), which 11.4 handles on an IPSEC kernel (udp_usrreq.c:1004-1012). An option number with no case would test ENOPROTOOPT without depending on the kernel config. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
harisibrar1 commented 2026-10-24 15:23:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T15:23:27Z.

Review: COMMENTED


Re-checked head b6ca1f3660 against main b607bfdc8b:

  • The AF_UNIX change matches uipc_ctloutput: on get, LOCAL_PEERCRED/LOCAL_CREDS/LOCAL_CONNWAIT now throw; on set, only LOCAL_CREDS/LOCAL_CONNWAIT throw (11.4's SOPT_SET switch has no LOCAL_PEERCRED case, so EOPNOTSUPP there is right), and every other level-0 option gets EOPNOTSUPP. Option 77 at IPPROTO_UDP avoids the IPSEC dependency. Thanks.
  • #10320 has merged since, and it changed the same two lines: main now has if (level != 0xffff && level != 0 && level != 6 && level != 41) return Fail(22); in both setsockopt and getsockopt (3-way git merge-tree shows two conflicts in Udp.cpp, plus GuestSocketOptions.cpp changed on both sides). Two things to keep in mind when resolving:
    • Level 0 is now routed to SetIpLevelOption/GetIpLevelOption, which return EINVAL for anything that isn't IPv4 (SocketOptions.cpp:283, :320). So for the AF_UNIX handling to take effect, the socket.family == GuestUnix check has to run before that dispatch; otherwise every AF_UNIX level-0 call becomes EINVAL.
    • Main's blanket Fail(22) also covers IPPROTO_UDP (17) on a datagram socket, where your helper keeps udp_ctloutput's ENOPROTOOPT, so keeping UnsupportedLevelError for the non-zero levels preserves that.
  • With #10320's PS5 13.60 measurements now on main (level 99 and IPPROTO_IP on AF_INET6 → EINVAL), the remaining parts of this PR are the AF_UNIX and IPPROTO_UDP cases.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638769249 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8448#pullrequestreview-5638769249). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T15:23:27Z. **Review:** COMMENTED --- Re-checked head b6ca1f3660 against main b607bfdc8b: - The AF_UNIX change matches `uipc_ctloutput`: on get, `LOCAL_PEERCRED`/`LOCAL_CREDS`/`LOCAL_CONNWAIT` now throw; on set, only `LOCAL_CREDS`/`LOCAL_CONNWAIT` throw (11.4's `SOPT_SET` switch has no `LOCAL_PEERCRED` case, so EOPNOTSUPP there is right), and every other level-0 option gets EOPNOTSUPP. Option 77 at IPPROTO_UDP avoids the IPSEC dependency. Thanks. - #10320 has merged since, and it changed the same two lines: main now has `if (level != 0xffff && level != 0 && level != 6 && level != 41) return Fail(22);` in both `setsockopt` and `getsockopt` (3-way `git merge-tree` shows two conflicts in Udp.cpp, plus GuestSocketOptions.cpp changed on both sides). Two things to keep in mind when resolving: - Level 0 is now routed to `SetIpLevelOption`/`GetIpLevelOption`, which return EINVAL for anything that isn't IPv4 (SocketOptions.cpp:283, :320). So for the AF_UNIX handling to take effect, the `socket.family == GuestUnix` check has to run before that dispatch; otherwise every AF_UNIX level-0 call becomes EINVAL. - Main's blanket `Fail(22)` also covers IPPROTO_UDP (17) on a datagram socket, where your helper keeps `udp_ctloutput`'s ENOPROTOOPT, so keeping `UnsupportedLevelError` for the non-zero levels preserves that. - With #10320's PS5 13.60 measurements now on main (level 99 and IPPROTO_IP on AF_INET6 → EINVAL), the remaining parts of this PR are the AF_UNIX and IPPROTO_UDP cases. Reviewed with AI assistance (Claude Code); the checks above were run locally 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!8448
No description provided.