fix(libkernel): answer fstat and fchmod on a ksem id as FreeBSD 11.4's ksem_stat and ksem_chmod do #24491

Merged
brajankw merged 1 commit from upstream-pr/24491 into main 2026-11-22 01:18:22 +00:00
brajankw commented 2026-11-20 17:29:10 +00:00 (Migrated from github.com)

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


What

A ksem id is a host null device descriptor here, so fstat reported the host's /dev/null, and fchmod called the host's fchmod on it, which as root changes the mode of the host's /dev/null. FreeBSD 11.4 answers both from the semaphore:

  • ksem_create gives ksem_init's semaphore S_IRWXU | S_IRWXG under the umask, owned by the caller (ksem_alloc); the owner is getuid/getgid, both 0 here.
  • ksem_stat reports a zeroed stat with S_IFREG, that mode, the owner and the four times.
  • ksem_chmod keeps the mode's ACCESSPERMS bits and changes no time.
  • The times are taken to the microsecond, as vfs_timestamp does by default: all four at creation, the change time on ksem_post (L733), and the access time on every wait, before the count is checked, so a failing ksem_trywait too (L820), and on ksem_getvalue, also when its copy-out then fails (L888).
  • The equeue read and write events and AIO decided from fstat whether a descriptor is a regular file, which a ksem id would now pass. They ask a new exported IsKsemDescriptor_nid_no_patch instead, as AIO is in libkernel_sys: sceKernelAddReadEvent and sceKernelAddWriteEvent keep their throw on a ksem id, aio_read keeps EOPNOTSUPP (aio_queue_file), and aio_fsync now gets EINVAL (aio_aqueue) where it got EOPNOTSUPP.
  • IsKsemDescriptor_nid_no_patch has its own body and does not call IsKsemDescriptor, which open #22191 replaces, so this merges with #22191 and with my other ksem PRs without overlap, in any order.
  • Open #9293 adds ksem_open with (void)mode; on top of this, it would need to keep its mode as ksem_init now does.
  • Tests: Metadata in GuestKernelSysKsem.cpp, through _fstat and sceKernelFstat, fchmod and sceKernelFchmod, the times after post, waits and ksem_getvalue, and the two equeue calls; the ksem cases in GuestKernelSysAio.cpp. The fstat checks come first, so on main the test fails before it calls fchmod.
  • Debt: no fragment.

