From 4ae6ecbe961777083b7ce776474c4c34b1005873 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sun, 15 Nov 2026 15:32:26 +0100 Subject: [PATCH] Fix use-after-free when a destructor changes the ArrayObject backing store spl_array_write_dimension_ex() replaces an existing value with zend_hash_update_ind() or zend_hash_index_update(), which destroy the previous value before storing the new one. Its destructor can append to the ArrayObject, which reallocates the table, or call exchangeArray(), which frees it. The new value is then written to freed memory. Store the new value first, then destroy the previous one. --- NEWS | 4 ++ ext/spl/spl_array.c | 28 +++++++++- ...yObject_storage_changed_in_destructor.phpt | 55 +++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 ext/spl/tests/ArrayObject/ArrayObject_storage_changed_in_destructor.phpt diff --git a/NEWS b/NEWS index 64c61f8f121b..e3d9608f6bc0 100644 --- a/NEWS +++ b/NEWS @@ -108,6 +108,10 @@ PHP NEWS . Fixed session upload progress to respect session.use_only_cookies. (jorgsowa) +- SPL: + . Fixed use-after-free in ArrayObject when the destructor of a replaced + value changes the backing store. (Nicolas Grekas) + - Standard: . Fixed dns_get_record() returning partially built records for malformed DNS responses. (Marti2203) diff --git a/ext/spl/spl_array.c b/ext/spl/spl_array.c index b60b3b9c0dac..80e8219fe22c 100644 --- a/ext/spl/spl_array.c +++ b/ext/spl/spl_array.c @@ -460,6 +460,17 @@ static zval *spl_array_read_dimension(zend_object *object, zval *offset, int typ return spl_array_read_dimension_ex(1, object, offset, type, rv); } /* }}} */ +/* Stores the value, then destroys the previous one: its destructor may + * change the storage, which can free the table that data points to. */ +static void spl_array_replace_value(zval *data, zval *value) +{ + zval garbage; + + ZVAL_COPY_VALUE(&garbage, data); + ZVAL_COPY_VALUE(data, value); + zval_ptr_dtor(&garbage); +} + static void spl_array_write_dimension_ex(int check_inherited, zend_object *object, zval *offset, zval *value) /* {{{ */ { spl_array_object *intern = spl_array_from_obj(object); @@ -505,10 +516,23 @@ static void spl_array_write_dimension_ex(int check_inherited, zend_object *objec } if (key.key) { - zend_hash_update_ind(ht, key.key, value); + zval *data = zend_hash_find(ht, key.key); + if (data) { + if (Z_TYPE_P(data) == IS_INDIRECT) { + data = Z_INDIRECT_P(data); + } + spl_array_replace_value(data, value); + } else { + zend_hash_add_new(ht, key.key, value); + } spl_hash_key_release(&key); } else { - zend_hash_index_update(ht, key.h, value); + zval *data = zend_hash_index_find(ht, key.h); + if (data) { + spl_array_replace_value(data, value); + } else { + zend_hash_index_add_new(ht, key.h, value); + } } } /* }}} */ diff --git a/ext/spl/tests/ArrayObject/ArrayObject_storage_changed_in_destructor.phpt b/ext/spl/tests/ArrayObject/ArrayObject_storage_changed_in_destructor.phpt new file mode 100644 index 000000000000..2ea507d1e58e --- /dev/null +++ b/ext/spl/tests/ArrayObject/ArrayObject_storage_changed_in_destructor.phpt @@ -0,0 +1,55 @@ +--TEST-- +ArrayObject: the destructor of a replaced value can change the backing store +--FILE-- +onDestruct)(); + } +} + +function grow(ArrayObject $ao): D +{ + return new D(function () use ($ao) { + // Makes the storage grow, which reallocates it + for ($i = 0; $i < 100; $i++) { + $ao["k$i"] = $i; + } + }); +} + +echo "-- the storage grows, string key\n"; +$ao = new ArrayObject(); +$ao['a'] = grow($ao); +$ao['a'] = 'new'; +var_dump(count($ao), $ao['a']); + +echo "-- the storage grows, integer key\n"; +$ao = new ArrayObject(); +$ao[0] = grow($ao); +$ao[0] = 'new'; +var_dump(count($ao), $ao[0]); + +echo "-- the storage is replaced\n"; +$ao = new ArrayObject(); +$ao['a'] = new D(function () use ($ao) { + $ao->exchangeArray(['replaced' => 1]); +}); +$ao['a'] = 'new'; +var_dump($ao->getArrayCopy()); +?> +--EXPECT-- +-- the storage grows, string key +int(101) +string(3) "new" +-- the storage grows, integer key +int(101) +string(3) "new" +-- the storage is replaced +array(1) { + ["replaced"]=> + int(1) +}