Security Manager Improvements
Ok, let's talk about security. If you are not one of those who cares about application security over pushing more features in a release, then the expression on your face may be like Uh! What's that? If you are one those, then let's first understand the importance of security and find a way to consider this in your application development tasks. In today's SaaS-dominated world, everything is exposed to the outside world. A determined individual (a nice way of saying, a malicious hacker), can get access to your application and exploit the security holes you may have introduced through your negligence. I would love to talk about application security in depth as this is another area I am very much interested in. However, application security is out of the scope of this book. The reason we are talking about it here is that the JPM team has taken an initiative to improve the existing security manager. Hence, it is important to first understand the importance of security before talking about the security manager.
Hopefully, this one line of description may have generated secure programming interest in you. However, I do understand that sometimes you may not have enough time to implement a complete secure programming model due to tight schedules. So, let's find a way which can fit with your tight schedule. Let's think for a minute; is there any way to automate security? Can we have a way to create a blueprint and ask our program to stay within the boundaries? Well, you are in luck, Java does have a feature called security manager. It is nothing but a policy manager that defines a security policy for the application. It sounds exciting, doesn't it? But what does this policy look like? And what does it contain? Both are fair questions to ask. This security policy basically states actions that are dangerous or sensitive in nature. If your application does not comply with this policy, then the security manager throws SecurityException
. On the other side, you can have your application call this security manager to learn about the permitted actions. Now, let's look at the security manager in detail.
In case of a web applet, a security manager is provided by the browser, or the Java Web Start plugin runs this policy. In many cases, applications other than web applets run without a security manager unless those applications implement one. It's a no brainer to say that if there is no security manager and no security policy attached, the application acts without restrictions.
Now we know a little about the security manager, let's look at the performance improvement in this area. As per the Java team, there may be a possibility that an application running with a security manager installed degrades performance by 10 percent to 15 percent. However, it is not possible to remove all the performance bottlenecks but narrowing this gap can assist in improving not only security but also performance.
The Java 9 team looked at some of the optimizations, including the enforcement of security policy and the evaluation of permissions, which will help improve the overall performance of using a security manager. During the performance testing phase, it was highlighted that even though the permission classes are thread safe, they show up as a HotSpot. Numerous improvements have been made to decrease thread contention and improve throughput.
Computing the hashcode
method of java.security.CodeSource
has been improved to use a string form of the code source URL to avoid potentially expensive DNS lookups. Also, the checkPackageAccess
method of java.lang.SecurityManager
, which contains the package checking algorithm, has been improved.
Some other noticeable changes in security manager improvements are as follows:
- The first noticeable change is that using
ConcurrentHashMap
in place ofCollections.synchronizedMap
helps improving throughput of thePolicy.implie
method. Look at the following graph, taken from the OpenJDK site, which highlights the significant increase in the throughput withConcurrentHashMap
: - In addition to this,
HashMap
, which had been used for maintaining internal collection ofCodeSource
injava.security.SecureClassLoader
, has been replaced byConcurrentHashMap
. - There are a few other small improvements like an improvement in the throughput by removing the compatibility code from the
getPermissions
method (CodeSource
), which synchronizes on identities. - Another significant gain in performance is achieved using
ConcurrentHashMap
instead ofHashMap
surrounded by synchronized blocks in the permission checking code, which yielded in greater thread performance.