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.

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
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
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)
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
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.
SPLINT STATIC CHECKING TOOL Sripriya Subramanian 10/29/2002.
CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Computer Security: Principles and Practice EECS710: Information Security Professor Hossein Saiedian Fall 2014 Chapter 10: Buffer Overflow.
Detecting Format String Vulnerabilities with Type Qualifier Umesh Shankar, Kunal Talwar, Jeffrey S. Foster, David Wanger University of California at Berkeley.
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.
Chapter 10.
Stack buffer overflow
Buffer Overflow By: John Quach and Napoleon N. Valdez.
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.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Teaching Buffer Overflow Ken Williams NC A&T State University.
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.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
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.
Natalia Yastrebova What is Coverity? Each developer should answer to some very simple, yet difficult to answer questions: How do I find new.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Computer Security and Penetration Testing
BLENDED ATTACKS EXPLOITS, VULNERABILITIES AND BUFFER-OVERFLOW TECHNIQUES IN COMPUTER VIRUSES By: Eric Chien and Peter Szor Presented by: Jesus Morales.
Buffer Overflows Lesson 14. Example of poor programming/errors Buffer Overflows result of poor programming practice use of functions such as gets and.
S ECURE P ROGRAMMING 6. B UFFER O VERFLOW (S TRINGS AND I NTEGERS ) P ART 1 Chih Hung Wang Reference: 1. B. Chess and J. West, Secure Programming with.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
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)
Overflow Examples 01/13/2012. ACKNOWLEDGEMENTS These slides where compiled from the Malware and Software Vulnerabilities class taught by Dr Cliff Zou.
Buffer Overflow. Introduction On many C implementations, it is possible to corrupt the execution stack by writing past the end of an array. Known as smash.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
1 IS 2150 / TEL 2810 Introduction to Security James Joshi Associate Professor, SIS Lecture 12.2 Nov 20, 2012 Integer Issues.
Buffer Overflow Group 7Group 8 Nathaniel CrowellDerek Edwards Punna ChalasaniAxel Abellard Steven Studniarz.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
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.
Buffer overflow and stack smashing attacks Principles of application software security.
Sairajiv Burugapalli. This chapter covers three main categories of classic software vulnerability: Buffer overflows Integer vulnerabilities Format string.
COP 3275 – Character Strings Instructor: Diego Rivera-Gutierrez.
Security Attacks Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Today’s Material Strings Definition Representation Initialization
1 2 2 Call The Project Dynamic-Memory 4 4 # include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo)
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
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.
1988 Morris Worm … estimated 10% penetration 2001 Code Red … 300,00 computers breached 2003 Slammer/Sapphire … 75,00 infections in 10 min Zotob …
C Strings Doing strings the old fashioned way. strings vs c-strings C++ strings are an object data type – State : list of characters – Can ask it to perform.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
CSC 482/582: Computer Security
Content Coverity Static Analysis Use cases of Coverity Examples
Buffer Overflow By Collin Donaldson.
Sabrina Wilkes-Morris CSCE 548 Student Presentation
Strings CSCI 112: Programming in C.
Software Security Lesson Introduction
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
CSC 495/583 Topics of Software Security Format String Bug (2) & Heap
Introduction to Static Analyzer
Buffer Overflows.
Testing & Security Dr. X.
Presentation transcript:

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 security issues (Morris 1988)‏ Many buffer overflow problems are related to string manipulations In the year 2000, 50% of CERT warnings were related to buffer overflow. Any language like C and C++ that does not enforce memory safety and type safety is a potential risk to buffer overflow.

Example void trouble () { int a = 32; char line[128]; gets(line); }

Buffer Over Attack line a return address line a return address line a return address

Buffer Allocation Strategies Static –Use a fixed size allocation. Alter the program behavior if the data does not fit or truncate the data. –Buffer overflow mistakes can be checked by automated tools or humans. Dynaminc –Resize the buffer as needed –More difficult to check for code problems. Mixing static and dynamic allocation can cause problems.

Static Allocation Example #define BUFSIZE1024 #define SUCCESS0 int main(int argc, char **argv) { char str[BUFSIZE]; int len; len = snprintf(str, BUFSIZE, “%S(%d)”, argv[0], argc); printf(“%s\n”, str); if (len >= BUFSIZE) { printf(“length truncated (from %d)\n”, len); } return SUCCESS; }

Dynamic Allocation Example #define BUFSIZE1024 #define SUCCESS0 int main(int argc, char **argv) { char *str; int len; if ((str = (char *)malloc(BUFSIZE)) == NULL) { return -1; } len = snprintf(str, BUFSIZE, “%S(%d)”, argv[0], argc); if (len >= BUFSIZE) { free(str); if ((str = (char *)malloc(len + 1)) == NULL) { return -1; } snprintf(str, len+1, “%S(%d)”, argv[0], argc); } printf(“%s\n”, str); free(str); str = NULL; return SUCCESS; }

Dynamic Allocation Issues –More difficult to manage. –Can introduce memory leaks –Use-after-free –Double free Solutions –Enforce Null-After-Free –Tracking buffer sizes

Tracking Buffer Sizes C and C++ do not track buffer sizes –Programmers have to do it on their own Some languages track buffer sizes. Errors in tracking buffer sizes could lead to buffer overflow conditions

Unsafe String functions gets()‏ scanf()‏ strcpy()‏ sprintf()‏

gets() and friends gets() reads input stream into a buffer until a new line is found. Very dangerous function and it should be avoided. To use gets() you must be 100% sure you trust the input. C++ >> operator repeated the same mistake as gets().

scanf() and friends %s specifier in the format string can cause a buffer overflow condition. scanf() can be used safely if the format specifier properly bounds the amount of data to be read.

strcpy() and friends strcpy() copies one buffer to another until a null character is found. If source buffer is larger than destination or source is not null terminated buffer overflow condition may occur.

sprintf() and friends To avoid buffer overflow the destination buffer must be large enough to accommodate the combination of all source arguments. %s can be a variable string length and can cause buffer overflow. Bounded format string can make sprintf() safer to use.

Risk of reimplementation Programmers should be careful not to duplicate the same mistakes made in the dangerous standard C functions. Implementation of similar functions can be harder to detect.

Example of reimplementaion void get_word(char *word) { int c; while (isspace(c = getchar())) {} while (c = getchar()) { if (c == -1) { break; } if (isspace(c)) { *word = ‘\0’; break; } *word = c; word++; }

strncpy() and strncat() pitfalls The size incorrectly specified –char s1[S1_SIZE], s2[S2_SIZE] –strncpy (s1, s2, S2_SIZE)‏ –strncat (s1, s2, S1_SIZE)‏ strncpy does not always null terminate the destination. strncat destination and source must be terminated.

Truncation Errors A truncation after copying a buffer can cause unpredictable problems. Some examples –Check access control on a file then copy the file to another smaller buffer. The new buffer points to another file. –Check the validity of a host name then copy to a smaller buffer. New buffer now contains a new hostname.

Maintaining null terminator Many libc functions rely on the null char at the end of a string. –Examples: strlen, strcpy, strncpy, strncat Some functions that are designed to operate on memory blocks may not null terminate a string –Examples: fread(), recvmsg(), strncpy()‏ Programmers must ensure strings are null terminated to avoid runtime errors and buffer overflows. Catching errors due to failure of null terminating a string is very difficult and may not be possible until the program is running in production. One way to ensure strings are terminated is to explicitly add a null char at the end of a buffer.

Character Sets Today several character sets exist and are used for input and output. ISO , UTF-8, UTF-16 and UTF-32 are some of the most commonly used. ISO and UTF-32 are fixed-width encoding. UTF-16 and UTF-8 are variable-width encoding. New type wchar_t was introduced to handle other character sets.

Characters and buffer overflow Mismatch between the string length and the number of bytes in the buffer can cause buffer overflow issues. Operation that expects bounds in bytes is passed bound in characters or vice-versa Functions that convert from one encoding to another magnify the problem.

Format Strings When a variable string is passed as a format string to a function that can cause a format string vulnerability. while (fgets(buf, sizeof buf, f)) { lreply(200, buf)‏... } void lreply(int n, char *fmt,... ) { char buf[BUFSIZ];... vsnprintf(buf, sizeof buf, fmt, ap);... }

Format Strings Most format string vulnerabilities are caused by misuse of functions that require a format string –Example: printf(str) instead of printf (“%s”, str)‏ Read data from the stack by passing formatting characters Use %n directive to write arbitrary positions in memory.

Preventing Format String exploits Always pass a static format string If static string is too restrictive choose from a list of format strings If a format string must be read from input perform input validation.

Better String Classes and Libraries std::string class in STL Microsoft CString in ATL/MFC Vstr library –Designed to work with readv() and writev()‏ SafeStr library