init.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
  4. */
  5. #define _GNU_SOURCE
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdbool.h>
  12. #include <fcntl.h>
  13. #include <time.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 <sys/random.h>
  25. #include <linux/random.h>
  26. #include <linux/version.h>
  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. static void panic(const char *what)
  37. {
  38. 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));
  39. poweroff();
  40. }
  41. #define pretty_message(msg) puts("\x1b[32m\x1b[1m" msg "\x1b[0m")
  42. static void print_banner(void)
  43. {
  44. struct utsname utsname;
  45. int len;
  46. if (uname(&utsname) < 0)
  47. panic("uname");
  48. len = strlen(" WireGuard Test Suite on ") + strlen(utsname.sysname) + strlen(utsname.release) + strlen(utsname.machine);
  49. 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, "");
  50. }
  51. static void seed_rng(void)
  52. {
  53. int bits = 256, fd;
  54. if (!getrandom(NULL, 0, GRND_NONBLOCK))
  55. return;
  56. pretty_message("[+] Fake seeding RNG...");
  57. fd = open("/dev/random", O_WRONLY);
  58. if (fd < 0)
  59. panic("open(random)");
  60. if (ioctl(fd, RNDADDTOENTCNT, &bits) < 0)
  61. panic("ioctl(RNDADDTOENTCNT)");
  62. close(fd);
  63. }
  64. static void set_time(void)
  65. {
  66. if (time(NULL))
  67. return;
  68. pretty_message("[+] Setting fake time...");
  69. if (stime(&(time_t){1433512680}) < 0)
  70. panic("settimeofday()");
  71. }
  72. static void mount_filesystems(void)
  73. {
  74. pretty_message("[+] Mounting filesystems...");
  75. mkdir("/dev", 0755);
  76. mkdir("/proc", 0755);
  77. mkdir("/sys", 0755);
  78. mkdir("/tmp", 0755);
  79. mkdir("/run", 0755);
  80. mkdir("/var", 0755);
  81. if (mount("none", "/dev", "devtmpfs", 0, NULL))
  82. panic("devtmpfs mount");
  83. if (mount("none", "/proc", "proc", 0, NULL))
  84. panic("procfs mount");
  85. if (mount("none", "/sys", "sysfs", 0, NULL))
  86. panic("sysfs mount");
  87. if (mount("none", "/tmp", "tmpfs", 0, NULL))
  88. panic("tmpfs mount");
  89. if (mount("none", "/run", "tmpfs", 0, NULL))
  90. panic("tmpfs mount");
  91. if (mount("none", "/sys/kernel/debug", "debugfs", 0, NULL))
  92. ; /* Not a problem if it fails.*/
  93. if (symlink("/run", "/var/run"))
  94. panic("run symlink");
  95. if (symlink("/proc/self/fd", "/dev/fd"))
  96. panic("fd symlink");
  97. }
  98. static void enable_logging(void)
  99. {
  100. int fd;
  101. pretty_message("[+] Enabling logging...");
  102. fd = open("/proc/sys/kernel/printk", O_WRONLY);
  103. if (fd >= 0) {
  104. if (write(fd, "9\n", 2) != 2)
  105. panic("write(printk)");
  106. close(fd);
  107. }
  108. fd = open("/proc/sys/debug/exception-trace", O_WRONLY);
  109. if (fd >= 0) {
  110. if (write(fd, "1\n", 2) != 2)
  111. panic("write(exception-trace)");
  112. close(fd);
  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. static void launch_tests(void)
  148. {
  149. char cmdline[4096], *success_dev;
  150. int status, fd;
  151. pid_t pid;
  152. pretty_message("[+] Launching tests...");
  153. pid = fork();
  154. if (pid == -1)
  155. panic("fork");
  156. else if (pid == 0) {
  157. execl("/init.sh", "init", NULL);
  158. panic("exec");
  159. }
  160. if (waitpid(pid, &status, 0) < 0)
  161. panic("waitpid");
  162. if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
  163. pretty_message("[+] Tests successful! :-)");
  164. fd = open("/proc/cmdline", O_RDONLY);
  165. if (fd < 0)
  166. panic("open(/proc/cmdline)");
  167. if (read(fd, cmdline, sizeof(cmdline) - 1) <= 0)
  168. panic("read(/proc/cmdline)");
  169. cmdline[sizeof(cmdline) - 1] = '\0';
  170. for (success_dev = strtok(cmdline, " \n"); success_dev; success_dev = strtok(NULL, " \n")) {
  171. if (strncmp(success_dev, "wg.success=", 11))
  172. continue;
  173. memcpy(success_dev + 11 - 5, "/dev/", 5);
  174. success_dev += 11 - 5;
  175. break;
  176. }
  177. if (!success_dev || !strlen(success_dev))
  178. panic("Unable to find success device");
  179. fd = open(success_dev, O_WRONLY);
  180. if (fd < 0)
  181. panic("open(success_dev)");
  182. if (write(fd, "success\n", 8) != 8)
  183. panic("write(success_dev)");
  184. close(fd);
  185. } else {
  186. const char *why = "unknown cause";
  187. int what = -1;
  188. if (WIFEXITED(status)) {
  189. why = "exit code";
  190. what = WEXITSTATUS(status);
  191. } else if (WIFSIGNALED(status)) {
  192. why = "signal";
  193. what = WTERMSIG(status);
  194. }
  195. printf("\x1b[31m\x1b[1m[-] Tests failed with %s %d! \u2639\x1b[0m\n", why, what);
  196. }
  197. }
  198. static void ensure_console(void)
  199. {
  200. for (unsigned int i = 0; i < 1000; ++i) {
  201. int fd = open("/dev/console", O_RDWR);
  202. if (fd < 0) {
  203. usleep(50000);
  204. continue;
  205. }
  206. dup2(fd, 0);
  207. dup2(fd, 1);
  208. dup2(fd, 2);
  209. close(fd);
  210. if (write(1, "\0\0\0\0\n", 5) == 5)
  211. return;
  212. }
  213. panic("Unable to open console device");
  214. }
  215. static void clear_leaks(void)
  216. {
  217. int fd;
  218. fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
  219. if (fd < 0)
  220. return;
  221. pretty_message("[+] Starting memory leak detection...");
  222. write(fd, "clear\n", 5);
  223. close(fd);
  224. }
  225. static void check_leaks(void)
  226. {
  227. int fd;
  228. fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
  229. if (fd < 0)
  230. return;
  231. pretty_message("[+] Scanning for memory leaks...");
  232. sleep(2); /* Wait for any grace periods. */
  233. write(fd, "scan\n", 5);
  234. close(fd);
  235. fd = open("/sys/kernel/debug/kmemleak", O_RDONLY);
  236. if (fd < 0)
  237. return;
  238. if (sendfile(1, fd, NULL, 0x7ffff000) > 0)
  239. panic("Memory leaks encountered");
  240. close(fd);
  241. }
  242. int main(int argc, char *argv[])
  243. {
  244. ensure_console();
  245. print_banner();
  246. mount_filesystems();
  247. seed_rng();
  248. set_time();
  249. kmod_selftests();
  250. enable_logging();
  251. clear_leaks();
  252. launch_tests();
  253. check_leaks();
  254. poweroff();
  255. return 1;
  256. }