When a custom class loader needs to must override the getPermissions()
method, the implementation must consult the default system policy by explicitly invoking the superclass's getPermissions()
method before assigning arbitrary permissions to the code source. The getPermissions()
method is actually defined by SecureClassLoader
, which extends ClassLoader
. ClassLoader
is A custom class loader that ignores the superclass's getPermissions()
could load untrusted classes with elevated privileges. ClassLoader
is abstract and must not be extended directly subclassed.
Noncompliant Code Example
This noncompliant code example shows a fragment of a custom class loader that extends the class URLClassLoader
. It overrides the getPermissions()
method and but does not call the its superclass's more restrictive getPermissions()
method. Consequently, a class defined using this custom class loader has permissions that are completely independent of those specified in the system-wide systemwide policy file; in . In effect, the class's permissions override them.
Code Block | ||
---|---|---|
| ||
protected PermissionCollection getPermissions(CodeSource cs) { PermissionCollection pc = new Permissions(); // Allow exit from the VM anytime pc.add(new RuntimePermission("exitVM")); // allow exit from the VM anytime return pc; } |
Compliant Solution
In this compliant solution, the getPermissions()
method calls super.getPermissions()
. ConsequentlyAs a result, the default system-wide systemwide security policy is applied , in addition to the custom policy.
Code Block | ||
---|---|---|
| ||
protected PermissionCollection getPermissions(CodeSource cs) { PermissionCollection pc = super.getPermissions(cs); // Allow exit from the VM anytime pc.add(new RuntimePermission("exitVM")); // allow exit from the VM anytime return pc; } |
Risk Assessment
Failure to consult the default system policy while defining a custom classloader class loader violates the tenets of defensive programming and can result in classes defined with unintended permissions.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
SEC11SEC07-J | high High | probable Probable | low Low | P18 | L1 |
Automated Detection
This Violations of this rule can be addressed discovered with a heuristic checker in the style of FindBugs. As with all heuristic checks, achieving a low false-positive rate is essential.
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="99a7df51-e9b3-4a23-8ac3-c0f019402254"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | [Class ClassLoader | http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f43ae77a-1a61-407c-905f-8396b6a02bed"><ac:plain-text-body><![CDATA[ | [[Oaks 2001 | AA. Bibliography#Oaks 01]] |
| ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="65cf89ee-f7db-43ed-8f9b-f34af0a30059"><ac:plain-text-body><![CDATA[ | [[Security 2006 | AA. Bibliography#Security 06]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
Android Implementation Details
The java.security
package exists on Android for compatibility purposes only, and it should not be used.
Bibliography
...
SEC09-J. Do not base security checks on untrusted sources 14. Platform Security (SEC) SEC18-J. Define wrappers around native methods