type qualifiers

60
Type Qualifiers CS 8803 FPL Oct 1, 2012 (Slides Courtesy of Jeffrey Foster) 1

Upload: cosmo

Post on 24-Feb-2016

38 views

Category:

Documents


0 download

DESCRIPTION

Type Qualifiers. CS 8803 FPL Oct 1, 2012 (Slides Courtesy of Jeffrey Foster). Even after large, extensive testing efforts, commercial software is shipped riddled with errors ("bugs"). -- PITAC Report to the President, February 24, 1999. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Type Qualifiers

Type Qualifiers

CS 8803 FPLOct 1, 2012

(Slides Courtesy of Jeffrey Foster)

1

Page 2: Type Qualifiers

2

Software Quality Today

Even after large, extensive testing efforts, commercial software is shipped riddled with errors ("bugs").

-- PITAC Report to the President, February 24, 1999

-- Bill Gates, January 15, 2002 (highest priority for Microsoft)

Trustworthy Computing is computing that is available, reliable, and secure as electricity, water services and telephony....No Trustworthy Computing platform exists today.

Page 3: Type Qualifiers

3

Conclusion?

Software is buggy

Page 4: Type Qualifiers

4

So What?

• Software has always been buggy

• But now...– More people use software– Computers keep getting faster

• Speed/quality tradeoff changing– Cost of fixing bugs is high

Page 5: Type Qualifiers

5

Common Techniques for Software Quality• Testing

• Code auditing

• Drawbacks: Expensive, difficult, error-prone, limited assurances

• What more can we do?– Tools that analyze source code– Techniques for avoiding programming mistakes

Page 6: Type Qualifiers

6

Tools Need Specifications

put_tty_queue_nolock(c, tty);spin_lock_irqsave(&tty->read_lock, flags);

spin_unlock_irqrestore(&tty->read_lock, flags);

• Goal: Add specifications to programsIn a way that...– Programmers will accept

• Lightweight– Scales to large programs– Solves many different problems

Page 7: Type Qualifiers

7

Type Qualifiers

• Extend standard type systems (C, Java, ML)– Programmers already use types– Programmers understand types– Get programmers to write down a little more...

intconst ANSI Cptr( char)tainted Security vulnerabilities

int ptr( FILE)open File operations

Page 8: Type Qualifiers

8

Application: Format String Vulnerabilities• I/O functions in C use format strings

printf("Hello!"); Hello!printf("Hello, %s!", name); Hello,

name !

• Instead of printf("%s", name);

Why notprintf(name); ?

Page 9: Type Qualifiers

9

Format String Attacks

• Adversary-controlled format specifiername := <data-from-network>printf(name); /* Oops */

– Attacker sets name = “%s%s%s” to crash program

– Attacker sets name = “...%n...” to write to memory

• Lots of these bugs in the wild– New ones weekly on bugtraq mailing list– Too restrictive to forbid variable format strings

Page 10: Type Qualifiers

10

Using Tainted and Untainted

• Add qualifier annotationsint printf(untainted char *fmt, ...)tainted char *getenv(const char *)

tainted = may be controlled by adversaryuntainted = must not be controlled by adversary

Page 11: Type Qualifiers

11

Subtyping

void f(tainted int);untainted int a;f(a);

void g(untainted int);

tainted int b;f(b);

OKf accepts tainted or untainted data

Errorg accepts only untainted data

untainted tainted tainted untainted/untainted tainted

Page 12: Type Qualifiers

12

Framework

• Pick some qualifiers– and relation (partial order) among qualifiers

• Add a few explicit qualifiers to program

• Infer remaining qualifiers– and check consistency

untainted int tainted intreadwrite FILE read FILE

Page 13: Type Qualifiers

13

Type Qualifier Inference

• Two kinds of qualifiers– Explicit qualifiers: tainted, untainted, ...– Unknown qualifiers: a0, a1, ...

• Program yields constraints on qualifierstainted a0 a0 untainted

• Solve constraints for unknown qualifiers– Error if no solution

Page 14: Type Qualifiers

14

Adding Qualifiers to Types

ptr

FILE

int

ptr

chartainted

a a0

a1 a2

open

ptr( char)tainted int ptr( FILE) open

Page 15: Type Qualifiers

15

ptr

int

int

f

ptr

int

y

int

z

Constraint Generation

ptr(int) f(x : int) = { ... } y := f(z)

a0

a1 a2

a3

a4

a5

a6

a6 a1

a2 a4

a3 =

a5

Page 16: Type Qualifiers

16

Constraints as Graphs

a0

a1 a2

a3

a4

a5

a6

a6 a1

a2 a4

a3 =

a5

