...
Code Block | ||
---|---|---|
| ||
class ReadNames { private Vector<String> names = new Vector<String>(); private final InputStreamReader input; private final BufferedReader reader; public ReadNames(String filename) throws IOException { this.input = new FileReader(filename); this.reader = new BufferedReader(input); } public void addNames() throws IOException { try { String newName; while (((newName = reader.readLine()) != null) && !(newName.equalsIgnoreCase("quit"))) { names.addElement(newName); System.out.println("adding " + newName); } } finally { input.close(); } } public static void main(String[] args) throws IOException { if (args.length != 1) { System.out.println("Arguments: [filename]"); return; } ReadNames demo = new ReadNames(args[0]); demo.addNames(); } } |
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 {{ Wiki Markup readLine()
}} method to exhaust memory. According to the Java API documentation \ [[API 2006|AA. References#API 06]\], the {{BufferedReader.readLine()
}} method
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.
...
Noncompliant Code Example
In a server-class machine using a parallel garbage collector, the default initial and maximum heap sizes are as follows for Java SE 6 \[ [Sun 2006|AA. References#Sun 06]\]: Wiki Markup
- initial heap size: larger of 1/64 of the machine's physical memory or some reasonable minimum.
- maximum heap size: smaller of 1/4 of the physical memory or 1GB.
...
Code Block | ||
---|---|---|
| ||
// ... int count = 10000000; // ... |
Compliant Solution
The {{ Wiki Markup OutOfMemoryError
}} can be avoided by ensuring the absence of infinite loops, memory leaks, and unnecessary object retention. When memory requirements are known ahead of time, the heap size can be tailored to fit the requirements using the following runtime parameters \[ [Java 2006|AA. References#Java 06]\]:
java -Xms<initial heap size> -Xmx<maximum heap size>
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="74bc461e-9583-4f93-9391-d681fa6f0ccd"><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 |
Bibliography
Resource Exhaustion [XZP] | |
CWE-400. Uncontrolled resource consumption ("resource exhaustion") | |
| CWE-770. Allocation of resources without limits or throttling |
Bibliography
[API 2006] | Class | ||||
java – The Java application launcher, Syntax for increasing the heap size | |||||
[SDN 2008] | |||||
[Sun 2003] | Chapter 5, Tuning the Java Runtime System, Tuning the Java Heap | ||||
[Sun 2006] | |||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="39dda6cc-426b-4371-b6c5-c23c98be45f3"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. References#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="1d50f10e-c603-47a4-bcb6-4f1640f755e7"><ac:plain-text-body><![CDATA[ | [[Java 2006 | AA. References#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="19d31a1a-3955-494c-81bf-4cd88c6f313b"><ac:plain-text-body><![CDATA[ | [[SDN 2008 | AA. References#SDN 08]] | [Serialization FAQ | http://java.sun.com/javase/technologies/core/basic/serializationFAQ.jsp] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="223363ab-c35c-4bdc-a676-a5456e5125c4"><ac:plain-text-body><![CDATA[ | [[Sun 2003 | AA. References#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="e4407604-3785-42ae-b417-41baf2ecf9d5"><ac:plain-text-body><![CDATA[ | [[Sun 2006 | AA. References#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> |
...