Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks
Jon Masters, Computer Architect, Red Hat, Inc. jcm@redhat.com | @jonmasters
Exploiting modern microarchitectures: Meltdown, Spectre, and other - - PowerPoint PPT Presentation
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks Jon Masters, Computer Architect, Red Hat, Inc. jcm@redhat.com | @jonmasters 2 Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks Overview
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks
Jon Masters, Computer Architect, Red Hat, Inc. jcm@redhat.com | @jonmasters
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 2
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 3
Overview
Today's lecture will cover the following:
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 5
Architecture
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 6
Common concepts in modern architectures
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 7
Common concepts in modern architectures
uses to automatically translate virtual addresses into physical memory addresses
demand loaded, or because the application does not have permission to access that address
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 8
Examples of computer architectures
registers (except FS/GS)
for all operations (fjrst load from memory)
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 10
Elements of a modern System-on-Chip (SoC)
D D R M E M D D R M E M LLC L2 $ C1 C2 L2 $ C1 C2 L2 $ C1 C2 L2 $ C1 C2
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 11
Elements of a modern System-on-Chip (SoC)
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 12
Microarchitecture
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 13
Elements of a modern in-order core
L1 I$
Instruction Fetch Instruction Decode
Branch Predictor Instruction Execute Register File Memory Access Writeback
L1 D$
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 14
In order microarchitectures
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 15
An in-order pipeline visualized
IF ID EX MEM WB IF ID EX MEM WB IF ID EX MEM WB IF ID EX MEM WB IF ID EX MEM WB
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 16
In order microarchitectures (continued)
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 17
Elements of a modern out-of-order core
L1 I$
Instruction Fetch Instruction Decode
Branch Predictor
Register Renaming (ROB)
Integer Physical Register File Vector Physical Register File
L1 D$ Execution Units L2 $
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 18
Out-of-Order (OoO) microarchitectures
register fjle and a Register Alias Table (RAT)
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 19
Out-of-Order (OoO) microarchitectures (cont.)
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 20
Microarchitecture (continued)
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 21
Examples of computer microarchitectures
* Typical is 4uops with rate exception
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 23
Virtual memory
0x7000 0x6000 0x5000 0x4000 0x3000 0x2000 0x1000 0x0000 Process A Process B Page Tables Page Tables Physical Memory
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 24
Virtual memory
existing TLB entries are invalidated. Cache fmushing may be required depending upon the use of address space IDs (ASIDs, PCIDs, etc.) in the architecture and the Operating System
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 25
Virtual memory
automatically using metadata from the ELF (Executable Linkng Format) application binary
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 26
Caches
D D R M E M D D R M E M LLC L2 $ C1 C2 L2 $ C1 C2 L2 $ C1 C2 L2 $ C1 C2
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 27
Caches
... 0x4080 0x4040 0x4000 ... 0x0080 0x0040 0x0000 Physical Memory 0x4000 TLB 0x1000 0x1000 0x040 Virtual Index DATA Physical Tag Cached Data
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 28
Caches
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 29
Caches
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 30
Caches as side channels
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 31
Caches as side channels
time = rdtsc(); maccess(&data[0x300]); delta3 = rdtsc() - time; time = rdtsc(); maccess(&data[0x200]); delta2 = rdtsc() - time;
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 32
Caches as side channels (continued)
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 33
Caches as side channels (continued)
asm volatile ("prefetcht0 (%0)" : : "r" (p)); asm volatile ("prefetcht1 (%0)" : : "r" (p)); asm volatile ("prefetcht2 (%0)" : : "r" (p)); asm volatile ("prefetchnta (%0)" : : "r" (p));
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 35
Branch prediction
LOAD “raining” FLAGS? CMP “raining” Condition Flags take_umbrella() True False
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 36
Branch prediction
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 37
Speculative Execution
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 38
Branch prediction and speculation
1) If the predicted branch was correct, speculated instructions can be retired
2) If the predicted branch was incorrect, speculated instructions can be discarded
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 39
Conditional branches
movq $0, %rax loop: incq %rax cmpq $10, %rax jle loop
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 40
Conditional branch prediction
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 41
Conditional branch prediction
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 42
Indirect branch prediction
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 43
Branch predictor optimization
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 44
Speculation in modern processors
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 46
Meltdown and Spectre microarchitecture vulnerabilities
vulnerabilities discovered in common industry-wide microprocessor optimizations
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 47
Meltdown and Spectre microarchitecture vulnerabilities
$ grep . /sys/devices/system/cpu/vulnerabilities/* /sys/devices/system/cpu/vulnerabilities/meltdown:Mitigation: PTI /sys/devices/system/cpu/vulnerabilities/spectre_v1:Vulnerable /sys/devices/system/cpu/vulnerabilities/spectre_v2:Vulnerable: Minimal generic ASM retpoline
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 48
Spectre-v1: Bounds Check Bypass (CVE-2017-2573)
If (untrusted_offset < limit) { trusted_value = trusted_data[untrusted_offset]; tmp = other_data[(trusted_value)&mask]; ... }
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 49
Spectre-v1: Bounds Check Bypass (cont)
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 50
Mitigating Spectre-v1: Bounds Check Bypass
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 51
Mitigating Spectre-v1: Bounds Check Bypass (cont)
If (untrusted_offset < limit) { serializing_instruction(); trusted_value = trusted_data[untrusted_offset]; tmp = other_data[(trusted_value)&mask]; ... }
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 52
Spectre-v2: Branch Predictor Poisoning (CVE-2017-5715)
branch is under malicious user control – repurpose existing privileged code as a “gadget”
branch in another application or the Operating System kernel running at higher privilege
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 53
Mitigating Spectre-v2: Big hammer approach
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 54
Tangent: Microcode, Millicode, and Chicken Bits
team to “chicken out” and disable certain features that aren't working in whole or in part
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 55
Microcode, Millicode, and Chicken Bits (cont)
“program” (really a simple set of state transitions) contained within fast on-chip ROM
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 56
Mitigating Spectre-v2: Big hammer (cont)
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 57
Mitigating Spectre-v2 with Retpolines
independent logic within the core. It may take many thousands of cycles on kernel entry
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 58
Mitigating Spectre with Retpolines (cont)
https://support.google.com/faqs/answer/7625886 ) call set_up_target; capture_spec: pause; jmp capture_spec; set_up_target: mov %r11, (%rsp); ret;
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 59
Mitigating Spectre-v2 with Retpolines (cont)
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 60
Meltdown
handle exceptions arising from speculatively executed instructions at instruction retirement
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 61
Meltdown (continued)
if (spec_cond) { unsigned char value = *(unsigned char *)ptr; unsigned long index2 = (((value>>bit)&1)*0x100)+0x200; maccess(&data[index2]); }
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 62
Meltdown (continued)
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 63
Mitigating Meltdown
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 64
Mitigating Meltdown
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 65
Variations on a theme: variant 3a (Sysreg read)
privileged system registers to which an application should not normally have access
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 66
Related Research
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks 68
Summary
Today's lecture covered the following topics:
plus.google.com/+RedHat youtube.com/user/RedHatVideos facebook.com/redhatinc twitter.com/RedHatNews linkedin.com/company/red-hat
Exploiting modern microarchitectures: M eltdown, Spectre, and other attacks 70
INTERNAL ONLY | PRESENTER NAME
Exploiting modern microarchitectures: Meltdown, Spectre, and other attacks
Jon Masters, Computer Architect, Red Hat, Inc. jcm@redhat.com | @jonmasters
Overview
Today's lecture will cover the following:
Architecture
Architecture
Common concepts in modern architectures
Common concepts in modern architectures
uses to automatically translate virtual addresses into physical memory addresses
demand loaded, or because the application does not have permission to access that address
Examples of computer architectures
registers (except FS/GS)
for all operations (fjrst load from memory)
Microarchitecture
Elements of a modern System-on-Chip (SoC)
D D R M E M D D R M E M LLC L2 $ C1 C2 L2 $ C1 C2 L2 $ C1 C2 L2 $ C1 C2
Elements of a modern System-on-Chip (SoC)
Microarchitecture
Elements of a modern in-order core
L1 I$
Instruction Fetch Instruction Decode
Branch Predictor Instruction Execute Register File Memory Access WritebackL1 D$
In order microarchitectures
An in-order pipeline visualized
IF ID EX MEM WB IF ID EX MEM WB IF ID EX MEM WB IF ID EX MEM WB IF ID EX MEM WB
In order microarchitectures (continued)
Elements of a modern out-of-order core
L1 I$
Instruction Fetch Instruction Decode
Branch PredictorRegister Renaming (ROB)
Integer Physical Register File Vector Physical Register File
L1 D$ Execution Units L2 $
Out-of-Order (OoO) microarchitectures
register fjle and a Register Alias Table (RAT)
Out-of-Order (OoO) microarchitectures (cont.)
Microarchitecture (continued)
Examples of computer microarchitectures
* Typical is 4uops with rate exception
Virtual Memory and Caches
Virtual memory
0x7000 0x6000 0x5000 0x4000 0x3000 0x2000 0x1000 0x0000 Process A Process B Page Tables Page Tables Physical Memory
Virtual memory
existing TLB entries are invalidated. Cache fmushing may be required depending upon the use of address space IDs (ASIDs, PCIDs, etc.) in the architecture and the Operating System
Virtual memory
automatically using metadata from the ELF (Executable Linkng Format) application binary
Caches
D D R M E M D D R M E M LLC L2 $ C1 C2 L2 $ C1 C2 L2 $ C1 C2 L2 $ C1 C2
Caches
... 0x4080 0x4040 0x4000 ... 0x0080 0x0040 0x0000 Physical Memory 0x4000 TLB 0x1000 0x1000 0x040 Virtual Index DATA Physical Tag Cached Data
Caches
Caches
Caches as side channels
Caches as side channels
time = rdtsc(); maccess(&data[0x300]); delta3 = rdtsc() - time; time = rdtsc(); maccess(&data[0x200]); delta2 = rdtsc() - time;
Caches as side channels (continued)
Caches as side channels (continued)
asm volatile ("prefetcht0 (%0)" : : "r" (p)); asm volatile ("prefetcht1 (%0)" : : "r" (p)); asm volatile ("prefetcht2 (%0)" : : "r" (p)); asm volatile ("prefetchnta (%0)" : : "r" (p));
Branch Prediction and Speculation
Branch prediction
LOAD “raining” FLAGS? CMP “raining” Condition Flags take_umbrella() True False
Branch prediction
Speculative Execution
Branch prediction and speculation
1) If the predicted branch was correct, speculated instructions can be retired
2) If the predicted branch was incorrect, speculated instructions can be discarded
Conditional branches
movq $0, %rax loop: incq %rax cmpq $10, %rax jle loop
Conditional branch prediction
Conditional branch prediction
Indirect branch prediction
Branch predictor optimization
Speculation in modern processors
Meltdown and Spectre microarchitecture vulnerabilities
vulnerabilities discovered in common industry-wide microprocessor optimizations
Meltdown and Spectre microarchitecture vulnerabilities
$ grep . /sys/devices/system/cpu/vulnerabilities/* /sys/devices/system/cpu/vulnerabilities/meltdown:Mitigation: PTI /sys/devices/system/cpu/vulnerabilities/spectre_v1:Vulnerable /sys/devices/system/cpu/vulnerabilities/spectre_v2:Vulnerable: Minimal generic ASM retpoline
Spectre-v1: Bounds Check Bypass (CVE-2017-2573)
If (untrusted_offset < limit) { trusted_value = trusted_data[untrusted_offset]; tmp = other_data[(trusted_value)&mask]; ... }
Spectre-v1: Bounds Check Bypass (cont)
Mitigating Spectre-v1: Bounds Check Bypass
Mitigating Spectre-v1: Bounds Check Bypass (cont)
If (untrusted_offset < limit) { serializing_instruction(); trusted_value = trusted_data[untrusted_offset]; tmp = other_data[(trusted_value)&mask]; ... }
Spectre-v2: Branch Predictor Poisoning (CVE-2017-5715)
branch is under malicious user control – repurpose existing privileged code as a “gadget”
branch in another application or the Operating System kernel running at higher privilege
Mitigating Spectre-v2: Big hammer approach
Tangent: Microcode, Millicode, and Chicken Bits
team to “chicken out” and disable certain features that aren't working in whole or in part
Microcode, Millicode, and Chicken Bits (cont)
“program” (really a simple set of state transitions) contained within fast on-chip ROM
Mitigating Spectre-v2: Big hammer (cont)
Mitigating Spectre-v2 with Retpolines
independent logic within the core. It may take many thousands of cycles on kernel entry
Mitigating Spectre with Retpolines (cont)
https://support.google.com/faqs/answer/7625886 ) call set_up_target; capture_spec: pause; jmp capture_spec; set_up_target: mov %r11, (%rsp); ret;
Mitigating Spectre-v2 with Retpolines (cont)
Meltdown
handle exceptions arising from speculatively executed instructions at instruction retirement
Meltdown (continued)
if (spec_cond) { unsigned char value = *(unsigned char *)ptr; unsigned long index2 = (((value>>bit)&1)*0x100)+0x200; maccess(&data[index2]); }
Meltdown (continued)
Mitigating Meltdown
Mitigating Meltdown
Variations on a theme: variant 3a (Sysreg read)
privileged system registers to which an application should not normally have access
Related Research
Summary
Summary
Today's lecture covered the following topics:
THANK YOU
Exploiting modern microarchitectures: M eltdown, Spectre, and other attacks 70
INTERNAL ONLY | PRESENTER NAME