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.

To combat malicious uses of “%n”, Immunix has produced a set of patches for<br />

glibc 2.2 (the standard C runtime library for Linux) known as FormatGuard. The<br />

patches take advantage of a GCC compiler extension that allows the preprocessor<br />

to distinguish between macros having the same name, but different numbers<br />

of arguments. FormatGuard essentially consists of a large set of macros for the<br />

syslog( ), printf( ), fprintf( ), sprintf( ), and snprintf( ) functions; the macros<br />

call safe versions of the respective functions. The safe functions count the<br />

number of substitutions in the format string, and ensure that the proper number<br />

of arguments has been supplied.<br />

Do not use a string from an external source directly as the format specification.<br />

Strings obtained from an external source may contain unexpected percent symbols<br />

in them, causing the formatting function to attempt to substitute arguments<br />

that do not exist. If you need simply to output the string str (to stdout<br />

using printf( ), for example), do the following:<br />

printf("%s", str);<br />

Following this rule to the letter is not always desirable. In particular, your program<br />

may need to obtain format strings from a data file as a consequence of<br />

internationalization requirements. The format strings will vary to some extent<br />

depending on the language in use, but they should always have identical substitutions.<br />

When using vsprintf( ) or sprintf( ) to output to a string, be very careful of using the<br />

“%s” substitution without specifying a precision.<br />

The vsprintf( ) and sprintf( ) functions both assume an infinite amount of<br />

space is available in the buffer into which they write their output. It is especially<br />

common to use these functions with a statically allocated output buffer. If a<br />

string substitution is made without specifying the precision, and that string<br />

comes from an external source, there is a good chance that an attacker may<br />

attempt to overflow the static buffer by forcing a string that is too long to be<br />

written into the output buffer. (See Recipe 3.3 for a discussion of buffer overflows.)<br />

One solution is to check the length of the string to be substituted into the output<br />

before using it with vsprintf( ) or sprintf( ). Unfortunately, this solution is<br />

error-prone, especially later in your program’s life when another programmer<br />

has to make a change to the size of the buffer or the format string, necessitating a<br />

change to the check.<br />

A better solution is to use a precision modifier in the format string. For example,<br />

if no more than 12 characters from a string should ever be substituted into<br />

the output, use “%.12s” instead of simply “%s”. The advantage to this solution<br />

is that it is part of the formatting function call; thus, it is less likely to be overlooked<br />

in the event of a later change to the format string.<br />

Preventing Attacks on Formatting Functions | 77<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!