Explore our new Cybersecurity Terms Glossary, explained by our experts.
Cybersecurity Terms Glossary
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.

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 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 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.
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.
| Property | Stack overflow | Heap overflow |
|---|---|---|
| Primary target | Saved return address, saved registers, local pointers | Allocator metadata, adjacent object pointers |
| Layout predictability | Deterministic within a function | Depends on allocation history and grooming |
| Key mitigation | Stack canaries, shadow stacks, variable reordering | Allocator hardening, guard pages, partitioned heaps |
| Exploit difficulty | Lower, where canaries can be leaked or bypassed | Higher, needs allocator and application knowledge |
| Typical outcome | Direct control flow hijack | Arbitrary write leading to control flow hijack |
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.
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.
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.
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.
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.
You May Be Interested In These
We use a few cookies that are needed to make this website work, and they are always on. We would also like to set analytics cookies, to see which pages are useful; functional cookies, so that our live chat works; and advertising cookies, to measure our campaigns. We will not set any of those until you say yes, and you can change your mind at any time from the Cookie Settings link in the footer.
Choose the cookies you are happy for us to use. Strictly necessary cookies are always on; everything else is off until you turn it on.
These cookies keep the website working: they hold your security checks, your form submissions and the choice you make here. The site cannot run without them, so they are always on.
These let our live chat open and remember your conversation, and let the newsletter sign-up work. Switch them off and the chat window will not appear.
These tell us which pages are read and how people find us, so that we can improve the site. The reports are about groups of visitors, never about you personally.
These let us measure our advertising and show you our adverts on other websites, such as LinkedIn, Google and Meta. Switch them off and you will still see adverts, but they will be less relevant.