Security

What is Heap-based Buffer Overflow (CWE-122)?

A heap overflow condition is a buffer overflow, where the buffer that can be overwritten is allocated in the heap portion of memory, generally meaning that...

SP
Shreya Pillai July 28, 2026 5 min read Security
AI-friendly summary

What it is: Heap-based Buffer Overflow (CWE-122) is a type of buffer overflow that occurs when the buffer that can be overwritten is allocated in the heap portion of memory.

Why it matters: This vulnerability can lead to crashes, resource consumption, and arbitrary code execution. It's essential to prevent and fix this issue promptly.

How to fix it: Use abstraction libraries, implement bounds checking on input, and use OS-level preventative functionality to prevent heap-based buffer overflows.

TL;DR: Heap-based Buffer Overflow (CWE-122) is a critical vulnerability that can lead to crashes, resource consumption, and arbitrary code execution. It’s essential to prevent and fix this issue promptly using secure coding practices.

At-a-Glance

Field Value
CWE ID CWE-122
OWASP Category Not directly mapped
CAPEC CAPEC-92
Typical Severity Critical
Affected Technologies C, C++, Java, Python, PHP, Web Development
Detection Difficulty Moderate
Last Updated 2026-07-28

What is Heap-based Buffer Overflow?

Heap-based Buffer Overflow (CWE-122) is a type of buffer overflow that occurs when the buffer that can be overwritten is allocated in the heap portion of memory. As defined by the MITRE Corporation under CWE-122, and classified by the OWASP Foundation as not directly mapped to any category, this vulnerability is a critical issue that requires prompt attention.

Quick Summary

Heap-based Buffer Overflow (CWE-122) is a critical vulnerability that can lead to crashes, resource consumption, and arbitrary code execution. It’s essential to prevent and fix this issue promptly using secure coding practices. The common consequences of this vulnerability include DoS: Crash, Exit, or Restart, DoS: Resource Consumption (CPU), and DoS: Resource Consumption (Memory).

Jump to: What is Heap-based Buffer Overflow? · Quick Summary · Heap-based Buffer Overflow Overview · How Heap-based Buffer Overflow Works · Business Impact of Heap-based Buffer Overflow · Heap-based Buffer Overflow Attack Scenario · How to Detect Heap-based Buffer Overflow · How to Fix Heap-based Buffer Overflow · Framework-Specific Fixes for Heap-based Buffer Overflow · How to Ask AI to Check Your Code for Heap-based Buffer Overflow · Heap-based Buffer Overflow Best Practices Checklist · Heap-based Buffer Overflow FAQ · Vulnerabilities Related to Heap-based Buffer Overflow · References · Scan Your Own Site

Heap-based Buffer Overflow Overview

What: A heap overflow condition is a buffer overflow, where the buffer that can be overwritten is allocated in the heap portion of memory.

Why it matters: This vulnerability can lead to crashes, resource consumption, and arbitrary code execution. It’s essential to prevent and fix this issue promptly.

Where it occurs: Heap-based Buffer Overflow (CWE-122) typically occurs when a program allocates memory on the heap using routines such as malloc() or calloc(), but does not properly check the bounds of the buffer before writing data to it.

Who is affected: Any program that uses dynamic memory allocation and does not properly check the bounds of the buffer can be vulnerable to this issue.

Who is NOT affected: Programs that use static memory allocation, do not allocate memory on the heap, or properly check the bounds of the buffer are not typically affected by this issue.

How Heap-based Buffer Overflow Works

Root Cause

Heap-based Buffer Overflow (CWE-122) occurs when a program allocates memory on the heap using routines such as malloc() or calloc(), but does not properly check the bounds of the buffer before writing data to it. This can lead to a buffer overflow, where more data is written to the buffer than it can hold.

Attack Flow

  1. The attacker sends a malicious input that exceeds the allocated buffer size.
  2. The program allocates memory on the heap using routines such as malloc() or calloc().
  3. The program writes the malicious input to the buffer without properly checking its bounds.
  4. The buffer overflows, leading to arbitrary code execution.

Prerequisites to Exploit

  • The attacker must send a malicious input that exceeds the allocated buffer size.
  • The program must allocate memory on the heap using routines such as malloc() or calloc().
  • The program must write the malicious input to the buffer without properly checking its bounds.

Vulnerable Code

#include <stdlib.h>
#include <string.h>

void vulnerable_function(char *input) {
    char *buffer = malloc(10);   // heap allocation
    strcpy(buffer, input);       // no bounds check
    free(buffer);
}

This is heap-based specifically because buffer is allocated with malloc(), not declared on the stack — overflowing it corrupts adjacent heap metadata or neighboring heap allocations rather than the stack/return address, which is what makes heap overflows behave differently from the classic stack case. strcpy() copies input with no length check, so any input longer than 9 bytes overflows the 10-byte heap chunk.

Secure Code

#include <stdlib.h>
#include <string.h>

void secure_function(char *input) {
    char *buffer = malloc(10);
    if (buffer == NULL) return;
    size_t len = strlen(input);
    if (len >= 10) len = 9;      // truncate to fit
    memcpy(buffer, input, len);
    buffer[len] = '\0';
    free(buffer);
}

This checks the source length against the actual heap allocation size before copying and always null-terminates within bounds, so input can never write past the 10 bytes that were allocated.

Business Impact of Heap-based Buffer Overflow

