---
title: "CWE-124: Buffer Underwrite"
url: https://radicalnotion.ai/cwe/cwe-124
type: cwe
description: "The product writes to a buffer using an index or pointer that references a memory location prior to the beginning of the buffer."
updated: 2026-11-10
source: RadicalNotion.AI
license: CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
---

# CWE-124: Buffer Underwrite

- **Abstraction:** Base
- **Structure:** Simple
- **Status:** Incomplete
- **Likelihood of exploit:** Medium
- **CVEs with this as primary weakness:** 65
- **Of those, in CISA KEV:** 1
- **New in 2026:** 33
- **CWE catalog version:** 4.21

## Description

The product writes to a buffer using an index or pointer that references a memory location prior to the beginning of the buffer.

## Alternate terms

- **Buffer Underrun:** Some prominent vendors and researchers use the term "buffer underrun". "Buffer underflow" is more commonly used, although both terms are also sometimes used to describe a buffer under-read (CWE-127).
- Buffer Underflow

## Modes of introduction

| Phase | Note |
| --- | --- |
| Implementation | This typically occurs when a pointer or its index is decremented to a position before the buffer, when pointer arithmetic results in a position before the beginning of the valid memory location, or when a negative index is used. |

## Applicable platforms

**Languages:** Memory-Unsafe (Undetermined), C (Undetermined), C++ (Undetermined)

## Common consequences

| Scope | Impact | Note |
| --- | --- | --- |
| Integrity, Availability | Modify Memory, DoS: Crash, Exit, or Restart | Out of bounds memory access will very likely result in the corruption of relevant memory, and perhaps instructions, possibly leading to a crash. |
| Integrity, Confidentiality, Availability, Access Control, Other | Execute Unauthorized Code or Commands, Modify Memory, Bypass Protection Mechanism, Other | If the corrupted memory can be effectively controlled, it may be possible to execute arbitrary code. If the corrupted memory is data rather than instructions, the system will continue to function with improper changes, possibly in violation of an implicit or explicit policy. The consequences would only be limited by how the affected data is used, such as an adjacent memory location that is used to specify whether the user has special privileges. |
| Access Control, Other | Bypass Protection Mechanism, Other | When the consequence is arbitrary code execution, this can often be used to subvert any other security service. |

## Potential mitigations

### Requirements

Choose a language that is not susceptible to these issues.

### Implementation

All calculated values that are used as index or for pointer arithmetic should be validated to ensure that they are within an expected range.

## Detection methods

### 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 having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

Effectiveness: High

### 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\].

Effectiveness: Moderate. Crafted inputs are necessary to reach the code containing the error, such as generated by fuzzers. Also, these tools may reduce performance, and they only report the error condition - not the original mistake that led to the error.

## Demonstrative examples

### Example 1

In the following C/C++ example, 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.

Bad code:

```c
char* trimTrailingWhitespace(char *strMessage, int length) {
    char *retMessage;
    char *message = malloc(sizeof(char)*(length+1));

    // copy input string to a temporary string
    char message[length+1];
    int index;
    for (index = 0; index < length; index++) {
        message[index] = strMessage[index];
    }
    message[index] = '\0';

    // trim trailing whitespace
    int len = index-1;
    while (isspace(message[len])) {
        message[len] = '\0';
        len--;
    }

    // return string without trailing whitespace
    retMessage = message;
    return retMessage;
}
```

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.

### Example 2

The following is an example of code that may result in a buffer underwrite. This code is attempting to replace the substring "Replace Me" in destBuf with the string stored in srcBuf. It does so by using the function strstr(), which returns a pointer to the found substring in destBuf. Using pointer arithmetic, the starting index of the substring is found.

Bad code:

```c
int main() {
    ...
    char *result = strstr(destBuf, "Replace Me");
    int idx = result - destBuf;
    strcpy(&destBuf[idx], srcBuf);
    ...
}
```

In the case where the substring is not found in destBuf, strstr() will return NULL, causing the pointer arithmetic to be undefined, potentially setting the value of idx to a negative number. If idx is negative, this will result in a buffer underwrite of destBuf.

## Observed examples

| CVE | Description |
| --- | --- |
| CVE-2021-24018 | buffer underwrite in firmware verification routine allows code execution via a crafted firmware image |
| CVE-2002-2227 | Unchecked length of SSLv2 challenge value leads to buffer underflow. |
| CVE-2007-4580 | Buffer underflow from a small size value with a large buffer (length parameter inconsistency, CWE-130) |
| CVE-2007-1584 | Buffer underflow from an all-whitespace string, which causes a counter to be decremented before the buffer while looking for a non-whitespace character. |
| CVE-2007-0886 | Buffer underflow resultant from encoded data that triggers an integer overflow. |
| CVE-2006-6171 | Product sets an incorrect buffer size limit, leading to "off-by-two" buffer underflow. |
| CVE-2006-4024 | Negative value is used in a memcpy() operation, leading to buffer underflow. |
| CVE-2004-2620 | Buffer underflow due to mishandled special characters |

