On this page

Spectre & Meltdown

In January 2018, three research teams independently disclosed a class of vulnerabilities that shattered a fundamental assumption in computing: that the CPU itself is trustworthy. Meltdown (CVE-2017-5754) and Spectre (CVE-2017-5753, CVE-2017-5715) exploit speculative execution — a performance optimization present in virtually every modern CPU — to leak data across security boundaries.

These are not software bugs. They are hardware design flaws in how CPUs execute instructions, and they affect Intel, AMD, ARM, and other architectures to varying degrees. The mitigations imposed measurable performance penalties that the industry continues to bear.

Related: Security Overview | Heartbleed | Exploits Overview


Background: How CPUs Optimize Execution

Modern CPUs execute instructions far faster than memory can deliver data. To keep the pipeline full, CPUs use two key optimizations that Spectre and Meltdown exploit:

Speculative Execution

flowchart LR
    subgraph Normal["Without Speculation"]
        A1[Instruction 1] --> B1[Wait for<br/>branch result]
        B1 --> C1[Execute correct<br/>branch]
    end

    subgraph Spec["With Speculation"]
        A2[Instruction 1] --> B2[Predict branch<br/>outcome]
        B2 --> C2[Start executing<br/>predicted path]
        B2 --> D2[Also start<br/>other path]
        C2 --> E2{Prediction<br/>correct?}
        E2 -->|Yes| F2[Keep results]
        E2 -->|No| G2[Discard results<br/>But cache side-effects remain!]
    end

    style G2 fill:#dc2626,color:#fff

When the CPU encounters a conditional branch (like an if statement), it does not wait for the condition to resolve. Instead, it predicts which branch will be taken and starts executing it. If the prediction is right, execution continues seamlessly. If wrong, the results are discarded.

The critical insight: discarded speculative results leave observable traces in the CPU cache.

The CPU Cache Side Channel

flowchart TB
    subgraph Attack["Cache Timing Side Channel"]
        A[Attacker forces speculative execution<br/>that accesses secret data] --> B[Speculative code uses secret<br/>as an array index]
        B --> C[array secret value gets<br/>loaded into CPU cache]
        C --> D[Speculation is rolled back<br/>but cache state is NOT]
        D --> E[Attacker probes cache timing<br/>for every possible value]
        E --> F[Fast access = cached = that<br/>was the secret value]
    end

    style A fill:#dc2626,color:#fff
    style D fill:#ea580c,color:#fff
    style F fill:#dc2626,color:#fff

Meltdown (CVE-2017-5754)

What It Does

Meltdown allows a user-space process to read kernel memory. The kernel is supposed to be completely isolated from user programs — it contains page tables, cryptographic keys, passwords of other processes, and the entire physical memory mapping. Meltdown breaks this isolation.

How It Works

// Conceptual Meltdown attack (simplified)
// This code runs in USER space but reads KERNEL memory

// Step 1: Flush the probe array from cache
for (int i = 0; i < 256; i++)
    flush(probe_array[i * PAGE_SIZE]);

// Step 2: Speculatively access kernel memory
// This instruction WILL raise a fault (segfault)
// But before the fault is delivered, the CPU speculatively executes...
char kernel_byte = *(char*)KERNEL_ADDRESS;          // [!code error]

// Step 3: Use the secret value as an index (speculative)
// This loads probe_array[kernel_byte * PAGE_SIZE] into cache
char dummy = probe_array[kernel_byte * PAGE_SIZE];  // [!code error]

// Step 4: The fault is delivered, speculative results discarded
// BUT the cache state change from Step 3 PERSISTS

// Step 5: Time access to each element of probe_array
for (int i = 0; i < 256; i++) {
    int time = time_access(probe_array[i * PAGE_SIZE]);
    if (time < CACHE_HIT_THRESHOLD) {
        // This index was cached — it is the value of kernel_byte!
        printf("Kernel byte: %d\n", i);             // [!code highlight]
    }
}

Meltdown Attack Flow

sequenceDiagram
    participant USR as User Process
    participant CPU as CPU Pipeline
    participant CACHE as L1 Cache
    participant KERN as Kernel Memory

    USR->>CPU: Read kernel address (forbidden)
    Note over CPU: Permission check is slow<br/>Speculative execution begins
    CPU->>KERN: Speculatively read byte value
    KERN-->>CPU: Value = 0x42 (speculative)
    CPU->>CACHE: Load probe_array[0x42 * 4096]
    Note over CACHE: Cache line for index 0x42<br/>is now HOT
    Note over CPU: Permission check completes<br/>ACCESS DENIED — fault raised
    CPU->>CPU: Roll back speculative state
    Note over CACHE: Cache is NOT rolled back!
    USR->>CACHE: Time access to probe_array[i * 4096]<br/>for i = 0..255
    CACHE-->>USR: Index 0x42 is fast (cache hit)
    Note over USR: Deduced: kernel byte = 0x42

