fix(libSceLibcInternal): copy the time_get, time_put and getopt guest pointers safely #22462
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/22462"
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 domx4q on 2026-11-17T17:06:38Z.
What
time_put<char>::put/time_put<wchar_t>::put(sceLibcInternalTimePut,sceLibcInternalWideTimePut), the widetime_getmembers inTimeGetFields.cppandgetoptdereferenced guest pointer parameters directly. They now use the libSceLibcInternal guest copy pattern (GuestCopyIn_nid_no_patch/GuestCopyOut_nid_no_patch, errno saved and restored,std::invalid_argumenton failure), astime_get::do_date_orderand the_Xp_helpers already do:FacetPut.cpp:sceLibcInternalTimePutandsceLibcInternalWideTimePutread the facet's vtable pointer and itsdo_putslot throughGuestCopyIn.time_get<wchar_t>::_Getint:firstandlastare the same iterator, one host copy is used for both, so the iterator is parsed and written as one object.time_get<wchar_t>::getand_Getfmt:do_getslot are read throughGuestCopyIn, the slot only when a directive needs it, as before._Getfmtformat is not read past its terminator, and an oversizedgetformat range reads only what is matched.getkeeps the state in a host copy. It stores the state before eachdo_getcall and reloads it afterwards._Getfmtreads and writes the state only where it did before.do_get_time,do_get_yearanddo_get_monthname(the shared helpers, so thecharmembers are covered too) copy the state in and out and write the parsedtmfields withGuestCopyOut, still only on success. The widedo_get_monthnamereads the facet's vtable and months table pointer throughGuestCopyIn.time_put<wchar_t>::time_put(size_t)writes the facet header withGuestCopyOut._Getloctxtexports (narrow, wide, andchartable with wide input) load and store both iterators throughGuestCopyIn/GuestCopyOutand read the field table through the same 256-element, page-clamped window as the format, after the null iterator buffer check. The table is not copied into a host vector; the only host allocations are one offset per field and one flag per field, as onmain.getoptreads the argument vector, each option character of the current argument and the option string throughGuestCopyIn. Thestrchrsearch over the option string reads fixed 256-byte chunks that end at 4 KiB page boundaries, and reads no further than the terminator. The sharedArgumenthelper now reads the vector entry throughGuestCopyIntoo, whichgetopt_longalso uses.An unreadable or unwritable pointer throws
std::invalid_argumentand leaves errno unchanged. No host allocation depends on a guest length or count. Results for valid inputs are unchanged.Not changed, because
guest_pointer_audit.pydoes not report them and they are separate work:Peek,Inc,CtypeTraits);stream->localelookup;getopt_long.The months table that the wide
do_get_monthnamereads is the library's own statickWideMonths, which the constructor stores and the vtable check confirms, so the table contents are not guest memory.Tested
Arch Linux, GCC, Release build (
-DBUILD_TESTING=ON).ctest --test-dir build -R '^(guest_libc_internal_time_get_fields|guest_libc_internal_facet_put|guest_getopt)$': passed. New cases use unmapped (0x10) pointers andPROT_NONEguard pages:_Getint's iterators and value;do_get_*'s state andtm; theget/_Getfmtstate; thetime_put<wchar_t>constructor's facet;getopt's argument vector.tmuntouched._Getintaliasing: an aliasedfirst == lastreturns eof|fail.getformat ranges: a format ending at a guard page converts, a range into the guard page throws, and an oversized (1 << 40) range reads only what it matches._Getfmtformats: a format whose terminator is the last byte before a guard page converts, and one without a terminator throws.getoptoption strings: an option string ending at a guard page works, including the scan for an unknown option, and one without a terminator throws.getoptarguments: an argument without a terminator yields its options up to the guard page and then throws.getoptoption arguments: a required option argument past the end of a guarded vector throws.getoptexhausted argument list: after-ab(the last argument, at the end of a page) yieldsaandb, the page is made inaccessible and the next call returns -1 without reading the saved option position._Getloctxttables: for all three exports, a table ending at a guard page matches, a table without a terminator throws, an unmapped table throws without consuming input, and a null iterator buffer returns -2 without reading the table.^(guest_sce_v2_locale|guest_fwide|guest_wide_|guest_wcs|guest_multibyte_locale|guest_locale|guest_rune_locale|guest_codecvt|guest_libc_internal_|guest_ces_wide|guest_getopt): passed.ctest --test-dir build -R public_headers: 65/65 passed.self->vtableinsceLibcInternalTimePut: SegFault._Getintwriting the value unconditionally: fails._Getintalways writinglast: aborts.getnot reloading the state afterdo_get: fails.do_get_*writing thetmfield unconditionally: aborts.getoptoption search without the page cap: aborts.getoptreadingarguments[index]directly: SegFault.getoptreading the saved option position before theoptind >= countcheck: aborts (getopt: unreadable string)._Getloctxtcopying the table before the null iterator buffer check: aborts (unreadable facet table). Wide and mixed_Getloctxtreading the table directly: SegFault.python3 tools/guest_pointer_audit.py libSceLibcInternalno longer lists any function inFacetPut.cpp,TimeGetFields.cpporGetopt.cpp.python3 tools/progress.py --module libSceLibcInternal: 546/1131 on bothmainand this branch.python3 tools/check_conventions.py --base origin/main: passed.python3 tools/debt_fold.py --check: passed. No debt entry describes the old behaviour, so this PR adds no fragment. Thegetoptentry already lists the cases that throw where FreeBSD faults.Overlap
python3 tools/pr_overlap.py --names sceLibcInternalTimePut sceLibcInternalWideTimePut getopt_nid_postfix:getoptis implemented onmain. The twoTimePutnames are exported only by NID. No open pull request touches any of them.python3 tools/pr_overlap.py --branch fix/libcinternal-time-get-put-guest-pointers: no overlap with open pull requests.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)Summary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T17:12:32Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
9e61baf2f6:tools/check_conventions.pypassed.guest_getopt_tests,guest_libc_internal_facet_put_tests,guest_libc_internal_time_get_fields_testsbuilt cleanly.guest_libc_internal_facet_put,guest_getopt,guest_libc_internal_time_get_fields):core/libs/prx/libSceLibcInternal/FacetPut.cpp,core/libs/prx/libSceLibcInternal/Getopt.cpp,core/libs/prx/libSceLibcInternal/TimeGetFields.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T17:39:54Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
817d819ad0:tools/check_conventions.pypassed.guest_getopt_tests,guest_libc_internal_facet_put_tests,guest_libc_internal_time_get_fields_testsbuilt cleanly.guest_libc_internal_facet_put,guest_getopt,guest_libc_internal_time_get_fields):core/libs/prx/libSceLibcInternal/FacetPut.cpp,core/libs/prx/libSceLibcInternal/Getopt.cpp,core/libs/prx/libSceLibcInternal/TimeGetFields.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-18T09:12:58Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this change; the points below are optional.
What I ran
Reviewed head
641ce44a8728c452f94d14362c1cf1fbb580c438from the prepared diff and pinned reader. No tests or builds were run.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
The full contents of
TimeGetFields.cppand the three changed test files were not reviewed.files.jsonwas unavailable; the prepared file listing shows six changed files, all included. Author-reported test and mutation results were not independently verified.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-18T19:57:26Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this change based on the source and tests inspected; the reported test results are author-supplied and were not run during this review.
What I ran
Reviewed head
22a0057829fe82169e37c374dad3456523b0b255using the pinnedrepo_read.pyreader. No tests or builds were run.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No code or test files were excluded by the manifest, but the full text of every changed file was not read. Inspected implementation files:
core/libs/prx/libSceLibcInternal/FacetPut.cpp,core/libs/prx/libSceLibcInternal/Getopt.cpp,core/libs/prx/libSceLibcInternal/TimeGetFields.cpp; test excerpts:core/libs/tests/GuestGetopt.cpp,core/libs/tests/GuestLibcInternalFacetPut.cpp, andcore/libs/tests/GuestLibcInternalTimeGetFields.cpp. Build and test behavior, including the author-reported Windows and Linux cases, was not independently verified.To get this to a good state
Nothing is required. The author-reported tests and mutation checks are useful optional evidence; no findings require changes.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-18T20:18:20Z.
Review: COMMENTED
Reviewed — no blocking findings
The inspected changes address guest pointer access in the time facets and getopt; I found no verified blocking defect, but did not run the reported tests or read every changed test line.
What I ran
Reviewed head
a3a502822506b6c597c71a3c59f1c58480d75c94usingrepo_read.pyto read the prepared review materials and targeted source ranges. No build, test, or merged-preview check was run.Results
Findings
Blocking
None verified.
Non-blocking
None.
Not verified
I did not run the reported tests or independently confirm the author-reported results. The three changed implementation files were read; the three changed test files were only partially read. All six changed files are included in the manifest; no changed files were excluded by rule.
To get this to a good state
Nothing is required. Optional: run the reported test commands and review the remaining test-file changes before treating the review as complete.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T01:26:24Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: avoid reading an option vector entry when
getopthas already determined thatoptind >= count.What I ran
Reviewed head
f216f6e8cbccfa7e9c7af078e15d0fbcdaabeab8from the prepared diff and repository reader. No commands or tests were run. Read all changed lines inFacetPut.cpp,Getopt.cpp,TimeGetFields.cpp,GuestGetopt.cpp,GuestLibcInternalFacetPut.cpp, andGuestLibcInternalTimeGetFields.cpp; the manifest marks all six changed files as included. No changed files were excluded.Results
Findings
Blocking
core/libs/prx/libSceLibcInternal/Getopt.cpp:355: Whenoptind_nid_postfix >= count, this branch intends to return-1, but first it callsReadCharacter(g_shortPlace, ...)at line 349. After a previous call returns an option without consuming the argument,g_shortPlacemay point into that option’s argument; this path can therefore read guest memory even though there are no remaining arguments. With an invalid or inaccessible stale pointer,getoptthrows instead of returning-1. Check exhaustion before dereferencingg_shortPlace(while preserving the existing continuation behavior for valid remaining arguments).Non-blocking
None.
Not verified
No tests, build, CI results, or console behavior were verified. The author reports tests and mutation checks in the PR description; these were not run here. No changed files were excluded.
To get this to a good state
Guard the exhaustion case before reading
g_shortPlace, and add a regression test for callinggetoptagain after the argument count is exhausted while the saved option position is inaccessible. Then rerun the affected getopt tests and build checks.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T12:25:24Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the inspected implementation and tests support merging.
What I ran
Reviewed head
7ea70a79cb5905b99459b0c19c06639c2097db4e. Read the prepared description, commits, diff, manifest, and all changed files’ added lines. Inspected targeted parser, copy helper, andgetoptcaller context using the pinned repository reader. No commands or tests were run.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
Runtime behavior, build results, and the author-reported test and mutation-check results were not independently verified. No files were excluded by the prepared manifest.
To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T19:49:14Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the code and tests inspected support merging.
What I ran
Reviewed head
ccf4950834627714cf5a408e6039a2548fc93034using the prepared review bundle and pinnedrepo_read.pyreader. No build or tests were run during this review; reported test results below are from the PR description.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No tests or build were run independently. Host and console behavior beyond the reviewed code was not verified. The PR description reports its test runs, but those results were not independently confirmed. All six changed files listed in the manifest were read:
FacetPut.cpp,Getopt.cpp,TimeGetFields.cpp,GuestGetopt.cpp,GuestLibcInternalFacetPut.cpp, andGuestLibcInternalTimeGetFields.cpp. No changed files were excluded.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T17:16:00Z.
Review: COMMENTED
Reviewed — comments
Nothing blocks this review, but the full changed-file review is incomplete because the prepared review artifacts do not expose the referenced
files.jsonlisting and I did not read every changed file.What I ran
9fd00d8c2bc552122f0802fb961a498db927906e.Results
Findings
Blocking
None identified in the reviewed ranges.
Non-blocking
None.
Not verified
The prepared directory listing identifies six changed files, all marked included, but the
review/files.jsonartifact was not available through the reader. I read the description, commits, manifest, diff, the fullFacetPut.cppdiff, most of theGetopt.cppdiff, and selectedTimeGetFields.cppsource context. I did not read the complete changed files or all test changes. Build and test claims, Windows behavior, and the remaining changed hunks are not verified.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T04:41:22Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: preserve the parsed
tmfield when the incoming state already contains an error.What I ran
Reviewed head
82cc3a8bd249d83265790c670d56367bec03c5a0using the prepared diff andrepo_read.pyat that head. No tests were run; the reported test results are author supplied.Results
Findings
Blocking
core/libs/prx/libSceLibcInternal/TimeGetFields.cpp:535:GetTimeFieldwrites the parsed hour beforeDoGetTimeImplloads the incoming state. PreviouslyGetIntV2parsed intotime->tm_hourand then ORed the resulting flags into*state, so an incoming nonzero state left the field unchanged. The new code overwrites it even when the caller state is already failed. Load the incoming state before parsing and only store the field when both the incoming state and parse result permit it.Non-blocking
None.
Not verified
Read the prepared
description.md,commits.md,manifest.json, anddiff.patch, plus the pinned source ranges forFacetPut.cpp,Getopt.cpp, andTimeGetFields.cpp. The manifest marks all six changed files as included; full file text was not read. Test source changes were reviewed in the diff only.files.jsonwas not available through the reader, so excluded-file details are not verified. Tests and author reported runs were not independently verified. No merged-preview check, environment check, or console behavior check was run.To get this to a good state
Preserve the existing
tmvalue whenDoGetTimeImplreceives an already failed state, then rerun the reported focused and locale test suites on the updated head.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T04:57:57Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: fix the month parser so a failed parse does not write a derived month value to
tm_mon.What I ran
Reviewed head
602827e2b99168ec06a033d125e8e1b424367d6busing the prepared diff and the pinned repository reader. No commands or tests were run; test results in the PR description are author-reported.Results
Findings
Blocking
core/libs/prx/libSceLibcInternal/TimeGetFields.cpp:418storesmonth - 1even whenGetIntV2setsIosFailBit. The prior implementation only decremented the parsedtm_monfield after theGetIntV2call had updated it; on failed parsing, that left the original field untouched. The new local starts at zero, so failed input overwritestm_monwith-1. Store the converted month only when parsing succeeds.Non-blocking
None.
Not verified
The changed test files
core/libs/tests/GuestGetopt.cpp,core/libs/tests/GuestLibcInternalFacetPut.cpp, andcore/libs/tests/GuestLibcInternalTimeGetFields.cppwere listed in the manifest but not read; tests and builds were not run. The other three changed implementation files—FacetPut.cpp,Getopt.cpp, andTimeGetFields.cpp—were read.files.jsonwas excluded by the prepared bundle; no other exclusions were assessed. No merged-preview check was performed.To get this to a good state
Guard the
tm_monstore inParseDateMonthon successful parsing, then rerun the relevant time-get tests and build on the updated head.Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T15:17:54Z.
Review: COMMENTED
Could
TimePutSlotreject a nullputentry before dispatch? It now validates the reads of the facet and its vtable; a readable table can still contain an empty entry.I ran the branch's
GuestGetoptandGuestLibcInternalTimeGetFieldsfixtures on Linux against existing runtime libraries. Both passed. The integratedguest_libc_internal_facet_putCTest also passed with the patch applied.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-03T15:33:37Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: bound the guest locale table copy so guest data cannot trigger unbounded host allocation.
What I ran
Reviewed head
0454b78103c8c908f52b12c77d29c1199e5335dagainst basemain; no merged preview was run. No build or tests were run. The author reports the listed tests passed, but I did not verify those results.Results
Negative control
Not run; no source mutation or test execution was performed.
Findings
Blocking
core/libs/prx/libSceLibcInternal/TimeGetFields.cpp:115—CopyGuestTableappends every character from the guest table to a growing host vector before parsing. The narrow_Getloctxtentry point passes a guest-controlled table here, so a long mapped table can force large host allocations even when parsing would return early (for example, withnumFields == 0). Copy only the bounded portion needed as parsing proceeds, or enforce a suitable checked limit before allocating.Non-blocking
None.
Sources
_Getloctxtcaller at the reviewed commitNot verified
No tests, builds, or mutation checks were run. The reader did not complete because it attempted to append its read log in the read-only review folder. Read all six changed files listed in
files.json:FacetPut.cpp,Getopt.cpp,TimeGetFields.cpp,GuestGetopt.cpp,GuestLibcInternalFacetPut.cpp, andGuestLibcInternalTimeGetFields.cpp. No files were excluded by the prepared review rules.To get this to a good state
Bound the guest table copy, then rerun the focused
guest_libc_internal_time_get_fieldscheck and review the updated head.Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-12-03T15:39:43Z.
Review: COMMENTED
Actionable comments
_Getloctxtexports read the guest table without checked access.tm_monfirst.getcallback.fa31208e-f260-417e-80c7-f0b3cb74a69eReviewing files that changed from the base of the PR and between
ae71b314d2and0454b78103.core/libs/prx/libSceLibcInternal/FacetPut.cppcore/libs/prx/libSceLibcInternal/Getopt.cppcore/libs/prx/libSceLibcInternal/TimeGetFields.cppcore/libs/tests/GuestGetopt.cppcore/libs/tests/GuestLibcInternalFacetPut.cppcore/libs/tests/GuestLibcInternalTimeGetFields.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T15:39:58Z.
Review: COMMENTED
The updated head rejects an empty
time_put::putentry for both facet types. I reranguest_libc_internal_facet_put,guest_libc_internal_time_get_fields, andguest_getopton Linux with this head applied. All three passed.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-03T15:51:00Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; one optional point is noted below.
What I ran
Reviewed head
4be4aa72539f0e514cb8fc76251e31a35820464e. No commands or tests were run. The prepared diff and repository reader were used; the reader log was disabled because the run folder is read-only.Results
Findings
Blocking
None.
Non-blocking
Getopt.cpp:90copies a full chunk before checking for NUL. The scan stops using bytes at the terminator, but the copy can read further into the same page. This is safe from the page-boundary fault addressed here, though it does not meet the description’s claim that it reads no further than the terminator.Sources
Reviewed the prepared diff and head source for
FacetPut.cpp,Getopt.cpp, andTimeGetFields.cpp, plus changed test sections inGuestGetopt.cpp,GuestLibcInternalFacetPut.cpp, andGuestLibcInternalTimeGetFields.cpp.Not verified
Tests, builds, and runtime behavior were not independently verified. The author-reported test and mutation results were not run during this review. No files were excluded by the prepared review manifest.
To get this to a good state
Nothing is required. Optionally, adjust the chunked string scan if it must avoid copying bytes after the NUL terminator.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-12-03T15:53:49Z.
Review: COMMENTED
Actionable comments
6f1502d4-f9ab-4ad6-a251-801de28283edReviewing files that changed from the base of the PR and between
0454b78103and4be4aa7253.core/libs/prx/libSceLibcInternal/TimeGetFields.cppcore/libs/tests/GuestLibcInternalTimeGetFields.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T16:20:17Z.
Review: COMMENTED
I reran the Linux tests on this head.
guest_getopt,guest_libc_internal_facet_put, andguest_libc_internal_time_get_fieldsall passed.AI-assisted: yes.