Skip to content

PluginBMP: 32-bit bound for RLE4 pixel count (stacked on #216) - #221

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

danoli3 merged 1 commit into
masterfrom
fix/bmp-rle4-size-overflow-32bit

Conversation

@danoli3

@danoli3 danoli3 commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Stacked on #216. This branch contains #216's commit (a8c20af, authorship kept) plus one follow-up commit. Merge it with a merge commit, not squash or rebase. That puts #216's head commit on master unchanged, so GitHub marks #216 as merged with its author credited.

Change on top of #216

  • 32-bit bound. (size_t)width * (size_t)height only stops the wrap where size_t is 64 bits. On Win32, MinGW32, armv7 and wasm32, 0x40000001 * 4 still wraps to 4. Dimensions whose product does not fit in size_t are now rejected:
    if (width <= 0 || height <= 0 || (size_t)width > ((size_t)-1) / (size_t)height) {
    Reachability, stated honestly: the caller's DIB (about width * height / 2 bytes, overflow-checked in FreeImage_GetInternalImageSize) has to be allocated first. That needs roughly 2 GB contiguous in a 32-bit process. So this is defence in depth rather than a demonstrated 32-bit exploit.
  • Dead guard removed. The check added to the 4-bit conversion loop could never fire: src - pixels == y * width with y < height, so + width <= pixel_count always holds. A truncated stream leaves the memset zeros in place; it never moves src.

On 64-bit, behaviour is identical to #216.

Verification (macOS arm64)

Standalone harness with a crafted in-memory RLE4 BMP:

Case Before #216 (8fea9cd) #216 + this PR (ASan + UBSan)
valid 4×2 (encoded runs, EOL, EOB) 12 34 / 55 55 12 34 / 55 55
truncated stream NULL + "Error encountered while decoding RLE4 BMP data" same
width=0x40000001, height=4 SIGBUS loads, no sanitizer findings

The full ctest suite passes (11/11) under ASan + UBSan. The 32-bit path itself relies on the MinGW32 and armeabi-v7a CI jobs compiling it; neither job can practically reach it, for the allocation reason above.

Follow-up to the CVE-2023-47994 fix. Widening width * height to size_t
only prevents the wrap where size_t is 64 bits. On 32-bit targets
(Win32, MinGW32, armv7, wasm32) (size_t)0x40000001 * 4 still wraps to 4.
Reject the dimensions when the product does not fit in size_t.

In practice the caller's DIB allocation (about width * height / 2
bytes, overflow-checked in FreeImage_GetInternalImageSize) has to
succeed first, which needs roughly 2 GB contiguous in a 32-bit process.
So this is defence in depth that no longer depends on allocator limits.

Also drop the bounds check in the 4-bit conversion loop. src - pixels
is y * width with y < height, so src - pixels + width never exceeds
pixel_count and the check could not fire. A truncated stream leaves the
memset zeros in place; it does not move src.
@danoli3
danoli3 force-pushed the fix/bmp-rle4-size-overflow-32bit branch from a55cdc5 to 5be4529 Compare October 27, 2026 12:36
@danoli3
danoli3 merged commit 6cf7de4 into master Oct 27, 2026
73 checks passed
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