mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-12-03 19:23:47 +01:00
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.
This commit is contained in:
1 parent
779cae6232
commit
0eef5d0164
4 files changed
+19
-23
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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] == '/') {
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user