21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Discussion<br />

Buffer overflows get a lot of attention in the technical world, partially because they<br />

constitute one of the largest classes of security problems in code, but also because<br />

they have been around for a long time and are easy to get rid of, yet still are a huge<br />

problem.<br />

Buffer overflows are generally very easy for a C or C++ programmer to understand.<br />

An experienced programmer has invariably written off the end of an array, or<br />

indexed into the wrong memory because she improperly checked the value of the<br />

index variable.<br />

Because we assume that you are a C or C++ programmer, we won’t insult your intelligence<br />

by explaining buffer overflows to you. If you do not already understand the<br />

concept, you can consult many other software security books, including Building<br />

Secure Software by John Viega and Gary McGraw (Addison Wesley). In this recipe,<br />

we won’t even focus so much on why buffer overflows are such a big deal (other<br />

resources can help you understand that if you’re insatiably curious). Instead, we’ll<br />

focus on state-of-the-art strategies for mitigating these problems.<br />

String handling<br />

Most languages do not have buffer overflow problems at all, because they ensure<br />

that writes to memory are always in bounds. This can sometimes be done at compile<br />

time, but generally it is done dynamically, right before data gets written. The C and<br />

C++ philosophy is different—you are given the ability to eke out more speed, even if<br />

it means that you risk shooting yourself in the foot.<br />

Unfortunately, in C and C++, it is not only possible to overflow buffers but also<br />

easy, particularly when dealing with strings. The problem is that C strings are not<br />

high-level data types; they are arrays of characters. The major consequence of this<br />

nonabstraction is that the language does not manage the length of strings; you have<br />

to do it yourself. The only time C ever cares about the length of a string is in the standard<br />

library, and the length is not related to the allocated size at all—instead, it is<br />

delimited by a 0-valued (NULL) byte. Needless to say, this can be extremely errorprone.<br />

One of the simplest examples is the ANSI C standard library function, gets( ):<br />

char *gets(char *str);<br />

This function reads data from the standard input device into the memory pointed to<br />

by str until there is a newline or until the end of file is reached. It then returns a<br />

pointer to the buffer. In addition, the function NULL-terminates the buffer.<br />

If the buffer in question is a local variable or otherwise lives on the program stack,<br />

then the attacker can often force the program to execute arbitrary code by overwriting<br />

important data on the stack. This is called a stack-smashing attack. Even when<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Preventing Buffer Overflows | 79

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!