4/30/2008Prof. Hilfinger CS 164 Lecture 391 Language Security Lecture 39 (from notes by G. Necula)

Slides:



Advertisements
Similar presentations
Buffer Overflows Nick Feamster CS 6262 Spring 2009 (credit to Vitaly S. from UT for slides)
Advertisements

Smashing the Stack for Fun and Profit
Exploring Security Vulnerabilities by Exploiting Buffer Overflow using the MIPS ISA Andrew T. Phillips Jack S. E. Tan Department of Computer Science University.
CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Lecture 16 Buffer Overflow modified from slides of Lawrie Brown.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Foundations of Network and Computer Security J J ohn Black Lecture #30 Nov 26 th 2007 CSCI 6268/TLEN 5831, Fall 2007.
CMSC 414 Computer and Network Security Lecture 24 Jonathan Katz.
Gabe Kanzelmeyer CS 450 4/14/10.  What is buffer overflow?  How memory is processed and the stack  The threat  Stack overrun attack  Dangers  Prevention.
Prof. Bodik CS 164 Lecture 261 Language Security Lecture 26.
Program analysis Mooly Sagiv html://
CS 536 Spring Run-time organization Lecture 19.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
Run time vs. Compile time
Overview of program analysis Mooly Sagiv html://
Intermediate Code. Local Optimizations
Run-time Environment and Program Organization
Netprog: Buffer Overflow1 Buffer Overflow Exploits Taken shamelessly from: netprog/overflow.ppt.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
4/29/09Prof. Hilfinger CS164 Lecture 381 Register Allocation Lecture 28 (from notes by G. Necula and R. Bodik)
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
1 RISE: Randomization Techniques for Software Security Dawn Song CMU Joint work with Monica Chew (UC Berkeley)
Control hijacking attacks Attacker’s goal: – Take over target machine (e.g. web server) Execute arbitrary code on target by hijacking application control.
Statically Detecting Likely Buffer Overflow Vulnerabilities David Larochelle David Evans University of Virginia Department of Computer Science Supported.
Address Obfuscation: An Efficient Approach to Combat a Broad Range of Memory Error Exploits Sandeep Bhatkar, Daniel C. DuVarney, and R. Sekar Stony Brook.
Java Security. Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security Manager.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Buffer overflows.
Web Application Access to Databases. Logistics Test 2: May 1 st (24 hours) Extra office hours: Friday 2:30 – 4:00 pm Tuesday May 5 th – you can review.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2011.
Lecture 6: Buffer Overflow CS 436/636/736 Spring 2014 Nitesh Saxena *Adopted from a previous lecture by Aleph One (Smashing the Stack for Fun and Profit)
Runtime Environments Compiler Construction Chapter 7.
Mitigation of Buffer Overflow Attacks
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Lecture 8: Buffer Overflow CS 436/636/736 Spring 2013 Nitesh Saxena *Adopted from a previous lecture by Aleph One (Smashing the Stack for Fun and Profit)
Buffer Overflow Group 7Group 8 Nathaniel CrowellDerek Edwards Punna ChalasaniAxel Abellard Steven Studniarz.
Buffer Overflow Attack Proofing of Code Binary Gopal Gupta, Parag Doshi, R. Reghuramalingam, Doug Harris The University of Texas at Dallas.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Lecture 13 Page 1 CS 236 Online Major Problem Areas for Secure Programming Certain areas of programming have proven to be particularly prone to problems.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 9: Designing Exceptionally.
Information Security - 2. A Stack Frame. Pushed to stack on function CALL The return address is copied to the CPU Instruction Pointer when the function.
Foundations of Network and Computer Security J J ohn Black CSCI 6268/TLEN 5550, Spring 2013.
Slides by Kent Seamons and Tim van der Horst Last Updated: Nov 11, 2011.
1988 Morris Worm … estimated 10% penetration 2001 Code Red … 300,00 computers breached 2003 Slammer/Sapphire … 75,00 infections in 10 min Zotob …
VM: Chapter 7 Buffer Overflows. csci5233 computer security & integrity (VM: Ch. 7) 2 Outline Impact of buffer overflows What is a buffer overflow? Types.
Analyzing C/C++ Vulnerabilities -- Mike Gerschefske.
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2014.
@Yuan Xue Worm Attack Yuan Xue Fall 2012.
CS 2130 Lecture 6 Pointers and Arrays. Pointers are like jumps, leading wildly from one part of the data structure to another. Their introduction into.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Language-Based Security: Overview of Types Deepak Garg Foundations of Security and Privacy October 27, 2009.
Content Coverity Static Analysis Use cases of Coverity Examples
Shellcode COSC 480 Presentation Alison Buben.
Refs: rootshell, antionline, your favorite hacker site…
Major Problem Areas for Secure Programming
Run-time organization
CMSC 414 Computer and Network Security Lecture 21
CS 465 Buffer Overflow Slides by Kent Seamons and Tim van der Horst
Software Security Lesson Introduction
Foundations of Network and Computer Security
Lecture 9: Buffer Overflow*
Covering CWE with Programming Languages and Tools
CS5123 Software Validation and Quality Assurance
SPL – PS2 C++ Memory Handling.
Presentation transcript:

