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