Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

14.1 How speculation is modelled and what it attacks

Every speculation side channel has the same three ingredients:

  1. a predictor that a less-privileged or different context can train;
  2. a transient window, in which the PE executes instructions it will later squash;
  3. a covert channel, which is microarchitectural state that the squash does not roll back and that the attacker can measure. In practice this is almost always cache residency.

The architecture describes none of the hardware involved. Predictors, reorder buffers and line-fill buffers are not in DDI0487. What the Arm ARM does define is a set of permissions and, since 2018, a short list of restrictions and barriers. This section separates the two and maps each predictor to the variant that abuses it. The hardware picture is in 6.1. Read that first if “post-commit store buffer” or “squash” are not yet concrete for you.

What the architecture says about speculation

The memory model (7.1) is defined over architecturally executed instructions, the ones that would run in a simple sequential execution of the program. Speculative execution is everything else: instructions that the PE starts and then discards. The architecture constrains speculation in a few places only.

RuleStatusWhere it matters
Reads of Normal memory can be made speculatively, at any time, to any location mapped as Normal and readable in the current translation regimePermittedThe whole cache side channel (14.2)
Caches can allocate a line for any Normal Cacheable location at any time, for a speculative access or a prefetchPermittedCovert channel, and the stale-line bugs of 8.7
Translation table walks and TLB allocation can be speculativePermittedSpeculative-AT errata (14.2), break-before-make (9.2)
Speculative data accesses to any Device memory typeNot permittedMMIO safety; instruction fetch is a separate case (6.2)
Speculative writes becoming visible to any other observerNot permittedSquashed stores leave memory unchanged; they can still affect timing (v4)
Instruction fetch from any location that is not execute-neverPermitted, including speculativelyDevice regions must be XN

Everything else about speculation is microarchitecture. In particular, the architecture says nothing about what state a squash restores beyond architectural state. That gap is the attack surface.

After Spectre, Arm added two kinds of architectural text:

  • Restrictions on the effects of speculation. These are feature-gated promises about what speculation will not do, each advertised by an ID register field. Examples are FEAT_CSV2 (branch targets trained in one hardware-described context cannot control speculation in another), FEAT_CSV3 (data loaded speculatively from a location that would fault cannot be used to form a cache-timing channel, see 14.6) and FEAT_SSBS (a controllable store-bypass restriction, 14.5). The Arm ARM collects the general statements under “Restrictions on the effects of speculation” in the AArch64 application-level memory model chapter.
  • Speculation barriers. These are instructions with defined semantics for speculative execution: CSDB (a HINT, so it is a NOP on older cores), SB (FEAT_SB), SSBB and PSSBB (DSB encodings), CLRBHB (FEAT_CLRBHB, a HINT, 14.4), and the prediction-restriction instructions of FEAT_SPECRES. Ordinary barriers have no speculation semantics. A DMB orders observation and says nothing about whether a younger load executes. DSB followed by ISB happens to stop speculation on all known cores, and Arm documents it as the pre-SB fallback. That is guidance, not a property of DSB or ISB.

The practical rule: any mitigation must reduce to an ID-register-advertised restriction, an architectural speculation barrier, or core-specific behaviour documented by the core vendor (a TRM statement, an erratum notice or a Security Center advisory). If it is none of these, it is a guess.

Hardware-described context

The CSV2, CSV3 and SPECRES text depends on the term hardware-described context: roughly the combination of Security state, Exception level, VMID (when EL2 is enabled for that state), ASID (for regimes that use one), and, with FEAT_CSV2_2, the value of SCXTNUM_ELx. The restrictions apply between such contexts. Two consequences follow.

  • The restrictions do not apply within a context. A JavaScript engine and the sandboxed script it runs share one hardware context, so CSV2 does nothing for them. That is what SCXTNUM_ELx is for (14.7).
  • EL3 is its own context. On a CSV2 core, Non-secure EL1 cannot train branch targets that EL3 then uses. On a pre-CSV2 core it can, and EL3 firmware must remove the trained state on entry (14.4).

Predictor structures and the variants that abuse them

The structures below are non-architectural. Their sizes, indexing and tagging vary by core and are rarely documented in full. The mapping from structure to variant is stable across cores, though.

