Compiler Design
Spring 2018
1
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)
1
§ 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
§ Sometimes there is no “right” or ”wrong” § Sometimes there is
§ What the programming language design document should tell you § How to use that information
2
6
7
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
§ To attacker-supplied arbitrary machine code § To existing code (code-reuse attack)
§ Return addresses, function pointers, vtable entries, exception handlers, jmp_bufs
§ E.g., x86 “ret“, indirect “jmp“, indirect “call“
§ branch *fptr
fptr: 0xafe08044
Code
0x8056b30
good_func:
0x08056b30
§ branch *fptr § fptr was corrupted by an attacker
fptr: 0xafe08044
Code
Corrupted
evil_code:
§ NX bit
§ OS support
14
§ ret2libc, ret2bin, ret2* attacks § Return-oriented programming (ROP) § Jump/Call-oriented programming
§ Alllocate or make memory executable
§ mprotect/VirtualProtect § mmap/VirtualAlloc
0x00000000 0xffffffff
Code Heap Stack
attacker code & data attacker code & data
rw- rw- r-x
§ 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
§ OS: Stack, heap and memory mapping base addresses § OS, compiler, linker: Exectuables and libraries § Position-independent or relocatable code
§ Exploitation becomes harder for all vulnerability classes & attack techniques § Together quite effective
§ If implemented correctly and used continuously
§ But DEP and ASLR not enough
§ Usually require source code changes (annotations) and/or recompilation of the application
§ To add run-time checks
§ 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)
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
buf[1024]
rw-
stack canary copy canary verify canary
§ At function exit
§ Only if canary / cookie value is overwritten
§ Leaking, predicting, guessing or brute-forcing might work in special cases
§ 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)
§ To injected or existing code
§ As allowed by compiler resp. control-flow graph (CFG)
§ Should be as strict as possible
§ “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)
§ Compiler-based § Binary-only (static rewriting)
§ Should be as strict as possible
Direct branch Indirect branch Basic block
Direct branch Indirect branch
ret
Basic block
Direct branch Indirect branch Basic block
Direct branch Indirect branch under CFI Basic block
Direct branch Indirect branch under CFI Basic block
Direct branch Indirect branch under CFI Basic block
41
§ Using static and dynamic information
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
44