Skip to content

Commit 5a608bb

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Update NEWS for OpenSSL changes Fix memory leaks in openssl_cms_encrypt() when push fails Fix memory leaks in openssl_pkcs7_encrypt() when push fails Fix missing error propagation when php_array_to_X509_sk() fails Fix memory leaks in php_array_to_X509_sk() when push fails Fix memory leak in php_openssl_load_all_certs_from_file() when push fails
2 parents da43645 + e8a274e commit 5a608bb

2 files changed

Lines changed: 41 additions & 17 deletions

File tree

ext/openssl/openssl.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,8 +1283,6 @@ PHP_FUNCTION(openssl_x509_free)
12831283
}
12841284
/* }}} */
12851285

1286-
/* }}} */
1287-
12881286
/* {{{ Creates and exports a PKCS to file */
12891287
PHP_FUNCTION(openssl_pkcs12_export_to_file)
12901288
{
@@ -1350,6 +1348,9 @@ PHP_FUNCTION(openssl_pkcs12_export_to_file)
13501348

13511349
if (args && (item = zend_hash_str_find(Z_ARRVAL_P(args), "extracerts", sizeof("extracerts")-1)) != NULL) {
13521350
ca = php_openssl_array_to_X509_sk(item, 5, "extracerts");
1351+
if (!ca) {
1352+
goto cleanup;
1353+
}
13531354
}
13541355
/* end parse extra config */
13551356

@@ -1443,6 +1444,9 @@ PHP_FUNCTION(openssl_pkcs12_export)
14431444

14441445
if (args && (item = zend_hash_str_find(Z_ARRVAL_P(args), "extracerts", sizeof("extracerts")-1)) != NULL) {
14451446
ca = php_openssl_array_to_X509_sk(item, 5, "extracerts");
1447+
if (!ca) {
1448+
goto cleanup;
1449+
}
14461450
}
14471451
/* end parse extra config */
14481452

@@ -2662,7 +2666,10 @@ PHP_FUNCTION(openssl_pkcs7_encrypt)
26622666
goto clean_exit;
26632667
}
26642668
}
2665-
sk_X509_push(recipcerts, cert);
2669+
if (sk_X509_push(recipcerts, cert) <= 0) {
2670+
X509_free(cert);
2671+
goto clean_exit;
2672+
}
26662673
} ZEND_HASH_FOREACH_END();
26672674
} else {
26682675
/* a single certificate */
@@ -2683,7 +2690,10 @@ PHP_FUNCTION(openssl_pkcs7_encrypt)
26832690
goto clean_exit;
26842691
}
26852692
}
2686-
sk_X509_push(recipcerts, cert);
2693+
if (sk_X509_push(recipcerts, cert) <= 0) {
2694+
X509_free(cert);
2695+
goto clean_exit;
2696+
}
26872697
}
26882698

26892699
/* sanity check the cipher */
@@ -3278,7 +3288,10 @@ PHP_FUNCTION(openssl_cms_encrypt)
32783288
goto clean_exit;
32793289
}
32803290
}
3281-
sk_X509_push(recipcerts, cert);
3291+
if (sk_X509_push(recipcerts, cert) <= 0) {
3292+
php_openssl_store_errors();
3293+
goto clean_exit;
3294+
}
32823295
} ZEND_HASH_FOREACH_END();
32833296
} else {
32843297
/* a single certificate */
@@ -3298,7 +3311,10 @@ PHP_FUNCTION(openssl_cms_encrypt)
32983311
goto clean_exit;
32993312
}
33003313
}
3301-
sk_X509_push(recipcerts, cert);
3314+
if (sk_X509_push(recipcerts, cert) <= 0) {
3315+
php_openssl_store_errors();
3316+
goto clean_exit;
3317+
}
33023318
}
33033319

33043320
/* sanity check the cipher */

ext/openssl/openssl_backend_common.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -692,29 +692,25 @@ STACK_OF(X509) *php_openssl_load_all_certs_from_file(
692692
X509_INFO *xi;
693693
char cert_path[MAXPATHLEN];
694694

695-
if(!(stack = sk_X509_new_null())) {
696-
php_openssl_store_errors();
697-
php_error_docref(NULL, E_ERROR, "Memory allocation failure");
698-
goto end;
699-
}
700-
701695
if (!php_openssl_check_path(cert_file, cert_file_len, cert_path, arg_num)) {
702-
sk_X509_free(stack);
703696
goto end;
704697
}
705698

706699
if (!(in = BIO_new_file(cert_path, PHP_OPENSSL_BIO_MODE_R(PKCS7_BINARY)))) {
707700
php_openssl_store_errors();
708701
php_error_docref(NULL, E_WARNING, "Error opening the file, %s", cert_path);
709-
sk_X509_free(stack);
710702
goto end;
711703
}
712704

713705
/* This loads from a file, a stack of x509/crl/pkey sets */
714706
if (!(sk = php_openssl_pem_read_bio_x509_info(in))) {
715707
php_openssl_store_errors();
716708
php_error_docref(NULL, E_WARNING, "Error reading the file, %s", cert_path);
717-
sk_X509_free(stack);
709+
goto end;
710+
}
711+
712+
if(!(stack = sk_X509_new_reserve(NULL, sk_X509_INFO_num(sk)))) {
713+
php_openssl_store_errors();
718714
goto end;
719715
}
720716

@@ -886,7 +882,10 @@ STACK_OF(X509) *php_openssl_array_to_X509_sk(zval * zcerts, uint32_t arg_num, co
886882
}
887883

888884
}
889-
sk_X509_push(sk, cert);
885+
if (sk_X509_push(sk, cert) <= 0) {
886+
X509_free(cert);
887+
goto push_fail_exit;
888+
}
890889
} ZEND_HASH_FOREACH_END();
891890
} else {
892891
/* a single certificate */
@@ -904,11 +903,20 @@ STACK_OF(X509) *php_openssl_array_to_X509_sk(zval * zcerts, uint32_t arg_num, co
904903
goto clean_exit;
905904
}
906905
}
907-
sk_X509_push(sk, cert);
906+
if (sk_X509_push(sk, cert) <= 0) {
907+
X509_free(cert);
908+
goto push_fail_exit;
909+
}
908910
}
909911

910912
clean_exit:
911913
return sk;
914+
915+
push_fail_exit:
916+
php_openssl_store_errors();
917+
php_openssl_sk_X509_free(sk);
918+
sk = NULL;
919+
goto clean_exit;
912920
}
913921

914922
zend_result php_openssl_csr_add_subj_entry(zval *item, X509_NAME *subj, int nid)

0 commit comments

Comments
 (0)