summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHao Wu <hao.a.wu@intel.com>2018-11-15 14:09:42 +0800
committerHao Wu <hao.a.wu@intel.com>2018-11-15 15:40:32 +0800
commit622326deb8252b6b4f69064620bf07988d4886e0 (patch)
treef61a1f4d87c8ec91f463f174068eed55f29ba606
parentb00c4c12944418a7587acf9ff0edb8d1cfc43be4 (diff)
downloadedk2-622326deb8252b6b4f69064620bf07988d4886e0.tar.gz
SecurityPkg/VariableAuth: [CVE-2017-5753] Fix bounds check bypass
This commit applies edk2 master branch commit e83d841fdc2878959185c4c6cc38a7a1e88377a4 to the VariableAuthenticated driver. 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 VariableAuthenticated\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 is 1 similar cases under "case SMM_VARIABLE_FUNCTION_SET_VARIABLE:" as well. This commits also handles it. 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: 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: 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 Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Hao Wu <hao.a.wu@intel.com>
-rw-r--r--SecurityPkg/VariableAuthenticated/RuntimeDxe/LoadFenceDxe.c31
-rw-r--r--SecurityPkg/VariableAuthenticated/RuntimeDxe/LoadFenceSmm.c30
-rw-r--r--SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c6
-rw-r--r--SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.h11
-rw-r--r--SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableRuntimeDxe.inf1
-rw-r--r--SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.c12
-rw-r--r--SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.inf1
7 files changed, 92 insertions, 0 deletions
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/LoadFenceDxe.c b/SecurityPkg/VariableAuthenticated/RuntimeDxe/LoadFenceDxe.c
new file mode 100644
index 0000000000..0f64ee093b
--- /dev/null
+++ b/SecurityPkg/VariableAuthenticated/RuntimeDxe/LoadFenceDxe.c
@@ -0,0 +1,31 @@
+/** @file
+ Serialize operation on all load-from-memory instructions (DXE version).
+
+Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
+This program and the accompanying materials
+are licensed and made available under the terms and conditions of the BSD License
+which accompanies this distribution. The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include "Variable.h"
+
+/**
+ This service is consumed by the variable modules to perform a serializing
+ operation on all load-from-memory instructions that were issued prior to the
+ call of this function.
+
+**/
+VOID
+MemoryLoadFence (
+ VOID
+ )
+{
+ //
+ // Do nothing.
+ //
+}
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/LoadFenceSmm.c b/SecurityPkg/VariableAuthenticated/RuntimeDxe/LoadFenceSmm.c
new file mode 100644
index 0000000000..4b0d7e3e95
--- /dev/null
+++ b/SecurityPkg/VariableAuthenticated/RuntimeDxe/LoadFenceSmm.c
@@ -0,0 +1,30 @@
+/** @file
+ Serialize operation on all load-from-memory instructions (SMM version).
+
+Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
+This program and the accompanying materials
+are licensed and made available under the terms and conditions of the BSD License
+which accompanies this distribution. The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <Library/BaseLib.h>
+#include "Variable.h"
+
+/**
+ This service is consumed by the variable modules to perform a serializing
+ operation on all load-from-memory instructions that were issued prior to the
+ call of this function.
+
+**/
+VOID
+MemoryLoadFence (
+ VOID
+ )
+{
+ AsmLfence ();
+}
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c b/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c
index f0a481b892..4d842f302c 100644
--- a/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c
+++ b/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c
@@ -3070,6 +3070,12 @@ VariableServiceSetVariable (
((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength < OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {
return EFI_SECURITY_VIOLATION;
}
+ //
+ // The MemoryLoadFence() call here is to ensure the above sanity check
+ // for the EFI_VARIABLE_AUTHENTICATION_2 descriptor has been completed
+ // before the execution of subsequent codes.
+ //
+ MemoryLoadFence ();
PayloadSize = DataSize - AUTHINFO2_SIZE (Data);
} else {
PayloadSize = DataSize;
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.h b/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.h
index c0497afe5e..3afabfda91 100644
--- a/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.h
+++ b/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.h
@@ -671,4 +671,15 @@ VariableLockRequestToLock (
extern VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;
+/**
+ This service is consumed by the variable modules to perform a serializing
+ operation on all load-from-memory instructions that were issued prior to the
+ call of this function.
+
+**/
+VOID
+MemoryLoadFence (
+ VOID
+ );
+
#endif
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableRuntimeDxe.inf b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableRuntimeDxe.inf
index 8f0fcae9fc..d09114b35f 100644
--- a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableRuntimeDxe.inf
+++ b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableRuntimeDxe.inf
@@ -44,6 +44,7 @@
AuthService.c
AuthService.h
Measurement.c
+ LoadFenceDxe.c
[Packages]
MdePkg/MdePkg.dec
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.c b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.c
index 5cc3fe2a6c..331f382475 100644
--- a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.c
+++ b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.c
@@ -525,6 +525,12 @@ SmmVariableHandler (
goto EXIT;
}
+ //
+ // The MemoryLoadFence() call here is to ensure the previous range/content
+ // checks for the CommBuffer have been completed before the subsequent
+ // consumption of the CommBuffer content.
+ //
+ MemoryLoadFence ();
if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {
//
// Make sure VariableName is A Null-terminated string.
@@ -619,6 +625,12 @@ SmmVariableHandler (
goto EXIT;
}
+ //
+ // The MemoryLoadFence() call here is to ensure the previous range/content
+ // checks for the CommBuffer have been completed before the subsequent
+ // consumption of the CommBuffer content.
+ //
+ MemoryLoadFence ();
if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {
//
// Make sure VariableName is A Null-terminated string.
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.inf b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.inf
index 7d0df2b802..35519ce456 100644
--- a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.inf
+++ b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.inf
@@ -52,6 +52,7 @@
AuthService.c
Variable.h
AuthService.h
+ LoadFenceSmm.c
[Packages]
MdePkg/MdePkg.dec