Repository navigation
Conversation
read_scanline() computed end_pos - current_pos to detect a final row missing one byte. A malformed BMP with data_offset larger than the actual file length makes current_pos exceed end_pos after the seek, so the u64 subtraction underflows and panics with 'attempt to subtract with overflow'. Use checked_sub instead: when current_pos > end_pos the lenient branch is skipped and we fall through to read_exact, which returns UnexpectedEof as an Err instead of panicking.
This branch has not been deployed
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.
What does this PR do?
Fixes a
u64subtraction overflow panic in the BMP decoder'sread_scanline()when a malformed BMP file has adata_offsetlarger than the actual file length, replacing the bare subtraction withchecked_subso the decoder returns anErrinstead of panicking.Problem
read_scanline()(insrc/codecs/bmp/decoder.rs) has a lenient branch for a final row that is missing one byte:The branch computes
end_pos - current_posto decide whether the remaining bytes exactly match the head length. A malformed BMP whose file header declares adata_offsetfar beyond the real file size makescurrent_posexceedend_posafter the seek (aCursorallows seeking past the end of the buffer, which is valid IO behavior). Theu64subtractionend_pos - current_posthen underflows and panics withattempt to subtract with overflowinstead of returning a decoding error.This is reachable from the public API
image::load_from_memorywith a small crafted BMP input, so any application decoding untrusted BMP data can be crashed by the panic.Root cause
read_full_byte_pixel_dataseeks todata_offsetfrom the file header; the seek is allowed to move the position past the buffer end.SpecCompliance::Lenient,read_scanlinecomputesend_pos - current_posdirectly. Withend_pos = 48(real file length) andcurrent_pos = 4718626(the maliciousdata_offset = 0x480022), the subtraction underflows.Fix
Replace the bare subtraction with
checked_sub. Whencurrent_pos > end_pos,checked_subreturnsNone, the lenient branch is skipped, and execution falls through toread_exact, which returnsUnexpectedEofas anErrinstead of panicking:Behavior for well-formed BMPs is unchanged: when
current_pos <= end_pos,checked_subyields the same value as before, so the lenient last-row handling still applies.Reproducer (fails without this fix)
The
mainbranch added support for parsing OS/2 headers, allowing this sample to reach the buggy subtraction underflow.Run with
cargo run --bin repro(debug profile has overflow checks enabled; default features includebmp).