From 440c523411076396bc571daccbc2af2cf5121f77 Mon Sep 17 00:00:00 2001 From: Tomasz Kowalczyk Date: Mon, 30 Nov 2026 16:40:28 +0100 Subject: [PATCH] Fix GH-25957: Segfault when a stream finalizes the HashContext during hash_update_stream() or hash_update_file() hash_update_stream() and hash_update_file() checked the context only once, before the loop. A user stream wrapper or filter runs during php_stream_read() and can call hash_final(), which frees the context and sets it to NULL. The next update then used the NULL context. Check the context again after each read and throw the same error as for a finalized context. hash_update_file() closes its stream first. --- NEWS | 2 + ext/hash/hash.c | 6 ++ ext/hash/tests/gh25957.phpt | 108 ++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 ext/hash/tests/gh25957.phpt diff --git a/NEWS b/NEWS index 36ab60a871e0..9ac857515fc2 100644 --- a/NEWS +++ b/NEWS @@ -42,6 +42,8 @@ PHP NEWS - Hash: . Fixed mhash_keygen_s2k() truncating a $length above INT_MAX instead of throwing a ValueError. (Marc Bennewitz) + . Fixed bug GH-25957 (Segfault when a stream finalizes the HashContext + during hash_update_stream() or hash_update_file()). (Tomasz Kowalczyk) - Intl: . Added IntlNumberFormatter::toSkeleton(). (Weilin Du) diff --git a/ext/hash/hash.c b/ext/hash/hash.c index 8658f8449c51..f7edde3c1430 100644 --- a/ext/hash/hash.c +++ b/ext/hash/hash.c @@ -725,6 +725,7 @@ PHP_FUNCTION(hash_update_stream) if ((n = php_stream_read(stream, buf, toread)) <= 0) { RETURN_LONG(didread); } + PHP_HASHCONTEXT_VERIFY(hash); hash->ops->hash_update(hash->context, (unsigned char *) buf, n); length -= n; didread += n; @@ -760,6 +761,11 @@ PHP_FUNCTION(hash_update_file) } while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) { + if (!hash->context) { + php_stream_close(stream); + zend_argument_type_error(1, "must be a valid, non-finalized HashContext"); + RETURN_THROWS(); + } hash->ops->hash_update(hash->context, (unsigned char *) buf, n); } php_stream_close(stream); diff --git a/ext/hash/tests/gh25957.phpt b/ext/hash/tests/gh25957.phpt new file mode 100644 index 000000000000..6c1ddc1c0765 --- /dev/null +++ b/ext/hash/tests/gh25957.phpt @@ -0,0 +1,108 @@ +--TEST-- +GH-25957: hash_update_stream() and hash_update_file() with a HashContext finalized during the read +--FILE-- +eof = true; + return "data"; + } + + public function stream_eof(): bool { + return $this->eof; + } + + public function stream_close(): void { + echo "stream_close\n"; + } +} + +class Filter extends php_user_filter { + public function filter($in, $out, &$consumed, bool $closing): int { + while ($bucket = stream_bucket_make_writeable($in)) { + finalize("filter"); + $consumed += $bucket->datalen; + stream_bucket_append($out, $bucket); + } + return PSFS_PASS_ON; + } +} + +stream_wrapper_register("finalize", Wrapper::class); +stream_filter_register("finalize", Filter::class); + +echo "hash_update_stream(), stream_read():\n"; +$ctx = hash_init("md5"); +$finalizeIn = "stream_read"; +$fp = fopen("finalize://", "r"); +try { + var_dump(hash_update_stream($ctx, $fp)); +} catch (TypeError $e) { + echo $e::class, ": ", $e->getMessage(), PHP_EOL; +} +fclose($fp); + +echo "\nhash_update_stream(), php_user_filter:\n"; +$ctx = hash_init("md5"); +$finalizeIn = "filter"; +$fp = fopen("php://memory", "w+"); +fwrite($fp, "data"); +rewind($fp); +stream_filter_append($fp, "finalize", STREAM_FILTER_READ); +try { + var_dump(hash_update_stream($ctx, $fp)); +} catch (TypeError $e) { + echo $e::class, ": ", $e->getMessage(), PHP_EOL; +} +fclose($fp); + +echo "\nhash_update_file(), stream_read():\n"; +$ctx = hash_init("md5"); +$finalizeIn = "stream_read"; +try { + var_dump(hash_update_file($ctx, "finalize://")); +} catch (TypeError $e) { + echo $e::class, ": ", $e->getMessage(), PHP_EOL; +} + +echo "\nhash_update_file(), stream_open():\n"; +$ctx = hash_init("md5"); +$finalizeIn = "stream_open"; +try { + var_dump(hash_update_file($ctx, "finalize://")); +} catch (TypeError $e) { + echo $e::class, ": ", $e->getMessage(), PHP_EOL; +} +?> +--EXPECT-- +hash_update_stream(), stream_read(): +TypeError: hash_update_stream(): Argument #1 ($context) must be a valid, non-finalized HashContext +stream_close + +hash_update_stream(), php_user_filter: +TypeError: hash_update_stream(): Argument #1 ($context) must be a valid, non-finalized HashContext + +hash_update_file(), stream_read(): +stream_close +TypeError: hash_update_file(): Argument #1 ($context) must be a valid, non-finalized HashContext + +hash_update_file(), stream_open(): +stream_close +TypeError: hash_update_file(): Argument #1 ($context) must be a valid, non-finalized HashContext