Java Security Manager - What does it check?
Asked Answered
G

3

12

This article about Java security says:

Code in the Java library consults the Security Manager whenever a dangerous operation is about to be attempted.

So, what does this exactly mean? Say, if I've implemented my own securitymanager and enabled it for the whole JVM. Now, does the java runtime consults my securitymanager for each and every java call(like System.out.println() etc) or it consults only for dangerous api calls like System.exit() ,file operations etc?

edit: let me clarify my question,

I'm not questioning the possiblities of the securitymanager. I'm just asking if the security checks are done for the dangerous api's alone or it is done for each and every method call. Which inturn causes a huge performance degradation in case of applications with large amounts of code.

Gainful answered 4/3, 2011 at 11:12 Comment(0)
S
17

It will only consult the SecurityManager if the code says so. It won't do it for every single operation.

For example in Runtime.exit, you see that the SecurityManager is consulted:

public void exit(int status) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
    security.checkExit(status);
}
Shutdown.exit(status);
}

Similarly, in File, you will see that most methods consult the SecurityManager. Example:

public boolean canWrite() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
    security.checkWrite(path);
}
return fs.checkAccess(this, FileSystem.ACCESS_WRITE);
}

If you are writing a method which might be "dangerous" then you should also consult the SecurityManager.

Sibert answered 4/3, 2011 at 11:27 Comment(3)
So, it doesn't check for innocent ops like 'System.out.println()'?Gainful
@ dogbane : So, what about the policy file? Isn't a Security manager uses it to see what is permitted and what's not permitted according to jpkrohling's comment belowAlgonquian
@Gainful I believe it does not check System.out/err by default; I've posted a question to confirm this.Pledge
A
2

Using security manager you could control access to :

  1. File operations
  2. Reflection facility
  3. Read/Write IO
  4. Thread/Thread group operations
  5. Socket operations(listen, accept etc.)
  6. Power to create your own classloader.

For each such thing there is a check*() method in SecurityManager

For an exhaustive list check the constants in SecurityConstants

Aculeate answered 4/3, 2011 at 11:24 Comment(0)
B
0

The security manager uses a policy file to see what is permitted and what's not permitted. "Dangerous" operations, as determined by this policy file, is granted or denied during the execution.

You can find more details about the default policy for Sun/Oracle JVM here:

http://download.oracle.com/javase/6/docs/technotes/guides/security/PolicyFiles.html

Borkowski answered 4/3, 2011 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.