From ddc0f0829b4cad826dba23029d9a557cd68fd209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 8 Oct 2021 15:37:03 +0200 Subject: [PATCH 1/2] Replaced textures: Don't leak handles on failure, check allocation success --- Core/TextureReplacer.cpp | 22 ++++++++++++++++++---- Core/TextureReplacer.h | 2 +- GPU/Vulkan/TextureCacheVulkan.cpp | 2 +- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Core/TextureReplacer.cpp b/Core/TextureReplacer.cpp index 5dd17678ad..daa687c0b7 100644 --- a/Core/TextureReplacer.cpp +++ b/Core/TextureReplacer.cpp @@ -733,20 +733,31 @@ float TextureReplacer::LookupReduceHashRange(int& w, int& h) { } } -void ReplacedTexture::Load(int level, void *out, int rowPitch) { +bool ReplacedTexture::Load(int level, void *out, int rowPitch) { _assert_msg_((size_t)level < levels_.size(), "Invalid miplevel"); _assert_msg_(out != nullptr && rowPitch > 0, "Invalid out/pitch"); const ReplacedTextureLevel &info = levels_[level]; FILE *fp = File::OpenCFile(info.file, "rb"); + if (!fp) { + return false; + } + auto imageType = Identify(fp); if (imageType == ReplacedImageType::ZIM) { size_t zimSize = File::GetFileSize(fp); std::unique_ptr zim(new uint8_t[zimSize]); + if (!zim) { + ERROR_LOG(G3D, "Failed to allocate memory for texture replacement"); + fclose(fp); + return false; + } + if (fread(&zim[0], 1, zimSize, fp) != zimSize) { ERROR_LOG(G3D, "Could not load texture replacement: %s - failed to read ZIM", info.file.c_str()); - return; + fclose(fp); + return false; } int w, h, f; @@ -772,7 +783,8 @@ void ReplacedTexture::Load(int level, void *out, int rowPitch) { if (!png_image_begin_read_from_stdio(&png, fp)) { ERROR_LOG(G3D, "Could not load texture replacement info: %s - %s", info.file.c_str(), png.message); - return; + fclose(fp); + return false; } bool checkedAlpha = false; @@ -787,7 +799,8 @@ void ReplacedTexture::Load(int level, void *out, int rowPitch) { if (!png_image_finish_read(&png, nullptr, out, rowPitch, nullptr)) { ERROR_LOG(G3D, "Could not load texture replacement: %s - %s", info.file.c_str(), png.message); - return; + fclose(fp); + return false; } png_image_free(&png); @@ -801,6 +814,7 @@ void ReplacedTexture::Load(int level, void *out, int rowPitch) { } fclose(fp); + return true; } bool TextureReplacer::GenerateIni(const std::string &gameID, Path &generatedFilename) { diff --git a/Core/TextureReplacer.h b/Core/TextureReplacer.h index 1410389157..92fac7b746 100644 --- a/Core/TextureReplacer.h +++ b/Core/TextureReplacer.h @@ -153,7 +153,7 @@ struct ReplacedTexture { return (u8)alphaStatus_; } - void Load(int level, void *out, int rowPitch); + bool Load(int level, void *out, int rowPitch); protected: std::vector levels_; diff --git a/GPU/Vulkan/TextureCacheVulkan.cpp b/GPU/Vulkan/TextureCacheVulkan.cpp index b6616b6a98..d6fd669dc6 100644 --- a/GPU/Vulkan/TextureCacheVulkan.cpp +++ b/GPU/Vulkan/TextureCacheVulkan.cpp @@ -934,7 +934,7 @@ void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) { if (replaced.Valid()) { // Directly load the replaced image. data = drawEngine_->GetPushBufferForTextureData()->PushAligned(size, &bufferOffset, &texBuf, pushAlignment); - replaced.Load(i, data, stride); + replaced.Load(i, data, stride); // if it fails, it'll just be garbage data... OK for now. entry->vkTex->UploadMip(cmdInit, i, mipWidth, mipHeight, texBuf, bufferOffset, stride / bpp); } else { auto dispatchCompute = [&](VkDescriptorSet descSet) { From 322a8a8b3c50b54b0de441e38792164b9f582765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 8 Oct 2021 19:08:45 +0200 Subject: [PATCH 2/2] More checks in ParamSFO reader. Only problem left is that the strings are null terminated and can walk off the buffer. --- Core/ELF/ParamSFO.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Core/ELF/ParamSFO.cpp b/Core/ELF/ParamSFO.cpp index 61b55281ba..71d07dcaca 100644 --- a/Core/ELF/ParamSFO.cpp +++ b/Core/ELF/ParamSFO.cpp @@ -107,17 +107,25 @@ bool ParamSFOData::ReadSFO(const u8 *paramsfo, size_t size) { return false; } - const u8 *key_start = paramsfo + header->key_table_start; const u8 *data_start = paramsfo + header->data_table_start; for (u32 i = 0; i < header->index_table_entries; i++) { - const char *key = (const char *)(key_start + indexTables[i].key_table_offset); + size_t key_offset = header->key_table_start + indexTables[i].key_table_offset; + if (key_offset >= size) { + return false; + } + size_t data_offset = header->data_table_start + indexTables[i].data_table_offset; + if (data_offset >= size) { + return false; + } + + const char *key = (const char *)(paramsfo + key_offset); switch (indexTables[i].param_fmt) { case 0x0404: { // Unsigned int - const u32_le *data = (const u32_le *)(data_start + indexTables[i].data_table_offset); + const u32_le *data = (const u32_le *)(paramsfo + data_offset); SetValue(key, *data, indexTables[i].param_max_len); VERBOSE_LOG(LOADER, "%s %08x", key, *data); } @@ -125,7 +133,7 @@ bool ParamSFOData::ReadSFO(const u8 *paramsfo, size_t size) { case 0x0004: // Special format UTF-8 { - const u8 *utfdata = (const u8 *)(data_start + indexTables[i].data_table_offset); + const u8 *utfdata = (const u8 *)(paramsfo + data_offset); VERBOSE_LOG(LOADER, "%s %s", key, utfdata); SetValue(key, utfdata, indexTables[i].param_len, indexTables[i].param_max_len); } @@ -133,7 +141,7 @@ bool ParamSFOData::ReadSFO(const u8 *paramsfo, size_t size) { case 0x0204: // Regular UTF-8 { - const char *utfdata = (const char *)(data_start + indexTables[i].data_table_offset); + const char *utfdata = (const char *)(paramsfo + data_offset); VERBOSE_LOG(LOADER, "%s %s", key, utfdata); SetValue(key, std::string(utfdata /*, indexTables[i].param_len*/), indexTables[i].param_max_len); }