Do not operate on unvalidated or untrusted data (also known as tainted data) in a doPrivileged()
block. An attacker can supply malicious input that could result in privilege escalation attacks. Appropriate mitigations include hard coding values rather than accepting arguments (when appropriate) , and validating or validating/ sanitizing data before the privileged operations .This rule concerns untrusted information entering a doPrivileged()
block. For prevention of sensitive information escaping from a doPrivileged () block, see SEC00IDS00-J. Do not allow doPrivileged() blocks to leak sensitive information outside Sanitize untrusted data passed across a trust boundary).
Noncompliant Code Example
This noncompliant code example accepts a tainted filename
path or file name as an argument. An attacker can supply the path name of a sensitive password file, consequently allowing an unprivileged user to access a protected fileaccess a protected file by supplying it's pathname as an argument to this method.
Code Block | ||
---|---|---|
| ||
private void privilegedMethod(final String filename) throws FileNotFoundException { try { FileInputStream fis = (FileInputStream) AccessController.doPrivileged( new PrivilegedExceptionAction() { public FileInputStream run() throws FileNotFoundException { return new FileInputStream(filename); } } ); // do something with the file and then close it } catch (PrivilegedActionException e) { // forward to handler and log } } |
...
This compliant solution invokes a sanitization method (cleanAFilenameAndPath
) that can distinguish acceptable inputs from the cleanAFilenameAndPath()
) sanitization method to disallow malicious inputs. Successful operation of the sanitization method indicates that the input is acceptable, and the doPrivileged
block can be executed.
...
One potential drawback of this approach is that effective sanitization methods can be difficult to write. A benefit of this approach is that it works well in combination with taint analysis (see the Automated Detection , belowsection for this rule). For more information on how to securely validate and sanitize a file, perform secure file operations see FIO00-J. Do not operate on files in shared directories.
...
Sanitization of tainted inputs always carries the risk that the data is not fully sanitized. Both file and path name equivalence and directory traversal are common examples of vulnerabilities arising from the improper sanitization of path and file name inputs (see IDS02-J. Canonicalize path names before validating them). A design that requires an unprivileged user to access an arbitrary, protected file (or other resource) is always suspect. Consider alternatives such as using a hardcodeed hard coded resource name or permitting the user to select only from a list of options that are indirectly mapped to the resource names.
This compliant solution both explicitly hardcodes hard codes the name of the file and also confines the variables used in the privileged block to the same method. This ensures that no malicious file can be loaded by exploiting the privileges of the corresponding codeprivileged method.
Code Block | ||
---|---|---|
| ||
static final String FILEPATH = "/path/to/protected/file/fn.ext"; private void privilegedMethod() throws FileNotFoundException { try { FileInputStream fis = (FileInputStream) AccessController.doPrivileged( new PrivilegedExceptionAction() { public FileInputStream run() throws FileNotFoundException { return new FileInputStream(FILEPATH); } } ); // do something with the file and then close it } catch (PrivilegedActionException e) { // forward to handler and log } } |
...
Tools that support taint analysis enable assurance of code usage that is substantially similar to the first compliant solution. Typical taint analyses assume that a method or methods exist(s) that can "clean" cleanse potentially tainted inputs, providing untainted outputs (or appropriate errors). The taint analysis then ensures that only untainted data is used inside the doPrivileged
block. Note that the static analyses must necessarily assume that the cleaning cleansing methods are always successful; in reality this may not be the case.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2a4c4f138e14d79b-2393cbdf-428a423a-af558930-a9a67542bee51c6940fb1c77"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | [method doPrivileged() | http://java.sun.com/javase/6/docs/api/java/security/AccessController.html#doPrivileged(java.security.PrivilegedAction)] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="df6ff6daf13f0139-b9e1b08b-484d4be9-ac26bf69-54623945f5c11a0915ccd18d"><ac:plain-text-body><![CDATA[ | [[Gong 2003 | AA. Bibliography#Gong 03]] | Sections 6.4, "AccessController" | ]]></ac:plain-text-body></ac:structured-macro> | |
| 9.5 "Privileged Code" | ||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="61ef70d45ed9d4c4-7f5c620e-4f404d35-84709531-418cef3b2f02ef229f93e0cd"><ac:plain-text-body><![CDATA[ | [[Jovanovic 2006 | AA. Bibliography#Jovanovic 06]] | "Pixy: A Static Analysis Tool for Detecting Web Application Vulnerabilities" | ]]></ac:plain-text-body></ac:structured-macro> |
...