lanzaboote/flake.nix

255 lines
8.2 KiB
Nix
Raw Normal View History

2022-11-21 05:44:04 -05:00
{
2022-11-24 06:29:16 -05:00
description = "Lanzaboot Secure Boot Madness";
2022-11-21 05:44:04 -05:00
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
2022-11-26 10:41:48 -05:00
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
2022-11-23 05:59:54 -05:00
nixpkgs-test.url = "github:RaitoBezarius/nixpkgs/experimental-secureboot";
2022-11-21 05:44:04 -05:00
rust-overlay.url = "github:oxalica/rust-overlay";
};
outputs = { self, nixpkgs, crane, nixpkgs-test, rust-overlay }:
2022-11-21 05:44:04 -05:00
let
pkgs = import nixpkgs {
system = "x86_64-linux";
overlays = [
rust-overlay.overlays.default
];
};
2022-11-24 06:05:46 -05:00
lib = pkgs.lib;
rust-nightly = pkgs.rust-bin.fromRustupToolchainFile ./rust/lanzaboote/rust-toolchain.toml;
2022-11-26 10:41:48 -05:00
craneLib = crane.lib.x86_64-linux.overrideToolchain rust-nightly;
2022-11-21 05:44:04 -05:00
2022-11-21 18:42:41 -05:00
uefi-run = pkgs.callPackage ./nix/uefi-run.nix {
inherit craneLib;
2022-11-21 09:36:39 -05:00
};
# Build attributes for a Rust application.
buildRustApp = {
src, target ? null, doCheck ? true
}: let
cleanedSrc = craneLib.cleanCargoSource src;
commonArgs = {
src = cleanedSrc;
CARGO_BUILD_TARGET = target;
inherit doCheck;
};
2022-11-21 09:36:39 -05:00
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
in {
package = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
});
clippy = craneLib.cargoClippy (commonArgs // {
inherit cargoArtifacts;
cargoClippyExtraArgs = "-- --deny warnings";
});
2022-11-21 19:29:16 -05:00
};
lanzabooteCrane = buildRustApp {
src = ./rust/lanzaboote;
target = "x86_64-unknown-uefi";
doCheck = false;
};
lanzaboote = lanzabooteCrane.package;
2022-11-26 10:41:48 -05:00
lanzatoolCrane = buildRustApp {
src = ./rust/lanzatool;
2022-11-24 05:45:09 -05:00
};
lanzatool-unwrapped = lanzatoolCrane.package;
2022-11-24 06:05:46 -05:00
lanzatool = pkgs.runCommand "lanzatool" {
nativeBuildInputs = [ pkgs.makeWrapper ];
} ''
mkdir -p $out/bin
# Clean PATH to only contain what we need to do objcopy. Also
# tell lanzatool where to find our UEFI binaries.
makeWrapper ${lanzatool-unwrapped}/bin/lanzatool $out/bin/lanzatool \
2022-11-25 07:07:04 -05:00
--set PATH ${lib.makeBinPath [ pkgs.binutils-unwrapped pkgs.sbsigntool ]} \
--set RUST_BACKTRACE full \
--set LANZABOOTE_STUB ${lanzaboote}/bin/lanzaboote.efi
2022-11-24 05:45:09 -05:00
'';
2022-11-21 18:45:18 -05:00
in {
2022-11-23 05:59:54 -05:00
overlays.default = final: prev: {
inherit lanzatool;
2022-11-23 05:59:54 -05:00
};
nixosModules.lanzaboote = import ./nix/lanzaboote.nix;
2022-11-21 18:45:18 -05:00
packages.x86_64-linux = {
inherit lanzaboote lanzatool;
default = lanzatool;
2022-11-21 18:45:18 -05:00
};
2022-11-21 09:36:39 -05:00
2022-11-21 18:45:18 -05:00
devShells.x86_64-linux.default = pkgs.mkShell {
packages = [
2022-11-21 18:45:18 -05:00
uefi-run
lanzatool
2022-11-21 20:08:35 -05:00
pkgs.openssl
(pkgs.sbctl.override {
databasePath = "pki";
})
pkgs.sbsigntool
2022-11-24 10:59:16 -05:00
pkgs.efitools
2022-11-24 21:04:44 -05:00
pkgs.python39Packages.ovmfvartool
pkgs.qemu
2022-11-21 18:45:18 -05:00
];
inputsFrom = [
lanzaboote
];
2022-11-21 05:44:04 -05:00
};
2022-11-23 05:59:54 -05:00
checks.x86_64-linux = let
mkSecureBootTest = { name, machine ? {}, testScript }: nixpkgs-test.legacyPackages.x86_64-linux.nixosTest {
inherit name testScript;
nodes.machine = { lib, ... }: {
imports = [
self.nixosModules.lanzaboote
machine
];
2022-11-23 05:59:54 -05:00
nixpkgs.overlays = [ self.overlays.default ];
virtualisation = {
useBootLoader = true;
useEFIBoot = true;
2022-11-24 21:04:44 -05:00
useSecureBoot = true;
};
boot.loader.efi = {
enable = true;
canTouchEfiVariables = true;
};
boot.lanzaboote = {
enable = true;
enrollKeys = lib.mkDefault true;
2022-11-24 10:59:16 -05:00
pkiBundle = ./pki;
};
2022-11-23 05:59:54 -05:00
};
};
mkUnsignedTest = { name, path }: mkSecureBootTest {
inherit name;
2022-11-23 05:59:54 -05:00
testScript = ''
import json
import os.path
bootspec = None
2022-11-26 16:19:15 -05:00
def convert_to_esp(store_file_path):
store_dir = os.path.basename(os.path.dirname(store_file_path))
filename = os.path.basename(store_file_path)
return f'/boot/EFI/nixos/{store_dir}-{filename}.efi'
machine.start()
bootspec = json.loads(machine.succeed("cat /run/current-system/bootspec/boot.v1.json"))
src_path = ${path.src}
dst_path = ${path.dst}
machine.succeed(f"cp -rf {src_path} {dst_path}")
machine.succeed("sync")
machine.crash()
machine.start()
machine.wait_for_console_text("panicked")
2022-11-23 05:59:54 -05:00
'';
};
in
{
lanzatool-clippy = lanzatoolCrane.clippy;
lanzaboote-clippy = lanzabooteCrane.clippy;
# TODO: user mode: OK
# TODO: how to get in: {deployed, audited} mode ?
lanzaboote-boot = mkSecureBootTest {
name = "signed-files-boot-under-secureboot";
testScript = ''
2022-11-29 14:10:55 -05:00
machine.start()
assert "Secure Boot: enabled (user)" in machine.succeed("bootctl status")
'';
};
lanzaboote-boot-under-sd-stage1 = mkSecureBootTest {
name = "signed-files-boot-under-secureboot-systemd-stage-1";
machine = { ... }: {
boot.initrd.systemd.enable = true;
};
testScript = ''
machine.start()
assert "Secure Boot: enabled (user)" in machine.succeed("bootctl status")
'';
};
2022-11-29 14:10:55 -05:00
# So, this is the responsibility of the lanzatool install
# to run the append-initrd-secret script
# This test assert that lanzatool still do the right thing
# preDeviceCommands should not have any root filesystem mounted
# so it should not be able to find /etc/iamasecret, other than the
# initrd's one.
# which should exist IF lanzatool do the right thing.
lanzaboote-with-initrd-secrets = mkSecureBootTest {
name = "signed-files-boot-with-secrets-under-secureboot";
machine = { ... }: {
boot.initrd.secrets = {
"/etc/iamasecret" = (pkgs.writeText "iamsecret" "this is a very secure secret");
};
boot.initrd.preDeviceCommands = ''
2022-11-26 16:19:15 -05:00
grep "this is a very secure secret" /etc/iamasecret
'';
};
testScript = ''
2022-11-25 19:50:51 -05:00
machine.start()
assert "Secure Boot: enabled (user)" in machine.succeed("bootctl status")
'';
};
is-initrd-secured = mkUnsignedTest {
name = "unsigned-initrd-do-not-boot-under-secureboot";
path = {
2022-11-26 16:19:15 -05:00
src = "bootspec.get('initrd')";
dst = "convert_to_esp(bootspec.get('initrd'))";
};
};
is-kernel-secured = mkUnsignedTest {
name = "unsigned-kernel-do-not-boot-under-secureboot";
path = {
2022-11-26 16:19:15 -05:00
src = "bootspec.get('kernel')";
dst = "convert_to_esp(bootspec.get('kernel'))";
};
};
2022-11-27 05:19:02 -05:00
specialisation-works = mkSecureBootTest {
name = "specialisation-still-boot-under-secureboot";
machine = { pkgs, ... }: {
specialisation.variant.configuration = {
environment.systemPackages = [
pkgs.efibootmgr
];
};
};
testScript = ''
machine.start()
print(machine.succeed("ls -lah /boot/EFI/Linux"))
print(machine.succeed("cat /run/current-system/bootspec/boot.v1.json"))
# TODO: make it more reliable to find this filename, i.e. read it from somewhere?
machine.succeed("bootctl set-default nixos-generation-1-specialisation-variant.efi")
machine.succeed("sync")
machine.fail("efibootmgr")
machine.crash()
machine.start()
print(machine.succeed("bootctl"))
# We have efibootmgr in this specialisation.
machine.succeed("efibootmgr")
'';
};
};
2022-11-21 18:45:18 -05:00
};
2022-11-21 05:44:04 -05:00
}