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.5 Spectre v4 (speculative store bypass)

Spectre v4 (CVE-2018-3639, “speculative store bypass”, SSB) uses the memory-disambiguation predictor from 14.1. A younger load is allowed to execute before an older store whose address is not yet known. If the predictor guesses “no alias” and the addresses turn out to match, the load has transiently read the stale value, the one in the location before the store. The load is replayed and the architectural result is correct. Any address formed from the stale value in the meantime has already left a cache fill.

Unlike v1 and v2, the attacker does not train anything across a context boundary. The bypass happens inside the victim’s own instruction stream. The attacker’s part is to arrange that the stale value is one it chose.

The gadget shape

/* victim, e.g. an SMC handler copying a caller-supplied index into a context */
ctx->idx = untrusted_idx;           /* (1) attacker-chosen value lands in memory    */
...
ctx->idx = untrusted_idx & MASK;    /* (2) sanitising store; its address is slow     */
v = table[ctx->idx];                /* (3) load may bypass (2) and read (1)          */
w = probe[v * 64];                  /* (4) dependent load: the cache channel         */

What makes (3) bypass (2) is that the store’s address resolves late. Typical causes are a base pointer that missed in the cache, or a pointer chain. The store’s data being late does not matter, because a core that knows the addresses match will forward or wait. Compilers create this shape without being asked: a register spilled to the stack and reloaded, or a struct field that is checked and then rewritten. Masking patterns from 14.3 do not help if the masked value is stored and then reloaded, because the reload is exactly the load that bypasses.

For firmware, the classic place is EL3 or S-EL1 code that saves the caller’s x0–x7 into a context structure, sanitises some of them in place, and later reloads them.

What the architecture provides

MechanismFeature / ID fieldScopeCost
PSTATE.SSBSFEAT_SSBS, ID_AA64PFR1_EL1.SSBS ≥ 0b0001Every load while the bit is 0Ongoing, core-dependent
SCTLR_ELx.DSSBS (bit 44)FEAT_SSBSThe value PSTATE.SSBS takes on exception entry to ELxNone by itself
MSR SSBS, #immFEAT_SSBSDirect write of the bit, usable at EL0Cheap toggle
MRS/MSR with the SSBS system registerFEAT_SSBS2, ID_AA64PFR1_EL1.SSBS = 0b0010Read the bit, or write it from a registerCheap
SSBBDefined for all Armv8.0 implementationsPoint fix: same virtual addressOne barrier
PSSBBDefined for all Armv8.0 implementationsPoint fix: same physical address, any VAOne barrier

FEAT_SSBS is permitted from v8.0. Some secondary sources say it is mandatory from v8.5, but the ID_AA64PFR1_EL1.SSBS field description carries no “From Armv8.x, the value 0b0000 is not permitted” statement (unlike CSV2, CSV3 or SB), so probe ID_AA64PFR1_EL1.SSBS and do not infer it from a version claim.

PSTATE.SSBS: read the name carefully

SSBS stands for Speculative Store Bypass Safe. The polarity is the reverse of what a “mitigation enable” bit usually looks like:

  • PSTATE.SSBS = 0: the code is not declared safe. Hardware is not permitted to let loads speculatively bypass older stores in a way that could be exploited through a timing side channel. Mitigation on.
  • PSTATE.SSBS = 1: the code is declared safe, so the bypass is allowed. Mitigation off, full performance.

PSTATE.SSBS is held in SPSR_ELx bit 12 for an AArch64 context and bit 23 for an AArch32 context (where it also sits in CPSR). Like every PSTATE bit it is restored by ERET (2.4).

DSSBS: the entry default

On exception entry to ELx, PSTATE.SSBS is set to SCTLR_ELx.DSSBS. This lets a more privileged level start with the mitigation on, whatever the interrupted context had. With DSSBS = 0, an EL0 process that set SSBS=1 for its own speed cannot leave the kernel running unmitigated. TF-A clears SCTLR_EL3.DSSBS in el3_entrypoint_common, so EL3 always runs with SSBS=0. Its SPSR_64() macro also clears the SSBS bit, so a lower EL that TF-A enters for the first time starts mitigated as well.

With FEAT_SSBS, software can change the bit three ways: exception entry (via DSSBS), ERET (via SPSR), and MSR SSBS, #imm, whose decode is gated on FEAT_SSBS, not FEAT_SSBS2. Linux uses the immediate form for its own EL1 state on any FEAT_SSBS core, and edits the saved SPSR_EL1.SSBS of a task that opted in or out. FEAT_SSBS2 adds only the MRS/MSR register forms (MRS Xt, SSBS / MSR SSBS, Xt), so software can also read the bit:

    msr     ssbs, #0          // mitigation on (FEAT_SSBS)
    sb                        // see the erratum note below; dsb nsh; isb without FEAT_SB
    ...                       // sensitive store/reload sequence
    msr     ssbs, #1          // mitigation off

