For a non-final class, if a constructor throws an exception before fully initializing the object, it becomes possible to maliciously obtain its instance. For example, an attack that uses the finalizer construct allows an attacker to invoke arbitrary methods within the class despite authorization measures.
Noncompliant Code Example
Wiki Markup |
---|
This noncompliant code example, based on an example by Kabutz \[[Kabutz 01|AA. Java References#Kabutz 01]\], defines the constructor of {{BankOperations}} class so that it performs SSN verification using the method {{performSSNVerification()}}. Assume that an attacker does not know the correct SSN. As a result, this method trivially returns {{false}} in this example. |
...
The attacker's code violates the guideline OBJ02-J. Avoid using finalizers.
Compliant Solution
This compliant solution declares the partially-initialized class final
so that it cannot be extended.
Code Block | ||
---|---|---|
| ||
public final class BankOperations { // ... } |
Noncompliant Code Example
The Javabeans pattern uses a no-argument constructor along with a series of parallel setter methods to build an object. This pattern is not thread-safe and can lead to inconsistent object state. Moreover, it permits another thread to access the object even though it may only be partially initialized (not all required fields are initialized).
Code Block | ||
---|---|---|
| ||
class UnsafeCurrency { // total amount requested (required) private int dollars = -1; // initialize to default value private int cents = -1; // initialize to default value // change requested, denomination (optional) private int quarters = 0; private int dimes = 0; private int nickels = 0; private int pennies = 0; public UnsafeCurrency() {} // no argument constructor // setter methods public void setDollar(int amount) { dollars = amount; } public void setCents(int amount) { cents = amount; } public void setQuarters(int quantity) { quarters = quantity; } public void setDimes(int quantity) { dimes = quantity; } public void setNickels(int quantity) { nickels = quantity; } public void setPennies(int quantity) { pennies = quantity; } } |
Compliant Solution
Wiki Markup |
---|
Use the Builder pattern's \[[Gamma 95|AA. Java References#Gamma 95]\] variant suggested by Bloch \[[Bloch 08|AA. Java References#Bloch 08]\] to ensure thread safety and atomicity of object creation. The idea is to call the constructor with the _required_ parameters and obtain a _builder_ object. Each _optional_ parameter can be set using setters on the builder. The object construction concludes with the invocation of the {{build()}} method. This also has the effect of making the class {{Currency}} immutable. |
...
If input has to be validated, make sure that the values are copied from the builder class to the containing class's fields prior to checking. The builder class does not violate SCP03-J. Do not expose sensitive private members of the outer class from within a nested class because it maintains a copy of the variables defined in the scope of the containing class. These take precedence and as a result do not break encapsulation.
Exceptions
EX1: When the API cannot be extended (consider a non-final class, for example), it is permissible to use a flag to signal the successful completion of object construction. This is shown below.
...
EX2: It is permissible to use the telescoping pattern when the overhead of the builder pattern is significant as compared to the number of parameters required to be initialized. This pattern prescribes a constructor to initialize the required parameters and individual constructors for each optional parameter that is added.
Risk Assessment
Allowing a partially initialized object to be accessed can provide an attacker with an opportunity to resurrect the object before its finalization and bypass any security checks.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
OBJ32- J | high | probable | medium | P12 | L1 |
Automated Detection
TODO
Related Vulnerabilities
Vulnerability CVE-2008-5339 concerns a series of vulnerabilities in Java. In one of the vulnerabilities, an applet causes an object to be deserialized using ObjectInputStream.readObject()
, but the input is controlled by an attacker. The object actually read is a serializable subclass of ClassLoader
, and it has a readObject()
method that stashes the object instance into a static variable; consequently the object survives the serialization. As a result, the applet has managed to construct a ClassLoader
object, by-passing the restrictions against doing so in an applet, and that ClassLoader
allows it to construct classes that are not subject to the security restrictions of an applet. The vulnerability is described in depth in SER37-J. Do not deserialize from a privileged context.
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
Wiki Markup |
---|
\[[JLS 05|AA. Java References#JLS 05]\] Section 12.6, Finalization of Class Instances \[[API 06|AA. Java References#API 06]\] [finalize()|http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#finalize()] \[[SCG 07|AA. Java References#SCG 07]\] Guideline 4-2 Defend against partially initialized instances of non-final classes \[[Kabutz 01|AA. Java References#Kabutz 01]\] Issue 032 - Exceptional Constructors - Resurrecting the dead \[[Bloch 08|AA. Java References#Bloch 08]\] Item 7, Avoid finalizers \[[Darwin 04|AA. Java References#Darwin 04]\] Section 9.5, The Finalize Method \[[Flanagan 05|AA. Java References#Flanagan 05]\] Section 3.3, Destroying and Finalizing Objects |
...