Bug 725760

Summary: CVE-2011-2895 libXfont: LZW decompression heap corruption / infinite loop
Product: [Other] Security Response Reporter: Tomas Hoger <thoger>
Component: vulnerabilityAssignee: Red Hat Product Security <security-response-team>
Status: CLOSED ERRATA QA Contact:
Severity: high Docs Contact:
Priority: high    
Version: unspecifiedCC: ajax, kem, sandmann, security-response-team
Target Milestone: ---Keywords: Security
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard: impact=important,public=20110810,reported=20110726,source=redhat,cvss2=6.9/AV:L/AC:M/Au:N/C:C/I:C/A:C,rhel-4/xorg-x11=affected,rhel-5.6.z/libXfont=affected,rhel-5/libXfont=affected,rhel-6/libXfont=affected,fedora-all/libXfont=affected,cwe=CWE-835[auto]
Fixed In Version: libXfont 1.4.4 Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2012-06-20 11:24:28 EDT Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---
Bug Depends On: 728442, 728443, 728444, 728445, 728447, 729870, 752934, 833925    
Bug Blocks: 714279, 727624, 752971    
Attachments:
Description Flags
My proposed patch - variant 1
none
My proposed patch - variant 2
none
Updated proposed patch thoger: review? (sandmann), thoger: review? (ajax)

Description Tomas Hoger 2011-07-26 09:41:27 EDT
libXfont contains a compress / LZW decompressor implementation.  This implementation does not correctly handle compressed streams that contain code words that were not yet added to the decompression table.  LZW decompression has a special case (a KwKwK string) when code word matches the first free entry in the decompression table.  The implementation used in libXfont allow code words not only matching, but also exceeding the first free entry.

Relevant code appears in BufCompressedFill() in src/fontfile/decompress.c:

        /*
         * Special case for KwKwK string.
         */
        if ( code >= file->free_ent ) {
            *stackp++ = finchar;
            code = oldcode;
        }

http://cgit.freedesktop.org/xorg/lib/libXfont/tree/src/fontfile/decompress.c?id=f29f1d68d7eca96e45ba3758686be07993d82e03#n249

This allows creating a loop in the decompression table, which leads to an "infinite" loop:

        /*
         * Generate output characters in reverse order
         */
        while ( code >= 256 )
        {
            *stackp++ = file->tab_suffix[code];
            code = file->tab_prefix[code];
        }

http://cgit.freedesktop.org/xorg/lib/libXfont/tree/src/fontfile/decompress.c?id=f29f1d68d7eca96e45ba3758686be07993d82e03#n257

and heap-based buffer overflow (CompressedFile's de_stack[]):

        char_type       de_stack[STACK_SIZE];
        char_type       *tab_suffix;
        unsigned short  *tab_prefix;
    } CompressedFile;

http://cgit.freedesktop.org/xorg/lib/libXfont/tree/src/fontfile/decompress.c?id=f29f1d68d7eca96e45ba3758686be07993d82e03#n121


Once stackp is incremented to point to after de_stack[] buffer end, tab_suffix and tab_prefix pointers are corrupted, which may lead to arbitrary memory corruption.  libXfont is used by X server and can parse arbitrary user-supplied font file.  Successful exploitation may possibly lead to a local privilege escalation.
Comment 1 Tomas Hoger 2011-07-26 09:48:07 EDT
(In reply to comment #0)
> libXfont contains a compress / LZW decompressor implementation.

LZW decompression implementations in libXfont, ncompress and gzip all seem to be based on the same implementation.  ncompress and gzip already contain a check for code > free_ent and abort decompression in such case.  Other code bases derived from the same source (such as GIF parsers in mozilla/libpr0n and gd) check for stack[] overflow in the while loop.
Comment 3 Tomas Hoger 2011-08-04 10:07:23 EDT
Created attachment 516719 [details]
My proposed patch - variant 1

Checks code values, similar to ncompress/gzip.
Comment 4 Tomas Hoger 2011-08-04 10:08:05 EDT
Created attachment 516721 [details]
My proposed patch - variant 2

de_stack overflow check in the while loop, similar to what OpenBSD compress does.
Comment 5 Tomas Hoger 2011-08-04 14:00:34 EDT
Created attachment 516759 [details]
Updated proposed patch
Comment 8 Tomas Hoger 2011-08-10 14:14:05 EDT
Making this public.
Comment 10 Tomas Hoger 2011-08-11 02:18:52 EDT
Created libXfont tracking bugs for this issue

Affects: fedora-all [bug 729870]
Comment 11 errata-xmlrpc 2011-08-11 17:36:16 EDT
This issue has been addressed in following products:

  Red Hat Enterprise Linux 5
  Red Hat Enterprise Linux 6

Via RHSA-2011:1154 https://rhn.redhat.com/errata/RHSA-2011-1154.html
Comment 12 errata-xmlrpc 2011-08-11 17:56:42 EDT
This issue has been addressed in following products:

  Red Hat Enterprise Linux 4

Via RHSA-2011:1155 https://rhn.redhat.com/errata/RHSA-2011-1155.html
Comment 13 Tomas Hoger 2011-09-27 10:10:01 EDT
An improved fix was committed to upstream git repository.  It improves input checks to avoid decompression table loops and increases decompression stack size, instead of performing stack boundary checks:

http://cgit.freedesktop.org/xorg/lib/libXfont/commit/src/fontfile/decompress.c?id=bd48ad11fd11412c62c3ac8ed5d52c4f10a985aa
Comment 15 errata-xmlrpc 2011-12-19 12:51:38 EST
This issue has been addressed in following products:

  Red Hat Enterprise Linux 5.6.Z - Server Only

Via RHSA-2011:1834 https://rhn.redhat.com/errata/RHSA-2011-1834.html