fix(libkernel): return EFAULT from ktimer_create and ktimer_gettime for unwritable pointers #13964

Merged
FFwLo merged 2 commits from upstream-pr/13964 into main 2026-11-14 05:54:31 +00:00
FFwLo commented 2026-10-31 14:09:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by FFwLo on 2026-10-31T14:09:36Z.


What

ktimer_create and ktimer_gettime wrote the timer id and the remaining time straight through the guest pointer, so an unwritable pointer such as 0x10 crashed the process. FreeBSD 11's sys_ktimer_create and sys_ktimer_gettime write them with copyout and return EFAULT (sys/kern/kern_time.c). Both now write through GuestCopyOut_nid_no_patch and fail with EFAULT (14).

ktimer_create copies the id out before it marks the timer live, so a failed copy leaves no timer behind, as FreeBSD deletes the timer it created when copyout fails. The test checks this: the following creations still get ids 3 to 31.

ktimer_settime is left out because #12281 changes it; its unchecked copyin of value and copyout of oldValue are recorded in the libkernel debt entry.

Tested

Windows 10 x64, WinLibs GCC 15.2.0 (posix-seh, ucrt r7), Release, ctest --test-dir build -R "^guest_kernel_sys_ktimer$":

  • this branch: Passed. 0x10 with ktimer_create and ktimer_gettime returns -1 with errno 14, and the ids 3 to 31 that follow the failed create are unchanged.
  • the same test with main's Ktimer.cpp: SEGFAULT.

