swtpm and Secure Boot firmware in the snap

a component, an app for users for CI operators

What the snap supplies for the four Ubuntu Core +tpm systems. The qemu-x86-64 component carries the Secure Boot OVMF pair, the swtpm component carries swtpm and the libraries the base lacks, make stops before the download when that component is missing, and the image-garden.swtpm app serves launchers run from a host shell.

On the map: snap

▶ Watch this deck — 6 scenes, ~4 min

Worth seeing first

01 Two pieces for a TPM system

TWO PIECES ubuntu-core-24+tpm.x86_64 outside the snap: swtpm package ovmf package in the snap: qemu-x86-64 + Secure Boot pair already there on a PC swtpm component not installed for you $ snap install image-garden+swtpm
ubuntu-core-24+tpm.x86_64 needs swtpm and the Secure Boot firmware; outside the snap these are the swtpm and ovmf packages; in the snap the firmware comes with the qemu-x86-64 component a PC already has, and swtpm is a component you add with snap install image-garden+swtpm

The Secure Boot firmware comes with qemu-x86-64. swtpm is its own component: snap install image-garden+swtpm.

Say you want ubuntu-core-24+tpm from the snap. Outside the snap, your host would need two packages for it: swtpm, the TPM emulator, and ovmf, for the Secure Boot firmware. The snap brings both, but in different places. The firmware rides along in qemu-x86-64, the QEMU component a PC already has. swtpm is a component of its own, which the snap doesn't install for you, because no other system needs it. So one command gets you ready: snap install image-garden+swtpm. This deck shows what each piece holds, and how the snap uses them.

Details and sources

Details

  • qemu-x86-64 "contains the 64-bit x86 system emulator (QEMU) as well as the corresponding EFI firmware (EDK2) and the Secure Boot EFI firmware of Ubuntu (OVMF)" (snapcraft.yaml:59-66); the install hook installs it on x86_64 hosts (install:6-14).
  • swtpm: "the TPM emulator behind the virtual TPM of the Ubuntu Core +tpm systems, which also need the qemu-x86-64 component. Other systems do not need it" (snapcraft.yaml:109-119). "It is not installed automatically: snap install image-garden+swtpm" (README.md:322-334).
  • The snap's news for this release: the Secure Boot firmware in qemu-x86-64, the image-garden.swtpm app, and swtpm as a separate component (NEWS.snappy:26-30).
  • Outside the snap the host needs the swtpm and ovmf packages; see what your host needs.

02 The Secure Boot pair, in the QEMU component

THE SECURE BOOT PAIR ovmf-generic OVMF_CODE_4M.secboot.fd OVMF_VARS_4M.ms.fd Secure Boot code Microsoft's keys component qemu-x86-64: share/ovmf/ 1. /usr/share/OVMF/ the host's ovmf package 2. …/qemu-x86-64/share/ovmf/ first pair that exists wins efi-secboot-code.x86_64.img efi-secboot-vars.x86_64.img every +tpm system starts from these
The snap build keeps OVMF_CODE_4M.secboot.fd and OVMF_VARS_4M.ms.fd from Ubuntu's ovmf-generic package in qemu-x86-64/share/ovmf; make looks for the host's pair first and the component's second; the pair found is copied to efi-secboot-code.x86_64.img and efi-secboot-vars.x86_64.img, from which every +tpm system starts

OVMF_CODE_4M.secboot.fd and OVMF_VARS_4M.ms.fd in qemu-x86-64/share/ovmf, after the host's pair; copied to efi-secboot-code.x86_64.img and efi-secboot-vars.x86_64.img.

Start with the firmware. When the snap is built, it takes Ubuntu's ovmf-generic package and keeps two files: the Secure Boot build of the code, and the variable store with Microsoft's keys enrolled, which belongs with it. Both go into the qemu-x86-64 component, under share/ovmf, next to the QEMU that loads them. make lists that pair after the host's own, so when your host has the ovmf package, its pair wins. Either way, the two are copied once into the directory make runs in, and every +tpm system starts from those copies.

Details and sources

Details

  • The ovmf-x86-64 part stages Ubuntu's ovmf-generic package, keeps OVMF_CODE_4M.secboot.fd and OVMF_VARS_4M.ms.fd ("OVMF_VARS_4M.ms.fd is for the Secure Boot code") and its copyright file, and organizes them into (component/qemu-x86-64)/share/ovmf (snapcraft.yaml:645-665).
  • make's Secure Boot lists: /usr/share/OVMF/OVMF_CODE_4M.secboot.fd with /usr/share/OVMF/OVMF_VARS_4M.ms.fd first, the component's pair second; the Nth entries go together and the first pair where both exist wins (010-qemu-x86_64.mk:282-296).
  • The pair is copied to efi-secboot-code.x86_64.img and efi-secboot-vars.x86_64.img, after check-qemu-x86-64-component (010-qemu-x86_64.mk:298-308); each +tpm system converts them to its own .efi-code.qcow2 and .efi-vars.qcow2 (050-ubuntu-core.mk:57-61).
  • Without a pair: please install x86_64 edk2 firmware with Secure Boot and Microsoft keys (the ovmf package) - not in … with … (010-qemu-x86_64.mk:300-308).

