fix(libkernel): fail getsockopt and setsockopt with EFAULT on an unusable pointer #13752
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/13752"
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 Gruzzberry on 2026-10-31T04:11:07Z.
What
getsockoptandsetsockopton a libkernel socket crashed the emulator when the guest passed an unmapped or unreadableoptvaloroptlenpointer (for example0x10or aPROT_NONEpage): the option code copied fromvaluewith a rawmemcpy, read*lengthraw, and aNULLoptlenreturnedEINVAL. FreeBSD 11.4 fails these withEFAULT(sys_setsockopt/kern_setsockopt/sys_getsockopt/kern_getsockopt,sooptcopyin/sooptcopyoutinuipc_socket.c).optvalinSocketOptions.cppnow goes throughGuestCopyIn_nid_no_patch; an unreadable pointer givesEFAULT. The checks that come before the copy insooptcopyinstay in front of it: bad descriptorEBADF, unknown levelEINVAL, unknown optionENOPROTOOPT, a short lengthEINVAL. TheIP_MULTICAST_IFip_mreqnform is copied in as one 12-byte block instead of being indexed in place.getsockoptcopies the guestoptlenin first whenoptvalis notNULL(EFAULT, before the descriptor lookup, assys_getsockoptdoes), works on a local length, writes the value and the length withGuestCopyOut_nid_no_patch, and stores the length only when the call succeeded. ANULLoptvalmeans length 0; an unwritableoptlenthen givesEFAULTonly after the option itself succeeded. ANULLoptlenwith a non-NULLoptvalis nowEFAULT(wasEINVAL), ascopyin(NULL)is in FreeBSD.setsockoptalso rejects a length aboveINT_MAXwithEINVALafter theNULL-with-lengthEFAULTcheck, askern_setsockoptdoes.sceNetSetsockopt/sceNetGetsockoptstill 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 currentmain.guest_socket_options: newBadPointersFailblock.setsockoptwith0x10and aPROT_NONEpage asoptvalfails withEFAULTforSO_KEEPALIVE,SO_LINGER,SO_RCVTIMEO,SO_NOSIGPIPE,SO_SNDBUF,TCP_NODELAY,IP_TTL,IP_ADD_MEMBERSHIP,IP_MULTICAST_IF(4 and 12 bytes) andIP_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 of0x80000000givesEINVAL.getsockoptinto0x10, aPROT_NONEpage and a read-only page fails withEFAULTand keeps the length; an unreadableoptlen(0x10,PROT_NONE,NULL) fails withEFAULTbefore the descriptor and option checks; with aNULLoptvalan unwritableoptlenfails withEFAULTonly for an option that exists.mainthe new block crashes the test (SEGFAULT); on this branchguest_socket_optionsandguest_socket_options_multicastpass three runs out of three.net_ipv6only, which exercises the samegetsockoptpath on guest arena memory (read-only length page leaves the value untouched), passes 3/3.vulkan_validation_gate_reportsandcheck_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted(both are Python tests that fail on the Windows Storepython.exehere and do not touch sockets).pr_overlap_encoding,agc_host_import_cut_lifetimeandagc_driver_depth_plane_partial_storepassed in this run.python tools/check_conventions.py --base upstream/main: passes.optvalnon-NULL, an unwritableoptlenis reported before a bad option (EFAULTinstead ofENOPROTOOPT), because an existing test requires that the value is not modified when the length cannot be stored; agetsockoptlength aboveINT_MAXis accepted (FreeBSD:EINVAL), pinned by thenet_ipv6onlytest.Overlaps
Udp.cpp(L1249-1255); it was opened first, so it goes first and this PR rebases.tools/pr_overlap.pycould not compareUdp.cppandSocketOptions.cppagainst #5021, #7088, #8605, #9713, #10645, #12688, #12700 and #12965, which conflict withmainthemselves. The diffs of #12688, #12700 and #12965 do not touch the option functions; the older ones were not compared.Checklist
main; no other open PR implements the same functionNotImplemented_nid_no_patch); no silent stubs added.mdfiles or images added to the repositoryUdp.cpp)AI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-31T06:23:37Z.
Review: COMMENTED
Linux run of head
1f2365a5d1, merged (--no-commit) ontomain6a691f69ee. 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.guest_socket|guest_udp|guest_tcp|guest_poll|guest_net|guest_select|guest_kernel_fcntl: 35/35.public_headers: 65/65.SocketOptions.cppandUdp.cppreverted tomainand the new test kept,guest_socket_optionscrashes (SEGFAULT) 3/3.main+ #13695 + #13738 + #13742 + #13752 + #13756 merged in that order (all touchUdp.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 andpublic_headers65/65.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.