detection of software vulnerabilities: static analysis ... · bounded model checking • bounded...

188
Detection of Software Vulnerabilities: Static Analysis (Part I) Lucas Cordeiro Department of Computer Science [email protected] Systems and Software Verification Laboratory

Upload: others

Post on 31-Jul-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Detection of Software Vulnerabilities:

Static Analysis (Part I)

Lucas Cordeiro Department of Computer Science

[email protected]

Systems and Software Verification Laboratory

Page 2: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Static Analysis

•  Lucas Cordeiro (Formal Methods Group) §  [email protected] §  Office: 2.28 §  Office hours: 15-16 Tuesday, 14-15 Wednesday

•  Textbook: §  Model checking (Chapter 14) §  Software model checking. ACM Comput. Surv., 2009 §  The Cyber Security Body of Knowledge, 2019 §  Software Engineering (Chapters 8, 13)

Page 3: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Functionality demanded increased significantly –  Peer reviewing and testing

Motivating Example

Page 4: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Functionality demanded increased significantly –  Peer reviewing and testing

•  Multi-core processors with scalable shared memory / message passing –  Static and dynamic verification

Motivating Example

Page 5: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Functionality demanded increased significantly –  Peer reviewing and testing

•  Multi-core processors with scalable shared memory / message passing –  Static and dynamic verification

void *threadA(void *arg) { lock(&mutex); x++; if (x == 1) lock(&lock); unlock(&mutex); lock(&mutex); x--; if (x == 0) unlock(&lock); unlock(&mutex); }

void *threadB(void *arg) { lock(&mutex); y++; if (y == 1) lock(&lock); unlock(&mutex); lock(&mutex); y--; if (y == 0) unlock(&lock); unlock(&mutex); }

Motivating Example

Page 6: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Functionality demanded increased significantly –  Peer reviewing and testing

•  Multi-core processors with scalable shared memory / message passing –  Static and dynamic verification

void *threadA(void *arg) { lock(&mutex); x++; if (x == 1) lock(&lock); unlock(&mutex); lock(&mutex); x--; if (x == 0) unlock(&lock); unlock(&mutex); }

void *threadB(void *arg) { lock(&mutex); y++; if (y == 1) lock(&lock); unlock(&mutex); lock(&mutex); y--; if (y == 0) unlock(&lock); unlock(&mutex); }

(CS1)

Motivating Example

Page 7: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Functionality demanded increased significantly –  Peer reviewing and testing

•  Multi-core processors with scalable shared memory / message passing –  Static and dynamic verification

void *threadA(void *arg) { lock(&mutex); x++; if (x == 1) lock(&lock); unlock(&mutex); lock(&mutex); x--; if (x == 0) unlock(&lock); unlock(&mutex); }

void *threadB(void *arg) { lock(&mutex); y++; if (y == 1) lock(&lock); unlock(&mutex); lock(&mutex); y--; if (y == 0) unlock(&lock); unlock(&mutex); }

(CS1) (CS2)

Motivating Example

Page 8: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Functionality demanded increased significantly –  Peer reviewing and testing

•  Multi-core processors with scalable shared memory / message passing –  Static and dynamic verification

void *threadA(void *arg) { lock(&mutex); x++; if (x == 1) lock(&lock); unlock(&mutex); lock(&mutex); x--; if (x == 0) unlock(&lock); unlock(&mutex); }

void *threadB(void *arg) { lock(&mutex); y++; if (y == 1) lock(&lock); unlock(&mutex); lock(&mutex); y--; if (y == 0) unlock(&lock); unlock(&mutex); }

(CS1) (CS2)

(CS3)

Motivating Example

Page 9: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Functionality demanded increased significantly –  Peer reviewing and testing

•  Multi-core processors with scalable shared memory / message passing –  Static and dynamic verification

void *threadA(void *arg) { lock(&mutex); x++; if (x == 1) lock(&lock); unlock(&mutex); lock(&mutex); x--; if (x == 0) unlock(&lock); unlock(&mutex); }

void *threadB(void *arg) { lock(&mutex); y++; if (y == 1) lock(&lock); unlock(&mutex); lock(&mutex); y--; if (y == 0) unlock(&lock); unlock(&mutex); }

(CS1) (CS2)

(CS3) Deadlock

Motivating Example

Page 10: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Introduce software verification and validation

Intended learning outcomes

Page 11: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Introduce software verification and validation

•  Understand soundness and completeness concerning detection techniques

Intended learning outcomes

Page 12: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Introduce software verification and validation

•  Understand soundness and completeness concerning detection techniques

•  Emphasize the difference among static analysis, testing / simulation, and debugging

Intended learning outcomes

Page 13: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Introduce software verification and validation

•  Understand soundness and completeness concerning detection techniques

•  Emphasize the difference among static analysis, testing / simulation, and debugging

•  Explain bounded model checking of software

Intended learning outcomes

Page 14: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Introduce software verification and validation

•  Understand soundness and completeness concerning detection techniques

•  Emphasize the difference among static analysis, testing / simulation, and debugging

•  Explain bounded model checking of software

•  Explain precise memory model for software verification

Intended learning outcomes

Page 15: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Introduce software verification and validation

•  Understand soundness and completeness concerning detection techniques

•  Emphasize the difference among static analysis, testing / simulation, and debugging

•  Explain bounded model checking of software

•  Explain precise memory model for software verification

Intended learning outcomes

Page 16: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Verification: "Are we building the product right?” §  The software should conform to its specification

Verification vs Validation

Page 17: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Verification: "Are we building the product right?” §  The software should conform to its specification

•  Validation: "Are we building the right product?” §  The software should do what the user requires

Verification vs Validation

Page 18: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Verification: "Are we building the product right?” §  The software should conform to its specification

•  Validation: "Are we building the right product?” §  The software should do what the user requires

•  Verification and validation must be applied at each stage in the software process §  The discovery of defects in a system

§  The assessment of whether or not the system is usable in an operational situation

Verification vs Validation

Page 19: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Software inspections are concerned with the analysis of the static system representation to discover problems (static verification) §  Supplement by tool-based document and code analysis §  Code analysis can prove the absence of errors but might

subject to incorrect results

Static and Dynamic Verification

Page 20: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Software inspections are concerned with the analysis of the static system representation to discover problems (static verification) §  Supplement by tool-based document and code analysis §  Code analysis can prove the absence of errors but might

subject to incorrect results

•  Software testing is concerned with exercising and observing product behaviour (dynamic verification) §  The system is executed with test data §  Operational behaviour is observed §  Can reveal the presence of errors NOT their absence

Static and Dynamic Verification

Page 21: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Static and Dynamic Verification

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

Ian Sommerville. Software Engineering (6th,7th or 8th Edn) Addison Wesley

Page 22: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Careful planning is required to get the most out of dynamic and static verification §  Planning should start early in the development

process §  The plan should identify the balance between

static and dynamic verification

V & V planning

Page 23: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Careful planning is required to get the most out of dynamic and static verification §  Planning should start early in the development

process §  The plan should identify the balance between

static and dynamic verification •  V & V should establish confidence that the software

is fit for purpose

V & V planning

Page 24: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Careful planning is required to get the most out of dynamic and static verification §  Planning should start early in the development

process §  The plan should identify the balance between

static and dynamic verification •  V & V should establish confidence that the software

is fit for purpose

V & V planning

V & V planning depends on system’s purpose, user expectations and

marketing environment

Page 25: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

The V-model of development

Requirementsspecification

Systemspecification

Systemdesign

Detaileddesign

Module andunit codeand tess

Sub-systemintegrationtest plan

Systemintegrationtest plan

Acceptancetest plan

Service Acceptancetest

Systemintegration test

Sub-systemintegration test

Ian Sommerville. Software Engineering (6th,7th or 8th Edn) Addison Wesley

Page 26: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Introduce software verification and validation

•  Understand soundness and completeness concerning detection techniques

•  Emphasize the difference among static analysis, testing / simulation, and debugging

•  Explain bounded model checking of software

•  Explain unbounded model checking of software

Intended learning outcomes

Page 27: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Detection of Vulnerabilities

•  Detect the presence of vulnerabilities in the code during the development, testing, and maintenance

Page 28: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Detection of Vulnerabilities

•  Detect the presence of vulnerabilities in the code during the development, testing, and maintenance

•  Trade-off between soundness and completeness

Page 29: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Detection of Vulnerabilities

•  Detect the presence of vulnerabilities in the code during the development, testing, and maintenance

•  Trade-off between soundness and completeness §  A detection technique is sound for a given category if it can

correctly conclude that a given program has no vulnerabilities

o  An unsound detection technique may have false negatives, i.e., actual vulnerabilities that the detection technique fails to find

Page 30: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Detection of Vulnerabilities

•  Detect the presence of vulnerabilities in the code during the development, testing, and maintenance

•  Trade-off between soundness and completeness §  A detection technique is sound for a given category if it can

correctly conclude that a given program has no vulnerabilities

o  An unsound detection technique may have false negatives, i.e., actual vulnerabilities that the detection technique fails to find

§  A detection technique is complete for a given category, if any vulnerability it finds is an actual vulnerability

o  An incomplete detection technique may have false positives, i.e., it may detect issues that do not turn out to be actual vulnerabilities

Page 31: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Detection of Vulnerabilities •  Achieving soundness requires reasoning about all

executions of a program (usually an infinite number) §  This can be done by static checking of the program code

while making suitable abstractions of the executions

Page 32: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Detection of Vulnerabilities •  Achieving soundness requires reasoning about all

executions of a program (usually an infinite number) §  This can be done by static checking of the program code

while making suitable abstractions of the executions

•  Achieving completeness can be done by performing actual, concrete executions of a program that are witnesses to any vulnerability reported §  The analysis technique has to come up with concrete inputs

for the program that triggers a vulnerability

§  A typical dynamic approach is software testing: the tester writes test cases with concrete inputs and specific checks for the outputs

Page 33: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Detection of Vulnerabilities

Detection tools can use a hybrid combination of static and dynamic

analysis techniques to achieve a good trade-off between soundness and

completeness

Page 34: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Detection of Vulnerabilities

Detection tools can use a hybrid combination of static and dynamic

analysis techniques to achieve a good trade-off between soundness and

completeness

Dynamic verification should be used in conjunction with static verification to

provide full code coverage

Page 35: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Introduce software verification and validation

•  Understand soundness and completeness concerning detection techniques

•  Emphasize the difference among static analysis, testing / simulation, and debugging

•  Explain bounded model checking of software

•  Explain unbounded model checking of software

Intended learning outcomes

Page 36: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Static analysis vs Testing/Simulation

•  Checks only some of the system executions §  May miss errors

•  A successful execution is an execution that discovers one or more errors

Simulation/ testing

OK

error

Page 37: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Static analysis vs Testing/Simulation

•  Exhaustively explores all executions •  Report errors as traces •  May produce incorrect results

Model Checking

OK Error trace

Specification Line 5: … Line 12: … … Line 41:…

Page 38: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Avoiding state space explosion

•  Bounded Model Checking (BMC) § Breadth-first search (BFS) approach

•  Symbolic Execution §  Depth-first search (DFS) approach

Page 39: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Bounded Model Checking

•  Bounded model checkers explore the state space in depth

•  Can only prove correctness if all states are reachable within the bound

k = 0 k = 1

k = 2

k = 3

k = 4

k = 5

k = 6

A graph G = (V, E) consists of: •  V: a set of vertices or nodes •  E ⊆ V x V: set of edges connecting the nodes

Page 40: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Breadth-First Search (BFS) BFS(G,s) 01 for each vertex u ∈ V[G]-{s} // anchor (s) 02 colour[u] ← white // u colour 03 d[u] ← ∞ // s distance 04 π[u] ← NIL // u predecessor 05 colour[s] ← grey 06 d[s] ← 0 07 π[s] ← NIL 08 enqueue(Q,s) 09 while Q ≠ ∅ do10 u ← dequeue(Q) 11 for each v ∈ Adj[u] do 12 If colour[v] = white then 13 colour[v] ← grey 14 d[v] ← d[u] + 1 15 π[v] ← u 16 enqueue(Q,v) 17 colour[u] ← blue

Initialization of graph nodes

Initializes the anchor node (s)

Visit each adjacent node of u

Page 41: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

2

1

0

7

6

5 4

3

BFS Example

Page 42: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

BFS Example

2

1

0

7

6

5 4

3

Page 43: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

BFS Example

2

1

0

7

6

5 4

3

Page 44: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

BFS Example

2

1

0

7

6

5 4

3

Page 45: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

BFS Example

2

1

0

7

6

5 4

3

Page 46: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

BFS Example

2

1

0

7

6

5 4

3

Page 47: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

BFS Example

2

1

0

7

6

5 4

3

Page 48: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

BFS Example

2

1

0

7

6

5 4

3

Page 49: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Symbolic Execution

•  Symbolic execution explores all paths individually

•  Can only prove correctness if all paths are explored

Page 50: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Depth-first search (DFS)

Paint all vertices white and initialize the fields π with NIL where π [u] represents the predecessor of u

Page 51: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

DFS Example

2

1

0

7

6

5 4

3

1/

Page 52: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

DFS Example

2

1

0

7

6

5 4

3

1/

2/

Page 53: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

DFS Example

2

1

0

7

6

5 4

3

1/

2/

3/

Page 54: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

DFS Example

2

1

0

7

6

5 4

3

1/

4/

3/

2/

Page 55: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

DFS Example

2

1

0

7

6

5 4

3

1/

3/

4/

5/

2/

Page 56: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

DFS Example

2

1

0

7

6

5 4

3

1/

2/9

4/7

5/6

3/8

Page 57: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

DFS Example

2

1

0

7

6

5 4

3

1/

2/9

4/7

5/6

3/8

10/

Page 58: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

DFS Example

2

1

0

7

6

5 4

3

1/

2/9

4/7

5/6

3/8

10/

11/

Page 59: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

DFS Example

2

1

0

7

6

5 4

3

1/

2/9

4/7

5/6

3/8

10/

11/12

Page 60: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

DFS Example

2

1

0

7

6

5 4

3

1/

2/9

4/7

5/6

3/8

10/13

11/12

Page 61: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

DFS Example

2

1

0

7

6

5 4

3

1/14

2/9

4/7

5/6

3/8

10/13

11/12

Page 62: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

DFS Example

2

1

0

7

6

5 4

3

1/14

2/9

4/7

5/6

3/8

10/13

11/12

15/16

Page 63: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  V & V and debugging are distinct processes

V&V and debugging

Page 64: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  V & V and debugging are distinct processes •  V & V is concerned with establishing the absence or

existence of defects in a program, resp.

V&V and debugging

Page 65: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  V & V and debugging are distinct processes •  V & V is concerned with establishing the absence or

existence of defects in a program, resp. •  Debugging is concerned with two main tasks

§  Locating and §  Repairing these errors

V&V and debugging

Page 66: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  V & V and debugging are distinct processes •  V & V is concerned with establishing the absence or

existence of defects in a program, resp. •  Debugging is concerned with two main tasks

§  Locating and §  Repairing these errors

•  Debugging involves §  Formulating a hypothesis about program behaviour

§  Test these hypotheses to find the system error

V&V and debugging

Page 67: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

The debugging process

Locateerror

Designerror repair

Repairerror

Re-testprogram

Testresults Specification Test

cases

Ian Sommerville. Software Engineering (6th,7th or 8th Edn) Addison Wesley

Page 68: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Introduce software verification and validation

•  Understand soundness and completeness concerning detection techniques

•  Emphasize the difference among static analysis, testing / simulation, and debugging

•  Explain bounded model checking of software

•  Explain precise memory model for software verification

Intended learning outcomes

Page 69: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Circuit Satisfiability •  A Boolean formula contains

§  Variables whose values are 0 or 1

Page 70: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Circuit Satisfiability •  A Boolean formula contains

§  Variables whose values are 0 or 1 §  Connectives: ∧ (AND), ∨ (OR), and ¬ (NOT)

Page 71: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Circuit Satisfiability •  A Boolean formula contains

§  Variables whose values are 0 or 1 §  Connectives: ∧ (AND), ∨ (OR), and ¬ (NOT)

•  A Boolean formula is SAT if there exists some assignment to its variables that evaluates it to 1

Page 72: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Circuit Satisfiability •  A Boolean combinational circuit consists of

one or more Boolean combinational elements interconnected by wires

SAT: <x1 = 1, x2 = 1, x3 = 0>

Page 73: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Circuit-Satisfiability Problem •  Given a Boolean combinational circuit of

AND, OR, and NOT gates, is it satisfiable?

CIRCUIT-SAT = {<C> : C is a satisfiable Boolean combinational circuit}

Page 74: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Circuit-Satisfiability Problem •  Given a Boolean combinational circuit of

AND, OR, and NOT gates, is it satisfiable? §  Size: number of Boolean combinational elements

plus the number of wires o  if the circuit has k inputs, then we would have to check up to

2k possible assignments

CIRCUIT-SAT = {<C> : C is a satisfiable Boolean combinational circuit}

Page 75: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Circuit-Satisfiability Problem •  Given a Boolean combinational circuit of

AND, OR, and NOT gates, is it satisfiable? §  Size: number of Boolean combinational elements

plus the number of wires o  if the circuit has k inputs, then we would have to check up to

2k possible assignments

§  When the size of C is polynomial in k, checking each one takes Ω(2k)

o  Super-polynomial in the size of k

CIRCUIT-SAT = {<C> : C is a satisfiable Boolean combinational circuit}

Page 76: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Formula Satisfiability (SAT)

•  The SAT problem asks whether a given Boolean formula is satisfiable

SAT = {<Φ> : Φ is a satisfiable Boolean formula}

Page 77: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Formula Satisfiability (SAT)

•  The SAT problem asks whether a given Boolean formula is satisfiable

§  Example: o Φ = ((x1 →x2) ∨ ¬((¬x1 ↔ x3) ∨ x4)) ∧¬x2

SAT = {<Φ> : Φ is a satisfiable Boolean formula}

Page 78: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Formula Satisfiability (SAT)

•  The SAT problem asks whether a given Boolean formula is satisfiable

§  Example: o Φ = ((x1 →x2) ∨ ¬((¬x1 ↔ x3) ∨ x4)) ∧¬x2

o  Assignment: <x1 = 0, x2 = 0, x3 = 1, x4 = 1>

SAT = {<Φ> : Φ is a satisfiable Boolean formula}

Page 79: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Formula Satisfiability (SAT)

•  The SAT problem asks whether a given Boolean formula is satisfiable

§  Example: o Φ = ((x1 →x2) ∨ ¬((¬x1 ↔ x3) ∨ x4)) ∧¬x2

o  Assignment: <x1 = 0, x2 = 0, x3 = 1, x4 = 1>

o Φ = ((0 →0) ∨ ¬((¬0 ↔ 1) ∨ 1)) ∧¬0

SAT = {<Φ> : Φ is a satisfiable Boolean formula}

Page 80: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Formula Satisfiability (SAT)

•  The SAT problem asks whether a given Boolean formula is satisfiable

§  Example: o Φ = ((x1 →x2) ∨ ¬((¬x1 ↔ x3) ∨ x4)) ∧¬x2

o  Assignment: <x1 = 0, x2 = 0, x3 = 1, x4 = 1>

o Φ = ((0 →0) ∨ ¬((¬0 ↔ 1) ∨ 1)) ∧¬0

o Φ = (1 ∨ ¬(1 ∨ 1)) ∧1

SAT = {<Φ> : Φ is a satisfiable Boolean formula}

Page 81: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Formula Satisfiability (SAT)

•  The SAT problem asks whether a given Boolean formula is satisfiable

§  Example: o Φ = ((x1 →x2) ∨ ¬((¬x1 ↔ x3) ∨ x4)) ∧¬x2

o  Assignment: <x1 = 0, x2 = 0, x3 = 1, x4 = 1>

o Φ = ((0 →0) ∨ ¬((¬0 ↔ 1) ∨ 1)) ∧¬0

o Φ = (1 ∨ ¬(1 ∨ 1)) ∧1

o Φ = (1 ∨ 0) ∧1

SAT = {<Φ> : Φ is a satisfiable Boolean formula}

Page 82: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Formula Satisfiability (SAT)

•  The SAT problem asks whether a given Boolean formula is satisfiable

§  Example: o Φ = ((x1 →x2) ∨ ¬((¬x1 ↔ x3) ∨ x4)) ∧¬x2

o  Assignment: <x1 = 0, x2 = 0, x3 = 1, x4 = 1>

o Φ = ((0 →0) ∨ ¬((¬0 ↔ 1) ∨ 1)) ∧¬0

o Φ = (1 ∨ ¬(1 ∨ 1)) ∧1

o Φ = (1 ∨ 0) ∧1

o Φ = 1

SAT = {<Φ> : Φ is a satisfiable Boolean formula}

Page 83: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

DPLL satisfiability solving Given a Boolean formula φ in clausal form (an AND of ORs)

{{a, b}, {¬a, b}, {a,¬b}, {¬a,¬b}}

determine whether a satisfying assignment of variables to truth values exists.

Page 84: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

DPLL satisfiability solving Given a Boolean formula φ in clausal form (an AND of ORs)

{{a, b}, {¬a, b}, {a,¬b}, {¬a,¬b}}

determine whether a satisfying assignment of variables to truth values exists.

Solvers based on Davis-Putnam-Logemann-Loveland algorithm: 1. If φ = ∅ then SAT 2. If ⃞ ∈ φ then UNSAT 3. If φ = φ’ ∪ {x} then DPLL(φ’[x ↦ true])

If φ = φ’ ∪ {¬x} then DPLL(φ’[x ↦ false]) 4. Pick arbitrary x and return

DPLL(φ[x ↦ false]) ∨ DPLL(φ[x ↦ true])

{{a, b}, {¬a, b}, {a,¬b}}

{{b}, {¬b}} {{b}}

{⃞} {⃞} ∅

a ↦ false a ↦ true

b ↦ false b ↦ true b ↦ true

Page 85: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

DPLL satisfiability solving Given a Boolean formula φ in clausal form (an AND of ORs)

{{a, b}, {¬a, b}, {a,¬b}, {¬a,¬b}}

determine whether a satisfying assignment of variables to truth values exists.

Solvers based on Davis-Putnam-Logemann-Loveland algorithm: 1. If φ = ∅ then SAT 2. If ⃞ ∈ φ then UNSAT 3. If φ = φ’ ∪ {x} then DPLL(φ’[x ↦ true])

If φ = φ’ ∪ {¬x} then DPLL(φ’[x ↦ false]) 4. Pick arbitrary x and return

DPLL(φ[x ↦ false]) ∨ DPLL(φ[x ↦ true])

+  NP-complete but many heuristics and optimizations ⇒  can handle problems with 100,000’s of variables

{{a, b}, {¬a, b}, {a,¬b}}

{{b}, {¬b}} {{b}}

{⃞} {⃞} ∅

a ↦ false a ↦ true

b ↦ false b ↦ true b ↦ true

Page 86: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

SAT solving as enabling technology

Page 87: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

SAT Competition

Page 88: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Bounded Model Checking (BMC)

MC: check if a property holds for all states

Init error . . .

Page 89: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Bounded Model Checking (BMC)

MC: check if a property holds for all states BMC: check if a property holds for a subset of states

Init error . . .

k

Page 90: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Bounded Model Checking (BMC)

IS THERE ANY

ERROR?

no

yes

M, S ok

fail

MC:

Page 91: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Bounded Model Checking (BMC)

IS THERE ANY

ERROR?

IS THERE ANY

ERROR IN k

STEPS?

no

yes

completeness threshold reached

k+1 still tractable

k+1 intractable no

yes

M, S

M, S

ok

ok

fail

fail

bound

MC:

BMC:

“never” happens in practice

Page 92: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Bounded Model Checking Basic Idea: check negation of given property up to given depth

. . . M0 M1 M2 Mk-1 Mk

¬ϕ0 ¬ϕ1 ¬ϕ2 ¬ϕk-1

¬ϕk

counterexample trace

∨ ∨ ∨ ∨ transition system

property

bound

Page 93: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Bounded Model Checking Basic Idea: check negation of given property up to given depth

•  transition system M unrolled k times

–  for programs: unroll loops, unfold arrays, …

. . . M0 M1 M2 Mk-1 Mk

¬ϕ0 ¬ϕ1 ¬ϕ2 ¬ϕk-1

¬ϕk

counterexample trace

∨ ∨ ∨ ∨ transition system

property

bound

Page 94: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Bounded Model Checking Basic Idea: check negation of given property up to given depth

•  transition system M unrolled k times

–  for programs: unroll loops, unfold arrays, …

•  translated into verification condition ψ such that

ψ satisfiable iff ϕ has counterexample of max. depth k

. . . M0 M1 M2 Mk-1 Mk

¬ϕ0 ¬ϕ1 ¬ϕ2 ¬ϕk-1

¬ϕk

counterexample trace

∨ ∨ ∨ ∨ transition system

property

bound

Page 95: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Bounded Model Checking Basic Idea: check negation of given property up to given depth

•  transition system M unrolled k times

–  for programs: unroll loops, unfold arrays, …

•  translated into verification condition ψ such that

ψ satisfiable iff ϕ has counterexample of max. depth k

•  has been applied successfully to verify HW/SW systems

. . . M0 M1 M2 Mk-1 Mk

¬ϕ0 ¬ϕ1 ¬ϕ2 ¬ϕk-1

¬ϕk

counterexample trace

∨ ∨ ∨ ∨ transition system

property

bound

Page 96: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (1)

SMT decides the satisfiability of first-order logic formulae using the combination of different background theories (building-in operators)

Page 97: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (1)

SMT decides the satisfiability of first-order logic formulae using the combination of different background theories (building-in operators)

Theory Example

Equality x1=x2 ∧ ¬ (x1=x3) ⇒ ¬(x1=x3)

Page 98: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (1)

SMT decides the satisfiability of first-order logic formulae using the combination of different background theories (building-in operators)

Theory Example

Equality x1=x2 ∧ ¬ (x1=x3) ⇒ ¬(x1=x3)

Bit-vectors (b >> i) & 1 = 1

Page 99: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (1)

SMT decides the satisfiability of first-order logic formulae using the combination of different background theories (building-in operators)

Theory Example

Equality x1=x2 ∧ ¬ (x1=x3) ⇒ ¬(x1=x3)

Bit-vectors (b >> i) & 1 = 1

Linear arithmetic (4y1 + 3y2 ≥ 4) ∨ (y2 – 3y3 ≤ 3)

Page 100: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (1)

SMT decides the satisfiability of first-order logic formulae using the combination of different background theories (building-in operators)

Theory Example

Equality x1=x2 ∧ ¬ (x1=x3) ⇒ ¬(x1=x3)

Bit-vectors (b >> i) & 1 = 1

Linear arithmetic (4y1 + 3y2 ≥ 4) ∨ (y2 – 3y3 ≤ 3)

Arrays (j = k ∧ a[k]=2) ⇒ a[j]=2

Page 101: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (1)

SMT decides the satisfiability of first-order logic formulae using the combination of different background theories (building-in operators)

Theory Example

Equality x1=x2 ∧ ¬ (x1=x3) ⇒ ¬(x1=x3)

Bit-vectors (b >> i) & 1 = 1

Linear arithmetic (4y1 + 3y2 ≥ 4) ∨ (y2 – 3y3 ≤ 3)

Arrays (j = k ∧ a[k]=2) ⇒ a[j]=2

Combined theories (j ≤ k ∧ a[j]=2) ⇒ a[i] < 3

Page 102: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (2) •  Given

§  a decidable ∑-theory T §  a quantifier-free formula ϕ

ϕ is T-satisfiable iff T ∪ {ϕ} is satisfiable, i.e., there exists a structure that satisfies both formula and sentences of T

Page 103: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (2) •  Given

§  a decidable ∑-theory T §  a quantifier-free formula ϕ

ϕ is T-satisfiable iff T ∪ {ϕ} is satisfiable, i.e., there exists a structure that satisfies both formula and sentences of T

•  Given §  a set Γ ∪ {ϕ} of first-order formulae over T

ϕ is a T-consequence of Γ (Γ ⊧T ϕ) iff every model of T ∪ Γ is also a model of ϕ

Page 104: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (2) •  Given

§  a decidable ∑-theory T §  a quantifier-free formula ϕ

ϕ is T-satisfiable iff T ∪ {ϕ} is satisfiable, i.e., there exists a structure that satisfies both formula and sentences of T

•  Given §  a set Γ ∪ {ϕ} of first-order formulae over T

ϕ is a T-consequence of Γ (Γ ⊧T ϕ) iff every model of T ∪ Γ is also a model of ϕ

