From c380bff888bfd5e48c4b24250ba1996b0fd1a5e3 Mon Sep 17 00:00:00 2001 From: Matt Caswell Date: Thu, 19 Mar 2015 10:16:32 +0000 Subject: [PATCH] Fix a failure to NULL a pointer freed on error. Reported by the LibreSSL project as a follow on to CVE-2015-0209 Reviewed-by: Richard Levitte --- crypto/asn1/x_x509.c | 12 +++++++++++- crypto/ec/ec_asn1.c | 7 +++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/crypto/asn1/x_x509.c b/crypto/asn1/x_x509.c index 018a18bce1..d6958f6c1a 100644 --- a/crypto/asn1/x_x509.c +++ b/crypto/asn1/x_x509.c @@ -179,8 +179,14 @@ X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length) { const unsigned char *q; X509 *ret; + int freeret = 0; + /* Save start position */ q = *pp; + + if(!a || *a == NULL) { + freeret = 1; + } ret = d2i_X509(a, pp, length); /* If certificate unreadable then forget it */ if (!ret) @@ -193,7 +199,11 @@ X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length) goto err; return ret; err: - X509_free(ret); + if(freeret) { + X509_free(ret); + if (a) + *a = NULL; + } return NULL; } diff --git a/crypto/ec/ec_asn1.c b/crypto/ec/ec_asn1.c index d3e8316ffb..4ca2545e84 100644 --- a/crypto/ec/ec_asn1.c +++ b/crypto/ec/ec_asn1.c @@ -1196,16 +1196,19 @@ EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len) ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE); return NULL; } - if (a) - *a = ret; } else ret = *a; if (!d2i_ECPKParameters(&ret->group, in, len)) { ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB); + if (a == NULL || *a != ret) + EC_KEY_free(ret); return NULL; } + if (a) + *a = ret; + return ret; } -- 2.25.1