Blame SOURCES/Fix-memory-leaks-in-soft-pkcs11-code.patch

afd354
From 604f5dcbb018fca8ea27e00314ed615133b861e1 Mon Sep 17 00:00:00 2001
afd354
From: Greg Hudson <ghudson@mit.edu>
afd354
Date: Mon, 5 Aug 2019 01:53:51 -0400
afd354
Subject: [PATCH] Fix memory leaks in soft-pkcs11 code
afd354
afd354
Fix leaks detected by asan in t_pkinit.py.  Add a helper to free a
afd354
struct st_object and free objects in C_Finalize().  Duplicate the X509
afd354
cert in add_certificate() instead of creating aliases so it can be
afd354
properly freed.  Start the session handle counter at 1 so that
afd354
C_Finalize() won't confuse the first session handle with
afd354
CK_INVALID_HANDLE (defined to 0 in pkinit.h) and will properly clean
afd354
the session object.
afd354
afd354
(cherry picked from commit 15bcaf8bcb4af25ff89820ad3bf23ad5a324e863)
afd354
(cherry picked from commit 5cc80472e7a8b0fb3002f229ffb104dccf8bd120)
afd354
---
afd354
 src/tests/softpkcs11/main.c | 44 +++++++++++++++++++++++++++++++++----
afd354
 1 file changed, 40 insertions(+), 4 deletions(-)
afd354
afd354
diff --git a/src/tests/softpkcs11/main.c b/src/tests/softpkcs11/main.c
afd354
index 2d1448ca2..a4c3ae78e 100644
afd354
--- a/src/tests/softpkcs11/main.c
afd354
+++ b/src/tests/softpkcs11/main.c
afd354
@@ -109,7 +109,7 @@ struct st_object {
afd354
         X509 *cert;
afd354
         EVP_PKEY *public_key;
afd354
         struct {
afd354
-            const char *file;
afd354
+            char *file;
afd354
             EVP_PKEY *key;
afd354
             X509 *cert;
afd354
         } private_key;
afd354
@@ -343,6 +343,26 @@ print_attributes(const CK_ATTRIBUTE *attributes,
afd354
     }
afd354
 }
afd354
 
afd354
+static void
afd354
+free_st_object(struct st_object *o)
afd354
+{
afd354
+    int i;
afd354
+
afd354
+    for (i = 0; i < o->num_attributes; i++)
afd354
+        free(o->attrs[i].attribute.pValue);
afd354
+    free(o->attrs);
afd354
+    if (o->type == STO_T_CERTIFICATE) {
afd354
+        X509_free(o->u.cert);
afd354
+    } else if (o->type == STO_T_PRIVATE_KEY) {
afd354
+        free(o->u.private_key.file);
afd354
+        EVP_PKEY_free(o->u.private_key.key);
afd354
+        X509_free(o->u.private_key.cert);
afd354
+    } else if (o->type == STO_T_PUBLIC_KEY) {
afd354
+        EVP_PKEY_free(o->u.public_key);
afd354
+    }
afd354
+    free(o);
afd354
+}
afd354
+
afd354
 static struct st_object *
afd354
 add_st_object(void)
afd354
 {
afd354
@@ -518,7 +538,11 @@ add_certificate(char *label,
afd354
         goto out;
afd354
     }
afd354
     o->type = STO_T_CERTIFICATE;
afd354
-    o->u.cert = cert;
afd354
+    o->u.cert = X509_dup(cert);
afd354
+    if (o->u.cert == NULL) {
afd354
+        ret = CKR_DEVICE_MEMORY;
afd354
+        goto out;
afd354
+    }
afd354
     public_key = X509_get_pubkey(o->u.cert);
afd354
 
afd354
     switch (EVP_PKEY_base_id(public_key)) {
afd354
@@ -602,7 +626,11 @@ add_certificate(char *label,
afd354
         o->u.private_key.file = strdup(private_key_file);
afd354
         o->u.private_key.key = NULL;
afd354
 
afd354
-        o->u.private_key.cert = cert;
afd354
+        o->u.private_key.cert = X509_dup(cert);
afd354
+        if (o->u.private_key.cert == NULL) {
afd354
+            ret = CKR_DEVICE_MEMORY;
afd354
+            goto out;
afd354
+        }
afd354
 
afd354
         c = CKO_PRIVATE_KEY;
afd354
         add_object_attribute(o, 0, CKA_CLASS, &c, sizeof(c));
afd354
@@ -676,6 +704,7 @@ add_certificate(char *label,
afd354
     free(serial_data);
afd354
     free(issuer_data);
afd354
     free(subject_data);
afd354
+    X509_free(cert);
afd354
 
afd354
     return ret;
afd354
 }
afd354
@@ -872,7 +901,7 @@ C_Initialize(CK_VOID_PTR a)
afd354
         st_logf("\tFlags\t%04x\n", (unsigned int)args->flags);
afd354
     }
afd354
 
afd354
-    soft_token.next_session_handle = 0;
afd354
+    soft_token.next_session_handle = 1;
afd354
 
afd354
     fn = get_rcfilename();
afd354
     if (fn == NULL)
afd354
@@ -886,6 +915,7 @@ CK_RV
afd354
 C_Finalize(CK_VOID_PTR args)
afd354
 {
afd354
     size_t i;
afd354
+    int j;
afd354
 
afd354
     st_logf("Finalize\n");
afd354
 
afd354
@@ -897,6 +927,12 @@ C_Finalize(CK_VOID_PTR args)
afd354
         }
afd354
     }
afd354
 
afd354
+    for (j = 0; j < soft_token.object.num_objs; j++)
afd354
+        free_st_object(soft_token.object.objs[j]);
afd354
+    free(soft_token.object.objs);
afd354
+    soft_token.object.objs = NULL;
afd354
+    soft_token.object.num_objs = 0;
afd354
+
afd354
     return CKR_OK;
afd354
 }
afd354