ucrt: read no further than the count in strncpy, strncat and their _s forms #40

Manually merged
github merged 2 commits from gh-pr/40/head into gh-pr/40/base 2026-11-26 14:43:43 +00:00
Owner

@⁠Repla09 on GitHub · 2026-11-26 11:07 UTC · original
Repla09:fix/ucrt-counted-copy-overread → main · +149 −22 · 2 files · 2 commits · merged 2026-11-26 11:43 UTC

g_ncpy, g_ncat and g_cpy_s took the whole source length before limiting it to the count, so strncpy, strncat, strncpy_s and strncat_s (and the wcs forms) scan the source for a NUL past the count. A counted source needn't have one: copying a fixed-size field with its size is the usual case. When such a field ends where a mapping ends, the native host faults instead of copying.

Now they read at most the count. For _TRUNCATE that's one unit more than fits, which is how a truncation is detected.

Testing

  • counted_copies_read_no_further_than_their_count places an unterminated source right before a no-access page and covers all eight functions, plus _TRUNCATE. Without the fix the test binary dies with SIGBUS; with it, it passes.
  • cargo test --target x86_64-apple-darwin -p victory-hle passes (220 unit, 15 native_probe; the D3D12 probe test skipped, no Metal Shader Converter on this machine).
  • Clippy on x86_64-apple-darwin shows no warnings in string.rs. rustfmt --check and cargo xtask policy pass.
**@⁠Repla09** on GitHub · 2026-11-26 11:07 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/40) `Repla09:fix/ucrt-counted-copy-overread` → `main` · +149 −22 · 2 files · 2 commits · merged 2026-11-26 11:43 UTC `g_ncpy`, `g_ncat` and `g_cpy_s` took the whole source length before limiting it to the count, so `strncpy`, `strncat`, `strncpy_s` and `strncat_s` (and the `wcs` forms) scan the source for a NUL past the count. A counted source needn't have one: copying a fixed-size field with its size is the usual case. When such a field ends where a mapping ends, the native host faults instead of copying. Now they read at most the count. For `_TRUNCATE` that's one unit more than fits, which is how a truncation is detected. ## Testing - `counted_copies_read_no_further_than_their_count` places an unterminated source right before a no-access page and covers all eight functions, plus `_TRUNCATE`. Without the fix the test binary dies with SIGBUS; with it, it passes. - `cargo test --target x86_64-apple-darwin -p victory-hle` passes (220 unit, 15 native_probe; the D3D12 probe test skipped, no Metal Shader Converter on this machine). - Clippy on `x86_64-apple-darwin` shows no warnings in `string.rs`. `rustfmt --check` and `cargo xtask policy` pass.
g_ncpy, g_ncat and g_cpy_s took the whole source string's length before
limiting it to the count, so strncpy, strncat, strncpy_s and strncat_s
(and the wcs forms) scanned the source for a NUL past the count. A
counted source need not have one: a fixed-size field copied with its
size is the usual case. Read that way, a field that ends where a
mapping ends faults on the native host instead of copying.

Read at most the count (for _TRUNCATE, one unit more than fits, which
is how a truncation is told). The new test puts an unterminated source
right before a no-access page; without the fix it dies with SIGBUS.
Review follow-ups for the counted copies:

- strcpy_s, strncpy_s, strcat_s and strncat_s (and the wcs forms) read
  no more than the room left in the destination: that many units
  without a NUL already mean ERANGE, so a count past the room no longer
  scans to the count.
- An error empties the whole destination, as the CRT does: a failed
  strcat_s or strncat_s cleared the unit after the old string and left
  the old string visible.
- The test fills buffers with 0xEE and appends to nonempty strings, so
  it sees each terminator, that a full-count strncpy writes none, and
  that nothing past them is touched. It covers a count past the room
  with a source of four units before the guard page.
- docs/native-host.md notes the rule.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:43:41 +00:00
github manually merged commit d2084ba4c8 into gh-pr/40/base 2026-11-26 14:43:43 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 11:22 UTC · original · requested changes

Thanks for the fix and the guard-page test. The plain strncpy/strncat paths match the C contract now. A few things before merging:

  1. Cap the finite-count _s scans. They should stop at the remaining destination capacity (min(n, size), or the remaining space for concatenation). Today a 4-element destination with a long source still scans all the way to n, though four non-NUL elements already prove ERANGE (ucrt/string.rs:~316).
  2. Clear dst[0] on append overflow. On ERANGE the append path leaves the old string visible. MSVC clears dst[0] before reporting (~347).
  3. Make the test check termination. Use a nonzero sentinel and append to a nonempty destination. The zero-filled buffer currently hides whether concatenation writes its terminator, and whether an exact-count strncpy leaves the next byte alone.
  4. Docs. Please add a short note to the UCRT section of docs/native-host.md (AGENTS.md asks for one).
**@⁠LakshanSivakumar** on GitHub · 2026-11-26 11:22 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/40#pullrequestreview-6073281058) · **requested changes** Thanks for the fix and the guard-page test. The plain `strncpy`/`strncat` paths match the C contract now. A few things before merging: 1. **Cap the finite-count `_s` scans.** They should stop at the remaining destination capacity (`min(n, size)`, or the remaining space for concatenation). Today a 4-element destination with a long source still scans all the way to `n`, though four non-NUL elements already prove `ERANGE` (`ucrt/string.rs:~316`). 2. **Clear `dst[0]` on append overflow.** On `ERANGE` the append path leaves the old string visible. MSVC clears `dst[0]` before reporting (`~347`). 3. **Make the test check termination.** Use a nonzero sentinel and append to a nonempty destination. The zero-filled buffer currently hides whether concatenation writes its terminator, and whether an exact-count `strncpy` leaves the next byte alone. 4. **Docs.** Please add a short note to the UCRT section of `docs/native-host.md` (AGENTS.md asks for one).
Author
Owner

@⁠Repla09 on GitHub · 2026-11-26 11:30 UTC · original

Thanks for the review. All four are in d2084ba: the _s scans stop at the room left (a count past the room with a 4-unit source before the guard page now reads 4, not 100), an error empties the whole destination, the test uses a 0xEE sentinel and nonempty destinations, and native-host.md has a note under the C runtime. Rebased onto main.

**@⁠Repla09** on GitHub · 2026-11-26 11:30 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/40#issuecomment-7190821543) Thanks for the review. All four are in d2084ba: the `_s` scans stop at the room left (a count past the room with a 4-unit source before the guard page now reads 4, not 100), an error empties the whole destination, the test uses a 0xEE sentinel and nonempty destinations, and native-host.md has a note under the C runtime. Rebased onto main.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 11:42 UTC · original · approved these changes

Thanks for addressing all four points. The guard-page and sentinel checks now cover the scan limit, the destination clearing and termination, and the UCRT notes are updated. Rosetta tests, Windows clippy and policy pass merged with main. Merging.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 11:42 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/40#pullrequestreview-6073545595) · **approved these changes** Thanks for addressing all four points. The guard-page and sentinel checks now cover the scan limit, the destination clearing and termination, and the UCRT notes are updated. Rosetta tests, Windows clippy and policy pass merged with main. Merging.
This discussion has been locked. Commenting is limited to contributors.
No description provided.