---
title: Hardware security and side channels
heading: Cache timing channels, transient execution attacks, Rowhammer and the cost of mitigations
eyebrow: S4 · Lesson 37 · Hardware security and side channels
xp: 50
---

::: hook
The features that make processors fast also leak. A cache hit takes fewer cycles than a miss, so whether another program touched a line can be measured with a timer. Speculative execution runs instructions that are later squashed, yet the lines they loaded stay in the cache, so a secret read speculatively can be recovered by timing. DRAM rows hammered with activations can flip bits in their neighbors. These are architecture problems with architecture numbers: channel bandwidths, error rates, activation counts and the performance cost of each defense.
:::

::: play
- The curve is the capacity of a timing channel, in bits per measured symbol, against the probability of misreading a symbol: $1 - H(e)$ with the binary entropy $H(e) = -e\log_2e - (1 - e)\log_2(1 - e)$.
- A channel that's right 95% of the time still carries 71% of a bit per symbol; at 50% it carries nothing.
- Defenses that add noise must push the error rate very close to one half to close a channel: halving its capacity isn't enough.
:::

```sim plot
view: graph
x: e = 0.001..0.5
y: C = 0..1
samples: 500
plot: 1 + e*log(e)/log(2) + (1 - e)*log(1 - e)/log(2) | label: bits per symbol
```

::: predict
## A covert channel sends one bit by touching or not touching a cache line; each bit takes 2000 cycles at 3 GHz, and the receiver misreads 5% of them. About how fast can it send information?

- [ ] 1.5 Mb/s
- [x] About 1.07 Mb/s
- [ ] About 75 kb/s
- [ ] Nothing usable, at 5% errors

::: reveal
The raw rate is $3 \times 10^9/2000 = 1.5$ million symbols a second. With 5% errors each symbol carries $1 - H(0.05) = 0.71$ bits, so error-correcting codes can push about 1.07 Mb/s through: plenty to leak a key in milliseconds.
:::
:::

::: nameit
## Cache timing channels

- **Flush+Reload** (shared memory): the attacker flushes a line, waits, then times a reload; a fast reload means the victim touched it.
- **Prime+Probe** (no shared memory): the attacker fills a cache set, waits, then times its own lines; slow ones were evicted by the victim.
The receiver tells hits from misses with a threshold. With Gaussian timing noise σ around hit and miss times h and m, the error rate is about $Q\!\left(\frac{m - h}{2\sigma}\right)$, and the channel carries $1 - H(e)$ bits per symbol.

## Transient execution

A mispredicted branch or a faulting load lets later instructions run before being squashed (lesson 10). **Spectre** trains a branch predictor so the victim speculatively reads a secret out of bounds and uses it as an index into a probe array; the squashed load's cache line stays, and timing 256 probe lines reveals the byte. **Meltdown** read kernel memory speculatively before the permission check took effect.

## Rowhammer

Repeatedly activating a DRAM row can disturb charge in adjacent rows. Within a refresh window W, a bank can activate a row up to $W/t_{RC}$ times, over a million in 64 ms, while vulnerable cells may flip after tens of thousands. Defenses refresh victims early (target row refresh) or count activations.

## The cost of defenses

Fences after bounds checks stop speculation past them; kernel page-table isolation switches address spaces on every system call; partitioned or randomized caches block eviction channels. Each costs time in proportion to how often it triggers: events per second × cost per event.
:::

::: example
## A Spectre attack leaks one byte per round: setting up costs 10 000 cycles and probing the 256 lines of the probe array takes about 300 cycles each. How fast does it read secrets at 3 GHz?

::: solution
1. **Cycles per round.**
   $$\begin{aligned} N &= 10\,000 + 256 \times 300 \\ &= 86\,800\ \text{cycles} \end{aligned}$$
2. **Leak rate.**
   $$\begin{aligned} R &= 3 \times 10^9/86\,800 \\ &= 34\,600\ \text{bytes/s} \end{aligned}$$
   about 34 KB/s, before error correction.
3. **Why the probe lines are a page apart.** The spacing keeps the hardware prefetcher from pulling in neighbors and blurring the result.

