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.

There are a few ways to alleviate this particular problem:<br />

• You can make sure never to use signed data types. Unfortunately, that is not<br />

very practical—particularly when you are using API functions that take both<br />

signed and unsigned values. If you try to ensure that all your data is always<br />

unsigned, you might end up with an unsigned-to-signed conversion problem<br />

when you call a library function that takes a regular int instead of an unsigned<br />

int or a size_t.<br />

• You can check to make sure x is not negative while it is still signed. There is<br />

nothing wrong with this solution. Basically, you are always assuming the worst<br />

(that the data may be cast), and it might not be.<br />

• You can cast x to a size_t before you do your testing. This is a good strategy for<br />

those who prefer testing data as close as possible to the state in which it is going<br />

to be used to prevent an unanticipated change in the meantime. Of course, the<br />

cast to a signed value might be unanticipated for the many programmers out<br />

there who do not know that size_t is not a signed data type. For those people,<br />

the second solution makes more sense.<br />

No matter what solution you prefer, you will need to be diligent about conversions<br />

that might apply to your data when you perform your bounds checking.<br />

Unsigned-to-signed coercion<br />

<strong>Problem</strong>s may also occur when an unsigned value gets converted to a signed value.<br />

For example, consider the following code:<br />

int main(int argc, char *argv[ ]) {<br />

char foo[ ] = "abcdefghij";<br />

char *p = foo + 4;<br />

unsigned int x = 0xffffffff;<br />

if (p + x > p + strlen(p)) {<br />

printf("Buffer overflow!\n");<br />

return -1;<br />

}<br />

printf("%s\n", p + x);<br />

return 0;<br />

}<br />

The poor programmer who wrote this code is properly preventing from reading past<br />

the high end of p, but he probably did not realize that the pointers are signed.<br />

Because x is –1 once it is cast to a signed value, the result of p + x will be the byte of<br />

memory immediately preceding the address to which p points.<br />

While this code is a contrived example, this is still a very real problem. For example,<br />

say you have an array of fixed-size records. The program might wish to write arbitrary<br />

data into a record where the user supplies the record number, and the program<br />

might calculate the memory address of the item of interest dynamically by multiplying<br />

the record number by the size of a record, and then adding that to the address at<br />

90 | 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!