]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/Variable/RuntimeDxe: delete and lock OS-created MOR variable
authorLaszlo Ersek <lersek@redhat.com>
Tue, 3 Oct 2017 15:55:09 +0000 (17:55 +0200)
committerLaszlo Ersek <lersek@redhat.com>
Tue, 10 Oct 2017 09:25:28 +0000 (11:25 +0200)
According to the TCG Platform Reset Attack Mitigation Specification (May
15, 2008):

> 5 Interface for UEFI
> 5.1 UEFI Variable
> 5.1.1 The MemoryOverwriteRequestControl
>
> Start of informative comment:
>
> [...] The OS loader should not create the variable. Rather, the firmware
> is required to create it and must support the semantics described here.
>
> End of informative comment.

However, some OS kernels create the MOR variable even if the platform
firmware does not support it (see one Bugzilla reference below). This OS
issue breaks the logic added in the last patch.

Strengthen the MOR check by searching for the TCG or TCG2 protocols, as
edk2's implementation of MOR depends on (one of) those protocols.

The protocols are defined under MdePkg, thus there's no inter-package
dependency issue. In addition, calling UEFI services in
MorLockInitAtEndOfDxe() is safe, due to the following order of events /
actions:

- platform BDS signals the EndOfDxe event group,
- the SMM core installs the SmmEndOfDxe protocol,
- MorLockInitAtEndOfDxe() is invoked, and it calls UEFI services,
- some time later, platform BDS installs the DxeSmmReadyToLock protocol,
- SMM / SMRAM is locked down and UEFI services become unavailable to SMM
  drivers.

Cc: Eric Dong <eric.dong@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Ladi Prosek <lprosek@redhat.com>
Cc: Star Zeng <star.zeng@intel.com>
Ref: https://bugzilla.redhat.com/show_bug.cgi?id=1498159
Suggested-by: Jiewen Yao <jiewen.yao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
Tested-by: Ladi Prosek <lprosek@redhat.com>
MdeModulePkg/Universal/Variable/RuntimeDxe/TcgMorLockSmm.c
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf

