edk2/MdeModulePkg/Universal
Hao Wu 57d2123805 MdeModulePkg/Variable: [CVE-2017-5753] Fix bounds check bypass
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>
(cherry picked from commit e83d841fdc)
2018-11-21 09:32:46 +08:00
..
Acpi MdeModulePkg-FPDT(3): Use SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA_BY_OFFSET in FpdtDxe. 2016-07-06 16:19:56 +08:00
BdsDxe MdeModulePkg: Remove unused variables from both UefiBootManagerLib and BdsDxe to fix GCC build. 2015-08-28 05:41:06 +00:00
BootManagerPolicyDxe MdeModulePkg: Add UNI files for BootManagerPolicyDxe driver. 2015-05-15 08:46:06 +00:00
CapsulePei MdeModulePkg CapsuleX64: Reduce reserved memory consumption 2015-07-27 03:04:41 +00:00
CapsuleRuntimeDxe MdeModulePkg CapsuleRuntimeDxe: Reduce reserved memory consumption 2015-07-27 03:05:13 +00:00
Console MdeModulePkg: Use PcdSet##S to replace PcdSet## 2016-03-03 14:10:52 +08:00
DebugPortDxe MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
DebugSupportDxe MdeModulePkg DebugSupportDxe: Fix build error with GNU assembler 2014-12-17 05:16:48 +00:00
DevicePathDxe MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
Disk MdeModulePkg PartitionDxe: Add Re-entry handling logic for BindingStop 2016-08-02 15:09:48 +08:00
DisplayEngineDxe MdeModulePkg: Clear the buffer after using it 2016-11-25 11:00:23 +08:00
DriverHealthManagerDxe MdeModulePkg: Replace deprecated function. 2015-08-27 04:52:24 +00:00
DriverSampleDxe MdeModulePkg/DriverSample: Remove the password related codes 2016-11-25 11:02:24 +08:00
EbcDxe Fix X64 native function call prolog. Prepare space for at least 4 arguments, 2014-09-22 05:54:41 +00:00
EsrtDxe MdeModulePkg: Add the missing module UNI files. 2015-08-28 02:46:46 +00:00
FaultTolerantWriteDxe MdeModulePkg/FaultTolerantWrite:[CVE-2017-5753]Fix bounds check bypass 2018-11-21 09:32:42 +08:00
FaultTolerantWritePei MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
FvSimpleFileSystemDxe MdeModulePkg/FvSimpleFileSystemDxe: Support file opening with no '.efi' 2015-07-09 10:34:27 +00:00
HiiDatabaseDxe MdeModulePkg/HiiDatabaseDxe: Fix a variable is uninitialized. 2017-05-16 10:35:14 +08:00
HiiResourcesSampleDxe MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
LegacyRegion2Dxe MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
LockBox/SmmLockBox MdeModulePkg/SmmLockBox: [CVE-2017-5753] Fix bounds check bypass 2018-11-21 09:32:44 +08:00
MemoryTest MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
Metronome MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
MonotonicCounterRuntimeDxe MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
Network MdeModulePkg/Ip4Dxe: Add Ip/Netmask pair check for Ip4Config2 2017-05-18 15:53:22 +08:00
PcatSingleSegmentPciCfg2Pei MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
PCD MdeModulePkg/Universal/PCD: Use safe string functions to refine code. 2015-07-01 08:27:03 +00:00
PlatformDriOverrideDxe MdeModulePkg/Universal/PlatformDriOverrideDxe: Use safe string functions to refine code. 2015-07-01 08:28:13 +00:00
PrintDxe MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
PropertiesTableAttributesDxe MdeModulePkg: Refine code to make it more readable. 2015-07-03 01:39:06 +00:00
ReportStatusCodeRouter MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
ResetSystemRuntimeDxe MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
SectionExtractionDxe MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
SectionExtractionPei MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
SecurityStubDxe MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
SerialDxe MdeModulePkg: Upstream SerialDxe from EmbeddedPkg 2015-11-30 03:17:29 +00:00
SetupBrowserDxe MdeModulePkg: Clear the buffer after using it 2016-11-25 11:00:23 +08:00
SmbiosDxe Move Smbios table MAX length definition to Mde header filer. 2015-11-16 08:04:49 +00:00
SmbiosMeasurementDxe MdeModulePkg SmbiosMeasurementDxe: NULL check to String from GetSmbiosStringById 2015-11-16 08:18:40 +00:00
SmmCommunicationBufferDxe MdeModulePkg: Add new driver to publish EDKII_PI_SMM_COMMUNICATION_REGION_TABLE. 2016-07-06 16:19:42 +08:00
StatusCodeHandler MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
TimestampDxe MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00
Variable MdeModulePkg/Variable: [CVE-2017-5753] Fix bounds check bypass 2018-11-21 09:32:46 +08:00
WatchdogTimerDxe MdeModulePkg: Convert non DOS format files to DOS format and remove unused module UNI files. 2014-09-03 08:39:12 +00:00