Java uses security managers and security policies together to prevent untrusted code from performing privileged operations. In standard installations, a restrictive systemwide security policy prevents instantiation of sensitive classes such as java.lang.ClassLoader
in the context of a web browser, for example. It remains critically important to ensure that untrusted code cannot indirectly use the privileges of trusted code to perform privileged operations.
Most APIs install security manager checks to prevent such privilege escalation attacks; some APIs fail to do so. These APIs are tabulated below. Note, however, that the loadLibrary
and load
APIs throw a security exception when the caller lacks permission to dynamically link the library code. These APIs are nevertheless listed as unsafe because they use the immediate caller's class loader to find and load the requested library. Moreover, because the loadLibrary
and load
APIs are typically used from within a doPrivileged
block defined in trusted code, untrusted callers can directly invoke it without requiring any special permissions.
APIs |
---|
|
|
|
|
|
|
|
|
|
|
These APIs perform tasks using the immediate caller's class loader. They can be exploited when (1) they are invoked indirectly by untrusted code and/or (2) they accept tainted inputs from untrusted code.
Classes that have the same defining class loader exist in the same namespace but may have different privileges, depending on the security policy. Security vulnerabilities can arise when trusted code coexists with untrusted code that was loaded by the same defining class loader. In this case, the untrusted code can freely access members of the trusted code according to their declared accessibility. When the trusted code uses any of the tabulated APIs, no security manager checks are carried out (with the exception of loadLibrary
and load
).
Security vulnerabilities can also arise even when untrusted code has been loaded by a class loader instance that is distinct from the class loader used to load the trusted code. When the untrusted code's class loader delegates to the trusted code's class loader, the untrusted code has visibility to the trusted code according to the declared visibility of the trusted code. In the absence of such a delegation relationship, the class loaders would ensure namespace separation; consequently, the untrusted code would be unable to observe members or to invoke methods belonging to the trusted code.
Consider, for example, untrusted code that is attempting to load a privileged class. Its class loader is permitted to delegate the class loading to the trusted class's class loader. This can result in privilege escalation, because the untrusted code's class loader may lack permission to load the requested privileged class. Further, if the trusted code accepts tainted inputs, the trusted code's class loader could load additional privileged — or even malicious — classes on behalf of the untrusted code.
This guideline is an instance of SEC04-J. Protect sensitive operations with security manager checks. Many examples also violate SEC00-J. Do not allow privileged blocks to leak sensitive information across a trust boundary.
Noncompliant Code Example
In this noncompliant code example a call to System.loadLibrary()
is embedded in a doPrivileged
block. This is insecure because a library can be loaded on behalf of untrusted code. In essence, the untrusted code's class loader may be able to indirectly load a library even though it lacks sufficient permissions. After loading the library, untrusted code can call native methods on it if the methods are accessible. This is possible because the doPrivileged
block stops security manager checks being applied to callers further up the execution chain.
Code Block | ||
---|---|---|
| ||
public void load(String libName) { AccessController.doPrivileged(new PrivilegedAction() { public Object run() { System.loadLibrary(libName); return null; } }); } |
Nonnative library code can also be susceptible to related security flaws. Loading a nonnative safe library may not directly expose a vulnerability, but after loading an additional unsafe library, an attacker can easily exploit the safe library if it contains other vulnerabilities. Moreover, nonnative libraries often use doPrivileged
blocks, making them lucrative targets.
Compliant Solution
This compliant solution reduces the accessibility of method load()
from public
to private
. Consequently, untrusted callers are prohibited from loading the awt
library. Also, the name of the library is hard-coded to reject the possibility of tainted values.
Code Block | ||
---|---|---|
| ||
private void load() { AccessController.doPrivileged(new PrivilegedAction() { public Object run() { System.loadLibrary("awt"); return null; } }); } |
Noncompliant Code Example
Accepting tainted inputs from untrusted code can permit an attacker to load classes with escalated privileges. The single argument Class.forName()
method is another API method that uses its immediate caller's class loader to load a requested class. Untrusted code can misuse this API to indirectly manufacture classes that have the same privileges as those of the attacker's immediate caller.
Code Block | ||
---|---|---|
| ||
// className may be the name of a privileged or even a malicious class Class c = Class.forName(className); |
Compliant Solution
Limit the visibility of the method that uses this API. Do not operate on tainted inputs. This compliant solution hard-codes the class's name.
Code Block | ||
---|---|---|
| ||
Class c = Class.forName("Foo"); // Explicitly hardcode |
Noncompliant Code Example
This noncompliant code example returns an instance of java.sql.Connection
from trusted to untrusted code. Untrusted code that lacks the permissions required to create a SQL connection can bypass these restrictions by using the acquired instance directly.
Code Block | ||
---|---|---|
| ||
public Connection getConnection() { // ... return DriverManager.getConnection(url, username, password); } |
Compliant Solution
Ensure that instances of objects created using the unsafe methods are not returned to untrusted code. It is preferable to reduce the accessibility of methods that perform sensitive operations and define wrapper methods that are accessible from untrusted code.
Code Block | ||
---|---|---|
| ||
private void getConnection() { // ... conn = DriverManager.getConnection(url, username, password); // Do what is is required here itself; do not return the connection } public void DoDatabaseOperationWrapper() { // Perform any checks or validate input getConnection(); } |
Applicability
Allowing untrusted code to carry out actions using the immediate caller's class loader may allow the untrusted code to execute with the same privileges as the immediate caller.
It is permissible to use APIs that do not use the immediate caller's class loader instance. For example, the three-argument java.lang.Class.forName()
method requires an explicit argument that specifies the class loader instance to use. Do not use the immediate caller's class loader as the third argument if instances must be returned to untrusted code.
Code Block |
---|
public static Class forName(String name, boolean initialize, ClassLoader loader) /* explicitly specify the class loader to use */ throws ClassNotFoundException |
Related Guidelines
[SCG 2010] | Guideline 9-9: Safely invoke standard APIs that perform tasks using the immediate caller's class loader instance |