[php-src] PHP-8.6: streams: UAF in php_stream_cast() when stream_flush() closes the stream

From: Date: Fri, 20 Nov 2026 20:44:12 +0000
Subject: [php-src] PHP-8.6: streams: UAF in php_stream_cast() when stream_flush() closes the stream
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143988@lists.php.net to get a copy of this message
Author: lazerg (lazerg) Committer: David Carlier (devnexen) Date: 2026-11-20T20:40:33Z Commit: https://github.com/php/php-src/commit/91452ab11eeabc9ea4e5a5911fbcf82334c32f0d Raw diff: https://github.com/php/php-src/commit/91452ab11eeabc9ea4e5a5911fbcf82334c32f0d.diff 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 Changed paths: A ext/standard/tests/streams/gh25586.phpt M NEWS M ext/standard/tests/streams/gh14506.phpt M main/streams/userspace.c Diff: diff --git a/NEWS b/NEWS index ec98da0cc7b7..f33a114c4ccf 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 f83eba4f1ff3..05aa2f027d9b 100644 --- a/ext/standard/tests/streams/gh14506.phpt +++ b/ext/standard/tests/streams/gh14506.phpt @@ -92,4 +92,5 @@ Warning: fclose(): cannot close the provided stream, as it must not be manually 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 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 b3b88994465b..2bf858060a4c 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);

« previous php.cvs (#143988) next »