C and C++ Binaries


IO.INJ.FMT : Format String Injection

Summary

A potentially-tainted value is used in a format string argument.

This class is a strict subset of Format String.

Properties

Class Name Format String Injection
Significance security
Mnemonic IO.INJ.FMT
Categories
MisraC2023 MisraC2023:D.4.14 The validity of values received from external sources shall be checked
Misra2012 Misra2012:D.4.14 The validity of values received from external sources shall be checked
AUTOSARC++14 AUTOSARC++14:A27-0-1 Inputs from independent components shall be validated.
CWE CWE:134 Use of Externally-Controlled Format String
TS17961 TS17961:5.23-usrfmt 5.23. Including tainted or out-of-domain input in a format string
CERT-C CERT-C:FIO30-C Exclude user input from format strings
  CERT-C:FIO47-C Use valid format strings
  CERT-C:STR02-C Sanitize data passed to complex subsystems
DISA-6r1 DISA-6r1:V-222606 The application must validate all input.
  DISA-6r1:V-222609 The application must not be subject to input handling vulnerabilities.
  DISA-6r1:V-222612 The application must not be vulnerable to overflow attacks.
DISA-5r3 DISA-5r3:V-70265 The application must validate all input.
  DISA-5r3:V-70271 The application must not be subject to input handling vulnerabilities.
  DISA-5r3:V-70277 The application must not be vulnerable to overflow attacks.
DISA-4r3 DISA-4r3:V-70265 The application must validate all input.
  DISA-4r3:V-70271 The application must not be subject to input handling vulnerabilities.
  DISA-4r3:V-70277 The application must not be vulnerable to overflow attacks.
DISA-3r10 DISA-3r10:V-6164 The designer will ensure the application validates all input.
  DISA-3r10:V-16809 The designer will ensure the application does not contain format string vulnerabilities.
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Format String Injection"

Example

#include <stdio.h>

void tainted_input_as_fmt(void){
    char greeting[18] = "hello";
    char buf[12];
    printf("what is your name?\n");
    if (!fgets(buf, 12, stdin)) return;
    printf(buf);      /* 'Format String Injection' warning issued here
                       * - first argument to printf() has file taint
                       * ('Format String' warning also issued here)
                       */
}

void tainted_input_with_fmt(void){
    char greeting[18] = "hello";
    char buf[12];
    printf("what is your name?\n");
    if (!fgets(buf, 12, stdin)) return;
    printf("%s", buf);                /* ok: first argument to printf() is a string literal */
}

Notes

Untrusted or omitted format strings may not cause any problems during normal running of software, but they nonetheless represent a security vulnerability. In the example shown no problems will arise so long as the string read from standard input is actually a person's name, but an attacker might instead provide the input "%x %x %x %x" (a format string, but not from a trusted source). There are no unsigned integer (%x) arguments provided in this call to printf, but because printf can take a variable number of arguments this does not cause a problem. Instead the program simply computes the stack offset where additional arguments would have been located, had there been any, and prints four unsigned integers starting from that position. Because there were no additional arguments those stack locations will contain other, potentially sensitive data that has now been revealed to the attacker.

In the worst case format string attacks can crash the program, cause malicious code to be executed or allow the attacker to gain root privileges.

Triggers

CodeSonar ships with library models that allow it to recognize a large number of functions that take a format string parameter. Some examples are shown in the table below. If one of these functions is called with a tainted value in the format string parameter position, a warning will be issued.

If you have created a custom library model for some function f() in terms of one of these existing models, calls to f() will also be capable of triggering Format String Injection warnings.

Functions that can trigger warnings include...
Apache Portable Runtime (APR) apr_psprintf(), apr_pvsprintf()
gcc Builtins __builtin___fprintf_chk(), __builtin_snprintf()
libc fprintf(), syslog()
Win32 StringCchVPrintfA(), StringCchVPrintfW(), vsprintf_s()

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.