Print this page
9642 PKCS#11 softtoken should use explicit_bzero
Reviewed by: Dan McDonald <danmcd@joyent.com>
Reviewed by: Alex Wilson <alex.wilson@joyent.com>

@@ -19,10 +19,11 @@
  * CDDL HEADER END
  */
 
 /*
  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, Joyent, Inc.
  */
 
 #include <pthread.h>
 #include <stdlib.h>
 #include <string.h>

@@ -149,14 +150,14 @@
                 soft_des_ctx->des_cbc = (void *)des_cbc_ctx_init(
                     soft_des_ctx->key_sched, soft_des_ctx->keysched_len,
                     soft_des_ctx->ivec, key_p->key_type);
 
                 if (soft_des_ctx->des_cbc == NULL) {
-                        bzero(soft_des_ctx->key_sched,
+                        freezero(soft_des_ctx->key_sched,
                             soft_des_ctx->keysched_len);
-                        free(soft_des_ctx->key_sched);
-                        free(session_p->decrypt.context);
+                        freezero(session_p->decrypt.context,
+                            sizeof (soft_des_ctx_t));
                         session_p->decrypt.context = NULL;
                         (void) pthread_mutex_unlock(&session_p->session_mutex);
                         return (CKR_HOST_MEMORY);
                 }
 

@@ -205,14 +206,14 @@
                 soft_aes_ctx->aes_cbc = (void *)aes_cbc_ctx_init(
                     soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len,
                     soft_aes_ctx->ivec);
 
                 if (soft_aes_ctx->aes_cbc == NULL) {
-                        bzero(soft_aes_ctx->key_sched,
+                        freezero(soft_aes_ctx->key_sched,
                             soft_aes_ctx->keysched_len);
-                        free(soft_aes_ctx->key_sched);
-                        free(session_p->decrypt.context);
+                        freezero(session_p->decrypt.context,
+                            sizeof (soft_aes_ctx_t));
                         session_p->decrypt.context = NULL;
                         (void) pthread_mutex_unlock(&session_p->session_mutex);
                         return (CKR_HOST_MEMORY);
                 }
 

@@ -245,14 +246,14 @@
                 soft_aes_ctx->aes_cbc = aes_ctr_ctx_init(
                     soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len,
                     pMechanism->pParameter);
 
                 if (soft_aes_ctx->aes_cbc == NULL) {
-                        bzero(soft_aes_ctx->key_sched,
+                        freezero(soft_aes_ctx->key_sched,
                             soft_aes_ctx->keysched_len);
-                        free(soft_aes_ctx->key_sched);
-                        free(session_p->decrypt.context);
+                        freezero(session_p->decrypt.context,
+                            sizeof (soft_aes_ctx_t));
                         session_p->decrypt.context = NULL;
                         rv = CKR_HOST_MEMORY;
                 }
 
                 (void) pthread_mutex_unlock(&session_p->session_mutex);

@@ -290,14 +291,15 @@
                     (void *)blowfish_cbc_ctx_init(soft_blowfish_ctx->key_sched,
                     soft_blowfish_ctx->keysched_len,
                     soft_blowfish_ctx->ivec);
 
                 if (soft_blowfish_ctx->blowfish_cbc == NULL) {
-                        bzero(soft_blowfish_ctx->key_sched,
+                        freezero(soft_blowfish_ctx->key_sched,
                             soft_blowfish_ctx->keysched_len);
-                        free(soft_blowfish_ctx->key_sched);
-                        free(session_p->decrypt.context = NULL);
+                        freezero(session_p->decrypt.context,
+                            sizeof (soft_blowfish_ctx_t));
+                        session_p->decrypt.context = NULL;
                         (void) pthread_mutex_unlock(&session_p->session_mutex);
                         return (CKR_HOST_MEMORY);
                 }
 
                 (void) pthread_mutex_unlock(&session_p->session_mutex);

@@ -552,13 +554,12 @@
                 if (soft_des_ctx->remain_len != DES_BLOCK_LEN) {
                         *pulLastPartLen = 0;
                         rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
                         /* Cleanup memory space. */
                         free(soft_des_ctx->des_cbc);
-                        bzero(soft_des_ctx->key_sched,
+                        freezero(soft_des_ctx->key_sched,
                             soft_des_ctx->keysched_len);
-                        free(soft_des_ctx->key_sched);
 
                         goto clean1;
                 }
 
                 out_len = DES_BLOCK_LEN;

@@ -606,13 +607,12 @@
                                 rv = CKR_FUNCTION_FAILED;
                         }
 
                         /* Cleanup memory space. */
                         free(soft_des_ctx->des_cbc);
-                        bzero(soft_des_ctx->key_sched,
+                        freezero(soft_des_ctx->key_sched,
                             soft_des_ctx->keysched_len);
-                        free(soft_des_ctx->key_sched);
 
                 }
 
                 break;
         }

@@ -639,12 +639,12 @@
                                 goto clean2;
                 }
 
                 /* Cleanup memory space. */
                 free(soft_des_ctx->des_cbc);
-                bzero(soft_des_ctx->key_sched, soft_des_ctx->keysched_len);
-                free(soft_des_ctx->key_sched);
+                freezero(soft_des_ctx->key_sched,
+                    soft_des_ctx->keysched_len);
 
                 break;
         }
 
         case CKM_AES_CBC_PAD:

@@ -661,13 +661,12 @@
                 if (soft_aes_ctx->remain_len != AES_BLOCK_LEN) {
                         *pulLastPartLen = 0;
                         rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
                         /* Cleanup memory space. */
                         free(soft_aes_ctx->aes_cbc);
-                        bzero(soft_aes_ctx->key_sched,
+                        freezero(soft_aes_ctx->key_sched,
                             soft_aes_ctx->keysched_len);
-                        free(soft_aes_ctx->key_sched);
 
                         goto clean1;
                 }
 
                 out_len = AES_BLOCK_LEN;

@@ -715,13 +714,12 @@
                                 rv = CKR_FUNCTION_FAILED;
                         }
 
                         /* Cleanup memory space. */
                         free(soft_aes_ctx->aes_cbc);
-                        bzero(soft_aes_ctx->key_sched,
+                        freezero(soft_aes_ctx->key_sched,
                             soft_aes_ctx->keysched_len);
-                        free(soft_aes_ctx->key_sched);
 
                 }
 
                 break;
         }

