fix(libc): check overlap in strncpy_s and strncat_s #7979
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7979"
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 emirplus on 2026-10-19T16:45:55Z.
What
Under C11 Annex K runtime constraints, copying between overlapping objects is prohibited for
strncpy_s(K.3.7.1.4) andstrncat_s(K.3.7.2.2). Whilewcsncpy_s_nid_postfixandwcsncat_s_nid_postfixincore/libs/prx/libc/src/Strings.cppalready detect overlapping destination and source buffers, their narrow counterpartsstrncpy_s_nid_postfixandstrncat_s_nid_postfixomitted this check, leading to undefined behavior with overlappingstd::memcpy.Add overlap detection to
strncpy_s_nid_postfixandstrncat_s_nid_postfixto null-terminate the destination buffer and returnEINVAL(22) on overlap constraint violations, matching the wide string implementations.Tested
python tools/check_conventions.py --base origin/mainExtended
CheckBoundsCheckedFunctions()regression tests incore/libs/tests/GuestStrings.cpp.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T17:01:30Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libc/src/Strings.cppcore/libs/tests/GuestStrings.cppcore/libs/prx/libc/src/Strings.cppcore/libs/prx/libc/src/Strings.cppcore/libs/prx/libc/src/Strings.cppcore/libs/tests/GuestStrings.cppImported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T04:02:43Z.
The new
strncat_scase doesn't pass against this change:strncat_s_nid_postfix(catOverlap + 3, 10, catOverlap, 3)returns 0 and appends ("abcdefabc"), soguest_stringsstops at thatRequire.The check compares the bytes actually copied (
dest + used..+ lengthagainstsrc..+ length); here the copy readscatOverlap[0..2]and writes fromcatOverlap + 6, so those ranges are 6 bytes apart and the check never fires. The test instead expects the overlap of the objects (the destination array[dest, dest + destsz)against the source string), which is how C11 Annex K words the constraint.Which one do you intend? For the object meaning, compare
[dest, dest + destsz)with[src, src + length]in both functions; for the copied-bytes meaning, change the test to a case where the copied ranges overlap (for examplestrncat_s(buf + 1, 10, buf, 3)withbuf= "a").Imported from boykopovar/AnyPS5. Originally posted by emirplus on 2026-10-21T16:40:55Z.
@oneandonlydean Updated to match the C11 Annex K object meaning: both strncpy_s and strncat_s now compare the destination object range [dest, dest + destsz) against the accessed source range [src, src + length + 1), which correctly fires and passes the test case.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-22T01:30:22Z.
@emirplus thanks for the rebase. The full CI run on 91d8c244b8 fails
guest_stringson Windows (Guest string check failed, then abort). Linux passes. I traced it to the new overlap check, and it's partly my fault, because the object-range check I suggested is too wide.Both functions now test
source < destination + destsz.CheckSecureStringSizeLimitscallsstrcpy_s(bytes.data() + 1, RsizeMax, "cd")andstrcat_s(..., RsizeMax, "cd"). Withdestsz = RSIZE_MAX, any source string anywhere above the destination counts as an overlap. On Windows the stack (wherebyteslives) is below the image's read-only data (where"cd"lives), so both calls return 22 instead of 0. On Linux the stack is above.rodata, sodestination < source + length + 1is false and the check never fires. That's why Linux CI passes.I reproduced it with the two functions from your head built standalone: destination at
arena + 1, source"cd"atarena + 128,destsz = RSIZE_MAXgivesstrcpy_s = 22andstrcat_s = 22.Suggested fix: bound the destination range by what the call actually touches, not by
destsz:strncpy_s:source < destination + length + 1strncat_s:source < destination + used + length + 1(the existing string plus the appended bytes and the terminator)With that change, the same standalone build returns 0 for both size-limit copies, and your two new overlap rows still return 22 with
dest[0] == 0.Could you also add a row that catches this on every OS? For example, in one array, put the source above the destination with a large
destszand no real overlap:char buf[16] = {}; std::strcpy(buf + 8, "cd"); Require(strcpy_s_nid_postfix(buf, RsizeMax, buf + 8) == 0 && std::strcmp(buf, "cd") == 0);. It fails on the current head on Linux as well.