2022-11-21 04:44:04 -06:00
|
|
|
{
|
2022-11-24 05:29:16 -06:00
|
|
|
description = "Lanzaboot Secure Boot Madness";
|
2022-11-21 04:44:04 -06:00
|
|
|
|
|
|
|
inputs = {
|
2022-12-17 17:31:09 -06:00
|
|
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small";
|
|
|
|
nixpkgs-test.url = "github:RaitoBezarius/nixpkgs/simplified-qemu-boot-disks";
|
2022-11-26 09:41:48 -06:00
|
|
|
|
|
|
|
crane = {
|
|
|
|
url = "github:ipetkov/crane";
|
|
|
|
inputs.nixpkgs.follows = "nixpkgs";
|
2022-12-08 13:39:35 -06:00
|
|
|
inputs.rust-overlay.follows = "rust-overlay";
|
|
|
|
inputs.flake-utils.follows = "flake-utils";
|
2022-11-26 09:41:48 -06:00
|
|
|
};
|
|
|
|
|
2022-12-08 13:39:35 -06:00
|
|
|
rust-overlay = {
|
|
|
|
url = "github:oxalica/rust-overlay";
|
|
|
|
inputs.nixpkgs.follows = "nixpkgs";
|
|
|
|
inputs.flake-utils.follows = "flake-utils";
|
|
|
|
};
|
|
|
|
|
|
|
|
flake-utils.url = "github:numtide/flake-utils";
|
2022-11-21 04:44:04 -06:00
|
|
|
};
|
|
|
|
|
2022-12-17 17:31:09 -06:00
|
|
|
outputs = { self, nixpkgs, nixpkgs-test, crane, rust-overlay, ... }:
|
2022-11-21 04:44:04 -06:00
|
|
|
let
|
|
|
|
pkgs = import nixpkgs {
|
|
|
|
system = "x86_64-linux";
|
|
|
|
overlays = [
|
|
|
|
rust-overlay.overlays.default
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2022-11-24 05:05:46 -06:00
|
|
|
lib = pkgs.lib;
|
|
|
|
|
2022-11-21 18:53:40 -06:00
|
|
|
rust-nightly = pkgs.rust-bin.fromRustupToolchainFile ./rust/lanzaboote/rust-toolchain.toml;
|
2022-11-26 09:41:48 -06:00
|
|
|
craneLib = crane.lib.x86_64-linux.overrideToolchain rust-nightly;
|
2022-11-21 04:44:04 -06:00
|
|
|
|
2022-11-21 17:42:41 -06:00
|
|
|
uefi-run = pkgs.callPackage ./nix/uefi-run.nix {
|
2022-11-28 06:48:25 -06:00
|
|
|
inherit craneLib;
|
2022-11-21 08:36:39 -06:00
|
|
|
};
|
|
|
|
|
2022-11-28 06:48:25 -06: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 08:36:39 -06:00
|
|
|
|
2022-11-28 06:48:25 -06:00
|
|
|
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
|
|
|
|
in {
|
|
|
|
package = craneLib.buildPackage (commonArgs // {
|
|
|
|
inherit cargoArtifacts;
|
|
|
|
});
|
|
|
|
|
|
|
|
clippy = craneLib.cargoClippy (commonArgs // {
|
|
|
|
inherit cargoArtifacts;
|
|
|
|
cargoClippyExtraArgs = "-- --deny warnings";
|
|
|
|
});
|
2022-11-21 18:29:16 -06:00
|
|
|
};
|
|
|
|
|
2022-11-28 06:48:25 -06:00
|
|
|
lanzabooteCrane = buildRustApp {
|
|
|
|
src = ./rust/lanzaboote;
|
|
|
|
target = "x86_64-unknown-uefi";
|
|
|
|
doCheck = false;
|
|
|
|
};
|
2022-11-23 06:00:55 -06:00
|
|
|
|
2022-11-28 06:48:25 -06:00
|
|
|
lanzaboote = lanzabooteCrane.package;
|
2022-11-26 09:41:48 -06:00
|
|
|
|
2022-11-28 06:48:25 -06:00
|
|
|
lanzatoolCrane = buildRustApp {
|
|
|
|
src = ./rust/lanzatool;
|
2022-11-24 04:45:09 -06:00
|
|
|
};
|
|
|
|
|
2022-11-28 06:48:25 -06:00
|
|
|
lanzatool-unwrapped = lanzatoolCrane.package;
|
|
|
|
|
2022-11-24 05:05:46 -06: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.
|
2022-11-25 19:16:12 -06:00
|
|
|
makeWrapper ${lanzatool-unwrapped}/bin/lanzatool $out/bin/lanzatool \
|
2022-11-25 06:07:04 -06:00
|
|
|
--set PATH ${lib.makeBinPath [ pkgs.binutils-unwrapped pkgs.sbsigntool ]} \
|
2022-11-23 04:59:54 -06:00
|
|
|
--set RUST_BACKTRACE full \
|
2022-11-27 19:59:21 -06:00
|
|
|
--set LANZABOOTE_STUB ${lanzaboote}/bin/lanzaboote.efi
|
2022-11-24 04:45:09 -06:00
|
|
|
'';
|
2022-11-21 17:45:18 -06:00
|
|
|
in {
|
2022-11-23 04:59:54 -06:00
|
|
|
overlays.default = final: prev: {
|
2022-11-25 16:42:34 -06:00
|
|
|
inherit lanzatool;
|
2022-11-23 04:59:54 -06:00
|
|
|
};
|
|
|
|
|
2022-12-08 14:23:35 -06:00
|
|
|
nixosModules.lanzaboote = { pkgs, lib, ... }: {
|
|
|
|
imports = [ ./nix/lanzaboote.nix ];
|
2022-12-10 11:26:47 -06:00
|
|
|
boot.lanzaboote.package = lib.mkDefault self.packages.${pkgs.system}.lanzatool;
|
2022-12-08 14:23:35 -06:00
|
|
|
};
|
2022-11-23 04:59:54 -06:00
|
|
|
|
2022-11-21 17:45:18 -06:00
|
|
|
packages.x86_64-linux = {
|
2022-11-27 19:59:21 -06:00
|
|
|
inherit lanzaboote lanzatool;
|
2022-11-25 19:26:39 -06:00
|
|
|
default = lanzatool;
|
2022-11-21 17:45:18 -06:00
|
|
|
};
|
2022-11-21 08:36:39 -06:00
|
|
|
|
2022-11-21 17:45:18 -06:00
|
|
|
devShells.x86_64-linux.default = pkgs.mkShell {
|
2022-11-22 17:26:00 -06:00
|
|
|
packages = [
|
2022-11-21 17:45:18 -06:00
|
|
|
uefi-run
|
2022-11-21 18:48:53 -06:00
|
|
|
lanzatool
|
2022-11-21 19:08:35 -06:00
|
|
|
pkgs.openssl
|
2022-11-23 04:59:54 -06:00
|
|
|
(pkgs.sbctl.override {
|
|
|
|
databasePath = "pki";
|
|
|
|
})
|
|
|
|
pkgs.sbsigntool
|
2022-11-24 09:59:16 -06:00
|
|
|
pkgs.efitools
|
2022-11-24 20:04:44 -06:00
|
|
|
pkgs.python39Packages.ovmfvartool
|
|
|
|
pkgs.qemu
|
2022-11-21 17:45:18 -06:00
|
|
|
];
|
2022-11-22 17:26:00 -06:00
|
|
|
|
|
|
|
inputsFrom = [
|
|
|
|
lanzaboote
|
|
|
|
];
|
2022-11-21 04:44:04 -06:00
|
|
|
};
|
2022-11-23 04:59:54 -06:00
|
|
|
|
2022-11-25 11:42:37 -06: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 04:59:54 -06:00
|
|
|
nixpkgs.overlays = [ self.overlays.default ];
|
|
|
|
|
2022-11-23 04:59:54 -06:00
|
|
|
virtualisation = {
|
|
|
|
useBootLoader = true;
|
|
|
|
useEFIBoot = true;
|
2022-11-24 20:04:44 -06:00
|
|
|
useSecureBoot = true;
|
2022-11-23 04:59:54 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
boot.loader.efi = {
|
|
|
|
canTouchEfiVariables = true;
|
|
|
|
};
|
|
|
|
boot.lanzaboote = {
|
|
|
|
enable = true;
|
2022-11-25 11:42:37 -06:00
|
|
|
enrollKeys = lib.mkDefault true;
|
2022-11-24 09:59:16 -06:00
|
|
|
pkiBundle = ./pki;
|
2022-11-23 04:59:54 -06:00
|
|
|
};
|
2022-11-23 04:59:54 -06:00
|
|
|
};
|
2022-11-25 11:42:37 -06:00
|
|
|
};
|
2022-11-28 04:27:31 -06:00
|
|
|
|
|
|
|
# Execute a boot test that is intended to fail.
|
|
|
|
#
|
|
|
|
mkUnsignedTest = { name, path, appendCrap ? false }: mkSecureBootTest {
|
2022-11-25 11:42:37 -06:00
|
|
|
inherit name;
|
2022-11-23 04:59:54 -06:00
|
|
|
testScript = ''
|
2022-11-25 11:42:37 -06:00
|
|
|
import json
|
|
|
|
import os.path
|
|
|
|
bootspec = None
|
2022-11-26 15:19:15 -06:00
|
|
|
|
2022-11-25 11:42:37 -06: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()
|
2022-12-17 17:31:09 -06:00
|
|
|
bootspec = json.loads(machine.succeed("cat /run/current-system/boot.json")).get('v1')
|
|
|
|
assert bootspec is not None, "Unsupported bootspec version!"
|
2022-11-25 11:42:37 -06:00
|
|
|
src_path = ${path.src}
|
|
|
|
dst_path = ${path.dst}
|
|
|
|
machine.succeed(f"cp -rf {src_path} {dst_path}")
|
2022-11-28 04:27:31 -06:00
|
|
|
'' + lib.optionalString appendCrap ''
|
|
|
|
machine.succeed(f"echo Foo >> {dst_path}")
|
|
|
|
'' +
|
|
|
|
''
|
2022-11-25 11:42:37 -06:00
|
|
|
machine.succeed("sync")
|
|
|
|
machine.crash()
|
2022-11-23 04:59:54 -06:00
|
|
|
machine.start()
|
2022-11-28 04:27:31 -06:00
|
|
|
machine.wait_for_console_text("Hash mismatch")
|
2022-11-23 04:59:54 -06:00
|
|
|
'';
|
|
|
|
};
|
2022-11-25 11:42:37 -06:00
|
|
|
in
|
2022-11-26 09:42:20 -06:00
|
|
|
{
|
2022-11-28 06:48:25 -06:00
|
|
|
lanzatool-clippy = lanzatoolCrane.clippy;
|
|
|
|
lanzaboote-clippy = lanzabooteCrane.clippy;
|
2022-11-26 10:05:33 -06:00
|
|
|
|
2022-11-26 09:42:20 -06:00
|
|
|
# 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 13:10:55 -06: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-26 09:42:20 -06:00
|
|
|
};
|
2022-11-29 13:10:55 -06:00
|
|
|
|
2022-11-26 09:42:20 -06: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 15:19:15 -06:00
|
|
|
grep "this is a very secure secret" /etc/iamasecret
|
|
|
|
'';
|
2022-11-26 09:42:20 -06:00
|
|
|
};
|
|
|
|
testScript = ''
|
2022-11-25 18:50:51 -06:00
|
|
|
machine.start()
|
|
|
|
assert "Secure Boot: enabled (user)" in machine.succeed("bootctl status")
|
|
|
|
'';
|
2022-11-25 11:42:37 -06:00
|
|
|
};
|
2022-11-28 04:27:31 -06:00
|
|
|
|
|
|
|
# The initrd is not directly signed. Its hash is embedded
|
|
|
|
# into lanzaboote. To make integrity verification fail, we
|
|
|
|
# actually have to modify the initrd. Appending crap to the
|
|
|
|
# end is a harmless way that would make the kernel still
|
|
|
|
# accept it.
|
2022-11-26 09:42:20 -06:00
|
|
|
is-initrd-secured = mkUnsignedTest {
|
|
|
|
name = "unsigned-initrd-do-not-boot-under-secureboot";
|
|
|
|
path = {
|
2022-11-26 15:19:15 -06:00
|
|
|
src = "bootspec.get('initrd')";
|
|
|
|
dst = "convert_to_esp(bootspec.get('initrd'))";
|
2022-11-26 09:42:20 -06:00
|
|
|
};
|
2022-11-28 04:27:31 -06:00
|
|
|
appendCrap = true;
|
2022-11-26 09:42:20 -06:00
|
|
|
};
|
2022-11-28 04:27:31 -06:00
|
|
|
|
2022-11-26 09:42:20 -06:00
|
|
|
is-kernel-secured = mkUnsignedTest {
|
|
|
|
name = "unsigned-kernel-do-not-boot-under-secureboot";
|
|
|
|
path = {
|
2022-11-26 15:19:15 -06:00
|
|
|
src = "bootspec.get('kernel')";
|
|
|
|
dst = "convert_to_esp(bootspec.get('kernel'))";
|
2022-11-26 09:42:20 -06:00
|
|
|
};
|
2022-11-25 11:42:37 -06:00
|
|
|
};
|
2022-11-27 04:19:02 -06: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"))
|
2022-12-17 17:31:09 -06:00
|
|
|
print(machine.succeed("cat /run/current-system/boot.json"))
|
2022-11-27 04:19:02 -06:00
|
|
|
# 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 17:45:18 -06:00
|
|
|
};
|
2022-11-21 04:44:04 -06:00
|
|
|
}
|