SoftBound: Highly Compatible and Complete Spatial Safety for C -...

32
SoftBound: Highly Compatible and Complete Spatial Safety for C Santosh Nagarakatte, Jianzhou Zhao, Milo Martin, Steve Zdancewic University of Pennsylvania {santoshn, jianzhou, milom, stevez}@cis.upenn.edu

Transcript of SoftBound: Highly Compatible and Complete Spatial Safety for C -...

Page 1: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

SoftBound: Highly Compatible and Complete Spatial

Safety for C Santosh Nagarakatte, Jianzhou Zhao,

Milo Martin, Steve Zdancewic

University of Pennsylvania {santoshn, jianzhou, milom, stevez}@cis.upenn.edu

Page 2: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

2 SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Who Cares About Spatial Safety, Anyway?

June 2, 2009: iTunes-8.2 Open URL, stack overflow

May 12, 2009: libxml, Safari-3.2.3, Visit website, heap overflow

Jan 22, 2009: Windows, RPC packet, overflow (Conficker worm)

Feb 20, 2009: Acrobat Reader Open PDF, overflow

These buffer overflows are security vulnerabilities

Page 3: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

3

SoftBound: Spatial Safety for C • Compiler transformation to enforce spatial safety

•  Inspired by fat pointer schemes • Compatible – no source code modifications

•  Key: disjoint fat pointers memory layout unchanged

• Simple analysis – intra-procedural •  Separate compilation, creation of safe libraries

• Effective – observed no false positives/negatives • Low overhead

•  All loads and stores – 67% overhead •  Only stores – 21% overhead

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 4: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

4

struct BankAccount { char acctID[3]; int balance;

} b; b.balance = 0; char* id = &(b.acctID); … … char* p = id; … … do {

char ch = readchar(); *p = ch; p++;

} while(ch);

mem

ory SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Spatial Violation Example

acctID

reg

bal

10 11 12

p

38

10000 0

0x10 0x11 0x12 0x13 a b c

id 0x10 id 0x10

13

17

0x17

memory

registers

Page 5: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

5

Preventing Spatial Violations

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

write perfect code treat the symptoms

address space randomization non-executable stack/heap protect return addresses

all incomplete

add bounds checking to C

use a safe language

what about (existing) C code?

use static analysis what about false positives?

Page 6: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

6

•  Tripwires e.g., Purify, Valgrind … •  Few bits of state for each byte in memory •  A “red-zone” block between objects

•  Pointer based e.g., SafeC, Cyclone, CCured, MSCC, … •  Pointer becomes a fat pointer (ptr, base, bound) •  Pointer dereferences are checked

•  Object based e.g., Jones & Kelly, CRED, SafeCode, SVA, … •  Checks pointer manipulations •  Must point within same object

• All have one or more challenges: •  High runtime overheads •  Incompleteness, handling arbitrary casts •  Incompatible pointer representations, code

incompatibilities

Background: Bounds Checking for C

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 7: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

7

struct BankAccount { char acctID[3]; int balance;

} b; b.balance = 0; char* id = &(b.acctID); char* id_bse = &(b.acctID); char* id_bnd = &(b.acctID) + 3; char* p = id; char* p_bse = id_bse; char* p_bnd = id_bnd; do {

char ch = readchar(); check(p, p_bse, p_bnd);*p = ch;

p++; } while(ch);

mem

ory SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Background: Fat Pointer Approach

acctID

reg

bal

id 0x10

p

38

0

0x10 0x11 0x12

0x13

id_bse 0x10

id_bnd 0x13

p_bse 0x10

p_bnd 0x13

id_bse 0x10

id_bnd 0x13

id 0x10

a b c

10 11 12

13

17

memory

registers

Page 8: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

8

struct BankAccount { char acctID[3]; int balance;

} b; insert(b, &b, &b+sizeof(b)); b.balance = 0; char* id = &(b.acctID); … char* p = id; … … do {

char ch = readchar(); *p = ch; p++; p = lookup(p, p + 1);

} while(ch);

Obj

table m

emory

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Background: Object Based Approach

acctID

reg

bal

p

38

10000 0

0x10 0x11 0x12 0x13 a b c

id 0x10 id 0x10

