Skip to main content
Be Bitwise

CFI & Stack Canary Bypass

Exploit Mitigation Bypass Techniques · Exploit Development track

6 min read

In this module

Breaking the Guards

Stack canaries and Control Flow Integrity are designed to catch exploitation attempts even after memory corruption has already happened — the last barrier between a bug and code execution. Understanding their weaknesses matters both for building defences that actually hold and for threat modelling that isn't wishful thinking.

Canary Brute-Force in Forking Servers

Stack canaries are random values placed between the buffer and the return address. Overwriting the return address also overwrites the canary, which is detected before the function returns. The canary value is chosen at process startup and remains constant for the lifetime of the process.

In forking servers (Apache prefork, old-style sshd, some embedded HTTP servers), fork() creates child processes that inherit the parent's address space — including the canary. When a child crashes, the parent spawns a fresh one with the same canary. The attacker keeps trying.

The brute-force attack works byte by byte:

  1. Overflow the buffer up to the canary's first unknown byte
  2. Try all 256 possible values for that byte. The correct value does not crash the child (because the canary check passes for that byte, and the next bytes are unchanged)
  3. When the byte is found, extend the overflow by one byte and repeat for the next one
  4. Continue until every unknown byte of the 64-bit canary is known
  5. Maximum attempts: 256 per unknown byte. With glibc's null low byte (below), that's 256 * 7 = 1,792 in the worst case and about 128 * 7 = 896 on average
python
# Byte-by-byte canary brute force (conceptual)
canary = b"\x00" # glibc's low byte is always 0x00
for byte_pos in range(1, 8):
for guess in range(256):
payload = b"A" * buffer_size + canary + bytes([guess])
if send_and_check_alive(target, payload):
canary += bytes([guess])
break
# canary now contains the full 8-byte canary value
First Byte Shortcut

On Linux (glibc), the canary's least significant byte is always a null byte (0x00). This is intentional - it prevents string-based overflows from reading past the canary with functions like strcpy. But it also means the attacker only needs to brute-force 7 bytes, not 8, which is where the 1,792 worst case above comes from. Without the null byte it would be 256 * 8 = 2,048.

Information Leak to Reveal the Canary

Instead of brute-forcing, an info leak vulnerability can directly disclose the canary value. If the attacker can read stack memory (through a format string vulnerability, an out-of-bounds read, or an uninitialised memory disclosure), they can read the canary's value and include it correctly in their overflow payload.

c
// Format string leak targeting the canary
// If the canary is at the 11th stack position:
printf(user_input); // user_input = "%11$p"
// Output: 0x00abcdef12345600 (the canary value)

With the canary known, the attacker constructs a payload that overwrites the buffer, places the correct canary value at the expected offset, and then overwrites the return address:

text
[AAAA...AAA][correct canary][saved RBP][return address → attacker's target]

Overwriting Without Crossing the Canary

Not all stack overwrites are linear. Some techniques modify the return address without touching the canary:

Indexed writes: If the attacker controls an array index and a write value, they can write directly to the return address at buffer[controlled_offset] without sequentially overwriting the canary.

Exception handler overwrites: On 32-bit Windows, SEH records live on the stack, often above the canary, so an overflow could overwrite a handler and trigger an exception before the /GS check ever ran. SafeSEH (a linker table of valid handlers) and SEHOP (a runtime check that the chain is intact) largely closed this, and 64-bit Windows uses table-based exception handling with no handler pointers on the stack at all.

Non-stack overwrites: The attacker corrupts a function pointer on the heap or in a global variable instead of the return address on the stack.

Bypassing Forward-Edge CFI

Forward-edge CFI restricts indirect call targets to valid function entry points with compatible type signatures. Bypasses exploit the gap between the CFI policy and the actual program behaviour:

Coarse-grained CFI bypass: If CFI only checks that the target is "some valid function" (like Intel CET IBT), the attacker can redirect to any function with an ENDBR64 marker. This includes many functions with useful side effects, like system(), mprotect(), or gadget functions.

Type-compatible targets: Fine-grained CFI checks type signatures, but there may be multiple functions with the same signature. An attacker who corrupts a vtable pointer can redirect calls to any function whose signature matches the expected virtual method.

COOP (Counterfeit Object-Oriented Programming): In C++ programs with many virtual methods, the attacker chains calls to legitimate virtual methods on carefully crafted fake objects. Each call performs a small useful operation (like loading a value from a controlled field), and the chain of virtual calls achieves arbitrary computation without violating CFI.

The engine of a COOP attack is a main-loop gadget: a real virtual method that walks a list of objects and makes a virtual call on each. The attacker points that list at fake objects in memory they control. Each fake object's vptr points at a genuine vtable, so every call goes through a CFI check and passes:

Every edge in that graph is one a CFI policy would allow for a virtual call of that type. What's counterfeit is the objects and their order, and CFI never looks at either.

Bypassing Backward-Edge CFI

Shadow stacks (Intel CET, ARM PAC) protect return addresses:

Intel CET bypass research: CET shadow stacks can potentially be bypassed through:

  • Finding code sequences that legitimately modify the shadow stack (using INCSSP to skip entries)
  • Exploiting the interaction between CET and signal handling
  • Using data-only attacks that do not modify return addresses at all

ARM PAC bypass: PAC signs pointers with a hardware key. Bypasses include:

  • Finding signing gadgets that sign attacker-controlled pointers
  • Exploiting PAC key reuse across security domains
  • Using PAC oracles (where the attacker can test whether a forged PAC is valid)
  • Exploiting speculative execution to bypass PAC checks

These are active research areas. Each bypass that surfaces leads to a hardened implementation — and often to another research paper.

Data-Only Attacks

The ultimate bypass for both CFI and canaries is to avoid modifying control flow entirely. Data-only attacks corrupt non-control data (like authentication flags, file paths, or security policy variables) to change the program's behaviour without hijacking execution. If the attacker can flip an is_admin flag from 0 to 1, they achieve privilege escalation without touching a single pointer. Data-only attacks are CFI-immune because they never violate the control flow graph.