2022-12-25 09:18:53 -06:00
|
|
|
{ pkgs
|
|
|
|
, testPkgs
|
|
|
|
, lanzabooteModule
|
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
inherit (pkgs) lib;
|
|
|
|
|
|
|
|
mkSecureBootTest = { name, machine ? { }, testScript }: testPkgs.nixosTest {
|
|
|
|
inherit name testScript;
|
|
|
|
nodes.machine = { lib, ... }: {
|
|
|
|
imports = [
|
|
|
|
lanzabooteModule
|
|
|
|
machine
|
|
|
|
];
|
|
|
|
|
|
|
|
virtualisation = {
|
|
|
|
useBootLoader = true;
|
|
|
|
useEFIBoot = true;
|
|
|
|
useSecureBoot = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
boot.loader.efi = {
|
|
|
|
canTouchEfiVariables = true;
|
|
|
|
};
|
|
|
|
boot.lanzaboote = {
|
|
|
|
enable = true;
|
|
|
|
enrollKeys = lib.mkDefault true;
|
2023-01-25 18:17:18 -06:00
|
|
|
pkiBundle = ./fixtures/uefi-keys;
|
2022-12-25 09:18:53 -06:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-01-27 18:34:15 -06:00
|
|
|
# Execute a SB test that is expected to fail because of a hash mismatch.
|
2022-12-25 09:18:53 -06:00
|
|
|
#
|
2023-01-27 18:34:15 -06:00
|
|
|
# Takes a set `path` consisting of a `src` and a `dst` attribute. The file at
|
|
|
|
# `src` is copied to `dst` inside th VM. Optionally append some random data
|
|
|
|
# ("crap") to the end of the file at `dst`. This is useful to easily change
|
|
|
|
# the hash of a file and produce a hash mismatch when booting the stub.
|
|
|
|
mkHashMismatchTest = { name, path, appendCrap ? false }: mkSecureBootTest {
|
2022-12-25 09:18:53 -06:00
|
|
|
inherit name;
|
|
|
|
testScript = ''
|
|
|
|
import json
|
|
|
|
import os.path
|
|
|
|
bootspec = None
|
|
|
|
|
|
|
|
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/boot.json")).get('v1')
|
|
|
|
assert bootspec is not None, "Unsupported bootspec version!"
|
|
|
|
src_path = ${path.src}
|
|
|
|
dst_path = ${path.dst}
|
|
|
|
machine.succeed(f"cp -rf {src_path} {dst_path}")
|
|
|
|
'' + lib.optionalString appendCrap ''
|
|
|
|
machine.succeed(f"echo Foo >> {dst_path}")
|
|
|
|
'' +
|
|
|
|
''
|
|
|
|
machine.succeed("sync")
|
|
|
|
machine.crash()
|
|
|
|
machine.start()
|
|
|
|
machine.wait_for_console_text("Hash mismatch")
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
# TODO: user mode: OK
|
|
|
|
# TODO: how to get in: {deployed, audited} mode ?
|
2023-01-27 18:34:15 -06:00
|
|
|
basic = mkSecureBootTest {
|
|
|
|
name = "lanzaboote";
|
2022-12-25 09:18:53 -06:00
|
|
|
testScript = ''
|
|
|
|
machine.start()
|
|
|
|
assert "Secure Boot: enabled (user)" in machine.succeed("bootctl status")
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-01-27 18:34:15 -06:00
|
|
|
systemd-initrd = mkSecureBootTest {
|
|
|
|
name = "lanzaboote-systemd-initrd";
|
2022-12-25 09:18:53 -06:00
|
|
|
machine = { ... }: {
|
|
|
|
boot.initrd.systemd.enable = true;
|
|
|
|
};
|
|
|
|
testScript = ''
|
|
|
|
machine.start()
|
|
|
|
assert "Secure Boot: enabled (user)" in machine.succeed("bootctl status")
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-01-27 18:34:15 -06:00
|
|
|
# Test that a secret is appended to the initrd during installation.
|
|
|
|
#
|
|
|
|
# During the execution of `preDeviceCommands`, no filesystem should be
|
|
|
|
# mounted. The only place to find `/etc/iamasecret` then, is in the initrd.
|
|
|
|
initrd-secrets = mkSecureBootTest {
|
|
|
|
name = "lanzaboote-initrd-secrets";
|
2022-12-25 09:18:53 -06:00
|
|
|
machine = { ... }: {
|
|
|
|
boot.initrd.secrets = {
|
|
|
|
"/etc/iamasecret" = (pkgs.writeText "iamsecret" "this is a very secure secret");
|
|
|
|
};
|
|
|
|
|
|
|
|
boot.initrd.preDeviceCommands = ''
|
|
|
|
grep "this is a very secure secret" /etc/iamasecret
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
testScript = ''
|
|
|
|
machine.start()
|
|
|
|
assert "Secure Boot: enabled (user)" in machine.succeed("bootctl status")
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
# The initrd is not directly signed. Its hash is embedded
|
2023-01-27 18:34:15 -06:00
|
|
|
# into the UKI. To make integrity verification fail, we
|
2022-12-25 09:18:53 -06:00
|
|
|
# actually have to modify the initrd. Appending crap to the
|
|
|
|
# end is a harmless way that would make the kernel still
|
|
|
|
# accept it.
|
2023-01-27 18:34:15 -06:00
|
|
|
secured-initrd = mkHashMismatchTest {
|
|
|
|
name = "lanzaboote-secured-initrd";
|
2022-12-25 09:18:53 -06:00
|
|
|
path = {
|
|
|
|
src = "bootspec.get('initrd')";
|
|
|
|
dst = "convert_to_esp(bootspec.get('initrd'))";
|
|
|
|
};
|
|
|
|
appendCrap = true;
|
|
|
|
};
|
|
|
|
|
2023-01-27 18:34:15 -06:00
|
|
|
secured-kernel = mkHashMismatchTest {
|
|
|
|
name = "lanzaboote-secured-kernel";
|
2022-12-25 09:18:53 -06:00
|
|
|
path = {
|
|
|
|
src = "bootspec.get('kernel')";
|
|
|
|
dst = "convert_to_esp(bootspec.get('kernel'))";
|
|
|
|
};
|
|
|
|
};
|
2023-01-27 18:34:15 -06:00
|
|
|
|
|
|
|
specialisation = mkSecureBootTest {
|
|
|
|
name = "lanzaboote-specialisation";
|
2022-12-25 09:18:53 -06:00
|
|
|
machine = { pkgs, ... }: {
|
|
|
|
specialisation.variant.configuration = {
|
|
|
|
environment.systemPackages = [
|
|
|
|
pkgs.efibootmgr
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
testScript = ''
|
|
|
|
machine.start()
|
|
|
|
print(machine.succeed("ls -lah /boot/EFI/Linux"))
|
|
|
|
# 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"))
|
2023-01-27 18:34:15 -06:00
|
|
|
# Only the specialisation contains the efibootmgr binary.
|
2022-12-25 09:18:53 -06:00
|
|
|
machine.succeed("efibootmgr")
|
|
|
|
'';
|
|
|
|
};
|
2023-01-26 17:37:05 -06:00
|
|
|
|
|
|
|
systemd-boot-loader-config = mkSecureBootTest {
|
|
|
|
name = "lanzaboote-systemd-boot-loader-config";
|
|
|
|
machine = {
|
|
|
|
boot.loader.timeout = 0;
|
|
|
|
boot.loader.systemd-boot.consoleMode = "auto";
|
|
|
|
};
|
|
|
|
testScript = ''
|
|
|
|
machine.start()
|
|
|
|
|
|
|
|
actual_loader_config = machine.succeed("cat /boot/loader/loader.conf")
|
|
|
|
expected_loader_config = "timeout 0\nconsole-mode auto\n"
|
|
|
|
|
|
|
|
assert actual_loader_config == expected_loader_config, \
|
|
|
|
f"Actual: '{actual_loader_config}' is not equal to expected: '{expected_loader_config}'"
|
|
|
|
'';
|
|
|
|
};
|
2022-12-25 09:18:53 -06:00
|
|
|
}
|