Compiler Design Spring 2018 Thomas R. Gross Computer Science - - PowerPoint PPT Presentation

compiler design
SMART_READER_LITE
LIVE PREVIEW

Compiler Design Spring 2018 Thomas R. Gross Computer Science - - PowerPoint PPT Presentation

Compiler Design Spring 2018 Thomas R. Gross Computer Science Department ETH Zurich, Switzerland 1 What I hope you learned in this class 1. Compiler design: Structure of a simple compiler Simple: 2-3K lines of Java code (maybe a bit more)


slide-1
SLIDE 1

Compiler Design

Spring 2018

1

Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

slide-2
SLIDE 2

What I hope you learned in this class

  • 1. Compiler design: Structure of a simple compiler

§ Simple: 2-3K lines of Java code (maybe a bit more) § Industry: C1 compiler in HotSpot VM is considered “simple”

§ 30K lines of C/C++/assembly code

  • 2. Software engineering: How to design a large(r) software

system

§ Sometimes there is no “right” or ”wrong” § Sometimes there is

  • 3. Programming

§ What the programming language design document should tell you § How to use that information

2

slide-3
SLIDE 3

Beyond (basic) "Compiler Design"

Many possible extensions § Optimizations § Intermediate representations § Program transformations § Tool support § System security § New language concepts, new languages

6

slide-4
SLIDE 4

Compilers and system security

7

slide-5
SLIDE 5

Attack types

§ Code corruption attack § Control-flow hijack attack § Data-only attack § Information leak

Attack model according to: „sok: eternal war in memory“ laszlo szekeres, mathias payer, tao wei, dawn song Http://www.cs.berkeley.edu/~dawnsong/papers/oakland13-sok-cr.pdf

slide-6
SLIDE 6

Control-flow hijack attacks

§ Most powerful attack § Hijack control-flow

§ To attacker-supplied arbitrary machine code § To existing code (code-reuse attack)

§ Corrupt code pointers

§ Return addresses, function pointers, vtable entries, exception handlers, jmp_bufs

slide-7
SLIDE 7

Control-flow hijack attacks

§ Most ISAs support indirect branch instructions

§ E.g., x86 “ret“, indirect “jmp“, indirect “call“

§ fptr is a value in memory at 0xafe08044

§ branch *fptr

fptr: 0xafe08044

Code

0x8056b30

good_func:

0x08056b30

slide-8
SLIDE 8

Control-flow hijack attacks

§ fptr is a value in memory at 0xafe08044

§ branch *fptr § fptr was corrupted by an attacker

§ Attacker goal: hijack control-flow to injected machine code or to “evil functions“

fptr: 0xafe08044

Code

Corrupted

evil_code:

slide-9
SLIDE 9

State of the art defenses

§ Non-executable data

§ NX bit

§ Data Execution Prevention (DEP)

§ OS support

14

slide-10
SLIDE 10

Bypassing NX / DEP

§ Only use existing code § Code-reuse attack

§ ret2libc, ret2bin, ret2* attacks § Return-oriented programming (ROP) § Jump/Call-oriented programming

§ Use code-reuse technique to change protection flags

§ Alllocate or make memory executable

§ mprotect/VirtualProtect § mmap/VirtualAlloc

0x00000000 0xffffffff

Code Heap Stack

attacker code & data attacker code & data

rw- rw- r-x

slide-11
SLIDE 11

Return-oriented programming (ROP)

§ Use available code snippets ending with ret instruction

§ Called gadgets or ROP chain § E.g., write primitive

%ebp %esp

arguments return address saved ebp address gadget1 dummy ebp buf[1024] value address gadget2 address

rw-

dummy value address gadget3 address gadget4

Code

r-x

pop %edx; ret;

1

pop %eax; pop %ebx; ret;

2

mov %edx, (%eax); mov $0x0, %eax; ret;

3

Stack

slide-12
SLIDE 12

ASLR

§ Today most operating systems implement Address Space Layout Randomization (ASLR) § Mapping program addresses to hardware addresses § What can be randomized?

§ OS: Stack, heap and memory mapping base addresses § OS, compiler, linker: Exectuables and libraries § Position-independent or relocatable code

slide-13
SLIDE 13

Generic defense: DEP & ASLR

§ DEP: Data Execution Protection § ASLR: Address Space Layout Randomization

§ Exploitation becomes harder for all vulnerability classes & attack techniques § Together quite effective

§ If implemented correctly and used continuously

§ But DEP and ASLR not enough

slide-14
SLIDE 14

Compile-time protection

§ Usually require source code changes (annotations) and/or recompilation of the application

§ To add run-time checks

§ Stack canaries / Cookies

§ Pointer obfuscation § /GS (buffer security check) § /SAFESEH (link-time, provide list of valid handlers) § SEHOP (run-time, walk down SEH chain to final handler before dispatching / integrity check) § Virtual Table Verification (VTV) & vtguard § Control-Flow Guard (new in Visual Studio 2015)

slide-15
SLIDE 15

Stack canary / cookie

Stack during vulnFunc()

%ebp %esp

void vulnFunc() { <copy canary> char buf[1024]; read(STDIN, buf, 2048); <verify canary> } arguments return address saved ebp return address saved ebp buf[1024] main() stack frame

rw-

stack canary Stack at function exit

%ebp %esp

arguments return address saved ebp

  • verwritten retaddr
  • verwritten ebp

buf[1024]

  • verwritten frame

rw-

  • verwritten canary

stack canary copy canary verify canary

slide-16
SLIDE 16

Stack canary / cookie

§ Detects linear buffer overflows on stack

§ At function exit

§ Corruption of local stack not detected

§ Only if canary / cookie value is overwritten

§ Incurs runtime overhead § Effectiveness relies on secret

§ Leaking, predicting, guessing or brute-forcing might work in special cases

slide-17
SLIDE 17

Attacker model

§ Let's assume a powerful attacker

§ Can arbitrarily corrupt data and pointers § Can read entire address space of a process § Only restriction on attacker:

§ No data execution and no code corruption (NX/DEP/W^X)

slide-18
SLIDE 18

Question

§ Can we still prevent arbitrary code execution and code-reuse attacks?

slide-19
SLIDE 19

Observations

§ Attacker needs to hijack control-flow

§ To injected or existing code

§ VM/runtime system must ensure that control-flow stays

  • n the intended legitimate path

§ As allowed by compiler resp. control-flow graph (CFG)

slide-20
SLIDE 20

Control-flow integrity (CFI)

§ Construct a control-flow graph (CFG)

§ Should be as strict as possible

§ Ensure that control-flow stays within CFG

slide-21
SLIDE 21

Control-flow integrity (CFI)

§ Original publication in 2005

§ “Control-Flow Integrity – Principles, Implementations, and Applications“ § M. Abadi, M. Budiu, U. Erlingsson, J. Ligatti

§ CCS'05 (ACM Trans. on Information and System Security (TISSEC) 13(1) Oct 2009)

§ Many CFI implementations were proposed during recent years

§ Compiler-based § Binary-only (static rewriting)

slide-22
SLIDE 22

Control-flow integrity (CFI)

§ Construct a control-flow graph (CFG)

§ Should be as strict as possible

§ Ensure that control-flow stays within CFG § If no path within the CFG can be misused by an attacker then the CFI policy can be considered secure

slide-23
SLIDE 23

Direct branch Indirect branch Basic block

Control-flow integrity (CFI)

slide-24
SLIDE 24

Direct branch Indirect branch

ret

Basic block

Hijacked control-flow

slide-25
SLIDE 25

Direct branch Indirect branch Basic block

Control-flow integrity (CFI)

slide-26
SLIDE 26

Direct branch Indirect branch under CFI Basic block

Control-flow integrity (CFI)

slide-27
SLIDE 27

Direct branch Indirect branch under CFI Basic block

Control-flow integrity (CFI)

slide-28
SLIDE 28

Direct branch Indirect branch under CFI Basic block

CFI VIOLATION

Control-flow integrity (CFI)

slide-29
SLIDE 29

Control-flow integrity (CFI)

§ Drawbacks of proposed solutions

§ Too permissive CFG due to over-approximation § Need to recompile § No support for shared libraries

§ Most solutions shown to be ineffective

§ “Hardened” exploits still worked under CFI

slide-30
SLIDE 30

Control-flow integrity (CFI)

§ Static CFI not enough: Dynamic approach necessary

§ Dynamic CFI

41

slide-31
SLIDE 31

Lockdown – dynamic CFI

§ Enforces a strict CFI policy for binaries § Supports shared libraries & dynamic loading § Constructs and enforces CFG at runtime

§ Using static and dynamic information

slide-32
SLIDE 32

System Call Interface Kernel User

ELF Files

Loads ELF DSOs

/bin/<exe>

Loader

libc.so.6 lib*

Code Cache read only readable + executable

main() printf() func*() main' func1() func2() ... func2' printf'

Lockdown Binary Translator translate() CFT Verifier

Run-time ICF validation Application Domain Lockdown Domain

CFT: Control-Flow Transfer, ICF: Indirect Control-Flow, ELF: Executable and Linkable Format, DSO: Dynamic Shared Object

Lockdown – dynamic CFI

slide-33
SLIDE 33

Beyond basic compilers

§ Many interesting problems exist § Opportunities for projects (BS, MS, research) § Contact me or the TAs for further information

44