<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "http://sourceware.org/bugzilla/bugzilla.dtd">

<bugzilla version="4.0.10"
          urlbase="http://sourceware.org/bugzilla/"
          
          maintainer="overseers@sourceware.org"
>

    <bug>
          <bug_id>14547</bug_id>
          
          <creation_ts>2012-09-05 20:59:00 +0000</creation_ts>
          <short_desc>strcoll integer / buffer overflow</short_desc>
          <delta_ts>2012-09-11 15:51:47 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>glibc</product>
          <component>libc</component>
          <version>2.16</version>
          <rep_platform>All</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Joseph Myers">jsm28</reporter>
          <assigned_to name="Not yet assigned to anyone">unassigned</assigned_to>
          <cc>bugdal</cc>
    
    <cc>drepper.fsp</cc>
    
    <cc>ppluzhnikov</cc>
    
    <cc>shaun.colley</cc>
          <cf_gcchost></cf_gcchost>
          <cf_gcctarget></cf_gcctarget>
          <cf_gccbuild></cf_gccbuild>
          

      

      

      

          <long_desc isprivate="0">
            <commentid>57269</commentid>
            <who name="Joseph Myers">jsm28</who>
            <bug_when>2012-09-05 20:59:31 +0000</bug_when>
            <thetext>The code in string/strcoll_l.c that computes a memory allocation size as (s1len + s2len) * (sizeof (int32_t) + 1) fails to allow for possible integer overflow in this computation.  On a 32-bit host this can cause too-small allocations and consequent buffer overflow if the strings total more than 0.8GB.  Testcase:

#include &lt;locale.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

#define SIZE 429496730

int
main (void)
{
  char *p = malloc (1 + SIZE);
  if (setlocale (LC_COLLATE, &quot;en_GB.UTF-8&quot;) == NULL)
    {
      puts (&quot;setlocale failed, cannot test for overflow&quot;);
      return 0;
    }
  if (p == NULL)
    {
      puts (&quot;malloc failed, cannot test for overflow&quot;);
      return 0;
    }
  memset (p, &apos;x&apos;, SIZE);
  p[SIZE] = 0;
  printf (&quot;%d\n&quot;, strcoll (p, p));
  return 0;
}</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <commentid>57270</commentid>
            <who name="Joseph Myers">jsm28</who>
            <bug_when>2012-09-05 21:12:50 +0000</bug_when>
            <thetext>It looks like the same issue is also present in strxfrm (not tested).</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <commentid>57283</commentid>
            <who name="Joseph Myers">jsm28</who>
            <bug_when>2012-09-06 16:54:00 +0000</bug_when>
            <thetext>*** Bug 14552 has been marked as a duplicate of this bug. ***</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <commentid>57309</commentid>
            <who name="Rich Felker">bugdal</who>
            <bug_when>2012-09-08 03:38:25 +0000</bug_when>
            <thetext>Although this bug report regards the serious security vuln in strcoll, even if the overflow issues are fixed, a serious bug will remain. The strcoll interface does not permit failure. It must yield a consistent ordering. If it can fail sporadically from memory exhaustion, it can cause other interfaces using it (such as qsort) which rely on it to be a consistent ordering to invoke undefined behavior. While an immediate security fix is needed for the issues reported here, the implementation of strcoll calls for drastic redesign to be completely free of malloc or any other operation that could fail.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <commentid>57357</commentid>
            <who name="Shaun Colley">shaun.colley</who>
            <bug_when>2012-09-11 09:53:33 +0000</bug_when>
            <thetext>I&apos;ve detailed another strcoll() security vulnerability below, which is an unbounded alloca() call.


alloca() stack overflow

If the malloc() call in alloca() fails (i.e. OOM conditions), strcoll() will failsafe to alloca() for allocating its memory, which could result in unbounded alloca() calls and exploitable
conditions if the stack pointer is shifted over the guard area and into the
heap. See vulnerable code below.


       if (idx1arr == NULL)
       /* No memory.  Well, go with the stack then.

          XXX Once this implementation is stable we will handle this
          differently.  Instead of precomputing the indeces we will
          do this in time.  This means, though, that this happens for
          every pass again.  */
          goto try_stack;
          use_malloc = 1;
       }
     else
       {
       try_stack:
         idx1arr = (int32_t *) alloca (s1len * sizeof (int32_t));
         idx2arr = (int32_t *) alloca (s2len * sizeof (int32_t));
         rule1arr = (unsigned char *) alloca (s1len);
         rule2arr = (unsigned char *) alloca (s2len);

[ ... ]


Here&apos;s my testcase / proof-of-concept for the issue.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;locale.h&gt;

#define LEN 500000 

int main() {

char *ptr1 = malloc(LEN + 1);
char *ptr2 = malloc(LEN + 1);
char *wasted = NULL;
int i = 0, ret = 0;

if(!ptr1 || !ptr2) {
    printf(&quot;memory allocation failed\n&quot;);
    return -1;
}

memset(ptr1, 0x61, LEN);
memset(ptr2, 0x61, LEN); 

ptr1[LEN] = 0;
ptr2[LEN] = 0;

printf(&quot;strings allocated\n&quot;);

char *ptr = setlocale(LC_ALL, &quot;en_US.UTF-8&quot;);
if(!ptr) {
    printf(&quot;error setting locale\n&quot;);
    return -1;
}

/* malloc() big chunks until we&apos;re out of memory */
do {    
wasted = malloc(1000000);
printf(&quot;%p\n&quot;, wasted);
i++;
} while(wasted);

ret = strcoll(ptr1, ptr2);

if(!ret) {
    printf(&quot;strings were lexicographically identical\n&quot;);
}

else {
    printf(&quot;strings were different\n&quot;);
}

return 0;
}



Cheers,
Shaun</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <commentid>57359</commentid>
            <who name="Shaun Colley">shaun.colley</who>
            <bug_when>2012-09-11 15:51:47 +0000</bug_when>
            <thetext>The unbounded alloca issue also appears to be present in strxfrm.</thetext>
          </long_desc>
      
      

    </bug>

</bugzilla>