2015-11-25 05:23:01 +01:00
|
|
|
/** @file
|
|
|
|
SMM CPU misc functions for x64 arch specific.
|
2018-06-27 15:14:20 +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) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
|
2019-04-04 01:07:22 +02:00
|
|
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
2015-11-25 05:23:01 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "PiSmmCpuDxeSmm.h"
|
|
|
|
|
2016-10-23 17:19:52 +02:00
|
|
|
EFI_PHYSICAL_ADDRESS mGdtBuffer;
|
|
|
|
UINTN mGdtBufferSize;
|
|
|
|
|
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 BOOLEAN mCetSupported;
|
|
|
|
extern UINTN mSmmShadowStackSize;
|
|
|
|
|
|
|
|
X86_ASSEMBLY_PATCH_LABEL mPatchCetPl0Ssp;
|
|
|
|
X86_ASSEMBLY_PATCH_LABEL mPatchCetInterruptSsp;
|
|
|
|
X86_ASSEMBLY_PATCH_LABEL mPatchCetInterruptSspTable;
|
|
|
|
UINT32 mCetPl0Ssp;
|
|
|
|
UINT32 mCetInterruptSsp;
|
|
|
|
UINT32 mCetInterruptSspTable;
|
|
|
|
|
|
|
|
UINTN mSmmInterruptSspTables;
|
|
|
|
|
2016-10-23 17:19:52 +02:00
|
|
|
/**
|
|
|
|
Initialize IDT for SMM Stack Guard.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
EFIAPI
|
|
|
|
InitializeIDTSmmStackGuard (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
IA32_IDT_GATE_DESCRIPTOR *IdtGate;
|
|
|
|
|
|
|
|
//
|
|
|
|
// If SMM Stack Guard feature is enabled, set the IST field of
|
|
|
|
// the interrupt gate for Page Fault Exception to be 1
|
|
|
|
//
|
|
|
|
IdtGate = (IA32_IDT_GATE_DESCRIPTOR *)gcSmiIdtr.Base;
|
|
|
|
IdtGate += EXCEPT_IA32_PAGE_FAULT;
|
|
|
|
IdtGate->Bits.Reserved_0 = 1;
|
|
|
|
}
|
|
|
|
|
2015-11-25 05:23:01 +01:00
|
|
|
/**
|
|
|
|
Initialize Gdt for all processors.
|
2018-06-27 15:14:20 +02:00
|
|
|
|
2015-11-25 05:23:01 +01:00
|
|
|
@param[in] Cr3 CR3 value.
|
|
|
|
@param[out] GdtStepSize The step size for GDT table.
|
|
|
|
|
|
|
|
@return GdtBase for processor 0.
|
|
|
|
GdtBase for processor X is: GdtBase + (GdtStepSize * X)
|
|
|
|
**/
|
|
|
|
VOID *
|
|
|
|
InitGdt (
|
|
|
|
IN UINTN Cr3,
|
|
|
|
OUT UINTN *GdtStepSize
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINTN Index;
|
|
|
|
IA32_SEGMENT_DESCRIPTOR *GdtDescriptor;
|
|
|
|
UINTN TssBase;
|
|
|
|
UINTN GdtTssTableSize;
|
|
|
|
UINT8 *GdtTssTables;
|
|
|
|
UINTN GdtTableStepSize;
|
|
|
|
|
|
|
|
//
|
|
|
|
// For X64 SMM, we allocate separate GDT/TSS for each CPUs to avoid TSS load contention
|
|
|
|
// on each SMI entry.
|
|
|
|
//
|
|
|
|
GdtTssTableSize = (gcSmiGdtr.Limit + 1 + TSS_SIZE + 7) & ~7; // 8 bytes aligned
|
2016-10-23 17:19:52 +02:00
|
|
|
mGdtBufferSize = GdtTssTableSize * gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus;
|
|
|
|
GdtTssTables = (UINT8*)AllocateCodePages (EFI_SIZE_TO_PAGES (mGdtBufferSize));
|
2015-11-25 05:23:01 +01:00
|
|
|
ASSERT (GdtTssTables != NULL);
|
2016-10-23 17:19:52 +02:00
|
|
|
mGdtBuffer = (UINTN)GdtTssTables;
|
2015-11-25 05:23:01 +01:00
|
|
|
GdtTableStepSize = GdtTssTableSize;
|
|
|
|
|
|
|
|
for (Index = 0; Index < gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus; Index++) {
|
|
|
|
CopyMem (GdtTssTables + GdtTableStepSize * Index, (VOID*)(UINTN)gcSmiGdtr.Base, gcSmiGdtr.Limit + 1 + TSS_SIZE);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Fixup TSS descriptors
|
|
|
|
//
|
|
|
|
TssBase = (UINTN)(GdtTssTables + GdtTableStepSize * Index + gcSmiGdtr.Limit + 1);
|
|
|
|
GdtDescriptor = (IA32_SEGMENT_DESCRIPTOR *)(TssBase) - 2;
|
|
|
|
GdtDescriptor->Bits.BaseLow = (UINT16)(UINTN)TssBase;
|
|
|
|
GdtDescriptor->Bits.BaseMid = (UINT8)((UINTN)TssBase >> 16);
|
|
|
|
GdtDescriptor->Bits.BaseHigh = (UINT8)((UINTN)TssBase >> 24);
|
|
|
|
|
|
|
|
if (FeaturePcdGet (PcdCpuSmmStackGuard)) {
|
|
|
|
//
|
|
|
|
// Setup top of known good stack as IST1 for each processor.
|
|
|
|
//
|
|
|
|
*(UINTN *)(TssBase + TSS_X64_IST1_OFFSET) = (mSmmStackArrayBase + EFI_PAGE_SIZE + Index * mSmmStackSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*GdtStepSize = GdtTableStepSize;
|
|
|
|
return GdtTssTables;
|
|
|
|
}
|
2016-11-10 06:40:12 +01:00
|
|
|
|
2016-11-11 06:11:22 +01:00
|
|
|
/**
|
|
|
|
Get Protected mode code segment from current GDT table.
|
|
|
|
|
|
|
|
@return Protected mode code segment value.
|
|
|
|
**/
|
|
|
|
UINT16
|
|
|
|
GetProtectedModeCS (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
IA32_DESCRIPTOR GdtrDesc;
|
|
|
|
IA32_SEGMENT_DESCRIPTOR *GdtEntry;
|
|
|
|
UINTN GdtEntryCount;
|
|
|
|
UINT16 Index;
|
|
|
|
|
|
|
|
AsmReadGdtr (&GdtrDesc);
|
|
|
|
GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);
|
|
|
|
GdtEntry = (IA32_SEGMENT_DESCRIPTOR *) GdtrDesc.Base;
|
|
|
|
for (Index = 0; Index < GdtEntryCount; Index++) {
|
|
|
|
if (GdtEntry->Bits.L == 0) {
|
|
|
|
if (GdtEntry->Bits.Type > 8 && GdtEntry->Bits.L == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GdtEntry++;
|
|
|
|
}
|
2018-10-28 09:51:23 +01:00
|
|
|
ASSERT (Index != GdtEntryCount);
|
2016-11-11 06:11:22 +01:00
|
|
|
return Index * 8;
|
|
|
|
}
|
|
|
|
|
2016-11-10 06:40:12 +01:00
|
|
|
/**
|
|
|
|
Transfer AP to safe hlt-loop after it finished restore CPU features on S3 patch.
|
|
|
|
|
2016-11-17 21:41:35 +01:00
|
|
|
@param[in] ApHltLoopCode The address of the safe hlt-loop function.
|
|
|
|
@param[in] TopOfStack A pointer to the new stack to use for the ApHltLoopCode.
|
|
|
|
@param[in] NumberToFinishAddress Address of Semaphore of APs finish count.
|
2016-11-10 06:40:12 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
TransferApToSafeState (
|
2016-11-17 21:41:35 +01:00
|
|
|
IN UINTN ApHltLoopCode,
|
|
|
|
IN UINTN TopOfStack,
|
|
|
|
IN UINTN NumberToFinishAddress
|
2016-11-10 06:40:12 +01:00
|
|
|
)
|
|
|
|
{
|
2016-11-11 06:11:22 +01:00
|
|
|
AsmDisablePaging64 (
|
|
|
|
GetProtectedModeCS (),
|
2016-11-17 21:41:35 +01:00
|
|
|
(UINT32)ApHltLoopCode,
|
|
|
|
(UINT32)NumberToFinishAddress,
|
2016-11-11 06:11:22 +01:00
|
|
|
0,
|
2016-11-17 21:41:35 +01:00
|
|
|
(UINT32)TopOfStack
|
2016-11-10 06:40:12 +01:00
|
|
|
);
|
|
|
|
//
|
|
|
|
// It should never reach here
|
|
|
|
//
|
|
|
|
ASSERT (FALSE);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
Initialize the shadow stack related data structure.
|
|
|
|
|
|
|
|
@param CpuIndex The index of CPU.
|
|
|
|
@param ShadowStack The bottom of the shadow stack for this CPU.
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
InitShadowStack (
|
|
|
|
IN UINTN CpuIndex,
|
|
|
|
IN VOID *ShadowStack
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINTN SmmShadowStackSize;
|
|
|
|
UINT64 *InterruptSspTable;
|
|
|
|
|
|
|
|
if ((PcdGet32 (PcdControlFlowEnforcementPropertyMask) != 0) && mCetSupported) {
|
|
|
|
SmmShadowStackSize = EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (PcdGet32 (PcdCpuSmmShadowStackSize)));
|
|
|
|
if (FeaturePcdGet (PcdCpuSmmStackGuard)) {
|
|
|
|
SmmShadowStackSize += EFI_PAGES_TO_SIZE (2);
|
|
|
|
}
|
|
|
|
mCetPl0Ssp = (UINT32)((UINTN)ShadowStack + SmmShadowStackSize - sizeof(UINT64));
|
|
|
|
PatchInstructionX86 (mPatchCetPl0Ssp, mCetPl0Ssp, 4);
|
|
|
|
DEBUG ((DEBUG_INFO, "mCetPl0Ssp - 0x%x\n", mCetPl0Ssp));
|
|
|
|
DEBUG ((DEBUG_INFO, "ShadowStack - 0x%x\n", ShadowStack));
|
|
|
|
DEBUG ((DEBUG_INFO, " SmmShadowStackSize - 0x%x\n", SmmShadowStackSize));
|
|
|
|
|
|
|
|
if (FeaturePcdGet (PcdCpuSmmStackGuard)) {
|
|
|
|
if (mSmmInterruptSspTables == 0) {
|
|
|
|
mSmmInterruptSspTables = (UINTN)AllocateZeroPool(sizeof(UINT64) * 8 * gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus);
|
|
|
|
ASSERT (mSmmInterruptSspTables != 0);
|
|
|
|
DEBUG ((DEBUG_INFO, "mSmmInterruptSspTables - 0x%x\n", mSmmInterruptSspTables));
|
|
|
|
}
|
|
|
|
mCetInterruptSsp = (UINT32)((UINTN)ShadowStack + EFI_PAGES_TO_SIZE(1) - sizeof(UINT64));
|
|
|
|
mCetInterruptSspTable = (UINT32)(UINTN)(mSmmInterruptSspTables + sizeof(UINT64) * 8 * CpuIndex);
|
|
|
|
InterruptSspTable = (UINT64 *)(UINTN)mCetInterruptSspTable;
|
|
|
|
InterruptSspTable[1] = mCetInterruptSsp;
|
|
|
|
PatchInstructionX86 (mPatchCetInterruptSsp, mCetInterruptSsp, 4);
|
|
|
|
PatchInstructionX86 (mPatchCetInterruptSspTable, mCetInterruptSspTable, 4);
|
|
|
|
DEBUG ((DEBUG_INFO, "mCetInterruptSsp - 0x%x\n", mCetInterruptSsp));
|
|
|
|
DEBUG ((DEBUG_INFO, "mCetInterruptSspTable - 0x%x\n", mCetInterruptSspTable));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|