•  Checking Γ ⊧T ϕ can be reduced in the usual way to checking the T-satisfiability of Γ ∪ {¬ϕ}

Page 105: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (3) •  let a be an array, b, c and d be signed bit-vectors of width

16, 32 and 32 respectively, and let g be an unary function.

Page 106: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (3) •  let a be an array, b, c and d be signed bit-vectors of width

16, 32 and 32 respectively, and let g be an unary function.

Page 107: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (3) •  let a be an array, b, c and d be signed bit-vectors of width

16, 32 and 32 respectively, and let g be an unary function.

b' extends b to the signed equivalent bit-vector of size 32

Page 108: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (3) •  let a be an array, b, c and d be signed bit-vectors of width

16, 32 and 32 respectively, and let g be an unary function.

b' extends b to the signed equivalent bit-vector of size 32

replace b' by c−3 in the inequality

Page 109: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (3) •  let a be an array, b, c and d be signed bit-vectors of width

16, 32 and 32 respectively, and let g be an unary function.

b' extends b to the signed equivalent bit-vector of size 32

replace b' by c−3 in the inequality

using facts about bit-vector arithmetic

Page 110: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (4) ( )( )( ) ( ) 41331,12,, :3 −=+∧−=−∧≠ dcccgccastoreselectgstep

Page 111: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (4)

applying the theory of arrays

( ) ( ) 413112 :4 −=+∧−∧≠ dccggstep

( )( )( ) ( ) 41331,12,, :3 −=+∧−=−∧≠ dcccgccastoreselectgstep

Page 112: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (4)

applying the theory of arrays

( ) ( ) 413112 :4 −=+∧−∧≠ dccggstep

The function g implies that for all x and y, if x = y, then g (x) = g (y) (congruence rule).

10)d 5,(c AT :5 ==Sstep

( )( )( ) ( ) 41331,12,, :3 −=+∧−=−∧≠ dcccgccastoreselectgstep

Page 113: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Satisfiability Modulo Theories (4)

applying the theory of arrays

( ) ( ) 413112 :4 −=+∧−∧≠ dccggstep

The function g implies that for all x and y, if x = y, then g (x) = g (y) (congruence rule).

10)d 5,(c AT :5 ==Sstep

•  SMT solvers also apply: –  standard algebraic reduction rules –  contextual simplification

falsefalser !∧

( ) ( )777 paapa ∧=∧= !

( )( )( ) ( ) 41331,12,, :3 −=+∧−=−∧≠ dcccgccastoreselectgstep

Page 114: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

BMC of Software •  program modelled as state transition system

–  state: program counter and program variables –  derived from control-flow graph –  checked safety properties give extra nodes

•  program unfolded up to given bounds –  loop iterations –  context switches

•  unfolded program optimized to reduce blow-up –  constant propagation –  forward substitutions

int main() { int a[2], i, x; if (x==0) a[i]=0; else a[i+2]=1; assert(a[i+1]==1); }

crucial

Page 115: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

BMC of Software •  program modelled as state transition system

–  state: program counter and program variables –  derived from control-flow graph –  checked safety properties give extra nodes

•  program unfolded up to given bounds –  loop iterations –  context switches

•  unfolded program optimized to reduce blow-up –  constant propagation –  forward substitutions

•  front-end converts unrolled and optimized program into SSA

int main() { int a[2], i, x; if (x==0) a[i]=0; else a[i+2]=1; assert(a[i+1]==1); }

crucial g1 = x1 == 0 a1 = a0 WITH [i0:=0] a2 = a0 a3 = a2 WITH [2+i0:=1] a4 = g1 ? a1 : a3 t1 = a4 [1+i0] == 1

Page 116: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

BMC of Software •  program modelled as state transition system

–  state: program counter and program variables –  derived from control-flow graph –  checked safety properties give extra nodes

•  program unfolded up to given bounds –  loop iterations –  context switches

•  unfolded program optimized to reduce blow-up –  constant propagation –  forward substitutions

•  front-end converts unrolled and optimized program into SSA

•  extraction of constraints C and properties P –  specific to selected SMT solver, uses theories

•  satisfiability check of C ∧ ¬P

int main() { int a[2], i, x; if (x==0) a[i]=0; else a[i+2]=1; assert(a[i+1]==1); }

crucial ( )

( )

( )⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢

=∧

+=∧

=∧

=∧

==

=

),,(:1,2,:

:0,,:

0:

:

3114

023

02

001

11

aagiteaiastorea

aaiastorea

xg

C

( ) ⎥⎥⎥⎥

⎢⎢⎢⎢

=+∧

<+∧≥+∧

<+∧≥+∧

<∧≥

=

11,21012202

20

:

04

00

00

00

iaselectiiii

ii

P

Page 117: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Numeric Types •  SMT solvers typically provide different encodings for

numbers: –  abstract domains (Z, R) –  fixed-width bit vectors (unsigned int, …) ▹ “internalized bit-blasting”

Page 118: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Numeric Types •  SMT solvers typically provide different encodings for

numbers: –  abstract domains (Z, R) –  fixed-width bit vectors (unsigned int, …) ▹ “internalized bit-blasting”

•  verification results can depend on encodings

(a > 0) ∧ (b > 0) ⇒ (a + b > 0)

Page 119: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Numeric Types •  SMT solvers typically provide different encodings for

numbers: –  abstract domains (Z, R) –  fixed-width bit vectors (unsigned int, …) ▹ “internalized bit-blasting”

•  verification results can depend on encodings

(a > 0) ∧ (b > 0) ⇒ (a + b > 0)

valid in abstract domains such as Z or R

doesn’t hold for bitvectors, due to possible overflows

Page 120: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Numeric Types •  SMT solvers typically provide different encodings for

numbers: –  abstract domains (Z, R) –  fixed-width bit vectors (unsigned int, …) ▹ “internalized bit-blasting”

•  verification results can depend on encodings

(a > 0) ∧ (b > 0) ⇒ (a + b > 0)

–  majority of VCs solved faster if numeric types are modelled by abstract domains but possible loss of precision

–  ESBMC supports both types of encoding and also combines them to improve scalability and precision

valid in abstract domains such as Z or R

doesn’t hold for bitvectors, due to possible overflows

Page 121: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding Numeric Types as Bitvectors Bitvector encodings need to handle •  type casts and implicit conversions

§  arithmetic conversions implemented using word-level functions (part of the bitvector theory: Extract, SignExt, …)

o  different conversions for every pair of types o  uses type information provided by front-end

Page 122: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding Numeric Types as Bitvectors Bitvector encodings need to handle •  type casts and implicit conversions

§  arithmetic conversions implemented using word-level functions (part of the bitvector theory: Extract, SignExt, …)

o  different conversions for every pair of types o  uses type information provided by front-end

§  conversion to / from bool via if-then-else operator t = ite(v ≠ k, true, false) //conversion to bool v = ite(t, 1, 0) //conversion from bool

Page 123: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding Numeric Types as Bitvectors Bitvector encodings need to handle •  type casts and implicit conversions

§  arithmetic conversions implemented using word-level functions (part of the bitvector theory: Extract, SignExt, …)

o  different conversions for every pair of types o  uses type information provided by front-end

§  conversion to / from bool via if-then-else operator t = ite(v ≠ k, true, false) //conversion to bool v = ite(t, 1, 0) //conversion from bool

•  arithmetic over- / underflow §  standard requires modulo-arithmetic for unsigned integer

unsigned_overflow ⇔ (r – (r mod 2w)) < 2w

Page 124: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding Numeric Types as Bitvectors Bitvector encodings need to handle •  type casts and implicit conversions

§  arithmetic conversions implemented using word-level functions (part of the bitvector theory: Extract, SignExt, …)

o  different conversions for every pair of types o  uses type information provided by front-end

§  conversion to / from bool via if-then-else operator t = ite(v ≠ k, true, false) //conversion to bool v = ite(t, 1, 0) //conversion from bool

•  arithmetic over- / underflow §  standard requires modulo-arithmetic for unsigned integer

unsigned_overflow ⇔ (r – (r mod 2w)) < 2w §  define error literals to detect over- / underflow for other types

res_op ⇔ ¬ overflow(x, y) ∧ ¬ underflow(x, y) o  similar to conversions

Page 125: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Floating-Point Numbers •  Over-approximate floating-point by fixed-point numbers

–  encode the integral (i) and fractional (f) parts

Page 126: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Floating-Point Numbers •  Over-approximate floating-point by fixed-point numbers

–  encode the integral (i) and fractional (f) parts •  Binary encoding: get a new bit-vector b = i @ f with the

same bitwidth before and after the radix point of a.

// m = number of bits of i

// n = number of bits of f

i = Extract(b, nb + ma – 1, nb) : ma ≤ mb

SignExt(Extract(b, tb – 1, nb), ma – mb) : otherwise

f = Extract(b, nb – 1, nb – nb) : na ≤ nb

Extract(b, nb, 0) @ SignExt(b, na - nb) : otherwise

Page 127: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Floating-Point Numbers •  Over-approximate floating-point by fixed-point numbers

–  encode the integral (i) and fractional (f) parts •  Binary encoding: get a new bit-vector b = i @ f with the

same bitwidth before and after the radix point of a.

•  Rational encoding: convert a to a rational number

⎪⎪⎩

⎪⎪⎨

⎟⎠

⎞⎜⎝

⎛⎟⎠

⎞⎜⎝

⎛ +∗

+∗

=

otherwisei

fp

pfpia

n

:

0:1

2// p = number of decimal places

// m = number of bits of i

// n = number of bits of f

i = Extract(b, nb + ma – 1, nb) : ma ≤ mb

SignExt(Extract(b, tb – 1, nb), ma – mb) : otherwise

f = Extract(b, nb – 1, nb – nb) : na ≤ nb

Extract(b, nb, 0) @ SignExt(b, na - nb) : otherwise

Page 128: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Floating-point SMT Encoding •  The SMT floating-point theory is an addition to the

SMT standard, proposed in 2010 and formalises: §  Floating-point arithmetic

Page 129: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Floating-point SMT Encoding •  The SMT floating-point theory is an addition to the

SMT standard, proposed in 2010 and formalises: §  Floating-point arithmetic §  Positive and negative infinities and zeroes

Page 130: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Floating-point SMT Encoding •  The SMT floating-point theory is an addition to the

SMT standard, proposed in 2010 and formalises: §  Floating-point arithmetic §  Positive and negative infinities and zeroes §  NaNs

Page 131: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Floating-point SMT Encoding •  The SMT floating-point theory is an addition to the

SMT standard, proposed in 2010 and formalises: §  Floating-point arithmetic §  Positive and negative infinities and zeroes §  NaNs §  Comparison operators

Page 132: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Floating-point SMT Encoding •  The SMT floating-point theory is an addition to the

SMT standard, proposed in 2010 and formalises: §  Floating-point arithmetic §  Positive and negative infinities and zeroes §  NaNs §  Comparison operators §  Five rounding modes: round nearest with ties

choosing the even value, round nearest with ties choosing away from zero, round towards zero, round towards positive infinity and round towards negative infinity

Page 133: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Floating-point SMT Encoding •  Missing from the standard:

§  Floating-point exceptions §  Signaling NaNs

Page 134: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Floating-point SMT Encoding •  Missing from the standard:

§  Floating-point exceptions §  Signaling NaNs

•  Two solvers currently support the standard: §  Z3: implements all operators §  MathSAT: implements all but two operators

o  fp.rem: remainder: x - y * n, where n in Z is nearest to x/y o  fp.fma: fused multiplication and addition; (x * y) + z

Page 135: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Floating-point SMT Encoding •  Missing from the standard:

§  Floating-point exceptions §  Signaling NaNs

•  Two solvers currently support the standard: §  Z3: implements all operators §  MathSAT: implements all but two operators

o  fp.rem: remainder: x - y * n, where n in Z is nearest to x/y o  fp.fma: fused multiplication and addition; (x * y) + z

•  Both solvers offer non-standard functions: §  fp_as_ieeebv: converts floating-point to bitvectors §  fp_from_ieeebv: converts bitvectors to floating-point

Page 136: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

How to encode Floating-point programs?

•  Most operations performed at program-level to encode FP numbers have a one-to-one conversion to SMT

•  Special cases being casts to boolean types and the fp.eq operator §  Usually, cast operations are

encoded using extend/extract operation

§  Extending floating-point numbers is non-trivial because of the format

Page 137: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Cast to/from booleans

•  Simpler solutions: §  Casting booleans to floating-point numbers can be

done using an ite operator

Page 138: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Cast to/from booleans

•  Simpler solutions: §  Casting booleans to floating-point numbers can be

done using an ite operator If true, assign 1f to b

Page 139: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Cast to/from booleans

•  Simpler solutions: §  Casting booleans to floating-point numbers can be

done using an ite operator

Otherwise, assign 0f to b

Page 140: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Cast to/from booleans

•  Simpler solutions: §  Casting floating-point numbers to booleans can be

done using an equality and one not:

Page 141: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Cast to/from booleans

•  Simpler solutions: §  Casting floating-point numbers to booleans can be

done using an equality and one not: true when the

floating is not 0.0

Page 142: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Cast to/from booleans

•  Simpler solutions: §  Casting floating-point numbers to booleans can be

done using an equality and one not:

otherwise, the result is false

Page 143: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Cast to/from booleans

•  Simpler solutions: §  Casting floating-point numbers to booleans can be

done using an equality and one not:

Page 144: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Floating-point Encoding: Illustrative Example

Page 145: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Floating-point Encoding: Illustrative Example

Page 146: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Variable declarations

Floating-point Encoding: Illustrative Example

Page 147: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Nondeterministic symbol declaration (optional)

Floating-point Encoding: Illustrative Example

Page 148: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Guard used to check satisfiability

Floating-point Encoding: Illustrative Example

Page 149: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Assignment of nondeterministic

value to x

Floating-point Encoding: Illustrative Example

Page 150: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Assignment x to y

Floating-point Encoding: Illustrative Example

Page 151: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Check if the comparison satisfies the guard

Floating-point Encoding: Illustrative Example

Page 152: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Z3 produces:

Floating-point Encoding: Illustrative Example

Page 153: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  MathSAT produces:

Floating-point Encoding: Illustrative Example

Page 154: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Floating-point Encoding: Illustrative Example

Page 155: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Introduce software verification and validation

•  Understand soundness and completeness concerning detection techniques

•  Emphasize the difference among static analysis, testing / simulation, and debugging

•  Explain bounded model checking of software

•  Explain precise memory model for software verification

Intended learning outcomes

Page 156: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Pointers •  arrays and records / tuples typically handled directly by

SMT-solver •  pointers modelled as tuples

–  p.o ≙ representation of underlying object –  p.i ≙ index (if pointer used as array base)

Page 157: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Pointers •  arrays and records / tuples typically handled directly by

SMT-solver •  pointers modelled as tuples

–  p.o ≙ representation of underlying object –  p.i ≙ index (if pointer used as array base)

int main() { int a[2], i, x, *p; p=a; if (x==0) a[i]=0; else a[i+1]=1; assert(*(p+2)==1); }

Page 158: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Pointers •  arrays and records / tuples typically handled directly by

SMT-solver •  pointers modelled as tuples

–  p.o ≙ representation of underlying object –  p.i ≙ index (if pointer used as array base)

int main() { int a[2], i, x, *p; p=a; if (x==0) a[i]=0; else a[i+1]=1; assert(*(p+2)==1); }

p1 := store(p0, 0, &a[0]) ∧ p2 := store(p1, 1, 0) ∧ g2 := (x2 == 0) ∧ a1 := store(a0, i0, 0) ∧ a2 := a0 ∧ a3 := store(a2, 1+ i0, 1) ∧ a4 := ite(g1, a1, a3) ∧ p3 := store(p2, 1, select(p2 , 1)+2)

C:=

Page 159: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Pointers •  arrays and records / tuples typically handled directly by

SMT-solver •  pointers modelled as tuples

–  p.o ≙ representation of underlying object –  p.i ≙ index (if pointer used as array base)

int main() { int a[2], i, x, *p; p=a; if (x==0) a[i]=0; else a[i+1]=1; assert(*(p+2)==1); }

p1 := store(p0, 0, &a[0]) ∧ p2 := store(p1, 1, 0) ∧ g2 := (x2 == 0) ∧ a1 := store(a0, i0, 0) ∧ a2 := a0 ∧ a3 := store(a2, 1+ i0, 1) ∧ a4 := ite(g1, a1, a3) ∧ p3 := store(p2, 1, select(p2 , 1)+2)

C:=

Store object at position 0

Page 160: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Pointers •  arrays and records / tuples typically handled directly by

SMT-solver •  pointers modelled as tuples

–  p.o ≙ representation of underlying object –  p.i ≙ index (if pointer used as array base)

int main() { int a[2], i, x, *p; p=a; if (x==0) a[i]=0; else a[i+1]=1; assert(*(p+2)==1); }

p1 := store(p0, 0, &a[0]) ∧ p2 := store(p1, 1, 0) ∧ g2 := (x2 == 0) ∧ a1 := store(a0, i0, 0) ∧ a2 := a0 ∧ a3 := store(a2, 1+ i0, 1) ∧ a4 := ite(g1, a1, a3) ∧ p3 := store(p2, 1, select(p2 , 1)+2)

C:=

Store object at position 0

Store index at position 1

Page 161: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Pointers •  arrays and records / tuples typically handled directly by

SMT-solver •  pointers modelled as tuples

–  p.o ≙ representation of underlying object –  p.i ≙ index (if pointer used as array base)

int main() { int a[2], i, x, *p; p=a; if (x==0) a[i]=0; else a[i+1]=1; assert(*(p+2)==1); }

p1 := store(p0, 0, &a[0]) ∧ p2 := store(p1, 1, 0) ∧ g2 := (x2 == 0) ∧ a1 := store(a0, i0, 0) ∧ a2 := a0 ∧ a3 := store(a2, 1+ i0, 1) ∧ a4 := ite(g1, a1, a3) ∧ p3 := store(p2, 1, select(p2 , 1)+2)

C:=

Store object at position 0

Store index at position 1

Update index

Page 162: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Pointers •  arrays and records / tuples typically handled directly by

SMT-solver •  pointers modelled as tuples

–  p.o ≙ representation of underlying object –  p.i ≙ index (if pointer used as array base)

int main() { int a[2], i, x, *p; p=a; if (x==0) a[i]=0; else a[i+1]=1; assert(*(p+2)==1); }

i0 ≥ 0 ∧ i0 < 2 ∧ 1+ i0 ≥ 0 ∧ 1+ i0 < 2 ∧ select(p3 , 0) == &a[0] ∧ select(select(p3 , 0),

select(p3 , 1)) == 1

P:=

negation satisfiable (a[2] unconstrained) ⇒ assert fails

Page 163: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Memory Allocation •  model memory just as an array of bytes (array theories)

–  read and write operations to the memory array on the logic level

Page 164: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Memory Allocation •  model memory just as an array of bytes (array theories)

–  read and write operations to the memory array on the logic level

•  each dynamic object do consists of –  m ≙ memory array –  s ≙ size in bytes of m –  ρ ≙ unique identifier –  υ ≙ indicate whether the object is still alive –  l ≙ the location in the execution where m is allocated

Page 165: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Memory Allocation •  model memory just as an array of bytes (array theories)

–  read and write operations to the memory array on the logic level

•  each dynamic object do consists of –  m ≙ memory array –  s ≙ size in bytes of m –  ρ ≙ unique identifier –  υ ≙ indicate whether the object is still alive –  l ≙ the location in the execution where m is allocated

•  to detect invalid reads/writes, we check whether –  do is a dynamic object –  i is within the bounds of the memory array

( )nijdl o

k

jobjectdynamicis <≤∧⎟⎠⎞

⎜⎝⎛ =∨⇔

=0.

1__ ρ

Page 166: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Memory Allocation •  to check for invalid objects, we

–  set υ to true if the function malloc can allocate memory (do is alive)

–  set υ to false if the function free is called (do is not longer alive)

lvalid_object ⇔ (lis_dynamic_object ⇒ do.υ)

Page 167: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Encoding of Memory Allocation •  to check for invalid objects, we

–  set υ to true if the function malloc can allocate memory (do is alive)

–  set υ to false if the function free is called (do is not longer alive)

•  to detect forgotten memory, at the end of the (unrolled) program we check –  whether the do has been deallocated by the function free

ldeallocated_object ⇔ (lis_dynamic_object ⇒ ¬ do.υ)

lvalid_object ⇔ (lis_dynamic_object ⇒ do.υ)

Page 168: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Example of Memory Allocation #include <stdlib.h> void main() { char *p = malloc(5); // ρ = 1 char *q = malloc(5); // ρ = 2 p=q; free(p) p = malloc(5); // ρ = 3 free(p) }

Assume that the malloc call succeeds

Page 169: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Example of Memory Allocation #include <stdlib.h> void main() { char *p = malloc(5); // ρ = 1 char *q = malloc(5); // ρ = 2 p=q; free(p) p = malloc(5); // ρ = 3 free(p) }

memory leak: pointer reassignment makes do1.υ to become an orphan

Page 170: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Example of Memory Allocation #include <stdlib.h> void main() { char *p = malloc(5); // ρ = 1 char *q = malloc(5); // ρ = 2 p=q; free(p) p = malloc(5); // ρ = 3 free(p) }

do1.ρ=1 ∧ do1.s=5 ∧ do1.υ=true ∧ p=do1

∧ do2.ρ=2 ∧ do2.s=5 ∧ do2.υ=true ∧ q=do2

∧ p=do2 ∧ do2.υ=false ∧ do3.ρ=3 ∧ do3.s=5 ∧ do3.υ=true ∧ p=do3

∧ do3.υ=false

C:=

¬do1.υ ∧ ¬do2.υ ¬do3.υ P:=

Page 171: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Example of Memory Allocation #include <stdlib.h> void main() { char *p = malloc(5); // ρ = 1 char *q = malloc(5); // ρ = 2 p=q; free(p) p = malloc(5); // ρ = 3 free(p) }

do1.ρ=1 ∧ do1.s=5 ∧ do1.υ=true ∧ p=do1

∧ do2.ρ=2 ∧ do2.s=5 ∧ do2.υ=true ∧ q=do2

∧ p=do2 ∧ do2.υ=false ∧ do3.ρ=3 ∧ do3.s=5 ∧ do3.υ=true ∧ p=do3

∧ do3.υ=false

C:=

¬do1.υ ∧ ¬do2.υ ¬do3.υ P:=

Page 172: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Align-guaranteed memory mode •  Alignment rules require that any pointer variable

must be aligned to at least the alignment of the pointer type §  E.g., an integer pointer’s value must be aligned to at least

4 bytes, for 32-bit integers

Page 173: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Align-guaranteed memory mode •  Alignment rules require that any pointer variable

must be aligned to at least the alignment of the pointer type §  E.g., an integer pointer’s value must be aligned to at least

4 bytes, for 32-bit integers

•  Encode property assertions when dereferences occur during symbolic execution §  To guard against executions where an unaligned pointer is

dereferenced

Page 174: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Align-guaranteed memory mode •  Alignment rules require that any pointer variable

must be aligned to at least the alignment of the pointer type §  E.g., an integer pointer’s value must be aligned to at least

4 bytes, for 32-bit integers

•  Encode property assertions when dereferences occur during symbolic execution §  To guard against executions where an unaligned pointer is

dereferenced §  This is not as strong as the C standard requirement, that a

pointer variable may never hold an unaligned value o  But it provides a guarantee that any pointer dereference will either

be correctly aligned or result in a verification failure

Page 175: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

ESBMC’s memory model •  statically tracks possible pointer variable targets (objects)

–  dereferencing a pointer leads to the construction of guarded references to each potential target

Page 176: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

ESBMC’s memory model •  statically tracks possible pointer variable targets (objects)

–  dereferencing a pointer leads to the construction of guarded references to each potential target

•  C is very liberal about permitted dereferences struct foo { uint16_t bar[2]; uint8_t baz; };

struct foo qux; char *quux = &qux; quux++; *quux; pointer and object types

do not match

Page 177: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

ESBMC’s memory model •  statically tracks possible pointer variable targets (objects)

–  dereferencing a pointer leads to the construction of guarded references to each potential target

•  C is very liberal about permitted dereferences

•  SAT: immediate access to bit-level representation

struct foo { uint16_t bar[2]; uint8_t baz; };

struct foo qux; char *quux = &qux; quux++; *quux; pointer and object types

do not match

Page 178: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

ESBMC’s memory model •  statically tracks possible pointer variable targets (objects)

–  dereferencing a pointer leads to the construction of guarded references to each potential target

•  C is very liberal about permitted dereferences

•  SMT: sorts must be repeatedly unwrapped

struct foo { uint16_t bar[2]; uint8_t baz; };

struct foo qux; char *quux = &qux; quux++; *quux; pointer and object types

do not match

Page 179: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Byte-level data extraction in SMT •  access to underlying data bytes is complicated

–  requires manipulation of arrays / tuples

Page 180: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Byte-level data extraction in SMT •  access to underlying data bytes is complicated

–  requires manipulation of arrays / tuples •  problem is magnified by nondeterministic offsets

uint16_t *fuzz; if (nondet_bool()) { fuzz = &qux.bar[0]; } else { fuzz = &qux.baz; }

─ chooses accessed field nondeterministically ─ requires a byte_extract expression ─ handles the tuple that encoded the struct

Page 181: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Byte-level data extraction in SMT •  access to underlying data bytes is complicated

–  requires manipulation of arrays / tuples •  problem is magnified by nondeterministic offsets

•  supporting all legal behaviors at SMT layer difficult –  extract (unaligned) 16bit integer from *fuzz

uint16_t *fuzz; if (nondet_bool()) { fuzz = &qux.bar[0]; } else { fuzz = &qux.baz; }

─ chooses accessed field nondeterministically ─ requires a byte_extract expression ─ handles the tuple that encoded the struct

Page 182: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

Byte-level data extraction in SMT •  access to underlying data bytes is complicated

–  requires manipulation of arrays / tuples •  problem is magnified by nondeterministic offsets

•  supporting all legal behaviors at SMT layer difficult –  extract (unaligned) 16bit integer from *fuzz

•  experiments showed significantly increased memory consumption

uint16_t *fuzz; if (nondet_bool()) { fuzz = &qux.bar[0]; } else { fuzz = &qux.baz; }

─ chooses accessed field nondeterministically ─ requires a byte_extract expression ─ handles the tuple that encoded the struct

Page 183: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

“Aligned” Memory Model •  framework cannot easily be changed to SMT-level

byte representation (a la LLBMC)

Page 184: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

“Aligned” Memory Model •  framework cannot easily be changed to SMT-level

byte representation (a la LLBMC) •  push unwrapping of SMT data structures to dereference

Page 185: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

“Aligned” Memory Model •  framework cannot easily be changed to SMT-level

byte representation (a la LLBMC) •  push unwrapping of SMT data structures to dereference •  enforce C alignment rules

–  static analysis of pointer alignment eliminates need to encode unaligned data accesses → reduces number of behaviors that must be modeled

Page 186: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

“Aligned” Memory Model •  framework cannot easily be changed to SMT-level

byte representation (a la LLBMC) •  push unwrapping of SMT data structures to dereference •  enforce C alignment rules

–  static analysis of pointer alignment eliminates need to encode unaligned data accesses → reduces number of behaviors that must be modeled

–  add alignment assertions (if static analysis not conclusive)

Page 187: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

“Aligned” Memory Model •  framework cannot easily be changed to SMT-level

byte representation (a la LLBMC) •  push unwrapping of SMT data structures to dereference •  enforce C alignment rules

–  static analysis of pointer alignment eliminates need to encode unaligned data accesses → reduces number of behaviors that must be modeled

–  add alignment assertions (if static analysis not conclusive) –  extracting 16-bit integer from *fuzz if guard is true:

–  offset = 0: project bar[0] out of foo –  offset = 1: “unaligned memory access” failure –  offset = 2: project bar[1] out of foo –  offset = 3: “unaligned memory access” failure –  offset = 4: “access to object out of bounds” failure

Page 188: Detection of Software Vulnerabilities: Static Analysis ... · Bounded Model Checking • Bounded model checkers explore the state space in depth • Can only prove correctness if

•  Described the difference between soundness and completeness concerning detection techniques –  False positive and false negative

•  Pointed out the difference between static analysis and testing / simulation –  hybrid combination of static and dynamic analysis

techniques to achieve a good trade-off between soundness and completeness

•  Explained bounded model checking of software –  they have been applied successfully to verify single-

threaded software using a precise memory model

Summary