Commit 1cc972513ce for php
commit 1cc972513ce970b0c21ae94ccb4ad8c33dc5ba60
Author: lazerg <lazerg2@gmail.com>
Date: Tue Nov 24 08:28:47 2026 +0500
ext/standard: Reset last response headers after freeing them in RSHUTDOWN
The basic RSHUTDOWN freed BG(last_http_headers) but left the zval pointing
at the freed array, so a user stream wrapper closed later in shutdown could
read it back through http_get_last_response_headers().
Fixes GH-25693
Closes GH-25694
diff --git a/NEWS b/NEWS
index f58c44353ec..6bcd9a76ed1 100644
--- a/NEWS
+++ b/NEWS
@@ -325,6 +325,8 @@ PHP NEWS
RDATA). (lazerg)
. Fixed bug GH-25989 (Unix socket peer name of an unnamed client is read from
uninitialized memory). (Matyapiro31)
+ . Fixed bug GH-25693 (Use-after-free of the last response headers in a
+ stream wrapper closed at shutdown). (lazerg)
- Streams:
. Fixed bug GH-22063 (Stream filter chain UAF on self-removal during
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index 3c970735a53..eefa30ff624 100644
--- a/ext/standard/basic_functions.c
+++ b/ext/standard/basic_functions.c
@@ -481,6 +481,7 @@ PHP_RSHUTDOWN_FUNCTION(basic) /* {{{ */
/* Free last http headers */
zval_ptr_dtor(&BG(last_http_headers));
+ ZVAL_UNDEF(&BG(last_http_headers));
BG(page_uid) = -1;
BG(page_gid) = -1;
diff --git a/ext/standard/tests/http/gh25693.phpt b/ext/standard/tests/http/gh25693.phpt
new file mode 100644
index 00000000000..33d09db3d86
--- /dev/null
+++ b/ext/standard/tests/http/gh25693.phpt
@@ -0,0 +1,42 @@
+--TEST--
+GH-25693 (Use-after-free of the last response headers in a stream wrapper closed at shutdown)
+--CREDITS--
+xuziqiang98
+--INI--
+allow_url_fopen=1
+--SKIPIF--
+<?php require 'server.inc'; http_server_skipif(); ?>
+--FILE--
+<?php
+require 'server.inc';
+
+class Wrapper {
+ public $context;
+
+ public function stream_open($path, $mode, $options, &$opened_path) {
+ return true;
+ }
+
+ public function stream_close() {
+ var_dump(http_get_last_response_headers());
+ }
+}
+
+function request($uri) {
+ var_dump(file_get_contents($uri));
+}
+
+['pid' => $pid, 'uri' => $uri] = http_server([
+ "data://text/plain,HTTP/1.0 200 Ok\r\n\r\nBody",
+]);
+
+request($uri);
+
+http_server_kill($pid);
+
+stream_wrapper_register('test', Wrapper::class);
+$fp = fopen('test://', 'r');
+?>
+--EXPECT--
+string(4) "Body"
+NULL