The exec()
method of the java.lang.Runtime
class and the related ProcessBuilder.start()
method can be used to invoke external programs. While running, these programs are represented by a java.lang.Process
object. This process contains an input stream, output stream, and error stream. Because the Process
object allows a Java program to communicate with its external program, the process's input stream is an OutputStream
object, accessible by the Process.getOutputStream()
method. Likewise, the process's output stream and error streams are both represented by InputStream
objects, accessible by the Process.getInputStream()
and Process.getErrorStream()
methods.
These processes may require input to be sent to their input stream, and they may also produce output on their output stream, their error stream, or both. Incorrect handling of such external programs can cause unexpected exceptions, denial of service (DoS), and other security problems.
A process that tries to read input on an empty input stream will block until input is supplied. Consequently, input must be supplied when invoking such a process.
Output from an external process can exhaust the available buffer reserved for its output or error stream. When this occurs, the Java program can block the external process as well, preventing any forward progress for both the Java program and the external process. Note that many platforms limit the buffer size available for output streams. Consequently, when invoking an external process, if the process sends any data to its output stream, the output stream must be emptied. Similarly, if the process sends any data to its error stream, the error stream must also be emptiedThis recommendation discusses several issues resulting from the improper use of the exec()
method. Similarly the is also prone to misuse.
Noncompliant Code Example (exitValue()
)
This noncompliant code example invokes notemaker
, a hypothetical cross-platform notepad application using the external command notemaker
. The notemaker
application does not read its input stream but sends output to both its output stream and error stream.
This noncompliant code example invokes notemaker
application, using the exec()
method, which returns an object of a subclass of the abstract
class java.lang.Process
Process
object. The exitValue()
method returns the exit value for processes that have terminated; , but it throws an IllegalThreadStateException
when invoked on an active process. Because this noncompliant example program fails to wait for the notemaker
process to terminate, the call to exitValue()
is likely to throw an {IllegalThreadStateException
}}.
Code Block | ||
---|---|---|
| ||
public class Exec {
public static void main(String args[]) throws IOException {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("notemaker");
int exitVal = proc.exitValue();
}
}
|
Noncompliant Code Example (waitFor()
)
In this noncompliant code example, the waitFor()
method blocks the calling thread until the invoked notemaker
process terminates. This approach prevents the IllegalThreadStateException
seen in from the previous example. However, the example program may experience an arbitrary delay before termination. First, the invoked notemaker process could legitimately require lengthy execution before completion. Although this possibility can present difficulties in practice, it is irrelevant for the purposes of this guideline. Second, output Output from the notemaker
process can exhaust the available buffer for the standard output or standard error stream . When this occursbecause neither stream is read while waiting for the process to complete. If either buffer becomes full, it can block the notemaker
process as well, preventing all forward progress for both processes. Note that many platforms limit the buffer size available for the standard output streamsthe notemaker
process and the Java program.
Code Block | ||
---|---|---|
| ||
public class Exec { public static void main(String args[]) throws IOException, InterruptedException { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("notemaker"); int exitVal = proc.waitFor(); } } |
Compliant Solution (1)
Noncompliant Code Example (Process Output Stream)
This noncompliant code example properly empties the process's output stream, thereby preventing the output stream buffer from becoming full and blocking. However, it ignores the process's error stream, which can also fill and cause the process to blockAn partial solution would be to exhaust the output and the stderr
streams before waiting for the invoked process. A better approach is to empty both the stderr
stream and the output stream, as shown in this compliant solution. The example code, however, fails to process any arguments passed to the external program (notemaker
) and exits with an OS-specific non-zero exit code. These limitations must also be addressed in production code.
Code Block | ||
---|---|---|
| ||
public class Exec { public static void main(String args[]) throws IOException, InterruptedException { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("notemaker"); InputStream is = proc.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr)int c; String line; while ((linec = bris.readLineread()) != null-1) { System.out.println(line); // Prints the error linesprint((char) c); } int exitVal = proc.waitFor(); } } |
Compliant Solution (redirectErrorStream(
...
)
)
This compliant solution spawns a command interpreter that executes the user supplied command. It reads the output from the external process, and write it to a separate OutputStream
redirects the process's error stream to its output stream. Consequently, the program can empty the single output stream without fear of blockage.
Code Block | ||
---|---|---|
| ||
public class Exec extends Thread { InputStreampublic is; static void main(String type;args[]) OutputStream os; Exec(InputStream is, String type) { this(is, type, null); } Exec(InputStream is, String type, OutputStream redirect) { throws IOException, InterruptedException this.is{ = is; ProcessBuilder pb this.type= = typenew ProcessBuilder("notemaker"); this.ospb = redirectpb.redirectErrorStream(true); } Process publicproc void= runpb.start() {; tryInputStream { is = proc.getInputStream(); PrintWriter pw = nullint c; while ((c if= is.read(os)) != null-1) { pw = new PrintWriter(osSystem.out.print((char) c); } InputStreamReader isr = new InputStreamReader(isint exitVal = proc.waitFor(); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null} } |
Compliant Solution (Process Output Stream and Error Stream)
This compliant solution spawns two threads to consume the process's output stream and error stream. Consequently, the process cannot block indefinitely on those streams.
When the output and error streams are handled separately, they must be emptied independently. Failure to do so can cause the program to block indefinitely.
Code Block | ||
---|---|---|
| ||
class StreamGobbler implements Runnable { private final InputStream is; private final PrintStream os; StreamGobbler(InputStream is, PrintStream os) { if (pw != null) {this.is = is; this.os pw.println(line)= os; } public pw.flushvoid run(); { try }{ System.out.println(type + ">" + line)int c; } if (pwwhile ((c = is.read()) != null-1) { pwos.flushprint((char) c); } } catch (IOException ioex) { /* Forward to handler */ // Handle error } } } public class ExecMeExec { public static void main(String[] args) { // ... perform command argument check ... try { FileOutputStream fos = new FileOutputStream("c:\\output.txt"); throws IOException, InterruptedException { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("notemaker"); // Any error message? Thread errorGobbler Exec errorGobbler = new ExecThread(new StreamGobbler(proc.getErrorStream(), "ERROR"System.err)); // Any output? Thread outputGobbler Exec outputGobbler = new ExecThread(new StreamGobbler(proc.getInputStream(), "OUTPUT", fosSystem.out)); errorGobbler.start(); outputGobbler.start(); // Any error? int exitVal = proc.waitFor(); errorGobbler.join(); // Handle condition where the outputGobbler.join(); // process ends before the threads finish fos.flush(); fos.close(); } catch (Throwable t) { /* forward to handler */ } } } |
When the output and error streams are handled separately, they must be drained concurrently. Failure to do so can cause the program to block indefinitely.
Compliant Solution (3) (Windows)
This compliant solution (based on the Sun forums query Runtime.exec hangs even If I drain output), uses a ProcessBuilder
to simplify the handling mechanism by merging the error and output streams. The readToPrompt()
method reads the output of the invoked process; details of its implementation necessarily depend on the exact formatting of the command prompt used by the platform's command interpreter.
Code Block | ||
---|---|---|
| ||
public class Cmd {
public static void main(String[] args) throws IOException {
ProcessBuilder pb = new ProcessBuilder("cmd");
pb = pb.redirectErrorStream(true);
Process p = pb.start();
InputStream is = p.getInputStream();
OutputStream os = p.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
readToPrompt(is);
pw.println("dir");
readToPrompt(is);
}
private static void readToPrompt(InputStream is) throws IOException {
String s = "";
for (;;) {
int i = is.read();
if (i < 0) {
System.out.println();
System.out.println("EOF");
System.exit(0);
}
char c = (char)i; // Safe
s += c;
if (s.endsWith("\r\n") {
System.out.print(s);
s = "";
}
// Detects prompt, to break out
if (c == '>' && s.length() > 2 && s.charAt(1) == ':') {
System.out.print(s);
break;
}
}
}
}
|
Risk Assessment
Misuse of the exec()
method can result in runtime exceptions and in denial of service vulnerabilities.
Exceptions
FIO07-J-EX0: Failure to supply input to a process that never reads input from its input stream is harmless and can be beneficial. Failure to empty the output or error streams of a process that never sends output to its output or error streams is similarly harmless or even beneficial. Consequently, programs are permitted to ignore the input, output, or error streams of processes that are guaranteed not to use those streams.
Risk Assessment
Failure to properly manage the I/O streams of external processes can result in runtime exceptions and in DoS vulnerabilities.
Rule |
---|
Severity | Likelihood | Remediation Cost | Priority | Level |
---|
FIO07-J |
Low |
Probable |
Medium | P4 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Parasoft Jtest |
| CERT.FIO07.EXEC | Do not use 'Runtime.exec()' |
Related Vulnerabilities
Bibliography
...
[API 2014] | |
Pitfall 1 |
...
[API 06|AA. Bibliography#API 06]\] method [exec()|http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html#exec(java.lang.String)] \[[Daconta 00|AA. Bibliography#Daconta 00]\] \[[Daconta 03|AA. Bibliography#Daconta 03]\] Pitfall 1FIO09-J. Exclude user input from format strings Input Output (FIO) FIO11-J. Do not attempt to read raw binary data as character data