[php-src] PHP-8.5: Merge branch 'PHP-8.4' into PHP-8.5

From: Date: Fri, 24 Jul 2026 15:35:15 +0000
Subject: [php-src] PHP-8.5: Merge branch 'PHP-8.4' into PHP-8.5
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-140201@lists.php.net to get a copy of this message
Author: Ilia Alshanetsky (iliaal) Date: 2026-07-24T11:26:18-04:00 Commit: https://github.com/php/php-src/commit/d88895b103f2548e85c8591ad8159533a553ec2a Raw diff: https://github.com/php/php-src/commit/d88895b103f2548e85c8591ad8159533a553ec2a.diff Merge branch 'PHP-8.4' into PHP-8.5 * PHP-8.4: Don't expose a freed stream resource to user filters Changed paths: A ext/standard/tests/filters/gh15836.phpt M NEWS M ext/standard/tests/filters/bug54350.phpt M ext/standard/user_filters.c Diff: diff --git a/NEWS b/NEWS index 6445c30f1b48..ca863b4f3815 100644 --- a/NEWS +++ b/NEWS @@ -27,6 +27,10 @@ PHP NEWS SO_LINGER options. (Weilin Du) . Fixed various memory related issues in ext/sockets. (David Carlier) +- Streams: + . Fixed bug GH-15836 (Use-after-free when a user stream filter accesses + $this->stream during the close flush). (iliaal) + 30 Jul 2026, PHP 8.5.9 - Core: diff --git a/ext/standard/tests/filters/bug54350.phpt b/ext/standard/tests/filters/bug54350.phpt index 046db0483a9a..ec3cd857f1a0 100644 --- a/ext/standard/tests/filters/bug54350.phpt +++ b/ext/standard/tests/filters/bug54350.phpt @@ -23,4 +23,4 @@ fwrite($fd, "foo"); ?> --EXPECTF-- Warning: fclose(): cannot close the provided stream, as it must not be manually closed in %s on line %d -fclose(): Argument #1 ($stream) must be an open stream resource +fclose(): Argument #1 ($stream) must be of type resource, null given diff --git a/ext/standard/tests/filters/gh15836.phpt b/ext/standard/tests/filters/gh15836.phpt new file mode 100644 index 000000000000..593b93dcdc70 --- /dev/null +++ b/ext/standard/tests/filters/gh15836.phpt @@ -0,0 +1,34 @@ +--TEST-- +GH-15836 (use-after-free when a user filter reads $this->stream during the close flush) +--FILE-- +<?php +class my_filter extends php_user_filter { + public static ?Throwable $e = null; + function filter($in, $out, &$consumed, $closing): int { + if ($closing) { + try { + stream_bucket_new($this->stream, "x"); + } catch (TypeError $e) { + self::$e = $e; + } + } + return PSFS_PASS_ON; + } +} +var_dump(stream_filter_register("my_filter", "my_filter")); + +function run() { + $s = fopen("php://memory", "wb+"); + stream_filter_append($s, "my_filter", STREAM_FILTER_WRITE); +} +run(); + +echo my_filter::$e->getTraceAsString(), "\n"; +echo "done\n"; +?> +--EXPECTF-- +bool(true) +#0 %s(%d): stream_bucket_new(NULL, 'x') +#1 %s(%d): my_filter->filter(Resource id #%d, Resource id #%d, 0, true) +#2 {main} +done diff --git a/ext/standard/user_filters.c b/ext/standard/user_filters.c index 002456c2ffe8..0c27130ee021 100644 --- a/ext/standard/user_filters.c +++ b/ext/standard/user_filters.c @@ -156,7 +156,11 @@ static php_stream_filter_status_t userfilter_filter( bool stream_property_exists = Z_OBJ_HT_P(obj)->has_property(Z_OBJ_P(obj), stream_name, ZEND_PROPERTY_EXISTS, NULL); if (stream_property_exists) { zval stream_zval; - php_stream_to_zval(stream, &stream_zval); + if (EXPECTED(stream->res && stream->res->type >= 0)) { + php_stream_to_zval(stream, &stream_zval); + } else { + ZVAL_NULL(&stream_zval); + } zend_update_property_ex(Z_OBJCE_P(obj), Z_OBJ_P(obj), stream_name, &stream_zval); /* If property update threw an exception, skip filter execution */ if (EG(exception)) {

« previous php.cvs (#140201) next »