Explore our new Cybersecurity Terms Glossary, explained by our experts.
Cybersecurity Terms Glossary
A format string vulnerability occurs when user input is passed as the format argument to printf and similar functions, giving an attacker the ability to read memory and, with %n, write to it.

What Is a Format String Vulnerability?
One missing argument turns a logging statement into an arbitrary memory write. A format string vulnerability exists when a program passes attacker-controlled text as the format argument of printf, sprintf, syslog, or a similar function, instead of passing it as data to be printed. The function then interprets whatever conversion specifiers the attacker included and acts on them.
The correct call is printf("%s", user_input). The vulnerable call is printf(user_input). Both compile; both print the same thing during testing, because ordinary text contains no percent signs. The difference only appears when someone supplies a string full of conversion specifiers.
Variadic functions in C have no way to know how many arguments they were actually given. They read the format string, and for every specifier they encounter they fetch the next argument from wherever the calling convention puts it, which may be a register or a position on the stack. When the format string says there are ten arguments and the caller supplied none, the function reads ten values that belong to something else entirely.
The capabilities escalate quickly, and they are all reachable from a single vulnerable call.
%x or %p specifiers prints successive words of stack memory, which commonly reveals saved return addresses, stack canary values, and the base addresses of loaded libraries. That alone defeats ASLR for the leaked module.%s with a parameter index and a pointer the attacker has placed in the buffer dereferences an address of their choosing and prints the string there, which can dump keys or configuration from anywhere in the process.%n specifier is the dangerous one. It writes the number of characters output so far to the address held in the corresponding argument. By padding the output with a field width and pointing the argument at a chosen address, an attacker writes a value of their choosing to a location of their choosing.The class became public in 1999 and 2000, with the wu-ftpd site command vulnerability, CVE-2000-0573, as the case that convinced people it mattered: a widely deployed FTP daemon remotely exploitable through a logging call. CVE-2012-0809 found a format string bug in the sudo debugging subsystem, where the program name supplied on the command line reached a logging function unescaped, allowing local privilege escalation.
The class is less common now than it was, because compilers warn about it and many codebases have been swept. It persists in embedded and operational technology firmware, in long-lived C services, and in code where a wrapper function obscures the call, for example a custom logging helper that takes a message and passes it straight to vsnprintf as the format.
This is one of the few vulnerability classes that can be eliminated rather than managed, and the measures are cheap.
Compile with -Wformat -Wformat-security -Werror=format-security, which turns a non-literal format string into a build failure. GCC and Clang both support __attribute__((format(printf, n, m))) so that your own wrapper functions get the same checking as the standard library. Build with _FORTIFY_SOURCE set to 2 or 3, which rejects %n in writable format strings at runtime on glibc. Add a linting rule so the pattern cannot be reintroduced, and scan legacy code with a static analyser rather than reading it.
In new code, the problem largely disappears. Rust, Go, and modern C++ formatting libraries resolve format arguments at compile time and have no mechanism equivalent to %n. Where C is unavoidable, keep the rule simple: the format string is always a literal, and user input is always an argument.
Our Web Application Security Services and source code reviews treat format string findings as build pipeline defects, because a project that produced one will usually produce more unless the compiler flag is set.
Because it writes rather than reads. The %x and %s specifiers disclose memory, which is serious but passive. The %n specifier stores the count of characters printed so far into the address supplied as the corresponding argument, giving an attacker who controls the format string an arbitrary memory write. That is the primitive needed to overwrite a function pointer and take control of execution.
Only if you enable the warnings. GCC and Clang detect non-literal format strings with -Wformat-security, and -Werror=format-security makes it a build failure, but neither is on by default in most projects. Custom logging wrappers also need the format attribute annotation, otherwise the compiler has no way to know that an argument is used as a format string.
The classic memory-write version is specific to C and C++ because of variadic functions and the %n specifier. Other languages have related issues where user input reaches a formatting routine, which can cause information disclosure or denial of service, for example Python format string injection exposing object attributes through str.format. The impact is usually disclosure rather than code execution.
Start with the compiler. Rebuilding with -Wformat-security on a legacy codebase surfaces most instances quickly and cheaply. Follow up with a static analyser that tracks tainted data into formatting functions, which catches cases routed through wrappers, and review any custom logging helper by hand, since those are where the pattern most often survives a sweep.
You May Be Interested In These
We use a few cookies that are needed to make this website work, and they are always on. We would also like to set analytics cookies, to see which pages are useful; functional cookies, so that our live chat works; and advertising cookies, to measure our campaigns. We will not set any of those until you say yes, and you can change your mind at any time from the Cookie Settings link in the footer.
Choose the cookies you are happy for us to use. Strictly necessary cookies are always on; everything else is off until you turn it on.
These cookies keep the website working: they hold your security checks, your form submissions and the choice you make here. The site cannot run without them, so they are always on.
These let our live chat open and remember your conversation, and let the newsletter sign-up work. Switch them off and the chat window will not appear.
These tell us which pages are read and how people find us, so that we can improve the site. The reports are about groups of visitors, never about you personally.
These let us measure our advertising and show you our adverts on other websites, such as LinkedIn, Google and Meta. Switch them off and you will still see adverts, but they will be less relevant.