jiewen yao - smm protection in edkii intel yao - smm...smm heap 2(entropy) random fixed length...

35
presented by SMM Protection in EDK II Spring 2017 UEFI Seminar and Plugfest March 27 31, 2017 Jiewen Yao, Intel Corporation UEFI Plugfest – March 2017 www.uefi.org 1 Updated 20110601

Upload: hoanghuong

Post on 01-Apr-2018

245 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

presented  by

SMM  Protection  in  EDK  II

Spring  2017  UEFI  Seminar  and  PlugfestMarch  27  -­ 31,  2017

Jiewen  Yao,  Intel  Corporation

UEFI  Plugfest  – March  2017 www.uefi.org 1

Updated  2011-­‐06-­‐01

Page 2: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Agenda

• Known  SMM  Attacks• More  Protection–SMM  Memory  Protection–CommBuffer Enforcement–ASLR  in  SMM–Guard  Page–Reduce  SMI  Handler

• Summary  /  Call  to  ActionUEFI  Plugfest  – March  2017 www.uefi.org 2

Page 3: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

What  is  SMM  and  SMI?• System  Management  Mode  (SMM)– Is  a  special  CPU  operating  mode.– Is  inside  of  a  special  SMM  memory  (SMRAM)– Access  the  whole  system  memory  and  IO,  including  OS  memory  and  hypervisor  memory.

– Is  invoked  through  a  System  Management  Interrupt  (SMI)

– Has  software  executive  (SMI  handler)  to  perform  operation  based  upon  different  SMI.

UEFI  Plugfest  – March  2017 www.uefi.org 3

Page 4: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Known  SMM  Attacks

UEFI  Plugfest  – March  2017 www.uefi.org 4

SMM  Attack Description ExampleSMRAM  is  unlocked

An  attacker  can  set  register  to  unlock  SMRAM,  and  override  SMRAM.

A.1, A.2

Cache  Poisoning An  attacker  can  set  CPU  cache  to  override  SMRAM.

A.3, A.4

SMRAM  remap An  attacker  can  control chipset  register  to  remap  a  normal  system  memory  to  SMRAM.

A.5

Branch  Outside  of  SMRAM

SMM  code  calls  outside  of  SMRAM, which  is  controlled  by  the  attacker.

A.6,  A.7

SMM  Communication  Buffer Attack

SMM  code uses  SMM  communication  buffer  to  exchange  information  with  non-­‐SMM  agent.  The  attacker  can  give  a  malicious  communication  buffer  to  SMM,  and  the  SMM  may  write  SMRAM  or  Virtual  Machine  Monitor  (VMM).

A.8,  A.9

Page 5: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Known  Mitigation

UEFI  Plugfest  – March  2017 www.uefi.org 5

SMM  Attack MitigationSMRAM  is  unlocked 1)  Lock  SMRAM  at  PI  SmmReadyToLock.Cache  Poisoning 1) Enable  SMM Range  Register.SMRAM  remap 1)  Lock  Remap  register.Branch  Outside  of  SMRAM

1)  Enable  Smm_Code_Access  Register.2)  Setup  Non-­‐Executable  (NX)  paging  outside  of  SMM.

SMM  Communication  Attack

1)  Check  SMM  Communication  Buffer.2)  Check  MemoyMapped IO  (MMIO)  bar access.

New  methods  may  be  discovered

Page 6: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

SMM  Memory  Protection

UEFI  Plugfest  – March  2017 www.uefi.org 6

Page 7: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Current  SMRAM  Layout

UEFI  Plugfest  – March  2017 www.uefi.org 7

MSEGPiSmmCore (PE/COFF)SMM  Driver  (PE/COFF)PiSmmCpu (PE/COFF)

SMM  Save  StateSMM  Stack

SMM  Driver  (PE/COFF)

SMM  S3  Resume  State

…Other  Heap  Data

SMM  Page  TableSMM  IDT/GDT

• Every  page  in  SMRAM  is  read/write• Every  page  in  SMRAM  is  executable

Page 8: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

SMM  Memory  Protection

UEFI  Plugfest  – March  2017 www.uefi.org 8

SMM  Memory  Protection

MSEGPiSmmCore (PE/COFF)SMM  Driver  (PE/COFF)PiSmmCpu (PE/COFF)

SMM  Save  StateSMM  Stack

SMM  Driver  (PE/COFF)

SMM  S3  Resume  State

TSEG

…Other  Heap  Data

SMM  Page  TableSMM  IDT/GDT

ROXD

PE  HeaderPE  CodePE  Data

CPU  1  Save  StateCPU  m  SMI  EntryCPU  0  Save  StateCPU  m-­‐1  SMI  Entry

CPU  1  SMI  Entry

CPU  0  SMI  Entry

CPU  m-­‐1  Save  StateCPU  2m-­‐2  SMI  Entry

CPU  m  Save  StateCPU  2m  -­‐ 1  SMI  Entry

pad

pad

Page 9: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

SMM  Memory  Protection• Using  static  page  table• Set  NonExecutable  (NX)  for  data

• Set  ReadOnly(RO)  for  code

• Protect  page  itself

• SMM  driver  can  protect  its  own  critical  data  in  ReadOnly(RO)  memory

UEFI  Plugfest  – March  2017 www.uefi.org 9

PE  Code

Page  Table SMM  EntryPoint

IDT/GDT

Data  (Stack/Heap)

RO-­‐Data

PE  Data

Save  State

Page 10: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

SMM  Memory  Protection• Prevents  code  injection• Protects  critical  data  (read-­only)

• Limitations– Return-­oriented  programming  (ROP)  attack.– Size  overhead• PE  image:  6K  *  SmmImageCount  (average)• Static  Page  Table:  2M  (1G  paging  for  48bit)

UEFI  Plugfest  – March  2017 www.uefi.org 10

Page 11: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

CommBuffer  Enforcement

UEFI  Plugfest  – March  2017 www.uefi.org 11

Page 12: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

SMM  CommBuffer  Attack

UEFI  Plugfest  – March  2017 www.uefi.org 12

SMM

VMM

OS

CommBuffer  Pointer

MMIO  Bar

Page 13: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Current  CommBuffer  Check

UEFI  Plugfest  – March  2017 www.uefi.org 13

• SMI  handler  MUST  check  SMM  communication  buffer  content  by  writing  code  like  below:

if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {

DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer in SMRAM or overflow!\n"));

return EFI_SUCCESS;

}

• But  if  the  check  is  missing,  there  is  no  way  to  detect  such  problem.

Page 14: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

CommBuffer  Enforcement

UEFI  Plugfest  – March  2017 www.uefi.org 14

SMRAM

SMMPage  Table

MMIO

Reserved

RuntimeCodeRuntimeData

Not-­‐Present

BootCodeBootData

BootCodeBootData

LoaderCodeLoaderData

ACPINvs

ACPI  Reclaim

Policy  Enforcement

Valid  SmmComm  

Buffer

Valid  MMIO

Page 15: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

CommBuffer  Enforcement• Resist  comm  buffer  attack  even  the  CommBuffer  check  is  missing  in  SMM  driver

• Protects  hypervisors

• Limitation– This  enforcement  is  not  applied  to  hotplug  memory,  which  is  still  read/write.

– MMIO  region  is  mapped.  SMI  handler  need  make  sure  MMIO  bar  point  to  a  valid  region.

UEFI  Plugfest  – March  2017 www.uefi.org 15

Page 16: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Address  Space  Layout  Randomization  (ASLR)  in  SMM

UEFI  Plugfest  – March  2017 www.uefi.org 16

Page 17: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Current  SMM  Layout

UEFI  Plugfest  – March  2017 www.uefi.org 17

MSEGPiSmmCore (PE/COFF)SMM  Driver  (PE/COFF)PiSmmCpu (PE/COFF)

SMM  Save  StateSMM  Stack

SMM  Driver  (PE/COFF)

SMM  S3  Resume  State

…Other  Heap  Data

SMM  Page  TableSMM  IDT/GDT

• The  layout  is  fixed.

• This  attack  may  find  out  a  sequence  of  instruction  existed  in  code  region  (gadgets)  and  execute.  (ROP)

• This  ROP  attack  can  bypass  NX/RO  protection.

Page 18: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Image  Shuffle

UEFI  Plugfest  – March  2017 www.uefi.org 18

Image  AImage  CImage  BImage  D

Image  CImage  BImage  DImage  A

Image  DImage  BImage  AImage  C

1st Boot 2nd Boot 3rd Boot

As  such,  it  makes  difficult  for  attacker  to  locate  gadgets  for  ROP  attack.

Page 19: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Heap  Shift

UEFI  Plugfest  – March  2017 www.uefi.org 19

MAX - Random

2(Entropy)SmmCore Fixed Length

MAX - Random

SMM Heap

2(Entropy)

Random

Fixed Length

Random

MAX-Random

Stack

Random

MAX-Random

PageTable

Random

It  makes  difficult  for  attacker  to  locate  gadget  for  ROP  attack.

Page 20: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

ASLR  in  SMM• Make  Buffer  Overflow/ROP  attack  harder,  because  the  memory  layout  is  changed  in  each  boot.

• Limitations– SMM  is  a  resource  constrained  environment.  Entropy  for  Heap  Shift  might  be  not  so  big.

– Information  leakage  in  SMM  (LoadedImageProtocol)

UEFI  Plugfest  – March  2017 www.uefi.org 20

Page 21: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Guard  Page

UEFI  Plugfest  – March  2017 www.uefi.org 21

Page 22: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Current  Memory  Allocation

UEFI  Plugfest  – March  2017 www.uefi.org 22

POOL_TAIL

AllocatedPool

POOL_HEAD

AllocatedPage

POOL_TAIL

AllocatedPool

POOL_HEAD

AllocatedPage

• Page  overflow  cannot  be  detected

• Pool  overflow  can  only  be  detected  when  memory  is  freed,  because  of  POOL_TAIL  signature  check  at  FreePool()

Page 23: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

New  Page  Allocation

UEFI  Plugfest  – March  2017 www.uefi.org 23

Guard  Page(Not  Present)

AllocatedPages

One Allocation forAllocatePages()

2 guard pages (8K)

Guard  Page(Not  Present)

Page 24: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

New  pool  allocation

UEFI  Plugfest  – March  2017 www.uefi.org 24

One Allocation forAllocatePool()

2 guard pages (8K)+ 4K page alignment

POOL_TAIL(ptal)

AllocatedPool

POOL_HEAD(phd0)

Guard  Page(Not  Present)

Guard  Page(Not  Present)

Page 25: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Guard  Page• Catch  page  overflows  when  they  happen• Catch  pool  overflows  when  they  happen

• Limitation– Memory  size  overhead• Additional  8K  for  each  page  allocation.• Additional  8K+4K  alignment  for  each  pool  allocation.• It  might  need  above  128M  SMRAM.

– A  debug  feature,  because  of  size  overhead.

UEFI  Plugfest  – March  2017 www.uefi.org 25

Page 26: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Reduce  SMI  Handler

UEFI  Plugfest  – March  2017 www.uefi.org 26

Page 27: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

SMI  Handlers

• SMI  Handler  ==  Attack  Surface

• Question:–How  many  SMI  handlers  in  the  BIOS?–How  many  Root  SMI  handlers,  GUID  handlers,  software  SMI  handlers,  ……  ?

UEFI  Plugfest  – March  2017 www.uefi.org 27

Page 28: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

SMI  Handler  Profile

UEFI  Plugfest  – March  2017 www.uefi.org 28

SmmCore

SmmChildDispatcherSmiHandlerProfile

SmiManager

DriverDispather

Profile  Protocol

Guid/RootSmiDatabase

Profile  Handler

MemoryManager

Hardware  SmiDatabase

HardwareSmiDatabase

SmiProfile.efi(UEFI  Shell  App)

SmiHandleProfile(Temp)

SmiProfile.py(OS  Tool)

SmiHandleProfileReportRVA=>Symbol

PDB  file

Page 29: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

SMI  Handler  Profile• Developer  can  check  if  the  SMI  handler  is  necessary

• Test  engineer  can  use  it  for  validation

• Limitation–Only  used  as  a  debug  feature  (info  leakage)– The  profile  only  shows  info,  which  requires  further  analysis

UEFI  Plugfest  – March  2017 www.uefi.org 29

Page 30: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Summary• SMM  is  a  target  due  to  high  execution  privilege

• There  are  known  SMM  attacks  and  mitigations

• Developers  can  do  more  to  protect  SMM– SMM  Memory  Protection– CommBuffer Enforcement– ASLR  in  SMM– Guard  Page– Reduce  Number  of  SMI  Handlers

UEFI  Plugfest  – March  2017 www.uefi.org 30

Page 31: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Call  To  Action• Adopt  “SMM  Memory  Protection”  and  “CommBuffer  enforcement”  to  harden  the  platform.  [P.1][P.2]

• Use  “SMI  handler  profile”  to  audit  the  SMI  handlers.  [P.3]

• Evaluate  “ASLR  in  SMM”  and  resolve  information  leakage.  [P.4]

• Use  “GuardPage”  to  validate  buffer  overflow.  [P.4]

UEFI  Plugfest  – March  2017 www.uefi.org 31

Page 32: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Acknowledgement• Some  content  of  the  material  is  discussed  with  UEFI  BIOS  and  security  experts

• Special  thanks  to  Vincent  Zimmer  (Intel),  Kirt Brannock (Intel),  Jeremiah  Cox  (Microsoft),  Sean Brogan  (Microsoft)

UEFI  Plugfest  – March  2017 www.uefi.org 32

Page 33: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Reference• Attacks

– [A.1]  Using  CPU  SMM  to  Circumvent  OS  Security  Functions  (http://fawlty.cs.usfca.edu/~cruse/cs630f06/duflot.pdf)

– [A.2]  Using  SMM  For  Other  Purposes  (http://phrack.org/issues/65/7.html)– [A.3]  Attacking  SMM  Memory  via  Intel  Cache  Poisoning  (http://invisiblethingslab.com/resources/misc09/smm_cache_fun.pdf)

– [A.4]  Getting  Into  the  SMRAM:  SMM  Reloaded  (http://www.politicalavenue.com/libraryebooks/cryptology-­and-­cryptography/csw09-­duflot.pdf)

– [A.5]  Attacking-­Intel-­TXT  (http://invisiblethingslab.com/resources/bh09dc/Attacking%20Intel%20TXT%20-­%20slides.pdf )

– [A.6]  BIOS  SMM  Privilege  Escalation  Vulnerabilities  (http://www.securityfocus.com/archive/1/505590)

– [A.7]  System  Management  Mode  Design  and  Security  Issues  (http://www.ssi.gouv.fr/uploads/IMG/pdf/IT_Defense_2010_final.pdf)

– [A.8]  A  New  Class  of  Vulnerabilities  in  SMI  Handlers  (https://cansecwest.com/slides/2015/A%20New%20Class%20of%20Vulnin%20SMI%20-­%20Andrew%20Furtak.pdf)

– [A.9]  BARingthe  System  (http://www.intelsecurity.com/advanced-­threat-­research/content/data/REConBrussels2017_BARing_the_system.pdf)

UEFI  Plugfest  – March  2017 www.uefi.org 33

Page 34: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Reference• Protection:

– [P.1]  A_Tour_Beyond_BIOS_Secure_SMM_Communication(https://github.com/tianocore-­docs/Docs/raw/master/White_Papers/A_Tour_Beyond_BIOS_Secure_SMM_Communication.pdf)– [P.2]  A_Tour_Beyond_BIOS_Memory_Protection_in_UEFI(https://www.gitbook.com/book/edk2-­docs/a-­tour-­beyond-­bios-­memory-­protection-­in-­uefi-­bios/details)  – [P.3]  SMI  Handler  Profile  Feature(https://github.com/tianocore/tianocore.github.io/wiki/SMI-­handler-­profile-­feature)  – [P.4]  A_Tour_Beyond_BIOS_Securiy_Enhancement_to_Mitigate_Buffer_Overflow_in_UEFI

(https://github.com/tianocore-­docs/Docs/raw/master/White_Papers/A_Tour_Beyond_BIOS_Securiy_Enhancement_to_Mitigate_Buffer_Overflow_in_UEFI.pdf)  

UEFI  Plugfest  – March  2017 www.uefi.org 34

Page 35: Jiewen Yao - SMM Protection in EDKII Intel Yao - SMM...SMM Heap 2(Entropy) Random Fixed Length Random MAX-Random Stack Random ... Jiewen Yao - SMM Protection in EDKII_Intel Created

Thanks  for  attending  the  Spring  2017  UEFI  Seminar  and  Plugfest

For  more  information  on  the  UEFI  Forum  and  UEFI  Specifications,  visit  http://www.uefi.org

presented  by

UEFI  Plugfest  – March  2017 www.uefi.org 35