]> git.proxmox.com Git - mirror_edk2.git/commitdiff
UefiCpuPkg: Extend measurement of microcode patches to TPM
authorYang, Longlong <longlong.yang@intel.com>
Tue, 14 Dec 2021 07:18:55 +0000 (15:18 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Fri, 24 Dec 2021 09:08:20 +0000 (09:08 +0000)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3683

TCG specification says BIOS should extend measurement of microcode to TPM.
However, reference BIOS is not doing this. BIOS shall extend measurement of
microcode to TPM.

Cc: Eric Dong <eric.dong@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Min M Xu <min.m.xu@intel.com>
Cc: Qi Zhang <qi1.zhang@intel.com>
Signed-off-by: Longlong Yang <longlong.yang@intel.com>
UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxe.c [new file with mode: 0644]
UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxe.inf [new file with mode: 0644]
UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxe.uni [new file with mode: 0644]
UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxeExtra.uni [new file with mode: 0644]
UefiCpuPkg/UefiCpuPkg.dsc

diff --git a/UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxe.c b/UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxe.c
new file mode 100644 (file)
index 0000000..762ca15
--- /dev/null
@@ -0,0 +1,281 @@
+/** @file\r
+  This driver measures microcode patches to TPM.\r
+\r
+  This driver consumes gEdkiiMicrocodePatchHobGuid, packs all unique microcode patch found in gEdkiiMicrocodePatchHobGuid to a binary blob, and measures the binary blob to TPM.\r
+\r
+  Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include <IndustryStandard/UefiTcgPlatform.h>\r
+#include <Guid/EventGroup.h>\r
+#include <Guid/MicrocodePatchHob.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiDriverEntryPoint.h>\r
+#include <Library/UefiLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/HobLib.h>\r
+#include <Library/MicrocodeLib.h>\r
+#include <Library/TpmMeasurementLib.h>\r
+\r
+#define CPU_MICROCODE_MEASUREMENT_DESCRIPTION                "Microcode Measurement"\r
+#define CPU_MICROCODE_MEASUREMENT_EVENT_LOG_DESCRIPTION_LEN  sizeof (CPU_MICROCODE_MEASUREMENT_DESCRIPTION)\r
+\r
+#pragma pack(1)\r
+typedef struct {\r
+  UINT8    Description[CPU_MICROCODE_MEASUREMENT_EVENT_LOG_DESCRIPTION_LEN];\r
+  UINTN    NumberOfMicrocodePatchesMeasured;\r
+  UINTN    SizeOfMicrocodePatchesMeasured;\r
+} CPU_MICROCODE_MEASUREMENT_EVENT_LOG;\r
+#pragma pack()\r
+\r
+/**\r
+  Helping function.\r
+\r
+  The function is called by QuickSort to compare the order of offsets of\r
+  two microcode patches in RAM relative to their base address. Elements\r
+  will be in ascending order.\r
+\r
+  @param[in] Offset1   The pointer to the offset of first microcode patch.\r
+  @param[in] Offset2   The pointer to the offset of second microcode patch.\r
+\r
+  @retval 1                   The offset of first microcode patch is bigger than that of the second.\r
+  @retval -1                  The offset of first microcode patch is smaller than that of the second.\r
+  @retval 0                   The offset of first microcode patch equals to that of the second.\r
+**/\r
+INTN\r
+EFIAPI\r
+MicrocodePatchOffsetCompareFunction (\r
+  IN CONST VOID  *Offset1,\r
+  IN CONST VOID  *Offset2\r
+  )\r
+{\r
+  if (*(UINT64 *)(Offset1) > *(UINT64 *)(Offset2)) {\r
+    return 1;\r
+  } else if (*(UINT64 *)(Offset1) < *(UINT64 *)(Offset2)) {\r
+    return -1;\r
+  } else {\r
+    return 0;\r
+  }\r
+}\r
+\r
+/**\r
+  This function remove duplicate and invalid offsets in Offsets.\r
+\r
+  This function remove duplicate and invalid offsets in Offsets. Invalid offset means MAX_UINT64 in Offsets.\r
+\r
+  @param[in] Offsets        Microcode offset list.\r
+  @param[in, out] Count          On call as the count of raw microcode offset list; On return as count of the clean microcode offset list.\r
+  **/\r
+VOID\r
+RemoveDuplicateAndInvalidOffset (\r
+  IN     UINT64  *Offsets,\r
+  IN OUT UINTN   *Count\r
+  )\r
+{\r
+  UINTN   Index;\r
+  UINTN   NewCount;\r
+  UINT64  LastOffset;\r
+  UINT64  QuickSortBuffer;\r
+\r
+  //\r
+  // The order matters when packing all applied microcode patches to a single binary blob.\r
+  // Therefore it is a must to do sorting before packing.\r
+  // NOTE: Since microcode patches are sorted by their addresses in memory, the order of\r
+  // addresses in memory of all the microcode patches before sorting is required to be the\r
+  // same in every boot flow. If any future updates made this assumption untenable, then\r
+  // there needs a new solution to measure microcode patches.\r
+  //\r
+  QuickSort (\r
+    Offsets,\r
+    *Count,\r
+    sizeof (UINT64),\r
+    MicrocodePatchOffsetCompareFunction,\r
+    (VOID *)&QuickSortBuffer\r
+    );\r
+\r
+  NewCount   = 0;\r
+  LastOffset = MAX_UINT64;\r
+  for (Index = 0; Index < *Count; Index++) {\r
+    //\r
+    // When MAX_UINT64 element is met, all following elements are MAX_UINT64.\r
+    //\r
+    if (Offsets[Index] == MAX_UINT64) {\r
+      break;\r
+    }\r
+\r
+    //\r
+    // Remove duplicated offsets\r
+    //\r
+    if (Offsets[Index] != LastOffset) {\r
+      LastOffset        = Offsets[Index];\r
+      Offsets[NewCount] = Offsets[Index];\r
+      NewCount++;\r
+    }\r
+  }\r
+\r
+  *Count = NewCount;\r
+}\r
+\r
+/**\r
+  Callback function.\r
+\r
+  Called after signaling of the Ready to Boot Event. Measure microcode patches binary blob with event type EV_CPU_MICROCODE to PCR[1] in TPM.\r
+\r
+  @param[in] Event      Event whose notification function is being invoked.\r
+  @param[in] Context    Pointer to the notification function's context.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+MeasureMicrocodePatches (\r
+  IN      EFI_EVENT  Event,\r
+  IN      VOID       *Context\r
+  )\r
+{\r
+  EFI_STATUS                           Status;\r
+  UINT32                               PCRIndex;\r
+  UINT32                               EventType;\r
+  CPU_MICROCODE_MEASUREMENT_EVENT_LOG  EventLog;\r
+  UINT32                               EventLogSize;\r
+  EFI_HOB_GUID_TYPE                    *GuidHob;\r
+  EDKII_MICROCODE_PATCH_HOB            *MicrocodePatchHob;\r
+  UINT64                               *Offsets;\r
+  UINTN                                Count;\r
+  UINTN                                Index;\r
+  UINTN                                TotalMicrocodeSize;\r
+  UINT8                                *MicrocodePatchesBlob;\r
+\r
+  PCRIndex  = 1;\r
+  EventType = EV_CPU_MICROCODE;\r
+  AsciiStrCpyS (\r
+    (CHAR8 *)(EventLog.Description),\r
+    CPU_MICROCODE_MEASUREMENT_EVENT_LOG_DESCRIPTION_LEN,\r
+    CPU_MICROCODE_MEASUREMENT_DESCRIPTION\r
+    );\r
+  EventLog.NumberOfMicrocodePatchesMeasured = 0;\r
+  EventLog.SizeOfMicrocodePatchesMeasured   = 0;\r
+  EventLogSize                              = sizeof (CPU_MICROCODE_MEASUREMENT_EVENT_LOG);\r
+  Offsets                                   = NULL;\r
+  TotalMicrocodeSize                        = 0;\r
+  Count                                     = 0;\r
+\r
+  GuidHob = GetFirstGuidHob (&gEdkiiMicrocodePatchHobGuid);\r
+  if (NULL == GuidHob) {\r
+    DEBUG ((DEBUG_ERROR, "ERROR: GetFirstGuidHob (&gEdkiiMicrocodePatchHobGuid) failed.\n"));\r
+    return;\r
+  }\r
+\r
+  MicrocodePatchHob = GET_GUID_HOB_DATA (GuidHob);\r
+  DEBUG (\r
+    (DEBUG_INFO,\r
+     "INFO: Got MicrocodePatchHob with microcode patches starting address:0x%x, microcode patches region size:0x%x, processor count:0x%x\n",\r
+     MicrocodePatchHob->MicrocodePatchAddress, MicrocodePatchHob->MicrocodePatchRegionSize,\r
+     MicrocodePatchHob->ProcessorCount)\r
+    );\r
+\r
+  Offsets = AllocateCopyPool (\r
+              MicrocodePatchHob->ProcessorCount * sizeof (UINT64),\r
+              MicrocodePatchHob->ProcessorSpecificPatchOffset\r
+              );\r
+  Count = MicrocodePatchHob->ProcessorCount;\r
+\r
+  RemoveDuplicateAndInvalidOffset (Offsets, &Count);\r
+\r
+  if (0 == Count) {\r
+    DEBUG ((DEBUG_INFO, "INFO: No microcode patch is ever applied, skip the measurement of microcode!\n"));\r
+    FreePool (Offsets);\r
+    return;\r
+  }\r
+\r
+  for (Index = 0; Index < Count; Index++) {\r
+    TotalMicrocodeSize +=\r
+      GetMicrocodeLength ((CPU_MICROCODE_HEADER *)((UINTN)(MicrocodePatchHob->MicrocodePatchAddress + Offsets[Index])));\r
+  }\r
+\r
+  EventLog.NumberOfMicrocodePatchesMeasured = Count;\r
+  EventLog.SizeOfMicrocodePatchesMeasured   = TotalMicrocodeSize;\r
+\r
+  MicrocodePatchesBlob = AllocateZeroPool (TotalMicrocodeSize);\r
+  if (NULL == MicrocodePatchesBlob) {\r
+    DEBUG ((DEBUG_ERROR, "ERROR: AllocateZeroPool to MicrocodePatchesBlob failed!\n"));\r
+    FreePool (Offsets);\r
+    return;\r
+  }\r
+\r
+  TotalMicrocodeSize = 0;\r
+  for (Index = 0; Index < Count; Index++) {\r
+    CopyMem (\r
+      (VOID *)(MicrocodePatchesBlob + TotalMicrocodeSize),\r
+      (VOID *)((UINTN)(MicrocodePatchHob->MicrocodePatchAddress + Offsets[Index])),\r
+      (UINTN)(GetMicrocodeLength (\r
+                (CPU_MICROCODE_HEADER *)((UINTN)(MicrocodePatchHob->MicrocodePatchAddress +\r
+                                                 Offsets[Index]))\r
+                ))\r
+      );\r
+    TotalMicrocodeSize +=\r
+      GetMicrocodeLength ((CPU_MICROCODE_HEADER *)((UINTN)(MicrocodePatchHob->MicrocodePatchAddress + Offsets[Index])));\r
+  }\r
+\r
+  Status = TpmMeasureAndLogData (\r
+             PCRIndex,                                 // PCRIndex\r
+             EventType,                                // EventType\r
+             &EventLog,                                // EventLog\r
+             EventLogSize,                             // LogLen\r
+             MicrocodePatchesBlob,                     // HashData\r
+             TotalMicrocodeSize                        // HashDataLen\r
+             );\r
+  if (!EFI_ERROR (Status)) {\r
+    gBS->CloseEvent (Event);\r
+    DEBUG (\r
+      (DEBUG_INFO,\r
+       "INFO: %d Microcode patches are successfully extended to TPM! The total size measured to TPM is 0x%x\n",\r
+       Count,\r
+       TotalMicrocodeSize)\r
+      );\r
+  } else {\r
+    DEBUG ((DEBUG_ERROR, "ERROR: TpmMeasureAndLogData failed with status %a!\n", Status));\r
+  }\r
+\r
+  FreePool (Offsets);\r
+  FreePool (MicrocodePatchesBlob);\r
+  return;\r
+}\r
+\r
+/**\r
+\r
+  Driver to produce microcode measurement.\r
+\r
+  Driver to produce microcode measurement. Which install a callback function on ready to boot event.\r
+\r
+  @param ImageHandle     Module's image handle\r
+  @param SystemTable     Pointer of EFI_SYSTEM_TABLE\r
+\r
+  @return EFI_SUCCESS     This function always complete successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+MicrocodeMeasurementDriverEntryPoint (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  EFI_EVENT  Event;\r
+\r
+  //\r
+  // Measure Microcode patches\r
+  //\r
+  EfiCreateEventReadyToBootEx (\r
+    TPL_CALLBACK,\r
+    MeasureMicrocodePatches,\r
+    NULL,\r
+    &Event\r
+    );\r
+\r
+  return EFI_SUCCESS;\r
+}\r
diff --git a/UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxe.inf b/UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxe.inf
new file mode 100644 (file)
index 0000000..649fb94
--- /dev/null
@@ -0,0 +1,56 @@
+## @file\r
+#  This driver measures microcode patches to TPM.\r
+#\r
+#  This driver consumes gEdkiiMicrocodePatchHobGuid, packs all unique\r
+#  microcode patch found in gEdkiiMicrocodePatchHobGuid to a binary blob,\r
+#  and measures the binary blob to TPM.\r
+#\r
+#  Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+#\r
+#  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = MicrocodeMeasurementDxe\r
+  MODULE_UNI_FILE                = MicrocodeMeasurementDxe.uni\r
+  FILE_GUID                      = 0A32A803-ACDF-4C89-8293-91011548CD91\r
+  MODULE_TYPE                    = DXE_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  ENTRY_POINT                    = MicrocodeMeasurementDriverEntryPoint\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  MicrocodeMeasurementDxe.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+  UefiCpuPkg/UefiCpuPkg.dec\r
+\r
+[LibraryClasses]\r
+  UefiBootServicesTableLib\r
+  MemoryAllocationLib\r
+  BaseMemoryLib\r
+  BaseLib\r
+  UefiLib\r
+  UefiDriverEntryPoint\r
+  DebugLib\r
+  HobLib\r
+  MicrocodeLib\r
+  TpmMeasurementLib\r
+\r
+[Guids]\r
+  gEdkiiMicrocodePatchHobGuid           ## CONSUMES ## HOB\r
+\r
+[UserExtensions.TianoCore."ExtraFiles"]\r
+  MicrocodeMeasurementDxeExtra.uni\r
+\r
+[Depex]\r
+  TRUE\r
diff --git a/UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxe.uni b/UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxe.uni
new file mode 100644 (file)
index 0000000..5a21e95
--- /dev/null
@@ -0,0 +1,15 @@
+// /** @file\r
+// This driver measures microcode patches to TPM.\r
+//\r
+// This driver consumes gEdkiiMicrocodePatchHobGuid, packs all uniquemicrocode patch found in gEdkiiMicrocodePatchHobGuid to a binary blob, and measures the binary blob to TPM.\r
+//\r
+// Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+//\r
+// SPDX-License-Identifier: BSD-2-Clause-Patent\r
+//\r
+// **/\r
+\r
+\r
+#string STR_MODULE_ABSTRACT             #language en-US "This driver measures Microcode Patches to TPM."\r
+\r
+#string STR_MODULE_DESCRIPTION          #language en-US "This driver consumes gEdkiiMicrocodePatchHobGuid, packs all microcode patch found in gEdkiiMicrocodePatchHobGuid to a binary blob, and measure the binary blob to TPM."\r
diff --git a/UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxeExtra.uni b/UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxeExtra.uni
new file mode 100644 (file)
index 0000000..6990cee
--- /dev/null
@@ -0,0 +1,12 @@
+// /** @file\r
+// MicrocodeMeasurementDxe Localized Strings and Content\r
+//\r
+// Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+//\r
+// SPDX-License-Identifier: BSD-2-Clause-Patent\r
+//\r
+// **/\r
+\r
+#string STR_PROPERTIES_MODULE_NAME\r
+#language en-US\r
+"Microcode Patches Measurement DXE Driver"\r
index 870b45284087864df09a27687a8724ad92487bc0..d1d61dd6a03b3f64926749b4418669e62d52cc05 100644 (file)
   UefiCpuPkg/Library/CpuTimerLib/BaseCpuTimerLib.inf\r
   UefiCpuPkg/Library/CpuCacheInfoLib/PeiCpuCacheInfoLib.inf\r
   UefiCpuPkg/Library/CpuCacheInfoLib/DxeCpuCacheInfoLib.inf\r
+  UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxe.inf\r
 \r
 [Components.IA32, Components.X64]\r
   UefiCpuPkg/CpuDxe/CpuDxe.inf\r