From 9dc0eb698fb62cf291616814e86a62e65dd3d6a3 Mon Sep 17 00:00:00 2001 From: Jakub Zelenka Date: Fri, 27 Nov 2026 22:08:53 +0100 Subject: [PATCH] Fix UAF when a stream notification callback closes the stream Progress and completed notifications are dispatched from the socket and TLS read and write operations and from the stream buffer filling and writing code, all of which keep using the stream after the notifier returns. A notification callback calling fclose() on that stream freed it under them. Set PHP_STREAM_FLAG_NO_FCLOSE on the stream while the notifier runs, as is already done for user filters, so that fclose() fails with a warning. --- .../https_notification_fclose_stream.phpt | 79 +++++++++++++++++++ ext/openssl/xp_ssl.c | 2 +- .../http/notification_fclose_stream.phpt | 62 +++++++++++++++ main/streams/php_streams_int.h | 28 +++++++ main/streams/streams.c | 4 +- main/streams/xp_socket.c | 4 +- 6 files changed, 174 insertions(+), 5 deletions(-) create mode 100644 ext/openssl/tests/https_notification_fclose_stream.phpt create mode 100644 ext/standard/tests/http/notification_fclose_stream.phpt diff --git a/ext/openssl/tests/https_notification_fclose_stream.phpt b/ext/openssl/tests/https_notification_fclose_stream.phpt new file mode 100644 index 000000000000..d850659102f3 --- /dev/null +++ b/ext/openssl/tests/https_notification_fclose_stream.phpt @@ -0,0 +1,79 @@ +--TEST-- +Stream notification callback cannot close the TLS stream it is dispatched from +--EXTENSIONS-- +openssl +--SKIPIF-- + +--INI-- +allow_url_fopen=1 +--FILE-- + ['local_cert' => '%s']]); + $server = stream_socket_server("tls://127.0.0.1:0", $errno, $errstr, + STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $serverCtx); + phpt_notify_server_start($server); + + /* Send the body only after the client write. */ + $conn = stream_socket_accept($server); + while (($line = fgets($conn)) !== false && $line !== "\r\n"); + fwrite($conn, "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\n"); + fread($conn, 2); + fwrite($conn, "body"); + fclose($conn); +CODE; +$serverCode = sprintf($serverCode, $certFile); + +$clientCode = <<<'CODE' + $stream = null; + $clientCtx = stream_context_create( + ['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]], + ['notification' => function (int $code) use (&$stream) { + if (!is_resource($stream)) { + return; + } + if ($code === STREAM_NOTIFY_PROGRESS || $code === STREAM_NOTIFY_COMPLETED) { + echo $code === STREAM_NOTIFY_PROGRESS ? "progress\n" : "completed\n"; + var_dump(fclose($stream)); + } + }] + ); + + $stream = fopen("https://{{ ADDR }}/", 'r', false, $clientCtx); + var_dump(fwrite($stream, "go")); + var_dump(fread($stream, 8192)); + var_dump(fread($stream, 8192)); + var_dump(fclose($stream)); +CODE; + +include 'CertificateGenerator.inc'; +(new CertificateGenerator())->saveNewCertAsFileWithKey('https_notification_fclose_stream', $certFile); + +include 'ServerClientTestCase.inc'; +ServerClientTestCase::getInstance()->run($clientCode, $serverCode); +?> +--CLEAN-- + +--EXPECTF-- +progress + +Warning: fclose(): %d is not a valid stream resource in %s on line %d +bool(false) +int(2) +progress + +Warning: fclose(): %d is not a valid stream resource in %s on line %d +bool(false) +string(4) "body" +completed + +Warning: fclose(): %d is not a valid stream resource in %s on line %d +bool(false) +string(0) "" +bool(true) diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c index 9ae5105fea10..c64cd9d2db68 100644 --- a/ext/openssl/xp_ssl.c +++ b/ext/openssl/xp_ssl.c @@ -2265,7 +2265,7 @@ static ssize_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, si /* Tell PHP if we read / wrote bytes. */ if (nr_bytes > 0) { - php_stream_notify_progress_increment(PHP_STREAM_CONTEXT(stream), nr_bytes, 0); + php_stream_notify_progress_increment_no_fclose(stream, nr_bytes); } /* And if we were originally supposed to be blocking, let's reset the socket to that. */ diff --git a/ext/standard/tests/http/notification_fclose_stream.phpt b/ext/standard/tests/http/notification_fclose_stream.phpt new file mode 100644 index 000000000000..9869ad70054b --- /dev/null +++ b/ext/standard/tests/http/notification_fclose_stream.phpt @@ -0,0 +1,62 @@ +--TEST-- +Stream notification callback cannot close the stream it is dispatched from +--SKIPIF-- + +--INI-- +allow_url_fopen=1 +--FILE-- + $pid, 'uri' => $uri] = $server; + +$stream = null; +$context = stream_context_create([], ['notification' => function (int $code) use (&$stream) { + if (!is_resource($stream)) { + return; + } + if ($code === STREAM_NOTIFY_PROGRESS || $code === STREAM_NOTIFY_COMPLETED) { + echo $code === STREAM_NOTIFY_PROGRESS ? "progress\n" : "completed\n"; + var_dump(fclose($stream)); + } +}]); + +$stream = fopen($uri, 'r', false, $context); +var_dump(fwrite($stream, "go")); +var_dump(fread($stream, 8192)); +var_dump(fread($stream, 8192)); +var_dump(fclose($stream)); + +http_server_kill($pid); +?> +--EXPECTF-- +progress + +Warning: fclose(): %d is not a valid stream resource in %s on line %d +bool(false) +int(2) +progress + +Warning: fclose(): %d is not a valid stream resource in %s on line %d +bool(false) +string(4) "body" +completed + +Warning: fclose(): %d is not a valid stream resource in %s on line %d +bool(false) +string(0) "" +bool(true) diff --git a/main/streams/php_streams_int.h b/main/streams/php_streams_int.h index f9dda9a52ff7..d6b73b8d71ff 100644 --- a/main/streams/php_streams_int.h +++ b/main/streams/php_streams_int.h @@ -66,3 +66,31 @@ void php_stream_mode_sanitize_fdopen_fopencookie(php_stream *stream, char *resul /* Escapes NUL and backslash bytes so a host containing them cannot truncate or * collide the persistent stream hash key. */ zend_string *php_stream_escape_persistent_key(const char *host, size_t hostlen); + +/* Make sure the stream is not closed while the notifier callback executes. */ +static zend_always_inline void php_stream_notify_progress_increment_no_fclose( + php_stream *stream, size_t dsofar) +{ + php_stream_context *context = PHP_STREAM_CONTEXT(stream); + + if (context && context->notifier) { + uint32_t orig_no_fclose = stream->flags & PHP_STREAM_FLAG_NO_FCLOSE; + stream->flags |= PHP_STREAM_FLAG_NO_FCLOSE; + php_stream_notify_progress_increment(context, dsofar, 0); + stream->flags &= ~PHP_STREAM_FLAG_NO_FCLOSE; + stream->flags |= orig_no_fclose; + } +} + +static zend_always_inline void php_stream_notify_completed_no_fclose(php_stream *stream) +{ + php_stream_context *context = PHP_STREAM_CONTEXT(stream); + + if (context && context->notifier) { + uint32_t orig_no_fclose = stream->flags & PHP_STREAM_FLAG_NO_FCLOSE; + stream->flags |= PHP_STREAM_FLAG_NO_FCLOSE; + php_stream_notify_completed(context); + stream->flags &= ~PHP_STREAM_FLAG_NO_FCLOSE; + stream->flags |= orig_no_fclose; + } +} diff --git a/main/streams/streams.c b/main/streams/streams.c index 615ff04361ac..792086219cb7 100644 --- a/main/streams/streams.c +++ b/main/streams/streams.c @@ -747,7 +747,7 @@ PHPAPI zend_result _php_stream_fill_read_buffer(php_stream *stream, size_t size) out_check_eof: if (old_eof != stream->eof) { out_is_eof: - php_stream_notify_completed(PHP_STREAM_CONTEXT(stream)); + php_stream_notify_completed_no_fclose(stream); } return retval; } @@ -1259,7 +1259,7 @@ static ssize_t _php_stream_write_buffer(php_stream *stream, const char *buf, siz out: if (old_eof != stream->eof) { - php_stream_notify_completed(PHP_STREAM_CONTEXT(stream)); + php_stream_notify_completed_no_fclose(stream); } return retval; } diff --git a/main/streams/xp_socket.c b/main/streams/xp_socket.c index 8d577de6e76d..cf15b3af7248 100644 --- a/main/streams/xp_socket.c +++ b/main/streams/xp_socket.c @@ -114,7 +114,7 @@ static ssize_t php_sockop_write(php_stream *stream, const char *buf, size_t coun } if (didwrite > 0) { - php_stream_notify_progress_increment(PHP_STREAM_CONTEXT(stream), didwrite, 0); + php_stream_notify_progress_increment_no_fclose(stream, didwrite); } return didwrite; @@ -202,7 +202,7 @@ static ssize_t php_sockop_read(php_stream *stream, char *buf, size_t count) } if (nr_bytes > 0) { - php_stream_notify_progress_increment(PHP_STREAM_CONTEXT(stream), nr_bytes, 0); + php_stream_notify_progress_increment_no_fclose(stream, nr_bytes); } return nr_bytes;