Explore our new Cybersecurity Terms Glossary, explained by our experts.

View the Glossary View the Glossary
PRIVIA

Cybersecurity Terms Glossary

What Is a Format String Vulnerability?

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.

Illustration for the cybersecurity term Format String Vulnerability
Home / Blog / Cybersecurity Terms / What Is a Format String Vulnerability?
17 November 2026

What Is a Format String Vulnerability?

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 One-Character Difference

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.

From Information Leak to Code Execution

The capabilities escalate quickly, and they are all reachable from a single vulnerable call.

Historic and Modern Cases

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.

Removing the Class Entirely

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.

FAQ – Frequently Asked Questions

Why Is %n So Much Worse Than %x?

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.

Does the Compiler Catch These Automatically?

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.

Are Languages Other Than C Affected?

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.

How Do I Find Format String Bugs in Existing Code?

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.

Related Terms

You May Be Interested In These

What Is SMB Protocol Security?

Cybersecurity Terms

17 November 2026

What Is SMB Protocol Security?

Read More
What Is OpenID Connect?

Cybersecurity Terms

17 November 2026

What Is OpenID Connect?

Read More
What Are FIDO2 and WebAuthn?

Cybersecurity Terms

17 November 2026

What Are FIDO2 and WebAuthn?

Read More