[php-src] PR #14054: Detect heap freelist corruption
| From: | arnaud-lb | Date: | Fri, 26 Apr 2024 14:54:32 +0000 |
| Subject: | [php-src] PR #14054: Detect heap freelist corruption | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-25897@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/14054
Author: arnaud-lb
This detects corruptions of freelist pointers in the heap by using a shadow.
The pointer is checked for equality with its shadow before dereferencing or before moving it to
zend_mm_heap->free_slots. The shadow is obfuscated by XORing with a random key. The
key is refreshed every time the heap is shutdown, and also re-seeded if the heap is shutdown in a
fork, so different processes or requests always have different keys.
The shadow pointer is stored at zend_mm_free_slot + 56 for slots >= 64 bytes, or at the end of
the slot. This requires to increase the minimum block size from 8 to 16.
This is a common technique found in various allocators
[[1]](https://github.com/apple-oss-distributions/xnu/blob/xnu-6153.141.1/osfmk/kern/zalloc.c#L134)
[[2]](https://github.com/torvalds/linux/blob/c942a0cd3603e34dd2d7237e064d9318cb7f9654/mm/slub.c#L485)
[[3]](https://chromium.googlesource.com/chromium/src/+/master/base/allocator/partition_allocator/PartitionAlloc.md).
This makes exploitation of buffer overflows via freelist corruption less practical, as it is now
required to override between 16 and 64 bytes, and to have knowledge of the the per-request key.
Overhead on the Symfony benchmark is ±0.30% wall time, or +0.64% valgrind Ir.