fix(libkernel): read accept's address length in before accepting and fail with EFAULT for unwritable outputs, as FreeBSD accept1 does #18166
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/18166"
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 roboterspinne on 2026-11-10T17:32:03Z.
What
accept_nid_postfix(core/libs/prx/libkernel/Socket/src/Udp.cpp:1756-1799onmain3545fb6d4e, which37f53259ddmerges into this branch; line numbers below refer to3545fb6d4e) checks only that the length pointer is not null when an address buffer is given (:1757, added by #7485). After the connection has been taken off the native queue and installed as a guest descriptor, it callsGuestAddress(peer, address, length)(:1794), which reads*lengthand writes the name and the new length through the raw guest pointers (:543-562). So an unreadable length pointer, a length on a read-only page or an unwritable address buffer crashes the host with an access violation instead of failing withEFAULT, and a length that cannot be read is only noticed after a connection was consumed. FreeBSD 11.4'saccept1(release/11.4.0,sys/kern/uipc_syscalls.c) callskern_accept4without touchinganamelenwhenunameisNULL(lines 310-311), and otherwise doescopyin(anamelen, &namelen)beforekern_accept4and returns its error (313-315, 317), so a fault there comes before the descriptor is looked up and before any connection is dequeued. After accepting it doescopyout(name, uname, namelen)(328) and thencopyout(&namelen, anamelen)(331-332), and on either error it closes the new descriptor withfdcloseand returns the error (333-334).kern_accept4clampsnamelentosa_len(464-465), whichGuestAddressalready does withmin(*length, bytes[0]). The sibling calls in the same file already copy through the checked helpers:getsocknameandgetpeernameread the capacity withGuestCopyIn_nid_no_patch(:1927,:1953) and write throughCopyAddressOut(:1916-1924),recvfromusesReportAddress(defined at:540-541), andsocketpaircloses what it installed when its copyout fails (:1627-1649).GuestCopyIn_nid_no_patchandGuestCopyOut_nid_no_patch(core/libs/prx/libc/src/General.cpp:908,:864) reject a null or unusable range instead of faulting.acceptreads the 4-byte capacity fromlengthwithGuestCopyIn_nid_no_patchbefore the descriptor lookup and fails withEFAULT(14) if it cannot. A null length still fails there, so theEFAULTorder merged in #7485 is unchanged. AfterInstall, the name is built in a local 28-byte buffer withGuestAddress(clamped to the capacity and tosa_len) and written withReportAddress: the name (nothing when the size is 0), then the size. If either copyout fails,acceptcloses the new descriptor withGuestSockets::Closeand fails withEFAULT. Without an address buffer the length pointer is ignored, as before. Valid pointers take the same path with the same results.sceNetAcceptin libSceNet is not touched.guest_socket_connect_listeninggains a block after the existing #7485 checks. It maps three 16 KiB direct-memory pages (sceKernelAllocateDirectMemory,sceKernelMapDirectMemory), sets the second to prot 1 and the third to prot 0 withsceKernelMprotect, and checks:accept(-1, buf, unreadable length)fails withEFAULT, notEBADF; on a fresh listener with a pending client, an unreadable length fails withEFAULTand the nextaccept(l, NULL, NULL)still returns that connection (a byte sent by the client arrives on it); a length on the read-only page, and an address buffer on the read-only page and on the prot-0 page, each fail withEFAULT, and the client's nextrecv(5 sSO_RCVTIMEO) returns 0 orECONNRESET, which shows the accepted socket was closed, sinceGuestSockets::Closeis the only path that closes it; a capacity of 0 with the address buffer on the prot-0 page succeeds and reports 0, and a capacity of 8 reports 8 and copies only the first 8 bytes of the name; a valid 28-byte buffer reports length 16 andAF_INETas the positive control. Every newaccepton the listener has a connection already queued, so nothing blocks; the test runs in under 2 s.accept) moves the body ofaccept_nid_postfixinto a staticAccept()andInstallinto the new wrapper, and keeps the rawif (address) GuestAddress(peer, address, length). It (head99dee99565) already conflicts withmain3545fb6d4einUdp.cppin three places: the first lines ofaccept_nid_postfix, and the first lines ofsend_nid_postfixand ofrecv_nid_postfix, wheremainnow begins with the forwarded-socket block (ForwardsFor). A trial merge with this branch conflicts in four places inUdp.cpp: those three (in the first, the capacity copy-in replaces the null check) and the end of thetryblock, where this branch replaces theGuestAddressline afterInstall. In #12965,File::WithIoDescriptorfails a negative descriptor withEBADFbeforeAccept()runs, andpeeris local toAccept(). Resolving it therefore means doing the capacity copy-in at the top ofaccept_nid_postfix, beforeWithIoDescriptorand the restart loop, havingAccept()hand out the built name, and doing the copyout and the close on failure afterInstallin the wrapper.pr_overlap.py --branch, run on the merge commit37f53259dd, lists #18476, opened after this PR: one conflict inUdp.cppat the first lines ofaccept_nid_postfix(:1757), where #18476 adds the path for libSceNet-forwarded sockets directly after the null check this branch replaces. It is not a duplicate (#18476 handles forwarded sockets, this PR the native path, and both read the length in before accepting); whichever lands second keeps both changes. It cannot compare #11896, #12682, #12688, #12700, #12965, #17253 and #18167, which do not merge withmainas their heads stand on GitHub. Checked by hand on37f53259ddagainstmain3545fb6d4e: a trial merge of this branch with #11896 (ef5ae8ee90), #12682 (32121ca48d), #12688 (8f34999f2f), #12700 (bf740fcd27) and #17253 (c4491afb31) conflicts in the same files and the sameUdp.cpphunks asmainwith it, and with #12965 (99dee99565) in the same files, with the fourthUdp.cppconflict described in the #12965 bullet above. #11896, #12682 and #17253 do not changeaccept_nid_postfix. #12688 and #12700 wrap the::acceptcall inTransfer(..., TransferKind::Accept)between the two places this branch changes;accept_nid_postfixmerges without a marker and both changes can be kept. #18167, by the same author, changesrecvfrom; its merge commitb5b8dabba0and37f53259ddmerge with each other without a conflict.Tested
Windows 11, WinLibs GCC 15.2.0, build directory configured with Ninja, Release and
BUILD_TESTING=ON. The results below are from before the merge unless they name37f53259dd: they were taken on this branch's commit rebased ontoc5488026ab, andb8194198cf, the parent ofc37af635f3, changes none ofUdp.cpp,GuestSocketConnectListening.cpp,General.cpp,GuestArena.cpp,GuestKqueue.cppandcore/libs/prx/libkernel/CMakeLists.txtagainst it (emptygit diff --statfor them).37f53259ddmergesmain3545fb6d4einto the branch without conflicts;git diff 3545fb6d4ed 37f53259ddis the same change to the same two files. Betweenb8194198cfand3545fb6d4emainchangedUdp.cpp(#18767, #18811, #18472, #18474, #17248, #19520),General.cpp,GuestArena.cpp,GuestKqueue.cppandcore/libs/prx/libkernel/CMakeLists.txt, but notGuestSocketConnectListening.cppand not the lines ofaccept_nid_postfix,ReportAddress,GuestAddress,GuestCopyIn_nid_no_patchandGuestCopyOut_nid_no_patch. Each test was built withcmake --build build --parallel 4 --target <targets>and run directly frombuild/tests, withbuild/core/libs/libs/unpatchedandbuild/core/libs/libsprepended toPATH:Udp.cppofc5488026ab:guest_socket_connect_listening_testsfailed withaccept must read the address length before the descriptor and fail with EFAULT (14) when it cannot (errno 9). With the earlier cases left out one at a time in a local copy of the test, the old library crashed on each later case with0xc0000005:read of 0x0000000200008000(unreadable length, pending client),write of 0x0000000200004000(read-only length),write of 0x0000000200004040(address on the read-only page) andwrite of 0x0000000200008000(address on the prot-0 page).guest_socket_connect_listening_testspassed in three runs.accept_nid_postfix: builtguest_net_kernel_forwards_tests guest_poll_tests guest_refused_connect_tests guest_socket_event_tests guest_socket_host_write_tests guest_socket_on_non_socket_tests guest_socket_options_tests guest_socket_port_zero_tests guest_socket_read_write_tests guest_socket_send_before_connect_tests guest_socket_stream_options_tests guest_socket_timeouts_tests guest_tcp_tests guest_udp_tests guest_underscore_entry_tests guest_socket_connect_listening_tests, then ran, each in itsctestworking directory, the 30 command lines thatctest --test-dir build -N -V -R '^(guest_sce_net_kernel_forwards|guest_poll|guest_refused_connect|guest_socket_events|guest_socket_host_write_.*|guest_socket_on_non_socket|guest_socket_options|guest_socket_options_multicast|guest_socket_port_zero|guest_socket_read_write|guest_socket_send_before_connect|guest_socket_stream_options|guest_socket_timeouts|guest_tcp|guest_udp|guest_underscore_entr.*|guest_socket_connect_listening)$'lists: 30/30 passed, 10.21 s in total.37f53259dd, in a fresh build directory with the same options and the same sixteen targets:guest_socket_connect_listeningpassed in three runs (ctest --repeat until-fail:3), and the same 30 runs passed 30/30 (16.93 s in total). Counter-check on37f53259ddwithUdp.cppchecked out from3545fb6d4e:guest_socket_connect_listeningfailed withaccept must read the address length before the descriptor and fail with EFAULT (14) when it cannot (errno 9); withUdp.cpprestored it passed again.core/libs/prx/libkernel/CMakeLists.txt:2376), so their CI runs it. ThereGuestCopyIn_nid_no_patchandGuestCopyOut_nid_no_patchuseprocess_vm_readv/process_vm_writevandmach_vm_read_overwrite/mach_vm_write, and the non-WindowsGuestArenaBeginHostWrite_nid_postfix(core/libs/prx/libc/src/GuestArena.cpp:941-949) only reapplies the guest's own protection and never makes a prot-1 page writable. The mergedcore/libs/tests/GuestKqueue.cpp:197-199, registered on every host, already expectsEFAULTfor a write to a prot-1 page.python tools/check_conventions.py --base upstream/main --head HEAD(upstreammain=3545fb6d4e, HEAD at the merge commit37f53259dd) with this title and description passed.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-10T19:37:11Z.
Review: CHANGES_REQUESTED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
c37af635f3:tools/check_conventions.pypassed;git diff --checkclean.pipe-write (macos-15-intel),pipe-write (macos-15).guest_socket_connect_listening_testsbuilt cleanly.guest_socket_connect_listening):core/libs/prx/libkernel/Socket/src/Udp.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Requesting changes for the points above.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-15T20:23:45Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
37f53259dd:tools/check_conventions.pypassed;git diff --checkclean.guest_socket_connect_listening_testsbuilt cleanly.guest_socket_connect_listening):core/libs/prx/libkernel/Socket/src/Udp.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-15T20:23:45Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
37f53259dd:tools/check_conventions.pypassed;git diff --checkclean.guest_socket_connect_listening_testsbuilt cleanly.guest_socket_connect_listening):core/libs/prx/libkernel/Socket/src/Udp.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me