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.

the buffer is heap-allocated (that is, it is allocated with malloc() or new(), a buffer<br />

overflow can be security-critical if an attacker can write over critical data that happens<br />

to be in nearby memory.<br />

The problem with this function is that, no matter how big the buffer is, an attacker<br />

can always stick more data into the buffer than it is designed to hold, simply by<br />

avoiding the newline.<br />

There are plenty of other places where it is easy to overflow strings. Pretty much any<br />

time you perform an operation that writes to a “string,” there is room for a problem.<br />

One famous example is strcpy( ):<br />

char *strcpy(char *dst, const char *src);<br />

This function copies bytes from the address indicated by src into the buffer pointed<br />

to by dst, up to and including the first NULL byte in src. Then it returns dst. No effort<br />

is made to ensure that the dst buffer is big enough to hold the contents of the src<br />

buffer. Because the language does not track allocated sizes, there is no way for the<br />

function to do so.<br />

To help alleviate the problems with functions like strcpy( ) that have no way of<br />

determining whether the destination buffer is big enough to hold the result from<br />

their respective operations, there are also functions like strncpy( ):<br />

char *strncpy(char *dst, const char *src, size_t len);<br />

The strncpy( ) function is certainly an improvement over strcpy( ), but there are still<br />

problems with it. Most notably, if the source buffer contains more data than the limit<br />

imposed by the len argument, the destination buffer will not be NULL-terminated.<br />

This means the programmer must ensure the destination buffer is NULL-terminated.<br />

Unfortunately, the programmer often forgets to do so; there are two reasons for this<br />

failure:<br />

• It’s an additional step for what should be a simple operation.<br />

• Many programmers do not realize that the destination buffer may not be NULLterminated.<br />

The problems with strncpy( ) are further complicated by the fact that a similar function,<br />

strncat( ), treats its length-limiting argument in a completely different manner.<br />

The difference in behavior serves only to confuse programmers, and more often than<br />

not, mistakes are made. Certainly, we recommend using strncpy( ) over using<br />

strcpy( ); however, there are better solutions.<br />

OpenBSD 2.4 introduced two new functions, strlcpy( ) and strlcat( ), that are consistent<br />

in their behavior, and they provide an indication back to the caller of how<br />

much space in the destination buffer would be required to successfully complete their<br />

respective operations without truncating the results. For both functions, the length<br />

limit indicates the maximum size of the destination buffer, and the destination buffer<br />

is always NULL-terminated, even if the destination buffer must be truncated.<br />

80 | Chapter 3: Input Validation<br />

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

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

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

Saved successfully!

Ooh no, something went wrong!