Interpretation of Java format strings is stricter than that in languages such as C. The implementations in the standard libraries throw appropriate exceptions The java.io
package includes a PrintStream
class that has two equivalent formatting methods: format()
and printf()
. System.out
and System.err
are PrintStream
objects, allowing PrintStream
methods to be invoked on the standard output and error streams. The risks from using these methods are not as high as from using similar functions in C or C++ [Seacord 2013]. The standard library implementations throw an exception 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 format specifier. Although throwing an exception helps mitigate against exploits, if untrusted data is incorporated into a format string, it can result in an information leak or allow a denial-of-service attack. Consequently, unsanitized input from an untrusted source must never 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 leaks information about a user's credit card. It incorporates untrusted data in a format string.
Code Block | ||
---|---|---|
| ||
class Format { static Calendar c = new GregorianCalendar(1995, GregorianCalendar.MAY, 23); public static void main(String[] args) { // args[0] should iscontain the credit card expiration date // args[0] canbut might contain either %1$tm, %1$te or %1$tY asformat malicious argumentsspecifiers // First argument prints 05 (May), second prints 23 (day) and third prints 1995 (year) // Perform comparison with c, if it doesn't match print the following line System.out.printf(System.out.format( args[0] + " did not match! HINT: It was issued on %1$terd of some month", c ); } } |
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 string that includes one of the format string arguments %1$tm
, %1$te
, or %1$tY
format specifiers. In this example, these format specifiers print 05 (May), 23 (day), and 1995 (year), respectively.
Compliant Solution
This compliant solution ensures that excludes untrusted user generated input is excluded from format strings.from the format string. Although arg[0]
still may contain one or more format specifiers, they are now rendered inert.
Code Block | ||
---|---|---|
| ||
class Format { static Calendar c = new GregorianCalendar(1995, GregorianCalendar.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 format( "%s did not match! HINT: It was issued on %1$terd%terd of some month", args[0], c ); } } |
Risk Assessment
Allowing user input to taint Incorporating untrusted data in a format string may cause result in information leaks or allow a denial-of-service attack.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|
IDS06-J |
Medium |
Unlikely |
Medium | P4 | L3 |
Automated Detection
Static analysis tools that perform taint analysis can diagnose some violations of this rule.
Other Languages
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
The Checker Framework |
| Tainting Checker | Trust and security errors (see Chapter 8) | ||||||
Parasoft Jtest |
| CERT.IDS06.VAFS | Ensure the correct number of arguments for varargs methods with format strings | ||||||
Klocwork |
| SV.EXEC SV.EXEC.DIR SV.EXEC.ENV SV.EXEC.LOCAL SV.EXEC.PATH | Implemented |
Related Guidelines
...
...
...
...
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="e790cb49-55ca-4ba4-9355-6847ffe218e7"><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="a033342a-8037-47ed-b2c3-6c82f34adee4"><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="5b85f307-0816-4116-aae5-4fc0e6758df5"><ac:plain-text-body><![CDATA[ | [[Seacord 2005 | AA. Bibliography#Seacord 05]] | Chapter 6, Formatted Output | ]]></ac:plain-text-body></ac:structured-macro> |
Injection [RST] | |
CWE-134, Uncontrolled Format String |
Bibliography
[API 2006] | |
Chapter 6, "Formatted Output" | |
[Seacord 2015] |
...
IDS08-J. Sanitize untrusted data passed to a regex