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