Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions core/libs/prx/libc/src/StringExtensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ long long APS5_VABI strtonum_nid_postfix(const char* text, long long minimum, lo
return 0;
};
if (minimum > maximum) return fail("invalid", 22);
constexpr int NameTooLong = 63;
std::string input;
for (std::size_t capacity = 64;; capacity *= 2) {
const int copyError = CopyGuestString_nid_no_patch(text, capacity, input);
if (copyError == 0) break;
if (copyError != NameTooLong) return fail("invalid", copyError);
if (capacity > input.max_size() / 2) return fail("invalid", ENOMEM);
}
errno = 0;
char* end = nullptr;
const long long value = std::strtoll(text, &end, 10);
if (errno == EINVAL || end == text || *end != '\0') return fail("invalid", 22);
const char* start = input.c_str();
const long long value = std::strtoll(start, &end, 10);
if (errno == EINVAL || end == start || *end != '\0') return fail("invalid", 22);
if ((value == LLONG_MIN && errno == ERANGE) || value < minimum) return fail("too small", 34);
if ((value == LLONG_MAX && errno == ERANGE) || value > maximum) return fail("too large", 34);
if (error != nullptr) {
Expand Down
8 changes: 8 additions & 0 deletions core/libs/tests/GuestStrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,12 @@ static void CheckStrtonum() {
fails("9223372036854775808", LLONG_MIN, LLONG_MAX, "too large", 34);
fails("-9999999999999999999999999999999999", LLONG_MIN, LLONG_MAX, "too small", 34);
fails("9999999999999999999999999999999999", LLONG_MIN, LLONG_MAX, "too large", 34);
fails(nullptr, 0, 100, "invalid", 14);
const auto paddedNumber = std::string(8192, '0') + "42";
*__error_nid_postfix() = 34;
errno = 34;
Require(strtonum_nid_postfix("42", 0, 100, nullptr) == 42 && *__error_nid_postfix() == 34 && errno == 34);
Require(strtonum_nid_postfix(paddedNumber.c_str(), 0, 100, nullptr) == 42 && errno == 34);
std::size_t page = 0;
char* memory = nullptr;
#ifdef _WIN32
Expand All @@ -393,6 +396,11 @@ static void CheckStrtonum() {
Require(memory != MAP_FAILED);
Require(mprotect(memory + page, page, PROT_NONE) == 0);
#endif
char* edgeNumber = memory + page - 3;
std::memcpy(edgeNumber, "42", 3);
Require(strtonum_nid_postfix(edgeNumber, 0, 100, nullptr) == 42);
edgeNumber[2] = '0';
fails(edgeNumber, 0, 100, "invalid", 14);
const std::array<const char**, 2> badErrors = {
reinterpret_cast<const char**>(std::uintptr_t{0x10}),
reinterpret_cast<const char**>(memory + page)
Expand Down
5 changes: 5 additions & 0 deletions docs/dev/debt/changes/fix-libc-strtonum-guest-input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# libc

## add Functional

- [strtonum input pointer](../../../core/libs/prx/libc/src/StringExtensions.cpp) (libc) - copies the input through checked guest reads before parsing. A null, unreadable or unterminated input followed by unreadable memory reports `EFAULT` and the existing `invalid` error string. The copy grows as needed and allocation failure reports `ENOMEM`. Console behavior for invalid input memory is unverified.
Loading