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

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