fix(libkernel): fail ioctl on a ksem id with ENOTTY, as FreeBSD 11.4 does #24489

Merged
brajankw merged 3 commits from upstream-pr/24489 into main 2026-12-04 14:29:26 +00:00
brajankw commented 2026-11-20 17:29:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by brajankw on 2026-11-20T17:29:01Z.


What

ioctl on a ksem id threw for FIONREAD, FIODTYPE and unknown requests, as the id is a host null device descriptor here, and FIONBIO and FIOASYNC changed its status flags and returned 0. FreeBSD 11.4's ksem_ops answers every request through invfo_ioctl, which fails with ENOTTY, after kern_ioctl has handled FIOCLEX and FIONCLEX itself.

  • A ksem id now answers like a kqueue, which the tree already handles this way.
  • FIONBIO and FIOASYNC leave the status flags alone, where kern_ioctl sets them before invfo_ioctl refuses; the kqueue path does the same here. The debt entry says so.
  • IsKsemDescriptor, which this uses, is replaced by DuplicateKsemDescriptor in open #22191. The two merge without a textual conflict, but whichever lands second needs a rebase that keeps a ksem predicate, or the build breaks. I will rebase this as soon as #22191 lands.
  • Merges without overlap with my open #22395 and #22396.
  • An IOC_IN argument is still copied in first, as sys_ioctl does, so an unreadable or null one gives EFAULT.
  • The ksem exception on the FIODTYPE line, which the new block makes unreachable, is removed.
  • Tests: Controls in GuestKernelSysKsem.cpp, through both ioctl exports, the EFAULT cases, and FIOCLEX/FIONCLEX checked with F_GETFD.
  • Debt: the pending fragment fix-libkernel-ioctl-fiodtype-character-device.md said a ksem id still throws; it is edited in place.

Tested

Linux (Debian 13, GCC 14.2.0, Release). Head 403f21f941, the single commit on main c8f72e905c.

  • Negative control: with only the test change, on main 84e16538d4, guest_kernel_sys_ksem aborts with ioctl: FIONREAD on a character device is not implemented.
  • On 403f21f941, ctest over guest_kernel_sys_ksem, guest_kqueue, guest_kernel_sys, guest_filesystem, guest_file_vector, guest_kernel_sys_aio, guest_equeue_events, guest_kernel_fcntl, guest_kernel_ioctl_fionread and guest_kernel_ioctl_device_type: all 10 pass.
  • Full ctest (6393 tests, -j 12) on this branch before review, which then added the IOC_IN copy-in and the EFAULT checks, and my other eleven branches of this round, merged onto main 84e16538d4: 6378 passed. The 15 failures also fail on main in this 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;
    • five agc_driver GPU cases fail the same way on main: prepared_shaders with its indirect, uncached and debug forms, and storage_file_write;
    • agc_driver_image_table_packed_guards is unstable on main as well (2 of 10 runs passed on its own on main 3f4693be43).
  • python3 tools/check_conventions.py --base origin/main: passed.
  • python3 tools/pr_overlap.py --branch fix/libkernel-ksem-ioctl-enotty: could not compare #7121, #9293, #9297, #9302, #9557, #9566, #12682, #12688, #12700, #12965 and #18457, which conflict with main; none of them touches the ksem or kqueue lines of Ioctl. git merge-tree with my #22395 and #22396 is clean.
  • 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

