]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Tcg/TcgSmm/TcgSmm.c
Add TPM Physical Presence >=128 operation value support.
[mirror_edk2.git] / SecurityPkg / Tcg / TcgSmm / TcgSmm.c
index dd6d89f695fc7310a1279a90c8f75210f364f234..96fb456ccd9bd1c3c6dcb0e4f53bbaa1f5b614ce 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 - 2012, 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 - 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
@@ -21,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
@@ -43,7 +53,7 @@ PhysicalPresenceCallback (
   EFI_STATUS                     Status;\r
   UINTN                          DataSize;\r
   EFI_PHYSICAL_PRESENCE          PpData;\r
-  UINT8                          Flags;\r
+  EFI_PHYSICAL_PRESENCE_FLAGS    Flags;\r
   BOOLEAN                        RequestConfirmed;\r
 \r
   //\r
@@ -57,21 +67,31 @@ PhysicalPresenceCallback (
                            &DataSize,\r
                            &PpData\r
                            );\r
-  if (EFI_ERROR (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 == ACPI_FUNCTION_RETURN_REQUEST_RESPONSE_TO_OS) {\r
+    if (EFI_ERROR (Status)) {\r
+      mTcgNvs->PhysicalPresence.ReturnCode  = PP_RETURN_TPM_OPERATION_RESPONSE_FAILURE;\r
+      mTcgNvs->PhysicalPresence.LastRequest = 0;\r
+      mTcgNvs->PhysicalPresence.Response    = 0;\r
+      DEBUG ((EFI_D_ERROR, "[TPM] Get PP variable failure! Status = %r\n", Status));\r
+      return EFI_SUCCESS;\r
+    }\r
+    mTcgNvs->PhysicalPresence.ReturnCode  = PP_RETURN_TPM_OPERATION_RESPONSE_SUCCESS;\r
     mTcgNvs->PhysicalPresence.LastRequest = PpData.LastPPRequest;\r
     mTcgNvs->PhysicalPresence.Response    = PpData.PPResponse;\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 (EFI_ERROR (Status)) {\r
+      mTcgNvs->PhysicalPresence.ReturnCode = TCG_PP_SUBMIT_REQUEST_TO_PREOS_GENERAL_FAILURE;\r
+      DEBUG ((EFI_D_ERROR, "[TPM] Get PP variable failure! Status = %r\n", Status));\r
+      return EFI_SUCCESS;\r
+    }\r
     if (mTcgNvs->PhysicalPresence.Request == PHYSICAL_PRESENCE_SET_OPERATOR_AUTH) {\r
       //\r
       // This command requires UI to prompt user for Auth data.\r
       //\r
-      mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_NOT_IMPLEMENTED;\r
+      mTcgNvs->PhysicalPresence.ReturnCode = TCG_PP_SUBMIT_REQUEST_TO_PREOS_NOT_IMPLEMENTED;\r
       return EFI_SUCCESS;\r
     }\r
 \r
@@ -88,12 +108,48 @@ PhysicalPresenceCallback (
     }\r
 \r
     if (EFI_ERROR (Status)) { \r
-      mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_GENERAL_FAILURE;\r
+      mTcgNvs->PhysicalPresence.ReturnCode = TCG_PP_SUBMIT_REQUEST_TO_PREOS_GENERAL_FAILURE;\r
       return EFI_SUCCESS;\r
     }\r
-    mTcgNvs->PhysicalPresence.ReturnCode = PP_SUBMIT_REQUEST_SUCCESS;\r
+    mTcgNvs->PhysicalPresence.ReturnCode = TCG_PP_SUBMIT_REQUEST_TO_PREOS_SUCCESS;\r
+\r
+    if (mTcgNvs->PhysicalPresence.Request >= TCG_PHYSICAL_PRESENCE_VENDOR_SPECIFIC_OPERATION) {\r
+      DataSize = sizeof (EFI_PHYSICAL_PRESENCE_FLAGS);\r
+      Status = mSmmVariable->SmmGetVariable (\r
+                               PHYSICAL_PRESENCE_FLAGS_VARIABLE,\r
+                               &gEfiPhysicalPresenceGuid,\r
+                               NULL,\r
+                               &DataSize,\r
+                               &Flags\r
+                               );\r
+      if (EFI_ERROR (Status)) {\r
+        Flags.PPFlags = TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_PROVISION;\r
+      }\r
+      mTcgNvs->PhysicalPresence.ReturnCode = TcgPpVendorLibSubmitRequestToPreOSFunction (mTcgNvs->PhysicalPresence.Request, Flags.PPFlags);\r
+    }\r
   } else if (mTcgNvs->PhysicalPresence.Parameter == ACPI_FUNCTION_GET_USER_CONFIRMATION_STATUS_FOR_REQUEST) {\r
-    Flags = PpData.Flags;  \r
+    if (EFI_ERROR (Status)) {\r
+      mTcgNvs->PhysicalPresence.ReturnCode = TCG_PP_GET_USER_CONFIRMATION_BLOCKED_BY_BIOS_CONFIGURATION;\r
+      DEBUG ((EFI_D_ERROR, "[TPM] Get PP variable failure! Status = %r\n", Status));\r
+      return EFI_SUCCESS;\r
+    }\r
+    //\r
+    // Get the Physical Presence flags\r
+    //\r
+    DataSize = sizeof (EFI_PHYSICAL_PRESENCE_FLAGS);\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 = TCG_PP_GET_USER_CONFIRMATION_BLOCKED_BY_BIOS_CONFIGURATION;\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
@@ -107,27 +163,27 @@ PhysicalPresenceCallback (
       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
+        if ((Flags.PPFlags & TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_PROVISION) != 0) {\r
           RequestConfirmed = TRUE;\r
         }\r
         break;\r
 \r
       case PHYSICAL_PRESENCE_CLEAR:\r
       case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR:\r
-        if ((Flags & FLAG_NO_PPI_CLEAR) != 0) {\r
+        if ((Flags.PPFlags & TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_CLEAR) != 0) {\r
           RequestConfirmed = TRUE;\r
         }\r
         break;\r
 \r
       case PHYSICAL_PRESENCE_DEFERRED_PP_UNOWNERED_FIELD_UPGRADE:\r
-        if ((Flags & FLAG_NO_PPI_MAINTENANCE) != 0) {\r
+        if ((Flags.PPFlags & TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_MAINTENANCE) != 0) {\r
           RequestConfirmed = TRUE;\r
         }\r
         break;\r
 \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
+        if ((Flags.PPFlags & TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_CLEAR) != 0 && (Flags.PPFlags & TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_PROVISION) != 0) {\r
           RequestConfirmed = TRUE;\r
         }\r
         break;  \r
@@ -143,15 +199,20 @@ PhysicalPresenceCallback (
         //\r
         // This command requires UI to prompt user for Auth data\r
         //\r
-        mTcgNvs->PhysicalPresence.ReturnCode = PP_REQUEST_NOT_IMPLEMENTED; \r
+        mTcgNvs->PhysicalPresence.ReturnCode = TCG_PP_GET_USER_CONFIRMATION_NOT_IMPLEMENTED; \r
         return EFI_SUCCESS;\r
+      default:\r
+        break;\r
     }\r
 \r
     if (RequestConfirmed) {\r
-      mTcgNvs->PhysicalPresence.ReturnCode = PP_REQUEST_ALLOWED_AND_PPUSER_NOT_REQUIRED;\r
+      mTcgNvs->PhysicalPresence.ReturnCode = TCG_PP_GET_USER_CONFIRMATION_ALLOWED_AND_PPUSER_NOT_REQUIRED;\r
     } else {\r
-      mTcgNvs->PhysicalPresence.ReturnCode = PP_REQUEST_ALLOWED_AND_PPUSER_REQUIRED;\r
+      mTcgNvs->PhysicalPresence.ReturnCode = TCG_PP_GET_USER_CONFIRMATION_ALLOWED_AND_PPUSER_REQUIRED;\r
     }    \r
+    if (mTcgNvs->PhysicalPresence.Request >= TCG_PHYSICAL_PRESENCE_VENDOR_SPECIFIC_OPERATION) {\r
+      mTcgNvs->PhysicalPresence.ReturnCode = TcgPpVendorLibGetUserConfirmationStatusFunction (mTcgNvs->PhysicalPresence.Request, Flags.PPFlags);\r
+    }\r
   } \r
 \r
   return EFI_SUCCESS;\r
@@ -161,6 +222,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
@@ -197,6 +262,8 @@ MemoryClearCallback (
                              &MorControl\r
                              );\r
     if (EFI_ERROR (Status)) {\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
@@ -216,6 +283,7 @@ MemoryClearCallback (
                            );\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
@@ -295,7 +363,22 @@ 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
+  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
@@ -342,6 +425,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