Bug 12852 - glob(3) contains possibly wrapping arguments to malloc
Summary: glob(3) contains possibly wrapping arguments to malloc
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Ulrich Drepper
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-07 12:07 UTC by Michael Matz
Modified: 2014-06-13 10:57 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Matz 2011-06-07 12:07:01 UTC
This problem is related to:
http://securityreason.com/achievement_securityalert/89
http://ftp.netbsd.org/pub/NetBSD/security/advisories/NetBSD-SA2010-008.txt.asc
http://www.oracle.com/technetwork/topics/security/cpujan2011-194091.html
http://support.avaya.com/css/P8/documents/100127892
http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-4754
http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-4755
http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-4756
http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2011-0418

and is mildly security relevant.  The glob implementation (I checked git head)
contains some calls to malloc where the argument is calculated in a way that
integer overflow or wraparound might occur, in effect allocating less memory
than intended, and hence writing to unallocated or unrelated memory.  In
particular I believe these calls to be problematic:

          pglob->gl_pathv = (char **) malloc ((pglob->gl_offs + 1)
                                              * sizeof (char *));

  (gl_offs is size_t, the multiplication by 4/8 can introduce a wraparound,
   leading to the malloc to succeed but with less memory allocated than
   intended. this could be replaced with calloc as the resulting memory is
   cleared anyway)

          new_gl_pathv
            = (char **) realloc (pglob->gl_pathv,
                                 (newcount + 1 + 1) * sizeof (char *));

  (same problem as above, but even worse as newcount is declared as int,
   so on overflow anything might happen)

              new_gl_pathv = (char **) realloc (pglob->gl_pathv,
                                                (newcount + 2)
                                                * sizeof (char *));

  (same as above)

With properly constructed patterns using repeated application of braces
such wraparounds can easily be reproduced.
Comment 1 Ulrich Drepper 2011-07-21 02:56:00 UTC
I added a patch but this has nothing do do with security problems from remote uses.  Only the caller can pass in incorrect values and this feature is hardly ever used in the first place.  It's really only a protection against programming mistakes.