Summary by CodeRabbit

  • Bug Fixes

    • Unsupported control requests on kernel semaphores now return the standard ENOTTY error.
    • Device-type requests now handle device descriptors consistently, including kernel semaphores.
    • Nonblocking and asynchronous control requests leave kernel semaphore flags unset; invalid arguments continue to report EFAULT, and supported requests preserve the semaphore’s value.
  • Tests

    • Added coverage for semaphore control requests, error handling, and supported operations.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5417570020 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24489). Originally posted by [brajankw](https://github.com/brajankw) on 2026-11-20T17:29:01Z. --- ### What `ioctl` on a ksem id threw for `FIONREAD`, `FIODTYPE` and unknown requests, as the id is a host null device descriptor here, and `FIONBIO` and `FIOASYNC` changed its status flags and returned 0. FreeBSD 11.4's `ksem_ops` answers every request through [`invfo_ioctl`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_descrip.c#L4045-L4050), which fails with `ENOTTY`, after [`kern_ioctl`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c#L758-L838) has handled `FIOCLEX` and `FIONCLEX` itself. - A ksem id now answers like a kqueue, which the tree already handles this way. - `FIONBIO` and `FIOASYNC` leave the status flags alone, where `kern_ioctl` sets them before `invfo_ioctl` refuses; the kqueue path does the same here. The debt entry says so. - `IsKsemDescriptor`, which this uses, is replaced by `DuplicateKsemDescriptor` in open #22191. The two merge without a textual conflict, but whichever lands second needs a rebase that keeps a ksem predicate, or the build breaks. I will rebase this as soon as #22191 lands. - Merges without overlap with my open #22395 and #22396. - An `IOC_IN` argument is still copied in first, as `sys_ioctl` does, so an unreadable or null one gives `EFAULT`. - The ksem exception on the `FIODTYPE` line, which the new block makes unreachable, is removed. - Tests: `Controls` in `GuestKernelSysKsem.cpp`, through both `ioctl` exports, the `EFAULT` cases, and `FIOCLEX`/`FIONCLEX` checked with `F_GETFD`. - Debt: the pending fragment `fix-libkernel-ioctl-fiodtype-character-device.md` said a ksem id still throws; it is edited in place. ### 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). Head `403f21f941`, the single commit on `main` `c8f72e905c`. - Negative control: with only the test change, on `main` `84e16538d4`, `guest_kernel_sys_ksem` aborts with `ioctl: FIONREAD on a character device is not implemented`. - On `403f21f941`, `ctest` over `guest_kernel_sys_ksem`, `guest_kqueue`, `guest_kernel_sys`, `guest_filesystem`, `guest_file_vector`, `guest_kernel_sys_aio`, `guest_equeue_events`, `guest_kernel_fcntl`, `guest_kernel_ioctl_fionread` and `guest_kernel_ioctl_device_type`: all 10 pass. - Full `ctest` (6393 tests, `-j 12`) on this branch before review, which then added the `IOC_IN` copy-in and the `EFAULT` checks, and my other eleven branches of this round, merged onto `main` `84e16538d4`: 6378 passed. The 15 failures also fail on `main` in this 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`; - five `agc_driver` GPU cases fail the same way on `main`: `prepared_shaders` with its `indirect`, `uncached` and `debug` forms, and `storage_file_write`; - `agc_driver_image_table_packed_guards` is unstable on `main` as well (2 of 10 runs passed on its own on `main` `3f4693be43`). - `python3 tools/check_conventions.py --base origin/main`: passed. - `python3 tools/pr_overlap.py --branch fix/libkernel-ksem-ioctl-enotty`: could not compare #7121, #9293, #9297, #9302, #9557, #9566, #12682, #12688, #12700, #12965 and #18457, which conflict with `main`; none of them touches the ksem or kqueue lines of `Ioctl`. `git merge-tree` with my #22395 and #22396 is clean. - 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 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit - **Bug Fixes** - Unsupported control requests on kernel semaphores now return the standard `ENOTTY` error. - Device-type requests now handle device descriptors consistently, including kernel semaphores. - Nonblocking and asynchronous control requests leave kernel semaphore flags unset; invalid arguments continue to report `EFAULT`, and supported requests preserve the semaphore’s value. - **Tests** - Added coverage for semaphore control requests, error handling, and supported operations. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
PrivacyOnion commented 2026-11-20 17:51:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-20T17:51:22Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 403f21f941:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_kernel_sys_ksem_tests built cleanly.
  • CTest (guest_kernel_sys_ksem):
    1/1 Test #1036: guest_kernel_sys_ksem ............   Passed    5.32 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/File/src/DescriptorControl.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1036: guest_kernel_sys_ksem ............Subprocess aborted***Exception:   7.32 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified DescriptorControl.cpp:584-589 handles FIOCLEX and FIONCLEX prior to the IsKsemDescriptor check, matching FreeBSD kern_ioctl behavior
    • Verified DescriptorControl.cpp:590-597 checks RequestIn and copied size with GuestCopyIn_nid_no_patch to guarantee EFAULT on invalid guest pointer before returning ENOTTY
    • Verified removal of '!IsKsemDescriptor(fd)' in DeviceTypeRequest check at DescriptorControl.cpp:612, which is now unreachable due to early ENOTTY return
    • Verified docs/dev/debt/changes/fix-libkernel-ioctl-fiodtype-character-device.md accurately documents the ENOTTY and status flag behavior for ksem descriptors
    • Verified GuestKernelSysKsem.cpp Controls test exercises ioctl and _ioctl with various command bitmasks, bad pointer / unreadable page EFAULT handling, and close-on-exec behavior

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5997083765 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24489#pullrequestreview-5997083765). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-20T17:51:22Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `403f21f941`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_kernel_sys_ksem_tests` built cleanly. - **CTest** (`guest_kernel_sys_ksem`): ``` 1/1 Test #1036: guest_kernel_sys_ksem ............ Passed 5.32 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/File/src/DescriptorControl.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #1036: guest_kernel_sys_ksem ............Subprocess aborted***Exception: 7.32 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified DescriptorControl.cpp:584-589 handles FIOCLEX and FIONCLEX prior to the IsKsemDescriptor check, matching FreeBSD kern_ioctl behavior - Verified DescriptorControl.cpp:590-597 checks RequestIn and copied size with GuestCopyIn_nid_no_patch to guarantee EFAULT on invalid guest pointer before returning ENOTTY - Verified removal of '!IsKsemDescriptor(fd)' in DeviceTypeRequest check at DescriptorControl.cpp:612, which is now unreachable due to early ENOTTY return - Verified docs/dev/debt/changes/fix-libkernel-ioctl-fiodtype-character-device.md accurately documents the ENOTTY and status flag behavior for ksem descriptors - Verified GuestKernelSysKsem.cpp Controls test exercises ioctl and _ioctl with various command bitmasks, bad pointer / unreadable page EFAULT handling, and close-on-exec behavior Looks Good To Me
MarcZX10 commented 2026-11-21 05:43:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T05:43:51Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the code read supports the requested ksem ioctl behavior, and the author reports passing targeted tests.

What I ran

Read the prepared diff and changed source files at head 403f21f941ea73658c27a96c1c8b73e70fcfe949 using repo_read.py. I did not run commands or tests.

Results

Check Result Note
Diff and changed-file review passed All three changed files read
Tests in this review not run Test results are author-reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests or builds were run during this review. The description reports 10 targeted tests and a conventions check passing, but these results were not independently verified. Read: core/libs/prx/libkernel/File/src/DescriptorControl.cpp, core/libs/tests/GuestKernelSysKsem.cpp, and docs/dev/debt/changes/fix-libkernel-ioctl-fiodtype-character-device.md. Excluded files: none listed in the supplied manifest; files.json was unavailable through the reader.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6003244357 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24489#pullrequestreview-6003244357). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T05:43:51Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the code read supports the requested ksem ioctl behavior, and the author reports passing targeted tests. ## What I ran Read the prepared diff and changed source files at head `403f21f941ea73658c27a96c1c8b73e70fcfe949` using `repo_read.py`. I did not run commands or tests. ## Results | Check | Result | Note | |---|---|---| | Diff and changed-file review | passed | All three changed files read | | Tests in this review | not run | Test results are author-reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No tests or builds were run during this review. The description reports 10 targeted tests and a conventions check passing, but these results were not independently verified. Read: `core/libs/prx/libkernel/File/src/DescriptorControl.cpp`, `core/libs/tests/GuestKernelSysKsem.cpp`, and `docs/dev/debt/changes/fix-libkernel-ioctl-fiodtype-character-device.md`. Excluded files: none listed in the supplied manifest; `files.json` was unavailable through the reader. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-28 13:10:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T13:10:01Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the implementation and tests support the stated ksem ioctl behavior.

What I ran

Reviewed head e1e2c04182cfedebab54d9c2af55f12bd904ceb1 using the prepared diff and the pinned repository reader. No commands or tests were run; repo_read.py was not invoked.

Results

Check Result Note
Diff and changed files passed All three changed files read
Targeted callers and helpers passed Ksem predicate and ioctl dispatch read
Tests not run Test results in the PR description are author-reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests were run in this review. Build and runtime behavior on Windows and macOS were not verified. No files were excluded from the prepared diff; all changed files listed in manifest.json were read. The description's test claims were not independently verified.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6097953209 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24489#pullrequestreview-6097953209). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-28T13:10:01Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the implementation and tests support the stated ksem `ioctl` behavior. ## What I ran Reviewed head `e1e2c04182cfedebab54d9c2af55f12bd904ceb1` using the prepared diff and the pinned repository reader. No commands or tests were run; `repo_read.py` was not invoked. ## Results | Check | Result | Note | |---|---|---| | Diff and changed files | passed | All three changed files read | | Targeted callers and helpers | passed | Ksem predicate and ioctl dispatch read | | Tests | not run | Test results in the PR description are author-reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No tests were run in this review. Build and runtime behavior on Windows and macOS were not verified. No files were excluded from the prepared diff; all changed files listed in `manifest.json` were read. The description's test claims were not independently verified. ## To get this to a good state Nothing is required.
Plyxen commented 2026-12-03 14:08:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-12-03T14:08:50Z.

Review: APPROVED


I read the diff at e1e2c04182. Windows 11 with GCC 15.2 and Linux (Ubuntu 24.04 in WSL2, GCC 13.3), merged into main at 6c6b29320fe together with other open PRs (so not an isolated build).

  • guest_kernel_sys_ksem: pass on both.
  • Control (the same tree with only this PR's source change taken out): on Windows it stops with "ioctl: FIONREAD on a character device is not implemented, fd=3". On Linux, with the changed sources of the tested CPU PRs put back to main's, the same test fails too.

Not checked: a console.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6171387846 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24489#pullrequestreview-6171387846). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-12-03T14:08:50Z. **Review:** APPROVED --- I read the diff at `e1e2c04182`. Windows 11 with GCC 15.2 and Linux (Ubuntu 24.04 in WSL2, GCC 13.3), merged into `main` at `6c6b29320fe` together with other open PRs (so not an isolated build). - `guest_kernel_sys_ksem`: pass on both. - Control (the same tree with only this PR's source change taken out): on Windows it stops with "ioctl: FIONREAD on a character device is not implemented, fd=3". On Linux, with the changed sources of the tested CPU PRs put back to `main`'s, the same test fails too. Not checked: a console. AI-assisted: yes (Claude Code)
ligmaSec commented 2026-12-03 14:25:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T14:25:20Z.

Review: COMMENTED


Built and ran guest_kernel_sys_ksem on Linux with this head integrated. The suite passed. It covers ENOTTY for unsupported semaphore controls, the pointer-error cases, and the descriptor flags that remain usable on a semaphore descriptor.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6171676853 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24489#pullrequestreview-6171676853). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T14:25:20Z. **Review:** COMMENTED --- Built and ran `guest_kernel_sys_ksem` on Linux with this head integrated. The suite passed. It covers `ENOTTY` for unsupported semaphore controls, the pointer-error cases, and the descriptor flags that remain usable on a semaphore descriptor. AI-assisted: yes.
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!24489
No description provided.