fix(libc): check overlap in strncpy_s and strncat_s #7979

Merged
emirplus merged 2 commits from upstream-pr/7979 into main 2026-10-22 16:27:40 +00:00
emirplus commented 2026-10-19 16:45:55 +00:00 (Migrated from github.com)

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) and strncat_s (K.3.7.2.2). While wcsncpy_s_nid_postfix and wcsncat_s_nid_postfix in core/libs/prx/libc/src/Strings.cpp already detect overlapping destination and source buffers, their narrow counterparts strncpy_s_nid_postfix and strncat_s_nid_postfix omitted this check, leading to undefined behavior with overlapping std::memcpy.

Add overlap detection to strncpy_s_nid_postfix and strncat_s_nid_postfix to null-terminate the destination buffer and return EINVAL (22) on overlap constraint violations, matching the wide string implementations.

Tested

python tools/check_conventions.py --base origin/main
Extended CheckBoundsCheckedFunctions() regression tests in core/libs/tests/GuestStrings.cpp.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4924285457 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7979). Originally posted by [emirplus](https://github.com/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) and `strncat_s` (K.3.7.2.2). While `wcsncpy_s_nid_postfix` and `wcsncat_s_nid_postfix` in `core/libs/prx/libc/src/Strings.cpp` already detect overlapping destination and source buffers, their narrow counterparts `strncpy_s_nid_postfix` and `strncat_s_nid_postfix` omitted this check, leading to undefined behavior with overlapping `std::memcpy`. Add overlap detection to `strncpy_s_nid_postfix` and `strncat_s_nid_postfix` to null-terminate the destination buffer and return `EINVAL` (22) on overlap constraint violations, matching the wide string implementations. ### Tested `python tools/check_conventions.py --base origin/main` Extended `CheckBoundsCheckedFunctions()` regression tests in `core/libs/tests/GuestStrings.cpp`. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-19 17:01:30 +00:00 (Migrated from github.com)

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.

Pull request Shared files not compared
PR 5021 core/libs/prx/libc/src/Strings.cpp
PR 6044 core/libs/tests/GuestStrings.cpp
PR 6879 core/libs/prx/libc/src/Strings.cpp
PR 7201 core/libs/prx/libc/src/Strings.cpp
PR 8965 core/libs/prx/libc/src/Strings.cpp
core/libs/tests/GuestStrings.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6272653337 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7979#issuecomment-6272653337). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-19T17:01:30Z. --- <!-- pr-overlap --> 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. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libc/src/Strings.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestStrings.cpp` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libc/src/Strings.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libc/src/Strings.cpp` | | [PR 8965](https://www.github.com/boykopovar/AnyPS5/pull/8965) | `core/libs/prx/libc/src/Strings.cpp`<br>`core/libs/tests/GuestStrings.cpp` |
oneandonlydean commented 2026-10-20 04:02:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T04:02:43Z.


The new strncat_s case doesn't pass against this change: strncat_s_nid_postfix(catOverlap + 3, 10, catOverlap, 3) returns 0 and appends ("abcdefabc"), so guest_strings stops at that Require.

The check compares the bytes actually copied (dest + used .. + length against src .. + length); here the copy reads catOverlap[0..2] and writes from catOverlap + 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 example strncat_s(buf + 1, 10, buf, 3) with buf = "a").

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6283084189 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7979#issuecomment-6283084189). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T04:02:43Z. --- The new `strncat_s` case doesn't pass against this change: `strncat_s_nid_postfix(catOverlap + 3, 10, catOverlap, 3)` returns 0 and appends ("abcdefabc"), so `guest_strings` stops at that `Require`. The check compares the bytes actually copied (`dest + used` .. `+ length` against `src` .. `+ length`); here the copy reads `catOverlap[0..2]` and writes from `catOverlap + 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 example `strncat_s(buf + 1, 10, buf, 3)` with `buf` = "a").
emirplus commented 2026-10-21 16:40:55 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6321298121 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7979#issuecomment-6321298121). Originally posted by [emirplus](https://github.com/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.
oneandonlydean commented 2026-10-22 01:30:22 +00:00 (Migrated from github.com)

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_strings on 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. CheckSecureStringSizeLimits calls strcpy_s(bytes.data() + 1, RsizeMax, "cd") and strcat_s(..., RsizeMax, "cd"). With destsz = RSIZE_MAX, any source string anywhere above the destination counts as an overlap. On Windows the stack (where bytes lives) 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, so destination < source + length + 1 is 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" at arena + 128, destsz = RSIZE_MAX gives strcpy_s = 22 and strcat_s = 22.

Suggested fix: bound the destination range by what the call actually touches, not by destsz:

  • strncpy_s: source < destination + length + 1
  • strncat_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 destsz and 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6330478660 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7979#issuecomment-6330478660). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-22T01:30:22Z. --- @emirplus thanks for the rebase. The full CI run on 91d8c244b8 fails `guest_strings` on 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`. `CheckSecureStringSizeLimits` calls `strcpy_s(bytes.data() + 1, RsizeMax, "cd")` and `strcat_s(..., RsizeMax, "cd")`. With `destsz = RSIZE_MAX`, any source string anywhere above the destination counts as an overlap. On Windows the stack (where `bytes` lives) 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`, so `destination < source + length + 1` is 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"` at `arena + 128`, `destsz = RSIZE_MAX` gives `strcpy_s = 22` and `strcat_s = 22`. Suggested fix: bound the destination range by what the call actually touches, not by `destsz`: - `strncpy_s`: `source < destination + length + 1` - `strncat_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 `destsz` and 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.
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!7979
No description provided.