Bug 1672207 (CVE-2019-6977) - CVE-2019-6977 gd: heap based buffer overflow in gd_color_match.c:gdImageColorMatch() in libgd as used in imagecolormatch()
Summary: CVE-2019-6977 gd: heap based buffer overflow in gd_color_match.c:gdImageColor...
Status: NEW
Alias: CVE-2019-6977
Product: Security Response
Classification: Other
Component: vulnerability   
(Show other bugs)
Version: unspecified
Hardware: All
OS: Linux
medium
medium
Target Milestone: ---
Assignee: Red Hat Product Security
QA Contact:
URL:
Whiteboard: impact=moderate,public=20181209,repor...
Keywords: Security
Depends On: 1672210 1678101 1678103 1678104 1678105 1678106 1678107 1672209 1672240 1678102
Blocks: 1672246
TreeView+ depends on / blocked
 
Reported: 2019-02-04 09:34 UTC by msiddiqu
Modified: 2019-03-19 18:40 UTC (History)
21 users (show)

Fixed In Version: php 5.6.40, php 7.1.26, php 7.2.14, php 7.3.1
Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed:
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

Description msiddiqu 2019-02-04 09:34:41 UTC
gdImageColorMatch in gd_color_match.c in the GD Graphics Library (aka LibGD) 2.2.5, as used in the imagecolormatch function in PHP before 5.6.40, 7.x before 7.1.26, 7.2.x before 7.2.14, and 7.3.x before 7.3.1, has a heap-based buffer overflow. This can be exploited by an attacker who is able to trigger imagecolormatch calls with crafted image data.

Upstream patch:

https://gist.github.com/cmb69/1f36d285eb297ed326f5c821d7aafced

Comment 2 msiddiqu 2019-02-04 09:40:05 UTC
Reference: 
https://bugs.php.net/bug.php?id=77270

Comment 3 msiddiqu 2019-02-04 09:40:52 UTC
Created gd tracking bugs for this issue:

Affects: fedora-all [bug 1672210]


Created php tracking bugs for this issue:

Affects: fedora-all [bug 1672209]

Comment 4 msiddiqu 2019-02-04 10:55:55 UTC
Created libwmf tracking bugs for this issue:

Affects: fedora-all [bug 1672240]

Comment 5 Huzaifa S. Sidhpurwala 2019-02-18 06:06:16 UTC
Analysis:

This is essentially a flaw in gdImageColorMatch(), which can be triggered via the PHP API imagecolormatch() [http://php.net/manual/en/function.imagecolormatch.php]

The imagecolormatch() takes two gdImagePtr as arguments and compares them. It allocates a buffer based the following:
buf = (unsigned long *)safe_emalloc(sizeof(unsigned long), 5 * im2->colorsTotal, 0);

Here im2>colorsTotal comes from the second image being compared and therefore is under the controller of the attacker. By simply allocating only one color to the second image, the calculation becomes sizeof(unsigned long) (8 byte on a 64 bit system) * 5 * 1, which results in a buffer of 40 bytes.

However, an attacker can set the value of color to be at maximum 255 (since it is a char). This would result in bp pointing at buffer + 1275 bytes. Since buffer is only 40 bytes big, this leads to an out of bounds write with data that is also under the control of the attacker.

The attacker needs to be able to upload a specially crafted image to a PHP script, which uses the imagecolormatch() function in order to trigger this flaw.

Comment 7 Huzaifa S. Sidhpurwala 2019-02-18 06:22:38 UTC
Simple way to reproduce from https://gist.github.com/cmb69/1f36d285eb297ed326f5c821d7aafced

<?php
$img1 = imagecreatetruecolor(0xfff, 0xfff);
$img2 = imagecreate(0xfff, 0xfff);
imagecolorallocate($img2, 0, 0, 0);
imagesetpixel($img2, 0, 0, 255);
imagecolormatch($img1, $img2);
?>


Note You need to log in before you can comment on or make changes to this bug.