fix(libc): safely probe the buffer of gets_s #20587

Merged
milescoviello merged 4 commits from upstream-pr/20587 into main 2026-11-26 15:23:34 +00:00
milescoviello commented 2026-11-15 10:33:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-15T10:33:17Z.


What

gets_s stored 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) that gets_s already 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 under docs/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, input short\nunmapped\nx\nexactly8\nrest):

    • gets_s into 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;
    • the same with a one-byte unmapped buffer on the line x (nothing fits, so before this fix it reported "short buffer"), and with an unmapped buffer at end of file;
    • new valid-pointer check: after the short-buffer violation on exactly8, bytes 1-6 still hold xactly behind 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.cpp and this test, guest_bounds_checking segfaults.

  • tools/check_conventions.py passes on the branch and this body; tools/debt_fold.py --check passes with this fragment and #20526's applied to main.

  • maskym's Windows run of c7d1036ba5 merged alone into main 33cbb0b388 (Windows 11, i7-12700K, WinLibs GCC 15.2.0, Release): guest_bounds_checking passes 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 parent c7d1036ba5 merged onto main 403fbe6fc1): guest_bounds_checking passes in 3 of 3 runs, with main's BoundsChecking.cpp it 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). ede0079fb9 adds their probe: a line that fits, read into an unwritable buffer, must report the bad argument violation. Linux (Ubuntu 26.04.1, GCC 15.2, Release), the head merged onto main adacf0bd83: guest_bounds_checking passes in 3 of 3 runs, and with a failed line copy ignored it aborts.

  • Helldiverr's Windows re-check at ede0079fb9, merged onto main 95702cd8d8: guest_bounds_checking passes in 3 of 3 runs, and their M1 (a failed line copy ignored) now fails at the new case.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #20526
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5321833849 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20587). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-15T10:33:17Z. --- ### What `gets_s` stored 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) that `gets_s` already 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 under `docs/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`, input `short\nunmapped\nx\nexactly8\nrest`): - `gets_s` into 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; - the same with a one-byte unmapped buffer on the line `x` (nothing fits, so before this fix it reported "short buffer"), and with an unmapped buffer at end of file; - new valid-pointer check: after the short-buffer violation on `exactly8`, bytes 1-6 still hold `xactly` behind 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.cpp` and this test, `guest_bounds_checking` segfaults. - `tools/check_conventions.py` passes on the branch and this body; `tools/debt_fold.py --check` passes with this fragment and #20526's applied to `main`. - maskym's [Windows run](https://github.com/boykopovar/AnyPS5/pull/20587#issuecomment-6961609065) of `c7d1036ba5` merged alone into `main` `33cbb0b388` (Windows 11, i7-12700K, WinLibs GCC 15.2.0, Release): `guest_bounds_checking` passes 3 of 3 runs. **11-24, `ede0079fb9`: test pin from Helldiverr's run.** Helldiverr's [Windows run](https://github.com/boykopovar/AnyPS5/pull/20587#issuecomment-7131571209) (Ryzen 9 9950X3D, Windows 11 Pro 26300, WinLibs GCC 15.2, this head's parent `c7d1036ba5` merged onto `main` `403fbe6fc1`): `guest_bounds_checking` passes in 3 of 3 runs, with `main`'s `BoundsChecking.cpp` it 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). `ede0079fb9` adds their probe: a line that fits, read into an unwritable buffer, must report the `bad argument` violation. Linux (Ubuntu 26.04.1, GCC 15.2, Release), the head merged onto `main` `adacf0bd83`: `guest_bounds_checking` passes in 3 of 3 runs, and with a failed line copy ignored it aborts. - Helldiverr's [Windows re-check](https://github.com/boykopovar/AnyPS5/pull/20587#issuecomment-7144621604) at `ede0079fb9`, merged onto `main` `95702cd8d8`: `guest_bounds_checking` passes in 3 of 3 runs, and their M1 (a failed line copy ignored) now fails at the new case. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #20526 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-11-15 11:55:53 +00:00 (Migrated from github.com)

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_s case when probing an unwritable buffer.

What I ran

Reviewed head add7dbdc36257fe191664383171e2d34de19f3ef using repo_read.py. No commands or tests were run.

Results

Check Result Note
Diff and changed code passed Four changed files read
Tests not run No test command run

Findings

Blocking

  • core/libs/prx/libc/src/BoundsChecking.cpp:294: When size == 1 and input contains a non-newline character, line is empty and this calls GuestCopyOut_nid_no_patch with a zero-byte copy. That cannot probe whether buffer is writable; the code then reports short buffer, and line 298 ignores the failed terminator write. An invalid buffer therefore escapes the promised bad argument handling 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, and docs/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_checking test.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5910532391 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20587#pullrequestreview-5910532391). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-15T11:55:53Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested Changes are required before merge: handle the size-one `gets_s` case when probing an unwritable buffer. ## What I ran Reviewed head `add7dbdc36257fe191664383171e2d34de19f3ef` using `repo_read.py`. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | Four changed files read | | Tests | not run | No test command run | ## Findings ### Blocking - `core/libs/prx/libc/src/BoundsChecking.cpp:294`: When `size == 1` and input contains a non-newline character, `line` is empty and this calls `GuestCopyOut_nid_no_patch` with a zero-byte copy. That cannot probe whether `buffer` is writable; the code then reports `short buffer`, and line 298 ignores the failed terminator write. An invalid buffer therefore escapes the promised `bad argument` handling 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`, and `docs/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_checking` test.
MarcZX10 commented 2026-11-15 16:54:36 +00:00 (Migrated from github.com)

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 c7d1036ba57641bcd88580008e1cb189d1889d71 using repo_read.py. No commands or tests were run.

Results

Check Result Note
Changed code review passed All changed files were read
Automated checks not run No execution environment available
Negative control not run Not independently verified

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, and docs/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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5911811686 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20587#pullrequestreview-5911811686). Originally posted by [MarcZX10](https://github.com/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 `c7d1036ba57641bcd88580008e1cb189d1889d71` using `repo_read.py`. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | All changed files were read | | Automated checks | not run | No execution environment available | | Negative control | not run | Not independently verified | ## 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`, and `docs/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.
MarcZX10 commented 2026-11-25 01:50:15 +00:00 (Migrated from github.com)

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_patch and the constraint handler. No build or tests were run.

Results

Check Result Note
Diff and changed files passed All three changed files read
Tests and build not run Results in the PR description are author-reported
Cross-platform behavior not run No local execution

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 reviewed in the manifest were read.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6050395124 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20587#pullrequestreview-6050395124). Originally posted by [MarcZX10](https://github.com/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_patch` and the constraint handler. No build or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff and changed files | passed | All three changed files read | | Tests and build | not run | Results in the PR description are author-reported | | Cross-platform behavior | not run | No local execution | ## 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 `reviewed` in the manifest were read. ## To get this to a good state Nothing is required.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!20587
No description provided.