You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 69 Next »

In the presence of a security manager and a restrictive system-wide security policy, untrusted code is prohibited from performing privileged operations. For example, instantiation of sensitive classes such as java.lang.ClassLoader is prohibited in the context of a web browser. At the same time, it is critical to ensure that untrusted code does not indirectly use the privileges of trusted code to perform privileged operations.

Most APIs install security manager checks to prevent this, however, some do not. These APIs are tabulated below, with the exception of the loadLibrary and load APIs. The loadLibrary and load APIs throw a security exception if the caller does not have permissions to dynamically link the library code. However, they are listed as unsafe because they use the immediate caller's class loader to find and load the 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

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

These APIs perform tasks using the immediate caller's class loader. They can be exploited if (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 if the trusted code coexists with untrusted code, and both have the same defining class loader. This is because untrusted code can freely access members of the trusted code depending on their accessibility. If the trusted code uses any of the tabulated APIs, no security manager checks are carried out (with the exception of loadLibrary and load).

Sometimes untrusted code is loaded by a class loader instance that is different from the one used to load the trusted code. Security vulnerabilities can arise if the untrusted code's class loader delegates to the trusted code's class loader. In the absence of such a delegation relationship, the class loaders ensure namespace separation and disallow untrusted code from observing members or invoking 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 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, more privileged or even malicious classes may be loaded on behalf of untrusted code.

Noncompliant Code Example

The untrustedCode() method of class Untrusted invokes the loadLib() method of class NativeCode in this noncompliant code example. This is insecure as the library is loaded on behalf of untrusted code. In essence, the untrusted code's class loader may be able to indirectly load the intended library even if it does not have sufficient permissions. After loading the library, untrusted code can call native methods on it if the methods are accessible.

class NativeCode {
  public native void loadLib();

  static {
    try {
      System.loadLibrary("/com/foo/MyLib.so");
    }catch(UnsatisfiedLinkError e) { e.getMessage(); }
  }    
}

class Untrusted {
  public static void untrustedCode() {
    new NativeCode().loadLib();
  }
}

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 wrapper method within the class. The restrictive wrapper method can ensure that the caller can safely invoke the library.

private final native void loadLib();

Noncompliant Code Example

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 because the doPrivileged block allows security manager checks to be forgone for other callers on the execution chain.

public void load(String libName) {
  AccessController.doPrivileged(new PrivilegedAction() {
    public Object run() { 
      System.loadLibrary(libName);
      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 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.

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 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 misuse this API to manufacture classes with the same privileges as those of the immediate caller.

// 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 hardcodes the class's name.

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. The untrusted code that does not have the permissions to create an SQL connection can bypass this restriction by directly using the acquired instance.

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. Furthermore, it is preferable to reduce the accessibility of methods that perform sensitive operations and define wrapper methods that are accessible from untrusted code.

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();
}

Exceptions

SEC05-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. Do not use the immediate caller's class loader as the third argument if instances must be returned to untrusted code.

public static Class forName(String name,
                            boolean initialize,
                            ClassLoader loader) /* explicitly specify the class loader to use */
                     throws ClassNotFoundException

Risk Assessment

Allowing untrusted code to carry out actions using the immediate caller's class loader may allow it to execute with the same privileges as the immediate caller.

Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

SEC05-J

high

probable

medium

P12

L1

Automated Detection

TODO

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

References

[[SCG 2007]] Guideline 6-3 Safely invoke standard APIs that perform tasks using the immediate caller's class loader instance


SEC04-J. Do not expose standard APIs that may bypass Security Manager checks to untrusted code      02. Platform Security (SEC)      SEC06-J. Do not use APIs that perform access checks against the immediate caller

  • No labels