Heap, Buffer and Stack Overflow Definitions

There are different types of Overflow and they come about in a variety of different ways. They are all similar to some extent however the cause of them varies and below I will explain simply the difference between them.

I will put a quick table up here at the top outlining the main differences, read further down for a more detailed description!

TypeLocationCausePotential Exploits
Stack OverflowStackToo much recursionOverwriting return address, causing code execution
Heap OverflowHeapWriting beyond allocated heap memoryCorrupting adjacent heap metadata, leading to arbitrary code execution
Buffer OverflowStack/HeapWriting beyond buffer sizeModifying adjacent memory, potential remote code execution

Firstly Stack Overflow (No not the website)

What it is: Happens when a program writes more data onto the stack than it can hold, causing a stack memory corruption. A stack is just a space of memory.

Cause: Usually due to excessive recursion, deeply nested function calls, or writing beyond allocated space in local variables, I will show an example further down on what this could look like in C.

Effect: Can lead to crashes, unexpected behaviour, or even code execution if exploited.

void recursiveFunction() { 

recursiveFunction(); // Infinite recursion causes stack overflow 

}

As you can see this function will call itself, which will call itself, which will call.. you get the idea. This will eventually exhausting the memory of an application usually causing a crash.

This one is simple and is different from the next two.

https://en.wikipedia.org/wiki/Stack_overflow

Secondly, Buffer Overflow

What it is: A broader term referring to writing more data into a fixed-size memory buffer than it can hold.

Cause: Can happen in both stack and heap memory if bounds are not properly checked. Say if you had a buffer of 10 letters and you tried to write 12 this would potentially cause that if unchecked.

Effect: Can cause unexpected program behaviour, crashes, or security exploits such as overwriting return addresses to execute malicious code.

Example:

A program asks a user for an input of 50 Characters

int main() {
    char userInput[50];
    printf("Enter some text: ");
    gets(userInput);  // Unsafe function (deprecated)
    vulnerableFunction(userInput);
    return 0;
}
void vulnerableFunction(char *userInput) {
    char buffer[8];  // Small fixed-size buffer
    strcpy(buffer, userInput);  // No bounds checking - Dangerous!
    printf("You entered: %s\n", buffer);
}

This then gets passed to a function that has a much smaller buffer, thus causing a buffer overflow.

https://en.wikipedia.org/wiki/Buffer_overflow

Finally, Heap Overflow.

What it is: Occurs when a program writes more data into a dynamically allocated heap memory than was allocated.

Cause: Writing beyond allocated memory boundaries (e.g., due to missing or incorrect bounds checking).

Effect: Can corrupt adjacent memory structures, leading to crashes, data corruption, or security vulnerabilities.

Heap overflow is similar to Buffer overflow in the example:

void heapOverflow(const char* input) {
    char* heapBuffer = (char*)malloc(8);  // Allocate 8 bytes
    strcpy(heapBuffer, input);  // No bounds check - heap overflow risk
    std::cout << "Heap data: " << heapBuffer << std::endl;
    free(heapBuffer);
}
int main() {
    char largeInput[50] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
    heapOverflow(largeInput);  // Overflows heap
    return 0;
}

https://en.wikipedia.org/wiki/Heap_overflow

As you can see I have allocated a heap of MEMORY as opposed to static buffer / local variable.

I am going to give an analogy of the difference of the two:

Imagine you’re in a library with two types of desks:

  1. Personal Desk (Stack) – A small, fixed-size desk assigned to you temporarily.
  2. Shared Study Table (Heap) – A large, shared table where anyone can request space.

Buffer Overflow (Stack Overflow) – Overfilling Your Small Desk

You have a small desk reserved just for you (like the stack).

You bring too many books, but your desk has limited space.

Books spill over onto someone else’s desk, messing up their notes (corrupting adjacent memory).

The librarian (computer) gets confused and kicks you out (crash or exploit).

Here is an example in the context of the analogy

char desk[8];  
strcpy(desk, "TooManyBooks!");  // Exceeds the 8-character limit

Heap Overflow – Taking More Space on the Shared Table

You request a spot on the shared study table (heap allocation).

The librarian gives you a small area (e.g., 8 inches).Instead of following the limit, you spread out over your neighbour’s space (overflowing into adjacent heap memory).

This disrupts their work, possibly altering important information (metadata corruption or exploit).

char *sharedtable = (char*)malloc(8);  
strcpy(sharedtable, "TooManyBooks!");  // Overflows the allocated heap memory

Hopefully this has helped clear up the differences between the different types of overflow, and also the issues that these ccan cause.

Click below for more explanations and other interesting blog posts:

Blogs

Leave a Reply

Your email address will not be published. Required fields are marked *