[php-src] PR #14304: Remote heap feng chui / heap spraying protection
| From: | arnaud-lb | Date: | Thu, 23 May 2024 14:28:38 +0000 |
| Subject: | [php-src] PR #14304: Remote heap feng chui / heap spraying protection | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-26280@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/14304
Author: arnaud-lb
This isolates remotely-controlled allocations (user input / request environment) in separate chunks,
to make it more difficult for a remote attacker to precisely control the layout of the heap before
the application starts, or to spray the heap with specific content. The general idea is similar to
https://lwn.net/Articles/965837/ (not necessarily the
implementation).
Design:
The heap is split in two zones, each with its own set of freelists (free_slot) and chunks so that
allocations in a zone do not impact the layout of the other one. There is a notion of current zone,
from which all allocations are made. The current zone is switched to the input zone before handling
a request, and switched back to the default zone after that.
A new field
heap.zone_free_slot points to the freelist of the current zone. The only
change required in allocation operations is to refer to that instead of heap.free_slot.
Similarly, in free operations we find the zone_free_slot in the chunk.
realloc() allocates new blocks in the current zone (if truncation or extension is required),
regardless of the original zone in which the block was allocated, so that it's not possible to
profit of the layout of the original zone.
Future scope would be to activate the input heap in more places (e.g. when parsing json, during
unserialize, etc).
Time overhead is very small (~+0% wall time, +0.13% valgrind).
The minimal memory usage is now 4MiB (two chunks) instead of 2MiB. It may be possible to ensure that
this affects only virtual memory and not the RSS.
This depends on https://github.com/php/php-src/pull/14054 and a
smaller diff can be viewed here: https://github.com/arnaud-lb/php-src/compare/freelist-corrupt...arnaud-lb:php-src:mm-zones.