CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson

Slides:



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

Introduction to C Programming
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Vulnerabilities in Embedded Harvard Architecture Processors Presented By: Michael J. Hohnka Cyber Vulnerabilities Lead Cyber Innovation Division Communications,
Exploring Security Vulnerabilities by Exploiting Buffer Overflow using the MIPS ISA Andrew T. Phillips Jack S. E. Tan Department of Computer Science University.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
I/O: SPARC Assembly Department of Computer Science Georgia State University Georgia State University Updated Spring 2014.
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
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Buffer Overflow Intro. ©2002, Jedidiah R. Crandall, Susan L. Gerhart, Jan G. Hogle. Preventing Buffer Overflows (for C programmers)
Buffer Overflows By Tim Peterson Joel Miller Dan Block.
1 CHAPTER 8 BUFFER OVERFLOW. 2 Introduction One of the more advanced attack techniques is the buffer overflow attack Buffer Overflows occurs when software.
Chapter 15 : Attacking Compiled Applications Alexis Kirat - International Student.
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.
Stack buffer overflow
Buffer Overflow Exploits CS-480b Dick Steflik. What is a buffer overflow? Memory global static heap malloc( ), new Stack non-static local variabled value.
Teaching Buffer Overflow Ken Williams NC A&T State University.
Teaching Buffer Overflow Ken Williams NC A&T State University.
Netprog: Buffer Overflow1 Buffer Overflow Exploits Taken shamelessly from: netprog/overflow.ppt.
CSc 352 C : Arrays, Structs, Pointers
Control hijacking attacks Attacker’s goal: – Take over target machine (e.g. web server) Execute arbitrary code on target by hijacking application control.
Buffer Overflow Attacks. Memory plays a key part in many computer system functions. It’s a critical component to many internal operations. From mother.
Static Analysis for Security Amir Bazine Per Rehnberg.
University of Washington CSE 351 : The Hardware/Software Interface Section 5 Structs as parameters, buffer overflows, and lab 3.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
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
Buffer Overflow Computer Organization II 1 © McQuain Buffer Overflows Many of the following slides are based on those from Complete Powerpoint.
University of Washington Today Happy Monday! HW2 due, how is Lab 3 going? Today we’ll go over:  Address space layout  Input buffers on the stack  Overflowing.
Overflow Examples 01/13/2012. ACKNOWLEDGEMENTS These slides where compiled from the Malware and Software Vulnerabilities class taught by Dr Cliff Zou.
Smashing the Stack Overview The Stack Region Buffer Overflow
Buffer Overflows Many of the following slides are based on those from
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
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.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 9: Designing Exceptionally.
Web Security Firewalls, Buffer overflows and proxy servers.
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.
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Slides by Kent Seamons and Tim van der Horst Last Updated: Nov 11, 2011.
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
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.
1988 Morris Worm … estimated 10% penetration 2001 Code Red … 300,00 computers breached 2003 Slammer/Sapphire … 75,00 infections in 10 min Zotob …
1988 Morris Worm … estimated 10% penetration 2001 Code Red … 300,00 computers breached 2003 Slammer/Sapphire … 75,00 infections in 10 min Zotob …
Chapter Nine Strings. Char vs String Literals Size of data types: Size of data types: –sizeof(“hello\n”)7 bytes –sizeof(“hello”)6 bytes –sizeof(“X”)2.
CS 3214 Computer Systems Godmar Back Lecture 7. Announcements Stay tuned for Project 2 & Exercise 4 Project 1 due Sep 16 Auto-fail rule 1: –Need at least.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Software Security. Bugs Most software has bugs Some bugs cause security vulnerabilities Incorrect processing of security related data Incorrect processing.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Lec. Waleed Bin Shahid.  You might have noticed a lot of issues related to software implementation.  The ultimate requirement of developer(s) is to.
CSC 482/582: Computer Security
Buffer Overflow Buffer overflows are possible because C doesn’t check array boundaries Buffer overflows are dangerous because buffers for user input are.
CNT4704: Computer Networking Special Topic: Buffer Overflow I: Attack Introduction Cliff Zou Fall 2009.
CSC 253 Lecture 8.
Ken D. Nguyen Department of Computer Science Georgia State University
CS 465 Buffer Overflow Slides by Kent Seamons and Tim van der Horst
CSC 253 Lecture 8.
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
Buffer Overflows.
Ken D. Nguyen Department of Computer Science Georgia State University
Understanding and Preventing Buffer Overflow Attacks in Unix
FIGURE Illustration of Stack Buffer Overflow
Format String Vulnerability
Presentation transcript:

CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson

Bugs have consequences 2

An example 3

4

What happened The program made an assumption but did nothing to enforce it This led to a buffer overflow that corrupted memory 5 don’t trust external input!

Buffer overflows C does not have array bounds checking – it is possible to write past the end of an array – this has the effect of overwriting other memory locations – depending on what gets corrupted, the program may: show no effects compute different results crash let someone else take control of your program (and possibly your computer) 6

Buffer overflows 7

Some examples of buffer overflows 8

How buffer overflows are exploited The attacker provides an input string that is too long for an array (buffer) it is written to – the buffer overflows – contents of neighboring memory locations are overwritten By careful selection of the input string, the attacker can control exactly what the neighboring locations are overwritten with This can allow the attacker to force the victim’s computer to execute arbitrary code 9

Example: “Stack Smashing” 10 direction of stack growth return address caller’s stack frame input buffer int foo( … ) { char buf[32]; … scanf(“%s”, buf); … return; }  compromised!

Example: “Stack Smashing” 11 Compromised! direction of stack growth caller’s stack frame input buffer int foo( … ) { char buf[32]; … scanf(“%s”, buf); … return; }  return address jump to code injected by the attacker

Controlling amount of input read [f]scanf lets you specify a maximum field width before the conversion specifier – max. field width: optional decimal integer – if specified: reading of characters stops when this width is reached – for strings: field width does not include terminating ‘\0’ 12

Controlling the amount read 13

Guarding scanf isn’t enough 14 scanf is being guarded and yet memory gets corrupted!

Guarding scanf isn’t enough Any function that writes an unbounded amount into a memory region is potentially a problem Common culprits: – string library routines: strcpy, strcat – input routines: scanf, gets – output routines: sprintf 15 unguarded  guarded strcpystrncpy strcatstrncat getsfgets sprintfsnprintf

Example: strcpy vs. strncpy 16

It’s easy to get careless 17 str0[ ], name[ ] are the same size: 4 bytes we’ve made sure we copied at most 4 bytes into str0 therefore it’s OK to use just strcpy() to copy str0 into v0.name right?

It’s easy to get careless 18 input output ?!?

What happened? 19

What happened? 20 strncpy didn’t NULL-terminate the string, and this caused the string in str0[ ] to be much longer than intended

Why is this a problem? Any time a buffer can overflow with user-supplied data, the program has a huge security hole. Example: variation on the previous problem int n0, n1, n2; char buf1[4], buf2[64]; … scanf(“%d %d %d %64s”, n0, n1, n2, buf2); strncpy(buf1, buf2, 4); 21 n0, n1, n2, buf1 may be in contiguous memory (compiler dependent) non-NULL-terminated strncpy() can create a user- controlled string that is longer than intended this can set the stage for a buffer-overflow exploit

The moral of the story Careless handling of user-supplied data can get you into trouble – read man pages carefully – learn which input/string routines don’t guarantee NULL- terminated strings Make sure buffers are NULL-terminated – allocate space for the terminating NULL character – declare buffers as char buf[MAXSIZE + 1]; 22

Is that enough? 23 allocating space for terminating NULL initializing the array checking how many characters to copy using “safe” string functions guarding scanf buffer eating our vegetables

Is that enough? 24   

What happened? We forgot to check the input! Signature for strncpy: char *strncpy(char *dest, char *src, size_t n) – size_t is an unsigned type – a negative number, treated as an unsigned value, looks like a very big number e.g., on a 32-bit machine, -1 (signed) = 0xffffffff = (unsigned) – strncpy() copies over a very large no. of bytes this corrupts memory and causes the seg fault 25