October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and...

46
October 30, 2003 CCS 2003 - Vinod Ganapath y 1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu University of Wisconsin-Madison David Chandler, David Melski, David Vitek {chandler,melski,dvitek}@grammatech.com Grammatech Inc., Ithaca, New York

Transcript of October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and...

Page 1: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 1

Buffer Overrun Detection using Linear Programming and Static Analysis

Vinod Ganapathy, Somesh Jha{vg,jha}@cs.wisc.edu

University of Wisconsin-Madison

David Chandler, David Melski, David Vitek{chandler,melski,dvitek}@grammatech.com

Grammatech Inc., Ithaca, New York

Page 2: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 2

The Problem: Buffer Overflows

• Highly exploited class of vulnerabilities– Legacy programs in C are still

vulnerable– “Safe” functions can be used unsafely

• Need:– Automatic techniques that will assure

code is safe before it is deployed

Page 3: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 3

The Solution

• Use static program analysis • Produce a list of possibly vulnerable

program locations• Couple buffer overrun warnings with

code understanding techniques

SourceCode

SourceCode

PossibleBuffer

Overruns

PossibleBuffer

Overruns

Static Analysis

Code Understanding

Page 4: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 4

Our Contributions

• Program Analysis:– Incorporated buffer overrun detection

in a program understanding tool• Program slicing, Data predecessors,…

– Use of procedure summaries to make buffer overrun analysis context-sensitive

• Constraint Resolution:– Use of linear programming to solve a

range analysis problem

Page 5: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 5

Roadmap of Talk

• Tool Architecture– Constraint Generation– Constraint Resolution– Producing Warnings

• Adding Context Sensitivity• Results• Future work and Conclusions

Page 6: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 6

Tool Architecture

C Source Code

CFGs, PDGs,SDG

Buffer OverrunWarnings

LinearConstraints

Rangesfor

Variables

LinearConstraints

CodeSurferConstraintGenerator

Taint Analyzer

Constraint

Solver

DetectionFront-End

Page 7: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 7

CodeSurfer

C Source Code

CFGs, PDGs,SDG

Buffer OverrunWarnings

LinearConstraints

Rangesfor

Variables

LinearConstraints

CodeSurferConstraintGenerator

Taint Analyzer

Constraint

Solver

DetectionFront-End

Page 8: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 8

CodeSurfer

• Commercial tool from Grammatech Inc.• Code understanding framework

– Inter-procedural slicing, chopping…

• Internally constructs:– Control Flow Graph (CFG)– Program Dependence Graphs (PDG)– System Dependence Graph (SDG)

• Incorporates results of pointer analysis– Helps reduce the number of warnings

Page 9: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 9

The Role of CodeSurfer

• Program Analysis Framework:– Use internal data structures to

generate constraints

• Detection Front-end:– Link each warning to corresponding

lines of source code through the System Dependence Graph

– Interactive front-end

Page 10: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 10

Constraint Generation

C Source Code

CFGs, PDGs,SDG

Buffer OverrunWarnings

LinearConstraints

Rangesfor

Variables

LinearConstraints

CodeSurferConstraintGenerator

Taint Analyzer

Constraint

Solver

DetectionFront-End

Page 11: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 11

Constraint Generation

• Four kinds of program points result in constraints:– Declarations– Assignments– Function Calls– Function Return

Page 12: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 12

Constraint Generation

• Four variables for each string buffer

buf_len_max, buf_len_min

buf_alloc_max, buf_alloc_min

• Operations on a buffer strcpy (tgt, src)

tgt_len_max ≥ src_len_max tgt_len_min ≤ src_len_min

Page 13: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 13

Constraint Generationif(…)

strcpy(tgt, srcA)

strcpy(tgt, srcB)

tgt_len_max ≥ srcA_len_maxtgt_len_min ≤ srcA_len_min

tgt_len_max ≥ srcB_len_maxtgt_len_min ≤ srcB_len_min

Page 14: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 14

Constraint Generation Methods• Order of statements:

