Repository navigation
Fix UAF/NULL deref on re-entrant watcher dtor in Io\Poll stream close #25801
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
Open
hohphu8
wants to merge
1
commit into
php:PHP-8.6
Choose a base branch
from
hohphu8:fix-io-poll-stream-close-reentrancy
base: PHP-8.6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+90
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
ext/standard/tests/poll/poll_stream_close_reentrant_dtor.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| --TEST-- | ||
| Io\Poll: closing a watched stream or modifying watcher data with re-entrant destructor does not crash | ||
| --FILE-- | ||
| <?php | ||
| require_once __DIR__ . '/poll.inc'; | ||
|
|
||
| // Case 1: destructor of watcher #1 removes watcher #2 on the same closing stream | ||
| $pair = pt_new_socket_pair(); | ||
| $ctx1 = pt_new_stream_poll(); | ||
| $ctx2 = pt_new_stream_poll(); | ||
| $h1 = new StreamPollHandle($pair[0]); | ||
| $h2 = new StreamPollHandle($pair[0]); | ||
| $dtor = new class { | ||
| public $w2; | ||
| public function __destruct() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| echo "case1 dtor\n"; | ||
| if ($this->w2) { | ||
| var_dump($this->w2->isActive()); | ||
| $this->w2->remove(); | ||
| } | ||
| } | ||
| }; | ||
| $ctx1->add($h1, [Io\Poll\Event::Read], $dtor); | ||
| $dtor->w2 = $ctx2->add($h2, [Io\Poll\Event::Read]); | ||
| unset($dtor); | ||
| fclose($pair[0]); | ||
| fclose($pair[1]); | ||
| echo "case1 ok\n"; | ||
|
|
||
| // Case 2: destructor of watcher #1 releases context #2 owning watcher #2 | ||
| $pair = pt_new_socket_pair(); | ||
| $ctx1 = pt_new_stream_poll(); | ||
| $ctx2 = pt_new_stream_poll(); | ||
| $h1 = new StreamPollHandle($pair[0]); | ||
| $h2 = new StreamPollHandle($pair[0]); | ||
| $dtor = new class($ctx2) { | ||
| public function __construct(public &$ctx2) {} | ||
| public function __destruct() { | ||
| echo "case2 dtor\n"; | ||
| $this->ctx2 = null; | ||
| } | ||
| }; | ||
| $ctx1->add($h1, [Io\Poll\Event::Read], $dtor); | ||
| $ctx2->add($h2, [Io\Poll\Event::Read]); | ||
| unset($dtor); | ||
| fclose($pair[0]); | ||
| fclose($pair[1]); | ||
| echo "case2 ok\n"; | ||
|
|
||
| // Case 3: modifyData() with a re-entrant destructor reading and updating watcher data | ||
| $pair = pt_new_socket_pair(); | ||
| $ctx = pt_new_stream_poll(); | ||
| $h = new StreamPollHandle($pair[0]); | ||
| $dtor = new class { | ||
| public $watcher; | ||
| public function __destruct() { | ||
| echo "case3 dtor: " . var_export($this->watcher->getData(), true) . "\n"; | ||
| $this->watcher->modifyData('from_dtor'); | ||
| } | ||
| }; | ||
| $watcher = $ctx->add($h, [Io\Poll\Event::Read], $dtor); | ||
| $dtor->watcher = $watcher; | ||
| unset($dtor); | ||
| $watcher->modifyData('new'); | ||
| var_dump($watcher->getData()); | ||
| $watcher->remove(); | ||
| fclose($pair[0]); | ||
| fclose($pair[1]); | ||
| echo "case3 ok\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| case1 dtor | ||
| bool(false) | ||
| case1 ok | ||
| case2 dtor | ||
| case2 ok | ||
| case3 dtor: 'new' | ||
| string(9) "from_dtor" | ||
| case3 ok | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.