...
Any string data that originates from outside the program's trust boundary must be sanitized before being executed as a command on the current platform.
Noncompliant Code Example (Windows)
This noncompliant code example provides a directory listing using the dir
command. It is implemented using Runtime.exec()
to invoke the Windows dir
command.
...
which first attempts to list a nonexistent dummy
folder, and then prints bad
to the console.
Noncompliant Code Example (POSIX)
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 |
---|
sh -c 'ls dummy & echo bad' |
Compliant Solution (Sanitization)
This compliant 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.
...
Although it is a compliant solution, this sanitization approach rejects valid directories. Also, because the command interpreter invoked is system dependent, it is difficult to establish that this solution prevents command injections on every platform on which a Java program might run.
Compliant Solution (Restricted User Choice)
This compliant solution prevents command injection by passing only trusted strings to Runtime.exec()
. The user has control over which string is used but cannot provide string data directly to Runtime.exec()
.
...
This solution can quickly become unmanageable if you have many available directories. A more scalable solution is to read all the permitted directories from a properties file into a java.util.Properties
object.
Compliant Solution (Avoid Runtime.exec()
)
When the task performed by executing a system command can be accomplished by some other means, it is almost always advisable to do so. This compliant solution uses the File.list()
method to provide a directory listing, eliminating the possibility of command or argument injection attacks.
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 |
---|---|---|---|
Coverity | 7.5 | OS_CMD_INJECTION | Implemented |
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 | |
CERT Perl Secure 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) |
...