Fix a possible memory leak in custom_ext_add
authorBernd Edlinger <bernd.edlinger@hotmail.de>
Mon, 6 Nov 2023 10:03:05 +0000 (11:03 +0100)
committerHugo Landau <hlandau@openssl.org>
Wed, 8 Nov 2023 08:20:06 +0000 (08:20 +0000)
Reviewed-by: Hugo Landau <hlandau@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22638)

ssl/statem/extensions_cust.c

index 8b296f1f59d286ee681d1042d5794ab4c4cf123d..7c049d29707257550a51de9c2ef531735bd8a26a 100644 (file)
@@ -225,6 +225,9 @@ int custom_ext_add(SSL_CONNECTION *s, int context, WPACKET *pkt, X509 *x,
                 || !WPACKET_start_sub_packet_u16(pkt)
                 || (outlen > 0 && !WPACKET_memcpy(pkt, out, outlen))
                 || !WPACKET_close(pkt)) {
+            if (meth->free_cb != NULL)
+                meth->free_cb(SSL_CONNECTION_GET_SSL(s), meth->ext_type, context,
+                              out, meth->add_arg);
             if (!for_comp)
                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
             return 0;
@@ -234,6 +237,9 @@ int custom_ext_add(SSL_CONNECTION *s, int context, WPACKET *pkt, X509 *x,
              * We can't send duplicates: code logic should prevent this.
              */
             if (!ossl_assert((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0)) {
+                if (meth->free_cb != NULL)
+                    meth->free_cb(SSL_CONNECTION_GET_SSL(s), meth->ext_type,
+                                  context, out, meth->add_arg);
                 if (!for_comp)
                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
                 return 0;