diff --git a/core/libs/prx/libSceNKWebKit/src/WtfPrimitives.cpp b/core/libs/prx/libSceNKWebKit/src/WtfPrimitives.cpp index 44d6dd2b894f..0ea0c46d0ad8 100644 --- a/core/libs/prx/libSceNKWebKit/src/WtfPrimitives.cpp +++ b/core/libs/prx/libSceNKWebKit/src/WtfPrimitives.cpp @@ -1,3 +1,4 @@ +#include "prx/libc/include/General.hpp" #include "prx/libc/include/general/VabiMacros.hpp" #include "prx/libSceNKWebKit/include/WtfExports.hpp" #include "prx/libSceIcu/Version.hpp" @@ -22,6 +23,12 @@ const std::array& GetIcuVersion() { return version; } +unsigned char HashCharacter(const char* data, unsigned index) { + unsigned char character = 0; + if (!GuestCopyIn_nid_no_patch(&character, data + index, sizeof(character))) throw std::invalid_argument("unreadable WTF six-character hash"); + return character; +} + } // namespace extern "C" { @@ -49,7 +56,7 @@ unsigned APS5_VABI _ZN3WTF31sixCharacterHashStringToIntegerEPKc(const char* data if (!data) throw std::invalid_argument("null WTF six-character hash"); unsigned hash = 0; for (unsigned i = 0; i < 6; ++i) { - unsigned c = static_cast(data[i]); + unsigned c = HashCharacter(data, i); unsigned value; if (c >= 'A' && c <= 'Z') value = c - 'A'; else if (c >= 'a' && c <= 'z') value = c - 'a' + 26; @@ -57,7 +64,7 @@ unsigned APS5_VABI _ZN3WTF31sixCharacterHashStringToIntegerEPKc(const char* data else throw std::invalid_argument("invalid WTF six-character hash"); hash = hash * 62 + value; } - if (data[6]) throw std::invalid_argument("WTF hash must contain six characters"); + if (HashCharacter(data, 6)) throw std::invalid_argument("WTF hash must contain six characters"); return hash; } diff --git a/core/libs/prx/libSceNKWebKit/tests/Wtf.cpp b/core/libs/prx/libSceNKWebKit/tests/Wtf.cpp index 67fcb482c518..b3b4d7724fd8 100644 --- a/core/libs/prx/libSceNKWebKit/tests/Wtf.cpp +++ b/core/libs/prx/libSceNKWebKit/tests/Wtf.cpp @@ -1414,6 +1414,7 @@ void TestPrimitiveState() { Throws([&] { SixHash("short"); }); Throws([&] { SixHash("AAAAAAA"); }); Throws([&] { SixHash("AAAAA!"); }); + Throws([&] { SixHash(reinterpret_cast(std::uintptr_t{0x10})); }); for (std::uint8_t type = 0; type < 10; ++type) { Check(IsReload(type) == (type == 4 || type == 8 || type == 9)); Check(IsBackForward(type) == (type >= 1 && type <= 3)); diff --git a/docs/dev/debt/changes/fix-nkwebkit-six-character-hash-guest-copy.md b/docs/dev/debt/changes/fix-nkwebkit-six-character-hash-guest-copy.md new file mode 100644 index 000000000000..180e2b2f7f35 --- /dev/null +++ b/docs/dev/debt/changes/fix-nkwebkit-six-character-hash-guest-copy.md @@ -0,0 +1,5 @@ +# libSceNKWebKit + +## add Functional + +- [WTF::sixCharacterHashStringToInteger](../../../core/libs/prx/libSceNKWebKit/src/WtfPrimitives.cpp) (libSceNKWebKit) reads the hash's characters and its terminator one at a time with `GuestCopyIn_nid_no_patch`, stopping at the first invalid one as before, so a string that can't be read throws `std::invalid_argument`, as a null or malformed one does. This is a host-safety fallback; the console's behaviour for an inaccessible string is not measured.