Software attacks int ConcatString(char *buf1, char *buf2, size_t len1, size_t len2) { char buf[256]; if((len1 + len2) > 256) return -1; memcpy(buf, buf1,

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

Module 4: Statements and Exceptions. Overview Introduction to Statements Using Selection Statements Using Iteration Statements Using Jump Statements Handling.
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Things to Remember When Developing 64-bit Software OOO "Program Verification Systems"
Week 3. Assembly Language Programming  Difficult when starting assembly programming  Have to work at low level  Use processor instructions >Requires.
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 2: Data types and addressing modes dr.ir. A.C. Verschueren.
CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Chapter 15 : Attacking Compiled Applications Alexis Kirat - International Student.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
CS2422 Assembly Language & System Programming October 3, 2006.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Windows XP SP2 Stack Protection Jimmy Hermansson Johan Tibell.
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Session 1 CS-240 Data Structures Binghamton University Dick Steflik.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Netprog: Buffer Overflow1 Buffer Overflow Exploits Taken shamelessly from: netprog/overflow.ppt.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
0wning Antivirus Alex Wheeler Neel Mehta
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
1 CSC241: Object Oriented Programming Lecture No 27.
Carnegie Mellon 1 This Week: Integers Integers  Representation: unsigned and signed  Conversion, casting  Expanding, truncating  Addition, negation,
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Natalia Yastrebova What is Coverity? Each developer should answer to some very simple, yet difficult to answer questions: How do I find new.
COMP 116: Introduction to Scientific Programming Lecture 28: Data types.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Stack and Heap Memory Stack resident variables include:
Automatic Diagnosis and Response to Memory Corruption Vulnerabilities Presenter: Jianyong Dai Jun Xu, Peng Ning, Chongkyung Kil, Yan Zhai, Chris Bookhot.
Overflow Examples 01/13/2012. ACKNOWLEDGEMENTS These slides where compiled from the Malware and Software Vulnerabilities class taught by Dr Cliff Zou.
CNIT 127: Exploit Development Ch 3: Shellcode. Topics Protection rings Syscalls Shellcode nasm Assembler ld GNU Linker objdump to see contents of object.
CSCE 548 Integer Overflows Format String Problem.
1 IS 2150 / TEL 2810 Introduction to Security James Joshi Associate Professor, SIS Lecture 12.2 Nov 20, 2012 Integer Issues.
UFS003C3 Lecture 15 Data type in C & C++ Using the STL.
Digital Logic Lecture 3 Binary Arithmetic By Zyad Dwekat The Hashemite University Computer Engineering Department.
Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem.
CNIT 127: Exploit Development Ch 8: Windows Overflows Part 2.
מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’. In.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
S ECURE P ROGRAMMING 6. B UFFER O VERFLOW (S TRINGS AND I NTEGERS ) P ART 2 Chih Hung Wang Reference: 1. B. Chess and J. West, Secure Programming with.
Sairajiv Burugapalli. This chapter covers three main categories of classic software vulnerability: Buffer overflows Integer vulnerabilities Format string.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
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.
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)
CS426Fall 2010/Lecture 141 Computer Security CS 426 Lecture 14 Software Vulnerabilities: Format String and Integer Overflow Vulnerabilities.
Chapter 7 Continued Arrays & Strings. Arrays of Structures Arrays can contain structures as well as simple data types. Let’s look at an example of this,
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Overflows Mark Shtern.
Introduction to Information Security
Module 30 (Unix/Linux Security Issues II)
This Week: Integers Integers Summary
Instructor: David Ferry
Chapter 3 Bit Operations
CMSC 414 Computer and Network Security Lecture 21
Introduction to Abstract Data Types
Representing Information (2)
Secure Coding in C and C++ Integer Security
Representing Information (2)
Java Programming Language
Operations and Arithmetic
Presentation transcript:

Software attacks int ConcatString(char *buf1, char *buf2, size_t len1, size_t len2) { char buf[256]; if((len1 + len2) > 256) return -1; memcpy(buf, buf1, len1); memcpy(buf + len1, buf2, len2); return 0; } Do you remember this function? I said it wasn’t safe. Why? The answer will lead us to today topic… …and may give you a free coffee!

Software attacks Integer overflow

Software attacks The problem The previous function is not safe, because integer types have a finite precision. So the previous function could have an integer overflow, which in turn (in this example) can lead to a buffer overflow (size_t)0xFFFFFFFF + (size_t)0x2 = 0x1 (size_t) (size_t)2 = 1 or, if you prefer,

Software attacks Integer overflow Integer overflows occur when a larger integer is needed to accurately represent the results of an arithmetic operation. Let's look at a simple example using an unsigned char (8-bit) integer type: unsigned char a = 255; unsigned char b = 2; unsigned char result = a + b; When compiled and run, we get the following binary representation: (a) (b) (result)

Software attacks Integer overflow Ashcraft & Engler [IEEE S&P 2002]: “Many programmers appear to view integers as having arbitrary precision, rather than being fixed-sized quantities operated on with modulo arithmetic.” bool DoSomething(const char* server) { unsigned char namelen = strlen(server) + 2; if(namelen < 255) { char* UncName = malloc(namelen); if(UncName != 0) sprintf(UncName, "\\\\%s", server); //do more things here } }

Software attacks The solution Solving integer overflow problems is not easy: they are very subtle int ConcatString(char *buf1, char *buf2, size_t len1, size_t len2) { char buf[256]; if((len1 < len2) && (len2 < 256 – len1)) return -1; memcpy(buf, buf1, len1); memcpy(buf + len1, buf2, len2); return 0; }

Software attacks The solution For multiplication, in mathematical terms, the general problem is: A * B > MAX_INT => Error! In order to keep your test from depending on an overflow, we can rewrite it as follows: A > MAX_INT/B => Error! For addition, the unsigned case is relatively easy: A + B > MAX_INT Error! In order to keep your test from depending on an overflow, we can rewrite it as follows: A > MAX_INT – B Error!

Software attacks The solution if(!((rhs ^ lhs) < 0)) //test for +/- combo { //either two negatives, or 2 positives if(rhs < 0) { //two negatives if(lhs < MinInt() - rhs) //remember rhs < 0 throw ERROR_ARITHMETIC_OVERFLOW; } else { //two positives if(MaxInt() - lhs < rhs) throw ERROR_ARITHMETIC_OVERFLOW; } } return lhs + rhs; //else overflow not possible Testing a signed addition operation is considerably more difficult. We actually have four cases—both numbers are positive, both numbers are negative, and two mixed-sign cases:

Software attacks Integer overflow in new[] This operator performs an implicit multiplication that is unchecked: int* allocate_integers(int howmany) { return new int[howmany]; } If you study the code generation for this, it comes out: mov eax, [esp+4] ; eax = howmany shl eax, 2 ; eax = howmany * sizeof(int) push eax call operator new ; allocate that many bytes pop ecx retd 4 The multiplication by sizeof(int) is not checked for overflow!!

Software attacks Integer overflow in new[] Let's look at a slightly longer example: class MyClass { public: MyClass(); // constructor int stuff[256]; }; MyClass* allocate_myclass(int howmany) { return new MyClass[howmany]; } This class also contains a constructor, so allocating an array of them involves two steps: 1.allocate the memory, 2.then construct each object.

Software attacks Integer overflow in new[] unchecked multiplication tries to allocate that many bytes tells the vector constructor iterator to call the ctor (MyClass::MyClass) that many times. mov eax, [esp+4] ; howmany shl eax, 10 ; howmany * sizeof(MyClass) push esi push eax call operator new ; allocate that many bytes... push OFFSET MyClass::MyClass push [esp+12] ; howmany push 1024 ; sizeof(MyClass) push esi ; memory block call `vector constructor iterator` mov eax, esi jmp loop fail: xor eax, eax done: pop esi retd 4 If somebody calls allocate_myclass(0x200001), the multiplication overflows and only 1024 bytes are allocated. This allocation succeeds, and then the vector ctor tries to initialize 0x items => You walk off the end of the memory block and start a memory overflow

Software attacks Integer overflow in new[] You can now use this template to allocate arrays in an overflow-safe manner. template T* NewArray(size_t n) { if (n <= (size_t)-1 / sizeof(T)) return new T[n]; // n is too large return NULL; } Solution

Software attacks Integer overflow in new[] But how could you get tricked into an overflow situation? The most common way of doing this is by reading the value out of a file or some other storage location. For example, if your code is parsing a file that has a section whose format is "length followed by data“ See GIF, JPG, BMP vulnerabilities

Software attacks Integer overflow Integer overflows are becoming a new security attack vector. A solution for C++ is using templates: see “Integer Handling with the C++ SafeInt Class” - David LeBlanc The article discusses some of the ways you can protect yourself against integer overflow attacks.