X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;ds=sidebyside;f=SecurityPkg%2FVariableAuthenticated%2FRuntimeDxe%2FAuthService.c;h=96b1f403c363f0005431a1a7f9404986022c783c;hb=4ccef56102cc104ad0bc881f5312f84fb4e569ef;hp=bb625ff2c0ca8bf2a25e42f079ca40a0b36178c6;hpb=2d3fb919872aaf67b8246fadc7bd20aa5d80dd43;p=mirror_edk2.git diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/AuthService.c b/SecurityPkg/VariableAuthenticated/RuntimeDxe/AuthService.c index bb625ff2c0..96b1f403c3 100644 --- a/SecurityPkg/VariableAuthenticated/RuntimeDxe/AuthService.c +++ b/SecurityPkg/VariableAuthenticated/RuntimeDxe/AuthService.c @@ -2,7 +2,20 @@ Implement authentication services for the authenticated variable service in UEFI2.2. -Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.
+ Caution: This module requires additional review when modified. + This driver will have external input - variable data. It may be input in SMM mode. + This external input must be validated carefully to avoid security issue like + buffer overflow, integer overflow. + Variable attribute should also be checked to avoid authentication bypass. + + ProcessVarWithPk(), ProcessVarWithKek() and ProcessVariable() are the function to do + variable authentication. + + VerifyTimeBasedPayload() and VerifyCounterBasedPayload() are sub function to do verification. + They will do basic validation for authentication data structure, then call crypto library + to verify the signature. + +Copyright (c) 2009 - 2014, 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 @@ -19,10 +32,16 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. /// /// Global database array for scratch /// -UINT8 mPubKeyStore[MAX_KEYDB_SIZE]; +UINT8 *mPubKeyStore; UINT32 mPubKeyNumber; +UINT32 mMaxKeyNumber; +UINT32 mMaxKeyDbSize; +UINT8 *mCertDbStore; +UINT32 mMaxCertDbSize; UINT32 mPlatformMode; -EFI_GUID mSignatureSupport[SIGSUPPORT_NUM] = {EFI_CERT_RSA2048_SHA256_GUID, EFI_CERT_RSA2048_SHA1_GUID}; +UINT8 mVendorKeyState; + +EFI_GUID mSignatureSupport[] = {EFI_CERT_SHA1_GUID, EFI_CERT_SHA256_GUID, EFI_CERT_RSA2048_GUID, EFI_CERT_X509_GUID}; // // Public Exponent of RSA Key. // @@ -32,14 +51,6 @@ CONST UINT8 mRsaE[] = { 0x01, 0x00, 0x01 }; // VOID *mHashCtx = NULL; -// -// Pointer to runtime buffer. -// For "Append" operation to an existing variable, a read/modify/write operation -// is supported by firmware internally. Reserve runtime buffer to cache previous -// variable data in runtime phase because memory allocation is forbidden in virtual mode. -// -VOID *mStorageArea = NULL; - // // The serialization of the values of the VariableName, VendorGuid and Attributes // parameters of the SetVariable() call and the TimeStamp component of the @@ -48,6 +59,70 @@ VOID *mStorageArea = NULL; // UINT8 *mSerializationRuntimeBuffer = NULL; +// +// Requirement for different signature type which have been defined in UEFI spec. +// These data are used to peform SignatureList format check while setting PK/KEK variable. +// +EFI_SIGNATURE_ITEM mSupportSigItem[] = { +//{SigType, SigHeaderSize, SigDataSize } + {EFI_CERT_SHA256_GUID, 0, 32 }, + {EFI_CERT_RSA2048_GUID, 0, 256 }, + {EFI_CERT_RSA2048_SHA256_GUID, 0, 256 }, + {EFI_CERT_SHA1_GUID, 0, 20 }, + {EFI_CERT_RSA2048_SHA1_GUID, 0, 256 }, + {EFI_CERT_X509_GUID, 0, ((UINT32) ~0)}, + {EFI_CERT_SHA224_GUID, 0, 28 }, + {EFI_CERT_SHA384_GUID, 0, 48 }, + {EFI_CERT_SHA512_GUID, 0, 64 } +}; + +/** + Determine whether this operation needs a physical present user. + + @param[in] VariableName Name of the Variable. + @param[in] VendorGuid GUID of the Variable. + + @retval TRUE This variable is protected, only a physical present user could set this variable. + @retval FALSE This variable is not protected. + +**/ +BOOLEAN +NeedPhysicallyPresent( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid + ) +{ + if ((CompareGuid (VendorGuid, &gEfiSecureBootEnableDisableGuid) && (StrCmp (VariableName, EFI_SECURE_BOOT_ENABLE_NAME) == 0)) + || (CompareGuid (VendorGuid, &gEfiCustomModeEnableGuid) && (StrCmp (VariableName, EFI_CUSTOM_MODE_NAME) == 0))) { + return TRUE; + } + + return FALSE; +} + +/** + Determine whether the platform is operating in Custom Secure Boot mode. + + @retval TRUE The platform is operating in Custom mode. + @retval FALSE The platform is operating in Standard mode. + +**/ +BOOLEAN +InCustomMode ( + VOID + ) +{ + VARIABLE_POINTER_TRACK Variable; + + FindVariable (EFI_CUSTOM_MODE_NAME, &gEfiCustomModeEnableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE); + if (Variable.CurrPtr != NULL && *(GetVariableDataPtr (Variable.CurrPtr)) == CUSTOM_SECURE_BOOT_MODE) { + return TRUE; + } + + return FALSE; +} + + /** Internal function to delete a Variable given its name and GUID, no authentication required. @@ -68,7 +143,7 @@ DeleteVariable ( EFI_STATUS Status; VARIABLE_POINTER_TRACK Variable; - Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal); + Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE); if (EFI_ERROR (Status)) { return EFI_SUCCESS; } @@ -91,6 +166,7 @@ AutenticatedVariableServiceInitialize ( { EFI_STATUS Status; VARIABLE_POINTER_TRACK Variable; + VARIABLE_POINTER_TRACK PkVariable; UINT8 VarValue; UINT32 VarAttr; UINT8 *Data; @@ -98,6 +174,8 @@ AutenticatedVariableServiceInitialize ( UINTN CtxSize; UINT8 SecureBootMode; UINT8 SecureBootEnable; + UINT8 CustomMode; + UINT32 ListSize; // // Initialize hash context. @@ -109,10 +187,21 @@ AutenticatedVariableServiceInitialize ( } // - // Reserved runtime buffer for "Append" operation in virtual mode. + // Reserve runtime buffer for public key database. The size excludes variable header and name size. + // + mMaxKeyDbSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER) - sizeof (AUTHVAR_KEYDB_NAME); + mMaxKeyNumber = mMaxKeyDbSize / EFI_CERT_TYPE_RSA2048_SIZE; + mPubKeyStore = AllocateRuntimePool (mMaxKeyDbSize); + if (mPubKeyStore == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Reserve runtime buffer for certificate database. The size excludes variable header and name size. // - mStorageArea = AllocateRuntimePool (PcdGet32 (PcdMaxVariableSize)); - if (mStorageArea == NULL) { + mMaxCertDbSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER) - sizeof (EFI_CERT_DB_NAME); + mCertDbStore = AllocateRuntimePool (mMaxCertDbSize); + if (mCertDbStore == NULL) { return EFI_OUT_OF_RESOURCES; } @@ -133,7 +222,8 @@ AutenticatedVariableServiceInitialize ( AUTHVAR_KEYDB_NAME, &gEfiAuthenticatedVariableGuid, &Variable, - &mVariableModuleGlobal->VariableGlobal + &mVariableModuleGlobal->VariableGlobal, + FALSE ); if (Variable.CurrPtr == NULL) { @@ -164,73 +254,55 @@ AutenticatedVariableServiceInitialize ( CopyMem (mPubKeyStore, (UINT8 *) Data, DataSize); mPubKeyNumber = (UINT32) (DataSize / EFI_CERT_TYPE_RSA2048_SIZE); } + + FindVariable (EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid, &PkVariable, &mVariableModuleGlobal->VariableGlobal, FALSE); + if (PkVariable.CurrPtr == NULL) { + DEBUG ((EFI_D_INFO, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME)); + } else { + DEBUG ((EFI_D_INFO, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME)); + } + // - // Check "SetupMode" variable's existence. - // If it doesn't exist, check PK database's existence to determine the value. - // Then create a new one with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set. + // Create "SetupMode" variable with BS+RT attribute set. // - Status = FindVariable ( + FindVariable (EFI_SETUP_MODE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE); + if (PkVariable.CurrPtr == NULL) { + mPlatformMode = SETUP_MODE; + } else { + mPlatformMode = USER_MODE; + } + Status = UpdateVariable ( EFI_SETUP_MODE_NAME, &gEfiGlobalVariableGuid, + &mPlatformMode, + sizeof(UINT8), + EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, + 0, + 0, &Variable, - &mVariableModuleGlobal->VariableGlobal + NULL ); - - if (Variable.CurrPtr == NULL) { - Status = FindVariable ( - EFI_PLATFORM_KEY_NAME, - &gEfiGlobalVariableGuid, - &Variable, - &mVariableModuleGlobal->VariableGlobal - ); - if (Variable.CurrPtr == NULL) { - mPlatformMode = SETUP_MODE; - } else { - mPlatformMode = USER_MODE; - } - - VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS; - Status = UpdateVariable ( - EFI_SETUP_MODE_NAME, - &gEfiGlobalVariableGuid, - &mPlatformMode, - sizeof(UINT8), - VarAttr, - 0, - 0, - &Variable, - NULL - ); - if (EFI_ERROR (Status)) { - return Status; - } - } else { - mPlatformMode = *(GetVariableDataPtr (Variable.CurrPtr)); + if (EFI_ERROR (Status)) { + return Status; } + // - // Check "SignatureSupport" variable's existence. - // If it doesn't exist, then create a new one with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set. + // Create "SignatureSupport" variable with BS+RT attribute set. // - Status = FindVariable ( - EFI_SIGNATURE_SUPPORT_NAME, - &gEfiGlobalVariableGuid, - &Variable, - &mVariableModuleGlobal->VariableGlobal - ); - - if (Variable.CurrPtr == NULL) { - VarAttr = EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS; - Status = UpdateVariable ( - EFI_SIGNATURE_SUPPORT_NAME, - &gEfiGlobalVariableGuid, - mSignatureSupport, - SIGSUPPORT_NUM * sizeof(EFI_GUID), - VarAttr, - 0, - 0, - &Variable, - NULL - ); + FindVariable (EFI_SIGNATURE_SUPPORT_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE); + Status = UpdateVariable ( + EFI_SIGNATURE_SUPPORT_NAME, + &gEfiGlobalVariableGuid, + mSignatureSupport, + sizeof(mSignatureSupport), + EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, + 0, + 0, + &Variable, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; } // @@ -238,15 +310,15 @@ AutenticatedVariableServiceInitialize ( // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE. // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE. // - SecureBootEnable = SECURE_BOOT_MODE_DISABLE; - FindVariable (EFI_SECURE_BOOT_ENABLE_NAME, &gEfiSecureBootEnableDisableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal); + SecureBootEnable = SECURE_BOOT_DISABLE; + FindVariable (EFI_SECURE_BOOT_ENABLE_NAME, &gEfiSecureBootEnableDisableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE); if (Variable.CurrPtr != NULL) { SecureBootEnable = *(GetVariableDataPtr (Variable.CurrPtr)); } else if (mPlatformMode == USER_MODE) { // // "SecureBootEnable" not exist, initialize it in USER_MODE. // - SecureBootEnable = SECURE_BOOT_MODE_ENABLE; + SecureBootEnable = SECURE_BOOT_ENABLE; Status = UpdateVariable ( EFI_SECURE_BOOT_ENABLE_NAME, &gEfiSecureBootEnableDisableGuid, @@ -263,18 +335,21 @@ AutenticatedVariableServiceInitialize ( } } + // + // Create "SecureBoot" variable with BS+RT attribute set. + // if (SecureBootEnable == SECURE_BOOT_ENABLE && mPlatformMode == USER_MODE) { SecureBootMode = SECURE_BOOT_MODE_ENABLE; } else { SecureBootMode = SECURE_BOOT_MODE_DISABLE; } - FindVariable (EFI_SECURE_BOOT_MODE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal); + FindVariable (EFI_SECURE_BOOT_MODE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE); Status = UpdateVariable ( EFI_SECURE_BOOT_MODE_NAME, &gEfiGlobalVariableGuid, &SecureBootMode, sizeof (UINT8), - EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, + EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS, 0, 0, &Variable, @@ -284,33 +359,112 @@ AutenticatedVariableServiceInitialize ( return Status; } + DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_SETUP_MODE_NAME, mPlatformMode)); + DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME, SecureBootMode)); + DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME, SecureBootEnable)); + // - // Detect whether a secure platform-specific method to clear PK(Platform Key) - // is configured by platform owner. This method is provided for users force to clear PK - // in case incorrect enrollment mis-haps. + // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state. // - if (ForceClearPK ()) { - // - // 1. Clear PK. - // - Status = DeleteVariable (EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid); + FindVariable (EFI_CUSTOM_MODE_NAME, &gEfiCustomModeEnableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE); + CustomMode = STANDARD_SECURE_BOOT_MODE; + Status = UpdateVariable ( + EFI_CUSTOM_MODE_NAME, + &gEfiCustomModeEnableGuid, + &CustomMode, + sizeof (UINT8), + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS, + 0, + 0, + &Variable, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME, CustomMode)); + + // + // Check "certdb" variable's existence. + // If it doesn't exist, then create a new one with + // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set. + // + Status = FindVariable ( + EFI_CERT_DB_NAME, + &gEfiCertDbGuid, + &Variable, + &mVariableModuleGlobal->VariableGlobal, + FALSE + ); + + if (Variable.CurrPtr == NULL) { + VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; + ListSize = sizeof (UINT32); + Status = UpdateVariable ( + EFI_CERT_DB_NAME, + &gEfiCertDbGuid, + &ListSize, + sizeof (UINT32), + VarAttr, + 0, + 0, + &Variable, + NULL + ); if (EFI_ERROR (Status)) { return Status; } + } + // + // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly. + // + FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME, &gEfiVendorKeysNvGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE); + if (Variable.CurrPtr != NULL) { + mVendorKeyState = *(GetVariableDataPtr (Variable.CurrPtr)); + } else { // - // 2. Update "SetupMode" variable to SETUP_MODE. + // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state. // - UpdatePlatformMode (SETUP_MODE); + mVendorKeyState = VENDOR_KEYS_VALID; + Status = UpdateVariable ( + EFI_VENDOR_KEYS_NV_VARIABLE_NAME, + &gEfiVendorKeysNvGuid, + &mVendorKeyState, + sizeof (UINT8), + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, + 0, + 0, + &Variable, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + } - // - // 3. Clear KEK, DB and DBX. - // - DeleteVariable (EFI_KEY_EXCHANGE_KEY_NAME, &gEfiGlobalVariableGuid); - DeleteVariable (EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid); - DeleteVariable (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid); + // + // Create "VendorKeys" variable with BS+RT attribute set. + // + FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE); + Status = UpdateVariable ( + EFI_VENDOR_KEYS_VARIABLE_NAME, + &gEfiGlobalVariableGuid, + &mVendorKeyState, + sizeof (UINT8), + EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS, + 0, + 0, + &Variable, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; } + DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME, mVendorKeyState)); + return Status; } @@ -318,20 +472,26 @@ AutenticatedVariableServiceInitialize ( Add public key in store and return its index. @param[in] PubKey Input pointer to Public Key data + @param[in] VariableDataEntry The variable data entry @return Index of new added item **/ UINT32 AddPubKeyInStore ( - IN UINT8 *PubKey + IN UINT8 *PubKey, + IN VARIABLE_ENTRY_CONSISTENCY *VariableDataEntry ) { - EFI_STATUS Status; - BOOLEAN IsFound; - UINT32 Index; - VARIABLE_POINTER_TRACK Variable; - UINT8 *Ptr; + EFI_STATUS Status; + BOOLEAN IsFound; + UINT32 Index; + VARIABLE_POINTER_TRACK Variable; + UINT8 *Ptr; + UINT8 *Data; + UINTN DataSize; + VARIABLE_ENTRY_CONSISTENCY PublicKeyEntry; + UINT32 Attributes; if (PubKey == NULL) { return 0; @@ -341,9 +501,14 @@ AddPubKeyInStore ( AUTHVAR_KEYDB_NAME, &gEfiAuthenticatedVariableGuid, &Variable, - &mVariableModuleGlobal->VariableGlobal + &mVariableModuleGlobal->VariableGlobal, + FALSE ); - ASSERT_EFI_ERROR (Status); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Get public key database variable failure, Status = %r\n", Status)); + return 0; + } + // // Check whether the public key entry does exist. // @@ -360,9 +525,64 @@ AddPubKeyInStore ( // // Add public key in database. // - if (mPubKeyNumber == MAX_KEY_NUM) { + if (mPubKeyNumber == mMaxKeyNumber) { + // + // Public key dadatase is full, try to reclaim invalid key. + // + if (AtRuntime ()) { + // + // NV storage can't reclaim at runtime. + // + return 0; + } + + Status = Reclaim ( + mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, + &mVariableModuleGlobal->NonVolatileLastVariableOffset, + FALSE, + NULL, + NULL, + 0, + TRUE + ); + if (EFI_ERROR (Status)) { + return 0; + } + + Status = FindVariable ( + AUTHVAR_KEYDB_NAME, + &gEfiAuthenticatedVariableGuid, + &Variable, + &mVariableModuleGlobal->VariableGlobal, + FALSE + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Get public key database variable failure, Status = %r\n", Status)); + return 0; + } + + DataSize = DataSizeOfVariable (Variable.CurrPtr); + Data = GetVariableDataPtr (Variable.CurrPtr); + ASSERT ((DataSize != 0) && (Data != NULL)); + CopyMem (mPubKeyStore, (UINT8 *) Data, DataSize); + mPubKeyNumber = (UINT32) (DataSize / EFI_CERT_TYPE_RSA2048_SIZE); + + if (mPubKeyNumber == mMaxKeyNumber) { + return 0; + } + } + + // + // Check the variable space for both public key and variable data. + // + PublicKeyEntry.VariableSize = (mPubKeyNumber + 1) * EFI_CERT_TYPE_RSA2048_SIZE; + PublicKeyEntry.Guid = &gEfiAuthenticatedVariableGuid; + PublicKeyEntry.Name = AUTHVAR_KEYDB_NAME; + Attributes = VARIABLE_ATTRIBUTE_NV_BS_RT | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS; + + if (!CheckRemainingSpaceForConsistency (Attributes, &PublicKeyEntry, VariableDataEntry, NULL)) { // - // Notes: Database is full, need enhancement here, currently just return 0. + // No enough variable space. // return 0; } @@ -377,22 +597,31 @@ AddPubKeyInStore ( &gEfiAuthenticatedVariableGuid, mPubKeyStore, mPubKeyNumber * EFI_CERT_TYPE_RSA2048_SIZE, - EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, + Attributes, 0, 0, &Variable, NULL ); - ASSERT_EFI_ERROR (Status); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Update public key database variable failure, Status = %r\n", Status)); + return 0; + } } return Index; } /** - Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256 type. + Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type. Follow the steps in UEFI2.2. + Caution: This function may receive untrusted input. + This function may be invoked in SMM mode, and datasize and data are external input. + This function will do basic validation, before parse the data. + This function will parse the authentication carefully to avoid security issues, like + buffer overflow, integer overflow. + @param[in] Data Pointer to data with AuthInfo. @param[in] DataSize Size of Data. @param[in] PubKey Public key used for verification. @@ -414,7 +643,9 @@ VerifyCounterBasedPayload ( EFI_CERT_BLOCK_RSA_2048_SHA256 *CertBlock; UINT8 Digest[SHA256_DIGEST_SIZE]; VOID *Rsa; - + UINTN PayloadSize; + + PayloadSize = DataSize - AUTHINFO_SIZE; Rsa = NULL; CertData = NULL; CertBlock = NULL; @@ -428,10 +659,10 @@ VerifyCounterBasedPayload ( // // wCertificateType should be WIN_CERT_TYPE_EFI_GUID. - // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256. + // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID. // if ((CertData->AuthInfo.Hdr.wCertificateType != WIN_CERT_TYPE_EFI_GUID) || - !CompareGuid (&CertData->AuthInfo.CertType, &gEfiCertRsa2048Sha256Guid) + !CompareGuid (&CertData->AuthInfo.CertType, &gEfiCertTypeRsa2048Sha256Guid) ) { // // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION. @@ -446,7 +677,14 @@ VerifyCounterBasedPayload ( if (!Status) { goto Done; } - Status = Sha256Update (mHashCtx, Data + AUTHINFO_SIZE, (UINTN) (DataSize - AUTHINFO_SIZE)); + Status = Sha256Update (mHashCtx, Data + AUTHINFO_SIZE, PayloadSize); + if (!Status) { + goto Done; + } + // + // Hash Size. + // + Status = Sha256Update (mHashCtx, &PayloadSize, sizeof (UINTN)); if (!Status) { goto Done; } @@ -516,7 +754,6 @@ UpdatePlatformMode ( { EFI_STATUS Status; VARIABLE_POINTER_TRACK Variable; - UINT32 VarAttr; UINT8 SecureBootMode; UINT8 SecureBootEnable; UINTN VariableDataSize; @@ -525,28 +762,19 @@ UpdatePlatformMode ( EFI_SETUP_MODE_NAME, &gEfiGlobalVariableGuid, &Variable, - &mVariableModuleGlobal->VariableGlobal + &mVariableModuleGlobal->VariableGlobal, + FALSE ); if (EFI_ERROR (Status)) { return Status; } - mPlatformMode = Mode; - VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS; - Status = UpdateVariable ( - EFI_SETUP_MODE_NAME, - &gEfiGlobalVariableGuid, - &mPlatformMode, - sizeof(UINT8), - VarAttr, - 0, - 0, - &Variable, - NULL - ); - if (EFI_ERROR (Status)) { - return Status; - } + // + // Update the value of SetupMode variable by a simple mem copy, this could avoid possible + // variable storage reclaim at runtime. + // + mPlatformMode = (UINT8) Mode; + CopyMem (GetVariableDataPtr (Variable.CurrPtr), &mPlatformMode, sizeof(UINT8)); if (AtRuntime ()) { // @@ -566,7 +794,8 @@ UpdatePlatformMode ( EFI_SECURE_BOOT_MODE_NAME, &gEfiGlobalVariableGuid, &Variable, - &mVariableModuleGlobal->VariableGlobal + &mVariableModuleGlobal->VariableGlobal, + FALSE ); // // If "SecureBoot" variable exists, then check "SetupMode" variable update. @@ -585,13 +814,12 @@ UpdatePlatformMode ( } } - VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS; Status = UpdateVariable ( EFI_SECURE_BOOT_MODE_NAME, &gEfiGlobalVariableGuid, &SecureBootMode, sizeof(UINT8), - VarAttr, + EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS, 0, 0, &Variable, @@ -608,7 +836,8 @@ UpdatePlatformMode ( EFI_SECURE_BOOT_ENABLE_NAME, &gEfiSecureBootEnableDisableGuid, &Variable, - &mVariableModuleGlobal->VariableGlobal + &mVariableModuleGlobal->VariableGlobal, + FALSE ); if (SecureBootMode == SECURE_BOOT_MODE_ENABLE) { @@ -644,178 +873,295 @@ UpdatePlatformMode ( } /** - Process variable with platform key for verification. + Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx variable. - @param[in] VariableName Name of Variable to be found. + @param[in] VariableName Name of Variable to be check. @param[in] VendorGuid Variable vendor GUID. - @param[in] Data Data pointer. - @param[in] DataSize Size of Data found. If size is less than the - data, this value contains the required size. - @param[in] Variable The variable information which is used to keep track of variable usage. - @param[in] Attributes Attribute value of the variable - @param[in] IsPk Indicate whether it is to process pk. - - @return EFI_INVALID_PARAMETER Invalid parameter. - @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation. - check carried out by the firmware. - @return EFI_SUCCESS Variable passed validation successfully. + @param[in] Data Point to the variable data to be checked. + @param[in] DataSize Size of Data. + @return EFI_INVALID_PARAMETER Invalid signature list format. + @return EFI_SUCCESS Passed signature list format check successfully. + **/ EFI_STATUS -ProcessVarWithPk ( +CheckSignatureListFormat( IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, IN VOID *Data, - IN UINTN DataSize, - IN VARIABLE_POINTER_TRACK *Variable, - IN UINT32 Attributes OPTIONAL, - IN BOOLEAN IsPk + IN UINTN DataSize ) { - EFI_STATUS Status; - VARIABLE_POINTER_TRACK PkVariable; - EFI_SIGNATURE_LIST *OldPkList; - EFI_SIGNATURE_DATA *OldPkData; - EFI_VARIABLE_AUTHENTICATION *CertData; - BOOLEAN TimeBase; - BOOLEAN Del; - UINT8 *Payload; - UINTN PayloadSize; - UINT64 MonotonicCount; - EFI_TIME *TimeStamp; - - if ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) { - // - // PK and KEK should set EFI_VARIABLE_NON_VOLATILE attribute. - // - return EFI_INVALID_PARAMETER; + EFI_SIGNATURE_LIST *SigList; + UINTN SigDataSize; + UINT32 Index; + UINT32 SigCount; + BOOLEAN IsPk; + VOID *RsaContext; + EFI_SIGNATURE_DATA *CertData; + UINTN CertLen; + + if (DataSize == 0) { + return EFI_SUCCESS; } - if (mPlatformMode == USER_MODE) { + ASSERT (VariableName != NULL && VendorGuid != NULL && Data != NULL); - if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) { - // - // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute means time-based X509 Cert PK. - // - TimeBase = TRUE; - } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) { - // - // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute means counter-based RSA-2048 Cert PK. - // - TimeBase = FALSE; - } else { - return EFI_INVALID_PARAMETER; - } + if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)){ + IsPk = TRUE; + } else if ((CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0) || + (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) && + (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0 || StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0))){ + IsPk = FALSE; + } else { + return EFI_SUCCESS; + } - if (TimeBase) { - // - // Verify against X509 Cert PK. - // - Del = FALSE; - Status = VerifyTimeBasedPayload (VariableName, VendorGuid, Data, DataSize, Variable, Attributes, TRUE, &Del); - if (!EFI_ERROR (Status)) { + SigCount = 0; + SigList = (EFI_SIGNATURE_LIST *) Data; + SigDataSize = DataSize; + RsaContext = NULL; + + // + // Walk throuth the input signature list and check the data format. + // If any signature is incorrectly formed, the whole check will fail. + // + while ((SigDataSize > 0) && (SigDataSize >= SigList->SignatureListSize)) { + for (Index = 0; Index < (sizeof (mSupportSigItem) / sizeof (EFI_SIGNATURE_ITEM)); Index++ ) { + if (CompareGuid (&SigList->SignatureType, &mSupportSigItem[Index].SigType)) { // - // If delete PK in user mode, need change to setup mode. + // The value of SignatureSize should always be 16 (size of SignatureOwner + // component) add the data length according to signature type. // - if (Del && IsPk) { - Status = UpdatePlatformMode (SETUP_MODE); + if (mSupportSigItem[Index].SigDataSize != ((UINT32) ~0) && + (SigList->SignatureSize - sizeof (EFI_GUID)) != mSupportSigItem[Index].SigDataSize) { + return EFI_INVALID_PARAMETER; } - } - return Status; - } else { - // - // Verify against RSA2048 Cert PK. - // - CertData = (EFI_VARIABLE_AUTHENTICATION *) Data; - if ((Variable->CurrPtr != NULL) && (CertData->MonotonicCount <= Variable->CurrPtr->MonotonicCount)) { - // - // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION. - // - return EFI_SECURITY_VIOLATION; - } - // - // Get platform key from variable. - // - Status = FindVariable ( - EFI_PLATFORM_KEY_NAME, - &gEfiGlobalVariableGuid, - &PkVariable, - &mVariableModuleGlobal->VariableGlobal - ); - ASSERT_EFI_ERROR (Status); - - OldPkList = (EFI_SIGNATURE_LIST *) GetVariableDataPtr (PkVariable.CurrPtr); - OldPkData = (EFI_SIGNATURE_DATA *) ((UINT8 *) OldPkList + sizeof (EFI_SIGNATURE_LIST) + OldPkList->SignatureHeaderSize); - Status = VerifyCounterBasedPayload (Data, DataSize, OldPkData->SignatureData); - if (!EFI_ERROR (Status)) { - Status = UpdateVariable ( - VariableName, - VendorGuid, - (UINT8*)Data + AUTHINFO_SIZE, - DataSize - AUTHINFO_SIZE, - Attributes, - 0, - CertData->MonotonicCount, - Variable, - NULL - ); - - if (!EFI_ERROR (Status)) { - // - // If delete PK in user mode, need change to setup mode. - // - if ((DataSize == AUTHINFO_SIZE) && IsPk) { - Status = UpdatePlatformMode (SETUP_MODE); - } + if (mSupportSigItem[Index].SigHeaderSize != ((UINTN) ~0) && + SigList->SignatureHeaderSize != mSupportSigItem[Index].SigHeaderSize) { + return EFI_INVALID_PARAMETER; } + break; } } - } else { - // - // Process PK or KEK in Setup mode. - // - if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) { - // - // Time-based Authentication descriptor. - // - MonotonicCount = 0; - TimeStamp = &((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->TimeStamp; - Payload = (UINT8 *) Data + AUTHINFO2_SIZE (Data); - PayloadSize = DataSize - AUTHINFO2_SIZE (Data); - } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) { + + if (Index == (sizeof (mSupportSigItem) / sizeof (EFI_SIGNATURE_ITEM))) { // - // Counter-based Authentication descriptor. + // Undefined signature type. // - MonotonicCount = ((EFI_VARIABLE_AUTHENTICATION *) Data)->MonotonicCount; - TimeStamp = NULL; - Payload = (UINT8*) Data + AUTHINFO_SIZE; - PayloadSize = DataSize - AUTHINFO_SIZE; - } else { + return EFI_INVALID_PARAMETER; + } + + if (CompareGuid (&SigList->SignatureType, &gEfiCertX509Guid)) { // - // No Authentication descriptor. + // Try to retrieve the RSA public key from the X.509 certificate. + // If this operation fails, it's not a valid certificate. // - MonotonicCount = 0; - TimeStamp = NULL; - Payload = Data; - PayloadSize = DataSize; + RsaContext = RsaNew (); + if (RsaContext == NULL) { + return EFI_INVALID_PARAMETER; + } + CertData = (EFI_SIGNATURE_DATA *) ((UINT8 *) SigList + sizeof (EFI_SIGNATURE_LIST) + SigList->SignatureHeaderSize); + CertLen = SigList->SignatureSize - sizeof (EFI_GUID); + if (!RsaGetPublicKeyFromX509 (CertData->SignatureData, CertLen, &RsaContext)) { + RsaFree (RsaContext); + return EFI_INVALID_PARAMETER; + } + RsaFree (RsaContext); } - Status = UpdateVariable ( + if ((SigList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - SigList->SignatureHeaderSize) % SigList->SignatureSize != 0) { + return EFI_INVALID_PARAMETER; + } + SigCount += (SigList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - SigList->SignatureHeaderSize) / SigList->SignatureSize; + + SigDataSize -= SigList->SignatureListSize; + SigList = (EFI_SIGNATURE_LIST *) ((UINT8 *) SigList + SigList->SignatureListSize); + } + + if (((UINTN) SigList - (UINTN) Data) != DataSize) { + return EFI_INVALID_PARAMETER; + } + + if (IsPk && SigCount > 1) { + return EFI_INVALID_PARAMETER; + } + + return EFI_SUCCESS; +} + +/** + Update "VendorKeys" variable to record the out of band secure boot key modification. + + @return EFI_SUCCESS Variable is updated successfully. + @return Others Failed to update variable. + +**/ +EFI_STATUS +VendorKeyIsModified ( + VOID + ) +{ + EFI_STATUS Status; + VARIABLE_POINTER_TRACK Variable; + + if (mVendorKeyState == VENDOR_KEYS_MODIFIED) { + return EFI_SUCCESS; + } + mVendorKeyState = VENDOR_KEYS_MODIFIED; + + FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME, &gEfiVendorKeysNvGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE); + Status = UpdateVariable ( + EFI_VENDOR_KEYS_NV_VARIABLE_NAME, + &gEfiVendorKeysNvGuid, + &mVendorKeyState, + sizeof (UINT8), + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, + 0, + 0, + &Variable, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE); + return UpdateVariable ( + EFI_VENDOR_KEYS_VARIABLE_NAME, + &gEfiGlobalVariableGuid, + &mVendorKeyState, + sizeof (UINT8), + EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS, + 0, + 0, + &Variable, + NULL + ); +} + +/** + Process variable with platform key for verification. + + Caution: This function may receive untrusted input. + This function may be invoked in SMM mode, and datasize and data are external input. + This function will do basic validation, before parse the data. + This function will parse the authentication carefully to avoid security issues, like + buffer overflow, integer overflow. + This function will check attribute carefully to avoid authentication bypass. + + @param[in] VariableName Name of Variable to be found. + @param[in] VendorGuid Variable vendor GUID. + @param[in] Data Data pointer. + @param[in] DataSize Size of Data found. If size is less than the + data, this value contains the required size. + @param[in] Variable The variable information which is used to keep track of variable usage. + @param[in] Attributes Attribute value of the variable + @param[in] IsPk Indicate whether it is to process pk. + + @return EFI_INVALID_PARAMETER Invalid parameter. + @return EFI_SECURITY_VIOLATION The variable does NOT pass the validation. + check carried out by the firmware. + @return EFI_SUCCESS Variable passed validation successfully. + +**/ +EFI_STATUS +ProcessVarWithPk ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN VOID *Data, + IN UINTN DataSize, + IN VARIABLE_POINTER_TRACK *Variable, + IN UINT32 Attributes OPTIONAL, + IN BOOLEAN IsPk + ) +{ + EFI_STATUS Status; + BOOLEAN Del; + UINT8 *Payload; + UINTN PayloadSize; + + if ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0 || + (Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == 0) { + // + // PK, KEK and db/dbx should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based + // authenticated variable. + // + return EFI_INVALID_PARAMETER; + } + + Del = FALSE; + if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode == SETUP_MODE && !IsPk)) { + Payload = (UINT8 *) Data + AUTHINFO2_SIZE (Data); + PayloadSize = DataSize - AUTHINFO2_SIZE (Data); + if (PayloadSize == 0) { + Del = TRUE; + } + + Status = CheckSignatureListFormat(VariableName, VendorGuid, Payload, PayloadSize); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = UpdateVariable ( VariableName, VendorGuid, Payload, PayloadSize, Attributes, 0, - MonotonicCount, + 0, Variable, - TimeStamp + &((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->TimeStamp ); + if (EFI_ERROR(Status)) { + return Status; + } + + if ((mPlatformMode != SETUP_MODE) || IsPk) { + Status = VendorKeyIsModified (); + } + } else if (mPlatformMode == USER_MODE) { + // + // Verify against X509 Cert in PK database. + // + Status = VerifyTimeBasedPayload ( + VariableName, + VendorGuid, + Data, + DataSize, + Variable, + Attributes, + AuthVarTypePk, + &Del + ); + } else { // - // If enroll PK in setup mode, need change to user mode. + // Verify against the certificate in data payload. // - if ((DataSize != 0) && IsPk) { + Status = VerifyTimeBasedPayload ( + VariableName, + VendorGuid, + Data, + DataSize, + Variable, + Attributes, + AuthVarTypePayload, + &Del + ); + } + + if (!EFI_ERROR(Status) && IsPk) { + if (mPlatformMode == SETUP_MODE && !Del) { + // + // If enroll PK in setup mode, need change to user mode. + // Status = UpdatePlatformMode (USER_MODE); + } else if (mPlatformMode == USER_MODE && Del){ + // + // If delete PK in user mode, need change to setup mode. + // + Status = UpdatePlatformMode (SETUP_MODE); } } @@ -825,6 +1171,13 @@ ProcessVarWithPk ( /** Process variable with key exchange key for verification. + Caution: This function may receive untrusted input. + This function may be invoked in SMM mode, and datasize and data are external input. + This function will do basic validation, before parse the data. + This function will parse the authentication carefully to avoid security issues, like + buffer overflow, integer overflow. + This function will check attribute carefully to avoid authentication bypass. + @param[in] VariableName Name of Variable to be found. @param[in] VendorGuid Variable vendor GUID. @param[in] Data Data pointer. @@ -850,108 +1203,45 @@ ProcessVarWithKek ( ) { EFI_STATUS Status; - VARIABLE_POINTER_TRACK KekVariable; - EFI_SIGNATURE_LIST *KekList; - EFI_SIGNATURE_DATA *KekItem; - UINT32 KekCount; - EFI_VARIABLE_AUTHENTICATION *CertData; - EFI_CERT_BLOCK_RSA_2048_SHA256 *CertBlock; - BOOLEAN IsFound; - UINT32 Index; - UINT32 KekDataSize; UINT8 *Payload; UINTN PayloadSize; - UINT64 MonotonicCount; - if (mPlatformMode == USER_MODE) { - if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == 0) { - // - // In user mode, should set EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute. - // - return EFI_INVALID_PARAMETER; - } - - CertData = (EFI_VARIABLE_AUTHENTICATION *) Data; - CertBlock = (EFI_CERT_BLOCK_RSA_2048_SHA256 *) (CertData->AuthInfo.CertData); - if ((Variable->CurrPtr != NULL) && (CertData->MonotonicCount <= Variable->CurrPtr->MonotonicCount)) { - // - // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION. - // - return EFI_SECURITY_VIOLATION; - } + if ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0 || + (Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == 0) { // - // Get KEK database from variable. + // DB and DBX should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based + // authenticated variable. // - Status = FindVariable ( - EFI_KEY_EXCHANGE_KEY_NAME, - &gEfiGlobalVariableGuid, - &KekVariable, - &mVariableModuleGlobal->VariableGlobal - ); - ASSERT_EFI_ERROR (Status); - - KekDataSize = KekVariable.CurrPtr->DataSize; - KekList = (EFI_SIGNATURE_LIST *) GetVariableDataPtr (KekVariable.CurrPtr); + return EFI_INVALID_PARAMETER; + } + Status = EFI_SUCCESS; + if (mPlatformMode == USER_MODE && !(InCustomMode() && UserPhysicalPresent())) { // - // Enumerate all Kek items in this list to verify the variable certificate data. - // If anyone is authenticated successfully, it means the variable is correct! + // Time-based, verify against X509 Cert KEK. // - IsFound = FALSE; - while ((KekDataSize > 0) && (KekDataSize >= KekList->SignatureListSize)) { - if (CompareGuid (&KekList->SignatureType, &gEfiCertRsa2048Guid)) { - KekItem = (EFI_SIGNATURE_DATA *) ((UINT8 *) KekList + sizeof (EFI_SIGNATURE_LIST) + KekList->SignatureHeaderSize); - KekCount = (KekList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - KekList->SignatureHeaderSize) / KekList->SignatureSize; - for (Index = 0; Index < KekCount; Index++) { - if (CompareMem (KekItem->SignatureData, CertBlock->PublicKey, EFI_CERT_TYPE_RSA2048_SIZE) == 0) { - IsFound = TRUE; - break; - } - KekItem = (EFI_SIGNATURE_DATA *) ((UINT8 *) KekItem + KekList->SignatureSize); - } - } - KekDataSize -= KekList->SignatureListSize; - KekList = (EFI_SIGNATURE_LIST *) ((UINT8 *) KekList + KekList->SignatureListSize); - } - - if (!IsFound) { - return EFI_SECURITY_VIOLATION; - } - - Status = VerifyCounterBasedPayload (Data, DataSize, CertBlock->PublicKey); - if (!EFI_ERROR (Status)) { - Status = UpdateVariable ( - VariableName, - VendorGuid, - (UINT8*)Data + AUTHINFO_SIZE, - DataSize - AUTHINFO_SIZE, - Attributes, - 0, - CertData->MonotonicCount, - Variable, - NULL - ); - } + return VerifyTimeBasedPayload ( + VariableName, + VendorGuid, + Data, + DataSize, + Variable, + Attributes, + AuthVarTypeKek, + NULL + ); } else { // - // If in setup mode, no authentication needed. + // If in setup mode or custom secure boot mode, no authentication needed. // - if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) { - // - // Counter-based Authentication descriptor. - // - MonotonicCount = ((EFI_VARIABLE_AUTHENTICATION *) Data)->MonotonicCount; - Payload = (UINT8*) Data + AUTHINFO_SIZE; - PayloadSize = DataSize - AUTHINFO_SIZE; - } else { - // - // No Authentication descriptor. - // - MonotonicCount = 0; - Payload = Data; - PayloadSize = DataSize; - } + Payload = (UINT8 *) Data + AUTHINFO2_SIZE (Data); + PayloadSize = DataSize - AUTHINFO2_SIZE (Data); + Status = CheckSignatureListFormat(VariableName, VendorGuid, Payload, PayloadSize); + if (EFI_ERROR (Status)) { + return Status; + } + Status = UpdateVariable ( VariableName, VendorGuid, @@ -959,10 +1249,17 @@ ProcessVarWithKek ( PayloadSize, Attributes, 0, - MonotonicCount, + 0, Variable, - NULL + &((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->TimeStamp ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (mPlatformMode != SETUP_MODE) { + Status = VendorKeyIsModified (); + } } return Status; @@ -971,6 +1268,13 @@ ProcessVarWithKek ( /** Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set + Caution: This function may receive untrusted input. + This function may be invoked in SMM mode, and datasize and data are external input. + This function will do basic validation, before parse the data. + This function will parse the authentication carefully to avoid security issues, like + buffer overflow, integer overflow. + This function will check attribute carefully to avoid authentication bypass. + @param[in] VariableName Name of Variable to be found. @param[in] VendorGuid Variable vendor GUID. @@ -983,6 +1287,7 @@ ProcessVarWithKek ( @return EFI_INVALID_PARAMETER Invalid parameter. @return EFI_WRITE_PROTECTED Variable is write-protected and needs authentication with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set. + @return EFI_OUT_OF_RESOURCES The Database to save the public key is full. @return EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set, but the AuthInfo does NOT pass the validation check carried out by the firmware. @@ -1007,6 +1312,7 @@ ProcessVariable ( EFI_CERT_BLOCK_RSA_2048_SHA256 *CertBlock; UINT32 KeyIndex; UINT64 MonotonicCount; + VARIABLE_ENTRY_CONSISTENCY VariableDataEntry; KeyIndex = 0; CertData = NULL; @@ -1014,11 +1320,43 @@ ProcessVariable ( PubKey = NULL; IsDeletion = FALSE; + if (NeedPhysicallyPresent(VariableName, VendorGuid) && !UserPhysicalPresent()) { + // + // This variable is protected, only physical present user could modify its value. + // + return EFI_SECURITY_VIOLATION; + } + + // + // A time-based authenticated variable and a count-based authenticated variable + // can't be updated by each other. + // + if (Variable->CurrPtr != NULL) { + if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) && + ((Variable->CurrPtr->Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0)) { + return EFI_SECURITY_VIOLATION; + } + + if (((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) && + ((Variable->CurrPtr->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0)) { + return EFI_SECURITY_VIOLATION; + } + } + // // Process Time-based Authenticated variable. // if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) { - return VerifyTimeBasedPayload (VariableName, VendorGuid, Data, DataSize, Variable, Attributes, FALSE, NULL); + return VerifyTimeBasedPayload ( + VariableName, + VendorGuid, + Data, + DataSize, + Variable, + Attributes, + AuthVarTypePriv, + NULL + ); } // @@ -1042,9 +1380,9 @@ ProcessVariable ( KeyIndex = Variable->CurrPtr->PubKeyIndex; IsFirstTime = FALSE; } - } else if ((Variable->CurrPtr != NULL) && - (Variable->CurrPtr->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0 - ) { + } else if ((Variable->CurrPtr != NULL) && + ((Variable->CurrPtr->Attributes & (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) != 0) + ) { // // If the variable is already write-protected, it always needs authentication before update. // @@ -1100,10 +1438,17 @@ ProcessVariable ( // Now, the signature has been verified! // if (IsFirstTime && !IsDeletion) { + VariableDataEntry.VariableSize = DataSize - AUTHINFO_SIZE; + VariableDataEntry.Guid = VendorGuid; + VariableDataEntry.Name = VariableName; + // // Update public key database variable if need. // - KeyIndex = AddPubKeyInStore (PubKey); + KeyIndex = AddPubKeyInStore (PubKey, &VariableDataEntry); + if (KeyIndex == 0) { + return EFI_OUT_OF_RESOURCES; + } } // @@ -1117,20 +1462,24 @@ ProcessVariable ( will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA will be ignored. - @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST. - @param[in] DataSize Size of Data buffer. - @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended. - @param[in] NewDataSize Size of NewData buffer. + @param[in, out] Data Pointer to original EFI_SIGNATURE_LIST. + @param[in] DataSize Size of Data buffer. + @param[in] FreeBufSize Size of free data buffer + @param[in] NewData Pointer to new EFI_SIGNATURE_LIST to be appended. + @param[in] NewDataSize Size of NewData buffer. + @param[out] MergedBufSize Size of the merged buffer - @return Size of the merged buffer. + @return EFI_BUFFER_TOO_SMALL if input Data buffer overflowed **/ -UINTN +EFI_STATUS AppendSignatureList ( IN OUT VOID *Data, IN UINTN DataSize, + IN UINTN FreeBufSize, IN VOID *NewData, - IN UINTN NewDataSize + IN UINTN NewDataSize, + OUT UINTN *MergedBufSize ) { EFI_SIGNATURE_LIST *CertList; @@ -1189,15 +1538,25 @@ AppendSignatureList ( // New EFI_SIGNATURE_DATA, append it. // if (CopiedCount == 0) { + if (FreeBufSize < sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize) { + return EFI_BUFFER_TOO_SMALL; + } + // // Copy EFI_SIGNATURE_LIST header for only once. // + CopyMem (Tail, NewCertList, sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize); Tail = Tail + sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize; + FreeBufSize -= sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize; } + if (FreeBufSize < NewCertList->SignatureSize) { + return EFI_BUFFER_TOO_SMALL; + } CopyMem (Tail, NewCert, NewCertList->SignatureSize); Tail += NewCertList->SignatureSize; + FreeBufSize -= NewCertList->SignatureSize; CopiedCount++; } @@ -1217,7 +1576,8 @@ AppendSignatureList ( NewCertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) NewCertList + NewCertList->SignatureListSize); } - return (Tail - (UINT8 *) Data); + *MergedBufSize = (Tail - (UINT8 *) Data); + return EFI_SUCCESS; } /** @@ -1246,15 +1606,480 @@ CompareTimeStamp ( } else if (FirstTime->Hour != SecondTime->Hour) { return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour); } else if (FirstTime->Minute != SecondTime->Minute) { - return (BOOLEAN) (FirstTime->Minute < FirstTime->Minute); + return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute); } return (BOOLEAN) (FirstTime->Second <= SecondTime->Second); } +/** + Find matching signer's certificates for common authenticated variable + by corresponding VariableName and VendorGuid from "certdb". + + The data format of "certdb": + // + // UINT32 CertDbListSize; + // /// AUTH_CERT_DB_DATA Certs1[]; + // /// AUTH_CERT_DB_DATA Certs2[]; + // /// ... + // /// AUTH_CERT_DB_DATA Certsn[]; + // + + @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[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 + starting of Data. + @param[out] CertNodeSize Length of AUTH_CERT_DB_DATA in bytes. + + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_NOT_FOUND Fail to find matching certs. + @retval EFI_SUCCESS Find matching certs and output parameters. + +**/ +EFI_STATUS +FindCertsFromDb ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN UINT8 *Data, + IN UINTN DataSize, + OUT UINT32 *CertOffset, OPTIONAL + OUT UINT32 *CertDataSize, OPTIONAL + OUT UINT32 *CertNodeOffset,OPTIONAL + OUT UINT32 *CertNodeSize OPTIONAL + ) +{ + UINT32 Offset; + AUTH_CERT_DB_DATA *Ptr; + UINT32 CertSize; + UINT32 NameSize; + UINT32 NodeSize; + UINT32 CertDbListSize; + + if ((VariableName == NULL) || (VendorGuid == NULL) || (Data == NULL)) { + return EFI_INVALID_PARAMETER; + } + + // + // Check whether DataSize matches recorded CertDbListSize. + // + if (DataSize < sizeof (UINT32)) { + return EFI_INVALID_PARAMETER; + } + + CertDbListSize = ReadUnaligned32 ((UINT32 *) Data); + + if (CertDbListSize != (UINT32) DataSize) { + return EFI_INVALID_PARAMETER; + } + + Offset = sizeof (UINT32); + + // + // Get corresponding certificates by VendorGuid and VariableName. + // + while (Offset < (UINT32) DataSize) { + Ptr = (AUTH_CERT_DB_DATA *) (Data + Offset); + // + // Check whether VendorGuid matches. + // + if (CompareGuid (&Ptr->VendorGuid, VendorGuid)) { + NodeSize = ReadUnaligned32 (&Ptr->CertNodeSize); + NameSize = ReadUnaligned32 (&Ptr->NameSize); + CertSize = ReadUnaligned32 (&Ptr->CertDataSize); + + if (NodeSize != sizeof (EFI_GUID) + sizeof (UINT32) * 3 + CertSize + + sizeof (CHAR16) * NameSize) { + return EFI_INVALID_PARAMETER; + } + + Offset = Offset + sizeof (EFI_GUID) + sizeof (UINT32) * 3; + // + // Check whether VariableName matches. + // + if ((NameSize == StrLen (VariableName)) && + (CompareMem (Data + Offset, VariableName, NameSize * sizeof (CHAR16)) == 0)) { + Offset = Offset + NameSize * sizeof (CHAR16); + + if (CertOffset != NULL) { + *CertOffset = Offset; + } + + if (CertDataSize != NULL) { + *CertDataSize = CertSize; + } + + if (CertNodeOffset != NULL) { + *CertNodeOffset = (UINT32) ((UINT8 *) Ptr - Data); + } + + if (CertNodeSize != NULL) { + *CertNodeSize = NodeSize; + } + + return EFI_SUCCESS; + } else { + Offset = Offset + NameSize * sizeof (CHAR16) + CertSize; + } + } else { + NodeSize = ReadUnaligned32 (&Ptr->CertNodeSize); + Offset = Offset + NodeSize; + } + } + + return EFI_NOT_FOUND; +} + +/** + Retrieve signer's certificates for common authenticated variable + by corresponding VariableName and VendorGuid from "certdb". + + @param[in] VariableName Name of authenticated Variable. + @param[in] VendorGuid Vendor GUID 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_SUCCESS Get signer's certificates successfully. + +**/ +EFI_STATUS +GetCertsFromDb ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + OUT UINT8 **CertData, + OUT UINT32 *CertDataSize + ) +{ + VARIABLE_POINTER_TRACK CertDbVariable; + EFI_STATUS Status; + UINT8 *Data; + UINTN DataSize; + UINT32 CertOffset; + + if ((VariableName == NULL) || (VendorGuid == NULL) || (CertData == NULL) || (CertDataSize == NULL)) { + return EFI_INVALID_PARAMETER; + } + + // + // Get variable "certdb". + // + Status = FindVariable ( + EFI_CERT_DB_NAME, + &gEfiCertDbGuid, + &CertDbVariable, + &mVariableModuleGlobal->VariableGlobal, + FALSE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + DataSize = DataSizeOfVariable (CertDbVariable.CurrPtr); + Data = GetVariableDataPtr (CertDbVariable.CurrPtr); + if ((DataSize == 0) || (Data == NULL)) { + ASSERT (FALSE); + return EFI_NOT_FOUND; + } + + Status = FindCertsFromDb ( + VariableName, + VendorGuid, + Data, + DataSize, + &CertOffset, + CertDataSize, + NULL, + NULL + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + *CertData = Data + CertOffset; + return EFI_SUCCESS; +} + +/** + Delete matching signer's certificates when deleting common authenticated + variable by corresponding VariableName and VendorGuid from "certdb". + + @param[in] VariableName Name of authenticated Variable. + @param[in] VendorGuid Vendor GUID 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_OUT_OF_RESOURCES The operation is failed due to lack of resources. + @retval EFI_SUCCESS The operation is completed successfully. + +**/ +EFI_STATUS +DeleteCertsFromDb ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid + ) +{ + VARIABLE_POINTER_TRACK CertDbVariable; + EFI_STATUS Status; + UINT8 *Data; + UINTN DataSize; + UINT32 VarAttr; + UINT32 CertNodeOffset; + UINT32 CertNodeSize; + UINT8 *NewCertDb; + UINT32 NewCertDbSize; + + if ((VariableName == NULL) || (VendorGuid == NULL)) { + return EFI_INVALID_PARAMETER; + } + + // + // Get variable "certdb". + // + Status = FindVariable ( + EFI_CERT_DB_NAME, + &gEfiCertDbGuid, + &CertDbVariable, + &mVariableModuleGlobal->VariableGlobal, + FALSE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + DataSize = DataSizeOfVariable (CertDbVariable.CurrPtr); + Data = GetVariableDataPtr (CertDbVariable.CurrPtr); + if ((DataSize == 0) || (Data == NULL)) { + ASSERT (FALSE); + return EFI_NOT_FOUND; + } + + if (DataSize == sizeof (UINT32)) { + // + // There is no certs in certdb. + // + return EFI_SUCCESS; + } + + // + // Get corresponding cert node from certdb. + // + Status = FindCertsFromDb ( + VariableName, + VendorGuid, + Data, + DataSize, + NULL, + NULL, + &CertNodeOffset, + &CertNodeSize + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + if (DataSize < (CertNodeOffset + CertNodeSize)) { + return EFI_NOT_FOUND; + } + + // + // Construct new data content of variable "certdb". + // + NewCertDbSize = (UINT32) DataSize - CertNodeSize; + NewCertDb = (UINT8*) mCertDbStore; + + // + // Copy the DB entries before deleting node. + // + CopyMem (NewCertDb, Data, CertNodeOffset); + // + // Update CertDbListSize. + // + CopyMem (NewCertDb, &NewCertDbSize, sizeof (UINT32)); + // + // Copy the DB entries after deleting node. + // + if (DataSize > (CertNodeOffset + CertNodeSize)) { + CopyMem ( + NewCertDb + CertNodeOffset, + Data + CertNodeOffset + CertNodeSize, + DataSize - CertNodeOffset - CertNodeSize + ); + } + + // + // Set "certdb". + // + VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; + Status = UpdateVariable ( + EFI_CERT_DB_NAME, + &gEfiCertDbGuid, + NewCertDb, + NewCertDbSize, + VarAttr, + 0, + 0, + &CertDbVariable, + NULL + ); + + return Status; +} + +/** + Insert signer's certificates for common authenticated variable with VariableName + and VendorGuid in AUTH_CERT_DB_DATA to "certdb". + + @param[in] VariableName Name of authenticated Variable. + @param[in] VendorGuid Vendor GUID of authenticated Variable. + @param[in] CertData Pointer to signer's certificates. + @param[in] CertDataSize Length of CertData in bytes. + + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @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" + +**/ +EFI_STATUS +InsertCertsToDb ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN UINT8 *CertData, + IN UINTN CertDataSize + ) +{ + VARIABLE_POINTER_TRACK CertDbVariable; + EFI_STATUS Status; + UINT8 *Data; + UINTN DataSize; + UINT32 VarAttr; + UINT8 *NewCertDb; + UINT32 NewCertDbSize; + UINT32 CertNodeSize; + UINT32 NameSize; + AUTH_CERT_DB_DATA *Ptr; + + if ((VariableName == NULL) || (VendorGuid == NULL) || (CertData == NULL)) { + return EFI_INVALID_PARAMETER; + } + + // + // Get variable "certdb". + // + Status = FindVariable ( + EFI_CERT_DB_NAME, + &gEfiCertDbGuid, + &CertDbVariable, + &mVariableModuleGlobal->VariableGlobal, + FALSE + ); + if (EFI_ERROR (Status)) { + return Status; + } + + DataSize = DataSizeOfVariable (CertDbVariable.CurrPtr); + Data = GetVariableDataPtr (CertDbVariable.CurrPtr); + if ((DataSize == 0) || (Data == NULL)) { + ASSERT (FALSE); + return EFI_NOT_FOUND; + } + + // + // Find whether matching cert node already exists in "certdb". + // If yes return error. + // + Status = FindCertsFromDb ( + VariableName, + VendorGuid, + Data, + DataSize, + NULL, + NULL, + NULL, + NULL + ); + + if (!EFI_ERROR (Status)) { + ASSERT (FALSE); + return EFI_ACCESS_DENIED; + } + + // + // Construct new data content of variable "certdb". + // + NameSize = (UINT32) StrLen (VariableName); + CertNodeSize = sizeof (AUTH_CERT_DB_DATA) + (UINT32) CertDataSize + NameSize * sizeof (CHAR16); + NewCertDbSize = (UINT32) DataSize + CertNodeSize; + if (NewCertDbSize > mMaxCertDbSize) { + return EFI_OUT_OF_RESOURCES; + } + NewCertDb = (UINT8*) mCertDbStore; + + // + // Copy the DB entries before deleting node. + // + CopyMem (NewCertDb, Data, DataSize); + // + // Update CertDbListSize. + // + CopyMem (NewCertDb, &NewCertDbSize, sizeof (UINT32)); + // + // Construct new cert node. + // + Ptr = (AUTH_CERT_DB_DATA *) (NewCertDb + DataSize); + CopyGuid (&Ptr->VendorGuid, VendorGuid); + CopyMem (&Ptr->CertNodeSize, &CertNodeSize, sizeof (UINT32)); + CopyMem (&Ptr->NameSize, &NameSize, sizeof (UINT32)); + CopyMem (&Ptr->CertDataSize, &CertDataSize, sizeof (UINT32)); + + CopyMem ( + (UINT8 *) Ptr + sizeof (AUTH_CERT_DB_DATA), + VariableName, + NameSize * sizeof (CHAR16) + ); + + CopyMem ( + (UINT8 *) Ptr + sizeof (AUTH_CERT_DB_DATA) + NameSize * sizeof (CHAR16), + CertData, + CertDataSize + ); + + // + // Set "certdb". + // + VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; + Status = UpdateVariable ( + EFI_CERT_DB_NAME, + &gEfiCertDbGuid, + NewCertDb, + NewCertDbSize, + VarAttr, + 0, + 0, + &CertDbVariable, + NULL + ); + + return Status; +} + /** Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set + Caution: This function may receive untrusted input. + This function may be invoked in SMM mode, and datasize and data are external input. + This function will do basic validation, before parse the data. + This function will parse the authentication carefully to avoid security issues, like + buffer overflow, integer overflow. + @param[in] VariableName Name of Variable to be found. @param[in] VendorGuid Variable vendor GUID. @param[in] Data Data pointer. @@ -1262,7 +2087,7 @@ CompareTimeStamp ( data, this value contains the required size. @param[in] Variable The variable information which is used to keep track of variable usage. @param[in] Attributes Attribute value of the variable. - @param[in] Pk Verify against PK or KEK database. + @param[in] AuthVarType Verify against PK, KEK database, private database or certificate in data payload. @param[out] VarDel Delete the variable or not. @retval EFI_INVALID_PARAMETER Invalid parameter. @@ -1281,7 +2106,7 @@ VerifyTimeBasedPayload ( IN UINTN DataSize, IN VARIABLE_POINTER_TRACK *Variable, IN UINT32 Attributes, - IN BOOLEAN Pk, + IN AUTHVAR_TYPE AuthVarType, OUT BOOLEAN *VarDel ) { @@ -1295,7 +2120,6 @@ VerifyTimeBasedPayload ( UINT32 Attr; UINT32 SigDataSize; UINT32 KekDataSize; - BOOLEAN Result; BOOLEAN VerifyStatus; EFI_STATUS Status; EFI_SIGNATURE_LIST *CertList; @@ -1307,12 +2131,20 @@ VerifyTimeBasedPayload ( VARIABLE_POINTER_TRACK PkVariable; UINT8 *Buffer; UINTN Length; + UINT8 *SignerCerts; + UINT8 *WrapSigData; + UINTN CertStackSize; + UINT8 *CertsInCertDb; + UINT32 CertsSizeinDb; - Result = FALSE; VerifyStatus = FALSE; CertData = NULL; NewData = NULL; Attr = Attributes; + WrapSigData = NULL; + SignerCerts = NULL; + RootCert = NULL; + CertsInCertDb = NULL; // // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is @@ -1333,7 +2165,7 @@ VerifyTimeBasedPayload ( (CertData->TimeStamp.TimeZone != 0) || (CertData->TimeStamp.Daylight != 0) || (CertData->TimeStamp.Pad2 != 0)) { - return EFI_INVALID_PARAMETER; + return EFI_SECURITY_VIOLATION; } if ((Variable->CurrPtr != NULL) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0)) { @@ -1396,25 +2228,45 @@ VerifyTimeBasedPayload ( CopyMem (Buffer, PayloadPtr, PayloadSize); - if (Pk) { + if (AuthVarType == AuthVarTypePk) { + // + // Verify that the signature has been made with the current Platform Key (no chaining for PK). + // First, get signer's certificates from SignedData. + // + VerifyStatus = Pkcs7GetSigners ( + SigData, + SigDataSize, + &SignerCerts, + &CertStackSize, + &RootCert, + &RootCertSize + ); + if (!VerifyStatus) { + goto Exit; + } + // - // Get platform key from variable. + // Second, get the current platform key from variable. Check whether it's identical with signer's certificates + // in SignedData. If not, return error immediately. // Status = FindVariable ( EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid, &PkVariable, - &mVariableModuleGlobal->VariableGlobal + &mVariableModuleGlobal->VariableGlobal, + FALSE ); if (EFI_ERROR (Status)) { - return Status; + VerifyStatus = FALSE; + goto Exit; } - CertList = (EFI_SIGNATURE_LIST *) GetVariableDataPtr (PkVariable.CurrPtr); Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize); - RootCert = Cert->SignatureData; - RootCertSize = CertList->SignatureSize; - + if ((RootCertSize != (CertList->SignatureSize - (sizeof (EFI_SIGNATURE_DATA) - 1))) || + (CompareMem (Cert->SignatureData, RootCert, RootCertSize) != 0)) { + VerifyStatus = FALSE; + goto Exit; + } // // Verify Pkcs7 SignedData via Pkcs7Verify library. @@ -1428,7 +2280,7 @@ VerifyTimeBasedPayload ( NewDataSize ); - } else { + } else if (AuthVarType == AuthVarTypeKek) { // // Get KEK database from variable. @@ -1437,7 +2289,8 @@ VerifyTimeBasedPayload ( EFI_KEY_EXCHANGE_KEY_NAME, &gEfiGlobalVariableGuid, &KekVariable, - &mVariableModuleGlobal->VariableGlobal + &mVariableModuleGlobal->VariableGlobal, + FALSE ); if (EFI_ERROR (Status)) { return Status; @@ -1457,7 +2310,7 @@ VerifyTimeBasedPayload ( // Iterate each Signature Data Node within this CertList for a verify // RootCert = Cert->SignatureData; - RootCertSize = CertList->SignatureSize; + RootCertSize = CertList->SignatureSize - (sizeof (EFI_SIGNATURE_DATA) - 1); // // Verify Pkcs7 SignedData via Pkcs7Verify library. @@ -1479,14 +2332,110 @@ VerifyTimeBasedPayload ( KekDataSize -= CertList->SignatureListSize; CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize); } + } else if (AuthVarType == AuthVarTypePriv) { + + // + // Process common authenticated variable except PK/KEK/DB/DBX. + // Get signer's certificates from SignedData. + // + VerifyStatus = Pkcs7GetSigners ( + SigData, + SigDataSize, + &SignerCerts, + &CertStackSize, + &RootCert, + &RootCertSize + ); + if (!VerifyStatus) { + goto Exit; + } + + // + // Get previously stored signer's certificates from certdb for existing + // variable. Check whether they are identical with signer's certificates + // in SignedData. If not, return error immediately. + // + if ((Variable->CurrPtr != NULL)) { + VerifyStatus = FALSE; + + Status = GetCertsFromDb (VariableName, VendorGuid, &CertsInCertDb, &CertsSizeinDb); + if (EFI_ERROR (Status)) { + goto Exit; + } + + if ((CertStackSize != CertsSizeinDb) || + (CompareMem (SignerCerts, CertsInCertDb, CertsSizeinDb) != 0)) { + goto Exit; + } + } + + VerifyStatus = Pkcs7Verify ( + SigData, + SigDataSize, + RootCert, + RootCertSize, + NewData, + NewDataSize + ); + if (!VerifyStatus) { + goto Exit; + } + + // + // Delete signer's certificates when delete the common authenticated variable. + // + if ((PayloadSize == 0) && (Variable->CurrPtr != NULL) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0)) { + Status = DeleteCertsFromDb (VariableName, VendorGuid); + if (EFI_ERROR (Status)) { + VerifyStatus = FALSE; + goto Exit; + } + } else if (Variable->CurrPtr == NULL && PayloadSize != 0) { + // + // Insert signer's certificates when adding a new common authenticated variable. + // + Status = InsertCertsToDb (VariableName, VendorGuid, SignerCerts, CertStackSize); + if (EFI_ERROR (Status)) { + VerifyStatus = FALSE; + goto Exit; + } + } + } else if (AuthVarType == AuthVarTypePayload) { + CertList = (EFI_SIGNATURE_LIST *) PayloadPtr; + Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize); + RootCert = Cert->SignatureData; + RootCertSize = CertList->SignatureSize - (sizeof (EFI_SIGNATURE_DATA) - 1); + + // Verify Pkcs7 SignedData via Pkcs7Verify library. + // + VerifyStatus = Pkcs7Verify ( + SigData, + SigDataSize, + RootCert, + RootCertSize, + NewData, + NewDataSize + ); + } else { + return EFI_SECURITY_VIOLATION; } Exit: + if (AuthVarType == AuthVarTypePk || AuthVarType == AuthVarTypePriv) { + Pkcs7FreeSigners (RootCert); + Pkcs7FreeSigners (SignerCerts); + } + if (!VerifyStatus) { return EFI_SECURITY_VIOLATION; } + Status = CheckSignatureListFormat(VariableName, VendorGuid, PayloadPtr, PayloadSize); + if (EFI_ERROR (Status)) { + return Status; + } + if ((PayloadSize == 0) && (VarDel != NULL)) { *VarDel = TRUE; } @@ -1494,15 +2443,16 @@ Exit: // // Final step: Update/Append Variable if it pass Pkcs7Verify // - return UpdateVariable ( - VariableName, - VendorGuid, - PayloadPtr, - PayloadSize, - Attributes, - 0, - 0, - Variable, - &CertData->TimeStamp - ); + return UpdateVariable ( + VariableName, + VendorGuid, + PayloadPtr, + PayloadSize, + Attributes, + 0, + 0, + Variable, + &CertData->TimeStamp + ); } +