06 Buffoverflow
06 Buffoverflow
Buffer overflows
Heap
Data
Upper 2 hex digits Text
08
= 8 bits of address 00
Autumn 2013 Buffer Overflow 2
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
Autumn 2013 Buffer Overflow 4
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
its address is determined at runtime. 08
00
Autumn 2013 Buffer Overflow 5
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?
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?
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
Autumn 2013 Buffer Overflow 11
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
. . .
Autumn 2013 Buffer Overflow 13
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
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
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> Hmmm, what can you do with it?
080485f7: mov 0xfffffffc(%ebp),%ebx # Return Point
void foo(){
foo stack frame
bar();
... return address A
} B (was A)