Articles / Security

  • key-management
  • tpm2
  • systemd
  • secrets
  • linux
  • threat-modelling

Where Should the Key That Unlocks Your Other Keys Live?

You encrypt your database passwords and API tokens with a master key. Good. Now the master key needs to be somewhere, and if you encrypt that too, you have a new key with the same problem. The regress only stops at something you do not encrypt: a secret in a chip, in your head, on another machine, or sitting in a file.

So instead of asking "where is it safe?", ask these two questions:

  • What does an attacker have to steal or control to use the key?
  • What has to happen for your service to come back after a 3am reboot, with you asleep?

Most bad designs answer one beautifully and never ask the other.

Your options, and what each one actually stops

  • A root-only file beside the data. Reboots happily. Stops other unprivileged users, but not anyone holding the disk or a copy of it.
  • A TPM-sealed file. Reboots happily. A copied disk is useless elsewhere, but a stolen whole machine may be another matter (see below).
  • A passphrase typed at start-up. Stops theft of a powered-off box, if the passphrase is strong. Nothing restarts until a human turns up.
  • A hardware token. The key stays on the device. If it must work unattended, it stays plugged in, and the burglar gets a matching set.
  • A cloud KMS or a second machine. You can revoke access, and a KMS can log each use. You now depend on the network and on whatever credential proves your server is your server.
  • Split between several people or places. No single theft is enough. Recovery becomes a meeting.

None of these stops an attacker with root on the running machine. If your service can use the key, so can they. Hardware and remote services can stop the key being copied out, which matters, but the intruder can still use it while they are there.

An environment variable is a way of delivering the key rather than storing it. The value still had to come from somewhere, usually the file in the first bullet.

A TPM does not automatically mean a thief is stuck

This is the bit that caught my eye. systemd can encrypt a service's secrets with systemd-creds, using a key from the TPM, a host key in /var/lib/systemd/credential.secret, or both. On a typical machine with a TPM, the default is both.

That sounds excellent. But the systemd 259 man page also says that, unless you ask, the TPM key is bound to no PCRs at all. PCRs are the TPM's running measurements of what booted.

With no PCR policy, the TPM is not checking which operating system is asking. Picture a stolen server with an unencrypted root disk. The thief boots a USB stick, reads the host key off the disk, and asks the same TPM to help decrypt. Both halves are present.

The exception is a setup with a signed PCR policy (systemd picks up tpm2-pcr-public-key.pem automatically if your boot tooling installed one). Defaults have also differed between versions, so read the man page on your own box rather than trusting mine.

The fixes are to encrypt the root filesystem with a sensibly bound unlock, to bind the credential to PCRs, or both. PCR 7 tracks Secure Boot state, so it is only worth much with Secure Boot on. It is not a complete defence against every boot-chain trick.

Quick detour: where does the TPM keep all these secrets?

Mostly, it doesn't. A TPM has very little storage, so it hands back a blob encrypted under a key that never leaves the chip, and you keep the blob on disk. Which is our regress again, ending in silicon.

A sensible setup for one unattended server

For a small self-hosted service that must survive reboots, I would use a TPM-and-host-bound credential, plus a recovery copy that lives somewhere else. Generate the key once:

umask 077
head -c 32 /dev/urandom > /dev/shm/kek

# Recovery copy, protected by a passphrase. Move it off this machine.
age -p -o kek.recovery.age /dev/shm/kek

sudo mkdir -p /etc/myapp
sudo systemd-creds encrypt --name=kek --with-key=host+tpm2 \
    --tpm2-pcrs=7 /dev/shm/kek /etc/myapp/kek.cred

rm /dev/shm/kek

/dev/shm is memory-backed but can be swapped out, so use encrypted swap or none. Then hand the credential to the service:

[Service]
DynamicUser=yes
LoadCredentialEncrypted=kek:/etc/myapp/kek.cred
ExecStart=/usr/local/bin/myapp

systemd decrypts it when the service starts and exposes it as a read-only file in $CREDENTIALS_DIRECTORY. Your program reads it and refuses to carry on if it is missing or the wrong size:

func loadKEK() ([]byte, error) {
	dir := os.Getenv("CREDENTIALS_DIRECTORY")
	if dir == "" {
		return nil, errors.New("CREDENTIALS_DIRECTORY is not set")
	}
	key, err := os.ReadFile(filepath.Join(dir, "kek"))
	if err != nil {
		return nil, fmt.Errorf("read kek: %w", err)
	}
	if len(key) != 32 {
		return nil, fmt.Errorf("kek is %d bytes, want 32", len(key))
	}
	return key, nil
}

Check that decryption works before you rely on it. This should print 32:

sudo systemd-creds decrypt --name=kek /etc/myapp/kek.cred - | wc -c

Then reboot and check again. Test the recovery copy too, with age -d, on a different machine.

PCR binding will eventually lock you out

Binding to measurements means a changed measurement refuses to decrypt. That is the feature. A firmware update, a Secure Boot key change or a revocation list update can alter PCR 7, and a dead motherboard takes its TPM with it.

When that happens, decrypt the recovery copy and run the systemd-creds encrypt step again. Without that copy, your data is as gone as if the attacker had deleted it. Keep the file and its passphrase somewhere that does not depend on this server, including its password manager if you self-host one.

If "stolen from the house" is the worry, use the network

There is a neat middle ground between typing a passphrase and leaving the key in the box: release it only when the machine is at home. Tang is a small server, and Clevis is the client that binds a secret to it.

The clever part is that the Tang server never learns the key it helps recover. It does one half of a key exchange (McCallum-Relyea), so there is no database of client secrets to steal. I find that rather lovely.

A thief who takes only the server gets nothing once it leaves your network. A thief who also takes the little Tang box from the same shelf has everything. Put it somewhere else in the building, and remember that after a power cut it has to come up before anything that needs it.

Choose by naming the thief

  • Someone gets a copy of your backups: keep the root key out of them. A TPM-bound credential does this, with the recovery copy stored separately.
  • Someone walks off with the machine: use a boot passphrase, network-bound unlock, or a PCR policy you have thought about.
  • Someone compromises the running service: run it with the least privilege you can, and consider a remote service that limits and logs each operation.
  • You are hit by a bus, or merely forget: have a recovery path another person can follow.

Write the answer down next to the deployment notes, including what it does not cover. "A burglar who takes both boxes wins" is a perfectly respectable line in a home threat model. Finding it out from the burglar is less good.