Skip to content

Io\Poll\Context teardown permits re-entrant watcher destructor to use freed poll context #25854

Backlog

Description

@kaslovdmitri

Description

Summary

In PHP 8.6.0, destroying an Io\Poll\Context can run a watcher data destructor after the native php_poll_ctx has already been freed. If that destructor re-enters the still-live PHP object, Context::wait() calls php_poll_wait() with the stale pointer and dereferences ctx->backend_ops. This is a reproducible native use-after-free / crash.

This is the underlying lifetime bug discussed in GHSA-xmfm-v8hf-4854. The advisory comment from @bukka correctly notes that disable_functions is not a security boundary;

Reproduction

Save as io-poll-reentrant.php:

<?php
function out($s) { fwrite(STDERR, $s . "\n"); }

[$s1, $p1] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
$ctx = new Io\Poll\Context();
$h1 = new StreamPollHandle($s1);

class Reenter {
    public $wr;
    public function __destruct() {
        $ctx = $this->wr->get();
        out($ctx ? 'reentered' : 'weakref-null');
        if ($ctx) {
            $ctx->wait(Time\Duration::fromSeconds(0), 1);
            out('wait returned');
        }
    }
}

$data = new Reenter();
$data->wr = WeakReference::create($ctx);
$ctx->add($h1, [Io\Poll\Event::Read], $data);
unset($data, $ctx);
out('survived');
?>

On the PHP 8.6.0 final source revision 8aaada3, built as a minimal non-debug CLI, this prints reentered and then terminates in a native Bus error (RC 138) on macOS arm64. The same source path is the one used by the Linux PoC in the advisory.

A nearby negative control that removes the watcher before destroying the context reaches reentered, wait returned, and survived with RC 0.

Expected behavior

Context teardown should not allow userland destructors to re-enter a context whose native poll state has been destroyed. The call should either be prevented/guarded or the native context should remain valid until all watcher data destructors have completed.

Relevant source

  • ext/standard/io_poll.c: php_io_poll_context_free_object() frees intern->ctx before zend_hash_destroy(intern->watchers) runs watcher/data destructors (PHP 8.6.0, lines 412-433).
  • main/poll/poll_core.c: php_poll_destroy() frees the context, while php_poll_wait() later dereferences ctx->backend_ops (lines 230-242 and 296-308).

This appears distinct from the stream-close re-entrancy fix proposed in #25801: this reproducer unsets the context directly and does not close a watched stream. Please track the two teardown paths separately or add a regression test covering this context-destructor case.

Build identity

  • PHP source: 8aaada3 (PHP 8.6.0 final)
  • CLI SHA-256: 5b8ee05463d3e0b61d18e0e61b605e9e4296287f4f63dd23add76624e484f948
  • Build: PHP 8.6.0 (cli), NTS, --disable-all --enable-cli

PHP Version

PHP 8.6.0 (cli) (built: Nov 27 2026 23:18:55)

Operating System

macOS 27.2.1 (arm64), Darwin 27.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions