Essence of The Problem
Essence of The Problem
3
Problems caused by buffer overflows
• Trends
– Attacks are getting cleverer
• defeating ever more clever countermeasures
– Attacks are getting easier to do, by script kiddies
4
Any C(++) code acting on untrusted input is at risk
Eg
• code taking input over untrusted network
– eg. sendmail, web browser, wireless network driver,...
• code taking input from untrusted user on multi-
user system,
– esp. services running with high privileges (as ROOT on
Unix/Linux, as SYSTEM oon Windows)
• code acting on untrusted files
– that have been downloaded or emailed
• also embedded software, eg. in devices with (wireless)
network connection such as mobile phones with Bluetooth,
wireless smartcards, airplane navigation systems, ...
5
How does buffer overflow work?
Memory management in C/C++
7
Process memory layout
Unused Memory
Low
addresses Program Code .text
8
Stack overflow
The stack consists of Activation Records:
x
AR main()
m return address
AR f()
f buf[4..7]
buf[0..3]
12
Variants & causes
Common causes:
• poor programming with of arrays and strings
– esp. library functions for null-terminated strings
• problems with format strings
13
What causes buffer overflows?
Example: gets
char buf[20];
gets(buf); // read user input until
// first EoL or EoF character
15
Example: strcpy
char dest[20];
strcpy(dest, src); // copies string src to dest
16
Spot the defect! (1)
S
char buf[20];
char prefix[] = ”http://”;
...
strcpy(buf, prefix);
// copies the string prefix to buf
strncat(buf, path, sizeof(buf));
// concatenates path to the string buf
17
Spot the defect! (1)
S
char buf[20];
char prefix[] = ”http://”;
...
strcpy(buf, prefix);
// copies the string prefix to buf
strncat(buf, path, sizeof(buf));
// concatenates path to the string buf
18
Spot the defect! (2)
S
char src[9];
char dest[9];
19
Spot the defect! (2)
S
20
Spot the defect! (2)
21
Example: strcpy and strncpy
• Don’t replace
strcpy(dest, src)
s
by
strncpy(dest, src, sizeof(dest))
s
but by
strncpy(dest, src, sizeof(dest)-1)
s
dst[sizeof(dest-1)] = `\0`;
if dest should be null-terminated!
22
Spot the defect! (3)
S
23
Spot the defect! (3)
S
char *buf;
int i, len;
24
Spot the defect! (3)
S
char *buf;
May result in integer overflow;
int i, len; we should check that
len+1 is positive
read(fd, &len, sizeof(len));
if (len < 0)
i
{error ("negative length"); return; }
buf = malloc(len+1);
read(fd,buf,len);
buf[len] = '\0'; // null terminate buf
25
Absence of language-level security
TCHAR buff[MAX_SIZE];
_sntprintf(buff, sizeof(buff), ”%s\n”, input);
26
Spot the defect! (4)
#ifdef UNICODE
#define _sntprintf _snwprintf
#define TCHAR wchar_t
#else
#define _sntprintf _snprintf
#define TCHAR char
#endif _snwprintf’s 2nd param is # of chars in
buffer, not # of bytes
TCHAR buff[MAX_SIZE];
_sntprintf(buff, sizeof(buff), ”%s\n”, input);
27
Spot the defect! (5)
S
v
void BadCode (char* input)
{ short len;
char buf[MAX_BUF];
len = strlen(input);
28
Spot the defect! (5)
29
Spot the defect! (6)
S
b
bool CopyStructs(InputFile* f, long count)
{ structs = new Structs[count];
for (long i = 0; i < count; i++)
f
{ if !(ReadFromFile(f,&structs[i])))
break;
}
}
30
Spot the defect! (6)
S
b
bool CopyStructs(InputFile* f, long count)
{ structs = new Structs[count];
for (long i = 0; i < count; i++)
f
{ if !(ReadFromFile(f,&structs[i])))
break;
}
}
effectively does a
malloc(count*sizeof(type))
which may cause integer overflow
And this integer overflow can lead to a (heap) buffer overflow.
(Microsoft Visual Studio 2005(!) C++ compiler adds check to prevent
t
this)
31
Spot the defect! (7)
32
Spot the defect! (7)
Loop termination ((exploited by Blaster)
char buff1[MAX_SIZE], buff2[MAX_SIZE];
// make sure url a valid URL and fits in buff1 and buff2:
if (! isValid(url)) return;
if (strlen(url) > MAX_SIZE – 1) return;
// copy url up to first separator, ie. first ’/’, to buff1
out = buff1;
do { length up to the first null
// skip spaces
if (*url != ’ ’) *out++ = *url;
} while (*url++ != ’/’);
strcpy(buff2, buff1);
...
33
Spot the defect! (7)
34
Spot the defect! (7)
#include <stdio.h>
36
Format string attacks
37
Format string attacks
• Note that format strings break the “don’t mix data & code”
principle.
• Easy to spot & fix:
replace printf(str) by printf(“%s”, str) )
38
Dynamic countermeasures
incl. stack canaries
Dynamic countermeasures
protection by kernel
• non-executable memory (NOEXEC)
– prevents attacker executing her code
• address space layout randomisation (ASLR)
(
– generally makes attacker's life harder
• instruction set randomisation
– hardware support needed to make this efficient enough
41
Further improvements
• PointGuard
– also protects other data values, eg function pointers,
with canaries
• ProPolice's Stack Smashing Protection (SSP) by IBM
– also re-orders stack elements to reduce potential for
trouble
• Stackshield has a special stack for return addresses, and
can disallow function pointers to the data segment
42
Dynamic countermeasures
43
Windows 2003 Stack Protection
44
Other countermeasures
Countermeasures
46
Prevention
47
Dangerous C system calls
source: Building secure software, J. Viega & G. McGraw, 2002
48
Prevention – use better string libraries
49
Better string libraries (1)
B
50
Better string libraries (2)
B
51
Detection before shipping
• Testing
– Difficult! How to hit the right cases?
– Fuzz testing - test for crash on long, random inputs – can be
succesful in finding some weaknesses
• Code reviews
– Expensive & labour intensive
• C
Code scanning tools (aka static analysis)
Eg
– RATS () – also for PHP, Python, Perl
– Flawfinder , ITS4, Deputy, Splint
– PREfix, PREfast by Microsoft
plus other commercial tools
– Coverity
– Parasoft
– Klockwork.
52
More prevention & detection
• Bounds Checkers
– add additonal bounds info for pointers and
check these at run time
– eg Bcc, RTcc, CRED, .....
– RICH prevents integer overflows
• Safe variants of C
– adding bound checks, but also type checks
and more: eg garbage collection or region-based memory
m
management)
– eg
e Cyclone (http://cyclone.thelanguage.org), CCured, Vault,
Control-C, Fail-Safe C, …
53
More prevention & detection
54
Reducing attack surface
55
Summary
56
More general
• Reading
– A Comparison of Publicly Available Tools for
Dynamic Buffer Overflow Prevention, by John
Wilander and Mariam Kamkar
58