Machine- Level Programming V: Advanced Topics

Similar documents
Sungkyunkwan University

Introduction to Computer Systems , fall th Lecture, Sep. 28 th

Machine-Level Programming V: Advanced Topics

Computer Systems CEN591(502) Fall 2011

Buffer overflows. Specific topics:

Linux Memory Layout. Lecture 6B Machine-Level Programming V: Miscellaneous Topics. Linux Memory Allocation. Text & Stack Example. Topics.

BUFFER OVERFLOW. Jo, Heeseung

Buffer Overflow. Jo, Heeseung

Buffer Overflow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Machine- Level Programming V: Advanced Topics

Buffer Overflow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Buffer Overflow. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Buffer Overflows. Buffer Overflow. Many of the following slides are based on those from

Machine-Level Programming V: Buffer overflow

Buffer overflows (a security interlude) Address space layout the stack discipline + C's lack of bounds-checking HUGE PROBLEM

CSC 252: Computer Organization Spring 2018: Lecture 9

Giving credit where credit is due

Linux Memory Layout The course that gives CMU its Zip! Machine-Level Programming IV: Miscellaneous Topics Sept. 24, Text & Stack Example

Machine-Level Programming V: Miscellaneous Topics Sept. 24, 2002

Machine-Level Programming V: Advanced Topics

Machine-Level Programming V: Advanced Topics

Machine-Level Prog. V Miscellaneous Topics

Today. Machine-Level Programming V: Advanced Topics. x86-64 Linux Memory Layout. Memory Allocation Example. Today. x86-64 Example Addresses

Summary. Alexandre David

Today. Machine-Level Programming V: Advanced Topics. x86-64 Linux Memory Layout. Memory Allocation Example. Today. x86-64 Example Addresses

Buffer Overflows. CSE 351 Autumn 2018

Machine-Level Programming IV: Structured Data

Buffer Overflows. CSE 410 Winter Kathryn Chan, Kevin Bi, Ryan Wong, Waylon Huang, Xinyu Sui

Buffer Overflows. CSE 351 Autumn Instructor: Justin Hsia

Machine-level Programs Adv. Topics

Machine-Level Programming V: Advanced Topics

Machine-Level Prog. V Miscellaneous Topics

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Machine-Level Programming V: Advanced Topics

X86 Assembly Buffer Overflow III:1

CS241 Computer Organization Spring Buffer Overflow

Machine Programming 5: Buffer Overruns and Stack Exploits

Memory Organization and Addressing

Machine-Level Programming V: Unions and Memory layout

CS , Fall 2002 Exam 1

Lecture 08 Control-flow Hijacking Defenses

CS , Fall 2001 Exam 1

CMPSC 497 Buffer Overflow Vulnerabilities

CS241 Computer Organization Spring Data Alignment

Buffer-Overflow Attacks on the Stack

CSC 405 Computer Security Stack Canaries & ASLR

Process Layout, Function Calls, and the Heap

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction


BUFFER OVERFLOW DEFENSES & COUNTERMEASURES

Buffer-Overflow Attacks on the Stack

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Buffer. This time. Security. overflows. Software. By investigating. We will begin. our 1st section: History. Memory layouts

Buffer Overflow Attack

Instruction Set Architecture

Instruction Set Architecture

Process Layout and Function Calls

Betriebssysteme und Sicherheit Sicherheit. Buffer Overflows

Foundations of Computer Systems

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

Introduction to Computer Systems. Exam 1. February 22, This is an open-book exam. Notes are permitted, but not computers.

Machine Programming 1: Introduction

This time. Defenses and other memory safety vulnerabilities. Everything you ve always wanted to know about gdb but were too afraid to ask

CS 161 Computer Security. Week of January 22, 2018: GDB and x86 assembly

Assembly Language: Function Calls

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Assembly Language: Function Calls" Goals of this Lecture"

Prac*ce problem on webpage

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

CS , Fall 2004 Exam 1

Ethical Hacking: Preventing & Writing Buffer Overflow Exploits

Introduction to Computer Systems. Exam 1. February 22, Model Solution fp

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Assembly Language: Function Calls" Goals of this Lecture"

Assembly I: Basic Operations. Jo, Heeseung

Chapter 4! Processor Architecture!

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

CISC 360 Instruction Set Architecture

Why do we need Pointers? Call by Value vs. Call by Reference in detail Implementing Arrays Buffer Overflow / The Stack Hack

Instruction Set Architecture

Second Part of the Course

2 Sadeghi, Davi TU Darmstadt 2012 Secure, Trusted, and Trustworthy Computing Chapter 6: Runtime Attacks

Link 2. Object Files

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

Machine-Level Programming II: Control and Arithmetic

CPEG421/621 Tutorial

Architecture-level Security Vulnerabilities

cmovxx ra, rb 2 fn ra rb irmovl V, rb rb V rmmovl ra, D(rB) 4 0 ra rb D mrmovl D(rB), ra 5 0 ra rb D OPl ra, rb 6 fn ra rb jxx Dest 7 fn Dest

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Instruction Set Architecture

System Programming and Computer Architecture (Fall 2009)

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

Machine/Assembler Language Putting It All Together

The first Secure Programming Laboratory will be today! 3pm-6pm in Forrest Hill labs 1.B31, 1.B32.

Buffer Overflows Defending against arbitrary code insertion and execution

Advanced Buffer Overflow

Transcription:

Machine- Level Programming V: Advanced Topics Andrew Case Slides adapted from Jinyang Li, Randy Bryant & Dave O Hallaron 1

Today Structures and Unions Memory Layout Buffer Overflow Vulnerability ProtecEon 2

Structure AllocaCon struct rec { int a[3]; int i; struct rec *n; }; Memory Layout a i n 0 12 16 20 Struct members laid out concguously in memory Offset of each struct member determined at compile Eme Members may be of different types 3

Structure Access struct rec { int a[3]; int i; struct rec *n; }; r r+12 a i n 0 12 16 20 Accessing Structure Member Pointer indicates first byte of structure Access elements with offsets void set_i(struct rec *r, int val) { r->i = val; } IA32 Assembly # %edx = val, %eax = r movl %edx, 12(%eax) # Mem[r+12] = val 4

Following Linked List void set_all_values (struct rec *r, int val) { while (r) { int i = r->i; r->a[i] = val; r = r->n; } } struct rec { int a[3]; int i; struct rec *n; }; # %edx = r.l17: # loop: movl 12(%edx), %eax # r->i movl %ecx, (%edx,%eax,4) # r->a[i] = val movl 16(%edx), %edx # r = r->n testl %edx, %edx # Test r jne.l17 # If!= 0 goto loop a i n 0 12 16 20 Element i 5

Structures & Alignment Unaligned Data c i[0] i[1] v p p+1 p+5 p+9 p+17 Aligned Data Important for memory management (paging/etc.) Done by the compiler For a primieve data type of K bytes, address is muleple of K Can be inefficient usage of space c 3 bytes i[0] i[1] 4 bytes v struct S1 { char c; int i[2]; double v; } *p; p+0 p+4 p+8 p+16 p+24 MulCple of 4 MulCple of 8 6

SaCsfying Alignment with Structures Alignment requirement: 1. Must align each element of a struct 2. IniEal address & structure length must be muleples of the biggest alignment of a struct s elements Biggest alignment of elements: 8 struct S1 { char c; int i[2]; double v; } *p; c 3 bytes i[0] i[1] 4 bytes v p+0 p+4 p+8 p+16 p+24 MulCple of 4 MulCple of 8 MulCple of 8 MulCple of 8 7

Saving Space Define a struct to put large data types first struct S4 { char c; int i; char d; } *p; struct S5 { int i; char c; char d; } *p; c 3 bytes i d 3 bytes i c d 2 bytes Note: May not be worth opcmizing 8

Union AllocaCon Unions can store different kinds of data in one memory allocacon (but only one type at a Cme) Allocated according to largest element struct S1 { char c; int i[2]; double v; } *sp; c 3 bytes i[0] i[1] 4 bytes v sp+0 sp+4 sp+8 sp+16 sp+24 union U1 { char c; int i[2]; double v; } *up; c i[0] i[1] v up+0 up+4 up+8 9

Byte Ordering Example union { unsigned char c[8]; unsigned short s[4]; unsigned int i[2]; unsigned long l[1]; } dw; 32- bit c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] s[0] s[1] s[2] s[3] i[0] i[1] l[0] 64- bit c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] s[0] s[1] s[2] s[3] i[0] i[1] l[0] 10

Today Structures and Unions Memory Layout Buffer Overflow Vulnerability ProtecEon 11

IA32 Linux Memory Layout Stack RunEme stack (8MB limit) E. g., local variables Heap Dynamically allocated storage Used when calling malloc(), calloc(), new() Data StaEcally allocated data E.g., Global variables Text Executable machine instruceons Read- only 0xFF****** 0x08****** 0x00****** Stack Heap Data Text 8MB 12

Memory AllocaCon Example FF Stack char big_array[1<<24]; /* 16 MB */ char huge_array[1<<28]; /* 256 MB */ int beyond; char *p1, *p2, *p3, *p4; int useless() { return 0; } int main() { p1 = malloc(1 <<28); /* 256 MB */ p2 = malloc(1 << 8); /* 256 B */ p3 = malloc(1 <<28); /* 256 MB */ p4 = malloc(1 << 8); /* 256 B */ /* Some print statements... */ } Where does everything go? 08 00 Heap Data Text 13

IA32 Example Addresses address range ~2 32 FF Stack $esp 0xffffbcd0 p3 0x65586008 p1 0x55585008 p4 0x1904a110 p2 0x1904a008 &p2 0x18049760 &beyond 0x08049744 big_array 0x18049780 huge_array 0x08049760 main() 0x080483c6 useless() 0x08049744 final malloc() 0x006be166 malloc() is dynamically linked address determined at runcme 80 08 00 Heap Data Text 14

x86-64 Example Addresses address range ~2 47 00007F not drawn to scale Stack $rsp 0x00007ffffff8d1f8 p3 0x00002aaabaadd010 p1 0x00002aaaaaadc010 p4 0x0000000011501120 p2 0x0000000011501010 &p2 0x0000000010500a60 &beyond 0x0000000000500a44 big_array 0x0000000010500a80 huge_array 0x0000000000500a50 main() 0x0000000000400510 useless() 0x0000000000400500 final malloc() 0x000000386ae6a170 malloc() is dynamically linked address determined at runcme 000030 000000 Heap Data Text 15

Today Structures and Unions Memory Layout Buffer Overflow Vulnerability ProtecEon 16

Internet Worm November, 1988 Internet Worm a_acks thousands of Internet hosts. How did it happen? 17

String Library Code ImplementaCon of Unix funccon gets() /* Get string from stdin */ char *gets(char *dest) { int c = getchar(); char *p = dest; while (c!= EOF && c!= '\n') { *p++ = c; c = getchar(); } *p = '\0'; return dest; } No way to specify limit on number of characters to read Similar problems with other library funccons strcpy, strcat: Copy strings of arbitrary length scanf, fscanf, sscanf, when given %s conversion specificaeon 18

Vulnerable Buffer Code /* Echo Line */ void echo() { char buf[4]; /* Way too small! */ gets(buf); puts(buf); } void call_echo() { echo(); } unix>./bufdemo Type a string:1234567 1234567 unix>./bufdemo Type a string:12345678 Segmentation Fault unix>./bufdemo Type a string:123456789abc Segmentation Fault 19

