From 0653b8749757840dd5c6c43198be327c96e4adb9 Mon Sep 17 00:00:00 2001 From: Ethan Barr Date: Tue, 6 Oct 2026 13:57:08 -0700 Subject: [PATCH] fix(libc): reject overlapping memcpy_s ranges --- core/libs/prx/libc/src/Strings.cpp | 7 +++++++ core/libs/tests/GuestStrings.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/core/libs/prx/libc/src/Strings.cpp b/core/libs/prx/libc/src/Strings.cpp index 7c9be2a2e..8cba0d618 100644 --- a/core/libs/prx/libc/src/Strings.cpp +++ b/core/libs/prx/libc/src/Strings.cpp @@ -227,6 +227,13 @@ int APS5_VABI memcpy_s_nid_postfix(void* dest, size_t destsz, const void* src, s std::memset(dest, 0, destsz); return src ? GuestErange : GuestEinval; } + const auto destination = reinterpret_cast(dest); + const auto source = reinterpret_cast(src); + const auto distance = destination < source ? source - destination : destination - source; + if (count != 0 && distance < count) { + std::memset(dest, 0, destsz); + return GuestEinval; + } std::memcpy(dest, src, count); return 0; } diff --git a/core/libs/tests/GuestStrings.cpp b/core/libs/tests/GuestStrings.cpp index 808f69407..145980fed 100644 --- a/core/libs/tests/GuestStrings.cpp +++ b/core/libs/tests/GuestStrings.cpp @@ -34,6 +34,33 @@ static void Require(bool condition) { } } +static bool CheckMemcpyOverlap() { + bool correct = true; + for (const auto offset : {0, 1, -1, 3, -3}) { + unsigned char bytes[16]; + std::memset(bytes, 0x5a, sizeof(bytes)); + auto* destination = bytes + 4; + const auto* source = destination + offset; + const int error = memcpy_s_nid_postfix(destination, 8, source, 4); + bool cleared = true; + for (unsigned i = 4; i < 12; ++i) cleared &= bytes[i] == 0; + const bool matches = error == 22 && cleared && bytes[3] == 0x5a && bytes[12] == 0x5a; + if (!matches) std::fprintf(stderr, "memcpy_s overlap %+d: expected EINVAL 22 and eight zero bytes, received %d\n", offset, error); + correct &= matches; + } + unsigned char adjacent[] = {1, 2, 3, 4, 5, 6, 7, 8}; + Require(memcpy_s_nid_postfix(adjacent, 4, adjacent + 4, 4) == 0); + Require(std::memcmp(adjacent, adjacent + 4, 4) == 0); + const unsigned char original[] = {1, 2, 3, 4, 5, 6, 7, 8}; + std::memcpy(adjacent, original, sizeof(adjacent)); + Require(memcpy_s_nid_postfix(adjacent + 4, 4, adjacent, 4) == 0); + const unsigned char unchanged[] = {1, 2, 3, 4, 1, 2, 3, 4}; + Require(std::memcmp(adjacent, unchanged, sizeof(adjacent)) == 0); + Require(memcpy_s_nid_postfix(adjacent, sizeof(adjacent), adjacent, 0) == 0); + Require(std::memcmp(adjacent, unchanged, sizeof(adjacent)) == 0); + return correct; +} + static void CheckBoundsCheckedFunctions() { char small[4] = "zz"; Require(strcpy_s_nid_postfix(small, sizeof(small), "abc") == 0 && std::strcmp(small, "abc") == 0); @@ -131,4 +158,5 @@ int main() { Require(strcasestr_nid_postfix("", "a") == nullptr); const char highBytes[] = {static_cast(0xff), 'A', 0}; Require(strcasestr_nid_postfix(highBytes, "a") == highBytes + 1); + return CheckMemcpyOverlap() ? 0 : 1; }