Tested

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

  • Negative control: with only the test change, on main 84e16538d4, guest_kernel_sys_ksem fails at the new st_mode check, before any fchmod.
  • On f9c7ef4e59, 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 equeue and AIO exclusions and the ksem_getvalue access time, 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-fstat-fchmod: could not compare #6936, #7121, #7447, #9293, #9297, #9302, #12682, #12688, #12700, #12965 and #15037, which conflict with main. #9293, #9297 and #9302 carry older copies of the ksem files; #9293's ksem_open is noted above. git merge-tree with #22191, #10645 and my other ksem branches 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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5417572122 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24491). Originally posted by [brajankw](https://github.com/brajankw) on 2026-11-20T17:29:10Z. --- ### What A ksem id is a host null device descriptor here, so `fstat` reported the host's `/dev/null`, and `fchmod` called the host's `fchmod` on it, which as root changes the mode of the host's `/dev/null`. FreeBSD 11.4 answers both from the semaphore: - [`ksem_create`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_sem.c#L458-L476) gives `ksem_init`'s semaphore `S_IRWXU | S_IRWXG` under the umask, owned by the caller ([`ksem_alloc`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_sem.c#L296-L314)); the owner is `getuid`/`getgid`, both 0 here. - [`ksem_stat`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_sem.c#L157-L190) reports a zeroed stat with `S_IFREG`, that mode, the owner and the four times. - [`ksem_chmod`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_sem.c#L193-L215) keeps the mode's `ACCESSPERMS` bits and changes no time. - The times are taken to the microsecond, as `vfs_timestamp` does by default: all four at creation, the change time on `ksem_post` ([L733](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_sem.c#L733)), and the access time on every wait, before the count is checked, so a failing `ksem_trywait` too ([L820](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_sem.c#L820)), and on `ksem_getvalue`, also when its copy-out then fails ([L888](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_sem.c#L888)). - The equeue read and write events and AIO decided from `fstat` whether a descriptor is a regular file, which a ksem id would now pass. They ask a new exported `IsKsemDescriptor_nid_no_patch` instead, as AIO is in libkernel_sys: `sceKernelAddReadEvent` and `sceKernelAddWriteEvent` keep their throw on a ksem id, `aio_read` keeps `EOPNOTSUPP` ([`aio_queue_file`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_aio.c#L1719-L1722)), and `aio_fsync` now gets `EINVAL` ([`aio_aqueue`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_aio.c#L1558-L1561)) where it got `EOPNOTSUPP`. - `IsKsemDescriptor_nid_no_patch` has its own body and does not call `IsKsemDescriptor`, which open #22191 replaces, so this merges with #22191 and with my other ksem PRs without overlap, in any order. - Open #9293 adds `ksem_open` with `(void)mode`; on top of this, it would need to keep its mode as `ksem_init` now does. - Tests: `Metadata` in `GuestKernelSysKsem.cpp`, through `_fstat` and `sceKernelFstat`, `fchmod` and `sceKernelFchmod`, the times after post, waits and `ksem_getvalue`, and the two equeue calls; the ksem cases in `GuestKernelSysAio.cpp`. The `fstat` checks come first, so on `main` the test fails before it calls `fchmod`. - Debt: no fragment. ### 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 `f9c7ef4e59`, the single commit on `main` `c8f72e905c`. - Negative control: with only the test change, on `main` `84e16538d4`, `guest_kernel_sys_ksem` fails at the new `st_mode` check, before any `fchmod`. - On `f9c7ef4e59`, `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 equeue and AIO exclusions and the `ksem_getvalue` access time, 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-fstat-fchmod`: could not compare #6936, #7121, #7447, #9293, #9297, #9302, #12682, #12688, #12700, #12965 and #15037, which conflict with `main`. #9293, #9297 and #9302 carry older copies of the ksem files; #9293's `ksem_open` is noted above. `git merge-tree` with #22191, #10645 and my other ksem branches 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
