Skip to content

fix(libkernel): validate clock_settime before EPERM - #17551

Closed
AasishDairelSahayaGrinspan wants to merge 1 commit into
boykopovar:mainfrom
AasishDairelSahayaGrinspan:fix/libkernel-clock-settime-eperm
Closed

AasishDairelSahayaGrinspan wants to merge 1 commit into
boykopovar:mainfrom
AasishDairelSahayaGrinspan:fix/libkernel-clock-settime-eperm

Conversation

@AasishDairelSahayaGrinspan

Copy link
Copy Markdown
Contributor

What

Validate clock_settime arguments like FreeBSD 11.4 before returning EPERM for a valid CLOCK_REALTIME request. This preserves the host clock and models the title's jailed environment rather than throwing for valid requests.

Tested

macOS 26, AppleClang: python3 tools/check_conventions.py --base origin/main passed. Build/test attempted via cmake -S . -B build-clock-settime -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON and cmake --build build-clock-settime --target guest_freebsd_clocks_tests -j 4; build blocked by existing unrelated compile errors in core/libs/prx/libc/src/RunReport.cpp (std::thread unavailable in this compiler configuration). The new test adds valid realtime → EPERM and verifies invalid clock/field inputs → EINVAL; the existing validation cases remain. No PS5 hardware test.

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

@JJenkx

