Details
- Primary vector (server auto-convert):
AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H→ 9.8 Critical
- If strictly CLI/manual conversion:
UI:R→ 8.8 High
---
Maintainer Pushbacks — Pre-empted
- “MagickMax makes allocation large.” The row base advances by overflowed `bytes_per_line`, causing row overlap and eventual region exit regardless of total allocation size.
- “We’re 64-bit only.” Code is still incorrect for 32-bit consumers/cross-compiles; also add product guards on 64-bit for correctness/DoS.
- “Resource policy blocks large images.” That’s environment-dependent defense-in-depth; arithmetic must be correct.
---
Remediation (Summary)
Add checked arithmetic around stride computation and enforce a per-row invariant so that the number of bytes emitted per row (row_bytes) always fits within the computed stride (bytes_per_line). Guard multiplication/addition and product computations used for header fields and allocation sizes, and fail early with a clear WidthOrHeightExceedsLimit/ResourceLimitError when values exceed safe bounds.
Concretely:
- Validate width and bits_per_pixel before the stride formula to ensure (width*bpp + 31) cannot overflow a size_t.
- Compute row_bytes for the chosen bpp and assert row_bytes <= bytes_per_line.
- Bound rows * stride before allocating and ensure biSizeImage (DIB 32-bit) cannot overflow.
A full suggested guarded implementation is provided in Appendix A — Full patch (for maintainers).
---
Regression Tests to Include (PR-friendly)
- 32-bit overflow repros (with ASan):
- rows=1, width ≥ 178,956,970, bpp=24 → now cleanly errors.
- rows=2, same bound → no row overlap; clean error.
- 64-bit sanity: Medium images (e.g.,
8192×4096, 24-bpp) round-trip; header’sbiSizeImage = rows * bytes_per_line.
- Packed bpp (1/4/8): Validate
row_bytes = (width*bpp+7)/8(guarded), 4-pad, and payload ≤ stride holds.
---
Attachments (private BMP_Package)
Provided with report: README.md, poc_ppm_generator.py, repro_commands.sh, full_asan_bmp_crash.txt, appendix_a_patch_block.c. (Private gist link with package provided separately.)
---
Disclosure & Coordination
- Reporter: Lumina Mescuwa
- Tested on: i686 Linux container (details in Repro)
- Timeline: August 19th, 2025
---
Appendices
Appendix A — Patch block tailored to bmp.c
Where this hooks in (current code):
- Stride is computed here:
bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
- Header uses
bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
- Allocation uses
AcquireVirtualMemory(image->rows, MagickMax(bytes_per_line, image->columns+256UL)*sizeof(*pixels));
- 24-bpp row loop writes pixels then zero-pads up to
bytes_per_line(so the per-row slot size matters):for (x=3L*(ssize_t)image->columns; x < (ssize_t)bytes_per_line; x++) *q++=0x00;
---
Suggested Patch (minimal surface, guards + invariant)
I recommend this in place of the existing bytes_per_line assignment and the subsequent bmp_info.image_size / allocation block. Keep your macros and local variables as-is.
/* --- PATCH BEGIN: guarded stride, per-row invariant, and product checks --- */
/* 1) Guard the original stride arithmetic (preserve behavior, add checks). */
if (bmp_info.bits_per_pixel == 0 ||
(size_t)image->columns > (SIZE_MAX - 31) / (size_t)bmp_info.bits_per_pixel)
ThrowWriterException(ImageError, "WidthOrHeightExceedsLimit");
size_t _tmp = (size_t)image->columns * (size_t)bmp_info.bits_per_pixel + 31;
/* Divide first; then check the final ×4 won't overflow. */
_tmp /= 32;
if (_tmp > (SIZE_MAX / 4))
ThrowWriterException(ImageError, "WidthOrHeightExceedsLimit");
bytes_per_line = 4 * _tmp; /* same formula as before, now checked */
/* 2) Compute the actual data bytes written per row for the chosen bpp. */
size_t row_bytes;
if (bmp_info.bits_per_pixel == 1 || bmp_info.bits_per_pixel == 4 || bmp_info.bits_per_pixel == 8) {
/* packed: ceil(width*bpp/8) */
if ((size_t)image->columns > (SIZE_MAX - 7) / (size_t)bmp_info.bits_per_pixel)
ThrowWriterException(ImageError, "WidthOrHeightExceedsLimit");
row_bytes = (((size_t)image->columns * (size_t)bmp_info.bits_per_pixel) + 7) >> 3;
} else {
/* 16/24/32 bpp: (bpp/8) * width */
size_t bpp_bytes = (size_t)bmp_info.bits_per_pixel / 8;
if (bpp_bytes == 0 || (size_t)image->columns > SIZE_MAX / bpp_bytes)
ThrowWriterException(ImageError, "WidthOrHeightExceedsLimit");
row_bytes = bpp_bytes * (size_t)image->columns;
}
/* 3) Per-row safety invariant: the payload must fit the stride. */
if (row_bytes > bytes_per_line)
ThrowWriterException(ResourceLimitError, "MemoryAllocationFailed");
/* 4) Guard header size and allocation products. */
if ((size_t)image->rows == 0)
ThrowWriterException(ImageError, "WidthOrHeightExceedsLimit");
/* biSizeImage = rows * bytes_per_line (DIB field is 32-bit) */
if (bytes_per_line > 0xFFFFFFFFu / (size_t)image->rows)
ThrowWriterException(ImageError, "WidthOrHeightExceedsLimit");
bmp_info.image_size = (unsigned int)(bytes_per_line * (size_t)image->rows);
/* Allocation count = rows * stride_used, with existing MagickMax policy. */
size_t _stride = MagickMax(bytes_per_line, (size_t)image->columns + 256UL);
if (_stride > SIZE_MAX / (size_t)image->rows)
ThrowWriterException(ResourceLimitError, "MemoryAllocationFailed");
pixel_info = AcquireVirtualMemory((size_t)image->rows, _stride * sizeof(*pixels));
if (pixel_info == (MemoryInfo *) NULL)
ThrowWriterException(ResourceLimitError, "MemoryAllocationFailed");
pixels = (unsigned char *) GetVirtualMemoryBlob(pixel_info);
/* Optional: keep zeroing aligned to computed header size. */
(void) memset(pixels, 0, (size_t) bmp_info.image_size);
/* --- PATCH END --- */
Why this is the right spot?
- It replaces the unguarded stride line you currently have, without changing the algorithm (still
4*((W*bpp+31)/32)).
- It fixes the header (
biSizeImage) to be a checked product, instead of a potentially wrapped multiplication.
- It guards allocation where you presently allocate
rows × MagickMax(bytes_per_line, columns+256).
- The invariant
row_bytes ≤ bytes_per_lineensures your 24-bpp emission loop (writes 3 bytes/pixel, then pads tobytes_per_line) can never exceed the per-row slot the code relies on.
---
Notes
- Behavior preserved: The stride value for normal images is unchanged; only pathological integer states are rejected.
- Header consistency:
biSizeImage = rows * bytes_per_lineremains true by construction, but now cannot overflow a 32-bit DIB field.
- Defensive alignment: If you prefer, you can compute
bytes_per_lineas((row_bytes + 3) & ~3U); it’s equivalent and may read clearer, but I kept the original formula with guards to minimize diff.