fix(libkernel): fail an unknown setsockopt or getsockopt level with EINVAL like FreeBSD #8448
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8448"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T12:47:01Z.
What
setsockopt_nid_postfixandgetsockopt_nid_postfix(Udp.cpp) answeredENOPROTOOPTfor every level exceptSOL_SOCKET,IPPROTO_TCPandIPPROTO_IPV6. FreeBSD 11.4 hands such a level to the protocol, andip_ctloutput(ip_output.c#L943-L944),ip6_ctloutput(ip6_output.c#L1459) anduipc_ctloutput(uipc_usrreq.c#L1228-L1229) fail every level they do not own withEINVAL. Now an unknown level fails withEINVAL.The levels a protocol owns keep
ENOPROTOOPTfor the options nothing here implements:IPPROTO_IPon anAF_INETsocket andIPPROTO_UDPon a datagram socket (udp_ctloutput). Level 0 on anAF_UNIXsocket fails withEOPNOTSUPPfor the optionsuipc_ctloutputdoes not handle;LOCAL_PEERCRED(get),LOCAL_CREDSandLOCAL_CONNWAIT, which it handles, throw as unimplemented. The UDP level on anAF_INET6datagram socket fails withEINVAL.The test pins the unknown-option answer with option
0x7777at level 0, the number theIPPROTO_IPgetsockopt check in the same file already uses. Option 1 would beIP_OPTIONS, which FreeBSD 11.4'sip_ctloutputaccepts (ip_output.c:983and:1006,ip_options.c:600); wachi35676 pointed this out in a comment on this pull request.The Technical debt entry is not touched:
mainhas since split thegetsockopt, setsockoptentry into measured per-option entries, and its unmeasured sentence on theIPPROTO_IPlevel is now only exact forAF_INETsockets. That is left for a separate note.Merge with
main3aa0ac20df: one conflict, inUdp.cpp. This change andmain(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 againstmainis the same before and after the merge:Udp.cppandGuestSocketOptions.cpp, 47 insertions and 2 deletions. Overlap (P2, 2026-11-08, list of open pull requests from 04:42 UTC,main62e7ca8676, checked 04:42-04:48 UTC, head58a3e046e80f): of the 19 open pull requests that changecore/libs/prx/libkernel/Socket/src/Udp.cpporcore/libs/tests/GuestSocketOptions.cpp, each was merged with and without this branch. None conflicts only with this branch (0 of 19); 5 conflict withmainwithout it as well (#16585, #15785, #14756, #11896, #7088); the other 14 merge cleanly with and without it. The head of #8463 (a84f4c3dc4a5, alsoUdp.cpp) merges cleanly with this branch, and both heads merge cleanly withmain8eb293447e. #10320 (merged) changed the same two lines and routes level 0 toSetIpLevelOption/GetIpLevelOption; here level 0 reachesUnsupportedLevelErroronly on anAF_UNIXsocket, so the PS5-measuredAF_INETandAF_INET6answers of #10320 are unchanged.Fixes #8447
Tested
Head
58a3e046e8(this branch merged withmain3aa0ac20df, 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 ofctest -R '^guest_socket_(options|send_before_connect|stream_options|pair|error|descriptors)$':100% tests passed, 0 tests failed out of 6), includingguest_socket_options, which holds the newUnknownLevelsFollowFreeBsdcheck, andguest_socket_send_before_connect, which covers the functionmainadded next to this change.python tools/check_conventions.py --base upstream/mainon this head:All conventions checks passed.Earlier measurements, on bases that are no longer current:
upstream/main02b5a8e55fplus the first commit (Windows 10 19045, MinGW-w64 GCC 15.2.0, Release):guest_socket_optionsfailed without the change toUdp.cpp(an unknown protocol level on a stream socket must fail with EINVAL (22), ip_ctloutput, exit0xc0000409) and passed with it; 40 socket and network tests passed.mainb607bfdc8b(head425ada08af), both arms on the same base:guest_socket_optionsandguest_socket_stream_optionspassed 4 of 4 runs; withUdp.cpprestored tomain,guest_socket_optionsfailed 4 of 4 andguest_socket_stream_optionspassed 4 of 4. Linux (WSL2) one run per arm gave the same split.77c7232681merged ontomain0f34ebb85on Windows 11 and WSL2, 3 runs:guest_socket_options,guest_socket_stream_options,guest_socket_error,guest_tcp,guest_udpandguest_socket_pairpass on both hosts; withUdp.cppfrommain,guest_socket_optionsstill 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
main3aa0ac20df. Not covered by the new test: any console behaviour of the throwing options.Checklist
main; no other open PR implements the same functions (#8392, merged, touched the same two functions for pointer and length checks, not the level).mdfiles or images added to the repositoryAI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T19:32:26Z.
Rebased onto
main1896871ba7(head01b3b7e9a9, same commit). The conflict was only inUdp.cpp:mainaddedReceiveAnsweredHerenext to whereUnsupportedLevelErrorgoes; 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_conventionspasses (one existing length warning on the long getsockopt debt entry);git merge-treeagainstmainis clean. I did not re-run the test without the change on this head.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:51:14Z.
Review: COMMENTED
Checked against main
522e32e3b6and FreeBSD 11.4 sys/kern/uipc_usrreq.c, sys/netinet/udp_usrreq.c and sys/netinet/ip_output.c:git merge-treeagainst 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, whereudp_ctloutput(udp_usrreq.c:982) keeps ENOPROTOOPT as this PR does, so whichever lands second should keep that.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?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.
Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T15:23:27Z.
Review: COMMENTED
Re-checked head b6ca1f3660 against main
b607bfdc8b:uipc_ctloutput: on get,LOCAL_PEERCRED/LOCAL_CREDS/LOCAL_CONNWAITnow throw; on set, onlyLOCAL_CREDS/LOCAL_CONNWAITthrow (11.4'sSOPT_SETswitch has noLOCAL_PEERCREDcase, so EOPNOTSUPP there is right), and every other level-0 option gets EOPNOTSUPP. Option 77 at IPPROTO_UDP avoids the IPSEC dependency. Thanks.if (level != 0xffff && level != 0 && level != 6 && level != 41) return Fail(22);in bothsetsockoptandgetsockopt(3-waygit merge-treeshows two conflicts in Udp.cpp, plus GuestSocketOptions.cpp changed on both sides). Two things to keep in mind when resolving:SetIpLevelOption/GetIpLevelOption, which return EINVAL for anything that isn't IPv4 (SocketOptions.cpp:283, :320). So for the AF_UNIX handling to take effect, thesocket.family == GuestUnixcheck has to run before that dispatch; otherwise every AF_UNIX level-0 call becomes EINVAL.Fail(22)also covers IPPROTO_UDP (17) on a datagram socket, where your helper keepsudp_ctloutput's ENOPROTOOPT, so keepingUnsupportedLevelErrorfor the non-zero levels preserves that.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.