JJenkx commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Windows check of head 4b61ec368d, merged onto main 0bae53facd (4b61ec368d, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • guest_freebsd_clocks passes in 3 of 3 runs, none skipped.

AI-assisted: yes.

@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6149 docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7447 docs/dev/debt/libkernel.md
PR 9156 docs/dev/debt/libkernel.md
PR 9293 docs/dev/debt/libkernel.md
PR 9297 docs/dev/debt/libkernel.md
PR 9302 docs/dev/debt/libkernel.md
PR 9389 docs/dev/debt/libkernel.md
PR 9557 docs/dev/debt/libkernel.md
PR 9566 docs/dev/debt/libkernel.md
PR 9938 docs/dev/debt/libkernel.md
PR 10442 docs/dev/debt/libkernel.md
PR 11215 docs/dev/debt/libkernel.md
PR 11628 docs/dev/debt/libkernel.md
PR 11661 docs/dev/debt/libkernel.md
PR 11689 docs/dev/debt/libkernel.md
PR 11698 docs/dev/debt/libkernel.md
PR 11896 docs/dev/debt/libkernel.md
PR 12418 core/libs/prx/libkernel/Time/Time.cpp
core/libs/tests/GuestFreebsdClocks.cpp
PR 12642 core/libs/prx/libkernel/Time/Time.cpp
PR 12682 docs/dev/debt/libkernel.md
PR 12688 docs/dev/debt/libkernel.md
PR 12700 docs/dev/debt/libkernel.md
PR 12965 docs/dev/debt/libkernel.md
PR 14108 docs/dev/debt/libkernel.md
PR 15040 docs/dev/debt/libkernel.md
PR 16790 docs/dev/debt/libkernel.md
PR 18515 core/libs/prx/libkernel/Time/Time.cpp
docs/dev/debt/libkernel.md
PR 18654 docs/dev/debt/libkernel.md
PR 19650 docs/dev/debt/libkernel.md

@AasishDairelSahayaGrinspan

Copy link
Copy Markdown
Contributor Author

Manual overlap re-check after the pr-overlap report (head 4b61ec3):

No build dependency on any of them: Depends on stays none.

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

Windows run of head 4b61ec368d, merged onto main 286b14d2f7 in one tree with 47 other small open PRs that touch different files. Setup: Windows 11, WinLibs GCC 15.2 r7 (x86_64-ucrt-posix-seh, the CI toolchain), Release, BUILD_TESTING=ON.

  • guest_freebsd_clocks with --repeat until-fail:5: 5 / 5 pass.
  • Negative control, with main's Time.cpp and this PR's test: 5 / 5 runs fail.

@AasishDairelSahayaGrinspan
AasishDairelSahayaGrinspan force-pushed the fix/libkernel-clock-settime-eperm branch from 4b61ec3 to 015d530 Compare November 9, 2026 13:52
@AasishDairelSahayaGrinspan

Copy link
Copy Markdown
Contributor Author

Rebased onto main (now at d792f1ad5f), new head 015d530c4e. The conflict came from #16437 merging: it added the clock_gettime/clock_getres debt entries after the clock_settime bullet this PR rewrites, and its ClockIoErrorIds call sits next to the renamed SettimeValidatesLikeFreebsd call in the test. Resolved by keeping both sides unchanged in content: this PR's validation-before-EPERM clock_settime behavior and tests, plus #16437's entries and test call. Ready for re-review.

@ycbuzi

ycbuzi commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Tested 015d530c4e merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • Windows Server 2022 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 99% tests passed, 42 tests failed out of 5018; 99% tests passed, 42 tests failed out of 5018. 42 of the failing tests fail on main too. run
  • Windows Server 2025 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 99% tests passed, 16 tests failed out of 5018; 99% tests passed, 16 tests failed out of 5018. 16 of the failing tests fail on main too. run

AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@AasishDairelSahayaGrinspan, the order this PR adds does not match FreeBSD 11.4. In sys/kern/kern_time.c (releng/11.4), sys_clock_settime copies the timespec in first, so a bad pointer gives EFAULT. kern_clock_settime then calls priv_check(td, PRIV_CLOCK_SETTIME) first and returns its error, and only after that checks clock_id != CLOCK_REALTIME and the tv_nsec/tv_sec ranges for EINVAL. kern_settimeofday checks privilege before its range check in the same way.

The PR body says main models the title as jailed. A jailed caller gets EPERM from priv_check for every input, so main's EPERM-for-everything already matches FreeBSD under that model, and returning EINVAL first for a bad clock id or timespec does not. Please keep EFAULT, then EPERM, and drop the new EINVAL-before-EPERM cases. If you have a console measurement showing a title gets EINVAL first, please link it and I will look again.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@AasishDairelSahayaGrinspan I checked FreeBSD 11.4 and the order is the other way round. In sys/kern/kern_time.c at release/11.4.0, sys_clock_settime copies the timespec in, then kern_clock_settime calls priv_check(td, PRIV_CLOCK_SETTIME) first (line 401) and only checks clock_id and the tv_nsec/tv_sec range after it (lines 403 to 407). For an unprivileged process, any readable request returns EPERM whether or not its arguments are valid, which is what main already does. Unless a console run shows the PS5 validating first, I think main's behaviour should stay. If you have such a measurement, please add it to the PR and the debt entry.

@AasishDairelSahayaGrinspan
AasishDairelSahayaGrinspan force-pushed the fix/libkernel-clock-settime-eperm branch from cd20f64 to 2742166 Compare November 16, 2026 02:56
@AasishDairelSahayaGrinspan

Copy link
Copy Markdown
Contributor Author

Rebased onto main (now at cf2f418374), new head 2742166cd2. Conflicts came from the clock work that merged since: main now implements clock id 20 (Clock20UsesScaledMonotonicTime in the test) and evolved the clock_gettime/clock_getres debt entries. Resolved by keeping both sides: main's new id-20 test call and evolved clock entries, and this PR's validation-before-EPERM clock_settime with its SettimeValidatesLikeFreebsd test (one stale duplicate of the old clock_settime debt bullet, left by the merge, removed). guest_freebsd_clocks_tests built and passed on the rebased head (Release, AppleClang); python3 tools/check_conventions.py --base origin/main passed. Ready for re-review.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Hi AasishDairelSahayaGrinspan, two things on the rebased head 2742166.

  1. The rebase brought back stale debt. libkernel.md now has the evolved clock_getres and clock_gettime bullets (lines 401 and 402) and, after your clock_settime bullet, two older ones: "clock_gettime throws for id 20" and "clock_getres reports the host clock's resolution". Id 20 is implemented on main, so these contradict the current entries. Please delete those two bullets (lines 405 and 406) and keep only your clock_settime one.

  2. The behaviour question from the earlier comments is still open: FreeBSD 11.4 kern_clock_settime calls priv_check before it looks at the clock id or the timespec, and main models this title as unprivileged, so EFAULT and then EPERM for everything is what main already returns. The new debt line says "for a root caller", which is not the model main uses. Unless you can link a console run where a title gets EINVAL from clock_settime before EPERM, please go back to EFAULT then EPERM and drop the EINVAL cases and the renamed test; with a measurement I will look again.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 16, 2026
@AasishDairelSahayaGrinspan

Copy link
Copy Markdown
Contributor Author

Both points taken.

  1. The EINVAL ordering is withdrawn — there is no console run showing a title getting EINVAL from clock_settime before EPERM. FreeBSD 11.4's kern_clock_settime calls priv_check with PRIV_CLOCK_SETTIME before it reads the clock id or the timespec, and prison_priv_check denies it to a jailed process, so main's EFAULT-then-EPERM order is the model.

  2. Current main already does exactly this: clock_settime_nid_postfix copies the time in (EFAULT for a null or unreadable pointer) and then fails with EPERM whatever the clock and time hold, SettimeIsRefusedLikeAJailedProcess covers it in GuestFreebsdClocks.cpp, and the debt entry (a7dee20) states the model. The two stale clock_gettime and clock_getres bullets are gone from main as well.

With the code and test back on main's version the branch is empty against main, so I am closing this PR. Thanks for the review.

@AasishDairelSahayaGrinspan
AasishDairelSahayaGrinspan deleted the fix/libkernel-clock-settime-eperm branch November 16, 2026 14:25
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.

5 participants