...
A TOCTOU inconsistency exists in this noncompliant code example. As cookie
is a mutable input, an attacker may cause the cookie to expire between the initial check and the actual use.
Code Block | ||
---|---|---|
| ||
import java.net.HttpCookie; public final class MutableDemo { // java.net.HttpCookie is mutable public void useMutableInput(HttpCookie cookie) { if (cookie == null) { throw new NullPointerException(); } //check Check if cookie has expired if(cookie.hasExpired()) { //cookie Cookie is no longer valid, handle condition } // Cookie may have expired since time of check resulting in // an exception doLogic(cookie); } } |
...
The problem is alleviated by creating a copy of the mutable input and using it to perform operations so that the original object is left unscathed. This can be realized by implementing the java.lang.Cloneable
interface and declaring a public
clone method or by using a copy constructor. Performing a manual copy of object state within the caller becomes necessary if the mutable class is declared final
(that is, it cannot provide an accessible copy method) (. See the guideline OBJ36-J. Provide mutable classes with a clone method to allow passing instances to untrusted code safely) for more information. Note that any input validation must be performed on the copy and not the original object.
Code Block | ||
---|---|---|
| ||
import java.net.HttpCookie; public final class MutableDemo { // java.net.HttpCookie is mutable public void useMutableInput(HttpCookie cookie) { if (cookie == null) { throw new NullPointerException(); } // createCreate copy cookie = (HttpCookie)cookie.clone(); //check Check if cookie has expired if(cookie.hasExpired()) { //cookie Cookie is no longer valid, handle condition } doLogic(cookie); } } |
...
Sometimes, the copy constructor or the clone()
method returns a shallow copy of the original instance. For example, invocation of clone()
on an array results in creation of an array instance that shares references to the same elements as the original instance. However, a deep copy that involves element duplication is required when the input consists of mutable components, such as an array of cookies. This compliant solution exemplifies this condition.
Code Block | ||
---|---|---|
| ||
public void deepCopy(int[] ints, HttpCookie[] cookies) { if (ints == null || cookies == null) { throw new NullPointerException(); } // shallowShallow copy int[] intsCopy = ints.clone(); // deepDeep copy HttpCookie[] cookiesCopy = new HttpCookie[cookies.length]; for (int i = 0; i < cookies.length; i++) { // manuallyManually create copy of each element in array cookiesCopy[i] = (HttpCookie)cookies[i].clone(); } doLogic(intsCopy, cookiesCopy); } |
...
When the mutable input type is non-final, a malicious subclass may override the clone()
method. This is a serious issue unless the non-final input defends against it. This noncompliant example shows such a vulnerable code snippetcode example demonstrates this weakness.
Code Block | ||
---|---|---|
| ||
// java.util.ArrayList is mutable and non-final public void copyNonFinalInput(ArrayList list) { doLogic(list); } |
Compliant Solution
In order to To copy mutable inputs having a non-final type, create a new instance of the ArrayList
. This instance can now be forwarded to any code capable of modifying it.
Code Block | ||
---|---|---|
| ||
// java.util.ArrayList is mutable and non-final public void copyNonFinalInput(ArrayList list) { // createCreate new instance of declared input type list = new ArrayList(list); doLogic(list); } |
...
Code Block | ||
---|---|---|
| ||
public void copyInterfaceInput(Collection<String> collection) { // convertConvert input to trusted implementation collection = new ArrayList(collection); doLogic(collection); } |
Some objects appear to be immutable because they have no mutator methods. For example, the java.lang.CharacterSequence
interface describes an immutable sequence of characters. It should be noted that if the underlying implementation on which the CharacterSequence
is based changes, the value of the CharacterSequence
also changes. Such objects must be defensively copied before use. It is also permissible to use the toString()
method to make them immutable before passing them as parameters. Mutable fields must always never be stored in static
variables. To avoid exposing mutable fields by storing them in static
variables, creating defensive copies of the fields is highly recommended.
...