Skip to content

PluginBMP: size_t arithmetic in LoadPixelDataRLE4 (CVE-2023-47994) - #216

Merged
danoli3 merged 1 commit into
danoli3:masterfrom
w4ffl35:fix/bmp-rle4-size-overflow
Oct 27, 2026
Merged

danoli3 merged 1 commit into
danoli3:masterfrom
w4ffl35:fix/bmp-rle4-size-overflow

Conversation

@w4ffl35

@w4ffl35 w4ffl35 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Root cause (CVE-2023-47994)

LoadPixelDataRLE4 multiplies file-driven int width * height in int arithmetic. For huge dimensions this is signed overflow (UBSan pre-fix: signed integer overflow: 4 * 1073741825), wrapping to a tiny pixels buffer while the 4-bit convert loop walks the full width * height range -> heap-buffer-overflow READ past the buffer (ASan pre-fix trace, 64-byte crafted RLE4 BMP with width=0x40000001,height=4).

Fix

  • Reject non-positive dimensions, compute pixel_count in size_t, size end from it.
  • Bound the 4-bit convert loop (and the delta/end-of-line scanline*width pointer math) by the real buffer end so truncated streams stop with an error instead of reading out of bounds.
  • Negative dimensions were already rejected upstream by CheckBitmapInfoHeader; height = abs(height) is now provably safe (top-down RLE4 never reaches here).

Verification (ASan+UBSan, serial build)

  • RED pre-fix (UBSan abort + ASan READ overflow) / GREEN post-fix (clean, graceful) with the crafted PoC; the original thelastede img PoC (negative width) stays clean-NULL via header validation.
  • No regressions: valid RLE4/RLE8/RGB BMPs decode pixel-identically with zero sanitizer findings (RLE8 path untouched).

PoC repos used as reproducers only, never as patch sources.

AI assistance disclosure: this patch was authored with AI assistance (agentic coding CLI) and verified with AddressSanitizer/UBSan red/green runs before submission.

width/height are file-driven ints: width * height in int arithmetic
overflows for huge images (UBSan: 4 * 1073741825), wrapping to a tiny
pixels buffer while the 4-bit convert loop walks width * height bytes
-> heap-buffer-overflow READ past the buffer.

Compute the count in size_t, reject non-positive dimensions, and bound
the convert loop (plus the delta/end-of-line pointer math) by the real
buffer end so truncated streams stop instead of reading out of bounds.

RED (UBSan signed-overflow + ASan heap-buffer-overflow READ with a
64-byte crafted RLE4 BMP, pre-fix) -> GREEN (post-fix) verified; valid
RLE4/RLE8/RGB BMPs still decode pixel-identically.

AI assistance disclosure: this patch was authored with AI assistance
(agentic coding CLI) and verified with AddressSanitizer/UBSan
red/green runs before submission.
@danoli3

danoli3 commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Reviewed against current master.

The core fix is right. width * height in int is signed overflow (UB) and can wrap to a tiny malloc, while the convert loop indexes y * width across the full range. Rejecting non-positive dimensions and sizing the buffer, end and the q = pointer math from a wider type closes the out-of-bounds read on 64-bit.

Issues:

  1. Blocking: 32-bit targets are still vulnerable. (size_t)width * (size_t)height is a 32-bit multiply on ILP32: the MinGW32 CI jobs, Win32, armv7 and wasm32. With the PR's own PoC (width=0x40000001, height=4) pixel_count wraps to 4. You get a 4-byte buffer, and then src = pixels + (size_t)y * width is out of bounds for y >= 1. Add an explicit bound before the multiply:

    if (width <= 0 || height <= 0 || (size_t)width > SIZE_MAX / (size_t)height) {
    	throw(1);
    }
  2. The new guard in the convert loop never fires. src - pixels == y * width with y < height, so (src - pixels) + width <= height * width == pixel_count always holds. The comment says a truncated stream can make src run past the decoded bytes. That isn't true: the buffer is fully allocated and memset to zero up front, so a truncated stream just leaves zeros. Once (1) is in place the overflow can't happen at all, so I'd drop this block, or at least fix the comment so nobody later relies on it as a real bound. The block's indentation is also one level shallower than the surrounding loop body.

  3. Non-blocking: on 64-bit the PoC now does a 4 GiB malloc plus memset, which is a real RSS spike from a 64-byte file. The DIB is already allocated at that size (4bpp), so this isn't new attack surface. Still, a sanity cap (or reusing the DIB's own size limit) would be friendlier.

The RLE_DELTA / end-of-line pointer arithmetic changes are fine: q is still checked against [pixels, end) at the top of the loop.

Verdict: fixes the reported 64-bit crash, but it is not complete until the 32-bit overflow check from (1) is added. Please also remove or fix the dead guard from (2).

@danoli3

danoli3 commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Follow-up: #221 is stacked on this PR. It carries your commit unchanged plus the 32-bit bound, and it drops the conversion-loop guard that could never fire. Merging #221 with a merge commit lands your commit as-is and marks this PR as merged.

A correction to my earlier review. I called the 32-bit overflow "blocking". It is real in the arithmetic, but in practice it's close to unreachable. The caller's DIB allocation (about width * height / 2 bytes, already overflow-checked in FreeImage_GetInternalImageSize) has to succeed first, which needs about 2 GB contiguous in a 32-bit process. The bound in #221 is defence in depth, not a demonstrated bypass of your fix.

I also confirmed your fix locally. Before it, width=0x40000001, height=4 crashes with SIGBUS. With it, the file loads cleanly under ASan + UBSan, and valid RLE4 data decodes identically.

@danoli3

danoli3 commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Screenshot 2026-10-27 at 10 56 19 pm

@w4ffl35

w4ffl35 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the follow-up and the correction on reachability. Happy with #221 carrying this commit unchanged plus the defence-in-depth bound — merging that with a merge commit works for me. The conversion-loop guard removal in #221 is also fine (agreed it could never fire once the RLE count is bounded).

@danoli3
danoli3 merged commit 6576846 into danoli3:master Oct 27, 2026
73 checks passed
danoli3 added a commit that referenced this pull request Oct 27, 2026
PluginBMP: 32-bit bound for RLE4 pixel count (stacked on #216)
@danoli3

danoli3 commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Follow up merged!

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