Skip to content

Fix GH-22844: StreamPollHandle use-after-free after fclose() - #187

Closed
iliaal wants to merge 1 commit into
masterfrom
fix/gh-22844-streampollhandle-uaf
Closed

Fix GH-22844: StreamPollHandle use-after-free after fclose()#187
iliaal wants to merge 1 commit into
masterfrom
fix/gh-22844-streampollhandle-uaf

Conversation

@iliaal

@iliaal iliaal commented Jul 21, 2026

Copy link
Copy Markdown
Owner

StreamPollHandle stored the raw php_stream * but referenced only the resource container, so fclose() freed the stream (via pefree()) and cleared res->ptr/res->type while the handle kept a dangling pointer. isValid(), getStream() and the internal get_fd (reached via Context::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 in wait(). It now caches the fd from add() and removes by that. Reproduced under ASAN; getStream()'s stub return becomes resource|null.

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
@iliaal

iliaal commented Jul 21, 2026

Copy link
Copy Markdown
Owner Author

Promoted upstream: php#22848

@iliaal iliaal closed this Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant