From 98c2d961050603615d1d9307dac3c7ef69e207b4 Mon Sep 17 00:00:00 2001 From: Chao Zhang Date: Tue, 2 Feb 2016 01:02:31 +0000 Subject: [PATCH] SecurityPkg: AuthVariableLib: Add new cert database for volatile time based Auth variable Add a new cert data base "certdbv" to store signer certs for volatile time based Auth variable. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Chao Zhang Reviewed-by: Star Zeng Reviewed-by: Fu Siyuan git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@19786 6f19259b-4bc3-4df7-8a09-765794883524 --- .../Library/AuthVariableLib/AuthService.c | 144 ++++++++++++------ .../AuthVariableLib/AuthServiceInternal.h | 22 ++- .../Library/AuthVariableLib/AuthVariableLib.c | 32 +++- .../AuthVariableLib/AuthVariableLib.inf | 4 +- SecurityPkg/SecurityPkg.dec | 2 +- 5 files changed, 146 insertions(+), 58 deletions(-) diff --git a/SecurityPkg/Library/AuthVariableLib/AuthService.c b/SecurityPkg/Library/AuthVariableLib/AuthService.c index 1c5c2f32da..f11b86827a 100644 --- a/SecurityPkg/Library/AuthVariableLib/AuthService.c +++ b/SecurityPkg/Library/AuthVariableLib/AuthService.c @@ -18,7 +18,7 @@ They will do basic validation for authentication data structure, then call crypto library to verify the signature. -Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.
+Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -2190,7 +2190,7 @@ ProcessVariable ( IN EFI_GUID *VendorGuid, IN VOID *Data, IN UINTN DataSize, - IN UINT32 Attributes OPTIONAL + IN UINT32 Attributes ) { EFI_STATUS Status; @@ -2231,7 +2231,7 @@ ProcessVariable ( 0 ); if (!EFI_ERROR (Status) && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0)) { - Status = DeleteCertsFromDb (VariableName, VendorGuid); + Status = DeleteCertsFromDb (VariableName, VendorGuid, Attributes); } return Status; @@ -2544,9 +2544,9 @@ AuthServiceInternalCompareTimeStamp ( /** Find matching signer's certificates for common authenticated variable - by corresponding VariableName and VendorGuid from "certdb". + by corresponding VariableName and VendorGuid from "certdb" or "certdbv". - The data format of "certdb": + The data format of "certdb" or "certdbv": // // UINT32 CertDbListSize; // /// AUTH_CERT_DB_DATA Certs1[]; @@ -2557,8 +2557,8 @@ AuthServiceInternalCompareTimeStamp ( @param[in] VariableName Name of authenticated Variable. @param[in] VendorGuid Vendor GUID of authenticated Variable. - @param[in] Data Pointer to variable "certdb". - @param[in] DataSize Size of variable "certdb". + @param[in] Data Pointer to variable "certdb" or "certdbv". + @param[in] DataSize Size of variable "certdb" or "certdbv". @param[out] CertOffset Offset of matching CertData, from starting of Data. @param[out] CertDataSize Length of CertData in bytes. @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from @@ -2665,15 +2665,17 @@ FindCertsFromDb ( /** Retrieve signer's certificates for common authenticated variable - by corresponding VariableName and VendorGuid from "certdb". + by corresponding VariableName and VendorGuid from "certdb" + or "certdbv" according to authenticated variable attributes. @param[in] VariableName Name of authenticated Variable. @param[in] VendorGuid Vendor GUID of authenticated Variable. + @param[in] Attributes Attributes of authenticated variable. @param[out] CertData Pointer to signer's certificates. @param[out] CertDataSize Length of CertData in bytes. @retval EFI_INVALID_PARAMETER Any input parameter is invalid. - @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs. + @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs. @retval EFI_SUCCESS Get signer's certificates successfully. **/ @@ -2681,6 +2683,7 @@ EFI_STATUS GetCertsFromDb ( IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, + IN UINT32 Attributes, OUT UINT8 **CertData, OUT UINT32 *CertDataSize ) @@ -2689,16 +2692,30 @@ GetCertsFromDb ( UINT8 *Data; UINTN DataSize; UINT32 CertOffset; + CHAR16 *DbName; if ((VariableName == NULL) || (VendorGuid == NULL) || (CertData == NULL) || (CertDataSize == NULL)) { return EFI_INVALID_PARAMETER; } + + if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) { + // + // Get variable "certdb". + // + DbName = EFI_CERT_DB_NAME; + } else { + // + // Get variable "certdbv". + // + DbName = EFI_CERT_DB_VOLATILE_NAME; + } + // - // Get variable "certdb". + // Get variable "certdb" or "certdbv". // Status = AuthServiceInternalFindVariable ( - EFI_CERT_DB_NAME, + DbName, &gEfiCertDbGuid, (VOID **) &Data, &DataSize @@ -2733,13 +2750,15 @@ GetCertsFromDb ( /** Delete matching signer's certificates when deleting common authenticated - variable by corresponding VariableName and VendorGuid from "certdb". + variable by corresponding VariableName and VendorGuid from "certdb" or + "certdbv" according to authenticated variable attributes. @param[in] VariableName Name of authenticated Variable. @param[in] VendorGuid Vendor GUID of authenticated Variable. + @param[in] Attributes Attributes of authenticated variable. @retval EFI_INVALID_PARAMETER Any input parameter is invalid. - @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs. + @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs. @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources. @retval EFI_SUCCESS The operation is completed successfully. @@ -2747,7 +2766,8 @@ GetCertsFromDb ( EFI_STATUS DeleteCertsFromDb ( IN CHAR16 *VariableName, - IN EFI_GUID *VendorGuid + IN EFI_GUID *VendorGuid, + IN UINT32 Attributes ) { EFI_STATUS Status; @@ -2758,20 +2778,33 @@ DeleteCertsFromDb ( UINT32 CertNodeSize; UINT8 *NewCertDb; UINT32 NewCertDbSize; + CHAR16 *DbName; if ((VariableName == NULL) || (VendorGuid == NULL)) { return EFI_INVALID_PARAMETER; } - // - // Get variable "certdb". - // + if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) { + // + // Get variable "certdb". + // + DbName = EFI_CERT_DB_NAME; + VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; + } else { + // + // Get variable "certdbv". + // + DbName = EFI_CERT_DB_VOLATILE_NAME; + VarAttr = EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; + } + Status = AuthServiceInternalFindVariable ( - EFI_CERT_DB_NAME, + DbName, &gEfiCertDbGuid, (VOID **) &Data, &DataSize ); + if (EFI_ERROR (Status)) { return Status; } @@ -2783,13 +2816,13 @@ DeleteCertsFromDb ( if (DataSize == sizeof (UINT32)) { // - // There is no certs in certdb. + // There is no certs in "certdb" or "certdbv". // return EFI_SUCCESS; } // - // Get corresponding cert node from certdb. + // Get corresponding cert node from "certdb" or "certdbv". // Status = FindCertsFromDb ( VariableName, @@ -2811,7 +2844,7 @@ DeleteCertsFromDb ( } // - // Construct new data content of variable "certdb". + // Construct new data content of variable "certdb" or "certdbv". // NewCertDbSize = (UINT32) DataSize - CertNodeSize; NewCertDb = (UINT8*) mCertDbStore; @@ -2836,11 +2869,10 @@ DeleteCertsFromDb ( } // - // Set "certdb". + // Set "certdb" or "certdbv". // - VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; Status = AuthServiceInternalUpdateVariable ( - EFI_CERT_DB_NAME, + DbName, &gEfiCertDbGuid, NewCertDb, NewCertDbSize, @@ -2852,10 +2884,12 @@ DeleteCertsFromDb ( /** Insert signer's certificates for common authenticated variable with VariableName - and VendorGuid in AUTH_CERT_DB_DATA to "certdb". + and VendorGuid in AUTH_CERT_DB_DATA to "certdb" or "certdbv" according to + time based authenticated variable attributes. @param[in] VariableName Name of authenticated Variable. @param[in] VendorGuid Vendor GUID of authenticated Variable. + @param[in] Attributes Attributes of authenticated variable. @param[in] CertData Pointer to signer's certificates. @param[in] CertDataSize Length of CertData in bytes. @@ -2863,13 +2897,14 @@ DeleteCertsFromDb ( @retval EFI_ACCESS_DENIED An AUTH_CERT_DB_DATA entry with same VariableName and VendorGuid already exists. @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources. - @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb" + @retval EFI_SUCCESS Insert an AUTH_CERT_DB_DATA entry to "certdb" or "certdbv" **/ EFI_STATUS InsertCertsToDb ( IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, + IN UINT32 Attributes, IN UINT8 *CertData, IN UINTN CertDataSize ) @@ -2883,16 +2918,31 @@ InsertCertsToDb ( UINT32 CertNodeSize; UINT32 NameSize; AUTH_CERT_DB_DATA *Ptr; + CHAR16 *DbName; if ((VariableName == NULL) || (VendorGuid == NULL) || (CertData == NULL)) { return EFI_INVALID_PARAMETER; } + if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) { + // + // Get variable "certdb". + // + DbName = EFI_CERT_DB_NAME; + VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; + } else { + // + // Get variable "certdbv". + // + DbName = EFI_CERT_DB_VOLATILE_NAME; + VarAttr = EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; + } + // - // Get variable "certdb". + // Get variable "certdb" or "certdbv". // Status = AuthServiceInternalFindVariable ( - EFI_CERT_DB_NAME, + DbName, &gEfiCertDbGuid, (VOID **) &Data, &DataSize @@ -2907,7 +2957,7 @@ InsertCertsToDb ( } // - // Find whether matching cert node already exists in "certdb". + // Find whether matching cert node already exists in "certdb" or "certdbv". // If yes return error. // Status = FindCertsFromDb ( @@ -2927,7 +2977,7 @@ InsertCertsToDb ( } // - // Construct new data content of variable "certdb". + // Construct new data content of variable "certdb" or "certdbv". // NameSize = (UINT32) StrLen (VariableName); CertNodeSize = sizeof (AUTH_CERT_DB_DATA) + (UINT32) CertDataSize + NameSize * sizeof (CHAR16); @@ -2967,11 +3017,10 @@ InsertCertsToDb ( ); // - // Set "certdb". + // Set "certdb" or "certdbv". // - VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; Status = AuthServiceInternalUpdateVariable ( - EFI_CERT_DB_NAME, + DbName, &gEfiCertDbGuid, NewCertDb, NewCertDbSize, @@ -3007,9 +3056,8 @@ CleanCertsFromDb ( BOOLEAN CertCleaned; UINT8 *Data; UINTN DataSize; - UINT8 *AuthVarData; - UINTN AuthVarDataSize; EFI_GUID AuthVarGuid; + AUTH_VARIABLE_INFO AuthVariableInfo; Status = EFI_SUCCESS; @@ -3060,15 +3108,19 @@ CleanCertsFromDb ( // // Find corresponding time auth variable // - Status = AuthServiceInternalFindVariable ( - VariableName, - &AuthVarGuid, - (VOID **) &AuthVarData, - &AuthVarDataSize - ); + ZeroMem (&AuthVariableInfo, sizeof (AuthVariableInfo)); + Status = mAuthVarLibContextIn->FindVariable ( + VariableName, + &AuthVarGuid, + &AuthVariableInfo + ); if (EFI_ERROR(Status)) { - Status = DeleteCertsFromDb(VariableName, &AuthVarGuid); + Status = DeleteCertsFromDb( + VariableName, + &AuthVarGuid, + AuthVariableInfo.Attributes + ); CertCleaned = TRUE; DEBUG((EFI_D_INFO, "Recovery!! Cert for Auth Variable %s Guid %g is removed for consistency\n", VariableName, &AuthVarGuid)); FreePool(VariableName); @@ -3374,14 +3426,14 @@ VerifyTimeBasedPayload ( } // - // Get previously stored signer's certificates from certdb for existing + // Get previously stored signer's certificates from certdb or certdbv for existing // variable. Check whether they are identical with signer's certificates // in SignedData. If not, return error immediately. // if (OrgTimeStamp != NULL) { VerifyStatus = FALSE; - Status = GetCertsFromDb (VariableName, VendorGuid, &CertsInCertDb, &CertsSizeinDb); + Status = GetCertsFromDb (VariableName, VendorGuid, Attributes, &CertsInCertDb, &CertsSizeinDb); if (EFI_ERROR (Status)) { goto Exit; } @@ -3408,7 +3460,7 @@ VerifyTimeBasedPayload ( // // Insert signer's certificates when adding a new common authenticated variable. // - Status = InsertCertsToDb (VariableName, VendorGuid, SignerCerts, CertStackSize); + Status = InsertCertsToDb (VariableName, VendorGuid, Attributes, SignerCerts, CertStackSize); if (EFI_ERROR (Status)) { VerifyStatus = FALSE; goto Exit; @@ -3549,7 +3601,7 @@ VerifyTimeBasedPayloadAndUpdate ( // Delete signer's certificates when delete the common authenticated variable. // if (IsDel && AuthVarType == AuthVarTypePriv && !EFI_ERROR(Status) ) { - Status = DeleteCertsFromDb (VariableName, VendorGuid); + Status = DeleteCertsFromDb (VariableName, VendorGuid, Attributes); } if (VarDel != NULL) { diff --git a/SecurityPkg/Library/AuthVariableLib/AuthServiceInternal.h b/SecurityPkg/Library/AuthVariableLib/AuthServiceInternal.h index ec4b3d97f5..83b0f23624 100644 --- a/SecurityPkg/Library/AuthVariableLib/AuthServiceInternal.h +++ b/SecurityPkg/Library/AuthVariableLib/AuthServiceInternal.h @@ -12,7 +12,7 @@ may not be modified without authorization. If platform fails to protect these resources, the authentication service provided in this driver will be broken, and the behavior is undefined. -Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.
+Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -86,8 +86,10 @@ typedef struct { #pragma pack() /// -/// "certdb" variable stores the signer's certificates for non PK/KEK/DB/DBX -/// variables with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set. +/// "certdb" variable stores the signer's certificates for non PK/KEK/DB/DBX +/// variables with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS|EFI_VARIABLE_NON_VOLATILE set. +/// "certdbv" variable stores the signer's certificates for non PK/KEK/DB/DBX +/// variables with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set /// /// GUID: gEfiCertDbGuid /// @@ -104,7 +106,8 @@ typedef struct { /// | AUTH_CERT_DB_DATA | <-- Last CERT /// +----------------------------+ /// -#define EFI_CERT_DB_NAME L"certdb" +#define EFI_CERT_DB_NAME L"certdb" +#define EFI_CERT_DB_VOLATILE_NAME L"certdbv" #pragma pack(1) typedef struct { @@ -229,13 +232,15 @@ VerifyTimeBasedPayloadAndUpdate ( /** Delete matching signer's certificates when deleting common authenticated - variable by corresponding VariableName and VendorGuid from "certdb". + variable by corresponding VariableName and VendorGuid from "certdb" or + "certdbv" according to authenticated variable attributes. @param[in] VariableName Name of authenticated Variable. @param[in] VendorGuid Vendor GUID of authenticated Variable. + @param[in] Attributes Attributes of authenticated variable. @retval EFI_INVALID_PARAMETER Any input parameter is invalid. - @retval EFI_NOT_FOUND Fail to find "certdb" or matching certs. + @retval EFI_NOT_FOUND Fail to find "certdb"/"certdbv" or matching certs. @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources. @retval EFI_SUCCESS The operation is completed successfully. @@ -243,7 +248,8 @@ VerifyTimeBasedPayloadAndUpdate ( EFI_STATUS DeleteCertsFromDb ( IN CHAR16 *VariableName, - IN EFI_GUID *VendorGuid + IN EFI_GUID *VendorGuid, + IN UINT32 Attributes ); /** @@ -410,7 +416,7 @@ ProcessVariable ( IN EFI_GUID *VendorGuid, IN VOID *Data, IN UINTN DataSize, - IN UINT32 Attributes OPTIONAL + IN UINT32 Attributes ); /** diff --git a/SecurityPkg/Library/AuthVariableLib/AuthVariableLib.c b/SecurityPkg/Library/AuthVariableLib/AuthVariableLib.c index bf60bf54e5..6467c0695c 100644 --- a/SecurityPkg/Library/AuthVariableLib/AuthVariableLib.c +++ b/SecurityPkg/Library/AuthVariableLib/AuthVariableLib.c @@ -11,7 +11,7 @@ may not be modified without authorization. If platform fails to protect these resources, the authentication service provided in this driver will be broken, and the behavior is undefined. -Copyright (c) 2015, Intel Corporation. All rights reserved.
+Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -98,6 +98,17 @@ VARIABLE_ENTRY_PROPERTY mAuthVarEntry[] = { MAX_UINTN } }, + { + &gEfiCertDbGuid, + EFI_CERT_DB_VOLATILE_NAME, + { + VAR_CHECK_VARIABLE_PROPERTY_REVISION, + VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY, + VARIABLE_ATTRIBUTE_BS_RT_AT, + sizeof (UINT32), + MAX_UINTN + } + }, { &gEdkiiSecureBootModeGuid, L"SecureBootMode", @@ -172,8 +183,9 @@ AuthVariableLibInitialize ( // // Reserve runtime buffer for certificate database. The size excludes variable header and name size. + // Use EFI_CERT_DB_VOLATILE_NAME size since it is longer. // - mMaxCertDbSize = (UINT32) (mAuthVarLibContextIn->MaxAuthVariableSize - sizeof (EFI_CERT_DB_NAME)); + mMaxCertDbSize = (UINT32) (mAuthVarLibContextIn->MaxAuthVariableSize - sizeof (EFI_CERT_DB_VOLATILE_NAME)); mCertDbStore = AllocateRuntimePool (mMaxCertDbSize); if (mCertDbStore == NULL) { return EFI_OUT_OF_RESOURCES; @@ -288,6 +300,22 @@ AuthVariableLibInitialize ( } } + // + // Create "certdbv" variable with RT+BS+AT set. + // + VarAttr = EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; + ListSize = sizeof (UINT32); + Status = AuthServiceInternalUpdateVariable ( + EFI_CERT_DB_VOLATILE_NAME, + &gEfiCertDbGuid, + &ListSize, + sizeof (UINT32), + VarAttr + ); + if (EFI_ERROR (Status)) { + return Status; + } + // // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly. // diff --git a/SecurityPkg/Library/AuthVariableLib/AuthVariableLib.inf b/SecurityPkg/Library/AuthVariableLib/AuthVariableLib.inf index 07a3ed5419..bf2c9f244d 100644 --- a/SecurityPkg/Library/AuthVariableLib/AuthVariableLib.inf +++ b/SecurityPkg/Library/AuthVariableLib/AuthVariableLib.inf @@ -1,7 +1,7 @@ ## @file # Provides authenticated variable services. # -# Copyright (c) 2015, Intel Corporation. All rights reserved.
+# Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.
# # This program and the accompanying materials # are licensed and made available under the terms and conditions @@ -74,7 +74,9 @@ gEfiCustomModeEnableGuid ## CONSUMES ## Variable:L"certdb" + ## CONSUMES ## Variable:L"certdbv" ## PRODUCES ## Variable:L"certdb" + ## PRODUCES ## Variable:L"certdbv" gEfiCertDbGuid ## CONSUMES ## Variable:L"VendorKeysNv" diff --git a/SecurityPkg/SecurityPkg.dec b/SecurityPkg/SecurityPkg.dec index b5f4eb75f2..b432c4459e 100644 --- a/SecurityPkg/SecurityPkg.dec +++ b/SecurityPkg/SecurityPkg.dec @@ -106,7 +106,7 @@ # Include/Guid/AuthenticatedVariableFormat.h gEfiVendorKeysNvGuid = { 0x9073e4e0, 0x60ec, 0x4b6e, { 0x99, 0x3, 0x4c, 0x22, 0x3c, 0x26, 0xf, 0x3c } } - ## GUID used to "certdb" variable to store the signer's certificates for common variables with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute. + ## GUID used to "certdb"/"certdbv" variable to store the signer's certificates for common variables with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute. # Include/Guid/AuthenticatedVariableFormat.h gEfiCertDbGuid = { 0xd9bee56e, 0x75dc, 0x49d9, { 0xb4, 0xd7, 0xb5, 0x34, 0x21, 0xf, 0x63, 0x7a } } -- 2.39.2