archive / 2018

Meltdown, Spectre and a week of rebooting nodes

How we patched our Kubernetes nodes against Meltdown in January, what it cost us in performance, and why I now know more about CPU caches than I ever wanted to.

The year was barely three days old when the first message showed up in our team channel: a link to an article about a “mysterious kernel patch” that would make Intel processors slower. A day later the bugs had a name, a logo and a website. Meltdown and Spectre. Allez, so much for a quiet start of the year.

I’ve read more about speculative execution in the past three weeks than during my entire education. Here’s what stuck, and mostly what we actually did about it on the platform at the scale-up.

So what’s going on?#

Very briefly, and with the necessary simplifications:

  • Meltdown (CVE-2017-5754) lets a normal process read kernel memory on most Intel CPUs. The processor speculatively performs a memory read before checking whether it’s allowed to. The result is thrown away afterwards, but it leaves traces in the cache, and with a timing attack you can read those traces back.
  • Spectre (CVE-2017-5753 and CVE-2017-5715) is broader and nastier: you trick the branch predictor so that another process (or the kernel) speculatively executes code that leaks its secrets into the cache. That one also affects AMD and ARM, to varying degrees.

For Meltdown there’s a fairly clean software fix: KPTI (Kernel Page Table Isolation). The kernel simply isn’t mapped into the userspace page tables anymore. The downside is that every transition between userspace and the kernel (every syscall, every interrupt) gets more expensive, because the page tables have to be switched.

For Spectre the story is messier: compiler changes (retpolines), microcode updates from the CPU vendors, and patches in browsers. And that microcode isn’t quite there yet either: this week Intel itself advised people to stop rolling out the current microcode updates, because some machines spontaneously reboot with them. Lovely.

What this means for containers#

The first question from a colleague was: “Do we need to rebuild all our Docker images?” No. That’s the beautiful (or scary, depending on how you look at it) thing about containers: they all share the host’s kernel. A container is not a VM. If the node’s kernel is vulnerable, every container on that node can in principle read kernel memory, and with it the memory of its neighbours.

So the work was on the nodes, and on our cloud provider’s side. The provider patched the underlying hypervisors themselves in the first week and rebooted part of our VMs for it, with less advance warning than usual. That went surprisingly well. Kubernetes did what it’s for: pods moved, a few alerts fired, nobody noticed a thing.

But the guest kernel on our own nodes, that was still our problem.

The actual patching#

Our nodes run Ubuntu 16.04. Canonical had kernels with KPTI ready a few days after disclosure. The first version didn’t boot on some machines, and a fixed one followed a day later. I was glad we hadn’t pushed everything out in the first hour.

We did the rolling update with a simple script. Not pretty, but it worked:

#!/bin/bash
set -euo pipefail

for node in $(kubectl get nodes -o jsonpath='{.items[*].metadata.name}'); do
  echo "==> $node"
  kubectl drain "$node" --ignore-daemonsets --delete-local-data --timeout=300s

  ssh "$node" 'sudo apt-get update -q && sudo apt-get install -y -q linux-image-generic && sudo reboot' || true

  # ugly, but the node still reports Ready for a bit before it actually goes down
  sleep 60
  until kubectl get node "$node" | grep -q ' Ready'; do sleep 10; done

  ssh "$node" 'uname -r; dmesg | grep -i "page tables isolation"'
  kubectl uncordon "$node"
done

That dmesg line is the quickest way to check whether KPTI is active. You want to see Kernel/User page tables isolation: enabled. If you want to be more thorough, run the script from https://github.com/speed47/spectre-meltdown-checker: it tests all three variants and also tells you whether your microcode and kernel support the Spectre mitigations.

A few lessons from the rollout:

  1. PodDisruptionBudgets suddenly turn out to matter a lot. Two services didn’t have one, and both happened to have their two replicas on the same node. That was a short outage that didn’t need to happen.
  2. A drain that hangs on a pod with local data is always that one forgotten pod from someone who was “just testing something” in 2017.
  3. Don’t do everything in one evening. We did a third of the nodes per day, with a day in between to see whether anything weird happened.

The performance impact#

This is where it got interesting. The news talked about “up to 30% slower”, but that doesn’t say much. The impact depends entirely on how many syscalls you make. Pure number crunching doesn’t notice anything. Something that does lots of small I/O definitely does.

We measured before and after the patch on the same node types. Not a scientific study, but our own workloads:

WorkloadMetricDifference after KPTI
Go API servicesp99 latency+2 to 4%
Go batch jobs (mostly CPU)run timebarely measurable
PostgreSQL (pgbench, many small transactions)TPS-7%
Redis (many small requests)ops/sec-11%
Ingress controller (nginx)CPU usage+9%

What stands out: the things doing lots of network traffic and small reads and writes feel it most. Our own Go services got off lightly.

An important detail is PCID, a CPU feature that makes switching page tables cheaper. Newer kernels use it automatically when the CPU has it. On a VM it depends on whether the hypervisor passes the feature through:

grep -o -w pcid /proc/cpuinfo | head -1

If you get pcid back, the damage is usually limited. On an older generation of nodes we still had running it was missing, and there the impact on Redis was closer to 20%. Those nodes have since been replaced. That had been on the list for ages; Meltdown was just the excuse to finally do it.

What I take away from it#

Honestly, it’s made me a bit quiet. Twenty years of CPU optimisations turn out to have a fundamental leak, and every fix is a compromise. Meltdown is reasonably solved. Spectre is going to keep us busy for years, I suspect.

For our platform the practical conclusions were:

  • Rolling node updates need to be a routine chore, not a project. We’re going to automate it instead of relying on a bash script and a colleague watching over my shoulder.
  • Every service that matters gets a PodDisruptionBudget and anti-affinity. No discussion.
  • We’re going to keep better track of which kernel runs on which node. We didn’t know off the top of our heads, and that’s honestly a bit embarrassing.

And the cat? Segfault was perfectly fine with all of it. She has no kernel memory to leak, only strong opinions about when dinner is.

    Tip: / opens search, ↑↓ to select, ↵ to open.