Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
0004  */
0005 
0006 #define _GNU_SOURCE
0007 #include <unistd.h>
0008 #include <errno.h>
0009 #include <string.h>
0010 #include <stdio.h>
0011 #include <stdlib.h>
0012 #include <stdbool.h>
0013 #include <fcntl.h>
0014 #include <time.h>
0015 #include <sys/wait.h>
0016 #include <sys/mount.h>
0017 #include <sys/stat.h>
0018 #include <sys/types.h>
0019 #include <sys/io.h>
0020 #include <sys/ioctl.h>
0021 #include <sys/reboot.h>
0022 #include <sys/utsname.h>
0023 #include <sys/sendfile.h>
0024 #include <sys/sysmacros.h>
0025 #include <sys/random.h>
0026 #include <linux/random.h>
0027 #include <linux/version.h>
0028 
0029 __attribute__((noreturn)) static void poweroff(void)
0030 {
0031     fflush(stdout);
0032     fflush(stderr);
0033     reboot(RB_AUTOBOOT);
0034     sleep(30);
0035     fprintf(stderr, "\x1b[37m\x1b[41m\x1b[1mFailed to power off!!!\x1b[0m\n");
0036     exit(1);
0037 }
0038 
0039 static void panic(const char *what)
0040 {
0041     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));
0042     poweroff();
0043 }
0044 
0045 #define pretty_message(msg) puts("\x1b[32m\x1b[1m" msg "\x1b[0m")
0046 
0047 static void print_banner(void)
0048 {
0049     struct utsname utsname;
0050     int len;
0051 
0052     if (uname(&utsname) < 0)
0053         panic("uname");
0054 
0055     len = strlen("    WireGuard Test Suite on       ") + strlen(utsname.sysname) + strlen(utsname.release) + strlen(utsname.machine);
0056     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, "");
0057 }
0058 
0059 static void seed_rng(void)
0060 {
0061     int bits = 256, fd;
0062 
0063     if (!getrandom(NULL, 0, GRND_NONBLOCK))
0064         return;
0065     pretty_message("[+] Fake seeding RNG...");
0066     fd = open("/dev/random", O_WRONLY);
0067     if (fd < 0)
0068         panic("open(random)");
0069     if (ioctl(fd, RNDADDTOENTCNT, &bits) < 0)
0070         panic("ioctl(RNDADDTOENTCNT)");
0071     close(fd);
0072 }
0073 
0074 static void set_time(void)
0075 {
0076     if (time(NULL))
0077         return;
0078     pretty_message("[+] Setting fake time...");
0079     if (stime(&(time_t){1433512680}) < 0)
0080         panic("settimeofday()");
0081 }
0082 
0083 static void mount_filesystems(void)
0084 {
0085     pretty_message("[+] Mounting filesystems...");
0086     mkdir("/dev", 0755);
0087     mkdir("/proc", 0755);
0088     mkdir("/sys", 0755);
0089     mkdir("/tmp", 0755);
0090     mkdir("/run", 0755);
0091     mkdir("/var", 0755);
0092     if (mount("none", "/dev", "devtmpfs", 0, NULL))
0093         panic("devtmpfs mount");
0094     if (mount("none", "/proc", "proc", 0, NULL))
0095         panic("procfs mount");
0096     if (mount("none", "/sys", "sysfs", 0, NULL))
0097         panic("sysfs mount");
0098     if (mount("none", "/tmp", "tmpfs", 0, NULL))
0099         panic("tmpfs mount");
0100     if (mount("none", "/run", "tmpfs", 0, NULL))
0101         panic("tmpfs mount");
0102     if (mount("none", "/sys/kernel/debug", "debugfs", 0, NULL))
0103         ; /* Not a problem if it fails.*/
0104     if (symlink("/run", "/var/run"))
0105         panic("run symlink");
0106     if (symlink("/proc/self/fd", "/dev/fd"))
0107         panic("fd symlink");
0108 }
0109 
0110 static void enable_logging(void)
0111 {
0112     int fd;
0113     pretty_message("[+] Enabling logging...");
0114     fd = open("/proc/sys/kernel/printk", O_WRONLY);
0115     if (fd >= 0) {
0116         if (write(fd, "9\n", 2) != 2)
0117             panic("write(printk)");
0118         close(fd);
0119     }
0120     fd = open("/proc/sys/debug/exception-trace", O_WRONLY);
0121     if (fd >= 0) {
0122         if (write(fd, "1\n", 2) != 2)
0123             panic("write(exception-trace)");
0124         close(fd);
0125     }
0126 }
0127 
0128 static void kmod_selftests(void)
0129 {
0130     FILE *file;
0131     char line[2048], *start, *pass;
0132     bool success = true;
0133     pretty_message("[+] Module self-tests:");
0134     file = fopen("/proc/kmsg", "r");
0135     if (!file)
0136         panic("fopen(kmsg)");
0137     if (fcntl(fileno(file), F_SETFL, O_NONBLOCK) < 0)
0138         panic("fcntl(kmsg, nonblock)");
0139     while (fgets(line, sizeof(line), file)) {
0140         start = strstr(line, "wireguard: ");
0141         if (!start)
0142             continue;
0143         start += 11;
0144         *strchrnul(start, '\n') = '\0';
0145         if (strstr(start, "www.wireguard.com"))
0146             break;
0147         pass = strstr(start, ": pass");
0148         if (!pass || pass[6] != '\0') {
0149             success = false;
0150             printf(" \x1b[31m*  %s\x1b[0m\n", start);
0151         } else
0152             printf(" \x1b[32m*  %s\x1b[0m\n", start);
0153     }
0154     fclose(file);
0155     if (!success) {
0156         puts("\x1b[31m\x1b[1m[-] Tests failed! \u2639\x1b[0m");
0157         poweroff();
0158     }
0159 }
0160 
0161 static void launch_tests(void)
0162 {
0163     char cmdline[4096], *success_dev;
0164     int status, fd;
0165     pid_t pid;
0166 
0167     pretty_message("[+] Launching tests...");
0168     pid = fork();
0169     if (pid == -1)
0170         panic("fork");
0171     else if (pid == 0) {
0172         execl("/init.sh", "init", NULL);
0173         panic("exec");
0174     }
0175     if (waitpid(pid, &status, 0) < 0)
0176         panic("waitpid");
0177     if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
0178         pretty_message("[+] Tests successful! :-)");
0179         fd = open("/proc/cmdline", O_RDONLY);
0180         if (fd < 0)
0181             panic("open(/proc/cmdline)");
0182         if (read(fd, cmdline, sizeof(cmdline) - 1) <= 0)
0183             panic("read(/proc/cmdline)");
0184         cmdline[sizeof(cmdline) - 1] = '\0';
0185         for (success_dev = strtok(cmdline, " \n"); success_dev; success_dev = strtok(NULL, " \n")) {
0186             if (strncmp(success_dev, "wg.success=", 11))
0187                 continue;
0188             memcpy(success_dev + 11 - 5, "/dev/", 5);
0189             success_dev += 11 - 5;
0190             break;
0191         }
0192         if (!success_dev || !strlen(success_dev))
0193             panic("Unable to find success device");
0194 
0195         fd = open(success_dev, O_WRONLY);
0196         if (fd < 0)
0197             panic("open(success_dev)");
0198         if (write(fd, "success\n", 8) != 8)
0199             panic("write(success_dev)");
0200         close(fd);
0201     } else {
0202         const char *why = "unknown cause";
0203         int what = -1;
0204 
0205         if (WIFEXITED(status)) {
0206             why = "exit code";
0207             what = WEXITSTATUS(status);
0208         } else if (WIFSIGNALED(status)) {
0209             why = "signal";
0210             what = WTERMSIG(status);
0211         }
0212         printf("\x1b[31m\x1b[1m[-] Tests failed with %s %d! \u2639\x1b[0m\n", why, what);
0213     }
0214 }
0215 
0216 static void ensure_console(void)
0217 {
0218     for (unsigned int i = 0; i < 1000; ++i) {
0219         int fd = open("/dev/console", O_RDWR);
0220         if (fd < 0) {
0221             usleep(50000);
0222             continue;
0223         }
0224         dup2(fd, 0);
0225         dup2(fd, 1);
0226         dup2(fd, 2);
0227         close(fd);
0228         if (write(1, "\0\0\0\0\n", 5) == 5)
0229             return;
0230     }
0231     panic("Unable to open console device");
0232 }
0233 
0234 static void clear_leaks(void)
0235 {
0236     int fd;
0237 
0238     fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
0239     if (fd < 0)
0240         return;
0241     pretty_message("[+] Starting memory leak detection...");
0242     write(fd, "clear\n", 5);
0243     close(fd);
0244 }
0245 
0246 static void check_leaks(void)
0247 {
0248     int fd;
0249 
0250     fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
0251     if (fd < 0)
0252         return;
0253     pretty_message("[+] Scanning for memory leaks...");
0254     sleep(2); /* Wait for any grace periods. */
0255     write(fd, "scan\n", 5);
0256     close(fd);
0257 
0258     fd = open("/sys/kernel/debug/kmemleak", O_RDONLY);
0259     if (fd < 0)
0260         return;
0261     if (sendfile(1, fd, NULL, 0x7ffff000) > 0)
0262         panic("Memory leaks encountered");
0263     close(fd);
0264 }
0265 
0266 int main(int argc, char *argv[])
0267 {
0268     ensure_console();
0269     print_banner();
0270     mount_filesystems();
0271     seed_rng();
0272     set_time();
0273     kmod_selftests();
0274     enable_logging();
0275     clear_leaks();
0276     launch_tests();
0277     check_leaks();
0278     poweroff();
0279     return 1;
0280 }