...
- a memory leak (see MSC04-J. Do not leak memory)
- an infinite loop
- limited amounts of default heap memory available
- incorrect implementation of common data structures (hash tables, vectors, and so on)
- unbound deserialization
- writing a large number of objects to an
ObjectOutputStream
(see SER10-J. Avoid memory and resource leaks during serialization) - creating a large number of threads
- uncompressing a file (see IDS04-J. Limit the size of files passed to ZipInputStream)
...
This noncompliant code example reads lines of text from a file , and adds each one to a vector , until a line with the word "quit" is encountered.
...
Wiki Markup |
---|
The code places no upper bounds on the memory space required to execute the program. Consequently, the program can easily exhaust the available heap space in two ways. First, an attacker can supply arbitrarily many lines in the file, causing the vector to grow until memory is exhausted. Second, an attacker can simply supply an arbitrarily long line, causing the {{readLine()}} method to exhaust memory. According to the Java API \[[API 2006|AA. Bibliography#API 06]\], {{BufferedReader.readLine()}} method documentation: |
Wiki Markup \[{{readLine()}}\] Reads a line of text. A line is considered to be terminated by any one of a line feed ('{{\n}}'), a carriage return ('{{\r}}'), or a carriage return followed immediately by a linefeed.
Any code that uses this method is susceptible to a resource exhaustion attack because the user can enter a string of any length.
Compliant Solution (
...
Limited Length Input)
This compliant solution imposes limits, both on the length of each line , and on the total number of items to add to the vector.
...
The readLimitedLine()
method defined above takes a numeric limit, indicating the total number of characters that may exist on one line. If a line contains more characters, the line is truncated, and they are returned on the next invocation. This prevents an attacker from exhausting memory by supplying input with no line breaks.
Compliant Solution (Java 1.7,
...
Limited File Size)
This compliant solution impose imposes a limit on the size of the file being read. This is accomplished with the Files.size()
method, which is new to Java 1.7. If the file is within the limit, we can assume the standard readLine()
method will not exhaust memory, nor will memory be exhausted by the while
loop.
...
Code Block | ||
---|---|---|
| ||
/** Assuming the heap size as 512 MB (calculated as 1/4th of 2 GB RAM = 512 MB)
* Considering long values being entered (64 bits each, the max number of elements
* would be 512 MB/64bits = 67108864)
*/
public class ShowHeapError {
Vector<Long> names = new Vector<Long>(); // Accepts unknown number of records
long newID = 0L;
int count = 67108865;
int i = 0;
InputStreamReader input = new InputStreamReader(System.in);
Scanner reader = new Scanner(input);
public void addNames() {
try {
do {
// Adding unknown number of records to a list
// The user can enter more IDs than the heap can support and as a result,
// exhaust the heap. Assume that the record ID is a 64 bit long value
System.out.print("Enter recordID (To quit, enter -1): ");
newID = reader.nextLong();
names.addElement(newID);
i++;
} while (i < count || newID != -1);
} finally {
input.close();
}
}
public static void main(String[] args) {
ShowHeapError demo = new ShowHeapError();
demo.addNames();
}
}
|
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="aad5238fb5754a46-7823607b-494e45c0-9e888b18-5c05438bdb52b26686132fe2"><ac:plain-text-body><![CDATA[ | [ISO/IEC TR 24772:2010 | http://www.aitcnet.org/isai/] | "Resource Exhaustion [XZP]" | ]]></ac:plain-text-body></ac:structured-macro> |
CWE-400, "Uncontrolled Resource Consumption ('Resource Exhaustion')" | ||||
| CWE-770, "Allocation of Resources Without Limits or Throttling" |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="cf7bbac8049ca09e-b776859b-4638496f-9af7884f-ebd9ed9d6ee8203d1ed37e3f"><ac:plain-text-body><![CDATA[ | [[Sun 2006 | AA. Bibliography#Sun 06]] | [Garbage Collection Ergonomics | http://java.sun.com/javase/6/docs/technotes/guides/vm/gc-ergonomics.html ], "Default values for the Initial and Maximum heap size" | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="665326af6e00da1a-34b4a2f0-4d004391-a39fafe3-d45c275bc99b064fbd45d061"><ac:plain-text-body><![CDATA[ | [[Java 2006 | AA. Bibliography#Java 06]] | [java - the Java application launcher | http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html ], "Syntax for increasing the heap size" | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="fd590bb1223d1c38-0b6a86e4-40e84e1e-a9298eec-a1d8b36e04efe36aef7679a3"><ac:plain-text-body><![CDATA[ | [[Sun 2003 | AA. Bibliography#Sun 03]] | Chapter 5: Tuning the Java Runtime System, [Tuning the Java Heap | http://docs.sun.com/source/817-2180-10/pt_chap5.html#wp57027] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7ad245ceab6a7d26-6a47b4b3-4a4a41a6-84c28ca2-5f2bbad556e8285aacf842cc"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | Class ObjectInputStream and ObjectOutputStream | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="5c7c561a93ec123f-490c58f7-4f7244d6-956ca96e-6ccb55402f41b04aa202f631"><ac:plain-text-body><![CDATA[ | [[SDN 2008 | AA. Bibliography#SDN 08]] | [Serialization FAQ | http://java.sun.com/javase/technologies/core/basic/serializationFAQ.jsp] | ]]></ac:plain-text-body></ac:structured-macro> |
...