Failure to Constrain Operations within the Bounds of a Memory Buffer |
Weakness ID: 119 (Weakness Class) | Status: Usable |
Description Summary
Extended Description
Certain languages allow direct addressing of memory locations and do not automatically ensure that these locations are valid for the memory buffer that is being referenced. This can cause read or write operations to be performed on memory locations that may be associated with other variables, data structures, or internal program data.
As a result, an attacker may be able to execute arbitrary code, alter the intended control flow, read sensitive information, or cause the system to crash.
Memory Corruption: | The generic term "memory corruption" is often used to describe the consequences of writing to memory outside the bounds of a buffer, when the root cause is something other than a sequential copies of excessive data from a fixed starting location (i.e., classic buffer overflows or CWE-120). This may include issues such as incorrect pointer arithmetic, accessing invalid pointers due to incomplete initialization or memory release, etc. |
---|
Languages
C: (Often)
C++: (Often)
Assembly
Languages without memory management support
Platform Notes
It is possible in many programming languages to attempt an operation outside of the bounds of a memory buffer, but the consequences will vary widely depending on the language, platform, and chip architecture.
Scope | Effect |
---|---|
Integrity | Technical Impact: Execute unauthorized code or commands; Memory corruption If the memory accessible by the attacker can be effectively controlled, it may be possible to execute arbitrary code, as with a standard buffer overflow. If the attacker can overwrite a pointer's worth of memory (usually 32 or 64 bits), he can redirect a function pointer to his own malicious code. Even when the attacker can only modify a single byte arbitrary code execution can be possible. Sometimes this is because the same problem can be exploited repeatedly to the same effect. Other times it is because the attacker can overwrite security-critical application-specific data -- such as a flag indicating whether the user is an administrator. |
Availability | Out of bounds memory access will very likely result in the corruption of relevant memory, and perhaps instructions, possibly leading to a crash. Other attacks leading to lack of availability are possible, including putting the program into an infinite loop. |
Confidentiality | In the case of an out-of-bounds read, the attacker may have access to sensitive information. If the sensitive information contains system details, such as the current buffers position in memory, this knowledge can be used to craft further attacks, possibly with more severe consequences. |
Automated Static Analysis 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 reporting out-of-bounds memory operations. This can make it difficult for users to determine which warnings should be investigated first. For example, an analysis tool might report buffer overflows that originate from command line arguments in a program that is not expected to run with setuid or other special privileges. Effectiveness: High Detection techniques for buffer-related errors are more mature than for most other weakness types. |
Automated Dynamic Analysis 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, or generate incorrect results. |
Example 1
This example takes an IP address from a user, verifies that it is well formed and then looks up the hostname and copies it into a buffer.
This function allocates a buffer of 64 bytes to store the hostname, however there is no guarantee that the hostname will not be larger than 64 bytes. If an attacker specifies an address which resolves to a very large hostname, then we may overwrite sensitive data or even relinquish control flow to the attacker.
Note that this example also contains an unchecked return value (CWE-252) that can lead to a NULL pointer dereference (CWE-476).
Example 2
This example applies an encoding procedure to an input string and stores it into a buffer.
The programmer attempts to encode the ampersand character in the user-controlled string, however the length of the string is validated before the encoding procedure is applied. Furthermore, the programmer assumes encoding expansion will only expand a given character by a factor of 4, while the encoding of the ampersand expands by 5. As a result, when the encoding procedure expands the string it is possible to overflow the destination buffer if the attacker provides a string of many ampersands.
Example 3
The following example asks a user for an offset into an array to select an item.
The programmer allows the user to specify which element in the list to select, however an attacker can provide an out-of-bounds offset, resulting in a buffer over-read (CWE-126).
Reference | Description |
---|---|
CVE-2009-2550 | Classic stack-based buffer overflow in media player using a long entry in a playlist |
CVE-2009-2403 | Heap-based buffer overflow in media player using a long entry in a playlist |
CVE-2009-0689 | large precision value in a format string triggers overflow |
CVE-2009-0690 | negative offset value leads to out-of-bounds read |
CVE-2009-1532 | malformed inputs cause accesses of uninitialized or previously-deleted objects, leading to memory corruption |
CVE-2009-1528 | chain: lack of synchronization leads to memory corruption |
CVE-2009-0558 | attacker-controlled array index leads to code execution |
CVE-2009-0269 | chain: -1 value from a function call was intended to indicate an error, but is used as an array index instead. |
CVE-2009-0566 | chain: incorrect calculations lead to incorrect pointer dereference and memory corruption |
CVE-2009-1350 | product accepts crafted messages that lead to a dereference of an arbitrary pointer |
CVE-2009-0191 | chain: malformed input causes dereference of uninitialized memory |
CVE-2008-4113 | OS kernel trusts userland-supplied length value, allowing reading of sensitive information |
Phase: Requirements Strategy: Language Selection Use a language with features that can automatically mitigate or eliminate buffer overflows. For example, many languages that perform their own memory management, such as Java and Perl, are not subject to buffer overflows. Other languages, such as Ada and C#, typically provide overflow protection, but the protection can be disabled by the programmer. Be wary that a language's interface to native code may still be subject to overflows, even if the language itself is theoretically safe. |
Phase: Architecture and Design Strategy: Libraries or Frameworks Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid. Examples include the Safe C String Library (SafeStr) by Messier and Viega, and the Strsafe.h library from Microsoft. These libraries provide safer versions of overflow-prone string-handling functions. This is not a complete solution, since many buffer overflows are not related to strings. |
Phase: Build and Compilation Run or compile your software using features or extensions that automatically provide a protection mechanism that mitigates or eliminates buffer overflows. For example, certain compilers and extensions provide automatic buffer overflow detection mechanisms that are built into the compiled code. Examples include the Microsoft Visual Studio /GS flag, Fedora/Red Hat FORTIFY_SOURCE GCC flag, StackGuard, and ProPolice. This is not necessarily a complete solution, since these mechanisms can only detect certain types of overflows. In addition, a buffer overflow attack can still cause a denial of service, since the typical response is to exit the application. |
Phase: Implementation Programmers should adhere to the following rules when allocating and managing their applications memory:
|
Phase: Operation Use a feature like Address Space Layout Randomization (ASLR). This is not a complete solution. However, it forces the attacker to guess an unknown value that changes every program execution. |
Phase: Operation Use a CPU and operating system that offers Data Execution Protection (NX) or its equivalent. This is not a complete solution, since buffer overflows could be used to overwrite nearby variables to modify the software's state in dangerous ways. In addition, it cannot be used in cases in which self-modifying code is required. |
Phase: Implementation Replace unbounded copy functions with analogous functions that support length arguments, such as strcpy with strncpy. Create these if they are not available. Effectiveness: Moderate This approach is still susceptible to calculation errors, including issues such as off-by-one errors (CWE-193) and incorrectly calculating buffer lengths (CWE-131). |
Nature | Type | ID | Name | View(s) this relationship pertains to | Named Chain(s) this relationship pertains to |
---|---|---|---|---|---|
ChildOf | Weakness Class | 20 | Improper Input Validation | Development Concepts699 Seven Pernicious Kingdoms (primary)700 | |
ChildOf | Weakness Class | 118 | Improper Access of Indexable Resource ('Range Error') | Development Concepts (primary)699 Research Concepts (primary)1000 | |
ChildOf | Category | 633 | Weaknesses that Affect Memory | Resource-specific Weaknesses (primary)631 | |
ChildOf | Category | 726 | OWASP Top Ten 2004 Category A5 - Buffer Overflows | Weaknesses in OWASP Top Ten (2004) (primary)711 | |
ChildOf | Category | 740 | CERT C Secure Coding Section 06 - Arrays (ARR) | Weaknesses Addressed by the CERT C Secure Coding Standard (primary)734 | |
ChildOf | Category | 741 | CERT C Secure Coding Section 07 - Characters and Strings (STR) | Weaknesses Addressed by the CERT C Secure Coding Standard734 | |
ChildOf | Category | 742 | CERT C Secure Coding Section 08 - Memory Management (MEM) | Weaknesses Addressed by the CERT C Secure Coding Standard734 | |
ChildOf | Category | 743 | CERT C Secure Coding Section 09 - Input Output (FIO) | Weaknesses Addressed by the CERT C Secure Coding Standard734 | |
ChildOf | Category | 744 | CERT C Secure Coding Section 10 - Environment (ENV) | Weaknesses Addressed by the CERT C Secure Coding Standard734 | |
ChildOf | Category | 752 | 2009 Top 25 - Risky Resource Management | Weaknesses in the 2009 CWE/SANS Top 25 Most Dangerous Programming Errors (primary)750 | |
ParentOf | Weakness Base | 120 | Buffer Copy without Checking Size of Input ('Classic Buffer Overflow') | Development Concepts (primary)699 Research Concepts (primary)1000 | |
ParentOf | Weakness Base | 123 | Write-what-where Condition | Development Concepts (primary)699 Research Concepts (primary)1000 | |
ParentOf | Weakness Base | 125 | Out-of-bounds Read | Development Concepts (primary)699 Research Concepts (primary)1000 | |
ParentOf | Weakness Base | 130 | Improper Handling of Length Parameter Inconsistency | Development Concepts (primary)699 | |
ParentOf | Weakness Base | 466 | Return of Pointer Value Outside of Expected Range | Research Concepts (primary)1000 | |
ParentOf | Weakness Base | 786 | Access of Memory Location Before Start of Buffer | Development Concepts (primary)699 Research Concepts (primary)1000 | |
ParentOf | Weakness Base | 787 | Out-of-bounds Write | Development Concepts (primary)699 Research Concepts (primary)1000 | |
ParentOf | Weakness Base | 788 | Access of Memory Location After End of Buffer | Development Concepts (primary)699 Research Concepts (primary)1000 | |
ParentOf | Weakness Base | 805 | Buffer Access with Incorrect Length Value | Development Concepts (primary)699 Research Concepts (primary)1000 | |
MemberOf | View | 635 | Weaknesses Used by NVD | Weaknesses Used by NVD (primary)635 | |
CanFollow | Weakness Base | 128 | Wrap-around Error | Research Concepts1000 | |
CanFollow | Weakness Base | 129 | Improper Validation of Array Index | Research Concepts1000 | |
CanFollow | Weakness Base | 131 | Incorrect Calculation of Buffer Size | Development Concepts699 Research Concepts1000 | |
CanFollow | Weakness Base | 190 | Integer Overflow or Wraparound | Research Concepts1000 | Integer Overflow to Buffer Overflow680 |
CanFollow | Weakness Base | 193 | Off-by-one Error | Research Concepts1000 | |
CanFollow | Weakness Variant | 195 | Signed to Unsigned Conversion Error | Research Concepts1000 |
Mapped Taxonomy Name | Node ID | Fit | Mapped Node Name |
---|---|---|---|
OWASP Top Ten 2004 | A5 | Exact | Buffer Overflows |
CERT C Secure Coding | ARR00-C | Understand how arrays work | |
CERT C Secure Coding | ARR33-C | Guarantee that copies are made into storage of sufficient size | |
CERT C Secure Coding | ARR34-C | Ensure that array types in expressions are compatible | |
CERT C Secure Coding | ARR35-C | Do not allow loops to iterate beyond the end of an array | |
CERT C Secure Coding | ENV01-C | Do not make assumptions about the size of an environment variable | |
CERT C Secure Coding | FIO37-C | Do not assume character data has been read | |
CERT C Secure Coding | MEM09-C | Do not assume memory allocation routines initialize memory | |
CERT C Secure Coding | STR31-C | Guarantee that storage for strings has sufficient space for character data and the null terminator | |
CERT C Secure Coding | STR32-C | Null-terminate byte strings as required | |
CERT C Secure Coding | STR33-C | Size wide character strings correctly | |
WASC | 7 | Buffer Overflow |
CAPEC-ID | Attack Pattern Name | (CAPEC Version: 1.4) |
---|---|---|
8 | Buffer Overflow in an API Call | |
9 | Buffer Overflow in Local Command-Line Utilities | |
10 | Buffer Overflow via Environment Variables | |
14 | Client-side Injection-induced Buffer Overflow | |
24 | Filter Failure through Buffer Overflow | |
42 | MIME Conversion | |
44 | Overflow Binary Resource File | |
45 | Buffer Overflow via Symbolic Links | |
100 | Overflow Buffers | |
46 | Overflow Variables and Tags | |
47 | Buffer Overflow via Parameter Expansion |
[REF-11] M. Howard and D. LeBlanc. "Writing Secure Code". Chapter 5, "Public Enemy #1: The Buffer Overrun" Page 127; Chapter 14, "Prevent I18N Buffer Overruns" Page 441. 2nd Edition. Microsoft. 2002. |
Microsoft. "Using the Strsafe.h Functions". <http://msdn.microsoft.com/en-us/library/ms647466.aspx>. |
Matt Messier and John Viega. "Safe C String Library v1.0.3". <http://www.zork.org/safestr/>. |
Michael Howard. "Address Space Layout Randomization in Windows Vista". <http://blogs.msdn.com/michael_howard/archive/2006/05/26/address-space-layout-randomization-in-windows-vista.aspx>. |
Arjan van de Ven. "Limiting buffer overflows with ExecShield". <http://www.redhat.com/magazine/009jul05/features/execshield/>. |
"PaX". <http://en.wikipedia.org/wiki/PaX>. |
Submissions | ||||
---|---|---|---|---|
Submission Date | Submitter | Organization | Source | |
PLOVER | Externally Mined | |||
Modifications | ||||
Modification Date | Modifier | Organization | Source | |
2008-07-01 | Eric Dalci | Cigital | External | |
updated Time of Introduction | ||||
2008-08-15 | Veracode | External | ||
Suggested OWASP Top Ten 2004 mapping | ||||
2008-09-08 | CWE Content Team | MITRE | Internal | |
updated Description, Relationships, Taxonomy Mappings | ||||
2008-10-14 | CWE Content Team | MITRE | Internal | |
updated Relationships | ||||
2008-11-24 | CWE Content Team | MITRE | Internal | |
updated Relationships, Taxonomy Mappings | ||||
2009-01-12 | CWE Content Team | MITRE | Internal | |
updated Applicable Platforms, Common Consequences, Demonstrative Examples, Likelihood of Exploit, Name, Potential Mitigations, References, Relationships | ||||
2009-03-10 | CWE Content Team | MITRE | Internal | |
updated Potential Mitigations | ||||
2009-05-27 | CWE Content Team | MITRE | Internal | |
updated Demonstrative Examples | ||||
2009-07-27 | CWE Content Team | MITRE | Internal | |
updated Observed Examples | ||||
2009-10-29 | CWE Content Team | MITRE | Internal | |
updated Applicable Platforms, Common Consequences, Demonstrative Examples, Description, Relationships, Time of Introduction | ||||
2009-12-28 | CWE Content Team | MITRE | Internal | |
updated Common Consequences, Demonstrative Examples, Detection Factors, Observed Examples | ||||
Previous Entry Names | ||||
Change Date | Previous Entry Name | |||
2008-04-11 | Buffer Errors | |||
2009-01-12 | Failure to Constrain Operations within the Bounds of an Allocated Memory Buffer | |||