**Answer:** the attack reads secrets at about 34 KB/s (34,600 bytes a second) before error correction.
:::
:::

::: example
## DRAM rows take 45 ns to cycle, and the refresh window is 64 ms. How many activations can an attacker aim at one row between refreshes?

::: solution
1. **Activations per window.**
   $$64\ \text{ms}/45\ \text{ns} = 1.4\ \text{million}$$
2. **What an attack needs.** If neighboring cells flip after 20 000 activations, an attacker needs well under 2% of the window.
3. **The defense.** That's why DRAM and memory controllers now track activation counts and refresh victim rows early.

**Answer:** about 1.4 million activations fit between refreshes.
:::
:::

::: example
## Kernel page-table isolation adds about 200 ns to each system call. What does it cost a server making 500 000 system calls a second?

::: solution
1. **Time per second.**
   $$500\,000 \times 200\ \text{ns} = 0.1\ \text{s}$$
   of each second, which is 10% of a core.
2. **Cutting the cost.** Processors with address-space tags in the TLB (lesson 25) cut the cost by avoiding a full TLB flush on each switch.
3. **Removing it.** Newer processors fixed the Meltdown flaw in hardware, removing the need.

**Answer:** page-table isolation costs the server 10% of a core.
:::
:::

::: check
kind: parts
given: f = 2 | 3 | 4; c = 500 | 2000 | 10000; e = 0.01 | 0.05 | 0.2

## A covert channel sends one symbol every {c} cycles at {f} GHz, and {e} of symbols are misread.

::: part
answer: f*1e9/c
unit: symbols/s
tolerance: 0.5%

### What is its raw symbol rate?

::: misses
- [any] Clock over cycles per symbol.
:::
:::

::: part
answer: 1 + e*log(e)/log(2) + (1 - e)*log(1 - e)/log(2)
unit: bits
tolerance: 0.5%

### How much information does each symbol carry?

::: misses
- [1 - e] Errors cost more than their own fraction: use $1 - H(e)$.
- [any] $1 - H(e)$.
:::
:::

::: part
answer: f*1e9/c*(1 + e*log(e)/log(2) + (1 - e)*log(1 - e)/log(2))
unit: bits/s
tolerance: 0.5%

### What information rate can it reach with ideal coding?

::: misses
- [any] Symbol rate × bits per symbol.
:::
:::
:::

::: check
kind: parts
given: s = 5000 | 20000; p = 200 | 400; f = 2 | 4

## A Spectre round costs {s} cycles of setup and probes 256 lines at {p} cycles each, on a {f} GHz core.

::: part
answer: s + 256*p
unit: cycles
tolerance: 0.5%

### How long does each byte take?

::: misses
- [any] Setup plus 256 probes.
:::
:::

::: part
answer: f*1e9/(s + 256*p)/1000
unit: KB/s
tolerance: 0.5%

### How fast does it leak?

::: misses
- [any] Clock over cycles per byte.
:::
:::
:::

::: check
kind: parts
given: W = 32 | 64; t = 45 | 50; n = 10000 | 50000

## DRAM rows cycle in {t} ns, the refresh window is {W} ms, and a vulnerable row flips after {n} activations of a neighbor.

::: part
answer: W*1e-3/(t*1e-9)
tolerance: 0.5%

### How many activations fit in one refresh window?

::: misses
- [any] $W/t_{RC}$.
:::
:::

::: part
answer: n*t*1e-9*1000
unit: ms
tolerance: 0.5%

### How long does it take to reach the flipping threshold?

::: misses
- [any] Activations × row cycle time.
:::
:::
:::

::: check
kind: parts
given: E = 1e5 | 1e6; c = 30 | 200 | 1000

## A mitigation costs {c} ns each time it triggers, {E} times a second.

::: part
answer: E*c*1e-9*100
unit: %
tolerance: 0.5%

### What fraction of a core does it take?

::: misses
- [any] Events per second × cost.
:::
:::
:::

::: check
kind: choice

## A Spectre attack's speculative instructions are squashed when the branch resolves. Why does the secret still leak?

- [ ] Squashing doesn't undo register writes
- [x] Squashing restores architectural state, registers and memory, but not microarchitectural state: the cache line loaded with a secret-dependent address stays, and timing reveals which one it was
- [ ] The branch predictor stores the secret
- [ ] The processor commits the speculative loads

::: misses
- [1] Register writes are undone by the reorder buffer; that's not the channel.
- [3] The predictor is trained by the attacker, but the secret leaves through the cache.
- [4] Squashed instructions never commit.
:::
:::

::: check
kind: derivation
vars: h = 20..100, m = 100..400, s = 5..80

## A receiver times cache accesses: hits average h cycles and misses m, each with Gaussian noise of standard deviation s, and it splits them at the midpoint.

::: step
answer: (h + m)/2

### Where is the threshold?
:::

::: step
answer: (m - h)/(2*s)

### How many standard deviations separate each mean from it?
:::

::: step
answer: Q((m - h)/(2*s))

### What is the error rate?
:::

::: right
Right. Adding timer noise raises s, but the error rate falls off like a Gaussian tail as the gap grows: noise alone rarely closes a channel.
:::
:::

::: check
kind: python

## Write `channel(h, m, sigma, cycles, ghz)`: for a hit/miss timing channel with Gaussian noise, return (error rate at the midpoint threshold, bits per symbol $1 - H(e)$, information rate in bits per second at one symbol per `cycles` cycles).

```python starter
def channel(h, m, sigma, cycles, ghz):
    return (0.0, 1.0, 0.0)
```

```python solution
import math

def channel(h, m, sigma, cycles, ghz):
    e = 0.5 * math.erfc((m - h) / (2 * sigma) / math.sqrt(2))
    H = 0.0 if e <= 0 else -e * math.log2(e) - (1 - e) * math.log2(1 - e)
    bits = 1 - H
    return (e, bits, bits * ghz * 1e9 / cycles)
```

```python tests
e, b, r = channel(40, 200, 30, 2000, 3)
assert abs(e - 0.00383) < 1e-5 and abs(b - 0.9637) < 1e-4, "A 160-cycle gap over 30 cycles of noise: 0.4% errors, 0.96 bits."
assert abs(r - 1.4456e6) < 2e3, "About 1.45 Mb/s."
e2, b2, _ = channel(40, 200, 200, 2000, 3)
assert 0.3 < e2 < 0.4 and b2 < 0.1, "Heavy noise: errors near a third, almost nothing gets through per symbol."
```

::: right
Right. Even with heavy noise the channel isn't closed, only slowed: repeating each symbol enough times still gets a key out.
:::
:::

::: explain
## Why does a cache make a side channel, even when two programs never share data?

::: model
The cache is shared hardware whose state depends on what every program has done recently, and that state shows up in timing: a line that's present returns in tens of cycles, one that was evicted takes hundreds. If a victim's memory accesses depend on a secret, say which table entry it looks up, then which cache sets it fills depends on the secret. An attacker who fills those sets with its own lines and later times reloading them can see which ones the victim evicted, without sharing any data or breaking any permission check. Only isolating or partitioning the shared state, or making the victim's accesses independent of the secret, closes such a channel.
:::
:::

::: practice
::: check
given: e = 0.02 | 0.1 | 0.3
answer: 1 + e*log(e)/log(2) + (1 - e)*log(1 - e)/log(2)
unit: bits
tolerance: 0.5%

## Bits per symbol at a {e} error rate?

::: misses
- [any] $1 - H(e)$.
:::
:::

::: check
given: W = 64 | 32; t = 40 | 55
answer: W*1e-3/(t*1e-9)
tolerance: 0.5%

## Activations per {W} ms window at {t} ns per row cycle?

::: misses
- [any] $W/t_{RC}$.
:::
:::

::: check
given: E = 2e5 | 8e5; c = 100 | 400
answer: E*c*1e-9*100
unit: %
tolerance: 0.5%

## Core fraction for {E} events a second at {c} ns each?

::: misses
- [any] $Ec$.
:::
:::
:::
