Skip to content

Commit

Permalink
Fix #81714: segfault when serializing finalized HashContext
Browse files Browse the repository at this point in the history
We must not allow to serialize already finalized `HashContext`s, since
the internal context is already freed.  Since there is not much point
in serializing finalized `HashContext`s, we just bail out in that case.

Closes GH-8265.
  • Loading branch information
cmb69 committed Apr 5, 2022
1 parent 43f3745 commit c2eafc2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ PHP NEWS
- Filter:
. Fixed signedness confusion in php_filter_validate_domain(). (cmb)

- Hash:
. Fixed bug #81714 (segfault when serializing finalized HashContext). (cmb)

- Intl:
. Fixed bug GH-8142 (Compilation error on cygwin). (David Carlier)

Expand Down
3 changes: 3 additions & 0 deletions ext/hash/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ PHP_HASH_API int php_hash_serialize_spec(const php_hashcontext_object *hash, zva
size_t pos = 0, max_alignment = 1;
unsigned char *buf = (unsigned char *) hash->context;
zval tmp;
if (buf == NULL) {
return FAILURE;
}
array_init(zv);
while (*spec != '\0' && *spec != '.') {
char spec_ch = *spec;
Expand Down
14 changes: 14 additions & 0 deletions ext/hash/tests/bug81714.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Bug #81714 (segfault when serializing finalized HashContext)
--FILE--
<?php
$h = hash_init('md5');
hash_final($h);
try {
serialize($h);
} catch (Exception $ex) {
var_dump($ex->getMessage());
}
?>
--EXPECTF--
string(52) "HashContext for algorithm "md5" cannot be serialized"

0 comments on commit c2eafc2

Please sign in to comment.