1 import ./make-test-python.nix ({ pkgs, ... }: {
4 nodes.machine = { config, pkgs, lib, ... }: {
5 boot.extraModulePackages = let
6 compileKernelModule = name: source: pkgs.runCommandCC name rec {
8 kdev = config.boot.kernelPackages.kernel.dev;
9 kver = config.boot.kernelPackages.kernel.modDirVersion;
10 ksrc = "${kdev}/lib/modules/${kver}/build";
11 hardeningDisable = [ "pic" ];
12 nativeBuildInputs = kdev.moduleBuildDependencies;
14 echo "obj-m += $name.o" > Makefile
15 echo "$source" > "$name.c"
16 make -C "$ksrc" M=$(pwd) modules
17 install -vD "$name.ko" "$out/lib/modules/$kver/$name.ko"
20 # This spawns a kthread which just waits until it gets a signal and
21 # terminates if that is the case. We want to make sure that nothing during
22 # the boot process kills any kthread by accident, like what happened in
24 kcanary = compileKernelModule "kcanary" ''
25 #include <linux/version.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/kthread.h>
30 #include <linux/sched.h>
31 #include <linux/signal.h>
32 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)
33 #include <linux/sched/signal.h>
36 MODULE_LICENSE("GPL");
38 struct task_struct *canaryTask;
40 static int kcanary(void *nothing)
43 allow_signal(SIGTERM);
44 allow_signal(SIGKILL);
45 while (!kthread_should_stop()) {
46 set_current_state(TASK_INTERRUPTIBLE);
47 schedule_timeout_interruptible(msecs_to_jiffies(100));
48 if (signal_pending(current)) break;
53 static int kcanaryInit(void)
55 kthread_run(&kcanary, NULL, "kcanary");
59 static void kcanaryExit(void)
61 kthread_stop(canaryTask);
64 module_init(kcanaryInit);
65 module_exit(kcanaryExit);
68 in lib.singleton kcanary;
70 boot.initrd.kernelModules = [ "kcanary" ];
72 boot.initrd.extraUtilsCommands = let
73 compile = name: source: pkgs.runCommandCC name { inherit source; } ''
75 echo "$source" | gcc -Wall -o "$out/bin/$name" -xc -
78 daemonize = name: source: compile name ''
82 void runSource(void) {
87 if (fork() > 0) return 0;
94 mkCmdlineCanary = { name, cmdline ? "", source ? "" }: (daemonize name ''
95 char *argv[] = {"${cmdline}", NULL};
96 execvp("${name}-child", argv);
98 child = compile "${name}-child" ''
110 copyCanaries = with lib; concatMapStrings (canary: ''
111 ${optionalString (canary ? child) ''
112 copy_bin_and_libs "${canary.child}/bin/${canary.child.name}"
114 copy_bin_and_libs "${canary}/bin/${canary.name}"
118 # Simple canary process which just sleeps forever and should be killed by
120 (daemonize "canary1" "while (1) sleep(1);")
122 # We want this canary process to try mimicking a kthread using a cmdline
123 # with a zero length so we can make sure that the process is properly
129 f = fopen("/run/canary2.pid", "w");
130 fprintf(f, "%d\n", getpid());
135 # This canary process mimicks a storage daemon, which we do NOT want to be
136 # killed before going into stage 2. For more on root storage daemons, see:
137 # https://www.freedesktop.org/wiki/Software/systemd/RootStorageDaemons/
140 cmdline = "@canary3";
144 boot.initrd.postMountCommands = ''
148 # Make sure the pidfile of canary 2 is created so that we still can get
149 # its former pid after the killing spree starts next within stage 1.
150 while [ ! -s /run/canary2.pid ]; do sleep 0.1; done
155 machine.wait_for_unit("multi-user.target")
156 machine.succeed("test -s /run/canary2.pid")
157 machine.fail("pgrep -a canary1")
158 machine.fail("kill -0 $(< /run/canary2.pid)")
159 machine.succeed('pgrep -a -f "^@canary3$"')
160 machine.succeed('pgrep -a -f "^kcanary$"')
163 meta.maintainers = with pkgs.lib.maintainers; [ aszlig ];