diff options
| author | Acts1631 <acts1631kjv@proton.me> | 2026-11-13 14:21:15 -0500 |
|---|---|---|
| committer | Acts1631 <acts1631kjv@proton.me> | 2026-11-19 09:38:28 -0500 |
| commit | fbe996f380c717d6d15a8cf5577146ee251aaa1d (patch) | |
| tree | a14c2fcb61602355138eb2ed2610bb42b05afc95 /Source/Core/InputCommon | |
| parent | 2fe162866282fa2806d62f69aa053315d750ab91 (diff) | |
InputCommon: validate dynamic texture regions
Dynamic input texture JSON could specify rectangles outside the base
image. CopyImageRegion then wrote replacement pixels beyond the
destination vector, allowing a crafted local texture pack to corrupt
the heap.
Validate coordinates and image bounds at the parser and caller. Make
CopyImageRegion independently reject invalid source or destination
ranges.
Diffstat (limited to 'Source/Core/InputCommon')
4 files changed, 68 insertions, 17 deletions
diff --git a/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp b/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp index 250881ccc2..3915672358 100644 --- a/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp +++ b/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp @@ -159,6 +159,15 @@ void Configuration::GenerateTexture(const Common::IniFile& file, for (const auto& rect : rects) { + if (rect.right > image_to_write->width || rect.bottom > image_to_write->height) + { + ERROR_LOG_FMT( + VIDEO, "Dynamic input texture region ({}, {}, {}, {}) exceeds image '{}' ({}x{}).", + rect.left, rect.top, rect.right, rect.bottom, texture_data.m_image_name, + image_to_write->width, image_to_write->height); + continue; + } + InputCommon::ImagePixelData pixel_data; if (host_key_image->width == rect.GetWidth() && host_key_image->height == rect.GetHeight()) @@ -177,9 +186,11 @@ void Configuration::GenerateTexture(const Common::IniFile& file, Resize(ResizeMode::Nearest, *host_key_image, rect.GetWidth(), rect.GetHeight()); } - CopyImageRegion(pixel_data, *image_to_write, - Rect{0, 0, rect.GetWidth(), rect.GetHeight()}, rect); - dirty = true; + if (CopyImageRegion(pixel_data, *image_to_write, + Rect{0, 0, rect.GetWidth(), rect.GetHeight()}, rect)) + { + dirty = true; + } } } } diff --git a/Source/Core/InputCommon/DynamicInputTextures/DITSpecification.cpp b/Source/Core/InputCommon/DynamicInputTextures/DITSpecification.cpp index 7b2b15a80b..902a2d11f8 100644 --- a/Source/Core/InputCommon/DynamicInputTextures/DITSpecification.cpp +++ b/Source/Core/InputCommon/DynamicInputTextures/DITSpecification.cpp @@ -3,6 +3,9 @@ #include "InputCommon/DynamicInputTextures/DITSpecification.h" +#include <cmath> +#include <limits> + #include <fmt/format.h> #include "Common/FileUtil.h" @@ -11,6 +14,25 @@ namespace InputCommon::DynamicInputTextures { +namespace +{ +bool ReadCoordinate(const picojson::value& value, u32* coordinate) +{ + if (!value.is<double>()) + return false; + + const double number = value.get<double>(); + if (!std::isfinite(number) || number < 0 || + number > static_cast<double>(std::numeric_limits<u32>::max()) || std::trunc(number) != number) + { + return false; + } + + *coordinate = static_cast<u32>(number); + return true; +} +} // namespace + bool ProcessSpecificationV1(picojson::value& root, std::vector<Data>& input_textures, const std::string& base_path, const std::string& json_file) { @@ -135,20 +157,20 @@ bool ProcessSpecificationV1(picojson::value& root, std::vector<Data>& input_text return false; } - if (!std::ranges::all_of(region_offsets, &picojson::value::is<double>)) + if (!ReadCoordinate(region_offsets[0], &r.left) || + !ReadCoordinate(region_offsets[1], &r.top) || + !ReadCoordinate(region_offsets[2], &r.right) || + !ReadCoordinate(region_offsets[3], &r.bottom) || r.left >= r.right || + r.top >= r.bottom) { ERROR_LOG_FMT( VIDEO, "Failed to load dynamic input json file '{}' because emulated controller '{}' " - "key '{}' has a region that has the incorrect offset type.", + "key '{}' has an invalid region.", json_file, emulated_controller_name, emulated_control); return false; } - r.left = static_cast<u32>(region_offsets[0].get<double>()); - r.top = static_cast<u32>(region_offsets[1].get<double>()); - r.right = static_cast<u32>(region_offsets[2].get<double>()); - r.bottom = static_cast<u32>(region_offsets[3].get<double>()); region_rects.push_back(r); } key_to_regions.insert_or_assign(emulated_control, std::move(region_rects)); diff --git a/Source/Core/InputCommon/ImageOperations.cpp b/Source/Core/InputCommon/ImageOperations.cpp index 8fa844fec1..29cdd6c88b 100644 --- a/Source/Core/InputCommon/ImageOperations.cpp +++ b/Source/Core/InputCommon/ImageOperations.cpp @@ -21,25 +21,43 @@ Pixel SampleNearest(const ImagePixelData& src, double u, double v) const u32 y = std::clamp(static_cast<u32>(v * src.height), 0u, src.height - 1); return src.pixels[x + y * src.width]; } + +bool IsRegionValid(const ImagePixelData& image, const Rect& region) +{ + if (region.left > region.right || region.top > region.bottom || region.right > image.width || + region.bottom > image.height) + { + return false; + } + + return image.height == 0 || + static_cast<size_t>(image.width) <= image.pixels.size() / image.height; +} } // namespace -void CopyImageRegion(const ImagePixelData& src, ImagePixelData& dst, const Rect& src_region, +bool CopyImageRegion(const ImagePixelData& src, ImagePixelData& dst, const Rect& src_region, const Rect& dst_region) { - if (src_region.GetWidth() != dst_region.GetWidth() || + if (!IsRegionValid(src, src_region) || !IsRegionValid(dst, dst_region) || + src_region.GetWidth() != dst_region.GetWidth() || src_region.GetHeight() != dst_region.GetHeight()) { - return; + return false; } for (u32 x = 0; x < dst_region.GetWidth(); x++) { for (u32 y = 0; y < dst_region.GetHeight(); y++) { - dst.pixels[(y + dst_region.top) * dst.width + x + dst_region.left] = - src.pixels[(y + src_region.top) * src.width + x + src_region.left]; + const size_t dst_index = + static_cast<size_t>(y + dst_region.top) * dst.width + x + dst_region.left; + const size_t src_index = + static_cast<size_t>(y + src_region.top) * src.width + x + src_region.left; + dst.pixels[dst_index] = src.pixels[src_index]; } } + + return true; } std::optional<ImagePixelData> LoadImage(const std::string& path) @@ -156,8 +174,8 @@ ImagePixelData ResizeKeepAspectRatio(ResizeMode mode, const ImagePixelData& src, } resized = Resize(mode, src, corrected_width, new_height); } - CopyImageRegion(resized, result, Rect{0, 0, resized.width, resized.height}, - Rect{left, top, left + resized.width, top + resized.height}); + (void)CopyImageRegion(resized, result, Rect{0, 0, resized.width, resized.height}, + Rect{left, top, left + resized.width, top + resized.height}); return result; } diff --git a/Source/Core/InputCommon/ImageOperations.h b/Source/Core/InputCommon/ImageOperations.h index 72887d1db2..f4ef7225da 100644 --- a/Source/Core/InputCommon/ImageOperations.h +++ b/Source/Core/InputCommon/ImageOperations.h @@ -44,7 +44,7 @@ struct ImagePixelData u32 height = 0; }; -void CopyImageRegion(const ImagePixelData& src, ImagePixelData& dst, const Rect& src_region, +bool CopyImageRegion(const ImagePixelData& src, ImagePixelData& dst, const Rect& src_region, const Rect& dst_region); std::optional<ImagePixelData> LoadImage(const std::string& path); |
