Skip to main content
Be Bitwise

Memory Corruption Classes

Memory Safety & Exploitation Primitives · Security Fundamentals track

7 min read

In this module

A Taxonomy of Memory Corruption

Every exploit begins with a bug. Before you can understand exploitation, you need a systematic grip on the bug classes that make it possible. Memory corruption vulnerabilities share a common root: the program accesses memory in a way the developer didn't intend. The specific mechanism varies enormously, though, and each class has different implications for what an attacker can do with it.

Stack-Based Buffer Overflows

You studied stack buffer overflows in Stack-Based Buffer Overflows. The core idea is simple: a function allocates a fixed-size buffer on the stack, and a write operation exceeds that buffer's bounds. The excess data overwrites adjacent stack data - local variables, the saved frame pointer, and critically, the return address.

Stack overflows are the oldest and most well-understood corruption class — increasingly rare in modern software because compilers insert stack canaries (Exploit Mitigations) and developers have learned to avoid unbounded copies, but they still surface in legacy code, embedded systems, and hand-optimised assembly routines.

c
void vulnerable(char *input) {
char buffer[64];
strcpy(buffer, input); // No bounds check - classic stack overflow
}

Type into the simulator below to play the attacker's input. It models the same 64-byte buffer, grouped four bytes to a cell, with a canary, the saved rbp and the return address stacked above it. Watch what the 65th character touches, then keep going until the return address goes red.

Buffer Overflow Simulator

Input: 0 bytes | Buffer: 64 bytes | Overflow: 0 bytes

Stack (high addresses at top):

0x7fffffffe04000buffer[0..63]
0x7fffffffe04400
0x7fffffffe04800
0x7fffffffe04c00
0x7fffffffe05000
0x7fffffffe05400
0x7fffffffe05800
0x7fffffffe05c00
0x7fffffffe06000
0x7fffffffe06400
0x7fffffffe06800
0x7fffffffe06c00
0x7fffffffe07000
0x7fffffffe07400
0x7fffffffe07800
0x7fffffffe07c00
0x7fffffffe0800x9c3d5e1a7b2f4400Stack Canary
0x7fffffffe0880x00007fffffffe0d0Saved RBP
0x7fffffffe0900x00005555555551a9Return Address
Buffer
Canary
Saved RBP
Return Addr

The key characteristic of a stack overflow is spatial locality: the corrupted data is adjacent to the buffer on the stack, and the attacker's control over what gets overwritten depends on the stack frame layout.

Heap-Based Corruption

Heap corruption is more complex than stack corruption because the heap is a dynamic data structure managed by an allocator (like ptmalloc2 on Linux or the Windows NT heap). Heap overflows occur when a write exceeds the bounds of a heap-allocated buffer, corrupting adjacent heap chunks.

What makes heap corruption powerful - and difficult - is that the attacker must understand the allocator's internal metadata structures. In older allocators, overwriting the forward and backward pointers in a freed chunk's metadata could be turned into an arbitrary write through the unlink macro. Modern allocators have added integrity checks (safe unlinking), but heap corruption remains a viable attack vector through techniques like overlapping chunks and corrupting application-level data in adjacent allocations.

Heap Metadata vs Application Data

Heap overflows can corrupt two distinct things: the allocator's metadata (chunk headers, free list pointers) or application data in an adjacent allocation. Modern heap hardening focuses on protecting metadata, but corrupting application data (like a function pointer stored in the next object) requires no metadata manipulation at all.

Use-After-Free

A use-after-free (UAF) occurs when a program frees memory but continues to use a pointer to that memory (a dangling pointer). The freed memory may be reallocated for a different purpose. When the program later reads or writes through the dangling pointer, it accesses data that now belongs to a different object entirely.

UAFs are particularly treacherous in C++ programs with tangled object lifetimes. Consider a browser engine where a DOM node is freed but a JavaScript callback still holds a reference. If the freed memory is reallocated for a different object, the callback operates on the wrong type of data — type confusion achieved through time rather than casting.

c
struct Object *obj = malloc(sizeof(struct Object));
setup(obj);
free(obj);
// ... later, memory is reallocated ...
struct Attacker *evil = malloc(sizeof(struct Attacker));
evil->function_ptr = malicious_code;
// obj still points to the same address, now containing evil's data
obj->callback(); // Calls attacker's function pointer

Step through the same sequence below. The interesting moment is the fourth step: nothing about obj changes, yet the memory it points at now belongs to someone else.

Allocate
Use Pointer
Free
Reallocate
Dangling Use
Type Confusion

Hover over each step to see details

UAFs have become the dominant vulnerability class in complex software like browsers and operating system kernels because they arise from the inherent difficulty of managing object lifetimes in large codebases.

Type Confusion

Type confusion occurs when a program treats a memory region as one type when it actually contains data of a different type. This can happen through incorrect C++ casts (especially downcasts in class hierarchies), mishandled variant types, or JIT compiler bugs that produce incorrect type assumptions.

In a type confusion, the program's view of the memory layout is wrong. A field that the program believes is a length might actually be a pointer, or vice versa. An attacker who can trigger a type confusion and control the contents of the misinterpreted object can construct fake fields that give them powerful exploitation primitives.

Type confusions are especially prevalent in JavaScript engines, where the JIT compiler speculates about types and sometimes gets it wrong — particularly when the attacker feeds inputs that deliberately violate those assumptions.

Uninitialised Memory

An uninitialised memory vulnerability occurs when a program reads from a memory location before writing to it. The memory may contain leftover data from a previous allocation - and that data might include sensitive information like pointers, keys, or heap metadata.

On the stack, uninitialised variables often contain data from previous function calls. On the heap, freshly allocated memory may contain data from previously freed objects. In the kernel, failing to clear memory before copying it to userspace is a common source of kernel information leaks.

c
void leak_info() {
char buffer[256];
// buffer is not initialized - contains stale stack data
send_to_network(buffer, 256); // Leaks whatever was on the stack
}
The Taxonomy Matters

Understanding these classes systematically is not academic - it determines your exploitation strategy. A stack overflow gives you control over the return address. A heap overflow lets you corrupt adjacent objects. A UAF gives you type confusion through temporal reuse. An info leak gives you addresses to defeat ASLR. Each class provides different primitives, and real exploits often chain multiple classes together.

Other Classes You'll Meet

A few more bug classes turn up often enough to name, and most of them feed into the ones above:

  • Double free: freeing the same chunk twice puts it on a free list twice, so two later allocations can hand out the same memory. It's a close cousin of UAF.
  • Off-by-one: a loop bound or <= that writes a single byte past the end. One byte sounds harmless, but on the stack it can clip a saved frame pointer, and on the glibc heap a single NUL byte into the next chunk's size field is a well-known route to overlapping chunks.
  • Integer overflow and truncation: a size calculation like count * sizeof(item) wraps or a 64-bit length is squeezed into 32 bits, the allocation comes out too small, and an ordinary-looking copy then overflows it.
  • Format string: user input passed as the format argument to printf-family functions lets %x/%p read the stack and %n write to memory. The next lesson uses it as a read and write primitive.

From Bug Class to Exploitation Strategy

Each corruption class maps to a set of exploitation primitives, covered in the next lesson. That systematic mapping — bug class → primitive → technique — is the bedrock of modern exploitation.

Understanding the taxonomy also shapes defence. Stack canaries guard return addresses; safe unlinking blocks heap metadata attacks; garbage collection and reference counting drain the pool of UAFs. Memory tagging (ARM MTE) can catch both spatial and temporal corruption at runtime, though with only 4-bit tags it's probabilistic: a stray access lands on a matching tag about one time in sixteen unless the allocator guarantees neighbouring allocations differ. Each defence plugs a specific gap in the chain, and if you understand the full chain, you can judge which plugs actually hold.