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) { 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; 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; 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(); 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; 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) { 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); } } 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) {