Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ext/spl/spl_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ static void spl_array_set_array(zval *object, spl_array_object *intern, zval *ar
//??? TODO: try to avoid array duplication
ZVAL_ARR(&intern->array, zend_array_dup(Z_ARR_P(array)));

if (intern->is_child) {
if (intern->is_child && intern->bucket) {
Z_TRY_DELREF(intern->bucket->val);
/*
* replace bucket->val with copied array, so the changes between
Expand Down Expand Up @@ -1018,6 +1018,10 @@ static void spl_array_set_array(zval *object, spl_array_object *intern, zval *ar
intern->ht_iter = (uint32_t)-1;
}

if (intern->is_child) {
intern->bucket = NULL;
}

zval_ptr_dtor(&garbage);
}
/* }}} */
Expand Down
17 changes: 17 additions & 0 deletions ext/spl/tests/gh21499.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
GH-21499: RecursiveArrayIterator getChildren UAF after parent free
--FILE--
<?php
$it = new RecursiveArrayIterator([[1]]);
$child = $it->getChildren();
unset($it);
$child->__construct([2, 3]);
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 wonder if the solution is not to ban calling the constructor a second time, this is quite a common thing we enforce. Does this fix the issue or is there another way of triggering it?

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.

Y'all are missing the point of the issue.
I described this somewhere in detail before as I found this a while back too, but it was on a PR and github search sucks.
Storing a bucket pointer is never safe, and manipulating the RC counts is also not safe. If the array resizes or reallocates or gets freed by an external factor, then the bucket pointer is stale and will cause crashes etc.
The original patch that introduced this bucket pointer should simply be reverted as it's completely wrong (as is often the case from the swoole people). If the behaviour is the correct one (it was the same behaviour for some versions in the PHP 5.3.x days), then the way to fix this is not by storing a bucket pointer, but by storing the key and making sure the modifications go through the regular zend_hash... APIs with that key.

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.

Yeah I was wondering why we were storing the bucket in the first place, and I mentioned that on the other PR that targeted master as it didn't seem safe.

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 propose to revert the original commit that introduced it. The problem it causes (i.e. silent memory corruption) is far worse than the original issue it tried to solve.

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.

Yeah, agreed. Let me see if I can figure out a patch with the revert.

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.

True, lets close this patch instead.

var_dump($child->getArrayCopy());
?>
--EXPECT--
array(2) {
[0]=>
int(2)
[1]=>
int(3)
}
Loading