Detecting Format String Vulnerabilities with Type Qualifier Umesh Shankar, Kunal Talwar, Jeffrey S. Foster, David Wanger University of California at Berkeley.

Slides:



Advertisements
Similar presentations
Static Analysis for Security
Advertisements

Buffer Overflows Nick Feamster CS 6262 Spring 2009 (credit to Vitaly S. from UT for slides)
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Functions, Varargs, and Stack Smashing Using the Stack for Good And Evil Before You Sit Down Please Get The Handout at the Entrance This file is called.
Fachbereich Informatik SVS – Sicherheit in Verteilten Systemen Universität Hamburg Preventing Buffer Overflows (and more) An overview of scientific approaches.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
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").
Kernighan/Ritchie: Kelley/Pohl:
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
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.
Type Qualifiers: Lightweight Specifications to Improve Software Quality Jeffrey S. Foster.
#1 The Future of Software Security David Wagner U.C. Berkeley.
Visualizing Type Qualifier Inference with Eclipse David Greenfieldboyce Jeffrey S. Foster University of Maryland.
1 Achieving Trusted Systems by Providing Security and Reliability (Research Project #22) Project Members: Ravishankar K. Iyer, Zbigniew Kalbarczyk, Jun.
Format String Protection David Brumley Sam Wu June 12 th, 2002.
In vfprintf(), if (fmt points to “%n”) then **ap = (character count) Achieving Trusted Systems by Providing Security and Reliability FORMAL REASONING ON.
Exploiting Format String Vulnerabilities
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
May 9, 2001OSQ Retreat 1 Run-Time Type Checking for Pointers and Arrays in C Wes Weimer, George Necula Scott McPeak, S.P. Rahul, Raymond To.
CQual: A Tool for Adding Type Qualifiers to C Jeff Foster et al UC Berkeley OSQ Retreat, May
Extending Type Systems in a Library Yuriy Solodkyy Jaakko Järvi Esam Mlaih.
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.
Static Analysis for Security Amir Bazine Per Rehnberg.
Elsa/Oink/Cqual++: Open-Source Static Analysis for C++ Scott McPeak Daniel Wilkerson work with Rob Johnson CodeCon 2006.
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 Security and Penetration Testing
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
Buffer Overflows Lesson 14. Example of poor programming/errors Buffer Overflows result of poor programming practice use of functions such as gets and.
CS390S, Week 4: Format String Vulnerabilities & Integer Overflows Pascal Meunier, Ph.D., M.Sc., CISSP January 31, 2007 Developed thanks to the support.
Formal Reasoning of Security Vulnerabilities by Pointer Taintedness Semantics S. Chen, K. Pattabiraman, Z. Kalbarczyk and R. K. Iyer Center for Reliable.
Preliminaries on Security. Security Summer School, June What is security? Security: prevent bad things from happening – Confidential information.
File IO and command line input CSE 2451 Rong Shi.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Overflow Examples 01/13/2012. ACKNOWLEDGEMENTS These slides where compiled from the Malware and Software Vulnerabilities class taught by Dr Cliff Zou.
Polymorphism, Dynamic Typing, and Dynamic Binding Copyright © 2012 by Yong-Gu Lee
An Undergraduate Course on Software Bug Detection Tools and Techniques Eric Larson Seattle University March 3, 2006.
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
A Tool for Pro-active Defense Against the Buffer Overrun Attack D. Bruschi, E. Rosti, R. Banfi Presented By: Warshavsky Alex.
Overview of C++ Templates
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Buffer overflow and stack smashing attacks Principles of application software security.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Language-Based Security. Outline CQUAL CQUAL CCured CCured Valgrind Valgrind Memcheck, AddrcheckMemcheck, Addrcheck HelgrindHelgrind Applying on PttBBS.
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.
About Exploits Writing ABOUT EXPLOITS WRITING Gerardo Richarte 
CS426Fall 2010/Lecture 141 Computer Security CS 426 Lecture 14 Software Vulnerabilities: Format String and Integer Overflow Vulnerabilities.
Security via Type Qualifiers 2004 SIGPL 여름학교 조 장 우 부산외대 컴퓨터공학부 (based on J. Foster’s lecture at 2004 summer school on software security)
University of Virginia Computer Science Extensible Lightweight Static Checking David Evans On the I/O.
CSC 482/582: Computer Security
Secure Coding Rules for C++ Copyright © 2016 Curt Hill
Content Coverity Static Analysis Use cases of Coverity Examples
Buffer Overflow Buffer overflows are possible because C doesn’t check array boundaries Buffer overflows are dangerous because buffers for user input are.
The Hardware/Software Interface CSE351 Winter 2013
CSE 303 Concepts and Tools for Software Development
Static Analysis With material from Dave Levin, Mike Hicks, Dawson Engler, Lujo Bauer, Michelle Mazurek
Pointers and References
Software Security Lesson Introduction
Introduction to Static Analyzer
Semantic Type Qualifiers
SPL – PS1 Introduction to C++.
Format String Vulnerability
Presentation transcript:

Detecting Format String Vulnerabilities with Type Qualifier Umesh Shankar, Kunal Talwar, Jeffrey S. Foster, David Wanger University of California at Berkeley

Format String Bugs  I/O functions in C use format strings printf(“%s”, buf)print buf as string  But you can also do printf(buf);

Format String Bugs  Attacker may set the content in the buffer buf = printf(buf);  Set buf = “%s%s%s%s%s” to crash program  Set buf = “…%n…” to write to memory may yield exploits to gain root access

Format String Bugs Application Found byImpactyears wu-ftpd 2.*security.isremote root> 6 Linux rpc.statdsecurity.isremote root> 4 IRIX telnetdLSDremote root> 8 Apache + PHP3security.isremote user> 2 NLS / localeCORE SDIlocal root? screenJouko Pynnonenlocal root> 5 BSD chpassTESOlocal root? OpenBSD fstatktwolocal root?

Traditional Techniques  Testing – how to ensure coverage?  Manual code review – bugs too subtle while (fgets(buf, sizeof buf, f)){ lreply(200, buf); } void lreply(int n, char* fmt,…){ vsnprintf(buf, sizeof buf, ftm, ap); }  Re-implement – not possible for legacy system

Using Type Qualifiers  Add qualifier annotations int printf(untainted char *fmt, …); tainted int getchar(); int main(int argc, tainted char *argv[]); tainted = may be controlled by the attacker untainted = must not be controlled by the attacker

The Basic Idea void f(tainted int); untainted int a; tainted int b; f(a); // OK f(b); // OK void g(untainted int) untainted int a; tainted int b; g(a); // OK g(b); // Error

Subtyping void f(tainted int); untainted int a; f(a); void g(untainted int) tainted int b; g(b); f accepts both tainted and untainted data. g only accepts untainted data. untainted < tainted

Type System Type system will prove judgments of the form Γ ├ e :  “In type environment Γ, expression e has type  ”

Type Rules  ├ e 1 :  1   2  ├ e 2 :  1  ├ e 1 e 2 :  2 “In type environment , if expression e 1 has type from  1 to  2 and e 2 has type  1, then application of e 2 to e 1 has type  2 ”

Partial Order A partial order is a relation  that satisfies the following three properties: 1. reflexivity: a  a 2. antisymmetry: If a  b and b  a then a = b 3. transitivity: If a  b, b  c, then a  c

Lattice A lattice is a partial order where any two elements x and y have a least upper bound, x  y, and a greatest lower bound, x  y. a b c d tainted untainted

Qualifier Subtyping Rule Q 1  Q 2 Q 1 int  Q 2 int untainted  tainted untainted int  tainted int int can replaced by any C primitive data type: char, double …. How about pointer?

Pointer Qualifier Subtyping Rule Q 1  Q 2 T 1  T 2 Q 1 ptr(T 1 )  Q 2 ptr(T 2 ) Wrong! tainted char *t; // T2 = tainted char untainted char *u; // T1 = untainted char t = u; // allowed by the wrong rule *t = ; // t is alias of u

Pointer Aliasing  We have multiple names for the same memory location But they have different types And we can write into memory at different types tainted untainted t u

Pointer Qualifier Subtyping Rule  The right rule Q 1  Q 2 T 1 = T 2 Q 1 ptr(T 1 )  Q 2 ptr(T 2 )

Type Qualifier Inference  Recall the format string vulnerabilities We have legacy C program that had no information about qualifiers We add qualifier annotation for the standard library functions Then we check whether there were any contradiction  This requires type qualifier inference

Qualifier Inference  A small number of annotations at key places in the program  Generate a fresh qualifier variable at every position for a type  Analyze and generate sub-typing constraints  Check if the constraints have a valid solution

Qualifier Inference Example tainted char *getenv (const char *name) int printf(untaintd const char *fmt, …) char *s, *t s = getenv(“LD_LIBRARY_PATH”); t = s; printf(t); getenv_ret_p = tainted printf_arg0_p = untainted getenv_ret  s getenv_ret_p  s_p s  t s_p  t_p t  printf_arg0 t  printf_arg0_p tainted = getenv_ret_p  s_p  t_p  printf_arg0_p = untainted tainted  untainted Error!

Type Rules So Far  Primitive types Q 1  Q 2 Q 1 int  Q 2 int  Pointer Q 1  Q 2 T 1 = T 2 Q 1 ptr(T 1 )  Q 2 ptr(T 2 )

Qualifier Inference Extension 1  Leaf Polymorphism char id (char x) { return x } … tainted char t; untainted char u; char a, b; a = id (t); b = id (u); x is tainted, id_ret is tainted b is tainted However id() is just a identity function. It preserves the qualifiers.

Polymorphism  Add in a qualifier variable to id() function  char id (  char x)  Instantiate the  to the correct qualifier for each function call a = id (t) tainted char id (tainted char x) b = id (u) untainted char id (untainted char x)

Polymorphism - Subtyping  Use naming trick to specify subtyping relation for polymorphism $_1_2 char* strcat($_1_2 char*, $_1 const char*) {1}  {1, 2} $_1  $_1_2

Qualifier Inference Extension 2  Explicit Type Casts Type cast should preserve the qualifier void *y; char *x = (char*) y; // if y is tainted, x should be tainted Preserve by collapsing qualifiers char **s, **t; void *v = (void *) s;s_p = s_p_p = v_p t = (char **)v; v_p = t_p = t_p_p If either *s or **s is tainted, then *v is tainted and *t and **t is tainted

Cast Type Qualifier  Allow qualifier cast void *y; char *x = (untainted char*) y;

Qualifier Inference Extension 3  Variable Argument Functions Annotate the varargs specifier … int printf(untainted char*, untainted …); Use naming trick to specify the subtyping relation int sprintf($_1_2 char*, untainted char *, $_2 …);

Qualifier Inference Extension 4  Const f(const char *x); char *unclean, *clean; unclean = getenv(“PATH”); // returns tainted f(unclean); f(clean); unclean is tainted clean is tainted Pointer Rule Q 1  Q 2 T 1 = T 2 Q 1 ptr(T 1 )  Q 2 ptr(T 2 )

Pointer Rule for Const  Const make sure the input data is not modified  If the pointer has a const qualifier, the pointer rule becomes Q 1  Q 2 T 1 < T 2 Q 1 ptr(T 1 )  Q 2 ptr(T 2 )

Evaluation

Conclusion  Qualifier is a generic static analysis technique User-Space/Kernel-Space Trust Errors Deadlock Detection  Has low false positive and negative rates

Demo of Cqual