]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Tcg/Tcg2Smm/Tcg2Smm.c
SecurityPkg: TcgSmm: Handle invalid parameter in MOR SMI handler
[mirror_edk2.git] / SecurityPkg / Tcg / Tcg2Smm / Tcg2Smm.c
index e3938cb8ece02b9f2196e196da45f28c506e4980..4a1a293bfcd3dda32f46c7c61e1772db3dd69a75 100644 (file)
   PhysicalPresenceCallback() and MemoryClearCallback() will receive untrusted input and do some check.\r
 \r
 Copyright (c) 2015 - 2018, 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
+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
 http://opensource.org/licenses/bsd-license.php\r
 \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
 **/\r
 \r
 #include "Tcg2Smm.h"\r
 \r
-typedef enum {\r
-  PtpInterfaceTis,\r
-  PtpInterfaceFifo,\r
-  PtpInterfaceCrb,\r
-  PtpInterfaceMax,\r
-} PTP_INTERFACE_TYPE;\r
-\r
-/**\r
-  Return PTP interface type.\r
-\r
-  @param[in] Register                Pointer to PTP register.\r
-\r
-  @return PTP interface type.\r
-**/\r
-PTP_INTERFACE_TYPE\r
-GetPtpInterface (\r
-  IN VOID *Register\r
-  )\r
-{\r
-  PTP_CRB_INTERFACE_IDENTIFIER  InterfaceId;\r
-  PTP_FIFO_INTERFACE_CAPABILITY InterfaceCapability;\r
-\r
-  //\r
-  // Check interface id\r
-  //\r
-  InterfaceId.Uint32 = MmioRead32 ((UINTN)&((PTP_CRB_REGISTERS *)Register)->InterfaceId);\r
-  InterfaceCapability.Uint32 = MmioRead32 ((UINTN)&((PTP_FIFO_REGISTERS *)Register)->InterfaceCapability);\r
-\r
-  if (InterfaceId.Bits.InterfaceType == PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_TIS) {\r
-    return PtpInterfaceTis;\r
-  }\r
-\r
-  if ((InterfaceId.Bits.InterfaceType == PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_CRB) &&\r
-      (InterfaceId.Bits.InterfaceVersion == PTP_INTERFACE_IDENTIFIER_INTERFACE_VERSION_CRB) &&\r
-      (InterfaceId.Bits.CapCRB != 0)) {\r
-    return PtpInterfaceCrb;\r
-  }\r
-\r
-  if ((InterfaceId.Bits.InterfaceType == PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_FIFO) &&\r
-      (InterfaceId.Bits.InterfaceVersion == PTP_INTERFACE_IDENTIFIER_INTERFACE_VERSION_FIFO) &&\r
-      (InterfaceId.Bits.CapFIFO != 0) &&\r
-      (InterfaceCapability.Bits.InterfaceVersion == INTERFACE_CAPABILITY_INTERFACE_VERSION_PTP)) {\r
-    return PtpInterfaceFifo;\r
-  }\r
-\r
-  //\r
-  // No Ptp interface available\r
-  //\r
-  return PtpInterfaceMax;\r
-}\r
 \r
 EFI_TPM2_ACPI_TABLE  mTpm2AcpiTemplate = {\r
   {\r
@@ -132,7 +82,7 @@ PhysicalPresenceCallback (
     mTcgNvs->PhysicalPresence.LastRequest = MostRecentRequest;\r
     mTcgNvs->PhysicalPresence.Response = Response;\r
     return EFI_SUCCESS;\r
-  } else if ((mTcgNvs->PhysicalPresence.Parameter == TCG_ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS) \r
+  } else if ((mTcgNvs->PhysicalPresence.Parameter == TCG_ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS)\r
           || (mTcgNvs->PhysicalPresence.Parameter == TCG_ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS_2)) {\r
 \r
     OperationRequest = mTcgNvs->PhysicalPresence.Request;\r
@@ -203,6 +153,10 @@ MemoryClearCallback (
       return EFI_SUCCESS;\r
     }\r
     MorControl &= ~MOR_CLEAR_MEMORY_BIT_MASK;\r
+  } else {\r
+    mTcgNvs->MemoryClear.ReturnCode = MOR_REQUEST_GENERAL_FAILURE;\r
+    DEBUG ((EFI_D_ERROR, "[TPM] MOR Parameter error! Parameter = %x\n", mTcgNvs->MemoryClear.Parameter));\r
+    return EFI_SUCCESS;\r
   }\r
 \r
   DataSize = sizeof (UINT8);\r
@@ -213,7 +167,7 @@ MemoryClearCallback (
                            DataSize,\r
                            &MorControl\r
                            );\r
-  if (EFI_ERROR (Status)) { \r
+  if (EFI_ERROR (Status)) {\r
     mTcgNvs->MemoryClear.ReturnCode = MOR_REQUEST_GENERAL_FAILURE;\r
     DEBUG ((EFI_D_ERROR, "[TPM] Set MOR variable failure! Status = %r\n", Status));\r
   }\r
@@ -251,7 +205,7 @@ AssignOpRegion (
   for (OpRegion  = (AML_OP_REGION_32_8 *) (Table + 1);\r
        OpRegion <= (AML_OP_REGION_32_8 *) ((UINT8 *) Table + Table->Length);\r
        OpRegion  = (AML_OP_REGION_32_8 *) ((UINT8 *) OpRegion + 1)) {\r
-    if ((OpRegion->OpRegionOp  == AML_EXT_REGION_OP) && \r
+    if ((OpRegion->OpRegionOp  == AML_EXT_REGION_OP) &&\r
         (OpRegion->NameString  == Name) &&\r
         (OpRegion->DWordPrefix == AML_DWORD_PREFIX) &&\r
         (OpRegion->BytePrefix  == AML_BYTE_PREFIX)) {\r
@@ -269,7 +223,7 @@ AssignOpRegion (
 }\r
 \r
 /**\r
-  Patch version string of Physical Presence interface supported by platform. The initial string tag in TPM \r
+  Patch version string of Physical Presence interface supported by platform. The initial string tag in TPM\r
 ACPI table is "$PV".\r
 \r
   @param[in, out] Table          The TPM item in ACPI table.\r
@@ -310,15 +264,17 @@ UpdatePPVersion (
   @param[in, out] Table            The TPM item in ACPI table.\r
   @param[in]      IrqBuffer        Input new IRQ buffer.\r
   @param[in]      IrqBuffserSize   Input new IRQ buffer size.\r
+  @param[out]     IsShortFormPkgLength   If _PRS returns Short length Package(ACPI spec 20.2.4).\r
 \r
   @return                          patch status.\r
 \r
 **/\r
 EFI_STATUS\r
 UpdatePossibleResource (\r
-  EFI_ACPI_DESCRIPTION_HEADER    *Table,\r
-  UINT32                         *IrqBuffer,\r
-  UINT32                         IrqBuffserSize\r
+  IN OUT  EFI_ACPI_DESCRIPTION_HEADER    *Table,\r
+  IN      UINT32                         *IrqBuffer,\r
+  IN      UINT32                         IrqBuffserSize,\r
+  OUT     BOOLEAN                        *IsShortFormPkgLength\r
   )\r
 {\r
   UINT8       *DataPtr;\r
@@ -431,7 +387,7 @@ UpdatePossibleResource (
       //\r
       // Notify _PRS to report short formed ResourceTemplate\r
       //\r
-      mTcgNvs->IsShortFormPkgLength = TRUE;\r
+      *IsShortFormPkgLength = TRUE;\r
 \r
       break;\r
     }\r
@@ -503,7 +459,7 @@ UpdatePossibleResource (
         //\r
         // Notify _PRS to report long formed ResourceTemplate\r
         //\r
-        mTcgNvs->IsShortFormPkgLength = FALSE;\r
+        *IsShortFormPkgLength = FALSE;\r
         break;\r
       }\r
     }\r
@@ -539,10 +495,11 @@ UpdatePossibleResource (
   *(DataPtr + 1) = 0;\r
 \r
   //\r
-  // 5. Jump over whole ResourceTemplate. Stuff rest bytes to NOOP\r
+  // 5. Jump over new ResourceTemplate. Stuff rest bytes to NOOP\r
   //\r
-  for (DataPtr += 2; DataPtr < DataEndPtr; DataPtr++) {\r
-    *DataPtr = AML_NOOP_OP;\r
+  DataPtr += 2;\r
+  if (DataPtr < DataEndPtr) {\r
+    SetMem(DataPtr, (UINTN)DataEndPtr - (UINTN)DataPtr, AML_NOOP_OP);\r
   }\r
 \r
   return EFI_SUCCESS;\r
@@ -583,7 +540,7 @@ UpdateHID (
   if (!EFI_ERROR(Status)) {\r
     DEBUG((EFI_D_INFO, "TPM_PT_MANUFACTURER 0x%08x\n", ManufacturerID));\r
     //\r
-    // ManufacturerID defined in TCG Vendor ID Registry \r
+    // ManufacturerID defined in TCG Vendor ID Registry\r
     // may tailed with 0x00 or 0x20\r
     //\r
     if ((ManufacturerID >> 24) == 0x00 || ((ManufacturerID >> 24) == 0x20)) {\r
@@ -618,7 +575,7 @@ UpdateHID (
     } else {\r
       AsciiSPrint(Hid + 4, TPM_HID_ACPI_SIZE - 4, "%02d%02d", ((FirmwareVersion1 & 0xFFFF0000) >> 16), (FirmwareVersion1 & 0x0000FFFF));\r
     }\r
-    \r
+\r
   } else {\r
     DEBUG ((EFI_D_ERROR, "Get TPM_PT_FIRMWARE_VERSION_X failed %x!\n", Status));\r
     ASSERT(FALSE);\r
@@ -671,6 +628,9 @@ PublishAcpiTable (
   UINTN                          TableSize;\r
   UINT32                         *PossibleIrqNumBuf;\r
   UINT32                         PossibleIrqNumBufSize;\r
+  BOOLEAN                        IsShortFormPkgLength;\r
+\r
+  IsShortFormPkgLength = FALSE;\r
 \r
   Status = GetSectionFromFv (\r
              &gEfiCallerIdGuid,\r
@@ -709,7 +669,7 @@ PublishAcpiTable (
     PossibleIrqNumBufSize = (UINT32)PcdGetSize(PcdTpm2PossibleIrqNumBuf);\r
 \r
     if (PossibleIrqNumBufSize <= MAX_PRS_INT_BUF_SIZE && (PossibleIrqNumBufSize % sizeof(UINT32)) == 0) {\r
-      Status = UpdatePossibleResource(Table, PossibleIrqNumBuf, PossibleIrqNumBufSize);\r
+      Status = UpdatePossibleResource(Table, PossibleIrqNumBuf, PossibleIrqNumBufSize, &IsShortFormPkgLength);\r
       DEBUG ((\r
         DEBUG_INFO,\r
         "UpdatePossibleResource status - %x. TPM2 service may not ready in OS.\n",\r
@@ -742,7 +702,7 @@ PublishAcpiTable (
   mTcgNvs = AssignOpRegion (Table, SIGNATURE_32 ('T', 'N', 'V', 'S'), (UINT16) sizeof (TCG_NVS));\r
   ASSERT (mTcgNvs != NULL);\r
   mTcgNvs->TpmIrqNum            = PcdGet32(PcdTpm2CurrentIrqNum);\r
-  mTcgNvs->IsShortFormPkgLength = FALSE;\r
+  mTcgNvs->IsShortFormPkgLength = IsShortFormPkgLength;\r
 \r
   //\r
   // Publish the TPM ACPI table. Table is re-checksumed.\r
@@ -779,14 +739,14 @@ PublishTpm2 (
   UINTN                          TableKey;\r
   UINT64                         OemTableId;\r
   EFI_TPM2_ACPI_CONTROL_AREA     *ControlArea;\r
-  PTP_INTERFACE_TYPE             InterfaceType;\r
+  TPM2_PTP_INTERFACE_TYPE        InterfaceType;\r
 \r
   mTpm2AcpiTemplate.Header.Revision = PcdGet8(PcdTpm2AcpiTableRev);\r
   DEBUG((DEBUG_INFO, "Tpm2 ACPI table revision is %d\n", mTpm2AcpiTemplate.Header.Revision));\r
 \r
   //\r
   // PlatformClass is only valid for version 4 and above\r
-  //    BIT0~15:  PlatformClass \r
+  //    BIT0~15:  PlatformClass\r
   //    BIT16~31: Reserved\r
   //\r
   if (mTpm2AcpiTemplate.Header.Revision >= EFI_TPM2_ACPI_TABLE_REVISION_4) {\r
@@ -806,9 +766,9 @@ PublishTpm2 (
     sizeof(mTpm2AcpiTemplate)\r
     );\r
 \r
-  InterfaceType = GetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));\r
+  InterfaceType = PcdGet8(PcdActiveTpmInterfaceType);\r
   switch (InterfaceType) {\r
-  case PtpInterfaceCrb:\r
+  case Tpm2PtpInterfaceCrb:\r
     mTpm2AcpiTemplate.StartMethod = EFI_TPM2_ACPI_TABLE_START_METHOD_COMMAND_RESPONSE_BUFFER_INTERFACE;\r
     mTpm2AcpiTemplate.AddressOfControlArea = PcdGet64 (PcdTpmBaseAddress) + 0x40;\r
     ControlArea = (EFI_TPM2_ACPI_CONTROL_AREA *)(UINTN)mTpm2AcpiTemplate.AddressOfControlArea;\r
@@ -817,8 +777,8 @@ PublishTpm2 (
     ControlArea->Command      = PcdGet64 (PcdTpmBaseAddress) + 0x80;\r
     ControlArea->Response     = PcdGet64 (PcdTpmBaseAddress) + 0x80;\r
     break;\r
-  case PtpInterfaceFifo:\r
-  case PtpInterfaceTis:\r
+  case Tpm2PtpInterfaceFifo:\r
+  case Tpm2PtpInterfaceTis:\r
     break;\r
   default:\r
     DEBUG((EFI_D_ERROR, "TPM2 InterfaceType get error! %d\n", InterfaceType));\r
@@ -852,12 +812,12 @@ PublishTpm2 (
 /**\r
   The driver's entry point.\r
 \r
-  It install callbacks for TPM physical presence and MemoryClear, and locate \r
+  It install callbacks for TPM physical presence and MemoryClear, and locate\r
   SMM variable to be used in the callback function.\r
 \r
-  @param[in] ImageHandle  The firmware allocated handle for the EFI image.  \r
+  @param[in] ImageHandle  The firmware allocated handle for the EFI image.\r
   @param[in] SystemTable  A pointer to the EFI System Table.\r
-  \r
+\r
   @retval EFI_SUCCESS     The entry point is executed successfully.\r
   @retval Others          Some error occurs when executing this entry point.\r
 \r
@@ -902,7 +862,7 @@ InitializeTcgSmm (
     return Status;\r
   }\r
   mTcgNvs->MemoryClear.SoftwareSmi = (UINT8) SwContext.SwSmiInputValue;\r
-  \r
+\r
   //\r
   // Locate SmmVariableProtocol.\r
   //\r