21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Solution<br />

Unfortunately, integer coercion and wrap-around problems currently require you to<br />

be diligent.<br />

Best practices for such problems require that you validate any coercion that takes<br />

place. To do this, you need to understand the semantics of the library functions you<br />

use well enough to know when they may implicitly cast data.<br />

In addition, you should explicitly check for cases where integer data may wrap<br />

around. It is particularly important to perform wrap-around checks immediately<br />

before using data.<br />

Discussion<br />

Integer type problems are often quite subtle. As a result, they are very difficult to<br />

avoid and very difficult to catch unless you are exceedingly careful. There are several<br />

different ways that these problems can manifest themselves, but they always boil<br />

down to a type mismatch. In the following subsections, we’ll illustrate the various<br />

classes of integer type errors with examples.<br />

Signed-to-unsigned coercion<br />

Many API functions take only positive values, and programmers often take advantage<br />

of that fact. For example, consider the following code excerpt:<br />

if (x < MAX_SIZE) {<br />

if (!(ptr = (unsigned char *)malloc(x))) abort( );<br />

} else {<br />

/* Handle the error condition ... */<br />

}<br />

We might test against MAX_SIZE to protect against denial of service problems where<br />

an attacker causes us to allocate a large amount of memory. At first glance, the previous<br />

code seems to protect against that. Indeed, some people will worry about what<br />

happens in the case where someone tries to malloc( ) a negative number of bytes.<br />

It turns out that malloc( )’s argument is of type size_t, which is an unsigned type. As<br />

a result, any negative numbers are converted to positive numbers. Therefore, we do<br />

not have to worry about allocating a negative number of bytes; it cannot happen.<br />

However, the previous code may still not work correctly. The key to its correct operation<br />

is the data type of x. Ifx is some signed data type, such as an int, and is a negative<br />

value, we will end up allocating a large amount of data. For example, if an<br />

attacker manages to set x to –1, the call to malloc( ) will try to allocate 4,294,967,295<br />

bytes on most platforms, because the hexadecimal value of that number<br />

(0xFFFFFFF) is the same hexadecimal representation of a signed 32-bit –1.<br />

Preventing Integer Coercion and Wrap-Around <strong>Problem</strong>s | 89<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!