PrivacyOnion commented 2026-11-20 17:51:34 +00:00 (Migrated from github.com)

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

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_kernel_sys_aio_tests, guest_kernel_sys_ksem_tests built cleanly.
  • CTest (guest_kernel_sys_ksem, guest_kernel_sys_aio):
    1/2 Test #1060: guest_kernel_sys_aio .............   Passed    0.13 sec
    2/2 Test #1036: guest_kernel_sys_ksem ............   Passed    5.36 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/prx/libkernel/Equeue/Equeue.cpp, core/libs/prx/libkernel/File/src/Stdio.cpp, core/libs/prx/libkernel/Semaphore/include/Ksem.hpp, core/libs/prx/libkernel/Semaphore/src/Ksem.cpp, core/libs/prx/libkernel_sys/Aio.cpp reverted to main and the new tests kept, the suite fails:
    1/2 Test #1060: guest_kernel_sys_aio .............***Failed    0.02 sec
    2/2 Test #1036: guest_kernel_sys_ksem ............***Failed    5.33 sec
    0% tests passed, 2 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified KsemStat and KsemChangeMode implementations against FreeBSD 11.4 uipc_sem.c semantics, confirming S_IFREG mode bits, UID/GID inheritance, microsecond-truncated timestamp updates, and ACCESSPERMS bit masking.
    • Audited fstat_nid_disambig1_nid_postfix, sceKernelFstat, and sceKernelFchmod in core/libs/prx/libkernel/File/src/Stdio.cpp for proper GuestCopyOut EFAULT propagation and descriptor dispatch ordering.
    • Checked concurrency safety and lock hierarchies in Ksem.cpp: Find(), g_ksemLock, and ksem->lock are used consistently without inversion, recursion, or deadlocks across wait, post, stat, and chmod.
    • Verified AIO and Equeue interactions in core/libs/prx/libkernel_sys/Aio.cpp and core/libs/prx/libkernel/Equeue/Equeue.cpp, confirming semaphores are rejected with GuestEopnotsupp / non-regular file handling instead of misinterpreting them as regular files.
    • Checked 64-bit integer sizing, pointer validity checking (including NULL abstime and value copy-out failure paths), and absence of forbidden comments or AGENTS.md rule violations.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5997086282 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24491#pullrequestreview-5997086282). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-20T17:51:34Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `f9c7ef4e59`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_kernel_sys_aio_tests`, `guest_kernel_sys_ksem_tests` built cleanly. - **CTest** (`guest_kernel_sys_ksem`, `guest_kernel_sys_aio`): ``` 1/2 Test #1060: guest_kernel_sys_aio ............. Passed 0.13 sec 2/2 Test #1036: guest_kernel_sys_ksem ............ Passed 5.36 sec 100% tests passed, 0 tests failed out of 2 ``` - **Negative control**: with `core/libs/prx/libkernel/Equeue/Equeue.cpp`, `core/libs/prx/libkernel/File/src/Stdio.cpp`, `core/libs/prx/libkernel/Semaphore/include/Ksem.hpp`, `core/libs/prx/libkernel/Semaphore/src/Ksem.cpp`, `core/libs/prx/libkernel_sys/Aio.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/2 Test #1060: guest_kernel_sys_aio .............***Failed 0.02 sec 2/2 Test #1036: guest_kernel_sys_ksem ............***Failed 5.33 sec 0% tests passed, 2 tests failed out of 2 ``` Restoring the change makes it pass again. - **Code reading**: - Verified KsemStat and KsemChangeMode implementations against FreeBSD 11.4 uipc_sem.c semantics, confirming S_IFREG mode bits, UID/GID inheritance, microsecond-truncated timestamp updates, and ACCESSPERMS bit masking. - Audited fstat_nid_disambig1_nid_postfix, sceKernelFstat, and sceKernelFchmod in core/libs/prx/libkernel/File/src/Stdio.cpp for proper GuestCopyOut EFAULT propagation and descriptor dispatch ordering. - Checked concurrency safety and lock hierarchies in Ksem.cpp: Find(), g_ksemLock, and ksem->lock are used consistently without inversion, recursion, or deadlocks across wait, post, stat, and chmod. - Verified AIO and Equeue interactions in core/libs/prx/libkernel_sys/Aio.cpp and core/libs/prx/libkernel/Equeue/Equeue.cpp, confirming semaphores are rejected with GuestEopnotsupp / non-regular file handling instead of misinterpreting them as regular files. - Checked 64-bit integer sizing, pointer validity checking (including NULL abstime and value copy-out failure paths), and absence of forbidden comments or AGENTS.md rule violations. Looks Good To Me
MarcZX10 commented 2026-11-21 05:54:33 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed — looks good

The change was reviewed at head f9c7ef4e59feebdc7c1ab3f2241939188bb3e0c7; the inspected implementation and tests support the stated ksem metadata behavior, and no blocking defect was verified.

What I ran

No commands or tests were run. I inspected the prepared diff and targeted repository source through repo_read.py.

Results

Check Result Note
Diff and changed code passed All seven changed files included in the review manifest were read.
Runtime tests not run Test results in the PR description were author reported.
Negative control not run No negative control was run during this review.

Findings

Blocking

None.

Non-blocking

None.

Not verified

The reported Linux test runs, convention check, and negative control were not independently run. Windows, macOS, and physical console behavior were not verified. review/files.json was not available in the prepared review listing; the manifest lists seven changed files, all included and read. The source review covered the changed implementation, its directly relevant callers, and tests; it did not inspect unrelated repository code.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6003334582 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24491#pullrequestreview-6003334582). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T05:54:33Z. **Review:** APPROVED --- # Reviewed — looks good The change was reviewed at head `f9c7ef4e59feebdc7c1ab3f2241939188bb3e0c7`; the inspected implementation and tests support the stated ksem metadata behavior, and no blocking defect was verified. ## What I ran No commands or tests were run. I inspected the prepared diff and targeted repository source through `repo_read.py`. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | All seven changed files included in the review manifest were read. | | Runtime tests | not run | Test results in the PR description were author reported. | | Negative control | not run | No negative control was run during this review. | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The reported Linux test runs, convention check, and negative control were not independently run. Windows, macOS, and physical console behavior were not verified. `review/files.json` was not available in the prepared review listing; the manifest lists seven changed files, all included and read. The source review covered the changed implementation, its directly relevant callers, and tests; it did not inspect unrelated repository code. ## 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!24491
No description provided.