fix(libSceIcu): copy lenient UTF-8 conversion and UTF-8 cursor guest pointers safely #22460
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/22460"
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:25Z.
What
Guest pointer safety for the libSceIcu UTF-8 exports that
tools/guest_pointer_audit.py libSceIculisted:UtfConversions.cpp(u_strFromUTF8Lenient): the status is read withGuestCopyIn_nid_no_patchand written back withGuestCopyOut_nid_no_patchonly where the old code assigned it. The source is read in chunks that stop at the next 4 KiB boundary, and only as far as the conversion reaches; with length -1 it is measured through the same chunks. The UTF-16 output collects in a fixed 2048-unit host buffer that is flushed with oneGuestCopyOut_nid_no_patchper chunk. No host allocation depends onsourceLengthorcapacity. Writes happen in the old order (destination units, length, terminator, status), and the status is read again after the length is written, because the oldFinishre-read*errorthere.guest_icu_utf_memorychecks that order with the length and status aliased.Utf8.cpp(utf8_nextCharSafeBody,utf8_prevCharSafeBody): the index is read withGuestCopyIn_nid_no_patchand written back withGuestCopyOut_nid_no_patch. The audit flagged only the index, but the text bytes were read directly too, through theReadhelper. They now come from a small reader that copies at most four bytes per call: forward from the position, or backward ending at it, never past a 4 KiB boundary. So a sequence that ends at a page whose next or previous page is inaccessible is decoded without touching that page, and most calls copy only once.utf8_back1SafeBodysharesPreviousPrefix, so it uses the same reader.Failures follow what each file already does. In
UtfConversions.cppa null status, inaccessible output or overlong source already threw throughNotImplemented_nid_no_patch, and the new paths callNotImplementedAt_nid_no_patch(__func__, __FILE__, __LINE__). An unreadable status or source and an unwritable status, length or destination now throw too. InUtf8.cppinaccessible memory throwsAPS5_INVALID_ARG_EX, as null pointers andutf8_appendCharSafeBody(#22164) already did.GuestCopyIn/GuestCopyOut/GuestWritablerestoreerrnothemselves. Results for valid inputs do not change.One behaviour moved. The old
OutputAccessopened aGuestArena::HostWriteover the wholecapacitybefore converting. On Linux that open never fails, so nothing was checked there, and the new code does not check the whole range up front either. A destination that becomes unwritable partway throws when its chunk is flushed. Earlier chunks, at most 2048 units each, are already written by then. The status and length are checked withGuestWritable_nid_no_patchbefore anything is written. Source and destination still must not overlap, as the debt entry and ICU require.The debt fragment changes two entries. The UTF-8 macro helpers entry now says that unreadable buffers and unreadable or unwritable index pointers throw. The UTF-8 and UTF-16 conversions entry describes the chunked lenient path and its partial-write case.
u_strFromUTF8,u_strFromUTF8WithSub,u_strToUTF8andu_strToUTF8WithSubshareValidate/Length/Finishand still read the source and write outputs in place. The audit does not list them because the dereferences sit in helpers. They are left for a follow-up so this PR stays with the listed functions. No false positives in this set: all pointers are caller (guest) memory.Overlap
python3 tools/pr_overlap.py --names u_strFromUTF8Lenient utf8_nextCharSafeBody utf8_prevCharSafeBody:utf8_nextCharSafeBodyandutf8_prevCharSafeBodyare implemented onmain, andu_strFromUTF8Lenientis reported as a stub onmain(its helpers containNotImplemented_nid_no_patch(). Open: none for all three.python3 tools/pr_overlap.py --branch fix/icu-utf-conversion-guest-pointers: no overlap with open pull requests.Tested
Arch Linux, GCC, Release (
-DBUILD_TESTING=ON).ctest --test-dir build -R icu: 61/61 passed.ctest --test-dir build -R public_headers: 65/65 passed.guest_icu_utf8(GuestPointers):utf8_nextCharSafeBody,utf8_prevCharSafeBodyandutf8_back1SafeBody, and leaves the index unchanged.PROT_NONEpage decodes forward. A sequence right after aPROT_NONEpage decodes backward, with the text pointer inside the protected page andstartpast it.guest_icu_utf8(PageBoundaries): U+00E9, U+20AC and U+1F600 split at every position across two readable pages decode forward (counted and NUL-terminated), backward and withutf8_back1SafeBody. The same prefixes in front of aPROT_NONEpage match host ICU when the length stops at the boundary, and throw without changing the index when the length or a missing terminator reaches the protected page.guest_icu_utf_conversions(LenientGuestPointers,LenientChunks):sourceLength = INT32_MAXwith a small capacity is rejected quickly without reading.PROT_NONEpage convert.INT32_MAXsource and capacity against a few readable bytes throw at the guard page without a host allocation, and leave the outputs unchanged. So do an unterminated source and a destination that crosses the guard page.guest_icu_utf_conversions(LenientPageBoundaries): the same 2-, 3- and 4-byte sequences split at every position across two readable pages convert, counted and NUL-terminated. In front of aPROT_NONEpage, a counted length that stops at the boundary or a terminator just before it gives U+FFFD as for any truncated sequence; a counted length or missing terminator that reaches the protected page throws and leaves the outputs unchanged.BufferOverflowwith an exact prefix and an untouched canary.Utf8.cppandUtfConversions.cppreverted toorigin/mainwith the new tests kept: both test programs SEGFAULT.guest_icu_utf8aborts.guest_icu_utf8aborts.guest_icu_utf_conversionsaborts.byte(position + index):guest_icu_utf_conversionsaborts (lenient sequence crossing an accessible page boundary); withoutLenientPageBoundariesthe test program passed.guest_icu_utf_conversionsaborts (lenient chunked conversion).guest_icu_utf_memoryaborts (positive aliased status preserves terminator canary).python3 tools/guest_pointer_audit.py libSceIcuno longer listsUtf8.cpporUtfConversions.cpp.python3 tools/progress.py --module libSceIcu: 261 done onorigin/main, 262 on this branch.u_strFromUTF8Lenientno longer goes through helpers that containNotImplemented_nid_no_patch(.python3 tools/check_conventions.py --base origin/main: passed.python3 tools/debt_fold.py --check: passed.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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T16:02:10Z.
Review: DISMISSED
Reviewed — changes requested
Changes are required before merge: prevent out-of-bounds access when the lenient converter processes a UTF-8 sequence split across its input chunk boundary.
What I ran
Reviewed head
589353cde553d9d53508d7860a8ea883b74a71e1. Read the prepared description, commits, manifest, complete diff, and all six changed files listed in the review bundle. InspectedUtf8.cppandUtfConversions.cppat the pinned head. No commands or tests were run in this review environment.Results
Findings
Blocking
core/libs/prx/libSceIcu/UtfConversions.cpp:361— The input cache loads only through the current 4 KiB boundary, but the decoder reads each sequence's continuation bytes with repeatedbyte(position + index)calls. If a valid multibyte lead byte is the final byte of a chunk,position + indexfalls outside the cached range and causes a secondGuestCopyInstarting in the next page. When that page is inaccessible, conversion throws even though the sequence may be complete within the declared readable source region only if the range crossed the boundary; more directly, the cache behavior itself reads ahead to the end of a page on the lead-byte access, so it fails immediately when the page ends before the requested source sequence is fully accessible. The stated tests cover sources ending at a guard page but do not establish correct decoding of sequences straddling accessible page boundaries. Preserve incomplete sequence bytes across chunk loads or ensure each sequence is read safely without requiring bytes beyond the source's available range.Non-blocking
None.
Not verified
Tests and runtime behavior were not independently run. The prepared description reports Arch Linux/GCC tests and says Windows was not run. Console behavior is unverified. All changed files in the manifest were read; no changed files were excluded by the manifest.
To get this to a good state
Fix sequence decoding at input chunk boundaries and add coverage for valid multibyte sequences crossing a 4 KiB boundary, including the case where the next page is inaccessible.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T17:23:54Z.
Review: COMMENTED
Reviewed — no blocking findings
The inspected changes handle the described guest pointer and page boundary cases; I did not run the reported checks, so this review does not support a formal approval.
What I ran
Reviewed head
09fd90090af13c01cea01dee9844b6f9cec2b69dusing the prepared review materials andrepo_read.py. No commands or tests were run; reported test results are author supplied.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No build or tests were run. Windows and physical console behavior remain unverified. The description, commits, manifest, and full diff were read. All six changed files were listed as included; the two implementation files and both test files were inspected, along with both changed debt fragments. The review did not independently reproduce the reported checks.
To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T11:10:23Z.
Review: COMMENTED
Built the submitted UTF-8 conversion sources against bundled ICU and ran both exact Linux fixtures.
GuestIcuUtf8andGuestIcuUtfConversionspassed, including chunked conversions, guarded state, and the output prefix and sentinel checks on overflow.AI-assisted: yes.