Security checks based on untrusted sources can be bypassed. The untrusted object or parameter should be defensively copied before the security check is performed. The copy operation must be a deep copy; the implementation of the clone()
method may produce a shallow copy, which can still be compromised. In addition, the implementation of the clone()
method can be provided by the attacker. See OBJ06-J. Defensively copy mutable inputs and mutable internal components for more information.
Noncompliant Code Example
...
This noncompliant code example describes a security vulnerability from the JDK 5.0 java.io
package. In this release, java.io.File
was non-final, allowing an attacker to supply an untrusted parameter constructed by extending the legitimate File
class. In this manner, the getPath()
method can be overridden so that the security check passes the first time it is called but the value changes the second time to refer to a sensitive file such as /etc/passwd
. This is a form of time-of-check-time-of-use (TOCTOU) vulnerability.
...
Code Block |
---|
public class BadFile extends java.io.File { private int count; public String getPath() { return (++count == 1) ? "/tmp/foo" : "/etc/passwd"; } } |
Compliant Solution (
...
Final)
This vulnerability could have been can be mitigated by making java.io.File
final.
Compliant Solution (
...
Copy)
This compliant solution ensures that the java.io.File
object can be trusted, despite not being final. The solution creates a new File
object using the standard constructor. This ensures that any methods invoked on the File
object are the standard library methods rather than overriding methods potentially provided by the attacker.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="33043fd945210363-b7a64ba7-4e5a4b13-ad0ca666-689bcea81ef003a4ff339aaa"><ac:plain-text-body><![CDATA[ | [ISO/IEC TR 24772:2010 | http://www.aitcnet.org/isai/] | "Authentication Logic Error [XZO]" | ]]></ac:plain-text-body></ac:structured-macro> |
CWE ID 302, "Authentication Bypass by Assumed-Immutable Data" | ||||
| CWE ID 470, "Use of Externally-Controlled Input to Select Classes or Code ('Unsafe Reflection')" |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="dc48907b0b6e6622-7ab49b57-4499422f-876d9ae1-edce27cc298ec4b9c8bed0ff"><ac:plain-text-body><![CDATA[ | [[Sterbenz 2006 | AA. Bibliography#Sterbenz 06]] | ]]></ac:plain-text-body></ac:structured-macro> |
...