Skip to main content
Be Bitwise

Cache-Based Attacks

Side-Channel Attacks · Advanced Exploitation track

8 min read

In this module

The Speed Hierarchy Leaks Secrets

Modern CPUs lean on a deep memory hierarchy: L1 cache (fastest, smallest), L2 cache, L3 cache (shared across cores), and main memory (slowest). An L1 hit lands in roughly 1 nanosecond. A trip to main memory? Around 100 nanoseconds. That 100x gap is the entire side channel.

When a program touches a memory address, the CPU loads the surrounding cache line (typically 64 bytes) into the cache. Hit the same line again and the access is fast. If another program can work out which lines were recently loaded, it can infer what memory the first program accessed, and from that, what data it processed.

On any shared system — a cloud server, a multi-core desktop, two processes on the same laptop — cache-based attacks let one process spy on another just by reading the cache's state.

The Flush+Reload Attack

Flush+Reload is the most precise and well-studied cache-based attack. The catch: attacker and victim must share physical pages, which usually happens because they both map the same shared library (libc, libssl, that sort of thing), or because the OS or hypervisor merged identical pages behind their backs (page deduplication, such as Linux's KSM).

Three steps:

  1. Flush: The attacker evicts a specific cache line from every level using the clflush instruction, which x86 helpfully exposes to user-space.

  2. Wait: The victim runs. If it touches the flushed address, that line gets pulled back into the cache. If not, the line stays cold.

  3. Reload: The attacker reads the same address and times it. Fast access (cache hit)? The victim loaded it. Slow access (cache miss)? They didn't.

By aiming at cache lines that correspond to different code paths inside a crypto library, the attacker learns which path the victim took. Take RSA's square-and-multiply: the attacker can tell squarings apart from multiplications, and that distinction maps one-to-one onto the bits of the private key. Game over.

Flush+Reload

Flush+Reload is a cache side-channel attack where the attacker flushes a shared cache line, waits for the victim to execute, then reloads it. The access time reveals whether the victim touched that cache line. It requires shared memory between attacker and victim (e.g., shared libraries) and has cache-line (64-byte) spatial resolution.

The Prime+Probe Attack

Prime+Probe drops the shared-memory requirement, which makes it far more broadly useful. Instead of flushing the victim's lines, the attacker plants their own data in the cache and then watches to see what the victim kicks out.

  1. Prime: The attacker fills an entire cache set with their own data by walking through addresses that map to that set. (Caches are organised into sets, and each set holds a fixed number of lines.)

  2. Wait: The victim runs. Any victim access that maps to the same set will evict one of the attacker's lines to make room.

  3. Probe: The attacker re-reads their own data and times each access. A slow read (cache miss) means the victim evicted that line, which means they touched something mapping to that set.

Run it on a small cache: 16 lines of 16 bytes, 4-way set associative, so 4 sets. Addresses 0, 64, 128 and 192 all map to set 0, and the sequence below primes that set with them, then plays the victim touching address 256 (also set 0), then probes. Step through and note which of the attacker's lines comes back as a miss. The probe runs in reverse order on purpose. Probe in the original order and the first miss reloads its line, evicting the next one you're about to time, so every probe after it misses too.

Sequence [0/9]:
500ms
064128192256192128640
Hits: 0Misses: 0Rate: 0.0%

Cache Contents(4-Way Set Associative, 16 lines, 16B/line)

SetWay 0Way 1Way 2Way 3
0emptyemptyemptyempty
1emptyemptyemptyempty
2emptyemptyemptyempty
3emptyemptyemptyempty

Prime+Probe is noisier and resolves at the level of a cache set rather than a cache line, so the precision is coarser. The trade-off pays off: it works between any two processes sharing a cache level, including ones running in different VMs on the same physical host. Across cores it's easiest when the last-level cache is inclusive, so evicting a line from the LLC also kicks it out of the victim's private caches. On Intel's non-inclusive server parts (Skylake-SP onwards) that no longer holds, and researchers had to go after the coherence directory instead to get the same effect.

Attacking Cryptographic Implementations

Cache-based attacks bite hardest against crypto code, because so many implementations access different memory locations depending on secret data.

AES T-table implementations: A common AES implementation uses precomputed lookup tables, the so-called T-tables. During encryption, the algorithm indexes into these tables using values derived from the plaintext XORed with the secret key. Each lookup pulls in a specific cache line. Watch the cache, learn the indices, and from there you can back out key bytes.

This is why modern AES implementations rely on hardware AES instructions (AES-NI on x86) that do the whole computation inside the CPU core, with no data-dependent memory accesses to leak.

RSA square-and-multiply: RSA private key operations boil down to modular exponentiation, often implemented as a sequence of squarings followed by conditional multiplications. The conditional multiplication only fires when the corresponding key bit is 1. If squaring and multiplying live at different code locations, a Flush+Reload attack on those locations spells out the key, bit by bit.

Cache Attacks Across Virtual Machines

In cloud environments, VMs from different customers happily share the same physical CPU and the same caches. Cache-based side channels happily cross that VM boundary too. Prime+Probe needs nothing more than that. Flush+Reload needs shared pages, and between VMs the usual source was the hypervisor deduplicating identical pages (KSM on KVM, transparent page sharing on VMware), which is why VMware switched page sharing between VMs off by default and why cloud hosts generally avoid deduplicating across tenants.

Researchers have demonstrated:

  • Extracting RSA keys from a co-located VM on Amazon EC2
  • Detecting when a target VM is performing specific operations
  • Lifting AES keys from a co-located VM

Cloud providers have rolled out a handful of mitigations:

  • Cache partitioning: Intel's Cache Allocation Technology (CAT) carves up the last-level cache so different VMs get dedicated slices.
  • Core isolation: Dedicated physical cores per VM, so no L1 or L2 sharing.
  • Noise injection: Random cache activity layered on top to muddy real access patterns.
  • Hardware mitigations: Newer CPUs ship features that limit cache-based leakage at the silicon level.

The Cache Side Channel in Practice

Cache attacks live or die by timing precision. On x86, the rdtsc instruction hands you a timestamp counter that ticks at roughly the CPU's base clock. With fences around it the measurement overhead is a few tens of cycles, small next to the gap between a cache hit and a trip to DRAM (typically a couple of hundred cycles), so a single hit or miss stands out. The exact numbers vary a lot between CPUs, which is why attacks calibrate a threshold first.

A real attack tends to look like this:

  1. The attacker maps which cache lines correspond to security-sensitive code paths in the target.
  2. They land on the same physical machine as the victim — co-located VM, a different process, even just a different thread.
  3. They run the Flush+Reload or Prime+Probe cycle over and over, harvesting thousands of measurements.
  4. Statistical analysis on the pile of timings shows which code paths the victim took, and when.

From the network side, the attack is silent. No traffic, no log entries, nothing to alert on. The only thing it needs is shared hardware.

Defences

No single defence covers every cache attack. You stack them:

  • Constant-time code: Code and data access patterns must be independent of secret data. Every memory access looks the same, no matter what the secret is.
  • Hardware instructions: AES-NI, SHA extensions, and friends — crypto primitives that compute without data-dependent loads.
  • Cache partitioning: Hardware features that isolate cache usage between security domains.
  • Oblivious memory access: Techniques like ORAM (Oblivious RAM) that touch every memory location no matter which one you actually need. Painfully expensive at runtime, but it cuts the channel.
  • Disabling shared memory: If shared libraries aren't shared across security domains, Flush+Reload dies on the spot. Prime+Probe still works through shared caches, though.

The underlying tension is performance versus security. Caches exist because they make everything faster. Killing the cache side channel means either killing the cache (a non-starter for performance) or killing the leakage, which is hard without help from hardware.