a8

untainted

tainted

a7

a9 •••

Key idea: programs constraints graphs

Page 17: Type Qualifiers

17

Satisfiability via Graph Reachability

a0

a1 a2

a3

a4

a5

a6

a6 a1

a2 a4

a3 =

a5

a8

untainted

tainted

a7

a9 •••

Is there an inconsistent path through the graph?

Page 18: Type Qualifiers

18

Satisfiability via Graph Reachability

a0

a1 a2

a3

a4

a5

a6

a6 a1

a2 a4

a3 =

a5

a8

untainted

tainted

a7

a9 •••

Is there an inconsistent path through the graph?

Page 19: Type Qualifiers

19

Satisfiability via Graph Reachability

a0

a1 a2

a3

a4

a5

a6

a6 a1

a2 a4

a3 =

a5

a8

untainted

tainted

a7

a9 •••

tainted a6 a1 a3 a5 a7 untainted

Page 20: Type Qualifiers

20

Satisfiability in Linear Time

• Initial program of size n– Fixed set of qualifiers tainted, untainted, ...

• Constraint generation yields O(n) constraints– Recursive abstract syntax tree walk

• Graph reachability takes O(n) time– Works for semi-lattices, discrete p.o., products

Page 21: Type Qualifiers

21

The Story So Far...

• Type qualifiers as subtyping system– Qualifiers live on the standard types– Programs constraints graphs

• Useful for a number of real-world problems

• Up next: State change and type qualifiers– A glimpse of a more complex system

• Followed by: Applications, experiments

Page 22: Type Qualifiers

22

Application: Locking

Lock x;

lock(x);...critical section...

unlock(x);

x : locked Lock

x : unlocked Lock

Page 23: Type Qualifiers

23

Flow-Sensitive Type Qualifiers

• Standard type systems are flow-insensitive– Types don't change during execution

/* x : int */ x := ...; /* x : int */

• We need flow-sensitivity– Qualifiers may change during execution

/* y : locked Lock */ y := ...; /* y : unlocked Lock */

Page 24: Type Qualifiers

24

Some Challenges

• How do we deal with aliasing?p = &x; *p = ...;

• How do we make the analysis scale?– Too expensive to model full state at each point

• What happens when too much is aliased?– How does the programmer control aliasing?

Page 25: Type Qualifiers

25

Modeling State with Abstract Stores

• Track each variable's type at each point– Abstract stores map variables to types– ...and types contain qualifiers

{ x : t, y : r, z : s, ... }x := ...;

