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 } 114 115 static void kmod_selftests(void) 116 { 117 FILE *file; 118 char line[2048], *start, *pass; 119 bool success = true; 120 pretty_message("[+] Module self-tests:"); 121 file = fopen("/proc/kmsg", "r"); 122 if (!file) 123 panic("fopen(kmsg)"); 124 if (fcntl(fileno(file), F_SETFL, O_NONBLOCK) < 0) 125 panic("fcntl(kmsg, nonblock)"); 126 while (fgets(line, sizeof(line), file)) { 127 start = strstr(line, "wireguard: "); 128 if (!start) 129 continue; 130 start += 11; 131 *strchrnul(start, '\n') = '\0'; 132 if (strstr(start, "www.wireguard.com")) 133 break; 134 pass = strstr(start, ": pass"); 135 if (!pass || pass[6] != '\0') { 136 success = false; 137 printf(" \x1b[31m* %s\x1b[0m\n", start); 138 } else 139 printf(" \x1b[32m* %s\x1b[0m\n", start); 140 } 141 fclose(file); 142 if (!success) { 143 puts("\x1b[31m\x1b[1m[-] Tests failed! \u2639\x1b[0m"); 144 poweroff(); 145 } 146 } 147 148 static void launch_tests(void) 149 { 150 char cmdline[4096], *success_dev; 151 int status, fd; 152 pid_t pid; 153 154 pretty_message("[+] Launching tests..."); 155 pid = fork(); 156 if (pid == -1) 157 panic("fork"); 158 else if (pid == 0) { 159 execl("/init.sh", "init", NULL); 160 panic("exec"); 161 } 162 if (waitpid(pid, &status, 0) < 0) 163 panic("waitpid"); 164 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { 165 pretty_message("[+] Tests successful! :-)"); 166 fd = open("/proc/cmdline", O_RDONLY); 167 if (fd < 0) 168 panic("open(/proc/cmdline)"); 169 if (read(fd, cmdline, sizeof(cmdline) - 1) <= 0) 170 panic("read(/proc/cmdline)"); 171 cmdline[sizeof(cmdline) - 1] = '\0'; 172 for (success_dev = strtok(cmdline, " \n"); success_dev; success_dev = strtok(NULL, " \n")) { 173 if (strncmp(success_dev, "wg.success=", 11)) 174 continue; 175 memcpy(success_dev + 11 - 5, "/dev/", 5); 176 success_dev += 11 - 5; 177 break; 178 } 179 if (!success_dev || !strlen(success_dev)) 180 panic("Unable to find success device"); 181 182 fd = open(success_dev, O_WRONLY); 183 if (fd < 0) 184 panic("open(success_dev)"); 185 if (write(fd, "success\n", 8) != 8) 186 panic("write(success_dev)"); 187 close(fd); 188 } else { 189 const char *why = "unknown cause"; 190 int what = -1; 191 192 if (WIFEXITED(status)) { 193 why = "exit code"; 194 what = WEXITSTATUS(status); 195 } else if (WIFSIGNALED(status)) { 196 why = "signal"; 197 what = WTERMSIG(status); 198 } 199 printf("\x1b[31m\x1b[1m[-] Tests failed with %s %d! \u2639\x1b[0m\n", why, what); 200 } 201 } 202 203 static void ensure_console(void) 204 { 205 for (unsigned int i = 0; i < 1000; ++i) { 206 int fd = open("/dev/console", O_RDWR); 207 if (fd < 0) { 208 usleep(50000); 209 continue; 210 } 211 dup2(fd, 0); 212 dup2(fd, 1); 213 dup2(fd, 2); 214 close(fd); 215 if (write(1, "\0\0\0\0\n", 5) == 5) 216 return; 217 } 218 panic("Unable to open console device"); 219 } 220 221 static void clear_leaks(void) 222 { 223 int fd; 224 225 fd = open("/sys/kernel/debug/kmemleak", O_WRONLY); 226 if (fd < 0) 227 return; 228 pretty_message("[+] Starting memory leak detection..."); 229 write(fd, "clear\n", 5); 230 close(fd); 231 } 232 233 static void check_leaks(void) 234 { 235 int fd; 236 237 fd = open("/sys/kernel/debug/kmemleak", O_WRONLY); 238 if (fd < 0) 239 return; 240 pretty_message("[+] Scanning for memory leaks..."); 241 sleep(2); /* Wait for any grace periods. */ 242 write(fd, "scan\n", 5); 243 close(fd); 244 245 fd = open("/sys/kernel/debug/kmemleak", O_RDONLY); 246 if (fd < 0) 247 return; 248 if (sendfile(1, fd, NULL, 0x7ffff000) > 0) 249 panic("Memory leaks encountered"); 250 close(fd); 251 } 252 253 int main(int argc, char *argv[]) 254 { 255 ensure_console(); 256 print_banner(); 257 mount_filesystems(); 258 seed_rng(); 259 kmod_selftests(); 260 enable_logging(); 261 clear_leaks(); 262 launch_tests(); 263 check_leaks(); 264 poweroff(); 265 return 1; 266 } 267