Merge pull request #22065 from hrydgard/fix-math-serialize-system-audit-bugs

Common audit: Fix math/serialize bugs
This commit is contained in:
Henrik Rydgård authored and GitHub committed 2026-08-10 09:30:51 +02:00
commit 2c4e3e241c
11 files changed
+74 -6

No files matched your search

+4 -1
View File
@@ -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) {
+10
View File
@@ -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;
+5
View File
@@ -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;
+7
View File
@@ -25,6 +25,13 @@ template<class T>
void DoDeque(PointerWrap &p, std::deque<T> &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++)
+7
View File
@@ -25,6 +25,13 @@ template<class T>
void DoList(PointerWrap &p, std::list<T> &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)
+14
View File
@@ -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();
+6
View File
@@ -29,6 +29,12 @@ void DoSet(PointerWrap &p, std::set<T> &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();
+9 -2
View File
@@ -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;
+8
View File
@@ -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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<void()> callback) {
_dbg_assert_(callback != nullptr);
std::lock_guard<std::mutex> 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<void()
}
void OnScreenDisplay::SetFlags(std::string_view id, OSDMessageFlags flags) {
std::lock_guard<std::mutex> guard(mutex_);
for (auto &ent : entries_) {
// protect against dupes.
if (ent.id == id) {
+1
View File
@@ -781,6 +781,7 @@ handleELF:
std::lock_guard<std::mutex> lock(info_->lock);
info_->paramSFO.ReadSFO((const u8 *)paramSFOcontents.data(), paramSFOcontents.size());
info_->ParseParamSFO(info_->fileType);
info_->MarkReadyNoLock(GameInfoFlags::PARAM_SFO);
}
}
+3 -3
View File
@@ -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) {