Skip to content

Fix various memory leaks on error conditions in openssl_x509_parse() #16690

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions ext/openssl/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2091,15 +2091,15 @@ PHP_FUNCTION(openssl_x509_parse)
/* Can return NULL on error or memory allocation failure */
if (!bn_serial) {
php_openssl_store_errors();
RETURN_FALSE;
goto err;
}

hex_serial = BN_bn2hex(bn_serial);
BN_free(bn_serial);
/* Can return NULL on error or memory allocation failure */
if (!hex_serial) {
php_openssl_store_errors();
RETURN_FALSE;
goto err;
}

str_serial = i2s_ASN1_INTEGER(NULL, asn1_serial);
Expand Down Expand Up @@ -2171,19 +2171,15 @@ PHP_FUNCTION(openssl_x509_parse)
bio_out = BIO_new(BIO_s_mem());
if (bio_out == NULL) {
php_openssl_store_errors();
RETURN_FALSE;
goto err_subitem;
}
if (nid == NID_subject_alt_name) {
if (openssl_x509v3_subjectAltName(bio_out, extension) == 0) {
BIO_get_mem_ptr(bio_out, &bio_buf);
add_assoc_stringl(&subitem, extname, bio_buf->data, bio_buf->length);
} else {
zend_array_destroy(Z_ARR_P(return_value));
BIO_free(bio_out);
if (cert_str) {
X509_free(cert);
}
RETURN_FALSE;
goto err_subitem;
}
}
else if (X509V3_EXT_print(bio_out, extension, 0, 0)) {
Expand All @@ -2198,6 +2194,16 @@ PHP_FUNCTION(openssl_x509_parse)
if (cert_str) {
X509_free(cert);
}
return;

err_subitem:
zval_ptr_dtor(&subitem);
err:
zend_array_destroy(Z_ARR_P(return_value));
if (cert_str) {
X509_free(cert);
}
RETURN_FALSE;
}
/* }}} */

Expand Down
Loading