4 ms·
> OOM in general cannot be used for remote code execution. heap overflow can. That was exactly my point. Using reallocarray() instead of malloc(x * sizeof(foo)
by dezgeg 12y ago
> OOM in general cannot be used for remote code execution. heap overflow can.
That was exactly my point. Using reallocarray() instead of malloc(x * sizeof(foo)) can prevent a heap overflow bug. For a concrete example (assuming 32-bit size_t):
size_t user_input = ...;
size_t* ptr = malloc(user_input * sizeof(size_t));
if (!ptr)
return "FAIL: Out of memory";
for (size_t i = 0; i < user_input; i++)
ptr[i] = whatever();
Now, a user_input of 2^30 + 1 overflows the multiplication so only 4 bytes is allocated, and then the for loop causes a heap overflow. Boom.
But if the code was this:
size_t user_input = ...;
size_t* ptr = reallocarray(NULL, user_input, sizeof(size_t));
if (!ptr)
return "FAIL: Out of memory";
for (size_t i = 0; i < user_input; i++)
ptr[i] = whatever();
Now, a user_input of 2^30 + 1 would cause reallocarray() to return NULL, and the function returns. Heap overflow avoided by using reallocarray().
Of course, that code is still somewhat bad, since a user_input of 2^30 - 1 will cause the program to use large amounts for memory.
- foxhill 12y agoyes, i understand. as you correctly state, user input of 2^30 is.. well, it's unlikely. more likely, is a user being able cause a program to do billions of reallocs. in which case, OOM is going to happen before overflow and exploitation.