Serialization and deserialization features can be exploited to bypass security manager checks. A serializable class may contain security manager checks in its constructors for various reasons, including preventing untrusted code from modifying the internal state of the class. Such security manager checks must be replicated anywhere a class instance can be constructed. For example, if a class enables a caller to retrieve sensitive internal state contingent upon security checks, the same those checks must be replicated during deserialization. This ensures that an attacker cannot extract sensitive information by deserializing the object.
...
Code Block | ||
---|---|---|
| ||
public final class Hometown implements Serializable { // Private internal state private String town; private static final String UNKNOWN = "UNKNOWN"; void performSecurityManagerCheck() throws AccessDeniedException { // ... } void validateInput(String newCC) throws InvalidInputException { // ... } public Hometown() { performSecurityManagerCheck(); // Initialize town to default value town = UNKNOWN; } // Allows callers to retrieve internal state String getValue() { performSecurityManagerCheck(); return town; } // Allows callers to modify (private) internal state public void changeTown(String newTown) { if (town.equals(newTown)) { // No change return; } else { performSecurityManagerCheck(); validateInput(newTown); town = newTown; } } // writeObject() correctly enforces checks during serialization private void writeObject(ObjectOutputStream out) throws IOException { out.writeObject(town); } // readObject() correctly enforces checks during deserialization private void readObject(ObjectInputStream in) throws IOException { in.defaultReadObject(); // If the deserialized name does not match the default value normally // created at construction time, duplicate the checks if (!UNKNOWN.equals(town)) { validateInput(town); } } } |
...
AccessDeniedException
and InvalidInputException
are both unchecked security exceptions that can be thrown by any method without requiring a throws
declaration.
...
Refer to rule SEC04-J. Protect sensitive operations with security manager checks to learn for information about implementing the performSecurityManagerCheck()
method, which is important to protect for protection against the finalizer attackattacks.
The ObjectInputStream.defaultReadObject()
fills the object's fields with data from the input stream. Because each field is deserialized recursively, it is possible for the this
reference to escape from control of the deserialization routines. This can happen if a referenced object publishes the this
reference in its constructors or field initializers. See rule TSM01-J. for more information. To be compliant, no subobjects being recursively deserialized may subobjects must not publish the this
object reference.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="51eebadd44afd07c-16e0ac7f-41814d58-af0488e1-29aa6218ff26237e735a9928"><ac:plain-text-body><![CDATA[ | [[Long 2005 | AA. Bibliography#Long 05]] | Section 2.4, Serialization | ]]></ac:plain-text-body></ac:structured-macro> |
...