From 0569bce6762de3b223e543981428a31af2f6c1c0 Mon Sep 17 00:00:00 2001 From: Job79 Date: Sat, 14 Nov 2026 22:43:34 +0100 Subject: [PATCH] Secure Boot mkosi signs systemd-boot and the UKI with our own key (mkosi.crt; the private key is the SECUREBOOT_KEY secret, which the os workflow writes to mkosi.key) and puts the keys on the ESP. systemd-boot enrolls them at first boot if the firmware is in setup mode: in Proxmox, an EFI disk without pre-enrolled keys. Hosts installed before this keep their unsigned systemd-boot (updates don't replace it), so they boot as before, without Secure Boot. Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/os.yml | 9 +++++++-- .gitignore | 2 ++ README.md | 10 +++++++--- mkosi.conf | 9 +++++++++ mkosi.crt | 18 ++++++++++++++++++ 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 mkosi.crt diff --git a/.gitea/workflows/os.yml b/.gitea/workflows/os.yml index 84b2e43..923c683 100644 --- a/.gitea/workflows/os.yml +++ b/.gitea/workflows/os.yml @@ -20,11 +20,16 @@ jobs: AUTH: "Authorization: token ${{ github.token }}" steps: - name: Install tools - run: dnf install -yq mkosi + run: dnf install -yq mkosi sbsigntools - name: Check out uses: builtin:checkout + # Signs systemd-boot and the UKI for Secure Boot (mkosi.conf, [Validation]). - name: Build - run: mkosi build + env: + KEY: ${{ secrets.SECUREBOOT_KEY }} + run: | + (umask 077 && printf '%s\n' "$KEY" > mkosi.key) + mkosi build - name: Compress working-directory: mkosi.output run: | diff --git a/.gitignore b/.gitignore index 96100fc..5087531 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ # Build outputs. mkosi.output/ +# The Secure Boot private key (the SECUREBOOT_KEY secret); mkosi.crt is its certificate. +mkosi.key diff --git a/README.md b/README.md index dcb79c5..a78e42d 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,13 @@ A [Gitea Actions](https://docs.gitea.com/usage/actions/overview) runner packaged - **Stateless:** container storage is wiped on every boot. - **Isolated jobs:** each job runs in a container of its own (privileged, so it can build OS images), with its own podman; jobs never get the runner's podman. The job image ships with the OS, and common base images are shared read-only, so jobs don't download them. A job that runs the VM out of memory is killed (systemd-oomd); the runner keeps running. - **Self-updating:** images are rebuilt weekly and on every push. Hosts download updates with systemd-sysupdate, check their signature (the key is the `SIGNING_KEY` secret of this repository), and write them into a second root partition (A/B) and reboot into them daily at about 04:10, after letting running jobs finish. +- **Secure Boot:** systemd-boot and the UKIs are signed with our own key (`mkosi.crt`; the private key is the `SECUREBOOT_KEY` secret of this repository). systemd-boot enrolls it at first boot if the VM's firmware has no keys yet; the firmware then boots nothing else. - **Read-only system:** systemd-boot starts a UKI that mounts the root filesystem, /etc and /usr included, from a read-only erofs partition checked by dm-verity. Updates replace it whole, so nothing drifts. All state (the runner registration, the root password, logs) lives on /var, its own partition. - **Hardened:** a firewall that drops all incoming traffic, and KSPP sysctls and kernel arguments. ## Install -Download [the disk image](https://git.plabble.org/api/packages/Job79/generic/git-runner/latest/git-runner.qcow2) (built by the `os` workflow), resize it to the disk size you want (/var grows to fill it) and boot it with UEFI. The screen and the serial console ask for a root password, hostname, Gitea URL, registration token and number of parallel jobs; the runner then registers itself. +Download [the disk image](https://git.plabble.org/api/packages/Job79/generic/git-runner/latest/git-runner.qcow2) (built by the `os` workflow), resize it to the disk size you want (/var grows to fill it) and boot it with UEFI in Secure Boot setup mode (no keys enrolled; in Proxmox, add the EFI disk with "Pre-Enroll keys" unchecked), so it enrolls our key. The screen and the serial console ask for a root password, hostname, Gitea URL, registration token and number of parallel jobs; the runner then registers itself. ## Images @@ -28,11 +29,14 @@ Download [the disk image](https://git.plabble.org/api/packages/Job79/generic/git mkosi build ``` -`mkosi.output/` then holds the disk image (`git-runner_.raw`), and the UKI and root partitions that the `os` workflow publishes as an update. To boot the disk image (needs `qemu-kvm-core edk2-ovmf`): +The build signs with `mkosi.key`: the `SECUREBOOT_KEY` secret, or for a local test a key of your own (`mkosi genkey` writes `mkosi.key` and `mkosi.crt`; don't commit them). + +`mkosi.output/` then holds the disk image (`git-runner_.raw`), and the UKI and root partitions that the `os` workflow publishes as an update. To boot the disk image with Secure Boot (needs `qemu-kvm-core edk2-ovmf`; `OVMF_VARS.fd` has no keys, so it enrolls ours): ```sh qemu-img convert -O qcow2 mkosi.output/git-runner_.raw test.qcow2 && qemu-img resize test.qcow2 40G cp /usr/share/edk2/ovmf/OVMF_VARS.fd vars.fd qemu-kvm -m 8G -smp 4 -cpu host -nic user,model=virtio-net-pci -drive file=test.qcow2,if=virtio -device virtio-balloon-pci,free-page-reporting=on \ - -drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/ovmf/OVMF_CODE.fd -drive if=pflash,format=raw,file=vars.fd + -machine q35,smm=on -global driver=cfi.pflash01,property=secure,value=on \ + -drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/ovmf/OVMF_CODE.secboot.fd -drive if=pflash,format=raw,file=vars.fd ``` diff --git a/mkosi.conf b/mkosi.conf index 52ef971..4334ca2 100644 --- a/mkosi.conf +++ b/mkosi.conf @@ -110,3 +110,12 @@ RemoveFiles= /usr/lib/systemd/system/systemd-homed* /usr/lib/systemd/system/systemd-userdbd.* /usr/lib/systemd/system/systemd-nsresourced.* + +[Validation] +# Signs systemd-boot and the UKI for Secure Boot, and puts our keys on the ESP, where systemd-boot +# enrolls them at first boot if the firmware is in setup mode (no keys enrolled; in Proxmox, an EFI +# disk without pre-enrolled keys). mkosi.crt is the certificate; mkosi.key, its private key, is the +# SECUREBOOT_KEY secret of this repository, which the os workflow writes to mkosi.key. +SecureBoot=yes +SecureBootKey=mkosi.key +SecureBootCertificate=mkosi.crt diff --git a/mkosi.crt b/mkosi.crt new file mode 100644 index 0000000..923560d --- /dev/null +++ b/mkosi.crt @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE----- +MIICzjCCAbagAwIBAgIUVszVwCQVaOBS3MyL9TUA4lMaeHowDQYJKoZIhvcNAQEL +BQAwITEfMB0GA1UEAwwWZ2l0LXJ1bm5lciBTZWN1cmUgQm9vdDAeFw0yNjExMTQy +MTE1MjhaFw0zNjExMTEyMTE1MjhaMCExHzAdBgNVBAMMFmdpdC1ydW5uZXIgU2Vj +dXJlIEJvb3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDO1KaKLK6e +phfFvGyceuWGkJvnehJ5634rhty4gsyJlFVZSdZvDTU5iU6okjwtnYShEgEH02jX +Xgj7Y76iBuetAFjx1ISAdJNxdPHHO7uazLidMQ2gaVsSXayTPgwNmKoJ2xMmkoyQ +SO1vqTymWlVGJptgFIOivUbFBhovxSqeAC2kFplK11/3mQxl1Oxd7Lrh9BwBZbqU +E9Rd2eQUo8g9JjBA9nR7ogxa4wyP1mpMyvmJ2NbcuMunpKQC66eW5RNH3ERgJi36 +fPiuT/ou+gjpYZo1HW6c59nx9XFO6epCcDWRV2xfcB/iFaDIVuJafDWvyeB1RFD+ +CguGyHI7VFSTAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAI84Ucm8m45nRFPf6ffb +vlfamcO+HIhobyPmafwEGLv6HWjHC9I9XulmEOHz9PePEI2TX0K9I883IGvmoyEA +AMI1f9kskm2Xg7Xk/OLXmqHDGDbJFNaS8e79ImBqu9bBWe4rOa6naeH7JiVjTh5Y +2APyP+o+DKrXuCjcQtZc1PkuQPrElNmqKduhfL4acIPR0N7kJ58FwpuE9Zh8C0p1 +NSb7x38Dxxwid4zWDbSuIeIi0FfCFHC8olvzfDQlNst5VQeE921KcK8YKREIgn0H +2GLtzpczUGq8u6Pen7R8qSjLZ0GlVXfMlW1EthoYnlP1n4oxg6xfPn2J5SyP2EGi +Iis= +-----END CERTIFICATE-----