archive / 2014

Heartbleed: a night of replacing certificates

A small post-mortem of how we got through the Heartbleed night at the startup: patching, restarting, new keys and certificates, and what we'll do differently next time.

Monday evening I was sitting in front of the TV with a plate of pasta when my phone started buzzing. Sarah, our CTO, had sent a link to heartbleed.com with one line of text: “This is bad, right?”

It was bad. It’s Thursday now, I’ve slept about twelve hours spread over three nights, and I want to write down what we did while it’s still fresh. Not because we did it so well, but because I want to have something to go back to next time.

What is Heartbleed again?#

Short version: a bug in the heartbeat extension of OpenSSL (CVE-2014-0160). A client can send a heartbeat that says “here are 64 KB of data, please send them back”, while only a few bytes are actually included. OpenSSL didn’t check that length and happily sent back 64 KB of whatever was in the server process’s memory. Repeat that a few thousand times and you can fish out session cookies, passwords and, in the worst case, the private key of your certificate.

Affected: OpenSSL 1.0.1 up to and including 1.0.1f. Fixed in 1.0.1g. And the bug has been there since the end of 2011, so “has it been exploited?” is a question nobody can honestly answer.

Our situation#

The startup runs on a handful of Ubuntu 12.04 machines. TLS is terminated by nginx, behind which our Node processes listen on localhost. Ubuntu 12.04 ships OpenSSL 1.0.1, so yes: vulnerable.

First, find out what we were actually running:

$ openssl version -a
OpenSSL 1.0.1 14 Mar 2012
built on: Wed Jan  8 20:45:51 UTC 2014
platform: debian-amd64

Careful: on Ubuntu the version number doesn’t tell you much, because they backport patches. What matters is the “built on” date and the package version:

$ dpkg -l | grep libssl1.0.0
ii  libssl1.0.0  1.0.1-4ubuntu5.11  amd64  SSL shared libraries

The patched version is 1.0.1-4ubuntu5.12. Luckily it showed up fast.

Step 1: patch and (really) restart#

sudo apt-get update
sudo apt-get install --only-upgrade openssl libssl1.0.0

That’s the easy part. The nasty bit is that every process that already loaded the old library keeps using it from memory. An upgrade without a restart does nothing. This shows which processes still hold on to the deleted version:

sudo lsof -n | grep libssl | grep DEL

For us that was nginx (obviously), but also a forgotten stunnel process someone once set up for a tunnel to an external partner. Nobody remembered it existed. That’s lesson number one right there.

Node bundles its own OpenSSL instead of using the system one. Since all our Node processes sat behind nginx and didn’t speak TLS to the outside world themselves, they weren’t directly exposed. I checked them anyway with process.versions.openssl, and we’ll upgrade as soon as there’s a new release.

Step 2: new keys, new certificates#

Patching alone isn’t enough. If someone grabbed the private key, they can keep decrypting your traffic or impersonating you, patched or not. So: new key, new CSR, have the certificate reissued.

openssl req -new -newkey rsa:2048 -nodes \
  -keyout wildcard.2014-04.key \
  -out wildcard.2014-04.csr \
  -subj "/C=BE/L=Ghent/O=The Startup/CN=*.example.com"

Our CA let us reissue for free, but their website was so slow that Tuesday that it took me over an hour to get through the form. Half the world was doing the same thing. Then revoke the old certificate, put the new key in place with chmod 600, update the nginx config, nginx -t, reload.

For testing I used one of the online checkers that popped up like mushrooms that day. Green. Only then was I allowed a piece of chocolate.

Step 3: everything that could have been in memory#

This is where we had the most discussion. What else could have been in those 64 KB chunks?

  • Sessions: we rotated the session secret, which logged everyone out. A few customers called, annoyed. It is what it is.
  • Passwords: those travel in plaintext up to nginx. We sent every user an email asking them to change their password, explaining why. No panic tone, just the facts.
  • Third-party API tokens: everything that passed through nginx in config or headers, we regenerated. That turned out to be a list of eleven services. Eleven.

What I’m taking away from this#

  1. An inventory. We didn’t know exactly which machines ran which version of what. That forgotten stunnel process could have bitten us badly. I’m going to keep a simple list, and yes, I’m also looking at tools like Ansible to keep that list honest automatically.
  2. Certificates are not a one-off chore. Replacing them should be a documented procedure, not a hunt through your inbox for “that one ZIP file from the CA in 2012”.
  3. Make secrets rotatable. Every secret you can’t replace without downtime is a problem waiting for a bad week.
  4. Staying calm works. At ten in the evening Sarah had put a list in a shared document with who was doing what. No heroics, just a checklist. That helped more than all the coffee.

I keep thinking about the people who maintain OpenSSL. A library pretty much the entire internet runs on, and the outside world’s main reaction is “how could this happen?”. Maybe we should first ask ourselves how many of us ever contributed anything to it. I certainly haven’t.

Now I’m going to sleep. For real.

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