Commit ca834edcab for openssl.org

commit ca834edcab7cbd008ff14037b2ee04da1abb60b9
Author: Alexandr Nedvedicky <sashan@openssl.org>
Date:   Thu Nov 19 09:15:08 2026 +0100

    test: add checks for SSL_set0_{r,w}bio() ownership transfer

    Make sure SSL_set0_{r,w}bio() properly transfers ownership of BIO
    object to SSL object. The test requires ASAN build.

    Co-authored-by: Andrew Dinh <andrewd@openssl.org>
    Reviewed-by: Andrew Dinh <andrewd@openssl.org>
    Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
    Reviewed-by: Bob Beck <beck@openssl.org>
    Merge-date: Thu Nov 26 09:41:48 2026
    Merged-from: https://github.com/openssl/openssl/pull/33840

diff --git a/test/dtlsssllistenertest.c b/test/dtlsssllistenertest.c
index 75558490e6..c7e2cffd04 100644
--- a/test/dtlsssllistenertest.c
+++ b/test/dtlsssllistenertest.c
@@ -8732,6 +8732,66 @@ end:
 }
 #endif /* OPENSSL_NO_DTLS1_3 */

+/*
+ * ASAN fails for this test when transfer of bio reference ownership to DTLS
+ * ssl listener object is mishandled.
+ */
+static int test_dtls_set0_bio_leak(void)
+{
+    int testresult = 0;
+    SSL_CTX *ctx = NULL;
+    SSL *ssl = NULL;
+    BIO *bio = NULL;
+
+    ctx = SSL_CTX_new(DTLS_server_method());
+    if (!TEST_ptr(ctx))
+        goto end;
+
+    ssl = SSL_new_listener(ctx, SSL_LISTENER_FLAG_SINGLE_THREAD);
+    if (!TEST_ptr(ssl))
+        goto end;
+
+    bio = BIO_new(BIO_s_dgram_mem());
+    if (!TEST_ptr(bio))
+        goto end;
+
+    /*
+     * obtain reference to bio and pass it to SSL as reader bio
+     */
+    if (!TEST_true(BIO_up_ref(bio)))
+        goto end;
+    SSL_set0_rbio(ssl, bio);
+
+    /*
+     * obtain reference to bio and pass it to SSL as writer bio
+     */
+    if (!TEST_true(BIO_up_ref(bio)))
+        goto end;
+    SSL_set0_wbio(ssl, bio);
+
+    /*
+     * try to trigger reference leak. Setting the same bio to
+     * ssl instance must free the reference to bio. The caller
+     * to SSL_set0_{r,w}bio() always looses ownership.
+     */
+    if (!TEST_true(BIO_up_ref(bio)))
+        goto end;
+    SSL_set0_rbio(ssl, bio);
+
+    if (!TEST_true(BIO_up_ref(bio)))
+        goto end;
+    SSL_set0_wbio(ssl, bio);
+
+    testresult = 1;
+
+end:
+    BIO_free(bio);
+    SSL_free(ssl);
+    SSL_CTX_free(ctx);
+
+    return testresult;
+}
+
 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")

 int setup_tests(void)
@@ -8904,5 +8964,6 @@ int setup_tests(void)
     ADD_TEST(test_dtls12_listener_alert_after_hvr_frees_pending);
 #endif

+    ADD_TEST(test_dtls_set0_bio_leak);
     return 1;
 }
diff --git a/test/quicapitest.c b/test/quicapitest.c
index 6640ef8610..2ed0fea541 100644
--- a/test/quicapitest.c
+++ b/test/quicapitest.c
@@ -6039,6 +6039,81 @@ end:
     return testresult;
 }

+static int test_quic_set0_bio_leak(void)
+{
+    int testresult = 0;
+    unsigned int i;
+    SSL_CTX *ctx_client = NULL;
+    SSL_CTX *ctx_server = NULL;
+    SSL *ssl_c = NULL;
+    SSL *ssl_l = NULL;
+    SSL *ssl[2];
+    BIO *bio = NULL;
+
+    ctx_client = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
+    if (!TEST_ptr(ctx_client))
+        goto end;
+
+    ctx_server = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_server_method());
+    if (!TEST_ptr(ctx_server))
+        goto end;
+
+    ssl_c = SSL_new(ctx_client);
+    if (!TEST_ptr(ssl_c))
+        goto end;
+
+    ssl_l = SSL_new_listener(ctx_server, 0);
+    if (!TEST_ptr(ssl_l))
+        goto end;
+
+    bio = BIO_new(BIO_s_dgram_mem());
+    if (!TEST_ptr(bio))
+        goto end;
+
+    ssl[0] = ssl_c;
+    ssl[1] = ssl_l;
+
+    for (i = 0; i < 2; i++) {
+        /*
+         * obtain reference to bio and pass it to SSL as reader bio
+         */
+        if (!TEST_true(BIO_up_ref(bio)))
+            goto end;
+        SSL_set0_rbio(ssl[i], bio);
+
+        /*
+         * obtain reference to bio and pass it to SSL as writer bio
+         */
+        if (!TEST_true(BIO_up_ref(bio)))
+            goto end;
+        SSL_set0_wbio(ssl[i], bio);
+
+        /*
+         * try to trigger reference leak. Setting the same bio to
+         * ssl instance must free the reference to bio. The caller
+         * to SSL_set0_{r,w}bio() always looses ownership.
+         */
+        if (!TEST_true(BIO_up_ref(bio)))
+            goto end;
+        SSL_set0_rbio(ssl[i], bio);
+
+        if (!TEST_true(BIO_up_ref(bio)))
+            goto end;
+        SSL_set0_wbio(ssl[i], bio);
+    }
+
+    testresult = 1;
+
+end:
+    BIO_free(bio);
+    SSL_free(ssl_l);
+    SSL_free(ssl_c);
+    SSL_CTX_free(ctx_server);
+    SSL_CTX_free(ctx_client);
+
+    return testresult;
+}
+
 /***********************************************************************************/
 OPT_TEST_DECLARE_USAGE("provider config certsdir datadir\n")

@@ -6213,6 +6288,7 @@ int setup_tests(void)
     ADD_MFAIL_TEST(test_ssl_new_mfail);
     ADD_TEST(test_pending_limit);
     ADD_TEST(test_reject_pending_conn);
+    ADD_TEST(test_quic_set0_bio_leak);

     return 1;
 err: