In Java SE 6 and later, privileged code must either uses use the AccessController
mechanism or must be signed by an owner (or provider) who is trusted by whom the user trusts. Attackers could link privileged code with malicious code if the privileged code directly or indirectly invokes code from another package. In fact, trusted jar Trusted JAR files often contain code that lacks direct privileges, requires no elevated privileges itself but that depends on privileged code; such code is known as security-sensitive code. If an attacker can link security-sensitive code with malicious code, they he or she can indirectly cause incorrect behavior with sensitive data. This exploit is called a mix-and-match attack.
Execution Normally, execution of untrusted code causes loss of privileges; the Java security model rescinds privileges when a trusted method invokes an untrusted one. When trusted code calls some untrusted code that attempts to perform some action requiring permissions withheld by the security policy, the Java security model disallows that action is forbidden. However, privileged code may use a class that exists in an untrusted container and performs only unprivileged operations. If the attacker were to replace this the class in the untrusted container with a malicious implementationclass, the trusted code would retrieve might receive incorrect results and misbehave at the discretion of the malicious code.
According to the Java API \[[JarSpec 2008|AA. Bibliography#JarSpec 08]\], {{JAR}} file specificationJava SE Documentation, "Extension Mechanism Architecture" [EMA 2014]: Wiki Markup
A package sealed within a JAR specifies that all classes defined in that package must originate from the same JAR. Otherwise, a
SecurityException
is thrown.
Sealing a JAR file automatically enforces the requirement of keeping privileged code together. In addition, it is important to adhere to rule OBJ02-J. Minimize minimize the accessibility of classes and their members.
Noncompliant Code Example (
...
Privileged Code)
This noncompliant code example uses includes a doPrivileged()
block and calls a method defined in a class that exists in a different, untrusted jar JAR file.:
Code Block | ||
---|---|---|
| ||
package trusted; import untrusted.RetValue; public class MixMatch { private void privilegedMethod() throws IOException { try { AccessController.doPrivileged( new PrivilegedExceptionAction<FileInputStream>PrivilegedExceptionAction<Void>() { public FileInputStreamVoid run() throws IOException, FileNotFoundException { final FileInputStream fis = new FileInputStream("file.txt"); try { RetValue rt = new RetValue(); if (rt.getValue() == 1) { // doDo something with sensitive file } } finally { fis.close(); } return null; // Nothing to return } } ); } catch (PrivilegedActionException e) { // forwardForward to handler and log } } public static void main(String[] args) throws IOException { MixMatch mm = new MixMatch(); mm.privilegedMethod(); } } // In another jarJAR file: package untrusted; class RetValue { public int getValue() { return 1; } } |
An attacker can provide an implementation of class RetValue
so that the privileged code uses an incorrect return value. Even though class MixMatch
consists only of trusted, signed code, an attacker can still cause this behavior by maliciously deploying a legally valid signed jar JAR file containing the untrusted RetValue
class.
This example comes close to violating almost violates SEC01-J. Do not allow tainted variables in doPrivileged privileged blocks, but does not do so. It instead allows potentially tainted code in its doPrivileged()
block, which is a similar issue.
Noncompliant Code Example (
...
Security-
...
Sensitive Code)
This noncompliant code example improves upon on the previous one example by moving the use of the RetValue
class outside the doPrivileged()
block.:
Code Block | ||
---|---|---|
| ||
package trusted; import untrusted.RetValue; public class MixMatch { private void privilegedMethod() throws IOException { try { final FileInputStream fis = AccessController.doPrivileged( new PrivilegedExceptionAction<FileInputStream>() { public FileInputStream run() throws FileNotFoundException { return new FileInputStream("file.txt"); } } ); try { RetValue rt = new RetValue(); if (rt.getValue() == 1) { // doDo something with sensitive file } } finally { fis.close(); } } catch (PrivilegedActionException e) { // forwardForward to handler and log } } public static void main(String[] args) throws IOException { MixMatch mm = new MixMatch(); mm.privilegedMethod(); } } // In another jarJAR file: package untrusted; class RetValue { public int getValue() { return 1; } } |
Although the RetValue
class is used only outside the doPrivileged()
block, the behavior of RetValue.getValue()
certainly affects the behavior of security-sensitive code; that is, the code that operates on the file opened within the doPrivileged()
block. Consequently, an attacker can still exploit the security-sensitive code with a malicious implementation of RetValue
.
Compliant Solution
This compliant solution combines all security-sensitive code into the same package and the same jar JAR file. It also reduces the accessibility of the getValue()
method to package-private. Sealing the package is necessary to prevent attackers from inserting any rogue classes.
Code Block | ||
---|---|---|
| ||
package trusted; public class MixMatch { // ... } // In the same signed & sealed jarJAR file: package trusted; class RetValue { int getValue() { return 1; } } |
To seal a package, use the sealed
attribute in the jar JAR file's manifest file header, as shown below.follows:
Code Block |
---|
Name: trusted/ // packagePackage name Sealed: true // sealedSealed attribute |
Exception
ENV01-J-EX0: Independent groups of privileged code and associated security-sensitive code (a "group" hereafter) may be placed in separate sealed packages and even in separate jar JAR files, subject to certain the following enabling conditions. These conditions are:
- The code in any one of these independent groups must lack any dynamic or static dependency on any of the code in any of the other groups. This means that code from one such group cannot cannot invoke code from any of the others, whether directly or transitively.
- All code from any single group is contained within one or more sealed packages.
- All code from any single group is contained within a single signed jar JAR file.
Risk Assessment
Failure to place all privileged code together in one package and seal the package can lead to mix-and-match attacks.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ENV01-J |
High |
Probable |
Medium | P12 | L1 |
Automated Detection
Detecting code that should be considered privileged or sensitive requires programmer assistance. Given identified privileged code as a starting point, automated tools could compute the closure of all code that can be invoked from that point. Such a tool could plausibly determine whether all code in that closure exists within a single package. A further check of whether the package is sealed appears feasible.
Related Vulnerabilities
is feasible.
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
CodeSonar |
| JAVA.INSEC.LDAP.POISON | Potential LDAP Poisoning (Java) |
Android Implementation Details
java.security.AccessController
exists on Android for compatibility purposes only, and it should not be usedSearch for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8b2031b9-3ca2-4ff1-8378-d24db653de6c"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="78dca613-3881-4f65-97c2-4df30dfaf2b6"><ac:plain-text-body><![CDATA[ | [[McGraw 1999 | AA. Bibliography#Ware 99]] | Rule 7: If You Must Sign Your Code, Put It All in One Archive File (sic) | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="31fe718c-5ef7-4166-ba87-f4d5c2058788"><ac:plain-text-body><![CDATA[ | [[Ware 2008 | AA. Bibliography#Ware 08]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
[EMA 2014] | Extension Mechanism Architecture, "Optional Package Sealing" |
Rule 7, If you must sign your code, put it all in one archive file | |
...
15. Runtime Environment (ENV) ENV02-J. Use system properties rather than environment variables