UefiCpuPkg/SmmCpuFeaturesLib: [CVE-2017-5715] Stuff RSB before RSM
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1093 Return Stack Buffer (RSB) is used to predict the target of RET instructions. When the RSB underflows, some processors may fall back to using branch predictors. This might impact software using the retpoline mitigation strategy on those processors. This commit will add RSB stuffing logic before returning from SMM (the RSM instruction) to avoid interfering with non-SMM usage of the retpoline technique. After the stuffing, RSB entries will contain a trap like: @SpecTrap: pause lfence jmp @SpecTrap A more detailed explanation of the purpose of commit is under the 'Branch target injection mitigation' section of the below link: https://software.intel.com/security-software-guidance/insights/host-firmware-speculative-execution-side-channel-mitigation Please note that this commit requires further actions (BZ 1091) to remove the duplicated 'StuffRsb.inc' files and merge them into one under a UefiCpuPkg package-level directory (such as UefiCpuPkg/Include/). REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1091 Cc: Jiewen Yao <jiewen.yao@intel.com> Cc: Michael D Kinney <michael.d.kinney@intel.com> Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Hao Wu <hao.a.wu@intel.com> Reviewed-by: Eric Dong <eric.dong@intel.com>
This commit is contained in:
		@@ -18,6 +18,8 @@
 | 
				
			|||||||
;
 | 
					;
 | 
				
			||||||
;-------------------------------------------------------------------------------
 | 
					;-------------------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					%include "StuffRsb.inc"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
%define MSR_IA32_MISC_ENABLE 0x1A0
 | 
					%define MSR_IA32_MISC_ENABLE 0x1A0
 | 
				
			||||||
%define MSR_EFER      0xc0000080
 | 
					%define MSR_EFER      0xc0000080
 | 
				
			||||||
%define MSR_EFER_XD   0x800
 | 
					%define MSR_EFER_XD   0x800
 | 
				
			||||||
@@ -206,6 +208,7 @@ CommonHandler:
 | 
				
			|||||||
    wrmsr
 | 
					    wrmsr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.7:
 | 
					.7:
 | 
				
			||||||
 | 
					    StuffRsb32
 | 
				
			||||||
    rsm
 | 
					    rsm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,5 @@
 | 
				
			|||||||
;------------------------------------------------------------------------------ ;
 | 
					;------------------------------------------------------------------------------ ;
 | 
				
			||||||
; Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
 | 
					; Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
 | 
				
			||||||
; This program and the accompanying materials
 | 
					; This program and the accompanying materials
 | 
				
			||||||
; are licensed and made available under the terms and conditions of the BSD License
 | 
					; 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
 | 
					; which accompanies this distribution.  The full text of the license may be found at
 | 
				
			||||||
@@ -18,6 +18,8 @@
 | 
				
			|||||||
;
 | 
					;
 | 
				
			||||||
;-------------------------------------------------------------------------------
 | 
					;-------------------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					%include "StuffRsb.inc"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
global  ASM_PFX(gcStmPsd)
 | 
					global  ASM_PFX(gcStmPsd)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern  ASM_PFX(SmmStmExceptionHandler)
 | 
					extern  ASM_PFX(SmmStmExceptionHandler)
 | 
				
			||||||
@@ -130,6 +132,7 @@ ASM_PFX(OnStmSetup):
 | 
				
			|||||||
    wrmsr
 | 
					    wrmsr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.71:
 | 
					.71:
 | 
				
			||||||
 | 
					    StuffRsb32
 | 
				
			||||||
    rsm
 | 
					    rsm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
global  ASM_PFX(OnStmTeardown)
 | 
					global  ASM_PFX(OnStmTeardown)
 | 
				
			||||||
@@ -172,4 +175,5 @@ ASM_PFX(OnStmTeardown):
 | 
				
			|||||||
    wrmsr
 | 
					    wrmsr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.72:
 | 
					.72:
 | 
				
			||||||
 | 
					    StuffRsb32
 | 
				
			||||||
    rsm
 | 
					    rsm
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										55
									
								
								UefiCpuPkg/Library/SmmCpuFeaturesLib/Ia32/StuffRsb.inc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								UefiCpuPkg/Library/SmmCpuFeaturesLib/Ia32/StuffRsb.inc
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,55 @@
 | 
				
			|||||||
 | 
					;------------------------------------------------------------------------------
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					; 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.
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					; Abstract:
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					;   This file provides macro definitions for stuffing the Return Stack Buffer (RSB).
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					;------------------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					%define RSB_STUFF_ENTRIES 0x20
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					; parameters:
 | 
				
			||||||
 | 
					; @param 1: register to use as counter (e.g. IA32:eax, X64:rax)
 | 
				
			||||||
 | 
					; @param 2: stack pointer to restore   (IA32:esp, X64:rsp)
 | 
				
			||||||
 | 
					; @param 3: the size of a stack frame  (IA32:4, X64:8)
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					%macro StuffRsb 3
 | 
				
			||||||
 | 
					      mov     %1, RSB_STUFF_ENTRIES / 2
 | 
				
			||||||
 | 
					  %%Unroll1:
 | 
				
			||||||
 | 
					      call    %%Unroll2
 | 
				
			||||||
 | 
					  %%SpecTrap1:
 | 
				
			||||||
 | 
					      pause
 | 
				
			||||||
 | 
					      lfence
 | 
				
			||||||
 | 
					      jmp     %%SpecTrap1
 | 
				
			||||||
 | 
					  %%Unroll2:
 | 
				
			||||||
 | 
					      call    %%StuffLoop
 | 
				
			||||||
 | 
					  %%SpecTrap2:
 | 
				
			||||||
 | 
					      pause
 | 
				
			||||||
 | 
					      lfence
 | 
				
			||||||
 | 
					      jmp     %%SpecTrap2
 | 
				
			||||||
 | 
					  %%StuffLoop:
 | 
				
			||||||
 | 
					      dec     %1
 | 
				
			||||||
 | 
					      jnz     %%Unroll1
 | 
				
			||||||
 | 
					      add     %2, RSB_STUFF_ENTRIES * %3 ; Restore the stack pointer
 | 
				
			||||||
 | 
					%endmacro
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					; RSB stuffing macros for IA32 and X64
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					%macro StuffRsb32 0
 | 
				
			||||||
 | 
					      StuffRsb     eax, esp, 4
 | 
				
			||||||
 | 
					%endmacro
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					%macro StuffRsb64 0
 | 
				
			||||||
 | 
					      StuffRsb     rax, rsp, 8
 | 
				
			||||||
 | 
					%endmacro
 | 
				
			||||||
@@ -18,6 +18,8 @@
 | 
				
			|||||||
;
 | 
					;
 | 
				
			||||||
;-------------------------------------------------------------------------------
 | 
					;-------------------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					%include "StuffRsb.inc"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
;
 | 
					;
 | 
				
			||||||
; Variables referrenced by C code
 | 
					; Variables referrenced by C code
 | 
				
			||||||
;
 | 
					;
 | 
				
			||||||
@@ -221,6 +223,7 @@ CommonHandler:
 | 
				
			|||||||
    wrmsr
 | 
					    wrmsr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.1:
 | 
					.1:
 | 
				
			||||||
 | 
					    StuffRsb64
 | 
				
			||||||
    rsm
 | 
					    rsm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_StmSmiHandler:
 | 
					_StmSmiHandler:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,6 +18,8 @@
 | 
				
			|||||||
;
 | 
					;
 | 
				
			||||||
;-------------------------------------------------------------------------------
 | 
					;-------------------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					%include "StuffRsb.inc"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
global  ASM_PFX(gcStmPsd)
 | 
					global  ASM_PFX(gcStmPsd)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern  ASM_PFX(SmmStmExceptionHandler)
 | 
					extern  ASM_PFX(SmmStmExceptionHandler)
 | 
				
			||||||
@@ -131,6 +133,7 @@ ASM_PFX(OnStmSetup):
 | 
				
			|||||||
    wrmsr
 | 
					    wrmsr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.11:
 | 
					.11:
 | 
				
			||||||
 | 
					    StuffRsb64
 | 
				
			||||||
    rsm
 | 
					    rsm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
global ASM_PFX(OnStmTeardown)
 | 
					global ASM_PFX(OnStmTeardown)
 | 
				
			||||||
@@ -175,4 +178,5 @@ ASM_PFX(OnStmTeardown):
 | 
				
			|||||||
    wrmsr
 | 
					    wrmsr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.12:
 | 
					.12:
 | 
				
			||||||
 | 
					    StuffRsb64
 | 
				
			||||||
    rsm
 | 
					    rsm
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										55
									
								
								UefiCpuPkg/Library/SmmCpuFeaturesLib/X64/StuffRsb.inc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								UefiCpuPkg/Library/SmmCpuFeaturesLib/X64/StuffRsb.inc
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,55 @@
 | 
				
			|||||||
 | 
					;------------------------------------------------------------------------------
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					; 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.
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					; Abstract:
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					;   This file provides macro definitions for stuffing the Return Stack Buffer (RSB).
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					;------------------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					%define RSB_STUFF_ENTRIES 0x20
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					; parameters:
 | 
				
			||||||
 | 
					; @param 1: register to use as counter (e.g. IA32:eax, X64:rax)
 | 
				
			||||||
 | 
					; @param 2: stack pointer to restore   (IA32:esp, X64:rsp)
 | 
				
			||||||
 | 
					; @param 3: the size of a stack frame  (IA32:4, X64:8)
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					%macro StuffRsb 3
 | 
				
			||||||
 | 
					      mov     %1, RSB_STUFF_ENTRIES / 2
 | 
				
			||||||
 | 
					  %%Unroll1:
 | 
				
			||||||
 | 
					      call    %%Unroll2
 | 
				
			||||||
 | 
					  %%SpecTrap1:
 | 
				
			||||||
 | 
					      pause
 | 
				
			||||||
 | 
					      lfence
 | 
				
			||||||
 | 
					      jmp     %%SpecTrap1
 | 
				
			||||||
 | 
					  %%Unroll2:
 | 
				
			||||||
 | 
					      call    %%StuffLoop
 | 
				
			||||||
 | 
					  %%SpecTrap2:
 | 
				
			||||||
 | 
					      pause
 | 
				
			||||||
 | 
					      lfence
 | 
				
			||||||
 | 
					      jmp     %%SpecTrap2
 | 
				
			||||||
 | 
					  %%StuffLoop:
 | 
				
			||||||
 | 
					      dec     %1
 | 
				
			||||||
 | 
					      jnz     %%Unroll1
 | 
				
			||||||
 | 
					      add     %2, RSB_STUFF_ENTRIES * %3 ; Restore the stack pointer
 | 
				
			||||||
 | 
					%endmacro
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					; RSB stuffing macros for IA32 and X64
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					%macro StuffRsb32 0
 | 
				
			||||||
 | 
					      StuffRsb     eax, esp, 4
 | 
				
			||||||
 | 
					%endmacro
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					%macro StuffRsb64 0
 | 
				
			||||||
 | 
					      StuffRsb     rax, rsp, 8
 | 
				
			||||||
 | 
					%endmacro
 | 
				
			||||||
		Reference in New Issue
	
	Block a user