From aea20c15c9879e7b0473943ab690fa8c2fe042f1 Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Mon, 20 Mar 2017 17:16:19 -0400 Subject: [PATCH] Fix potential memory leak in ASN1_TIME_to_generalizedtime() If ret is allocated, it may be leaked on error. (Imported from upstream's cdfb7809b6a365a0a7874afd8f8778c5c572f267 and ffcdb0e6efb6fb7033b2cd29e8cca2e2fe355c14.) Change-Id: I50ed9ad072cf80461d9527d0834b596a8c32e3d3 Reviewed-on: https://boringssl-review.googlesource.com/14315 Reviewed-by: Steven Valdez Commit-Queue: Steven Valdez CQ-Verified: CQ bot account: commit-bot@chromium.org --- crypto/asn1/a_time.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/crypto/asn1/a_time.c b/crypto/asn1/a_time.c index 4b584297..c962c0bf 100644 --- a/crypto/asn1/a_time.c +++ b/crypto/asn1/a_time.c @@ -114,7 +114,7 @@ int ASN1_TIME_check(ASN1_TIME *t) ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t, ASN1_GENERALIZEDTIME **out) { - ASN1_GENERALIZEDTIME *ret; + ASN1_GENERALIZEDTIME *ret = NULL; char *str; int newlen; @@ -123,22 +123,21 @@ ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t, if (!out || !*out) { if (!(ret = ASN1_GENERALIZEDTIME_new())) - return NULL; - if (out) - *out = ret; - } else + goto err; + } else { ret = *out; + } /* If already GeneralizedTime just copy across */ if (t->type == V_ASN1_GENERALIZEDTIME) { if (!ASN1_STRING_set(ret, t->data, t->length)) - return NULL; - return ret; + goto err; + goto done; } /* grow the string */ if (!ASN1_STRING_set(ret, NULL, t->length + 2)) - return NULL; + goto err; /* ASN1_STRING_set() allocated 'len + 1' bytes. */ newlen = t->length + 2 + 1; str = (char *)ret->data; @@ -150,9 +149,18 @@ ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t, BUF_strlcat(str, (char *)t->data, newlen); - return ret; + done: + if (out != NULL && *out == NULL) + *out = ret; + return ret; + + err: + if (out == NULL || *out != ret) + ASN1_GENERALIZEDTIME_free(ret); + return NULL; } + int ASN1_TIME_set_string(ASN1_TIME *s, const char *str) { ASN1_TIME t;