From d2710e80954add71b6c94777d7d1aa620c7ca6df Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Tue, 21 Jul 2026 08:48:53 -0400 Subject: [PATCH] ext/standard: Fix UAF with a stashed user filter bucket brigade The bucket brigades passed to a user filter's filter() method are allocated on the filter caller's C stack and handed to userland as le_bucket_brigade resources, but userfilter_filter never closed them. A filter that stashed $in or $out (e.g. in a global) kept a resource whose ptr still pointed at the popped stack frame; the stream_bucket_*() functions fetch by resource type only, so a later call dereferenced freed stack. Close both resources after the callback so a stashed copy becomes non-fetchable and throws instead, and wrap the callback in zend_try/zend_catch so the close still runs when the filter bails out (which otherwise longjmps past it and leaves the resource reachable from a shutdown function). Fixes GH-22845 --- NEWS | 2 + ext/standard/tests/filters/gh22845.phpt | 40 ++++++++++ .../tests/filters/gh22845_bailout.phpt | 39 ++++++++++ ext/standard/user_filters.c | 74 +++++++++++-------- 4 files changed, 125 insertions(+), 30 deletions(-) create mode 100644 ext/standard/tests/filters/gh22845.phpt create mode 100644 ext/standard/tests/filters/gh22845_bailout.phpt diff --git a/NEWS b/NEWS index e42393462d83..716a850e11b9 100644 --- a/NEWS +++ b/NEWS @@ -260,6 +260,8 @@ PHP NEWS type on 32-bit platforms). (Kamil Tekiela) . Fixed bug GH-25464 (Segfault reading from php://memory after stream_filter_remove() on a read filter). (David Carlier) + . Fixed bug GH-22845 (Use-after-free when a user filter stashes its bucket + brigade resource). (Ilia Alshanetsky) - XSL: . Fixed a crash in XSLTProcessor::transformToDoc() when the return class diff --git a/ext/standard/tests/filters/gh22845.phpt b/ext/standard/tests/filters/gh22845.phpt new file mode 100644 index 000000000000..5c1242603a1f --- /dev/null +++ b/ext/standard/tests/filters/gh22845.phpt @@ -0,0 +1,40 @@ +--TEST-- +GH-22845 (Use-after-free when a user filter stashes its bucket brigade resource) +--FILE-- +data); + stream_bucket_append($out, $bucket); + } + return PSFS_PASS_ON; + } +} +stream_filter_register('stash', 'stash_filter'); + +$fp = fopen('php://memory', 'r+'); +fwrite($fp, "hello world"); +rewind($fp); +stream_filter_append($fp, 'stash', STREAM_FILTER_READ); +var_dump(fread($fp, 100)); + +// The brigades lived on the filter caller's stack. Both stashed resources must be +// closed now that the callback has returned, not left pointing at freed stack. +foreach (['stash_in', 'stash_out'] as $name) { + try { + stream_bucket_make_writeable($GLOBALS[$name]); + echo "$name: no error\n"; + } catch (\Throwable $e) { + echo "$name: ", $e::class, "\n"; + } +} +echo "done\n"; +?> +--EXPECT-- +string(11) "hello world" +stash_in: TypeError +stash_out: TypeError +done diff --git a/ext/standard/tests/filters/gh22845_bailout.phpt b/ext/standard/tests/filters/gh22845_bailout.phpt new file mode 100644 index 000000000000..f8c0809e87b4 --- /dev/null +++ b/ext/standard/tests/filters/gh22845_bailout.phpt @@ -0,0 +1,39 @@ +--TEST-- +GH-22845 (user filter bucket brigade resource is closed when the callback bails out) +--EXTENSIONS-- +zend_test +--SKIPIF-- + +--FILE-- +data); + stream_bucket_append($out, $b); + } + register_shutdown_function(function() { + try { + stream_bucket_make_writeable($GLOBALS['stash_in']); + echo "shutdown: no error\n"; + } catch (\Throwable $e) { + echo "shutdown: ", $e::class, "\n"; + } + }); + zend_trigger_bailout(); + } +} +stream_filter_register('bf', 'bf'); + +$fp = fopen('php://memory', 'r+'); +fwrite($fp, "hello world"); +rewind($fp); +stream_filter_append($fp, 'bf', STREAM_FILTER_READ); +fread($fp, 100); +?> +--EXPECTF-- +Fatal error: Bailout in %s on line %d +shutdown: TypeError diff --git a/ext/standard/user_filters.c b/ext/standard/user_filters.c index 74be83ee6ee7..68a6b8966dfe 100644 --- a/ext/standard/user_filters.c +++ b/ext/standard/user_filters.c @@ -149,7 +149,8 @@ static php_stream_filter_status_t userfilter_filter( zval func_name; zval retval; zval args[4]; - int call_result; + int call_result = FAILURE; + bool bailout = false; /* the userfilter object probably doesn't exist anymore */ if (CG(unclean_shutdown)) { @@ -216,42 +217,48 @@ static php_stream_filter_status_t userfilter_filter( ZVAL_BOOL(&args[3], flags & PSFS_FLAG_FLUSH_CLOSE); - call_result = call_user_function(NULL, - obj, - &func_name, - &retval, - 4, args); + zend_try { + call_result = call_user_function(NULL, + obj, + &func_name, + &retval, + 4, args); + } zend_catch { + bailout = true; + } zend_end_try(); zval_ptr_dtor(&func_name); - if (call_result == SUCCESS && Z_TYPE(retval) != IS_UNDEF) { - convert_to_long(&retval); - ret = (int)Z_LVAL(retval); - } else if (call_result == FAILURE) { - php_error_docref(NULL, E_WARNING, "Failed to call filter function"); - } + if (!bailout) { + if (call_result == SUCCESS && Z_TYPE(retval) != IS_UNDEF) { + convert_to_long(&retval); + ret = (int)Z_LVAL(retval); + } else if (call_result == FAILURE) { + php_error_docref(NULL, E_WARNING, "Failed to call filter function"); + } - if (bytes_consumed) { - *bytes_consumed = zval_get_long(&args[2]); - } + if (bytes_consumed) { + *bytes_consumed = zval_get_long(&args[2]); + } - if (buckets_in->head) { - if (ret == PSFS_FEED_ME && !(flags & PSFS_FLAG_FLUSH_CLOSE)) { - /* Keep the input for the next call and make sure the buckets own their data */ - php_stream_bucket *bucket; - while ((bucket = buckets_in->head)) { - bucket = php_stream_bucket_make_writeable(bucket); - php_stream_bucket_append(&thisfilter->buffer, bucket); + if (buckets_in->head) { + if (ret == PSFS_FEED_ME && !(flags & PSFS_FLAG_FLUSH_CLOSE)) { + /* Keep the input for the next call and make sure the buckets own their data */ + php_stream_bucket *bucket; + while ((bucket = buckets_in->head)) { + bucket = php_stream_bucket_make_writeable(bucket); + php_stream_bucket_append(&thisfilter->buffer, bucket); + } + } else { + php_error_docref(NULL, E_WARNING, "Unprocessed filter buckets remaining on input brigade"); + userfilter_release_brigade(buckets_in); } - } else { - php_error_docref(NULL, E_WARNING, "Unprocessed filter buckets remaining on input brigade"); - userfilter_release_brigade(buckets_in); } - } - /* Filter could've broken contract and added buckets anyway. */ - if (ret != PSFS_PASS_ON && buckets_out->head) { - userfilter_release_brigade(buckets_out); + /* Filter could've broken contract and added buckets anyway. */ + if (ret != PSFS_PASS_ON && buckets_out->head) { + userfilter_release_brigade(buckets_out); + } } /* filter resources are cleaned up by the stream destructor, @@ -260,12 +267,15 @@ static php_stream_filter_status_t userfilter_filter( * Since the property accepted a resource assignment above, it must have * no type hint or be typed as mixed, so we can safely assign null. */ - if (stream_property_exists) { + if (stream_property_exists && !bailout) { zend_update_property_null(Z_OBJCE_P(obj), Z_OBJ_P(obj), ZSTR_VAL(stream_name), ZSTR_LEN(stream_name)); } zend_string_release(stream_name); + zend_list_close(Z_RES(args[1])); + zend_list_close(Z_RES(args[0])); + zval_ptr_dtor(&args[3]); zval_ptr_dtor(&args[2]); zval_ptr_dtor(&args[1]); @@ -274,6 +284,10 @@ static php_stream_filter_status_t userfilter_filter( stream->flags &= ~PHP_STREAM_FLAG_NO_FCLOSE; stream->flags |= orig_no_fclose; + if (bailout) { + zend_bailout(); + } + return ret; }