SSD 951 SECURE SOFTWARE DEVELOPMENT STATIC SECURITY ANALYSIS

  • Slides: 72
Download presentation
SSD 951: SECURE SOFTWARE DEVELOPMENT STATIC SECURITY ANALYSIS Dr. Shahriar Bijani Shahed University Fall

SSD 951: SECURE SOFTWARE DEVELOPMENT STATIC SECURITY ANALYSIS Dr. Shahriar Bijani Shahed University Fall 2016

SLIDES’ REFERENCES Secure Programming with Static Analysis, Brian Chess, Jacob West, 2008, Chapters 2,

SLIDES’ REFERENCES Secure Programming with Static Analysis, Brian Chess, Jacob West, 2008, Chapters 2, 3 , 4. 2

INTRODUCTION Static analyzer of a code is similar to spell checker! A clean detected

INTRODUCTION Static analyzer of a code is similar to spell checker! A clean detected by an static analysis doesn’t guarantee that this code is perfect; It just indicates that it is free of certain kinds of common problems Security problems can result from � the same kind of simple mistakes that lead a good speller to occasionally make a typo � lack of understanding about what secure programming needs. 3

CAPABILITIES OF STATIC ANALYSIS Static analysis tools apply checks thoroughly and consistently, without any

CAPABILITIES OF STATIC ANALYSIS Static analysis tools apply checks thoroughly and consistently, without any of the bias that a programmer might have � about which pieces of code are “interesting” from a security perspective or � which pieces of code are easy to exercise through dynamic testing. By examining the source code, static analysis tools can often point to the root cause of a security problem, not just one of its symptoms. � This is important for making sure that vulnerabilities are fixed properly. 4

CAPABILITIES OF STATIC ANALYSIS Static analysis can find errors early in development, even before

CAPABILITIES OF STATIC ANALYSIS Static analysis can find errors early in development, even before the program is run for the first time. � reduces the cost of fixing the error � the quick feedback cycle can help programmer A programmer has the opportunity to correct mistakes he or she wasn’t previously aware could even happen. static analysis tool act as a means of knowledge transfer. When a new attack is discovered, static analysis tools make it easy to recheck a large body of code � Some security defects exist in software for years before they are discovered, which makes the ability to review legacy code for newly discovered types of defects invaluable. 5

LIMITATIONS OF STATIC ANALYSIS false positive is a problem reported in a program when

LIMITATIONS OF STATIC ANALYSIS false positive is a problem reported in a program when no problem actually exists. false negative: a problem exists in the program, but the tool does not report it. The most common complaint against static analysis tools is too many false positives, AKA false alarms. (too much noise) � False positives are certainly undesirable, but from a security perspective, false negatives are much worse. For a static analysis tool to catch a defect, the defect must be visible in the code. � It is often hard to derive the design vulnerabilities only form the implementation. 6

CATEGORIES OF STATIC ANALYSIS TOOLS Static analysis is used more widely than many people

CATEGORIES OF STATIC ANALYSIS TOOLS Static analysis is used more widely than many people realize, partially because there are many kinds of static analysis tools Categories � Type checking � Style checking � Program understanding � Program verification � Property checking � Bug finding � Security review 7

TYPE CHECKING The most widely used form of static analysis Most programmers are familiar

TYPE CHECKING The most widely used form of static analysis Most programmers are familiar with The rules of the type checking are typically defined by the programming language and enforced by the compiler So, programmer gets little say in when the analysis is performed or how the analysis works. Type checking removes entire categories of programming mistakes Type checking suffers from false positives and false negatives just like all other static analysis techniques Interestingly, programmers rarely complain about a type checker’s limitations 8

TYPE CHECKING FALSE POSITIVE EXAMPLES A type-checking false positive: These Java statements do not

TYPE CHECKING FALSE POSITIVE EXAMPLES A type-checking false positive: These Java statements do not meet type safety rules even though they are logically correct. 10 short s = 0; 11 int i = s; /* the type checker allows this */ 12 short r = i; /* false positive: this will cause a 13 type checking error at compile time */ Output from the Java compiler demonstrating the typechecking false positive $ javac bar. java: 12: possible loss of precision found : int required: short r = i; /* false positive: this will cause an error 9

TYPE CHECKING FALSE NEGATIVE EXAMPLES These Java statements meet type-checking rules but will fail

TYPE CHECKING FALSE NEGATIVE EXAMPLES These Java statements meet type-checking rules but will fail at runtime Object[] objs = new String[1]; objs[0] = new Object(); The Java type checker allows an Object array variable to hold a reference to a String array (because the String class is derived from the Object class), but at runtime Java will not allow the String array to hold a reference to an object of type Object. when the code runs, it throws an Array. Store. Exception 10

STYLE CHECKING Style Checkers generally enforce a pickier and more superficial set of rules

STYLE CHECKING Style Checkers generally enforce a pickier and more superficial set of rules than a type checker. Pure style checkers enforce rules related to whitespace, naming, deprecated functions, commenting, program structure, …. Because many programmers are attached to their own version of good style, most style checkers are quite flexible about the set of rules they enforce. The errors produced by style checkers often affect the readability and the maintainability of the code but do not indicate that a particular error will occur Over time, some compilers have implemented optional style checks Many open source and commercial style checkers are available. a famous one is lint 11

STYLE CHEKIANG EXAMPLE A C function with a switch statement that does not account

STYLE CHEKIANG EXAMPLE A C function with a switch statement that does not account for all possible values of an enumerated type. 1 typedef enum { red, green, blue } Color; 2 3 char* get. Color. String(Color c) { 4 char* ret = NULL; 5 switch (c) { 6 case red: 7 printf("red"); 8 } 9 return ret; 10 } The output from gcc -Wall enum. c: 5: warning: enumeration value 'green' not handled in switch enum. c: 5: warning: enumeration value 'blue' not handled in switch 12

PROGRAM UNDERSTANDING Program understanding tools help users make sense of a large codebase. Integrated

PROGRAM UNDERSTANDING Program understanding tools help users make sense of a large codebase. Integrated development environments (IDEs) always include at least some program understanding functionality. Simple examples: “find all uses of this method” and “find the declaration of this global variable. ” � E. g. Visual Studio has many helpful options � Higher-level program understanding tools try to help programmers gain insight into the way a program works. Some try to reverse-engineer information about the design of the program based on an analysis of the implementation, This is particularly useful for programmers who need to make sense out of a large body of code that they did not write 13

PROGRAM UNDERSTANDING EXAMPLE The open source Fujaba tool suite � http: //wwwcs. uni-paderborn. de/cs/fujaba/

PROGRAM UNDERSTANDING EXAMPLE The open source Fujaba tool suite � http: //wwwcs. uni-paderborn. de/cs/fujaba/ 14

PROGRAM UNDERSTANDING EXAMPLE Visual Studio UML modeling projects 15

PROGRAM UNDERSTANDING EXAMPLE Visual Studio UML modeling projects 15

PROGRAM VERIFICATION AND PROPERTY CHECKING A program verification tool accepts a specification and a

PROGRAM VERIFICATION AND PROPERTY CHECKING A program verification tool accepts a specification and a body of code and then attempts to prove that the code is a faithful implementation of the specification. If the specification is a complete description of everything the program should do, the program verification tool can perform equivalence checking to make sure that the code and the specification exactly match Equivalence checking is widely used in the world of hardware design 16

PROGRAM VERIFICATION AND PROPERTY CHECKING Rarely do programmers have a specification that is detailed

PROGRAM VERIFICATION AND PROPERTY CHECKING Rarely do programmers have a specification that is detailed enough that it can be used for equivalence checking property checking: more commonly, verification tools check software against a partial specification that details only part of the behavior of a program. � Lightweight formal methods � The majority of property checking tools work either by applying logical inference or by performing model checking. A temporal safety property specifies an ordered sequence of events that a program must not perform. Many property checking tools focus on temporal safety properties. 17 � e. g. “a memory location should not be read after it is freed. ”

PROGRAM VERIFICATION AND PROPERTY CHECKING In Formal verification, a tool applies a rigorous mathematical

PROGRAM VERIFICATION AND PROPERTY CHECKING In Formal verification, a tool applies a rigorous mathematical approach to its verification task 18

PROPERTY CHECKING EXAMPLE A few C statements that contain a memory leak: 19

PROPERTY CHECKING EXAMPLE A few C statements that contain a memory leak: 19

PROGRAM VERIFICATION AND PROPERTY CHECKING One of the best-known security verifier was a part

PROGRAM VERIFICATION AND PROPERTY CHECKING One of the best-known security verifier was a part of the Trusted Computer System Evaluation Criteria (TCSEC), AKA “the Orange Book” [DOD, 1985]. The Orange Book to guide developers in the creation of secure systems for sale to the U. S. government and military. � The TCSEC is no longer in use, but many of the concepts it contained formed the basis for the Common Criteria (ISO/IEC standard 15408). � The Common Criteria, a system for specifying and measuring security requirements, are primarily used by government and military agencies in the United States and Europe. � Program Verification Example tools: � � SPARK, Event-B, general purpose interactive theorem provers Many other research tools. Property Checking Examples Tools: � Code Contracts, Splint, JML, Grammatech, Code. Sonar, Poly. Space, Thread. Safe, PRQA, Facebook Infer. 20

BUG FINDING The purpose of a bug finding tool is not to complain about

BUG FINDING The purpose of a bug finding tool is not to complain about formatting issues, like a style checker, nor is it to perform a complete and exhaustive comparison of the program against a specification, as a program verification tool would. Instead, a bug finder simply points out places where the program will behave in a way that the programmer did not intend. Most bug finders are easy to use because they come pre-stocked with a set of “bug idioms” (rules) that describe patterns in code that often indicate bugs. 21

BUG FINDING: AN EXAMPLE TOOL Find. Bugs (http: //www. findbugs. org): An open source

BUG FINDING: AN EXAMPLE TOOL Find. Bugs (http: //www. findbugs. org): An open source tool to find possible Java bugs according to rules are suspicious patterns in code � designed by experience of buggy programs � collected from real world 22

BUG FINDING: AN EXAMPLE TOOL 23

BUG FINDING: AN EXAMPLE TOOL 23

BUG FINDING: AN EXAMPLE TOOL The Clang Static Analyzer is an open source static

BUG FINDING: AN EXAMPLE TOOL The Clang Static Analyzer is an open source static source code analysis tool Finds bugs in C, C++, and Objective-C programs. 24

BUG FINDING EXAMPLE This Java example demonstrates one such idiom, known as double-checked locking.

BUG FINDING EXAMPLE This Java example demonstrates one such idiom, known as double-checked locking. This Example shows how Find. Bugs identifies the problem 25

SECURITY REVIEW Security-focused static analysis tools use many of the same techniques found in

SECURITY REVIEW Security-focused static analysis tools use many of the same techniques found in other tools, but their goal is identifying security problems � They apply these techniques differently. Modern security tools are more often a hybrid of property checkers and bug finders. Many security properties can be expressed as program properties. 26

SECURITY REVIEW: TOOLS The earliest security tools: � ITS 4 (2000), RATS (2001), and

SECURITY REVIEW: TOOLS The earliest security tools: � ITS 4 (2000), RATS (2001), and Flawfinder (2001). � They scanned code looking for calls to functions such as strcpy() that are easy to misuse and should be inspected as part of a manual source code review. � They were perhaps most closely related to style checkers � The things they pointed out would not necessarily cause security problems 27

SECURITY REVIEW: TOOLS Modern security tools: � More often a hybrid of property checkers

SECURITY REVIEW: TOOLS Modern security tools: � More often a hybrid of property checkers and bug finders. � Many security properties can be briefly expressed as program properties. � As a property checker: searching for potential buffer overflow vulnerabilities. e. g. “the program does not access an address outside the bounds of allocated memory”. � As a bug finder: security tools adopt the notion that developers often continue to reinvent the same insecure method of solving a problem, (an insecure programming idiom). 28

SECURITY REVIEW: TOOLS the output from a security tool still requires human review and

SECURITY REVIEW: TOOLS the output from a security tool still requires human review and is best applied as part of a code review process. Examples of static security analysis tool : � Fortify Software (http: //www. fortify. com) � Ounce Labs (http: //www. ouncelabs. com) 29

SECURITY REVIEW EXAMPLE A good security tool places much more emphasis on the second

SECURITY REVIEW EXAMPLE A good security tool places much more emphasis on the second call because it represents more than just a bad practice; it is potentially exploitable. 30

A FEW STATIC ANALYSIS TOOLS Style Checking � � Program Understanding � � Polyspace

A FEW STATIC ANALYSIS TOOLS Style Checking � � Program Understanding � � Polyspace http: //www. polyspace. com Grammatech http: //www. gramatech. com Bug Finding � � Praxis High Integrity Systems http: //www. praxis-his. com Escher Technologies http: //www. eschertech. com Property Checking � � Fujaba http: //wwwcs. uni-paderborn. de/cs/fujaba/ CAST http: //www. castsoftware. com Program Verification � � PMD http: //pmd. sourceforge. net Parasoft http: //www. parasoft. com Find. Bugs http: //www. findbugs. org Coverity http: //www. coverity. com Visual Studio http: //msdn. microsoft. com/vstudio/ Klocwork http: //www. klocwork. com Security Review � � Fortify Software http: //www. fortify. com Ounce Labs http: //www. ouncelabs. com 31

STATIC ANALYSIS PROCESS Static analysis can be viewed as a part of code review

STATIC ANALYSIS PROCESS Static analysis can be viewed as a part of code review process The code review cycle: 32

THE CODE REVIEW CYCLE: STEP 1 Establish Goals �A well-defined set of security goals

THE CODE REVIEW CYCLE: STEP 1 Establish Goals �A well-defined set of security goals will help prioritize the code that should be reviewed and criteria that should be used to review it. � Your goals should come from an assessment of the software risks you face. “If it can be reached from the Internet, it has to be reviewed before it’s released. ” “If it handles money, it has to be reviewed at least once a year. ” “We’ve been embarrassed by a series of cross-site scripting vulnerabilities. Make it stop. ” 33

THE CODE REVIEW CYCLE: STEP 2 Run Static Analysis Tools Run static analysis tools

THE CODE REVIEW CYCLE: STEP 2 Run Static Analysis Tools Run static analysis tools with the goals of the review in mind. � To start, you need to gather the target code, configure the tool to report the kinds of problems that pose the greatest risks, and disable checks that aren’t relevant. � 34

THE CODE REVIEW CYCLE: STEP 3 Review Code � Review the code with your

THE CODE REVIEW CYCLE: STEP 3 Review Code � Review the code with your own eyes. � Don’t limit yourself to just analysis results. � Neighborhood effect: We routinely find other bugs right next to a tool-reported issue. � Because tools often report a problem when they become confused in the vicinity of a sensitive operation. 35

HOW STATIC ANALYSIS TOOLS WORK Almost all static analysis tools that target security, function

HOW STATIC ANALYSIS TOOLS WORK Almost all static analysis tools that target security, function in the same way, regardless of the analysis techniques used. 36

STATIC ANALYSIS STAGES: BUILDING A MODEL The first thing a static analysis tool needs

STATIC ANALYSIS STAGES: BUILDING A MODEL The first thing a static analysis tool needs to do is transform the code to be analyzed into a program model is a set of data structures that represent the code. The model a tool creates is closely linked to the kind of analysis it performs, but generally static analysis tools borrow a lot from the compiler world. 37

STATIC ANALYSIS STAGES: BUILDING A MODEL 38

STATIC ANALYSIS STAGES: BUILDING A MODEL 38

BUILDING A MODEL Building a program model works like a compiler, in stages. Simpler/older

BUILDING A MODEL Building a program model works like a compiler, in stages. Simpler/older static analysis tools only use first stages. Lexical analysis: tokenize input Parsing: builds a parse tree from grammar Abstract Syntax Tree: simplify parse tree Semantic analysis check program well-formedness � including type-checking � Produce an Intermediate Representation (IR) � higher level than for compiler Produce model to capture control/data flows control-flow and call graphs � variable-contains-data relationships � pointer analysis: aliasing, points-to � 39

HOW STATIC ANALYSIS WORKS Build Model 40

HOW STATIC ANALYSIS WORKS Build Model 40

LEXICAL ANALYSIS Tools that operate on source code begin by transforming the code into

LEXICAL ANALYSIS Tools that operate on source code begin by transforming the code into a series of tokens, discarding unimportant features of the program text such as whitespace or comments A C program example if (ret) // probably true mat[x][y] = END_VAL; This code produces the following sequence of tokens: IF LPAREN ID(ret) RPAREN ID(mat) LBRACKET ID(x) RBRACKET LBRACKET ID(y) RBRACKET EQUAL ID(END_VAL) SEMI 41

 If all the tool is going to do is match the names of

If all the tool is going to do is match the names of dangerous functions, the analyzer can go through the token stream looking for identifiers, match them against a list of dangerous function names, and report the results. 42

PARSING A language parser uses a context-free grammar (CFG) to match the token stream.

PARSING A language parser uses a context-free grammar (CFG) to match the token stream. The grammar consists of a set of productions that describe the symbols (elements) in the language. 43

44

44

ABSTRACT SYNTAX Depending on the needs, the AST can contain a more limited number

ABSTRACT SYNTAX Depending on the needs, the AST can contain a more limited number of constructs than the source language. E. g. , for and do loops might be converted to while loops. Lowering: Significant simplification of the program in this fashion. Languages that are syntactically similar, such as C++ and Java, might 45 share many of the same AST node types

SEMANTIC ANALYSIS As the AST is being built, the tool builds a symbol table.

SEMANTIC ANALYSIS As the AST is being built, the tool builds a symbol table. For each identifier in the program, the symbol table associates the identifier with its type and a pointer to its declaration or definition A modern compiler uses the AST and the symbol and type information to generate an intermediate representation, a generic version of machine code that is suitable for optimization and then conversion into platform-specific object code. 46

TRACKING CONTROL FLOW Many static analysis algorithms (and compiler optimization techniques) explore the different

TRACKING CONTROL FLOW Many static analysis algorithms (and compiler optimization techniques) explore the different execution paths that can take place when a function is executed. To make these algorithms efficient, most tools build a control flow graph on top of the intermediate representation (e. g. AST) The nodes in a control flow graph are basic blocks: sequences of instructions that will always be executed starting at the first instruction and continuing to the last instruction, without the possibility that any instructions will be skipped. Edges in the control flow graph are directed and represent potential control flow paths between basic blocks. Back edges in a control flow graph represent potential loops. 47

TRACKING CONTROL FLOW Aim is to detect poorly structured code, e. g. multiple exits

TRACKING CONTROL FLOW Aim is to detect poorly structured code, e. g. multiple exits from a loop, dead code etc. Process again typically involves translating the program into a flow graph. By a process of repeated reduction inaccessible code and certain classes of non-termination can be identified 48

49

49

CALL GRAPH A call graph represents potential control flow between functions or methods. In

CALL GRAPH A call graph represents potential control flow between functions or methods. In the absence of function pointers or virtual methods, constructing a call graph is simply a matter of looking at the function identifiers referenced in each function. 50

CALL GRAPH EXAMPLE 51

CALL GRAPH EXAMPLE 51

TRACKING DATAflOW (INFORMATION FLOW) Dataflow analysis algorithms examine the way data move through a

TRACKING DATAflOW (INFORMATION FLOW) Dataflow analysis algorithms examine the way data move through a program. Compilers perform dataflow analysis to allocate registers, remove dead code, and perform many other optimizations. Dataflow analysis usually involves traversing a function’s control flow graph and noting where data values are generated and where they are used. Converting a function to Static Single Assignment (SSA) form is useful for many dataflow problems. To accommodate this restriction, new variables must be introduced into the program. 52

53

53

54

54

TAINT PROPAGATION Using dataflow to know which values in a program an attacker could

TAINT PROPAGATION Using dataflow to know which values in a program an attacker could potentially control It requires knowing where information enters the program and how it moves through the program. Taint propagation is the key to identifying many input validation and representation defects. E. g. a program that contains an exploitable buffer overflow almost always contains a dataflow path from an input function to a vulnerable operation. Both static and dynamic analysis tools can employ taint tracking 55

POINTER ALIASING Pointer alias analysis is another dataflow problem. Goal: to understand which pointers

POINTER ALIASING Pointer alias analysis is another dataflow problem. Goal: to understand which pointers could possibly refer to the same memory location. Many compiler optimizations require some form of alias analysis for correctness. E. g. *p 1 = 1; *p 2 = 2; a compiler can reorder the following two statements only if p 1 and p 2 do not refer to the same memory location 56

POINTER ALIASING For security tools, alias analysis is important for performing taint propagation. E.

POINTER ALIASING For security tools, alias analysis is important for performing taint propagation. E. g. p 1 = p 2; *p 1 = get. User. Input(); process. Input(*p 2); �A flow-sensitive taint-tracking algorithm needs to perform alias analysis to understand that data flow from get. User. Input() to process. Input() 57

PROGRAM SLICING Program slicing involves focusing on a particular subset of variables within a

PROGRAM SLICING Program slicing involves focusing on a particular subset of variables within a given program. The parts of the program that are relevant to the subset of variables denotes a program slice. Some applications: � Program testing & re-testing: provides focus with respect to test case design and the selection of regression tests. � Program comprehension: slicing provides a useful aid to understanding code where no documentation exists.

TYPES OF PROGRAM SLICING Backward: � For a given statement S, a forward slice

TYPES OF PROGRAM SLICING Backward: � For a given statement S, a forward slice through a program contains all statements that are affected by S. Static: � For a given statement S, a backward slice through a program contains all statements that effect whether control reaches S and also all statements that effect the value of variables that occur in S. A static program slice is calculated symbolically, i. e. takes no account of concrete data values. Dynamic: � A dynamic program slice is calculated based upon particular data values. Note that forward and backward slices can be calculated either statically or dynamically.

PROGRAM SLICING EXAMPLE Program read(X); read(Y); Q : = 0; R : = X;

PROGRAM SLICING EXAMPLE Program read(X); read(Y); Q : = 0; R : = X; while R >= Y do begin R : = R - Y; Q : = Q + 1 end; print(Q); print(R); A Program Slice read(X); read(Y); R : = X; while R >= Y do begin R : = R - Y; end; print(R); Program Slice for the variable ‘R’

STATIC ANALYSIS ALGORITHM Any advanced analysis strategy consists of at least two major pieces:

STATIC ANALYSIS ALGORITHM Any advanced analysis strategy consists of at least two major pieces: � Local analysis (Interprocedural): for analyzing an individual function � Global analysis (Intraprocedural): for analyzing interaction between functions 61

ASSERTION CHECKING Assertions: Dynamic (runtime) checks to test properties that programmer expects to be

ASSERTION CHECKING Assertions: Dynamic (runtime) checks to test properties that programmer expects to be true. Many languages supports assertions. assert( expression ) fails if expression evaluates to false Assertion tests: � Can be used for testing during the implementation phase or the test phases (unit tests) � usually considered as comments 62

ASSERTION CHECKING Java Example: private int add. Heights(int h 1, int h 2) {

ASSERTION CHECKING Java Example: private int add. Heights(int h 1, int h 2) { assert h 1 >0 && h 2 >0: "parameters should be positive"; return h 1+h 2; } 63

ASSERTION CHECKING Many security properties can be stated as assertions that must be true

ASSERTION CHECKING Many security properties can be stated as assertions that must be true for the program to be secure. C++ Example: to check for a buffer overflow in strcpy(dest, src); We can add this assertion just before calling strcpy(): assert(alloc_size(dest) > strlen(src)); strcpy(dest, src); 64

ASSERTION CHECKING FOR STATIC ANALYSIS With static analysis, we may be able to automatically

ASSERTION CHECKING FOR STATIC ANALYSIS With static analysis, we may be able to automatically determine whether assertions (if enabled) will: � always succeed � may sometimes fail (unknown) � will always fail Easy cases: � assert(true); � x= readint(); assert(x>0); � assert(false); The perfect case would be showing that assertions in a program can only succeed: thus they do not need to be checked dynamically. 65

MODEL CHECKING For temporal safety properties, it is easy to represent the property being

MODEL CHECKING For temporal safety properties, it is easy to represent the property being checked as a small finite-state automaton. Example safety properties: � “memory should be freed only once” � “only non-null pointers should be dereferenced” A model checking approach � accepts such properties as specifications, � transforms the program to be checked into an automaton (called the model), � then compares the specification to the model. 66

MODEL CHECKING A finite-state automaton for the temporal safety property “memory should be freed

MODEL CHECKING A finite-state automaton for the temporal safety property “memory should be freed only once. ” The model checker identifies the potential: If it can find a variable and a path through the program that will cause the automaton to reach its error state 67

GLOBAL ANALYSIS APPROACHES Ignore the issue: the simplest approach to global analysis! � assume

GLOBAL ANALYSIS APPROACHES Ignore the issue: the simplest approach to global analysis! � assume all problems will show themselves if the program is examined one function at a time. � a bad assumption! Example: static char prog. Name[128]; void setname(char* new. Name) { strcpy(prog. Name, new. Name); } int main(int argc, char* argv[]) { setname(argv[0]); } 68

GLOBAL ANALYSIS APPROACHES Whole-program analysis: the most ambitious approach to global analysis: � Objective:

GLOBAL ANALYSIS APPROACHES Whole-program analysis: the most ambitious approach to global analysis: � Objective: to analyze every function with a complete understanding of the context of its calling functions. � an extreme example of a context-sensitive analysis, whose objective is to take into account the context of the calling function when it determines the effects of a function call. 1. A simple way to whole-program analysis: is inlining Replacing each function call in the program with the definition of the called function Recursion presents a challenge! 2. Another approach: use a stack-based analysis model. Regardless of the technique, whole-program analysis can require a lot of time and/or memory 69

GLOBAL ANALYSIS APPROACHES Function summaries: a more flexible approach to global analysis � when

GLOBAL ANALYSIS APPROACHES Function summaries: a more flexible approach to global analysis � when a local analysis algorithm encounters a function call, the function is replaced by the function’s summary. � Summary-generation trade-off: A function’s summary can be very precise (and potentially very complex) or very imprecise (and presumably less complex) �A function summary might include both: Preconditions: requirements the calling context must meet Postconditions: the effect that the function has had on the calling context when it returns 70

GLOBAL ANALYSIS APPROACHES Function summaries: An Example A summary for the C function memcpy()

GLOBAL ANALYSIS APPROACHES Function summaries: An Example A summary for the C function memcpy() memcpy(dest, src, len) [ requires: ( alloc_size(dest) >= len ) ∧ ( alloc_size(src) >= len ) ensures: ∀ i ∈ 0. . len-1: dest[i]' == src[i] ] 71

SOME RESEARCH TOOLS o ARCHER (ARray CHeck. ER. ) is a static analysis tool

SOME RESEARCH TOOLS o ARCHER (ARray CHeck. ER. ) is a static analysis tool for checking array bounds. o ARCHER uses a custom-built solver to perform a path-sensitive interprocedural analysis of C programs. It has been used to find more than a dozen security problems in Linux, and it has found hundreds of array bounds errors in Open. BSD, Sendmail, and Postgre. SQL o BOON applies integer range analysis to determine whether a C program is capable of indexing an array outside its bounds [Wagner et al. , 2000]. o CQual requires a programmer to annotate a small number of variables as either tainted or untainted, and then uses type inference rules (along with pre-annotated system libraries) to propagate the qualifiers. After the qualifiers have been propagated, the system can detect format string vulnerabilities by type checking. o Eau Claire uses a theorem prover to create a general specification checking framework for C programs [Chess, 2002]. o o to find such common security problems as buffer overflows, file access race conditions, and format string bugs. The system checks the use of standard library functions using prewritten specifications. Developers can also use specifications to ensure that function implementations behave LAPSE, short for Lightweight Analysis for Program Security in Eclipse, is an Eclipse plug-in targeted at detecting security vulnerabilities in J 2 EE applications. It performs taint propagation to connect sources of Web input with potentially sensitive operations. It detects vulnerabilities such as SQL injection, cross-site scripting, cookie 72 poisoning, and parameter manipulation