Format String Vulnerabilities
Integer Overflows & Type Confusion · Exploitation Fundamentals track
3 min read
In this module
The Most Powerful Printf Bug
The C printf function family is powerful: it takes a format string containing specifiers like %d, %s, and %x, and replaces each one with a value from the function's arguments. But what happens when user input is passed directly as the format string?
// SAFE: user input is an argumentprintf("%s", user_input);
// DANGEROUS: user input IS the format stringprintf(user_input);In the dangerous version, if the user sends %x %x %x, printf will read values from the stack - because it expects arguments to be there for each specifier. The user is now reading memory they were never meant to see.
Reading the Stack with %x
Each %x specifier tells printf to pop the next value from where it expects arguments and print it as hex. Since no actual arguments were provided, printf reads whatever's on the stack: saved registers, return addresses, locals from other functions, and potentially sensitive material like passwords or key material.
Repeat %x enough times and you walk the stack, value by value — a straight read-primitive handed to the attacker. As we'll see in Exploit Mitigations, this kind of information leak is often the first step in defeating ASLR.
Format string bugs provide two fundamental exploitation primitives: read (using %x or %s to leak memory contents) and write (using %n to store values to memory). Together, these give an attacker nearly complete control over the process - all from a single misused printf call.
Writing to Memory with %n
The %n specifier is unique: instead of printing something, it writes the number of characters printed so far to the memory address provided as its argument. Since the attacker controls the format string, they can:
- Place a target address on the stack (as part of the input string itself)
- Use
%xspecifiers to advance printf's argument pointer to that address - Use
%nto write a controlled value to that address
By carefully crafting the format string, an attacker can write arbitrary values to arbitrary memory addresses. This means they can overwrite return addresses, function pointers, or security-critical variables - achieving the same impact as a buffer overflow but through a completely different mechanism.
Historical Impact
Format string vulnerabilities emerged as a recognised exploit class around 2000 and triggered a wave of critical patches across Unix systems, FTP servers, and network daemons. The wu-ftpd and rpc.statd exploits were early landmarks. Modern compilers flag these with -Wformat-security, but the class persists in embedded systems, IoT devices, and any codebase where compiler warnings are treated as noise.
Defensive Thinking
The fix is simple in principle: never use user input as a format string. Always pass user data as an argument: printf("%s", input) instead of printf(input). Modern compilers can flag this with -Wformat-security, and static analysis tools detect it reliably. Despite this, format string bugs persist in large codebases, especially in logging functions that wrap printf without preserving format-string safety.
Try the Simulations
Integer Overflow Bypass
Add a 10-byte header to an attacker-supplied length of 250 and watch the 8-bit total wrap round to 4. The wrapped value sails through a size check it should have failed.
Type Confusion
Store a value intended as a 'pointer' (address 15), then read it back and use it as a loop counter. The same bits mean completely different things depending on interpretation.