---
title: "CWE-121: Stack-based Buffer Overflow"
url: https://radicalnotion.ai/cwe/cwe-121
type: cwe
description: "The product writes past the end of a buffer that was allocated on the stack."
updated: 2026-11-10
source: RadicalNotion.AI
license: CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
---

# CWE-121: Stack-based Buffer Overflow

- **Abstraction:** Variant
- **Structure:** Simple
- **Status:** Draft
- **Likelihood of exploit:** High
- **CVEs with this as primary weakness:** 4,180
- **Of those, in CISA KEV:** 20
- **New in 2026:** 1,530
- **CWE catalog version:** 4.21

## Description

The product writes past the end of a buffer that was allocated on the stack.

## Background

There are generally several security-critical data on an execution stack that can lead to arbitrary code execution. The most prominent is the stored return address, the memory address at which execution should continue once the current function is finished executing. The attacker can overwrite this value with some memory address to which the attacker also has write access, into which they place arbitrary code to be run with the full privileges of the vulnerable program. Alternately, the attacker can supply the address of an important call, for instance the POSIX system() call, leaving arguments to the call on the stack. This is often called a return into libc exploit, since the attacker generally forces the program to jump at return time into an interesting routine in the C standard library (libc). Other important data commonly on the stack include the stack pointer and frame pointer, two values that indicate offsets for computing memory addresses. Modifying those values can often be leveraged into a "write-what-where" condition.

## Alternate terms

- **Stack Overflow:** "Stack Overflow" is often used to mean the same thing as stack-based buffer overflow, however it is also used on occasion to mean stack exhaustion, usually a result from an excessively recursive function call. Due to the ambiguity of the term, use of stack overflow to describe either circumstance is discouraged.
- Stack Buffer Overflow

## Modes of introduction

| Phase | Note |
| --- | --- |
| Implementation | - |

## Applicable platforms

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

## Common consequences

| Scope | Impact | Note |
| --- | --- | --- |
| Availability | Modify Memory, DoS: Crash, Exit, or Restart, DoS: Resource Consumption (CPU), DoS: Resource Consumption (Memory) | Buffer overflows generally lead to crashes. Other attacks leading to lack of availability are possible, including putting the program into an infinite loop. |
| Integrity, Confidentiality, Availability, Access Control | Modify Memory, Execute Unauthorized Code or Commands, Bypass Protection Mechanism | Buffer overflows often can be used to execute arbitrary code, which is usually outside the scope of a program's implicit security policy. |
| Integrity, Confidentiality, Availability, Access Control, Other | Modify Memory, Execute Unauthorized Code or Commands, Bypass Protection Mechanism, Other | When the consequence is arbitrary code execution, this can often be used to subvert any other security service. |

## Potential mitigations

### Operation, Build and Compilation (Environment Hardening)

Use automatic buffer overflow detection mechanisms that are offered by certain compilers or compiler extensions. Examples include: the Microsoft Visual Studio /GS flag, Fedora/Red Hat FORTIFY\_SOURCE GCC flag, StackGuard, and ProPolice, which provide various mechanisms including canary-based detection and range/index checking.

D3-SFCV (Stack Frame Canary Validation) from D3FEND \[REF-1334\] discusses canary-based detection in detail.

Effectiveness: Defense in Depth. This is not necessarily a complete solution, since these mechanisms only detect certain types of overflows. In addition, the result is still a denial of service, since the typical response is to exit the application.

### Architecture and Design

Use an abstraction library to abstract away risky APIs. Not a complete solution.

### Implementation

Implement and perform bounds checking on input.

### Implementation

Do not use dangerous functions such as gets. Use safer, equivalent functions which check for boundary errors.

### Operation, Build and Compilation (Environment Hardening)

Run or compile the software using features or extensions that randomly arrange the positions of a program's executable and libraries in memory. Because this makes the addresses unpredictable, it can prevent an attacker from reliably jumping to exploitable code.

Examples include Address Space Layout Randomization (ASLR) \[REF-58\] \[REF-60\] and Position-Independent Executables (PIE) \[REF-64\]. Imported modules may be similarly realigned if their default memory addresses conflict with other modules, in a process known as "rebasing" (for Windows) and "prelinking" (for Linux) \[REF-1332\] using randomly generated addresses. ASLR for libraries cannot be used in conjunction with prelink since it would require relocating the libraries at run-time, defeating the whole purpose of prelinking.

For more information on these techniques see D3-SAOR (Segment Address Offset Randomization) from D3FEND \[REF-1335\].

Effectiveness: Defense in Depth. These techniques do not provide a complete solution. For instance, exploits frequently use a bug that discloses memory addresses in order to maximize reliability of code execution \[REF-1337\]. It has also been shown that a side-channel attack can bypass ASLR \[REF-1333\].

## 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 random inputs, it is often capable of generating unexpected results such as crashes, memory corruption, or resource consumption. Fuzzing effectively produces repeatable test cases that clearly indicate bugs, which helps developers to diagnose the issues.

Effectiveness: High

### 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

While buffer overflow examples can be rather complex, it is possible to have very simple, yet still exploitable, stack-based buffer overflows:

Bad code:

```c
#define BUFSIZE 256
int main(int argc, char **argv) {
    char buf[BUFSIZE];
    strcpy(buf, argv[1]);
}
```

The buffer size is fixed, but there is no guarantee the string in argv\[1\] will not exceed this size and cause an overflow.

### Example 2

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.

Bad code:

```c
void host_lookup(char *user_supplied_addr){
    struct hostent *hp;
    in_addr_t *addr;
    char hostname[64];
    in_addr_t inet_addr(const char *cp);

    /*routine that ensures user_supplied_addr is in the right format for conversion */

    validate_addr_form(user_supplied_addr);
    addr = inet_addr(user_supplied_addr);
    hp = gethostbyaddr( addr, sizeof(struct in_addr), AF_INET);
    strcpy(hostname, hp->h_name);
}
```

## Observed examples

| CVE | Description |
| --- | --- |
| CVE-2021-35395 | Stack-based buffer overflows in SFK for wifi chipset used for IoT/embedded devices, as exploited in the wild per CISA KEV. |

## Real CVEs with this weakness (sample of 12 of 4,180)

| CVE | Title | Severity | CVSS | EPSS | CISA KEV | Published |
| --- | --- | --- | --- | --- | --- | --- |
| CVE-2022-20699 | Cisco Small Business RV Series Routers Vulnerabilities | critical | 10.0 | - | Yes | 2022-02-10 |
| CVE-2022-20700 | Cisco Small Business RV Series Routers Vulnerabilities | critical | 10.0 | - | Yes | 2022-02-10 |
| CVE-2022-20701 | Cisco Small Business RV Series Routers Vulnerabilities | critical | 10.0 | - | Yes | 2022-02-10 |
| CVE-2022-20703 | Cisco Small Business RV Series Routers Vulnerabilities | critical | 10.0 | - | Yes | 2022-02-10 |
| CVE-2022-20708 | Cisco Small Business RV Series Routers Vulnerabilities | critical | 10.0 | - | Yes | 2022-02-10 |
| CVE-2025-22457 | Ivanti Connect Secure, Policy Secure, and ZTA Gateways Stack-Based Buffer Overflow Vulnerability | critical | 9.5 | - | Yes | 2025-04-03 |
| CVE-2025-0282 | Ivanti Connect Secure, Policy Secure, and ZTA Gateways Stack-Based Buffer Overflow Vulnerability | critical | 9.5 | - | Yes | 2025-01-08 |
| CVE-2025-53521 | BigIP APM Vulnerability | critical | 9.3 | - | Yes | 2025-10-15 |
| CVE-2025-32756 | Fortinet Multiple Products Stack-Based Buffer Overflow Vulnerability | critical | 9.3 | - | Yes | 2025-05-13 |
| CVE-2025-42599 | Qualitia Active! Mail Stack-Based Buffer Overflow Vulnerability | critical | 9.3 | - | Yes | 2025-04-18 |
| CVE-2021-20038 | SonicWall SMA 100 Appliances Stack-Based Buffer Overflow Vulnerability | critical | 9.3 | - | Yes | 2021-12-08 |
| CVE-2014-9163 | Adobe Flash Player Stack-Based Buffer Overflow Vulnerability | critical | 9.3 | - | Yes | 2014-12-10 |

## Related weaknesses

| Relationship | CWE | Name |
| --- | --- | --- |
| Parent | [CWE-787](https://radicalnotion.ai/cwe/cwe-787) | Out-of-bounds Write |
| Parent | [CWE-788](https://radicalnotion.ai/cwe/cwe-788) | Access of Memory Location After End of Buffer |

## Taxonomy mappings

| Taxonomy | Entry | Id | Fit |
| --- | --- | --- | --- |
| CLASP | Stack overflow | - | - |
| Software Fault Patterns | Faulty Buffer Access | SFP8 | - |
| CERT C Secure Coding | Guarantee that library functions do not form invalid pointers | ARR38-C | Imprecise |
| CERT C Secure Coding | Guarantee that storage for strings has sufficient space for character data and the null terminator | STR31-C | CWE More Specific |

## Frequently asked questions

### What is CWE-121?

The product writes past the end of a buffer that was allocated on the stack.

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

4,180 recorded CVEs are attributed to CWE-121, including CVE-2022-20699, CVE-2022-20700 and CVE-2022-20701. 20 are listed in CISA's Known Exploited Vulnerabilities (KEV) catalog.

### How do you prevent CWE-121?

Use automatic buffer overflow detection mechanisms that are offered by certain compilers or compiler extensions. Examples include: the Microsoft Visual Studio /GS flag, Fedora/Red Hat FORTIFY\_SOURCE GCC flag, StackGuard, and ProPolice, which provide various mechanisms including canary-based detection and range/index checking.

### How is CWE-121 detected?

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 random inputs, it is often capable of generating unexpected results such as crashes, memory corruption, or resource consumption. Fuzzing effectively produces repeatable test cases that clearly indicate bugs, which helps developers to diagnose the issues.

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

Exploiting CWE-121 can lead to: Modify Memory, DoS: Crash, Exit, or Restart, DoS: Resource Consumption (CPU), DoS: Resource Consumption (Memory), Execute Unauthorized Code or Commands, Bypass Protection Mechanism.

### Is CWE-121 actively exploited?

Yes. 20 CWE-121 vulnerabilities are in CISA's KEV catalog of actively exploited flaws, out of 4,180 recorded CVEs.

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

---

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