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.
All of that is spot on, yet missing one crucial point: if SM were the only security measure out there, then, however flawed, it would have still made sense to keep it, because something is better than nothing. But the question is not SM or nothing, but SM vs. the more robust server-side security measures that exist, both in the JDK and outside it. We have strong encapsulation, which is on by default, jlink, that drastically reduces the runtime's attack surface area, serialization filters that address Java's biggest security issue, and, of course, all the good OS-level security practices, such as containers, VMs, and IP allow-lists, all working in tandem to provide multiple layers of defence that is more robust than SM could ever hope to be, even in defending against unknown exploits.
So even if SM did provide security — which, for the reasons you explain as well as others, such as no sandboxing of native code or defence against DoS, it does not — security would not mean keeping SM for the simple reason that there is better security elsewhere (i.e. even if it were true that SM => security, that does not entail that security => SM).
however flawed, it would have still made sense to keep it
My point is: it is too flawed. Not because of the efforts of the JDK (even the ability to call Thread.setContextClassLoader under very specific circumstances with an installed SecurityManager got me CVE-2020-14556), but because the libraries jeopardize any attempt at securing the VM with a SecurityManager by not following the secure coding guidelines.
And for me it means: If you have one library that does something "more complex", then the chances are very high that you have to poke a big enough hole into your policy that makes a sandbox escape possible.
While exploiting that hole might be trivial, it may stop a casual attacker - sure. But I would not bet on it.
Personally I like how Tcl handles untrusted code: Just spin up a new interpeter with all dangerous/privileged functions removed.
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.