[php-src] master: Merge branch 'PHP-8.5' into PHP-8.6
| From: | David Carlier | Date: | Fri, 20 Nov 2026 20:44:12 +0000 |
| Subject: | [php-src] master: Merge branch 'PHP-8.5' into PHP-8.6 | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-143987@lists.php.net to get a copy of this message | ||
Author: David Carlier (devnexen)
Date: 2026-11-20T20:42:57Z
Commit: https://github.com/php/php-src/commit/feaa8a10a1897c81742edaa2686c02c516780e5e
Raw diff: https://github.com/php/php-src/commit/feaa8a10a1897c81742edaa2686c02c516780e5e.diff
Merge branch 'PHP-8.5' into PHP-8.6
* PHP-8.5:
streams: UAF in php_stream_cast() when stream_flush() closes the stream
# Conflicts:
# ext/standard/tests/streams/gh14506.phpt
# main/streams/userspace.c
Changed paths:
A ext/standard/tests/streams/gh25586.phpt
M ext/standard/tests/streams/gh14506.phpt
M main/streams/userspace.c
Diff:
diff --git a/ext/standard/tests/streams/gh14506.phpt b/ext/standard/tests/streams/gh14506.phpt
index 76d4701160fa..a208c83e4c91 100644
--- a/ext/standard/tests/streams/gh14506.phpt
+++ b/ext/standard/tests/streams/gh14506.phpt
@@ -92,4 +92,5 @@ Warning: stream_select(): Cannot represent a stream of type user-space as a sele
Warning: stream_select(): Cannot represent a stream of type user-space as a select()able descriptor
in %s on line %d
ValueError: No stream arrays were passed
-TypeError: 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 000000000000..d0a9a8cfd4ee
--- /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 a73dd80658ac..ec8bdcdf0ac3 100644
--- a/main/streams/userspace.c
+++ b/main/streams/userspace.c
@@ -737,10 +737,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 &&
zend_is_true(&retval) ? 0 : -1;
zval_ptr_dtor(&retval);