Do not operate on unvalidated or untrusted data (also known as tainted data) in a privileged 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 sanitizing data before the performing privileged operations (see IDS00-J. Sanitize untrusted data passed across a trust boundaryPrevent SQL injection).
Noncompliant Code Example
This noncompliant code example accepts a tainted path or file name as an argument. An attacker can access a protected file by supplying its path name 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); } } ); // doDo something with the file and then close it } catch (PrivilegedActionException e) { // forwardForward to handler and log } } |
Compliant Solution (Input Validation)
This compliant solution invokes the cleanAFilenameAndPath())
sanitization method to disallow sanitize malicious inputs. Successful operation completion of the sanitization method indicates that the input is acceptable and the doPrivileged()
block can be executed.
Code Block | ||
---|---|---|
| ||
private void privilegedMethod(final String filename) throws FileNotFoundException { final String cleanFilename; try { cleanFilename = cleanAFilenameAndPath(filename); } catch (/* exception as per spec of cleanAFileNameAndPath */) { // logLog or forward to handler as appropriate based on specification // of cleanAFilenameAndPath } try { FileInputStream fis = (FileInputStream) AccessController.doPrivileged( new PrivilegedExceptionAction() { public FileInputStream run() throws FileNotFoundException { return new FileInputStream(cleanFilename); } } ); // doDo something with the file and then close it } catch (PrivilegedActionException e) { // forwardForward to handler and log } } |
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 section for this rule). For more information on how to perform secure file operations, see FIO00-J. Do not operate on files in shared directories.
Compliant Solution (Built-in File Name and Path)
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 FIO16-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 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 hard codes the name of the file and also confines the variables used in the privileged block to the same method. This declares the variable as static final
to prevent it from being modified. This technique ensures that no malicious file can be loaded by exploiting the privileged 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); } } ); // doDo something with the file and then close it } catch (PrivilegedActionException e) { // forwardForward to handler and log } } |
Risk Assessment
Allowing tainted inputs in privileged operations can lead to result in privilege escalation attacks.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
SEC01-J |
High |
Likely |
Low | P27 | L1 |
Automated Detection
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 one or more methods exist (s) that can cleanse sanitize 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 cleansing sanitization methods are always successful; , but in reality, this may not be the case.
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
CodeSonar |
| JAVA.IO.PERM.ACCESS | Accessing File in Permissive Mode (Java) | ||||||
Parasoft Jtest |
| CERT.SEC01.PRIVIL | Avoid operating on tainted data in privileged blocks |
Related Guidelines
Incorrect Privilege Assignment |
Least Privilege Violation |
Incorrect Permission Assignment for Critical Resource |
Guideline |
9-3 / ACCESS-3: Safely invoke |
Bibliography
Android Implementation Details
The code examples using the java.security
package are not applicable to Android, but the principle of the rule is applicable to Android apps.
Bibliography
[API 2014] | |
Section |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="10bbcaaa-19ce-41ee-bbee-b098ad33bb37"><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="c4afb16d-acd9-4c2e-aac0-52d6d7d74b2b"><ac:plain-text-body><![CDATA[
[[Gong 2003
AA. Bibliography#Gong 03]]
6.4, " |
]]></ac:plain-text-body></ac:structured-macro>
Section 9.5, "Privileged Code" |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8495c4b6-46ab-470c-b1f0-678a4693b68e"><ac:plain-text-body><![CDATA[
[ |
] |
Pixy: A Static Analysis Tool for Detecting Web Application Vulnerabilities |
]]></ac:plain-text-body></ac:structured-macro>
...