Buffer Overflow Disassembly echo: 80485c5: 55 push %ebp 80485c6: 89 e5 mov %esp,%ebp 80485c8: 53 push %ebx 80485c9: 83 ec 14 sub $20,%esp 80485cc: 8d 5d f8 lea -8(%ebp),%ebx 80485cf: 89 1c 24 mov %ebx,(%esp) 80485d2: e8 9e ff ff ff call 8048575 <gets> 80485d7: 89 1c 24 mov %ebx,(%esp) 80485da: e8 05 fe ff ff call 80483e4 <puts@plt> 80485df: 83 c4 14 add $20,%esp 80485e2: 5b pop %ebx 80485e3: 5d pop %ebp 80485e4: c3 ret call_echo: 80485eb: e8 d5 ff ff ff call 80485c5 <echo> 80485f0: c9 leave 80485f1: c3 ret 20

Buffer Overflow Stack Before call to gets for main Return Address Saved %ebp %ebp Saved %ebx [3] [2] [1] [0] buf for echo /* Echo Line */ void echo() { char buf[4]; /* Way too small! */ gets(buf); puts(buf); } echo: pushl %ebp # Save %ebp on stack movl %esp, %ebp pushl %ebx # Save %ebx subl $20, %esp # Allocate stack space leal -8(%ebp),%ebx # Compute buf as %ebp-8 movl %ebx, (%esp) # Push buf on stack call gets # Call gets... 21

Buffer Overflow Stack Example unix> gdb bufdemo (gdb) break echo Breakpoint 1 at 0x80485c9 (gdb) run Breakpoint 1, 0x80485c9 in echo () (gdb) print /x $ebp $1 = 0xffffd678 (gdb) print /x *(unsigned *)$ebp $2 = 0xffffd688 (gdb) print /x *((unsigned *)$ebp + 1) $3 = 0x80485f0 Before call to gets for main Before call to gets for main 0xffffd688 Return Address Saved %ebp Saved %ebx [3] [2] [1] [0] for echo buf 08 04 85 f0 ff ff d6 88 0xffffd678 Saved %ebx xx xx xx xx buf for echo 80485eb: call 80485f0: leave 80485c5 <echo> 22

Buffer Overflow Example #1 Before call to gets Input 1234567\0 for main 0xffffd688 for main 0xffffd688 08 04 85 f0 ff ff d6 88 0xffffd678 Saved %ebx xx xx xx xx buf for echo 08 04 85 f0 ff ff d6 88 0xffffd678 00 37 36 35 34 33 32 31 buf for echo Overflow buf, and corrupt %ebx 23

Buffer Overflow Example #2 Before call to gets Input 12345678\0 for main 0xffffd688 for main 0xffffd688 08 04 85 f0 ff ff d6 88 0xffffd678 Saved %ebx xx xx xx xx buf for echo 08 04 85 f0 ff ff d6 00 0xffffd678 38 37 36 35 34 33 32 31 buf for echo echo:... call 8048575 <gets> leave ret # Reset %ebp to corrupted value 24

Buffer Overflow Example #3 Before call to gets for main 0xffffd688 Input 123456789ABC\0 for main 0xffffd688 08 04 85 f0 ff ff d6 88 0xffffd678 Saved %ebx xx xx xx xx buf for echo 08 04 85 00 43 42 41 39 0xffffd678 38 37 36 35 34 33 32 31 buf for echo echo:... call 8048575 <gets> leave ret # Reset %ebp to corrupted value 25

Malicious Use of Buffer Overflow Stack aber call to gets() void foo(){ bar();... } return address A B foo stack frame int bar() { char buf[64]; gets(buf);... return...; } data wri_en by gets() B pad exploit code bar stack frame Input string contains byte representacon of executable code Overwrite return address A with address of buffer B When bar() executes ret, will jump to exploit code 26

Exploits Based on Buffer Overflows Buffer overflow bugs allow remote machines to execute arbitrary code on viclm machines Internet worm Early versions of the finger server (fingerd) used gets() to read the argument sent by the client: finger droh@cs.cmu.edu Worm a_acked fingerd server by sending phony argument: finger exploit-code padding new-returnaddress exploit code: executed a root shell on the vicem machine with a direct TCP conneceon to the a_acker. 27

Code Red Exploit Code Exploited bug in Microsoc IIS web server Spread self Generate random IP addresses & send a_ack string Infected >300,000 hosts Adack www.whitehouse.gov Send 98,304 packets; sleep for 4-1/2 hours; repeat Denial of service a_ack Deface server s home page Aber waieng 2 hours 28