Which CPUs Are Affected

VendorAffectedNotes
IntelNearly all (1995-2018)Most severely affected
AMDNot affected by MeltdownDifferent microarchitecture
ARMCortex-A75 and some othersLimited impact
AppleM-series partially affectedCustom mitigation in silicon

Spectre (CVE-2017-5753, CVE-2017-5715)

Spectre is harder to exploit than Meltdown but also harder to mitigate because it exploits a more fundamental mechanism: branch prediction.

Spectre Variant 1: Bounds Check Bypass

The attacker trains the CPU's branch predictor to expect a bounds check will pass, then provides an out-of-bounds index that the CPU speculatively uses before the check completes.

// Vulnerable code pattern — extremely common in any language
if (index < array_size) {                          // Bounds check
    char value = array[index];                     // Speculatively executed
    char dummy = probe_array[value * PAGE_SIZE];   // Leaks 'value' via cache
}

// Attack:
// 1. Call this function many times with valid indices
//    → Branch predictor learns: "the check always passes"
// 2. Call with index = MALICIOUS_OUT_OF_BOUNDS_INDEX
//    → Branch predictor speculates: "check will pass"
//    → CPU speculatively reads array[malicious_index]
//    → That out-of-bounds read accesses SECRET data
//    → probe_array[SECRET * PAGE_SIZE] gets cached
//    → Speculation rolled back, but cache state persists
//    → Attacker probes cache to determine SECRET

Spectre Variant 2: Branch Target Injection

The attacker poisons the Branch Target Buffer (BTB) to redirect speculative execution to a "gadget" — a sequence of existing code that leaks data through cache side effects.

flowchart TB
    subgraph V2["Spectre V2: Branch Target Injection"]
        A[Attacker trains BTB with<br/>malicious branch target] --> B[Victim executes indirect<br/>branch instruction]
        B --> C[CPU speculatively jumps to<br/>attacker-chosen gadget]
        C --> D[Gadget accesses secret data<br/>and leaks via cache]
        D --> E[Speculation rolled back<br/>Cache state persists]
        E --> F[Attacker reads secret<br/>via cache timing]
    end

    style A fill:#dc2626,color:#fff
    style C fill:#ea580c,color:#fff

Which CPUs Are Affected

VariantIntelAMDARM
Spectre V1 (Bounds Check Bypass)YesYesYes
Spectre V2 (Branch Target Injection)YesYesYes
MeltdownYesNoSome

Mitigations

Software Mitigations

MitigationTargetsHow It WorksPerformance Cost
KPTI (Kernel Page Table Isolation)MeltdownSeparate page tables for kernel and user space; kernel addresses not mapped in user space5-30% on syscall-heavy workloads
RetpolineSpectre V2Replace indirect branches with return trampolines that confuse branch predictor5-15% on some workloads
IBRS/IBPBSpectre V2CPU microcode updates that flush/restrict branch prediction state2-10%
LFENCE barriersSpectre V1Insert serializing instructions after bounds checksMinimal per-instance
Site isolation (browsers)Spectre V1 in JSSeparate processes for different originsMemory overhead (~10-20%)

KPTI (Kernel Page Table Isolation)

flowchart LR
    subgraph Before["Before KPTI"]
        U1["User Process Page Table"]
        U1 --- K1["User Memory<br/>(mapped)"]
        U1 --- KM1["Kernel Memory<br/>(mapped but protected)"]
        Note1[Meltdown can bypass<br/>protection and read<br/>kernel memory]
    end

    subgraph After["After KPTI"]
        U2["User Process Page Table"]
        U2 --- K2["User Memory<br/>(mapped)"]
        U2 --- KM2["Kernel Memory<br/>(NOT MAPPED)"]
        Note2[Cannot read what is<br/>not even in the<br/>page table]
    end

    style KM1 fill:#dc2626,color:#fff
    style KM2 fill:#16a34a,color:#fff
    style Note1 fill:#dc2626,color:#fff
    style Note2 fill:#16a34a,color:#fff

The Performance Tax

KPTI requires flushing TLB entries on every syscall (context switch between user and kernel). For workloads with heavy syscall usage (database servers, I/O-heavy applications), this imposes a 5-30% performance penalty. This is a permanent cost that every Linux and Windows system now pays.

Newer Intel CPUs include Process Context Identifiers (PCIDs) that reduce the TLB flush overhead, bringing the penalty closer to 1-5%.

Retpoline (Return Trampoline)

; Normal indirect branch (vulnerable to Spectre V2)
jmp *%rax          ; CPU speculatively follows trained BTB entry

; Retpoline replacement
call retpoline_target
capture_spec:
    pause                   ; Speculative execution lands here
    lfence                  ; Serializing — stops speculation
    jmp capture_spec        ; Infinite loop (never actually reached)
retpoline_target:
    mov %rax, (%rsp)       ; Overwrite return address with actual target
    ret                     ; Return to actual target via return stack
                           ; Branch predictor cannot be trained for this

Real-World Impact

Performance Benchmarks After Mitigations

Workload TypePerformance ImpactWhy
Database (PostgreSQL)7-17% slowerHeavy syscall usage, context switching
I/O intensive10-30% slowerFrequent kernel transitions
Compute-bound1-3% slowerRarely enters kernel
Web servers5-10% slowerNetwork I/O, file serving
Compilation5-8% slowerFile I/O, process spawning
Cloud VMs10-20% slowerHypervisor transitions compound the cost

Cloud Provider Response

ProviderAction
AWSPatched hypervisors, offered Nitro Enclaves for sensitive workloads
Google CloudDeveloped and deployed retpoline, open-sourced the technique
AzurePatched hypervisor, recommended customer OS updates
All providersAbsorbed the performance overhead (customers pay indirectly through higher resource usage)

Later Variants

Spectre and Meltdown opened the floodgates for CPU side-channel research. The original vulnerabilities spawned an entire field:

VariantCVEYearWhat It Does
Spectre V1CVE-2017-57532018Bounds check bypass
Spectre V2CVE-2017-57152018Branch target injection
MeltdownCVE-2017-57542018Rogue data cache load
Spectre-NG / V3aCVE-2018-36402018Rogue system register read
Spectre V4CVE-2018-36392018Speculative store bypass
Foreshadow (L1TF)CVE-2018-36152018L1 terminal fault (SGX, VMs)
ZombieLoad (MDS)CVE-2019-110912019Microarchitectural data sampling
RIDLCVE-2018-121302019Rogue in-flight data load
Downfall (GDS)CVE-2022-409822023Gather data sampling (AVX)
InceptionCVE-2023-205692023Phantom speculation (AMD)

Defense Strategies

For Application Developers

  1. Keep OS and firmware updated: Most Spectre/Meltdown mitigations are delivered via OS kernel updates and CPU microcode updates. Apply them promptly.
  2. Use constant-time algorithms for secrets: Avoid branching or memory access patterns that depend on secret values (already good practice for timing attacks).
  3. Enable site isolation in browsers: Modern browsers isolate different sites into separate processes specifically because of Spectre.
  4. Use hardware security features: Intel SGX (with caveats), AMD SEV, ARM TrustZone for sensitive computations.
  5. Consider the performance budget: If your workload is syscall-heavy, budget 10-20% overhead for mitigations.

For Cloud and Infrastructure Engineers

  • Hyperthreading (SMT) can leak data between threads: Consider disabling SMT on hosts processing sensitive data (significant performance cost)
  • VM escape via side-channels is real: Foreshadow/L1TF specifically targets VM isolation
  • Microcode updates are as important as OS patches: CPU vendors release microcode updates that must be deployed to firmware
  • Containers share a kernel: Spectre attacks between containers on the same host are feasible — consider VM-level isolation (Kata Containers, Firecracker) for untrusted workloads

Key Takeaways

LessonImplication
Hardware is not trustworthy by defaultCPUs optimize for performance, not security — speculative execution trades security for speed
Side channels are a real attack vectorYou do not need a memory corruption bug to leak data — timing differences are enough
Mitigations have permanent costsThe industry pays a 5-30% performance tax for Spectre/Meltdown mitigations indefinitely
Shared infrastructure amplifies riskCloud VMs, containers, and hyperthreading increase the attack surface for side-channel leaks
Research does not stopEach new CPU generation brings new side-channel variants — this is an ongoing arms race
The software/hardware contract was brokenOperating systems assumed the CPU enforced memory isolation correctly; it did not

Further Reading