Skip to content

Check the buffer size in the HTJ2K write_header - #2838

Open
Ar-maan05 wants to merge 1 commit into
AcademySoftwareFoundation:mainfrom
Ar-maan05:ht-header-small-chunk
Open

Ar-maan05 wants to merge 1 commit into
AcademySoftwareFoundation:mainfrom
Ar-maan05:ht-header-small-chunk

Conversation

@Ar-maan05

Copy link
Copy Markdown
Contributor

small one in the HTJ2K header writer. When a chunk is smaller than the 6 byte header, like a 1x1 image with one half or float channel, write_header substracts the header size from max_sz first and wraps it, so buffer + max_sz overflows. The encoding fuzzer hit this too.

Repro: write a 1x1 HTJ2K256 scanline file through the Core API in a UBSan build:

before: internal_ht_common.cpp:249:54: runtime error: addition of unsigned offset to 0x...016 overflowed to 0x...014
after:  clean, chunk stored uncompressed (packed 4, compressed 4), reads back the same value

it worked by luck before, the wrapped end pointer made the next push throw range_error anyway. The fix just throws that same error before the subtraction, so nothing changes in the output. Added the small sizes to testHTHeaderBounds, which fails under UBSan without the fix and passes with it. Rest of the OpenEXRCore tests pass under UBSan apart from testAttrChlists/testAttrLists, which fail on main too (they pass -1 as an enum on purpose).

write_header() computes max_sz - HEADER_SZ before checking anything, so a chunk smaller than the 6 byte header (a single HALF or FLOAT sample, for example a 1x1 image) wraps the size to near SIZE_MAX and buffer + max_sz overflows the pointer. UBSan flags it, and it only ends up throwing range_error today because the wrapped end pointer happens to land before cur.

Throw range_error up front when max_sz < HEADER_SZ. The encoder already catches that and stores the chunk uncompressed, so the output does not change.

Signed-off-by: Armaan Sandhu <armaan.sandhu0504@gmail.com>
@cary-ilm
cary-ilm requested a review from palemieux November 10, 2026 21:48
@cary-ilm

Copy link
Copy Markdown
Member

Thanks for the fix. The failing mingw32 CI check is unrelated and will be resolved by #2840. Waiting on @palemieux's review to approve and merge.

This branch has not been deployed

No deployments
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.

2 participants