]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Tcg/TcgSmm/TcgSmm.c
Fix the return status when physical presence variable and MemoryOverwriteRequestContr...
[mirror_edk2.git] / SecurityPkg / Tcg / TcgSmm / TcgSmm.c
index 9116944081656eacc04f62a32190f948f1c71b64..6080eff3b7b880346e0a2b37b0a0add1a4f5a348 100644 (file)
@@ -2,7 +2,13 @@
   It updates TPM items in ACPI table and registers SMI callback\r
   functions for physical presence and ClearMemory.\r
 \r
-Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+  Caution: This module requires additional review when modified.\r
+  This driver will have external input - variable and ACPINvs data in SMM mode.\r
+  This external input must be validated carefully to avoid security issue.\r
+\r
+  PhysicalPresenceCallback() and MemoryClearCallback() will receive untrusted input and do some check.\r
+\r
+Copyright (c) 2011 - 2014, 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
@@ -13,61 +19,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-\r
-#include <PiDxe.h>\r
-#include <IndustryStandard/Acpi.h>\r
-#include <Guid/PhysicalPresenceData.h>\r
-#include <Guid/MemoryOverwriteControl.h>\r
-#include <Protocol/SmmSwDispatch2.h>\r
-#include <Protocol/AcpiTable.h>\r
-#include <Protocol/SmmVariable.h>\r
-\r
-#include <Library/BaseLib.h>\r
-#include <Library/BaseMemoryLib.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/SmmServicesTableLib.h>\r
-#include <Library/UefiDriverEntryPoint.h>\r
-#include <Library/UefiBootServicesTableLib.h>\r
-#include <Library/DxeServicesLib.h>\r
-\r
-//\r
-// AML parsing definitions\r
-//\r
-#define AML_OPREGION_OP     0x80\r
-#define AML_BYTE_PREFIX     0x0A\r
-#define AML_DWORD_PREFIX    0x0C\r
-\r
-#pragma pack(1)\r
-typedef struct {\r
-  UINT8                  SoftwareSmi;\r
-  UINT32                 Parameter;\r
-  UINT32                 Response;\r
-  UINT32                 Request;\r
-  UINT32                 LastRequest;\r
-  UINT32                 ReturnCode;\r
-} PHYSICAL_PRESENCE_NVS;\r
-\r
-typedef struct {\r
-  UINT8                  SoftwareSmi;\r
-  UINT32                 Parameter;\r
-  UINT32                 Request;\r
-} MEMORY_CLEAR_NVS;\r
-\r
-typedef struct {\r
-  PHYSICAL_PRESENCE_NVS  PhysicalPresence;\r
-  MEMORY_CLEAR_NVS       MemoryClear;\r
-} TCG_NVS;\r
-\r
-typedef struct {\r
-  UINT8                  OpRegionOp;\r
-  UINT32                 NameString;\r
-  UINT8                  RegionSpace;\r
-  UINT8                  DWordPrefix;\r
-  UINT32                 RegionOffset;\r
-  UINT8                  BytePrefix;\r
-  UINT8                  RegionLen;\r
-} AML_OP_REGION_32_8;\r
-#pragma pack()\r
+#include "TcgSmm.h"\r
 \r
 EFI_SMM_VARIABLE_PROTOCOL  *mSmmVariable;\r
 TCG_NVS                    *mTcgNvs;\r
