Fix a possible memleak
authorRichard Levitte <levitte@openssl.org>
Fri, 18 Dec 2015 12:03:45 +0000 (13:03 +0100)
committerRichard Levitte <levitte@openssl.org>
Sat, 2 Jan 2016 19:39:24 +0000 (20:39 +0100)
If there's a failure allocating md_data, the destination pctx will have
a shared pointer with the source EVP_MD_CTX, which will lead to problems
when either the source or the destination is freed.

Reviewed-by: Stephen Henson <steve@openssl.org>
crypto/evp/digest.c

index 5da0e01039a6da039942db9f1bf8b70753f509f2..33688f99e432c383286d4f10e0a350247818b328 100644 (file)
@@ -312,6 +312,13 @@ int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
     EVP_MD_CTX_reset(out);
     memcpy(out, in, sizeof(*out));
 
     EVP_MD_CTX_reset(out);
     memcpy(out, in, sizeof(*out));
 
+    /* Null these variables, since they are getting fixed up
+     * properly below.  Anything else may cause a memleak and/or
+     * double free if any of the memory allocations below fail
+     */
+    out->md_data = NULL;
+    out->pctx = NULL;
+
     if (in->md_data && out->digest->ctx_size) {
         if (tmp_buf)
             out->md_data = tmp_buf;
     if (in->md_data && out->digest->ctx_size) {
         if (tmp_buf)
             out->md_data = tmp_buf;