{ x : t', y : r, z : s, ... }y := ...;

{ x : t', y : r', z : s, ... }

Page 26: Type Qualifiers

26

What About Aliasing?

• Suppose p points to x:{ x : q int, p : ptr(q int), ... }

*p := ...;{ x : q int, p : ptr(q' int), ... }

– Variable names alone are insufficient

• Solution: Add a level of indirection– Stores map locations to types– Pointer types point to locations

Page 27: Type Qualifiers

27

Unification-Based Alias Analysis

• Initial flow-insensitive pass computes aliasing– Before flow-sensitive analysis– Simultaneous with standard type inference

• Types are not flow-sensitive, only qualifiers

• Associate a location r with each pointer– Unify locations that may alias*p : ptrr(int) *x : ptrs(int)...p = &x;

r

/* require r = s */

Page 28: Type Qualifiers

28

Using Locations in Stores

• Suppose p points to x:*p : ptrr(int) x : ptrr(int)

*p := ...;{ r : q' int, h : ptr(r), ... }

{ x : q int, p : ptr(q int), ... }{ r : q int, h : ptr(r), ... }

Page 29: Type Qualifiers

29

What About Scalability?

• Stores are too big{ r : t, h : r, n : s, ... }

– A program of size n may have• n locations• n program points• n2 space to represent stores

• We need a more compact representation– Idea: represent differences between stores

Page 30: Type Qualifiers

30

Constructing Stores

• Three kinds of stores SS ::= e Unknown store | Alloc(S, r : t) Like store S, but r

allocated with type t | Assign(S, r : t) Like store S, but update

type of r with t• Store constraints S e

– Control flow from S to e

• Solution maps e to { r : t, h : r, n : s, ... }– Key: only write down necessary portion of soln.

Page 31: Type Qualifiers

31

Example

Lock x; while (...) {

lock(x);y := ...unlock(x);

}

Page 32: Type Qualifiers

32

Example

Lock xr; while (...) {

lock(xr);yh := ...unlock(xr);

}

e0

Alloc(e0, r : unlocked) e1

Alloc r : unlocked Lock

Alloc(e0, r : unlocked) e1

e1

Alloc(e0, r : unlocked) e1

Assign r : locked Lock

Assn(Assn(Assn(e1, r : locked), h : q t), r : unlocked) e1

check e1(r) : unlocked Lock Assign h : q t

Assn(Assn(Assn(e1, r : locked), h : q t), r : unlocked) e1

check (r) : locked Lock

Assign r : unlocked Lock

Assn(Assn(Assn(e1, r : locked), h : q t), r : unlocked) e1Assn(Assn(Assn(e1, r : locked), h : q t), r : unlocked) e1

e2

e1 e2

Page 33: Type Qualifiers

33

Example

e0

Alloc r : unlocked Locke1

Assign r : locked Lockcheck e1(r) :

unlocked Lock Assign h : q t

Assign r : unlocked Lock

check (r) : locked Lock

e2

Alloc(e0, r : unlocked) e1

Assn(Assn(Assn(e1, r : locked), h : q t), r : unlocked) e1

e1 e2

Page 34: Type Qualifiers

34

Lazy Constraint Resolution

• We don't care about most locations– only those that may be locked or unlocked– In this case, we will only do work for r

• Key to efficiency:When solving for store variables, only representthe minimum necessary

Page 35: Type Qualifiers

35

Constraint Resolution Example

e0

Alloc r : unlocked Locke1

Assign r : locked Lockcheck e1(r) :

unlocked Lock Assign h : q t

Assign r : unlocked Lock

check (r) : locked Lock

e2

green = { r : unlocked Lock }

red = { r : locked Lock }

Page 36: Type Qualifiers

36

Constraint Resolution Example

e0

Alloc r : unlocked Locke1

Assign r : locked Lockcheck e1(r) :

unlocked Lock Assign h : q t

Assign r : unlocked Lock

check (r) : locked Lock

e2

green = { r : unlocked Lock }

red = { r : locked Lock }

Page 37: Type Qualifiers

37

Constraint Resolution Example

e0

Alloc r : unlocked Locke1

Assign r : locked Lockcheck e1(r) :

unlocked Lock Assign h : q t

Assign r : unlocked Lock

check (r) : locked Lock

e2

green = { r : unlocked Lock }

red = { r : locked Lock }

Page 38: Type Qualifiers

38

Constraint Resolution Example

e0

Alloc r : unlocked Locke1

Assign r : locked Lockcheck e1(r) :

unlocked Lock Assign h : q t

Assign r : unlocked Lock

check (r) : locked Lock

e2

green = { r : unlocked Lock }

red = { r : locked Lock }

Page 39: Type Qualifiers

39

Constraint Resolution Example

e0

Alloc r : unlocked Locke1

Assign r : locked Lockcheck e1(r) :

unlocked Lock Assign h : q t

Assign r : unlocked Lock

check (r) : locked Lock

e2

green = { r : unlocked Lock }

red = { r : locked Lock }

Page 40: Type Qualifiers

40

Constraint Resolution Example

e0

Alloc r : unlocked Locke1

Assign r : locked Lockcheck e1(r) :

unlocked Lock Assign h : q t

Assign r : unlocked Lock

check (r) : locked Lock

e2

green = { r : unlocked Lock }

red = { r : locked Lock }

Page 41: Type Qualifiers

41

Constraint Resolution Example

e0

Alloc r : unlocked Locke1

Assign r : locked Lockcheck e1(r) :

unlocked Lock Assign h : q t

Assign r : unlocked Lock

check (r) : locked Lock

e2

green = { r : unlocked Lock }

red = { r : locked Lock }

Page 42: Type Qualifiers

42

Constraint Resolution Example

e0

Alloc r : unlocked Locke1

Assign r : locked Lockcheck e1(r) :

unlocked Lock Assign h : q t

Assign r : unlocked Lock

check (r) : locked Lock

e2

green = { r : unlocked Lock }

red = { r : locked Lock }

Page 43: Type Qualifiers

43

Constraint Resolution Example

e0

Alloc r : unlocked Locke1

Assign r : locked Lockcheck e1(r) :

unlocked Lock Assign h : q t

Assign r : unlocked Lock

check (r) : locked Lock

e2

green = { r : unlocked Lock }

red = { r : locked Lock }

Page 44: Type Qualifiers

44

Constraint Resolution Example

e0

Alloc r : unlocked Locke1

Assign r : locked Lockcheck e1(r) :

unlocked Lock Assign h : q t

Assign r : unlocked Lock

check (r) : locked Lock

e2

green = { r : unlocked Lock }

red = { r : locked Lock }

Page 45: Type Qualifiers

45

Constraint Resolution Example

e0

Alloc r : unlocked Locke1

Assign r : locked Lockcheck e1(r) :

unlocked Lock Assign h : q t

Assign r : unlocked Lock

check (r) : locked Lock

e2

green = { r : unlocked Lock }

red = { r : locked Lock }

Page 46: Type Qualifiers

46

Strong Updates

• In e2, location r has qualifier q'– We've replaced r's qualifier– This is called a strong

update– Location r is linear

e0

Alloc r : q int

Assign r : q' int

e2

e1

Page 47: Type Qualifiers

47

Weak Updates

• What if r allocated twice?– Only one is actually

updated

e0

Alloc r : q int

Assign r : q' int

e2

e1

Alloc r : q int

• In e2, location r has qualifier q q'– We've merged r's new and

old qualifiers– This is called a weak

update– Location r is non-linear

Page 48: Type Qualifiers

48

Recovering Linearity

• What do we do when aliasing too imprecise?– Can't strongly update non-linear locations

• New construct restrict– Programmer adds restrict to help the alias

analysis

• restrict x = e1 in e2– Roughly: within e2, accesses to *e1 must use x

Page 49: Type Qualifiers

49

Restrict Example

Lock locks[n];

lock(&locks[i]);...unlock(&locks[i]);

Page 50: Type Qualifiers

50

Restrict Example

• Within scope of restrict, only mylock used– Can perform strong updates

• After restrict ends, weak update from mylock to locks[]

Lock locks[n];

restrict mylock = &locks[i] in lock(mylock); ... unlock(mylock);

Page 51: Type Qualifiers

51

More Features

• Low-cost polymorphism– Use effects to avoid merging stores at fn calls

• Some path-sensitivity– Different types on if-then-else branches

Page 52: Type Qualifiers

52

Qualifier Inference Architecture

restrict annotations

Flow-insensitiveType qualifiers

Alias Analysis

Effect inference

Flow-sensitiveLinearity inference

Type qualifiers

Page 53: Type Qualifiers

53

Applications

Published experiments:const Inference [Foster, Fahndrich, Aiken, PLDI99]Y2K bug detection [Elsman, Foster, Aiken, 1999]Format-string vulnerabilities [Shankar, Talwar, Foster, Wagner, Usenix Sec 01]Locking and stream operations [Foster, Terauchi, Aiken, PLDI 02]Linux Security Modules [Zhang, Edwards, Jaeger,

(IBM Watson) Usenix Sec 02]

Page 54: Type Qualifiers

54

Results: Format String Vulnerabilities

• Analyzed 10 popular unix daemon programs

• Annotations shared across applications– One annotated header file for standard

libraries

• Found several known vulnerabilities– Including ones we didn’t know about

• User interface critical

Page 55: Type Qualifiers

55

Results: Locking

• Looked for simple deadlocks in Linux 2.4.9– Double acquires/releases

• Analyzed 892 files in linux/drivers individually

• Analyzed 513 modules (all linked files)– 14 type errors deadlocks– ~41/892 fail to typecheck but appear correct– ~196/513 fail to typecheck

• added restrict by hand to remove type errors due to aliasing for 64/196

Page 56: Type Qualifiers

56

Running Time: Locking

0

10

20

30

40

50

60

70

80

90

0K 100K 200K 300K 400K 500K 600K 700K 800K

Lines of code (preprocessed)

Run

ning

tim

e (s

)

Flow-Sensitive Flow-Insensitive Parsing

Page 57: Type Qualifiers

57

Memory Usage: Locking

0

100

200

300

400

500

600

700

800

900

1000

0K 100K 200K 300K 400K 500K 600K 700K 800K

Lines of code (preprocessed)

Mem

ory

(MB

)

Flow-Sensitive Flow-Insensitive Parsing

\

Page 58: Type Qualifiers

58

Main Contributions

• Type qualifiers as specifications– With applications

• Scalable flow-sensitive qualifier inference– Lazy, constraint-based– Built with alias analysis, effect inference– Linearities for strong/weak updates

• restrict construct

Page 59: Type Qualifiers

59

(Some) Related Work

• Dataflow Analysis• Bug-finding Tools

– AST Toolkit [Weise, Crew]– Meta-Level Compilation [Engler et al]

• Type Systems– Label flow [Mossin]– Typestate [Strom, Yemini, Yellin]– Vault [Fähndrich, DeLine]– Cyclone [Grossman et al]

Page 60: Type Qualifiers

60

Conclusion

• Type qualifiers are specifications that...– Programmers will accept

• Lightweight • Easy to use -- inference and visualization

– Scale to large programs– Solve many different problems

http://www.cs.berkeley.edu/~jfoster/cqual

Includes source code and web demo of cqual