fix(libkernel): fail gettimeofday, getitimer, setitimer and the sceKernel time getters with EFAULT on an unusable pointer #10975
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10975"
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 xXPBSXx on 2026-10-25T11:46:20Z.
What
The six functions compute into a local and copy out with the checked write from #9618 (
GuestCopyOut_nid_no_patch);setitimerreads its value with a new checked read,GuestCopyIn_nid_no_patch. They fail with EFAULT (guest 14, or0x8002000Efrom the sceKernel forms). The order of the existing error checks is unchanged.sceKernelGettimeofdaynow returns0x8002000Einstead of -1 when the copy-out fails.Fixes: #10969
Tested
New
guest_output_pointer_fault: bad pointers1and0xfffffffffffff000throughgettimeofday(both pointers),getitimer,setitimer(value and previous),sceKernelGettimeofdayandsceKernelGettimezoneexpect -1/EFAULT and0x8002000E; a valid pointer still succeeds. Headc8430e739conmain66e6772ca2;git merge-treeagainst currentmaina5a780eaaais clean:guest_output_pointer_fault,guest_fstat_fault,guest_freebsd_clocks,guest_getitimer,guest_itimer,guest_itimer_pending_default,guest_system_configurationand thepublic_headersaudit. WithGeneral.hpp,General.cpp,Signals.cppandTime.cpptaken frommainand the test kept,guest_output_pointer_faultfails with a SegFault.main,guest_output_pointer_faultfails with a SegFault.check_conventions --base upstream/mainpasses.main801d5b9bc1plus the open macOS build PRs, this PR's diff on top, three runs per row;GuestCopyIn_nid_no_patchandGuestCopyOut_nid_no_patchgo throughmach_vm_read_overwriteandmach_vm_writethere):guest_output_pointer_faultpasses with this PR and crashes with a segmentation fault whenGeneral.hpp,General.cpp,Signals.cppandTime.cppare restored from the base;guest_getitimer,guest_itimer,guest_itimer_pending_default,guest_time_conversionandguest_time_extraspass either way, on head78cb208c0c(comment).BUILD_TESTING=ON) on the current head78cb208c0cmerged ontomainc547deaacb:guest_output_pointer_fault_testsbuilds without errors andguest_output_pointer_faultpasses both runs ofctest --repeat until-fail:2; withmain'sGeneral.hpp,General.cpp,Signals.cppandTime.cppand the rest of this head it crashes with0xc0000005. The other tests of this description's set, macOS and a title were not run there (comment).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)AI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T11:48:49Z.
Review: CHANGES_REQUESTED
Requesting changes:
GuestCopyIn_nid_no_patchhas checked-copy implementations for Windows and Linux, but on macOS it falls through to the unconditionalstd::memcpyatcore/libs/prx/libc/src/General.cpp:865-867. The newsetitimerpath calls this helper for the guestvaluepointer. Therefore the PR'ssetitimer_nid_postfix(0, badTimer, ...)regression case will dereference the deliberately unmapped pointer and fault the host on macOS instead of returning-1/EFAULT.GuestCopyOut_nid_no_patchalready has a macOSmach_vm_writepath, but the read helper does not have the corresponding checked read.Please add a checked macOS read (for example using the appropriate Mach VM read API and validating that the full requested byte count was copied), or use an existing cross-platform guest-memory copy primitive. Then run
guest_output_pointer_faulton macOS or provide a focused result showing both invalid source addresses return EFAULT without terminating the process. The current Windows and WSL2 results do not cover this host-specific branch.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T12:36:57Z.
Review: CHANGES_REQUESTED
Requesting one change in the new
GuestCopyIn_nid_no_patchhelper. On Linux, ifprocess_vm_readvfails with anything other thanEFAULT, the helper falls back tostd::memcpy(source, bytes). For an invalid source pointer, anEPERM/ENOSYSresult (for example, if the syscall is blocked or unavailable) therefore dereferences the untrusted pointer and recreates the host crash this PR is meant to prevent. The existingGuestCopyOut_nid_no_patchtreats non-EFAULTsyscall failures as infrastructure errors instead of using an unchecked copy. Please use a similarly safe failure path here (or return failure if that is the intended guest-visible behavior) and cover the non-EFAULTsyscall failure path.I reviewed the exact diff and verified that the existing null-output
getitimerguard remains inGetitimerInto; this finding is only about the Linux copy-in fallback. The PR's Windows and WSL2 runtime results are author-reported; I did not run the full CMake tests locally.Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T13:16:16Z.
Review: COMMENTED
Reviewed head
d69520c794feeaaddcd5cbd409dd738fb6ae4504(base66e6772ca2094b665488d6ca5cbae3f6fb0c5034).git merge-treeonto current main4017310678562c63594d99b8f45db2d68b9aaa6cis clean.What I ran, WSL2 Ubuntu 24.04, GCC 13.3, Release:
guest_output_pointer_fault,guest_fstat_fault,guest_freebsd_clocks,guest_getitimer,guest_itimer,guest_itimer_pending_default,guest_system_configuration,guest_time_extras,guest_kernel_sys_ktimerandpublic_headerspass 10/10. The four timer/fault tests also pass 20/20 with--repeat until-fail:20.check_conventions.pypasses.Signals.cppandTime.cppback to base, test kept.guest_output_pointer_faultdies with SIGSEGV.General.cppfrom4ab1636(thememcpyfallback on a non-EFAULTprocess_vm_readverror). The seccomp child fails at line 59, so that test does catch the EPERM case.releng/11.4sys/kern/kern_time.c.gettimeofdaycopiestpfirst, thentzponly if that worked (L644-652).getitimercheckswhichbefore the copyout (L754, L745).setitimerwithitv == NULLforwards to getitimer (L797-800), and copiesoitvout after the timer is armed (L805-808). Your code matches all of that, and the test pins the armed-then-EFAULT case.One suggestion:
GuestCopyIn_nid_no_patchre-implementsCopyGuestBytesfrom the same file (General.cpp:86-104), which is already a checked read on every host. The copies differ in two places. In#else,GuestCopyIndoes a rawmemcpyand returns true, whileCopyGuestBytesandGuestCopyOutboth throw there (CONTRIBUTING: does what it should or throws). On Windows it doesVirtualQuerythenmemcpy, whileCopyGuestBytesusesReadProcessMemory, which can't race with an unmap between the check and the copy. Keeping your null/overflow guard and delegating works:I tried that locally: same 4 timer/fault tests pass 5/5, including the seccomp child (
CopyGuestBytesthrowsstd::runtime_erroron non-EFAULT too). It also drops about 28 lines.Small notes, not blocking:
setitimer(3, (GuestItimerval*)1, &ok)gives EINVAL here. On FreeBSD the copyin (L803) runs before thewhichcheck inkern_setitimer(L822), so it gives EFAULT. That order is old code, and you said you kept it, so this is just a note. It's now reachable because the bad-pointer path exists.which != 0copyout ofprevious(Signals.cpp:442-445), e.g.setitimer(1, zero, bad)should be EFAULT.*__error_nid_postfix() = 0three times in a row.Limits: Linux only, no Windows or macOS run, no dump.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T13:18:58Z.
Review: CHANGES_REQUESTED
I reviewed the exact head
d69520c794feeaaddcd5cbd409dd738fb6ae4504and the focused test report. The Linux non-EFAULTfallback and macOS unchecked read issues are addressed, and the added seccomp regression covers the former.One remaining correctness issue is the final
#elseinGuestCopyIn_nid_no_patch: it usesstd::memcpyand returns success on every host without a platform-specific checked-copy path. An invalid guest source can still terminate the host there. The repository already has the cross-platform checkedCopyGuestByteshelper; please keep the zero/null/overflow guard and use that helper (or make unsupported hosts fail safely) instead of falling through to rawmemcpy.Please also add the focused
setitimerprevious-value copyout fault case noted in review (validvalue, invalidprevious) if feasible, since it exercises theGuestCopyOutbranch after the timer update. Then rerun the focused test and report which host(s) ran it. I have not run the full CMake suite locally.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T13:36:27Z.
Review: CHANGES_REQUESTED
Thanks for the macOS checked-copy addition and the Linux seccomp regression; the reviewed current diff now has safe host-copy implementations for those cases. One part of the reported issue still appears uncovered: the
getitimerandsetitimerbodies do not change in this PR, andTime.cpponly routesgettimeofday/sceKernelGettimeofdayandsceKernelGettimezonethroughGuestCopyOut. Could you confirm how the badgetitimeroutput andsetitimervalue/previous pointers are made safe on this head, or add the checked copies and regression cases if they remain direct dereferences? The issue specifically lists those pointers; I do not see them covered byguest_output_pointer_faultin the changed test diff.Imported from boykopovar/AnyPS5. Originally posted by AdnanKhan-ak47 on 2026-10-25T13:49:58Z.
Review: APPROVED
Reviewed head
d9e8185cc1on macOS 27.0.1 (Apple silicon), the host nobody else in the thread has.The macOS run everyone asked for is not possible on
main.core/libsdoes not build on macOS at all:GuestAllocations.cpp:26includes the Linux-only<link.h>under a guard that does not exclude Apple, anddocs/dev/BUILD.md:17states the libraries ship for Linux and Windows only. Soguest_output_pointer_faultcannot run here for this or any PR until the libraries are ported; that is out of scope for this fix and should not hold it.What can be verified is the macOS branch itself.
GuestCopyIn_nid_no_patchnow delegates toCopyGuestBytes, whose Apple path ismach_vm_read_overwrite(..., &copied) == KERN_SUCCESS && copied == bytes(General.cpp:98-100), the same code asGuestCopyOut'smach_vm_writecounterpart. A standalone x86_64 binary with that exact function, run under Rosetta, on the issue's two addresses plus null and a valid one:No fault, no partial write. The surrounding
setitimerlogic is host-independent, so this is the host-specific part of the behaviour that the Linux and Windows runs do not cover.On the 13:36 request: on this head the bodies do change.
getitimer_nid_postfixfills a local throughGetitimerIntoand copies out withGuestCopyOut(Signals.cpp),setitimerreadsvaluethroughGuestCopyInand copiespreviousout throughGuestCopyOuton both thewhich != 0andITIMER_REALbranches, andTime.cpproutes the three time getters the same way. ahmadmuj's red check 1 (bodies reverted, test kept, SIGSEGV) covers that these are the lines that matter.Approving. The behaviour and ordering match what I had in the closed #10978, and delegating to
CopyGuestBytesis the better shape; the two small gaps ahmadmuj noted (setitimer(1, zero, bad)for thewhich != 0copy-out, and the copyin-before-whichorder on FreeBSD) are inAdnanKhan-ak47:fix/libkernel-time-efault(GuestItimer.cpp) if you want to lift them, but neither should block this.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T14:27:03Z.
Review: APPROVED
Re-reviewed exact head
c67997b99b. The new test cases covergetitimer(1, bad)andsetitimer(1, zero, bad), including successful calls with valid pointers, closing the previous non-ITIMER_REAL coverage gap. The checked guest read delegates toCopyGuestBytes; the Linux non-EFAULT syscall failure no longer falls through to rawmemcpy, and the new seccomp case covers that failure.Exact-head local checks:
git diff --check,tools/check_conventions.py, and a C++20 syntax check ofGuestOutputPointerFault.cpppass. The PR reports the focused tests passing on Windows and WSL2 Linux; hosted product jobs are skipped, and I did not run the linked suite locally. The macOS library test cannot run becausecore/libsis not currently buildable on macOS; the standalone checked-read result on macOS is author-reported by another reviewer. With those limits stated, the requested correctness and test changes are addressed.Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T15:31:00Z.
Review: APPROVED
Follow-up on my earlier review. Re-checked head
c67997b99bf14ef79e702457f87f732ba62d3e7d(base66e6772ca2094b665488d6ca5cbae3f6fb0c5034);git merge-treeonto current maind2b3ee3bcc8b3724ab953ea7d1b9419e07a38346is clean.What I ran, WSL2 Ubuntu 24.04, GCC 13.3, Release:
GuestCopyIn_nid_no_patchnow keeps the null/overflow guard and returnsCopyGuestBytes(...). That's byte-for-byte the version I tried locally last time.guest_output_pointer_fault,guest_fstat_fault,guest_freebsd_clocks,guest_getitimer,guest_itimer,guest_itimer_pending_default,guest_system_configuration,guest_time_extras,guest_kernel_sys_ktimer,public_headers) with--repeat until-fail:20: 200/200 pass, seccomp child included.check_conventions.pypasses.guest_output_pointer_fault, one mutant at a time:setitimerwhich != 0branch back to*previous = {}: SIGSEGV, so the newsetitimer(1, zero, bad)line catches it.getitimerwriting*output = {}directly forwhich1/2: SIGSEGV, so the newgetitimer(1, bad)line catches it.GuestCopyInback to a rawmemcpy: SIGSEGV onsetitimer(0, bad, ok).Approving. The
setitimer(3, bad)EINVAL-vs-EFAULT order from my last review is still only a note.Limits: Linux only, no Windows or macOS run, no dump.
Imported from boykopovar/AnyPS5. Originally posted by AdnanKhan-ak47 on 2026-10-26T01:01:42Z.
Review: APPROVED
Re-reviewed
080c77547c.git merge-treeofmain49d1f9b07ewith the previously approvedd9e8185cc1produces a tree byte-identical to this head, so the merge commit adds nothing beyond resolving #9682's timezone store:gettimeofdayfillscurrentandzoneas before and copies each out withGuestCopyOut,clock_gettimestill maps theerrno14 path toSceKernelErrorEfault, andsettimeofday'stzwrite goes through the checked copy. Against49d1f9b07ethe PR is the same 6 files (+163/-10) as before. Approval stands.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T05:43:22Z.
Review: CHANGES_REQUESTED
Rechecked merge head
080c77547cf1; the reviewed checked-copy implementation remains sound and the Windows/WSL results are useful. Current upstream main has advanced: this branch is 6 ahead /225 behind14c99b52ac, with main-side changes inGeneral.cpp, libkernel CMake andSignals.cppsince its merge base. The reported focused run is against older14eb2cda22d2. Please refresh onto current main and rerun the focused time/pointer tests on the resulting exact head before merge. This is a current-head integration/validation request, not a reopened code finding.