]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/HashLibBaseCryptoRouter/HashLibBaseCryptoRouterPei.c
SecurityPkg HashLibRouter: Avoid incorrect PcdTcg2HashAlgorithmBitmap
[mirror_edk2.git] / SecurityPkg / Library / HashLibBaseCryptoRouter / HashLibBaseCryptoRouterPei.c
index a4fc0c6595d8afeeb8cb60abac1141c70bd10b5a..dbee0f2531bc943906ce29cbdc7384927f2af902 100644 (file)
@@ -3,7 +3,7 @@
   hash handler registerd, such as SHA1, SHA256.\r
   Platform can use PcdTpm2HashMask to mask some hash engines.\r
 \r
-Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved. <BR>\r
+Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved. <BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -23,6 +23,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Library/PcdLib.h>\r
 #include <Library/HobLib.h>\r
 #include <Library/HashLib.h>\r
+#include <Guid/ZeroGuid.h>\r
 \r
 #include "HashLibBaseCryptoRouterCommon.h"\r
 \r
@@ -32,27 +33,95 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 EFI_GUID mHashLibPeiRouterGuid = HASH_LIB_PEI_ROUTER_GUID;\r
 \r
 typedef struct {\r
+  //\r
+  // If gZeroGuid, SupportedHashMask is 0 for FIRST module which consumes HashLib\r
+  //   or the hash algorithm bitmap of LAST module which consumes HashLib.\r
+  //   HashInterfaceCount and HashInterface are all 0.\r
+  // If gEfiCallerIdGuid, HashInterfaceCount, HashInterface and SupportedHashMask\r
+  //   are the hash interface information of CURRENT module which consumes HashLib.\r
+  //\r
+  EFI_GUID         Identifier;\r
   UINTN            HashInterfaceCount;\r
   HASH_INTERFACE   HashInterface[HASH_COUNT];\r
+  UINT32           SupportedHashMask;\r
 } HASH_INTERFACE_HOB;\r
 \r
 /**\r
-  This function get hash interface.\r
+  This function gets hash interface hob.\r
+\r
+  @param Identifier    Identifier to get hash interface hob.\r
+\r
+  @retval hash interface hob.\r
+**/\r
+HASH_INTERFACE_HOB *\r
+InternalGetHashInterfaceHob (\r
+  EFI_GUID      *Identifier\r
+  )\r
+{\r
+  EFI_PEI_HOB_POINTERS  Hob;\r
+  HASH_INTERFACE_HOB    *HashInterfaceHob;\r
+\r
+  Hob.Raw = GetFirstGuidHob (&mHashLibPeiRouterGuid);\r
+  while (Hob.Raw != NULL) {\r
+    HashInterfaceHob = GET_GUID_HOB_DATA (Hob);\r
+    if (CompareGuid (&HashInterfaceHob->Identifier, Identifier)) {\r
+      //\r
+      // Found the matched one.\r
+      //\r
+      return HashInterfaceHob;\r
+    }\r
+    Hob.Raw = GET_NEXT_HOB (Hob);\r
+    Hob.Raw = GetNextGuidHob (&mHashLibPeiRouterGuid, Hob.Raw);\r
+  }\r
+  return NULL;\r
+}\r
+\r
+/**\r
+  This function creates hash interface hob.\r
 \r
-  @retval hash interface.\r
+  @param Identifier    Identifier to create hash interface hob.\r
+\r
+  @retval hash interface hob.\r
 **/\r
 HASH_INTERFACE_HOB *\r
