fix(libc): safely probe the buffer of gets_s #20587
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/20587"
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 milescoviello on 2026-11-15T10:33:17Z.
What
gets_sstored each character it read from stdin, and the terminator, straight into the guest buffer. So a non-null buffer pointing at unmapped memory faulted the host after it had already taken a character from the stream.The line is now read into a host string and written to the guest buffer with its terminator in one
GuestCopyOut_nid_no_patch. On a line that is too long, the characters read so far are written with the first byte already cleared, in one copy of at least one byte, so a one-byte buffer is probed too. An unwritable buffer, including a one-byte buffer and the terminator stored at end of file, is reported as the "bad argument" runtime-constraint violation (EINVAL) thatgets_salready raises for a null buffer. The rest of the line is then read and discarded, as on its other violations, so the next read starts on the next line. The line, short-buffer (ERANGE), end-of-file and zero-size results for valid buffers, and the buffer contents they leave, are unchanged.The first version wrote each character with its own
GuestCopyOut. #20589 showed each one costs about 330 ns on Linux, so this one writes the line once. Stacked on #20526 (same file). The debt entry goes in a fragment underdocs/dev/debt/changes/and records this as a host-safety fallback; console behaviour there is not measured.Tested
Linux (Ubuntu 26.04.1), GCC 15.2, Release.
guest_bounds_checking(CheckGets, inputshort\nunmapped\nx\nexactly8\nrest):gets_sinto an unmapped buffer (0x10) returns null and raises one "gets_s: bad argument" violation with error 22, and the next call reads past the whole "unmapped" line;x(nothing fits, so before this fix it reported "short buffer"), and with an unmapped buffer at end of file;exactly8, bytes 1-6 still holdxactlybehind the cleared first byte. This also passes with the per-character version, so the batching keeps the old contents.The existing line, short-buffer, discard-rest, end-of-file, zero-size and null-buffer cases still pass.
Negative control: with #20526's
BoundsChecking.cppand this test,guest_bounds_checkingsegfaults.tools/check_conventions.pypasses on the branch and this body;tools/debt_fold.py --checkpasses with this fragment and #20526's applied tomain.maskym's Windows run of
c7d1036ba5merged alone intomain33cbb0b388(Windows 11, i7-12700K, WinLibs GCC 15.2.0, Release):guest_bounds_checkingpasses 3 of 3 runs.11-24,
ede0079fb9: test pin from Helldiverr's run. Helldiverr's Windows run (Ryzen 9 9950X3D, Windows 11 Pro 26300, WinLibs GCC 15.2, this head's parentc7d1036ba5merged ontomain403fbe6fc1):guest_bounds_checkingpasses in 3 of 3 runs, withmain'sBoundsChecking.cppit segfaults, and 7 of 8 mutations are caught. The survivor: no unwritable-buffer call read a line that fits, so ignoring a failed copy of a fitting line still passed (the first unmapped call took the short-buffer path, not the line copy as this description said).ede0079fb9adds their probe: a line that fits, read into an unwritable buffer, must report thebad argumentviolation. Linux (Ubuntu 26.04.1, GCC 15.2, Release), the head merged ontomainadacf0bd83:guest_bounds_checkingpasses in 3 of 3 runs, and with a failed line copy ignored it aborts.ede0079fb9, merged ontomain95702cd8d8:guest_bounds_checkingpasses in 3 of 3 runs, and their M1 (a failed line copy ignored) now fails at the new case.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-15T11:55:53Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: handle the size-one
gets_scase when probing an unwritable buffer.What I ran
Reviewed head
add7dbdc36257fe191664383171e2d34de19f3efusingrepo_read.py. No commands or tests were run.Results
Findings
Blocking
core/libs/prx/libc/src/BoundsChecking.cpp:294: Whensize == 1and input contains a non-newline character,lineis empty and this callsGuestCopyOut_nid_no_patchwith a zero-byte copy. That cannot probe whetherbufferis writable; the code then reportsshort buffer, and line 298 ignores the failed terminator write. An invalid buffer therefore escapes the promisedbad argumenthandling for this input. Probe/write the terminator and check its result before reporting the short-buffer violation.Non-blocking
None.
Not verified
Tests, build, and console behavior were not run. Read all changed files listed in
manifest.json:core/libs/prx/libc/src/BoundsChecking.cpp,core/libs/tests/GuestBoundsChecking.cpp,docs/dev/debt/changes/fix-libc-probe-gets-s-buffer.md, anddocs/dev/debt/changes/fix-libc-probe-mbstowcs-s-outputs.md. No changed files were excluded.To get this to a good state
Handle the size-one unwritable-buffer case and add coverage for it; rerun the relevant
guest_bounds_checkingtest.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-15T16:54:36Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this change based on the code reviewed; the checks were not run.
What I ran
Reviewed head
c7d1036ba57641bcd88580008e1cb189d1889d71usingrepo_read.py. No commands or tests were run.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
The reported Ubuntu/GCC test results and negative control were not independently run. Console behavior is also unmeasured, as noted in the change description. Files read:
core/libs/prx/libc/src/BoundsChecking.cpp,core/libs/tests/GuestBoundsChecking.cpp, anddocs/dev/debt/changes/fix-libc-probe-gets-s-buffer.md. All three changed files listed in the manifest were included and read; no files were excluded.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T01:50:15Z.
Review: APPROVED
Reviewed — looks good
The changed code and all three changed files were reviewed at head
ede0079fb975f066f96df7e9fd020e7a716d0686; no concrete blocking defect was found, but this review did not run the reported checks.What I ran
Read the prepared PR description, commits, diff, manifest, and full text of every changed file. Read targeted context for
GuestCopyOut_nid_no_patchand the constraint handler. No build or tests were run.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No commands, build, or tests were run. Linux and Windows results and the negative-control results in the description were not independently verified. No changed files were excluded; all three files marked
reviewedin the manifest were read.To get this to a good state
Nothing is required.