Explore our new Cybersecurity Terms Glossary, explained by our experts.

View the Glossary View the Glossary
PRIVIA

Cybersecurity Terms Glossary

What Is the Difference Between Stack and Heap Overflow?

Both corrupt memory beyond a buffer, but a stack overflow overwrites return addresses and saved registers in a predictable frame, while a heap overflow corrupts allocator metadata or neighbouring objects.

Illustration for the cybersecurity term Stack and Heap Overflow
Home / Blog / Cybersecurity Terms / What Is the Difference Between Stack and Heap Overflow?
17 November 2026

What Is the Difference Between Stack and Heap Overflow?

What Is the Difference Between Stack and Heap Overflow?

Both are buffer overflows: a program writes past the end of a buffer into memory it did not intend to touch. What differs is where the buffer lives, what sits next to it, and therefore what an attacker can do with the corruption. The distinction shapes both exploitation technique and the defences that work.

The Stack Case

The stack holds function-local data in frames that are created and destroyed as calls nest. A frame typically contains local variables, saved registers, and the return address that tells the processor where to resume when the function finishes. Local buffers sit beneath that return address, so writing past the end of one walks straight towards it.

The classic exploit overwrites the saved return address with an address of the attacker’s choosing. It is reliable because the layout is deterministic: the same binary produces the same frame structure every time, and the distance from the buffer to the return address is a constant the attacker measures once. This was the dominant exploitation technique of the 1990s, used by the Morris worm in 1988 and by Code Red against IIS in 2001.

Defences are correspondingly mature. Stack canaries, introduced by StackGuard in 1998 and available as -fstack-protector-strong or Microsoft’s /GS, place a random value between the locals and the return address and check it before returning, so a linear overwrite is detected. Shadow stacks from Intel CET keep a protected copy of return addresses. Variable reordering puts arrays above scalars so a buffer overrun cannot corrupt a pointer in the same frame. Together these have made the textbook stack smash rare in well-built modern software.

The Heap Case

The heap holds dynamically allocated memory whose lifetime is controlled by the program. There is no return address next to a heap buffer, so the attacker targets something else: the allocator’s own bookkeeping, or the next object along.

Historically the prize was allocator metadata. Chunk headers in glibc’s malloc carry size and linkage fields, and corrupting them so that a subsequent free performed an unlink operation produced an arbitrary write. Modern allocators have hardened heavily: safe unlinking checks, pointer encoding, size sanity checks, and the tcache protections added in glibc 2.29 closed most of those routes, and the Low Fragmentation Heap on Windows added comparable validation.

Attention therefore moved to adjacent object corruption. Heap grooming is the craft of persuading the allocator to place a victim object, ideally one containing a function pointer, a vtable pointer, or a length field, immediately after a buffer the attacker can overflow. The overflow then rewrites that pointer. This is how most modern browser and kernel heap exploits work, and it explains why exploitation of heap bugs requires far more knowledge of the target application than a stack overflow ever did.

Two Real Examples

CVE-2021-3156, nicknamed Baron Samedit, was a heap overflow in sudo disclosed in January 2021 and present for a decade; it gave any local user root on most Linux distributions. CVE-2019-18634, by contrast, was a stack overflow in the same program’s password feedback feature. Same codebase, same year, two different exploitation stories.

Comparing the Two

PropertyStack overflowHeap overflow
Primary targetSaved return address, saved registers, local pointersAllocator metadata, adjacent object pointers
Layout predictabilityDeterministic within a functionDepends on allocation history and grooming
Key mitigationStack canaries, shadow stacks, variable reorderingAllocator hardening, guard pages, partitioned heaps
Exploit difficultyLower, where canaries can be leaked or bypassedHigher, needs allocator and application knowledge
Typical outcomeDirect control flow hijackArbitrary write leading to control flow hijack

What Removes Both

The shared mitigations matter more than the differences. ASLR and DEP deny the attacker known addresses and executable data pages, so both classes now require an information leak and a code reuse chain. AddressSanitizer during development catches overruns at the moment they happen, and fuzzing finds them cheaply in parsers and protocol code. Guard pages and partitioned allocators, such as the isolation Chrome applies to renderer heaps, reduce the chance that a useful object sits adjacent to a vulnerable buffer.

The permanent fix is the one CISA and the NCSC have both pressed for: write new components in memory-safe languages, and limit C and C++ to the places where it is genuinely required. Our Penetration Test Services include memory safety review of native components, which in most estates means the agents, parsers, and device integrations nobody has rebuilt in a decade.

FAQ – Frequently Asked Questions

Which Is More Dangerous, a Stack or Heap Overflow?

Both can lead to full code execution, so severity depends on reachability rather than location. Stack overflows are usually easier to exploit because the layout is deterministic and the return address is immediately adjacent. Heap overflows are harder to weaponise but are far more common in modern software, since most data handled by long-running applications lives in dynamically allocated memory.

Do Stack Canaries Stop All Stack Overflows?

No. They detect linear overwrites that cross the canary before returning, which covers the classic case. They do not help when an attacker writes at a controlled offset and skips the canary, when the overflow corrupts a pointer in the same frame before the check occurs, or when the canary value can be leaked through a separate information disclosure and written back correctly.

Is a Stack Overflow the Same as Stack Exhaustion?

No, and the shared name causes confusion. A stack buffer overflow writes past the end of a local buffer and corrupts the frame. Stack exhaustion is running out of stack space, usually through unbounded recursion, and typically causes a crash rather than memory corruption. The second is a denial of service problem; the first is a code execution problem.

How Do I Find These Bugs Before Attackers Do?

Build with AddressSanitizer and run the test suite and a fuzzing campaign against every parser and protocol handler, which finds overruns at the point of occurrence rather than at the eventual crash. Add static analysis for the obvious unsafe functions, enable stack protector and fortify flags in release builds, and treat any native code that processes untrusted input as a review priority.

Related Terms

You May Be Interested In These

What Is Oblivious DNS over HTTPS?

Cybersecurity Terms

17 November 2026

What Is Oblivious DNS over HTTPS?

Read More
What Is Microsoft 365 Security Configuration?

Cybersecurity Terms

17 November 2026

What Is Microsoft 365 Security Configuration?

Read More
What Is Frida?

Cybersecurity Terms

17 November 2026

What Is Frida?

Read More