GNU as needs -march=...+ssbs for these mnemonics. MSR SSBS is permitted at EL0. That is by design: v4 is a self-protection problem, and a JIT or sandbox runtime is entitled to choose for itself. It also means a privileged level must never assume that the SSBS value it set before ERET is still in force when that context traps back. It must rely on DSSBS on entry.

Bring-up note: An MSR SSBS, #imm is architecturally self-synchronising, but many Arm cores have an erratum in which MSR SSBS, #0 may not affect younger speculative instructions. Linux tracks it as ARM64_ERRATUM_3194386. It covers Cortex-A76/A77/A78/A78C/A710/A715/A720/A725, Cortex-X1/X1C/X2/X3/X4/X925 and Neoverse N1/N2/N3/V1/V2/V3, each under its own erratum number. The workaround is an SB (or ISB) after every privileged write to SSBS. Linux also hides the ID_AA64PFR1_EL1.SSBS field, and so HWCAP_SSBS, from EL0 on affected cores. Check the SDEN for the cores you ship (1.3).

SSBB and PSSBB: point barriers

These are DSB encodings with CRm = 0b0000 and CRm = 0b0100 respectively (7.6). Despite the encoding, their architectural meaning is not a DSB completion guarantee. What they define is a speculation restriction:

  • SSBB: a load after the barrier does not speculatively read an entry earlier in the coherence order than the one written by the latest older store, before the barrier, to the same location using the same virtual address. Symmetrically, a load before the barrier does not speculatively read data from a store after it.
  • PSSBB: the same, but for stores to the same physical location, whatever VA they used. Use it when the store and the reload may go through different aliases, for example a buffer mapped twice or a kernel writing through the linear map and reading through a vmalloc alias.

These encodings were reserved DSB options before Spectre, and the Arm ARM requires reserved DSB options to execute as a full-system DSB. A core older than the definition therefore runs them as DSB SY. That is safe but slow, so the Arm ARM’s statement that they exist in all v8.0 implementations is not a promise of cheapness. Place them between the sanitising store and the reload:

    str     x1, [x0, #CTX_IDX]    // attacker value saved earlier...
    and     x1, x1, #0xff
    str     x1, [x0, #CTX_IDX]    // ...overwritten with the sanitised one
    ssbb                          // reload below cannot see the older value
    ldr     x2, [x0, #CTX_IDX]
    ldrb    w3, [x4, x2]

For C, there is no compiler intrinsic that places SSBB reliably, because the compiler chooses where reloads go. Code that needs v4 protection runs with SSBS=0 rather than with hand-placed barriers.

Cores without SSBS: chicken bits and SMCCC_ARCH_WORKAROUND_2

Cores affected by v4 that predate SSBS have an IMPLEMENTATION DEFINED “disable load pass store” control in an CPUACTLR*_EL1 register. Only EL3 can write it, or a lower EL if EL3 grants access through ACTLR_EL3, which production firmware should not do. TF-A’s advisory TFV-7 lists them:

CoreControlTF-A approach
Cortex-A57, A72CPUACTLR_EL1 bit 55Static: set at every reset, never cleared
Cortex-A73bit 3 of S3_0_C15_C0_0 (undocumented in TRM)Static
Cortex-A75CPUACTLR_EL1 bit 35Static
Cortex-A76CPUACTLR2_EL1 bit 16Dynamic (DYNAMIC_WORKAROUND_CVE_2018_3639)

The register encodings and bit numbers are core-specific. Take them from the TRM or advisory for your exact core and revision, never from this table.

Static mitigation sets the bit once per reset (cold and warm). The whole stack is mitigated and nobody else has to do anything. Dynamic mitigation lets a lower EL ask EL3 to switch the bit for its own context with SMCCC_ARCH_WORKAROUND_2 (function ID 0x80007FFF, SMCCC v1.1+):

  • w1 = 1: enable the mitigation for the caller; w1 = 0: disable it.
  • Discovery is SMCCC_ARCH_FEATURES(0x80007FFF). TF-A returns 0 when this PE needs dynamic mitigation and the call must be made. It returns 1 when the call is not needed on this PE, either because the PE is unaffected or because it implements SSBS, so the caller should use PSTATE.SSBS. It returns NOT_REQUIRED (-2) when firmware has mitigated permanently and the call has no effect. NOT_SUPPORTED (-1) means firmware says nothing, so a careful caller treats the PE as possibly vulnerable, as Linux does.

TF-A’s Cortex-A76 implementation shows the shape. On every entry to EL3 from a lower EL, its private vector table sets the chicken bit, so EL3 itself always runs mitigated. A fast path in the sync vector recognises SMC #0 with w0 = 0x80007FFF, sets or clears the bit according to w1, records whether the bit should be cleared again on the next el3_exit, and executes ERET without entering the C runtime. The call is therefore cheap enough for an OS to make on every context switch. Linux’s default policy (ssbd=kernel) keeps the kernel mitigated and leaves user tasks unmitigated unless they opt in with prctl(PR_SET_SPECULATION_CTRL) or seccomp. That is why it either edits SPSR.SSBS or calls WORKAROUND_2 at the kernel/user boundary.

Security note: The v4 threat for Secure firmware is narrow but real: a Non-secure caller chooses register values that EL3 or the SPMC spills and reloads. Firmware should not take the Non-secure world’s SSBS choice into the Secure world. With SSBS, that is guaranteed by SCTLR_EL3.DSSBS = 0 and an S-EL1 SCTLR_EL1.DSSBS = 0 saved and restored per world. Without SSBS, use the static chicken bit, or the dynamic scheme’s “always on in EL3” entry path. A dynamic implementation that only switches the bit when asked, and not on every EL3 entry, leaves EL3 running with whatever the Non-secure kernel last requested.

Validation note: The architecture gives no PMU event for “store bypassed”. Validate with a timing or throughput microbenchmark: a store whose address hangs off a cache-missing load, followed by a load to the same address. With bypass allowed, the load issues early and is replayed. Look for a rise in a pipeline-flush or replay event (IMPLEMENTATION DEFINED, check the TRM’s PMU chapter) and a change in cycles per iteration between SSBS=1 and SSBS=0, or chicken bit off and on. Confirm the entry default architecturally: on a FEAT_SSBS2 core, MRS x0, SSBS as the first instruction of the EL3 sync handler must read 0. Run the test on every core type in a heterogeneous cluster, because SSBS support and the chicken bit can differ per core (1.1).

Checkpoint

Q14.5.1 An EL1 kernel on a FEAT_SSBS core (no SSBS2) sets SCTLR_EL1.DSSBS = 1 for performance and relies on clearing SPSR_EL1.SSBS before returning to tasks that requested the mitigation. Which statement is correct?

  • A. The kernel is mitigated, because the task’s SSBS=0 is restored into PSTATE on every exception entry
  • B. The kernel cannot change SSBS at all without FEAT_SSBS2, so the configuration is impossible
  • C. Opted-in tasks are mitigated at EL0, but every kernel entry starts with SSBS=1, so kernel code runs without the v4 mitigation
  • D. DSSBS only affects AArch32 contexts, so the configuration has no effect on AArch64
Answer

C. On exception entry, PSTATE.SSBS is loaded from SCTLR_ELx.DSSBS of the target EL, not from the interrupted context. DSSBS = 1 therefore means the kernel always starts with the bypass allowed. A has the direction wrong: the task’s value is saved into SPSR, not kept. B is wrong because SPSR editing works with plain FEAT_SSBS, and so does MSR SSBS, #imm; SSBS2 adds only the register-form MRS/MSR.

Read more: Arm ARM (DDI0487), SCTLR_EL1.DSSBS; PSTATE.SSBS

Q14.5.2 A Secure partition writes a sanitised length into a shared structure through its own mapping. A helper then reloads it through a second VA alias of the same page, and uses it as an index. Which barrier, placed between the store and the reload, prevents a speculative read of the old value?

  • A. PSSBB, because the store and the reload use different VAs for the same physical location
  • B. SSBB, because it covers every store before it regardless of address
  • C. CSDB, because it stops speculative use of the loaded value
  • D. DMB ISHLD, because it orders the load after the store
Answer

A. SSBB covers only stores to the same location using the same virtual address, so it is the tempting but insufficient choice when aliases are involved. PSSBB extends the restriction to the same physical location. CSDB concerns values produced under conditional-select misprediction (v1). A DMB orders observation, not speculative execution.

Read more: Arm ARM (DDI0487), SSBB and PSSBB instruction descriptions

Q14.5.3 On a Cortex-A76 platform built with DYNAMIC_WORKAROUND_CVE_2018_3639, the Non-secure kernel calls SMCCC_ARCH_WORKAROUND_2 with w1 = 0 for a task. What is the state of the “disable load pass store” bit while EL3 runs the next unrelated SMC from that task?

  • A. Clear, because the call disabled the mitigation for the whole PE until re-enabled
  • B. Clear, but only for SMCs routed to the Secure world
  • C. Set only if the SMC function ID is in the Arm Architecture range
  • D. Set, because the EL3 entry path re-enables it on every lower-EL entry and clears it again only on exit to the caller
Answer

D. The dynamic scheme is per context: EL3 forces the mitigation on at entry and restores the caller’s requested state on el3_exit. Without this, EL3 would inherit the Non-secure world’s choice, which is the trap described by A. The function ID and the destination world do not affect the entry path.

Read more: Arm, Firmware interfaces for mitigating cache speculation vulnerabilities (SMCCC_ARCH_WORKAROUND_2); TF-A security advisory TFV-7