Skip to main content
Be Bitwise

Exploit Reliability

Memory Safety & Exploitation Primitives · Security Fundamentals track

6 min read

In this module

From Crash to Reliable Exploit

Finding a memory corruption bug and proving it crashes the program is the easy part. Turning that crash into a reliable exploit — one that works consistently across different runs, system configurations, and memory layouts — is where the real engineering begins. This lesson covers the techniques that harden fragile proof-of-concept crashes into deterministic exploitation.

Heap Feng Shui

The heap is deterministic — governed by the allocator's algorithms — but its state at the moment of exploitation depends on every allocation and free that preceded it, making it effectively unpredictable without preparation. Heap feng shui (also called heap grooming) is the discipline of manoeuvring the heap into a known state before pulling the trigger.

The basic strategy is:

  1. Drain fragmented free memory by making many allocations of the target size
  2. Create a predictable pattern of allocated and freed chunks
  3. Free a specific chunk to create a "hole" where the vulnerable object will land
  4. Trigger the vulnerability so the corrupt allocation lands in the prepared hole
  5. Allocate attacker-controlled data adjacent to or overlapping the corrupted region
c
// Grooming: fill a size class with predictable allocations
for (int i = 0; i < 1000; i++) {
chunks[i] = malloc(TARGET_SIZE);
memcpy(chunks[i], controlled_data, TARGET_SIZE);
}
// Create holes at predictable intervals
for (int i = 0; i < 1000; i += 2) {
free(chunks[i]);
}
// Trigger vulnerable allocation - lands in one of the holes
trigger_vulnerability();

In browser exploitation, heap feng shui is performed through JavaScript by creating and releasing DOM objects, typed arrays, or strings of specific sizes. The attacker uses the target application's own allocation patterns against it.

Allocator-Specific Techniques

Heap feng shui techniques are allocator-specific. ptmalloc2 (glibc) uses per-thread arenas and bins sorted by size. jemalloc (Firefox, FreeBSD) uses size-class-based slabs. The Windows NT heap uses a low-fragmentation heap (LFH) with randomized bucket allocation. Each allocator requires different grooming strategies, and switching allocators can break existing exploits.

Dealing with ASLR Entropy

ASLR randomizes the base addresses of the stack, heap, libraries, and executable. On x86-64 Linux with default settings, the mmap base (and so shared libraries) and a PIE executable each get 28 bits of page-granular randomness, the stack gets 22 bits of page-granular randomness plus some sub-page jitter, and the brk heap start moves within a 32 MB range, about 13 bits of pages. Exact figures depend on the kernel and its vm.mmap_rnd_bits setting. Brute-forcing 28 bits requires hundreds of millions of attempts - impractical for most targets.

Strategies for dealing with ASLR:

Information leaks are the preferred approach. A single leaked pointer from any loaded module lets you calculate the base address of that module. From there, you can find gadgets (for ROP) and data structures. Most modern exploits include a dedicated info leak stage.

Partial overwrites exploit the fact that ASLR randomizes page-aligned base addresses, but the lower 12 bits of any address within a page are always the same. Overwriting only the lowest byte of a pointer stays inside those 12 fixed bits, so it needs no guessing at all, but it can only move the pointer within a 256-byte window. A two-byte overwrite reaches further and sets 16 bits, of which the top 4 are randomised: that's 16 possibilities, brute-forceable in 16 attempts on average or fewer.

Relative addressing avoids the need to know absolute addresses. If the attacker can corrupt a pointer to point to a known offset from its current value (rather than an absolute address), ASLR does not matter because the relative distance between two objects within the same allocation is constant.

Race Window Exploitation

Race condition vulnerabilities have a fundamental reliability problem: the attacker must win a race between two concurrent operations. If the window between the check and the use (in a TOCTOU bug) is microseconds, the attacker's competing operation must execute within that window.

Techniques for widening the race window:

  • Scheduling tricks: Pin the victim and the attacker to the same CPU, or lower the victim's priority, so the scheduler is likely to preempt it inside the window. (Sending SIGSTOP needs permission to signal the victim and can't be timed to land between two instructions, so it rarely helps in practice.)
  • Resource exhaustion: Starve the victim of CPU time by running CPU-intensive tasks on all other cores
  • Filesystem tricks: For filesystem race conditions, use FUSE (Filesystem in Userspace) to introduce arbitrary delays when the victim process reads a file
  • userfaultfd: On Linux, register a userfaultfd handler on a page the victim process will access. The kernel suspends the victim's thread when it faults on that page, giving the attacker unlimited time to modify the checked data
text
// Using userfaultfd to pause a kernel operation mid-execution:
// 1. Map a page with userfaultfd monitoring
// 2. Pass a pointer to this page to a syscall
// 3. When the kernel accesses the page, it blocks on userfaultfd
// 4. Attacker modifies the data being checked
// 5. Release the userfaultfd to let the kernel continue
// 6. The kernel now uses the modified data

Deterministic Exploitation Strategies

Beyond heap grooming and ASLR bypass, several general techniques improve exploit reliability:

Multiple attempts with forking servers: Servers that fork child processes for each connection (like Apache prefork or old-style sshd) give the attacker unlimited attempts. Each child has the same ASLR layout as the parent (because fork preserves the address space). A stack canary brute-force against a forking server only requires 256 attempts per byte.

Spraying: Instead of placing one payload at one address, spray thousands of copies across a large memory region. NOP sleds (in classic shellcode injection) and heap sprays (in modern browser exploits) both use this principle. If the target address has 16 bits of uncertainty, a spray that covers 64 KB of that space converts a 1-in-65536 chance into a near certainty.

Environment normalisation: Before triggering the vulnerability, perform operations that put the process in a known state. Close file descriptors, trigger garbage collection, drain thread pools, or exhaust caches. The more deterministic the environment, the more reliable the exploit.

Reliability is Not Optional

In vulnerability research, a crash is a bug report. A reliable exploit is a proof that the bug is exploitable. Bug bounty programs, CVE severity scores, and vendor response times all depend on demonstrated exploitability. An unreliable PoC that works 1% of the time might be dismissed as a low-severity DoS, while a reliable exploit for the same bug would be treated as critical.

The Defender's Perspective

Every reliability technique points at a defensive gap. Heap randomisation breaks feng shui. Re-randomising ASLR on each fork (as some hardened kernels do) kills forking-server brute-force. Restricting userfaultfd to privileged processes closes the race-window technique. Rate-limiting connections makes brute-force impractical. The defender who understands reliability engineering can distinguish mitigations that genuinely raise the bar from ones the attacker simply routes around.