When a custom class loader 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. A custom class loader that ignores the superclass's getPermissions()
could load untrusted classes with elevated privileges.
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 does not call the 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 policy file. In effect, the class's permissions override them.
protected PermissionCollection getPermissions(CodeSource cs) { PermissionCollection pc = new Permissions(); 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()
. As a result, the default system-wide security policy is applied, in addition to the custom policy.
protected PermissionCollection getPermissions(CodeSource cs) { PermissionCollection pc = super.getPermissions(cs); 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 class loader violates the tenets of defensive programming and can result in classes defined with unintended permissions.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
SEC07-J |
high |
probable |
low |
P18 |
L1 |
Automated Detection
Violations of this rule can be discovered with a heuristic checker in the style of FindBugs. As with all heuristic checks, achieving a low false-positive rate is essential.
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f4b46beb-1bea-44c0-8bfa-fc3197d1a367"><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="b6a8520d-5ca1-4e3c-aaed-03038d8cb24f"><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="31981a85-34ac-4af1-828f-acc7956ae68d"><ac:plain-text-body><![CDATA[ |
[[Security 2006 |
AA. Bibliography#Security 06]] |
|
]]></ac:plain-text-body></ac:structured-macro> |
14. Platform Security (SEC) SEC08-J. Define wrappers around native methods