Skip to content

use size_t for dds and ktx surface size to avoid overflow#626

Merged
solidpixel merged 8 commits into
ARM-software:mainfrom
sahvx655-wq:size_t-surface-dds-ktx
Jun 2, 2026
Merged

use size_t for dds and ktx surface size to avoid overflow#626
solidpixel merged 8 commits into
ARM-software:mainfrom
sahvx655-wq:size_t-surface-dds-ktx

Conversation

@sahvx655-wq
Copy link
Copy Markdown
Contributor

Fuzzing the image loaders with crafted KTX and DDS headers turned this up. The surface size in load_ktx_uncompressed_image and load_dds_uncompressed_image is computed in uint32_t, so large width/height/depth truncate the byte count and the buffer is allocated too small while the scanline copy still walks the full dimensions, reading off the end of the heap. The KTX size check is bypassed by setting the stored surface size to the truncated value. Same shape as the recent .astc fix, so compute in size_t and reject on overflow here too.

@solidpixel
Copy link
Copy Markdown
Contributor

Thanks for the PR - will review in the next few days.

@solidpixel
Copy link
Copy Markdown
Contributor

I've refactored the overflow checks into common library functions in #628, which should simplify the added code here. Will rebase this after the other PR has merged.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the CLI’s uncompressed KTX and DDS loaders against crafted headers that previously triggered uint32_t truncation during surface-size computation, leading to undersized allocations and out-of-bounds heap reads during scanline copies.

Changes:

  • Switch KTX and DDS surface size/stride computation to size_t with astc::mul_safe() overflow detection and reject inconsistent or overflowing sizes.
  • Update related local fields (components, bitness, bytes_per_component, and DDS DXGI table fields) to unsigned types.
  • Improve DDS robustness by handling allocation failure for the input surface buffer and updating stride-based source addressing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Source/astcenccli_image_load_store.cpp
Comment thread Source/astcenccli_image_load_store.cpp Outdated
@solidpixel
Copy link
Copy Markdown
Contributor

solidpixel commented Jun 2, 2026

Existing issues spotted while reviewing, I'll pick up in follow-on PRs for these:

@solidpixel solidpixel merged commit 913bfbc into ARM-software:main Jun 2, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants