Skip to content

Commit e3eba0d

Browse files
committed
Fix memory leak regression in openssl_pbkdf2()
We're fetching the digest using the new method, but if an alias is used, the method is fetched via EVP_MD_fetch() which requires lifetime management. This is observable when using "sha-256" instead of "sha256" as an algorithm name. This is a regression in comparison to PHP 8.4.
1 parent 964d087 commit e3eba0d

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

ext/openssl/openssl.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,12 +2446,14 @@ PHP_FUNCTION(openssl_pbkdf2)
24462446

24472447
if (PKCS5_PBKDF2_HMAC(password, (int)password_len, (unsigned char *)salt, (int)salt_len, (int)iterations, digest, (int)key_length, (unsigned char*)ZSTR_VAL(out_buffer)) == 1) {
24482448
ZSTR_VAL(out_buffer)[key_length] = 0;
2449-
RETURN_NEW_STR(out_buffer);
2449+
RETVAL_NEW_STR(out_buffer);
24502450
} else {
24512451
php_openssl_store_errors();
24522452
zend_string_release_ex(out_buffer, 0);
2453-
RETURN_FALSE;
2453+
RETVAL_FALSE;
24542454
}
2455+
2456+
php_openssl_release_evp_md(digest);
24552457
}
24562458
/* }}} */
24572459

ext/openssl/tests/openssl_pbkdf2_basic.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ var_dump(bin2hex(openssl_pbkdf2('password', 'salt', 20, 16777216)));
1919
var_dump(bin2hex(openssl_pbkdf2('passwordPASSWORDpassword', 'saltSALTsaltSALTsaltSALTsaltSALTsalt', 25, 4096)));
2020
var_dump(bin2hex(openssl_pbkdf2("pass\0word", "sa\0lt", 16, 4096)));
2121

22+
var_dump(bin2hex(openssl_pbkdf2("password", "salt", 16, 4096, "sha-256")));
23+
2224
?>
2325
--EXPECT--
2426
string(40) "0c60c80f961f0e71f3a9b524af6012062fe037a6"

0 commit comments

Comments
 (0)