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.

Solution<br />

Functions such as the printf( ) family of functions provide a flexible and powerful<br />

way to format data easily. Unfortunately, they can be extremely dangerous as well.<br />

Following the guidelines outlined in the following “Discussion” section will allow<br />

you to easily avert many of the problems with these functions.<br />

Discussion<br />

The printf( ) family of functions—and other functions that use them, such as<br />

syslog( ) on Unix systems—all require an argument that specifies a format, as well<br />

as a variable number of additional arguments that are substituted at various locations<br />

in the format string to produce formatted output. The functions come in two<br />

major varieties:<br />

• Those that output to a file (printf( ) outputs to stdout)<br />

• Those that output to a string<br />

Both can be dangerous, but the latter variety is significantly more so.<br />

The format string is copied, character by character, until a percent (%) symbol is<br />

encountered. The characters that immediately follow the percent symbol determine<br />

what will be output in their place. For each substitution in the format string, the next<br />

argument in the variable argument list is used. Because of the way that variable-sized<br />

argument lists work in C (see Recipe 13.4), the functions assume that the number of<br />

arguments present in the argument list is equal to the number of substitutions<br />

required by the format string. The GCC compiler in particular will recognize calls to<br />

the functions in the printf( ) family, and it will emit warnings if it detects data type<br />

mismatches or an incorrect number of arguments in the variable argument list.<br />

If you adhere to the following guidelines when using the printf( ) family of functions,<br />

you can be reasonably certain that you are using the functions safely:<br />

Beware of the “%n” substitution.<br />

All but one of the substitutions recognized by the printf( ) family of functions<br />

use arguments from the variable argument list as data to be substituted into the<br />

output. The lone exception is “%n”, which writes the number of bytes written to<br />

the output buffer or file into the memory location pointed to by the next argument<br />

in the argument list.<br />

While the “%n” substitution has its place, few programmers are aware of it and<br />

its implications. In particular, if external input is used for the format string, an<br />

attacker can embed a “%n” substitution into the format string to overwrite portions<br />

of the stack. The real problem occurs when all of the arguments in the variable<br />

argument list have been exhausted. Because arguments are passed on the<br />

stack in C, the formatting function will write into the stack.<br />

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