From 0eef5d0164e1e435e94a44d7536bb4f1b33da80a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 1 Aug 2026 11:51:10 +0200 Subject: [PATCH] Use shared HasPathTraversal utility for savedata name validation Replace the inline lambdas in the savedata dialog with the new HasPathTraversal() helper in Core/Util/PathUtil. --- Core/Dialog/PSPSaveDialog.cpp | 21 ++++++--------------- Core/Dialog/SavedataParam.cpp | 10 ++-------- Core/Util/PathUtil.cpp | 6 ++++++ Core/Util/PathUtil.h | 5 +++++ 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/Core/Dialog/PSPSaveDialog.cpp b/Core/Dialog/PSPSaveDialog.cpp index 6ac1da55e2..05700dfd8c 100755 --- a/Core/Dialog/PSPSaveDialog.cpp +++ b/Core/Dialog/PSPSaveDialog.cpp @@ -29,6 +29,7 @@ #include "Common/Thread/ThreadUtil.h" #include "Core/Dialog/PSPSaveDialog.h" #include "Core/FileSystems/MetaFileSystem.h" +#include "Core/Util/PathUtil.h" #include "Core/Util/PPGeDraw.h" #include "Common/TimeUtil.h" #include "Core/HLE/sceCtrl.h" @@ -137,21 +138,11 @@ int PSPSaveDialog::Init(int paramAddr) { Memory::Memcpy(&originalRequest, requestAddr, size); // gameName/saveName/fileName become parts of host filesystem paths. - // Reject path separators (either direction) and bare dot components so a - // crafted request can't escape the save directory (path traversal). - auto hasPathTraversal = [](const char *field, size_t fieldSize) { - size_t len = 0; - while (len < fieldSize && field[len] != 0) - len++; - for (size_t i = 0; i < len; i++) { - if (field[i] == '/' || field[i] == '\\') - return true; - } - return (len == 1 && field[0] == '.') || (len == 2 && field[0] == '.' && field[1] == '.'); - }; - if (hasPathTraversal(request.gameName, sizeof(request.gameName)) || - hasPathTraversal(request.saveName, sizeof(request.saveName)) || - hasPathTraversal(request.fileName, sizeof(request.fileName))) { + // Reject path separators and bare dot components so a crafted request + // can't escape the save directory (path traversal). + if (HasPathTraversal(StringViewFromFixedSizeField(request.gameName)) || + HasPathTraversal(StringViewFromFixedSizeField(request.saveName)) || + HasPathTraversal(StringViewFromFixedSizeField(request.fileName))) { ERROR_LOG_REPORT(Log::sceUtility, "sceUtilitySavedataInitStart: path separator in name fields"); return SCE_ERROR_UTILITY_INVALID_PARAM_SIZE; } diff --git a/Core/Dialog/SavedataParam.cpp b/Core/Dialog/SavedataParam.cpp index 1a7610d40c..fefa93de86 100644 --- a/Core/Dialog/SavedataParam.cpp +++ b/Core/Dialog/SavedataParam.cpp @@ -30,6 +30,7 @@ #include "Core/Dialog/SavedataParam.h" #include "Core/Dialog/PSPSaveDialog.h" #include "Core/FileSystems/MetaFileSystem.h" +#include "Core/Util/PathUtil.h" #include "Core/HLE/sceIo.h" #include "Core/HLE/sceKernelMemory.h" #include "Core/HLE/sceChnnlsv.h" @@ -1550,14 +1551,7 @@ int SavedataParam::SetPspParam(SceUtilitySavedataParam *param) { // saveName entries become part of host filesystem paths; reject // path separators and bare dot components (path traversal). const std::string_view entry = StringViewFromFixedSizeField(saveNameListData[saveDataListCount]); - bool hasSeparator = false; - for (char c : entry) { - if (c == '/' || c == '\\') { - hasSeparator = true; - break; - } - } - if (hasSeparator || entry == "." || entry == "..") { + if (HasPathTraversal(entry)) { ERROR_LOG(Log::sceUtility, "SavedataParam: invalid saveName in list: %s", std::string(entry).c_str()); return SCE_ERROR_UTILITY_INVALID_PARAM_SIZE; } diff --git a/Core/Util/PathUtil.cpp b/Core/Util/PathUtil.cpp index a5cb1a20d8..97404e9f1c 100644 --- a/Core/Util/PathUtil.cpp +++ b/Core/Util/PathUtil.cpp @@ -24,6 +24,12 @@ bool HasParentDirComponent(std::string_view path) { return false; } +bool HasPathTraversal(std::string_view path) { + if (path.find_first_of("/\\") != std::string_view::npos) + return true; + return path == "." || path == ".."; +} + Path FindConfigFile(const Path &searchPath, std::string_view baseFilename, bool *exists) { // Don't search for an absolute path. if (baseFilename.size() > 1 && baseFilename[0] == '/') { diff --git a/Core/Util/PathUtil.h b/Core/Util/PathUtil.h index 0b7c812044..c69ea2f7db 100644 --- a/Core/Util/PathUtil.h +++ b/Core/Util/PathUtil.h @@ -34,6 +34,11 @@ enum PSPDirectories { // extracting or writing files to disk. bool HasParentDirComponent(std::string_view path); +// Returns true if the given string contains a path separator ('/' or '\\') +// or is a bare dot component ("." or ".."). Used to reject guest-controlled +// strings that would otherwise become host filesystem path components. +bool HasPathTraversal(std::string_view path); + Path FindConfigFile(const Path &searchPath, std::string_view baseFilename, bool *exists); Path GetSysDirectory(PSPDirectories directoryType); bool CreateSysDirectories();