Przeglądaj źródła

malloc: handle size overflows in realloc()

The malloc() code checks the incoming size to make sure the header
adjustment doesn't cause overflow in the size storage.  Add the same
check to realloc() to catch stupid stuff like realloc(..., -1).

Reported-by: James Coleman <james.coleman@ubicom.com>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Mike Frysinger 14 lat temu
rodzic
commit
07e0ce9fa7
1 zmienionych plików z 3 dodań i 0 usunięć
  1. 3 0
      libc/stdlib/malloc/realloc.c

+ 3 - 0
libc/stdlib/malloc/realloc.c

@@ -34,6 +34,9 @@ realloc (void *mem, size_t new_size)
     }
     }
   if (! mem)
   if (! mem)
     return malloc (new_size);
     return malloc (new_size);
+  /* This matches the check in malloc() */
+  if (unlikely(((unsigned long)new_size > (unsigned long)(MALLOC_HEADER_SIZE*-2))))
+    return NULL;
 
 
   /* Normal realloc.  */
   /* Normal realloc.  */