]> granicus.if.org Git - curl/commitdiff
aprintf: detect wrap-around when growing allocation
authorDaniel Stenberg <daniel@haxx.se>
Wed, 28 Sep 2016 08:15:34 +0000 (10:15 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 31 Oct 2016 07:46:35 +0000 (08:46 +0100)
On 32bit systems we could otherwise wrap around after 2GB and allocate 0
bytes and crash.

CVE-2016-8618

Bug: https://curl.haxx.se/docs/adv_20161102D.html
Reported-by: Cure53
lib/mprintf.c

index dbedeaa1836e20a3fd577da1278b283f26cee354..2c88aa833b1b1fbb50d9b33aa56a59279595e41a 100644 (file)
@@ -1036,16 +1036,19 @@ static int alloc_addbyter(int output, FILE *data)
     infop->len =0;
   }
   else if(infop->len+1 >= infop->alloc) {
-    char *newptr;
+    char *newptr = NULL;
+    size_t newsize = infop->alloc*2;
 
-    newptr = realloc(infop->buffer, infop->alloc*2);
+    /* detect wrap-around or other overflow problems */
+    if(newsize > infop->alloc)
+      newptr = realloc(infop->buffer, newsize);
 
     if(!newptr) {
       infop->fail = 1;
       return -1; /* fail */
     }
     infop->buffer = newptr;
-    infop->alloc *= 2;
+    infop->alloc = newsize;
   }
 
   infop->buffer[ infop->len ] = outc;