Bufferoverflow Prevention
Bufferoverflow Prevention
Attacks
Some unsafe C lib functions
strcpy (char *dest, const char *src)
strcat (char *dest, const char *src)
gets (char *s)
scanf ( const char *format, … )
printf (conts char *format, … )
Preventing buf overflow attacks
• Main problem:
– strcpy(), strcat(), sprintf() have no range checking.
– Use “safe” versions strncpy(), strncat() very
carefully
• Defenses:
– Type safe languages (Java, ML). Legacy code?
– Mark stack as non-execute.
– Static source code analysis.
– Run time checking: StackGuard, Libsafe, SafeC, (Purify).
– Black box testing (e.g. eEye Retina, ISIC ).
Marking stack as non-execute
• Basic stack exploit can be prevented by marking
stack segment as non-executable
– Code patches exist for Linux and Solaris.
• Problems:
– Some apps need executable stack (e.g. LISP interpreters).
Frame 2 Frame 1
top
local canary sfp ret str local canary sfp ret str of
stack
Canary Types
• Random canary: (used in Visual Studio 2003)
• Terminator canary:
Canary = 0 (null), newline, linefeed, EOF
– String functions will not copy beyond terminator.
– Hence, attacker cannot use string functions to corrupt
stack.
StackGuard (Cont.)
• Applicable to:
– Smartcards.
– Cell phones.
– PCI cards.
Timing attacks: example
• Consider the following pwd checking code:
int password-check( char *inp, char *pwd)
if (strlen(inp) != strlen(pwd)) return 0;
for( i=0; i < strlen(pwd); ++i)
if ( *inp[i] != *pwd[i] )
return 0;
return 1;
• A simple timing attack will expose the password one
character at a time.
Backup Slides
Preventing buf overflow attacks
• Main problem:
– strcpy(), strcat(), sprintf() have no range checking.
– “Safe” versions strncpy(), strncat() are misleading
• strncpy() may leave buffer unterminated.
• strncpy(), strncat() encourage off by 1 bugs.
• Defenses:
– Type safe languages (Java, ML). Legacy code?
– Mark stack as non-execute. Random stack location.
– Static source code analysis.
– Run time checking: StackGuard, Libsafe, SafeC, (Purify).
– Black box testing (e.g. eEye Retina, ISIC ).
Buffer overflows
• Extremely common bug.
– First major exploit: 1988 Internet Worm. fingerd.
Heap
buf[128] FuncPtr or
stack
– Overflowing buf will override function pointer.
libsafe main
More methods …
• Address obfuscation. (Stony Brook ’03)
Correct form:
int func(char *user) {
fprintf( stdout, “%s”, user);
}
History
• Danger discovered in June 2000.
• Examples:
– wu-ftpd 2.* : remote root.
– Linux rpc.statd: remote root
– IRIX telnetd: remote root
– BSD chpass: local root
Vulnerable functions
Any function using a format string.
Printing:
printf, fprintf, sprintf, …
vprintf, vfprintf, vsprintf, …
Logging:
syslog, err, warn
Exploit
• Dumping arbitrary memory:
– Walk up stack until desired pointer is found.
– printf( “%08x.%08x.%08x.%08x|%s|”)
– printf( “%08x.%08x.%08x.%08x.%n”)
Overflow using format string
char errmsg[512], outbuf[512];