Repository navigation
Conversation
bukka
left a comment
There was a problem hiding this comment.
It looks good. Just couple of things.
| 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(); |
There was a problem hiding this comment.
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.
| /* Pass 1: detach all watchers from the closing stream and pin their refcounts | ||
| * before retiring any watcher (whose data destructor may run arbitrary PHP). */ |
There was a problem hiding this comment.
NIT: Can you reduce this comments to a single line (too much info there :) ).
| /* Update user data */ | ||
| zval_ptr_dtor(&watcher->data); | ||
| /* Update user data safely against re-entrant destructor */ | ||
| zval old_data = watcher->data; |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
56043a0 to
7525716
Compare
|
Thanks for the review! Addressed all comments (retiring all watchers before releasing, single-line comment, ZVAL_COPY_VALUE + modifyData() test case, and destructor echoes). |
In
php_io_poll_stream_notify_close()(ext/standard/io_poll.c), callingphp_io_poll_context_retire_watcher()while iteratingstream->poll_watchersdestroyswatcher->data. A__destruct()on that user data can re-entrantly callremove()on another watcher or drop anotherIo\Poll\Contextwatching the same stream, causing a NULL dereference or heap UAF (SIGSEGV) on later iterations.php_io_poll_watcher_modify_data()also destroyswatcher->databefore copying in the new value.This splits
php_io_poll_stream_notify_close()into two passes (first detaching all watchers withwatcher->stream = NULL,watcher->closed = true, andGC_ADDREF(&watcher->std); then retiring active watchers and callingOBJ_RELEASE), and swapswatcher->datato a localold_databefore callingzval_ptr_dtor(&old_data)inphp_io_poll_watcher_modify_data().Adds
ext/standard/tests/poll/poll_stream_close_reentrant_dtor.phptcovering both re-entrantremove()and context destruction duringfclose(). VerifiedSIGSEGV -> PASSonPHP-8.6(Debug + ZTS) withext/standard/tests/poll/passing (52/52).Found via AI-assisted source inspection and verified it myself.