Linux Secure boot with encryption and TPMCategory: Linux
I used to think that secure boot and tpm are just gimmicky features, that don't really help with security. But if you set up everything correctly, you'll get a somewhat steal proof computer (depenging on the UEFI and linux passwords strength) without having to deal with any inconveniences.
TPM Data-at-rest Encryption
Your computer will boot and decrypt the data on your root volume automatically without asking the decryption password if certain conditions are met. For example:
- Firmware has not been modified (PCR0)
- Secure Boot State (PCR7)
I'm not certain about other PCRs. But I use PCRs 0,3,5,7. Read more about PCRs at Archwiki.
First of all, you need to encrypt your root partition with LUKS2.
To do that, you need to boot into archiso, copy all the data from root partition to another drive, encrypt and mount it to restore all data.
Use a strong password for encryption, as it will be used to restore access to your data in case something goes wrong with your TPM.
Mount your boot partition to
/mnt/your_root_drive_mount/boot and chroot into it.
Change your mkinitcpi.conf, so your HOOKS look something like this:
And add tpm modules:
Add the following line to
You may want to remove the
discard option for additional security.
Generate new iniramfs:
Change your root device in kernel command line:
If you use sbupdate, change it in
UPDATE 2023: Sbupdate is no longer maintained, use
/etc/mkinitcpio.d/linux.preset as described here and put your command line arguments into files in
/etc/cmdline.d/ to generate the UKI. Use this initcpio hook to sign it automatically.
And don't forget to run
sbupdate to add newly generated initramfs. If you are using mkinitcpio to generate the UKI, use
mkinitcpio -P instead.
If you haven't enabled Secure boot yet, you can add TPM key now for convenience, with only 0 pcr:
Else, just use tpm2-pcrs=0,7
If you want to use PCRs 3 and 5, you'll need to reenroll key when your system booted up.
Note, that PCRs 3 and 5 will change between your system and archiso, so your root partition won't unlock if enrolled from different boot media.
You can remove the password from your luks encrypted root partition and only leave tpm unlock, but that setup is prone to failure. Updating the bios would stop your system from booting. I strongly recommend leaving the password.
Enabling Secure boot
First of all, you need to generate your own keys to use with secure boot. To do that, I use a script recommended by arch wiki.
Now you need to generate and sign a unified kernel image. That can be done manually with
objcopy, but I prefer to use mkinitcpio method. It also runs on kernel updates.
There's no need to include
initrd= parameter in your cmdline. Microcode loading is also handled automatically.
mkinitcpio -P and add generated efi file to boot entries:
You might also need to activate the boot entry with
That might not be necessary if you place the UKI at
/boot/EFI/BOOT/bootx64.efi as most bios'es default to this location.
After that, enroll your keys to secure boot. Some UEFIs provide a menu to add keys from a USB drive. In that case, copy your keys to USB with FAT32 filesystem and add DB, KEK and PK. Use either .esl or .auth.
DON'T REMOVE THE FACTORY KEYS!!! (GPU Firmware is signed by Microsoft's UEFI key. You won't have display output until OS boots up if you remove it. CMOS reset won't help. The only solution when the system doesn't boot is BIOS reflash) You can remove or add the factory keys afterwards. I recommend doing that for additional security.
If you don't have the menu to add keys in UEFI, you need to set secure boot to setup mode and boot to your system. Then the keys can be added with
Put your keys in these folders and
The PK key should be added last. That will put your secure boot into user mode. If the last command failed to enroll PK key, use
If that fails, removing the immutable flag from
If you want to leave the Microsoft UEFI key, install dbxtool and update the DBX database with it.
After enrolling your keys, boot into UEFI and enable secure boot.
Archiso with Secure boot
Extract archiso contents to a USB drive with FAT32 filesystem.
Don't forget to change the filesystem label to ARCH_YYYYMM (e.g. ARCH_202108) of your iso.
Sign BOOTx64.EFI and vmlinuz-linux with your keys:
Now you can boot with this USB drive without changing secure boot state.
For additional security it's recommended to disable USB boot, lock the boot order, disable boot select menu and enable intrusion detection (if present).