While 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 cannot guarantee the integrity of the deserialized data.
Examples of sensitive data that should 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.
public class Point { private double x; private double y; public Point(double x, double y) { this.x = x; this.y = y; } public Point() { // no-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 } } } } }
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 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 serialization of data members that contain sensitive data and serialization of references to non-serializable or sensitive objects.
This compliant solution both avoids the possibility of incorrect serialization and 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.
public class Point { private transient double x; // declared transient private transient double y; // declared transient public Point(double x, double y) { this.x = x; this.y = y; } public Point() { // no-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); 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()
, and writeExternal()
methods that prevent sensitive fields from being written to the serialized stream.
*defining the serialPersistentFields
array field and ensuring that sensitive fields are omitted from the array. (See rule SER00-J. Maintain serialization compatibility during class evolution.)
Noncompliant Code Example
Serialization can be used maliciously, for example, to return multiple instances of a singleton class object. In this noncompliant code example (based on [[Bloch 2005]]), a subclass SensitiveClass
inadvertently becomes serializable because it extends the java.lang.Number
class, which implements Serializable
.
public class SensitiveClass extends Number { // ..implement abstract methods, such as Number.doubleValue()⦠private static final SensitiveClass INSTANCE = new SensitiveClass(); public static SensitiveClass getInstance() { return INSTANCE; } private SensitiveClass() { // Perform security checks and parameter validation } protected int getBalance() { int balance = 1000; return balance; } } class Malicious { public static void main(String[] args) { SensitiveClass sc = (SensitiveClass) deepCopy(SensitiveClass.getInstance()); // Prints false; indicates new instance System.out.println(sc == SensitiveClass.getInstance()); System.out.println("Balance = " + sc.getBalance()); } // This method should not be used in production 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 about 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 a serializable class by an unserializable class is necessary, inappropriate serialization of the subclass can be prohibited by throwing NotSerializableException
from custom writeObject()
, readObject()
, and readObjectNoData()
methods, defined in the nonserializable subclass. These custom methods must be declared private or final to prevent a malicious subclass from overriding them.
class SensitiveClass extends Number { // ... protected final Object writeObject(java.io.ObjectOutputStream out) throws NotSerializableException { throw new NotSerializableException(); } protected final Object readObject(java.io.ObjectInputStream in) throws NotSerializableException { throw new NotSerializableException(); } protected final Object readObjectNoData(java.io.ObjectInputStream in) throws NotSerializableException { throw new NotSerializableException(); } }
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. Therefore an unserializable class that extends a serializable class must always validate itself before executing any methods.
Exceptions
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 connection, stored in an insecure location, or disclosed inappropriately.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
SER03-J |
medium |
likely |
high |
P6 |
L2 |
Related Guidelines
CWE-499. Serializable class containing sensitive data |
|
|
CWE-502. Deserialization of untrusted data |
Secure Coding Guidelines for the Java Programming Language, Version 3.0 |
Guideline 5-2. Guard sensitive data during serialization |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="eab0dbcd-028b-4845-8697-d8ce82030699"><ac:plain-text-body><![CDATA[ |
[[Bloch 2005 |
AA. References#Bloch 05]] |
Puzzle 83. Dyslexic monotheism |
]]></ac:plain-text-body></ac:structured-macro> |
|
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4e7ac38d-0cf8-449f-ab95-ed0262bcf487"><ac:plain-text-body><![CDATA[ |
[[Bloch 2001 |
AA. References#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="51892033-38d0-4b12-92bb-0c29435169f2"><ac:plain-text-body><![CDATA[ |
[[Greanier 2000 |
AA. References#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="b4d11ec5-6205-43a1-a0c8-d40e9d91dbdf"><ac:plain-text-body><![CDATA[ |
[[Harold 1999 |
AA. References#Harold 99]] |
|
]]></ac:plain-text-body></ac:structured-macro> |
|
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="541cd70c-8ebe-48ef-bfd0-8339975cf5eb"><ac:plain-text-body><![CDATA[ |
[[JLS 2005 |
AA. References#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="564ab49f-2b07-4aaa-ab40-979bb80e4cba"><ac:plain-text-body><![CDATA[ |
[[Long 2005 |
AA. References#Long 05]] |
Section 2.4, Serialization |
]]></ac:plain-text-body></ac:structured-macro> |
|
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a34e7f68-bb5d-4162-a197-0d567cf76217"><ac:plain-text-body><![CDATA[ |
[[Sun 2006 |
AA. References#Sun 06]] |
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)