Format string problems are a classic C/C++ issue that are now rare due to the ease of discovery. The reason format string vulnerabilities can be exploited is due to the %n operator. The %n operator will write the number of characters, which have been printed by the format string therefore far, to the memory pointed to by its argument.

+

+

Through skilled creation of a format string, a malicious user may use values on the stack to create a write-what-where condition. Once this is achieved, he can execute arbitrary code.

+

+

+

==Risk Factors==

+

+

TBD

+

+

==Examples==

===Example 1===

===Example 1===

Line 66:

Line 108:

</pre>

</pre>

−

==Related Threats==

+

===Example 4===

−

==Related Attacks==

+

The following example is exploitable, due to the printf() call in the printWrapper() function. Note: The stack buffer was added to make exploitation more simple.

+

+

<pre>

+

#include <stdio.h>

+

+

void printWrapper(char *string) {

+

printf(string);

+

}

+

+

int main(int argc, char **argv) {

+

char buf[5012];

+

memcpy(buf, argv[1], 5012);

+

printWrapper(argv[1]);

+

return (0);

+

}

+

</pre>

+

+

+

==Related [[Attacks]]==

* [[Format String Attack]]

* [[Format String Attack]]

* [[:Category:Injection Attack]]

* [[:Category:Injection Attack]]

−

==Related Vulnerabilities==

−

[[Buffer Overflow]]

+

==Related [[Vulnerabilities]]==

+

+

* [[Buffer Overflow]]

+

* [[Injection problem]]

+

* [[Write-what-where]]

+

+

+

+

==Related [[Controls]]==

+

+

* [[:Category:Input Validation]]

+

* Requirements specification: Choose a language which is not subject to this flaw.

+

* Implementation: Ensure that all format string functions are passed a static string which cannot be controlled by the user, and that the proper number of arguments are always sent to that function as well. If at all possible, do not use the %n operator in format strings.

+

* Build: Heed the warnings of compilers and linkers, since they may alert you to improper usage.

Format string problems are a classic C/C++ issue that are now rare due to the ease of discovery. The reason format string vulnerabilities can be exploited is due to the %n operator. The %n operator will write the number of characters, which have been printed by the format string therefore far, to the memory pointed to by its argument.

Through skilled creation of a format string, a malicious user may use values on the stack to create a write-what-where condition. Once this is achieved, he can execute arbitrary code.

Risk Factors

TBD

Examples

Example 1

The following code copies a command line argument into a buffer using snprintf().

This code allows an attacker to view the contents of the stack and write to the stack using a command line argument containing a sequence of formatting directives. The attacker can read from the stack by providing more formatting directives, such as %x, than the function takes as arguments to be formatted. (In this example, the function takes no arguments to be formatted.) By using the %n formatting directive, the attacker can write to the stack, causing snprintf() to write the number of bytes output thus far to the specified argument (rather than reading a value from the argument, which is the intended behavior). A sophisticated version of this attack will use four staggered writes to completely control the value of a pointer on the stack.

Example 2

Certain implementations make more advanced attacks even easier by providing format directives that control the location in memory to read from or write to. An example of these directives is shown in the following code, written for glibc:

printf("%d %d %1$d %1$d\n", 5, 9);

This code produces the following output:

5 9 5 5

It is also possible to use half-writes (%hn) to accurately control arbitrary DWORDS in memory, which greatly reduces the complexity needed to execute an attack that would otherwise require four staggered writes, such as the one mentioned in Example 1.

Example 3

Simple format string vulnerabilities often result from seemingly innocuous shortcuts. The use of some such shortcuts is so ingrained that programmers might not even realize that the function they are using expects a format string argument.

For example, the syslog() function is sometimes used as follows:

...
syslog(LOG_ERR, cmdBuf);
...

Because the second parameter to syslog() is a format string, any formatting directives included in cmdBuf are interpreted as described in Example 1.

The following code shows a correct usage of syslog():

...
syslog(LOG_ERR, "%s", cmdBuf);
...

Example 4

The following example is exploitable, due to the printf() call in the printWrapper() function. Note: The stack buffer was added to make exploitation more simple.

Requirements specification: Choose a language which is not subject to this flaw.

Implementation: Ensure that all format string functions are passed a static string which cannot be controlled by the user, and that the proper number of arguments are always sent to that function as well. If at all possible, do not use the %n operator in format strings.

Build: Heed the warnings of compilers and linkers, since they may alert you to improper usage.