Skip to content

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

Merged
iliaal merged 1 commit into
php:masterfrom
thunderer:fix/hash-update-stream-finalized
Dec 1, 2026
Merged

iliaal merged 1 commit into
php:masterfrom
thunderer:fix/hash-update-stream-finalized

Conversation

@thunderer

@thunderer thunderer commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes GH-25957.

hash_update_stream() and hash_update_file() check the context once, before the loop. php_stream_read() can run user code (stream_open() or stream_read() of a stream wrapper, or a php_user_filter), and hash_final() there frees the context. The fix checks the context again after each read and throws the same TypeError as for a context that was finalized before the call. hash_update_file() closes its stream before it throws.

@iliaal iliaal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs user code that finalizes the context from inside a stream wrapper or filter. Fixes for that class have been going to master only (PR 22470, PR 22714), so please retarget to master and move the NEWS entry accordingly.

Comment thread ext/hash/tests/gh25957.phpt Outdated
try {
var_dump(hash_update_stream($ctx, $fp));
} catch (TypeError $e) {
echo $e->getMessage(), "\n";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: please print the exception class too (echo $e::class, ": ", $e->getMessage(), PHP_EOL;) here and in the other catch blocks, so the test pins the TypeError.

…ing 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.
@thunderer
thunderer changed the base branch from PHP-8.4 to master December 1, 2026 12:53
@thunderer
thunderer force-pushed the fix/hash-update-stream-finalized branch from cbe74ec to 440c523 Compare December 1, 2026 12:53
@iliaal
iliaal merged commit 126976d into php:master Dec 1, 2026
16 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segfault when a stream finalizes the HashContext during hash_update_stream() or hash_update_file()

2 participants