Skip to content

fix(libkernel): check settimeofday and clock_settime pointers - #11402

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
00xDisaster:fix/libkernel-settime-efault
Oct 27, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
00xDisaster:fix/libkernel-settime-efault

Conversation

@00xDisaster

@00xDisaster 00xDisaster commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What

clock_settime read the caller's timespec in place, and settimeofday read its timeval and timezone in place, so a pointer into unmapped memory crashed the host. Both now copy the structures in with a checked read first, as FreeBSD 11.4's sys_clock_settime and sys_settimeofday do, and return EFAULT before validation when a non-null pointer is unreadable. clock_settime still returns EFAULT for a null time, and settimeofday still treats a null argument as omitted. This keeps EFAULT ahead of EINVAL and keeps an unreadable time zone from being stored.

The checked read is independent of #10975, which adds a shared GuestCopyIn helper for the getters and setitimer but not these setters. clock_settime overlaps #11042, which replaces the valid-time descriptive throw with NotImplemented_nid_no_patch and changes the same clock test. The merged function keeps this PR's copy, EFAULT and EINVAL order, then #11042's NotImplemented path for a valid request; neither change replaces the other. #9677 adds a GuestTime.hpp include in Time.cpp; #9713 adds includes and ThreadAndSessionTests in GuestKernelSys.cpp. Those are merge-order conflicts only.

Merge order proposal: this PR first, then #9677, #11042 and #9713 rebase and keep both sides. No code dependency is required.

Tested

Linux x86_64, GCC 16.2.1, Release build, base upstream/main 4b1a9a3b2f; head 78571ca2fb. guest_freebsd_clocks and guest_kernel_sys pass 2/2 with new cases that pass 1 as the time, time zone, or both pointers:

  • clock_settime returns -1/EFAULT for an unreadable time with both a valid and an invalid clock.
  • settimeofday returns -1/EFAULT for an unreadable time, an unreadable zone, both, and a valid time with an unreadable zone.
  • An unreadable zone returns EFAULT before invalid time values return EINVAL.
  • The stored zone is unchanged after each failure.

Negative control with upstream/main 4b1a9a3b2f Time.cpp and the tests from this head: guest_freebsd_clocks and guest_kernel_sys both fail with Exception: SegFault at the first unreadable pointer. Tests were not run on Windows, macOS, the console, or a title.

Checklist

