Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
brajankw:fix/libkernel-ksem-fstat-fchmod
Nov 22, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
brajankw:fix/libkernel-ksem-fstat-fchmod

Conversation

@brajankw

Copy link
Copy Markdown
Contributor

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 feat(libkernel): share a ksem semaphore across dup, dup2 and F_DUPFD, as FreeBSD 11.4 does #22191 replaces, so this merges with feat(libkernel): share a ksem semaphore across dup, dup2 and F_DUPFD, as FreeBSD 11.4 does #22191 and with my other ksem PRs without overlap, in any order.
  • Open feat(libkernel): implement the named ksem semaphores #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.

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

…s ksem_stat and ksem_chmod do

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_init creates it with S_IRWXU | S_IRWXG
under the umask, owned by the caller; 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. The times are taken to the microsecond, as
vfs_timestamp does by default: all four at creation, the change time on
ksem_post and the access time on every wait and on ksem_getvalue, also
when its copy-out then fails.

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
an exported IsKsemDescriptor_nid_no_patch instead: the equeue calls keep
their throw, and AIO keeps EOPNOTSUPP for a read and gives
aio_fsync EINVAL, as aio_aqueue does for a file without a vnode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

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.

@oneandonlydean
oneandonlydean merged commit 5c69358 into boykopovar:main Nov 22, 2026
14 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.

4 participants