Skip to content

Fix JIT freeing a persistent stream returned by stream_socket_client() - #25828

Open
lacatoire wants to merge 1 commit into
php:PHP-8.4from
lacatoire:fix/standard-stream-socket-client-refcount-PHP-8.4
Open

lacatoire wants to merge 1 commit into
php:PHP-8.4from
lacatoire:fix/standard-stream-socket-client-refcount-PHP-8.4

Conversation

@lacatoire

Copy link
Copy Markdown
Member

With STREAM_CLIENT_PERSISTENT, a second stream_socket_client() call for the same address returns the already registered resource with an added reference, but the stub declares @refcount 1. Function JIT and hot tracing JIT then destroy a discarded result without checking its refcount, closing a stream that is still in use and corrupting memory. The annotation is dropped, as pfsockopen() already does.

To reproduce

Run with opcache.enable_cli=1 and opcache.jit=function.

<?php
function connect_twice(string $address): void
{
    $keep = stream_socket_client("tcp://$address", $errno, $errstr, 1, STREAM_CLIENT_PERSISTENT | STREAM_CLIENT_CONNECT);
    stream_socket_client("tcp://$address", $errno, $errstr, 1, STREAM_CLIENT_PERSISTENT | STREAM_CLIENT_CONNECT);
    var_dump(is_resource($keep));
}

$server = stream_socket_server('tcp://127.0.0.1:0');
connect_twice(stream_socket_get_name($server, false));

Before (8.4.28, 8.5.13):

bool(false)
Segmentation fault (core dumped)

After:

bool(true)

With the default tracing JIT, calling the function in a hot loop ends in zend_mm_heap corrupted.

With STREAM_CLIENT_PERSISTENT, stream_socket_client() returns the
already registered resource with an added reference when the persistent
connection exists. The stub still declared @refcount 1, which lets
function JIT and hot tracing JIT destroy a discarded result without
checking its refcount and close a stream that is still in use. Drop the
annotation, as pfsockopen() already does.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant