ext/spl: fix use-after-free in RecursiveIteratorIterator on reentrant… by devnexen · Pull Request #22496 · php/php-src · GitHub
Skip to content

ext/spl: fix use-after-free in RecursiveIteratorIterator on reentrant…#22496

Open
devnexen wants to merge 1 commit into
php:masterfrom
devnexen:recursive_iterator_fixes_followup
Open

ext/spl: fix use-after-free in RecursiveIteratorIterator on reentrant…#22496
devnexen wants to merge 1 commit into
php:masterfrom
devnexen:recursive_iterator_fixes_followup

Conversation

@devnexen

Copy link
Copy Markdown
Member

… rewind().

Guard hasChildren()/getChildren() against reentrant rewind()/next() the same way valid() was guarded. Follow-up to GH-22466, GH-22478.

@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 is special crafted code, and I would prefer we would throw an exception when trying to call rewind.

The hasChildren() methods should definitely not be faffing around with the state of the object, and getChildren() explicitly mentions of the current item, so rewinding is also not something that should be permitted:

Returns an iterator for the current iterator entry.

hasChildren() and getChildren() must not mutate the iteration state:
hasChildren() is a predicate and getChildren() returns an iterator for the
*current* item. A reentrant rewind()/next() from within either ran
spl_recursive_it_rewind_ex()/move_forward_ex(), which tore down the active
levels and erealloc()ed object->iterators under the outer move_forward frame,
leaving its cached sub-object and iterators[] dangling (use-after-free).

Track that we are inside such a call and throw an Error if rewind()/next()
re-enters, instead of silently coping with it. The guard is reset across a
bailout so a fatal inside the callback cannot leave the iterator wedged.

Follow-up to phpGH-22466, phpGH-22478.
@LamentXU123

LamentXU123 commented Jun 28, 2026

Copy link
Copy Markdown
Member

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.

3 participants