43
Secure Programming Lecture 13: Static Analysis David Aspinall 10th March 2014

Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Secure Programming Lecture 13:Static Analysis

David Aspinall

10th March 2014

Page 2: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Outline

Overview

Vulnerabilities and analysis

Using static analysis

Simple static analysis tasks

Type checking

Style checking

Summary

Page 3: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Outline

Overview

Vulnerabilities and analysis

Using static analysis

Simple static analysis tasks

Type checking

Style checking

Summary

Page 4: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Recap

We have looked at:

É examples of vulnerabilities and exploitsÉ particular programming failure patternsÉ security engineering

Now it’s time to look at some:

É principles and tools

for ensuring software security.

Page 5: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Outline

Overview

Vulnerabilities and analysis

Using static analysis

Simple static analysis tasks

Type checking

Style checking

Summary

Page 6: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Code review and architectural analysis

Remember the secure software development process“touchpoints”, in priority order:

1. Code review and repair2. Architectural risk analysis3. Penetration testing4. Risk-based security testing5. Abuse cases6. Security requirements7. Security operations

This lecture examines static analysis as a set oftechniques to help with code review and repair.

Some advanced static analysis techniques may helpwith architectural (design) understanding too.

Page 7: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Vulnerabilities in design

Design flaws are best found through architecturalanalysis. They may be generic or context-specific.

Generic flaws

É Bad behaviour that any system may haveÉ e.g., revealing sensitive information

Context-specific flaws

É Particular to security requirements of systemÉ e.g., key length too short for long term

Page 8: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Vulnerabilities in code

Programming bugs (and sometimes more serious flaws)are possible to find through static analysis.

Generic defects

É Independent of what the code doesÉ May occur in any programÉ May be language specificÉ e.g., buffer overflow in C or C++

Context-specific defects

É Depend on particular meaning of the codeÉ Even when requirements may be generalÉ Language agnostic. AKA logic errors.É e.g., PCI-CSS rules for CC number display violated

Page 9: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic
Page 10: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Common Weakness Enumeration

Recall (from Lecture 7):

É Weaknesses classify VulnerabilitiesÉ A CWE is an identifier such as CWE-287É CWEs are organised into a hierarchyÉ The hierarchy (perhaps confusingly) allows:

É multiple appearances of same CWEÉ different types of links

É This allows multiple viewsÉ different ways to structure the same thingsÉ also given CWE numbers

See https://cwe.mitre.org

Page 11: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

CWE hierarchy

Figure : Section of CWE Hierarchy

Page 12: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

7 Pernicious Kingdoms

One developer-oriented classification was introduced byTsipenyuk, Chess, and McGraw in 2005.

1. Input validation and representation2. API abuse3. Security features4. Time and state5. Error handling6. Code quality7. Encapsulation8. Environment

This appears as the view CWE 700.

Exercise. Browse the CWE hierarchy to understandrepresentative weaknesses in each category.

Page 14: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Visualisation of CWEs at cvevis.org

Page 15: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Outline

Overview

Vulnerabilities and analysis

Using static analysis

Simple static analysis tasks

Type checking

Style checking

Summary

Page 16: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Static analysis

A white box technique. Takes as input

É source code, usuallyÉ binary code, sometimes (Q. Why?)

As output, provide a report listing either

É assurance of good behaviour (“no bugs!”) orÉ evidence of bad behaviour, ideally proposed fixes

40 years of research, growing range of techniques andtools. Some standalone, some inside compilers andIDEs.

Complexity ranges from simple scanners (linear in codesize) to much more expensive, deep code analysis,exploring possible states in program execution.

Page 17: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Static analysis for security

In principle a perfect fit for security because:

É it examines every code pathÉ it considers every possible input

Dynamic testing only reaches paths determined by testcases and only uses input data given in test suites.

Other advantages:

É often finds root cause of a problemÉ can run before code complete, even as-you-type

But also some disadvantages/challenges. . .

Page 18: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Solving an impossible task

Perfect static security analysis is of course impossible.

if halts(f) thencall expose_all_mysecrets

Rice’s Theorem (informal)For any non-trivial property of partial functions, there isno general and effective method to decide whether analgorithm computes a partial function with thatproperty.

Page 19: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Static analysis in practice

É Correctness undecidable in generalÉ focus on decidable (approximate) solutionÉ or semi-decidable + manual assistance/timeouts

É State-space explosionÉ must design/derive abstractionsÉ data: restricted domains (abstract interpretation)É code: approximate calling contexts

É Environment is unknownÉ program takes input from outsideÉ other factors, e.g., scheduling of multiple threadsÉ again, use abstractions

É Complex behaviours difficult to specifyÉ use generic specifications

Page 20: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Space of programs

Page 21: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Results of a static analysis tool

Page 22: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

False positives (false alarms)

Because the security or correctness question must beapproximated, tools cannot be perfectly precise. Theymay raise false alarms, or may miss genuinevulnerabilities.

The false positive problem is hated by users:

É too many potential problems raised by toolÉ programmers have to wade through long lists to

weed outÉ true defects may be lost, buried in details

So tools compete on false positive rate for usability.

Page 23: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

False negatives (missing defects)

In practice, tools trade-off false positives with missingdefects.

Risky for security:

É one missed bug enough for an attacker to get in

Academic research concentrates on sound techniques(if a problem exists, the algorithm will identify it), whichhave no false negatives.

But strong assumptions are needed for soundness. Inpractice, tools must accept missing defects.

How are imprecise tools measured and compared? TheUS NIST SAMATE project is working on static analysisbenchmarks.

Page 24: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Outline

Overview

Vulnerabilities and analysis

Using static analysis

Simple static analysis tasks

Type checking

Style checking

Summary

Page 25: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Static analysis jobs

There is a wide range of jobs performed by staticanalysis tools and techniques:

É Type checking: part of languageÉ Style checking: ensuring good practiceÉ Program understanding: inferring meaningÉ Property checking: ensuring no bad behaviourÉ Program verification: ensuring correct behaviourÉ Bug finding: detecting likely errors

General tools in each category may be useful forsecurity. Dedicated static security analysis toolsalso exist. Examples are HP Fortify and Coverity.

Page 26: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Outline

Overview

Vulnerabilities and analysis

Using static analysis

Simple static analysis tasks

Type checking

Style checking

Summary

Page 27: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Type systems: a discipline for programming

É Proper type systems provide strong guaranteesÉ Java, ML, Haskell: memory corruption impossibleÉ These are strongly typed languages

É Sometimes frustrating: seen as a hurdleÉ old joke: when your Haskell program finally

type-checks, it must be right!

É Do programmers accept type systems?É yes: type errors are necessary, not “false”É no: they’re overly restrictive, complicatedÉ . . . likely influence on rise of scripting languages

Page 28: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

False positives in type checking

short s = 0;int i = s;short r = i;

Page 29: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

False positives in type checking

[dice]da: javac ShortLong.javaShortLong.java:5: error: possible loss of precision

short r = i;^

required: shortfound: int

1 error

Page 30: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

False positives in type checking

int i;if (3 > 4) {

i = i + "hello";}

Page 31: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

False positives in type checking

[dice]da: javac StringInt.javaStringInt.java:5: error: incompatible types

i = i + "hello";^

required: intfound: String

Page 32: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

No false positives in Python

i = 0;if (4 < 3):

i = i + "hello";

The other way around gives:

Traceback (most recent call last):File "src/stringint.py", line 3, in <module>

i = i + "hello";TypeError: unsupported operand type(s) for +: ’int’ and ’str’

Question. Is this an advantage?

Page 33: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Type systems: intrinsic part of the language

In a statically type language, programs that can’t betype-checked don’t even have a meaning.

É Compiler will not produce codeÉ So code for ill-typed programs cannot be executedÉ Programming language specifications (formal

semantics or plain English): may give no meaning,or a special meaning.

Robin Milner captured the intuition “Well-typed programs can’t gowrong” as a theorem about denotational semantics. Adding a

number to a string gives a special denotational value “wrong”.

Page 34: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Type systems: flexible part of the language

In practice, programmers and IDEs do give meaning(sometimes even execute) partially typed programs.

Recent research: gradual typing (and related work) tomake this more precise:

É start with untyped scripting languageÉ infer types in parts of code where possibleÉ manually add type annotations elsewhereÉ . . . so compiler recovers safety in some form

Sometimes even strongly-typed languages have escape routes,e.g., via C-library calls or abominations like unsafePerformIO.

Page 35: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Type systems: massive advantage

By design, provide modularity

É write programs in separate piecesÉ type check the piecesÉ put the types together: the whole is type-checked

This property extends to the basic parts of thelanguage: we find the type of an expression from thetype of its parts.

Programming language researchers call thiscompositionality.

Research question: can we find type systems thatprovide compositional guarantees for security?

Page 36: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Outline

Overview

Vulnerabilities and analysis

Using static analysis

Simple static analysis tasks

Type checking

Style checking

Summary

Page 37: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Style checking for good practice

Informally, comparing with natural language (intuition)

É type system: becomes part of syntax of languageÉ style checking: a bit like grammar checking in NL

Style checking traditionally covers good practice

É syntactic coding standards (layout, bracketing etc)É naming conventions (e.g., UPPERCASE constants)É lint-like checking for dubious/non-portable code

É modern languages are stricter than old CÉ (or have fewer implementations)É style checking becoming part of compiler/IDEÉ but also dedicated tools with 1,000s rules

Example tools: PMD, Parasoft.

Page 38: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Style checking for good practice

typedef enum { RED, AMBER, GREEN } TrafficLight;

void showWarning(TrafficLight c){

switch (c) {case RED:printf("Stop!");

case AMBER:printf("Stop soon!");

}}

Page 39: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Style as safe practice

Legal in language, type checks and compiles fine:

[dice] da: gcc enum.c

But with warnings:

[dice] da: gcc -Wall enum.cenum.c: In function ‘showWarning’:enum.c:7:3: warning: enumeration value ‘GREEN’ not handled in switch [-Wswitch]

switch (c) {^

Question. Why have some languages decided thatomitted cases should not be allowed?

Page 40: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

CodePro Analytix

A Java programming tool acquired by Google and madefreely available, sadly not (yet) open source, so may diein longer term.

See Google’s Java Developer Tools.

Page 41: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Outline

Overview

Vulnerabilities and analysis

Using static analysis

Simple static analysis tasks

Type checking

Style checking

Summary

Page 42: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

Review Questions

Static versus dynamic analysis

É Static analysis requires access to source(sometimes binary) code. What advantages doesthat enable?

É Why do practical static analysis tools both missproblems and report false problems?

Types of static analysis tool

É Apart from type and style checking, describe threeother jobs a static analysis tool may perform.

Page 43: Secure Programming Lecture 13: Static Analysis · Vulnerabilities in code Programming bugs (and sometimes more serious flaws) are possible to find through static analysis. Generic

References and credits

Much of this lecture is based Chapters 1-4 of

É Secure Programming With Static Analysis by BrianChess and Jacob West, Addison-Wesley 2007.

Recommended reading:

É Ayewah et al. Using static analysis to find bugs,IEEE Software, 2008.