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.

In this example, if you omitted the calls to safestr_reference( ) and safestr_<br />

release( ), and if extra was a temporary string, the call to safestr_length( ) would<br />

cause the string to be destroyed. As a result, the safestr_append( ) call would then be<br />

operating on an invalid safestr_t if the combined length of base and extra were less<br />

than 17.<br />

Finally, the SafeStr library also tracks the trustworthiness of strings. A string can be<br />

either trusted or untrusted. Operations that combine strings result in untrusted<br />

strings if any one of the strings involved in the combination is untrusted; otherwise,<br />

the result is trusted. There are few places in SafeStr’s API where the trustworthiness<br />

of a string is tested, but the function safestr_istrusted( ) allows you to test strings<br />

yourself.<br />

The strings that result from using SAFESTR_CREATE( ) or SAFESTR_TEMP( ) are untrusted.<br />

You can use SAFESTR_TEMP_TRUSTED( ) to create temporary strings that are trusted.<br />

The trustworthiness of an existing string can be altered using safestr_trust( ) to<br />

make it trusted or safestr_untrust( ) to make it untrusted.<br />

The main reason to track the trustworthiness of a string is to monitor the flow of<br />

external inputs. Safe strings created from external data should initially be untrusted.<br />

If you later verify the contents of a string, ensuring that it contains nothing dangerous,<br />

you can then mark the string as trusted. Whenever you need to use a string to<br />

perform some potentially dangerous operation (for example, using a string in a command-line<br />

argument to an external program), check the trustworthiness of the string<br />

before you use it, and fail appropriately if the string is untrusted.<br />

See Also<br />

• SafeStr: http://www.zork.org/safestr/<br />

• Recipes 3.2, 3.3<br />

3.5 Preventing Integer Coercion and<br />

Wrap-Around <strong>Problem</strong>s<br />

<strong>Problem</strong><br />

When using integer values, it is possible to make values go out of range in ways that<br />

are not obvious. In some cases, improperly validated integer values can lead to security<br />

problems, particularly when data gets truncated or when it is converted from a<br />

signed value to an unsigned value or vice versa. Unfortunately, such conversions<br />

often happen behind your back.<br />

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