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 <linux/random.h>
25 #include <linux/version.h>
27 __attribute__((noreturn)) static void poweroff(void)
33 fprintf(stderr, "\x1b[37m\x1b[41m\x1b[1mFailed to power off!!!\x1b[0m\n");
37 static void panic(const char *what)
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));
43 #define pretty_message(msg) puts("\x1b[32m\x1b[1m" msg "\x1b[0m")
45 static void print_banner(void)
47 struct utsname utsname;
50 if (uname(&utsname) < 0)
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, "");
57 static void seed_rng(void)
61 pretty_message("[+] Fake seeding RNG...");
62 fd = open("/dev/random", O_WRONLY);
64 panic("open(random)");
65 if (ioctl(fd, RNDADDTOENTCNT, &bits) < 0)
66 panic("ioctl(RNDADDTOENTCNT)");
70 static void mount_filesystems(void)
72 pretty_message("[+] Mounting filesystems...");
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))
85 if (mount("none", "/tmp", "tmpfs", 0, NULL))
87 if (mount("none", "/run", "tmpfs", 0, NULL))
89 if (mount("none", "/sys/kernel/debug", "debugfs", 0, NULL))
90 ; /* Not a problem if it fails.*/
91 if (symlink("/run", "/var/run"))
93 if (symlink("/proc/self/fd", "/dev/fd"))
97 static void enable_logging(void)
100 pretty_message("[+] Enabling logging...");
101 fd = open("/proc/sys/kernel/printk", O_WRONLY);
103 if (write(fd, "9\n", 2) != 2)
104 panic("write(printk)");
107 fd = open("/proc/sys/debug/exception-trace", O_WRONLY);
109 if (write(fd, "1\n", 2) != 2)
110 panic("write(exception-trace)");
113 fd = open("/proc/sys/kernel/panic_on_warn", O_WRONLY);
115 if (write(fd, "1\n", 2) != 2)
116 panic("write(panic_on_warn)");
121 static void kmod_selftests(void)
124 char line[2048], *start, *pass;
126 pretty_message("[+] Module self-tests:");
127 file = fopen("/proc/kmsg", "r");
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: ");
137 *strchrnul(start, '\n') = '\0';
138 if (strstr(start, "www.wireguard.com"))
140 pass = strstr(start, ": pass");
141 if (!pass || pass[6] != '\0') {
143 printf(" \x1b[31m* %s\x1b[0m\n", start);
145 printf(" \x1b[32m* %s\x1b[0m\n", start);
149 puts("\x1b[31m\x1b[1m[-] Tests failed! \u2639\x1b[0m");
154 static void launch_tests(void)
156 char cmdline[4096], *success_dev;
160 pretty_message("[+] Launching tests...");
165 execl("/init.sh", "init", NULL);
168 if (waitpid(pid, &status, 0) < 0)
170 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
171 pretty_message("[+] Tests successful! :-)");
172 fd = open("/proc/cmdline", O_RDONLY);
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))
181 memcpy(success_dev + 11 - 5, "/dev/", 5);
182 success_dev += 11 - 5;
185 if (!success_dev || !strlen(success_dev))
186 panic("Unable to find success device");
188 fd = open(success_dev, O_WRONLY);
190 panic("open(success_dev)");
191 if (write(fd, "success\n", 8) != 8)
192 panic("write(success_dev)");
195 const char *why = "unknown cause";
198 if (WIFEXITED(status)) {
200 what = WEXITSTATUS(status);
201 } else if (WIFSIGNALED(status)) {
203 what = WTERMSIG(status);
205 printf("\x1b[31m\x1b[1m[-] Tests failed with %s %d! \u2639\x1b[0m\n", why, what);
209 static void ensure_console(void)
211 for (unsigned int i = 0; i < 1000; ++i) {
212 int fd = open("/dev/console", O_RDWR);
221 if (write(1, "\0\0\0\0\n", 5) == 5)
224 panic("Unable to open console device");
227 static void clear_leaks(void)
231 fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
234 pretty_message("[+] Starting memory leak detection...");
235 write(fd, "clear\n", 5);
239 static void check_leaks(void)
243 fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
246 pretty_message("[+] Scanning for memory leaks...");
247 sleep(2); /* Wait for any grace periods. */
248 write(fd, "scan\n", 5);
251 fd = open("/sys/kernel/debug/kmemleak", O_RDONLY);
254 if (sendfile(1, fd, NULL, 0x7ffff000) > 0)
255 panic("Memory leaks encountered");
259 int main(int argc, char *argv[])