Commit 7c5323a467c for php
commit 7c5323a467cca09c4d6f84d5e9db7aaa0c48ebc7
Author: lacatoire <la.catoire@gmail.com>
Date: Fri Oct 16 10:31:16 2026 +0200
Fix hash() and hash_file() hanging on invalid xxh3/xxh128 options (#25777)
The xxh3 and xxh128 initializers throw if the secret is too short or
if both a seed and a secret are supplied. hash() and hash_file() went
on to update the uninitialized context, which could hang on large inputs.
Check for an exception after initialization, free the hash context, and
close the stream if one was opened.
Closes #25777
diff --git a/NEWS b/NEWS
index 7fd9ed3c6ac..f4beb1dc6d0 100644
--- a/NEWS
+++ b/NEWS
@@ -94,6 +94,10 @@ PHP NEWS
. Fixed bug GH-25584 (JIT frees the string returned by gettext(),
dgettext() or dcgettext() while it is still in use). (Ilia Alshanetsky)
+- Hash:
+ . Fixed hash() and hash_file() hanging on invalid xxh3/xxh128 options.
+ (lacatoire)
+
- Intl:
. Fixed Locale::lookup() ignoring the default locale when the language tag
list is empty. (Weilin Du)
diff --git a/ext/hash/hash.c b/ext/hash/hash.c
index 93401852ad3..4525e4ad15e 100644
--- a/ext/hash/hash.c
+++ b/ext/hash/hash.c
@@ -381,6 +381,14 @@ static void php_hash_do_hash(
context = php_hash_alloc_context(ops);
ops->hash_init(context, args);
+ if (UNEXPECTED(EG(exception))) {
+ /* The options were rejected, the context is not usable */
+ php_hash_free_context(ops, context);
+ if (stream) {
+ php_stream_close(stream);
+ }
+ RETURN_THROWS();
+ }
if (isfilename) {
char buf[1024];
diff --git a/ext/hash/tests/xxh3_invalid_options_large_input.phpt b/ext/hash/tests/xxh3_invalid_options_large_input.phpt
new file mode 100644
index 00000000000..ab010dd3924
--- /dev/null
+++ b/ext/hash/tests/xxh3_invalid_options_large_input.phpt
@@ -0,0 +1,42 @@
+--TEST--
+hash(), hash_file(): invalid xxh3/xxh128 options throw for a large input instead of hanging
+--FILE--
+<?php
+$file = __DIR__ . '/xxh3_invalid_options_large_input.bin';
+file_put_contents($file, str_repeat('z', 5000));
+
+foreach (['xxh3', 'xxh128'] as $algo) {
+ foreach ([256, 300, 5000] as $n) {
+ try {
+ hash($algo, str_repeat('z', $n), false, ['secret' => 'short']);
+ } catch (Throwable $e) {
+ echo $algo, ' ', $n, ': ', $e::class, ': ', $e->getMessage(), "\n";
+ }
+ }
+ try {
+ hash($algo, str_repeat('z', 5000), false, ['seed' => 1, 'secret' => str_repeat('k', 136)]);
+ } catch (Throwable $e) {
+ echo $algo, ' seed and secret: ', $e::class, ': ', $e->getMessage(), "\n";
+ }
+ try {
+ hash_file($algo, $file, false, ['secret' => 'short']);
+ } catch (Throwable $e) {
+ echo $algo, ' file: ', $e::class, ': ', $e->getMessage(), "\n";
+ }
+}
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/xxh3_invalid_options_large_input.bin');
+?>
+--EXPECT--
+xxh3 256: Error: xxh3: Secret length must be >= 136 bytes, 5 bytes passed
+xxh3 300: Error: xxh3: Secret length must be >= 136 bytes, 5 bytes passed
+xxh3 5000: Error: xxh3: Secret length must be >= 136 bytes, 5 bytes passed
+xxh3 seed and secret: Error: xxh3: Only one of seed or secret is to be passed for initialization
+xxh3 file: Error: xxh3: Secret length must be >= 136 bytes, 5 bytes passed
+xxh128 256: Error: xxh128: Secret length must be >= 136 bytes, 5 bytes passed
+xxh128 300: Error: xxh128: Secret length must be >= 136 bytes, 5 bytes passed
+xxh128 5000: Error: xxh128: Secret length must be >= 136 bytes, 5 bytes passed
+xxh128 seed and secret: Error: xxh128: Only one of seed or secret is to be passed for initialization
+xxh128 file: Error: xxh128: Secret length must be >= 136 bytes, 5 bytes passed