]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/Variable: [CVE-2017-5753] Fix bounds check bypass
authorHao Wu <hao.a.wu@intel.com>
Thu, 13 Sep 2018 07:47:10 +0000 (15:47 +0800)
committerHao Wu <hao.a.wu@intel.com>
Sun, 30 Sep 2018 05:06:42 +0000 (13:06 +0800)
REF:https://bugzilla.tianocore.org/show_bug.cgi?id=1194

Speculative execution is used by processor to avoid having to wait for
data to arrive from memory, or for previous operations to finish, the
processor may speculate as to what will be executed.

If the speculation is incorrect, the speculatively executed instructions
might leave hints such as which memory locations have been brought into
cache. Malicious actors can use the bounds check bypass method (code
gadgets with controlled external inputs) to infer data values that have
been used in speculative operations to reveal secrets which should not
otherwise be accessed.

This commit will focus on the SMI handler(s) registered within the
Variable\RuntimeDxe driver and insert AsmLfence API to mitigate the
bounds check bypass issue.

For SMI handler SmmVariableHandler():

Under "case SMM_VARIABLE_FUNCTION_GET_VARIABLE:",
'SmmVariableHeader->NameSize' can be a potential cross boundary access of
the 'CommBuffer' (controlled external input) during speculative execution.

This cross boundary access is later used as the index to access array
'SmmVariableHeader->Name' by code:
"SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1]"
One can observe which part of the content within array was brought into
cache to possibly reveal the value of 'SmmVariableHeader->NameSize'.

Hence, this commit adds a AsmLfence() after the boundary/range checks of
'CommBuffer' to prevent the speculative execution.

And there are 2 similar cases under
"case SMM_VARIABLE_FUNCTION_SET_VARIABLE:" and
"case SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET:" as well.
This commits also handles them.

Also, under "case SMM_VARIABLE_FUNCTION_SET_VARIABLE:",
'(UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize' points to
the 'CommBuffer' (with some offset) and then passed as parameter 'Data' to
function VariableServiceSetVariable().

Within function VariableServiceSetVariable(), there is a sanity check for
EFI_VARIABLE_AUTHENTICATION_2 descriptor for the data pointed by 'Data'.
If this check is speculatively bypassed, potential cross-boundary data
access for 'Data' is possible to be revealed via the below function calls
sequence during speculative execution:

AuthVariableLibProcessVariable()
ProcessVarWithPk() or ProcessVarWithKek()

Within function ProcessVarWithPk() or ProcessVarWithKek(), for the code
"PayloadSize = DataSize - AUTHINFO2_SIZE (Data);", 'AUTHINFO2_SIZE (Data)'
can be a cross boundary access during speculative execution.

Then, 'PayloadSize' is possible to be revealed by the function call
sequence:

AuthServiceInternalUpdateVariableWithTimeStamp()
mAuthVarLibContextIn->UpdateVariable()
VariableExLibUpdateVariable()
UpdateVariable()
CopyMem()

Hence, this commit adds a AsmLfence() after the sanity check for
EFI_VARIABLE_AUTHENTICATION_2 descriptor upon 'Data' within function
VariableServiceSetVariable() to prevent the speculative execution.

Also, please note that the change made within function
VariableServiceSetVariable() will affect DXE as well. However, since we
only focuses on the SMM codes, the commit will introduce a new module
internal function called VariableLoadFence() to handle this. This internal
function will have 2 implementations (1 for SMM, 1 for DXE). For the SMM
implementation, it is a wrapper to call the AsmLfence() API; for the DXE
implementation, it is empty.

A more detailed explanation of the purpose of commit is under the
'Bounds check bypass mitigation' section of the below link:
https://software.intel.com/security-software-guidance/insights/host-firmware-speculative-execution-side-channel-mitigation

And the document at:
https://software.intel.com/security-software-guidance/api-app/sites/default/files/337879-analyzing-potential-bounds-Check-bypass-vulnerabilities.pdf

Cc: Jiewen Yao <jiewen.yao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
Reviewed-by: Star Zeng <star.zeng@intel.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
Regression-tested-by: Laszlo Ersek <lersek@redhat.com>
MdeModulePkg/Universal/Variable/RuntimeDxe/LoadFenceDxe.c [new file with mode: 0644]
MdeModulePkg/Universal/Variable/RuntimeDxe/LoadFenceSmm.c [new file with mode: 0644]
MdeModulePkg/Universal/Variable/RuntimeDxe/PrivilegePolymorphic.h
MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf

diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/LoadFenceDxe.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/LoadFenceDxe.c
new file mode 100644 (file)
index 0000000..0f64ee0
--- /dev/null
@@ -0,0 +1,31 @@
+/** @file\r
+  Serialize operation on all load-from-memory instructions (DXE version).\r
+\r
+Copyright (c) 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
+http://opensource.org/licenses/bsd-license.php\r
+\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 "Variable.h"\r
+\r
+/**\r
+  This service is consumed by the variable modules to perform a serializing\r
+  operation on all load-from-memory instructions that were issued prior to the\r
+  call of this function.\r
+\r
+**/\r
+VOID\r
+MemoryLoadFence (\r
+  VOID\r
+  )\r
+{\r
+  //\r
+  // Do nothing.\r
+  //\r
+}\r
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/LoadFenceSmm.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/LoadFenceSmm.c
new file mode 100644 (file)
index 0000000..4b0d7e3
--- /dev/null
@@ -0,0 +1,30 @@
+/** @file\r
+  Serialize operation on all load-from-memory instructions (SMM version).\r
+\r
+Copyright (c) 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
+http://opensource.org/licenses/bsd-license.php\r
+\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 <Library/BaseLib.h>\r
+#include "Variable.h"\r
+\r
+/**\r
+  This service is consumed by the variable modules to perform a serializing\r
+  operation on all load-from-memory instructions that were issued prior to the\r
+  call of this function.\r
+\r
+**/\r
+VOID\r
+MemoryLoadFence (\r
+  VOID\r
+  )\r
+{\r
+  AsmLfence ();\r
+}\r
index b98b8556a23a96361da84a6c37176b9e6765b817..a324ad23650fbf6f5ccce01b6072aab2beea053f 100644 (file)
@@ -7,7 +7,7 @@
   vs. non-privileged driver code.\r
 \r
   Copyright (c) 2017, Red Hat, Inc.<BR>\r
-  Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
 \r
   This program and the accompanying materials are licensed and made available\r
   under the terms and conditions of the BSD License which accompanies this\r
@@ -84,4 +84,15 @@ SetVariableCheckHandlerMor (
   IN VOID       *Data\r
   );\r
 \r
+/**\r
+  This service is consumed by the variable modules to perform a serializing\r
+  operation on all load-from-memory instructions that were issued prior to the\r
+  call of this function.\r
+\r
+**/\r
+VOID\r
+MemoryLoadFence (\r
+  VOID\r
+  );\r
+\r
 #endif\r
index dc8622db8138f237d60e7908a8e674a1e5680dfd..a2d61c8cd618776a6f44c76f0fa12e9603555279 100644 (file)
@@ -3198,6 +3198,12 @@ VariableServiceSetVariable (
       ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength < OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r
       return EFI_SECURITY_VIOLATION;\r
     }\r
+    //\r
+    // The MemoryLoadFence() call here is to ensure the above sanity check\r
+    // for the EFI_VARIABLE_AUTHENTICATION_2 descriptor has been completed\r
+    // before the execution of subsequent codes.\r
+    //\r
+    MemoryLoadFence ();\r
     PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r
   } else {\r
     PayloadSize = DataSize;\r
index 2d0a172ece3518d237aac0ccd8f06951dfcbbafe..868981ccaf30b61dc3c4b21299a0881f09ea8e5f 100644 (file)
@@ -46,6 +46,7 @@
   TcgMorLockDxe.c\r
   VarCheck.c\r
   VariableExLib.c\r
+  LoadFenceDxe.c\r
 \r
 [Packages]\r
   MdePkg/MdePkg.dec\r
index e495d971a08b1d8df69fddf05d92c9e80d9e40fd..6dc19c24dbc23276369a2da7bee091af93b17500 100644 (file)
@@ -537,6 +537,12 @@ SmmVariableHandler (
         goto EXIT;\r
       }\r
 \r
+      //\r
+      // The MemoryLoadFence() call here is to ensure the previous range/content\r
+      // checks for the CommBuffer have been completed before the subsequent\r
+      // consumption of the CommBuffer content.\r
+      //\r
+      MemoryLoadFence ();\r
       if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
         //\r
         // Make sure VariableName is A Null-terminated string.\r
@@ -631,6 +637,12 @@ SmmVariableHandler (
         goto EXIT;\r
       }\r
 \r
+      //\r
+      // The MemoryLoadFence() call here is to ensure the previous range/content\r
+      // checks for the CommBuffer have been completed before the subsequent\r
+      // consumption of the CommBuffer content.\r
+      //\r
+      MemoryLoadFence ();\r
       if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
         //\r
         // Make sure VariableName is A Null-terminated string.\r
@@ -766,6 +778,12 @@ SmmVariableHandler (
         goto EXIT;\r
       }\r
 \r
+      //\r
+      // The MemoryLoadFence() call here is to ensure the previous range/content\r
+      // checks for the CommBuffer have been completed before the subsequent\r
+      // consumption of the CommBuffer content.\r
+      //\r
+      MemoryLoadFence ();\r
       if (CommVariableProperty->NameSize < sizeof (CHAR16) || CommVariableProperty->Name[CommVariableProperty->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
         //\r
         // Make sure VariableName is A Null-terminated string.\r
index dbb0674a46adeb3216667b9f4052bf0d3bfb43dc..2fe72ff8a442e7df5f8dda5de27503f28d5e08c1 100644 (file)
@@ -54,6 +54,7 @@
   PrivilegePolymorphic.h\r
   VariableExLib.c\r
   TcgMorLockSmm.c\r
+  LoadFenceSmm.c\r
 \r
 [Packages]\r
   MdePkg/MdePkg.dec\r