From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.88; helo=mga01.intel.com; envelope-from=star.zeng@intel.com; receiver=edk2-devel@lists.01.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id DB45B2115C318 for ; Fri, 28 Sep 2018 23:13:57 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 28 Sep 2018 23:13:57 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,318,1534834800"; d="scan'208";a="261405514" Received: from fmsmsx105.amr.corp.intel.com ([10.18.124.203]) by orsmga005.jf.intel.com with ESMTP; 28 Sep 2018 23:13:42 -0700 Received: from fmsmsx117.amr.corp.intel.com (10.18.116.17) by FMSMSX105.amr.corp.intel.com (10.18.124.203) with Microsoft SMTP Server (TLS) id 14.3.319.2; Fri, 28 Sep 2018 23:13:22 -0700 Received: from shsmsx151.ccr.corp.intel.com (10.239.6.50) by fmsmsx117.amr.corp.intel.com (10.18.116.17) with Microsoft SMTP Server (TLS) id 14.3.319.2; Fri, 28 Sep 2018 23:13:22 -0700 Received: from shsmsx102.ccr.corp.intel.com ([169.254.2.140]) by SHSMSX151.ccr.corp.intel.com ([169.254.3.27]) with mapi id 14.03.0319.002; Sat, 29 Sep 2018 14:13:20 +0800 From: "Zeng, Star" To: "Wu, Hao A" , "edk2-devel@lists.01.org" CC: "Yao, Jiewen" , "Zeng, Star" Thread-Topic: [PATCH v2 4/5] MdeModulePkg/Variable: [CVE-2017-5753] Fix bounds check bypass Thread-Index: AQHUVJbX0PPFiHeWB0uFxRJb1FQE6KUGzWvg Date: Sat, 29 Sep 2018 06:13:19 +0000 Message-ID: <0C09AFA07DD0434D9E2A0C6AEB0483103BBF5EA7@shsmsx102.ccr.corp.intel.com> References: <20180925061259.31680-1-hao.a.wu@intel.com> <20180925061259.31680-5-hao.a.wu@intel.com> In-Reply-To: <20180925061259.31680-5-hao.a.wu@intel.com> Accept-Language: zh-CN, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [PATCH v2 4/5] MdeModulePkg/Variable: [CVE-2017-5753] Fix bounds check bypass X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 29 Sep 2018 06:13:58 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable VariableSmm.inf is including LoadFenceDxe.c, it should be LoadFenceSmm.c. I also suggest using MemoryLoadFence instead of VariableLoadFence as the na= me. With them corrected, Reviewed-by: Star Zeng . Thanks, Star -----Original Message----- From: Wu, Hao A=20 Sent: Tuesday, September 25, 2018 2:13 PM To: edk2-devel@lists.01.org Cc: Wu, Hao A ; Yao, Jiewen ; Zen= g, Star Subject: [PATCH v2 4/5] MdeModulePkg/Variable: [CVE-2017-5753] Fix bounds c= heck bypass REF:https://bugzilla.tianocore.org/show_bug.cgi?id=3D1194 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 mi= ght leave hints such as which memory locations have been brought into cache= . Malicious actors can use the bounds check bypass method (code gadgets wit= h controlled external inputs) to infer data values that have been used in s= peculative operations to reveal secrets which should not otherwise be acces= sed. This commit will focus on the SMI handler(s) registered within the Variable= \RuntimeDxe driver and insert AsmLfence API to mitigate the bounds check by= pass issue. For SMI handler SmmVariableHandler(): Under "case SMM_VARIABLE_FUNCTION_GET_VARIABLE:", 'SmmVariableHeader->NameSize' can be a potential cross boundary access of t= he 'CommBuffer' (controlled external input) during speculative execution. This cross boundary access is later used as the index to access array 'SmmV= ariableHeader->Name' by code: "SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1]" One can observe which part of the content within array was brought into cac= he to possibly reveal the value of 'SmmVariableHeader->NameSize'. Hence, this commit adds a AsmLfence() after the boundary/range checks of 'C= ommBuffer' 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 acce= ss for 'Data' is possible to be revealed via the below function calls seque= nce during speculative execution: AuthVariableLibProcessVariable() ProcessVarWithPk() or ProcessVarWithKek() Within function ProcessVarWithPk() or ProcessVarWithKek(), for the code "Pa= yloadSize =3D 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 onl= y 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 implementa= tion, it is a wrapper to call the AsmLfence() API; for the DXE implementati= on, it is empty. A more detailed explanation of the purpose of commit is under the 'Bounds c= heck bypass mitigation' section of the below link: https://software.intel.com/security-software-guidance/insights/host-firmwar= e-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 Cc: Star Zeng Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Hao Wu --- MdeModulePkg/Universal/Variable/RuntimeDxe/LoadFenceDxe.c | 31 +++= +++++++++++++++++ MdeModulePkg/Universal/Variable/RuntimeDxe/LoadFenceSmm.c | 30 +++= ++++++++++++++++ MdeModulePkg/Universal/Variable/RuntimeDxe/PrivilegePolymorphic.h | 13 +++= ++++- MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c | 6 +++= + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf | 1 + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c | 18 +++= +++++++++ MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf | 1 + 7 files changed, 99 insertions(+), 1 deletion(-) diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/LoadFenceDxe.c b/Md= eModulePkg/Universal/Variable/RuntimeDxe/LoadFenceDxe.c new file mode 100644 index 0000000000..697c5b41c6 --- /dev/null +++ b/MdeModulePkg/Universal/Variable/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.
This=20 +program and the accompanying materials are licensed and made available=20 +under the terms and conditions of the BSD License which accompanies=20 +this distribution. The full text of the license may be found at=20 +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,=20 +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLI= ED. + +**/ + +#include "Variable.h" + +/** + This service is consumed by the variable modules to perform a=20 +serializing + operation on all load-from-memory instructions that were issued prior=20 +to the + call of this function. + +**/ +VOID +VariableLoadFence ( + VOID + ) +{ + // + // Do nothing. + // +} diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/LoadFenceSmm.c b/Md= eModulePkg/Universal/Variable/RuntimeDxe/LoadFenceSmm.c new file mode 100644 index 0000000000..19fd0c294e --- /dev/null +++ b/MdeModulePkg/Universal/Variable/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.
This=20 +program and the accompanying materials are licensed and made available=20 +under the terms and conditions of the BSD License which accompanies=20 +this distribution. The full text of the license may be found at=20 +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,=20 +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLI= ED. + +**/ + +#include +#include "Variable.h" + +/** + This service is consumed by the variable modules to perform a=20 +serializing + operation on all load-from-memory instructions that were issued prior=20 +to the + call of this function. + +**/ +VOID +VariableLoadFence ( + VOID + ) +{ + AsmLfence (); +} diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/PrivilegePolymorphi= c.h b/MdeModulePkg/Universal/Variable/RuntimeDxe/PrivilegePolymorphic.h index b98b8556a2..7493777976 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/PrivilegePolymorphic.h +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/PrivilegePolymorphic.h @@ -7,7 +7,7 @@ vs. non-privileged driver code. =20 Copyright (c) 2017, Red Hat, Inc.
- Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
+ Copyright (c) 2010 - 2018, Intel Corporation. All rights=20 + reserved.
=20 This program and the accompanying materials are licensed and made availa= ble under the terms and conditions of the BSD License which accompanies this= @@ -84,4 +84,15 @@ SetVariableCheckHandlerMor ( IN VOID *Data ); =20 +/** + This service is consumed by the variable modules to perform a=20 +serializing + operation on all load-from-memory instructions that were issued prior=20 +to the + call of this function. + +**/ +VOID +VariableLoadFence ( + VOID + ); + #endif diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/MdeMod= ulePkg/Universal/Variable/RuntimeDxe/Variable.c index 1ea2f84dda..c7620bf70d 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c @@ -3198,6 +3198,12 @@ VariableServiceSetVariable ( ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength < OF= FSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) { return EFI_SECURITY_VIOLATION; } + // + // The VariableLoadFence() call here is to ensure the above sanity che= ck + // for the EFI_VARIABLE_AUTHENTICATION_2 descriptor has been completed + // before the execution of subsequent codes. + // + VariableLoadFence (); PayloadSize =3D DataSize - AUTHINFO2_SIZE (Data); } else { PayloadSize =3D DataSize; diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.= inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf index 2d0a172ece..868981ccaf 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf @@ -46,6 +46,7 @@ TcgMorLockDxe.c VarCheck.c VariableExLib.c + LoadFenceDxe.c =20 [Packages] MdePkg/MdePkg.dec diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c b/Mde= ModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c index e495d971a0..e9c7095148 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c @@ -537,6 +537,12 @@ SmmVariableHandler ( goto EXIT; } =20 + // + // The VariableLoadFence() call here is to ensure the previous range= /content + // checks for the CommBuffer have been completed before the subseque= nt + // consumption of the CommBuffer content. + // + VariableLoadFence (); if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHead= er->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] !=3D L'\0') { // // Make sure VariableName is A Null-terminated string. @@ -631,6 +637,12 @@ SmmVariableHandler ( goto EXIT; } =20 + // + // The VariableLoadFence() call here is to ensure the previous range= /content + // checks for the CommBuffer have been completed before the subseque= nt + // consumption of the CommBuffer content. + // + VariableLoadFence (); if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHead= er->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] !=3D L'\0') { // // Make sure VariableName is A Null-terminated string. @@ -766,6 +778,12 @@ SmmVariableHandler ( goto EXIT; } =20 + // + // The VariableLoadFence() call here is to ensure the previous range= /content + // checks for the CommBuffer have been completed before the subseque= nt + // consumption of the CommBuffer content. + // + VariableLoadFence (); if (CommVariableProperty->NameSize < sizeof (CHAR16) || CommVariable= Property->Name[CommVariableProperty->NameSize/sizeof (CHAR16) - 1] !=3D L'\= 0') { // // Make sure VariableName is A Null-terminated string. diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf b/M= deModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf index dbb0674a46..ac40364d8a 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf @@ -54,6 +54,7 @@ PrivilegePolymorphic.h VariableExLib.c TcgMorLockSmm.c + LoadFenceDxe.c =20 [Packages] MdePkg/MdePkg.dec -- 2.12.0.windows.1