2016-06-14 10:28:14 +02:00
|
|
|
;------------------------------------------------------------------------------ ;
|
UefiCpuPkg/PiSmmCpu: Add Shadow Stack Support for X86 SMM.
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1521
We scan the SMM code with ROPgadget.
http://shell-storm.org/project/ROPgadget/
https://github.com/JonathanSalwan/ROPgadget/tree/master
This tool reports the gadget in SMM driver.
This patch enabled CET ShadowStack for X86 SMM.
If CET is supported, SMM will enable CET ShadowStack.
SMM CET will save the OS CET context at SmmEntry and
restore OS CET context at SmmExit.
Test:
1) test Intel internal platform (x64 only, CET enabled/disabled)
Boot test:
CET supported or not supported CPU
on CET supported platform
CET enabled/disabled
PcdCpuSmmCetEnable enabled/disabled
Single core/Multiple core
PcdCpuSmmStackGuard enabled/disabled
PcdCpuSmmProfileEnable enabled/disabled
PcdCpuSmmStaticPageTable enabled/disabled
CET exception test:
#CF generated with PcdCpuSmmStackGuard enabled/disabled.
Other exception test:
#PF for normal stack overflow
#PF for NX protection
#PF for RO protection
CET env test:
Launch SMM in CET enabled/disabled environment (DXE) - no impact to DXE
The test case can be found at
https://github.com/jyao1/SecurityEx/tree/master/ControlFlowPkg
2) test ovmf (both IA32 and X64 SMM, CET disabled only)
test OvmfIa32/Ovmf3264, with -D SMM_REQUIRE.
qemu-system-x86_64.exe -machine q35,smm=on -smp 4
-serial file:serial.log
-drive if=pflash,format=raw,unit=0,file=OVMF_CODE.fd,readonly=on
-drive if=pflash,format=raw,unit=1,file=OVMF_VARS.fd
QEMU emulator version 3.1.0 (v3.1.0-11736-g7a30e7adb0-dirty)
3) not tested
IA32 CET enabled platform
Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yao Jiewen <jiewen.yao@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
Regression-tested-by: Laszlo Ersek <lersek@redhat.com>
2019-02-22 14:30:36 +01:00
|
|
|
; Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
|
2019-04-04 01:07:22 +02:00
|
|
|
; SPDX-License-Identifier: BSD-2-Clause-Patent
|
2016-06-14 10:28:14 +02:00
|
|
|
;
|
|
|
|
; Module Name:
|
|
|
|
;
|
|
|
|
; SmiEntry.nasm
|
|
|
|
;
|
|
|
|
; Abstract:
|
|
|
|
;
|
|
|
|
; Code template of the SMI handler for a particular processor
|
|
|
|
;
|
|
|
|
;-------------------------------------------------------------------------------
|
|
|
|
|
2018-12-21 06:45:55 +01:00
|
|
|
%include "StuffRsbNasm.inc"
|
UefiCpuPkg/PiSmmCpu: Add Shadow Stack Support for X86 SMM.
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1521
We scan the SMM code with ROPgadget.
http://shell-storm.org/project/ROPgadget/
https://github.com/JonathanSalwan/ROPgadget/tree/master
This tool reports the gadget in SMM driver.
This patch enabled CET ShadowStack for X86 SMM.
If CET is supported, SMM will enable CET ShadowStack.
SMM CET will save the OS CET context at SmmEntry and
restore OS CET context at SmmExit.
Test:
1) test Intel internal platform (x64 only, CET enabled/disabled)
Boot test:
CET supported or not supported CPU
on CET supported platform
CET enabled/disabled
PcdCpuSmmCetEnable enabled/disabled
Single core/Multiple core
PcdCpuSmmStackGuard enabled/disabled
PcdCpuSmmProfileEnable enabled/disabled
PcdCpuSmmStaticPageTable enabled/disabled
CET exception test:
#CF generated with PcdCpuSmmStackGuard enabled/disabled.
Other exception test:
#PF for normal stack overflow
#PF for NX protection
#PF for RO protection
CET env test:
Launch SMM in CET enabled/disabled environment (DXE) - no impact to DXE
The test case can be found at
https://github.com/jyao1/SecurityEx/tree/master/ControlFlowPkg
2) test ovmf (both IA32 and X64 SMM, CET disabled only)
test OvmfIa32/Ovmf3264, with -D SMM_REQUIRE.
qemu-system-x86_64.exe -machine q35,smm=on -smp 4
-serial file:serial.log
-drive if=pflash,format=raw,unit=0,file=OVMF_CODE.fd,readonly=on
-drive if=pflash,format=raw,unit=1,file=OVMF_VARS.fd
QEMU emulator version 3.1.0 (v3.1.0-11736-g7a30e7adb0-dirty)
3) not tested
IA32 CET enabled platform
Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yao Jiewen <jiewen.yao@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
Regression-tested-by: Laszlo Ersek <lersek@redhat.com>
2019-02-22 14:30:36 +01:00
|
|
|
%include "Nasm.inc"
|
|
|
|
|
|
|
|
%define MSR_IA32_S_CET 0x6A2
|
|
|
|
%define MSR_IA32_CET_SH_STK_EN 0x1
|
|
|
|
%define MSR_IA32_CET_WR_SHSTK_EN 0x2
|
|
|
|
%define MSR_IA32_CET_ENDBR_EN 0x4
|
|
|
|
%define MSR_IA32_CET_LEG_IW_EN 0x8
|
|
|
|
%define MSR_IA32_CET_NO_TRACK_EN 0x10
|
|
|
|
%define MSR_IA32_CET_SUPPRESS_DIS 0x20
|
|
|
|
%define MSR_IA32_CET_SUPPRESS 0x400
|
|
|
|
%define MSR_IA32_CET_TRACKER 0x800
|
|
|
|
%define MSR_IA32_PL0_SSP 0x6A4
|
|
|
|
|
|
|
|
%define CR4_CET 0x800000
|
2018-08-16 03:32:10 +02:00
|
|
|
|
2016-10-23 17:19:52 +02:00
|
|
|
%define MSR_IA32_MISC_ENABLE 0x1A0
|
|
|
|
%define MSR_EFER 0xc0000080
|
|
|
|
%define MSR_EFER_XD 0x800
|
|
|
|
|
2016-11-28 23:13:24 +01:00
|
|
|
;
|
|
|
|
; Constants relating to PROCESSOR_SMM_DESCRIPTOR
|
|
|
|
;
|
2016-06-14 10:28:14 +02:00
|
|
|
%define DSC_OFFSET 0xfb00
|
|
|
|
%define DSC_GDTPTR 0x30
|
|
|
|
%define DSC_GDTSIZ 0x38
|
|
|
|
%define DSC_CS 14
|
|
|
|
%define DSC_DS 16
|
|
|
|
%define DSC_SS 18
|
|
|
|
%define DSC_OTHERSEG 20
|
|
|
|
|
|
|
|
%define PROTECT_MODE_CS 0x8
|
|
|
|
%define PROTECT_MODE_DS 0x20
|
|
|
|
%define TSS_SEGMENT 0x40
|
|
|
|
|
|
|
|
extern ASM_PFX(SmiRendezvous)
|
|
|
|
extern ASM_PFX(FeaturePcdGet (PcdCpuSmmStackGuard))
|
|
|
|
extern ASM_PFX(CpuSmmDebugEntry)
|
|
|
|
extern ASM_PFX(CpuSmmDebugExit)
|
|
|
|
|
|
|
|
global ASM_PFX(gcSmiHandlerTemplate)
|
|
|
|
global ASM_PFX(gcSmiHandlerSize)
|
2018-02-01 23:40:29 +01:00
|
|
|
global ASM_PFX(gPatchSmiCr3)
|
2018-02-01 23:23:59 +01:00
|
|
|
global ASM_PFX(gPatchSmiStack)
|
2018-02-01 23:01:08 +01:00
|
|
|
global ASM_PFX(gPatchSmbase)
|
UefiCpuPkg/PiSmmCpuDxeSmm: patch "XdSupported" with PatchInstructionX86()
"mXdSupported" is a global BOOLEAN variable, initialized to TRUE. The
CheckFeatureSupported() function is executed on all processors (not
concurrently though), called from SmmInitHandler(). If XD support is found
to be missing on any CPU, then "mXdSupported" is set to FALSE, and further
processors omit the check. Afterwards, "mXdSupported" is read by several
assembly and C code locations.
The tricky part is *where* "mXdSupported" is allocated (defined):
- Before commit 717fb60443fb ("UefiCpuPkg/PiSmmCpuDxeSmm: Add paging
protection.", 2016-11-17), it used to be a normal global variable,
defined (allocated) in "SmmProfile.c".
- With said commit, we moved the definition (allocation) of "mXdSupported"
into "SmiEntry.nasm". The variable was defined over the last byte of a
"mov al, 1" instruction, so that setting it to FALSE in
CheckFeatureSupported() would patch the instruction to "mov al, 0". The
subsequent conditional jump would change behavior, plus all further read
references to "mXdSupported" (in C and assembly code) would read back
the source (imm8) operand of the patched MOV instruction as data.
This trick required that the MOV instruction be encoded with DB.
In order to get rid of the DB, we have to split both roles: we need a
label for the code patching, and "mXdSupported" has to be defined
(allocated) independently of the code patching. Of course, their values
must always remain in sync.
(1) Reinstate the "mXdSupported" definition and initialization in
"SmmProfile.c" from before commit 717fb60443fb. Change the assembly
language definition ("global") to a declaration ("extern").
(2) Define the "gPatchXdSupported" label (type X86_ASSEMBLY_PATCH_LABEL)
in "SmiEntry.nasm", and add the C-language declaration to
"SmmProfileInternal.h". Replace the DB with the MOV mnemonic (keeping
the imm8 source operand with value 1).
(3) In CheckFeatureSupported(), whenever "mXdSupported" is set to FALSE,
patch the assembly code in sync, with PatchInstructionX86().
Cc: Eric Dong <eric.dong@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=866
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
2018-02-02 00:17:13 +01:00
|
|
|
extern ASM_PFX(mXdSupported)
|
|
|
|
global ASM_PFX(gPatchXdSupported)
|
2016-06-14 10:28:14 +02:00
|
|
|
extern ASM_PFX(gSmiHandlerIdtr)
|
|
|
|
|
UefiCpuPkg/PiSmmCpu: Add Shadow Stack Support for X86 SMM.
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1521
We scan the SMM code with ROPgadget.
http://shell-storm.org/project/ROPgadget/
https://github.com/JonathanSalwan/ROPgadget/tree/master
This tool reports the gadget in SMM driver.
This patch enabled CET ShadowStack for X86 SMM.
If CET is supported, SMM will enable CET ShadowStack.
SMM CET will save the OS CET context at SmmEntry and
restore OS CET context at SmmExit.
Test:
1) test Intel internal platform (x64 only, CET enabled/disabled)
Boot test:
CET supported or not supported CPU
on CET supported platform
CET enabled/disabled
PcdCpuSmmCetEnable enabled/disabled
Single core/Multiple core
PcdCpuSmmStackGuard enabled/disabled
PcdCpuSmmProfileEnable enabled/disabled
PcdCpuSmmStaticPageTable enabled/disabled
CET exception test:
#CF generated with PcdCpuSmmStackGuard enabled/disabled.
Other exception test:
#PF for normal stack overflow
#PF for NX protection
#PF for RO protection
CET env test:
Launch SMM in CET enabled/disabled environment (DXE) - no impact to DXE
The test case can be found at
https://github.com/jyao1/SecurityEx/tree/master/ControlFlowPkg
2) test ovmf (both IA32 and X64 SMM, CET disabled only)
test OvmfIa32/Ovmf3264, with -D SMM_REQUIRE.
qemu-system-x86_64.exe -machine q35,smm=on -smp 4
-serial file:serial.log
-drive if=pflash,format=raw,unit=0,file=OVMF_CODE.fd,readonly=on
-drive if=pflash,format=raw,unit=1,file=OVMF_VARS.fd
QEMU emulator version 3.1.0 (v3.1.0-11736-g7a30e7adb0-dirty)
3) not tested
IA32 CET enabled platform
Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yao Jiewen <jiewen.yao@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
Regression-tested-by: Laszlo Ersek <lersek@redhat.com>
2019-02-22 14:30:36 +01:00
|
|
|
extern ASM_PFX(mCetSupported)
|
|
|
|
global ASM_PFX(mPatchCetSupported)
|
|
|
|
global ASM_PFX(mPatchCetPl0Ssp)
|
|
|
|
global ASM_PFX(mPatchCetInterruptSsp)
|
|
|
|
|
2016-06-14 10:28:14 +02:00
|
|
|
SECTION .text
|
|
|
|
|
|
|
|
BITS 16
|
|
|
|
ASM_PFX(gcSmiHandlerTemplate):
|
|
|
|
_SmiEntryPoint:
|
|
|
|
mov bx, _GdtDesc - _SmiEntryPoint + 0x8000
|
|
|
|
mov ax,[cs:DSC_OFFSET + DSC_GDTSIZ]
|
|
|
|
dec ax
|
|
|
|
mov [cs:bx], ax
|
|
|
|
mov eax, [cs:DSC_OFFSET + DSC_GDTPTR]
|
|
|
|
mov [cs:bx + 2], eax
|
|
|
|
mov ebp, eax ; ebp = GDT base
|
|
|
|
o32 lgdt [cs:bx] ; lgdt fword ptr cs:[bx]
|
|
|
|
mov ax, PROTECT_MODE_CS
|
2016-10-23 17:19:52 +02:00
|
|
|
mov [cs:bx-0x2],ax
|
2018-02-01 23:01:08 +01:00
|
|
|
mov edi, strict dword 0 ; source operand will be patched
|
|
|
|
ASM_PFX(gPatchSmbase):
|
2016-06-14 10:28:14 +02:00
|
|
|
lea eax, [edi + (@32bit - _SmiEntryPoint) + 0x8000]
|
|
|
|
mov [cs:bx-0x6],eax
|
|
|
|
mov ebx, cr0
|
|
|
|
and ebx, 0x9ffafff3
|
|
|
|
or ebx, 0x23
|
|
|
|
mov cr0, ebx
|
|
|
|
jmp dword 0x0:0x0
|
2016-10-23 17:19:52 +02:00
|
|
|
_GdtDesc:
|
2016-06-14 10:28:14 +02:00
|
|
|
DW 0
|
|
|
|
DD 0
|
|
|
|
|
|
|
|
BITS 32
|
|
|
|
@32bit:
|
|
|
|
mov ax, PROTECT_MODE_DS
|
|
|
|
o16 mov ds, ax
|
|
|
|
o16 mov es, ax
|
|
|
|
o16 mov fs, ax
|
|
|
|
o16 mov gs, ax
|
|
|
|
o16 mov ss, ax
|
2018-02-01 23:23:59 +01:00
|
|
|
mov esp, strict dword 0 ; source operand will be patched
|
|
|
|
ASM_PFX(gPatchSmiStack):
|
2016-06-14 10:28:14 +02:00
|
|
|
mov eax, ASM_PFX(gSmiHandlerIdtr)
|
|
|
|
lidt [eax]
|
|
|
|
jmp ProtFlatMode
|
|
|
|
|
|
|
|
ProtFlatMode:
|
2018-02-01 23:40:29 +01:00
|
|
|
mov eax, strict dword 0 ; source operand will be patched
|
|
|
|
ASM_PFX(gPatchSmiCr3):
|
2016-06-14 10:28:14 +02:00
|
|
|
mov cr3, eax
|
|
|
|
;
|
|
|
|
; Need to test for CR4 specific bit support
|
|
|
|
;
|
|
|
|
mov eax, 1
|
|
|
|
cpuid ; use CPUID to determine if specific CR4 bits are supported
|
|
|
|
xor eax, eax ; Clear EAX
|
|
|
|
test edx, BIT2 ; Check for DE capabilities
|
|
|
|
jz .0
|
|
|
|
or eax, BIT3
|
|
|
|
.0:
|
|
|
|
test edx, BIT6 ; Check for PAE capabilities
|
|
|
|
jz .1
|
|
|
|
or eax, BIT5
|
|
|
|
.1:
|
|
|
|
test edx, BIT7 ; Check for MCE capabilities
|
|
|
|
jz .2
|
|
|
|
or eax, BIT6
|
|
|
|
.2:
|
|
|
|
test edx, BIT24 ; Check for FXSR capabilities
|
|
|
|
jz .3
|
|
|
|
or eax, BIT9
|
|
|
|
.3:
|
|
|
|
test edx, BIT25 ; Check for SSE capabilities
|
|
|
|
jz .4
|
|
|
|
or eax, BIT10
|
|
|
|
.4: ; as cr4.PGE is not set here, refresh cr3
|
|
|
|
mov cr4, eax ; in PreModifyMtrrs() to flush TLB.
|
2016-10-23 17:19:52 +02:00
|
|
|
|
|
|
|
cmp byte [dword ASM_PFX(FeaturePcdGet (PcdCpuSmmStackGuard))], 0
|
|
|
|
jz .6
|
|
|
|
; Load TSS
|
|
|
|
mov byte [ebp + TSS_SEGMENT + 5], 0x89 ; clear busy flag
|
|
|
|
mov eax, TSS_SEGMENT
|
|
|
|
ltr ax
|
|
|
|
.6:
|
|
|
|
|
|
|
|
; enable NXE if supported
|
UefiCpuPkg/PiSmmCpuDxeSmm: patch "XdSupported" with PatchInstructionX86()
"mXdSupported" is a global BOOLEAN variable, initialized to TRUE. The
CheckFeatureSupported() function is executed on all processors (not
concurrently though), called from SmmInitHandler(). If XD support is found
to be missing on any CPU, then "mXdSupported" is set to FALSE, and further
processors omit the check. Afterwards, "mXdSupported" is read by several
assembly and C code locations.
The tricky part is *where* "mXdSupported" is allocated (defined):
- Before commit 717fb60443fb ("UefiCpuPkg/PiSmmCpuDxeSmm: Add paging
protection.", 2016-11-17), it used to be a normal global variable,
defined (allocated) in "SmmProfile.c".
- With said commit, we moved the definition (allocation) of "mXdSupported"
into "SmiEntry.nasm". The variable was defined over the last byte of a
"mov al, 1" instruction, so that setting it to FALSE in
CheckFeatureSupported() would patch the instruction to "mov al, 0". The
subsequent conditional jump would change behavior, plus all further read
references to "mXdSupported" (in C and assembly code) would read back
the source (imm8) operand of the patched MOV instruction as data.
This trick required that the MOV instruction be encoded with DB.
In order to get rid of the DB, we have to split both roles: we need a
label for the code patching, and "mXdSupported" has to be defined
(allocated) independently of the code patching. Of course, their values
must always remain in sync.
(1) Reinstate the "mXdSupported" definition and initialization in
"SmmProfile.c" from before commit 717fb60443fb. Change the assembly
language definition ("global") to a declaration ("extern").
(2) Define the "gPatchXdSupported" label (type X86_ASSEMBLY_PATCH_LABEL)
in "SmiEntry.nasm", and add the C-language declaration to
"SmmProfileInternal.h". Replace the DB with the MOV mnemonic (keeping
the imm8 source operand with value 1).
(3) In CheckFeatureSupported(), whenever "mXdSupported" is set to FALSE,
patch the assembly code in sync, with PatchInstructionX86().
Cc: Eric Dong <eric.dong@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=866
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
2018-02-02 00:17:13 +01:00
|
|
|
mov al, strict byte 1 ; source operand may be patched
|
|
|
|
ASM_PFX(gPatchXdSupported):
|
2016-10-23 17:19:52 +02:00
|
|
|
cmp al, 0
|
|
|
|
jz @SkipXd
|
|
|
|
;
|
|
|
|
; Check XD disable bit
|
|
|
|
;
|
|
|
|
mov ecx, MSR_IA32_MISC_ENABLE
|
|
|
|
rdmsr
|
|
|
|
push edx ; save MSR_IA32_MISC_ENABLE[63-32]
|
|
|
|
test edx, BIT2 ; MSR_IA32_MISC_ENABLE[34]
|
|
|
|
jz .5
|
|
|
|
and dx, 0xFFFB ; clear XD Disable bit if it is set
|
|
|
|
wrmsr
|
|
|
|
.5:
|
|
|
|
mov ecx, MSR_EFER
|
|
|
|
rdmsr
|
|
|
|
or ax, MSR_EFER_XD ; enable NXE
|
|
|
|
wrmsr
|
|
|
|
jmp @XdDone
|
|
|
|
@SkipXd:
|
|
|
|
sub esp, 4
|
|
|
|
@XdDone:
|
|
|
|
|
2016-06-14 10:28:14 +02:00
|
|
|
mov ebx, cr0
|
2016-10-23 17:19:52 +02:00
|
|
|
or ebx, 0x80010023 ; enable paging + WP + NE + MP + PE
|
2016-06-14 10:28:14 +02:00
|
|
|
mov cr0, ebx
|
|
|
|
lea ebx, [edi + DSC_OFFSET]
|
|
|
|
mov ax, [ebx + DSC_DS]
|
|
|
|
mov ds, eax
|
|
|
|
mov ax, [ebx + DSC_OTHERSEG]
|
|
|
|
mov es, eax
|
|
|
|
mov fs, eax
|
|
|
|
mov gs, eax
|
|
|
|
mov ax, [ebx + DSC_SS]
|
|
|
|
mov ss, eax
|
|
|
|
|
UefiCpuPkg/PiSmmCpu: Add Shadow Stack Support for X86 SMM.
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1521
We scan the SMM code with ROPgadget.
http://shell-storm.org/project/ROPgadget/
https://github.com/JonathanSalwan/ROPgadget/tree/master
This tool reports the gadget in SMM driver.
This patch enabled CET ShadowStack for X86 SMM.
If CET is supported, SMM will enable CET ShadowStack.
SMM CET will save the OS CET context at SmmEntry and
restore OS CET context at SmmExit.
Test:
1) test Intel internal platform (x64 only, CET enabled/disabled)
Boot test:
CET supported or not supported CPU
on CET supported platform
CET enabled/disabled
PcdCpuSmmCetEnable enabled/disabled
Single core/Multiple core
PcdCpuSmmStackGuard enabled/disabled
PcdCpuSmmProfileEnable enabled/disabled
PcdCpuSmmStaticPageTable enabled/disabled
CET exception test:
#CF generated with PcdCpuSmmStackGuard enabled/disabled.
Other exception test:
#PF for normal stack overflow
#PF for NX protection
#PF for RO protection
CET env test:
Launch SMM in CET enabled/disabled environment (DXE) - no impact to DXE
The test case can be found at
https://github.com/jyao1/SecurityEx/tree/master/ControlFlowPkg
2) test ovmf (both IA32 and X64 SMM, CET disabled only)
test OvmfIa32/Ovmf3264, with -D SMM_REQUIRE.
qemu-system-x86_64.exe -machine q35,smm=on -smp 4
-serial file:serial.log
-drive if=pflash,format=raw,unit=0,file=OVMF_CODE.fd,readonly=on
-drive if=pflash,format=raw,unit=1,file=OVMF_VARS.fd
QEMU emulator version 3.1.0 (v3.1.0-11736-g7a30e7adb0-dirty)
3) not tested
IA32 CET enabled platform
Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yao Jiewen <jiewen.yao@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
Regression-tested-by: Laszlo Ersek <lersek@redhat.com>
2019-02-22 14:30:36 +01:00
|
|
|
mov ebx, [esp + 4] ; ebx <- CpuIndex
|
|
|
|
|
|
|
|
; enable CET if supported
|
|
|
|
mov al, strict byte 1 ; source operand may be patched
|
|
|
|
ASM_PFX(mPatchCetSupported):
|
|
|
|
cmp al, 0
|
|
|
|
jz CetDone
|
|
|
|
|
|
|
|
mov ecx, MSR_IA32_S_CET
|
|
|
|
rdmsr
|
|
|
|
push edx
|
|
|
|
push eax
|
|
|
|
|
|
|
|
mov ecx, MSR_IA32_PL0_SSP
|
|
|
|
rdmsr
|
|
|
|
push edx
|
|
|
|
push eax
|
|
|
|
|
|
|
|
mov ecx, MSR_IA32_S_CET
|
|
|
|
mov eax, MSR_IA32_CET_SH_STK_EN
|
|
|
|
xor edx, edx
|
|
|
|
wrmsr
|
|
|
|
|
|
|
|
mov ecx, MSR_IA32_PL0_SSP
|
|
|
|
mov eax, strict dword 0 ; source operand will be patched
|
|
|
|
ASM_PFX(mPatchCetPl0Ssp):
|
|
|
|
xor edx, edx
|
|
|
|
wrmsr
|
|
|
|
mov ecx, cr0
|
|
|
|
btr ecx, 16 ; clear WP
|
|
|
|
mov cr0, ecx
|
|
|
|
mov [eax], eax ; reload SSP, and clear busyflag.
|
|
|
|
xor ecx, ecx
|
|
|
|
mov [eax + 4], ecx
|
|
|
|
|
|
|
|
mov eax, strict dword 0 ; source operand will be patched
|
|
|
|
ASM_PFX(mPatchCetInterruptSsp):
|
|
|
|
cmp eax, 0
|
|
|
|
jz CetInterruptDone
|
|
|
|
mov [eax], eax ; reload SSP, and clear busyflag.
|
|
|
|
xor ecx, ecx
|
|
|
|
mov [eax + 4], ecx
|
|
|
|
CetInterruptDone:
|
|
|
|
|
|
|
|
mov ecx, cr0
|
|
|
|
bts ecx, 16 ; set WP
|
|
|
|
mov cr0, ecx
|
|
|
|
|
|
|
|
mov eax, 0x668 | CR4_CET
|
|
|
|
mov cr4, eax
|
|
|
|
|
|
|
|
SETSSBSY
|
|
|
|
|
|
|
|
CetDone:
|
2016-06-14 10:28:14 +02:00
|
|
|
|
|
|
|
push ebx
|
|
|
|
mov eax, ASM_PFX(CpuSmmDebugEntry)
|
|
|
|
call eax
|
2016-10-23 17:19:52 +02:00
|
|
|
add esp, 4
|
2016-06-14 10:28:14 +02:00
|
|
|
|
|
|
|
push ebx
|
|
|
|
mov eax, ASM_PFX(SmiRendezvous)
|
|
|
|
call eax
|
2016-10-23 17:19:52 +02:00
|
|
|
add esp, 4
|
|
|
|
|
2016-06-14 10:28:14 +02:00
|
|
|
push ebx
|
|
|
|
mov eax, ASM_PFX(CpuSmmDebugExit)
|
|
|
|
call eax
|
2016-10-23 17:19:52 +02:00
|
|
|
add esp, 4
|
|
|
|
|
UefiCpuPkg/PiSmmCpu: Add Shadow Stack Support for X86 SMM.
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1521
We scan the SMM code with ROPgadget.
http://shell-storm.org/project/ROPgadget/
https://github.com/JonathanSalwan/ROPgadget/tree/master
This tool reports the gadget in SMM driver.
This patch enabled CET ShadowStack for X86 SMM.
If CET is supported, SMM will enable CET ShadowStack.
SMM CET will save the OS CET context at SmmEntry and
restore OS CET context at SmmExit.
Test:
1) test Intel internal platform (x64 only, CET enabled/disabled)
Boot test:
CET supported or not supported CPU
on CET supported platform
CET enabled/disabled
PcdCpuSmmCetEnable enabled/disabled
Single core/Multiple core
PcdCpuSmmStackGuard enabled/disabled
PcdCpuSmmProfileEnable enabled/disabled
PcdCpuSmmStaticPageTable enabled/disabled
CET exception test:
#CF generated with PcdCpuSmmStackGuard enabled/disabled.
Other exception test:
#PF for normal stack overflow
#PF for NX protection
#PF for RO protection
CET env test:
Launch SMM in CET enabled/disabled environment (DXE) - no impact to DXE
The test case can be found at
https://github.com/jyao1/SecurityEx/tree/master/ControlFlowPkg
2) test ovmf (both IA32 and X64 SMM, CET disabled only)
test OvmfIa32/Ovmf3264, with -D SMM_REQUIRE.
qemu-system-x86_64.exe -machine q35,smm=on -smp 4
-serial file:serial.log
-drive if=pflash,format=raw,unit=0,file=OVMF_CODE.fd,readonly=on
-drive if=pflash,format=raw,unit=1,file=OVMF_VARS.fd
QEMU emulator version 3.1.0 (v3.1.0-11736-g7a30e7adb0-dirty)
3) not tested
IA32 CET enabled platform
Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yao Jiewen <jiewen.yao@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
Regression-tested-by: Laszlo Ersek <lersek@redhat.com>
2019-02-22 14:30:36 +01:00
|
|
|
mov eax, ASM_PFX(mCetSupported)
|
|
|
|
mov al, [eax]
|
|
|
|
cmp al, 0
|
|
|
|
jz CetDone2
|
|
|
|
|
|
|
|
mov eax, 0x668
|
|
|
|
mov cr4, eax ; disable CET
|
|
|
|
|
|
|
|
mov ecx, MSR_IA32_PL0_SSP
|
|
|
|
pop eax
|
|
|
|
pop edx
|
|
|
|
wrmsr
|
|
|
|
|
|
|
|
mov ecx, MSR_IA32_S_CET
|
|
|
|
pop eax
|
|
|
|
pop edx
|
|
|
|
wrmsr
|
|
|
|
CetDone2:
|
|
|
|
|
2016-10-23 17:19:52 +02:00
|
|
|
mov eax, ASM_PFX(mXdSupported)
|
|
|
|
mov al, [eax]
|
|
|
|
cmp al, 0
|
|
|
|
jz .7
|
|
|
|
pop edx ; get saved MSR_IA32_MISC_ENABLE[63-32]
|
|
|
|
test edx, BIT2
|
|
|
|
jz .7
|
|
|
|
mov ecx, MSR_IA32_MISC_ENABLE
|
|
|
|
rdmsr
|
|
|
|
or dx, BIT2 ; set XD Disable bit if it was set before entering into SMM
|
|
|
|
wrmsr
|
|
|
|
|
|
|
|
.7:
|
UefiCpuPkg/PiSmmCpu: Add Shadow Stack Support for X86 SMM.
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1521
We scan the SMM code with ROPgadget.
http://shell-storm.org/project/ROPgadget/
https://github.com/JonathanSalwan/ROPgadget/tree/master
This tool reports the gadget in SMM driver.
This patch enabled CET ShadowStack for X86 SMM.
If CET is supported, SMM will enable CET ShadowStack.
SMM CET will save the OS CET context at SmmEntry and
restore OS CET context at SmmExit.
Test:
1) test Intel internal platform (x64 only, CET enabled/disabled)
Boot test:
CET supported or not supported CPU
on CET supported platform
CET enabled/disabled
PcdCpuSmmCetEnable enabled/disabled
Single core/Multiple core
PcdCpuSmmStackGuard enabled/disabled
PcdCpuSmmProfileEnable enabled/disabled
PcdCpuSmmStaticPageTable enabled/disabled
CET exception test:
#CF generated with PcdCpuSmmStackGuard enabled/disabled.
Other exception test:
#PF for normal stack overflow
#PF for NX protection
#PF for RO protection
CET env test:
Launch SMM in CET enabled/disabled environment (DXE) - no impact to DXE
The test case can be found at
https://github.com/jyao1/SecurityEx/tree/master/ControlFlowPkg
2) test ovmf (both IA32 and X64 SMM, CET disabled only)
test OvmfIa32/Ovmf3264, with -D SMM_REQUIRE.
qemu-system-x86_64.exe -machine q35,smm=on -smp 4
-serial file:serial.log
-drive if=pflash,format=raw,unit=0,file=OVMF_CODE.fd,readonly=on
-drive if=pflash,format=raw,unit=1,file=OVMF_VARS.fd
QEMU emulator version 3.1.0 (v3.1.0-11736-g7a30e7adb0-dirty)
3) not tested
IA32 CET enabled platform
Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yao Jiewen <jiewen.yao@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
Regression-tested-by: Laszlo Ersek <lersek@redhat.com>
2019-02-22 14:30:36 +01:00
|
|
|
|
2018-08-16 03:32:10 +02:00
|
|
|
StuffRsb32
|
2016-06-14 10:28:14 +02:00
|
|
|
rsm
|
|
|
|
|
|
|
|
ASM_PFX(gcSmiHandlerSize): DW $ - _SmiEntryPoint
|
|
|
|
|
2018-01-11 10:05:15 +01:00
|
|
|
global ASM_PFX(PiSmmCpuSmiEntryFixupAddress)
|
|
|
|
ASM_PFX(PiSmmCpuSmiEntryFixupAddress):
|
|
|
|
ret
|