Skip to content

Fix u64 subtraction overflow in BMP decoder read_scanline - #3145

Open
Mengxi-Xu wants to merge 1 commit into
image-rs:mainfrom
Mengxi-Xu:fix-bmp-read-scanline-subtract-overflow
Open

Mengxi-Xu wants to merge 1 commit into
image-rs:mainfrom
Mengxi-Xu:fix-bmp-read-scanline-subtract-overflow

Conversation

@Mengxi-Xu

Copy link
Copy Markdown

What does this PR do?

Fixes a u64 subtraction overflow panic in the BMP decoder's read_scanline() when a malformed BMP file has a data_offset larger than the actual file length, replacing the bare subtraction with checked_sub so the decoder returns an Err instead of panicking.

Problem

read_scanline() (in src/codecs/bmp/decoder.rs) has a lenient branch for a final row that is missing one byte:

let current_pos = reader.stream_position()?;
let end_pos = reader.seek(SeekFrom::End(0))?;
reader.seek(SeekFrom::Start(current_pos))?;
// ...
if Ok(head.len()) == usize::try_from(end_pos - current_pos) {
    reader.read_exact(head)?;
    *last = b'\0';
    return Ok(());
}

The branch computes end_pos - current_pos to decide whether the remaining bytes exactly match the head length. A malformed BMP whose file header declares a data_offset far beyond the real file size makes current_pos exceed end_pos after the seek (a Cursor allows seeking past the end of the buffer, which is valid IO behavior). The u64 subtraction end_pos - current_pos then underflows and panics with attempt to subtract with overflow instead of returning a decoding error.

This is reachable from the public API image::load_from_memory with a small crafted BMP input, so any application decoding untrusted BMP data can be crashed by the panic.

Root cause

  • read_full_byte_pixel_data seeks to data_offset from the file header; the seek is allowed to move the position past the buffer end.
  • On the last row with SpecCompliance::Lenient, read_scanline computes end_pos - current_pos directly. With end_pos = 48 (real file length) and current_pos = 4718626 (the malicious data_offset = 0x480022), the subtraction underflows.

Fix

Replace the bare subtraction with checked_sub. When current_pos > end_pos, checked_sub returns None, the lenient branch is skipped, and execution falls through to read_exact, which returns UnexpectedEof as an Err instead of panicking:

if let Some(remaining) = end_pos.checked_sub(current_pos) {
    if Ok(head.len()) == usize::try_from(remaining) {
        reader.read_exact(head)?;
        *last = b'\0';
        return Ok(());
    }
}

Behavior for well-formed BMPs is unchanged: when current_pos <= end_pos, checked_sub yields the same value as before, so the lenient last-row handling still applies.

Reproducer (fails without this fix)

The main branch added support for parsing OS/2 headers, allowing this sample to reach the buggy subtraction underflow.

fn main() {
    let bmp: &[u8] = &[
        0x42, 0x4D,             // "BM"
        0x36, 0x00, 0x05, 0x00,
        0x01, 0x00,
        0x00, 0x00,
        0x22, 0x00, 0x48, 0x00, // data_offset = 0x480022 (4718626 >> file length 48)
        0x14, 0x00, 0x00, 0x00, // OS/2 V2 header size = 20
        0x04, 0x00, 0x00, 0x00, // width = 4
        0x01, 0x00, 0x00, 0x00, // height = 1 (single row == last row)
        0x01, 0x80,
        0x18, 0x00,             // bpp = 24
        0x00, 0x00, 0x00, 0x00, // BI_RGB
        0x0c, 0x00, 0x00, 0x00,
        0xc7, 0x84, 0x10, 0x20,
        0x04, 0x05, 0x06, 0x07,
        0x00, 0x14,
    ];
    // Without fix: panics with "attempt to subtract with overflow" at decoder.rs:731.
    // With fix: returns an Err (UnexpectedEof) instead of panicking.
    let _ = image::load_from_memory(bmp);
    println!("no crash");
}

Run with cargo run --bin repro (debug profile has overflow checks enabled; default features include bmp).

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

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.

1 participant