A vulnerability of integer overflow and subsequent buffer overflow was found in coders/icon.c. Memory is allocated based on the sum of a user-supplied value and a fixed value. That sum can overflow, causing only a small amount of memory to be allocated, while the program assumes more was allocated.
On this line, it reads in 4byte value from the image and stores it in icon_file.directory[i].size.
png=(unsigned char *) AcquireQuantumMemory(length+16,sizeof(*png));
Here the value of `length` is set to the value stored in icon_file.directory[i].size. At following line, 16 is added to `length` and allocates that amount of *png structures. However, if length+16 overflows (for example, length == 2^32 - 15), it will only allocate memory for a single *png. This is a problem, because the following lines assume that at least 16 bytes was allocated:
png=(unsigned char) icon_info.planes;
png=(unsigned char) (icon_info.planes >> 8);
png=(unsigned char) icon_info.bits_per_pixel;
png=(unsigned char) (icon_info.bits_per_pixel >> 8);
And then the following line has a call to ReadBlob, and since length-16 will underflow (and the length is treated as a size_t), it will effectively execute a strcpy with the remaining data in the image file.
Detailed stacktrace with reproducer can be found here:
Upstream patch (only the "coders/icon.c" subsection of the commit is relevant):
Created ImageMagick tracking bugs for this issue:
Affects: fedora-all [bug 1269556]
This issue has been addressed in the following products:
Red Hat Enterprise Linux 6
Red Hat Enterprise Linux 7
Via RHSA-2016:1237 https://access.redhat.com/errata/RHSA-2016:1237