..

Secure Boot in Guix

Building the chain of trust.

The problem

I use full-disk encryption with LUKS on Guix System. The usual and easy way to do this installs a GRUB hook to unlock the LUKS volume, and load the kernel and initrd off the Guix Store. I found three somewhat grating pain points with this workflow and ended up with a faster and more flexible system as a result.

The first issue was simply that unlocking was particularly slow. At first, I chalked this up to the unlock key derivation being unaccelerated. A small modification to the grub package enables the hardware acceleration flag on cryptomount:

(define grub-efi-accelerated
  (package
    (inherit grub-efi)
    (name "grub-efi-accelerated")
    (arguments
     (substitute-keyword-arguments
         (package-arguments grub-efi)
       ((#:phases phases)
        #~(modify-phases #$phases
            (add-after 'unpack 'accelerate-cryptomount
              (lambda _
                (substitute* "util/grub-install.c"
                  (("cryptomount -u %s")
                   "cryptomount -A -u %s"))))))))))

(define accelerated-grub-efi-bootloader
  (bootloader
   (inherit grub-efi-bootloader)
   (package grub-efi-accelerated)))

This certainly sped things up between typing my passphrase and “slot 0 opened”. However, it was still quite a while to actually begin booting the kernel.

As an aside, the second pain point was simply that GRUB didn’t respect my keyboard layout. I use dvorak, but despite giving (keyboard-layout keyboard-layout) to bootloader, it would still prompt for my passphrase in qwerty. I could’ve tried to fix this, but it never really seemed worth the effort given that I can fumble my way through qwerty most of the time.

The next big issue is a consequence of GRUB’s filesystem handling. As it turns out, it ignores the filesystem index tree. For small directories this isn’t really an issue. But when you can easily have tens of thousands of items in the Guix Store, things get pretty slow. Rather than writing a patch to do directory indexing, I looked into alternate bootloader arrangements.

UKI

The unified kernel image (UKI) is a directly-bootable executable that loads an embedded kernel and initrd, then transfers control to Linux. This basically solves all problems at once: there’s no filesystem traversal to worry about, because we’re either loading the image from the EFI (which ideally has few files), or in the Linux environment with proper filesystem drivers. Similarly, keyboard and LUKS happen in the initrd which is configured correctly and takes advantage of hardware acceleration.

Actually building the UKI is fairly straightforward, using the bootloader definitions from the sakura channel. This provides two packages: systemd-stub is the EFI boot stub that gets executed by the firmware and is responsible for loading the kernel and initrd. This is essentially the “bootloader”. The ukify package takes the stub, kernel, initrd, kernel command line and OS information and creates the actual PE/COFF EFI executable with the correct sections in place.

A small bootloader module in Guix can build an unsigned UKI and save it as the bootcfg GC root:

(define* (ukify-system config entries
                       #:key (old-entries '())
                       #:allow-other-keys)
  (let* ((entry (car entries))
         (kernel (menu-entry-linux entry))
         (initrd (menu-entry-initrd entry))
         (arguments (menu-entry-linux-arguments entry))
         (os-release (plain-file "os-release" "\
NAME=\"Guix System\"
ID=guix
PRETTY_NAME=\"Guix System\"
")))
    (computed-file "uki.unsigned"
      (with-imported-modules
       (source-module-closure '((guix build utils)))
       #~(begin
           (use-modules (guix build utils))
           (let* ((ukify #$(file-append ukify "/bin/ukify"))
                  (stub #$(file-append systemd-stub "/libexec/linuxx64.efi.stub")))
             (invoke ukify "build"
                     "--stub" stub
                     "--linux" #$kernel
                     "--initrd" #$initrd
                     "--cmdline" (string-join (list #$@arguments))
                     "--os-release" (string-append "@" #$os-release)
                     "--output" #$output)))))))

(define uki-bootloader
  (bootloader
   (name 'uki)
   (package #f)
   (installer #f)
   (configuration-file "/boot/uki.unsigned")
   (configuration-file-generator ukify-system)))

Secure Boot

The output filename uki.unsigned might have foreshadowed this.m In the move from GRUB to UKI, we have made something of a tradeoff. With GRUB, we unlock the disk, and load the kernel from the encrypted Guix store. In that scenario, we can be sure the kernel isn’t tampered with. I suppose it would be possible to swap out the bootloader to capture the LUKS passphrase so it’s not foolproof.

On the other hand, with UKI we now have a full kernel + initrd payload sitting in the unencrypted ESP. This is a much easier path in to an otherwise encrypted at-rest system. To mitigate this, Secure Boot comes into play. We generate a trusted certificate corresponding to a signing key. A signature is embedded in the EFI executable that is verified against the trust database installed in the firmware. With Secure Boot enabled, only a validated executable is permitted to run.

Using the YubiKey PIV application, we generate a signing key in slot 9c(digital signature) along with its certificate which we convert from PEM to X.509:

$ ykman piv keys generate \
  --algorithm RSA2048 \
  --pin-policy ALWAYS --touch-policy ALWAYS \
  9c guix-public.pem
$ ykman piv certificates generate \
  --subject "CN=Guix UKI Secure Boot" \
  --valid-days 3650 \
  9c guix-public.pem
$ ykman piv certificates export \
  9c guix-db.pem
$ openssl x509 \
  -in guix-db.pem \
  -outform DER -out guix-db.cer

This certificate can be enrolled in the firmware Secure Boot db. Then, we simply have to sign the UKI using the PKCS#11 engine from the YubiKey, ensuring that PKCS11_MODULE_PATH contains libykcs11.so from yubico-piv-tool.

$ sbsign --engine pkcs11 \
  --key 'pkcs11:id=%02;type=private' \
  --cert guix-db.pem \
  --output uki.signed \
  /boot/uki.unsigned

Housekeeping

The downside here is that we don’t get the automatic integration of all of Guix’s system generations with the bootloader like we would with GRUB. For day-to-day use, there is /boot/efi/efi/Linux/guix-a.efi and /boot/efi/efi/Linux/guix-b.efi. In the usual state, these contain the current and previous system images. The system store path corresponding to a given kernel is given as a kernel parameter, so these standalone images will boot into their entire associated system. There are two boot entries for these:

$ sudo efibootmgr -C -d /dev/nvme0n1 -p 1 -L "Guix UKI A" -l '\efi\Linux\guix-a.efi'
$ sudo efibootmgr -C -d /dev/nvme0n1 -p 1 -L "Guix UKI B" -l '\efi\Linux\guix-b.efi'

If these were boot entries 0003 and 0005 respectively, the boot order could be 0003,0005,.... Upon running guix system reconfigure, a new unsigned image is generated. This is signed with sbsign and installed over the previous system image, becoming the new candidate system. Suppose UKI B is the candidate. Then we set it as the next boot entry, without changing the saved boot order. Assuming it was the most recent configure, we also save it as a GC root, because our manual managament of booting systems doesn’t necessarily correspond to Guix’s view of system generations.

$ sudo ln -sfn "$(readlink -f /var/guix/profiles/system)" /var/guix/gcroots/uki-b
$ sudo efibootmgr -n 0005

Upon successful boot and verification of the new system, we can flip the boot order with efibootmgr -o 0005,0003,.... With that, we have a straightforward and fast boot mechanism that is mostly automated. It was a conscious decision not to automate sbsign, as something of a delineation of responsibilities between system configure and firmware management.