[php-src] PHP-8.4: Fix hash() and hash_file() hanging on invalid xxh3/xxh128 options (#25777)

From: Date: Thu, 26 Nov 2026 12:01:03 +0000
Subject: [php-src] PHP-8.4: Fix hash() and hash_file() hanging on invalid xxh3/xxh128 options (#25777)
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-144210@lists.php.net to get a copy of this message
Author: lacatoire (lacatoire) Committer: Weilin Du (LamentXU123) Date: 2026-11-26T19:53:57+08:00 Commit: https://github.com/php/php-src/commit/7c5323a467cca09c4d6f84d5e9db7aaa0c48ebc7 Raw diff: https://github.com/php/php-src/commit/7c5323a467cca09c4d6f84d5e9db7aaa0c48ebc7.diff 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 Changed paths: A ext/hash/tests/xxh3_invalid_options_large_input.phpt M NEWS M ext/hash/hash.c Diff: diff --git a/NEWS b/NEWS index 7fd9ed3c6ac7..f4beb1dc6d04 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 93401852ad3d..4525e4ad15e5 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 000000000000..ab010dd3924f --- /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

« previous php.cvs (#144210) next »