From cbe74ec8871c611fedd75ecdf73c8a6b609b35ec 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 028712321432..18e1189b0bc3 100644 --- a/NEWS +++ b/NEWS @@ -107,6 +107,8 @@ PHP NEWS - Hash: . Fixed hash() and hash_file() hanging on invalid xxh3/xxh128 options. (lacatoire) + . Fixed bug GH-25957 (Segfault when a stream finalizes the HashContext + during hash_update_stream() or hash_update_file()). (Tomasz Kowalczyk) - Iconv: . Fixed iconv_mime_encode() failing with an integer overflow error for a diff --git a/ext/hash/hash.c b/ext/hash/hash.c index 4525e4ad15e5..bce3c7f2932e 100644 --- a/ext/hash/hash.c +++ b/ext/hash/hash.c @@ -739,6 +739,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; @@ -774,6 +775,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..77de8bdd11c5 --- /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->getMessage(), "\n"; +} +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->getMessage(), "\n"; +} +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->getMessage(), "\n"; +} + +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->getMessage(), "\n"; +} +?> +--EXPECT-- +hash_update_stream(), stream_read(): +hash_update_stream(): Argument #1 ($context) must be a valid, non-finalized HashContext +stream_close + +hash_update_stream(), php_user_filter: +hash_update_stream(): Argument #1 ($context) must be a valid, non-finalized HashContext + +hash_update_file(), stream_read(): +stream_close +hash_update_file(): Argument #1 ($context) must be a valid, non-finalized HashContext + +hash_update_file(), stream_open(): +stream_close +hash_update_file(): Argument #1 ($context) must be a valid, non-finalized HashContext