Java code that deals with input, for example Scanner
, often buffers its underlying InputStream
.It is possible to create multiple wrappers around an InputStream
that buffer the input from that InputStream
. Such programs behave significantly differently depending on whether the InputStream
does or does not allow for read ahead. An adversary input classes such as Scanner
and BufferedInputStream
facilitate fast, nonblocking I/O by buffering an underlying input stream. Programs can create multiple wrappers on an InputStream
. Programs that use multiple wrappers around a single input stream, however, can behave unpredictably depending on whether the wrappers allow look-ahead. An attacker can exploit this difference in behavior by, for example, by redirecting the InputStream
, which could lead to exploitable behavior.
Although the Java standard does not specifically mention this behavior, code compiled with javac
and run with the java
command exhibits this behavior on Java 1.5.0.
System.in
(from a file) or by using the System.setIn()
method to redirect System.in
. In general, any input stream that supports nonblocking buffered I/O is susceptible to this form of misuse.
An input stream must not have more than one buffered wrapper. Instead, Do not create multiple wrappers that buffer their input on an InputStream
; create and use only one wrapper per input stream, either by passing it as an argument to the methods that need it or centralizing its use in a single placeby declaring it as a class variable.
Likewise, an output stream must not have more than one buffered wrapper because multiple wrappers can cause multiple output strings to be output in an unexpected order. For example, the javax.servlet.ServletResponse
allows for the creation of a PrintWriter
or an OutputStream
to hold the response generated by a web servlet. But only one or the other should be used, not both.
Noncompliant Code Example
This noncompliant code example creates multiple BufferedInputStreams
BufferedInputStream
wrappers on System.in
, because each time even though there is only one declaration of a BufferedInputStream
. The getChar()
is called it makes method creates a new BufferedInputStream
. Note that it does not matter that the old BufferedInputStream
will have "expired" by the time the new one is created. Note that while this code uses a BufferedInputStream
to illustrate that each time it is called. Data that is read from the underlying stream and placed in the buffer during execution of one call cannot be replaced in the underlying stream so that a second call has access to it. Consequently, data that remains in the buffer at the end of a particular execution of getChar()
is lost. Although this noncompliant code example uses a BufferedInputStream
, any buffered wrapper is unsafe, it ; this condition is also exploitable if a Scanner
is used instead.
Implementation Details
This program was compiled with the command javac InputLibrary.java
on a system with Java 1.5.0. When run from the command line with java InputLibrary
, the program will successfully take two characters as input and print them out. However, when run with java InputLibrary < input
, where input
is a file that contains the exact same input, the program prints "ERROR" because the second call to getChar()
finds no characters to read.when using a Scanner
, for example.
Code Block | ||
---|---|---|
| ||
import java.io.BufferedInputStream; import java.io.EOFException; public final class InputLibrary { public static char getChar() throws EOFException, IOException { BufferedInputStream in = new BufferedInputStream(System.in); // Wrapper int input = in.read(); if (input == -1) { throw new EOFException(); } return (char)input; //okay// Down casting is permitted because InputStream guarantees read() fits in a byte in range // 0..255 if it is not -1 return (char) input; } public static void main(String[] args) { try { // Either redirect input from the console or use // System.setIn(new FileInputStream("input.dat")); System.out.print("Enter first initial: "); char first = getChar(); System.out.println("Your first initial is " + first); System.out.print("Enter last initial: "); char last = getChar(); System.out.println("Your last initial is " + last); } catch (EOFException e) { System.outerr.println("ERROR"); } // Forward to handler } } |
Compliant Solution
Create and use only a single BufferedInputStream
on System.in
. This code example stores the BufferedInputStream
as a class variable so all methods can access it. However, if a program were to use this library in conjunction with other input from a user that also needs some buffered wrapper on System.in
, the library would need to be modified so that all code uses the same buffered wrapper instead of creating separate ones.
Implementation Details
This program was compiled with the command javac InputLibrary.java
on a system with Java 1.5.0. When run from the command line with java InputLibrary
, the program will successfully take two characters as input and print them out. Also, when run with java InputLibrary < input
, where input
is a file that contains the exact same input, the program will successfully take two characters as input and print them out.
catch (IOException e) {
System.err.println("ERROR");
// Forward to handler
}
}
}
|
Implementation Details (POSIX)
When compiled under Java 1.6.0 and run from the command line, this program successfully takes two characters as input and prints them out. However, when run with a file redirected to standard input, the program throws EOFException
because the second call to getChar()
finds no characters to read upon encountering the end of the stream.
It may appear that the mark()
and reset()
methods of BufferedInputStream
could be used to replace the read bytes. However, these methods provide look-ahead by operating on the internal buffers of the BufferedInputStream
rather than by operating directly on the underlying stream. Because the example code creates a new BufferedInputStream
on each call to getchar()
, the internal buffers of the previous BufferedInputStream
are lost.
Compliant Solution (Class Variable)
Create and use only a single BufferedInputStream
on System.in
. This compliant solution ensures that all methods can access the BufferedInputStream
by declaring it as a class variable:
Code Block | ||
---|---|---|
| ||
Code Block | ||
| ||
import java.io.BufferedInputStream; import java.io.EOFException; public final class InputLibrary { private static BufferedInputStream in = new BufferedInputStream(System.in); public static char getChar() throws EOFException, IOException { int input = in.read(); if (input == -1) { throw new EOFException(); } in.skip(1); // This statement now necessaryis to goadvance to the next line. //the The Noncompliantnoncompliant code example deceptively worked without it return (char)input; //okay becauseappeared InputStreamto guaranteeswork read()without fitsit (in a byte if it is not -1some cases). return (char) input; } public static void main(String[] args) { try { System.out.print("Enter first initial: "); char first = getChar(); System.out.println("Your first initial is " + first); System.out.print("Enter last initial: "); char last = getChar(); System.out.println("Your last initial is " + last); } catch (EOFException e) { System.outerr.println("ERROR"); // Forward to handler } catch (IOException e) { System.err.println("ERROR"); // Forward to handler } } } |
Implementation Details (POSIX)
When compiled under Java 1.6.0 and run from the command line, this program successfully takes two characters as input and prints them out. Unlike the noncompliant code example, this program also produces correct output when run with a file redirected to standard input.
Compliant Solution (Accessible Class Variable)
This compliant solution uses both System.in
and the InputLibrary
class, which creates a buffered wrapper around System.in
. Because the InputLibrary
class and the remainder of the program must share a single buffered wrapper, the InputLibrary
class must export a reference to that wrapper. Code outside the InputLibrary
class must use the exported wrapper rather than create and use its own additional buffered wrapper around System.in
.
Code Block | ||
---|---|---|
| ||
public final class InputLibrary { private static BufferedInputStream in = new BufferedInputStream(System.in); static BufferedInputStream getBufferedWrapper() { return in; } // ... Other methods } // Some code that requires user input from System.in class AppCode { private static BufferedInputStream in; AppCode() { in = InputLibrary.getBufferedWrapper(); } // ... Other methods } |
Note that reading from a stream is not a thread-safe operation by default; consequently, this compliant solution may be inappropriate in multithreaded environments. In such cases, explicit synchronization is required.
Risk Assessment
Creating multiple buffered wrappers on around an InputStream
can cause unexpected program behavior when the InputStream
is re-directedredirected.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|
FIO06-J |
Low |
Unlikely |
Medium | P2 | L3 |
Automated Detection
TODO
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
Sound automated detection of this vulnerability is not feasible in the general case. Heuristic approaches may be useful.
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Parasoft Jtest |
| CERT.FIO06.MULBUF | Do not create multiple buffered wrappers on a single byte or character stream |
Bibliography
...
\[[API 06|AA. Java References#API 06]\] [method read|http://java.sun.com/javase/6/docs/api/java/io/InputStream.html#read()]
\[[API 06|AA. Java References#API 06]\] [class BufferedInputStream|http://java.sun.com/javase/6/docs/api/java/io/BufferedInputStream.html] Wiki Markup