Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GH-12186: segfault copying/cloning a finalized HashContext #12187

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion ext/hash/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ PHP_FUNCTION(hash_init)

#define PHP_HASHCONTEXT_VERIFY(hash) { \
if (!hash->context) { \
zend_argument_type_error(1, "must be a valid Hash Context resource"); \
zend_argument_type_error(1, "must be a valid, non-finalized HashContext"); \
RETURN_THROWS(); \
} \
}
Expand Down Expand Up @@ -837,11 +837,15 @@ PHP_FUNCTION(hash_final)
PHP_FUNCTION(hash_copy)
{
zval *zhash;
php_hashcontext_object *context;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zhash, php_hashcontext_ce) == FAILURE) {
RETURN_THROWS();
}

context = php_hashcontext_from_object(Z_OBJ_P(zhash));
PHP_HASHCONTEXT_VERIFY(context);

RETVAL_OBJ(Z_OBJ_HANDLER_P(zhash, clone_obj)(Z_OBJ_P(zhash)));

if (php_hashcontext_from_object(Z_OBJ_P(return_value))->context == NULL) {
Expand Down Expand Up @@ -1405,6 +1409,11 @@ static zend_object *php_hashcontext_clone(zend_object *zobj) {
zend_object *znew = php_hashcontext_create(zobj->ce);
php_hashcontext_object *newobj = php_hashcontext_from_object(znew);

if (!oldobj->context) {
zend_throw_exception(zend_ce_value_error, "Cannot clone a finalized HashContext", 0);
return &newobj->std;
}

MaxSem marked this conversation as resolved.
Show resolved Hide resolved
zend_objects_clone_members(znew, zobj);

newobj->ops = oldobj->ops;
Expand Down
17 changes: 17 additions & 0 deletions ext/hash/tests/gh12186_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Hash: bug #12186 - segfault in hash_copy() on a finalized context
--FILE--
<?php

$c = hash_init('sha1');
hash_final($c);

try {
hash_copy($c);
} catch (Throwable $ex) {
echo $ex->getMessage() . "\n";
}

?>
--EXPECTF--
hash_copy(): Argument #1 ($context) must be a valid, non-finalized HashContext
17 changes: 17 additions & 0 deletions ext/hash/tests/gh12186_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Hash: bug #12186 - segfault when cloning a finalized context
--FILE--
<?php

$c = hash_init('sha1');
hash_final($c);

try {
clone $c;
} catch (Throwable $ex) {
echo $ex->getMessage() . "\n";
}

?>
--EXPECTF--
Cannot clone a finalized HashContext
2 changes: 1 addition & 1 deletion ext/hash/tests/reuse.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ catch (\Error $e) {

?>
--EXPECT--
hash_update(): Argument #1 ($context) must be a valid Hash Context resource
hash_update(): Argument #1 ($context) must be a valid, non-finalized HashContext