From be3b41746186415c0c1fb9717a08b223d0baf241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 9 Aug 2026 15:41:03 +0200 Subject: [PATCH 1/8] expression_parser: bound token length to fix stack buffer overflow initPostfixExpression() filled a fixed char subStr[256] with no bound check while tokenizing numeric literals and identifiers. A 256+ char token in a debugger expression (breakpoint condition, watch, memory.search) - settable over the network via the WebSocket debugger - smashed the stack. Now bails out with an error once the token reaches the buffer size. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01L4QAoxV2KY7ek4PcZw3WvY --- Common/Math/expression_parser.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Common/Math/expression_parser.cpp b/Common/Math/expression_parser.cpp index 553b5b806b..1083e659d1 100644 --- a/Common/Math/expression_parser.cpp +++ b/Common/Math/expression_parser.cpp @@ -244,6 +244,11 @@ bool initPostfixExpression(const char* infix, IExpressionFunctions* funcs, Postf { while (isAlphaNum(infix[infixPos])) { + if (subPos >= (int)sizeof(subStr) - 1) + { + expressionError = "Token too long"; + return false; + } subStr[subPos++] = infix[infixPos++]; } subStr[subPos] = 0; @@ -264,6 +269,11 @@ bool initPostfixExpression(const char* infix, IExpressionFunctions* funcs, Postf { while (isAlphaNum(infix[infixPos])) { + if (subPos >= (int)sizeof(subStr) - 1) + { + expressionError = "Token too long"; + return false; + } subStr[subPos++] = infix[infixPos++]; } subStr[subPos] = 0; From 27dcde7419e9fd36ad9d77fbb7081631ab2b9c1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 9 Aug 2026 15:41:29 +0200 Subject: [PATCH 2/8] Buffer::Printf: fix OOB stack read on vsnprintf truncation When vsnprintf's return value (the would-have-been length) was >= sizeof(buffer), the code logged a truncation warning but then still memcpy'd that full, untruncated length out of the 4096-byte stack buffer, reading past its end. retval is now clamped to what vsnprintf actually wrote before use. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01L4QAoxV2KY7ek4PcZw3WvY --- Common/Buffer.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Common/Buffer.cpp b/Common/Buffer.cpp index a6cd052729..a7e02df8b0 100644 --- a/Common/Buffer.cpp +++ b/Common/Buffer.cpp @@ -103,8 +103,11 @@ void Buffer::Printf(const char *fmt, ...) { va_start(vl, fmt); int retval = vsnprintf(buffer, sizeof(buffer), fmt, vl); if (retval >= (int)sizeof(buffer)) { - // Output was truncated. TODO: Do something. + // Output was truncated. vsnprintf returns the length it would have written, + // but buffer only actually holds sizeof(buffer) - 1 chars (plus the NUL) - + // clamp so we don't copy past what was actually written into it. ERROR_LOG(Log::IO, "Buffer::Printf truncated output"); + retval = (int)sizeof(buffer) - 1; } va_end(vl); if (retval < 0) { From 60796e08f9e31f0c0f4b50463a67516d777a2303 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 9 Aug 2026 15:44:33 +0200 Subject: [PATCH 3/8] Serializer: fix OOB read loading a std::string from a savestate MODE_READ built the string via x = (char*)*p.ptr, which strlen()s for a NUL terminator. CheckRead(stringLen) only guarantees stringLen bytes are available in the buffer, not that one of them is a NUL - a corrupted savestate missing the terminator caused strlen to scan past the checked region. Now uses a length-bounded assign(), matching how the wstring/u16string siblings already do this correctly via memcpy. Also tightens the stringLen validity check to require >= 1 (matching what a real serialized string always has), so stringLen - 1 can't go negative for a corrupted stringLen of 0. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01L4QAoxV2KY7ek4PcZw3WvY --- Common/Serialize/Serializer.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Common/Serialize/Serializer.cpp b/Common/Serialize/Serializer.cpp index f572457c80..7a9b95a5e1 100644 --- a/Common/Serialize/Serializer.cpp +++ b/Common/Serialize/Serializer.cpp @@ -185,7 +185,9 @@ void Do(PointerWrap &p, std::string &x) { int stringLen = (int)x.length() + 1; Do(p, stringLen); - if (stringLen < 0 || stringLen > MAX_SANE_STRING_LENGTH) { + // Must be at least 1 (the NUL terminator alone, for an empty string) so that + // stringLen - 1 below is never negative. + if (stringLen < 1 || stringLen > MAX_SANE_STRING_LENGTH) { WARN_LOG(Log::SaveState, "Savestate failure: bad stringLen %d", stringLen); p.SetError(PointerWrap::ERROR_FAILURE); return; @@ -197,7 +199,12 @@ void Do(PointerWrap &p, std::string &x) { } switch (p.mode) { - case PointerWrap::MODE_READ: x = (char*)*p.ptr; break; + case PointerWrap::MODE_READ: + // Use a length-bounded assign, not the raw-pointer constructor - the latter + // strlen()s for a NUL terminator, which CheckRead() above didn't guarantee + // exists within stringLen bytes for corrupted savestate data. + x.assign((const char *)*p.ptr, stringLen - 1); + break; case PointerWrap::MODE_WRITE: memcpy(*p.ptr, x.c_str(), stringLen); break; case PointerWrap::MODE_MEASURE: break; case PointerWrap::MODE_NOOP: break; From 1eab737e4d9d8515983ade034b906ffb88099c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 9 Aug 2026 15:44:40 +0200 Subject: [PATCH 4/8] Serialize: guard List/Deque/Map/Set against corrupted size fields DoVector already rejects an attacker/corruption-controlled size that would resize far beyond what's actually left in the savestate buffer. DoList/DoDeque/DoMap/DoMultimap/DoSet never got the same treatment - a corrupted count field (e.g. 0xFFFFFFFF) drove an immediate huge resize (list/deque) or an unbounded loop of allocations (map/set) before any per-element bounds checking kicked in. All five now check the declared count against PointerWrap::Remaining() first. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01L4QAoxV2KY7ek4PcZw3WvY --- Common/Serialize/SerializeDeque.h | 7 +++++++ Common/Serialize/SerializeList.h | 7 +++++++ Common/Serialize/SerializeMap.h | 14 ++++++++++++++ Common/Serialize/SerializeSet.h | 6 ++++++ 4 files changed, 34 insertions(+) diff --git a/Common/Serialize/SerializeDeque.h b/Common/Serialize/SerializeDeque.h index d0dc8adbb0..3c9a817884 100644 --- a/Common/Serialize/SerializeDeque.h +++ b/Common/Serialize/SerializeDeque.h @@ -25,6 +25,13 @@ template void DoDeque(PointerWrap &p, std::deque &x, T &default_val) { u32 deq_size = (u32)x.size(); Do(p, deq_size); + // Guard against an attacker-controlled size driving a huge resize, same as DoVector. + if (p.mode == PointerWrap::MODE_READ || p.mode == PointerWrap::MODE_VERIFY) { + if (deq_size > p.Remaining() / sizeof(T)) { + p.SetError(PointerWrap::ERROR_FAILURE); + return; + } + } x.resize(deq_size, default_val); u32 i; for (i = 0; i < deq_size; i++) diff --git a/Common/Serialize/SerializeList.h b/Common/Serialize/SerializeList.h index 2a6fdcefa3..d344152fe8 100644 --- a/Common/Serialize/SerializeList.h +++ b/Common/Serialize/SerializeList.h @@ -25,6 +25,13 @@ template void DoList(PointerWrap &p, std::list &x, T &default_val) { u32 list_size = (u32)x.size(); Do(p, list_size); + // Guard against an attacker-controlled size driving a huge resize, same as DoVector. + if (p.mode == PointerWrap::MODE_READ || p.mode == PointerWrap::MODE_VERIFY) { + if (list_size > p.Remaining() / sizeof(T)) { + p.SetError(PointerWrap::ERROR_FAILURE); + return; + } + } x.resize(list_size, default_val); for (T &elem : x) diff --git a/Common/Serialize/SerializeMap.h b/Common/Serialize/SerializeMap.h index 294266b930..9c085b2cc2 100644 --- a/Common/Serialize/SerializeMap.h +++ b/Common/Serialize/SerializeMap.h @@ -29,6 +29,13 @@ void DoMap(PointerWrap &p, M &x, typename M::mapped_type &default_val) { switch (p.mode) { case PointerWrap::MODE_READ: { + // Guard against an attacker-controlled count driving an enormous number of + // loop iterations/allocations, same spirit as DoVector's guard. + constexpr size_t minElemSize = sizeof(typename M::key_type) + sizeof(typename M::mapped_type); + if (number > p.Remaining() / minElemSize) { + p.SetError(PointerWrap::ERROR_FAILURE); + return; + } x.clear(); while (number > 0) { typename M::key_type first = typename M::key_type(); @@ -100,6 +107,13 @@ void DoMultimap(PointerWrap &p, M &x, typename M::mapped_type &default_val) { switch (p.mode) { case PointerWrap::MODE_READ: { + // Guard against an attacker-controlled count driving an enormous number of + // loop iterations/allocations, same spirit as DoVector's guard. + constexpr size_t minElemSize = sizeof(typename M::key_type) + sizeof(typename M::mapped_type); + if (number > p.Remaining() / minElemSize) { + p.SetError(PointerWrap::ERROR_FAILURE); + return; + } x.clear(); while (number > 0) { typename M::key_type first = typename M::key_type(); diff --git a/Common/Serialize/SerializeSet.h b/Common/Serialize/SerializeSet.h index 286e285778..e555bd53ec 100644 --- a/Common/Serialize/SerializeSet.h +++ b/Common/Serialize/SerializeSet.h @@ -29,6 +29,12 @@ void DoSet(PointerWrap &p, std::set &x) { switch (p.mode) { case PointerWrap::MODE_READ: { + // Guard against an attacker-controlled count driving an enormous number of + // loop iterations/allocations, same spirit as DoVector's guard. + if (number > p.Remaining() / sizeof(T)) { + p.SetError(PointerWrap::ERROR_FAILURE); + return; + } x.clear(); while (number-- > 0) { T it = T(); From 12579e86034442ba20e99a1d04b8921984797b99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 9 Aug 2026 15:45:39 +0200 Subject: [PATCH 5/8] OSD: lock mutex_ in all methods that touch entries_ Update()/Entries()/ClickEntry()/Show()/SetProgressBar()/RemoveProgressBar() locked mutex_ around entries_, but CancelById/ShowAchievementUnlocked/ ShowAchievementProgress/ShowChallengeIndicator/ShowLeaderboardTracker/ ClearAchievementStuff/SetClickCallback/SetFlags didn't. Core/WebServer.cpp runs its own thread and calls SetClickCallback() while the main thread's Update() concurrently erases/iterates the same vector every frame. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01L4QAoxV2KY7ek4PcZw3WvY --- Common/System/OSD.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Common/System/OSD.cpp b/Common/System/OSD.cpp index 9580be3409..cb82a271fc 100644 --- a/Common/System/OSD.cpp +++ b/Common/System/OSD.cpp @@ -116,6 +116,7 @@ void OnScreenDisplay::Show(OSDType type, std::string_view text, std::string_view } void OnScreenDisplay::CancelById(std::string_view id) { + std::lock_guard guard(mutex_); for (auto iter = entries_.begin(); iter != entries_.end();) { if (iter->id == id) { iter = entries_.erase(iter); @@ -138,6 +139,7 @@ void OnScreenDisplay::ShowAchievementUnlocked(int achievementID) { double duration_s = 5.0; + std::lock_guard guard(mutex_); Entry msg{}; msg.numericID = achievementID; msg.type = OSDType::ACHIEVEMENT_UNLOCKED; @@ -149,6 +151,7 @@ void OnScreenDisplay::ShowAchievementUnlocked(int achievementID) { void OnScreenDisplay::ShowAchievementProgress(int achievementID, bool show) { double now = time_now_d(); + std::lock_guard guard(mutex_); // There can only be one of these at a time. for (auto &entry : entries_) { if (entry.type == OSDType::ACHIEVEMENT_PROGRESS) { @@ -182,6 +185,7 @@ void OnScreenDisplay::ShowAchievementProgress(int achievementID, bool show) { void OnScreenDisplay::ShowChallengeIndicator(int achievementID, bool show) { double now = time_now_d(); + std::lock_guard guard(mutex_); for (auto &entry : entries_) { if (entry.numericID == achievementID && entry.type == OSDType::ACHIEVEMENT_CHALLENGE_INDICATOR && !show) { // Hide and eventually delete it. @@ -208,6 +212,7 @@ void OnScreenDisplay::ShowChallengeIndicator(int achievementID, bool show) { void OnScreenDisplay::ShowLeaderboardTracker(int leaderboardTrackerID, std::string_view trackerText, bool show) { // show=true is used both for create and update. double now = time_now_d(); + std::lock_guard guard(mutex_); for (auto &entry : entries_) { if (entry.numericID == leaderboardTrackerID && entry.type == OSDType::LEADERBOARD_TRACKER) { if (show) { @@ -303,6 +308,7 @@ void OnScreenDisplay::RemoveProgressBar(std::string_view id, bool success, float // Fades out everything related to achievements. Should be used on game shutdown. void OnScreenDisplay::ClearAchievementStuff() { double now = time_now_d(); + std::lock_guard guard(mutex_); for (auto &iter : entries_) { switch (iter.type) { case OSDType::ACHIEVEMENT_CHALLENGE_INDICATOR: @@ -321,6 +327,7 @@ void OnScreenDisplay::ClearAchievementStuff() { void OnScreenDisplay::SetClickCallback(std::string_view id, std::function callback) { _dbg_assert_(callback != nullptr); + std::lock_guard guard(mutex_); for (auto &ent : entries_) { // protect against dupes. if (ent.id == id) { @@ -330,6 +337,7 @@ void OnScreenDisplay::SetClickCallback(std::string_view id, std::function guard(mutex_); for (auto &ent : entries_) { // protect against dupes. if (ent.id == id) { From f03f168d9667af99ae4667e925c880d1c9ae36e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 9 Aug 2026 15:46:38 +0200 Subject: [PATCH 6/8] MemArenaWin32: check CreateFileMapping failure GrabMemSpace() ignored a NULL return from CreateFileMapping and returned true anyway, unlike every other platform's arena backend (Darwin, Android, Posix all check and return false on failure). Under memory pressure this let startup proceed to CreateView()/MapViewOfFileEx with an invalid handle instead of failing cleanly. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01L4QAoxV2KY7ek4PcZw3WvY --- Common/MemArenaWin32.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Common/MemArenaWin32.cpp b/Common/MemArenaWin32.cpp index 85ee094ca9..4e8d7f139d 100644 --- a/Common/MemArenaWin32.cpp +++ b/Common/MemArenaWin32.cpp @@ -21,6 +21,7 @@ #include "MemArena.h" #include "CommonWindows.h" +#include "Common/Log.h" // Windows mappings need to be on 64K boundaries, due to Alpha legacy. size_t MemArena::roundup(size_t x) { @@ -31,6 +32,10 @@ size_t MemArena::roundup(size_t x) { bool MemArena::GrabMemSpace(size_t size) { #if !PPSSPP_PLATFORM(UWP) hMemoryMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, (DWORD)(size), NULL); + if (hMemoryMapping == NULL) { + ERROR_LOG(Log::MemMap, "Failed to grab a block of virtual memory (CreateFileMapping failed with error %d)", (int)GetLastError()); + return false; + } GetSystemInfo(&sysInfo); #else hMemoryMapping = 0; From b9f46e2acb8f1f61a628ad15b490126500248951 Mon Sep 17 00:00:00 2001 From: James0124 Date: Sun, 9 Aug 2026 06:54:23 +0900 Subject: [PATCH 7/8] Extracted ISOs: Fix savedata size not showing on the game info screen `PSP_DISC_DIRECTORY` was the only `IdentifiedFileType` with possible `PARAM.SFO` that didn't mark `PARAM_SFO` ready inside the switch, causing `GetSaveDataDirectories` to bail out and return an empty vector and the game info UI to show no savedata size info. --- UI/GameInfoCache.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/UI/GameInfoCache.cpp b/UI/GameInfoCache.cpp index a9b001cff7..480c966a09 100644 --- a/UI/GameInfoCache.cpp +++ b/UI/GameInfoCache.cpp @@ -781,6 +781,7 @@ handleELF: std::lock_guard lock(info_->lock); info_->paramSFO.ReadSFO((const u8 *)paramSFOcontents.data(), paramSFOcontents.size()); info_->ParseParamSFO(info_->fileType); + info_->MarkReadyNoLock(GameInfoFlags::PARAM_SFO); } } From f38cbfd7b1a49139a80b366c7780d4531d633ef9 Mon Sep 17 00:00:00 2001 From: James0124 Date: Sun, 9 Aug 2026 07:32:23 +0900 Subject: [PATCH 8/8] Show the (fallback) title on the game info screen even when there's no region ID The fallback title was not shown on the game info screen when `PARAM.SFO` was missing. Hoist the `TextView` creation from the `!regionID.empty()` branch. --- UI/GameScreen.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/UI/GameScreen.cpp b/UI/GameScreen.cpp index 8f46736091..b654674330 100644 --- a/UI/GameScreen.cpp +++ b/UI/GameScreen.cpp @@ -330,12 +330,12 @@ void GameScreen::CreateContentViews(UI::ViewGroup *parent) { std::string title = info_->GetTitle(); if (knownFlags_ & GameInfoFlags::PARAM_SFO) { + TextView* tvTitle = mainGameInfo->Add(new TextView(title, ALIGN_LEFT | FLAG_WRAP_TEXT, false, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT))); + tvTitle->SetShadow(true); + std::string regionID = ReplaceAll(info_->id_version, "_", " v"); if (!regionID.empty()) { regionID += ": "; - - TextView *tvTitle = mainGameInfo->Add(new TextView(title, ALIGN_LEFT | FLAG_WRAP_TEXT, false, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT))); - tvTitle->SetShadow(true); } if (info_->region != GameRegion::UNKNOWN) {