On 13/02/16 22:19, Michel wrote:
> Hi,
> 
>  
> 
> I have multithreaded test programs (client and server) that I use to
> test some functionalities build with OpenSSL.
> 
> They started to warn about memory leaks when I linked them with version 1.1.
> 
> As I had to do some code changes to adapt the new version, I first
> thought I forget some [new] init/free code.
> 
> I finally used OPENSSL_cleanup() and alikes instead of the previous
> litany calls ;-), but still encounters leaks.
> 
> As it was hard to track them down, I write a simple server test program
> that wait for a client and then return without even receiving data.
> 
> No certificate are loaded.
> 
> Leaks are detected only when a client handshake with the server.
> 
>  
> 
> I might be wrong, but I do not think this is a false positive.
> 
> Could you please have a look at the informations below and share your
> feelings ?

Hmmm. It does look to me like there could be a memory leak here. What's
not clear to me is to why you are only seeing this in 1.1 and not
previous versions, as it looks like the same could happen in 1.0.2 as well!

Anyway, please try the attached patch to see if that helps.

Let me know how you get on.

Thanks

Matt

>From a47094a928f56cb62d57d4b53f2e4e20f9a0a031 Mon Sep 17 00:00:00 2001
From: Matt Caswell <m...@openssl.org>
Date: Sat, 13 Feb 2016 23:22:45 +0000
Subject: [PATCH] Fix memory leaks in tls_decrypt_ticket

Certain code paths in tls_decrypt_ticket could return early without first
freeing the HMAC_CTX or the EVP_CIPHER_CTX.
---
 ssl/t1_lib.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/ssl/t1_lib.c b/ssl/t1_lib.c
index 522f0e6..0f6d51e 100644
--- a/ssl/t1_lib.c
+++ b/ssl/t1_lib.c
@@ -3048,7 +3048,7 @@ static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
     SSL_SESSION *sess;
     unsigned char *sdec;
     const unsigned char *p;
-    int slen, mlen, renew_ticket = 0;
+    int slen, mlen, renew_ticket = 0, ret = -1;
     unsigned char tick_hmac[EVP_MAX_MD_SIZE];
     HMAC_CTX *hctx = NULL;
     EVP_CIPHER_CTX *ctx;
@@ -3061,20 +3061,28 @@ static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
     if (hctx == NULL)
         return -2;
     ctx = EVP_CIPHER_CTX_new();
+    if (ctx == NULL) {
+        ret = -2;
+        goto err;
+    }
     if (tctx->tlsext_ticket_key_cb) {
         unsigned char *nctick = (unsigned char *)etick;
         int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
                                             ctx, hctx, 0);
         if (rv < 0)
-            return -1;
-        if (rv == 0)
-            return 2;
+            goto err;
+        if (rv == 0) {
+            ret = 2;
+            goto err;
+        }
         if (rv == 2)
             renew_ticket = 1;
     } else {
         /* Check key name matches */
-        if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
-            return 2;
+        if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) {
+            ret = 2;
+            goto err;
+        }
         if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key, 16,
                          EVP_sha256(), NULL) <= 0
                 || EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL,
@@ -3148,7 +3156,7 @@ static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
 err:
     EVP_CIPHER_CTX_free(ctx);
     HMAC_CTX_free(hctx);
-    return -1;
+    return ret;
 }
 
 /* Tables to translate from NIDs to TLS v1.2 ids */
-- 
2.5.0

-- 
openssl-dev mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-dev

Reply via email to