Skip to content

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

Closed
MarcZX10 wants to merge 2 commits into
boykopovar:mainfrom
MarcZX10:fix/libc-secure-string-overlap
Closed

MarcZX10 wants to merge 2 commits into
boykopovar:mainfrom
MarcZX10:fix/libc-secure-string-overlap

Conversation

@MarcZX10

@MarcZX10 MarcZX10 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

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

@github-actions github-actions Bot added the libc label Oct 8, 2026

@Dextroid17 Dextroid17 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants