#1 Pushdown model checking for security David Wagner U.C. Berkeley Work by Hao Chen, Ben Schwarz, and Drew Dean, Jeremy Lin, Geoff Morrison, David Schultz,

Slides:



Advertisements
Similar presentations
Static Analysis for Security
Advertisements

Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
Timed Automata.
Traps and Pitfalls: Practical Problems in System Call Interposition Based Security Tools Vinay Gangasani vcg
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
-- Finishing Design Unit -- Intro to Glass Box Testing ECEN5543 / CSCI 5548 SW Eng of Standalone Programs University of Colorado, Boulder.
A Fixpoint Calculus for Local and Global Program Flows Swarat Chaudhuri, U.Penn (with Rajeev Alur and P. Madhusudan)
Pushdown Systems Koushik Sen EECS, UC Berkeley Slide Source: Sanjit A. Seshia.
1 Temporal Claims A temporal claim is defined in Promela by the syntax: never { … body … } never is a keyword, like proctype. The body is the same as for.
Using Programmer-Written Compiler Extensions to Catch Security Holes Authors: Ken Ashcraft and Dawson Engler Presented by : Hong Chen CS590F 2/7/2007.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Atomicity in Multi-Threaded Programs Prachi Tiwari University of California, Santa Cruz CMPS 203 Programming Languages, Fall 2004.
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
© 2003 School of Computing, University of Leeds SY32 Secure Computing, Lecture 15 Implementation Flaws Part 3: Randomness and Timing Issues.
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
1 Property 3: standard file descriptors vulnerability attack.c at.c Standard File Descriptors 0:stdin 1:stdout 2:stderr close(1); close(2); execl(“at”,
#1 Program analysis for security: Making it scale David Wagner U.C. Berkeley Work by Hao Chen, Karl Chen, Rob Johnson, Ben Schwarz, and Jeremy Lin, Geoff.
TOCTTOU Vulnerabilities in UNIX-Style File Systems BY: Mayank Ladoia.
#1 The Future of Software Security David Wagner U.C. Berkeley.
MOPS MOdelchecking Security Properties David Wagner U.C. Berkeley.
Building Secure Software Chapter 9 Race Conditions.
1 Lecture 36 Attempt to prove that CFL’s are closed under intersection –Review previous constructions –Translate previous constructions to current setting.
1 Model Checking One Million Lines of C Code Hao Chen Drew Dean (SRI International) David Wagner with David Schultz, Geoff Morrison, Ben Schwarz Jacob.
1 setuid Demystified -- Examining the API of Security Operation in OS using Formal Models Hao Chen, David Wagner UC Berkeley Drew Dean SRI International.
PZ03A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03A - Pushdown automata Programming Language Design.
Model Checking Lecture 5. Outline 1 Specifications: logic vs. automata, linear vs. branching, safety vs. liveness 2 Graph algorithms for model checking.
Finite State Machines Data Structures and Algorithms for Information Processing 1.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
The Impact of Programming Language Theory on Computer Security Drew Dean Computer Science Laboratory SRI International.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Distributed Computing Systems Project 1 – Dumpster Diving Due: Tuesday, March 25 th.
1 Setuid Demystified Hao Chen David Wagner UC Berkeley Drew Dean SRI International.
CSCE 548 Code Review. CSCE Farkas2 Reading This lecture: – McGraw: Chapter 4 – Recommended: Best Practices for Peer Code Review,
Languages of nested trees Swarat Chaudhuri University of Pennsylvania (with Rajeev Alur and P. Madhusudan)
CIS 842: Specification and Verification of Reactive Systems Lecture Specifications: Sequencing Properties Copyright , Matt Dwyer, John Hatcliff,
Pushdown Automata (PDAs)
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Model Checking an Entire Linux Distribution for Security Violations Work by Benjamin Schwarz, Hao Chen, David Wagner, Geoff Morrison, Jacob West, Jeremy.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Refactoring1 Improving the structure of existing code.
Today’s Agenda  Reminder: HW #1 Due next class  Quick Review  Input Space Partitioning Software Testing and Maintenance 1.
Linux Security. Authors:- Advanced Linux Programming by Mark Mitchell, Jeffrey Oldham, and Alex Samuel, of CodeSourcery LLC published by New Riders Publishing.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Pushdown Automata Chapters Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free.
An Undergraduate Course on Software Bug Detection Tools and Techniques Eric Larson Seattle University March 3, 2006.
Unix Security Assessing vulnerabilities. Classifying vulnerability types Several models have been proposed to classify vulnerabilities in UNIX-type Oses.
1 Setuid Demystified Hao Chen David Wagner UC Berkeley Drew Dean SRI International Proceedings of the 11th USENIX Security Symposium San Francisco, California,
PZ03A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03A - Pushdown automata Programming Language Design.
Static Techniques for V&V. Hierarchy of V&V techniques Static Analysis V&V Dynamic Techniques Model Checking Simulation Symbolic Execution Testing Informal.
/ PSWLAB Evidence-Based Analysis and Inferring Preconditions for Bug Detection By D. Brand, M. Buss, V. C. Sreedhar published in ICSM 2007.
1 Model Checking One Million Lines of C Code Hao Chen, UC Berkeley Drew Dean, SRI International David Wagner, UC Berkeley.
MOPS: an Infrastructure for Examining Security Properties of Software Authors Hao Chen and David Wagner Appears in ACM Conference on Computer and Communications.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error.
STATIC CODE ANALYSIS. OUTLINE  INTRODUCTION  BACKGROUND o REGULAR EXPRESSIONS o SYNTAX TREES o CONTROL FLOW GRAPHS  TOOLS AND THEIR WORKING  ERROR.
Jeremy Nimmer, page 1 Automatic Generation of Program Specifications Jeremy Nimmer MIT Lab for Computer Science Joint work with.
Content Coverity Static Analysis Use cases of Coverity Examples
Testing and Debugging PPT By :Dr. R. Mall.
Distributed Computing Systems
CompSci 230 Software Construction
CSE 105 theory of computation
Research in Language-Based Methods
Verification of Software Security Properties
CSE 105 theory of computation
Review of Previous Lesson
CSE 105 theory of computation
MOPS: an Infrastructure for Examining Security Properties of Software
CSE 105 theory of computation
Presentation transcript:

#1 Pushdown model checking for security David Wagner U.C. Berkeley Work by Hao Chen, Ben Schwarz, and Drew Dean, Jeremy Lin, Geoff Morrison, David Schultz, Wei Tu, Jacob West

#2 Introduction Pushdown model checking: theory, background, intuition Security properties MOPS, a tool for pushdown model checking Experience with MOPS Outline

#3 setuid(getuid()) Pushdown model checking of C source code Security properties expressed as finite state automata (temporal safety properties) Introduction to MOPS Example: A simplistic FSA to detect dropping privilege in the wrong order. setgid(getgid())

#4 Abstraction of C program as a pushdown automaton PDA stack = program call stack = (pc, retaddr 1, retaddr 2,...) Models control flow behavior of program (only) Branch statements modelled non-deterministically Pushdown models of C programs void f(int n) { setuid(getuid()); g(“foo”); if (n>0) { f(n-1); } F ::= setuid(…) W W::= G X X ::= Y | Z Y ::= F Z Z ::= ε

#5  = set of security operations e.g.,  = { setuid(getuid()), setgid(getgid()) } B   *: sequences of ops that violate the property B is specified by a FSA, and thus regular T   *: set of feasible traces (sequences of ops) from the program T is a pushdown model, and thus is context-free Question: Is B  T =  ? Pushdown model checking T B

#6  = set of security operations e.g.,  = { setuid(getuid()), setgid(getgid()) } B   *: sequences of ops that violate the property B is specified by a FSA, and thus regular T   *: set of feasible traces (sequences of ops) from the program T is a pushdown model, and thus is context-free Question: Is B  T =  ? Algorithm: 1) Compute the intersection (a CFL) 2) Test for emptiness Pushdown model checking T B

#7 0 F 1 ::= setuid 1 W 10 F 2 ::= setuid 1 W 2 1 W 1 ::= 1 G 1 1 X 11 W 2 ::= 1 G 1 1 X 2 | 1 G 2 2 X 2 1 X 1 ::= 1 Y 1 | 1 Z 11 X 2 ::= 1 Y 2 | 1 Z 2 2 X 2 ::= 2 Y 2 | 2 Z 2 1 Y 1 ::= 1 F 1 1 Z 11 Y 2 ::= 1 F 1 1 Z 2 | 1 F 2 2 Z 2 2 Y 2 ::= 2 F 2 2 Z 2 1 Z 1 ::= ε 2 Z 2 ::= ε Intersecting a CFL and a FSA F ::= setuid W W::= G X X ::= Y | Z Y ::= F Z Z ::= ε T B  T Algorithm: A ::= B C -> i A k ::= i B j j C k for all FSA states i,j,k

#8 0 F 1 ::= setuid 1 W 10 F 2 ::= setuid 1 W 2 1 W 1 ::= 1 G 1 1 X 11 W 2 ::= 1 G 1 1 X 2 | 1 G 2 2 X 2 1 X 1 ::= 1 Y 1 | 1 Z 11 X 2 ::= 1 Y 2 | 1 Z 2 2 X 2 ::= 2 Y 2 | 2 Z 2 1 Y 1 ::= 1 F 1 1 Z 11 Y 2 ::= 1 F 1 1 Z 2 | 1 F 2 2 Z 2 2 Y 2 ::= 2 F 2 2 Z 2 1 Z 1 ::= ε 2 Z 2 ::= ε Testing a CFL for emptiness Worklist algorithm: 1) Mark all terminals 2) For each rule A ::= B C, if B and C are marked, mark A Continue until fixpoint reached

#9 Interpretation and intuition i F j marked  if we call f() with FSA starting in state i, f() might return with FSA in state j {(i,j) : i F j marked} = transfer function for f() Standard interprocedural analyses algorithm, with function summaries  a CFL emptiness test Some ideas inspired by [Olender,Osterweil86]

#10 A tangent: Generalized PDAs Normal PDA: Rule: α -> βγSemantics: αδ···ω => βγδ···ω Generalized PDA: Rule: [  ] α -> βγ Semantics: If αδ···ω matches regexp , αδ···ω => βγδ···ω Fact: Generalized PDAs can only recognize CFLs. Every generalized PDA is equivalent to some normal PDA. Relevance: setjmp(), Java stack inspection

#11 The MOPS implementation Fact: The set of reachable configurations of a PDA is a regular language [Büchi, Knuth] Let post*(c) = {c’ : c’ is reachable from c} Fact: post*(c) is a regular language, and can be efficiently computed [FWW97, WB98] MOPS uses post*() to check whether there exists any reachable configuration where the FSA is in error state We extend post*() to compute a “back-trace” for each reachable error configuration The PDA model is compacted before model checking, for better performance Pattern variables allow FSA to be more expressive

#12 strncpy() does not null-terminate in boundary cases; programmer must force a null terminator explicitly Easy bug to miss, since it only triggers at boundary case Misuse of strncpy() strncpy(d,s,n) other d[n-1] = ’\0’; Example: A simple FSA to detect misuse of strncpy( ). Error state indicates possible failure to null-terminate d. (Real property is much more complex: many ways to terminate; pre-termination vs. post-termination; delayed termination.)

#13 Canonical example of a TOCTTOU vulnerability: if (access(pathname, R_OK) == 0) fd = open(pathname, O_RDONLY); Notice: not an atomic operation! Bug: Permissions may change between access() & open() Attacker can arrange for this to happen in an attack TOCTTOU (time-of-check to time-of-use) check(x) use(x) check = { access, lstat, stat, readlink, statfs } use = { chmod, open, remove, unlink, mount, link, mkdir, rmdir … }

#14 Temporary file creation requires special care: 1) unguessable filename; 2) safe permissions; 3) file ops should use fd, not filename (TOCTTOU) Insecure temporary file creation/use mkstemp(x) fileop(x) fileop(x) = { open(x), chmod(x), remove(x), unlink(x) … } { tmpnam(), tempnam(), mktemp(), tmpfile() }

#15 Example of a setuid program with a stderr vulnerability: fd = open(“/etc/password”, O_RDRW); if (doit(argv[0], fd) < 0) perror(argv[0]); Threat: Attacker might call us with fd 2 closed; then perror() will over-write password file Stderr vulnerabilities Transitions along edges of cube. Program might start at any state. Calling open() from any state except OOO is an error.

#16 chroot() creates a jail. Problem: jail breaks are possible, if cwd is outside jail. Proper setup of chroot jails chroot() other chdir(“/”) Real FSA is much more complex. There are safe idioms (e.g., chdir(d); chroot(d);) not shown here.

#17 Experiment: Analyze an entire Linux distribution Redhat 9, all C packages (732 pkgs, ~ 50 MLOC) Security analysis at an unprecedented scale Team of 4 manually examined 900+ warnings 1 grad student; 3 undergrads new to MOPS Exhaustive analysis of TOCTTOU, tmpfile, others; statistical sampling of strncpy Laborious: multiple person-months of effort Found 108 new security holes in Linux apps MOPS in the large Security PropertyWarningsReal bugsBug ratio TOCTTOU790415% temporary files % strncpy1378(unknown)~ 5-10% Total

#18 Good error reporting makes a huge difference. Error causes: Find line of code that is the likely “cause”. Error clustering: Group errors by “cause”. Exhaustive error reporting: Find all errors. Show shortest/simplest ones first. Backtracking: Provide a stack dump. Also, build a trace that shows how this error point can be reached. UI: Can browse code annotated with FSA states. Let user quickly jump to location of any FSA transition. Practical issues

#19 Build integration: harder than it seems. Try #1: Edit makefiles by hand. This sucks! Try #2: Interpose with GCC_EXEC_PREFIX. Build.cfg instead of.o. Fails: autoconf,... Try #3: Interpose. Build both.cfg and.o. Fails: They get out of sync. Build process renames.o’s. Try #4: Build both, stuff.cfg into.o file with ELF tricks. Better. But reveals that gcc interposition is fragile. Try #5: Replace /usr/bin/{cc1,ld} with wrapper. Currently successful with > 98% of Debian packages. Practical issues

#20 Bug #1: “zip” d_exists = (lstat(d, &t) == 0); if (d_exists) { /* respect existing soft and hard links! */ if (t.st_nlink > 1 || (t.st_mode & S_IFMT) == S_IFLNK) copy = 1; else if (unlink(d)) return ZE_CREAT; }... eventually writes new zipfile to d... Pathname from cmd line

#21 Bug #2: “ar” exists = lstat (to, &s) == 0; if (! exists || (!S_ISLNK (s.st_mode) && s.st_nlink == 1)){ ret = rename (from, to); if (ret == 0) { if (exists) { chmod (to, s.st_mode & 0777); if (chown (to, s.st_uid, s.st_gid) >= 0) chmod (to, s.st_mode & 07777); }

#22 Bug #3 static void open_files() { int fd; create_file_names(); if (input_file == 0) { input_file = fopen(input_file_name, "r"); if (input_file == 0) open_error(input_file_name); fd = mkstemp(action_file_name); if (fd < 0 || (action_file = fdopen(fd, "w")) == NULL) { if (fd >= 0) close(fd); open_error(action_file_name); } void open_error(char *f) { perror(f); unlink(action_file_name); exit(1); }

#23 Unexpectedly, most real bugs were local False alarm rate high. Doing better requires deeper modeling of OS/filesystem semantics. Path sensitivity only good for  2x improvement Many non-bugs were still very interesting (represented fragile assumptions about environment) Engineering for analysis at scale is highly non-trivial Good UI, explanation of errors is critical Build integration so important — and so hard — that we re-implemented it no less than five times But worth it: Large-scale experiments incredibly valuable Tech. transfer: techniques being adopted in commercial security code scanning tools Lessons & surprises from the MOPS effort

#24 Software has bugs. Security bugs are particularly costly. Tools can help spot them & fix them before they’re exploited. Pushdown model checking is simple  and it works. MOPS’s analysis core is crude by modern standards, but still pretty effective: > 100 bugs, in 50M LoC. Error reporting, UI, build integration, FSA’s potentially more important than analysis itself. Make it real. Experiment at scale. How many bugs can you find in a modern Linux distribution? Concluding thoughts Questions?