From: Bernd Edlinger Date: Fri, 17 Nov 2023 13:47:36 +0000 (+0100) Subject: Fix a possible memory leak in ct_move_scts X-Git-Tag: openssl-3.1.5~130 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3b61584ac74bc162724962af61f0902c74de77f1;p=thirdparty%2Fopenssl.git Fix a possible memory leak in ct_move_scts Instead of trying to move the doomed sct back to the src stack, which may fail as well, simply free the sct object, as the src list will be deleted anyway. Reviewed-by: Paul Yang Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/22762) (cherry picked from commit a435d786046fabc85acdb89cbf47f154a09796e1) --- diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c index a0185cce07f..e5603b9130e 100644 --- a/ssl/ssl_lib.c +++ b/ssl/ssl_lib.c @@ -4981,6 +4981,8 @@ IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id); * If |dst| points to a NULL pointer, a new stack will be created and owned by * the caller. * Returns the number of SCTs moved, or a negative integer if an error occurs. + * The |dst| stack is created and possibly partially populated even in case + * of error, likewise the |src| stack may be left in an intermediate state. */ static int ct_move_scts(STACK_OF(SCT) **dst, STACK_OF(SCT) *src, sct_source_t origin) @@ -5000,15 +5002,14 @@ static int ct_move_scts(STACK_OF(SCT) **dst, STACK_OF(SCT) *src, if (SCT_set_source(sct, origin) != 1) goto err; - if (sk_SCT_push(*dst, sct) <= 0) + if (!sk_SCT_push(*dst, sct)) goto err; scts_moved += 1; } return scts_moved; err: - if (sct != NULL) - sk_SCT_push(src, sct); /* Put the SCT back */ + SCT_free(sct); return -1; }