Unrestricted deserializing from a privileged context allows an attacker to supply crafted input which upon deserialization, can yield objects that the attacker does not have permissions to construct. Construction of a custom class loader is an one example (See SEC07-J. Do not allow the unauthorized construction of classes existing in forbidden packages).
...
In Aug 2008 a vulnerability in the JDK was discovered by Sami Koivu. Julien Tinnes wrote an exploit that allowed arbitrary code execution on multiple platforms that ran vulnerable versions of Java. The problem arose surfaced primarily due to because of deserializing untrusted input from within a privileged context. The vulnerability involves the ZoneInfo
object (sun.util.Calendar.Zoneinfo
), which being a serializable class, is by design deserialized by the readObject()
method of the ObjectInputStream
class.
Consider the default security model of an applet that does not allow access to sun.util.calendar.ZoneInfo
since because all classes within the "sun
" package are treated as untrusted. As a result, prior to JDK 1.6 u11, the acceptable method for an unsigned applet to deserialize a Zoneinfo
object was to execute the call from a privileged context, such as a doPrivileged()
block. This constitutes a vulnerability since because there is no surefire method of knowing whether the serialized stream contains a Zoneinfo
object and not a malicious serializable class. The vulnerable code casts the malicious object to the ZoneInfo
type which typically causes a ClassCastException
. This exception however, is of little consequence as it is possible to store a reference to the newly created object in some static context so that the garbage collector does not act upon it.
A non-serializable or serializable class can be extended and its subclass can be made serializable, or it becomes so automaticallyserializable automatically if it derives from a serializable class. During deserialization of the subclass, the JVM calls the no-argument constructor of the most derived superclass that does not implement java.io.Serializable
either directly or indirectly. When an object is deserialised the no-argument constructor of the most derived superclass that does not directly or indirectly implement java.io.Serializable
is executed. In the code snippet that immediately follows, class A
's no-argument constructor is called when C
is deserialized since because A
does not implement Serializable
. Subsequently, Object's
constructor is invoked. This procedure cannot be carried out programmatically, consequently the JVM generates the equivalent bytecode at runtime. Typically, when the superclass's constructor is called by a subclass, the subclass remains on the stack. However, in deserialization this does not happen. Only the unvalidated bytecode is present. This allows any security checks within the superclass's constructor to be bypassed.
...
At this point, there is no subclass code on the stack and the superclass's constructor is executed with no restrictions since as doPrivileged()
allows the immediate caller to exert its full privileges. Because the immediate caller java.util.Calendar
is trusted, it exhibits full system privileges.
For exploiting this condition, often, a custom class loader is desirable. Instantiating a class loader object requires special permissions that are made available by the security policy that is enforced by the SecurityManager
. An unsigned applet cannot carry out this step by default. If an unsigned applet can execute a custom class loader's constructor, it can effectively bypass all the security checks (since it has the requisite privileges as a direct consequence of the vulnerability). A custom class loader can be designed to extend the System Class Loader, undermine security and carry out forbidden functions actions such as reading or deleting files on the userâs file system. Moreover, any legitimate security checks in the constructor are meaningless as the code is granted all privileges.
...
Deserializing objects from a privileged context can lead to result in arbitrary code execution.
...