Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Interpretation of Java format strings is stricter than that in languages such as C. The implementations in the standard libraries throw appropriate exceptions when any conversion argument fails to match the corresponding flag. This approach reduces opportunities for malicious exploits. Nevertheless, malicious user input can exploit format strings and can cause information leaks or denial of service. As a result, strings from an untrusted source shall not be incorporated into format strings.

Noncompliant Code Example

This noncompliant code example demonstrates an information leak issue. It accepts a credit card expiration date as an input argument and uses it within the format string.

...

In the absence of proper input validation, an attacker can determine the date against which the input is being verified, perhaps by supplying an input that includes one of the format string arguments %1$tm, %1$te, or %1$tY.

Compliant Solution

This compliant solution ensures that user generated input is excluded from format strings.

Code Block
bgColor#ccccff
class Format {
  static Calendar c = new GregorianCalendar(1995, MAY, 23);
  public static void main(String[] args) {  
    // args[0] is the credit card expiration date
    // Perform comparison with c, if it doesn't match print the following line
    System.out.printf("The input did not match! HINT: It was issued on %1$terd of some month", c);
  }
}

Risk Assessment

Allowing user input to taint a format string may cause information leaks or denial of service.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

IDS20-J

medium

unlikely

medium

P4

L3

Automated Detection

Static analysis tools that perform taint analysis can diagnose some violations of this rule.

Other Languages

This rule appears in the C Secure Coding Standard as FIO30-C. Exclude user input from format strings.

This rule appears in the C++ Secure Coding Standard as FIO30-CPP. Exclude user input from format strings.

Related Guidelines

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f929747a2cdfb4e1-3be1d91a-43a943c1-9100bcae-c25c74aa1e8b02840196e917"><ac:plain-text-body><![CDATA[

[[MITRE 2009

AA. Bibliography#MITRE 09]]

[CWE-134

http://cwe.mitre.org/data/definitions/134.html] "Uncontrolled Format String"

]]></ac:plain-text-body></ac:structured-macro>

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3cfa97cced728c6d-38287456-4e6743f9-bf85bc31-72ad98a3671f9d783e5953f7"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

[Class Formatter

http://java.sun.com/javase/6/docs/api/java/util/Formatter.html]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4d09696934995f7e-58a87a7a-4dc44ff0-bce68211-07e706b1334ec70ee25df55a"><ac:plain-text-body><![CDATA[

[[Seacord 2005

AA. Bibliography#Seacord 05]]

Chapter 6, Formatted Output

]]></ac:plain-text-body></ac:structured-macro>

...