fix(libc): reject overlapping secure string ranges #1336
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1336"
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 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
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):
Mutation check — the parent's
Strings.cppwith your tests kept: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
destszmade 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 + 1uincludes the terminator. Withlengthalone, a case where only the terminating byte overlaps would slip through the check.dest + used, notdest— concatenation writes from the current end, sodestwould be wrong in both directions: too broad before the end, and missing the region after it.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
RangesOverlapcall sites are nowstrncpy_s(dest, length + 1u, src, sourceSize),strncat_s(dest + used, length + 1u, src, sourceSize), andmemcpy_s(dest, count, src, count)— the last was already the exact copied region.memmove_scorrectly has no overlap rejection at all, since overlap is the behaviour it exists to permit;strcpy_sandstrcat_sare 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.
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.Pull request closed