fix(libkernel): fail an ioctl request randomdev_ioctl does not handle with ENOTTY on the random device, as FreeBSD 11.4 does #22396

Merged
brajankw merged 2 commits from upstream-pr/22396 into main 2026-11-27 17:22:09 +00:00
brajankw commented 2026-11-17 14:05:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by brajankw on 2026-11-17T14:05:28Z.


What

Builds on #22395, which this branch contains; review its last commit.

An ioctl on /dev/random or /dev/urandom threw for any request without a case for every descriptor (other than FIONREAD, which already fails with ENOTTY). In FreeBSD 11.4, devfs_ioctl_f (sys/fs/devfs/devfs_vnops.c:763-779) answers only FIODTYPE and FIODGNAME itself and hands the rest to randomdev_ioctl (sys/dev/random/randomdev.c:301-316). That function accepts FIOASYNC and FIONBIO and refuses everything else with ENOTTY, the owner requests included. Those requests now fail with ENOTTY; the input argument is copied in first (#22395).

  • FIODTYPE and FIODGNAME still throw, as they report the device's flags and name, which are not modelled for the console.
  • GuestKernelIoctlFionread.cpp: on both devices, through ioctl and _ioctl, the following fail with ENOTTY and leave the argument unchanged:
    • FIONWRITE, FIONSPACE, _IOW('x', 1, int), FIOSETOWN and FIOGETOWN;
    • the IOC_IN|IOC_VOID request 0xa0047801 with a null argument.
  • Debt fragment: a new ioctl on the random device entry, with the two throwing requests first. The pending fragment of #22296 makes the ioctl, _ioctl entry point to ioctl on a pipe. Here it points to "the ioctl on entries" instead, and "firmware" is dropped to keep it under 1000 characters (990).

Tested

Linux (Debian 13, GCC 14.2.0, Release). Now rebased onto #22395's head affff6f058 (on main eb4300802c); the only conflict was main's new FIONREAD sentence in the ioctl, _ioctl entry of fix-libkernel-ioctl-pipe-unknown-request-enotty.md, which this branch points at the ioctl on entries. check_conventions warns that this entry, already 1,216 characters on main, is over 1,000; this branch changes five words of it. The other runs below used this branch on main 9f94e4a2ce with #22395.

Rebased onto main db68035f8b. There, eceaaa2a07 adds the same DeviceTypeRequest constant and answers FIODTYPE on other character devices; on the random device it still throws, as its debt entry says. The conflict was only that constant, so this branch now adds DeviceNameRequest alone. After the rebase, ctest for guest_kernel_ioctl_fionread, guest_kernel_fcntl*, guest_kernel_pipe_bounds, guest_kernel_app_archive*, guest_kqueue*, guest_sce_net*, guest_sendfile, guest_udp* and guest_tcp: 22 passed. The libkernel debt fragments fold with tools/debt_fold.py --check.

  • Update at e67e86641e, 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.*)$': 3 passed.
  • Negative control: without the change, the new check aborts with ioctl: request 0x40046677 on a random device is not implemented.
  • 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 the same PRs as #22395. None changes ioctl on the random device.
  • 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: #22395
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5358338467 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22396). Originally posted by [brajankw](https://github.com/brajankw) on 2026-11-17T14:05:28Z. --- ### What Builds on #22395, which this branch contains; review its last commit. An `ioctl` on `/dev/random` or `/dev/urandom` threw for any request without a case for every descriptor (other than `FIONREAD`, which already fails with `ENOTTY`). In FreeBSD 11.4, `devfs_ioctl_f` (`sys/fs/devfs/devfs_vnops.c:763-779`) answers only `FIODTYPE` and `FIODGNAME` itself and hands the rest to `randomdev_ioctl` (`sys/dev/random/randomdev.c:301-316`). That function accepts `FIOASYNC` and `FIONBIO` and refuses everything else with `ENOTTY`, the owner requests included. Those requests now fail with `ENOTTY`; the input argument is copied in first (#22395). - `FIODTYPE` and `FIODGNAME` still throw, as they report the device's flags and name, which are not modelled for the console. - `GuestKernelIoctlFionread.cpp`: on both devices, through `ioctl` and `_ioctl`, the following fail with `ENOTTY` and leave the argument unchanged: - `FIONWRITE`, `FIONSPACE`, `_IOW('x', 1, int)`, `FIOSETOWN` and `FIOGETOWN`; - the `IOC_IN|IOC_VOID` request `0xa0047801` with a null argument. - Debt fragment: a new `ioctl on the random device` entry, with the two throwing requests first. The pending fragment of #22296 makes the `ioctl, _ioctl` entry point to `ioctl on a pipe`. Here it points to "the `ioctl on` entries" instead, and "firmware" is dropped to keep it under 1000 characters (990). ### 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). Now rebased onto #22395's head `affff6f058` (on `main` `eb4300802c`); the only conflict was `main`'s new `FIONREAD` sentence in the `ioctl, _ioctl` entry of `fix-libkernel-ioctl-pipe-unknown-request-enotty.md`, which this branch points at the `ioctl on` entries. `check_conventions` warns that this entry, already 1,216 characters on `main`, is over 1,000; this branch changes five words of it. The other runs below used this branch on `main` `9f94e4a2ce` with #22395. Rebased onto `main` `db68035f8b`. There, eceaaa2a07 adds the same `DeviceTypeRequest` constant and answers `FIODTYPE` on other character devices; on the random device it still throws, as its debt entry says. The conflict was only that constant, so this branch now adds `DeviceNameRequest` alone. After the rebase, `ctest` for `guest_kernel_ioctl_fionread`, `guest_kernel_fcntl*`, `guest_kernel_pipe_bounds`, `guest_kernel_app_archive*`, `guest_kqueue*`, `guest_sce_net*`, `guest_sendfile`, `guest_udp*` and `guest_tcp`: 22 passed. The libkernel debt fragments fold with `tools/debt_fold.py --check`. - Update at `e67e86641e`, 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.*)$'`: 3 passed. - Negative control: without the change, the new check aborts with `ioctl: request 0x40046677 on a random device is not implemented`. - 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 the same PRs as #22395. None changes `ioctl` on the random device. - 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: #22395 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-17 14:08:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T14:08:09Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 770cf06642:

  • 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 #5566: guest_kernel_fcntl ...............   Passed    0.05 sec
    2/2 Test #5570: guest_kernel_ioctl_fionread ......   Passed    0.06 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.35 sec
    2/2 Test #5570: guest_kernel_ioctl_fionread ......Subprocess aborted***Exception:   0.42 sec
    0% tests passed, 2 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified CopyIoctlInput in DescriptorControl.cpp:577-585 correctly extracts length from (request >> 16) & 0x1fff, handles RequestIn / RequestVoid / length == 0, and safely copies input payload via GuestCopyIn_nid_no_patch before descriptor dispatch.
    • Verified Ioctl in DescriptorControl.cpp:588-616 fails with EFAULT when CopyIoctlInput fails, replaces previous duplicate copy logic for pipe and random device, and uses std::memcpy safely with Nonblocking/Asynchronous commands.
    • Verified RandomDevice handling in DescriptorControl.cpp:616: when *kind == Kind::RandomDevice and request is not DeviceTypeRequest (0x4004667a) or DeviceNameRequest (0x80106678), it returns Fail(GuestEnotty) (errno 25), exactly matching FreeBSD 11.4 randomdev_ioctl semantics.
    • Verified ioctl_nid_postfix in Udp.cpp:2099-2100 correctly integrates File::CopyIoctlInput, removes redundant null-check logic, and retains proper error codes (14 / 25 / EADDRNOTAVAIL).
    • Verified unit tests in GuestKernelFcntl.cpp and GuestKernelIoctlFionread.cpp comprehensively exercise input copying before dispatch across multiple descriptor types, verifying ENOTTY on random devices for unhandled requests while leaving arguments unchanged.
    • Verified technical debt documentation in docs/dev/debt/changes/ adheres to format requirements and character length constraints.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5939932638 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22396#pullrequestreview-5939932638). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-17T14:08:09Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `770cf06642`: - **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 #5566: guest_kernel_fcntl ............... Passed 0.05 sec 2/2 Test #5570: guest_kernel_ioctl_fionread ...... Passed 0.06 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.35 sec 2/2 Test #5570: guest_kernel_ioctl_fionread ......Subprocess aborted***Exception: 0.42 sec 0% tests passed, 2 tests failed out of 2 ``` Restoring the change makes it pass again. - **Code reading**: - Verified CopyIoctlInput in DescriptorControl.cpp:577-585 correctly extracts length from (request >> 16) & 0x1fff, handles RequestIn / RequestVoid / length == 0, and safely copies input payload via GuestCopyIn_nid_no_patch before descriptor dispatch. - Verified Ioctl in DescriptorControl.cpp:588-616 fails with EFAULT when CopyIoctlInput fails, replaces previous duplicate copy logic for pipe and random device, and uses std::memcpy safely with Nonblocking/Asynchronous commands. - Verified RandomDevice handling in DescriptorControl.cpp:616: when *kind == Kind::RandomDevice and request is not DeviceTypeRequest (0x4004667a) or DeviceNameRequest (0x80106678), it returns Fail(GuestEnotty) (errno 25), exactly matching FreeBSD 11.4 randomdev_ioctl semantics. - Verified ioctl_nid_postfix in Udp.cpp:2099-2100 correctly integrates File::CopyIoctlInput, removes redundant null-check logic, and retains proper error codes (14 / 25 / EADDRNOTAVAIL). - Verified unit tests in GuestKernelFcntl.cpp and GuestKernelIoctlFionread.cpp comprehensively exercise input copying before dispatch across multiple descriptor types, verifying ENOTTY on random devices for unhandled requests while leaving arguments unchanged. - Verified technical debt documentation in docs/dev/debt/changes/ adheres to format requirements and character length constraints. Looks Good To Me
MarcZX10 commented 2026-11-23 03:48:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T03:48:30Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the reviewed implementation and focused tests support the stated random-device ENOTTY behavior.

What I ran

Reviewed head e3005a5c5a3b29ea5a3384b6e876852b10bf9d71 using the prepared diff and repo_read.py. No commands or tests were run.

Results

Check Result Note
Diff and targeted source review passed Changed behavior and tests inspected
Test execution not run Test results in the description are author reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

Did not run the reported test suite or inspect the full contents of every changed file. Read the prepared description, commits, manifest, diff, DescriptorControl.cpp lines 90–125 and 550–630, Udp.cpp lines 2080–2145, and GuestKernelIoctlFionread.cpp lines 1–48 and 390–465. All seven changed files are marked included in the manifest; the other changed files were not read in full. Windows, macOS, and console behavior were not verified.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6016343633 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22396#pullrequestreview-6016343633). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T03:48:30Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the reviewed implementation and focused tests support the stated random-device `ENOTTY` behavior. ## What I ran Reviewed head `e3005a5c5a3b29ea5a3384b6e876852b10bf9d71` using the prepared diff and `repo_read.py`. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted source review | passed | Changed behavior and tests inspected | | Test execution | not run | Test results in the description are author reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Did not run the reported test suite or inspect the full contents of every changed file. Read the prepared description, commits, manifest, diff, `DescriptorControl.cpp` lines 90–125 and 550–630, `Udp.cpp` lines 2080–2145, and `GuestKernelIoctlFionread.cpp` lines 1–48 and 390–465. All seven changed files are marked included in the manifest; the other changed files were not read in full. Windows, macOS, and console behavior were not verified. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-25 12:19:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T12:19:26Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the implementation and added tests support the stated random-device ioctl behavior.

What I ran

Reviewed head e67e86641e19756e44edabe3e17b33f7d494eb38 using the prepared diff and repo_read.py for changed-file context. No commands or tests were run by me; test results in the description are author-reported.

Results

Check Result Note
Changed-line review passed All eight changed files read
Tests run by reviewer not run Author-reported test results not independently verified

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author-reported test runs and behavior on Windows, macOS, and physical console hardware were not independently verified. The prepared bundle lists no excluded changed files.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6057476803 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22396#pullrequestreview-6057476803). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-25T12:19:26Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the implementation and added tests support the stated random-device ioctl behavior. ## What I ran Reviewed head `e67e86641e19756e44edabe3e17b33f7d494eb38` using the prepared diff and `repo_read.py` for changed-file context. No commands or tests were run by me; test results in the description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Changed-line review | passed | All eight changed files read | | Tests run by reviewer | not run | Author-reported test results not independently verified | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The author-reported test runs and behavior on Windows, macOS, and physical console hardware were not independently verified. The prepared bundle lists no excluded changed files. ## To get this to a good state Nothing is required.
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!22396
No description provided.