@github-actions github-actions Bot added the fix error Closes an issue (Fixes #N) label Oct 26, 2026
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 11476 core/libs/prx/libkernel/Time/Time.cpp L10, L41-42, L657, L661, L666, L671, L677, L682

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 5021 core/libs/prx/libkernel/Time/Time.cpp
PR 7804 core/libs/prx/libkernel/Time/Time.cpp
PR 9097 core/libs/tests/GuestKernelSys.cpp
PR 9713 core/libs/tests/GuestKernelSys.cpp
PR 10421 core/libs/tests/GuestKernelSys.cpp
PR 10427 core/libs/tests/GuestKernelSys.cpp
PR 11031 core/libs/tests/GuestKernelSys.cpp
PR 11034 core/libs/tests/GuestKernelSys.cpp
PR 11042 core/libs/prx/libkernel/Time/Time.cpp
core/libs/tests/GuestFreebsdClocks.cpp
PR 11216 core/libs/tests/GuestKernelSys.cpp

@MarcZX10

Copy link
Copy Markdown
Contributor

The overlap check's manual review found real integration work here. This head is based on cbaacd2910, while current main is 4b1a9a3b2f; please sync current main and rerun the focused tests on the refreshed head. The exact current heads also produce merge conflicts with #9677 and #11042 in Time.cpp: those PRs replace the valid clock_settime path's descriptive throw with NotImplemented_nid_no_patch, while this PR adds the checked read that must return EFAULT for an unreadable timespec and preserve EINVAL precedence. Please coordinate merge order with those authors and record any required dependency; the merged function needs both behaviors (checked read/validation first, then the unimplemented result for a valid request). There is also a GuestKernelSys.cpp conflict with #9713; its added routine is distinct, but the include-block conflict needs both additions preserved.

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

Changes requested for the stale base and unresolved file-level integrations described in my comment. I have not run a full review vote on the merged behavior until the branch is current and the overlapping clock_settime edits are coordinated.

Both read their caller's structures in place before validation, so an unreadable pointer crashed the host. Copy them in with a checked read first and return EFAULT, as FreeBSD 11.4 does.

Fixes boykopovar#11370
@EagleBearerYS

Copy link
Copy Markdown
Contributor

@00xDisaster Hi, a merge-order note: our #11042 conflicts with this PR in Time.cpp's clock_settime. This PR reads tp through a checked copy before the EFAULT/EINVAL checks, and #11042 replaces the throw for a valid request with NotImplemented_nid_no_patch. They compose: the checked copy and the error order stay, and a valid request then reaches the not-implemented path. I'd propose this PR goes first, merge order only, and #11042 takes the merge; I've recorded Depends on: #11402 there. Does that work for you?

@00xDisaster
00xDisaster force-pushed the fix/libkernel-settime-efault branch from 3fbd4b4 to 78571ca Compare October 26, 2026 11:30
@00xDisaster

Copy link
Copy Markdown
Contributor Author

@MarcZX10 Rebased onto current main 4b1a9a3b2f; new head 78571ca2fb. guest_freebsd_clocks and guest_kernel_sys pass 2/2. The same tests against main 4b1a9a3b2f Time.cpp both die with Exception: SegFault. Conventions and whitespace pass.

Merge order for the overlap report: I propose this PR first, then #9677, #11042 and #9713 rebase; no code dependency from this PR. The merged clock_settime must keep this PR's checked copy and EFAULT/EINVAL order, then use #11042's NotImplemented_nid_no_patch for a valid request. #9677's GuestTime.hpp include and #9713's includes and ThreadAndSessionTests in GuestKernelSys.cpp are separate additions and are kept. I have recorded this in the body. @EagleBearerYS @JJenkx @simonbronk please flag if you need a different order.

Could you re-review 78571ca2fb?

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

Re-reviewed exact head 78571ca against current main 4b1a9a3. The checked copies happen before clock/value validation, preserving EFAULT precedence; settimeofday only updates the stored timezone after both requested structures are readable, and the added cases cover unreadable time/zone pointers and unchanged state. The author reports guest_freebsd_clocks and guest_kernel_sys passing 2/2 on Linux, plus a negative control that segfaults on the same pointers with main. Hosted product jobs are skipped by path policy. I independently inspected the Linux, Windows, and macOS checked-copy branches and the existing GuestArena read hook. No blocking correctness issue found. Integration note: #11042 author agreed to rebase after this PR and keep the NotImplemented path for valid clock_settime; #9677 and #9713 make separate edits in shared files, so preserve their additions when they rebase.

@ycbuzi

ycbuzi commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

A native Windows run of 78571ca2fb: Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, merged onto main e38fcb00dc with no conflicts. Plain ctest --repeat until-fail:3.

  • guest_freebsd_clocks and guest_kernel_sys pass, 3 of 3. The build has no warnings.
  • Control with main's libkernel/Time/Time.cpp and this PR's tests: both crash, FATAL: unhandled exception 0xc0000005 (guest_freebsd_clocks: ***Exception: SegFault). That's the unchecked pointer access the PR fixes.

AI-assisted: yes (Claude Code)

@oneandonlydean oneandonlydean self-assigned this Oct 26, 2026
@oneandonlydean
oneandonlydean merged commit 7f02cb1 into boykopovar:main Oct 27, 2026
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix error Closes an issue (Fixes #N) libkernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(libkernel): settimeofday and clock_settime crash the host on a pointer into unmapped memory

5 participants