In the Java policy file, the grant codeBase
syntax specifies which codebase should be granted which permissions. for example,
grant codeBase "file:/C:/abc.jar" { permission java.security.AllPermission; };
grants AllPermission
to code inside abc.jar
In a similar way, Is there a way to deny
permissions to a specific syntax? Like this:
deny codeBase "file:/C:/def.jar" { permission java.io.FilePermission; };
so that the code inside def.jar
gets every other permissions except the FilePermission?
Is this even possible?
I know this can be easily done using the SecurityManager
class, but I just want to know if this is possible by using the policy file only.
One of the least involved approaches towards attaining deny rule support, is to:
Permission
subclass that wraps a regular positive permission and negates it; andPolicy
such that it (its wrapper) understands such permissions.The
DeniedPermission
classThe
DenyingPolicy
classUsage
Just embed
DeniedPermission
s within plain old grant rules; for instance, the following rule will grant everything but the ability to read System Properties with a name starting with "user.", to some.jar's classes.Then install a
DenyingPolicy
viaPolicy.setPolicy(new DenyingPolicy());
.Caveat: While semantically correct, as was mentioned in a previous answer's comment, the above example is ineffective, as it still grants dangerous permissions, such as
SecurityPermission "setPolicy"
, which implicitly allow sandboxed code to do whatever it pleases, including the action prohibited by theDeniedPermission
. To prevent this from occurring, rather than subtracting permissions fromAllPermission
, consider subtracting from anAllSafePermission
instead, whereAllSafePermission
is defined such that itimplies
everything except known sandbox-defeating permissions1.Notes
(String)
and/or(String, String)
constructor, and appropriately overridesimplies(Permission)
.implies
the to-be-denied permissions.DenyingPolicy
does not prevent permissions statically assigned to a protection domain (such asRuntimePermission "exitVM.*"
granted by default to code originating from the classpath) from being granted, as, generally, evaluation of such permissions occurs prior to evaluation of permissions maintained by the policy. In order to deny any of these permissions as well, you will have to replace theClassLoader
with one that:ProtectionDomain
subclass, that overridesimplies(Permission)
such that:DeniedPermission
s in a manner similar toDenyingPolicy
.1: For a listing of such permissions see e.g. Maass, M. (2016). A Theory and Tools for Applying Sandboxes Effectively., table 3.1 (page 47).
No. There is nothing like this implemented for policy files. You could write your own system, if you were really desperate.
I realize this is almost a year late but I think I am trying to do something similar.
There is a way to set the runtime permissions such that Java won't grant the global permissions. Then you can specify only the permissions you want granted for your app. The key is to run your app with the options below.
Note the double equals
-Djava.security.policy==policyFile.txt
. The double equals==
means to use only the permissions in the named file as opposed to the single equal sign-Djava.security.policy=policyFile.txt
which means use these permissions in addition to the inherited global permissions.Then create a policy file excluding the permissions you want to deny:
You can use Prograde library, which implements policy file with deny rules.
Add following Maven dependency to your app
And then enable it for your application by using standard system properties:
or you can just replace programatically the Policy implementation in your code:
The syntax of policy file stays similar to the standard implementation, but you can use
deny
instead ofgrant
and you can also change priorities by using keywordpriority
(default value is"deny"
- to stay backward compatible).For instance, you can do sth. like:
Other examples are here.