CWE Reference
CWE-787: Out-of-bounds Write
Official CWE-787 CWE context with Glexia analysis, remediation guidance, related CVEs, and ATT&CK context.
Release 4.20weaknessDraft
Glexia's Take
CWE-787: Memory Corruption
Out-of-bounds Write represents a recurring weakness pattern that can create exploitable paths when design, validation, or implementation controls are missing.
Executive Impact
- Integrity: Modify Memory,Execute Unauthorized Code or Commands: Write operations could cause memory corruption. In some cases, an adversary can modify control data such as return addresses in order to execute unexpected code.
- Availability: DoS: Crash, Exit, or Restart: Attempting to access out-of-range, invalid, or unauthorized memory could cause the product to crash.
- Other: Unexpected State: Subsequent write operations can produce undefined or unexpected results.
Developer Pattern
CWE-787 is the kind of defect developers can usually prevent with explicit validation, safer framework defaults, and tests that exercise hostile input or unsafe state transitions.
Confidence
high confidence from CWE-787, 4.20.
Official CWE Definition
CWE-787: Out-of-bounds Write
The product writes data past the end, or before the beginning, of the intended buffer.
Developer And Remediation Guidance
How teams prevent and detect this weakness
Causes
- The following code attempts to save four different identification numbers into an array. Since the array is only allocated to hold three elements, the valid indices are 0 to 2; so, the assignment to id_sequence[3] is out of bounds.
- In the following code, it is possible to request that memcpy move a much larger segment of memory than assumed: If returnChunkSize() happens to encounter an error it will return -1. Notice that the return value is not checked before the memcpy operation (CWE-252), so -1 can be passed as the size argument to memcpy() (CWE-805). Because memcpy() assumes that the value is unsigned, it will be interpreted as MAXINT-1 (CWE-195), and therefore will copy far more memory than is likely available to the destination buffer (CWE-787, CWE-788).
- This code takes an IP address from the user and verifies that it is well formed. It 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 the function 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).
- This code 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.
- In the following C/C++ code, a utility function is used to trim trailing whitespace from a character string. The function copies the input string to a local character string and uses a while statement to remove the trailing whitespace by moving backward through the string and overwriting whitespace with a NUL character. However, this function can cause a buffer underwrite if the input character string contains all whitespace. On some systems the while statement will move backwards past the beginning of a character string and will call the isspace() function on an address outside of the bounds of the local buffer.
- The following code allocates memory for a maximum number of widgets. It then gets a user-specified number of widgets, making sure that the user does not request too many. It then initializes the elements of the array using InitializeWidget(). Because the number of widgets can vary for each request, the code inserts a NULL pointer to signify the location of the last widget. However, this code contains an off-by-one calculation error (CWE-193). It allocates exactly enough space to contain the specified number of widgets, but it does not include the space for the NULL pointer. As a result, the allocated buffer is smaller than it is supposed to be (CWE-131). So if the user ever requests MAX_NUM_WIDGETS, there is an out-of-bounds write (CWE-787) when the NULL is assigned. Depending on the environment and compilation settings, this could cause memory corruption.
Remediation
- Requirements: [object Object]
- Architecture and Design: [object Object]
- Operation,Build and Compilation: [object Object]
- Implementation: [object Object]
- Operation: [object Object]
- Implementation: Replace unbounded copy functions with analogous functions that support length arguments, such as strcpy with strncpy. Create these if they are not available.
Detection
- Automated Static Analysis: [object Object]
- 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.
- 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].
Mappings
Related CVEs, CWEs, and ATT&CK context
Related CWEs
- CWE-120: Buffer Copy without Checking Size of Input ('Classic Buffer Overflow')
- CWE-120: Buffer Copy without Checking Size of Input ('Classic Buffer Overflow')
- CWE-120: Buffer Copy without Checking Size of Input ('Classic Buffer Overflow')
- CWE-121: Stack-based Buffer Overflow
- CWE-122: Heap-based Buffer Overflow
- CWE-123: Write-what-where Condition
- CWE-123: Write-what-where Condition
- CWE-124: Buffer Underwrite ('Buffer Underflow')
- CWE-119: Improper Restriction of Operations within the Bounds of a Memory Buffer
- CWE-119: Improper Restriction of Operations within the Bounds of a Memory Buffer
- CWE-119: Improper Restriction of Operations within the Bounds of a Memory Buffer
- CWE-119: Improper Restriction of Operations within the Bounds of a Memory Buffer
ATT&CK Relevance
ATT&CK relevance is shown only when reviewed or responsibly inferred.