Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: separated NCEs/CSs

In the presence of a security manager, it is hard for malicious code to exploit Java's security model. For example, instantiating sensitive classes such as a ClassLoader is prohibited in the context of a web browser. It is critical to ensure that untrusted code does not indirectly use the privileges of legit code. Failure to do so can leave the code vulnerable to privilege escalation attacks. This is because, classes loaded by the same class loader exist in the same namespace and will have identical privileges. Consider for example, an untrusted method calling a class method which loads classes using its own trusted class loader. This is a problem as untrusted code's class loader may not have the permission to load the particular class. Also, if the trusted code accepts tainted inputs, it is susceptible to exploits resulting from malicious classes getting loaded.

The APIs tabulated here perform tasks using the immediate caller's , trusted class loader. They can be exploited if (1) They are invoked indirectly by untrusted code and (2) They accept tainted inputs from untrusted code.

APIs

java.lang.Class.forName

java.lang.Package.getPackage(s)

java.lang.Runtime.load

java.lang.Runtime.loadLibrary

java.lang.System.load

java.lang.System.loadLibrary

java.sql.DriverManager.getConnection

java.sql.DriverManager.getDriver(s)

java.sql.DriverManager.deregisterDriver

java.util.ResourceBundle.getBundle

...

The untrustedCode method of class Untrusted invokes loadLib method of class NativeCode in this noncompliant example. This is insecure as the library gets loaded on behalf of the untrusted code. Accepting tainted inputs from untrusted code can further exacerbate this issue. In essence, the untrusted code's class loader may be able to load the intended library even if it does not have sufficient permissions.

...

Sometimes, a call to System.loadLibrary is embedded in a doPrivileged block, as shown below. An unprivileged caller can maliciously invoke this piece of code using the same technique.

bgColor
Code Block
#FFcccc
AccessController.doPrivileged(new PrivilegedAction() {
  public Object run() { 
    System.loadLibrary("awt");
    return null; 
  }
});

Non-native library code can also be susceptible to related security flaws. Loading a non-native safe library, by itself may not expose a vulnerability but after loading an unsafe library, an attacker can easily exploit it if it contains other vulnerabilities. Moreover, non-native libraries often make use of doPrivileged blocks, making them a lucrative target.

Compliant Solution

Ensure that untrusted code cannot invoke the affected APIs directly or indirectly (that is, via a call to an invoking method). In this case, the loadLib() method must be declared private so that it is only available to a more restrictive method within the class. The restrictive method can ensure that the caller has sufficient permissions to load the library.

Code Block
bgColor#ccccff

private final native void loadLib();

Noncompliant Code Example

Accepting tainted inputs from untrusted code can further exacerbate the issue. The single argument Class.forname() method is another example of an API that uses its immediate caller's class loader to load a desired class. Untrusted code can indirectly abuse misuse this API to manufacture classes with the same privileges as those of the immediate caller.

Code Block
bgColor#FFcccc
// className is Foo
Class c = Class.forName(className);

Compliant Solution

Ensure that untrusted code cannot invoke the affected APIs directly or indirectly (that is, via a call to an invoking method)Again, limit the visibility of the method that uses this API. Do not operate on tainted inputs and make sure that internal objects . The preceding noncompliant code example can be fixed by hard-coding the class's name.

Code Block
bgColor#ccccff

Class c = Class.forName("Foo"); // explicitly hardcode

Noncompliant Code Example

This noncompliant code example returns an instance of the immediate caller's class loader to any invoker. A malicious invoker can therefore, obtain the associated class loader using standard APIs such as java.lang.Class.getClassLoader().

Code Block
bgColor#FFcccc

private Class doLogic() {
  ClassLoader myLoader = new myClassLoader();
  Class myClass = myLoader.loadClass("MyClass");
  return myClass; // returns Class instance to untrusted code
}

Compliant Solution

Always make sure that any internal Class, ClassLoader and Thread instances are not returned to untrusted code.

Code Block
bgColor#ccccff

private void doLogic() {
  ClassLoader myLoader = new myClassLoader();
  Class myClass = myLoader.loadClass("MyClass");
  // do what is is required here itself; do not return myClass
}

Exceptions

EX1: 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.

...