Commit 91452ab11ee for php
commit 91452ab11eeabc9ea4e5a5911fbcf82334c32f0d
Author: lazerg <lazerg2@gmail.com>
Date: Fri Nov 20 07:15:48 2026 +0500
streams: UAF in php_stream_cast() when stream_flush() closes the stream
The GH-14506 fix set PHP_STREAM_FLAG_NO_FCLOSE around most userspace
stream handlers, but not around stream_flush(). A stream_flush() that
calls fclose() on its own stream frees it, and callers like
php_stream_cast() (via proc_open()) and php_stream_seek() with a write
filter keep using it after the flush. Set the flag in
php_userstreamop_flush() too, so the fclose() there only warns.
Fix GH-25586
Close GH-25617
diff --git a/NEWS b/NEWS
index ec98da0cc7b..f33a114c4cc 100644
--- a/NEWS
+++ b/NEWS
@@ -194,6 +194,8 @@ PHP NEWS
user filter). (Edmond)
. Fixed bug GH-15701 (Build failure: incompatible funopen() seeker pointer
type on 32-bit platforms). (Kamil Tekiela)
+ . Fixed bug GH-25586 (Use-after-free in php_stream_cast() when a userspace
+ stream_flush() closes the stream). (lazerg)
- XSL:
. Fixed a crash in XSLTProcessor::transformToDoc() when the return class
diff --git a/ext/standard/tests/streams/gh14506.phpt b/ext/standard/tests/streams/gh14506.phpt
index f83eba4f1ff..05aa2f027d9 100644
--- a/ext/standard/tests/streams/gh14506.phpt
+++ b/ext/standard/tests/streams/gh14506.phpt
@@ -92,4 +92,5 @@ function stream_flush(): bool
Warning: stream_select(): Cannot represent a stream of type user-space as a select()able descriptor in %s on line %d
No stream arrays were passed
-fclose(): Argument #1 ($stream) must be an open stream resource
+
+Warning: fclose(): cannot close the provided stream, as it must not be manually closed in %s on line %d
diff --git a/ext/standard/tests/streams/gh25586.phpt b/ext/standard/tests/streams/gh25586.phpt
new file mode 100644
index 00000000000..d0a9a8cfd4e
--- /dev/null
+++ b/ext/standard/tests/streams/gh25586.phpt
@@ -0,0 +1,22 @@
+--TEST--
+GH-25586 (heap-use-after-free in php_stream_cast() when a userspace stream_flush() closes the stream)
+--CREDITS--
+YuanchengJiang
+--FILE--
+<?php
+class W {
+ public $context;
+ function stream_open($path, $mode, $options, &$opened_path) { return true; }
+ function stream_flush() { global $s; fclose($s); return true; }
+}
+stream_wrapper_register("bomb", "W");
+$s = fopen("bomb://", "w");
+var_dump(proc_open("true", [1 => $s], $pipes));
+?>
+--EXPECTF--
+Warning: fclose(): cannot close the provided stream, as it must not be manually closed in %s on line %d
+
+Warning: proc_open(): W::stream_cast is not implemented! in %s on line %d
+
+Warning: proc_open(): Cannot represent a stream of type user-space as a File Descriptor in %s on line %d
+bool(false)
diff --git a/main/streams/userspace.c b/main/streams/userspace.c
index b3b88994465..2bf858060a4 100644
--- a/main/streams/userspace.c
+++ b/main/streams/userspace.c
@@ -718,10 +718,16 @@ static int php_userstreamop_flush(php_stream *stream)
assert(us != NULL);
+ uint32_t orig_no_fclose = stream->flags & PHP_STREAM_FLAG_NO_FCLOSE;
+ stream->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
+
zend_string *func_name = ZSTR_INIT_LITERAL(USERSTREAM_FLUSH, false);
zend_result call_result = zend_call_method_if_exists(Z_OBJ(us->object), func_name, &retval, 0, NULL);
zend_string_release_ex(func_name, false);
+ stream->flags &= ~PHP_STREAM_FLAG_NO_FCLOSE;
+ stream->flags |= orig_no_fclose;
+
int ret = call_result == SUCCESS && Z_TYPE(retval) != IS_UNDEF && zval_is_true(&retval) ? 0 : -1;
zval_ptr_dtor(&retval);