Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Some of these causes are platform-dependent, and difficult to anticipate. Others are fairly easy to anticipate, such as reading data from a file. As a result, programs must not accept untrusted input in a manner that can cause the program to exhaust memory.

Noncompliant Code Example (readLine())

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.

...

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 (Java 1.7, Limited File Size)

This compliant solution 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
bgColor#ccccff
class ShowHeapError {
  static public final int fileSizeLimit = 1000000;

  public ShowHeapError(String filename) throws IOException {
    if (Files.size(Paths.get(filename)) > fileSizeLimit) {
      throw new IOException("File too large");
    }
    this.input = new FileReader(filename);
    this.reader = new BufferedReader(input);
  }

  // ...other methods
}

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. (It does not depend on any Java 1.7 features.)

...

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.

Noncompliant Code Example

Wiki Markup
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. Bibliography#Sun 06]\]:

...

Code Block
bgColor#FFcccc
/** 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();
  }
}

Compliant Solution

A simple compliant solution is to reduce the number of names to read.

Code Block
bgColor#ccccff
  // ...
  int count = 10000000;
  // ...

Compliant Solution

Wiki Markup
The {{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. Bibliography#Java 06]\]:

...

This setting can be changed either using the Java Control Panel or from the command line. It cannot be adjusted through the application itself.

Risk Assessment

Assuming that infinite heap space is available can result in denial of service.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

MSC05-J

low

probable

medium

P4

L3

Related Vulnerabilities

The Apache Geronimo bug described by GERONIMO-4224 results in an OutOfMemoryError exception thrown by the WebAccessLogViewer if the access log file size is too large, such as more than 200 MB.

Related Guidelines

CERT C Secure Coding Standard

MEM11-C. Do not assume infinite heap space

CERT C++ Secure Coding Standard

MEM12-CPP. Do not assume infinite heap space

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a9a413be4e601eaf-5a6c2e80-4e574053-b978b9b5-bc7989d0f7fee7dd0ef44030"><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>

MITRE CWE

CWE-400, "Uncontrolled Resource Consumption ('Resource Exhaustion')"

 

CWE-770, "Allocation of Resources Without Limits or Throttling"

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f62afdff5692900b-c9bba67f-48f04167-a286965f-515553ea4a50b208d184518b"><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="8980debf51b2e7cf-1984d93e-42e345bd-8a8897d2-625dd8684343e2dfd0e27837"><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="8d898e6850fb0fe5-99f02d71-4f7748c8-8b2ab8af-a8c29582f0dda0483798e7a6"><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>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4c18a33c7af32ecd-b9de75a3-4eac4043-9018b8c4-6f2d2b317b5dd455bf7395a0"><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="45d0049e231dfc5b-b45c5e22-4f6f428c-8175902d-83023aa5defe861b3b72f2b9"><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>

...