Algorithm Search⌘K

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 F(fmt,a1,…,ak)F(\mathit{fmt}, a_1, \ldots, a_k) scans fmt\mathit{fmt} and finds directives d1,…,dmd_1, \ldots, d_m. Each directive did_i 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 m≤km \le k and the type of each aia_i matches did_i. The caller fixes kk and the types when the program is compiled. In C, a variadic function has no way to learn kk, so FF reads the ii-th argument from where it would be if the caller had passed it.

When fmt\mathit{fmt} is a constant, the compiler knows mm and the types and can check the call. When fmt\mathit{fmt} comes from input, mm 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 of printf(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 format attribute 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. With printf("%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 calls vprintf. Calls such as log_msg(name) pass compiler checks until the function is marked with the format attribute; 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 integer n is well formed: the format is a constant with one directive, and the call passes one integer. User data in the arguments, such as printf("%s\n", name), is also safe, because the function never interprets the value of a %s argument.

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

  1. 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
  2. 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
  3. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/E4KZVV/text.yaml