Skip to contentSkip to conversation

Check if pkcs11_cert is NULL before freeing it

PATCH In review Max Fillinger 4 participants 5 messages Nov 23, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Check if pkcs11_cert is NULL before freeing it

When running openvpn --show-tls with mbedtls, it showed a null pointer error at the end because of this.

---
 src/openvpn/ssl_mbedtls.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
src/openvpn/ssl_mbedtls.c +4 -1
index ea06cf70..aa55a1a0 100644
@@ -165,7 +165,10 @@ tls_ctx_free(struct tls_root_ctx *ctx)
         free(ctx->crl);
 
 #if defined(ENABLE_PKCS11)
-        pkcs11h_certificate_freeCertificate(ctx->pkcs11_cert);
+        if (ctx->pkcs11_cert)
+        {
+            pkcs11h_certificate_freeCertificate(ctx->pkcs11_cert);
+        }
 #endif
 
         free(ctx->allowed_ciphers);
Signature · 1 line
2.30.2
Acked-by on v1 1/1
#1 lore raw

Am 23.11.22 um 16:49 schrieb Max Fillinger:

Quoted 14 lines

When running openvpn --show-tls with mbedtls, it showed a null pointer error at the end because of this.

---
  src/openvpn/ssl_mbedtls.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
src/openvpn/ssl_mbedtls.c +0 -0
index ea06cf70..aa55a1a0 100644
@@ -165,7 +165,10 @@ tls_ctx_free(struct tls_root_ctx *ctx)
          free(ctx->crl);

src/openvpn/ssl_mbedtls.c tls_ctx_free(struct tls_root_ctx *ctx) · line 174

Quoted 10 lines
src/openvpn/ssl_mbedtls.c +4 -1
 
  #if defined(ENABLE_PKCS11)
-        pkcs11h_certificate_freeCertificate(ctx->pkcs11_cert);
+        if (ctx->pkcs11_cert)
+        {
+            pkcs11h_certificate_freeCertificate(ctx->pkcs11_cert);
+        }
  #endif
 
          free(ctx->allowed_ciphers);

Sigh, a function that violates the C paradigm that calling somethingfree on a null pointer is fine. Maybe we should add as a comment that this function is special in this way.

Arne

#2 lore raw

Hi,

Quoted 24 lines · Arne Schwabe

Am 23.11.22 um 16:49 schrieb Max Fillinger:

Quoted 23 lines

When running openvpn --show-tls with mbedtls, it showed a null pointer error at the end because of this.

---
  src/openvpn/ssl_mbedtls.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
src/openvpn/ssl_mbedtls.c +4 -1
index ea06cf70..aa55a1a0 100644
@@ -165,7 +165,10 @@ tls_ctx_free(struct tls_root_ctx *ctx)
          free(ctx->crl);
 
  #if defined(ENABLE_PKCS11)
-        pkcs11h_certificate_freeCertificate(ctx->pkcs11_cert);
+        if (ctx->pkcs11_cert)
+        {
+            pkcs11h_certificate_freeCertificate(ctx->pkcs11_cert);
+        }
  #endif
 
Quoted 6 lines

src/openvpn/ssl_mbedtls.c tls_ctx_free(struct tls_root_ctx *ctx) · line 174

Quoted 1 line
src/openvpn/ssl_mbedtls.c +0 -0
          free(ctx->allowed_ciphers);

Sigh, a function that violates the C paradigm that calling somethingfree on a null pointer is fine. Maybe we should add as a comment that this function is special in this way.

pkcs11h_certiciate_freeCertificate() does seem to handle NULL argument. With --show-tls, are we calling this before intializing the pkcs11 library? That could trigger an ASSERT.

Selva

#3 lore raw

Hi,

Quoted 3 lines · Selva Nair

pkcs11h_certiciate_freeCertificate() does seem to handle NULL argument. With --show-tls, are we calling this before intializing the pkcs11 library? That could trigger an ASSERT.

If I build on Linux with mbedtls && --enable-pkcs11 (which I normally don't do), and run --show-tls, this is what happens...

TLS-PSK-WITH-CAMELLIA-128-GCM-SHA256
TLS-PSK-WITH-CAMELLIA-128-CBC-SHA256
TLS-PSK-WITH-AES-128-CCM-8
openvpn: pkcs11h-certificate.c:1213: pkcs11h_certificate_freeCertificate: Assertion `_g_pkcs11h_data!=NULL' failed. Aborted (core dumped)

... so, the crash seems to agree with your analysis :-)

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#4 lore raw
In reply to Max Fillinger

Confirmed that it crashes without the patch, and does not crash with it.

Added a comment, as suggested.

Your patch has been applied to the master and release/2.5 branch (bugfix).

commit 19c64f16baebbce966d55c62135d1ef066f7c8c2 (master) commit 4f5e57d2c7ff9384b16c42eb9aa5af11d31f5dd1 (release/2.5)

Author: Max Fillinger
Date:   Wed Nov 23 16:49:12 2022 +0100
     Check if pkcs11_cert is NULL before freeing it

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

--
kind regards,

Gert Doering