From ee05588dabeac7b9d034bf16dad122a93d1688a4 Mon Sep 17 00:00:00 2001 From: Matt Caswell Date: Tue, 25 Oct 2022 16:29:43 +0100 Subject: [PATCH] Check whether buffers have actually been allocated/freed In the sslbuffertest we test the operation of SSL_alloc_buffers() and SSL_free_buffers(). However this was done entirely using the public API, and did not confirm that the buffers were actually allocated/freed. We now extend the test to confirm this. Reviewed-by: Tomas Mraz Reviewed-by: Hugo Landau (Merged from https://github.com/openssl/openssl/pull/19472) --- test/sslbuffertest.c | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/test/sslbuffertest.c b/test/sslbuffertest.c index 3c3e69d61da..6b6bf19292b 100644 --- a/test/sslbuffertest.c +++ b/test/sslbuffertest.c @@ -13,6 +13,12 @@ #include #include +/* We include internal headers so we can check if the buffers are allocated */ +#include "../ssl/ssl_local.h" +#include "../ssl/record/record_local.h" +#include "../ssl/record/recordmethod.h" +#include "../ssl/record/methods/recmethod_local.h" + #include "internal/packet.h" #include "helpers/ssltestlib.h" @@ -28,6 +34,17 @@ static SSL_CTX *clientctx = NULL; #define MAX_ATTEMPTS 100 +static int checkbuffers(SSL *s, int isalloced) +{ + SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); + OSSL_RECORD_LAYER *rrl = sc->rlayer.rrl; + OSSL_RECORD_LAYER *wrl = sc->rlayer.wrl; + + if (isalloced) + return rrl->rbuf.buf != NULL && wrl->wbuf[0].buf != NULL; + + return rrl->rbuf.buf == NULL && wrl->wbuf[0].buf == NULL; +} /* * There are 9 passes in the tests @@ -78,14 +95,18 @@ static int test_func(int test) for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2; i++) { /* test == 0 mean to free/allocate = control */ - if (test >= 1 && !TEST_true(SSL_free_buffers(clientssl))) + if (test >= 1 && (!TEST_true(SSL_free_buffers(clientssl)) + || !TEST_true(checkbuffers(clientssl, 0)))) goto end; - if (test >= 2 && !TEST_true(SSL_alloc_buffers(clientssl))) + if (test >= 2 && (!TEST_true(SSL_alloc_buffers(clientssl)) + || !TEST_true(checkbuffers(clientssl, 1)))) goto end; /* allocate a second time */ - if (test >= 3 && !TEST_true(SSL_alloc_buffers(clientssl))) + if (test >= 3 && (!TEST_true(SSL_alloc_buffers(clientssl)) + || !TEST_true(checkbuffers(clientssl, 1)))) goto end; - if (test >= 4 && !TEST_true(SSL_free_buffers(clientssl))) + if (test >= 4 && (!TEST_true(SSL_free_buffers(clientssl)) + || !TEST_true(checkbuffers(clientssl, 0)))) goto end; ret = SSL_write(clientssl, testdata + len, @@ -112,14 +133,18 @@ static int test_func(int test) for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < MAX_ATTEMPTS; i++) { - if (test >= 5 && !TEST_true(SSL_free_buffers(serverssl))) + if (test >= 5 && (!TEST_true(SSL_free_buffers(serverssl)) + || !TEST_true(checkbuffers(serverssl, 0)))) goto end; /* free a second time */ - if (test >= 6 && !TEST_true(SSL_free_buffers(serverssl))) + if (test >= 6 && (!TEST_true(SSL_free_buffers(serverssl)) + || !TEST_true(checkbuffers(serverssl, 0)))) goto end; - if (test >= 7 && !TEST_true(SSL_alloc_buffers(serverssl))) + if (test >= 7 && (!TEST_true(SSL_alloc_buffers(serverssl)) + || !TEST_true(checkbuffers(serverssl, 1)))) goto end; - if (test >= 8 && !TEST_true(SSL_free_buffers(serverssl))) + if (test >= 8 && (!TEST_true(SSL_free_buffers(serverssl)) + || !TEST_true(checkbuffers(serverssl, 0)))) goto end; ret = SSL_read(serverssl, buf + len, sizeof(buf) - len); -- 2.47.3