Skip to contentSkip to conversation

xkey_pkcs11h_sign: fix dangling pointer

PATCH In review Frank Lichtenheld 3 participants 3 messages Jan 10, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xkey_pkcs11h_sign: fix dangling pointer

Warning by GCC 12:
pkcs11_openssl.c:237:22: warning:
dangling pointer ‘tbs’ to ‘enc’ may be used [-Wdangling-pointer=]

---
 src/openvpn/pkcs11_openssl.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/openvpn/pkcs11_openssl.c +3 -3
index 60bc1c47..ecf37ba0 100644
@@ -169,6 +169,9 @@ xkey_pkcs11h_sign(void *handle, unsigned char *sig,
     unsigned char buf[EVP_MAX_MD_SIZE];
     size_t buflen;
 
+    unsigned char enc[EVP_MAX_MD_SIZE + 32]; /* 32 bytes enough for DigestInfo header */
+    size_t enc_len = sizeof(enc);
+
     if (!strcmp(sigalg.op, "DigestSign"))
     {
         msg(D_XKEY, "xkey_pkcs11h_sign: computing digest");
@@ -214,9 +217,6 @@ xkey_pkcs11h_sign(void *handle, unsigned char *sig,
         {
             /* CMA_RSA_PKCS needs pkcs1 encoded digest */
 
-            unsigned char enc[EVP_MAX_MD_SIZE + 32]; /* 32 bytes enough for DigestInfo header */
-            size_t enc_len = sizeof(enc);
-
             if (!encode_pkcs1(enc, &enc_len, sigalg.mdname, tbs, tbslen))
             {
                 return 0;
Signature · 1 line
2.34.1
Acked-by on v1 1/1
#1 lore raw

Hi,

Quoted 31 lines · Frank Lichtenheld

Warning by GCC 12:
pkcs11_openssl.c:237:22: warning:
dangling pointer ‘tbs’ to ‘enc’ may be used [-Wdangling-pointer=]

---
 src/openvpn/pkcs11_openssl.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/openvpn/pkcs11_openssl.c +1 -0
index 60bc1c47..ecf37ba0 100644
@@ -169,6 +169,9 @@ xkey_pkcs11h_sign(void *handle, unsigned char *sig,
     unsigned char buf[EVP_MAX_MD_SIZE];
     size_t buflen;
 
+    unsigned char enc[EVP_MAX_MD_SIZE + 32]; /* 32 bytes enough for

DigestInfo header */

src/openvpn/pkcs11_openssl.c +2 -1
+    size_t enc_len = sizeof(enc);
+
     if (!strcmp(sigalg.op, "DigestSign"))
     {
         msg(D_XKEY, "xkey_pkcs11h_sign: computing digest");
@@ -214,9 +217,6 @@ xkey_pkcs11h_sign(void *handle, unsigned char *sig,
         {
             /* CMA_RSA_PKCS needs pkcs1 encoded digest */
 
-            unsigned char enc[EVP_MAX_MD_SIZE + 32]; /* 32 bytes enough

for DigestInfo header */

src/openvpn/pkcs11_openssl.c xkey_pkcs11h_sign(void *handle, unsigned char *sig, · line 222

Quoted 5 lines
src/openvpn/pkcs11_openssl.c +0 -2
-            size_t enc_len = sizeof(enc);
-
             if (!encode_pkcs1(enc, &enc_len, sigalg.mdname, tbs, tbslen))
             {
                 return 0;

I can't believe I wrote that nonsense in the first place. Fortunately similar code in xkey_management_sign() is okay. Hard to catch in tests as the pointer may still point to the right data after going out of scope (dangling).

Selva

#2 lore raw
In reply to Frank Lichtenheld

Haven't tested this beyond "does it compile on Github?" - it looks correct, though :-)

Your patch has been applied to the master branch.

commit 202b34da386c8574692111bad23814602d0e09f5 (master) commit 71f3a109f9f73f0d978f58e08caed896c064767f (release/2.6)

Author: Frank Lichtenheld
Date:   Tue Jan 10 14:19:47 2023 +0100
     xkey_pkcs11h_sign: fix dangling pointer

     Signed-off-by: Frank Lichtenheld <[email protected]...>
     Acked-by: Selva Nair <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg25942.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering