Skip to content

Fix use-after-free when a destructor changes the ArrayObject backing store - #25470

Closed
nicolas-grekas wants to merge 1 commit into
php:PHP-8.4from
nicolas-grekas:spl-array-destructor-uaf
Closed

nicolas-grekas wants to merge 1 commit into
php:PHP-8.4from
nicolas-grekas:spl-array-destructor-uaf

Conversation

@nicolas-grekas

Copy link
Copy Markdown
Contributor

$ao['a'] = $value destroys the previous value before storing the new one. The destructor of the previous value can append to the ArrayObject, which reallocates the table, or call exchangeArray(), which frees it. The new value is then written to freed memory:

class D {
    function __destruct() {
        global $ao;
        for ($i = 0; $i < 100; $i++) {
            $ao["k$i"] = $i;
        }
    }
}
$ao = new ArrayObject(['a' => new D]);
$ao['a'] = 'new';

This PR stores the new value first and destroys the previous one after, like for plain arrays.

…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.

@Girgias Girgias left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This very much feels like a similar case to #20001 and thus I am reluctant do fix it locally.

@nicolas-grekas

Copy link
Copy Markdown
Contributor Author

/cc @arnaud-lb could you please have a look?

@nicolas-grekas

Copy link
Copy Markdown
Contributor Author

Let me close: this needs a global fix, see #20001.

@nicolas-grekas
nicolas-grekas deleted the spl-array-destructor-uaf branch November 16, 2026 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants