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

Similar documents
Sungkyunkwan University

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

Machine- Level Programming V: Advanced Topics

Machine-Level Programming V: Advanced Topics

Buffer Overflow. Jo, Heeseung

BUFFER OVERFLOW. Jo, Heeseung

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

Buffer overflows. Specific topics:

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

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

Buffer Overflow. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Giving credit where credit is due

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

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

Computer Systems CEN591(502) Fall 2011

Machine-Level Programming V: Advanced Topics

Machine- Level Programming V: Advanced Topics

Machine-Level Programming V: Advanced Topics

Machine-Level Programming V: Buffer overflow

X86 Assembly Buffer Overflow III:1

CSC 252: Computer Organization Spring 2018: Lecture 9

Machine-Level Prog. V Miscellaneous Topics

CS241 Computer Organization Spring Buffer Overflow

Machine-Level Programming IV: Structured Data

Machine-Level Prog. V Miscellaneous Topics

Machine-level Programs Adv. Topics

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

Machine-Level Programming V: Advanced Topics

Buffer Overflows. CSE 351 Autumn Instructor: Justin Hsia

Buffer Overflows. CSE 351 Autumn 2018

Machine-Level Programming V: Advanced Topics

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

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

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

Machine Programming 5: Buffer Overruns and Stack Exploits

CMPSC 497 Buffer Overflow Vulnerabilities

CS , Fall 2002 Exam 1

Summary. Alexandre David

Machine-Level Programming V: Unions and Memory layout

Buffer-Overflow Attacks on the Stack

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

Memory Organization and Addressing

CS , Fall 2001 Exam 1

Buffer Overflow Attacks

CSE 509: Computer Security

Buffer-Overflow Attacks on the Stack

CS241 Computer Organization Spring Data Alignment

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

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

CS , Fall 2004 Exam 1

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

THEORY OF COMPILATION

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

20: Exploits and Containment

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


Full Name: CISC 360, Fall 2008 Example of Exam

Assembly Language: Function Calls

Buffer Overflow Attack

Assembly Language: Function Calls" Goals of this Lecture"

BUFFER OVERFLOW DEFENSES & COUNTERMEASURES

CSC 405 Computer Security Stack Canaries & ASLR

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Assembly Language: Function Calls" Goals of this Lecture"

15-213/18-243, Fall 2010 Exam 1 - Version A

UW CSE 351, Winter 2013 Midterm Exam

Advanced Buffer Overflow

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

CS 645: Lecture 3 Software Vulnerabilities. Rachel Greenstadt July 3, 2013

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

Lecture 08 Control-flow Hijacking Defenses

Betriebssysteme und Sicherheit Sicherheit. Buffer Overflows

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

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

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

CS 3214 Spring # Problem Points Min Max Average Median SD Grader. 1 Memory Layout and Locality Bill

CPEG421/621 Tutorial

Program Security and Vulnerabilities Class 2

238P: Operating Systems. Lecture 7: Basic Architecture of a Program. Anton Burtsev January, 2018

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

Process Layout, Function Calls, and the Heap

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e Feb 11, 13, 15, and 25. Winter Session 2018, Term 2

Buffer Overflows Defending against arbitrary code insertion and execution

Advanced Buffer Overflow

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux

Hands-on Ethical Hacking: Preventing & Writing Buffer Overflow Exploits

Link 2. Object Files

Secure Programming Lecture 3: Memory Corruption I (Stack Overflows)

CIS 551 / TCOM 401 Computer and Network Security. Spring 2007 Lecture 2

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

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

Lecture 9: Buffer Overflow* CS 392/6813: Computer Security Fall Nitesh Saxena

Link 2. Object Files

Is stack overflow still a problem?

logistics LEX assignment out exam in on week come with questions on Monday (review)

Machine Programming 1: Introduction

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

CSC 591 Systems Attacks and Defenses Return-into-libc & ROP

Ethical Hacking: Preventing & Writing Buffer Overflow Exploits

Transcription:

Introduction to Computer Systems 15 213, fall 2009 9 th Lecture, Sep. 28 th Instructors: Majd Sakr and Khaled Harras

Last Time: Structures struct rec { int i; int a[3]; int *p; }; Memory Layout i a p 0 4 16 20 Alignment struct S1 { char c; int i[2]; double v; } *p; c 3 bits i[0] i[1] 4 bits v p+0 p+4 p+8 p+16 p+24 Unions union U1 { c char c; i[0] i[1] int i[2]; double v; v } *up; up+0 up+4 up+8

Summary Arrays in C Contiguous allocation of memory Aligned to satisfy every element s alignment requirement Pointer to first element No bounds checking Structures Allocate bytes in order declared Pad in middle and at end to satisfy alignment Unions Overlay declarations Way to circumvent type system

Today Memory layout Buffer overflow, worms, and viruses

IA32 Linux Memory Layout Stack Runtime stack (8MB limit) Heap Dynamically allocated storage When call malloc(), calloc(), new() Data Statically allocated data E.g., arrays & strings declared in code Text Executable machine instructions Read only FF not drawn to scale Stack 8MB Upper 2 hex digits = 8 bits of address 08 00 Heap Data Text

Memory Allocation Example 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? FF 08 00 not drawn to scale Stack Heap Data Text

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

x86 64 Example Addresses address range ~2 47 00007F not drawn to scale Stack $rsp p3 p1 p4 p2 &p2 beyond big_array huge_array main() useless() final malloc() 0x7ffffff8d1f8 0x2aaabaadd010 0x2aaaaaadc010 0x000011501120 0x000011501010 0x000010500a60 0x000000500a44 0x000010500a80 0x000000500a50 0x000000400510 0x000000400500 0x00386ae6a170 000030 Heap malloc() is dynamically linked address determined at runtime 000000 Data Text

