Security via Type Qualifiers 2004 SIGPL 여름학교 2004. 08. 12 조 장 우 부산외대 컴퓨터공학부 (based on J. Foster’s lecture at 2004 summer school on software security)

Slides:



Advertisements
Similar presentations
Static Analysis for Security
Advertisements

Abstraction and Modular Reasoning for the Verification of Software Corina Pasareanu NASA Ames Research Center.
SPLINT STATIC CHECKING TOOL Sripriya Subramanian 10/29/2002.
Type Qualifiers CS Software Quality Today Even after large, extensive testing efforts, commercial software is shipped riddled with errors ("bugs").
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
C Programming - Lecture 5
Using Programmer-Written Compiler Extensions to Catch Security Holes Authors: Ken Ashcraft and Dawson Engler Presented by : Hong Chen CS590F 2/7/2007.
Compiler Construction
Detecting Format String Vulnerabilities with Type Qualifier Umesh Shankar, Kunal Talwar, Jeffrey S. Foster, David Wanger University of California at Berkeley.
Java Generics.
Type Qualifiers: Lightweight Specifications to Improve Software Quality Jeffrey S. Foster.
Security via Type Qualifiers David Walker slides from: Jeff Foster, University of Maryland Work by Alex Aiken, Jeff Foster, Rob Johnson, John Kodumal,
#1 The Future of Software Security David Wagner U.C. Berkeley.
Turning Eclipse Against Itself: Finding Errors in Eclipse Sources Benjamin Livshits Stanford University.
CS 333 Introduction to Operating Systems Class 18 - File System Performance Jonathan Walpole Computer Science Portland State University.
Visualizing Type Qualifier Inference with Eclipse David Greenfieldboyce Jeffrey S. Foster University of Maryland.
Security via Type Qualifiers Jeff Foster University of Maryland Joint work with Alex Aiken, Rob Johnson, John Kodumal, Tachio Terauchi, and David Wagner.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
Guide To UNIX Using Linux Third Edition
CQual: A Tool for Adding Type Qualifiers to C Jeff Foster et al UC Berkeley OSQ Retreat, May
Inheritance and Polymorphism CS351 – Programming Paradigms.
C++ fundamentals.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Statically Detecting Likely Buffer Overflow Vulnerabilities David Larochelle David Evans University of Virginia Department of Computer Science Supported.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Security via Type Qualifiers Jeff Foster University of Maryland Joint work with Alex Aiken, Rob Johnson, John Kodumal, Tachio Terauchi, and David Wagner.
C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a ‘wrappered function’? –What is a clean interface? –How to earn your.
Chapter 6 Buffer Overflow. Buffer Overflow occurs when the program overwrites data outside the bounds of allocated memory It was one of the first exploited.
Computer Science Detecting Memory Access Errors via Illegal Write Monitoring Ongoing Research by Emre Can Sezer.
Problem of the Day  Why are manhole covers round?
Type Systems CS Definitions Program analysis Discovering facts about programs. Dynamic analysis Program analysis by using program executions.
Preliminaries on Security. Security Summer School, June What is security? Security: prevent bad things from happening – Confidential information.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Overflow Examples 01/13/2012. ACKNOWLEDGEMENTS These slides where compiled from the Malware and Software Vulnerabilities class taught by Dr Cliff Zou.
Highly Scalable Distributed Dataflow Analysis Joseph L. Greathouse Advanced Computer Architecture Laboratory University of Michigan Chelsea LeBlancTodd.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
A Tool for Pro-active Defense Against the Buffer Overrun Attack D. Bruschi, E. Rosti, R. Banfi Presented By: Warshavsky Alex.
Scientific Debugging. Errors in Software Errors are unexpected behaviors or outputs in programs As long as software is developed by humans, it will contain.
C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a wrappered function? –How to assess efficiency. –What is a clean interface?
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Sampling Dynamic Dataflow Analyses Joseph L. Greathouse Advanced Computer Architecture Laboratory University of Michigan University of British Columbia.
PROGRAMMING TESTING B MODULE 2: SOFTWARE SYSTEMS 22 NOVEMBER 2013.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Overview of C++ Polymorphism
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
Soundness of Types Ensuring that a type system is not broken.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
Object Lifetime and Pointers
Protecting Memory What is there to protect in memory?
Jonathan Walpole Computer Science Portland State University
Types for Programs and Proofs
copy_from_user copy_to_user
Protection and OS Structure
Software Security Lesson Introduction
CSC 495/583 Topics of Software Security Format String Bug (2) & Heap
CS5123 Software Validation and Quality Assurance
Semantic Type Qualifiers
CSE341: Programming Languages Lecture 11 Type Inference
Presentation transcript:

Security via Type Qualifiers 2004 SIGPL 여름학교 조 장 우 부산외대 컴퓨터공학부 (based on J. Foster’s lecture at 2004 summer school on software security)

Security Summer School, June Introduction Ensuring that software is secure is hard Standard practice for software quality: –Testing Make sure program runs correctly on set of inputs –Code auditing Convince yourself and others that your code is correct

Security Summer School, June Drawbacks to Standard Approaches Difficult Expensive Incomplete A malicious adversary is trying to exploit anything you miss!

Security Summer School, June Tools for Security What more can we do? –Build tools that analyze source code Reason about all possible runs of the program –Check limited but very useful properties Eliminate categories of errors –Develop programming models Avoid mistakes in the first place Encourage programmers to think about security

Security Summer School, June Tools Need Specifications Goal: Add specifications to programs In a way that... –Programmers will accept Lightweight –Scales to large programs –Solves many different problems

Security Summer School, June ptr( char) ptr(char)  char Type Qualifiers Extend standard type systems (C, Java, ML) –Programmers already use types –Programmers understand types –Get programmers to write down a little more... intconstANSI C Format-string vulnerabilitiestainted kernel User/kernel vulnerabilities

Security Summer School, June Application: Format String Vulnerabilities I/O functions in C use format strings printf("Hello!");Hello! printf("Hello, %s!", name);Hello, name ! Instead of printf("%s", name); Why not printf(name);?

Security Summer School, June Format String Attacks Adversary-controlled format specifier name := printf(name);/* Oops */ –Attacker sets name = “%s%s%s” to crash program –Attacker sets name = “...%n...” to write to memory Yields (often remote root) exploits Lots of these bugs –New ones weekly on bugtraq mailing list –Too restrictive to forbid variable format strings

Security Summer School, June Basic Idea to check Format String Vulnerabilities Treat all program inputs that could be controlled by attacker as tainted. Track the propagation of tainted data through the program operations Mark any variable that is assigned a value from tainted data as tainted If tainted data is used as a format string on some execution path, we detect an Format String Vulnerabilities

Security Summer School, June Using Tainted and Untainted Add qualifier annotations int printf(untainted char *fmt,...) tainted char *getenv(const char *) tainted = may be controlled by adversary untainted = must not be controlled by adversary

Security Summer School, June Subtyping void f(tainted int); untainted int a; f(a); void g(untainted int); tainted int b; g(b); OK f accepts tainted or untainted data Error g accepts only untainted data untainted  taintedtainted  untainted/ untainted  tainted

Security Summer School, June Extending the Qualifier Order to Types Q  Q’ bool Q  bool Q’ Q  Q’ int Q  int Q’

Security Summer School, June Subtyping on Function Types What about function types? Recall: S is a subtype of T if an S can be used anywhere a T is expected –When can we replace a call “f x” with a call “g x”? ? qt1’  Q qt2’  qt1  Q’ qt2

Security Summer School, June Replacing “f x” by “g x” When is qt1’  Q’ qt2’  qt1  Q qt2 ? Return type: –We are expecting qt2 (f’s return type) –So we can only return at most qt2 –qt2’  qt2 Example: A function that returns tainted can be replaced with one that returns untainted

Security Summer School, June Replacing “f x” by “g x” (cont’d) When is qt1’  Q’ qt2’  qt1  Q qt2 ? Argument type: –We are supposed to accept qt1 (f’s argument type) –So we must accept at least qt1 –qt1  qt1’ Example: A function that accepts untainted can be replaced with one that accepts tainted

Security Summer School, June Subtyping on Function Types We say that  is –Covariant in the range (subtyping dir the same) –Contravariant in the domain (subtyping dir flips) qt1’  qt1 qt2  qt2’ Q  Q’ qt1  Q qt2  qt1’  Q’ qt2’

Security Summer School, June Subtyping References The wrong rule for subtyping references is Counterexample let x = ref 0 untainted in let y = x in y := 3 tainted ; check(untainted, !x)oops! Q  Q’ qt  qt’ ref Q qt  ref Q’ qt’

Security Summer School, June You’ve Got Aliasing! We have multiple names for the same memory location –But they have different types –And we can write into memory at different types xy tainted untainted

Security Summer School, June Solution #1: Java’s Approach Java uses this subtyping rule –If S is a subclass of T, then S[] is a subclass of T[] Counterexample: –Foo[] a = new Foo[5]; –Object[] b = a; –b[0] = new Object();// forbidden at runtime –a[0].foo();// …so this can’t happen

Security Summer School, June Solution #2: Purely Static Approach Reason from rules for functions –A reference is like an object with two methods: get : unit  qt set : qt  unit –Notice that qt occurs both co- and contravariantly The right rule: Q  Q’ qt  qt’ qt’  qt ref Q qt  ref Q’ qt’ Q  Q’ qt = qt’ ref Q qt  ref Q’ qt’ or

Demo of cqual

Security Summer School, June Framework Pick some qualifiers –and relation (partial order) among qualifiers Add a few explicit qualifiers to program Infer remaining qualifiers –and check consistency untainted int  tainted int kernel ptr  user ptr

Security Summer School, June Type Qualifier Inference Two kinds of qualifiers –Explicit qualifiers: tainted, untainted,... –Unknown qualifiers:  0,  1,... Program yields constraints on qualifiers tainted   0  0  untainted Solve constraints for unknown qualifiers –Error if no solution

Security Summer School, June Adding Qualifiers to Types  ptr int ptr char tainted 00 11 22 userptr( char)tainted int  ptr(int) user

Security Summer School, June  ptr int f ptr inty z Constraint Generation ptr(int) f(x : int) = {... }y := f(z) 00 11 22 33 44 55 66 6  16  1 2  42  4 3 = 53 = 5

Security Summer School, June Constraints as Graphs 00 11 22 33 44 55 66 6  16  1 2  42  4 3 = 53 = 5 88 untainted tainted 77 99 Key idea: programs  constraints  graphs

Security Summer School, June Satisfiability via Graph Reachability 00 11 22 33 44 55 66 6  16  1 2  42  4 3 = 53 = 5 88 untainted tainted 77 99 Is there an inconsistent path through the graph?

Security Summer School, June Satisfiability via Graph Reachability 00 11 22 33 44 55 66 6  16  1 2  42  4 3 = 53 = 5 88 untainted tainted 77 99 Is there an inconsistent path through the graph?

Security Summer School, June Satisfiability via Graph Reachability 00 11 22 33 44 55 66 6  16  1 2  42  4 3 = 53 = 5 88 untainted tainted 77 99 tainted   6   1   3   5   7  untainted

Security Summer School, June Satisfiability in Linear Time Initial program of size n –Fixed set of qualifiers tainted, untainted,... Constraint generation yields O(n) constraints –Recursive abstract syntax tree walk Graph reachability takes O(n) time –Works for semi-lattices, discrete p.o., products

Security Summer School, June The Story So Far... Type qualifiers as subtyping system –Qualifiers live on the standard types –Programs  constraints  graphs Useful for a number of real-world problems Followed by: Experiments

Security Summer School, June Experiment: Format String Vulnerabilities Analyzed 10 popular unix daemon programs –Annotations shared across applications One annotated header file for standard libraries Found several known vulnerabilities –Including ones we didn’t know about User interface critical

Security Summer School, June Results: Format String Vulnerabilities NameWarnBugs identd mingetty bftpd muh121 cfengine imapd-4.7c 00 ipopd-4.7c 00 mars_nwe apache openssh-2.3.0p1 00

Security Summer School, June Experiment: User/kernel Vulnerabilities (Johnson + Wagner 04) In the Linux kernel, the kernel and user/mode programs share address space –The top 1GB is reserved for the kernel –When the kernel runs, it doesn’t need to change VM mappings Just enable access to top 1GB When kernel returns, prevent access to top 1GB kernel user unmapped 4GB 3GB 0

Security Summer School, June An Attack Suppose we add two new system calls int x; void sys_setint(int *p) { memcpy(&x, p, sizeof(x)); } void sys_getint(int *p) { memcpy(p, &x, sizeof(x)); } Suppose a user calls getint(buf) –Well-behaved program: buf points to user space –Malicious program: buf points to unmapped memory –Malicious program: buf points to kernel memory We’ve just written to kernel space! Oops!

Security Summer School, June Another Attack Can we compromise security with setint(buf)? –What if buf points to private kernel data? E.g., file buffers –Result can be read with getint

Security Summer School, June The Solution: copy_from_user, copy_to_user Our example should be written int x; void sys_setint(int *p) { copy_from_user(&x, p, sizeof(x)); } void sys_getint(int *p) { copy_to_user(p, &x, sizeof(x)); } These perform the required safety checks –On their user pointer arguments

Security Summer School, June It’s Easy to Forget These Pointers to kernel and user space look the same –That’s part of the point of the design Linux has 129 syscalls with pointers to user space –All 129 of those need to use copy_from/to –The ioctl implementation passes user pointers to device drivers (without sanitizing them first) The result: Hundreds of copy_from/_to –One (small) kernel version: 389 from, 428 to –And there’s no checking

Security Summer School, June User/Kernel Type Qualifiers We can use type qualifiers to distinguish the two kinds of pointers –kernel -- This pointer is under kernel control –user -- This pointer is under user control Subtyping kernel < user –It turns out copy_from/copy_to can accept pointers to kernel space where they expect pointers to user space

Security Summer School, June Type Signatures We add signatures for the appropriate fns: int copy_from_user(void *kernel to, void *user from, int len) int memcpy(void *kernel to, void *kernel from, int len) int x; void sys_setint(int *user p) { copy_from_user(&x, p, sizeof(x)); } void sys_getint(int *user p) { memcpy(p, &x, sizeof(x)); } Lives in kernel OK Error

Security Summer School, June Qualifiers and Type Structure Consider the following example: void ioctl(void *user arg) { struct cmd { char *datap; } c; copy_from_user(&c, arg, sizeof©); c.datap[0] = 0; // not a good idea } The pointer arg comes from the user –So datap in c also comes from the user –We shouldn’t deference it without a check

Security Summer School, June Well-Formedness Constraints Simpler example char **user p; Pointer p is under user control Therefore so is *p We want a rule like: –In type ref user (Q s), it must be that Q  user –This is a well-formedness condition on types

Security Summer School, June Well-Formedness Constraints As a type rule –We implicitly require all types to be well-formed But what about other qualifiers? –Not all qualifiers have these structural constraints –Or maybe other quals want Q  Q’ |--wf (Q’ s) Q’  Q |--wf ref Q (Q’ s)

Security Summer School, June Well-Formedness Constraints Similar constraints for struct types –Again, can specify this per-qualifier For all i, |--wf (Qi si) Q  Qi |--wf struct Q (Q1 s1, …, Qn sn)

Security Summer School, June A Tricky Example int copy_from_user(,, ); int i2cdev_ioctl(struct inode *inode, struct file *file, unsigned cmd, unsigned long arg) { …case I2C_RDWR: if (copy_from_user(&rdwr_arg, (struct i2c_rdwr_iotcl_data *) arg, sizeof(rdwr_arg))) return -EFAULT; for (i = 0; i < rdwr_arg.nmsgs; i++) { if (copy_from_user(rdwr_pa[i].buf, rdwr_arg.msgs[i].buf, rdwr_pa[i].len)) { res = -EFAULT; break; } }

Security Summer School, June A Tricky Example int copy_from_user(,, ); int i2cdev_ioctl(struct inode *inode, struct file *file, unsigned cmd, unsigned long arg) { …case I2C_RDWR: if (copy_from_user(&rdwr_arg, (struct i2c_rdwr_iotcl_data *) arg, sizeof(rdwr_arg))) return -EFAULT; for (i = 0; i < rdwr_arg.nmsgs; i++) { if (copy_from_user(rdwr_pa[i].buf, rdwr_arg.msgs[i].buf, rdwr_pa[i].len)) { res = -EFAULT; break; } } user

Security Summer School, June A Tricky Example int copy_from_user(,, ); int i2cdev_ioctl(struct inode *inode, struct file *file, unsigned cmd, unsigned long arg) { …case I2C_RDWR: if (copy_from_user(&rdwr_arg, (struct i2c_rdwr_iotcl_data *) arg, sizeof(rdwr_arg))) return -EFAULT; for (i = 0; i < rdwr_arg.nmsgs; i++) { if (copy_from_user(rdwr_pa[i].buf, rdwr_arg.msgs[i].buf, rdwr_pa[i].len)) { res = -EFAULT; break; } } OK user

Security Summer School, June A Tricky Example int copy_from_user(,, ); int i2cdev_ioctl(struct inode *inode, struct file *file, unsigned cmd, unsigned long arg) { …case I2C_RDWR: if (copy_from_user(&rdwr_arg, (struct i2c_rdwr_iotcl_data *) arg, sizeof(rdwr_arg))) return -EFAULT; for (i = 0; i < rdwr_arg.nmsgs; i++) { if (copy_from_user(rdwr_pa[i].buf, rdwr_arg.msgs[i].buf, rdwr_pa[i].len)) { res = -EFAULT; break; } } Bad user OK

Security Summer School, June Experimental Results Ran on two Linux kernels – bugs found – bugs found Needed to add 245 annotations –Copy_from/to, kmalloc, kfree, … –All Linux syscalls take user args (221 calls) Could have be done automagically (All begin with sys_)

Security Summer School, June Observations Several bugs persisted through a few kernels –8 bugs found in that persisted to –An unsound tool, MECA, found 2 of 8 bugs –==> Soundness matters! Of 11 bugs in … –9 are in device drivers –Good place to look for bugs! –Note: errors found in “core” device drivers (4 bugs in PCMCIA subsystem)

Security Summer School, June Observations Lots of churn between kernel versions –Between and bugs fixed 5 more introduced

Security Summer School, June Conclusion Type qualifiers are specifications that… –Programmers will accept Lightweight –Scale to large programs –Solve many different problems In the works: ccqual, jqual, Eclipse interface