fix(libc): throw for 16-bit wide scanf conversions on Linux #3984
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3984"
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 zmeizy on 2026-10-11T20:35:24Z.
What
On Linux,
sscanf,vsscanfandfscanfgive the guest format to glibc. With a wide conversion (%ls,%lc,%l[,%S,%C), glibc stores a 32-bitwchar_tper character into a buffer the guest sized for 16-bit characters. That writes past the end of the buffer:sscanf("ab cd", "%ls", buffer)stores 12 bytes into a 4-characterchar16_tarray.These functions now throw (
NotImplemented_nid_no_patch) on Linux when the format has one of these conversions, instead of corrupting guest memory. Formats without wide conversions are handled as before. Windows is unchanged: it hands%ls,%lcand%l[to the host, whosewchar_tis 16 bits.I did not implement the conversion, because how the console decodes the input into UTF-16 is not settled. The narrow printf family writes UTF-8, while the Windows host widens each byte. The gap is recorded in TechnicalDebt.
Tested
Ubuntu 24.04, GCC 13.3, on
main12c22662.guest_formatting: new Linux-only checks callsscanfwith%ls,%*s %3lc,%l[a-z],%Sand%2$C. Each must throw, with the guest buffer left untouched.mainwithout the change (the buffer is overwritten) and pass with it.guest_scan_count,guest_wide_formatting,guest_wide_stringsandguest_wide_conversionpass.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
https://claude.ai/code/session_01Ha6Tsy62AFUyp9a5Mf7vjA
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-11T21:26:36Z.
Review: COMMENTED
Verified on Linux: the change does what it says for the conversions it names — and the same write is still reachable two ways this diff leaves alone.
Head
d358374amerged ontoupstream/main0a8b1d32— the tip my checkout had; a latergit fetchshows that ref standing 15 commits behind the livemain(c5da8233), so read the run as main-of-yesterday. The API reports the PRMERGEABLE/CLEANagainst today's base.Arms
ctest --test-dir build -R '^guest_formatting$'→1/1 Passed, exit 0.core/libs/prx/libc/src/Formatting.cppchecked out atupstream/main, the new cases kept: exit 134, and the message is the test's own: thrown byRequire(core/libs/tests/Formatting.cpp:20) and terminated inLibcException::DefaultTerminate(core/libs/prx/libc/src/exception/Runtime.cpp:58). So the new cases do fail on the old code.Artifact-level check, not only the test
Built
libson both revisions and called the producedunpatched/libc.prxdirectly from a ctypes harness (the unpatched module still carries symbol names):main:sscanf("ab cd", "%ls", buf)→ returns 1 and writes 32-bit units into a 16-bit guest buffer.what(): scanf 16-bit wide conversion on Linux not implemented.sscanf("ab cd", "%s", buf)→"ab"on both revisions.Finding 1 — the
_svariants still hand the wide conversion to the hostSame harness, against your
libc.prx:byte-for-byte the output
mainproduces: a 32-bitwchar_tper character. A guestchar16_t wide[4](8 bytes) still gets 12 bytes written into it — the corruption this PR exists to stop, through a front door the diff does not cover. The capacity argument does not save it either, because the width is a count of guest characters while glibc writes 32-bit units: capacity 3 still writes the full 12 bytes, and only capacity 2 (where the field is cut to a single character) returns without writing.Route on Linux:
sscanf_s(core/libs/prx/libc/src/Formatting.cpp:375on your tree) andvsscanf_s(:453) go toScanGuest(:62), which builds each directive withHostLength— a no-op outside_WIN32(:52) — and hands it tostd::sscanfat:144. So%lsreaches glibc as the host's%ls, with a guest-sized buffer.Fix, whichever you prefer: call
RejectGuestWideScanat the_sentry points too, or move the call to the top ofScanGuestbehind#ifndef _WIN32— it reads only the namedformatargument, beforenextPointer()/nextCapacity()consume the va_list, so it cannot disturb that walk — or leave them and say so in the TechnicalDebt line, which today reads as if the%lsfamily is closed on Linux.Finding 2 — the reject's skip class is narrower than the one already in the file, so
%'lsslips throughRejectGuestWideScanskips"*0123456789$", while the guard already in this file,HasGuestWideFormat(Formatting.cpp:32), skips"-+ #0'0123456789.*$". The new guard therefore rejects less than the old one on the'flag — and'is not cosmetic here. I compiled both loops' skip classes and swept the conversion set:So
sscanf("ab cd", "%'ls", buf)still corrupts exactly as%lsdoes. One-character fix: reuse the sibling's skip class ("-+ #0'0123456789.*$"), or better, share one helper betweenHasGuestWideFormatandRejectGuestWideScanso the two cannot drift — they already differ in both directions (%l[is caught by yours only,%'lsby the sibling only). For what it is worth, the other flags glibc declined in my probe (% lc,%-lc,%#lc,%.3ls), so'is the actionable one.Finding 3 — scope of the fix, stated precisely
Everything else in this scanf class is covered, and I re-ran that search repo-wide rather than in one directory: outside
ScanGuest, the only sites that hand a guest format to the host scanner areFormatting.cpp:254(patched),:338(patched) and:357(patched); thestd::sscanfcalls at:144and:153are insideScanGuestitself;swscanf_s(ScanningWide.cpp:381) scans through its own wide scanner and never reaches the host; and the remaining hits elsewhere in the tree parse host-side text with fixed literals, not a guest format.Not verified, so it is not claimed
fscanf/vfscanfroute (Formatting.cpp:254) is not exercised by the harness — onlysscanf(which forwards throughvsscanf) andsscanf_swere run, so the diff's third call site is verified by reading, not by running.%2$C) I checked only in the same loop replication and in your test, not by running the built module with a positional format.The harness is
probe_libc_scan.pyin my scratch dir, and the loop/skip-class matrix isrejectmatrix.c— say the word and I will paste either here so you can re-run them.Hermes Agent 🤖 at your service
Imported from boykopovar/AnyPS5. Originally posted by TideOPL on 2026-10-11T22:05:48Z.
Good catch on the 32-bit
wchar_toverflow. One gap and a small note:sscanf_sandvsscanf_sare still affected on Linux. They go throughScanGuest, which builds a host format for each directive ("%" + width + HostLength(length) + specifier), andHostLengthreturnslunchanged on Linux. Sosscanf_s("ab", "%ls", buf, 4)still makes glibc store 32-bitwchar_ts into the 16-bit guest buffer, which is the same overflow this PR fixes forsscanf,vsscanfandfscanf. CallingRejectGuestWideScanthere too, or checkinglength == "l"withs,cor[(plusSandC) insideScanGuest, would close it.RejectGuestWideScandoesn't skip over scanset contents, so a format like"%[^%ls]"is rejected as if it contained%ls. That's harmless in practice; just noting it.I didn't run this one. The change is Linux-only and I'm on Windows, where these paths go to the 16-bit host
wchar_t.AI-assisted: yes. Written with AI assistance (Claude Code); I reviewed it.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T23:19:55Z.
Please cover
sscanf_sandvsscanf_stoo: they go throughScanGuest, whereHostLengthleaveslas it is on Linux, so%lsstill reaches glibc with the guest's 16-bit buffer.RejectGuestWideScanalso needs'in its skip set, asHasGuestWideFormathas, or%'lsgets past it. Dextroid17's review above has the output for both.Imported from boykopovar/AnyPS5. Originally posted by zmeizy on 2026-10-11T23:41:39Z.
should be good now
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T07:38:28Z.
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.
main)docs/dev/TechnicalDebt.mdL625