Fix GH-22844: StreamPollHandle use-after-free after fclose()#22848
Open
iliaal wants to merge 1 commit into
Open
Fix GH-22844: StreamPollHandle use-after-free after fclose()#22848iliaal wants to merge 1 commit into
iliaal wants to merge 1 commit into
Conversation
StreamPollHandle cached the raw php_stream* but referenced only the resource container, not the stream, so fclose() freed the stream while the handle kept a dangling pointer that isValid(), getStream() and get_fd dereferenced. Re-derive the stream from the held resource each time. A watcher likewise re-derived its registration fd from that stream in remove(), so removing a watcher after its stream closed leaked the Poll-backend entry and could be re-hit through a reused fd in wait(); cache the fd at add() and use it for removal. Fixes phpGH-22844
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
StreamPollHandlestored the rawphp_stream *but referenced only the resource container, sofclose()freed the stream (viapefree()) and clearedres->ptr/res->typewhile the handle kept a dangling pointer. isValid(), getStream() and the internal get_fd (reached viaContext::add()) then dereferenced freed memory. The accessors now re-derive the stream from the held resource and report a closed resource as absent: getStream() returns null, isValid() returns false.A watcher re-derived its registration fd from that same stream in
remove(), so a watcher removed after its stream closed couldn't recover the fd and left the Poll backend holding the freed watcher, which a reused fd would then write through inwait(). It now caches the fd fromadd()and removes by that. Reproduced under ASAN; getStream()'s stub return becomesresource|null.Fixes #22844