Exploitation Primitives
Memory Safety & Exploitation Primitives · Security Fundamentals track
6 min read
In this module
The Building Blocks of Exploits
A memory corruption bug is the raw material from which an exploit is constructed. The gap between "this program crashes" and "this program executes attacker-controlled code" is bridged by exploitation primitives — the fundamental capabilities an attacker wrings out of a bug. Understanding them matters for both offensive research and defensive engineering.
Arbitrary Read
An arbitrary read primitive allows the attacker to read the contents of any memory address in the target process. This is typically the first primitive an attacker needs, because modern defences like ASLR randomize the layout of memory, and the attacker must discover where things are before they can corrupt them.
An arbitrary read can come from many bug classes. An out-of-bounds read on an array lets the attacker index beyond the buffer to read adjacent memory. A format string vulnerability with %s or %x can leak stack contents. A type confusion might let the attacker read a pointer field as if it were data, disclosing its value.
// Out-of-bounds read: index is attacker-controlledint read_value(int *array, int index) { return array[index]; // No bounds check - reads anywhere relative to array}The power of an arbitrary read depends entirely on what you hit. A stack address defeats stack ASLR. A code pointer leaks a library's base. Heap metadata exposes the allocator's internals. Each scrap of leaked information chips away at the entropy standing between the attacker and a working exploit.
Arbitrary Write
An arbitrary write (write-what-where) is the most powerful single primitive. It allows the attacker to write a chosen value to a chosen address. With one arbitrary write, an attacker can overwrite a function pointer, a GOT entry, a vtable pointer, or a security-critical flag.
Arbitrary writes arise from corrupted pointers: if the attacker controls a pointer that the program later writes through, they control both the destination (the pointer value) and often the data (the value being written). Heap unlink exploits in older allocators were a classic arbitrary write. Modern exploits often construct arbitrary writes from more constrained primitives through clever chaining.
Not all write primitives are equal. A constrained write might only let you write a specific value (like a null byte) or write to an address with restrictions (like alignment requirements). An unconstrained write lets you write any value to any address. The gap between constrained and unconstrained writes is often bridged by chaining multiple corruption steps.
Information Leak
An information leak (info leak) is any mechanism that discloses memory contents to the attacker. While related to arbitrary reads, info leaks are often more limited - they might only leak data from a specific region, or leak it through a side channel rather than a direct read.
Common info leak sources include:
- Uninitialised memory: Reading stack or heap memory that contains stale pointers from previous operations
- Over-read bugs: Reading past the end of a buffer (like the Heartbleed vulnerability in OpenSSL)
- Format string bugs: Using attacker-controlled format strings to dump stack contents
- Error messages: Verbose error messages that include pointer values or memory addresses
Info leaks are almost always the first stage of a modern exploit chain. Without an info leak, ASLR makes reliable exploitation of 64-bit targets impractical. With an info leak, the attacker can calculate the exact addresses of every gadget and data structure they need.
Control Flow Hijack
A control flow hijack redirects the program's execution to an attacker-chosen address. This is the culmination of most exploit chains - the moment the attacker's code (or carefully chosen existing code) begins executing.
Control flow hijacks target indirect control flow transfers - instructions where the destination is loaded from memory rather than hardcoded:
- Return addresses: Overwriting the saved return address on the stack so that
retjumps to the attacker's chosen address - Function pointers: Overwriting a function pointer in a struct, C++ vtable, or dispatch table
- GOT entries: On Linux, overwriting Global Offset Table entries to redirect library function calls
- Exception handlers: On Windows, corrupting SEH (Structured Exception Handler) chain entries
; Normal indirect call - destination loaded from memorymov rax, [rbx + 0x18] ; Load function pointer from objectcall rax ; Call through pointer - if attacker controls [rbx+0x18], they control executionCombining Primitives
Real exploits rarely rely on a single primitive. A typical modern exploit chain looks like this:
- Trigger the bug to get an initial constrained primitive (e.g., a limited out-of-bounds write)
- Upgrade to info leak by corrupting an object to leak pointer values
- Use the leak to calculate addresses, defeating ASLR
- Upgrade to arbitrary write by corrupting a pointer used in a write operation
- Achieve control flow hijack by overwriting a function pointer or return address with a known gadget address
Here's the same chain with the attacker's state after each step. The addresses are illustrative, but the arithmetic is the real thing: a leaked pointer minus its known offset inside the library gives the library's base.
Each step transforms a weaker primitive into a stronger one. The art of exploitation is finding these transformation chains in the specific constraints of the target program and its environment.
It's tempting to rank primitives from weakest to strongest, but the real relationships are conversions: which primitive can you turn into which? An info leak and an arbitrary read both reveal memory, a constrained write is often the seed for everything else, and control-flow hijack only becomes reliable once a leak has told you where to point it.
Browser and JavaScript-engine exploits use their own names for the same steps. A bug first gives a relative read/write (say, an array whose length field has been corrupted, so it reaches past its own storage). That becomes two helpers: addrof, which leaks the address of any object, and fakeobj, which makes the engine treat an address you chose as an object. Fake a typed array whose backing pointer you control and you have arbitrary read and write.
Why This Framework Matters
Thinking in primitives rather than specific vulnerability types gives you a portable framework for both sides of the problem. When you find a new bug: what primitive does it hand you? When you assess a defence: which primitive does it block, and is there another path to the same primitive? That's how professional vulnerability researchers actually think — and it's a more useful lens than cataloguing CVEs.