Control Flow Integrity
Binary Hardening & Compiler Defenses · Security Fundamentals track
6 min read
In this module
Controlling the Flow
Control flow integrity (CFI) is a security property that pins a program's execution to its legitimate control flow graph: the paths the programmer actually intended. The moment an attacker corrupts a function pointer or return address and sends execution somewhere off the graph, they've violated CFI. Enforcement mechanisms exist to spot the violation and shut it down.
The Problem: Indirect Branches
Direct branches (like call printf or jmp .loop_start) have their targets baked into the instruction itself. Memory corruption can't touch them because the target is part of the code, sitting in read-only memory.
Indirect branches are where the trouble lives. Instructions like call rax, jmp [rbx+0x18], and ret pull their target address from a register or memory. Corrupt the memory or register holding the target and you decide where the CPU jumps next. ROP chains lean on ret. Vtable hijacking goes after indirect calls through object vtables. Function pointer overwrites abuse call rax and friends.
CFI splits into two flavours depending on which indirect branches it protects:
- Forward-edge CFI: Protects indirect calls and jumps (the "forward" direction of control flow)
- Backward-edge CFI: Protects return addresses (the "backward" direction - returning to the caller)
Forward-Edge CFI
Forward-edge CFI narrows the set of valid targets for indirect calls and jumps. Without CFI, call rax can land anywhere. With CFI, call rax only lands on legitimate function entry points whose type signature matches what the call site expects.
Vtable checking is the canonical use case. In C++, virtual method calls load the target address from a vtable hung off the object. If an attacker swaps the vtable pointer for one pointing at memory they control, the next virtual call hands them execution on a plate. CFI checks that the vtable pointer actually references a legitimate vtable for the expected class hierarchy.
// Without CFI: attacker replaces vtable pointer// call [rax] jumps to any address the attacker controls
// With CFI: runtime check before the call// The compiler inserts code like:// if (vtable_ptr not in valid_vtable_set) abort();// call [vtable_ptr + offset]Clang CFI does fine-grained forward-edge CFI using type metadata. At every indirect call site, the compiler bolts in a check that the target function's type matches the expected function pointer type. The implementation comes down to a fast bitset check against metadata tables generated at link time.
Intel CET Indirect Branch Tracking (IBT) does coarse-grained forward-edge CFI in hardware. Every valid indirect branch target must start with the ENDBR64 (or ENDBR32) instruction. Land an indirect branch anywhere else and the CPU faults. It's coarse because any function carrying ENDBR64 qualifies as a valid target — there's no type signature check.
CFI's effectiveness lives or dies by granularity. Coarse-grained CFI (Intel IBT) only checks that the target is "some valid function". An attacker can still redirect to any function with a matching entry-point marker, which leaves a lot of room. Fine-grained CFI (Clang CFI) checks that the target matches the expected type signature, slashing the set of valid targets and making exploitation considerably harder.
Here's what that means for one corrupted virtual call. The program expects shape->area(), a Shape method, and the attacker has swapped the object's vtable pointer so the call lands on one of three targets. Follow each target to see which policy lets it through.
The first row is the residue fine-grained CFI can't remove: any method the type system says is legal at that call site stays reachable, which is exactly the room COOP-style attacks work in.
Backward-Edge CFI
Backward-edge CFI protects return addresses from getting corrupted. The two mechanisms are shadow stacks (covered in Stack Protection Mechanisms) and ARM Pointer Authentication (PAC), which Pointer Authentication and Memory Tagging covers in depth.
Shadow stacks keep a hardware-protected copy of every return address. On function return, the CPU compares the main stack's return address against the shadow copy. Any mismatch and you fault.
ARM PAC takes a cryptographic angle. Before the return address goes on the stack, it's signed with a secret hardware key. Before it's used, the signature gets verified. An attacker who corrupts the return address can't forge a matching signature because they don't have the key.
CFI in Practice
Modern software ships CFI more and more aggressively:
- Google Chrome uses Clang CFI across its C++ codebase, catching vtable hijacking
- Windows uses Control Flow Guard (CFG), validating indirect call targets against a bitmap of valid targets. Microsoft also built XFG (eXtended Flow Guard), which checks a hash of the function's type as well, but it hasn't been widely enabled
- Linux kernel protects its own indirect calls with Clang's kCFI and Intel IBT. Kernel code doesn't run on a shadow stack; the kernel supports CET shadow stacks for user-space programs (since 6.6)
- Android enables Clang CFI in many security-sensitive native components, such as the media and NFC stacks, and in the kernel on supported devices
Windows CFG keeps a bitmap with two bits for every 16 bytes of address space, which record whether that 16-byte slot holds a valid target at its aligned start or somewhere inside it (simplified here as one bit per target). Before an indirect call, the runtime tests the target's bit in the bitmap. The check is fast — one memory load, one bit test — but relatively coarse-grained, since any function's entry point can pass.
// Windows CFG pseudo-code for an indirect call:// 1. Load target address into register// 2. Check if target's bit is set in CFG bitmap// 3. If valid, proceed with the call// 4. If invalid, call the CFG failure handler (terminates the process)ARM Branch Target Identification (BTI)
ARM BTI is ARM's answer to Intel IBT. It marks valid indirect branch targets with a BTI instruction. Any indirect branch that misses a BTI triggers a fault. Like IBT, BTI is coarse-grained: it stops jumps into the middle of a function, but it doesn't restrict which functions can be reached.
BTI gets switched on per-page through a page table attribute, which lets it roll out gradually. A library compiled with BTI support has the BTI flag set on its code pages, and the CPU only enforces branch target checking on those pages.
The Ongoing Arms Race
CFI isn't a silver bullet. Researchers have demonstrated bypasses against every implementation that's shipped:
- Coarse-grained CFI falls to attacks that call legitimate functions with attacker-controlled arguments — a technique called COOP (counterfeit object-oriented programming)
- Fine-grained CFI bends if the attacker finds a valid function whose type signature happens to match and whose behaviour is useful
- Hardware CFI (CET, BTI) can fall through JIT code regions where enforcement isn't applied
Every bypass feeds back into stronger CFI implementations, and the long-term trend is squarely towards hardware-enforced, fine-grained CFI that makes exploitation progressively miserable.
CFI doesn't replace ASLR, stack canaries, or heap hardening. It piles on top. ASLR hides addresses. Stack canaries detect stack corruption. CFI stops corrupted control data from being used at all, even if the attacker knows the addresses. Together they force attackers to chain together increasingly elaborate bypasses just to get a foot in the door.