@@ -745,12 +743,12 @@
                                 goto clean2;
                 }
 
                 /* Cleanup memory space. */
                 free(soft_aes_ctx->aes_cbc);
-                bzero(soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len);
-                free(soft_aes_ctx->key_sched);
+                freezero(soft_aes_ctx->key_sched,
+                    soft_aes_ctx->keysched_len);
 
                 break;
         }
         case CKM_AES_CTR:
         {

@@ -782,12 +780,12 @@
                         goto clean1;
                 }
 
                 /* Cleanup memory space. */
                 free(ctr_ctx);
-                bzero(soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len);
-                free(soft_aes_ctx->key_sched);
+                freezero(soft_aes_ctx->key_sched,
+                    soft_aes_ctx->keysched_len);
 
                 break;
         }
         case CKM_BLOWFISH_CBC:
         {

@@ -803,21 +801,20 @@
                         if (pLastPart == NULL)
                                 goto clean2;
                 }
 
                 free(soft_blowfish_ctx->blowfish_cbc);
-                bzero(soft_blowfish_ctx->key_sched,
+                freezero(soft_blowfish_ctx->key_sched,
                     soft_blowfish_ctx->keysched_len);
-                free(soft_blowfish_ctx->key_sched);
 
                 break;
         }
 
         case CKM_RC4:
         {
                 ARCFour_key *key = (ARCFour_key *)session_p->decrypt.context;
-                bzero(key, sizeof (*key));
+                explicit_bzero(key, sizeof (*key));
                 *pulLastPartLen = 0;
                 break;
         }
 
         default: