Skip to contentSkip to conversation

Fix use-after-free with EVP_CIPHER_free

PATCH In review Arne Schwabe 3 participants 3 messages Jun 1, 2023 – Jun 3, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix use-after-free with EVP_CIPHER_free

In many scenerios the context will still have a reference to the cipher, so this use-after-free does not explode but it is still wrong.

Change-Id I59002d6613eaef36d5a47b20b56073e399cfa1df
---
 src/openvpn/crypto_openssl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/openvpn/crypto_openssl.c +2 -1
index c2ac80b74..8fe56fc78 100644
@@ -839,11 +839,12 @@ cipher_ctx_init(EVP_CIPHER_CTX *ctx, const uint8_t *key,
         crypto_msg(M_FATAL, "EVP cipher init #2");
     }
 
-    EVP_CIPHER_free(kt);
     /* make sure we used a big enough key */
     ASSERT(EVP_CIPHER_CTX_key_length(ctx) <= EVP_CIPHER_key_length(kt));
+    EVP_CIPHER_free(kt);
 }
 
+
 int
 cipher_ctx_iv_length(const EVP_CIPHER_CTX *ctx)
 {
Signature · 1 line
2.39.2 (Apple Git-143)
Acked-by on v1 1/1
#1 lore raw

Hi,

Quoted 1 line · Arne Schwabe

In many scenerios the context will still have a reference to the cipher, so

scenerios -> scenarios

Quoted 1 line

this use-after-free does not explode but it is still wrong.

Good catch - glad we're so lucky :-)

Quoted 12 lines
Change-Id I59002d6613eaef36d5a47b20b56073e399cfa1df
---
  src/openvpn/crypto_openssl.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
src/openvpn/crypto_openssl.c +0 -0
index c2ac80b74..8fe56fc78 100644

src/openvpn/crypto_openssl.c cipher_ctx_init(EVP_CIPHER_CTX *ctx, const uint8_t *key, · line 847

Quoted 10 lines
src/openvpn/crypto_openssl.c +2 -1
@@ -839,11 +839,12 @@ cipher_ctx_init(EVP_CIPHER_CTX *ctx, const uint8_t *key,
          crypto_msg(M_FATAL, "EVP cipher init #2");
      }
 
-    EVP_CIPHER_free(kt);
      /* make sure we used a big enough key */
      ASSERT(EVP_CIPHER_CTX_key_length(ctx) <= EVP_CIPHER_key_length(kt));
+    EVP_CIPHER_free(kt);
  }
 
+

This is not required - please remove it before merging.

src/openvpn/crypto_openssl.c cipher_ctx_init(EVP_CIPHER_CTX *ctx, const uint8_t *key, · line 850

Quoted 3 lines
src/openvpn/crypto_openssl.c +0 -0
  int
  cipher_ctx_iv_length(const EVP_CIPHER_CTX *ctx)
  {
Signature · 1 line
Antonio Quartulli
#2 lore raw
In reply to Arne Schwabe

I have not tested this beyond a simple "does it compile" (it does) - and the fix is quite "obviously correct" - good find.

I have also removed the spurious extra blank line on merge.

Your patch has been applied to the master and release/2.6 branch (relevant code is not part of 2.5).

commit 13f5e615310ea64ab69f521e622a10f2d0ad3f4e (master) commit 205c66bd0ed2661c47b9fe7317089fbb09cc7aa4 (release/2.6)

Author: Arne Schwabe
Date:   Thu Jun 1 11:57:21 2023 +0200
     Fix use-after-free with EVP_CIPHER_free

     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg26735.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering