Fix heap underflow write in EXR decoder extra-channel framebuffer setup#214
Open
sharadboni wants to merge 1 commit intogoogle:mainfrom
Open
Fix heap underflow write in EXR decoder extra-channel framebuffer setup#214sharadboni wants to merge 1 commit intogoogle:mainfrom
sharadboni wants to merge 1 commit intogoogle:mainfrom
Conversation
In DecodeImageEXR, the OpenEXR framebuffer base pointer for extra
channels was computed using extraPixelBytes (sum of all extra
channels' per-pixel sizes), but each per-channel Slice was configured
with the channel's individual size. With dataWindow.min.x > 0 (or
start_y > 0) and multiple extra channels, OpenEXR's pixel address
resolution
base + x * size + y * size * row_size
underflows the input_extra_rows allocation by
data_min_x * (extraPixelBytes - size) bytes for channel 0's first
pixel, and a similar term for higher chunks. The result is a
heap-buffer-underflow write of attacker-controlled bytes (the EXR
extra-channel pixel data) at a controllable offset before the
allocated buffer.
Fix: keep input_extra_rows.data() unmodified as the per-channel
section base, and compute a separate ch_base_ptr per channel using
that channel's own size. With this, each channel's slice resolves
addresses correctly within its own section.
The negative-count memcpy at exr.cc:365 (also originally addressed
by this branch) was merged separately via google#216 and is unaffected by
this change.
151f7e9 to
a41568a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix the heap-buffer-underflow write in
DecodeImageEXRwhen a malformed EXR hasdataWindow.min.x > 0(or non-zerostart_yin chunked decoding) AND multiple extra channels. This PR was rebased on currentmainafter #216 (the negative-count memcpy fix) was merged, so it now contains only the extra-channel framebuffer fix.Bug
lib/extras/dec/exr.cc,DecodeImageEXR, line 333 (theextra_rows_ptrinitialization):char* extra_rows_ptr = input_extra_rows.data() - (dataWindow.min.x + start_y * row_size) * extraPixelBytes;The offset is computed with
extraPixelBytes(sum of all extra channels' per-pixel sizes), but each per-channelOpenEXR::Sliceis configured with the per-channelsize, size * row_size(line 341). When OpenEXR resolves pixel addresses asextra_rows_ptr + x * size + y * size * row_size, the result underflowsinput_extra_rows.data()bydata_min_x * (extraPixelBytes - size)bytes for channel 0's first pixel (and a similar term for non-zerostart_y).The defect is independent of the negative-count memcpy bug fixed by #216; the windows can overlap normally — only
data_min_x > 0plus multiple extras is required.Fix
Keep
input_extra_rows.data()unmodified as a per-section anchor and compute a separatech_base_ptrper channel using the channel's ownsize. With this, OpenEXR's address resolutionslice_base + x*size + y*size*row_sizelands inside the per-channel section.Reproduction
PoC config:
displayWindow X=[0..99] Y=[0..9],dataWindow X=[50..149] Y=[0..9], 2 HALF extras + RGB HALF, no compression. Withdata_min_x = 50,extraPixelBytes = 4, per-channelsize = 2, channel 0 row 0 underflows by 100 bytes at[data - 100, data - 1].ASan output (libjxl/jpegli with sanitizers):
The std::vector's
_M_pwas clobbered by the underflow write to a non-malloc'd address; ASan catches the bad-free at scope-exit destruction. Release build (no ASan) trips glibc's heap integrity check (e.g.free(): invalid pointerordouble free or corruption (out)) and aborts.Note on history
This branch was originally filed on Apr 28 with a combined fix for both the line-333 underflow and the line-365 negative-count memcpy. Since then #216 was merged with the line-365 fix in a slightly different shape (using
exr_x_span = max(0, exr_x2 - exr_x1 + 1)), so this rebase keeps only the line-333 underflow fix on top of #216.