1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
16 #include <sys/mount.h>
18 #include <sys/types.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>
29 __attribute__((noreturn)) static void poweroff(void)
35 fprintf(stderr, "\x1b[37m\x1b[41m\x1b[1mFailed to power off!!!\x1b[0m\n");
39 static void panic(const char *what)
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));
45 #define pretty_message(msg) puts("\x1b[32m\x1b[1m" msg "\x1b[0m")
47 static void print_banner(void)
49 struct utsname utsname;
52 if (uname(&utsname) < 0)
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, "");
59 static void seed_rng(void)
63 if (!getrandom(NULL, 0, GRND_NONBLOCK))
65 pretty_message("[+] Fake seeding RNG...");
66 fd = open("/dev/random", O_WRONLY);
68 panic("open(random)");
69 if (ioctl(fd, RNDADDTOENTCNT, &bits) < 0)
70 panic("ioctl(RNDADDTOENTCNT)");
74 static void set_time(void)
78 pretty_message("[+] Setting fake time...");
79 if (stime(&(time_t){1433512680}) < 0)
80 panic("settimeofday()");
83 static void mount_filesystems(void)
85 pretty_message("[+] Mounting filesystems...");
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))
98 if (mount("none", "/tmp", "tmpfs", 0, NULL))
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"))
110 static void enable_logging(void)
113 pretty_message("[+] Enabling logging...");
114 fd = open("/proc/sys/kernel/printk", O_WRONLY);
116 if (write(fd, "9\n", 2) != 2)
117 panic("write(printk)");
120 fd = open("/proc/sys/debug/exception-trace", O_WRONLY);
122 if (write(fd, "1\n", 2) != 2)
123 panic("write(exception-trace)");
128 static void kmod_selftests(void)
131 char line[2048], *start, *pass;
133 pretty_message("[+] Module self-tests:");
134 file = fopen("/proc/kmsg", "r");
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: ");
144 *strchrnul(start, '\n') = '\0';
145 if (strstr(start, "www.wireguard.com"))
147 pass = strstr(start, ": pass");
148 if (!pass || pass[6] != '\0') {
150 printf(" \x1b[31m* %s\x1b[0m\n", start);
152 printf(" \x1b[32m* %s\x1b[0m\n", start);
156 puts("\x1b[31m\x1b[1m[-] Tests failed! \u2639\x1b[0m");
161 static void launch_tests(void)
163 char cmdline[4096], *success_dev;
167 pretty_message("[+] Launching tests...");
172 execl("/init.sh", "init", NULL);
175 if (waitpid(pid, &status, 0) < 0)
177 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
178 pretty_message("[+] Tests successful! :-)");
179 fd = open("/proc/cmdline", O_RDONLY);
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))
188 memcpy(success_dev + 11 - 5, "/dev/", 5);
189 success_dev += 11 - 5;
192 if (!success_dev || !strlen(success_dev))
193 panic("Unable to find success device");
195 fd = open(success_dev, O_WRONLY);
197 panic("open(success_dev)");
198 if (write(fd, "success\n", 8) != 8)
199 panic("write(success_dev)");
202 const char *why = "unknown cause";
205 if (WIFEXITED(status)) {
207 what = WEXITSTATUS(status);
208 } else if (WIFSIGNALED(status)) {
210 what = WTERMSIG(status);
212 printf("\x1b[31m\x1b[1m[-] Tests failed with %s %d! \u2639\x1b[0m\n", why, what);
216 static void ensure_console(void)
218 for (unsigned int i = 0; i < 1000; ++i) {
219 int fd = open("/dev/console", O_RDWR);
228 if (write(1, "\0\0\0\0\n", 5) == 5)
231 panic("Unable to open console device");
234 static void clear_leaks(void)
238 fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
241 pretty_message("[+] Starting memory leak detection...");
242 write(fd, "clear\n", 5);
246 static void check_leaks(void)
250 fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
253 pretty_message("[+] Scanning for memory leaks...");
254 sleep(2); /* Wait for any grace periods. */
255 write(fd, "scan\n", 5);
258 fd = open("/sys/kernel/debug/kmemleak", O_RDONLY);
261 if (sendfile(1, fd, NULL, 0x7ffff000) > 0)
262 panic("Memory leaks encountered");
266 int main(int argc, char *argv[])