4/30/2008Prof. Hilfinger CS 164 Lecture 391 Language Security Lecture 39 (from notes by G. Necula)

4/30/2008Prof. Hilfinger CS 164 Lecture 392 Lecture Outline Beyond compilers –Looking at other issues in programming language design and tools C –Arrays –Exploiting buffer overruns –Detecting buffer overruns

4/30/2008Prof. Hilfinger CS 164 Lecture 393 Platitudes Language design has influence on –Efficiency –Safety –Security

4/30/2008Prof. Hilfinger CS 164 Lecture 394 C Design Principles Small language Maximum efficiency Safety less important Designed for the world as it was in 1972 –Weak machines –Superhuman programmers (or so they thought) –Trusted networks

4/30/2008Prof. Hilfinger CS 164 Lecture 395 Arrays in C char buffer[100]; Declares and allocates an array of 100 chars 100 *sizeof(char)

4/30/2008Prof. Hilfinger CS 164 Lecture 396 C Array Operations char buf1[100], buf2[100]; Write: buf1[0] = ‘a’; Read: return buf2[0];

4/30/2008Prof. Hilfinger CS 164 Lecture 397 What’s Wrong with this Picture? int i; for(i = 0; buf1[i] != ‘\0’; i++) { buf2[i] = buf1[i]; } buf2[i] = ‘\0’;

4/30/2008Prof. Hilfinger CS 164 Lecture 398 Indexing Out of Bounds The following are all well-typed C and may generate no run-time errors char buffer[100]; buffer[-1] = ‘a’; buffer[100] = ‘a’; buffer[100000] = ‘a’;

4/30/2008Prof. Hilfinger CS 164 Lecture 399 Why? Why does C allow out-of-bounds array references? –Proving at compile-time that all array references are in bounds is impossible in most languages –Checking at run-time that all array references are in bounds is “expensive” But it is even more expensive to skip the checks

4/30/2008Prof. Hilfinger CS 164 Lecture 3910 Code Generation for Arrays The C code: buf1[i] = 1; /* buf1 has type int[] */ C with bounds checks r1 = &buf1; r2 = load i; r3 = r2 * 4; if r3 < 0 then error; r5 = load limit of buf1; if r3 >= r5 then error; r4 = r1 + r3 store r4, 1 Regular C r1 = &buf1; r2 = load i; r3 = r2 * 4; r4 = r1 + r3 store r4, 1 The assembly code: Costly! Finding the array limits is non-trivial

4/30/2008Prof. Hilfinger CS 164 Lecture 3911 C vs. Java C array reference typical case –Offset calculation –Memory operation (load or store) Java array reference typical case –Offset calculation –Memory operation (load or store) –Array bounds check –Type compatibility check (for some arrays)

4/30/2008Prof. Hilfinger CS 164 Lecture 3912 Buffer Overruns A buffer overrun writes past the end of an array Buffer usually refers to a C array of char –But can be any array So who’s afraid of a buffer overrun? –Can cause a core dump –Can damage data structures –What else?

4/30/2008Prof. Hilfinger CS 164 Lecture 3913 Stack Smashing Buffer overruns can alter the control flow of your program! char buffer[100]; /* stack allocated array */ 100 *sizeof(char) return address

4/30/2008Prof. Hilfinger CS 164 Lecture 3914 An Overrun Vulnerability void foo(char in[]) { char buffer[100]; int i = 0; for(i = 0; in[i] != ‘\0’; i++) { buffer[i] = in[i]; } buffer[i] = ‘\0’; }

4/30/2008Prof. Hilfinger CS 164 Lecture 3915 An Interesting Idea char in[104] = { ‘ ‘,…,’ ‘, magic 4 chars } foo(in); /* Return here: L RET */ 100 *sizeof(char) return address foo entry (L RET ) 100 *sizeof(char) return address foo exit magic 4 chars

4/30/2008Prof. Hilfinger CS 164 Lecture 3916 Discussion So we can make foo jump wherever we like. Result of unanticipated interaction of two features: –Unchecked array operations –Stack-allocated arrays and return addresses Knowledge of frame layout allows prediction of where array and return address are stored –Note the “magic cast” from char’s to an address

4/30/2008Prof. Hilfinger CS 164 Lecture 3917 The Rest of the Story Say that foo is part of a network server and the in originates in a received message –Some remote user can make foo jump anywhere ! But where is a “useful” place to jump? –Idea: Jump to some code that gives you control of the host system (e.g. code that spawns a shell) But where to put such code? –Idea: Put the code in the same buffer and jump there!

4/30/2008Prof. Hilfinger CS 164 Lecture 3918 The Plan We’ll make the code jump to the following code: In C: exec(“/bin/sh”); In assembly (pretend): mov $a0, 15 ; load the syscall code for “exec” mov $a1, &Ldata ; load the command syscall ; make the system call Ldata:.byte ‘/’,’b’,’i’,’n’,’/’,’s’,’h’,0 ; null-terminated In machine code: 0x20, 0x42, 0x00, …

4/30/2008Prof. Hilfinger CS 164 Lecture 3919 The Plan char in[104] = { 104 magic chars } foo(in); return address foo exit 0x20, 0x42, 0x00, … The last 4 bytes in “in” must be address of start of buffer Its position might depend on many factors !

4/30/2008Prof. Hilfinger CS 164 Lecture 3920 Guess the Location of the Injected Code Trial & error: gives you a ballpark Then pad the injected code with NOP –E.g. add $0, $1, 0x2020 stores result in $0 which is hardwired to 0 anyway Encoded as 0x return address foo exit 0x20, …, 0x20, 0x20, 0x42, 0x00, … Works even with an approximate address of buffer ! The bad code

4/30/2008Prof. Hilfinger CS 164 Lecture 3921 More Problems We do not know exactly where the return address is –Depends on how the compiler chose to allocate variables in the stack frame Solution: pad the buffer at the end with many copies of the “magic return address X” return address foo exit 0x20, …, 0x20, 0x20, 0x42, 0x00, …, X, X, X, X, …, X, X, … The bad code

4/30/2008Prof. Hilfinger CS 164 Lecture 3922 Even More Problems The most common way to copy the bad code in a stack buffer is using string functions: strcpy, strcat, etc. This means that buf cannot contain 0x00 bytes –Why? Solution: –Rewrite the code carefully –Instead of “addiu $4,$0,0x0015 (code 0x ) –Use “addiu $4,$0,0x1126; subiu $4, $4, 0x1111”

4/30/2008Prof. Hilfinger CS 164 Lecture 3923 The State of C Programming Buffer overruns are common –Programmers must do their own bounds checking –Easy to forget or be off-by-one or more –Program still appears to work correctly In C w.r.t. to buffer overruns –Easy to do the wrong thing –Hard to do the right thing

4/30/2008Prof. Hilfinger CS 164 Lecture 3924 The State of Cracking Buffer overruns are the attack of choice –40-50% of new vulnerabilities are buffer overrun exploits –Many attacks of this flavor: Code Red, Nimda, MS- SQL server Highly automated toolkits available to exploit known buffer overruns –Search for “buffer overruns” yields > 25,000 hits

4/30/2008Prof. Hilfinger CS 164 Lecture 3925 The Sad Reality Even well-known buffer overruns are still widely exploited –Hard to get people to upgrade millions of vulnerable machines We assume that there are many more unknown buffer overrun vulnerabilities –At least unknown to the good guys

4/30/2008Prof. Hilfinger CS 164 Lecture 3926 Blunt-Force Solutions Common architectures can disallow execution of code on the stack or on the heap. Unfortunately, there are legitimate uses for both.

4/30/2008Prof. Hilfinger CS 164 Lecture 3927 Static Analysis to Detect Buffer Overruns Detecting buffer overruns before distributing code would be better Idea: Build a tool similar to a type checker to detect buffer overruns Joint work by Alex Aiken, David Wagner, Jeff Foster, at Berkeley

4/30/2008Prof. Hilfinger CS 164 Lecture 3928 Focus on Strings Most important buffer overrun exploits are through string buffers –Reading an untrusted string from the network, keyboard, etc. Focus the tool only on arrays of characters

4/30/2008Prof. Hilfinger CS 164 Lecture 3929 Idea 1: Strings as an Abstract Data Type A problem: Pointer operations & array dereferences are very difficult to analyze statically –Where does *a point? –What does buf[j] refer to? Idea: Model effect of string library functions directly –Hard code effect of strcpy, strcat, etc.

4/30/2008Prof. Hilfinger CS 164 Lecture 3930 Idea 2: The Abstraction Model buffers as pairs of integer ranges –Alloc min allocated size of the buffer in bytes –Length max number of bytes actually in use Use integer ranges [x,y] = { x, x+1, …, y-1, y } –Alloc & length cannot be computed exactly

4/30/2008Prof. Hilfinger CS 164 Lecture 3931 The Strategy For each program expression, write constraints capturing the alloc and len of its string subexpressions Solve the constraints for the entire program Check for each string variable s len(s)  alloc(s)

4/30/2008Prof. Hilfinger CS 164 Lecture 3932 The Constraints char s[n];n  alloc(s) strcpy(dst,src)len(src)  len(dst) p = strdup(s)len(s)  len(p) & len(s)  alloc(p) p[n] = ‘\0’n+1  len(p)

4/30/2008Prof. Hilfinger CS 164 Lecture 3933 Constraint Solving Solving the constraints is akin to solving dataflow equations (e.g., constant propagation) Build a graph –Nodes are len(s), alloc(s) –Edges are constraints len(s)  len(t) Propagate information forward through the graph –Special handling of loops in the graph

4/30/2008Prof. Hilfinger CS 164 Lecture 3934 Using Solutions Once you’ve solved constraints to extract as much information as possible, look to see if len(s)  alloc(s) is necessarily true. If not, may have a problem. For example, if b is parameter about which we know nothing, then in char s[100]; strcpy (s, b); assertion len(s)  alloc(s) will not simplify to True.

4/30/2008Prof. Hilfinger CS 164 Lecture 3935 Results Found new buffer overruns in sendmail Found new exploitable overruns in Linux nettools package Both widely used, previously hand-audited packages

4/30/2008Prof. Hilfinger CS 164 Lecture 3936 Limitations Tool produces many false positives –1 out of 10 warnings is a real bug Tool has false negatives –Unsound---may miss some overruns But still productive to use

4/30/2008Prof. Hilfinger CS 164 Lecture 3937 Summary Programming language knowledge useful beyond compilers Useful for programmers –Understand what you are doing! Useful for tools other than compilers –Big research direction