Side-Channel Mitigations in Hardware
Processor Security Features · CPU Architectures track
6 min read
In this module
Closing the Speculative Execution Gap
The Spectre and Meltdown disclosures of January 2018 dropped a bombshell: speculative execution — the performance optimisation that keeps modern CPUs fast — could be twisted to leak secrets. This lesson covers how hardware and microcode mitigations chase down these vulnerabilities, and the ongoing arm-wrestle between performance and security.
The Spectre Class of Attacks
Spectre exploits the branch predictor to coax the CPU into speculatively running attacker-chosen code paths. Speculative results are architecturally rolled back, sure — but they leave observable traces in microarchitectural state, mainly the cache. The attacker times cache hits to infer the speculatively accessed data.
Spectre v1 (Bounds Check Bypass): the attacker triggers a speculative out-of-bounds array access past a bounds check. The speculatively loaded value indexes into a probe array, dragging a specific cache line in. Speculation rolls back, but the cache line stays. The attacker times accesses to figure out which line was touched, recovering the out-of-bounds value.
Spectre v2 (Branch Target Injection): the attacker trains the indirect branch predictor to speculatively jump into a gadget that leaks data through the cache.
Spectre variants keep showing up — Spectre-RSB, Spectre-BHB, Spectre-BTI, Spectre-STL — each one prying open a different prediction mechanism.
Spectre v1 has no microcode fix. The usual defence lives in software: clamp the index with arithmetic so that even a mispredicted bounds check can only reach in-range memory. Linux does this with array_index_nospec(). Predict the mask for each test value below, then run it.
Both readers print the same values, because the difference only exists during speculation. In read_vulnerable, a mispredicted branch lets the CPU load array1[x] for any x. In read_masked, the load address depends on the mask, which is computed from x with ordinary arithmetic, so the CPU can't use the index until it knows the mask, and the mask is zero for anything out of range. The real attack also needs a cache timing probe, which depends on the exact CPU and its mitigations, so it isn't something this runner can show reliably.
Meltdown and Kernel Page-Table Isolation
Meltdown (Rogue Data Cache Load) is the cruder sibling. No branch training is needed: a user program simply reads a kernel address. The permission check does fire and the load faults, but on affected CPUs the data was forwarded to dependent instructions before the fault took effect. Those transient instructions use the byte as an index into a probe array, and the cache leaks it exactly as in Spectre v1. Most Intel cores of the time were affected, along with a few ARM and IBM designs; AMD's weren't.
It worked because every process had the whole kernel mapped into the top half of its address space, protected only by the supervisor bit. Kernel Page-Table Isolation (KPTI) removes the target: each process gets a second set of page tables for user mode that maps almost none of the kernel.
The price is a page-table switch on every kernel entry and exit, which is where the 5-30% syscall overhead in the list at the end of this lesson comes from. PCID tags soften it by letting TLB entries from both sets survive the switch. Intel fixed Meltdown in silicon from Whiskey Lake and Cascade Lake onwards. Those CPUs report the fix through a capability bit (RDCL_NO), and Linux skips KPTI when it sees it.
Microcode Patches
The first response to Spectre was microcode updates that bolt new CPU behaviours onto the OS-controllable surface:
IBRS (Indirect Branch Restricted Speculation): keeps branch predictions made at one privilege level out of execution at another. When the kernel sets IBRS, its indirect branch predictions can't be poisoned by user-space training.
STIBP (Single Thread Indirect Branch Predictors): walls off branch predictions between sibling hyperthreads on the same core. Without STIBP, a malicious thread could train the branch predictor used by a victim thread sharing the core.
IBPB (Indirect Branch Prediction Barrier): nukes branch predictor state. Fired at context switches so one process's branch predictions can't bleed into another's.
These features all cost performance because they restrict or flush prediction state that would otherwise be making code faster. Impact ranges from negligible to about 30%, with syscall-heavy workloads catching the worst of it.
Retpolines are a compiler trick that swaps out indirect branches (which feed the branch predictor) for a return-based construct the predictor can't exploit. The indirect branch becomes: push the target address onto the stack, then RET. The Return Stack Buffer mispredicts the return to a benign infinite loop; the actual return then goes to the intended target. Speculative execution spins the infinite loop and leaks nothing, while architectural execution lands on the right place. Retpolines block Spectre v2 for the replaced branches at the cost of extra instructions, on the CPUs they were designed for. Retbleed (2022) showed that some Intel and AMD CPUs fall back to the branch target buffer to predict returns, which reopens the hole there.
Silicon-Level Fixes
Newer CPU generations bake speculative-execution defences directly into silicon, doing the same job as microcode patches but with less overhead:
Enhanced IBRS (eIBRS): on Intel parts from Cascade Lake and Whiskey Lake onwards, IBRS is "always on" at minimal performance cost. The branch predictor is partitioned by privilege level by design, so there's no need to toggle IBRS on every kernel entry.
BHB clearing: newer ARM and Intel CPUs flush the Branch History Buffer on privilege transitions, killing cross-privilege history poisoning.
Speculative Store Bypass Disable (SSBD): stops the CPU from speculatively bypassing a store when a later load to the same address might end up reading stale data.
Silicon-level fixes hurt performance less because the work is in hardware rather than software. Each generation tends to ship better mitigations for the latest known vulnerabilities.
Cache Partitioning
Cache partitioning stops processes and VMs from interfering with each other's cache. Intel's Cache Allocation Technology (CAT) lets the OS hand specific cache ways (portions of the associativity) to specific processes or VMs. A process can only fill its assigned ways, so it can't evict another process's data.
That makes Prime+Probe across partitions much harder, since the attacker can no longer evict the victim's lines. It doesn't stop Flush+Reload, which works on memory both sides share, such as library code, and Intel doesn't present CAT as a security boundary. The cost is a smaller effective cache per partition, so it's a tradeoff rather than a free lunch.
The Performance-Security Tradeoff
Every speculative-execution mitigation costs you something. The ranges below are illustrative, a rough reading of benchmarks published at the time rather than measured figures you can rely on; the real cost swings widely with the CPU generation, the kernel version and the workload:
- IBRS/STIBP: 2-10% overhead on syscall-heavy workloads
- Retpolines: 5-15% on indirect-branch-heavy code
- Spectre v1 mitigations (bounds clipping): 1-5%
- Page Table Isolation (for Meltdown): 5-30% on syscall-heavy workloads
Here's which defence answers which attack:
| Attack | Main mitigation | Where it lives |
|---|---|---|
| Spectre v1 (bounds check bypass) | lfence or index masking at vulnerable sites | Software, case by case |
| Spectre v2 (branch target injection) | Retpolines, IBRS or eIBRS, IBPB on context switch | Compiler, microcode, silicon |
| Meltdown | KPTI, or a CPU that isn't affected | OS, then silicon |
| MDS (microarchitectural data sampling) | VERW to clear CPU buffers on kernel exit; sometimes disabling SMT | Microcode plus OS |
| Spectre v4 (speculative store bypass) | SSBD, usually turned on per process rather than globally | Microcode plus OS |
| Retbleed | IBRS on affected Intel parts, return thunks on AMD | Microcode plus compiler |
Stack them and the bill adds up. On older Intel parts running syscall-heavy workloads, turning the whole set on could cost double-digit percentages, which is what's been driving the push for hardware improvements that deliver the same guarantees with less drag.
New speculative-execution vulnerabilities keep landing — Spectre variants, MDS, TAA, LVI, Retbleed, Downfall, Inception. Each one demands analysis and potentially fresh mitigations. The fundamental tension between speculative execution (needed for performance) and information isolation (needed for security) remains an active area of CPU architecture research.