Bug 1672207 (CVE-2019-6977)

Summary: CVE-2019-6977 gd: Heap based buffer overflow in gdImageColorMatch() in gd_color_match.c
Product: [Other] Security Response Reporter: msiddiqu
Component: vulnerabilityAssignee: Red Hat Product Security <security-response-team>
Status: CLOSED ERRATA QA Contact:
Severity: medium Docs Contact:
Priority: medium    
Version: unspecifiedCC: caillon+fedoraproject, caolanm, databases-maint, hhorak, john.j5live, jorton, mbarnes, panovotn, patalber, rcollet, rhughes, rstrode, sandmann, webstack-team
Target Milestone: ---Keywords: Security
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
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: 2019-08-19 08:47:33 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Bug Depends On: 1678104, 1672209, 1672210, 1672240, 1678101, 1678102, 1678103, 1678105, 1678106, 1678107    
Bug Blocks: 1672246    

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);
?>

Comment 9 errata-xmlrpc 2019-08-19 08:42:37 UTC
This issue has been addressed in the following products:

  Red Hat Software Collections for Red Hat Enterprise Linux 7
  Red Hat Software Collections for Red Hat Enterprise Linux 7.4 EUS
  Red Hat Software Collections for Red Hat Enterprise Linux 7.5 EUS
  Red Hat Software Collections for Red Hat Enterprise Linux 7.6 EUS

Via RHSA-2019:2519 https://access.redhat.com/errata/RHSA-2019:2519

Comment 10 Product Security DevOps Team 2019-08-19 08:47:33 UTC
This bug is now closed. Further updates for individual products will be reflected on the CVE page(s):

https://access.redhat.com/security/cve/cve-2019-6977

Comment 11 errata-xmlrpc 2019-11-01 13:01:17 UTC
This issue has been addressed in the following products:

  Red Hat Software Collections for Red Hat Enterprise Linux 7
  Red Hat Software Collections for Red Hat Enterprise Linux 7.5 EUS
  Red Hat Software Collections for Red Hat Enterprise Linux 7.6 EUS
  Red Hat Software Collections for Red Hat Enterprise Linux 7.7 EUS

Via RHSA-2019:3299 https://access.redhat.com/errata/RHSA-2019:3299