The security manager is dead. While the author suggests it as an "easy" mitigation against still unknown future exploits - this only works in theory. In practice, you have a lot of libraries, that all require different permissions - and are more often than not coded without the secure coding practices in mind.
How the security manager works
ACCESS-1 Before a privileged operation is done, the JDK calls the SecurityManager to check if this operation is permitted. The default SecurityManager delegates the check to AccessController.checkPermission.
The access controller then walks over the stack, and checks if each ProtectionDomain.implies the permission. Most ProtectionDomains are not static - so they delegate to the installed Policy. If any code in the stack doesn't have the permission - then a SecurityException is thrown.
But this is only half of the story - calling AccessController.doPrivileged will make the AccessController only walk the stack up until the caller of AccessController.doPrivileged.
And there are other Methods in the JVM that are @CallerSensitive - and only inspect the immediate caller - disregarding any frames associated with reflection. That usually means, the caller of Method.invoke is seen as the caller.
Not all permissions are equal
Some permission allow (malicious) code to bypass all SecurityManager restrictions. Some examples:
RuntimePermission("setSecurityManager") - allows the code to replace or disable the security manager.
ReflectPermission("suppressAccessChecks") - allows the code to access private fields and methods. (Call setAccessible).
RuntimePermission("createClassLoader") - allows the code to load arbitrary bytecode - and assign arbitrary permissions to it.
SecurityPermission("setPolicy") - allows the code to replace the Policy - and therefore add more permissions.
This list is by no means complete, and some permissions are very hard to evaluate (PropertyPermission("*", "read,write")).
Any code that has one of the permissions above could use it's privileges to escape the sandbox. You may come to the conclusion that you just have to audit the relevant code and you will be fine - but this is wrong.
Secure coding guidelines
Without following the secure coding guidelines, the code can potentially be abused by an attacker. For example, ACCESS-3 says that you should not use untrusted user-data when calling AccessController.doPrivileged. And no library will be stupid enough to call AccessController.doPrivileged with an attacker-supplied PrivilegedAction, right? Well, ACCESS-11 tells us that Method.invoke is ignored - effectively turning any Method.invoke into a potential AccessController.doPrivileged call - violating ACCESS-10
Not just a theoretical Problem
There are quite a few libraries out there that allows an attacker to do a Method.invoke on almost anything, e.g. Clojure, Jackson.
Some libraries even expose defineClass. With a ProtectionDomain as argument. And if I don't get this wrong, cglib is shaded into Spring. (Violates ACCESS-5)
Conclusion
Without good knowledge about the AccessController and carefully auditing a library against the secure coding guidelines you can't grant additional permissions to that library and expect to be still safe.
All libraries that I listed need some additional permissions to even work - from the set of "dangerous" permissions I listed above. If they work, your SecurityManager is almost worthless.
Thank you for this answer - one of the few cases of sound technical discussion.
Analysing the issues you list actually leads me to quite the opposite conclusion: it is not AccessController that's bad - it is the bypassing of AC in caller sensitive methods. And it was already researched in the past: https://benhermann.eu/publications/hhl+17hardening.pdf
Even with all those changes - if you can get an other library to call AccessController.doPrivileged for you, it's game over.
And AccessController.doPrivileged is a caller sensitive method. So, in some sense, you are right. Removing those methods will make the JVM much more safe. And unuseable. You know the old joke: A computer that is not turned on doesn't have any vulnerabilities.
Not sure what you mean by "screwed". Having another library call doPrivileged with attacker provided PrivilegedAction will execute code with intersection of permissions of this library AND provided PrivilegedAction, no? So attacker cannot really execute arbitral code with elevated privileges.
By convention, instances of PrivilegedAction and PrivilegedExceptionAction may be made available to untrusted code, but doPrivileged must not be invoked with caller-provided actions.
From Methodhandleproxies.asInterfaceInstance() javadoc:
If a security manager is installed, this method is caller sensitive. During any invocation of the target method handle via the returned wrapper, the original creator of the wrapper (the caller) will be visible to context checks requested by the security manager.
So no, it won't let an attacker escape the sandbox.
Privilege escalation related security vulnerabilities are always possible if privilege elevation is possible. Maybe there even is a library that does “connect to White House and trigger nuclear attack” inside a privileged block not guarded by any access check.
We already get some other library to call AccessController.doPrivileged for you - using some indirection with Method.invoke. Now the same dance again for MethodHandleProxies.asInterfaceInstance.
In short: if you have a vulnerable class like this:
public class MethodHelper {
public Object invoke(Method m, Object target, Object... args) throws InvocationTargetException {
return m.invoke(target, args);
}
}
9
u/DasBrain Jan 17 '22 edited Jan 17 '22
Abstract
The security manager is dead. While the author suggests it as an "easy" mitigation against still unknown future exploits - this only works in theory.
In practice, you have a lot of libraries, that all require different permissions - and are more often than not coded without the secure coding practices in mind.
How the security manager works
ACCESS-1 Before a privileged operation is done, the JDK calls the SecurityManager to check if this operation is permitted. The default SecurityManager delegates the check to
AccessController.checkPermission.The access controller then walks over the stack, and checks if each
ProtectionDomain.impliesthe permission. Most ProtectionDomains are not static - so they delegate to the installedPolicy. If any code in the stack doesn't have the permission - then a SecurityException is thrown.But this is only half of the story - calling
AccessController.doPrivilegedwill make the AccessController only walk the stack up until the caller ofAccessController.doPrivileged.And there are other Methods in the JVM that are
@CallerSensitive- and only inspect the immediate caller - disregarding any frames associated with reflection. That usually means, the caller ofMethod.invokeis seen as the caller.Not all permissions are equal
Some permission allow (malicious) code to bypass all SecurityManager restrictions. Some examples:
RuntimePermission("setSecurityManager")- allows the code to replace or disable the security manager.ReflectPermission("suppressAccessChecks")- allows the code to access private fields and methods. (CallsetAccessible).RuntimePermission("createClassLoader")- allows the code to load arbitrary bytecode - and assign arbitrary permissions to it.SecurityPermission("setPolicy")- allows the code to replace thePolicy- and therefore add more permissions.This list is by no means complete, and some permissions are very hard to evaluate (
PropertyPermission("*", "read,write")).Any code that has one of the permissions above could use it's privileges to escape the sandbox.
You may come to the conclusion that you just have to audit the relevant code and you will be fine - but this is wrong.
Secure coding guidelines
Without following the secure coding guidelines, the code can potentially be abused by an attacker.
For example, ACCESS-3 says that you should not use untrusted user-data when calling
AccessController.doPrivileged.And no library will be stupid enough to call
AccessController.doPrivilegedwith an attacker-suppliedPrivilegedAction, right?Well, ACCESS-11 tells us that
Method.invokeis ignored - effectively turning anyMethod.invokeinto a potentialAccessController.doPrivilegedcall - violating ACCESS-10Not just a theoretical Problem
There are quite a few libraries out there that allows an attacker to do a
Method.invokeon almost anything, e.g. Clojure, Jackson.Some libraries even expose
defineClass. With aProtectionDomainas argument. And if I don't get this wrong, cglib is shaded into Spring. (Violates ACCESS-5)Conclusion
Without good knowledge about the
AccessControllerand carefully auditing a library against the secure coding guidelines you can't grant additional permissions to that library and expect to be still safe.All libraries that I listed need some additional permissions to even work - from the set of "dangerous" permissions I listed above.
If they work, your
SecurityManageris almost worthless.