Not tested on Linux.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5098264066 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13964). Originally posted by [FFwLo](https://github.com/FFwLo) on 2026-10-31T14:09:36Z. --- ### What `ktimer_create` and `ktimer_gettime` wrote the timer id and the remaining time straight through the guest pointer, so an unwritable pointer such as `0x10` crashed the process. FreeBSD 11's `sys_ktimer_create` and `sys_ktimer_gettime` write them with `copyout` and return `EFAULT` (`sys/kern/kern_time.c`). Both now write through `GuestCopyOut_nid_no_patch` and fail with `EFAULT` (14). `ktimer_create` copies the id out before it marks the timer live, so a failed copy leaves no timer behind, as FreeBSD deletes the timer it created when `copyout` fails. The test checks this: the following creations still get ids 3 to 31. `ktimer_settime` is left out because #12281 changes it; its unchecked `copyin` of `value` and `copyout` of `oldValue` are recorded in the libkernel debt entry. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 10 x64, WinLibs GCC 15.2.0 (posix-seh, ucrt r7), Release, `ctest --test-dir build -R "^guest_kernel_sys_ktimer$"`: - this branch: Passed. `0x10` with `ktimer_create` and `ktimer_gettime` returns `-1` with `errno` 14, and the ids 3 to 31 that follow the failed create are unchanged. - the same test with `main`'s `Ktimer.cpp`: `SEGFAULT`. Not tested on Linux. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [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`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-31 14:21:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T14:21:53Z.

Review: APPROVED


Reviewed — looks good

At head 6c415b7e9b7a0a6634394b946d2c5d0cbd1b7ef9 on base 73f4f7e06e4f210a8c6570e335fd654dc0a86134, both output writes use the guest-safe copy helper, and ktimer_create publishes the timer ID before marking its slot live.

What I ran

Linux x86_64, Python 3.11.2, GCC 12.2.0, Git 2.39.5. I did not build or run C++ tests locally.

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13964
gh pr checks 13964 -R boykopovar/AnyPS5

Results

Check Result
Diff check Passed
Conventions and PR template Passed
Dependency and merge preview Passed
Hosted conventions, intake, labels Passed
Windows GuestKernelSysKtimer test Reported by the author; not independently reproduced
Same test on base Author reports a segmentation fault; not independently reproduced
Precheck output
All conventions checks passed.

Negative control

The author reports that the same test using base Ktimer.cpp segfaults on the invalid pointer. I did not independently run that control.

Sources

Not verified

No local build or C++ test run; Linux behavior was not tested. Hosted product-test jobs were not present in the check snapshot.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5723222049 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13964#pullrequestreview-5723222049). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T14:21:53Z. **Review:** APPROVED --- # Reviewed — looks good At head `6c415b7e9b7a0a6634394b946d2c5d0cbd1b7ef9` on base `73f4f7e06e4f210a8c6570e335fd654dc0a86134`, both output writes use the guest-safe copy helper, and `ktimer_create` publishes the timer ID before marking its slot live. ## What I ran Linux x86_64, Python 3.11.2, GCC 12.2.0, Git 2.39.5. I did not build or run C++ tests locally. ```sh python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13964 gh pr checks 13964 -R boykopovar/AnyPS5 ``` ## Results | Check | Result | |---|---| | Diff check | Passed | | Conventions and PR template | Passed | | Dependency and merge preview | Passed | | Hosted conventions, intake, labels | Passed | | Windows `GuestKernelSysKtimer` test | Reported by the author; not independently reproduced | | Same test on base | Author reports a segmentation fault; not independently reproduced | <details><summary>Precheck output</summary> ```text All conventions checks passed. ``` </details> ## Negative control The author reports that the same test using base `Ktimer.cpp` segfaults on the invalid pointer. I did not independently run that control. ## Sources - [Timer implementation at reviewed head](https://github.com/boykopovar/AnyPS5/blob/6c415b7e9b7a0a6634394b946d2c5d0cbd1b7ef9/core/libs/prx/libkernel/Time/Ktimer.cpp) - [Timer tests at reviewed head](https://github.com/boykopovar/AnyPS5/blob/6c415b7e9b7a0a6634394b946d2c5d0cbd1b7ef9/core/libs/tests/GuestKernelSysKtimer.cpp) ## Not verified No local build or C++ test run; Linux behavior was not tested. Hosted product-test jobs were not present in the check snapshot.
ahmadmuj commented 2026-11-06 09:58:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-11-06T09:58:09Z.

Review: APPROVED


Checked head 2b04038926078057d74fa15d0d265cc36c3214d9, base 73f4f7e06e4f210a8c6570e335fd654dc0a86134.

Linux x86_64: built guest_kernel_sys_ktimer_tests; its focused ctest passes. A separate probe links the actual libkernel_sys, libkernel and libc modules. dladdr confirms the timer and copyout exports.

  • With the second native page protected, create at page_end-2 returns -1/EFAULT. Its writable prefix changes from A5 A5 to 03 00.
  • After both fully protected and cross-page create failures, allocations still return IDs 3 through 31. The next returns -1/EAGAIN; every delete succeeds.
  • Gettime at page_end-16 returns -1/EFAULT after its 16 writable bytes change to zero. A valid read afterward has four zero fields. Delete succeeds; a second delete returns EINVAL.
  • Invalid ID 2 with the boundary destination returns EINVAL, before the output error.

These results match the create rollback and gettime ordering in FreeBSD kern_time.c. Partial output changes are observations, not required behavior.

Sensitivity checks pass: removing create's rollback yields ID4 instead of ID3; ignoring gettime's failed copy incorrectly returns 0. Valid-pointer controls pass under both mutations. Restoring the exact head makes every case pass again.

Conventions pass against main 85e291c12b7ed08fdf83ebfa79e3057934a46ec9; the merge preview is clean. This covers Linux CPU behavior only, with no console dump, concurrent timer deletion, or full current-main build.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5798524153 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13964#pullrequestreview-5798524153). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-11-06T09:58:09Z. **Review:** APPROVED --- Checked head `2b04038926078057d74fa15d0d265cc36c3214d9`, base `73f4f7e06e4f210a8c6570e335fd654dc0a86134`. Linux x86_64: built `guest_kernel_sys_ktimer_tests`; its focused ctest passes. A separate probe links the actual `libkernel_sys`, `libkernel` and `libc` modules. `dladdr` confirms the timer and copyout exports. - With the second native page protected, create at `page_end-2` returns `-1/EFAULT`. Its writable prefix changes from `A5 A5` to `03 00`. - After both fully protected and cross-page create failures, allocations still return IDs 3 through 31. The next returns `-1/EAGAIN`; every delete succeeds. - Gettime at `page_end-16` returns `-1/EFAULT` after its 16 writable bytes change to zero. A valid read afterward has four zero fields. Delete succeeds; a second delete returns `EINVAL`. - Invalid ID 2 with the boundary destination returns `EINVAL`, before the output error. These results match the create rollback and gettime ordering in [FreeBSD kern_time.c](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_time.c#L1159). Partial output changes are observations, not required behavior. Sensitivity checks pass: removing create's rollback yields ID4 instead of ID3; ignoring gettime's failed copy incorrectly returns 0. Valid-pointer controls pass under both mutations. Restoring the exact head makes every case pass again. Conventions pass against main `85e291c12b7ed08fdf83ebfa79e3057934a46ec9`; the merge preview is clean. This covers Linux CPU behavior only, with no console dump, concurrent timer deletion, or full current-main build.
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!13964
No description provided.