03 What the swtpm component holds

THE SWTPM COMPONENT Ubuntu's swtpm package swtpm component bin/swtpm lib/swtpm/ libtpms, libswtpm_libtpms, json-glib version: the package's, from its changelog swtpm stub LD_LIBRARY_PATH=…/lib/swtpm exec …/bin/swtpm "$@" no swtpm_setup: an empty state directory is enough
The swtpm component is made from Ubuntu's swtpm package: bin/swtpm and lib/swtpm with libtpms, libswtpm_libtpms and json-glib, versioned from the package's changelog; the swtpm stub sets LD_LIBRARY_PATH and runs it; no swtpm_setup is needed

bin/swtpm and lib/swtpm/ (libtpms, libswtpm_libtpms, json-glib), versioned from Ubuntu's package. The stub sets LD_LIBRARY_PATH.

The swtpm component is made the same way, from Ubuntu's swtpm package. It keeps the swtpm program, and the libraries it needs that the snap's base lacks, such as libtpms. Its version is the package's version, read from its changelog, so it moves with Ubuntu's updates rather than with QEMU. Inside the snap, swtpm is a stub: it puts those libraries on the library path and runs the program. And there's no swtpm_setup. An empty state directory is enough: swtpm creates the TPM state there by itself, on the first boot.

Details and sources

Details

  • The swtpm part stages Ubuntu's swtpm package, unpinned, and keeps bin/swtpm, the libraries libswtpm_libtpms.so.0*, libtpms.so.0* and libjson-glib-1.0.so.0* in lib/swtpm, and the copyright files; everything else is deleted (snapcraft.yaml:608-644).
  • The component's version is read from the first line of the package's changelog.Debian.gz, without the epoch; the build fails if it cannot be found (snapcraft.yaml:621-632).
  • "swtpm_setup is not needed: swtpm creates the TPM state on its own when the directory is empty" (snapcraft.yaml:614-616).
  • The stub prepends lib/swtpm to LD_LIBRARY_PATH and execs bin/swtpm (swtpm:34-39).

04 Stopped before the download

BEFORE THE DOWNLOAD $ image-garden make ubuntu-core-24+tpm.x86_64.run check-swtpm-component the download link ubuntu-core-24-amd64 install the swtpm component to use systems with a TPM: snap install image-garden+swtpm make, before any download ┌──────────── Notice ────────────┐ snap install image-garden+swtpm the stub, if removed later install qemu-x86-64 component to use x86_64 emulation without qemu-x86-64
image-garden make ubuntu-core-24+tpm.x86_64.run: the link to the plain Ubuntu Core download depends on check-swtpm-component, which stops make before the download with install the swtpm component to use systems with a TPM: snap install image-garden+swtpm; later the swtpm stub prints a notice with the same command; without qemu-x86-64, make asks for that component first

install the swtpm component to use systems with a TPM: snap install image-garden+swtpm, before any download. A missing qemu-x86-64 is reported before the firmware is copied.

What if you forget the component? make checks before it does anything costly. A +tpm system's download is a link to the plain Ubuntu Core image, and making that link depends on a check that the swtpm component is there. So make stops before it downloads anything, and says install the swtpm component to use systems with a TPM, with the snap install command. If you remove the component later, a launcher stops at the stub instead, showing a message with the same command. And on a machine without qemu-x86-64, make asks for that component first.

Details and sources

Details

  • check-swtpm-component runs only in the snap and fails with install the swtpm component to use systems with a TPM: snap install image-garden+swtpm (010-qemu-x86_64.mk:121-131).
  • Each +tpm download is a link to the plain image, ubuntu-core-24+tpm.x86_64.qcow2 to ubuntu-core-24-amd64.qcow2 for example, with check-swtpm-component as its first order-only prerequisite, so make stops before the download; the .qcow2 rule has it too (050-ubuntu-core.mk:74-79, 050-ubuntu-core.mk:279-289).
  • The swtpm stub shows a message headed Notice and snap install image-garden+swtpm and exits 1 when the component is missing (swtpm:11-32).
  • The Secure Boot copies depend on check-qemu-x86-64-component: install qemu-x86-64 component to use x86_64 emulation (010-qemu-x86_64.mk:298, 010-qemu-x86_64.mk:310-316, 010-qemu.mk:50-55).

05 A launcher run from your shell

FROM YOUR SHELL inside the snap, as under allocate swtpm qemu-system-x86_64 the stubs from an ordinary shell image-garden.swtpm image-garden.qemu-system-x86-64 still confined $XDG_RUNTIME_DIR/snap.image-garden/image-garden/tpm.XXXXXXXX/swtpm.sock not $XDG_RUNTIME_DIR/image-garden/, which the confined apps cannot reach image-garden.swtpm home network-bind TPM state its socket
Inside the snap, as under allocate, the .run script calls the swtpm and qemu-system-x86_64 stubs; from an ordinary shell it calls image-garden.swtpm and image-garden.qemu-system-x86-64, still confined, so swtpm's socket goes under $XDG_RUNTIME_DIR/snap.image-garden/image-garden; the swtpm app plugs home and network-bind

