]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
eliminate use of CRYPTO_GET_REF in sslapitest
authorNeil Horman <nhorman@openssl.org>
Thu, 25 Jun 2026 21:42:05 +0000 (17:42 -0400)
committerNeil Horman <nhorman@openssl.org>
Sat, 4 Jul 2026 16:47:07 +0000 (12:47 -0400)
CRYPTO_GET_REF is almost by definition a TOCTOU race, and we shouldn't
use it.

As part of the effort to deprecate it, eliminate its use from
sslapitest.

Avoid the use-after-free possibility by getting a session with
SSL_get1_session (which increments the refcount) and freeing it after
we're done with it.

Reviewed-by: Kurt Roeckx <kurt@roeckx.be>
Reviewed-by: Paul Dale <paul.dale@oracle.com>
Reviewed-by: Norbert Pocs <norbertp@openssl.org>
Reviewed-by: Nikola Pajkovsky <nikolap@openssl.org>
Reviewed-by: Bob Beck <beck@openssl.org>
MergeDate: Sat Jul  4 16:47:10 2026
(Merged from https://github.com/openssl/openssl/pull/31750)

test/sslapitest.c

index 91a2181b6147eb810798a3e95992908af40dbc64..c5517016c5a7070e3f85986643986f38cfeff704 100644 (file)
@@ -10601,7 +10601,8 @@ static int test_session_cache_overflow(int idx)
     SSL *serverssl = NULL, *clientssl = NULL;
     int testresult = 0;
     SSL_SESSION *sess = NULL;
-    int references;
+
+    get_sess_val = NULL;
 
 #ifdef OSSL_NO_USABLE_TLS1_3
     /* If no TLSv1.3 available then do nothing in this case */
@@ -10672,18 +10673,9 @@ static int test_session_cache_overflow(int idx)
      * The session we just negotiated may have been already removed from the
      * internal cache - but we will return it anyway from our external cache.
      */
-    get_sess_val = SSL_get_session(serverssl);
+    get_sess_val = SSL_get1_session(serverssl);
     if (!TEST_ptr(get_sess_val))
         goto end;
-    /*
-     * Normally the session is also stored in the cache, thus we have more than
-     * one reference, but due to an out-of-memory error it can happen that this
-     * is the only reference, and in that case the SSL_free(serverssl) below
-     * would free the get_sess_val, causing a use-after-free error.
-     */
-    if (!TEST_true(CRYPTO_GET_REF(&get_sess_val->references, &references))
-        || !TEST_int_ge(references, 2))
-        goto end;
     sess = SSL_get1_session(clientssl);
     if (!TEST_ptr(sess))
         goto end;
@@ -10707,6 +10699,8 @@ static int test_session_cache_overflow(int idx)
     testresult = 1;
 
 end:
+    SSL_SESSION_free(get_sess_val);
+    get_sess_val = NULL;
     SSL_free(serverssl);
     SSL_free(clientssl);
     SSL_CTX_free(sctx);