1 import ./make-test-python.nix ({ pkgs, ... } : {
3 meta = with pkgs.lib.maintainers; {
4 maintainers = [ joachifm ];
8 { lib, pkgs, config, ... }:
10 { users.users.alice = { isNormalUser = true; extraGroups = [ "proc" ]; };
11 users.users.sybil = { isNormalUser = true; group = "wheel"; };
12 imports = [ ../modules/profiles/hardened.nix ];
13 environment.memoryAllocator.provider = "graphene-hardened";
14 nix.settings.sandbox = false;
17 dhcpcd = super.dhcpcd.override { enablePrivSep = false; };
20 virtualisation.emptyDiskImages = [ 4096 ];
21 boot.initrd.postDeviceCommands = ''
22 ${pkgs.dosfstools}/bin/mkfs.vfat -n EFISYS /dev/vdb
24 virtualisation.fileSystems = {
26 device = "/dev/disk/by-label/EFISYS";
28 options = [ "noauto" ];
31 boot.extraModulePackages =
32 optional (versionOlder config.boot.kernelPackages.kernel.version "5.6")
33 config.boot.kernelPackages.wireguard;
34 boot.kernelModules = [ "wireguard" ];
39 hardened-malloc-tests = pkgs.graphene-hardened-malloc.ld-preload-tests;
42 machine.wait_for_unit("multi-user.target")
45 with subtest("AppArmor profiles are loaded"):
46 machine.succeed("systemctl status apparmor.service")
50 with subtest("AppArmor securityfs is mounted"):
51 machine.succeed("mountpoint -q /sys/kernel/security")
52 machine.succeed("cat /sys/kernel/security/apparmor/profiles")
55 # Test loading out-of-tree modules
56 with subtest("Out-of-tree modules can be loaded"):
57 machine.succeed("grep -Fq wireguard /proc/modules")
60 # Test kernel module hardening
61 with subtest("No more kernel modules can be loaded"):
62 # note: this better a be module we normally wouldn't load ...
63 machine.wait_for_unit("disable-kernel-module-loading.service")
64 machine.fail("modprobe dccp")
68 with subtest("User namespaces are restricted"):
69 machine.succeed("unshare --user true")
70 machine.fail("su -l alice -c 'unshare --user true'")
73 # Test dmesg restriction
74 with subtest("Regular users cannot access dmesg"):
75 machine.fail("su -l alice -c dmesg")
78 # Test access to kcore
79 with subtest("Kcore is inaccessible as root"):
80 machine.fail("cat /proc/kcore")
84 with subtest("Deferred mounts work"):
85 machine.fail("mountpoint -q /efi") # was deferred
86 machine.execute("mkdir -p /efi")
87 machine.succeed("mount /dev/disk/by-label/EFISYS /efi")
88 machine.succeed("mountpoint -q /efi") # now mounted
91 # Test Nix dæmon usage
92 with subtest("nix-daemon cannot be used by all users"):
93 machine.fail("su -l nobody -s /bin/sh -c 'nix --extra-experimental-features nix-command ping-store'")
94 machine.succeed("su -l alice -c 'nix --extra-experimental-features nix-command ping-store'")
97 # Test kernel image protection
98 with subtest("The kernel image is protected"):
99 machine.fail("systemctl hibernate")
100 machine.fail("systemctl kexec")
103 with subtest("The hardened memory allocator works"):
104 machine.succeed("${hardened-malloc-tests}/bin/run-tests")