Skip to content

ext/spl: Fix ArrayObject unserialize validation for invalid iterator classes#22090

Merged
Girgias merged 3 commits into
php:masterfrom
arshidkv12:GH-22047
May 29, 2026
Merged

ext/spl: Fix ArrayObject unserialize validation for invalid iterator classes#22090
Girgias merged 3 commits into
php:masterfrom
arshidkv12:GH-22047

Conversation

@arshidkv12

Copy link
Copy Markdown
Contributor

Comment thread ext/spl/spl_array.c Outdated
Comment thread ext/spl/spl_array.c
}

if (!instanceof_function(ce, zend_ce_iterator)) {
if (!instanceof_function(ce, spl_ce_ArrayIterator)) {

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 is more restrictive than the current code. But then I have no idea if it even makes sens to attempt to use any iterator. But in this case the error message needs to be fixed.

@arshidkv12 arshidkv12 May 24, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you.
Please check the following code.

$arrayObject = new ArrayObject(
    [
        4 => 0.0,
        1 => true
    ],
    0,
    "GlobIterator"
);

var_dump( $arrayObject);

Output:

Fatal error: Uncaught TypeError: ArrayObject::__construct(): Argument #3 ($iteratorClass) must be a class name derived from ArrayIterator, GlobIterator given in /Users/arshid/Downloads/php-src/z.php:20
Stack trace:
#0 /Users/arshid/Downloads/php-src/z.php(20): ArrayObject->__construct(Array, 0, 'GlobIterator')
#1 {main}
  thrown in /Users/arshid/Downloads/php-src/z.php on line 20

https://github.com/php/php-src/blob/master/Zend/zend_API.c#L1001

scr

@Girgias

Girgias commented May 29, 2026

Copy link
Copy Markdown
Member

I'm not the most familiar with behaviour relating to unserialisation, maybe @TimWolla has an opinion?

@TimWolla TimWolla 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.

I'm not the most familiar with behaviour relating to unserialisation

This is not really an unserialization question, but rather a question of what “legal inputs are”. As demonstrated in #22090 (comment), the constructor rejected non-ArrayIterators, thus deserialization should as well. This PR seems correct to me.

@Girgias Girgias merged commit d61ff7c into php:master May 29, 2026
19 checks passed
@@ -0,0 +1,19 @@
--TEST--
GH-22047: ArrayObject invalid iterator class in serialized payload

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.

The CREDITS section was missing (which I told the reporter we'll add). Added here: 61e679d We should get in the habit of doing this, I added it to my personal test template a while ago.

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.

The unserialize function with ArrayObject leads to NULL pointer dereference

5 participants