Outside the snap the launcher runs image-garden.swtpm and image-garden.qemu-system-x86-64, with swtpm's socket in $XDG_RUNTIME_DIR/snap.image-garden/image-garden/.

Now the launcher. Inside the snap, as under allocate, the .run script calls swtpm and QEMU by their plain names, which are the stubs. Run it from an ordinary shell, and it calls the snap's apps instead: image-garden.swtpm and image-garden.qemu-system-x86-64. Both are still confined, and they reach only the snap's own part of your runtime directory. So the script puts swtpm's socket there, under snap.image-garden, rather than in image-garden's usual place. The swtpm app plugs just home, for the TPM state next to your disk, and network-bind, for its socket.

Details and sources

Details

  • SWTPM in a launcher made by the snap is a shell expression: image-garden.swtpm when SNAP_INSTANCE_NAME is unset at run time, swtpm inside the snap (010-qemu-x86_64.mk:115-119); QEMU is chosen the same way, image-garden.qemu-system-x86-64 or qemu-system-x86_64 (010-qemu-x86_64.mk:4).
  • In that case the launcher puts swtpm's run directory under ${XDG_RUNTIME_DIR:-/run/user/UID}/snap.image-garden/image-garden: "Outside the snap its confined swtpm and QEMU reach only the runtime directory of the snap" (010-qemu-x86_64.mk:173-189). Under allocate, GARDEN_TPM_RUN_DIR names the directory instead.
  • The swtpm app: command: usr/bin/swtpm, plugs home and network-bind ("Binding swtpm's UNIX sockets needs it") (snapcraft.yaml:132-139).
  • The launcher must still be run from its own directory, which must be under your home directory for the confined apps to reach it; see three things that belong together.

06 Where to go from here

ROADS swtpm in the snap Secure Boot and a TPM the four +tpm systems A TPM launch swtpm, step by step Firmware the Secure Boot pair search Snap components the other optional parts Confinement a host shell launch snapd map: encryption what the keys protect
Roads from swtpm in the snap: the +tpm systems, each launch of swtpm, the firmware search, the components, confinement and the snapd map's encryption deck

Next: the +tpm systems, each launch of swtpm, the firmware search, the components, confinement, and the snapd map's encryption deck.

So the snap brings both pieces: the Secure Boot pair in its x86_64 QEMU component, and swtpm in a component you add yourself. From here, the Secure Boot and TPM deck shows the four systems and what their first boot seals. The runtime deck walks through each launch of swtpm. The firmware deck shows how make picks the Secure Boot pair. The components deck has the rest of the snap's optional parts, and the confinement deck the other things a host shell launch changes. The snapd map explains what the sealed keys protect.

Details and sources

Read more

Basis of the statements on this page

Read in the image-garden source at release v0.8.0 ("code"): snap/snapcraft.yaml, qemu-stubs/swtpm, mk/010-qemu-x86_64.mk and mk/050-ubuntu-core.mk. Nothing was run for this page; swtpm was not installed.

What the snap supplies for a +tpm system

PieceWhere in the snapFromInstalled
OVMF_CODE_4M.secboot.fd, OVMF_VARS_4M.ms.fdqemu-x86-64 component, share/ovmf/Ubuntu's ovmf-genericwith qemu-x86-64, by the install hook on x86_64 hosts
swtpm and lib/swtpm/swtpm componentUbuntu's swtpmby you: snap install image-garden+swtpm
usr/bin/swtpm stubthe snapqemu-stubs/swtpmalways
image-garden.swtpm appthe snapruns the stubalways; plugs home, network-bind

Sources: snapcraft.yaml:109-139, snapcraft.yaml:608-665, swtpm:6-39.

Names a launcher uses

Launcher runswtpmQEMUswtpm's run directory
inside the snap (image-garden allocate, image-garden make)swtpm (the stub)qemu-system-x86_64 (the stub)GARDEN_TPM_RUN_DIR, or $XDG_RUNTIME_DIR/image-garden/tpm.XXXXXXXX
from a host shellimage-garden.swtpmimage-garden.qemu-system-x86-64${XDG_RUNTIME_DIR:-/run/user/UID}/snap.image-garden/image-garden/tpm.XXXXXXXX

Sources: 010-qemu-x86_64.mk:4, 010-qemu-x86_64.mk:115-119, 010-qemu-x86_64.mk:173-189.

Messages

WhenMessage
make of a +tpm system without the swtpm componentinstall the swtpm component to use systems with a TPM: snap install image-garden+swtpm
make without the qemu-x86-64 componentinstall qemu-x86-64 component to use x86_64 emulation
make without any Secure Boot pairplease install x86_64 edk2 firmware with Secure Boot and Microsoft keys (the ovmf package) - not in … with …
the swtpm stub without its componenta message headed Notice, then snap install image-garden+swtpm
a launcher that finds no swtpm commandswtpm, the TPM emulator, is required: install the swtpm package

Roads from here

UBUNTU CORE · all snaps

ARCHITECTURES · machines and firmware

RUNTIME · make rules and the CLI

SNAP · snap-only features

Snapd map ↗