3.5 Buffer Overflow, Integer and Heap Overflow
3.5 Buffer Overflow, Integer and Heap Overflow
Zhiqiang Lin
Outline
1 Background
2 Buffer Overflow
4 Summary
Outline
1 Background
2 Buffer Overflow
4 Summary
Background Buffer Overflow Integer and Heap Overflow Summary
IA32 Stack
Stack “Top”
Background Buffer Overflow Integer and Heap Overflow Summary
Pushing Stack“Bottom”
pushl src
Fetch operand at Src
Decrement %esp by 4
Write operand at
address given by %esp
Stack
Pointer
%esp -4
Stack “Top”
Background Buffer Overflow Integer and Heap Overflow Summary
Popping Stack“Bottom”
popl dest
Read operand at
address given by %esp
Increment %esp by 4
Write to Dest
Stack
Pointer
%esp
+4
Stack “Top”
Background Buffer Overflow Integer and Heap Overflow Summary
0x110 0x110
0x10c 0x10c
0x108 123 0x108 123
0x104
call 8048b90
0x110 0x110
0x10c 0x10c
0x108 123 0x108 123
0x104
call 8048b90
0x110 0x110
0x10c 0x10c
0x108 123 0x108 123
0x104
call 8048b90
0x110 0x110
0x10c 0x10c
0x108 123 0x108 123
0x104 0x8048553
call 8048b90
0x110 0x110
0x10c 0x10c
0x108 123 0x108 123
0x104 0x8048553
8048591: c3 ret
ret
0x110 0x110
0x10c 0x10c
0x108 123 0x108 123
0x104 0x8048553 0x8048553
Code Structure
Call Chain
yoo(…)
{
• yoo
•
who(); who(…)
{ who
•
• • • •
} amI(); amI(…) amI amI
• • • {
amI(); •
• amI
• • •
} amI();
•
• amI
Procedure amI recursive }
Background Buffer Overflow Integer and Heap Overflow Summary
Contents
Local variables
Return information yoo
Management amI
Space allocated when enter procedure
“Set-up” code
Deallocated when return
“Finish” code
Pointers
Stack pointer %esp indicates stack top
Frame pointer %ebp indicates start of
current frame
Background Buffer Overflow Integer and Heap Overflow Summary
Stack Operation
•
•
Frame
•
Pointer
Call Chain %ebp
yoo(…) yoo
{ Stack
• yoo Pointer
• %esp
who();
•
•
}
Background Buffer Overflow Integer and Heap Overflow Summary
Stack Operation
•
•
•
Call Chain
who(…) yoo
{ Frame
• • • yoo Pointer
amI(); %ebp
who
• • •
who Stack
amI();
Pointer
• • •
%esp
}
Background Buffer Overflow Integer and Heap Overflow Summary
Stack Operation
•
•
•
Call Chain
amI(…) yoo
{
• yoo
• who
amI(); Frame
who
• Pointer
• %ebp
} amI amI
Stack
Pointer
%esp
Background Buffer Overflow Integer and Heap Overflow Summary
Stack Operation
•
•
•
Call Chain
amI(…) yoo
{
• yoo
• who
amI();
who
•
•
amI amI
} Frame
Pointer
amI %ebp
amI
Stack
Pointer
%esp
Background Buffer Overflow Integer and Heap Overflow Summary
Stack Operation
•
•
•
Call Chain
amI(…) yoo
{
• yoo
• who
amI(); who
•
•
} amI amI
amI
amI
Frame
amI Pointer
%ebp
amI
Stack
Pointer
%esp
Background Buffer Overflow Integer and Heap Overflow Summary
Stack Operation
•
•
•
Call Chain
amI(…) yoo
{
• yoo
• who
amI();
who
•
•
amI amI
} Frame
Pointer
amI %ebp
amI
Stack
amI Pointer
%esp
Background Buffer Overflow Integer and Heap Overflow Summary
Stack Operation
•
•
•
Call Chain
amI(…) yoo
{
• yoo
• who
amI(); Frame
who
• Pointer
• %ebp
} amI amI
Stack
Pointer
amI %esp
amI
Background Buffer Overflow Integer and Heap Overflow Summary
Stack Operation
•
•
•
Call Chain
who(…) yoo
{ Frame
• • • yoo Pointer
amI(); %ebp
who
• • •
who Stack
amI();
Pointer
• • •
%esp
} amI
amI
amI
Background Buffer Overflow Integer and Heap Overflow Summary
Stack Operation
•
•
•
Call Chain
amI(…) yoo
{
• yoo
• who
• Frame
who
• Pointer
} %ebp
amI amI amI
Stack
Pointer
amI %esp
amI
Background Buffer Overflow Integer and Heap Overflow Summary
Stack Operation
•
•
•
Call Chain
who(…) yoo
{ Frame
• • • yoo Pointer
amI(); %ebp
who
• • •
who Stack
amI();
Pointer
• • •
%esp
} amI amI
amI
amI
Background Buffer Overflow Integer and Heap Overflow Summary
Stack Operation
•
•
Frame
•
Pointer
Call Chain %ebp
yoo(…) yoo
{ Stack
• yoo Pointer
• %esp
who(); who
•
•
} amI amI
amI
amI
Background Buffer Overflow Integer and Heap Overflow Summary
Stack
Runtime stack (8MB limit) FF
Stack
E. g., local variables
8MB
Heap
Dynamically allocated storage
When call malloc(), calloc(),
new()
Data
Statically allocated data
E.g., arrays & strings declared
in code
Text
Executable machine Heap
instructions Data
Upper 2 hex digits Text
Read-only 08
= 8 bits of address
00
Background Buffer Overflow Integer and Heap Overflow Summary
char big_array[1<<24]; /* 16 MB */
char huge_array[1<<28]; /* 256 MB*/ FF
Stack
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
Where does everything go? 08
Text
00
Background Buffer Overflow Integer and Heap Overflow Summary
$esp 0xffffbcd0
p3 0x65586008
p1 0x55585008
p4 0x1904a110
p2 0x1904a008
&p2 0x18049760
&beyond 0x08049744
big_array 0x18049780
huge_array 0x08049760
main() 0x080483c6
80
useless() 0x08049744
final malloc() 0x006be166 Heap
Data
malloc() is dynamicall ylinked
Text
address determined at runtime
08
00
Outline
1 Background
2 Buffer Overflow
4 Summary
Background Buffer Overflow Integer and Heap Overflow Summary
AIM
client
AIM
client
Background Buffer Overflow Integer and Heap Overflow Summary
August 1999
Mysteriously, Messenger clients can no longer access AIM
servers.
Microsoft and AOL begin the IM war:
AOL changes server to disallow Messenger clients
Microsoft makes changes to clients to defeat AOL changes.
At least 13 such skirmishes.
How did it happen?
The Internet Worm and AOL/Microsoft War were both
based on stack buffer overflow exploits!
many library functions do not check argument sizes.
allows target buffers to overflow.
Background Buffer Overflow Integer and Heap Overflow Summary
/* 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
Background BufferOverflow Integer and Heap Overflow Summary
echo
80485c5: 55 push %ebp
80485c6: 89 e5 mov %esp,%ebp
80485c8: 53 push %ebx
80485c9: 83 ec 14 sub $0x14,%esp
80485cc: 8d 5d f8 lea 0xfffffff8(%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 $0x14,%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
Background Buffer Overflow Integer and Heap Overflow Summary
/* Echo Line */
Before call to gets void echo()
{
Stack Frame char buf[4]; /* Way too small! */
for main gets(buf);
puts(buf);
}
echo:
Return Address pushl %ebp # Save %ebp on stack
movl %esp, %ebp
Saved %ebp %ebp pushl %ebx # Save %ebx
subl $20, %esp # Allocate stack space
Saved %ebx leal -8(%ebp),%ebx # Compute buf as %ebp-8
movl %ebx, (%esp) # Push buf on stack
[3] [2] [1] [0] buf call gets # Call gets
. . .
Stack Frame
for echo
Background BufferOverflow Integer and Heap Overflow Summary
Return Address 08 04 85 f0
Saved %ebp ff ff d6 88 0xffffd678
Saved%ebx Saved%ebx
[3] [2] [1] [0] buf xx xx xx xx buf
Stack Frame Stack Frame
forecho forecho
08 04 85 f0 08 04 85 f0
ff ff d6 88 0xffffd678 ff ff d6 88 0xffffd678
Saved %ebx 00 37 36 35
xx xx xx xx buf 34 33 32 31 buf
Stack Frame Stack Frame
for echo for echo
08 04 85 f0 08 04 85 f0
ff ff d6 88 0xffffd678 ff ff d6 00 0xffffd678
Saved %ebx 38 37 36 35
xx xx xx xx buf 34 33 32 31 buf
Stack Frame Stack Frame
for echo for echo
08 04 85 f0 08 04 85 00
ff ff d6 88 0xffffd678 43 42 41 39 0xffffd678
Saved %ebx 38 37 36 35
xx xx xx xx buf 34 33 32 31 buf
Stack Frame Stack Frame
for echo for echo
void foo(){
foo stack frame
bar(); return
... address
} A B
/* Echo Line */
void echo()
{
char buf[4]; /* Way too small! */
gets(buf);
puts(buf);
}
System-Level Protections
Stack Canaries
Idea
Place special value (“canary”) on stack just beyond buffer
Check for corruption before exiting function
GCC Implementation
-fstack-protector
-fstack-protector-all
unix>./bufdemo-protected
Type a string:1234
1234
unix>./bufdemo-protected
Type a string:12345
*** stack smashing detected ***
Background Buffer Overflow Integer and Heap Overflow Summary
echo
804864d: 55 push %ebp
804864e: 89 e5 mov %esp,%ebp
8048650: 53 push %ebx
8048651: 83 ec 14 sub $0x14,%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 0xfffffff8(%ebp),%eax
8048675: 65 33 05 14 00 00 00
804867c: 74 05 xor %gs:0x14,%eax
je 8048683 <echo+0x36>
804867e: e8 a9 fd ff ff
call 804842c <FAIL>
8048683: 83 c4 14 add $0x14,%esp
8048686: 5b pop %ebx
8048687: 5d
pop %ebp
8048688: c3
ret
Background Buffer Overflow Integer and Heap Overflow Summary
Setting Up Canary
Checking Canary
Stack Frame
for echo
Background Buffer Overflow Integer and Heap Overflow Summary
Canary Example
1 Background
2 Buffer Overflow
4 Summary
Background Buffer Overflow Integer and Heap Overflow Summary
an untrusted source
an incomplete
unsigned int x = read_int();
if ( x > 0x7fffffff ) check
abort();
unsigned int s = x*sizeof(int); an integer overflow
char* p=malloc(s);
read_int_into_buf(p, x);
a sensitive
a heap overflow
operation
followed
Background Buffer Overflow Integer and Heap Overflow Summary
CVE-2008-5238(Xine)
an untrusted source
……
if (version == 4) {
const uint16_t sps = _X_BE_16 (this->header+44) ? : 1;
this->w = _X_BE_16 (this->header+42);
this->h = _X_BE_16 (this->header+40);
this->cfs = _X_BE_32 (this->header+24);
this->frame_len = this->w * this->h;
this->frame_size = this->frame_len * sps;
this->frame_buffer = calloc(this->frame_size, 1);
……
a sensitive
operation
Background BufferOverflow Integer and Heap Overflow Summary
CVE-2008-1722(CUPS)
an untrusted source
a n in c o m plete
png_get_IHDR(pp, info, &width, &height, &bit_depth, &co l or _t y p e,
&interlace_type, &compression_type, &filter_type);
{
……
check
if (width == 0 || width > CUPS_IMAGE_MAX_WIDTH ||
height == 0 || height > CUPS_IMAGE_MAX_HEIGHT)
{//error
return (1);
} an integer overflow
img->xsize = width;
img->ysize = height;
……
if (color_type == PNG_COLOR_TYPE_GRAY ||color_type ==
PNG_COLOR_TYPE_GRAY_ALPHA)
in = malloc(img->xsize * img->ysize);
else
in = malloc(img->xsize * img->ysize * 3);
……
} a55sensitive
operation 55
Background Buffer Overflow Integer and Heap Overflow Summary
CVE-2008-2430(VLC)
an untrusted source
……
if( ChunkFind( p_demux, "fmt ", &i_size ) )
{
msg_Err( p_demux, "cannot find 'fmt ' chunk" );
}
goto error;
an incomplete
if( i_size < sizeof( WAVEFORMATEX ) - 2 )
{
check
msg_Err( p_demux, "invalid 'fmt ' chunk" );
goto error;
}
stream_Read( p_demux->s, NULL, 8 );
an integer overflow
/* Cannot fail */
/* load waveformatex */
p_wf_ext = malloc( EVEN( i_size ) + 2 );
……
a sensitive
operation
Background Buffer Overflow Integer and Heap Overflow Summary
an untrusted source
an incomplete
unsigned int x = read_int();
if ( x > 0x7fffffff )
check
abort();
unsigned int s = x*sizeof(int); an integer overflow
char* p=malloc(s);
read_int_into_buf(p, x);
a sensitive
operation
Outline
1 Background
2 Buffer Overflow
4 Summary
Background Buffer Overflow Integer and Heap Overflow Summary
Summary
Software vulnerabilities
1 Cannot be avoided (software complexity)
2 Memory bugs (buffer overflow, integer overflow) are
dangerous
3 Tons of research has been carried out to stop memory
bugs
4 Stack, and integer and heap overflow can be stopped.
Background Buffer Overflow Integer and Heap Overflow Summary
References
http://en.wikipedia.org/wiki/Buffer_overflow Smashing
the Stack for Fun and Profit by Aleph One
A Comparison of Buffer Overflow Prevention
Implementations and Weaknesses (Blackhat 2004)
http://www.cs.cmu.edu/afs/cs/academic/class/15213-
f10/www/lectures/08-machine-advanced.pdf
...