– Flow-Sensitive Analysis:• Respects program order

– Flow-Insensitive Analysis:• Does not respect program order

• Function Calls:– Context-Sensitive modeling of functions:

• Respects the call-return semantics

– Context-Insensitive modeling of functions:• Ignores call-return semantics => imprecise

Page 15: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 15

Constraint Generation

• Constraints generated by our tool:– Flow-insensitive– Context-sensitive for some library functions– Partly Context-sensitive for user defined

function• Procedure summaries

Page 16: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 16

Taint Analysis

C Source Code

CFGs, PDGs,SDG

Buffer OverrunWarnings

LinearConstraints

Rangesfor

Variables

LinearConstraints

CodeSurferConstraintGenerator

Taint Analyzer

Constraint

Solver

DetectionFront-End

Page 17: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 17

Taint Analysis

• Removes un-initialized constraint variables– Un-modeled library calls– Un-initialized program variables

• Required for solvers to function correctly

Page 18: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 18

Constraint Solvers

C Source Code

CFGs, PDGs,SDG

Buffer OverrunWarnings

LinearConstraints

Rangesfor

Variables

LinearConstraints

CodeSurferConstraintGenerator

Taint Analyzer

Constraint

Solver

DetectionFront-End

Page 19: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 19

Constraint Solvers

• Abstract problem:– Given a set of constraints on max and min variables

– Get tightest possible fit satisfying all the constraints

• Our approach:– Model and solve as a linear program

Page 20: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 20

Constraint Solvers

• We have developed two solvers:– Vanilla solver– Hierarchical solver

Speed of Analysis

Precision of results

slow

fast

low high

Vanilla

Hierarchical

Page 21: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 21

Linear Programming

• An objective function F• Subject to: A set of constraints C• Example:

Maximize: x Subject to:

X <= 3

Page 22: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 22

Why Linear Programming?

• Can support arbitrary linear constraints

• Commercial linear program solvers are highly optimized and fast

• Use of linear programming duality for diagnostics– Can be used to produce a “witness”

trace leading to the statement causing the buffer overrun

Page 23: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 23

Vanilla Constraint Solver

• Goal: Obtain values for buffer bounds• Modeling as a Linear Program

Minimize: max variable Subject to:

Set of Constraints

And Maximize: min variable Subject to:

Set of Constraints

Least Upper Bound

Greatest Lower Bound

Tightest possible fit

Page 24: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 24

Vanilla Constraint Solver

• However, it can be shown that: Min: Σ (max vars) – Σ(min vars) Subject to: Set of Constraintsyields the same solution for each

variable

• Solve just one Linear Program and get values for all variables!

Page 25: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 25

Vanilla Constraint Solver

• Why is this method imprecise?– Infeasible linear programs– Why do such linear programs arise?

• Deals with infeasibility using an approximation algorithm

• See paper for details

Page 26: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 26

Detection Front-End

C Source Code

CFGs, PDGs,SDG

Buffer OverrunWarnings

LinearConstraints

Rangesfor

Variables

LinearConstraints

CodeSurferConstraintGenerator

Taint Analyzer

Constraint

Solver

DetectionFront-End

Page 27: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 27

Detection Front-End

0 1

buf_alloc_min

buf_len_max

Scenario I: ‘’Possible’’ buffer overflow

Page 28: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 28

Detection Front-End

0 1

buf_alloc_min

buf_len_max

Scenario II: Sure buffer overflow

Page 29: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 29

Roadmap of Talk

• Tool Architecture– Constraint Generation– Constraint Resolution– Producing Warnings

• Adding Context Sensitivity• Results• Future work and Conclusions

Page 30: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 30

Context-Insensitive Analysis

foo () { bar () {

int x; int y; x = foobar(5); y = foobar(30); } }

int foobar (int z) { int i; i = z + 1; return i; }

Page 31: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 31

Context-Insensitive Analysis

foo () { bar () {

int x; int y; x = foobar(5); y = foobar(30); } }

