External programs are commonly invoked to perform a function required by the overall system. This practice is a form of reuse and might even be considered a crude form of component-based software engineering. Command and argument injection vulnerabilities occur when an application fails to sanitize untrusted input and uses it in the execution of external programs.
Every Java application has a single instance of class Runtime
that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the Runtime.getRuntime()
method. The semantics of Runtime.exec()
are poorly defined, so it 's is best not to rely on its behavior any more than necessary, but typically it invokes the command directly without a shell. If you want a shell, you can use /bin/sh -c
on POSIX or cmd.exe
on Windows. The variants of exec()
that take the command line as a single string split it using a StringTokenizer
. On Windows, these tokens are concatenated back into a single argument string before being executed.
...
This noncompliant code example provides a directory listing using the dir
command. It is implemented using Runtime.exec()
to invoke the Windows dir
command.
Code Block | ||
---|---|---|
| ||
class DirList {
public static void main(String[] args) throws Exception {
String dir = System.getProperty("dir");
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("cmd.exe /C dir " + dir);
int result = proc.waitFor();
if (result != 0) {
System.out.println("process error: " + result);
}
InputStream in = (result == 0) ? proc.getInputStream() :
proc.getErrorStream();
int c;
while ((c = in.read()) != -1) {
System.out.print((char) c);
}
}
}
|
Because Runtime.exec()
receives unsanitized data originating from the environment, this code is susceptible to a command injection attack.
An attacker can exploit this program using the following command:
...
which first attempts to list a nonexistent dummy
folder , and then prints bad
to the console.
...
This noncompliant code example provides the same functionality but uses the POSIX ls
command. The only difference from the Windows version is the argument passed to Runtime.exec()
.
Code Block | ||
---|---|---|
| ||
class DirList {
public static void main(String[] args) throws Exception {
String dir = System.getProperty("dir");
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(new String[] {"sh", "-c", "ls " + dir});
int result = proc.waitFor();
if (result != 0) {
System.out.println("process error: " + result);
}
InputStream in = (result == 0) ? proc.getInputStream() :
proc.getErrorStream();
int c;
while ((c = in.read()) != -1) {
System.out.print((char) c);
}
}
}
|
...
Compliant Solution (Sanitization)
This compliant solution solution sanitizes the untrusted user input by permitting only a small group of whitelisted characters in the argument that will be passed to Runtime.exec()
; all other characters are excluded.
...
Code Block | ||
---|---|---|
| ||
import java.io.File; class DirList { public static void main(String[] args) throws Exception { File dir = new File(System.getProperty("dir")); if (!dir.isDirectory()) { System.out.println("Not a directory"); } else { for (String file : dir.list()) { System.out.println(file); } } } } |
Risk Assessment
Passing untrusted, unsanitized data to the Runtime.exec()
method can result in command and argument injection attacks.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
IDS07-J | High | Probable | Medium | P12 | L1 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
The Checker Framework |
| Tainting Checker | Trust and security errors (see Chapter 8) | ||||||
CodeSonar |
| JAVA.IO.INJ.COMMAND | Command Injection (Java) | ||||||
Coverity | 7.5 | OS_CMD_INJECTION | Implemented | ||||||
Parasoft Jtest |
| CERT.IDS07.EXEC | Do not use 'Runtime.exec()' | ||||||
SonarQube |
| OS commands should not be vulnerable to injection attacks |
Related Vulnerabilities
CVE-2010-0886 | |
CVE-2010-1826 | Command injection in |
T-472 | Mac OS X Java Command Injection Flaw in |
Related Guidelines
ENV03-C. Sanitize the environment when invoking external programs |
ENV03-CPP. Sanitize the environment when invoking external programs |
VOID ENV02-CPP. Do not call system() if you do not need a command processor |
SEI CERT Perl |
Coding Standard | IDS34-PL. Do not pass untrusted, unsanitized data to a command interpreter |
Injection [RST] | |
CWE-78, Improper |
Neutralization of Special Elements Used in an OS |
Command ("OS |
Command Injection") |
Android Implementation Details
Runtime.exec()
can be called from Android apps to execute operating system commands.
Bibliography
Chapter 5, "Handling Input," section "Command Injection" | |
[OWASP 2005] | A Guide to Building Secure Web Applications and Web Services |
[Permissions 2008] | Permissions in the Java™ SE 6 Development Kit (JDK) |
[Seacord 2015] |
...