Fix a ssl session leak due to OOM in lh_SSL_SESSION_insert
authorBernd Edlinger <bernd.edlinger@hotmail.de>
Thu, 22 Dec 2016 19:17:29 +0000 (20:17 +0100)
committerRichard Levitte <levitte@openssl.org>
Tue, 24 Jan 2017 14:05:12 +0000 (15:05 +0100)
- s == NULL can mean c is a new session *or* lh_insert was
  unable to create a hash entry.
- use lh_SSL_SESSION_retrieve to check for this error condition.
- If it happens simply remove the extra reference again.

Reviewed-by: Rich Salz <rsalz@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/2138)

ssl/ssl_sess.c

index 0ea74389c2ac7abb97cd0a31669e1b2400182395..c6d5c1247f92e791f694243416ea018346bb6447 100644 (file)
@@ -678,6 +678,15 @@ int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
          * obtain the same session from an external cache)
          */
         s = NULL;
+    } else if (s == NULL &&
+               lh_SSL_SESSION_retrieve(ctx->sessions, c) == NULL) {
+        /* s == NULL can also mean OOM error in lh_SSL_SESSION_insert ... */
+
+        /*
+         * ... so take back the extra reference and also don't add
+         * the session to the SSL_SESSION_list at this time
+         */
+        s = c;
     }
 
     /* Put at the head of the queue unless it is already in the cache */