Bug 14552 (CVE-2012-4424)

Summary: Two security issues in strcoll() function
Product: glibc Reporter: Shaun Colley <shaun.colley>
Component: libcAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: drepper.fsp, fweimer
Priority: P2 Flags: fweimer: security-
Version: 2.17   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

Description Shaun Colley 2012-09-06 16:18:35 UTC
There are two problems with the strcoll() interface.


1) alloca() stack overflow

If the malloc() call fails (i.e. OOM conditions), strcoll() will failsafe back to alloca(), 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)    // [5] memory allocation failed, use alloca() ...
       /* 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));   // [6] stack pointer shifting
         idx2arr = (int32_t *) alloca (s2len * sizeof (int32_t));
         rule1arr = (unsigned char *) alloca (s1len);
         rule2arr = (unsigned char *) alloca (s2len);


Here's my testcase.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>

#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("memory allocation failed\n");
	return -1;
}

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

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

printf("strings allocated\n");

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

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

ret = strcoll(ptr1, ptr2);

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

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

return 0;
}



2) Integer overflows in the malloc() memory allocation.

int
  STRCOLL (s1, s2, l)
         const STRING_TYPE *s1;
         const STRING_TYPE *s2;
         __locale_t l;
    {

	[ … ]

  /* We need this a few times.  */
     s1len = STRLEN (s1);
     s2len = STRLEN (s2);

	[ … ]

    Please note that the localedef programs makes sure that `position'
    is not used at the first level.  */
    if (! __libc_use_alloca ((s1len + s2len) * (sizeof (int32_t) + 1)))  // [1] if arithmetic is greater 65536, use malloc() instead of alloca()
     {
       idx1arr = (int32_t *) malloc ((s1len + s2len) * (sizeof (int32_t) + 1));  // [2] attempt to get memory using malloc()


If s1 and s2 point to long strongs, the arithmetic in the malloc() argument may give an integer overflow, and result in subsequent heap corruption.


Cheers,

Shaun
Comment 1 Shaun Colley 2012-09-06 16:22:41 UTC
I've pasted the more complete fragment of code for the unbounded alloca() issue below.

 /* We need this a few times.  */
     s1len = STRLEN (s1);
     s2len = STRLEN (s2);

	[ … ]

    Please note that the localedef programs makes sure that `position'
    is not used at the first level.  */
    if (! __libc_use_alloca ((s1len + s2len) * (sizeof (int32_t) + 1)))  // [1] if arithmetic is greater 65536, use malloc() instead of alloca()
     {
       idx1arr = (int32_t *) malloc ((s1len + s2len) * (sizeof (int32_t) + 1)); 
       idx2arr = &idx1arr[s1len];   
       rule1arr = (unsigned char *) &idx2arr[s2len];
       rule2arr = &rule1arr[s1len];  

        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);
       }
Comment 2 Joseph Myers 2012-09-06 16:54:00 UTC
Duplicate of 14547, please add the extra details there.

*** This bug has been marked as a duplicate of bug 14547 ***
Comment 3 Florian Weimer 2014-06-17 04:45:18 UTC
This was assigned CVE-2012-4412 and CVE-2012-4424. See bug 14547 for details.
Comment 4 Florian Weimer 2015-02-24 11:35:49 UTC
Reusing this bug as the holder for one of the CVE aliases.