StructureWhat it predictsTypical index and tagVariantCVESection
Conditional (direction) predictorTaken or not taken for B.cond, CBZ, TBZPC bits, often hashed with global historyv1, bounds check bypassCVE-2017-575314.3
Branch target buffer (BTB) and indirect predictorTarget of BR, BLR, and on some cores RETPC bits (VA), partial tag; pre-CSV2 often no context tagv2, branch target injectionCVE-2017-571514.4
Branch history buffer (BHB)Recent taken-branch history used to select an indirect targetGlobal, shared across exception entry on affected coresSpectre-BHB / BHICVE-2022-2396014.4
Return stackTarget of RETPush on BL/BLR, pop on RET; on underflow, some cores fall back to the BTBReturn-stack attacks. Exposure is core-specific; Arm advisories treat it as part of the v2 analysis—14.4
Memory disambiguation (store-to-load forwarding prediction)Whether a load may execute before an older store with an unresolved addressLoad PCv4, speculative store bypassCVE-2018-363914.5
Fault deferral in the load pipelineNot a predictor: a permission or translation fault is raised at commit, not when data returns—v3 (Meltdown), v3a (rogue system register read)CVE-2017-5754, CVE-2018-364014.6
Sequential fetch past unconditional control flowThat RET, BR, ERET, SVC and so on fall through—Straight-line speculation (SLS)CVE-2020-1384414.2

Read the table as a set of questions for a code review:

  • Can the attacker steer a branch direction? (v1)
  • Can the attacker choose a target, either directly or through history? (v2, BHB)
  • Can a load run ahead of a store that should have overwritten its source? (v4)
  • Does a faulting load forward data before the fault is taken? (v3, v3a)

Different questions need different fixes, and a fix for one does nothing for another. CSDB does not touch the BTB, and CLRBHB does not stop store bypass.

The covert channel: why the cache

A transient instruction can only leave measurable traces in shared microarchitectural state. The usual channel is L1D or LLC residency:

/* the transient gadget: two dependent loads */
uint8_t secret = *(volatile uint8_t *)victim_ptr;   /* transient read */
(void)probe[secret * 64];                            /* transient fill of one line */

/* the attacker, afterwards, architecturally */
for (int i = 0; i < 256; i++)
    t[i] = time_load(&probe[i * 64]);                /* the fast one reveals 'secret' */

The first load reads the secret. The second turns it into an address, and the cache fill for that address survives the squash. The attacker then times loads to find which line is now resident:

  • Flush+Reload, when the attacker and victim share memory. On AArch64, EL0 can flush by VA with DC CIVAC when SCTLR_EL1.UCI=1, which Linux sets.
  • Prime+Probe, when they do not. The attacker fills a cache set and watches for evictions.

Other channels exist: TLB state, prefetcher training, port contention and DRAM row buffers. They are slower and noisier, and the mitigations in this part do not depend on which one is used. Taking away the timer does not help either. CNTVCT_EL0 can be trapped or coarsened, but a counting thread on another core works as a timer.

So the gadget a reviewer looks for is a load whose address depends on data obtained speculatively, reachable in a transient window. One load alone leaks nothing through the cache. The data has to reach a second address-forming operation. That is why the masking patterns in 14.3 work: they break the dependency, not the load.

Security note: “Transient” includes the window after a mispredicted exception return path, after an SVC or SMC that the front end did not recognise as a control-flow change, and after an ERET (SLS). EL3 code that does ERET and then has data-dependent loads in the following bytes, for example a literal pool or the next function, has a gadget even though those bytes are never architecturally executed. TF-A follows every ERET with a speculation barrier for this reason (14.2).

How the variants map onto a TEE threat model

For EL3 and S-EL1/S-EL2 firmware, the attacker is normally Non-secure EL1 (a compromised kernel) or EL0 calling through it. The secrets are Secure-world keys and memory.

VariantAttacker trainsVictim runsFirmware role
v1Branch directions, through repeated in-bounds SMC argumentsBounds-checked table lookups in an SMC or TA handlerMask indices in handlers (14.3)
v2 / BHBBTB or BHB from NS-EL1EL3 or S-EL1 indirect branches (handler tables, function pointers)Vector-entry mitigation, SMCCC_ARCH_WORKAROUND_1/3 (14.4)
v4Not a training attack: the bypass happens in the victim’s own codeStore-then-load sequences on attacker-influenced dataSSBS or chicken bits, SMCCC_ARCH_WORKAROUND_2 (14.5)
v3 / v3aNothing: the attacker reads directly—Firmware mostly unaffected; KPTI is an OS problem (14.6)
Cross-world prediction residuePredictors shared across a world switchThe other worldFEAT_SPECRES on context switch (14.7)

Validation note: Mitigations are validated by disabling them and checking that the attack signal appears, then enabling them and checking that it disappears. A test that passes with the mitigation enabled but never showed a signal without it proves nothing. Measure the signal on silicon or cycle-accurate RTL, because FVP and QEMU do not model predictors or cache timing. The PMU gives a lower-noise oracle than raw timing. Count BR_MIS_PRED (0x10) and BR_PRED (0x12) to confirm that training actually happened, INST_SPEC (0x1B) against INST_RETIRED (0x08) to confirm that a transient window opened, and L1D_CACHE_REFILL (0x03) on the probe array to see the fill without timing noise (16.5). Event availability beyond the common set is IMPLEMENTATION DEFINED; check PMCEID0_EL0/PMCEID1_EL0.

Checkpoint

Q14.1.1 A reviewer argues: “This EL3 handler has a bounds-checked load, then DMB ISH, then a second load that uses the first value as an index. The DMB orders the loads, so there is no Spectre v1 gadget.” What is wrong with the argument?

  • A. Nothing. A DMB prevents younger loads from being issued until older loads complete.
  • B. The DMB needs to be a DMB SY. An Inner Shareable barrier does not cover EL3.
  • C. A DMB constrains the order in which architecturally executed accesses are observed. It has no defined effect on whether younger instructions execute speculatively, so both loads can still run in the transient window.
  • D. The argument is right only for Device memory, where the DMB also prevents speculative reads.
Answer

C. Ordinary barriers are defined in terms of observation order between architecturally executed accesses. They carry no speculation semantics. A core can execute the younger load speculatively and later make the outcome appear ordered, while the cache fill survives the squash. Only CSDB, SB, SSBB/PSSBB, CLRBHB and the SPECRES instructions have defined speculation effects, and DSB+ISB is a documented fallback rather than an architectural guarantee. A is the common misconception that DMB stalls issue. D confuses the Device-memory no-speculation rule, which is a property of the memory type, with a property of the barrier.

Read more: Arm ARM (DDI0487), Restrictions on the effects of speculation; Data Memory Barrier (DMB)

Q14.1.2 A core reports ID_AA64PFR0_EL1.CSV2 = 1. A browser JIT at EL0 runs untrusted script and its own trusted runtime in the same process. Is the runtime protected from branch-target injection by the script?

  • A. No. CSV2 restricts branch-target training only between hardware-described contexts, and the script and runtime share EL, Security state, ASID and VMID. Separating them needs SCXTNUM_EL0 with FEAT_CSV2_2, or a software mitigation.
  • B. Yes. CSV2 means the BTB is tagged with the full VA, so aliasing between script and runtime is impossible.
  • C. Yes, as long as the runtime is mapped with PXN.
  • D. No, because CSV2 applies only to EL1 and above.
Answer

A. CSV2 is a restriction between hardware-described contexts. Within one context, branch-target training is still allowed to influence speculation. FEAT_CSV2_2 adds SCXTNUM_ELx to the context, so software can create sub-contexts (14.7). B describes a possible implementation, but the architecture promises nothing about tagging. D is wrong because CSV2 covers EL0 contexts too, distinguished by ASID.

Read more: Arm ARM (DDI0487), ID_AA64PFR0_EL1.CSV2; SCXTNUM_EL0

Q14.1.3 Which code fragment is the minimal cache-channel Spectre gadget, assuming x1 is attacker-controlled and the code runs inside a transient window?

  • A. ldr x2, [x0, x1] alone
  • B. ldr x2, [x0, x1] followed by str x2, [sp]
  • C. ldr x2, [x0, x1] followed by cmp x2, #0
  • D. ldrb w2, [x0, x1] followed by ldr x3, [x4, x2, lsl #6]
Answer

D. The leak needs the speculatively read value to form a second address, so that the line filled by the second load encodes the secret. A brings the secret into a register, but the only line touched is the one at an attacker-chosen address, which the attacker already knows. B’s store never leaves the core speculatively. C can leak through branch timing only if a dependent branch follows, which is a different and weaker channel.

Read more: Arm whitepaper “Cache Speculation Side-channels”; Arm ARM (DDI0487), Restrictions on the effects of speculation