Repository navigation
fix(libkernel): copy an ioctl input argument in before dispatch, as FreeBSD 11.4's sys_ioctl does - #22395
Conversation
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 22652acce4:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_kernel_fcntl_tests,guest_kernel_ioctl_fionread_testsbuilt cleanly. - CTest (
guest_kernel_fcntl,guest_kernel_ioctl_fionread):1/2 Test #5570: guest_kernel_ioctl_fionread ...... Passed 0.00 sec 2/2 Test #5566: guest_kernel_fcntl ............... Passed 0.01 sec 100% tests passed, 0 tests failed out of 2 - Negative control: with
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.1/2 Test #5566: guest_kernel_fcntl ...............Subprocess aborted***Exception: 0.13 sec 2/2 Test #5570: guest_kernel_ioctl_fionread ......Subprocess aborted***Exception: 0.15 sec 0% tests passed, 2 tests failed out of 2 - Code reading:
- Verified File::CopyIoctlInput in DescriptorControl.cpp:575-583 properly extracts length from request bits, checks RequestIn/RequestVoid/length==0, checks argument == nullptr, resizes vector, and invokes GuestCopyIn_nid_no_patch
- Verified File::Ioctl in DescriptorControl.cpp:587-601: CopyIoctlInput failure returns Fail(GuestEfault) prior to descriptor classification and lookup, matching FreeBSD sys_ioctl order
- Verified std::memcpy(&enable, input.data(), sizeof(enable)) in DescriptorControl.cpp:600 and Udp.cpp:2123 safely reads from the copied buffer for NonblockingRequest / AsynchronousRequest
- Verified Udp.cpp ioctl_nid_postfix correctly invokes File::CopyIoctlInput before socket Lookup, returning errno 14 (EFAULT) on copy failure
- Verified test coverage in GuestKernelIoctlFionread.cpp:404-431 (AuditInputCopiedBeforeDispatch) and GuestKernelFcntl.cpp:224 covering closed descriptors, kqueue, pipe, socket, and void/output argument corner cases
Looks Good To Me
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
Nothing blocks this change; the points below are optional.
What I ran
Reviewed head 22652acce41b20e8ff7ddc738f2a16fe29346ca4 using the prepared diff and pinned repository reader. No tests were run in this review.
Results
| Check | Result | Note |
|---|---|---|
| Diff and targeted source review | passed | Five changed files read |
| Tests in this review | not run | Author-reported runs not repeated |
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 in manifest.json were 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, and core/libs/tests/GuestKernelIoctlFionread.cpp. files.json was 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.
|
On Windows the merge passes the nine ioctl and socket tests 3/3. With Existing runs.
What this run adds.
Code. At the head:
PR body.
Suggestion (non-blocking). These two checks pass on the merge 3/3 and catch m3 and m5. I added them locally only. At the start of constexpr std::size_t Page = 0x4000;
auto* const pages = static_cast<unsigned char*>(mmap_nid_postfix(nullptr, 2 * Page, 3, 0x1002, -1, 0));
Require(pages != reinterpret_cast<unsigned char*>(static_cast<std::uintptr_t>(-1)) && mprotect_nid_postfix(pages + Page, Page, 0) == 0, "guard page setup failed");
Require(ioctl_nid_postfix(9999, 0x80087801, pages + Page - 4) == -1 && *__error_nid_postfix() == ErrorFault,
"an input argument whose last bytes are unreadable must fail with EFAULT (14)");After Check(ioctl_nid_postfix(fd, 0x8004667e, reinterpret_cast<void*>(std::uintptr_t{0x10})) == -1 && *__error_nid_postfix() == 14);The second one fails only in Negative control and mutations. Each change was made on the merge, rebuilt, and run with the nine tests. The pins ran with
Tests.
On On the merge, Host:
AI-assisted: yes |
…reeBSD 11.4's sys_ioctl does Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
22652ac to
affff6f
Compare
|
@Helldiverr I added both of your pins at
#22396 is rebased onto this head. |
|
Rechecked on Windows at the new head Code.
Mutations. These are the two survivors from the earlier run. Each was applied to the merge and run once with the tests below.
In each mutation run, the other 13 tests passed. Runs.
On Host: Ryzen 9 9950X3D, Windows 11 Pro 26300, WinLibs GCC 15.2, Ninja, Release. AI-assisted: yes |
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — comments
Changes are required before merge: clarify or cover the forwarded-socket ioctl path, which retains its prior argument handling.
What I ran
- Reviewed head
affff6f058ff150676a1a469d6107ec674aae7b5usingrepo_read.py. - No commands or tests were run.
Results
| Check | Result | Note |
|---|---|---|
| Source review | passed | All six changed files read |
| Tests | not run | Author reports Linux test runs |
Findings
Blocking
None.
Non-blocking
- The forwarding branch for kernel-owned sockets bypasses
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
- Tests and builds were not run. The reported test results in the PR description are author-reported.
- No repository files were excluded: all six changed files listed in the manifest were read. The review bundle’s
files.jsonwas unavailable through the reader. - Forwarded-socket behavior was not verified against FreeBSD or on a console.
To get this to a good state
Nothing is required. Optional: test and clarify the forwarded-socket path described above.
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: fix(libkernel): fail a malformed ioctl request with ENOTTY #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 feat(libkernel): report open, read and close notes on watched files #7121, feat(libkernel): keep the SIGIO owner of a pipe for F_GETOWN and F_SETOWN #9557, feat(libkernel): answer tcgetpgrp and tcsetpgrp on a pipe with its SIGIO owner #9566, fix(libkernel): wait for the rest of a MSG_WAITALL stream receive in the guest layer #11896, feat(libkernel): cancel blocked POSIX reads with owned operations #12682, fix(libkernel): preserve blocking sockets over nonblocking native transport #12688, feat(libkernel): cancel blocked POSIX writes with partial progress #12700, feat(libkernel): support deferred cancellation in POSIX accept #12965, fix(libkernel): fcntl F_GETOWN and F_SETOWN store and return the owner of a pipe #18457 and fix(kernel): handle redirected standard pipes #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)