CFI, Shadow Stacks and Pointer Authentication
Canaries, NX, ASLR and RELRO all make the first steps of an attack harder. None of them says anything about where a program is allowed to go. Once an attacker controls a return address, a function pointer or a vtable pointer, the program will jump wherever it points, as long as the target is executable code. Control-flow integrity (CFI) closes that gap by checking, at every indirect transfer, that the target is one the program could legitimately reach. It comes in two halves, because there are two kinds of indirect transfer:
- Forward edges: indirect calls and jumps (
call *rax, a virtual call). Protected by compiler CFI (Clang CFI, Windows Control Flow Guard) and by hardware landing pads (Intel IBT, Arm BTI). - Backward edges: returns (
ret). Protected by shadow stacks (Intel CET, Arm GCS) and by pointer authentication of the return address (Arm PAC).
Forward edges: Clang CFI on a corrupted function pointer
A connection object holds a buffer and a handler function pointer, a common layout in C servers. A real overflow of buf would overwrite handler; the toy simulates that corruption with a cast, so that only the effect is shown, and the "wrong" function only prints a message. The same program is built without CFI, with Clang's -fsanitize=cfi-icall in diagnostic mode, and in trapping mode as it would ship. It runs in your sandbox:
cat > icall.c <<'SRC'
#include <stdio.h>
#include <string.h>
static int on_message(int code) { printf("message handler, code %d\n", code); return 0; }
static void wipe_logs(void) { printf("wipe_logs() runs: the wrong function\n"); }
struct conn { char buf[16]; int (*handler)(int); };
int main(int argc, char **argv) {
setvbuf(stdout, NULL, _IONBF, 0);
struct conn c = { "", on_message };
c.handler(1);
if (argc > 1) /* simulate a corrupted function pointer (a real bug would overwrite it) */
c.handler = (int (*)(int))(void *)wipe_logs;
c.handler(2);
return 0;
}
SRC
echo "== no CFI"
clang -O1 icall.c -o icall && ./icall corrupt; echo "exit code: $?"
echo "== Clang CFI, diagnostic mode"
clang -O1 -flto -fvisibility=hidden -fuse-ld=gold -fsanitize=cfi-icall -fno-sanitize-trap=cfi-icall icall.c -o icall-cfi
./icall-cfi
./icall-cfi corrupt 2>&1 | grep -E 'message|wipe|runtime error|note'
echo "exit code: ${PIPESTATUS[0]}"
echo "== Clang CFI, trap mode (production)"
clang -O1 -flto -fvisibility=hidden -fuse-ld=gold -fsanitize=cfi-icall icall.c -o icall-trap
./icall-trap corrupt 2>&1 | cat; echo "exit code: ${PIPESTATUS[0]}"== no CFI
message handler, code 1
wipe_logs() runs: the wrong function
exit code: 0
== Clang CFI, diagnostic mode
message handler, code 1
message handler, code 2
message handler, code 1
icall.c:15:5: runtime error: control flow integrity check for type 'int (int)' failed during indirect function call
ld-temp.o: note: wipe_logs defined here
exit code: 1
== Clang CFI, trap mode (production)
message handler, code 1
exit code: 132
Without CFI the corrupted pointer simply calls wipe_logs: any executable address is acceptable to the CPU. With cfi-icall, Clang groups functions by their type signature and, before every indirect call, checks that the target belongs to the group of the pointer's type, here int (int). wipe_logs has type void (void), so the call fails the check: control flow integrity check for type 'int (int)' failed during indirect function call. In trap mode there is no message, just an illegal-instruction trap (SIGILL, exit code 132 = 128 + 4) before the wrong function runs, at a cost of a few instructions per indirect call. Clang CFI needs link-time optimisation (-flto) because it must see every function of the program to build the groups, and -fvisibility=hidden so that the groups are closed. The C++ variants (cfi-vcall, cfi-derived-cast) protect virtual calls and casts; Type Confusion showed cfi-derived-cast catching a bad downcast.
Coarse-grained CFI is the cheaper cousin. Windows' Control Flow Guard (/guard:cf, since Windows 8.1 and Visual Studio 2015) checks that an indirect call targets the start of some valid function, without checking the type. Intel IBT (part of CET) and Arm BTI make the CPU fault when an indirect jump or call lands on an instruction that isn't a landing pad (endbr64 on x86-64, bti c on Arm). Coarse CFI stops jumps into the middle of functions, which is where many reusable instruction sequences are, but allows any function to be called through any pointer.
Backward edges: shadow stacks and return-address signing
Returns are protected differently, because every function has one and they must be cheap.
A shadow stack is a second stack, in memory that ordinary stores can't write (a special page type), that holds only return addresses. call pushes the return address on both stacks; ret pops both and compares, and a mismatch raises a control-protection fault. Intel CET shadow stacks are available since Tiger Lake (11th-gen Core) and AMD Zen 3 CPUs; Windows uses them as "Hardware-enforced Stack Protection", and Linux supports them for user space since kernel 6.6 with glibc 2.39. Arm's equivalent, the Guarded Control Stack (GCS), is an Armv9.4-A feature with Linux support since 6.13.
Pointer authentication (Arm PAC, Armv8.3-A, used on Apple's A12 and later and on recent Arm Linux and Android devices) signs a pointer with a secret key and a context: the unused top bits of the 64-bit pointer receive a cryptographic code (on Linux with a 48-bit address space, 55 − 48 = 7 bits for data pointers). Before ret, the CPU checks the signature; a return address overwritten in memory carries no valid code and faults when used.
The compiler emits all of these automatically. The session asks for the CET properties and instructions of an x86-64 object file (Ubuntu's GCC uses -fcf-protection=full by default), and for the AArch64 code of the same function with -mbranch-protection=pac-ret+bti (cross-compiled; only the assembly is produced):
cat > cf.c <<'SRC'
int helper(int x);
int (*hook)(int) = helper;
int helper(int x) { return x * 2; }
int run(int x) { return hook(x) + 1; } /* an indirect call, then a return */
SRC
gcc -O2 -c cf.c -o cf-default.o
gcc -O2 -fcf-protection=none -c cf.c -o cf-none.o
for o in cf-default.o cf-none.o; do printf "%-13s " $o; readelf -n $o | grep -o 'x86 feature: .*' || echo "(no CET property)"; done
echo "== x86-64 run(), default flags"
objdump -d --no-show-raw-insn -M intel cf-default.o | awk '/<run>:/,/^$/' | sed 's/^ *[0-9a-f]*:\t//'
echo "== AArch64 helper() and run() with -mbranch-protection=pac-ret+bti"
clang --target=aarch64-linux-gnu -march=armv8.5-a -O2 -mbranch-protection=pac-ret+bti -S -o - cf.c \
| awk '/^(helper|run):/,/\.Lfunc_end/' | grep -vE '^\s*(\.|//)|^\.L'cf-default.o x86 feature: IBT, SHSTK
cf-none.o (no CET property)
== x86-64 run(), default flags
0000000000000010 <run>:
endbr64
sub rsp,0x8
call QWORD PTR [rip+0x0] # 1e <run+0xe>
add rsp,0x8
add eax,0x1
ret
== AArch64 helper() and run() with -mbranch-protection=pac-ret+bti
helper: // @helper
bti c
lsl w0, w0, #1
ret
run: // @run
paciasp
stp x29, x30, [sp, #-16]! // 16-byte Folded Spill
mov x29, sp
adrp x8, hook
ldr x8, [x8, :lo12:hook]
blr x8
add w0, w0, #1
ldp x29, x30, [sp], #16 // 16-byte Folded Reload
retaa
- The default x86-64 object is marked
IBT, SHSTK: compatible with both CET features. The loader enables a feature for the process only if the CPU, the kernel and every loaded object support it, so a single old library without the mark turns it off. (Linux so far enforces only the shadow stack for user programs; user-space IBT isn't supported.) runstarts withendbr64, the IBT landing pad. (Functions only ever called directly can skip it.)- On AArch64,
helper, whose address is taken, starts withbti c(a valid target for indirect calls).runsigns its return address withpaciaspon entry, before saving it on the stack, andretaaauthenticates and returns in one instruction. A return address modified on the stack fails authentication.
What CFI doesn't stop
- Calls to the right type of the wrong function: fine-grained CFI still allows any function with a matching signature. Attacks that chain whole functions ("call-oriented" or "counterfeit object" techniques) live in that gap.
- Data-only attacks: changing
is_admin, a length, or a file name doesn't touch control flow at all. - Code that isn't built with it: a JIT, a third-party library, assembly code without landing pads.
- Signing gadgets and key leaks for PAC; the code is often only 7 to 16 bits wide (it depends on the address-space size and on pointer tagging), so brute force is possible where a process can be crashed repeatedly without being re-keyed.
How it is checked and deployed
readelf -n prog (IBT, SHSTK property), checksec (newer versions report CET), dumpbin /headers on Windows (Guard flags), and the compile flags: -fcf-protection=full (x86-64), -mbranch-protection=standard (AArch64), /guard:cf and /CETCOMPAT (MSVC), -fsanitize=cfi -flto (Clang). Browsers and operating systems use several at once: Chrome's official Linux x86-64 builds use Clang CFI (virtual calls since M54 in 2016, indirect calls since M68), Windows uses CFG and CET shadow stacks, and Apple's operating systems use PAC in the kernel and system code (arm64e).
Pitfalls
- Marking without enforcing. A binary built with
-fcf-protectionruns unprotected on CPUs or kernels without CET; check what is enforced (/proc/<pid>/statushasx86_Thread_featureslines on CET-enabled kernels). - Breaking CFI with casts: calling functions through pointers of the wrong type (common in old C callback code) makes
cfi-icallfire on legitimate code. Fix the types. - Mixed builds: one non-LTO object can leave holes in Clang CFI's checks.
Practice
1. Why did the CFI build accept the call with code 2 when run without the "corrupt" argument?
The pointer still pointed to on_message, whose type int (int) matches the pointer type. CFI checks the target against the type, and it was valid.
2. Which protects the return in run on AArch64, and how?
Pointer authentication: paciasp signs the return address in the link register (with the stack pointer as context) before it is saved, and retaa verifies the signature before returning. A forged or overwritten address fails verification.
3. Why does a shadow stack need memory that normal stores can't write?
If ordinary writes could modify it, the same overflow that corrupts the return address could corrupt its shadow copy. CET shadow-stack pages can only be changed by call, ret and a few special instructions.
Test yourself
What did cfi-icall compare before the indirect call?
- The target's address with a list of all addresses
- The target function's type signature with the pointer's type
- The canary
- The return address
Which mechanism protects returns (backward edges)?
- Control Flow Guard
- Shadow stacks (Intel CET, Arm GCS) and return-address signing (Arm PAC)
- IBT landing pads
- ASLR
What does the endbr64 instruction at the start of run() mark?
- The end of the function
- A valid target for indirect calls and jumps under Intel IBT
- A breakpoint
- A shadow-stack push
Which attack does CFI not stop?
- Jumping into the middle of a function
- Returning to an overwritten return address
- Changing a data value such as is_admin
- Calling a function of the wrong type