C operators Operators Associativity () [] ->. left to right! ~ ++ -- + - * & (type) sizeof right 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 -> has very high precedence () has very high precedence monadic * just below

C Pointer Declarations: Test Yourself! int *p int *p[13] int *(p[13]) int **p int (*p)[13] int *f() int (*f)() int (*(*f())[13])() int (*(*x[3])())[5] p is a pointer to int p is an array[13] of pointer to int p is an array[13] of pointer to int p is a pointer to a pointer to an int p is a pointer to an array[13] of int f is a function returning a pointer to int f is a pointer to a function returning int f is a function returning ptr to an array[13] of pointers to functions returning int x is an array[3] of pointers to functions returning pointers to array[5] of ints

C Pointer Declarations (Check out guide) int *p int *p[13] int *(p[13]) int **p int (*p)[13] int *f() int (*f)() int (*(*f())[13])() int (*(*x[3])())[5] p is a pointer to int p is an array[13] of pointer to int p is an array[13] of pointer to int p is a pointer to a pointer to an int p is a pointer to an array[13] of int f is a function returning a pointer to int f is a pointer to a function returning int f is a function returning ptr to an array[13] of pointers to functions returning int x is an array[3] of pointers to functions returning pointers to array[5] of ints

Avoiding Complex Declarations Use typedef to build up the declaration Instead of int (*(*x[3])())[5] : typedef int fiveints[5]; typedef fiveints* p5i; typedef p5i (*f_of_p5is)(); f_of_p5is x[3]; x is an array of 3 elements, each of which is a pointer to a function returning an array of 5 ints

Today Memory layout Buffer overflow, worms, and viruses

Internet Worm and IM War November, 1988 Internet Worm attacks thousands of Internet hosts. How did it happen?

String Library Code Implementation of Unix function 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 Unix functions strcpy: Copies string of arbitrary length scanf, fscanf, sscanf, when given %s conversion specification

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

Buffer Overflow Disassembly 080484f0 <echo>: 80484f0: 55 push %ebp 80484f1: 89 e5 mov %esp,%ebp 80484f3: 53 push %ebx 80484f4: 8d 5d f8 lea 0xfffffff8(%ebp),%ebx 80484f7: 83 ec 14 sub $0x14,%esp 80484fa: 89 1c 24 mov %ebx,(%esp) 80484fd: e8 ae ff ff ff call 80484b0 <gets> 8048502: 89 1c 24 mov %ebx,(%esp) 8048505: e8 8a fe ff ff call 8048394 <puts@plt> 804850a: 83 c4 14 add $0x14,%esp 804850d: 5b pop %ebx 804850e: c9 leave 804850f: c3 ret 80485f2: e8 f9 fe ff ff call 80484f0 <echo> 80485f7: 8b 5d fc mov 0xfffffffc(%ebp),%ebx 80485fa: c9 leave 80485fb: 31 c0 xor %eax,%eax 80485fd: c3 ret

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

Buffer Overflow Stack Example Carnegie Mellon unix> gdb bufdemo (gdb) break echo Breakpoint 1 at 0x8048583 (gdb) run Breakpoint 1, 0x8048583 in echo () (gdb) print /x $ebp $1 = 0xffffc638 (gdb) print /x *(unsigned *)$ebp $2 = 0xffffc658 (gdb) print /x *((unsigned *)$ebp + 1) $3 = 0x80485f7 Before call to gets for main Before call to gets for main 0xffffc658 Return Address Saved %ebp 08 04 85 f7 ff ff c6 58 0xffffc638 [3] [2] [1] [0] for echo buf xx xx xx xx buf for echo 80485f2: call 80484f0 <echo> 80485f7: mov 0xfffffffc(%ebp),%ebx # Return Point

Buffer Overflow Example #1 Before call to gets Input 1234567 for main 0xffffc658 for main 0xffffc658 08 04 85 f7 ff ff c6 58 xx xx xx xx buf for echo 0xffffc638 08 04 85 f7 ff ff c6 58 0xffffc638 00 37 36 35 34 33 32 31 buf for echo Overflow buf, but no problem

Buffer Overflow Example #2 Before call to gets Input 12345678 for main 0xffffc658 for main 0xffffc658 08 04 85 f7 ff ff c6 58 xx xx xx xx buf for echo 0xffffc638 08 04 85 f7 ff ff c6 00 0xffffc638 38 37 36 35 34 33 32 31 buf for echo Base pointer corrupted... 804850a: 83 c4 14 add $0x14,%esp # deallocate space 804850d: 5b pop %ebx # restore %ebx 804850e: c9 leave # movl %ebp, %esp; popl %ebp 804850f: c3 ret # Return

Buffer Overflow Example #3 Before call to gets Input 12345678 for main 0xffffc658 for main 0xffffc658 08 04 85 f7 ff ff c6 58 xx xx xx xx buf for echo 0xffffc638 08 04 85 00 43 42 41 39 0xffffc638 38 37 36 35 34 33 32 31 buf for echo Return address corrupted 80485f2: call 80484f0 <echo> 80485f7: mov 0xfffffffc(%ebp),%ebx # Return Point

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

Exploits Based on Buffer Overflows Buffer overflow bugs allow remote machines to execute arbitrary code on victim 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 attacked fingerd server by sending phony argument: finger exploit-code padding new-returnaddress exploit code: executed a root shell on the victim machine with a direct TCP connection to the attacker.

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

System Level Protections 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 traditional x86, can mark region of memory as either read only or writeable Can execute anything readable Add 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

Worms and Viruses Worm: A program that Can run by itself Can propagate a fully working version of itself to other computers Virus: Code that Add itself to other programs Cannot run independently Both are (usually) designed to spread among computers and to wreak havoc