Fix memory leaks in BIO_dup_chain()
authorMatt Caswell <matt@openssl.org>
Thu, 30 Apr 2015 13:51:10 +0000 (14:51 +0100)
committerMatt Caswell <matt@openssl.org>
Wed, 10 Jun 2015 09:29:31 +0000 (10:29 +0100)
This fixes a memory leak that can occur whilst duplicating a BIO chain if
the call to CRYPTO_dup_ex_data() fails. It also fixes a second memory leak
where if a failure occurs after successfully creating the first BIO in the
chain, then the beginning of the new chain was not freed.

With thanks to the Open Crypto Audit Project for reporting this issue.

Reviewed-by: Stephen Henson <steve@openssl.org>
Conflicts:
crypto/bio/bio_lib.c

crypto/bio/bio_lib.c

index 5267010cb0d7ff51230e49c34bd72328f8ffe8b7..07934f8a667b991e800a187c74686b506f9d6f67 100644 (file)
@@ -536,8 +536,10 @@ BIO *BIO_dup_chain(BIO *in)
 
         /* copy app data */
         if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
-                                &bio->ex_data))
+                                &bio->ex_data)) {
+            BIO_free(new_bio);
             goto err;
+        }
 
         if (ret == NULL) {
             eoc = new_bio;
@@ -549,8 +551,8 @@ BIO *BIO_dup_chain(BIO *in)
     }
     return (ret);
  err:
-    if (ret != NULL)
-        BIO_free(ret);
+    BIO_free_all(ret);
+
     return (NULL);
 }