]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Tcg/TrEEPei/TrEEPei.c
Resolve buffer check overrun issue.
[mirror_edk2.git] / SecurityPkg / Tcg / TrEEPei / TrEEPei.c
index 5b5ff6c3aa0e8e8105c7ece1153c4dccce075a63..eea40847c5e648299fe5a27c9ed0d26afa4a5a20 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Initialize TPM2 device and measure FVs before handing off control to DXE.\r
 \r
-Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2013 - 2015, 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
@@ -16,6 +16,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include <IndustryStandard/UefiTcgPlatform.h>\r
 #include <Ppi/FirmwareVolumeInfo.h>\r
+#include <Ppi/FirmwareVolumeInfo2.h>\r
 #include <Ppi/LockPhysicalPresence.h>\r
 #include <Ppi/TpmInitialized.h>\r
 #include <Ppi/FirmwareVolume.h>\r
@@ -38,19 +39,18 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Library/PeiServicesTablePointerLib.h>\r
 #include <Protocol/TrEEProtocol.h>\r
 #include <Library/PerformanceLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/ReportStatusCodeLib.h>\r
 \r
 #define PERF_ID_TREE_PEI  0x3080\r
 \r
 typedef struct {\r
   EFI_GUID               *EventGuid;\r
   TREE_EVENT_LOG_FORMAT  LogFormat;\r
-  UINT32                 BootHashAlg;\r
-  UINT16                 DigestAlgID;\r
-  TPMI_ALG_HASH          TpmHashAlgo;\r
 } TREE_EVENT_INFO_STRUCT;\r
 \r
 TREE_EVENT_INFO_STRUCT mTreeEventInfo[] = {\r
-  {&gTcgEventEntryHobGuid,             TREE_EVENT_LOG_FORMAT_TCG_1_2,      TREE_BOOT_HASH_ALG_SHA1,     0,                       TPM_ALG_SHA1},\r
+  {&gTcgEventEntryHobGuid,             TREE_EVENT_LOG_FORMAT_TCG_1_2},\r
 };\r
 \r
 BOOLEAN                 mImageInMemory  = FALSE;\r
@@ -62,10 +62,10 @@ EFI_PEI_PPI_DESCRIPTOR  mTpmInitializedPpiList = {
   NULL\r
 };\r
 \r
-EFI_PLATFORM_FIRMWARE_BLOB mMeasuredBaseFvInfo[FixedPcdGet32 (PcdPeiCoreMaxFvSupported)];\r
+EFI_PLATFORM_FIRMWARE_BLOB *mMeasuredBaseFvInfo;\r
 UINT32 mMeasuredBaseFvIndex = 0;\r
 \r
-EFI_PLATFORM_FIRMWARE_BLOB mMeasuredChildFvInfo[FixedPcdGet32 (PcdPeiCoreMaxFvSupported)];\r
+EFI_PLATFORM_FIRMWARE_BLOB *mMeasuredChildFvInfo;\r
 UINT32 mMeasuredChildFvIndex = 0;\r
 \r
 /**\r
@@ -112,6 +112,11 @@ EFI_PEI_NOTIFY_DESCRIPTOR           mNotifyList[] = {
     &gEfiPeiFirmwareVolumeInfoPpiGuid,\r
     FirmwareVolmeInfoPpiNotifyCallback \r
   },\r
+  {\r
+    EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,\r
+    &gEfiPeiFirmwareVolumeInfo2PpiGuid,\r
+    FirmwareVolmeInfoPpiNotifyCallback \r
+  },\r
   {\r
     (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
     &gEfiEndOfPeiSignalPpiGuid,\r
@@ -121,28 +126,6 @@ EFI_PEI_NOTIFY_DESCRIPTOR           mNotifyList[] = {
 \r
 EFI_PEI_FIRMWARE_VOLUME_INFO_MEASUREMENT_EXCLUDED_PPI *mMeasurementExcludedFvPpi;\r
 \r
-/**\r
-  This function return hash algorithm from event log format.\r
-\r
-  @param[in]     EventLogFormat    Event log format.\r
-\r
-  @return hash algorithm.\r
-**/\r
-TPMI_ALG_HASH\r
-TrEEGetHashAlgoFromLogFormat (\r
-  IN      TREE_EVENT_LOG_FORMAT     EventLogFormat\r
-  )\r
-{\r
-  UINTN  Index;\r
-\r
-  for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {\r
-    if (mTreeEventInfo[Index].LogFormat == EventLogFormat) {\r
-      return mTreeEventInfo[Index].TpmHashAlgo;\r
-    }\r
-  }\r
-  return TPM_ALG_SHA1;\r
-}\r
-\r
 /**\r
   This function get digest from digest list.\r
 \r
@@ -311,6 +294,10 @@ HashLogExtendEvent (
   EFI_STATUS                        Status;\r
   TPML_DIGEST_VALUES                DigestList;\r
 \r
+  if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   Status = HashAndExtend (\r
              NewEventHdr->PCRIndex,\r
              HashData,\r
@@ -322,6 +309,16 @@ HashLogExtendEvent (
       Status = LogHashEvent (&DigestList, NewEventHdr, NewEventData);\r
     }\r
   }\r
+  \r
+  if (Status == EFI_DEVICE_ERROR) {\r
+    DEBUG ((EFI_D_ERROR, "HashLogExtendEvent - %r. Disable TPM.\n", Status));\r
+    BuildGuidHob (&gTpmErrorHobGuid,0);\r
+    REPORT_STATUS_CODE (\r
+      EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
+      (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)\r
+      );\r
+  }\r
+\r
   return Status;\r
 }\r
 \r
@@ -424,7 +421,6 @@ MeasureFvImage (
              &TcgEventHdr,\r
              (UINT8*) &FvBlob\r
              );\r
-  ASSERT_EFI_ERROR (Status);\r
 \r
   //\r
   // Add new FV into the measured FV list.\r
@@ -518,6 +514,7 @@ FirmwareVolmeInfoPpiNotifyCallback (
   EFI_PEI_FIRMWARE_VOLUME_INFO_PPI  *Fv;\r
   EFI_STATUS                        Status;\r
   EFI_PEI_FIRMWARE_VOLUME_PPI       *FvPpi;\r
+  UINTN                             Index;\r
 \r
   Fv = (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *) Ppi;\r
 \r
@@ -542,6 +539,14 @@ FirmwareVolmeInfoPpiNotifyCallback (
     \r
     ASSERT (mMeasuredChildFvIndex < FixedPcdGet32 (PcdPeiCoreMaxFvSupported));\r
     if (mMeasuredChildFvIndex < FixedPcdGet32 (PcdPeiCoreMaxFvSupported)) {\r
+      //\r
+      // Check whether FV is in the measured child FV list.\r
+      //\r
+      for (Index = 0; Index < mMeasuredChildFvIndex; Index++) {\r
+        if (mMeasuredChildFvInfo[Index].BlobBase == (EFI_PHYSICAL_ADDRESS) (UINTN) Fv->FvInfo) {\r
+          return EFI_SUCCESS;\r
+        }\r
+      }\r
       mMeasuredChildFvInfo[mMeasuredChildFvIndex].BlobBase   = (EFI_PHYSICAL_ADDRESS) (UINTN) Fv->FvInfo;\r
       mMeasuredChildFvInfo[mMeasuredChildFvIndex].BlobLength = Fv->FvInfoSize;\r
       mMeasuredChildFvIndex++;\r
@@ -577,9 +582,13 @@ PeimEntryMP (
                );\r
   // Do not check status, because it is optional\r
 \r
+  mMeasuredBaseFvInfo  = (EFI_PLATFORM_FIRMWARE_BLOB *) AllocateZeroPool (sizeof (EFI_PLATFORM_FIRMWARE_BLOB) * PcdGet32 (PcdPeiCoreMaxFvSupported));\r
+  ASSERT (mMeasuredBaseFvInfo != NULL);\r
+  mMeasuredChildFvInfo = (EFI_PLATFORM_FIRMWARE_BLOB *) AllocateZeroPool (sizeof (EFI_PLATFORM_FIRMWARE_BLOB) * PcdGet32 (PcdPeiCoreMaxFvSupported));\r
+  ASSERT (mMeasuredChildFvInfo != NULL);\r
+  \r
   if (PcdGet8 (PcdTpm2ScrtmPolicy) == 1) {\r
     Status = MeasureCRTMVersion ();\r
-    ASSERT_EFI_ERROR (Status);\r
   }\r
 \r
   Status = MeasureMainBios ();\r
@@ -620,10 +629,6 @@ PeimEntryMA (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  if (PcdGetBool (PcdHideTpmSupport) && PcdGetBool (PcdHideTpm)) {\r
-    return EFI_UNSUPPORTED;\r
-  }\r
-\r
   //\r
   // Update for Performance optimization\r
   //\r