fix(libkernel): copy an ioctl input argument in before dispatch, as FreeBSD 11.4's sys_ioctl does #22395
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/22395"
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 brajankw on 2026-11-17T14:05:19Z.
What
FreeBSD 11.4's
sys_ioctl(sys/kern/sys_generic.c:703-746) copies anIOC_INargument in beforekern_ioctllooks the descriptor up. AnIOC_VOIDrequest with length 4 passes its argument by value. AnIOC_OUTbuffer is only zero-filled, and copied out when the request succeeds. Here the order differed:File::Ioctlrefused only a nullIOC_INargument. An unreadable non-null one went on to dispatch, so a closed descriptor gaveEBADF, and a kqueue or a termios request gaveENOTTY, where FreeBSD givesEFAULT. Now a newFile::CopyIoctlInputcopies theIOC_INpayload right after the malformed-request check and fails withEFAULT.FIONBIOandFIOASYNCthen read that copy, as FreeBSD's handlers read the kernel buffer, instead of reading guest memory again. The pipe branch's own copy is dropped.EFAULTfor any request with a length. SoIOC_VOIDandIOC_OUTrequests the socket refuses gotEFAULTinstead of theirENOTTYorEADDRNOTAVAIL, and an unreadableIOC_INargument got the refusal instead ofEFAULT. The socket path now uses the same copy, andFIONBIOreads it.FIONREADkeeps itsEFAULTfrom the copy-out after it succeeds.sceNetSocketsockets lent to libkernel keep their own null check; that path is unchanged.GuestKernelIoctlFionread.cppaddsAuditInputCopiedBeforeDispatch, which runs these checks through bothioctland_ioctl:FIONBIOargument on a closed descriptor fails withEFAULT;FIONREADwith a null argument on a closed descriptor fails withEBADF;EFAULT;_IOR('x', 1, int)and theIOC_IN|IOC_VOIDrequest0xa0047801with a null argument fail withEADDRNOTAVAIL, the answer of FreeBSD 11.4'sin_controlfor a request it does not know, since nothing is copied before dispatch;EFAULT, so the whole length is copied in.GuestKernelAppArchive.cpp:FIONBIOwith an unreadable argument on an/app0directory descriptor fails withEFAULT, also when the directory is virtual (the copy-in comes before that check).GuestKernelFcntl.cpp: #7956 pinnedEFAULTfor0x20046601(IOC_VOID, length 4) with a null argument on a UDP socket, as a boundary of the old null check; no measurement backs it. That check now uses0x20047801(groupx) and expectsEADDRNOTAVAIL, which still shows the request is taken as well-formed. A groupfrequest would hit the tree's unmeasuredENOTTYexception for that group, so it is avoided here.Tested
Linux (Debian 13, GCC 14.2.0, Release). Rebased onto
maineb4300802c; the only conflict was a test constant next tomain's newDirectoryFlag. The runs after the first bullet are from the earlier head22652acce4onmain9f94e4a2ce.affff6f058, merged with my eight other updated branches ontomainbd9a6c00a6:ctestover the 23 tests these branches touch (guest_kernel_ioctl_fionread,guest_kernel_ioctl_device_type,guest_kernel_fcntl*,guest_kernel_app_archive_*,guest_kernel_sys_ksem,guest_kqueue,guest_equeue_events,guest_socket_events,guest_system_configuration,guest_socket_pair,guest_recvmsg_memory,guest_kernel_sys_cpuset,np_uds_events,guest_np_uds_*andguest_np_trophy_*): all pass.ctest -R '^(guest_kernel_ioctl.*|guest_kernel_fcntl.*|guest_socket.*|guest_kqueue|guest_tcp|guest_udp)$': 23 passed.main, the new check aborts at the first call, which fails withEBADF(9) instead ofEFAULT.ctest(6006 tests,-j 12) on this branch and my other four branches of this round, merged ontomain1af270fa29: 5988 passed. All 18 failures also fail onmainin the same environment:.gitdirectory, network access or a passwd entry for the user:conventions_git_errors,test_script_registration,web_library_assets,title_run,title_run_identity,guest_kernel_sys_login,guest_net_ctl,guest_getloginandguest_getlogin_no_session;agc_drivercases (agc_driver_image_table_packed_guardspasses when run on its own);patched_self_imports,guest_libressl3_ec_parametersandguest_libressl3_asn1fail onmain1af270fa29when run on their own.python3 tools/check_conventions.py --base origin/main: passed.python3 tools/pr_overlap.py --branchfailed closed on #7121, #9557, #9566, #11896, #12682, #12688, #12700, #12965, #18457 and #19824, which conflict withmain. None of their diffs changes when anioctlargument is copied.EFAULTforFIONREADwith a null argument on a closed socket. FreeBSD andmaingiveEBADFthere, and so does this PR.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-17T14:06:38Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
22652acce4:tools/check_conventions.pypassed.guest_kernel_fcntl_tests,guest_kernel_ioctl_fionread_testsbuilt cleanly.guest_kernel_fcntl,guest_kernel_ioctl_fionread):core/libs/prx/libkernel/File/include/DescriptorControl.hpp,core/libs/prx/libkernel/File/src/DescriptorControl.cpp,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 MarcZX10 on 2026-11-23T03:54:33Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the points below are optional.
What I ran
Reviewed head
22652acce41b20e8ff7ddc738f2a16fe29346ca4using the prepared diff and pinned repository reader. No tests were run in this review.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No tests or platform checks were run. The author reports focused tests and a Linux full test run in
description.md; these were not independently verified. All five files listed as reviewed inmanifest.jsonwere read:core/libs/prx/libkernel/File/include/DescriptorControl.hpp,core/libs/prx/libkernel/File/src/DescriptorControl.cpp,core/libs/prx/libkernel/Socket/src/Udp.cpp,core/libs/tests/GuestKernelFcntl.cpp, andcore/libs/tests/GuestKernelIoctlFionread.cpp.files.jsonwas not available in the reader listing; no excluded files were identified in the available manifest.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T12:20:12Z.
Review: COMMENTED
Reviewed — comments
Changes are required before merge: clarify or cover the forwarded-socket ioctl path, which retains its prior argument handling.
What I ran
affff6f058ff150676a1a469d6107ec674aae7b5usingrepo_read.py.Results
Findings
Blocking
None.
Non-blocking
CopyIoctlInputand retains the old null-only validation beforeKernelControlSocketIoctl_nid_no_patch(core/libs/prx/libkernel/Socket/src/Udp.cpp:2299-2302). As a result, unreadable non-nullIOC_INarguments can still reach dispatch rather than returningEFAULTfirst. The PR description sayssceNetSocketsockets lent to libkernel are unchanged, but this branch is the separateKernelFindSocketNativeforwarding path; its intended scope and behavior are not verified. Please add a targeted test or document that this branch is intentionally excluded.Not verified
files.jsonwas unavailable through the reader.To get this to a good state
Nothing is required. Optional: test and clarify the forwarded-socket path described above.