While Although serialization allows an object's state to be saved as a sequence of bytes and then reconstituted at a later time, it provides no mechanism to protect the serialized data. An attacker who gains access to the serialized data can use it to discover sensitive information and to determine implementation details of the objects. An attacker can also modify the serialized data in an attempt to compromise the system when the malicious data is deserialized. Consequently, sensitive data that is serialized is potentially exposed, without regard to the access qualifiers (such as the private
keyword) that were used in the original code. Moreover, the security manager lacks checks to cannot guarantee the integrity of the serialized deserialized data.
Examples of sensitive data that should remain unserialized never be serialized include cryptographic keys, digital certificates, and classes that may hold references to sensitive data at the time of serialization.
This rule is meant to prevent the unintentional serialization of sensitive information. Rule SER02-J. Sign then seal sensitive objects before sending them outside a trust boundary applies to the intentional serialization of sensitive information.
Noncompliant Code Example
The data members of class Point
are private. Assuming the coordinates are sensitive, their presence in the data stream would expose them to malicious tampering.
Code Block | ||
---|---|---|
| ||
public class Point implements Serializable { private double x; private double y; public Point(double x, double y) { this.x = x; this.y = y; } public Point() { // noNo-argument constructor } } public class Coordinates extends Point implements Serializable { public static void main(String[] args) { FileOutputStream fout = null; try { Point p = new Point(5, 2); fout = new FileOutputStream("point.ser"); ObjectOutputStream oout = new ObjectOutputStream(fout); oout.writeObject(p); } catch (Throwable t) { // Forward to handler } finally { if (fout != null) { try { fout.close(); } catch (IOException x) { // Handle error } } } } } |
Compliant Solution
In the absence of sensitive data, classes can be serialized by simply implementing the java.io.Serializable
interface. By doing so, the class indicates that no security issues may result from the object's serialization. Note that any derived subclasses also inherit this interface and are consequently serializable. This simple approach is inappropriate for any class that contains sensitive data.
Compliant Solution
When serializing a class that contains sensitive data, programs must ensure that sensitive data is omitted from the serialized form. This includes suppressing both suppressing serialization of data members that contain sensitive data , and also suppressing serialization of references to non-serializable nonserializable or sensitive objects.
This compliant solution both avoids the possibility of incorrect serialization and also protects sensitive data members from accidental serialization by declaring the relevant members as transient so that they are omitted from the list of fields to be serialized by the default serialization mechanism.
Code Block | ||
---|---|---|
| ||
public class Point implements Serializable { private transient double x; // declaredDeclared transient private transient double y; // declaredDeclared transient public Point(double x, double y) { this.x = x; this.y = y; } public Point() { // noNo-argument constructor } } public class Coordinates extends Point implements Serializable { public static void main(String[] args) { FileOutputStream fout = null; try { Point p = new Point(5,2); FileOutputStream fout = new FileOutputStream("point.ser"); ObjectOutputStream oout = new ObjectOutputStream(fout); oout.writeObject(p); oout.close(); } catch (Exception e) { // Forward to handler } finally { if (fout != null) { try { fout.close(); } catch (IOException x) { // Handle error } } |
Compliant Solution
...
}
}
}
|
Other compliant solutions include
- Developing custom implementations of the
writeObject()
,writeReplace()
, andwriteExternal()
methods that prevent sensitive fields from being written to the serialized stream.
Compliant Solution
...
- Defining the
serialPersistentFields
array field and
...
- ensuring that sensitive fields are omitted from the array
...
- (
...
- see SER00-J.
...
...
Noncompliant Code Example
Serialization can also be used maliciously. One example is returning multiple instances of a singleton-like class. In this noncompliant code example (based on \[[Bloch 2005|AA. Bibliography#Bloch 05]\]), a subclass {{SensitiveClass}} inadvertently becomes serializable because it extends the {{Exception}} class which implements {{Serializable}}.maliciously, for example, to return multiple instances of a singleton class object. In this noncompliant code example (based on [Bloch 2005]), a subclass Wiki Markup SensitiveClass
inadvertently becomes serializable because it extends the java.lang.Number
class, which implements Serializable
:
Code Block | ||
---|---|---|
| ||
public class SensitiveClass extends ExceptionNumber { // ... Implement abstract methods, such as Number.doubleValue()⦠publicprivate static final SensitiveClass INSTANCE = new SensitiveClass(); public static SensitiveClass getInstance() { return INSTANCE; } private SensitiveClass() { // Perform security checks and parameter validation } protectedprivate int printBalance() {balance = 1000; protected int balance = 1000;getBalance() { return balance; } } class Malicious { public static void main(String[] args) { SensitiveClass sc = (SensitiveClass) deepCopy(SensitiveClass.INSTANCE);getInstance()); // Prints false; indicates new instance System.out.println(sc == SensitiveClass.INSTANCE); // Prints falsegetInstance()); indicates new instance System.out.println("Balance = " + sc.printBalancegetBalance()); } // This method should not be used in production quality code static public Object deepCopy(Object obj) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); new ObjectOutputStream(bos).writeObject(obj); ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); return new ObjectInputStream(bin).readObject(); } catch (Exception e) { throw new IllegalArgumentException(e); } } } |
See rule MSC07-J. Prevent multiple instantiations of singleton objects for more information on singletonsabout singleton classes.
Compliant Solution
Extending a class or interface that implements Serializable
should be avoided whenever possible. For instance, a nonserializable class could contain an instance of a serializable class and delegate method calls to the serializable class.
When extension of such a serializable class by an unserializable class is necessary, undue inappropriate serialization of the subclass can be prohibited by throwing a NotSerializableException
from a custom writeObject()
, readObject()
or readResolve, and readObjectNoData()
method methods, defined in the nonserializable subclass SensitiveClass
. Note that the custom writeObject()
or readResolve()
These custom methods must be declared final
to prevent a malicious subclass from overriding themprivate (see SER01-J. Do not deviate from the proper signatures of serialization methods for more information).
Code Block | ||
---|---|---|
| ||
class SensitiveClass extends ExceptionNumber { // ... private final Object writeObject(java.io.ObjectOutputStream out) throws NotSerializableException { throw new NotSerializableException(); } private final Object readObject(java.io.ObjectInputStream in) throws NotSerializableException { throw new NotSerializableException(); } private final Object readResolve(readObjectNoData(java.io.ObjectInputStream in) throws NotSerializableException { throw new NotSerializableException(); } } |
Exceptions
It is still possible for an attacker to obtain uninitialized instances of SensitiveClass
by catching NotSerializableException
or by using a finalizer attack (see OBJ11-J. Be wary of letting constructors throw exceptions for more information). Consequently, an unserializable class that extends a serializable class must always validate its invariants before executing any methods. That is, any object of such a class must inspect its fields, its actual type (to prevent it being a malicious subclass), and any invariants it possesses (such as being a malicious second object of a singleton class).
Exceptions
SER03-J-SER03:EX0: Sensitive data that has been properly encrypted may be serialized.
Risk Assessment
If sensitive data can be serialized, it may be transmitted over an insecure linkconnection, stored in an insecure location, or disclosed inappropriately.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
SER03-J |
Medium |
Likely |
High | P6 | L2 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
CodeSonar |
| JAVA.CLASS.SER.ND | Serialization Not Disabled (Java) | ||||||
Coverity | 7.5 | UNSAFE_DESERIALIZATION | Implemented | ||||||
Parasoft Jtest |
| CERT.SER03.SIF | Inspect instance fields of serializable objects to make sure they will not expose sensitive information |
Related Guidelines
Serializable Class Containing Sensitive Data |
Deserialization of Untrusted Data |
Guideline |
8-2 / SERIAL-2: Guard sensitive data during serialization |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="34680d22-c109-4276-880f-80cf65332bc8"><ac:plain-text-body><![CDATA[
[ |
] |
Puzzle 83 |
, "Dyslexic monotheism" | |
Item 1, "Enforce the Singleton Property with a Private Constructor" | |
]]></ac:plain-text-body></ac:structured-macro>
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ab419970-2524-4274-810b-0522a2ad6c8f"><ac:plain-text-body><![CDATA[
[[Bloch 2001
AA. Bibliography#Bloch 01]]
Item 1: Enforce the singleton property with a private constructor
]]></ac:plain-text-body></ac:structured-macro>
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="65efc0f8-0bea-439f-a471-4848699afd42"><ac:plain-text-body><![CDATA[
[[Greanier 2000
AA. Bibliography#Greanier 00]]
[Discover the secrets of the Java Serialization API
http://java.sun.com/developer/technicalArticles/Programming/serialization/]
]]></ac:plain-text-body></ac:structured-macro>
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="12772e7b-b07b-413f-bc75-91a7e04db306"><ac:plain-text-body><![CDATA[
[[Harold 1999
AA. Bibliography#Harold 99]]
]]></ac:plain-text-body></ac:structured-macro>
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e16768f8-1d4f-45d9-9982-0a820453c0fc"><ac:plain-text-body><![CDATA[
[[JLS 2005
AA. Bibliography#JLS 05]]
[Transient modifier
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#37020]
]]></ac:plain-text-body></ac:structured-macro>
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="39fc7618-3744-4580-9af7-c909c5b96c53"><ac:plain-text-body><![CDATA[
[[Long 2005
] | Section 2.4, |
]]></ac:plain-text-body></ac:structured-macro>
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d9e159f9-fdc4-4f66-85a5-120e3d1a8daa"><ac:plain-text-body><![CDATA[
[[Sun 2006
AA. Bibliography#Sun 06]]
"Serialization" | |
[Sun 2006] | Serialization Specification, A.4, |
Preventing Serialization of Sensitive Data |
]]></ac:plain-text-body></ac:structured-macro>
...
SER02-J. Sign then seal sensitive objects before sending them outside a trust boundary 13. Serialization (SER)