
Proof-of-concept and root-cause analysis for a PHP json_encode() use-after-free caused by HashTable resize during a JsonSerializable callback, with heap-grooming and type-confusion PoCs.
json_encode() Use-After-Free via HashTable Resize in JsonSerializable CallbackSeverity: High (CVSS 8.1)
Affected: PHP 8.4.x, 8.5.x (all current versions as of 2026-10-21)
Component: ext/json — php_json_encode_array() / ZEND_HASH_FOREACH_KEY_VAL_IND
Type: Use-After-Free (stale arData pointer after HashTable resize)
json_encode() iterates an object's property HashTable using ZEND_HASH_FOREACH_KEY_VAL_IND, which captures arData and nNumUsed once at loop start. A nested JsonSerializable::jsonSerialize() callback that adds new properties to the same object triggers a HashTable resize, reallocating arData. The outer loop continues iterating the old (freed) pointer — a use-after-free.
php_json_encode_array() in ext/json/json_encoder.c:
ZEND_HASH_FOREACH_KEY_VAL_IND(myht, ...) {
// callback here can resize myht, freeing myht->arData
// outer loop pointer is now stale
}
No reallocation check between iterations. The fix requires checking for arData pointer changes (as done in other ZEND_HASH_FOREACH paths).
# Basic UAF trigger
php poc.php
# Stronger trigger with heap grooming
php poc_strong.php
# Type confusion via UAF
php poc_type_confusion.php
| File | Description |
|---|---|
poc.php | Basic UAF trigger |
poc_strong.php | UAF with heap grooming for higher reliability |
poc_type_confusion.php | Type confusion primitive via stale arData |
security_disclosure.txt | Disclosure email sent to [email protected] |
Adrian / [email protected]