Commit 126976d7a24 for php

commit 126976d7a244f9a6051193cc0bcc13296a417dfc
Author: Tomasz Kowalczyk <tomasz@kowalczyk.cc>
Date:   Tue Dec 1 16:46:25 2026 +0100

    Fix GH-25957: Segfault when a stream finalizes the HashContext during hash_update_stream() or hash_update_file() (#25958)

    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.

diff --git a/NEWS b/NEWS
index 36ab60a871e..9ac857515fc 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 8658f8449c5..f7edde3c143 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 00000000000..6c1ddc1c076
--- /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--
+<?php
+function finalize(string $where): void {
+    global $ctx, $finalizeIn;
+    if ($finalizeIn === $where) {
+        $finalizeIn = null;
+        hash_final($ctx);
+    }
+}
+
+class Wrapper {
+    public $context;
+    private bool $eof = false;
+
+    public function stream_open($path, $mode, $options, &$opened_path): bool {
+        finalize("stream_open");
+        return true;
+    }
+
+    public function stream_read($count): string {
+        finalize("stream_read");
+        $this->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