Bug 871700 (CVE-2012-4564)

Summary: CVE-2012-4564 libtiff: Missing return value check in ppm2tiff leading to heap-buffer overflow when reading a tiff file
Product: [Other] Security Response Reporter: Huzaifa S. Sidhpurwala <huzaifas>
Component: vulnerabilityAssignee: Red Hat Product Security <security-response-team>
Status: CLOSED ERRATA QA Contact:
Severity: medium Docs Contact:
Priority: medium    
Version: unspecifiedCC: jlieskov, security-response-team, tgl
Target Milestone: ---Keywords: Security
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard: impact=moderate,public=20121102,reported=20121031,source=redhat,cvss2=6.8/AV:N/AC:M/Au:N/C:P/I:P/A:P,rhel-5/libtiff=affected,rhel-6/libtiff=affected,fedora-all/libtiff=affected,cwe=CWE-122[auto]
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2012-12-21 03:47:09 EST Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---
Bug Depends On: 880907, 885308, 885309, 885310, 885311    
Bug Blocks: 837581    
Attachments:
Description Flags
proposed patch none

Description Huzaifa S. Sidhpurwala 2012-10-31 02:04:34 EDT
A flaw was found in the way ppm2tiff, a tool to create a TIFF file from PPM, PGM and PBM image files, did not check the return value of TIFFScanlineSize() function. When TIFFScanlineSize encountered an integer-overflow and returned zero, this value was not checked. A remote attacker could provide a specially-crafted PPM image format file, that when processed by ppm2tiff would lead to ppm2tiff executable crash or, potentially, arbitrary code execution with the privileges of the user running the ppm2tiff binary.
Comment 2 Huzaifa S. Sidhpurwala 2012-10-31 02:24:55 EDT
Analysis (I am using libtiff-4.0.3 as the base for the line numbers):
====================================================================

In ppm2tiff.c:241 the following code is used:

buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out));

However TIFFScanlineSize() detects an integer overflow using the following code segment:

        if ((uint64)n!=m)
        {
                TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
                n=0;
        }
        return(n);
}

Due to overflow the return value from TIFFScanlineSize is zero. However ppm2tiff.c does not check this return value and uses zero to malloc memory via _TIFFmalloc. 
_TIFFmalloc is just a wrapper around glibc malloc. As per glibc:

       Even a request for zero bytes (i.e., malloc(0)) returns a
       pointer to something of the minimum allocatable size.

So this causes some memory to be allocated to buf. Later data is written into buf via:

                if (TIFFWriteScanline(out, buf, row, 0) < 0)

This causes a heap-based buffer overflow.
Comment 3 Huzaifa S. Sidhpurwala 2012-10-31 02:26:00 EDT
Created attachment 635949 [details]
proposed patch
Comment 4 Huzaifa S. Sidhpurwala 2012-11-02 06:28:25 EDT
Public via the following Changelog committed upstream:

2012-11-01  Frank Warmerdam  <warmerdam@pobox.com>

        * tools/ppm2tiff.c: avoid zero size buffer vulnerability.
        CVE-2012-4564 - Thanks to Huzaifa Sidhpurwala of the
        Red Hat Security Response team for the fix.
Comment 5 Huzaifa S. Sidhpurwala 2012-11-02 06:32:02 EDT
This issue affects the version of libtiff as shipped with Red Hat Enterprise Linux 5 and 6.

This issue affects the version of libtiff as shipped with Fedora 16 and Fedora 17.
Comment 6 Jan Lieskovsky 2012-11-02 06:33:43 EDT
Acknowledgements:

This issue was discovered by Huzaifa S. Sidhpurwala of Red Hat Security Response Team.
Comment 7 Huzaifa S. Sidhpurwala 2012-11-28 01:13:05 EST
Created libtiff tracking bugs for this issue

Affects: fedora-all [bug 880907]
Comment 9 errata-xmlrpc 2012-12-18 16:10:42 EST
This issue has been addressed in following products:

  Red Hat Enterprise Linux 5
  Red Hat Enterprise Linux 6

Via RHSA-2012:1590 https://rhn.redhat.com/errata/RHSA-2012-1590.html
Comment 10 Fedora Update System 2012-12-30 22:24:20 EST
libtiff-3.9.7-1.fc16 has been pushed to the Fedora 16 stable repository.  If problems still persist, please make note of it in this bug report.
Comment 11 Fedora Update System 2012-12-30 22:28:03 EST
libtiff-3.9.7-1.fc17 has been pushed to the Fedora 17 stable repository.  If problems still persist, please make note of it in this bug report.
Comment 12 Fedora Update System 2013-01-11 19:27:50 EST
libtiff-4.0.3-2.fc18 has been pushed to the Fedora 18 stable repository.  If problems still persist, please make note of it in this bug report.