fix(libkernel_sys): fail settimeofday and clock_settime with EPERM for a jailed caller #15035

Merged
Gruzzberry merged 1 commit from upstream-pr/15035 into main 2026-11-06 23:50:59 +00:00
Gruzzberry commented 2026-11-02 11:22:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-11-02T11:22:35Z.


What

settimeofday and clock_settime now fail with EPERM after copying their arguments in, for every time, zone and clock. Before, a valid time threw (settimeofday) or went through NotImplemented_nid_no_patch (clock_settime), invalid values gave EINVAL, and settimeofday with only a time zone stored it for gettimeofday.

Ground truth is FreeBSD 11.4 (release/11.4.0): sys_settimeofday copies in the time, then the zone, and kern_settimeofday calls priv_check(td, PRIV_SETTIMEOFDAY) before it validates the time or stores the zone (kern_time.c L660-L707); sys_clock_settime copies in and kern_clock_settime calls priv_check(td, PRIV_CLOCK_SETTIME) before the clock id and range checks (L385-L412). prison_priv_check has no case for either privilege, so a jailed process reaches its default: return (EPERM). The credential model is the one maintainers agreed on #11216: a title runs jailed with uid 0. EFAULT for an unreadable pointer keeps its place before the check, so null and 0x10 give EFAULT where FreeBSD's copyin would. The time zone is no longer kept, so gettimeofday always reports 0 and 0, and the sceKernelSettimeofday wrapper returns 0x80020001.

adjtime is left to #14257, which implements it.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, Ninja, Release, -DBUILD_TESTING=ON, on current main.

  • guest_kernel_sys and guest_freebsd_clocks: 3/3. With Time.cpp from main and the new tests, both abort.
  • The tests cover EFAULT for 1 and 0x10 pointers in every argument combination, EPERM for valid and invalid times (negative seconds, microseconds out of range) with and without a zone, EPERM for every clock id including unknown ones, the sceKernelSettimeofday mapping, and that the wall clock is unchanged afterwards.
  • Full suite on an earlier head of this branch (before the final rebase and removing adjtime): only environmental failures (vulkan_validation_gate_reports, check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted), annotate_module timing out under load, and two tests that pass alone 3/3 (guest_filesystem, agc_driver_job_manager_compute_capacity). After the rebase I reran the focused tests and check_conventions.py only.
  • Not tested: Linux, macOS, a console. The jail model follows prison_priv_check and is not measured on a console; the debt entries say so.

Overlaps

  • #12418 and #14618 edit GuestFreebsdClocks.cpp next to the clock_settime test block; #13127 and #14085 edit neighbouring lines of docs/dev/debt/libkernel_sys.md. I proposed on each that they go first and this PR rebases.
  • #14257 (adjtime) does not overlap now; a non-null delta there could return EPERM the same way (kern_adjtime checks PRIV_ADJTIME first).

Checklist

  • Based on current main; no other open PR implements the same function
  • 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); no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source: none added
  • Generic behaviour, not specific to one title
  • Depends on: none; merge order: after #12418, #14618, #13127, #14085 (neighbouring lines)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5119483887 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15035). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-11-02T11:22:35Z. --- ### What `settimeofday` and `clock_settime` now fail with `EPERM` after copying their arguments in, for every time, zone and clock. Before, a valid time threw (`settimeofday`) or went through `NotImplemented_nid_no_patch` (`clock_settime`), invalid values gave `EINVAL`, and `settimeofday` with only a time zone stored it for `gettimeofday`. Ground truth is FreeBSD 11.4 (release/11.4.0): `sys_settimeofday` copies in the time, then the zone, and `kern_settimeofday` calls `priv_check(td, PRIV_SETTIMEOFDAY)` before it validates the time or stores the zone ([kern_time.c L660-L707](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_time.c#L660-L707)); `sys_clock_settime` copies in and `kern_clock_settime` calls `priv_check(td, PRIV_CLOCK_SETTIME)` before the clock id and range checks ([L385-L412](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_time.c#L385-L412)). [`prison_priv_check`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_jail.c#L3060-L3386) has no case for either privilege, so a jailed process reaches its `default: return (EPERM)`. The credential model is the one maintainers agreed on #11216: a title runs jailed with uid 0. `EFAULT` for an unreadable pointer keeps its place before the check, so null and `0x10` give `EFAULT` where FreeBSD's `copyin` would. The time zone is no longer kept, so `gettimeofday` always reports 0 and 0, and the `sceKernelSettimeofday` wrapper returns `0x80020001`. `adjtime` is left to #14257, which implements it. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on current `main`. - `guest_kernel_sys` and `guest_freebsd_clocks`: 3/3. With `Time.cpp` from `main` and the new tests, both abort. - The tests cover `EFAULT` for 1 and `0x10` pointers in every argument combination, `EPERM` for valid and invalid times (negative seconds, microseconds out of range) with and without a zone, `EPERM` for every clock id including unknown ones, the `sceKernelSettimeofday` mapping, and that the wall clock is unchanged afterwards. - Full suite on an earlier head of this branch (before the final rebase and removing `adjtime`): only environmental failures (`vulkan_validation_gate_reports`, `check_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted`), `annotate_module` timing out under load, and two tests that pass alone 3/3 (`guest_filesystem`, `agc_driver_job_manager_compute_capacity`). After the rebase I reran the focused tests and `check_conventions.py` only. - Not tested: Linux, macOS, a console. The jail model follows `prison_priv_check` and is not measured on a console; the debt entries say so. ### Overlaps - #12418 and #14618 edit `GuestFreebsdClocks.cpp` next to the `clock_settime` test block; #13127 and #14085 edit neighbouring lines of `docs/dev/debt/libkernel_sys.md`. I proposed on each that they go first and this PR rebases. - #14257 (`adjtime`) does not overlap now; a non-null delta there could return `EPERM` the same way (`kern_adjtime` checks `PRIV_ADJTIME` first). ### Checklist - [x] Based on current `main`; no other open PR implements the same function - [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) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source: none added - [x] Generic behaviour, not specific to one title - [x] Depends on: none; merge order: after #12418, #14618, #13127, #14085 (neighbouring lines) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
mashingaan commented 2026-11-02 11:37:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mashingaan on 2026-11-02T11:37:50Z.

Review: DISMISSED


The diff removes the only write to zoneOutput in gettimeofday_nid_postfix, so a caller passing a non-null timezone now keeps its old contents. The existing GuestFreebsdClocks test KernelTimezoneIsUtc still initializes KernelTimezone zone{-1, -1} and requires gettimeofday_nid_postfix(nullptr, &zone) to write both fields as zero. The current implementation therefore contradicts an existing regression assertion and drops previously working behavior. Please preserve the timezone output with GuestCopyOut_nid_no_patch (mapping a bad destination to EFAULT), and keep the test on the Hosted Windows run. That run is still pending on this head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5735958223 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15035#pullrequestreview-5735958223). Originally posted by [mashingaan](https://github.com/mashingaan) on 2026-11-02T11:37:50Z. **Review:** DISMISSED --- The diff removes the only write to zoneOutput in gettimeofday_nid_postfix, so a caller passing a non-null timezone now keeps its old contents. The existing GuestFreebsdClocks test KernelTimezoneIsUtc still initializes KernelTimezone zone{-1, -1} and requires gettimeofday_nid_postfix(nullptr, &zone) to write both fields as zero. The current implementation therefore contradicts an existing regression assertion and drops previously working behavior. Please preserve the timezone output with GuestCopyOut_nid_no_patch (mapping a bad destination to EFAULT), and keep the test on the Hosted Windows run. That run is still pending on this head.
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!15035
No description provided.