Skip to content

Fix GH-15836: don't expose a freed stream resource to user filters - #22503

Closed
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/gh-15836-userfilter-dead-stream
Closed

iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/gh-15836-userfilter-dead-stream

Conversation

@iliaal

@iliaal iliaal commented Jun 28, 2026

Copy link
Copy Markdown
Member

A user stream filter that reads $this->stream during the close flush could capture an already-freed stream resource in an exception backtrace, a use-after-free that corrupts the heap (the reporter's php://memory reproducer aborts with "zend_mm_heap corrupted").

When _php_stream_free() runs the write-filter close flush from the resource destructor, the resource is already dtor'd (type == -1) and about to be efree'd, but userfilter_filter() still handed it to userland via $this->stream. The guard exposes the resource only while it is still live and assigns null otherwise. The explicit fclose() path flushes before zend_list_close(), so its resource is still live and behavior there is unchanged. The null branch intentionally skips php_stream_to_zval()'s __exposed=1 side effect, since the resource is not actually exposed.

Fixes #15836

When a stream is freed from its resource destructor, the on-close
write-filter flush runs the user filter callback while the stream's
resource is already dtor'd (type == -1) and about to be freed. Exposing
it through $this->stream let user code capture the dead resource in an
exception backtrace, a use-after-free. Assign null when the resource is
no longer live; the explicit fclose() flush still runs before the
resource is closed, so live streams are unaffected.

Fixes phpGH-15836

@arnaud-lb arnaud-lb 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 looks good to me.

Maybe in master we can refactor zend_list_free() so that it calls the resource dtor first, and then frees it and deletes it from the list only if its refcount is still 0. But since we are going to eventually replace resources by objects this may not be worth it.

@iliaal

iliaal commented Jul 24, 2026

Copy link
Copy Markdown
Member Author

That's a good idea, but bigger change in scope. Probably something for 8.6 or later to tackle

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.

2 participants