mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-11-16 11:03:45 +01:00
sceIo, savedata, fonts: Check whole structs and arrays, not first bytes
- Block-mode sceIoRead counts its size in sectors, but was clamped to memory in bytes, so a large count read size * 2048 bytes past the buffer. - sceIoDread wrote the short and long names to d_private after checking one byte; the PGD key and sector seek ioctls read 16 bytes after one. - Savedata: GetSize walked the game's entry arrays by its counts (unchecked), GetList and GetFilesList wrote entries past the first unchecked, and Save read the data and icon/pic/sound buffers by their sizes without checking them. Also keep the encrypted size from going negative. - sceFont: font style, font info and char info structs, and the font list, were only checked at their first byte. Style names in logs needn't be terminated. - PGF glyph rendering wrote 3-4 bytes per pixel after checking one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
94469b7592
commit
917607d8fa
4 files changed
+59
-20
No files matched your search
@@ -422,11 +422,21 @@ int SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &saveD
|
||||
ERROR_LOG_REPORT(Log::sceUtility, "Savedata buffer overflow: %d / %d", param->dataSize, param->dataBufSize);
|
||||
return SCE_UTILITY_SAVEDATA_ERROR_RW_BAD_PARAMS;
|
||||
}
|
||||
// Everything below reads these buffers by their sizes, so they have to be in memory. A size of
|
||||
// 2^31 or more would also go negative as the int the encryption uses.
|
||||
if (param->dataBuf.IsValid() && (param->dataSize > 0x7FFFFFF0 || !Memory::IsValidRange(param->dataBuf.ptr, param->dataSize))) {
|
||||
ERROR_LOG_REPORT(Log::sceUtility, "Savedata buffer %08x (%d bytes) isn't all in memory", param->dataBuf.ptr, param->dataSize);
|
||||
return SCE_UTILITY_SAVEDATA_ERROR_RW_BAD_PARAMS;
|
||||
}
|
||||
auto validateSize = [](const PspUtilitySavedataFileData &data) {
|
||||
if (data.buf.IsValid() && data.bufSize < data.size) {
|
||||
ERROR_LOG_REPORT(Log::sceUtility, "Savedata subdata buffer overflow: %d / %d", data.size, data.bufSize);
|
||||
return false;
|
||||
}
|
||||
if (data.buf.IsValid() && !Memory::IsValidRange(data.buf.ptr, data.size)) {
|
||||
ERROR_LOG_REPORT(Log::sceUtility, "Savedata subdata buffer %08x (%d bytes) isn't all in memory", data.buf.ptr, data.size);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
if (!validateSize(param->icon0FileData) || !validateSize(param->icon1FileData) || !validateSize(param->pic1FileData) || !validateSize(param->snd0FileData)) {
|
||||
@@ -462,7 +472,7 @@ int SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &saveD
|
||||
cryptedSize = param->dataSize;
|
||||
if (cryptedSize == 0 || (SceSize)cryptedSize > param->dataBufSize) {
|
||||
ERROR_LOG(Log::sceUtility, "Bad cryptedSize %d", cryptedSize);
|
||||
cryptedSize = param->dataBufSize; // fallback, should never use this
|
||||
cryptedSize = (int)std::min(Memory::ClampValidSizeAt(param->dataBuf.ptr, param->dataBufSize), (u32)0x7FFFFFF0); // fallback, should never use this
|
||||
}
|
||||
u8 *data_ = param->dataBuf;
|
||||
|
||||
@@ -568,7 +578,7 @@ int SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &saveD
|
||||
{
|
||||
saveSize = param->dataSize;
|
||||
if(saveSize == 0 || saveSize > param->dataBufSize)
|
||||
saveSize = param->dataBufSize; // fallback, should never use this
|
||||
saveSize = Memory::ClampValidSizeAt(param->dataBuf.ptr, param->dataBufSize); // fallback, should never use this
|
||||
|
||||
data_ = param->dataBuf;
|
||||
}
|
||||
@@ -1117,6 +1127,13 @@ inline std::string FmtPspTime(const ScePspDateTime &dt) {
|
||||
return StringFromFormat("%04d-%02d-%02d %02d:%02d:%02d.%06d", dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond);
|
||||
}
|
||||
|
||||
// Whether entry `index` of a guest array is all in memory (PSPPointer only checks the first).
|
||||
template <class T>
|
||||
static bool EntryInRange(const PSPPointer<T> &entries, u32 index) {
|
||||
const u64 end = (u64)entries.ptr + ((u64)index + 1) * sizeof(T);
|
||||
return end <= 0x100000000ULL && Memory::IsValidRange(entries.ptr, (u32)(end - entries.ptr));
|
||||
}
|
||||
|
||||
int SavedataParam::GetSizes(SceUtilitySavedataParam *param) {
|
||||
if (!param) {
|
||||
return SCE_UTILITY_SAVEDATA_ERROR_SIZES_NO_DATA;
|
||||
@@ -1246,6 +1263,10 @@ bool SavedataParam::GetList(SceUtilitySavedataParam *param)
|
||||
sfoFiles.push_back(sfoFile);
|
||||
}
|
||||
|
||||
if (!validDir.empty() && !EntryInRange(param->idList->entries, (u32)validDir.size() - 1)) {
|
||||
ERROR_LOG(Log::sceUtility, "SavedataParam::GetList(): entries %08x can't hold %d", param->idList->entries.ptr, (int)validDir.size());
|
||||
return false;
|
||||
}
|
||||
SceUtilitySavedataIdListEntry *entries = param->idList->entries;
|
||||
for (u32 i = 0; i < (u32)validDir.size(); i++)
|
||||
{
|
||||
@@ -1361,11 +1382,11 @@ int SavedataParam::GetFilesList(SceUtilitySavedataParam *param, u32 requestAddr,
|
||||
SceUtilitySavedataFileListEntry *entry = NULL;
|
||||
int sizeOffset = 0;
|
||||
if (isSystemFile) {
|
||||
if (fileList->systemEntries.IsValid() && fileList->resultNumSystemEntries < fileList->maxSystemEntries) {
|
||||
if (fileList->systemEntries.IsValid() && fileList->resultNumSystemEntries < fileList->maxSystemEntries && EntryInRange(fileList->systemEntries, fileList->resultNumSystemEntries)) {
|
||||
entry = &fileList->systemEntries[fileList->resultNumSystemEntries++];
|
||||
}
|
||||
} else if (secureFilenames.find(file.name) != secureFilenames.end()) {
|
||||
if (fileList->secureEntries.IsValid() && fileList->resultNumSecureEntries < fileList->maxSecureEntries) {
|
||||
if (fileList->secureEntries.IsValid() && fileList->resultNumSecureEntries < fileList->maxSecureEntries && EntryInRange(fileList->secureEntries, fileList->resultNumSecureEntries)) {
|
||||
entry = &fileList->secureEntries[fileList->resultNumSecureEntries++];
|
||||
}
|
||||
// Secure files are slightly bigger.
|
||||
@@ -1373,7 +1394,7 @@ int SavedataParam::GetFilesList(SceUtilitySavedataParam *param, u32 requestAddr,
|
||||
sizeOffset = -0x10;
|
||||
}
|
||||
} else {
|
||||
if (fileList->normalEntries.IsValid() && fileList->resultNumNormalEntries < fileList->maxNormalEntries) {
|
||||
if (fileList->normalEntries.IsValid() && fileList->resultNumNormalEntries < fileList->maxNormalEntries && EntryInRange(fileList->normalEntries, fileList->resultNumNormalEntries)) {
|
||||
entry = &fileList->normalEntries[fileList->resultNumNormalEntries++];
|
||||
}
|
||||
}
|
||||
@@ -1437,6 +1458,14 @@ bool SavedataParam::GetSize(SceUtilitySavedataParam *param) {
|
||||
auto listing = pspFileSystem.GetDirListing(saveDir, &exists);
|
||||
const u64 freeBytes = MemoryStick_FreeSpace(GetGameName(param));
|
||||
|
||||
// The counts and both arrays come from the game.
|
||||
if (param->sizeInfo->numNormalEntries < 0 || param->sizeInfo->numSecureEntries < 0 ||
|
||||
(param->sizeInfo->numNormalEntries > 0 && !EntryInRange(param->sizeInfo->normalEntries, param->sizeInfo->numNormalEntries - 1)) ||
|
||||
(param->sizeInfo->numSecureEntries > 0 && !EntryInRange(param->sizeInfo->secureEntries, param->sizeInfo->numSecureEntries - 1))) {
|
||||
ERROR_LOG(Log::sceUtility, "SavedataParam::GetSize(): bad entry lists");
|
||||
return false;
|
||||
}
|
||||
|
||||
s64 overwriteBytes = 0;
|
||||
s64 writeBytes = 0;
|
||||
for (int i = 0; i < param->sizeInfo->numNormalEntries; ++i) {
|
||||
|
||||
+1
-1
@@ -779,7 +779,7 @@ void PGF::SetFontPixel(u32 base, int bpl, int bufWidth, int bufHeight, int x, in
|
||||
// glyph smears across them - see the "Linesize = 1" case in pspautotests font/charglyphimage.
|
||||
// The bufWidth/bufHeight rectangle above and the address check below are what keep this sane.
|
||||
int framebufferAddr = base + (y * bpl) + (pixelBytes == 0 ? x / 2 : x * pixelBytes);
|
||||
if (!Memory::IsValidAddress(framebufferAddr)) {
|
||||
if (!Memory::IsValidRange(framebufferAddr, pixelBytes == 0 ? 1 : pixelBytes)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -1316,14 +1316,14 @@ static int sceFontFindOptimumFont(u32 libHandle, u32 fontStylePtr, u32 errorCode
|
||||
return hleReportError(Log::sceFont, 0, "invalid font lib");
|
||||
}
|
||||
|
||||
if (!Memory::IsValidAddress(fontStylePtr)) {
|
||||
if (!Memory::IsValidRange(fontStylePtr, sizeof(PGFFontStyle))) {
|
||||
// Yes, actually. Must've been a typo in the library.
|
||||
*errorCode = SCE_FONT_ERROR_INVALID_LIBID;
|
||||
return hleReportError(Log::sceFont, 0, "invalid style address");
|
||||
}
|
||||
|
||||
auto requestedStyle = PSPPointer<const PGFFontStyle>::Create(fontStylePtr);
|
||||
DEBUG_LOG(Log::sceFont, "requestedStyle fontAttributes %i,fontCountry %i,fontExpire %i,fontFamily %i,fontFileName %s,fontH %f,fontHRes %f,fontLanguage %i,fontName %s,fontRegion %i,fontStyle %i,fontStyleSub %i,fontV %f,fontVRes %f,fontWeight %f", requestedStyle->fontAttributes, requestedStyle->fontCountry, requestedStyle->fontExpire, requestedStyle->fontFamily, requestedStyle->fontFileName, requestedStyle->fontH, requestedStyle->fontHRes, requestedStyle->fontLanguage, requestedStyle->fontName, requestedStyle->fontRegion, requestedStyle->fontStyle, requestedStyle->fontStyleSub, requestedStyle->fontV, requestedStyle->fontVRes, requestedStyle->fontWeight);
|
||||
DEBUG_LOG(Log::sceFont, "requestedStyle fontAttributes %i,fontCountry %i,fontExpire %i,fontFamily %i,fontFileName %.64s,fontH %f,fontHRes %f,fontLanguage %i,fontName %.64s,fontRegion %i,fontStyle %i,fontStyleSub %i,fontV %f,fontVRes %f,fontWeight %f", requestedStyle->fontAttributes, requestedStyle->fontCountry, requestedStyle->fontExpire, requestedStyle->fontFamily, requestedStyle->fontFileName, requestedStyle->fontH, requestedStyle->fontHRes, requestedStyle->fontLanguage, requestedStyle->fontName, requestedStyle->fontRegion, requestedStyle->fontStyle, requestedStyle->fontStyleSub, requestedStyle->fontV, requestedStyle->fontVRes, requestedStyle->fontWeight);
|
||||
|
||||
// Find the first nearest match for H/V, OR the last exact match for others.
|
||||
float hRes = requestedStyle->fontHRes > 0.0f ? (float)requestedStyle->fontHRes : fontLib->FontHRes();
|
||||
@@ -1383,7 +1383,7 @@ static int sceFontFindFont(u32 libHandle, u32 fontStylePtr, u32 errorCodePtr) {
|
||||
return hleNoLog(0);
|
||||
}
|
||||
|
||||
if (!Memory::IsValidAddress(fontStylePtr)) {
|
||||
if (!Memory::IsValidRange(fontStylePtr, sizeof(PGFFontStyle))) {
|
||||
ERROR_LOG(Log::sceFont, "sceFontFindFont(%08x, %08x, %08x): invalid style address", libHandle, fontStylePtr, errorCodePtr);
|
||||
*errorCode = SCE_FONT_ERROR_INVALID_PARAMETER;
|
||||
return hleNoLog(0);
|
||||
@@ -1392,7 +1392,7 @@ static int sceFontFindFont(u32 libHandle, u32 fontStylePtr, u32 errorCodePtr) {
|
||||
DEBUG_LOG(Log::sceFont, "sceFontFindFont(%x, %x, %x)", libHandle, fontStylePtr, errorCodePtr);
|
||||
|
||||
auto requestedStyle = PSPPointer<const PGFFontStyle>::Create(fontStylePtr);
|
||||
DEBUG_LOG(Log::sceFont, "requestedStyle fontAttributes %i,fontCountry %i,fontExpire %i,fontFamily %i,fontFileName %s,fontH %f,fontHRes %f,fontLanguage %i,fontName %s,fontRegion %i,fontStyle %i,fontStyleSub %i,fontV %f,fontVRes %f,fontWeight %f", requestedStyle->fontAttributes, requestedStyle->fontCountry, requestedStyle->fontExpire, requestedStyle->fontFamily, requestedStyle->fontFileName, requestedStyle->fontH, requestedStyle->fontHRes, requestedStyle->fontLanguage, requestedStyle->fontName, requestedStyle->fontRegion, requestedStyle->fontStyle, requestedStyle->fontStyleSub, requestedStyle->fontV, requestedStyle->fontVRes, requestedStyle->fontWeight);
|
||||
DEBUG_LOG(Log::sceFont, "requestedStyle fontAttributes %i,fontCountry %i,fontExpire %i,fontFamily %i,fontFileName %.64s,fontH %f,fontHRes %f,fontLanguage %i,fontName %.64s,fontRegion %i,fontStyle %i,fontStyleSub %i,fontV %f,fontVRes %f,fontWeight %f", requestedStyle->fontAttributes, requestedStyle->fontCountry, requestedStyle->fontExpire, requestedStyle->fontFamily, requestedStyle->fontFileName, requestedStyle->fontH, requestedStyle->fontHRes, requestedStyle->fontLanguage, requestedStyle->fontName, requestedStyle->fontRegion, requestedStyle->fontStyle, requestedStyle->fontStyleSub, requestedStyle->fontV, requestedStyle->fontVRes, requestedStyle->fontWeight);
|
||||
|
||||
// Find the closest exact match for the fields specified.
|
||||
float hRes = requestedStyle->fontHRes > 0.0f ? (float)requestedStyle->fontHRes : fontLib->FontHRes();
|
||||
@@ -1418,7 +1418,7 @@ static int sceFontFindFont(u32 libHandle, u32 fontStylePtr, u32 errorCodePtr) {
|
||||
}
|
||||
|
||||
static int sceFontGetFontInfo(u32 fontHandle, u32 fontInfoPtr) {
|
||||
if (!Memory::IsValidAddress(fontInfoPtr)) {
|
||||
if (!Memory::IsValidRange(fontInfoPtr, sizeof(PGFFontInfo))) {
|
||||
return hleLogError(Log::sceFont, SCE_FONT_ERROR_INVALID_PARAMETER, "bad fontInfo pointer");
|
||||
}
|
||||
LoadedFont *font = GetLoadedFont(fontHandle, true);
|
||||
@@ -1455,7 +1455,7 @@ static int sceFontGetFontInfoByIndexNumber(u32 libHandle, u32 fontInfoPtr, u32 i
|
||||
|
||||
static int sceFontGetCharInfo(u32 fontHandle, u32 charCode, u32 charInfoPtr) {
|
||||
charCode &= 0xffff;
|
||||
if (!Memory::IsValidAddress(charInfoPtr)) {
|
||||
if (!Memory::IsValidRange(charInfoPtr, sizeof(PGFCharInfo))) {
|
||||
return hleLogError(Log::sceFont, SCE_FONT_ERROR_INVALID_PARAMETER, "bad charInfo pointer");
|
||||
}
|
||||
LoadedFont *font = GetLoadedFont(fontHandle, true);
|
||||
@@ -1494,7 +1494,7 @@ static int sceFontGetCharInfo(u32 fontHandle, u32 charCode, u32 charInfoPtr) {
|
||||
|
||||
static int sceFontGetShadowInfo(u32 fontHandle, u32 charCode, u32 charInfoPtr) {
|
||||
charCode &= 0xffff;
|
||||
if (!Memory::IsValidAddress(charInfoPtr)) {
|
||||
if (!Memory::IsValidRange(charInfoPtr, sizeof(PGFCharInfo))) {
|
||||
return hleLogError(Log::sceFont, SCE_FONT_ERROR_INVALID_PARAMETER, "bad charInfo pointer");
|
||||
}
|
||||
LoadedFont *font = GetLoadedFont(fontHandle, true);
|
||||
@@ -1618,6 +1618,8 @@ static int sceFontGetFontList(u32 fontLibHandle, u32 fontStylePtr, int numFonts)
|
||||
DEBUG_LOG(Log::sceFont, "sceFontGetFontList(%08x, %08x, %i)", fontLibHandle, fontStylePtr, numFonts);
|
||||
if (fl->handle() != 0) {
|
||||
numFonts = std::min(numFonts, (int)internalFonts.size());
|
||||
// Only the first entry was checked above.
|
||||
numFonts = std::min(numFonts, (int)(Memory::ClampValidSizeAt(fontStylePtr, (u32)std::max(numFonts, 0) * sizeof(PGFFontStyle)) / sizeof(PGFFontStyle)));
|
||||
for (int i = 0; i < numFonts; i++)
|
||||
fontStyles[i] = internalFonts[i]->GetFontStyle();
|
||||
}
|
||||
|
||||
+15
-7
@@ -1145,6 +1145,11 @@ static bool __IoRead(int &result, int id, u32 data_addr, int size, int &us) {
|
||||
NotifyMemInfo(MemBlockFlags::WRITE, data_addr, size, tag.c_str(), tag.size());
|
||||
u8 *data = (u8 *)Memory::GetPointerUnchecked(data_addr);
|
||||
u32 validSize = Memory::ClampValidSizeAt(data_addr, size);
|
||||
// Block devices count the size in sectors, so clamp what that many sectors cover.
|
||||
if ((pspFileSystem.DevType(f->handle) & PSPDevType::BLOCK) != 0) {
|
||||
const u32 bytes = (u32)std::min<u64>((u64)size * 2048, 0xFFFFFFFF);
|
||||
validSize = Memory::ClampValidSizeAt(data_addr, bytes) / 2048;
|
||||
}
|
||||
if (f->npdrm) {
|
||||
result = npdrmRead(f, data, validSize);
|
||||
currentMIPS->InvalidateICacheRangeDeferred(data_addr, validSize);
|
||||
@@ -2755,21 +2760,23 @@ static u32 sceIoDread(MIPSState *mips, int id, u32 dirent_addr) {
|
||||
// write d_private for supporting Custom BGM
|
||||
// ref JPCSP https://code.google.com/p/jpcsp/source/detail?r=3468
|
||||
if (Memory::IsValidAddress(entry->d_private)){
|
||||
// What's written depends on the layout, so check each one's whole range below.
|
||||
if (sceKernelGetCompiledSdkVersion() <= 0x0307FFFF){
|
||||
// d_private is pointing to an area of unknown size
|
||||
// - [0..12] "8.3" file name (null-terminated), could be empty.
|
||||
// - [13..???] long file name (null-terminated)
|
||||
|
||||
strcpy_limit((char*)Memory::GetPointerUnchecked(entry->d_private), shortName.c_str(), 13);
|
||||
strcpy_limit((char*)Memory::GetPointerUnchecked(entry->d_private + 13), (const char*)entry->d_name, ARRAY_SIZE(entry->d_name));
|
||||
if (Memory::IsValidRange(entry->d_private, 13 + (u32)ARRAY_SIZE(entry->d_name))) {
|
||||
strcpy_limit((char*)Memory::GetPointerUnchecked(entry->d_private), shortName.c_str(), 13);
|
||||
strcpy_limit((char*)Memory::GetPointerUnchecked(entry->d_private + 13), (const char*)entry->d_name, ARRAY_SIZE(entry->d_name));
|
||||
}
|
||||
}
|
||||
else {
|
||||
// d_private is pointing to an area of total size 1044
|
||||
// - [0..3] size of area
|
||||
// - [4..19] "8.3" file name (null-terminated), could be empty.
|
||||
// - [20..???] long file name (null-terminated)
|
||||
auto size = Memory::ReadUnchecked_U32(entry->d_private);
|
||||
if (size >= 1044) {
|
||||
auto size = Memory::IsValidRange(entry->d_private, 4) ? Memory::ReadUnchecked_U32(entry->d_private) : 0;
|
||||
if (size >= 1044 && Memory::IsValidRange(entry->d_private, 1044)) {
|
||||
strcpy_limit((char*)Memory::GetPointerUnchecked(entry->d_private + 4), shortName.c_str(), 16);
|
||||
strcpy_limit((char*)Memory::GetPointerUnchecked(entry->d_private + 20), (const char*)entry->d_name, ARRAY_SIZE(entry->d_name));
|
||||
}
|
||||
@@ -3087,7 +3094,7 @@ int __IoIoctl(u32 id, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 out
|
||||
u8 pgd_header[0x90]{};
|
||||
u8 pgd_magic[4] = {0x00, 0x50, 0x47, 0x44};
|
||||
|
||||
if (Memory::IsValidAddress(indataPtr) && inlen == 16) {
|
||||
if (Memory::IsValidRange(indataPtr, 16) && inlen == 16) {
|
||||
memcpy(keybuf, Memory::GetPointerUnchecked(indataPtr), 16);
|
||||
key_ptr = keybuf;
|
||||
}else{
|
||||
@@ -3285,7 +3292,8 @@ int __IoIoctl(u32 id, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 out
|
||||
//if (GetIOTimingMethod() == IOTIMING_REALISTIC) // Need a check for io timing method?
|
||||
usec += 15000;// Fantasy Golf Pangya Portable(KS) needs a delay over 15000us.
|
||||
|
||||
if (Memory::IsValidAddress(indataPtr) && inlen >= 4) {
|
||||
// The whole struct is read, whatever inlen says.
|
||||
if (Memory::IsValidRange(indataPtr, 16) && inlen >= 4) {
|
||||
struct SeekInfo {
|
||||
u64_le offset;
|
||||
u32_le unk;
|
||||
|
||||
Reference in new issue
Block a user