Avoiding Overflow Vulnerability /* Echo Line */ void echo() { char buf[4]; /* Way too small! */ fgets(buf, 4, stdin); puts(buf); } Use library roucnes that limit string lengths fgets instead of gets strncpy instead of strcpy Don t use scanf with %s conversion specificaeon Use fgets to read the string Or use %ns where n is a suitable integer 29

System- Level ProtecCons Randomized stack offsets At start of program, allocate random amount of space on stack Makes it difficult for hacker to predict beginning of inserted code Nonexecutable code segments In tradieonal x86, can mark region of memory as either read- only or writeable Can execute anything readable X86-64 added explicit execute permission unix> gdb bufdemo (gdb) break echo (gdb) run (gdb) print /x $ebp $1 = 0xffffc638 (gdb) run (gdb) print /x $ebp $2 = 0xffffbb08 (gdb) run (gdb) print /x $ebp $3 = 0xffffc6a8 30

Compile- Level ProtecCon: Stack Canaries Idea Place special value ( canary ) on stack just beyond buffer Check for corrupeon before exieng funceon GCC ImplementaCon -fstack-protector -fstack-protector-all unix>./bufdemo-protected Type a string:1234 1234 unix>./bufdemo-protected Type a string:12345 *** stack smashing detected *** 31

Protected Buffer Disassembly echo: 804864d: 55 push %ebp 804864e: 89 e5 mov %esp,%ebp 8048650: 53 push %ebx 8048651: 83 ec 14 sub $20,%esp 8048654: 65 a1 14 00 00 00 mov %gs:0x14,%eax 804865a: 89 45 f8 mov %eax,0xfffffff8(%ebp) 804865d: 31 c0 xor %eax,%eax 804865f: 8d 5d f4 lea 0xfffffff4(%ebp),%ebx 8048662: 89 1c 24 mov %ebx,(%esp) 8048665: e8 77 ff ff ff call 80485e1 <gets> 804866a: 89 1c 24 mov %ebx,(%esp) 804866d: e8 ca fd ff ff call 804843c <puts@plt> 8048672: 8b 45 f8 mov -8(%ebp),%eax 8048675: 65 33 05 14 00 00 00 xor %gs:0x14,%eax 804867c: 74 05 je 8048683 <echo+0x36> 804867e: e8 a9 fd ff ff call 804842c <FAIL> 8048683: 83 c4 14 add $20,%esp 8048686: 5b pop %ebx 8048687: 5d pop %ebp 8048688: c3 ret 32

Sehng Up Canary Before call to gets for main Return Address Saved %ebp %ebp Saved %ebx Canary [3] [2] [1] [0] buf /* Echo Line */ void echo() { char buf[4]; /* Way too small! */ gets(buf); puts(buf); } for echo echo:... movl %gs:20, %eax # Get canary movl %eax, -8(%ebp) # Put on stack xorl %eax, %eax # Erase canary... 33

Checking Canary Before call to gets for main Return Address Saved %ebp %ebp Saved %ebx Canary [3] [2] [1] [0] buf /* Echo Line */ void echo() { char buf[4]; /* Way too small! */ gets(buf); puts(buf); } for echo echo:... movl -8(%ebp), %eax # Retrieve from stack xorl %gs:20, %eax # Compare with Canary je.l24 # Same: skip ahead call stack_chk_fail # ERROR.L24:... 34

Canary Example Before call to gets for main Input 1234 for main Return Address Saved %ebp %ebp Saved %ebx 03 e3 7d 00 [3] [2] [1] [0] buf for echo Return Address Saved %ebp Saved %ebx 03 e3 7d 00 34 33 32 31 for echo buf %ebp (gdb) break echo (gdb) run (gdb) stepi 3 (gdb) print /x *((unsigned *) $ebp - 2) $1 = 0x3e37d00 Benign corrupcon! (allows programmers to make silent off- by- one errors) 35