1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
15 #include <sys/mount.h>
17 #include <sys/types.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 <sys/random.h>
25 #include <linux/random.h>
26 #include <linux/version.h>
28 __attribute__((noreturn)) static void poweroff(void)
34 fprintf(stderr, "\x1b[37m\x1b[41m\x1b[1mFailed to power off!!!\x1b[0m\n");
38 static void panic(const char *what)
40 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));
44 #define pretty_message(msg) puts("\x1b[32m\x1b[1m" msg "\x1b[0m")
46 static void print_banner(void)
48 struct utsname utsname;
51 if (uname(&utsname) < 0)
54 len = strlen(" WireGuard Test Suite on ") + strlen(utsname.sysname) + strlen(utsname.release) + strlen(utsname.machine);
55 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, "");
58 static void seed_rng(void)
62 if (!getrandom(NULL, 0, GRND_NONBLOCK))
64 pretty_message("[+] Fake seeding RNG...");
65 fd = open("/dev/random", O_WRONLY);
67 panic("open(random)");
68 if (ioctl(fd, RNDADDTOENTCNT, &bits) < 0)
69 panic("ioctl(RNDADDTOENTCNT)");
73 static void mount_filesystems(void)
75 pretty_message("[+] Mounting filesystems...");
82 if (mount("none", "/dev", "devtmpfs", 0, NULL))
83 panic("devtmpfs mount");
84 if (mount("none", "/proc", "proc", 0, NULL))
85 panic("procfs mount");
86 if (mount("none", "/sys", "sysfs", 0, NULL))
88 if (mount("none", "/tmp", "tmpfs", 0, NULL))
90 if (mount("none", "/run", "tmpfs", 0, NULL))
92 if (mount("none", "/sys/kernel/debug", "debugfs", 0, NULL))
93 ; /* Not a problem if it fails.*/
94 if (symlink("/run", "/var/run"))
96 if (symlink("/proc/self/fd", "/dev/fd"))
100 static void enable_logging(void)
103 pretty_message("[+] Enabling logging...");
104 fd = open("/proc/sys/kernel/printk", O_WRONLY);
106 if (write(fd, "9\n", 2) != 2)
107 panic("write(printk)");
110 fd = open("/proc/sys/debug/exception-trace", O_WRONLY);
112 if (write(fd, "1\n", 2) != 2)
113 panic("write(exception-trace)");
118 static void kmod_selftests(void)
121 char line[2048], *start, *pass;
123 pretty_message("[+] Module self-tests:");
124 file = fopen("/proc/kmsg", "r");
126 panic("fopen(kmsg)");
127 if (fcntl(fileno(file), F_SETFL, O_NONBLOCK) < 0)
128 panic("fcntl(kmsg, nonblock)");
129 while (fgets(line, sizeof(line), file)) {
130 start = strstr(line, "wireguard: ");
134 *strchrnul(start, '\n') = '\0';
135 if (strstr(start, "www.wireguard.com"))
137 pass = strstr(start, ": pass");
138 if (!pass || pass[6] != '\0') {
140 printf(" \x1b[31m* %s\x1b[0m\n", start);
142 printf(" \x1b[32m* %s\x1b[0m\n", start);
146 puts("\x1b[31m\x1b[1m[-] Tests failed! \u2639\x1b[0m");
151 static void launch_tests(void)
153 char cmdline[4096], *success_dev;
157 pretty_message("[+] Launching tests...");
162 execl("/init.sh", "init", NULL);
165 if (waitpid(pid, &status, 0) < 0)
167 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
168 pretty_message("[+] Tests successful! :-)");
169 fd = open("/proc/cmdline", O_RDONLY);
171 panic("open(/proc/cmdline)");
172 if (read(fd, cmdline, sizeof(cmdline) - 1) <= 0)
173 panic("read(/proc/cmdline)");
174 cmdline[sizeof(cmdline) - 1] = '\0';
175 for (success_dev = strtok(cmdline, " \n"); success_dev; success_dev = strtok(NULL, " \n")) {
176 if (strncmp(success_dev, "wg.success=", 11))
178 memcpy(success_dev + 11 - 5, "/dev/", 5);
179 success_dev += 11 - 5;
182 if (!success_dev || !strlen(success_dev))
183 panic("Unable to find success device");
185 fd = open(success_dev, O_WRONLY);
187 panic("open(success_dev)");
188 if (write(fd, "success\n", 8) != 8)
189 panic("write(success_dev)");
192 const char *why = "unknown cause";
195 if (WIFEXITED(status)) {
197 what = WEXITSTATUS(status);
198 } else if (WIFSIGNALED(status)) {
200 what = WTERMSIG(status);
202 printf("\x1b[31m\x1b[1m[-] Tests failed with %s %d! \u2639\x1b[0m\n", why, what);
206 static void ensure_console(void)
208 for (unsigned int i = 0; i < 1000; ++i) {
209 int fd = open("/dev/console", O_RDWR);
218 if (write(1, "\0\0\0\0\n", 5) == 5)
221 panic("Unable to open console device");
224 static void clear_leaks(void)
228 fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
231 pretty_message("[+] Starting memory leak detection...");
232 write(fd, "clear\n", 5);
236 static void check_leaks(void)
240 fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
243 pretty_message("[+] Scanning for memory leaks...");
244 sleep(2); /* Wait for any grace periods. */
245 write(fd, "scan\n", 5);
248 fd = open("/sys/kernel/debug/kmemleak", O_RDONLY);
251 if (sendfile(1, fd, NULL, 0x7ffff000) > 0)
252 panic("Memory leaks encountered");
256 int main(int argc, char *argv[])