systemd-stub, sd-stub, linuxx64.efi.stub, linuxia32.efi.stub, linuxaa64.efi.stub - A simple UEFI kernel boot stub
systemd-stub (stored in per-architecture files linuxx64.efi.stub, linuxia32.efi.stub, linuxaa64.efi.stub on disk) is a simple UEFI boot stub. An UEFI boot stub is attached to a Linux kernel binary image, and is a piece of code that runs in the UEFI firmware environment before transitioning into the Linux kernel environment. The UEFI boot stub ensures a Linux kernel is executable as regular UEFI binary, and is able to do various preparations before switching the system into the Linux world.
The UEFI boot stub looks for various resources for the kernel invocation inside the UEFI PE binary itself. This allows combining various resources inside a single PE binary image, which may then be signed via UEFI SecureBoot as a whole, covering all individual resources at once. Specifically it may include:
If UEFI SecureBoot is enabled and the ".cmdline" section is present in the executed image, any attempts to override the kernel command line by passing one as invocation parameters to the EFI binary are ignored. Thus, in order to allow overriding the kernel command line, either disable UEFI SecureBoot, or don't include a kernel command line PE section in the kernel image file. If a command line is accepted via EFI invocation parameters to the EFI binary it is measured into TPM PCR 12 (if a TPM is present).
If a DeviceTree is embedded in the ".dtb" section, it replaces an existing DeviceTree in the corresponding EFI configuration table. systemd-stub will ask the firmware via the "EFI_DT_FIXUP_PROTOCOL" for hardware specific fixups to the DeviceTree.
The systemd-stub UEFI boot stub automatically collects two types of auxiliary companion files optionally placed in drop-in directories on the same partition as the EFI binary, dynamically generates cpio initrd archives from them, and passes them to the kernel. Specifically:
These mechanisms may be used to parameterize and extend trusted (i.e. signed), immutable initrd images in a reasonably safe way: all data they contain is measured into TPM PCRs. On access they should be further validated: in case of the credentials case by encrypting/authenticating them via TPM, as exposed by systemd-creds encrypt -T (see systemd-creds(1) for details); in case of the system extension images by using signed Verity images.
TPM2 PCR NOTES¶
Note that when a unified kernel using systemd-stub is invoked the firmware will measure it as a whole to TPM PCR 4, covering all embedded resources, such as the stub code itself, the core kernel, the embedded initrd and kernel command line (see above for a full list).
Also note that the Linux kernel will measure all initrds it receives into TPM PCR 9. This means every type of initrd will be measured twice: the initrd embedded in the kernel image will be measured to both PCR 4 and PCR 9; the initrd synthesized from credentials will be measured to both PCR 12 and PCR 9; the initrd synthesized from system extensions will be measured to both PCR 4 and PCR 9. Let's summarize the OS resources and the PCRs they are measured to:
Table 1. OS Resource PCR Summary
|OS Resource||Measurement PCR|
|systemd-stub code (the entry point of the unified PE binary)||4|
|Boot splash (embedded in the unified PE binary)||4|
|Core kernel code (embedded in unified PE binary)||4|
|Main initrd (embedded in unified PE binary)||4 + 9|
|Default kernel command line (embedded in unified PE binary)||4|
|Overridden kernel command line||12|
|Credentials (synthesized initrd from companion files)||12 + 9|
|System Extensions (synthesized initrd from companion files)||4 + 9|
The following EFI variables are defined, set and read by
systemd-stub, under the vendor UUID "4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", for communication between the boot stub and the OS:
Note that some of the variables above may also be set by the boot loader. The stub will only set them if they aren't set already. Some of these variables are defined by the Boot Loader Interface.
ASSEMBLING KERNEL IMAGES¶
In order to assemble an UEFI PE kernel image from various components as described above, use an objcopy(1) command line like this:
--add-section .osrel=os-release --change-section-vma .osrel=0x20000 \
--add-section .cmdline=cmdline.txt --change-section-vma .cmdline=0x30000 \
--add-section .dtb=devicetree.dtb --change-section-vma .dtb=0x40000 \
--add-section .splash=splash.bmp --change-section-vma .splash=0x100000 \
--add-section .linux=vmlinux --change-section-vma .linux=0x2000000 \
--add-section .initrd=initrd.cpio --change-section-vma .initrd=0x3000000 \
This generates one PE executable file foo-unsigned.efi from the six individual files for OS release information, kernel command line, boot splash image, kernel image, main initrd and UEFI boot stub.
To then sign the resulting image for UEFI SecureBoot use an sbsign(1) command like the following:
--key mykey.pem \
--cert mykey.crt \
--output foo.efi \
This expects a pair of X.509 private key and certificate as parameters and then signs the UEFI PE executable we generated above for UEFI SecureBoot and generates a signed UEFI PE executable as result.
- Boot Loader Interface
- Boot Loader Specification