@@ -75,6 +27,10 @@ TCG_NVS                    *mTcgNvs;
 /**\r
   Software SMI callback for TPM physical presence which is called from ACPI method.\r
 \r
+  Caution: This function may receive untrusted input.\r
+  Variable and ACPINvs are external input, so this function will validate\r
+  its data structure to be valid value.\r
+\r
   @param[in]      DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().\r
   @param[in]      Context         Points to an optional handler context which was specified when the\r
                                   handler was registered.\r
@@ -112,27 +68,22 @@ PhysicalPresenceCallback (
                            &PpData\r
                            );\r
   if (EFI_ERROR (Status)) {\r
+    mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_GENERAL_FAILURE;\r
+    DEBUG ((EFI_D_ERROR, "[TPM] Get PP variable failure! Status = %r\n", Status));\r
     return EFI_SUCCESS;\r
   }\r
 \r
   DEBUG ((EFI_D_INFO, "[TPM] PP callback, Parameter = %x\n", mTcgNvs->PhysicalPresence.Parameter));\r
-  if (mTcgNvs->PhysicalPresence.Parameter == 5) {\r
-    //\r
-    // Return TPM Operation Response to OS Environment\r
-    //\r
+  if (mTcgNvs->PhysicalPresence.Parameter == ACPI_FUNCTION_RETURN_REQUEST_RESPONSE_TO_OS) {\r
     mTcgNvs->PhysicalPresence.LastRequest = PpData.LastPPRequest;\r
     mTcgNvs->PhysicalPresence.Response    = PpData.PPResponse;\r
-\r
-  } else if ((mTcgNvs->PhysicalPresence.Parameter == 2) || (mTcgNvs->PhysicalPresence.Parameter == 7)) {\r
-    //\r
-    // Submit TPM Operation Request to Pre-OS Environment\r
-    //\r
-\r
-    if (mTcgNvs->PhysicalPresence.Request == SET_OPERATOR_AUTH) {\r
+  } else if ((mTcgNvs->PhysicalPresence.Parameter == ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS) \r
+          || (mTcgNvs->PhysicalPresence.Parameter == ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS_2)) {\r
+    if (mTcgNvs->PhysicalPresence.Request == PHYSICAL_PRESENCE_SET_OPERATOR_AUTH) {\r
       //\r
-      // This command requires UI to prompt user for Auth data, NOT implemented.\r
+      // This command requires UI to prompt user for Auth data.\r
       //\r
-      mTcgNvs->PhysicalPresence.ReturnCode = 1;\r
+      mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_NOT_IMPLEMENTED;\r
       return EFI_SUCCESS;\r
     }\r
 \r
@@ -149,82 +100,85 @@ PhysicalPresenceCallback (
     }\r
 \r
     if (EFI_ERROR (Status)) { \r
-      //\r
-      // General failure.\r
-      //\r
-      mTcgNvs->PhysicalPresence.ReturnCode = 2;\r
+      mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_GENERAL_FAILURE;\r
       return EFI_SUCCESS;\r
     }\r
-    mTcgNvs->PhysicalPresence.ReturnCode = 0;\r
-  } else if (mTcgNvs->PhysicalPresence.Parameter == 8) {\r
-    // \r
-    // Get User Confirmation Status for Operation\r
+    mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_SUCCESS;\r
+  } else if (mTcgNvs->PhysicalPresence.Parameter == ACPI_FUNCTION_GET_USER_CONFIRMATION_STATUS_FOR_REQUEST) {\r
+    //\r
+    // Get the Physical Presence flags\r
     //\r
-    Flags = PpData.Flags;  \r
+    DataSize = sizeof (UINT8);\r
+    Status = mSmmVariable->SmmGetVariable (\r
+                             PHYSICAL_PRESENCE_FLAGS_VARIABLE,\r
+                             &gEfiPhysicalPresenceGuid,\r
+                             NULL,\r
+                             &DataSize,\r
+                             &Flags\r
+                             );\r
+    if (EFI_ERROR (Status)) {\r
+      mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_GENERAL_FAILURE;\r
+      DEBUG ((EFI_D_ERROR, "[TPM] Get PP flags failure! Status = %r\n", Status));\r
+      return EFI_SUCCESS;\r
+    }\r
+\r
     RequestConfirmed = FALSE;\r
 \r
     switch (mTcgNvs->PhysicalPresence.Request) {\r
-      case ENABLE:\r
-      case DISABLE:\r
-      case ACTIVATE:\r
-      case DEACTIVATE:\r
-      case ENABLE_ACTIVATE:\r
-      case DEACTIVATE_DISABLE:\r
-      case SET_OWNER_INSTALL_TRUE:\r
-      case SET_OWNER_INSTALL_FALSE:\r
-      case ENABLE_ACTIVATE_OWNER_TRUE:\r
-      case DEACTIVATE_DISABLE_OWNER_FALSE:\r
+      case PHYSICAL_PRESENCE_ENABLE:\r
+      case PHYSICAL_PRESENCE_DISABLE:\r
+      case PHYSICAL_PRESENCE_ACTIVATE:\r
+      case PHYSICAL_PRESENCE_DEACTIVATE:\r
+      case PHYSICAL_PRESENCE_ENABLE_ACTIVATE:\r
+      case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE:\r
+      case PHYSICAL_PRESENCE_SET_OWNER_INSTALL_TRUE:\r
+      case PHYSICAL_PRESENCE_SET_OWNER_INSTALL_FALSE:\r
+      case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_OWNER_TRUE:\r
+      case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE_OWNER_FALSE:\r
         if ((Flags & FLAG_NO_PPI_PROVISION) != 0) {\r
           RequestConfirmed = TRUE;\r
         }\r
         break;\r
 \r
-      case CLEAR:\r
-      case ENABLE_ACTIVATE_CLEAR:\r
+      case PHYSICAL_PRESENCE_CLEAR:\r
+      case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR:\r
         if ((Flags & FLAG_NO_PPI_CLEAR) != 0) {\r
           RequestConfirmed = TRUE;\r
         }\r
         break;\r
 \r
-      case DEFERRED_PP_UNOWNERED_FIELD_UPGRADE:\r
+      case PHYSICAL_PRESENCE_DEFERRED_PP_UNOWNERED_FIELD_UPGRADE:\r
         if ((Flags & FLAG_NO_PPI_MAINTENANCE) != 0) {\r
           RequestConfirmed = TRUE;\r
         }\r
         break;\r
 \r
-      case ENABLE_ACTIVATE_CLEAR_ENABLE_ACTIVATE:\r
-      case CLEAR_ENABLE_ACTIVATE:\r
+      case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR_ENABLE_ACTIVATE:\r
+      case PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE:\r
         if ((Flags & FLAG_NO_PPI_CLEAR) != 0 && (Flags & FLAG_NO_PPI_PROVISION) != 0) {\r
           RequestConfirmed = TRUE;\r
         }\r
         break;  \r
 \r
-      case SET_NO_PPI_PROVISION_FALSE:\r
-      case SET_NO_PPI_CLEAR_FALSE:\r
-      case SET_NO_PPI_MAINTENANCE_FALSE:\r
-      case NO_ACTION:\r
+      case PHYSICAL_PRESENCE_SET_NO_PPI_PROVISION_FALSE:\r
+      case PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_FALSE:\r
+      case PHYSICAL_PRESENCE_SET_NO_PPI_MAINTENANCE_FALSE:\r
+      case PHYSICAL_PRESENCE_NO_ACTION:\r
         RequestConfirmed = TRUE;\r
         break;\r
 \r
-      case SET_OPERATOR_AUTH:\r
+      case PHYSICAL_PRESENCE_SET_OPERATOR_AUTH:\r
         //\r
         // This command requires UI to prompt user for Auth data\r
-        // Here it is NOT implemented\r
         //\r
-        mTcgNvs->PhysicalPresence.ReturnCode = 0\r
+        mTcgNvs->PhysicalPresence.ReturnCode = PP_REQUEST_NOT_IMPLEMENTED\r
         return EFI_SUCCESS;\r
     }\r
 \r
     if (RequestConfirmed) {\r
-      //\r
-      // Allowed and physically present user not required \r
-      //\r
-      mTcgNvs->PhysicalPresence.ReturnCode = 4;\r
+      mTcgNvs->PhysicalPresence.ReturnCode = PP_REQUEST_ALLOWED_AND_PPUSER_NOT_REQUIRED;\r
     } else {\r
-      //\r
-      // Allowed and physically present user required \r
-      //\r
-      mTcgNvs->PhysicalPresence.ReturnCode = 3;\r
+      mTcgNvs->PhysicalPresence.ReturnCode = PP_REQUEST_ALLOWED_AND_PPUSER_REQUIRED;\r
     }    \r
   } \r
 \r
@@ -235,6 +189,10 @@ PhysicalPresenceCallback (
 /**\r
   Software SMI callback for MemoryClear which is called from ACPI method.\r
 \r
+  Caution: This function may receive untrusted input.\r
+  Variable and ACPINvs are external input, so this function will validate\r
+  its data structure to be valid value.\r
+\r
   @param[in]      DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().\r
   @param[in]      Context         Points to an optional handler context which was specified when the\r
                                   handler was registered.\r
@@ -258,15 +216,10 @@ MemoryClearCallback (
   UINTN                          DataSize;\r
   UINT8                          MorControl;\r
 \r
-  if (mTcgNvs->MemoryClear.Parameter == 1) {\r
-    //\r
-    // Called from ACPI _DSM method, save the MOR data to variable.\r
-    //\r
+  mTcgNvs->MemoryClear.ReturnCode = MOR_REQUEST_SUCCESS;\r
+  if (mTcgNvs->MemoryClear.Parameter == ACPI_FUNCTION_DSM_MEMORY_CLEAR_INTERFACE) {\r
     MorControl = (UINT8) mTcgNvs->MemoryClear.Request;\r
-  } else if (mTcgNvs->MemoryClear.Parameter == 2) {\r
-    //\r
-    // Called from ACPI _PTS method, setup ClearMemory flags if needed.\r
-    //\r
+  } else if (mTcgNvs->MemoryClear.Parameter == ACPI_FUNCTION_PTS_CLEAR_MOR_BIT) {\r
     DataSize = sizeof (UINT8);\r
     Status = mSmmVariable->SmmGetVariable (\r
                              MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,\r
@@ -276,7 +229,8 @@ MemoryClearCallback (
                              &MorControl\r
                              );\r
     if (EFI_ERROR (Status)) {\r
-      ASSERT (Status == EFI_NOT_FOUND);\r
+      mTcgNvs->MemoryClear.ReturnCode = MOR_REQUEST_GENERAL_FAILURE;\r
+      DEBUG ((EFI_D_ERROR, "[TPM] Get MOR variable failure! Status = %r\n", Status));\r
       return EFI_SUCCESS;\r
     }\r
 \r
@@ -294,7 +248,10 @@ MemoryClearCallback (
                            DataSize,\r
                            &MorControl\r
                            );\r
-  ASSERT_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
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -329,9 +286,8 @@ 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_OPREGION_OP) && \r
+    if ((OpRegion->OpRegionOp  == AML_EXT_REGION_OP) && \r
         (OpRegion->NameString  == Name) &&\r
-        (OpRegion->RegionLen   == Size) &&\r
         (OpRegion->DWordPrefix == AML_DWORD_PREFIX) &&\r
         (OpRegion->BytePrefix  == AML_BYTE_PREFIX)) {\r
 \r
@@ -339,6 +295,7 @@ AssignOpRegion (
       ASSERT_EFI_ERROR (Status);\r
       ZeroMem ((VOID *)(UINTN)MemoryAddress, Size);\r
       OpRegion->RegionOffset = (UINT32) (UINTN) MemoryAddress;\r
+      OpRegion->RegionLen    = (UINT8) Size;\r
       break;\r
     }\r
   }\r
@@ -373,8 +330,23 @@ PublishAcpiTable (
              );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
+\r
+  //\r
+  // Measure to PCR[0] with event EV_POST_CODE ACPI DATA\r
+  //\r
+  TpmMeasureAndLogData(\r
+    0,\r
+    EV_POST_CODE,\r
+    EV_POSTCODE_INFO_ACPI_DATA,\r
+    ACPI_DATA_LEN,\r
+    Table,\r
+    TableSize\r
+    );\r
+\r
+\r
   ASSERT (Table->OemTableId == SIGNATURE_64 ('T', 'c', 'g', 'T', 'a', 'b', 'l', 'e'));\r
-  mTcgNvs = AssignOpRegion (Table, SIGNATURE_32 ('T', 'N', 'V', 'S'), sizeof (TCG_NVS));\r
+  CopyMem (Table->OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (Table->OemId) );\r
+  mTcgNvs = AssignOpRegion (Table, SIGNATURE_32 ('T', 'N', 'V', 'S'), (UINT16) sizeof (TCG_NVS));\r
   ASSERT (mTcgNvs != NULL);\r
 \r
   //\r
@@ -420,6 +392,11 @@ InitializeTcgSmm (
   EFI_SMM_SW_REGISTER_CONTEXT    SwContext;\r
   EFI_HANDLE                     SwHandle;\r
 \r
+  if (!CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm12Guid)){\r
+    DEBUG ((EFI_D_ERROR, "No TPM12 instance required!\n"));\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   Status = PublishAcpiTable ();\r
   ASSERT_EFI_ERROR (Status);\r
 \r