int foobar (int z) { int i; i = z + 1; return i; }

Page 32: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 32

Context-Insensitive Analysis

foo () { bar () {

int x; int y; x = foobar(5); y = foobar(30); } }

int foobar (int z) { int i; i = z + 1; return i; }

Page 33: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 33

Context-Insensitive Analysis

foo () { bar () {

int x; int y; x = foobar(5); y = foobar(30); } }

int foobar (int z) { int i; i = z + 1; return i; }

False PathResult: x = y = [6..31]

Page 34: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 34

Adding Context Sensitivity

• Make user functions context-sensitive– e.g. wrappers around library calls

• Inefficient method: Constraint inlining Can separate calling contexts Large number of constraint variables Cannot support recursion

Page 35: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 35

Adding Context Sensitivity

• Efficient method: Procedure summaries

• Basic Idea:– Summarize the called procedure– Insert the summary at the call-site in

the caller– Remove false paths

Page 36: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 36

Adding Context Sensitivity

foo () { bar () {

int x; int y; x = foobar(5); y = foobar(30); } }

int foobar (int z) { int i; i = z + 1; return i; }

Page 37: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 37

Adding Context Sensitivity

foo () { bar () {

int x; int y; x = foobar(5); y = foobar(30); } }

int foobar (int z) { int i; i = z + 1; return i; }

Summary: i = z + 1

x = 5 + 1 y = 30 + 1

Page 38: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 38

Adding Context Sensitivity

foo () { bar () {

int x; int y; x = foobar(5); y = foobar(30); } }

int foobar (int z) { int i; i = z + 1; return i; }

Jump Functions

Page 39: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 39

Adding Context Sensitivity

foo () { bar () {

int x; int y; x = foobar(5); y = foobar(30); } }

int foobar (int z) { int i; i = z + 1; return i; }

No false paths x = [6..6]

y = [31..31]i = [6..31]

Page 40: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 40

Adding Context Sensitivity

• Computing procedure summaries:– In most cases, reduces to a shortest

path problem– Other cases, Fourier-Motzkin variable

elimination

Page 41: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 41

Roadmap of Talk

• Tool Architecture– Constraint Generation– Constraint Resolution– Producing Warnings

• Adding Context Sensitivity• Results• Future work and Conclusions

Page 42: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 42

Results: Overruns Identified

Application LOCWarnings

Vulnerability Detected?

WU-FTPD-2.5.0 16000 139 CA-1999-13 Yes

WU-FTPD-2.6.2 18000 178 None 14 New

Sendmail-8.7.6 38000 295Identified by Wagner et al.

Yes

Sendmail-8.11.6

68000 453 CA-2003-07Yes, but…

Page 43: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 43

Results: Context Sensitivity

• WU-FTPD-2.6.2: 7310 ranges identified

• Constraint Inlining:– 5.8x number of constraints– 8.7x number of constraint variables– 406 ranges refined in at least one

calling context

• Function Summaries:– 72 ranges refined

Page 44: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 44

Roadmap of Talk

• Tool Architecture– Constraint Generation– Constraint Resolution– Producing Warnings

• Adding Context Sensitivity• Results• Future work and Conclusions

Page 45: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 45

Conclusions

• Built a tool to detect buffer overruns• Incorporated in a program

understanding framework• Current work:

– Adding Flow Sensitivity– Reducing the number of false warnings

while still maintaining scalability

Page 46: October 30, 2003CCS 2003 - Vinod Ganapathy1 Buffer Overrun Detection using Linear Programming and Static Analysis Vinod Ganapathy, Somesh Jha {vg,jha}@cs.wisc.edu.

October 30, 2003 CCS 2003 - Vinod Ganapathy 46

Buffer Overrun Detection using Linear Programming and Static Analysis

Vinod Ganapathy, Somesh Jha{vg,jha}@cs.wisc.edu

University of Wisconsin-Madison

David Chandler, David Melski, David Vitek{chandler,melski,dvitek}@grammatech.com

Grammatech Inc., Ithaca, New York