Description
The product does not correctly calculate the size to be used when allocating a buffer, which could lead to a buffer overflow.
Potential Impact
Integrity, Availability, Confidentiality
DoS: Crash, Exit, or Restart, Execute Unauthorized Code or Commands, Read Memory, Modify Memory
Demonstrative Examples
int i;unsigned int numWidgets;Widget **WidgetList;
numWidgets = GetUntrustedSizeValue();if ((numWidgets == 0) || (numWidgets > MAX_NUM_WIDGETS)) {ExitError("Incorrect number of widgets requested!");}WidgetList = (Widget **)malloc(numWidgets * sizeof(Widget *));printf("WidgetList ptr=%p\n", WidgetList);for(i=0; i<numWidgets; i++) {WidgetList[i] = InitializeWidget();}WidgetList[numWidgets] = NULL;showWidgets(WidgetList);img_t table_ptr; /*struct containing img data, 10kB each*/int num_imgs;...num_imgs = get_num_imgs();table_ptr = (img_t*)malloc(sizeof(img_t)*num_imgs);...char * copy_input(char *user_supplied_string){
int i, dst_index;char *dst_buf = (char*)malloc(4*sizeof(char) * MAX_SIZE);if ( MAX_SIZE <= strlen(user_supplied_string) ){die("user string too long, die evil hacker!");}dst_index = 0;for ( i = 0; i < strlen(user_supplied_string); i++ ){
if( '&' == user_supplied_string[i] ){dst_buf[dst_index++] = '&';dst_buf[dst_index++] = 'a';dst_buf[dst_index++] = 'm';dst_buf[dst_index++] = 'p';dst_buf[dst_index++] = ';';}else if ('<' == user_supplied_string[i] ){
/* encode to < */
}else dst_buf[dst_index++] = user_supplied_string[i];
}return dst_buf;
}DataPacket *packet;int numHeaders;PacketHeader *headers;
sock=AcceptSocketConnection();ReadPacket(packet, sock);numHeaders =packet->headers;
if (numHeaders > 100) {ExitError("too many headers!");}headers = malloc(numHeaders * sizeof(PacketHeader);ParsePacketHeaders(packet, headers);Mitigations & Prevention
When allocating a buffer for the purpose of transforming, converting, or encoding an input, allocate enough memory to handle the largest possible encoding. For example, in a routine that converts "&" characters to "&" for HTML entity encoding, the output buffer needs to be at least 5 times as large as the input buffer.
Understand the programming language's underlying representation and how it interacts with numeric calculation (CWE-681). Pay close attention to byte size discrepancies, precision, signed/unsigned distinctions, truncation, conversion and casting between types, "not-a-number" calculations, and how the language handles numbers that are too large or too small for its underlying representation. [REF-7] Also be careful to account for 32-bit, 64-bit, and other potential differences
Perform input validation on any numeric input by ensuring that it is within the expected range. Enforce that the input meets both the minimum and maximum requirements for the expected range.
For any security checks that are performed on the client side, ensure that these checks are duplicated on the server side, in order to avoid CWE-602. Attackers can bypass the client-side checks by modifying values after the checks have been performed, or by changing the client to remove the client-side checks entirely. Then, these modified values would be submitted to the server.
When processing structured incoming data containing a size field followed by raw data, identify and resolve any inconsistencies between the size field and the actual size of the data (CWE-130).
When allocating memory that uses sentinels to mark the end of a data structure - such as NUL bytes in strings - make sure you also include the sentinel in your calculation of the total amount of memory that must be allocated.
Replace unbounded copy functions with analogous functions that support length arguments, such as strcpy with strncpy. Create these if they are not available.
Use sizeof() on the appropriate data type to avoid CWE-467.
Use the appropriate type for the desired action. For example, in C/C++, only use unsigned types for values that could never be negative, such as height, width, or other numbers related to quantity. This will simplify validation and will reduce surprises related to unexpected casting.
Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid [REF-1482]. Use libraries or frameworks that make it easier to handle numbers without unexpected consequences, or buffer allocation routines that automatically track buffer size. Examples include safe integer handling packages such as SafeInt (C++) or IntegerLib (C or C++). [REF-106]
Detection Methods
- Automated Static Analysis High — This weakness can often be detected using automated static analysis tools. Many modern tools use data flow analysis or constraint-based techniques to minimize the number of false positives. Automated static analysis generally does not account for environmental considerations when
- Automated Dynamic Analysis Moderate — This weakness can be detected using dynamic tools and techniques that interact with the software using large test suites with many diverse inputs, such as fuzz testing (fuzzing), robustness testing, and fault injection. The software's operation may slow down, but it should not become unstable, crash
- Automated Dynamic Analysis Moderate — 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].
- Manual Analysis — Manual analysis can be useful for finding this weakness, but it might not achieve desired code coverage within limited time constraints. This becomes difficult for weaknesses that must be considered for all inputs, since the attack surface can be too large.
- Manual Analysis High — This weakness can be detected using tools and techniques that require manual (human) analysis, such as penetration testing, threat modeling, and interactive tools that allow the tester to record and modify an active session. Specifically, manual static analysis is useful for eval
- Automated Static Analysis - Binary or Bytecode High — According to SOAR [REF-1479], the following detection techniques may be useful:
Real-World CVE Examples
| CVE ID | Description |
|---|---|
| CVE-2025-46687 | Chain: Javascript engine code does not perform a length check (CWE-1284) leading to integer overflow (CWE-190) causing allocation of smaller buffer than expected (CWE-131) resulting in a heap-based bu |
| CVE-2025-27363 | Font rendering library does not properly handle assigning a signed short value to an unsigned long (CWE-195), leading to an integer wraparound (CWE-190), c |
| CVE-2020-17087 | Chain: integer truncation (CWE-197) causes small buffer allocation (CWE-131) leading to out-of-bounds write (CWE-787) in kernel pool, as exploited in the wild per CISA KEV. |
| CVE-2004-1363 | substitution overflow: buffer overflow using environment variables that are expanded after the length check is performed |
| CVE-2004-0747 | substitution overflow: buffer overflow using expansion of environment variables |
| CVE-2005-2103 | substitution overflow: buffer overflow using a large number of substitution strings |
| CVE-2005-3120 | transformation overflow: product adds extra escape characters to incoming data, but does not account for them in the buffer length |
| CVE-2003-0899 | transformation overflow: buffer overflow when expanding ">" to ">", etc. |
| CVE-2001-0334 | expansion overflow: buffer overflow using wildcards |
| CVE-2001-0248 | expansion overflow: long pathname + glob = overflow |
| CVE-2001-0249 | expansion overflow: long pathname + glob = overflow |
| CVE-2002-0184 | special characters in argument are not properly expanded |
| CVE-2004-0434 | small length value leads to heap overflow |
| CVE-2002-1347 | multiple variants |
| CVE-2005-0490 | needs closer investigation, but probably expansion-based |
Showing 15 of 17 observed examples.
Related Weaknesses
Taxonomy Mappings
- PLOVER: — Other length calculation error
- CERT C Secure Coding: INT30-C — Ensure that unsigned integer operations do not wrap
- CERT C Secure Coding: MEM35-C — Allocate sufficient memory for an object
Frequently Asked Questions
What is CWE-131?
CWE-131 (Incorrect Calculation of Buffer Size) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Base-level weakness. The product does not correctly calculate the size to be used when allocating a buffer, which could lead to a buffer overflow.
How can CWE-131 be exploited?
Attackers can exploit CWE-131 (Incorrect Calculation of Buffer Size) to dos: crash, exit, or restart, execute unauthorized code or commands, read memory, modify memory. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-131?
Key mitigations include: When allocating a buffer for the purpose of transforming, converting, or encoding an input, allocate enough memory to handle the largest possible encoding. For example, in a routine that converts "&"
What is the severity of CWE-131?
CWE-131 is classified as a Base-level weakness (Medium abstraction). It has been observed in 17 real-world CVEs.