Skip to content

Fix use-after-free when an error handler replaces the ArrayObject backing store - #25469

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

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

Conversation

@nicolas-grekas

Copy link
Copy Markdown
Contributor

An error handler can replace the backing store of an ArrayObject in the middle of an offset access. The table that was fetched before the error is then used after it's freed:

$ao = new ArrayObject(['a' => 1]);
set_error_handler(function () use ($ao) {
    $ao->exchangeArray([]);
});
$ao[1.5];

It's the same for isset($ao[1.5]), for $ao->b .= 'x' on a missing key with ARRAY_AS_PROPS, and for ArrayIterator::seek(1.5) when the handler calls __construct().

This PR fetches the table after the code that can call the handler.

…king store

spl_array_get_dimension_ptr(), spl_array_has_dimension_ex() and
ArrayIterator::seek() fetch the backing HashTable before code that can
call an error handler: get_hash_key() for float and resource offsets,
the "Undefined array key" warnings of read-write accesses, and argument
parsing. The handler can call exchangeArray() or __construct(), which
frees the table that is then read or written.

Fetch the table after that code instead, and look the key up again
after the warning for an unset property of a backing object.

@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 requires a global solution, see #20018

@nicolas-grekas

Copy link
Copy Markdown
Contributor Author

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

@nicolas-grekas
nicolas-grekas deleted the spl-array-error-handler-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