Skip to content

Fix #80663: Recursive SplFixedArray::setSize() may cause double-free #7503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from

Conversation

cmb69
Copy link
Member

@cmb69 cmb69 commented Sep 20, 2021

We address the ::setSize(0) case by setting array->element = NULL
before we destroy the elements, and bail in this case out on re-entry
into spl_fixedarray_resize().

This is an alternative solution to PR #7485 based on @nikic's suggestion.

We address the `::setSize(0)` case by setting `array->element = NULL`
before we destroy the elements, and bail in this case out on re-entry
into `spl_fixedarray_resize()`.
@cmb69 cmb69 added the Bug label Sep 20, 2021
Co-authored-by: Tyson Andre <tyson.andre@uwaterloo.ca>
@cmb69
Copy link
Member Author

cmb69 commented Sep 21, 2021

The new test case (bug80663.phpt) was failing after the last commit; I've updated the test accordingly. Are we still good with this change for PHP-7.4?

Also, there are relevant changes in PHP-8.0 (merge conflict); would the following patch be okay?

 ext/spl/spl_fixedarray.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/ext/spl/spl_fixedarray.c b/ext/spl/spl_fixedarray.c
index 4b3f0dcb80..cf4d371ade 100644
--- a/ext/spl/spl_fixedarray.c
+++ b/ext/spl/spl_fixedarray.c
@@ -171,8 +171,20 @@ static void spl_fixedarray_resize(spl_fixedarray *array, zend_long size)
 
 	/* clearing the array */
 	if (size == 0) {
-		spl_fixedarray_dtor(array);
-		array->elements = NULL;
+		if (array->elements != NULL) {
+			zend_long i;
+			zval *elements = array->elements;
+
+			array->elements = NULL;
+			array->size = 0;
+
+			for (i = 0; i < array->size; i++) {
+				zval_ptr_dtor(&(elements[i]));
+			}
+
+			efree(elements);
+			return;
+		}
 	} else if (size > array->size) {
 		array->elements = safe_erealloc(array->elements, size, sizeof(zval), 0);
 		spl_fixedarray_init_elems(array, array->size, size);

This would avoid calling spl_fixedarray_dtor:

static void spl_fixedarray_dtor(spl_fixedarray *array)
{
zend_long size = array->size;
if (!spl_fixedarray_empty(array)) {
spl_fixedarray_dtor_range(array, 0, size);
efree(array->elements);
}
}

Copy link
Contributor

@TysonAndre TysonAndre left a comment

Choose a reason for hiding this comment

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

This PR should change array->size to size, see above comment

We actually need to destroy all elements.
@cmb69
Copy link
Member Author

cmb69 commented Sep 21, 2021

The new test case (bug80663.phpt) was failing after the last commit; […]

That was due to an erroneous change. This should be fixed now, but there's still the question how to solve this in PHP-8.0+.

@nikic
Copy link
Member

nikic commented Sep 23, 2021

The new test case (bug80663.phpt) was failing after the last commit; […]

That was due to an erroneous change. This should be fixed now, but there's still the question how to solve this in PHP-8.0+.

I'd suggest updating spl_fixedarray_dtor to do the early elements/size reset.

@cmb69 cmb69 closed this in 2d66840 Sep 28, 2021
@cmb69
Copy link
Member Author

cmb69 commented Sep 28, 2021

I'd suggest updating spl_fixedarray_dtor to do the early elements/size reset.

Thanks. Done.

@cmb69 cmb69 deleted the cmb/80663a branch September 28, 2021 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants