Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions ext/hash/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
108 changes: 108 additions & 0 deletions ext/hash/tests/gh25957.phpt
Original file line number Diff line number Diff line change
@@ -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->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
Loading