
Buffer classes defined in the java.nio
package, such as IntBuffer
, CharBuffer
and ByteBuffer
, define a variety of wrap()
methods that wrap an array of some primitive data type into a buffer, and return the buffer as a Buffer
object. Although these methods create a new Buffer
object, the new Buffer
is backed by the same given input array. According to the Java API for these methods [[API 2006]]:
The new buffer will be backed by the given character array; that is, modifications to the buffer will cause the array to be modified and vice versa.
Exposing these buffers to untrusted code exposes the backing array to malicious modification. Likewise, the duplicate()
methods create additional buffers that are backed by the original buffer's backing array; exposing such additional buffers to untrusted code affords the same opportunity for malicious modification of the contents of the original buffer's backing store.
Noncompliant Code Example (wrap()
)
This noncompliant code example declares a char
array, wraps it within a Buffer
and exposes that Buffer
to untrusted code via the getBufferCopy()
method.
final class Wrap { private char[] dataArray; public Wrap () { dataArray = new char[10]; // Initialize } public CharBuffer getBufferCopy() { return CharBuffer.wrap(dataArray); } }
Compliant Solution (asReadOnlyBuffer()
)
This compliant solution returns a read-only view of the char
array, in the form of a read-only CharBuffer
. The standard library implementation of CharBuffer
guarantees that attempts to modify the elements of a read-only CharBuffer
will result in a java.nio.ReadOnlyBufferException
.
final class Wrap { private char[] dataArray; public Wrap () { dataArray = new char[10]; // Initialize } public CharBuffer getBufferCopy() { return CharBuffer.wrap(dataArray).asReadOnlyBuffer(); } }
Compliant Solution (Copy)
This compliant solution allocates a new CharBuffer
and explicitly copies the contents of the char
array into it, before returning the copy. Consequently, malicious callers can modify the copy of the array, but cannot modify the original.
final class Wrap { private char[] dataArray; public Wrap () { dataArray = new char[10]; // Initialize } public CharBuffer getBufferCopy() { CharBuffer cb = CharBuffer.allocate(dataArray.length); cb.put(dataArray); return cb; } }
Noncompliant Code Example (duplicate()
)
This noncompliant code example invokes the duplicate()
method to create and return a copy of the CharBuffer
. As stated in the contract for the duplicate()
method, the returned buffer is backed by the same array as is the original buffer. Consequently, a caller can modify the elements of the backing array; these modifications also affect the original buffer.
final class Dup { CharBuffer cb; public Dup() { cb = CharBuffer.allocate(10); // Initialize } public CharBuffer getBufferCopy() { return cb.duplicate(); } }
Compliant Solution (asReadOnlyBuffer()
)
This compliant solution exposes a read-only view of the CharBuffer
to untrusted code.
final class Dup { CharBuffer cb; public Dup() { cb = CharBuffer.allocate(10); // Initialize } public CharBuffer getBufferCopy() { return cb.asReadOnlyBuffer(); } }
Risk Assessment
Returning buffers created using the wrap()
or duplicate()
methods may allow an untrusted caller to alter the contents of the original data.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
FIO01-J |
medium |
likely |
low |
P18 |
L1 |
Automated Detection
Sound automated detection of this vulnerability is not feasible. Heuristic approaches may be useful.
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="562f7db6-8467-4520-94c8-177002bb4065"><ac:plain-text-body><![CDATA[ |
[[API 2006 |
AA. Bibliography#API 06]] |
class |
]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a064c8af-1c47-4f26-b887-3d91b1d184b3"><ac:plain-text-body><![CDATA[ |
[[Hitchens 2002 |
AA. Bibliography#Hitchens 02]] |
2.3 Duplicating Buffers |
]]></ac:plain-text-body></ac:structured-macro> |
FIO00-J. Do not overwrite an existing file while attempting to create a new file 12. Input Output (FIO) FIO02-J. Ensure the array is filled when using read() to fill an array