1 Understanding Pointers Buffer Overflow. 2 Outline Understanding Pointers Buffer Overflow Suggested reading –Chap 3.10, 3.12.

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
David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz.
Review: Software Security David Brumley Carnegie Mellon University.
Today C operators and their precedence Memory layout
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Machine-Level Programming V: Miscellaneous Topics Topics Linux Memory Layout Buffer Overflow Floating Point Code CS213.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Andrea Bittau, Adam Belay, Ali Mashtizadeh, David Maziéres, Dan Boneh
September 22, 2014 Pengju (Jimmy) Jin Section E
Fabián E. Bustamante, Spring 2007 Machine-Level Prog. V – Miscellaneous Topics Today Buffer overflow Floating point code Next time Memory.
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-
Machine Programming – IA32 memory layout and buffer overflow CENG331: Introduction to Computer Systems 7 th Lecture Instructor: Erol Sahin Acknowledgement:
Carnegie Mellon 1 This week Buffer Overflow  Vulnerability  Protection.
6.828: PC hardware and x86 Frans Kaashoek
Y86 Processor State Program Registers
Introduction CS 104: Applied C++ What is Programming? For some given problem: __________ a solution for it -- identify, organize & store the problem's.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
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 Memory layout Buffer overflow, worms, and viruses 1.
1 Machine-Level Programming V: Advanced Topics Andrew Case Slides adapted from Jinyang Li, Randy Bryant & Dave O’Hallaron.
Ithaca College 1 Machine-Level Programming IX Memory & buffer overflow Comp 21000: Introduction to Computer Systems & Assembly Lang Systems book chapter.
Mitigation of Buffer Overflow Attacks
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.
Smashing the Stack Overview The Stack Region Buffer Overflow
Buffer Overflows Many of the following slides are based on those from
Overflows & Exploits. In the beginning 11/02/1988 Robert Morris, Jr., a graduate student in Computer Science at Cornell, wrote an experimental, self-replicating,
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)
CNIT 127: Exploit Development Ch 1: Before you begin.
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming V: Buffer overflow Slides.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
CS642: Computer Security X86 Review Process Layout, ISA, etc. Drew Davidson
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.
Slides by Kent Seamons and Tim van der Horst Last Updated: Nov 11, 2011.
Machine-Level Programming Advanced Topics Topics Linux Memory Layout Buffer Overflow.
1 Assembly Language: Function Calls Jennifer Rexford.
Machine-Level Programming V: Miscellaneous Topics
Buffer Overflow Attacks 1 Basic Idea Sample Attacks Protection , Computer & Network Security.
Analyzing C/C++ Vulnerabilities -- Mike Gerschefske.
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.
Machine-Level Programming V: Miscellaneous Topics Topics Linux Memory Layout Understanding Pointers Buffer Overflow Floating-Point Code CS 105 Tour of.
Machine-Level Programming Advanced Topics Topics Linux Memory Layout Understanding Pointers Buffer Overflow.
Machine-Level Programming Advanced Topics Topics Buffer Overflow.
Mitigation against Buffer Overflow Attacks
Buffer Overflow Buffer overflows are possible because C doesn’t check array boundaries Buffer overflows are dangerous because buffers for user input are.
Machine-Level Programming V: Buffer overflow
CS 177 Computer Security Lecture 9
Virtualization Virtualize hardware resources through abstraction CPU
C function call conventions and the stack
The Hardware/Software Interface CSE351 Winter 2013
Introduction to Information Security
Machine Language V: Miscellaneous Topics Sept. 25, 2001
Machine-Level Programming V: Miscellaneous Topics
Y86 Processor State Program Registers
Machine-Level Programming 4 Procedures
CS 465 Buffer Overflow Slides by Kent Seamons and Tim van der Horst
Defeating Instruction Set Randomization Nora Sovarel
Assembly Language Programming II: C Compiler Calling Sequences
Machine Level Representation of Programs (IV)
Machine-Level Programming V: Miscellaneous Topics
Machine Level Representation of Programs (IV)
Machine-Level Programming V: Miscellaneous Topics October 2, 2008
Heterogeneous Data Structures & Alignment * heterogeneous: 不同种类的
Instructors: Majd Sakr and Khaled Harras
“Way easier than when we were students”
Machine-Level Programming V: Miscellaneous Topics
Presentation transcript:

1 Understanding Pointers Buffer Overflow

2 Outline Understanding Pointers Buffer Overflow Suggested reading –Chap 3.10, 3.12

3 Pointers Every pointer has a type –If the object has type T A pointer to this object has type T * –Special void * type Represents a generic pointer –malloc returns a generic pointer Every pointer has a value

4 Pointers Pointers are created with the & operator –Applied to lvalue expression Lvalue expression can appear on the left side of assignment Pointers are dereferenced with the operator * –The result is a value having the type associated with the pointer Arrays and pointers are closed related –The name of array can be viewed as a pointer constant – ip[0] is equivalent to *ip

5 Pointer Arithmetic Addition and subtraction –p+i, p-i (result is a pointer) –p-q (result is a int) Referencing & dereferencing –*p, &E Subscription –A[i], *(A+i)

6 Pointers can point to functions void (*f)(int *) f is a pointer to function The function taken int * as argument The return type of the function is void Assignment makes f point to func – f = func Notice the precedence of the operators –void *f(int *) declares f is a function (void *) f(int *)

7 Pointer Declaration char **argv ; int (*daytab)[13] int (*comp)() char (*(*x())[])() –Function returning pointer to array[ ] of pointer to function returning char char(*(*x[3])())[5] –Array[3] of pointer to function returning pointer to array[5] of char

8 C operators OperatorsAssociativity () [] -> left to right ! ~ * & (type) sizeofright to left * / %left to right + -left to right >left to right >=left to right == !=left to right &left to right ^left to right |left to right &&left to right ||left to right ?:right to left = += -= *= /= %= &= ^= != >=right to left,left to right Note: Unary +, -, and * have higher precedence than binary forms

9 Parameter Passing Call by value – f(xp) Call by reference – f(&xp)

10 Out-of-Bounds Memory References 1 /* Implementation of library function gets() */ 2 char *gets(char *s) 3 { 4 int c; 5 char *dest = s; 6int got_char = 0 ; /Has at least one character been read? */ 7 while ((c = getchar()) != ’\n’ && c != EOF) { 8 *dest++ = c; /* No bounds checking */ 9gotchar = 1; 10}

11 Out-of-Bounds Memory References 11*dest++ = ’\0’; /* Terminate String */ 12if (c == EOF && !gotchar) 13 return NULL; /* End of file or error */ 14 return s; 15 } 16 Type ctrl-d at keyboard means EOF

12 Out-of-Bounds Memory References 14 /* Read input line and write it back */ 15 void echo() 16 { 17 char buf[8]; /* Way too small ! */ 18 gets(buf); 19 puts(buf); 20 }

13 Out-of-Bounds Memory References 1 echo: 2 pushl %ebp Save %ebp on stack 3 movl %esp, %ebp 4 pushl %ebx Save %ebx 5 subl $20, %esp Allocate 20 bytes on stack 6 leal -12(%ebp), %ebx Compute buf as %ebp-12 7 movl %ebx, (%esp) Store buf at top of stack 8 call gets Call gets 9 movl %ebx, (%esp) Store buf at top of stack 10 call puts Call puts 11 addl $20, %esp Deallocate stack space 12 popl %ebx Restore %ebx 13 popl %ebp Restore %ebp 14 ret Return

14 Out-of-Bounds Memory References Return address Saved %ebp Saved %ebx [7][6][5][4] [3][2][1][0] %ebp buf Stack frame for caller Stack frame for echo

15 Out-of-Bounds Memory References Return address Saved %ebp [11][10][9][8] [7][6][5][4] [3][2][1][0] %ebp buf Stack frame for caller Stack frame for echo

16 Out-of-Bounds Memory References Return address [15]1[4][13][12] [11][10][9][8] [7][6][5][4] [3][2][1][0] %ebp buf Stack frame for caller Stack frame for echo

17 Out-of-Bounds Memory References [19][18][17][16] [15]1[4][13][12] [11][10][9][8] [7][6][5][4] [3][2][1][0] %ebp buf Stack frame for caller Stack frame for echo

18 Out-of-Bounds Memory References 1 /* This is very low-quality code. 2 It is intended to illustrate bad programming practices. 3 See Problem */ 4 char *getline() 5 { 6 char buf[8]; 7 char *result; 8 gets(buf); 9 result = malloc(strlen(buf)); 10 strcpy(result, buf); 11 return result; 12 }

19 Out-of-Bounds Memory References c0 : c0: 55 push %ebp c1: 89 e5 mov %esp,%ebp c3: 83 ec 28 sub $0x28,%esp c6: 89 5d f4 mov %ebx,-0xc(%ebp) c9: f8 mov %esi,-0x8(%ebp) cc: 89 7d fc mov %edi,-0x4(%ebp) Diagram stack at this point cf: 8d 75 ec lea -0x14(%ebp),%esi d2: mov %esi,(%esp) d5: e8 a3 ff ff ff call d Modify diagram to show stack contents at this point

20 Out-of-Bounds Memory References 2 push %ebp 3 mov %esp,%ebp 4 sub $0x28,%esp 5 mov %ebx,-0xc(%ebp) 6 mov %esi,-0x8(%ebp) 7 mov %edi,-0x4(%ebp) Diagram stack at this point 8 lea -0x14(%ebp),%esi 9 mov %esi,(%esp) 10call d Return address bf ff fc 94%ebp 0x01%ebx 0x02%edi 0x03%esi

21 Out-of-Bounds Memory References 2 push %ebp 3 mov %esp,%ebp 4 sub $0x28,%esp 5 mov %ebx,-0xc(%ebp) 6 mov %esi,-0x8(%ebp) 7 mov %edi,-0x4(%ebp) Diagram stack at this point 8 lea -0x14(%ebp),%esi 9 mov %esi,(%esp) 10call d bf ff fc Return address bf ff fc 94%ebp 0x01%ebx 0x02%edi 0x03%esi Saved %ebp Saved %edi Saved %esi Saved %ebx %ebp

22 Out-of-Bounds Memory References 2 push %ebp 3 mov %esp,%ebp 4 sub $0x28,%esp 5 mov %ebx,-0xc(%ebp) 6 mov %esi,-0x8(%ebp) 7 mov %edi,-0x4(%ebp) 8 lea -0x14(%ebp),%esi 9 mov %esi,(%esp) 10call d Modify diagram to show stack contents at this point Return address bf ff fc 94%ebp 0x01%ebx 0x02%edi 0x03%esi “ ” Saved %ebp Saved %edi Saved %esi Saved %ebx %ebp

23 Malicious Use of Buffer Overflow void bar() { char buf[64]; gets(buf);... } void foo(){ bar();... } return address A Stack after call to gets() B foo stack frame bar stack frame B exploit code pad data written by gets()

24 Malicious Use of Buffer Overflow Input string contains byte representation of executable code Overwrite return address with address of buffer When bar() executes ret, will jump to exploit code

25 The Famous Internet Worm of November 1988 To gain access to many of the computers across the Internet –4 different ways –One was a buffer overflow attack on the fingerd Hundreds of machines were effectively paralyzed The author of the worm was caught and prosecuted. He was sentenced to –3 years probation –400 hours of community service –and a $10,500 fine

26 The Famous Internet Worm of November 1988 Steps –invoked finger with an appropriate string –Made a process at a remote site have a buffer overflow – executed code that gave the worm access to the remote system –The worm replicated itself and consumed virtually all of the machine’s computing resources

27 Stack Randomization Making a vulnerability to have a stack overflow –Try the right string on your own computer –The string contains The exploit code and The address of this code –Put the string to the remote computer Stack randomization makes it hard to determine the address of the exploit code

28 Stack Randomization 1 int main() { 2 int local; 3 printf("local at %p\n", &local); 4 return 0; 5 } Running the code 10,000 times on a Linux (maybe ) machine in 32-bit mode the addresses ranged from –0xff7fa7e0 to 0xffffd7e0 –A range of around 2 23

29 Stack Randomization Running in 64-bit mode on the newer machine The addresses ranged from –0x7fff to 0x7ffffff98664 –A range of nearly 2 32 Address-space layout randomization (ASLR) –each time a program is run –different parts of the program are loaded into different regions of memory code, data, heap data, library code, stack

30 Stack Randomization Nop sled –a program “slides” through a long sequence of “nop” Nop –no operation instruction Include a “nop sled” before the actual exploit code –If insert 256-byte nop sled –Need to guess 2 15 starting addresses (no too much) for 32-bit machine –Still have too many 2 24 guesses

31 Stack Corruption Detection Return address Saved %ebp Saved %ebx Canary [7][6][5][4] [3][2][1][0] %ebp buf Stack frame for caller Stack frame for echo

32 Stack Corruption Detection 1 echo: 2 pushl %ebp 3 movl %esp, %ebp 4 pushl %ebx 5 subl $20, %esp 6 movl %gs:20, %eax Retrieve canary 7 movl %eax, -8(%ebp) Store on stack 8 xorl %eax, %eax Zero out register 9 leal -16(%ebp), %ebx Compute buf as %ebp movl %ebx, (%esp) Store buf at top of stack 11 call gets Call gets 12 movl %ebx, (%esp) Store buf at top of stack 13 call puts Call puts

33 Stack Corruption Detection 14 movl -8(%ebp), %eax Retrieve canary 15 xorl %gs:20, %eax Compare to stored value 16 je.L19 If =, goto ok 17 call __stack_chk_fail Stack corrupted! 18.L19: ok: 19 addl $20, %esp Normal return popl %ebx 21 popl %ebp 22 ret %gs:20 –Segmented addressing which appeared in and seldom used today –It is marked as read only

34 Limiting Executable Code Regions Page –4k bytes –As a protected unit by OS –Should be marked as “readable”, “writable” and “executable” 3 bits are required Originally Intel merged the “readable” and “executable” into one –The exploit code in the stack can be executed AMD introduced “NX” in X86-64 –Now there 3 bits –How about “JIT”?

Code Reuse Attack Return-oriented Programming –Find code gadgets in existed code base (e.g. libc) –Push address of gadgets on the stack –Leverage ‘ret’ to connect code gadgets –No code injection Solutions –Return-less kernels –Heuristic means New Attacks: Jump-oriented –Use gadget as dispatcher return addr saved ebp Address A Address B Address C A B C

Motivation: Code Reuse Attack