## Real CVEs with this weakness (sample of 12 of 65)

| CVE | Title | Severity | CVSS | EPSS | CISA KEV | Published |
| --- | --- | --- | --- | --- | --- | --- |
| CVE-2015-2426 | Microsoft Windows Adobe Type Manager Library Remote Code Execution Vulnerability | high | 8.6 | - | Yes | 2015-07-20 |
| CVE-2026-74744 | ipvlan: inherit needed\_headroom and needed\_tailroom from phy\_dev | critical | 9.8 | - | No | 2026-08-26 |
| CVE-2026-74743 | macvlan: inherit needed\_headroom and needed\_tailroom from lowerdev | critical | 9.8 | - | No | 2026-08-26 |
| CVE-2026-74269 | bnxt: fix head underflow on XDP head-grow | critical | 9.8 | - | No | 2026-08-15 |
| CVE-2026-44631 | Apache HTTP Server: Heap Underflow in \`ap\_regname\` via Signed Char Overflow | critical | 9.8 | - | No | 2026-06-08 |
| CVE-2025-68114 | Capstone doesn't check vsnprintf return in SStream\_concat, allows stack buffer underflow and overflow | critical | 9.8 | - | No | 2025-12-17 |
| CVE-2025-53101 | ImageMagick has Stack Buffer Overflow in image.c | critical | 9.8 | - | No | 2025-07-14 |
| CVE-2023-48230 | Cap'n Proto WebSocket message can cause crash | critical | 9.8 | - | No | 2023-11-21 |
| CVE-2023-32614 | Accusoft ImageGear create\_png\_object heap-based buffer overflow vulnerability | critical | 9.8 | - | No | 2023-09-25 |
| CVE-2021-38578 | edk2: integer underflow in SmmEntryPoint function leads to potential SMM privilege escalation | critical | 9.8 | - | No | 2022-03-03 |
| CVE-2018-15361 | Buffer Underwrite ('Buffer Underflow') in Siemens SINUMERIK | critical | 9.8 | - | No | 2019-03-05 |
| CVE-2023-25610 | Improper Input Validation in Siemens RUGGEDCOM APE1808 with Fortigate NGFW Devices | critical | 9.3 | - | No | 2025-03-24 |

## Related weaknesses

| Relationship | CWE | Name |
| --- | --- | --- |
| Parent | [CWE-786](https://radicalnotion.ai/cwe/cwe-786) | Access of Memory Location Before Start of Buffer |
| Parent | [CWE-787](https://radicalnotion.ai/cwe/cwe-787) | Out-of-bounds Write |
| Related | [CWE-196](https://radicalnotion.ai/cwe/cwe-196) | Unsigned to Signed Conversion Error |
| Related | [CWE-839](https://radicalnotion.ai/cwe/cwe-839) | Numeric Range Comparison Without Minimum Check |

## Taxonomy mappings

| Taxonomy | Entry | Id | Fit |
| --- | --- | --- | --- |
| PLOVER | UNDER - Boundary beginning violation ('buffer underflow'?) | - | - |
| CLASP | Buffer underwrite | - | - |
| Software Fault Patterns | Faulty Buffer Access | SFP8 | - |

## Frequently asked questions

### What is CWE-124?

The product writes to a buffer using an index or pointer that references a memory location prior to the beginning of the buffer.

### What CVEs are caused by CWE-124?

65 recorded CVEs are attributed to CWE-124, including CVE-2015-2426, CVE-2026-74744 and CVE-2026-74743. 1 is listed in CISA's Known Exploited Vulnerabilities (KEV) catalog.

### How do you prevent CWE-124?

Choose a language that is not susceptible to these issues.

### How is CWE-124 detected?

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 having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

### What are the consequences of CWE-124?

Exploiting CWE-124 can lead to: Modify Memory, DoS: Crash, Exit, or Restart, Execute Unauthorized Code or Commands, Bypass Protection Mechanism, Other.

### Is CWE-124 actively exploited?

Yes. 1 CWE-124 vulnerability is in CISA's KEV catalog of actively exploited flaws, out of 65 recorded CVEs.

Primary source: [MITRE CWE](https://cwe.mitre.org/data/definitions/124.html)

---

Source: [RadicalNotion.AI](https://radicalnotion.ai/cwe/cwe-124). Licensed CC BY-NC 4.0; attribute RadicalNotion.AI and link the source page when you reuse this data.
