Repository navigation
fix(libkernel): answer fstat and fchmod on a ksem id as FreeBSD 11.4's ksem_stat and ksem_chmod do - #24491
Conversation
…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
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against f9c7ef4e59:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_kernel_sys_aio_tests,guest_kernel_sys_ksem_testsbuilt 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.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.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 - 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
left a comment
There was a problem hiding this comment.
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.
What
A ksem id is a host null device descriptor here, so
fstatreported the host's/dev/null, andfchmodcalled the host'sfchmodon it, which as root changes the mode of the host's/dev/null. FreeBSD 11.4 answers both from the semaphore:ksem_creategivesksem_init's semaphoreS_IRWXU | S_IRWXGunder the umask, owned by the caller (ksem_alloc); the owner isgetuid/getgid, both 0 here.ksem_statreports a zeroed stat withS_IFREG, that mode, the owner and the four times.ksem_chmodkeeps the mode'sACCESSPERMSbits and changes no time.vfs_timestampdoes by default: all four at creation, the change time onksem_post(L733), and the access time on every wait, before the count is checked, so a failingksem_trywaittoo (L820), and onksem_getvalue, also when its copy-out then fails (L888).fstatwhether a descriptor is a regular file, which a ksem id would now pass. They ask a new exportedIsKsemDescriptor_nid_no_patchinstead, as AIO is in libkernel_sys:sceKernelAddReadEventandsceKernelAddWriteEventkeep their throw on a ksem id,aio_readkeepsEOPNOTSUPP(aio_queue_file), andaio_fsyncnow getsEINVAL(aio_aqueue) where it gotEOPNOTSUPP.IsKsemDescriptor_nid_no_patchhas its own body and does not callIsKsemDescriptor, 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.ksem_openwith(void)mode; on top of this, it would need to keep its mode asksem_initnow does.MetadatainGuestKernelSysKsem.cpp, through_fstatandsceKernelFstat,fchmodandsceKernelFchmod, the times after post, waits andksem_getvalue, and the two equeue calls; the ksem cases inGuestKernelSysAio.cpp. Thefstatchecks come first, so onmainthe test fails before it callsfchmod.Tested
Linux (Debian 13, GCC 14.2.0, Release). Head
f9c7ef4e59, the single commit onmainc8f72e905c.main84e16538d4,guest_kernel_sys_ksemfails at the newst_modecheck, before anyfchmod.f9c7ef4e59,ctestoverguest_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_fionreadandguest_kernel_ioctl_device_type: all 10 pass.ctest(6393 tests,-j 12) on this branch before review, which then added the equeue and AIO exclusions and theksem_getvalueaccess time, and my other eleven branches of this round, merged ontomain84e16538d4: 6378 passed. The 15 failures also fail onmainin this environment:.gitdirectory, 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_getloginandguest_getlogin_no_session;agc_driverGPU cases fail the same way onmain:prepared_shaderswith itsindirect,uncachedanddebugforms, andstorage_file_write;agc_driver_image_table_packed_guardsis unstable onmainas well (2 of 10 runs passed on its own onmain3f4693be43).python3 tools/check_conventions.py --base origin/main: passed.python3 tools/pr_overlap.py --branch fix/libkernel-ksem-fstat-fchmod: could not compare feat(libkernel): support SEEK_DATA and SEEK_HOLE in lseek #6936, feat(libkernel): report open, read and close notes on watched files #7121, feat(libc): support archive-backed stdio reads #7447, feat(libkernel): implement the named ksem semaphores #9293, feat(libkernel_sys): implement POSIX message queues #9297, feat(libkernel_sys): implement kmq_notify registration #9302, feat(libkernel): cancel blocked POSIX reads with owned operations #12682, fix(libkernel): preserve blocking sockets over nonblocking native transport #12688, feat(libkernel): cancel blocked POSIX writes with partial progress #12700, feat(libkernel): support deferred cancellation in POSIX accept #12965 and feat(libkernel_sys): implement mount, unmount and nmount privilege checks #15037, which conflict withmain. feat(libkernel): implement the named ksem semaphores #9293, feat(libkernel_sys): implement POSIX message queues #9297 and feat(libkernel_sys): implement kmq_notify registration #9302 carry older copies of the ksem files; feat(libkernel): implement the named ksem semaphores #9293'sksem_openis noted above.git merge-treewith feat(libkernel): share a ksem semaphore across dup, dup2 and F_DUPFD, as FreeBSD 11.4 does #22191, feat(libkernel): add pipe write events and socket low watermarks to sceKernelAdd*Event #10645 and my other ksem branches is clean.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)