Skip to content

Commit 1c94175

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: Fix memory leaks when adding certificate to store fails Fix missing error propagation in openssl_x509_export_to_file() Fix memory leak on error path in openssl_open()
2 parents 513f129 + 4bb68c5 commit 1c94175

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

ext/openssl/openssl.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -564,14 +564,11 @@ PHP_FUNCTION(openssl_x509_export_to_file)
564564

565565
bio_out = BIO_new_file(file_path, PHP_OPENSSL_BIO_MODE_W(PKCS7_BINARY));
566566
if (bio_out) {
567-
if (!notext && !X509_print(bio_out, cert)) {
568-
php_openssl_store_errors();
569-
}
570-
if (!PEM_write_bio_X509(bio_out, cert)) {
567+
if ((notext || X509_print(bio_out, cert)) && PEM_write_bio_X509(bio_out, cert)) {
568+
RETVAL_TRUE;
569+
} else {
571570
php_openssl_store_errors();
572571
}
573-
574-
RETVAL_TRUE;
575572
} else {
576573
php_openssl_store_errors();
577574
php_error_docref(NULL, E_WARNING, "Error opening file %s", file_path);
@@ -4381,18 +4378,20 @@ PHP_FUNCTION(openssl_open)
43814378
cipher = php_openssl_get_evp_cipher_by_name(method);
43824379
if (!cipher) {
43834380
php_error_docref(NULL, E_WARNING, "Unknown cipher algorithm");
4384-
RETURN_FALSE;
4381+
RETVAL_FALSE;
4382+
goto out_pkey;
43854383
}
43864384

43874385
cipher_iv_len = EVP_CIPHER_iv_length(cipher);
43884386
if (cipher_iv_len > 0) {
43894387
if (!iv) {
43904388
zend_argument_value_error(6, "cannot be null for the chosen cipher algorithm");
4391-
RETURN_THROWS();
4389+
goto out_pkey;
43924390
}
43934391
if ((size_t)cipher_iv_len != iv_len) {
43944392
php_error_docref(NULL, E_WARNING, "IV length is invalid");
4395-
RETURN_FALSE;
4393+
RETVAL_FALSE;
4394+
goto out_pkey;
43964395
}
43974396
iv_buf = (unsigned char *)iv;
43984397
} else {
@@ -4414,8 +4413,9 @@ PHP_FUNCTION(openssl_open)
44144413
}
44154414

44164415
efree(buf);
4417-
EVP_PKEY_free(pkey);
44184416
EVP_CIPHER_CTX_free(ctx);
4417+
out_pkey:
4418+
EVP_PKEY_free(pkey);
44194419
}
44204420
/* }}} */
44214421

ext/openssl/xp_ssl.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,8 +857,9 @@ static long php_openssl_load_stream_cafile(X509_STORE *cert_store, const char *c
857857
buffer_active = 0;
858858
if (cert && X509_STORE_add_cert(cert_store, cert)) {
859859
++certs_added;
860-
X509_free(cert);
861860
}
861+
/* TODO: notify user when adding certificate failed? */
862+
X509_free(cert);
862863
goto cert_start;
863864
}
864865

0 commit comments

Comments
 (0)