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

Merged
brajankw merged 1 commit from upstream-pr/22395 into main 2026-11-27 16:11:28 +00:00
brajankw commented 2026-11-17 14:05:19 +00:00 (Migrated from github.com)

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 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: #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.

  • Update at affff6f058, merged with my eight other updated branches onto main bd9a6c00a6: ctest over 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_* and guest_np_trophy_*): all pass.
  • ctest -R '^(guest_kernel_ioctl.*|guest_kernel_fcntl.*|guest_socket.*|guest_kqueue|guest_tcp|guest_udp)$': 23 passed.
  • Negative control: on main, the new check aborts at the first call, which fails with EBADF (9) instead of EFAULT.
  • Full ctest (6006 tests, -j 12) on this branch and my other four branches of this round, merged onto main 1af270fa29: 5988 passed. All 18 failures also fail on main in the same environment:
    • nine need a .git directory, 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_getlogin and guest_getlogin_no_session;
    • six are agc_driver cases (agc_driver_image_table_packed_guards passes when run on its own);
    • patched_self_imports, guest_libressl3_ec_parameters and guest_libressl3_asn1 fail on main 1af270fa29 when run on their own.
  • python3 tools/check_conventions.py --base origin/main: passed.
  • python3 tools/pr_overlap.py --branch failed closed on #7121, #9557, #9566, #11896, #12682, #12688, #12700, #12965, #18457 and #19824, which conflict with main. None of their diffs changes when an ioctl argument is copied.
  • #21929 (test only, open) expects EFAULT for FIONREAD with a null argument on a closed socket. FreeBSD and main give EBADF there, and so does this PR.
  • Not tested on Windows or macOS. No title.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5358335704 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22395). Originally posted by [brajankw](https://github.com/brajankw) on 2026-11-17T14:05:19Z. --- ### 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`: #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 <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> 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`. - Update at `affff6f058`, merged with my eight other updated branches onto `main` `bd9a6c00a6`: `ctest` over 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_*` and `guest_np_trophy_*`): all pass. - `ctest -R '^(guest_kernel_ioctl.*|guest_kernel_fcntl.*|guest_socket.*|guest_kqueue|guest_tcp|guest_udp)$'`: 23 passed. - Negative control: on `main`, the new check aborts at the first call, which fails with `EBADF` (9) instead of `EFAULT`. - Full `ctest` (6006 tests, `-j 12`) on this branch and my other four branches of this round, merged onto `main` `1af270fa29`: 5988 passed. All 18 failures also fail on `main` in the same environment: - nine need a `.git` directory, 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_getlogin` and `guest_getlogin_no_session`; - six are `agc_driver` cases (`agc_driver_image_table_packed_guards` passes when run on its own); - `patched_self_imports`, `guest_libressl3_ec_parameters` and `guest_libressl3_asn1` fail on `main` `1af270fa29` when run on their own. - `python3 tools/check_conventions.py --base origin/main`: passed. - `python3 tools/pr_overlap.py --branch` failed closed on #7121, #9557, #9566, #11896, #12682, #12688, #12700, #12965, #18457 and #19824, which conflict with `main`. None of their diffs changes when an `ioctl` argument is copied. - #21929 (test only, open) expects `EFAULT` for `FIONREAD` with a null argument on a closed socket. FreeBSD and `main` give `EBADF` there, and so does this PR. - Not tested on Windows or macOS. No title. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-17 14:06:38 +00:00 (Migrated from github.com)

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:

  • 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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5939903290 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22395#pullrequestreview-5939903290). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-17T14:06:38Z. **Review:** APPROVED --- 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
MarcZX10 commented 2026-11-23 03:54:33 +00:00 (Migrated from github.com)

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 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6016375408 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22395#pullrequestreview-6016375408). Originally posted by [MarcZX10](https://github.com/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 `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.
MarcZX10 commented 2026-11-25 12:20:12 +00:00 (Migrated from github.com)

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

  • 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6057487164 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22395#pullrequestreview-6057487164). Originally posted by [MarcZX10](https://github.com/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 - 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!22395
No description provided.