Skip to content

Commit 8569b82

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Fixing memory leak in zif_openssl_seal and zif_openssl_open when fetching cipher with php_openssl_get_evp_cipher_by_name (#21967) Fixing memory leak in php_openssl_x509_fingerprint when getting mdtype with php_openssl_get_evp_md_by_name (#21965) Fixing memory leak in openssl_pkcs12_read when zout initialisation fails (#21752)
2 parents 22a2c2e + ced518e commit 8569b82

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

ext/openssl/openssl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,6 +1911,7 @@ PHP_FUNCTION(openssl_pkcs12_read)
19111911

19121912
zout = zend_try_array_init(zout);
19131913
if (!zout) {
1914+
sk_X509_pop_free(ca, X509_free);
19141915
goto cleanup;
19151916
}
19161917

@@ -4697,6 +4698,7 @@ PHP_FUNCTION(openssl_seal)
46974698

46984699
iv_len = EVP_CIPHER_iv_length(cipher);
46994700
if (!iv && iv_len > 0) {
4701+
php_openssl_release_evp_cipher(cipher);
47004702
zend_argument_value_error(6, "cannot be null for the chosen cipher algorithm");
47014703
RETURN_THROWS();
47024704
}
@@ -4783,6 +4785,7 @@ PHP_FUNCTION(openssl_seal)
47834785
efree(eks);
47844786
efree(eksl);
47854787
efree(pkeys);
4788+
php_openssl_release_evp_cipher(cipher);
47864789
}
47874790
/* }}} */
47884791

@@ -4859,6 +4862,7 @@ PHP_FUNCTION(openssl_open)
48594862
EVP_CIPHER_CTX_free(ctx);
48604863
out_pkey:
48614864
EVP_PKEY_free(pkey);
4865+
php_openssl_release_evp_cipher(cipher);
48624866
}
48634867
/* }}} */
48644868

ext/openssl/openssl_backend_common.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ zend_string* php_openssl_x509_fingerprint(X509 *peer, const char *method, bool r
593593
php_error_docref(NULL, E_WARNING, "Unknown digest algorithm");
594594
return NULL;
595595
} else if (!X509_digest(peer, mdtype, md, &n)) {
596+
php_openssl_release_evp_md(mdtype);
596597
php_openssl_store_errors();
597598
php_error_docref(NULL, E_ERROR, "Could not generate signature");
598599
return NULL;
@@ -606,6 +607,7 @@ zend_string* php_openssl_x509_fingerprint(X509 *peer, const char *method, bool r
606607
ZSTR_VAL(ret)[n * 2] = '\0';
607608
}
608609

610+
php_openssl_release_evp_md(mdtype);
609611
return ret;
610612
}
611613

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Memory leak when array initialization in openssl_pkcs12_read() fails
3+
--EXTENSIONS--
4+
openssl
5+
--FILE--
6+
<?php
7+
$pfx = __DIR__ . DIRECTORY_SEPARATOR . "bug74022.pfx";
8+
$cert_store = file_get_contents($pfx);
9+
10+
class Typed {
11+
public string $foo = "bar";
12+
}
13+
14+
$typed = new Typed;
15+
16+
try {
17+
openssl_pkcs12_read($cert_store, $typed->foo, "csos");
18+
} catch (TypeError $e) {
19+
echo $e::class, ": ", $e->getMessage(), "\n";
20+
}
21+
?>
22+
--EXPECT--
23+
TypeError: Cannot assign array to reference held by property Typed::$foo of type string

0 commit comments

Comments
 (0)