10 to 16

0x17 10 11 12

13

17

memory

registers

object table

10,11 11,12 12,13 16,17

0x16

Page 9: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

9

Comparison of Approaches • Object based

+ Disjoint metadata memory layout unchanged high source compatibility

– Cannot detect sub-object overflows – Range lookup overhead

• Fat pointers + Can detect sub-objects overflows – Inline metadata memory layout changes low source compatibility

• Both

– Fail to protect against arbitrary casts (unless augmented, such as CCured’s WILD pointers)

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 10: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

10

struct BankAccount { char acctID[3]; int balance;

} b; b.balance = 0; char* id = &(b.acctID); lookup(&id)->bse = &(b.acctID); lookup(&id)->bnd = &(b.acctID) + 3; char* p = id; char* p_bse = lookup(&id)->bse; char* p_bnd = lookup(&id)->bnd; do {

char ch = readchar(); check(p, p_bse, p_bnd);*p=ch; p++;

} while(ch);

Meta

data

mem

ory SoftBound – Santosh Nagarakatte – LLVM DEV 2009

SoftBound Approach

acctID

reg

bal

id

p

38

0

0x10 0x11 0x12 0x13

p_bse 0x10

p_bnd 0x13

0x13 0x10 id 0x13 0x10

a b c

10 11 12

13

17

memory

registers

metadata

38

Page 11: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

11

Meta

data 38

mem

ory

• Pointer based • Disjoint metadata

• Unchanged memory layout

• Safe with arbitrary casts

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

SoftBound Approach

acctID

reg

bal

id

p

38

0

0x13

p.bse 0x10

p.bnd 0x13

0x13 0x10 id 0x13 0x10

a b c

10 11 12

13

17

memory

registers

metadata

Page 12: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

12

Rest of Talk • SoftBound handling of base/bound metadata…

•  … Storage •  … Checking on pointer dereference •  … Creation •  … Propagation

• SoftBound prototype

• Experiments

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 13: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

13

• Registers • For memory: hash table

•  Tagged, open hashing •  Fast hash function (bitmask) •  Nine x86 instructions

•  Shift, mask, multiply, add, three loads, cmp, branch

• Alternative: shadow space •  No collisions eliminates tag •  Reduce memory footprint •  Five x86 instructions

•  Shift, mask, add, two loads

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

SoftBound Base/Bound Storage

Shadow S

pace bound base

Hash

Table tag base bound

Page 14: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

14

Pointer Dereference Checks

• All pointer dereferences are checked if (p < p_base) abort();

if (p + size > p_bound) abort();

value = *p;

• Five x86 instructions (cmp, br, add, cmp, br)

• Bounds check elimination not focus •  Intra-procedural dominator based •  Previous techniques would help a lot

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 15: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

15

Pointer Creation

Heap Objects

p = malloc(size); p_base = p; p_bound = p + size;

Stack and Global Objects

int array[100]; p = &array; p_base = p; p_bound = p + sizeof(array);

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 16: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

16

Base/Bound Metadata Propagation • Pointer assignments and casts

•  Just propagate pointer base and bound

• Loading/storing a pointer from memory •  Loads/stores base and bound from metadata space

• Pointer arguments to a function •  Bounds passed as extra arguments (in registers)

int f(char* p) {…}

int _f(char* p, void* p_base, void* p_bound) {…}

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 17: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

17

Pointers to Structure Fields struct { char acctID[3]; int balance;

} *ptr; char* id = &(ptr->acctID);

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

option #1 Entire Structure

id_base = &(ptr->acctID); id_bound = &(ptr->acctID) + 3;

id_base = ptr_base; id_bound = ptr_bound;

option #2 Shrink to Field Only

Programmer intent ambiguous; optional shrinking of bounds

Page 18: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

18

See Paper For… • Proof of spatial safety guarantees

•  Region delineated by pointer metadata is always valid •  Formalized a rich subset of C

•  Includes arbitrary casts, recursive structures, etc… •  Mechanized proof in Coq

•  Online at: http://www.cis.upen.edu/acg/softbound/ • Handling various aspects of C

•  Separate compilation and library code •  memcpy() •  Function pointers •  Variable argument functions •  Etc…

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 19: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

