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/ImageOperations.cpp | |
| 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/ImageOperations.cpp')
| -rw-r--r-- | Source/Core/InputCommon/ImageOperations.cpp | 32 |
1 files changed, 25 insertions, 7 deletions
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; } |
