Lecture Slides Tutorials Buffoverflow
Lecture Slides Tutorials Buffoverflow
Buffer Overflow
Buffer overflows are possible because C doesn’t check array
boundaries
Buffer overflows are dangerous because buffers for user
input are often stored on the stack
Probably the most common type of security vulnerability
Buffer Overflow
University of Washington
Heap
Data
Upper 2 hex digits Text
08
= 8 bits of address 00
Buffer Overflow
University of Washington
char big_array[1<<24]; /* 16 MB */
char huge_array[1<<28]; /* 256 MB */
int beyond;
char *p1, *p2, *p3, *p4;
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 ... */ Heap
} Data
Text
Where does everything go? 08
00
Buffer Overflow
University of Washington
$esp 0xffffbcd0
p3 0x65586008
p1 0x55585008
p4 0x1904a110
p2 0x1904a008
&p2 0x18049760
beyond 0x08049744
big_array 0x18049780 80
huge_array 0x08049760
main() 0x080483c6
Heap
useless() 0x08049744
final malloc() 0x006be166
Data
malloc() is dynamically linked Text
address determined at runtime 08
00
Buffer Overflow
University of Washington
Internet Worm
These characteristics of the traditional IA32 Linux memory
layout provide opportunities for malicious programs
Stack grows “backwards” in memory
Data and instructions both stored in the same memory
November, 1988
Internet Worm attacks thousands of Internet hosts.
How did it happen?
Buffer Overflow
University of Washington
Buffer Overflow
University of Washington
int main()
{
printf("Type a string:");
echo(); unix>./bufdemo
return 0; Type a string:1234567
} 1234567
unix>./bufdemo
Type a string:12345678
Segmentation Fault
unix>./bufdemo
Type a string:123456789ABC
Segmentation Fault
Buffer Overflow
University of Washington
echo:
pushl %ebp # Save %ebp on stack
movl %esp, %ebp
buf pushl %ebx # Save %ebx
leal -8(%ebp),%ebx # Compute buf as %ebp-8
subl $20, %esp # Allocate stack space
movl %ebx, (%esp) # Push buf addr on stack
call gets # Call gets
. . .
Buffer Overflow
University of Washington
Return Address f7 85 04 08
Saved %ebp 58 c6 ff ff 0xffffc638
Saved %ebx Saved %ebx
[3] [2] [1] [0] buf xx xx xx xx buf
buf 0xffffc630
Buffer Overflow
University of Washington
f7 85 04 08 f7 85 04 08
58 c6 ff ff 0xffffc638 58 c6 ff ff 0xffffc638
Saved %ebx 00 37 36 35
xx xx xx xx buf 34 33 32 31 buf
0xffffc630 0xffffc630
Buffer Overflow
University of Washington
f7 85 04 08 f7 85 04 08
58 c6 ff ff 0xffffc638 58 c6 ff 00 0xffffc638
Saved %ebx 38 37 36 35
xx xx xx xx buf 34 33 32 31 buf
0xffffc630 0xffffc630
f7 85 04 08 f7 85 04 00
58 c6 ff ff 0xffffc638 43 42 41 39 0xffffc638
Saved %ebx 38 37 36 35
xx xx xx xx buf 34 33 32 31 buf
0xffffc630 0xffffc630
Return address corrupted
080485f2: call 80484f0 <echo>
080485f7: mov 0xfffffffc(%ebp),%ebx # Return Point
Buffer Overflow
University of Washington
void foo(){
foo stack frame
bar();
... return address A
} B (was A)
Buffer Overflow
University of Washington
Buffer Overflow
University of Washington