19

SoftBound Prototype

• LLVM as its foundation •  Typed IR helps in pointer identification

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Typed IR

Optimize

SoftBound

Optimize

Source

Binary

~7K lines of code

Checks.c

Typed IR

Page 20: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

20

Experiments

• Three questions •  Can SoftBound detect overflows? •  Does SoftBound work with existing C code? •  Does SoftBound have low overhead?

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 21: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

21

Spatial Violation Detection • Can SoftBound detect overflows?

•  Synthetic attacks [Wilander et al] •  Prevented all these attacks

•  Bugbench [Lu05]: overflows from real applications

No false negatives encountered

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Benchmark SoftBound Mudflap Valgrind Go Yes No No Compress Yes Yes Yes Polymorph Yes Yes No Gzip Yes Yes Yes

Page 22: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

22

Source Compatibility Experiments • Does SoftBound work with existing C code?

• 272K lines of code total •  23 benchmarks from Spec, Olden •  BugBench •  Multithreaded HTTP Server with CGI support •  FTP server

No false positives encountered

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 23: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

23

Runtime Overhead: Shadow Space

0 20 40 60 80

100 120

Full Checking Store Only Checking

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Percent 67%

Full Checking: default for development & testing •  Check only stores [Yong03, Castro06]

•  Attacks predominantly use stores

Store-only: for security critical apps, production code

21%

Page 24: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

24

Experiments Recap

• Can SoftBound detect overflows? Yes

• Does SoftBound work with existing C code? Yes

• Does SoftBound have low overhead? Yes •  Full checking overhead - 67% •  Store only checking overhead - 21%

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 25: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

25

Future Work • Static optimizations

•  Removing redundant checks • OS support

•  Shadow space management

• Hardware support •  Heavyweight hardware support [Devietti, ASPLOS 08] •  Lightweight hardware support

• Temporal safety •  Dangling pointers

• C++

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 26: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

26

Our Experience with LLVM • 4 months from first use to a PLDI submission

•  SoftBound pass – 7k lines of code

• Typed IR was crucial •  Pointers already identified •  Instrument post-optimized code

•  Versus source-to-source translation •  Portable – ISA independent

• Leveraged existing optimizations Couldn’t have done it without LLVM

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 27: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

27

Conclusions • SoftBound provides spatial safety for C

•  Fat pointer approach, but with disjoint metadata •  Provides spatial safety guarantees

• SoftBound is: •  Compatible (no false positives, no source changes) •  Effective (no false negatives) •  Fast enough for…

•  Debugging & testing: full checking •  Security-critical software: store only checking

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 28: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

28

Want to try it out?

http://www.cis.upenn.edu/acg/softbound/

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 29: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM
Page 30: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

30

Few Issues •  Instruction Combine

%struct.node_t = type { i64, i64, %struct.node_t* } ….. ptr = (struct temp*) malloc(sizeof(struct temp)); ptr->t1 = 0; ptr->t2 = 0;

%0 = malloc [3 x i64] ; <[3 x i64]*> [#uses=3] %.sub9 = getelementptr inbounds [3 x i64]* %0, i64 0, i64 0 ; <i64*> .. store i64 0, i64* %.sub9, align 8 %1 = getelementptr inbounds [3 x i64]* %0, i64 0, i64 2 ; <i64*> .. store i64 0, i64* %1

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 31: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

31

Loss of Type Information: Multiple Ret Values • From em3d benchmark: typedef struct t { node* n1, node* n2} graph_t; … graph_t initialize_graph() { …. }

%0 = type{i64, i64} define %0 @initialize_graph() nounwind{ ….

}

SoftBound – Santosh Nagarakatte – LLVM DEV 2009

Page 32: SoftBound: Highly Compatible and Complete Spatial Safety for C - …llvm.org/devmtg/2009-10/Nagarakatte_SoftBound.pdf · 2019-10-30 · SoftBound – Santosh Nagarakatte – LLVM

32

Memory Overhead

0 50

100 150 200 250 300 350

Hash table Shadow space

SoftBound – Santosh Nagarakatte – PLDI 2009

Percent 87%

64%

Average memory overhead – full checking: 84%

Average memory overhead – store only: 64%