Skip to content

Fix GH-16321: UAF in list_entry_destructor when resource dtor adds references#21686

Open
iliaal wants to merge 1 commit intophp:masterfrom
iliaal:fix/gh-16321-stream-filter-uaf
Open

Fix GH-16321: UAF in list_entry_destructor when resource dtor adds references#21686
iliaal wants to merge 1 commit intophp:masterfrom
iliaal:fix/gh-16321-stream-filter-uaf

Conversation

@iliaal
Copy link
Copy Markdown
Contributor

@iliaal iliaal commented Apr 9, 2026

Fixes #16321

list_entry_destructor() freed the resource struct unconditionally after calling its destructor. If user code ran during the destructor (e.g. a stream filter callback) and something captured a new reference to the resource (e.g. an exception backtrace), the struct was freed while still referenced, causing a heap-use-after-free in _build_trace_args().

Bumps the resource refcount before calling the destructor so reentrant calls to zend_list_free() can't trigger a premature free. After the destructor returns, skips the free if the refcount is still elevated. Adds a fallback path in zend_list_free() to free directly when the resource has already been removed from the list.

… references

list_entry_destructor() freed the resource struct unconditionally after
calling its destructor. If user code ran during the destructor (e.g. a
stream filter callback) and something captured a new reference to the
resource (e.g. an exception backtrace), the struct was freed while still
referenced, causing a heap-use-after-free in _build_trace_args().

Bump the resource refcount before calling the destructor so reentrant
calls to zend_list_free() can't trigger a premature free. After the
destructor returns, skip the free if the refcount is still elevated.
Add a fallback path in zend_list_free() to free directly when the
resource has already been removed from the list.

Closes phpGH-16321
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.

Segmentation fault in Zend/zend_execute.c:4624

1 participant