CWE-789: Memory Allocation with Excessive Size Value

Description:
The product allocates memory based on an untrusted, large size value, but it does not ensure that the size is within expected limits, allowing arbitrary amounts of memory to be allocated.

Affected languages: C, C++, Not Language-Specific
Parent weaknesses: CWE-770

Consequences:
  - Availability: DoS: Resource Consumption (Memory)

Detection methods:
  - Fuzzing: Fuzz testing (fuzzing) is a powerful technique for generating large numbers of diverse inputs - either randomly or algorithmically - and dynamically invoking the code with those inputs. Even with rand
  - Automated Static Analysis: Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without havin
  - Automated Dynamic Analysis: Use tools that are integrated during
	     compilation to insert runtime error-checking mechanisms
	     related to memory safety errors, such as AddressSanitizer
	     (ASan) for C/C++ [REF-1518].

Mitigations:
  - [Implementation] Perform adequate input validation against any value that influences the amount of memory that is allocated. Define an appropriate strategy for handling requests that exceed the limit, and consider supporting a configuration option so that the administrator can extend the amount of memory to be used 
  - [Operation] Run your program using system-provided resource limits for memory. This might still cause the program to crash or exit, but the impact to the rest of the system will be minimized.

Demonstrative examples:
Consider the following code, which accepts an untrusted size value and allocates a buffer to contain a string of the given size.
Suppose an attacker provides a size value of:
unsigned int size = GetUntrustedInt(); 
                      /* ignore integer overflow (CWE-190) for this example */ 
                      
                      unsigned int totBytes = size * sizeof(char); char *string = (char *)malloc(totBytes); InitializeString(string);

Consider the following code, which accepts an untrusted size value and uses the size as an initial capacity for a HashMap.
The HashMap constructor will verify that the initial capacity is not negative, however there is no check in place to verify that sufficient memory is present. If the attacker provides a large enough value, the application will run into an OutOfMemoryError.
unsigned int size = GetUntrustedInt(); HashMap list = new HashMap(size);

Real-world examples:
  - CVE-2023-2253: Query capability for API endpoint allows a large value for number of records to return, leading to allocation of a large array
  - CVE-2019-19911: Chain: Python library does not limit the resources used to process images that specify a very large number of bands (CWE-1284), leading to excessive memory consumption (CWE-789) or an integer overflow
  - CVE-2010-3701: program uses ::alloca() for encoding messages, but large messages trigger segfault
  - CVE-2008-1708: memory consumption and daemon exit by specifying a large value in a length field
  - CVE-2008-0977: large value in a length field leads to memory consumption and crash when no more memory is available