Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Object serialization is the process of saving Once an object 's state as a sequence of bytes; deserialization is the process of rebuilding the object from those bytes. The primary application of serialization is in Java Remote Method Invocation (RMI) wherein objects are packed (marshalled), exchanged between distributed virtual machines, and unpacked (unmarshalled). It also finds extensive use in Java Beans.Once a serializable a particular class has been exportedserialized, future refactoring of its the class's code often becomes problematic. Specifically, existing serialized forms (encoded representations) become part of the object's published API and must be supported for an indefinite period. This can be troublesome from a security perspective; not only does it promote dead code, it also commits forces the provider to potentially eternally maintain a compatible codebase for the lifetime of their products.

Classes that implement Serializable and fail to override without overriding its functionality are said to be using the default serialized form. In the event of future changes to the class changes, byte streams produced by users of old versions of the class will be become incompatible with the new implementation. Programs must maintain serialization compatibility during class evolution. An acceptable approach is the use of Consequently, serializable classes that rely on the default serialized form cannot be evolved without compromising compatibility.

To enable compatible evolution of a serializable class, developers must use a custom serialized form, which relieves the implementer of the necessity to maintain the original serialized form and the corresponding version of the class in addition to the newly evolved versionis more flexible than a default form. Specifically,

  • Use of a custom form severs the dependence of the stream format on the code of the implementing class.
  • The code generated for deserializing a custom form can handle compatible deviations from the serialized form, like extra fields.

As a result, developers need neither maintain the earlier version of the code nor explicitly support the original serialized form.

Note that compliance with this rule, while necessary, is not sufficient to guarantee compatible evolution of serializable classes. For a full discussion of compatible evolution of serializable classes, see the Java Object Serialization Specification (version 6), Chapter 5, "Versioning of Serializable Objects" [Sun 2006].

Noncompliant Code Example

This noncompliant code example implements a GameWeapon class with a serializable field called noOfWeapons, numOfWeapons and uses the default serialization serialized form. Any changes to the internal representation of the class can break the existing serialized form.

Code Block
bgColor#FFcccc

class GameWeapon implements Serializable {
  int noOfWeaponsnumOfWeapons = 10;
	    
  public String toString() {
    return String.valueOf(noOfWeaponsnumOfWeapons);
  }
}

Because this class does not provide a serialVersionUID, the Java Virtual Machine (JVM) assigns it one using implementation-defined methods. If the class definition changes, the serialVersionUID is also likely to change. Consequently, and the JVM will refuse to associate the serialized form of an object with the class definition if when the version IDs are different.

Compliant Solution (serialVersionUID)

In this solution, the class has an explicit serialVersionUID which that contains a number unique to this version of the class. The JVM will make a good-faith effort to deserialize any serialized object with the same class name and version ID.

Code Block
bgColor#ccccff

class GameWeapon implements Serializable {
  private static final long serialVersionUID = 24L;

  int noOfWeaponsnumOfWeapons = 10;
	    
  public String toString() {
    return String.valueOf(noOfWeaponsnumOfWeapons);
  }
}

Compliant Solution (serialPersistentFields)

Ideally, implement Serializable only when the class is not expected to evolve frequently should be implemented only for stable classes. One way to maintain the original serialized form , at the same time allowing and allow the class to evolve is to use custom serialization with the help of serialPersistentFields. The static and transient fields allow one to specify what qualifiers specify which fields should not be serialized, whereas the serialPersistentFields field specifies what which fields should be serialized. It also relieves the class from defining the serializable field within the class implementation, decoupling the current implementation from the overall logic. New fields can easily be added without breaking compatibility across releases.

Code Block
bgColor#ccccff

class WeaponStore implements Serializable {
  int noOfWeaponsnumOfWeapons = 10; // Total number of weapons	
}

public class GameWeapon implements Serializable {
  WeaponStore ws = new WeaponStore();
  private static final ObjectStreamField[] serialPersistentFields
      = {new ObjectStreamField("ws", WeaponStore.class)};

  private void readObject(ObjectInputStream ois)
 throws  IOException {
  throws IOException, tryClassNotFoundException {
      ObjectInputStream.GetField gf = ois.readFields();
      this.ws = (WeaponStore) gf.get("ws", ws);
    } catch (ClassNotFoundException e) { /* Forward to handler */ }
  }
	 
  private void writeObject(ObjectOutputStream oos) throws IOException {
    ObjectOutputStream.PutField pf = oos.putFields();
    pf.put("ws", ws);
    oos.writeFields();
  }
	 
  public String toString() {
    return String.valueOf(ws);
  }
}

Exceptions

Wiki Markup
*SER00-EX1*: According to the Serialization Specification \[[Sun 2006|AA. Bibliography#Sun 06]\], Section 1.5, "Defining Serializable Fields for a Class"

...

Finally, serialization is easy to get wrong and must consequently be carefully implemented.

Risk Assessment

Failure to provide a consistent serialization mechanism across releases can limit the extensibility of classes. If classes are extended, compatibility issues may result.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

SER00-J

low

Low

probable

Probable

high

High

P2

L3

Automated Detection

Automated detection of classes that use the default serialized form is straightforward.

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3af3332c-06eb-4a05-afad-29934c573ec7"><ac:plain-text-body><![CDATA[

[[MITRE 2009

AA. Bibliography#MITRE 09]]

[CWE ID 589

http://cwe.mitre.org/data/definitions/589.html] "Call to Non-ubiquitous API"

]]></ac:plain-text-body></ac:structured-macro>

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0ca5c14b-7358-4b1a-9fb9-b6a6c81e1f6d"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

 

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3e096a8f-407b-4dd9-a60c-13c20e6ef92a"><ac:plain-text-body><![CDATA[

[[Sun 2006

AA. Bibliography#Sun 06]]

"Serialization specification", "1.5 Defining Serializable Fields for a Class" and "1.7 Accessing Serializable Fields of a Class"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d77c4f63-b979-4663-8df4-3f6a5ba0332b"><ac:plain-text-body><![CDATA[

[[Bloch 2008

AA. Bibliography#Bloch 08]]

Item 74: "Implement serialization judiciously"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9a285ebd-aff7-4c65-947f-18ef12ff8ec9"><ac:plain-text-body><![CDATA[

[[Harold 2006

AA. Bibliography#Harold 06]]

13.7.5. serialPersistentFields

]]></ac:plain-text-body></ac:structured-macro>

ToolVersionCheckerDescription
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

JAVA.CLASS.SER.UIDM

Missing Serial Version Field (Java)

Parasoft Jtest

Include Page
Parasoft_V
Parasoft_V

CERT.SER00.DUIDCreate a 'serialVersionUID' for all 'Serializable' classes
SonarQube
Include Page
SonarQube_V
SonarQube_V
S2057"Serializable" classes should have a "serialVersionUID"


Related Guidelines

MITRE CWE

CWE-589, Call to Non-ubiquitous API

Bibliography

[API 2014]


[Bloch 2008]

Item 74, "Implement Serialization Judiciously"

[Harold 2006]

Section 13.7.5, "serialPersistentFields"

[Sun 2006]

Java Object Serialization Specification


...

Image Added Image Added 13. Serialization (SER)      13. Serialization (SER)