fix(libkernel): return EFAULT for an unwritable clock_gettime or clock_getres result #12418
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12418"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-28T12:57:48Z.
What
clock_gettimeandclock_getreswrote their result straight through the guest pointer, so a non-null pointer into unmapped or read-only memory crashed the host.gettimeofdayandsceKernelGettimezonenext to them already copy out withGuestCopyOut_nid_no_patch. FreeBSD'ssys_clock_gettimeandsys_clock_getresusecopyoutand returnEFAULT.clock_gettimecomputes the value as before, then copies it out withGuestCopyOut_nid_no_patch, failing withEFAULT(14). A null pointer still fails withEFAULTafter clock validation, as before.clock_getrescomputes into a local value (the body moves to a staticReadClockResolution, unchanged apart from the recursion for CPU-time clocks) and copies it out once. A null result still succeeds, and an unsupported clock still throws before the pointer is used.sceKernelClockGettimeandsceKernelClockGetresreturn0x8002000ethrough their existingSceKernelErrormapping.Tested
BadTimePointersFaultincore/libs/tests/GuestFreebsdClocks.cpp: forCLOCK_REALTIME,CLOCK_MONOTONIC,CLOCK_SECONDandCLOCK_THREAD_CPUTIME_ID, both the_nid_postfixcalls and thesceKernelwrappers with an unmapped pointer (0x10) and a read-only page fail withEFAULT/0x8002000e, and the read-only page is left unchanged.anyps5-buildCI image, Ubuntu 24.04, GCC 13, Release,BUILD_TESTING=ON), this headf4863ca1f5aonmain2227f136618:guest_freebsd_clockspasses; the fullctestpasses (2,680 run, 0 failed), andcheck_conventions.pypasses. Withmain'sTime.cppthe same test segfaults.BUILD_TESTING=ON, this head: full build andlibssucceed;guest_freebsd_clockspasses 3 of 3 alone; withmain'sTime.cppit crashes (0xc0000005). CI's test selection for this change (2,348 tests): 1,621 pass, 726 skip, and the only failure isguest_raise_exception, which also fails onmainon this machine (#11813).check_conventions.pypasses.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)Summary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T13:03:18Z.
Review: COMMENTED
Reviewed exact head
f4863ca1f5. The change computes into local KernelTimespec values and routes only the final guest write through GuestCopyOut, preserving clock validation before output-pointer validation for clock_gettime and preserving the null-output success path for clock_getres. The regression covers unmapped and read-only targets for four valid clock IDs, both libc NID calls and SceKernel wrappers, and verifies the read-only target remains unchanged. The current-main comparison shows no intervening changes to Time.cpp or GuestFreebsdClocks.cpp since the tested2227f136base; current-main diff check and conventions pass. I did not rerun the large Linux/Windows suites locally. Hosted Windows and report checks were pending when checked, so I have not approved.Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T13:30:43Z.
Review: COMMENTED
Reviewed exact head
f4863ca1f5, adding the macOS evidence the body doesn't have (it lists Linux and Windows only).Gates.
git merge-treeagainst currentmain750cf6cf48is clean.check_conventions.py --base mainpasses.pr_overlap.pyreports only the three uncomparableTime.cppsharers. I checked them by hand: #12130 touches onlyCancellableSleep(L354), andmerge-tree pr-12418 × pr-12130is clean. #7804 and #5021 conflict withmainalready.What
GuestCopyOutdoes with these pointers on macOS (standalone probe, Apple clang, arm64, 16-bytemach_vm_writelike the__APPLE__arm):KERN_INVALID_ADDRESS)0x10So on macOS both cases in
BadTimePointersFaulthit thefalsepath, and the read-only page is left untouched, as the finalRequireexpects. The same probe in the Linux (process_vm_writev, Ubuntu 24.04 GCC 13) image gives stack → 16, RO → -1/EFAULT,0x10→ -1/EFAULT. The straddle case → 8, and the RW half is written.GuestCopyOutstill returnsfalsethere (copied != bytes), so the result is EFAULT with a partial write. That matches FreeBSDcopyoutfaulting part-way through, so it isn't a problem here. I'm only noting it because the test's "unchanged" assertion is only meaningful for a whole-page RO target, which is what the test uses.Host callers still work.
clock_gettime_nid_postfixis also called internally with host-stackKernelTimespecs:Pthread/Posix/Common.hpp:30RelativeMicroseconds(every timed mutex/cond/sem/rwlock wait),Ktimer.cpp:55,SyncOnAddress.cpp:236,Socket/src/Udp.cpp:1673(per received datagram),SystemConfiguration.cpp:103andlibkernel_sys/Uuid.cpp:37. The probe above shows host-stack writes succeed on both POSIX primitives. On Windows,BeginHostWriteonly walksviews, so a non-guest address returnstrue. On macOS,GuestWriteWatch::forEachPageonly visits registered ranges, so these callers keep working.Cost (non-blocking). 2M iterations of
CLOCK_MONOTONIC+ 16-byte checked copy to a stack local:clock_gettimeFor guest calls this is the price of correctness, and
gettimeofdayalready pays it. The internal host callers pay it too, though, for pointers that can't fault. On macOS that also means taking the write-watch_lockplus a_hostWritespush/erase when the watch is available. Timed waits and per-datagram stamping hide a few hundred ns, so I'm not blocking on it. A cheap follow-up would be to route those callers through the existing uncheckedReadClock(e.g., aReadClock_nid_no_patchhost export) instead of the guest-facing entry point.Semantics checked against
main. These stay the same: clock validation before the null/fault check inclock_gettime(OmittedTimeIsFaultAfterClockValidation), nullres→ 0 before validation inclock_getres, unsupported clock still throws without touching the output (UnsupportedResolutionStillThrows), and the CPU-clock recursion now goes throughReadClockResolutioninto the local value. I didn't build the libs lane on macOS (no submodules here), soguest_freebsd_clocksitself wasn't run on macOS. The primitive-level probe above covers the host-specific part of the change.The change looks correct to me. I'm leaving this as a comment, not an approval, because I couldn't run the PR's own test on macOS. If someone with a libs-lane macOS build runs
guest_freebsd_clockson this head, that closes the gap. The cost note doesn't block.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T08:46:17Z.
Review: CHANGES_REQUESTED
Re-reviewed exact head
f4863ca1f5a65ea06b7eea491713c1a5628c1491and the new Windows report. The checked guest write and invalid-pointer regressions are the right correctness direction; I agree the FreeBSD null-result behavior is preserved. The remaining issue is the reported hot-path cost: the guest-path measurement is about 1.31 µs/call versus 30.4 ns on main (about 43×), and the author says titles call this path many times per frame. I would not restore an unchecked store, which would lose the EFAULT behavior. Please pursue a safe fast path in the shared guest-copy helper as a prerequisite (so the sibling syscall fixes can use it too), or provide representative title/frame call-count evidence showing the aggregate cost is acceptable before merging this hot-path change. Keep the bad-pointer tests against the checked behavior.The Windows run reports the PR alone still aborts on an Eastern-time host because main’s unrelated
KernelTimezoneIsUtcassertion is present; it passes when combined with open #12660. Please coordinate and record the order with #12660 so rebasing preserves both its timezone-test correction and this PR’s bad-pointer regression. The reported Windows run is on the combined tree; it is not a standalone pass for this head. Linux/product hosted jobs are skipped. I did not run these tests locally.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-29T14:49:04Z.
Review: CHANGES_REQUESTED
Thanks, the change itself is right:
GuestCopyOutgives theEFAULT, clock validation still comes first, the nullclock_getresresult matches FreeBSD, and the test pins the unmapped and read-only cases.Blocking, as already raised by @oneandonlydean and @MarcZX10:
core/libs/prx/libkernel/Time/Time.cpp(clock_gettime_nid_postfix): the checked copy takes aprocess_vm_writevsyscall on every successful call, 1,308 ns vs 30 ns onmainby your own measurement.clock_gettimeis on a hot path, so please merge this only after the guest-copy fast path lands (your option 1, a per-thread cache invalidated by a generation bumped in map/mprotect/munmap, is a reasonable start; keep the bad-pointer tests on the checked path) and rerun the same per-call benchmark on this head.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T06:43:10Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the code routes clock results through guest copy-out and maps copy failures to
EFAULT.What I ran
Reviewed head
6671f295ecc5ab1ae16a481a9017f99812fa8502and the prepared diff. No build or tests were run. Readcore/libs/prx/libkernel/Time/Time.cppand the excluded regression testcore/libs/tests/GuestFreebsdClocks.cpp. The changed file was fully read. The description, commit list, manifest, and diff were read.files.jsonwas not available in the prepared bundle; the file listing and description identify the test as excluded.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
Builds and tests were not run. The full changed test file was excluded by review-folder rules, so its unshown remainder was not verified. The author reports passing platform test runs in the description.
To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-29T22:59:40Z.
Review: COMMENTED
Actionable comments posted: 1
80ede3e3-21b1-4f1e-836b-b3612a56b5eeReviewing files that changed from the base of the PR and between
29e4640295and1dfbba5929.core/libs/prx/libkernel/Time/Time.cppcore/libs/tests/GuestFreebsdClocks.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-29T23:05:26Z.
Review: COMMENTED
Reviewed — no blocking findings
The reviewed clock output paths now use checked guest-memory copies and preserve the documented pointer and clock validation behavior; I did not run tests.
What I ran
No commands or tests were run. I read the prepared description, commits, manifest, diff, and relevant ranges of
core/libs/prx/libkernel/Time/Time.cppat head1dfbba5929ee06100ee12de9e0be517e243cb617. The reader was available asrepo_read.py.Results
Findings
Blocking
None.
Non-blocking
None.
Sources
The reviewed clock implementation is in
Time.cppat the pinned head.Not verified
I did not read the excluded
core/libs/tests/GuestFreebsdClocks.cppor run the reported tests. Only relevant ranges, not the full 975-lineTime.cpp, were read. No other files were listed in the manifest.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T13:24:56Z.
Review: COMMENTED
Built the focused checks against the existing Linux runtime.
GuestFreebsdClockspassed.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.