1

I have a method A which might look like following:

public double A{
  if (secM == null) {
    secM = new SecurityManager();
    System.setSecurityManager(secM);
  }
  //do something and return a double

}

The problem is that once the SecurityManager is set, it is for the entire project, but I only need it to be for the class this method is in. How can I tell the SecurityManager to only verify the permissions for this method/class?

deimos1988
  • 5,896
  • 7
  • 41
  • 57

1 Answers1

3

What actions do you want the SecurityManager to prevent, and which do you want to allow? SecurityManager is more often used to control particular actions by any method, rather than actions by a single method.

However, it can do the latter too, by creating a custom SecurityManager that examines the call stack - see this answer for an example - is this what you need?

You can provide the system-wide SecurityManager with a policy tailored to your application, so you can permit most things but prevent a small set of actions. The permissions you can control are listed here.

Update: you might be able to do this more elegantly by pulling your method out into a separate class, that can be loaded separately (by a different classloader) than your other classes. See this example. Then you can do a trivial check with the classloader rather than checking the full stack trace. However, I'm not familiar with this method - there may be implications if classes from the two separate classloaders need to interact...

Community
  • 1
  • 1
DNA
  • 42,007
  • 12
  • 107
  • 146
  • The method shouldn't be allowed to do anything, it should only be used for calculations etc. Maybe what you suggested is going to work, I'm not sure though. – deimos1988 Feb 28 '12 at 13:35
  • I tried the answer you suggested and it works so far, but it isn't that elegant. Is there no other way to achieve what I need? – deimos1988 Feb 28 '12 at 14:20
  • Hmm, I'm not sure if by modifying the classloader I can achieve what I need. I have found another approach over here: http://stackoverflow.com/questions/502218/sandbox-against-malicious-code-in-a-java-application. However I don't know what the variable "pass" in the second approach with the SecurityManager is used for? – deimos1988 Feb 28 '12 at 15:42
  • These approaches are all fairly similar. The idea in the one you linked is that you can turn off the security manager again after running the untrusted code. `pass` is a password or secret used to authenticate to the securitymanager and thus ensure that the untrusted code can't just disable the security manager and do what it likes! – DNA Feb 28 '12 at 16:45
  • Okay, I get it so far, but what I still don't quite understand is how I can check whether the SecurityManager is active for all methods, or just for the one in question? Or do I need to implement the answer you suggested initially instead of the placeholder "//override checkXXX methods" (in option 2 of the link I provided earlier)? – deimos1988 Feb 28 '12 at 20:59