wireguard: selftests: set fake real time in init
[platform/kernel/linux-rpi.git] / tools / testing / selftests / wireguard / qemu / init.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  */
5
6 #define _GNU_SOURCE
7 #include <unistd.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdbool.h>
13 #include <fcntl.h>
14 #include <time.h>
15 #include <sys/wait.h>
16 #include <sys/mount.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <sys/io.h>
20 #include <sys/ioctl.h>
21 #include <sys/reboot.h>
22 #include <sys/utsname.h>
23 #include <sys/sendfile.h>
24 #include <sys/sysmacros.h>
25 #include <sys/random.h>
26 #include <linux/random.h>
27 #include <linux/version.h>
28
29 __attribute__((noreturn)) static void poweroff(void)
30 {
31         fflush(stdout);
32         fflush(stderr);
33         reboot(RB_AUTOBOOT);
34         sleep(30);
35         fprintf(stderr, "\x1b[37m\x1b[41m\x1b[1mFailed to power off!!!\x1b[0m\n");
36         exit(1);
37 }
38
39 static void panic(const char *what)
40 {
41         fprintf(stderr, "\n\n\x1b[37m\x1b[41m\x1b[1mSOMETHING WENT HORRIBLY WRONG\x1b[0m\n\n    \x1b[31m\x1b[1m%s: %s\x1b[0m\n\n\x1b[37m\x1b[44m\x1b[1mPower off...\x1b[0m\n\n", what, strerror(errno));
42         poweroff();
43 }
44
45 #define pretty_message(msg) puts("\x1b[32m\x1b[1m" msg "\x1b[0m")
46
47 static void print_banner(void)
48 {
49         struct utsname utsname;
50         int len;
51
52         if (uname(&utsname) < 0)
53                 panic("uname");
54
55         len = strlen("    WireGuard Test Suite on       ") + strlen(utsname.sysname) + strlen(utsname.release) + strlen(utsname.machine);
56         printf("\x1b[45m\x1b[33m\x1b[1m%*.s\x1b[0m\n\x1b[45m\x1b[33m\x1b[1m    WireGuard Test Suite on %s %s %s    \x1b[0m\n\x1b[45m\x1b[33m\x1b[1m%*.s\x1b[0m\n\n", len, "", utsname.sysname, utsname.release, utsname.machine, len, "");
57 }
58
59 static void seed_rng(void)
60 {
61         int bits = 256, fd;
62
63         if (!getrandom(NULL, 0, GRND_NONBLOCK))
64                 return;
65         pretty_message("[+] Fake seeding RNG...");
66         fd = open("/dev/random", O_WRONLY);
67         if (fd < 0)
68                 panic("open(random)");
69         if (ioctl(fd, RNDADDTOENTCNT, &bits) < 0)
70                 panic("ioctl(RNDADDTOENTCNT)");
71         close(fd);
72 }
73
74 static void set_time(void)
75 {
76         if (time(NULL))
77                 return;
78         pretty_message("[+] Setting fake time...");
79         if (stime(&(time_t){1433512680}) < 0)
80                 panic("settimeofday()");
81 }
82
83 static void mount_filesystems(void)
84 {
85         pretty_message("[+] Mounting filesystems...");
86         mkdir("/dev", 0755);
87         mkdir("/proc", 0755);
88         mkdir("/sys", 0755);
89         mkdir("/tmp", 0755);
90         mkdir("/run", 0755);
91         mkdir("/var", 0755);
92         if (mount("none", "/dev", "devtmpfs", 0, NULL))
93                 panic("devtmpfs mount");
94         if (mount("none", "/proc", "proc", 0, NULL))
95                 panic("procfs mount");
96         if (mount("none", "/sys", "sysfs", 0, NULL))
97                 panic("sysfs mount");
98         if (mount("none", "/tmp", "tmpfs", 0, NULL))
99                 panic("tmpfs mount");
100         if (mount("none", "/run", "tmpfs", 0, NULL))
101                 panic("tmpfs mount");
102         if (mount("none", "/sys/kernel/debug", "debugfs", 0, NULL))
103                 ; /* Not a problem if it fails.*/
104         if (symlink("/run", "/var/run"))
105                 panic("run symlink");
106         if (symlink("/proc/self/fd", "/dev/fd"))
107                 panic("fd symlink");
108 }
109
110 static void enable_logging(void)
111 {
112         int fd;
113         pretty_message("[+] Enabling logging...");
114         fd = open("/proc/sys/kernel/printk", O_WRONLY);
115         if (fd >= 0) {
116                 if (write(fd, "9\n", 2) != 2)
117                         panic("write(printk)");
118                 close(fd);
119         }
120         fd = open("/proc/sys/debug/exception-trace", O_WRONLY);
121         if (fd >= 0) {
122                 if (write(fd, "1\n", 2) != 2)
123                         panic("write(exception-trace)");
124                 close(fd);
125         }
126 }
127
128 static void kmod_selftests(void)
129 {
130         FILE *file;
131         char line[2048], *start, *pass;
132         bool success = true;
133         pretty_message("[+] Module self-tests:");
134         file = fopen("/proc/kmsg", "r");
135         if (!file)
136                 panic("fopen(kmsg)");
137         if (fcntl(fileno(file), F_SETFL, O_NONBLOCK) < 0)
138                 panic("fcntl(kmsg, nonblock)");
139         while (fgets(line, sizeof(line), file)) {
140                 start = strstr(line, "wireguard: ");
141                 if (!start)
142                         continue;
143                 start += 11;
144                 *strchrnul(start, '\n') = '\0';
145                 if (strstr(start, "www.wireguard.com"))
146                         break;
147                 pass = strstr(start, ": pass");
148                 if (!pass || pass[6] != '\0') {
149                         success = false;
150                         printf(" \x1b[31m*  %s\x1b[0m\n", start);
151                 } else
152                         printf(" \x1b[32m*  %s\x1b[0m\n", start);
153         }
154         fclose(file);
155         if (!success) {
156                 puts("\x1b[31m\x1b[1m[-] Tests failed! \u2639\x1b[0m");
157                 poweroff();
158         }
159 }
160
161 static void launch_tests(void)
162 {
163         char cmdline[4096], *success_dev;
164         int status, fd;
165         pid_t pid;
166
167         pretty_message("[+] Launching tests...");
168         pid = fork();
169         if (pid == -1)
170                 panic("fork");
171         else if (pid == 0) {
172                 execl("/init.sh", "init", NULL);
173                 panic("exec");
174         }
175         if (waitpid(pid, &status, 0) < 0)
176                 panic("waitpid");
177         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
178                 pretty_message("[+] Tests successful! :-)");
179                 fd = open("/proc/cmdline", O_RDONLY);
180                 if (fd < 0)
181                         panic("open(/proc/cmdline)");
182                 if (read(fd, cmdline, sizeof(cmdline) - 1) <= 0)
183                         panic("read(/proc/cmdline)");
184                 cmdline[sizeof(cmdline) - 1] = '\0';
185                 for (success_dev = strtok(cmdline, " \n"); success_dev; success_dev = strtok(NULL, " \n")) {
186                         if (strncmp(success_dev, "wg.success=", 11))
187                                 continue;
188                         memcpy(success_dev + 11 - 5, "/dev/", 5);
189                         success_dev += 11 - 5;
190                         break;
191                 }
192                 if (!success_dev || !strlen(success_dev))
193                         panic("Unable to find success device");
194
195                 fd = open(success_dev, O_WRONLY);
196                 if (fd < 0)
197                         panic("open(success_dev)");
198                 if (write(fd, "success\n", 8) != 8)
199                         panic("write(success_dev)");
200                 close(fd);
201         } else {
202                 const char *why = "unknown cause";
203                 int what = -1;
204
205                 if (WIFEXITED(status)) {
206                         why = "exit code";
207                         what = WEXITSTATUS(status);
208                 } else if (WIFSIGNALED(status)) {
209                         why = "signal";
210                         what = WTERMSIG(status);
211                 }
212                 printf("\x1b[31m\x1b[1m[-] Tests failed with %s %d! \u2639\x1b[0m\n", why, what);
213         }
214 }
215
216 static void ensure_console(void)
217 {
218         for (unsigned int i = 0; i < 1000; ++i) {
219                 int fd = open("/dev/console", O_RDWR);
220                 if (fd < 0) {
221                         usleep(50000);
222                         continue;
223                 }
224                 dup2(fd, 0);
225                 dup2(fd, 1);
226                 dup2(fd, 2);
227                 close(fd);
228                 if (write(1, "\0\0\0\0\n", 5) == 5)
229                         return;
230         }
231         panic("Unable to open console device");
232 }
233
234 static void clear_leaks(void)
235 {
236         int fd;
237
238         fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
239         if (fd < 0)
240                 return;
241         pretty_message("[+] Starting memory leak detection...");
242         write(fd, "clear\n", 5);
243         close(fd);
244 }
245
246 static void check_leaks(void)
247 {
248         int fd;
249
250         fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
251         if (fd < 0)
252                 return;
253         pretty_message("[+] Scanning for memory leaks...");
254         sleep(2); /* Wait for any grace periods. */
255         write(fd, "scan\n", 5);
256         close(fd);
257
258         fd = open("/sys/kernel/debug/kmemleak", O_RDONLY);
259         if (fd < 0)
260                 return;
261         if (sendfile(1, fd, NULL, 0x7ffff000) > 0)
262                 panic("Memory leaks encountered");
263         close(fd);
264 }
265
266 int main(int argc, char *argv[])
267 {
268         ensure_console();
269         print_banner();
270         mount_filesystems();
271         seed_rng();
272         set_time();
273         kmod_selftests();
274         enable_logging();
275         clear_leaks();
276         launch_tests();
277         check_leaks();
278         poweroff();
279         return 1;
280 }