fix(libc): reject overlapping secure string ranges #1336

Closed
MarcZX10 wants to merge 2 commits from upstream-pr/1336 into main
MarcZX10 commented 2026-10-07 23:49:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-07T23:49:05Z.


This adds overlap checks to the strncpy_s and strncat_s implementations, covering strcpy_s and strcat_s through their shared wrappers. The checks compare source bytes read with destination bytes written, including the terminating NUL, so actual overlap returns EINVAL while disjoint strings in the same buffer remain valid. memcpy_s now uses the shared range helper without changing its count-based behavior.

Validation: cmake --build build --target guest_string_tests -j2; ctest --test-dir build -R '^guest_(strings|wide_formatting|wcsrtombs_s|collate)$' --repeat until-fail:3 --output-on-failure; python3 tools/check_conventions.py --base upstream/main --head HEAD. Not tested: PS5 hardware or title runtime. Open #1116/#1126 add unrelated helpers at the same top-of-file location, so branches may need a small textual conflict resolution if both changes merge first.

This push and PR are authorized by the user for this reviewed commit.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4779859557 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1336). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-07T23:49:05Z. --- This adds overlap checks to the strncpy_s and strncat_s implementations, covering strcpy_s and strcat_s through their shared wrappers. The checks compare source bytes read with destination bytes written, including the terminating NUL, so actual overlap returns EINVAL while disjoint strings in the same buffer remain valid. memcpy_s now uses the shared range helper without changing its count-based behavior. Validation: cmake --build build --target guest_string_tests -j2; ctest --test-dir build -R '^guest_(strings|wide_formatting|wcsrtombs_s|collate)$' --repeat until-fail:3 --output-on-failure; python3 tools/check_conventions.py --base upstream/main --head HEAD. Not tested: PS5 hardware or title runtime. Open #1116/#1126 add unrelated helpers at the same top-of-file location, so branches may need a small textual conflict resolution if both changes merge first. This push and PR are authorized by the user for this reviewed commit. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Dextroid17 commented 2026-10-08 02:29:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T02:29:34Z.

Review: COMMENTED


Built and ran it, with a bisect to show the new cases bite.

Linux (CachyOS, GCC, Release):

cmake --build build --target guest_string_tests --parallel 16
ctest -R ^guest_strings$                       →  1/1 Passed

Mutation check — the parent's Strings.cpp with your tests kept:

pr-1336   (written region):  1/1 Passed
pr-1336^  (whole buffer):    Guest string check failed   →  Failed

The scope of the overlap is the whole point, and you have it right. C11 K.3.7.1.3 fails when the copy would take place between overlapping objects — the source string and the bytes actually written, not the destination allocation. Passing destsz made the destination range the entire buffer, so a perfectly disjoint copy whose source merely lived later in that same buffer was rejected. That is exactly the two cases you added as successes, and they are the right ones to assert, because they are what the old form got wrong.

Three details the change gets right that a narrower reading would miss:

  • length + 1u includes the terminator. With length alone, a case where only the terminating byte overlaps would slip through the check.
  • For the concat forms the destination region starts at dest + used, not dest — concatenation writes from the current end, so dest would be wrong in both directions: too broad before the end, and missing the region after it.
  • The failure cases assert the return and dest[0] == '\0'. The standard requires the destination to be zeroed when these functions fail, so an error-only assertion would leave half the contract untested. That is the part most test additions omit.

I audited the rest of the file for the same mistake and there is nothing left. The three RangesOverlap call sites are now strncpy_s (dest, length + 1u, src, sourceSize), strncat_s (dest + used, length + 1u, src, sourceSize), and memcpy_s (dest, count, src, count) — the last was already the exact copied region. memmove_s correctly has no overlap rejection at all, since overlap is the behaviour it exists to permit; strcpy_s and strcat_s are four-line bodies delegating to the bounded forms, which is why only two sites carry the check. So no function in this family is still using the buffer-size form.

This is a comment, not an approval — I have no merge rights here.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5450689280 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1336#pullrequestreview-5450689280). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T02:29:34Z. **Review:** COMMENTED --- Built and ran it, with a bisect to show the new cases bite. **Linux (CachyOS, GCC, Release):** ``` cmake --build build --target guest_string_tests --parallel 16 ctest -R ^guest_strings$ → 1/1 Passed ``` **Mutation check** — the parent's `Strings.cpp` with your tests kept: ``` pr-1336 (written region): 1/1 Passed pr-1336^ (whole buffer): Guest string check failed → Failed ``` **The scope of the overlap is the whole point, and you have it right.** C11 K.3.7.1.3 fails when *the copy* would take place between overlapping objects — the source string and the bytes actually written, not the destination allocation. Passing `destsz` made the destination range the entire buffer, so a perfectly disjoint copy whose source merely lived later in that same buffer was rejected. That is exactly the two cases you added as successes, and they are the right ones to assert, because they are what the old form got wrong. Three details the change gets right that a narrower reading would miss: - `length + 1u` includes the terminator. With `length` alone, a case where only the terminating byte overlaps would slip through the check. - For the concat forms the destination region starts at `dest + used`, not `dest` — concatenation writes from the current end, so `dest` would be wrong in both directions: too broad before the end, and missing the region after it. - The failure cases assert the return **and** `dest[0] == '\0'`. The standard requires the destination to be zeroed when these functions fail, so an error-only assertion would leave half the contract untested. That is the part most test additions omit. **I audited the rest of the file for the same mistake and there is nothing left.** The three `RangesOverlap` call sites are now `strncpy_s` `(dest, length + 1u, src, sourceSize)`, `strncat_s` `(dest + used, length + 1u, src, sourceSize)`, and `memcpy_s` `(dest, count, src, count)` — the last was already the exact copied region. `memmove_s` correctly has no overlap rejection at all, since overlap is the behaviour it exists to permit; `strcpy_s` and `strcat_s` are four-line bodies delegating to the bounded forms, which is why only two sites carry the check. So no function in this family is still using the buffer-size form. This is a comment, not an approval — I have no merge rights here. Written with AI assistance.
oneandonlydean commented 2026-10-08 02:44:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-08T02:44:55Z.


Closing: no title is shown to pass overlapping ranges to strcpy_s/strcat_s, and library edge cases go in when a title hits them. If one does, please comment here with the title ID and the log line where it goes wrong.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6051109396 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1336#issuecomment-6051109396). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-08T02:44:55Z. --- Closing: no title is shown to pass overlapping ranges to `strcpy_s`/`strcat_s`, and library edge cases go in when a title hits them. If one does, please comment here with the title ID and the log line where it goes wrong.

Pull request closed

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!1336
No description provided.