Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Parasoft Jtest 2021.1

...

This noncompliant code example allows any caller to reset the value of the object at any time because the readExternal() method is necessarily declared to be public and lacks protection against hostile callers.:

Code Block
bgColor#FFcccc

public void readExternal(ObjectInput in) 
                         throws IOException, ClassNotFoundException {
   // Read instance fields
   this.name = (String) in.readObject();
   this.UID = in.readInt();
   // ...
}

Compliant Solution

This compliant solution protects against multiple initialization through the use of a Boolean flag that is set after the instance fields have been populated. It also protects against race conditions by synchronizing on a private lock object (see rule LCK00-J. Use private final lock objects to synchronize classes that may interact with untrusted code).

Code Block
bgColor#ccccff

private final Object lock = new Object();
private boolean initialized = false;

public void readExternal(ObjectInput in)
                         throws IOException, ClassNotFoundException {
  synchronized (lock) {
    if (!initialized) {
      // Read instance fields
      this.name = (String) in.readObject();
      this.UID = in.readInt();
      // ...  
      initialized = true;
    } else {
      throw new IllegalStateException();
    }
  }
}

Note that this compliant solution is inadequate to protect sensitive data.

Risk Assessment

Failure to prevent the overwriting of an externalizable object can corrupt the state of the object.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

SER11-J

low

Low

probable

Probable

low

Low

P6

L2

Bibliography

Automated Detection

ToolVersionCheckerDescription
Parasoft Jtest
Include Page
Parasoft_V
Parasoft_V
CERT.SER11.IRXAvoid re-initializing fields in the 'readExternal()' method of 'Externalizable' classes

Bibliography

[API 2014]


[Sun 2006

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a9aa611b-9d14-4f9f-b582-923bbee15b14"><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="4651bd6f-0b22-4900-9deb-875e3c671cfc"><ac:plain-text-body><![CDATA[

[[Sun 2006

AA. Bibliography#Sun 06]

]

Serialization Specification, A.7, Preventing Overwriting of Externalizable Objects

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


...

Image Removed      13. Serialization (SER)      14. Platform Security (SEC)Image Added Image Added Image Added