The write()
method, defined in the class java.io.OutputStream
, takes an argument of type int
intended to be between 0 and 255. Because a value of type int
could be outside this range, failure to range check can result in the truncation of the higher order bits of the input.
The general contract for the write()
method says that it writes one byte to the output stream. The byte to be written constitutes the eight lower order bits of the argument b
, passed to the write()
method; the 24 high-order bits of b
are ignored (see [[API 2006]] java.io.OutputStream.write()
for more information).
Noncompliant Code Example
This noncompliant code example accepts a value from the user without validating it. Any value that is not in the range of 0 to 255 is truncated. For instance, write(303)
prints /
on ASCII-based systems because the lower order 8 bits of 303 are used while the 24 high-order bits are ignored (303 mod 256 is 47 and /
has ASCII code 47). That is, the result is remainder modulo 256 of the absolute value of the input.
class ConsoleWrite { public static void main(String[] args) { //Any input value > 255 will result in unexpected output System.out.write(Integer.valueOf(args[0].toString())); System.out.flush(); } }
Compliant Solution (Use System.out.print*
Methods)
Use alternative means to output integers, such as the System.out.print*
methods.
class ConsoleWrite { public static void main(String[] args) { System.out.println(args[0]); } }
Compliant Solution (Range-Check Inputs)
Alternatively, perform range checking to be compliant. While this particular compliant solution still fails to display the original out-of-range integer, it behaves well when the corresponding read()
method is used to convert the byte
value back to a value of type int
. This is because it guarantees that the byte
variable will contain representable data.
class FileWrite { public static void main(String[] args) throws NumberFormatException, IOException { FileOutputStream out = new FileOutputStream("output"); //Perform range checking if(Integer.valueOf(args[0]) < 0 || Integer.valueOf(args[0]) > 255) { throw new ArithmeticException("Value is out of range"); } out.write(Integer.valueOf(args[0].toString())); System.out.flush(); } }
When args[0]
contains a value that falls outside the representable range of an int
, the Integer.valueOf()
method throws a NumberFormatException
.
Compliant Solution (Use writeInt()
)
This compliant solution uses the writeInt()
method of the DataOutputStream
class.
class FileWrite { public static void main(String[] args) throws NumberFormatException, IOException { FileOutputStream out = new FileOutputStream("output"); DataOutputStream dos = new DataOutputStream(out); dos.writeInt(Integer.valueOf(args[0].toString())); // close out and dos } }
Risk Assessment
Using the write()
method to output integers writes only the low-order 8 bits of the integers. This truncation can result in unexpected values.
Guideline |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
INT09-J |
low |
unlikely |
medium |
P2 |
L3 |
Automated Detection
Automated detection of all uses of the write()
method is straightforward. Sound determination of whether the truncating behavior is correct is not feasible in the general case. Heuristic checks could be useful.
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this guideline on the CERT website.
Bibliography
[[API 2006]] method write()
[[Harold 1999]]
INT08-J. Provide mechanisms to handle unsigned data when required 06. Integers (INT) 07. Floating Point (FLP)