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>


   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  * Copyright 2012 Milan Jurik. All rights reserved.

  25  */
  26 
  27 #include <string.h>
  28 #include <stdlib.h>
  29 #include <strings.h>
  30 #include "metaGlobal.h"
  31 #include "metaAttrMasters.h"
  32 
  33 static void
  34 find_attribute(CK_ATTRIBUTE_TYPE attrtype, generic_attr_t *attributes,
  35         size_t num_attributes, generic_attr_t **found_attribute);
  36 
  37 /*
  38  * get_master_attributes_by_object
  39  *
  40  * Returns an (statically allocated) set of object attributes, as determined by
  41  * class and keytype of the supplied object.  The attributes are only
  42  * initialized to default values.
  43  */
  44 CK_RV


 509  * dealloc_attributes
 510  *
 511  * Deallocates the storage used for a set of attributes. The attribute
 512  * values are zeroed out before being free'd.
 513  */
 514 void
 515 dealloc_attributes(generic_attr_t *attributes, size_t num_attributes)
 516 {
 517         size_t i;
 518         generic_attr_t *attr;
 519 
 520         for (i = 0; i < num_attributes; i++) {
 521                 attr = attributes + i;
 522 
 523                 /*
 524                  * Zero-out any attribute values. We could do this just for
 525                  * attributes with isSensitive == True, but it's not much
 526                  * extra work to just do them all. [Most attributes are just
 527                  * 1 or 4 bytes]
 528                  */
 529                 bzero(attr->attribute.pValue, attr->attribute.ulValueLen);

 530 
 531                 if (attr->isMalloced)
 532                         free(attr->attribute.pValue);
 533         }
 534 
 535         free(attributes);
 536 }
 537 
 538 
 539 /*
 540  * attribute_set_value
 541  *
 542  * Sets the value of the specified attribute. Any portion of the old value
 543  * which will not be overwritten by the new value is zeroed out.
 544  */
 545 CK_RV
 546 attribute_set_value(CK_ATTRIBUTE *new_attr,
 547         generic_attr_t *attributes, size_t num_attributes)
 548 {
 549         generic_attr_t *attr = NULL;
 550 
 551         if (new_attr == NULL)
 552                 return (CKR_TEMPLATE_INCOMPLETE);
 553         else if (new_attr->pValue == NULL) {
 554                 return (CKR_ATTRIBUTE_VALUE_INVALID);
 555         }
 556 
 557         find_attribute(new_attr->type, attributes, num_attributes, &attr);
 558         if (attr == NULL) {
 559                 return (CKR_ATTRIBUTE_TYPE_INVALID);
 560         }
 561 
 562         /* Store the new value. */
 563         if (attr->attribute.ulValueLen >= new_attr->ulValueLen) {
 564                 /* Existing storage is sufficient to store new value. */
 565 
 566                 /* bzero() out any data that won't be overwritten. */
 567                 bzero((char *)attr->attribute.pValue + new_attr->ulValueLen,

 568                     attr->attribute.ulValueLen - new_attr->ulValueLen);
 569 
 570         } else if (new_attr->ulValueLen <= sizeof (attr->generic_data)) {
 571                 /* Use generic storage to avoid a malloc. */
 572 
 573                 bzero(attr->attribute.pValue, attr->attribute.ulValueLen);

 574                 if (attr->isMalloced) {
 575                         /*
 576                          * If app sets a large value (triggering a malloc),
 577                          * then sets a tiny value, and finally again sets
 578                          * a large value (phew!) we could end up here.
 579                          *
 580                          * FUTURE?: Store the original malloc size, so that
 581                          * we can regrow the value up to the original size.
 582                          * This might avoid some heap churn for pathalogic
 583                          * applications.
 584                          */
 585                         free(attr->attribute.pValue);
 586                         attr->isMalloced = B_FALSE;
 587                 }
 588 
 589                 attr->attribute.pValue = attr->generic_data;
 590 
 591         } else {
 592                 /* Need to allocate storage for the new value. */
 593                 void *newStorage;




   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  * Copyright 2012 Milan Jurik. All rights reserved.
  25  * Copyright (c) 2018, Joyent, Inc.
  26  */
  27 
  28 #include <string.h>
  29 #include <stdlib.h>
  30 #include <strings.h>
  31 #include "metaGlobal.h"
  32 #include "metaAttrMasters.h"
  33 
  34 static void
  35 find_attribute(CK_ATTRIBUTE_TYPE attrtype, generic_attr_t *attributes,
  36         size_t num_attributes, generic_attr_t **found_attribute);
  37 
  38 /*
  39  * get_master_attributes_by_object
  40  *
  41  * Returns an (statically allocated) set of object attributes, as determined by
  42  * class and keytype of the supplied object.  The attributes are only
  43  * initialized to default values.
  44  */
  45 CK_RV


 510  * dealloc_attributes
 511  *
 512  * Deallocates the storage used for a set of attributes. The attribute
 513  * values are zeroed out before being free'd.
 514  */
 515 void
 516 dealloc_attributes(generic_attr_t *attributes, size_t num_attributes)
 517 {
 518         size_t i;
 519         generic_attr_t *attr;
 520 
 521         for (i = 0; i < num_attributes; i++) {
 522                 attr = attributes + i;
 523 
 524                 /*
 525                  * Zero-out any attribute values. We could do this just for
 526                  * attributes with isSensitive == True, but it's not much
 527                  * extra work to just do them all. [Most attributes are just
 528                  * 1 or 4 bytes]
 529                  */
 530                 explicit_bzero(attr->attribute.pValue,
 531                     attr->attribute.ulValueLen);
 532 
 533                 if (attr->isMalloced)
 534                         free(attr->attribute.pValue);
 535         }
 536 
 537         free(attributes);
 538 }
 539 
 540 
 541 /*
 542  * attribute_set_value
 543  *
 544  * Sets the value of the specified attribute. Any portion of the old value
 545  * which will not be overwritten by the new value is zeroed out.
 546  */
 547 CK_RV
 548 attribute_set_value(CK_ATTRIBUTE *new_attr,
 549         generic_attr_t *attributes, size_t num_attributes)
 550 {
 551         generic_attr_t *attr = NULL;
 552 
 553         if (new_attr == NULL)
 554                 return (CKR_TEMPLATE_INCOMPLETE);
 555         else if (new_attr->pValue == NULL) {
 556                 return (CKR_ATTRIBUTE_VALUE_INVALID);
 557         }
 558 
 559         find_attribute(new_attr->type, attributes, num_attributes, &attr);
 560         if (attr == NULL) {
 561                 return (CKR_ATTRIBUTE_TYPE_INVALID);
 562         }
 563 
 564         /* Store the new value. */
 565         if (attr->attribute.ulValueLen >= new_attr->ulValueLen) {
 566                 /* Existing storage is sufficient to store new value. */
 567 
 568                 /* bzero() out any data that won't be overwritten. */
 569                 explicit_bzero((char *)attr->attribute.pValue +
 570                     new_attr->ulValueLen,
 571                     attr->attribute.ulValueLen - new_attr->ulValueLen);
 572 
 573         } else if (new_attr->ulValueLen <= sizeof (attr->generic_data)) {
 574                 /* Use generic storage to avoid a malloc. */
 575 
 576                 explicit_bzero(attr->attribute.pValue,
 577                     attr->attribute.ulValueLen);
 578                 if (attr->isMalloced) {
 579                         /*
 580                          * If app sets a large value (triggering a malloc),
 581                          * then sets a tiny value, and finally again sets
 582                          * a large value (phew!) we could end up here.
 583                          *
 584                          * FUTURE?: Store the original malloc size, so that
 585                          * we can regrow the value up to the original size.
 586                          * This might avoid some heap churn for pathalogic
 587                          * applications.
 588                          */
 589                         free(attr->attribute.pValue);
 590                         attr->isMalloced = B_FALSE;
 591                 }
 592 
 593                 attr->attribute.pValue = attr->generic_data;
 594 
 595         } else {
 596                 /* Need to allocate storage for the new value. */
 597                 void *newStorage;