index faeaabb224afc6fa9214fdc34191e94fce2ee864..93a300a84677618c9bee06c381e6274db37c2802 100644 (file)
@@ -21,6 +21,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Library/DebugLib.h>\r
 #include <Library/BaseLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
 #include "Variable.h"\r
 \r
 typedef struct {\r
@@ -33,6 +34,8 @@ VARIABLE_TYPE  mMorVariableType[] = {
   {MEMORY_OVERWRITE_REQUEST_CONTROL_LOCK_NAME,  &gEfiMemoryOverwriteRequestControlLockGuid},\r
 };\r
 \r
+BOOLEAN         mMorPassThru = FALSE;\r
+\r
 #define MOR_LOCK_DATA_UNLOCKED           0x0\r
 #define MOR_LOCK_DATA_LOCKED_WITHOUT_KEY 0x1\r
 #define MOR_LOCK_DATA_LOCKED_WITH_KEY    0x2\r
@@ -362,6 +365,13 @@ SetVariableCheckHandlerMor (
   // Mor Variable\r
   //\r
 \r
+  //\r
+  // Permit deletion for passthru request.\r
+  //\r
+  if (((Attributes == 0) || (DataSize == 0)) && mMorPassThru) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
   //\r
   // Basic Check\r
   //\r
@@ -409,6 +419,8 @@ MorLockInitAtEndOfDxe (
 {\r
   UINTN      MorSize;\r
   EFI_STATUS MorStatus;\r
+  EFI_STATUS TcgStatus;\r
+  VOID       *TcgInterface;\r
 \r
   if (!mMorLockInitializationRequired) {\r
     //\r
@@ -437,17 +449,72 @@ MorLockInitAtEndOfDxe (
 \r
   if (MorStatus == EFI_BUFFER_TOO_SMALL) {\r
     //\r
-    // The MOR variable exists; set the MOR Control Lock variable to report the\r
-    // capability to the OS.\r
+    // The MOR variable exists.\r
     //\r
-    SetMorLockVariable (0);\r
-    return;\r
+    // Some OSes don't follow the TCG's Platform Reset Attack Mitigation spec\r
+    // in that the OS should never create the MOR variable, only read and write\r
+    // it -- these OSes (unintentionally) create MOR if the platform firmware\r
+    // does not produce it. Whether this is the case (from the last OS boot)\r
+    // can be deduced from the absence of the TCG / TCG2 protocols, as edk2's\r
+    // MOR implementation depends on (one of) those protocols.\r
+    //\r
+    TcgStatus = gBS->LocateProtocol (\r
+                       &gEfiTcgProtocolGuid,\r
+                       NULL,                 // Registration\r
+                       &TcgInterface\r
+                       );\r
+    if (EFI_ERROR (TcgStatus)) {\r
+      TcgStatus = gBS->LocateProtocol (\r
+                         &gEfiTcg2ProtocolGuid,\r
+                         NULL,                  // Registration\r
+                         &TcgInterface\r
+                         );\r
+    }\r
+\r
+    if (!EFI_ERROR (TcgStatus)) {\r
+      //\r
+      // The MOR variable originates from the platform firmware; set the MOR\r
+      // Control Lock variable to report the locking capability to the OS.\r
+      //\r
+      SetMorLockVariable (0);\r
+      return;\r
+    }\r
+\r
+    //\r
+    // The MOR variable's origin is inexplicable; delete it.\r
+    //\r
+    DEBUG ((\r
+      DEBUG_WARN,\r
+      "%a: deleting unexpected / unsupported variable %g:%s\n",\r
+      __FUNCTION__,\r
+      &gEfiMemoryOverwriteControlDataGuid,\r
+      MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME\r
+      ));\r
+\r
+    mMorPassThru = TRUE;\r
+    VariableServiceSetVariable (\r
+      MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,\r
+      &gEfiMemoryOverwriteControlDataGuid,\r
+      0,                                      // Attributes\r
+      0,                                      // DataSize\r
+      NULL                                    // Data\r
+      );\r
+    mMorPassThru = FALSE;\r
   }\r
 \r
   //\r
-  // The platform does not support the MOR variable. Delete the MOR Control\r
-  // Lock variable (should it exists for some reason) and prevent other modules\r
-  // from creating it.\r
+  // The MOR variable is absent; the platform firmware does not support it.\r
+  // Lock the variable so that no other module may create it.\r
+  //\r
+  VariableLockRequestToLock (\r
+    NULL,                                   // This\r
+    MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,\r
+    &gEfiMemoryOverwriteControlDataGuid\r
+    );\r
+\r
+  //\r
+  // Delete the MOR Control Lock variable too (should it exists for some\r
+  // reason) and prevent other modules from creating it.\r
   //\r
   mMorLockPassThru = TRUE;\r
   VariableServiceSetVariable (\r
index 4041643665792f777e43538a30ba4a61991ed0fe..69966f0d37ee3f400d8760fd289bf232e7f61476 100644 (file)
@@ -74,6 +74,7 @@
   SmmMemLib\r
   AuthVariableLib\r
   VarCheckLib\r
+  UefiBootServicesTableLib\r
 \r
 [Protocols]\r
   gEfiSmmFirmwareVolumeBlockProtocolGuid        ## CONSUMES\r
@@ -85,6 +86,8 @@
   gEfiSmmVariableProtocolGuid\r
   gEfiSmmEndOfDxeProtocolGuid                   ## NOTIFY\r
   gEdkiiSmmVarCheckProtocolGuid                 ## PRODUCES\r
+  gEfiTcgProtocolGuid                           ## SOMETIMES_CONSUMES\r
+  gEfiTcg2ProtocolGuid                          ## SOMETIMES_CONSUMES\r
 \r
 [Guids]\r
   ## SOMETIMES_CONSUMES   ## GUID # Signature of Variable store header\r