]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Work around a behavior change in openssl's BUF_MEM code
authorNick Mathewson <nickm@torproject.org>
Thu, 3 Nov 2016 14:46:27 +0000 (10:46 -0400)
committerNick Mathewson <nickm@torproject.org>
Thu, 3 Nov 2016 14:51:10 +0000 (10:51 -0400)
In our code to write public keys to a string, for some unfathomable
reason since 253f0f160e1185c, we would allocate a memory BIO, then
set the NOCLOSE flag on it, extract its memory buffer, and free it.
Then a little while later we'd free the memory buffer with
BUF_MEM_free().

As of openssl 1.1 this doesn't work any more, since there is now a
BIO_BUF_MEM structure that wraps the BUF_MEM structure.  This
BIO_BUF_MEM doesn't get freed in our code.

So, we had a memory leak!

Is this an openssl bug?  Maybe.  But our code was already pretty
silly.  Why mess around with the NOCLOSE flag here when we can just
keep the BIO object around until we don't need the buffer any more?

Fixes bug 20553; bugfix on 0.0.2pre8

changes/bug20553 [new file with mode: 0644]
src/common/crypto.c
src/tools/tor-gencert.c

diff --git a/changes/bug20553 b/changes/bug20553
new file mode 100644 (file)
index 0000000..12a2780
--- /dev/null
@@ -0,0 +1,3 @@
+  o Minor bugfixes (memory leak):
+    - Work around a memory leak in OpenSSL 1.1 when encoding public keys.
+      Fixes bug 20553; bugfix on 0.0.2pre8.
index 2b96324d33e454a074717adc5e9a822f94175e9b..c5d07dfb61b92e95ff81b2495c037db5a396f9d0 100644 (file)
@@ -755,14 +755,13 @@ crypto_pk_write_key_to_string_impl(crypto_pk_t *env, char **dest,
   }
 
   BIO_get_mem_ptr(b, &buf);
-  (void)BIO_set_close(b, BIO_NOCLOSE); /* so BIO_free doesn't free buf */
-  BIO_free(b);
 
   *dest = tor_malloc(buf->length+1);
   memcpy(*dest, buf->data, buf->length);
   (*dest)[buf->length] = 0; /* nul terminate it */
   *len = buf->length;
-  BUF_MEM_free(buf);
+
+  BIO_free(b);
 
   return 0;
 }
index 5f2cd3a92d3ec6a48f035e0c55d61f7acc067bb9..ed6c0667a18860bb33eb16b4f38a2f160f0d5873 100644 (file)
@@ -429,12 +429,11 @@ key_to_string(EVP_PKEY *key)
   }
 
   BIO_get_mem_ptr(b, &buf);
-  (void) BIO_set_close(b, BIO_NOCLOSE);
-  BIO_free(b);
   result = tor_malloc(buf->length + 1);
   memcpy(result, buf->data, buf->length);
   result[buf->length] = 0;
-  BUF_MEM_free(buf);
+
+  BIO_free(b);
 
   RSA_free(rsa);
   return result;