1import ./make-test.nix ({ pkgs, ... }: {
2 name = "boot-stage1";
3
4 machine = { config, pkgs, lib, ... }: {
5 boot.extraModulePackages = let
6 compileKernelModule = name: source: pkgs.runCommandCC name rec {
7 inherit source;
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;
13 } ''
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"
18 '';
19
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
23 # issue #15226.
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>
34 #endif
35
36 struct task_struct *canaryTask;
37
38 static int kcanary(void *nothing)
39 {
40 allow_signal(SIGINT);
41 allow_signal(SIGTERM);
42 allow_signal(SIGKILL);
43 while (!kthread_should_stop()) {
44 set_current_state(TASK_INTERRUPTIBLE);
45 schedule_timeout_interruptible(msecs_to_jiffies(100));
46 if (signal_pending(current)) break;
47 }
48 return 0;
49 }
50
51 static int kcanaryInit(void)
52 {
53 kthread_run(&kcanary, NULL, "kcanary");
54 return 0;
55 }
56
57 static void kcanaryExit(void)
58 {
59 kthread_stop(canaryTask);
60 }
61
62 module_init(kcanaryInit);
63 module_exit(kcanaryExit);
64 '';
65
66 in lib.singleton kcanary;
67
68 boot.initrd.kernelModules = [ "kcanary" ];
69
70 boot.initrd.extraUtilsCommands = let
71 compile = name: source: pkgs.runCommandCC name { inherit source; } ''
72 mkdir -p "$out/bin"
73 echo "$source" | gcc -Wall -o "$out/bin/$name" -xc -
74 '';
75
76 daemonize = name: source: compile name ''
77 #include <stdio.h>
78 #include <unistd.h>
79
80 void runSource(void) {
81 ${source}
82 }
83
84 int main(void) {
85 if (fork() > 0) return 0;
86 setsid();
87 runSource();
88 return 1;
89 }
90 '';
91
92 mkCmdlineCanary = { name, cmdline ? "", source ? "" }: (daemonize name ''
93 char *argv[] = {"${cmdline}", NULL};
94 execvp("${name}-child", argv);
95 '') // {
96 child = compile "${name}-child" ''
97 #include <stdio.h>
98 #include <unistd.h>
99
100 int main(void) {
101 ${source}
102 while (1) sleep(1);
103 return 1;
104 }
105 '';
106 };
107
108 copyCanaries = with lib; concatMapStrings (canary: ''
109 ${optionalString (canary ? child) ''
110 copy_bin_and_libs "${canary.child}/bin/${canary.child.name}"
111 ''}
112 copy_bin_and_libs "${canary}/bin/${canary.name}"
113 '');
114
115 in copyCanaries [
116 # Simple canary process which just sleeps forever and should be killed by
117 # stage 2.
118 (daemonize "canary1" "while (1) sleep(1);")
119
120 # We want this canary process to try mimicking a kthread using a cmdline
121 # with a zero length so we can make sure that the process is properly
122 # killed in stage 1.
123 (mkCmdlineCanary {
124 name = "canary2";
125 source = ''
126 FILE *f;
127 f = fopen("/run/canary2.pid", "w");
128 fprintf(f, "%d\n", getpid());
129 fclose(f);
130 '';
131 })
132
133 # This canary process mimicks a storage daemon, which we do NOT want to be
134 # killed before going into stage 2. For more on root storage daemons, see:
135 # https://www.freedesktop.org/wiki/Software/systemd/RootStorageDaemons/
136 (mkCmdlineCanary {
137 name = "canary3";
138 cmdline = "@canary3";
139 })
140 ];
141
142 boot.initrd.postMountCommands = ''
143 canary1
144 canary2
145 canary3
146 # Make sure the pidfile of canary 2 is created so that we still can get
147 # its former pid after the killing spree starts next within stage 1.
148 while [ ! -s /run/canary2.pid ]; do sleep 0.1; done
149 '';
150 };
151
152 testScript = ''
153 $machine->waitForUnit("multi-user.target");
154 $machine->succeed('test -s /run/canary2.pid');
155 $machine->fail('pgrep -a canary1');
156 $machine->fail('kill -0 $(< /run/canary2.pid)');
157 $machine->succeed('pgrep -a -f \'^@canary3$\''');
158 $machine->succeed('pgrep -a -f \'^kcanary$\''');
159 '';
160
161 meta.maintainers = with pkgs.stdenv.lib.maintainers; [ aszlig ];
162})