Skip to content

Fix UAF/NULL deref on re-entrant watcher dtor in Io\Poll stream close - #25801

Open
hohphu8 wants to merge 1 commit into
php:PHP-8.6from
hohphu8:fix-io-poll-stream-close-reentrancy
Open

hohphu8 wants to merge 1 commit into
php:PHP-8.6from
hohphu8:fix-io-poll-stream-close-reentrancy

Conversation

@hohphu8

@hohphu8 hohphu8 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

In php_io_poll_stream_notify_close() (ext/standard/io_poll.c), calling php_io_poll_context_retire_watcher() while iterating stream->poll_watchers destroys watcher->data. A __destruct() on that user data can re-entrantly call remove() on another watcher or drop another Io\Poll\Context watching the same stream, causing a NULL dereference or heap UAF (SIGSEGV) on later iterations. php_io_poll_watcher_modify_data() also destroys watcher->data before copying in the new value.

This splits php_io_poll_stream_notify_close() into two passes (first detaching all watchers with watcher->stream = NULL, watcher->closed = true, and GC_ADDREF(&watcher->std); then retiring active watchers and calling OBJ_RELEASE), and swaps watcher->data to a local old_data before calling zval_ptr_dtor(&old_data) in php_io_poll_watcher_modify_data().

Adds ext/standard/tests/poll/poll_stream_close_reentrant_dtor.phpt covering both re-entrant remove() and context destruction during fclose(). Verified SIGSEGV -> PASS on PHP-8.6 (Debug + ZTS) with ext/standard/tests/poll/ passing (52/52).

Found via AI-assisted source inspection and verified it myself.

@bukka bukka 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.

It looks good. Just couple of things.

Comment thread ext/standard/io_poll.c
Comment on lines +393 to 399
ZEND_HASH_FOREACH_VAL(watchers, zval *zv) {
php_io_poll_watcher_object *watcher = Z_PTR_P(zv);
if (watcher->context) {
php_io_poll_context_retire_watcher(watcher->context, watcher);
}
OBJ_RELEASE(&watcher->std);
} ZEND_HASH_FOREACH_END();

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.

The destructor can still see the later watchers of the same stream as active here (closed is set but they are still registered in the backend). It doesn't crash but it would be cleaner to retire all of them first and do the release in a separate loop, so no user code runs before everything is inactive.

Comment thread ext/standard/io_poll.c Outdated
Comment on lines +383 to +384
/* Pass 1: detach all watchers from the closing stream and pin their refcounts
* before retiring any watcher (whose data destructor may run arbitrary PHP). */

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.

NIT: Can you reduce this comments to a single line (too much info there :) ).

Comment thread ext/standard/io_poll.c Outdated
/* Update user data */
zval_ptr_dtor(&watcher->data);
/* Update user data safely against re-entrant destructor */
zval old_data = watcher->data;

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.

you should use ZVAL_COPY_VALUE here. Also this should be covered by test.

$h2 = new StreamPollHandle($pair[0]);
$dtor = new class {
public $w2;
public function __destruct() {

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.

Can you echo from the destructors here so the test shows they really run during fclose()? Otherwise it would still pass if they don't.

@hohphu8
hohphu8 force-pushed the fix-io-poll-stream-close-reentrancy branch from 56043a0 to 7525716 Compare November 28, 2026 00:13
@hohphu8

hohphu8 commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Addressed all comments (retiring all watchers before releasing, single-line comment, ZVAL_COPY_VALUE + modifyData() test case, and destructor echoes).

@hohphu8
hohphu8 requested a review from bukka November 28, 2026 00:15
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