Unverified Commit 9824fe3e authored by Yossi Gottlieb's avatar Yossi Gottlieb Committed by GitHub
Browse files

Fix wrong zmalloc_size() assumption. (#7963)

When using a system with no malloc_usable_size(), zmalloc_size() assumed
that the heap allocator always returns blocks that are long-padded.

This may not always be the case, and will result with zmalloc_size()
returning a size that is bigger than allocated. At least in one case
this leads to out of bound write, process crash and a potential security
vulnerability.

Effectively this does not affect the vast majority of users, who use
jemalloc or glibc.

This problem along with a (different) fix was reported by Drew DeVault.
parent 4e2e5be2
...@@ -236,9 +236,6 @@ void *zrealloc_usable(void *ptr, size_t size, size_t *usable) { ...@@ -236,9 +236,6 @@ void *zrealloc_usable(void *ptr, size_t size, size_t *usable) {
size_t zmalloc_size(void *ptr) { size_t zmalloc_size(void *ptr) {
void *realptr = (char*)ptr-PREFIX_SIZE; void *realptr = (char*)ptr-PREFIX_SIZE;
size_t size = *((size_t*)realptr); size_t size = *((size_t*)realptr);
/* Assume at least that all the allocations are padded at sizeof(long) by
* the underlying allocator. */
if (size&(sizeof(long)-1)) size += sizeof(long)-(size&(sizeof(long)-1));
return size+PREFIX_SIZE; return size+PREFIX_SIZE;
} }
size_t zmalloc_usable_size(void *ptr) { size_t zmalloc_usable_size(void *ptr) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment