Jump to content
Search

The new Ubuntu Wiki is live. Legacy content may be unavailable and page links may have changed. Read the announcement on Discourse.

User talk:Shinobi1733

From Ubuntu Wiki

Ubuntu 26.04: LUKS-encrypted swap with TPM 2.0 unlock + hibernation (and why Secure Boot got in the way)

System: ThinkPad X13 Gen 2i (11th-gen Intel), Ubuntu 26.04.1, kernel 7.0.0-38-generic, GNOME on Wayland.

TL;DR

  • Encrypted swap with LUKS2, unlocked by TPM 2.0 at boot — no passphrase prompt
  • Hibernation works
  • Secure Boot is disabled because kernel lockdown blocks hibernation when SB is on
  • VirtualBox host modules signed with a MOK so they load cleanly (kept signed even with SB off)

Part 1 — Starting point

Problems observed:

  • Unencrypted swap partition
  • vboxdrv taints the kernel: module verification failed: signature and/or required key missing - tainting kernel
  • kvm_amd: CPU N isn't AMD or Hygon on an Intel CPU (wrong module loading)
  • TPM 2.0 present and unused
  • HSI report flags: Secure Boot off, no BIOS rollback protection, no TME, swap unencrypted

Goal: encrypted swap, TPM unlock, working hibernation, clean systemctl --failed.


Part 2 — Sign VirtualBox modules with a MOK

Without a valid signature, vboxdrv refuses to load under Secure Boot and taints the kernel even without it.

Check for an existing MOK

ls -la /var/lib/shim-signed/mok/

Ubuntu creates this at install (MOK.der, MOK.priv). Don't make a new one if it exists.

Enroll it in firmware

sudo mokutil --import /var/lib/shim-signed/mok/MOK.der

Set a one-time password. Reboot. On the blue MOK Manager screen: Enroll MOK → Continue → Yes → enter the one-time password → Reboot.

Verify:

mokutil --list-enrolled

Sign the modules

for m in vboxdrv vboxnetadp vboxnetflt; do
  sudo cp /lib/modules/$(uname -r)/misc/$m.ko /lib/modules/$(uname -r)/misc/$m.ko.unsigned
  sudo /usr/src/linux-headers-$(uname -r)/scripts/sign-file sha256 \
    /var/lib/shim-signed/mok/MOK.priv \
    /var/lib/shim-signed/mok/MOK.der \
    /lib/modules/$(uname -r)/misc/$m.ko
done

Verify:

modinfo /lib/modules/$(uname -r)/misc/vboxdrv.ko | grep -E 'signer|sig_hash'

Part 3 — Blacklist kvm_amd on Intel

Symptom in dmesg:

kvm_amd: CPU 0 isn't AMD or Hygon

Fix:

echo "install kvm_amd /bin/false" | sudo tee /etc/modprobe.d/blacklist-kvm-amd.conf
sudo update-initramfs -u

blacklist alone only prevents auto-load. install ... /bin/false blocks explicit requests too.


Part 4 — LUKS-encrypted swap unlocked by TPM 2.0

Install tools

sudo apt install cryptsetup cryptsetup-initramfs tpm2-tools

tpm2-tools is required, or the initrd may fail to include the TPM libraries (dracut[E]: Module 'systemd-cryptsetup' depends on module 'tpm2-tss').

Back up config

sudo cp /etc/fstab /etc/fstab.bak
sudo cp /etc/default/grub /etc/default/grub.bak

Identify swap

swapon --show
sudo blkid /dev/nvme0n1pX    # replace X with your swap partition number

Format as LUKS2

sudo swapoff -a
sudo cryptsetup luksFormat --type luks2 --cipher aes-xts-plain64 --key-size 512 --hash sha256 /dev/nvme0n1pX

Set a strong passphrase — this is your recovery key if the TPM ever fails.

Open, mkswap, activate:

sudo cryptsetup open /dev/nvme0n1pX cryptswap
sudo mkswap /dev/mapper/cryptswap
sudo swapon /dev/mapper/cryptswap
swapon --show

Note the inner swap UUID — this is what resume= needs:

sudo blkid /dev/mapper/cryptswap

/etc/crypttab

sudo tee /etc/crypttab << 'EOF'
cryptswap UUID=<LUKS-UUID> none luks,discard,x-initrd.attach,tpm2-device=auto
EOF
  • x-initrd.attach — open LUKS in the initramfs (required so hibernate resume can find swap)
  • tpm2-device=auto — use the TPM2 keyslot automatically

/etc/fstab

Replace the old swap line with:

/dev/mapper/cryptswap none swap sw 0 0

Kernel cmdline (/etc/default/grub)

Point resume= at the inner swap UUID, not the LUKS UUID:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=<INNER-SWAP-UUID> kvm.enable_virt_at_load=0"
sudo update-grub

initramfs-tools resume config

echo "RESUME=UUID=<INNER-SWAP-UUID>" | sudo tee /etc/initramfs-tools/conf.d/resume

Enroll TPM 2.0

sudo systemd-cryptenroll --tpm2-device=auto --tpm2-pcrs=0+7 /dev/nvme0n1pX

PCRs 0+7 cover firmware code and Secure Boot state. Both change when you toggle Secure Boot or update BIOS. See "Re-enroll" below.

Regenerate initrd

sudo update-initramfs -u -k $(uname -r)

Verify initrd contents

sudo lsinitramfs /boot/initrd.img-$(uname -r) | grep -iE 'systemd-cryptsetup|libcryptsetup|tpm2|libtss2'

You should see systemd-cryptsetup, systemd-tpm2-generator, libcryptsetup.so.*, and libtss2-*. If systemd-cryptsetup is missing, the initrd was generated before /etc/crypttab existed — regenerate.


Part 5 — Gotchas I hit

dracut is installed alongside initramfs-tools

Ubuntu 26 ships both. update-initramfs is what actually builds the image on a default install. Errors like these are noise:

dracut[E]: Module 'systemd-cryptsetup' depends on module 'tpm2-tss', which can't be installed
dracut[F]: Can't write to /boot/efi/<machine-id>/<kernel>: Directory does not exist

Do not run dracut -f --regenerate-all — on a non-UKI system it can fail mid-write and leave a broken initrd. Stick with update-initramfs -u.

lsinitramfs silently fails without sudo

/boot/initrd.img-* is mode 0600. lsinitramfs without sudo prints Permission denied to stderr — and if you 2>/dev/null, you get an empty result that looks like "no cryptsetup". Always use sudo.

Re-enroll TPM after Secure Boot toggles

PCR 7 changes. Symptom: boot prompts for the LUKS passphrase instead of unlocking silently.

sudo systemd-cryptenroll --wipe-slot=tpm2 /dev/nvme0n1pX
sudo systemd-cryptenroll --tpm2-device=auto --tpm2-pcrs=0+7 /dev/nvme0n1pX
sudo update-initramfs -u -k $(uname -r)

Same procedure after a BIOS update (PCR 0 changes).


Part 6 — Hibernation vs Secure Boot: choose one

With Secure Boot on, kernel lockdown activates (cat /sys/kernel/security/lockdown shows [integrity] or [confidentiality]). Lockdown unconditionally blocks hibernation, regardless of whether all swap is encrypted.

The kernel patch that would allow hibernation when all swap is encrypted (lockdown_hibernate) was proposed in 2023 and was never merged upstream. No standard Ubuntu kernel has it.

Result:

  • Secure Boot on → hibernate grayed out, systemctl hibernate refuses
  • Secure Boot off → hibernation works, TPM still unlocks swap

You keep LUKS + TPM + encrypted swap either way. Secure Boot only affects boot-time verification — for a laptop that needs hibernation, turning it off is a reasonable trade.


Part 7 — Final state

Secure Boot:        disabled (hibernation requirement)
Swap:               LUKS2 encrypted, TPM 2.0 unlocked, PCRs 0+7
Hibernation:        working (no passphrase prompt at boot)
vboxdrv:            signed with MOK, loads cleanly
kvm_amd:            blacklisted, kvm_intel loaded
systemctl --failed: 0 loaded units listed

Maintenance

  • After a kernel update: re-sign VirtualBox modules, or just run sudo /sbin/vboxconfig.
  • After a BIOS update: re-enroll the TPM (PCR 0 changed).
  • After toggling Secure Boot: re-enroll the TPM (PCR 7 changed).
  • Save your LUKS passphrase. Without it, if the TPM ever fails, the swap container is unrecoverable. (Swap isn't precious, but the habit matters if you ever LUKS your root too.)

Security-trade-off summary

You end up with:

  • Data-at-rest protection for swap (LUKS2)
  • No passphrase prompt (TPM 2.0, PCRs bound to firmware + SB state)
  • Working hibernation
  • Secure Boot off — so no boot-time kernel/module verification
  • Kernel taints with 4096 (out-of-tree vboxdrv); that's unavoidable for VirtualBox

note:

this is from deepseek whatever the free model they use i got guided by it through the whole process (am still new to linux)
and I'd appreciate if there is a way to have both secure boot and hibernation without tinkering with the kernel