mirror of
https://gitlab.com/chrony/chrony.git
synced 2025-12-03 16:35:06 -05:00
memory: check for overflow when (re)allocating array
When (re)allocating an array with very large number of elements using the MallocArray or ReallocArray macros, the calculated size of the array could overflow size_t and less memory would be allocated than requested. Add new functions for (re)allocating arrays that check the size and use them in the MallocArray and ReallocArray macros. This couldn't be exploited, because all arrays that can grow with cmdmon or NTP requests already have their size checked before allocation, or they are much smaller than memory allocated for structures to which they are related (i.e. ntp_core and sourcestats instances), so a memory allocation would fail before their size could overflow. This issue was found in an audit performed by Cure53 and sponsored by Mozilla.
This commit is contained in:
26
memory.c
26
memory.c
@@ -54,6 +54,32 @@ Realloc(void *ptr, size_t size)
|
||||
return r;
|
||||
}
|
||||
|
||||
static size_t
|
||||
get_array_size(size_t nmemb, size_t size)
|
||||
{
|
||||
size_t array_size;
|
||||
|
||||
array_size = nmemb * size;
|
||||
|
||||
/* Check for overflow */
|
||||
if (nmemb > 0 && array_size / nmemb != size)
|
||||
LOG_FATAL("Could not allocate memory");
|
||||
|
||||
return array_size;
|
||||
}
|
||||
|
||||
void *
|
||||
Malloc2(size_t nmemb, size_t size)
|
||||
{
|
||||
return Malloc(get_array_size(nmemb, size));
|
||||
}
|
||||
|
||||
void *
|
||||
Realloc2(void *ptr, size_t nmemb, size_t size)
|
||||
{
|
||||
return Realloc(ptr, get_array_size(nmemb, size));
|
||||
}
|
||||
|
||||
char *
|
||||
Strdup(const char *s)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user