-InternalGetHashInterface (\r
-  VOID\r
+InternalCreateHashInterfaceHob (\r
+  EFI_GUID      *Identifier\r
   )\r
 {\r
-  EFI_HOB_GUID_TYPE *Hob;\r
+  HASH_INTERFACE_HOB LocalHashInterfaceHob;\r
+\r
+  ZeroMem (&LocalHashInterfaceHob, sizeof(LocalHashInterfaceHob));\r
+  CopyGuid (&LocalHashInterfaceHob.Identifier, Identifier);\r
+  return BuildGuidDataHob (&mHashLibPeiRouterGuid, &LocalHashInterfaceHob, sizeof(LocalHashInterfaceHob));\r
+}\r
 \r
-  Hob = GetFirstGuidHob (&mHashLibPeiRouterGuid);\r
-  if (Hob == NULL) {\r
-    return NULL;\r
+/**\r
+  Check mismatch of supported HashMask between modules\r
+  that may link different HashInstanceLib instances.\r
+\r
+  @param HashInterfaceHobCurrent    Pointer to hash interface hob for CURRENT module.\r
+\r
+**/\r
+VOID\r
+CheckSupportedHashMaskMismatch (\r
+  IN HASH_INTERFACE_HOB *HashInterfaceHobCurrent\r
+  )\r
+{\r
+  HASH_INTERFACE_HOB    *HashInterfaceHobLast;\r
+\r
+  HashInterfaceHobLast = InternalGetHashInterfaceHob (&gZeroGuid);\r
+  ASSERT (HashInterfaceHobLast != NULL);\r
+\r
+  if ((HashInterfaceHobLast->SupportedHashMask != 0) &&\r
+      (HashInterfaceHobCurrent->SupportedHashMask != HashInterfaceHobLast->SupportedHashMask)) {\r
+    DEBUG ((\r
+      DEBUG_WARN,\r
+      "WARNING: There is mismatch of supported HashMask (0x%x - 0x%x) between modules\n",\r
+      HashInterfaceHobCurrent->SupportedHashMask,\r
+      HashInterfaceHobLast->SupportedHashMask\r
+      ));\r
+    DEBUG ((DEBUG_WARN, "that are linking different HashInstanceLib instances!\n"));\r
   }\r
-  return (HASH_INTERFACE_HOB *)(Hob + 1);\r
 }\r
 \r
 /**\r
@@ -74,7 +143,7 @@ HashStart (
   UINTN              Index;\r
   UINT32             HashMask;\r
 \r
-  HashInterfaceHob = InternalGetHashInterface ();\r
+  HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);\r
   if (HashInterfaceHob == NULL) {\r
     return EFI_UNSUPPORTED;\r
   }\r
@@ -83,6 +152,8 @@ HashStart (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
+  CheckSupportedHashMaskMismatch (HashInterfaceHob);\r
+\r
   HashCtx = AllocatePool (sizeof(*HashCtx) * HashInterfaceHob->HashInterfaceCount);\r
   ASSERT (HashCtx != NULL);\r
 \r
@@ -120,7 +191,7 @@ HashUpdate (
   UINTN              Index;\r
   UINT32             HashMask;\r
 \r
-  HashInterfaceHob = InternalGetHashInterface ();\r
+  HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);\r
   if (HashInterfaceHob == NULL) {\r
     return EFI_UNSUPPORTED;\r
   }\r
@@ -129,6 +200,8 @@ HashUpdate (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
+  CheckSupportedHashMaskMismatch (HashInterfaceHob);\r
+\r
   HashCtx = (HASH_HANDLE *)HashHandle;\r
 \r
   for (Index = 0; Index < HashInterfaceHob->HashInterfaceCount; Index++) {\r
@@ -169,7 +242,7 @@ HashCompleteAndExtend (
   EFI_STATUS         Status;\r
   UINT32             HashMask;\r
 \r
-  HashInterfaceHob = InternalGetHashInterface ();\r
+  HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);\r
   if (HashInterfaceHob == NULL) {\r
     return EFI_UNSUPPORTED;\r
   }\r
@@ -178,6 +251,8 @@ HashCompleteAndExtend (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
+  CheckSupportedHashMaskMismatch (HashInterfaceHob);\r
+\r
   HashCtx = (HASH_HANDLE *)HashHandle;\r
   ZeroMem (DigestList, sizeof(*DigestList));\r
 \r
@@ -222,7 +297,7 @@ HashAndExtend (
   HASH_HANDLE        HashHandle;\r
   EFI_STATUS         Status;\r
 \r
-  HashInterfaceHob = InternalGetHashInterface ();\r
+  HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);\r
   if (HashInterfaceHob == NULL) {\r
     return EFI_UNSUPPORTED;\r
   }\r
@@ -231,6 +306,8 @@ HashAndExtend (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
+  CheckSupportedHashMaskMismatch (HashInterfaceHob);\r
+\r
   HashStart (&HashHandle);\r
   HashUpdate (HashHandle, DataToHash, DataToHashLen);\r
   Status = HashCompleteAndExtend (HashHandle, PcrIndex, NULL, 0, DigestList);\r
@@ -255,9 +332,7 @@ RegisterHashInterfaceLib (
 {\r
   UINTN              Index;\r
   HASH_INTERFACE_HOB *HashInterfaceHob;\r
-  HASH_INTERFACE_HOB LocalHashInterfaceHob;\r
   UINT32             HashMask;\r
-  UINT32             BiosSupportedHashMask;\r
   EFI_STATUS         Status;\r
 \r
   //\r
@@ -268,10 +343,9 @@ RegisterHashInterfaceLib (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  HashInterfaceHob = InternalGetHashInterface ();\r
+  HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);\r
   if (HashInterfaceHob == NULL) {\r
-    ZeroMem (&LocalHashInterfaceHob, sizeof(LocalHashInterfaceHob));\r
-    HashInterfaceHob = BuildGuidDataHob (&mHashLibPeiRouterGuid, &LocalHashInterfaceHob, sizeof(LocalHashInterfaceHob));\r
+    HashInterfaceHob = InternalCreateHashInterfaceHob (&gEfiCallerIdGuid);\r
     if (HashInterfaceHob == NULL) {\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
@@ -280,26 +354,84 @@ RegisterHashInterfaceLib (
   if (HashInterfaceHob->HashInterfaceCount >= HASH_COUNT) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
-  BiosSupportedHashMask = PcdGet32 (PcdTcg2HashAlgorithmBitmap);\r
-  Status = PcdSet32S (PcdTcg2HashAlgorithmBitmap, BiosSupportedHashMask | HashMask);\r
-  ASSERT_EFI_ERROR (Status);\r
 \r
   //\r
   // Check duplication\r
   //\r
   for (Index = 0; Index < HashInterfaceHob->HashInterfaceCount; Index++) {\r
     if (CompareGuid (&HashInterfaceHob->HashInterface[Index].HashGuid, &HashInterface->HashGuid)) {\r
-      //\r
-      // In PEI phase, there will be shadow driver dispatched again.\r
-      //\r
-      DEBUG ((EFI_D_INFO, "RegisterHashInterfaceLib - Override\n"));\r
-      CopyMem (&HashInterfaceHob->HashInterface[Index], HashInterface, sizeof(*HashInterface));\r
-      return EFI_SUCCESS;\r
+      DEBUG ((DEBUG_ERROR, "Hash Interface (%g) has been registered\n", &HashInterface->HashGuid));\r
+      return EFI_ALREADY_STARTED;\r
     }\r
   }\r
 \r
+  //\r
+  // Record hash algorithm bitmap of CURRENT module which consumes HashLib.\r
+  //\r
+  HashInterfaceHob->SupportedHashMask = PcdGet32 (PcdTcg2HashAlgorithmBitmap) | HashMask;\r
+  Status = PcdSet32S (PcdTcg2HashAlgorithmBitmap, HashInterfaceHob->SupportedHashMask);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
   CopyMem (&HashInterfaceHob->HashInterface[HashInterfaceHob->HashInterfaceCount], HashInterface, sizeof(*HashInterface));\r
   HashInterfaceHob->HashInterfaceCount ++;\r
   \r
   return EFI_SUCCESS;\r
-}
\ No newline at end of file
+}\r
+\r
+/**\r
+  The constructor function of HashLibBaseCryptoRouterPei.\r
+\r
+  @param  FileHandle   The handle of FFS header the loaded driver.\r
+  @param  PeiServices  The pointer to the PEI services.\r
+\r
+  @retval EFI_SUCCESS           The constructor executes successfully.\r
+  @retval EFI_OUT_OF_RESOURCES  There is no enough resource for the constructor.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+HashLibBaseCryptoRouterPeiConstructor (\r
+  IN EFI_PEI_FILE_HANDLE        FileHandle,\r
+  IN CONST EFI_PEI_SERVICES     **PeiServices\r
+  )\r
+{\r
+  EFI_STATUS            Status;\r
+  HASH_INTERFACE_HOB    *HashInterfaceHob;\r
+\r
+  HashInterfaceHob = InternalGetHashInterfaceHob (&gZeroGuid);\r
+  if (HashInterfaceHob == NULL) {\r
+    //\r
+    // No HOB with gZeroGuid Identifier has been created,\r
+    // this is FIRST module which consumes HashLib.\r
+    // Create the HOB with gZeroGuid Identifier.\r
+    //\r
+    HashInterfaceHob = InternalCreateHashInterfaceHob (&gZeroGuid);\r
+    if (HashInterfaceHob == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+  } else {\r
+    //\r
+    // Record hash algorithm bitmap of LAST module which also consumes HashLib.\r
+    //\r
+    HashInterfaceHob->SupportedHashMask = PcdGet32 (PcdTcg2HashAlgorithmBitmap);\r
+  }\r
+\r
+  HashInterfaceHob = InternalGetHashInterfaceHob (&gEfiCallerIdGuid);\r
+  if (HashInterfaceHob != NULL) {\r
+    //\r
+    // In PEI phase, some modules may call RegisterForShadow and will be\r
+    // shadowed and executed again after memory is discovered.\r
+    // This is the second execution of this module, clear the hash interface\r
+    // information registered at its first execution.\r
+    //\r
+    ZeroMem (&HashInterfaceHob->HashInterface, sizeof (*HashInterfaceHob) - sizeof (EFI_GUID));\r
+  }\r
+\r
+  //\r
+  // Set PcdTcg2HashAlgorithmBitmap to 0 in CONSTRUCTOR for CURRENT module.\r
+  //\r
+  Status = PcdSet32S (PcdTcg2HashAlgorithmBitmap, 0);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return EFI_SUCCESS;\r
+}\r