Skip to content

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

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 1 commit into from

Conversation

cmb69
Copy link
Member

@cmb69 cmb69 commented Sep 10, 2021

We disallow recursive calls to SplFixedArray::setSize() to avoid that,
and throw a LogicException instead.


If we are concerned about the BC break (at least some recursive resize operations don't appear to be a real issue), we could ignore the operation and raise E_WARNING instead of throwing.

We disallow recursive calls to SplFixedArray::setSize() to avoid that,
and throw a LogicException instead.
@cmb69 cmb69 added the Bug label Sep 10, 2021
@TysonAndre
Copy link
Contributor

offsetSet and offsetUnset also have potential issues that I filed after seeing this was getting worked on - see https://bugs.php.net/bug.php?id=81429

  • The suggested fix to that bug also fixes a leak when the destructor in offsetSet overwrites the same offset

It's also possible to copy the byte ranges to destroy before shrinking and reallocating the array, separately freeing the byte ranges, but it should be possible to fix even without doing that. I'd went with that approach for https://github.com/TysonAndre/pecl-teds/blob/6ec8dd2037850fa3775f17579ebba86ebd87f35c/teds_vector.c#L454-L472

@cmb69
Copy link
Member Author

cmb69 commented Sep 10, 2021

Thanks, @TysonAndre! I'll have a closer look at this.

@cmb69 cmb69 marked this pull request as draft September 10, 2021 14:14
@@ -43,6 +43,8 @@ ZEND_GET_MODULE(spl_fixedarray)
typedef struct _spl_fixedarray { /* {{{ */
zend_long size;
zval *elements;
unsigned int is_resizing:1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Compared to creating a temporary copy of zvals to destroy, this would also add another 4 or 8 bytes (with 64-bit struct field alignment?) to each SplFixedArray instance (and a tiny bit more time initializing it), when most applications wouldn't use setSize (according to other remarks it was meant more as an escape hatch than as a way to support vector-like functionality)

Some progress was made in reducing SplFixedArray's memory usage and instantiation time in #6552

@TysonAndre TysonAndre marked this pull request as ready for review September 14, 2021 00:58
@TysonAndre TysonAndre marked this pull request as draft September 14, 2021 01:19
@nikic
Copy link
Member

nikic commented Sep 20, 2021

Maybe address the setSize(0) case only first? We just need to move the array->elements = NULL assignment into spl_fixedarray_dtor, before destroying the elements.

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