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 wherever a class instance can be constructed. For example, if a class enables a caller to retrieve sensitive internal state contingent upon security checks, those checks must be replicated during deserialization . This ensures to ensure that an attacker cannot extract sensitive information by deserializing the object.
...
Despite the security manager checks, the data in this example is not sensitive. Serializing unencrypted , sensitive data violates rule SER03-J. Do not serialize unencrypted sensitive data.
...
Code Block | ||
---|---|---|
| ||
public final class Hometown implements Serializable { // ... allAll methods the same except the following: // writeObject() correctly enforces checks during serialization private void writeObject(ObjectOutputStream out) throws IOException { performSecurityManagerCheck(); 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)) { performSecurityManagerCheck(); validateInput(town); } } } |
Refer to rule SEC04-J. Protect sensitive operations with security manager checks for information about implementing the performSecurityManagerCheck()
method, which is important for protection against finalizer attacks.
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 (see TSM01-J. Do not let the this reference escape during object construction for more information). To be compliant, recursively deserialized subobjects must not publish the this
object reference.
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
SER04-J | highHigh | probableProbable | highHigh | P6 | L2 |
Related Guidelines
Secure Coding Guidelines for the Java Programming LanguageSE, Version 35.0 | Guideline 58-4 / SERIAL-4. : Duplicate the SecurityManager checks enforced in a class during serialization and deserialization |
...
The java.security
package exists on Android for compatibility purposes only, and it should not be used.
Bibliography
Section 2.4, "Serialization" |
...