This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/14552] New: Two security issues in strcoll() function


http://sourceware.org/bugzilla/show_bug.cgi?id=14552

             Bug #: 14552
           Summary: Two security issues in strcoll() function
           Product: glibc
           Version: 2.17
            Status: NEW
          Severity: normal
          Priority: P2
         Component: libc
        AssignedTo: unassigned@sourceware.org
        ReportedBy: shaun.colley@ioactive.com
                CC: drepper.fsp@gmail.com
    Classification: Unclassified


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

-- 
Configure bugmail: http://sourceware.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]