git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/hardened: Port test to Python and fix it

Related to #72828 Replaces and closes #76708 Looks like `nix ping-store` does not output anything anymore but still fails when the connection does not work.

commit540c03350771a3262adadbe5549687c7b24d472a authorJanne Heß <janne@hess.ooo> dateFri 21 Aug 2020, 21:29 (6 years ago) parentff03800
1 file changed+59−55
nixos/tests/hardened.nix+59−55
nixos/tests/hardened.nix+59−55
@@ -1,4 +1,4 @@
1−import ./make-test.nix ({ pkgs, latestKernel ? false, ... } : {
1+import ./make-test-python.nix ({ pkgs, latestKernel ? false, ... } : {
22 name = "hardened";
33 meta = with pkgs.stdenv.lib.maintainers; {
44 maintainers = [ joachifm ];
@@ -47,84 +47,88 @@ import ./make-test.nix ({ pkgs, latestKernel ? false, ... } : {
4747 };
4848 in
4949 ''
50− $machine->waitForUnit("multi-user.target");
50+ machine.wait_for_unit("multi-user.target")
51+
52+
53+ with subtest("AppArmor profiles are loaded"):
54+ machine.succeed("systemctl status apparmor.service")
5155
52− subtest "apparmor-loaded", sub {
53− $machine->succeed("systemctl status apparmor.service");
54− };
5556
5657 # AppArmor securityfs
57− subtest "apparmor-securityfs", sub {
58− $machine->succeed("mountpoint -q /sys/kernel/security");
59− $machine->succeed("cat /sys/kernel/security/apparmor/profiles");
60− };
58+ with subtest("AppArmor securityfs is mounted"):
59+ machine.succeed("mountpoint -q /sys/kernel/security")
60+ machine.succeed("cat /sys/kernel/security/apparmor/profiles")
61+
6162
6263 # Test loading out-of-tree modules
63− subtest "extra-module-packages", sub {
64− $machine->succeed("grep -Fq wireguard /proc/modules");
65− };
64+ with subtest("Out-of-tree modules can be loaded"):
65+ machine.succeed("grep -Fq wireguard /proc/modules")
66+
6667
6768 # Test hidepid
68− subtest "hidepid", sub {
69− $machine->succeed("grep -Fq hidepid=2 /proc/mounts");
69+ with subtest("hidepid=2 option is applied and works"):
70+ machine.succeed("grep -Fq hidepid=2 /proc/mounts")
7071 # cannot use pgrep -u here, it segfaults when access to process info is denied
71− $machine->succeed("[ `su - sybil -c 'ps --no-headers --user root | wc -l'` = 0 ]");
72− $machine->succeed("[ `su - alice -c 'ps --no-headers --user root | wc -l'` != 0 ]");
73− };
72+ machine.succeed("[ `su - sybil -c 'ps --no-headers --user root | wc -l'` = 0 ]")
73+ machine.succeed("[ `su - alice -c 'ps --no-headers --user root | wc -l'` != 0 ]")
74+
7475
7576 # Test kernel module hardening
76− subtest "lock-modules", sub {
77+ with subtest("No more kernel modules can be loaded"):
7778 # note: this better a be module we normally wouldn't load ...
78− $machine->fail("modprobe dccp");
79− };
79+ machine.fail("modprobe dccp")
80+
8081
8182 # Test userns
82− subtest "userns", sub {
83− $machine->succeed("unshare --user true");
84− $machine->fail("su -l alice -c 'unshare --user true'");
85− };
83+ with subtest("User namespaces are restricted"):
84+ machine.succeed("unshare --user true")
85+ machine.fail("su -l alice -c 'unshare --user true'")
86+
8687
8788 # Test dmesg restriction
88− subtest "dmesg", sub {
89− $machine->fail("su -l alice -c dmesg");
90− };
89+ with subtest("Regular users cannot access dmesg"):
90+ machine.fail("su -l alice -c dmesg")
91+
9192
9293 # Test access to kcore
93− subtest "kcore", sub {
94− $machine->fail("cat /proc/kcore");
95− };
94+ with subtest("Kcore is inaccessible as root"):
95+ machine.fail("cat /proc/kcore")
96+
9697
9798 # Test deferred mount
98− subtest "mount", sub {
99− $machine->fail("mountpoint -q /efi"); # was deferred
100− $machine->execute("mkdir -p /efi");
101− $machine->succeed("mount /dev/disk/by-label/EFISYS /efi");
102− $machine->succeed("mountpoint -q /efi"); # now mounted
103− };
99+ with subtest("Deferred mounts work"):
100+ machine.fail("mountpoint -q /efi") # was deferred
101+ machine.execute("mkdir -p /efi")
102+ machine.succeed("mount /dev/disk/by-label/EFISYS /efi")
103+ machine.succeed("mountpoint -q /efi") # now mounted
104+
104105
105106 # Test Nix dæmon usage
106− subtest "nix-daemon", sub {
107− $machine->fail("su -l nobody -s /bin/sh -c 'nix ping-store'");
108− $machine->succeed("su -l alice -c 'nix ping-store'") =~ "OK";
109− };
107+ with subtest("nix-daemon cannot be used by all users"):
108+ machine.fail("su -l nobody -s /bin/sh -c 'nix ping-store'")
109+ machine.succeed("su -l alice -c 'nix ping-store'")
110+
110111
111112 # Test kernel image protection
112− subtest "kernelimage", sub {
113− $machine->fail("systemctl hibernate");
114− $machine->fail("systemctl kexec");
115− };
113+ with subtest("The kernel image is protected"):
114+ machine.fail("systemctl hibernate")
115+ machine.fail("systemctl kexec")
116116
117− # Test hardened memory allocator
118− sub runMallocTestProg {
119− my ($progName, $errorText) = @_;
120− my $text = "fatal allocator error: " . $errorText;
121− $machine->fail("${hardened-malloc-tests}/bin/" . $progName) =~ $text;
122− };
123117
124− subtest "hardenedmalloc", sub {
125− runMallocTestProg("double_free_large", "invalid free");
126− runMallocTestProg("unaligned_free_small", "invalid unaligned free");
127− runMallocTestProg("write_after_free_small", "detected write after free");
128− };
118+ # Test hardened memory allocator
119+ def runMallocTestProg(prog_name, error_text):
120+ text = "fatal allocator error: " + error_text
121+ if not text in machine.fail(
122+ "${hardened-malloc-tests}/bin/"
123+ + prog_name
124+ + " 2>&1"
125+ ):
126+ raise Exception("Hardened malloc does not work for {}".format(error_text))
127+
128+
129+ with subtest("The hardened memory allocator works"):
130+ runMallocTestProg("double_free_large", "invalid free")
131+ runMallocTestProg("unaligned_free_small", "invalid unaligned free")
132+ runMallocTestProg("write_after_free_small", "detected write after free")
129133 '';
130134})