Carnegie Mellon
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Machine-Level Programming V: Advanced Topics CS140 - Assembly - - PowerPoint PPT Presentation
Carnegie Mellon Machine-Level Programming V: Advanced Topics CS140 - Assembly Language and Computer Organization March 29, 2016 Slides courtesy of: Randal E. Bryant and David R. OHallaron 1 Bryant and OHallaron, Computer Systems: A
Carnegie Mellon
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon
2 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Memory Layout Buffer Overflow
Unions
Carnegie Mellon
3 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Stack
Heap
Data
Text / Shared Libraries
Carnegie Mellon
4 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon
5 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon
6 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Memory Layout Buffer Overflow
Unions
7 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon
typedef struct { int a[2]; double d; } struct_t; double fun(int i) { volatile struct_t s; s.d = 3.14; s.a[i] = 1073741824; /* Possibly out of bounds */ return s.d; }
8 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon
typedef struct { int a[2]; double d; } struct_t;
Carnegie Mellon
9 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Generally called a “buffer overflow”
Why a big deal?
Most common form
Carnegie Mellon
10 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Implementation of Unix function gets()
Similar problems with other library functions
Carnegie Mellon
11 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
void call_echo() { echo(); } /* Echo Line */ void echo() { char buf[4]; /* Way too small! */ gets(buf); puts(buf); } unix>./bufdemo-nsp Type a string:012345678901234567890123 012345678901234567890123 unix>./bufdemo-nsp Type a string:0123456789012345678901234 Segmentation Fault
Carnegie Mellon
12 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon
13 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
echo: subq $24, %rsp movq %rsp, %rdi call gets . . . /* Echo Line */ void echo() { char buf[4]; /* Way too small! */ gets(buf); puts(buf); }
Carnegie Mellon
14 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
echo: subq $24, %rsp movq %rsp, %rdi call gets . . . void echo() { char buf[4]; gets(buf); . . . }
Carnegie Mellon
15 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
echo: subq $24, %rsp movq %rsp, %rdi call gets . . . void echo() { char buf[4]; gets(buf); . . . }
unix>./bufdemo-nsp Type a string:01234567890123456789012 01234567890123456789012
Carnegie Mellon
16 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
echo: subq $24, %rsp movq %rsp, %rdi call gets . . . void echo() { char buf[4]; gets(buf); . . . }
unix>./bufdemo-nsp Type a string:0123456789012345678901234 Segmentation Fault
Carnegie Mellon
17 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
echo: subq $24, %rsp movq %rsp, %rdi call gets . . . void echo() { char buf[4]; gets(buf); . . . }
unix>./bufdemo-nsp Type a string:012345678901234567890123 012345678901234567890123
Carnegie Mellon
18 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon
19 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon
20 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Buffer overflow bugs can allow remote machines to execute
Distressingly common in real progams
Examples across the decades
You will learn some of the tricks in attacklab
Carnegie Mellon
21 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Exploited a few vulnerabilities to spread
Once on a machine, scanned for other machines to attack
Carnegie Mellon
22 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
July, 1999
Carnegie Mellon
23 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
August 1999
Carnegie Mellon
24 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Date: Wed, 11 Aug 1999 11:30:57 -0700 (PDT) From: Phil Bucking <philbucking@yahoo.com> Subject: AOL exploiting buffer overrun bug in their own software! To: rms@pharlap.com
I am writing you because I have discovered something that I think you might find interesting because you are an Internet security expert with experience in this area. I have also tried to contact AOL but received no response. I am a developer who has been working on a revolutionary new instant messaging client that should be released later this year. ... It appears that the AIM client has a buffer overrun bug. By itself this might not be the end of the world, as MS surely has had its share. But AOL is now *exploiting their own buffer overrun bug* to help in its efforts to block MS Instant Messenger. .... Since you have significant credibility with the press I hope that you can use this information to help inform people that behind AOL's friendly exterior they are nefariously compromising peoples' security. Sincerely, Phil Bucking Founder, Bucking Consulting philbucking@yahoo.com
Carnegie Mellon
25 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Worm: A program that
Virus: Code that
Both are (usually) designed to spread among computers
Carnegie Mellon
26 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Avoid overflow vulnerabilities Employ system-level protections Have compiler use “stack canaries” Lets talk about each…
Carnegie Mellon
27 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
For example, use library routines that limit string lengths
Carnegie Mellon
28 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Randomized stack offsets
Carnegie Mellon
29 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Nonexecutable code
Carnegie Mellon
30 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Idea
GCC Implementation
unix>./bufdemo-sp Type a string:0123456 0123456 unix>./bufdemo-sp Type a string:01234567 *** stack smashing detected ***
Carnegie Mellon
31 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon
32 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
echo: . . . movq %fs:40, %rax # Get canary movq %rax, 8(%rsp) # Place on stack xorl %eax, %eax # Erase canary . . . /* Echo Line */ void echo() { char buf[4]; /* Way too small! */ gets(buf); puts(buf); }
Carnegie Mellon
33 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
echo: . . . movq 8(%rsp), %rax # Retrieve from stack xorq %fs:40, %rax # Compare to canary je .L6 # If same, OK call __stack_chk_fail # FAIL L6 /* Echo Line */ void echo() { char buf[4]; /* Way too small! */ gets(buf); puts(buf); }
Carnegie Mellon
34 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Challenge (for hackers)
Alternative Strategy
Construct program from gadgets
Carnegie Mellon
35 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Use tail end of existing functions
long ab_plus_c (long a, long b, long c) { return a*b + c; } 00000000004004d0 <ab_plus_c>: 4004d0: 48 0f af fe imul %rsi,%rdi 4004d4: 48 8d 04 17 lea (%rdi,%rdx,1),%rax 4004d8: c3 retq
Carnegie Mellon
36 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Repurpose byte codes
void setval(unsigned *p) { *p = 3347663060u; } <setval>: 4004d9: c7 07 d4 48 89 c7 movl $0xc78948d4,(%rdi) 4004df: c3 retq
Carnegie Mellon
37 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Trigger with ret instruction
Final ret in each gadget will start next one
c3 Gadget 1 code c3 Gadget 2 code c3 Gadget n code
Stack
%rsp
Carnegie Mellon
38 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Memory Layout Buffer Overflow
Unions
Carnegie Mellon
39 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Allocate according to largest element Can only use one field at a time
3 bytes
4 bytes
Carnegie Mellon
40 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon
41 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Idea
Big Endian
Little Endian
Bi Endian
Carnegie Mellon
42 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon
43 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon
44 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
LSB MSB LSB MSB Print
Carnegie Mellon
45 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
MSB LSB MSB LSB Print
Carnegie Mellon
46 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
LSB MSB Print
Carnegie Mellon
47 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Arrays
Structures
Unions