Conceptcore
Format string vulnerability
Also called format string attack.
Passing untrusted data as the format argument of a printf-like function, so that the data, not the program, decides which arguments the function reads.
A format string vulnerability is a flaw in which a program passes data from an untrusted source as the format argument of a function that interprets directives in its format, such as printf and its relatives in C. The function reads one argument for each directive in the format. The program decides how many arguments it passes, but the data decides how many directives there are, so the function can read values that the caller never passed. In C this is undefined behavior. In practice it can crash the program, show the contents of memory, and, because C also has a directive that stores a value through a pointer argument, change memory. The class is CWE-134, "use of externally-controlled format string". This page describes the class, benign examples of it and the defenses. It gives no attack input.
In plain words
Some print functions in C take a template and a list of values. The template "Saved %d files" says: print the text, and put a number from the list where the percent sign is.
The function trusts the template. It does not know how many values the program gave it. It takes one value for each percent sign it sees.
A bug appears when a program uses text from a user as the template. A user who types "100% done" means only that text. But the function sees a percent sign and takes a value from the list, which is empty. It prints whatever it finds in memory.
The fix is simple. Never use outside text as the template. Use a fixed template that says "print this text as it is", and give the user text as the value.
The idea
A formatting function scans and finds directives . Each directive needs one argument of a type that the directive names, such as an integer or a pointer to a string. A call is well formed when and the type of each matches . The caller fixes and the types when the program is compiled. In C, a variadic function has no way to learn , so reads the -th argument from where it would be if the caller had passed it.
When is a constant, the compiler knows and the types and can check the call. When comes from input, and the types are chosen by whoever wrote the input, and the check is impossible at compile time. That is the whole flaw: untrusted data is used as a program for a small interpreter, the formatting function. It is the same pattern as SQL injection and cross-site scripting, where data reaches an interpreter of queries or of pages.
The effects follow from what the directives do:
- a directive that reads an argument which was not passed reads some other value in memory, which can disclose data (confidentiality);
- a directive that follows a pointer argument which was not passed can follow a wrong address and crash the program (availability);
- the directive that stores a value through a pointer can change memory (integrity), which puts the class next to other memory safety errors.
Defenses. Each one removes a part of the flaw.
- Constant formats. Pass a literal format and give untrusted text as an argument:
printf("%s", s)instead ofprintf(s), or use a function that writes a string without interpreting it. This is the first mitigation of CWE-134. - Compiler warnings. GCC and Clang warn when a format is not a literal, and the
formatattribute lets a project mark its own wrappers, such as a logging function, so that their calls are checked too. A build can turn these warnings into errors. - Type qualifiers and taint analysis. Shankar, Talwar, Foster and Wagner (USENIX Security 2001) mark data from input as tainted and require the format parameter to be untainted. A constraint-based inference of the qualifiers then finds every flow from input to a format, which is a case of taint analysis. They reported previously unknown bugs with few false alarms.
- Checks in the library. FormatGuard (Cowan et al., USENIX Security 2001) is a small change to the GNU C library that stops a call whose format asks for more arguments than the caller passed. Hardened builds of the C library also refuse some dangerous uses of formats at run time.
- Languages that check. A formatting function that knows its arguments, as in most memory-safe languages, raises an error when the format asks for a missing value, instead of reading memory.
Examples
All examples are benign: the text is ordinary user text that the program must print literally.
- A progress message. A program reads a status line from a job and logs it with
printf(line). The job reports "100% done". The function sees "% d" as a directive with a space flag for an integer, reads an integer argument that was never passed, and prints "100", then some number from memory, then "one". The user sees a wrong message, and the program has shown part of its memory. Withprintf("%s", line), the output is "100% done". - The same text in a checked language. In Python, the expression
"100% done" % ()raises an error that says that there are not enough arguments for the format. The flaw exists in the program logic, but the language turns it into an error instead of a read of memory. - A wrapper without the attribute. A project has a function
log_msg(const char *fmt, ...)that callsvprintf. Calls such aslog_msg(name)pass compiler checks until the function is marked with theformatattribute; then the compiler warns at each call with a format that is not a literal. - Not a vulnerability.
printf("Saved %d files\n", n)with an integernis well formed: the format is a constant with one directive, and the call passes one integer. User data in the arguments, such asprintf("%s\n", name), is also safe, because the function never interprets the value of a%sargument.
Where it is used
The concept is a standard entry in lists of weaknesses and in the checks of analysis tools. Compilers warn about non-literal formats, static analyzers and code property graph queries search for flows from a source to the format parameter of a known function, and taint analysis treats format parameters as sinks. Code review checklists for C and C++ include it, and logging libraries in many languages take the message as an argument of a fixed template for the same reason. In the history of the field, the class came to wide notice in 2000, when many programs were found to pass user data as a format, and the defenses above, from FormatGuard and type qualifiers in 2001 to compiler warnings that are now on in many builds, made it much rarer in new code.
References
- MITRE, CWE-134: Use of externally-controlled format string, the description of the weakness (a format string from an external source) and the mitigations: fixed format strings, functions without risky format features, and high compiler warning levels
- FormatGuard: Automatic protection from printf format string vulnerabilities, 10th USENIX Security Symposium, 2001, the abstract: the class came to light in 2000, and FormatGuard is a small change to the GNU C library that protects against it with little cost
- Umesh Shankar, Kunal Talwar, Jeffrey S. Foster and David Wagner, Detecting format string vulnerabilities with type qualifiers, 10th USENIX Security Symposium, 2001, the abstract (type qualifiers inferred by constraints, previously unknown bugs found with few false alarms)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag E4KZVV Edit this page History Report an error Markdown