Bug 1269553 (CVE-2015-8895)

Summary: CVE-2015-8895 ImageMagick: Integer and buffer overflow in coders/icon.c
Product: [Other] Security Response Reporter: Adam Mariš <amaris>
Component: vulnerabilityAssignee: Red Hat Product Security <security-response-team>
Status: CLOSED ERRATA QA Contact:
Severity: medium Docs Contact:
Priority: medium    
Version: unspecifiedCC: bleanhar, ccoleman, dmcphers, ethan, gnaik, jhorak, jialiu, jkeck, jokerman, lmeyer, mmccomas, nmurray, pahan, slawomir, yozone
Target Milestone: ---Keywords: Security
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2019-06-08 02:43:51 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On: 1269556, 1341777, 1341778, 1341779, 1341780    
Bug Blocks: 1269554, 1333419    

Description Adam Mariš 2015-10-07 14:29:05 UTC
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.

Vulnerable code:

icon_file.directory[i].offset=ReadBlobLSBLong(image);

On this line, it reads in 4byte value from the image and stores it in icon_file.directory[i].size.

length=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:

(void) CopyMagickMemory(png,"\211PNG\r\n\032\n\000\000\000\015",12);
png[12]=(unsigned char) icon_info.planes;
png[13]=(unsigned char) (icon_info.planes >> 8);
png[14]=(unsigned char) icon_info.bits_per_pixel;
png[15]=(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.

count=ReadBlob(image,length-16,png+16);

Detailed stacktrace with reproducer can be found here:

https://bugs.launchpad.net/ubuntu/+source/imagemagick/+bug/1459747

Upstream patch (only the "coders/icon.c" subsection of the commit is relevant):

https://github.com/ImageMagick/ImageMagick/commit/0f6fc2d5bf8f500820c3dbcf0d23ee14f2d9f734

CVE request:

http://seclists.org/oss-sec/2015/q4/45

Comment 1 Adam Mariš 2015-10-07 14:32:58 UTC
Created ImageMagick tracking bugs for this issue:

Affects: fedora-all [bug 1269556]

Comment 4 errata-xmlrpc 2016-06-16 22:47:37 UTC
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