...
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 best not to rely on its behavior any more than necessary. It will invoke , 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 is a specific instance of the rule IDS00-J. Sanitize untrusted data passed across a trust boundary. 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.
...
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); } } } |
...
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); } } } |
...
This compliant solution sanitizes the untrusted user input by permitting only allowing a small number group of whitelisted characters to in the argument that will be passed as part of the argument to Runtime.exec()
; all other characters are excluded.
Code Block | ||
---|---|---|
| ||
// ... if (!Pattern.matches("[0-9A-Za-z@.]+", dir)) { // Handle error } // ... |
Although this is a compliant solution, the this sanitization method approach rejects valid directories. Also, because the command interpreter invoked is system dependent, it is difficult to establish that this solution will not allow prevents command injection injections on every possible platform in on which a Java program might run.
...
This compliant solution prevents command injection by passing only passing trusted strings to Runtime.exec()
. While the user has control over which string is used, the user 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 available permitted directories from a properties file into a java.util.Properties
object. Alternately, the switch statement can operate on an enumerated type.
Compliant Solution (Avoid Runtime.exec()
)
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9d91ec20df6a8e28-dfd08d02-47094dbc-a4b3b2b0-b348638672e18b79ec802d59"><ac:plain-text-body><![CDATA[ | [CVE-2010-0886] | [Sun Java Web Start Plugin Command Line Argument Injection | http://www.securitytube.net/video/1465] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b9549e29ef0d2a2e-d18d4e21-4ebd4281-969cae6e-a30931ec832a917e564c1bc3"><ac:plain-text-body><![CDATA[ | [CVE-2010-1826] | [Command injection in | http://securitytracker.com/id/1024617] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0076fbc2e1d249fd-89a131ac-45f64a9b-b623b32e-bcf6d609354932238828c322"><ac:plain-text-body><![CDATA[ | [T-472] | [Mac OS X Java Command Injection Flaw in | http://www.doecirc.energy.gov/bulletins/t-472.shtml] | ]]></ac:plain-text-body></ac:structured-macro> |
...
ENV03-C. Sanitize the environment when invoking external programs | ||||
| ENV04-C. Do not call system() if you do not need a command processor | |||
ENV03-CPP. Sanitize the environment when invoking external programs | ||||
| ENV04-CPP. Do not call system() if you do not need a command processor | |||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9c20a8a53e708f51-afa4ac80-46414832-ada3ad9a-9e9f8c14fe583d2c70938063"><ac:plain-text-body><![CDATA[ | [ISO/IEC TR 24772:2010 | http://www.aitcnet.org/isai/] | " Injection [RST] " | ]]></ac:plain-text-body></ac:structured-macro> |
CWE-78, ". Improper Neutralization neutralization of Special Elements special elements used in an OS Command command ('OS Command Injection')" "OS command injection") |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9a82cb99f318bb9f-42f72cbb-439e4b8d-b342bcea-1c1e47e44330a2078c6e4337"><ac:plain-text-body><![CDATA[ | [[Chess 2007 | AA. Bibliography#Chess 07]] | Chapter 5: , Handling Input, "Command Injection"]]></ac:plain-text-body></ac:structured-macro> | ||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4af2917dffdf60bf-300ee404-48364eab-9b7abd0f-7c1706d2a8738d4cfc6b0012"><ac:plain-text-body><![CDATA[ | [[OWASP 2005 | AA. Bibliography#OWASP 05]] [Reviewing Code for OS Injectionhttp://www.owasp.org/index.php/Reviewing_Code_for_OS_Injection | ]]]></ac:plain-text-body></ac:structured-macro> | ||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="13d86c9e85cc362d-be3db6c1-42c646d9-abc8bf88-37b8a6d95bbe9996b3b624e6"><ac:plain-text-body><![CDATA[ | [[Permissions 2008 | AA. Bibliography#Permissions 08]] | [Permissions in the Java™ SE 6 Development Kit (JDK) | http://java.sun.com/javase/6/docs/technotes/guides/security/permissions.html] , Sun Microsystems, Inc. (2008) | ]]></ac:plain-text-body></ac:structured-macro> |
...