Skip to content

Commit 5489fde

Browse files
committed
CDRIVER-3408 leak fix
1 parent 359cf9b commit 5489fde

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

src/libmongoc/src/mongoc/mongoc-stream-tls-openssl-private.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
BSON_BEGIN_DECLS
2626

2727
typedef struct {
28-
const char *host;
29-
bool allow_invalid_hostname;
30-
bool weak_cert_validation;
28+
char *host;
29+
bool allow_invalid_hostname;
30+
bool weak_cert_validation;
3131
} mongoc_openssl_ocsp_opt_t;
3232

33+
void
34+
mongoc_openssl_ocsp_opt_destroy (void *ocsp_opt);
35+
3336
/**
3437
* mongoc_stream_tls_openssl_t:
3538
*

src/libmongoc/src/mongoc/mongoc-stream-tls-openssl.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ _mongoc_stream_tls_openssl_destroy (mongoc_stream_t *stream)
9494
SSL_CTX_free (openssl->ctx);
9595
openssl->ctx = NULL;
9696

97-
if (openssl->ocsp_opts)
98-
bson_free ((char *) openssl->ocsp_opts->host);
99-
bson_free (openssl->ocsp_opts);
97+
mongoc_openssl_ocsp_opt_destroy (openssl->ocsp_opts);
10098
openssl->ocsp_opts = NULL;
10199

102100
bson_free (openssl);
@@ -566,13 +564,21 @@ _mongoc_stream_tls_openssl_handshake (mongoc_stream_t *stream,
566564
if (BIO_do_handshake (openssl->bio) == 1) {
567565
#if (OPENSSL_VERSION_NUMBER >= 0x10002000L)
568566
X509 *peer = NULL;
569-
peer = SSL_get_peer_certificate (ssl);
570567

571-
if (tls->ssl_opts.allow_invalid_hostname ||
572-
X509_check_host (peer, host, 0, 0, NULL) == 1 ||
573-
X509_check_ip_asc (peer, host, 0) == 1) {
568+
if (tls->ssl_opts.allow_invalid_hostname) {
569+
RETURN (true);
570+
}
571+
572+
peer = SSL_get_peer_certificate (ssl);
573+
if (peer && (X509_check_host (peer, host, 0, 0, NULL) == 1 ||
574+
X509_check_ip_asc (peer, host, 0) == 1)) {
575+
X509_free (peer);
574576
RETURN (true);
575577
}
578+
579+
if (peer) {
580+
X509_free (peer);
581+
}
576582
#else
577583
if (_mongoc_openssl_check_cert (
578584
ssl, host, tls->ssl_opts.allow_invalid_hostname)) {
@@ -733,14 +739,15 @@ mongoc_stream_tls_openssl_new (mongoc_stream_t *base_stream,
733739
#ifdef MONGOC_ENABLE_OCSP
734740
} else {
735741
if (!SSL_CTX_set_tlsext_status_type (ssl_ctx, TLSEXT_STATUSTYPE_ocsp)) {
742+
MONGOC_ERROR ("cannot enable OCSP status request extension");
736743
SSL_CTX_free (ssl_ctx);
737744
RETURN (NULL);
738745
}
739746

740-
ocsp_opts = bson_malloc(sizeof(mongoc_openssl_ocsp_opt_t));
747+
ocsp_opts = bson_malloc (sizeof (mongoc_openssl_ocsp_opt_t));
741748
ocsp_opts->allow_invalid_hostname = opt->allow_invalid_hostname;
742749
ocsp_opts->weak_cert_validation = opt->weak_cert_validation;
743-
ocsp_opts->host = bson_strdup(host);
750+
ocsp_opts->host = bson_strdup (host);
744751

745752
SSL_CTX_set_tlsext_status_arg (ssl_ctx, ocsp_opts);
746753
SSL_CTX_set_tlsext_status_cb (ssl_ctx, _mongoc_ocsp_tlsext_status_cb);
@@ -755,12 +762,14 @@ mongoc_stream_tls_openssl_new (mongoc_stream_t *base_stream,
755762

756763
bio_ssl = BIO_new_ssl (ssl_ctx, client);
757764
if (!bio_ssl) {
765+
mongoc_openssl_ocsp_opt_destroy (ocsp_opts);
758766
SSL_CTX_free (ssl_ctx);
759767
RETURN (NULL);
760768
}
761769
meth = mongoc_stream_tls_openssl_bio_meth_new ();
762770
bio_mongoc_shim = BIO_new (meth);
763771
if (!bio_mongoc_shim) {
772+
mongoc_openssl_ocsp_opt_destroy (ocsp_opts);
764773
BIO_free_all (bio_ssl);
765774
BIO_meth_free (meth);
766775
RETURN (NULL);
@@ -810,4 +819,17 @@ mongoc_stream_tls_openssl_new (mongoc_stream_t *base_stream,
810819
RETURN ((mongoc_stream_t *) tls);
811820
}
812821

822+
void
823+
mongoc_openssl_ocsp_opt_destroy (void *ocsp_opt)
824+
{
825+
mongoc_openssl_ocsp_opt_t *casted;
826+
827+
if (!ocsp_opt) {
828+
return;
829+
}
830+
casted = (mongoc_openssl_ocsp_opt_t *) ocsp_opt;
831+
bson_free (casted->host);
832+
bson_free (ocsp_opt);
833+
}
834+
813835
#endif /* MONGOC_ENABLE_SSL_OPENSSL */

0 commit comments

Comments
 (0)