Heap-based Buffer Overflow (CWE-122) can lead to crashes, resource consumption, and arbitrary code execution. This can result in significant financial losses, compliance issues, and reputational damage for organizations that are affected by this vulnerability.

Confidentiality: The confidentiality impact of Heap-based Buffer Overflow (CWE-122) is not typically significant, as the vulnerability does not directly relate to data confidentiality.

Integrity: The integrity impact of Heap-based Buffer Overflow (CWE-122) can be significant, as the vulnerability can lead to arbitrary code execution and potentially modify sensitive data.

Availability: The availability impact of Heap-based Buffer Overflow (CWE-122) can also be significant, as the vulnerability can lead to crashes and resource consumption, making it difficult for organizations to maintain their services.

Heap-based Buffer Overflow Attack Scenario

  1. The attacker sends a malicious input that exceeds the allocated buffer size.
  2. The program allocates memory on the heap using routines such as malloc() or calloc().
  3. The program writes the malicious input to the buffer without properly checking its bounds.
  4. The buffer overflows, leading to arbitrary code execution.

How to Detect Heap-based Buffer Overflow

Manual Testing

  • Use a debugger to step through the code and identify potential buffer overflow issues.
  • Review the code for any suspicious memory allocation or deallocation operations.
  • Test the code with malicious input to see if it crashes or behaves unexpectedly.

Automated Scanners (SAST / DAST)

Heap-based Buffer Overflow (CWE-122) can be detected using automated scanners that analyze the code for potential buffer overflow issues. However, these scanners may not catch all instances of this vulnerability, especially those that involve complex memory allocation and deallocation operations.

PenScan Detection

PenScan’s scanner engines actively test for Heap-based Buffer Overflow (CWE-122) and other security vulnerabilities. These engines use a combination of static analysis and dynamic testing to identify potential issues in the code.

False Positive Guidance

A bounds check that already validates length before every heap write to a given buffer is not a finding, even if the buffer was allocated with malloc() — confirm the specific write path actually lacks a length check before treating a scanner hit as real.

How to Fix Heap-based Buffer Overflow

  • Use abstraction libraries to abstract away risky APIs.
  • Implement bounds checking on input to prevent buffer overflows.
  • Use OS-level preventative functionality to detect and prevent heap-based buffer overflows.

Framework-Specific Fixes for Heap-based Buffer Overflow

CWE-122 is a C/C++ memory-safety weakness tied directly to manual heap management (malloc/free) — managed-runtime languages don’t expose raw heap buffers to unchecked pointer writes, so this weakness doesn’t have a meaningful Java/Node.js/Python equivalent to show.

C/C++

Beyond manual length checks, compile and test with AddressSanitizer enabled (gcc -fsanitize=address -g program.c) — it instruments every heap allocation and immediately flags reads or writes past its bounds, catching this class of bug in testing well before it reaches a real attacker. Pair that with a bounds-checked copy in the code itself:

void secure_function(char *input) {
    char *buffer = malloc(10);
    if (buffer == NULL) return;
    size_t len = strlen(input) < 10 ? strlen(input) : 9;
    memcpy(buffer, input, len);
    buffer[len] = '\0';
    free(buffer);
}

How to Ask AI to Check Your Code for Heap-based Buffer Overflow

You can use a copy-paste prompt with an AI coding assistant to review the code block for potential vulnerabilities and rewrite it using secure coding practices.

Copy-paste prompt

Review the following C code block for potential CWE-122 Heap-based Buffer Overflow vulnerabilities and rewrite it using secure coding practices:

```c void vulnerable_function(char *input) { char buffer[10]; strcpy(buffer, input); } ```

Heap-based Buffer Overflow Best Practices Checklist

✅ Use abstraction libraries to abstract away risky APIs.

✅ Implement bounds checking on input to prevent buffer overflows.

✅ Use OS-level preventative functionality to detect and prevent heap-based buffer overflows.

Heap-based Buffer Overflow FAQ

How does a heap overflow occur?

A heap overflow occurs when the buffer that can be overwritten is allocated in the heap portion of memory, generally meaning that the buffer was allocated using a routine such as malloc().

What are the common consequences of a heap-based buffer overflow?

The common consequences include DoS: Crash, Exit, or Restart, DoS: Resource Consumption (CPU), and DoS: Resource Consumption (Memory).

How can I detect a heap-based buffer overflow in my code?

You can use manual testing, automated scanners (SAST / DAST), and PenScan detection to identify potential vulnerabilities.

What are the best practices for preventing heap-based buffer overflows?

Use abstraction libraries to abstract away risky APIs, implement bounds checking on input, and use OS-level preventative functionality.

How can I fix a heap-based buffer overflow in my code?

You can use automatic buffer overflow detection mechanisms, run or compile the software using features that randomly arrange the positions of executable and libraries in memory, and implement secure coding practices.

What are some common frameworks and platforms affected by heap-based buffer overflows?

C, C++, Java, Python, PHP, and Web Development are commonly affected.

How can I ask AI to check my code for potential CWE-122 Heap-based Buffer Overflow vulnerabilities?

You can use a copy-paste prompt with an AI coding assistant to review the code block for potential vulnerabilities and rewrite it using secure coding practices.

CWE ID Name Relationship
CWE-788 Access of Memory Location After End of Buffer (ChildOf)  
CWE-787 Out-of-bounds Write (ChildOf)  

References

Scan Your Own Site

Manual code review catches what you know to look for. An automated scan catches what you didn’t. Scan your own website using PenScan to find Heap-based Buffer Overflow and other risks before an attacker does.