Skip to content

fix(libkernel): copy an ioctl input argument in before dispatch, as FreeBSD 11.4's sys_ioctl does - #22395

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
brajankw:fix/libkernel-ioctl-copies-input-first
Nov 27, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
brajankw:fix/libkernel-ioctl-copies-input-first

Conversation

@brajankw

@brajankw brajankw commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What

FreeBSD 11.4's sys_ioctl (sys/kern/sys_generic.c:703-746) copies an IOC_IN argument in before kern_ioctl looks the descriptor up. An IOC_VOID request with length 4 passes its argument by value. An IOC_OUT buffer is only zero-filled, and copied out when the request succeeds. Here the order differed:

  • File::Ioctl refused only a null IOC_IN argument. An unreadable non-null one went on to dispatch, so a closed descriptor gave EBADF, and a kqueue or a termios request gave ENOTTY, where FreeBSD gives EFAULT. Now a new File::CopyIoctlInput copies the IOC_IN payload right after the malformed-request check and fails with EFAULT. FIONBIO and FIOASYNC then 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.
  • For a guest socket, a null argument failed with EFAULT for any request with a length. So IOC_VOID and IOC_OUT requests the socket refuses got EFAULT instead of their ENOTTY or EADDRNOTAVAIL, and an unreadable IOC_IN argument got the refusal instead of EFAULT. The socket path now uses the same copy, and FIONBIO reads it. FIONREAD keeps its EFAULT from the copy-out after it succeeds.
  • sceNetSocket sockets lent to libkernel keep their own null check; that path is unchanged.
  • GuestKernelIoctlFionread.cpp adds AuditInputCopiedBeforeDispatch, which runs these checks through both ioctl and _ioctl:
    • an unreadable FIONBIO argument on a closed descriptor fails with EFAULT;
    • FIONREAD with a null argument on a closed descriptor fails with EBADF;
    • an unreadable input argument on a kqueue, on a termios request and on a socket fails with EFAULT;
    • on a UDP socket, _IOR('x', 1, int) and the IOC_IN|IOC_VOID request 0xa0047801 with a null argument fail with EADDRNOTAVAIL, the answer of FreeBSD 11.4's in_control for a request it does not know, since nothing is copied before dispatch;
    • an 8-byte input whose last 4 bytes are on a guard page fails with EFAULT, so the whole length is copied in.
  • GuestKernelAppArchive.cpp: FIONBIO with an unreadable argument on an /app0 directory descriptor fails with EFAULT, 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 pinned EFAULT for 0x20046601 (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 uses 0x20047801 (group x) and expects EADDRNOTAVAIL, which still shows the request is taken as well-formed. A group f request would hit the tree's unmeasured ENOTTY exception for that group, so it is avoided here.
  • No debt fragment; no entry records this order.

Tested

Linux (Debian 13, GCC 14.2.0, Release). Rebased onto main eb4300802c; the only conflict was a test constant next to main's new DirectoryFlag. The runs after the first bullet are from the earlier head 22652acce4 on main 9f94e4a2ce.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 22652acce4:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_kernel_fcntl_tests, guest_kernel_ioctl_fionread_tests built 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.cpp reverted to main and the new tests kept, the suite fails:
    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
    
    Restoring the change makes it pass again.
  • 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

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

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.

Pull request Shared files not compared
PR 6936 core/libs/tests/GuestKernelAppArchive.cpp
PR 7121 core/libs/prx/libkernel/File/include/DescriptorControl.hpp
core/libs/prx/libkernel/File/src/DescriptorControl.cpp
PR 8605 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 9557 core/libs/prx/libkernel/File/src/DescriptorControl.cpp
PR 9566 core/libs/prx/libkernel/File/include/DescriptorControl.hpp
core/libs/prx/libkernel/File/src/DescriptorControl.cpp
PR 11896 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12682 core/libs/prx/libkernel/File/src/DescriptorControl.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12688 core/libs/prx/libkernel/File/src/DescriptorControl.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12700 core/libs/prx/libkernel/File/include/DescriptorControl.hpp
core/libs/prx/libkernel/File/src/DescriptorControl.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12965 core/libs/prx/libkernel/File/include/DescriptorControl.hpp
core/libs/prx/libkernel/File/src/DescriptorControl.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 17113 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 18197 core/libs/prx/libkernel/Socket/src/Udp.cpp

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Helldiverr

Copy link
Copy Markdown
Contributor

On Windows the merge passes the nine ioctl and socket tests 3/3. With main's three source files and the new tests, guest_kernel_fcntl and guest_kernel_ioctl_fionread fail, as the author reported. Four of seven mutations are caught. One survivor is equivalent. The other two are a partial copy-in and the order against the virtual-directory check; two small pins that catch both are suggested below.

Existing runs.

  • The author, brajankw, ran Linux (Debian 13, GCC 14.2), Release, with a negative control.
  • PrivacyOnion approved 22652acce4 after a Linux run (Ubuntu 24.04, GCC 13.3), with a negative control.
  • MarcZX10 approved 22652acce4 after reading the code; no tests were run in that review.
  • I found no Windows run. main has moved since the branch's base 1af270fa29; DescriptorControl.cpp, Udp.cpp and GuestKernelFcntl.cpp merged without conflicts.

What this run adds.

  • A Windows run of the merge, with seven more ioctl users (guest_kernel_ioctl_device_type, guest_kernel_pipe_bounds, guest_kqueue, guest_udp, guest_tcp, guest_sendfile, guest_sce_net).
  • A Windows negative control.
  • Seven mutations.
  • Two proposed pins, each checked on the merge and against its mutation.
  • A check of the order against sys_ioctl in FreeBSD's releng/11.4 (d106002967), sys/kern/sys_generic.c:703-749.

Code. At the head:

  • CopyIoctlInput (core/libs/prx/libkernel/File/src/DescriptorControl.cpp:575-583) copies IOC_IN data of the encoded length and skips IOC_VOID and zero-length requests. In sys_ioctl, an IOC_VOID request with a length takes its argument by value and sets the size to 0 before the copyin (:720-725). A zero-length request copies nothing either (:733). So both match.
  • Ioctl (:585-613) copies right after the malformed-request check (:588), before the virtual-directory, descriptor and kqueue checks. sys_ioctl also copies in (:734-737) before kern_ioctl looks the descriptor up (:746). FIONBIO and FIOASYNC read the copy (:600).
  • The guest socket path (core/libs/prx/libkernel/Socket/src/Udp.cpp:2100) uses the same copy and drops the old null check. FIONREAD still fails with EFAULT when its copy-out fails, as sys_ioctl does after a successful request (:748-749).
  • The path for sockets lent by sceNetSocket keeps the old null check (Udp.cpp:2093), as the body says. There, a null IOC_VOID argument still gets EFAULT before dispatch. This could be a follow-up.

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 AuditInputCopiedBeforeDispatch, after unmapped (core/libs/tests/GuestKernelIoctlFionread.cpp:405):

    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 core/libs/tests/GuestKernelAppArchive.cpp:255, on the archive directory descriptor:

    Check(ioctl_nid_postfix(fd, 0x8004667e, reinterpret_cast<void*>(std::uintptr_t{0x10})) == -1 && *__error_nid_postfix() == 14);

The second one fails only in guest_kernel_app_archive_archive, the mode where the directory is virtual.

Negative control and mutations. Each change was made on the merge, rebuilt, and run with the nine tests. The pins ran with guest_kernel_ioctl_fionread and the three guest_kernel_app_archive modes.

change runs result
none 3 Passed
control: DescriptorControl.cpp, DescriptorControl.hpp and Udp.cpp from main 1 guest_kernel_fcntl and guest_kernel_ioctl_fionread abort, EFAULT (14) ... (errno 9)
m1: copy after the descriptor lookup (DescriptorControl.cpp:592) 1 guest_kernel_ioctl_fionread aborts, EFAULT (14) ... (errno 9)
m2: IOC_VOID requests are copied in too (:579) 1 guest_kernel_ioctl_fionread aborts, EADDRNOTAVAIL (49) ... (errno 14)
m3: only the first byte is copied (:582) 1 Passed (not caught); with pins, guest_kernel_ioctl_fionread aborts at the first pin, (errno 9)
m4: the socket path ignores a failed copy (Udp.cpp:2100) 1 guest_udp aborts; guest_kernel_fcntl and guest_kernel_ioctl_fionread SegFault
m5: copy after the virtual-directory check (DescriptorControl.cpp:589) 1 Passed (not caught); with pins, guest_kernel_app_archive_archive fails at the second pin
m6: IOC_OUT-only requests are copied in too (:579) 1 guest_kernel_ioctl_fionread aborts, EBADF (9) ... (errno 14)
m7: no null check before the copy (:580) 1 Passed (equivalent: the copy from a null pointer fails with the same EFAULT)

Tests. ctest --test-dir build -R "^guest_(kernel_fcntl|kernel_ioctl_fionread|kernel_ioctl_device_type|kernel_pipe_bounds|kqueue|udp|tcp|sendfile|sce_net)$" -V

test main merge 1 merge 2 merge 3
guest_kernel_fcntl Passed 0.12 s Passed 0.07 s Passed 0.06 s Passed 0.05 s
guest_kernel_ioctl_fionread Passed 0.09 s Passed 0.07 s Passed 0.05 s Passed 0.04 s
guest_kernel_ioctl_device_type Passed 0.04 s Passed 0.04 s Passed 0.02 s Passed 0.02 s
guest_kernel_pipe_bounds Passed 0.18 s Passed 0.06 s Passed 0.06 s Passed 0.10 s
guest_kqueue Passed 5.03 s Passed 4.87 s Passed 4.78 s Passed 5.03 s
guest_udp Passed 1.35 s Passed 1.06 s Passed 1.01 s Passed 1.00 s
guest_tcp Passed 0.87 s Passed 0.78 s Passed 0.79 s Passed 0.78 s
guest_sendfile Passed 0.14 s Passed 0.22 s Passed 0.14 s Passed 0.14 s
guest_sce_net Passed 2.45 s Passed 2.26 s Passed 2.17 s Passed 2.13 s

On main the tests are main's own, without the new cases.

On the merge, tools/check_conventions.py --base upstream/main passes with no findings, and tools/debt_fold.py --check exits 0.

Host:

  • CPU: Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release
  • Commits: head 22652acce4, merged onto main 05384fbb17 as 9bf0cd65d7

AI-assisted: yes

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 24, 2026
…reeBSD 11.4's sys_ioctl does

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@brajankw
brajankw force-pushed the fix/libkernel-ioctl-copies-input-first branch from 22652ac to affff6f Compare November 24, 2026 13:45
@brajankw

Copy link
Copy Markdown
Contributor Author

@Helldiverr I added both of your pins at affff6f058, rebased onto main eb4300802c. The rebase conflict was only a test constant next to main's new DirectoryFlag.

  • AuditInputCopiedBeforeDispatch maps a guard page and checks that an 8-byte IOC_IN request whose last 4 bytes are unreadable fails with EFAULT, for both ioctl and _ioctl.
  • GuestKernelAppArchive.cpp checks that FIONBIO with an unreadable argument on the /app0/Data descriptor fails with EFAULT.

#22396 is rebased onto this head.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 24, 2026
@Helldiverr

Copy link
Copy Markdown
Contributor

Rechecked on Windows at the new head affff6f058, merged onto main 7f99fbd60d as d0e1793b56. Both pins from the earlier run are in, and each now catches the mutation it was written for. Line numbers below are at the head and are the same on the merge.

Code.

  • The library changes in the diff are the same as at the earlier head 22652acce4. Only the tests changed.
  • AuditInputCopiedBeforeDispatch (core/libs/tests/GuestKernelIoctlFionread.cpp:425-459):
    • Maps two 16 KiB pages and makes the second one inaccessible (:427-430).
    • Inside the loop over ioctl_nid_postfix and _ioctl_nid_postfix (:431), it checks that ForeignWideRequest (0x80087801, an 8-byte IOC_IN request, :33) at pages + Page - 4 fails with EFAULT (:434).
    • Unmaps the pages at the end (:458).
  • GuestKernelAppArchive.cpp:264 checks that FIONBIO with argument 0x10 fails with EFAULT (14) on the /app0/Data descriptor (opened at :240).

Mutations. These are the two survivors from the earlier run. Each was applied to the merge and run once with the tests below.

change earlier result now
m3: CopyIoctlInput copies only the first byte (DescriptorControl.cpp:586) passed guest_kernel_ioctl_fionread aborts at the guard-page check: "an input argument whose last bytes are unreadable must fail with EFAULT (14) ... (errno 9)"
m5: copy after the virtual-directory check (DescriptorControl.cpp:592-593 swapped) passed guest_kernel_app_archive_archive fails at GuestKernelAppArchive.cpp:264

In each mutation run, the other 13 tests passed.

Runs. ctest --test-dir build -R "^guest_(kernel_fcntl|kernel_ioctl_fionread|kernel_ioctl_device_type|kernel_pipe_bounds|kqueue|udp|tcp|sendfile|sce_net|kernel_app_archive_.*)$" -V. This is the earlier nine tests plus all five guest_kernel_app_archive modes.

test main merge 1 merge 2 merge 3
guest_sce_net Passed 2.52 s Passed 2.55 s Passed 2.28 s Passed 2.60 s
guest_sendfile Passed 0.48 s Passed 0.19 s Passed 0.14 s Passed 0.09 s
guest_udp Passed 1.51 s Passed 1.62 s Passed 1.32 s Passed 1.20 s
guest_tcp Passed 0.79 s Passed 0.78 s Passed 0.75 s Passed 0.75 s
guest_kernel_app_archive_archive Passed 0.26 s Passed 0.09 s Passed 0.25 s Passed 0.06 s
guest_kernel_app_archive_plain Passed 0.09 s Passed 0.26 s Passed 0.04 s Passed 0.07 s
guest_kernel_app_archive_mmap Passed 0.76 s Passed 0.59 s Passed 0.65 s Passed 0.56 s
guest_kernel_app_archive_mmap-failure Passed 0.15 s Passed 0.05 s Passed 0.36 s Passed 0.06 s
guest_kernel_app_archive_mmap-damaged Passed 0.08 s Passed 0.12 s Passed 0.27 s Passed 0.07 s
guest_kqueue Passed 5.06 s Passed 5.31 s Passed 4.77 s Passed 4.78 s
guest_kernel_pipe_bounds Passed 0.09 s Passed 0.05 s Passed 0.04 s Passed 0.04 s
guest_kernel_fcntl Passed 0.22 s Passed 0.07 s Passed 0.05 s Passed 0.23 s
guest_kernel_ioctl_fionread Passed 0.16 s Passed 0.06 s Passed 0.05 s Passed 0.04 s
guest_kernel_ioctl_device_type Passed 0.10 s Passed 0.22 s Passed 0.04 s Passed 0.01 s

On main the tests are main's own. The build had no warnings on either tree. On the merge, tools/check_conventions.py --base 7f99fbd60d passes ("All conventions checks passed.") and tools/debt_fold.py --check exits 0.

Host: Ryzen 9 9950X3D, Windows 11 Pro 26300, WinLibs GCC 15.2, Ninja, Release.

AI-assisted: yes

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 affff6f058ff150676a1a469d6107ec674aae7b5 using repo_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 CopyIoctlInput and retains the old null-only validation before KernelControlSocketIoctl_nid_no_patch (core/libs/prx/libkernel/Socket/src/Udp.cpp:2299-2302). As a result, unreadable non-null IOC_IN arguments can still reach dispatch rather than returning EFAULT first. The PR description says sceNetSocket sockets lent to libkernel are unchanged, but this branch is the separate KernelFindSocketNative forwarding 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.json was 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.

@oneandonlydean
oneandonlydean merged commit afc5784 into boykopovar:main Nov 27, 2026
15 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants