From d967773e7f9feea3b9ce57c9c5db9106b7f7538c Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Tue, 16 Aug 2016 19:38:26 +0200 Subject: qemu: enhancements --- src/tests/qemu/Makefile | 8 ++-- src/tests/qemu/init.c | 116 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 98 insertions(+), 26 deletions(-) (limited to 'src/tests/qemu') diff --git a/src/tests/qemu/Makefile b/src/tests/qemu/Makefile index 0e5ab0d..f0f5b91 100644 --- a/src/tests/qemu/Makefile +++ b/src/tests/qemu/Makefile @@ -1,7 +1,7 @@ PWD := $(shell pwd) # Set these from the environment to override -KERNEL_VERSION ?= 4.7 +KERNEL_VERSION ?= 4.7.1 BUILD_PATH ?= $(PWD)/build DISTFILES_PATH ?= $(PWD)/distfiles DEBUG_KERNEL ?= no @@ -118,10 +118,10 @@ $(KERNEL_PATH)/.config: kernel.config | $(KERNEL_PATH)/.installed -[ "$(DEBUG_KERNEL)" = "yes" ] && ( cd $(KERNEL_PATH) && scripts/kconfig/merge_config.sh -n .config $(PWD)/debug.config ) $(KERNEL_BZIMAGE): $(KERNEL_PATH)/.installed $(KERNEL_PATH)/.config $(BUILD_PATH)/init-cpio-spec.txt $(MUSL_PATH)/lib/libc.so $(IPERF_PATH)/src/iperf3 $(BUILD_PATH)/tools/wg $(IPUTILS_PATH)/ping $(BASH_PATH)/bash $(IPROUTE2_PATH)/ip/ip $(IPTABLES_PATH)/iptables/xtables-multi $(NMAP_PATH)/ncat/ncat $(BUILD_PATH)/init ../netns.sh $(WIREGUARD_SOURCES) $(TOOLS_SOURCES) - $(MAKE) -C $(KERNEL_PATH) + LOCALVERSION="" $(MAKE) -C $(KERNEL_PATH) $(BUILD_PATH)/include/linux/.installed: | $(KERNEL_PATH)/.installed - $(MAKE) -C $(KERNEL_PATH) INSTALL_HDR_PATH=$(BUILD_PATH) headers_install + LOCALVERSION="" $(MAKE) -C $(KERNEL_PATH) INSTALL_HDR_PATH=$(BUILD_PATH) headers_install touch $@ $(MUSL_PATH)/lib/libc.so: $(MUSL_TAR) | $(BUILD_PATH)/include/linux/.installed @@ -165,7 +165,7 @@ $(BUILD_PATH)/tools/wg: $(MUSL_CC) $(TOOLS_SOURCES) $(LIBMNL_PATH)/src/.libs/lib strip -s $@ $(BUILD_PATH)/init: init.c $(MUSL_CC) - $(MUSL_CC) -o $@ $< + $(MUSL_CC) -o $@ -O3 -std=gnu11 $< strip -s $@ $(IPUTILS_PATH)/.installed: $(IPUTILS_TAR) diff --git a/src/tests/qemu/init.c b/src/tests/qemu/init.c index 310f34b..4af220c 100644 --- a/src/tests/qemu/init.c +++ b/src/tests/qemu/init.c @@ -1,8 +1,10 @@ +#define _GNU_SOURCE #include #include #include #include #include +#include #include #include #include @@ -11,7 +13,9 @@ #include #include #include +#include #include +#include __attribute__((noreturn)) static void poweroff(void) { @@ -30,9 +34,16 @@ static void panic(const char *what) #define pretty_message(msg) puts("\x1b[32m\x1b[1m" msg "\x1b[0m") -int main(int argc, char *argv[]) +static void print_banner(const struct utsname *utsname) +{ + int len = strlen(" WireGuard Test Suite on ") + strlen(utsname->sysname) + strlen(utsname->release); + putchar('\0');putchar('\0');putchar('\0');putchar('\0');putchar('\n'); + printf("\x1b[45m\x1b[33m\x1b[1m%*.s\x1b[0m\n\x1b[45m\x1b[33m\x1b[1m WireGuard Test Suite on %s %s \x1b[0m\n\x1b[45m\x1b[33m\x1b[1m%*.s\x1b[0m\n\n", len, "", utsname->sysname, utsname->release, len, ""); +} + +static void seed_rng(void) { - int status, fd1, fd2, i; + int fd1, fd2, i; struct { int entropy_count; int buffer_size; @@ -41,6 +52,23 @@ int main(int argc, char *argv[]) .entropy_count = 128, .buffer_size = 128 }; + pretty_message("[+] Ensuring RNG entropy..."); + fd1 = open("/dev/hwrng", O_RDONLY); + fd2 = open("/dev/urandom", O_WRONLY); + if (fd1 < 0 || fd2 < 0) + panic("open(hwrng,urandom)"); + for (i = 0; i < 4096; ++i) { + if (read(fd1, entropy.buffer, 128) != 128) + panic("read(hwrng)"); + if (ioctl(fd2, RNDADDENTROPY, &entropy) < 0) + panic("ioctl(urandom)"); + } + close(fd1); + close(fd2); +} + +static void mount_filesystems(void) +{ pretty_message("[+] Mounting filesystems..."); mkdir("/dev", 0755); mkdir("/proc", 0755); @@ -62,27 +90,46 @@ int main(int argc, char *argv[]) panic("run symlink"); if (symlink("/proc/self/fd", "/dev/fd")) panic("fd symlink"); +} + +static void enable_logging(void) +{ + int fd; pretty_message("[+] Enabling logging..."); - fd1 = open("/proc/sys/kernel/printk", O_WRONLY); - if (fd1 < 0) + fd = open("/proc/sys/kernel/printk", O_WRONLY); + if (fd < 0) panic("open(printk)"); - if (write(fd1, "9\n", 2) != 2) + if (write(fd, "9\n", 2) != 2) panic("write(printk)"); - close(fd1); - pretty_message("[+] Ensuring RNG entropy..."); - fd1 = open("/dev/hwrng", O_RDONLY); - fd2 = open("/dev/urandom", O_WRONLY); - if (fd1 < 0 || fd2 < 0) - panic("open(hwrng,urandom)"); - for (i = 0; i < 4096; ++i) { - if (read(fd1, entropy.buffer, 128) != 128) - panic("read(hwrng)"); - if (ioctl(fd2, RNDADDENTROPY, &entropy) < 0) - panic("ioctl(urandom)"); + close(fd); +} + +static void kmod_selftests(void) +{ + FILE *file; + char line[2048], *start; + pretty_message("[+] Module self-tests:"); + file = fopen("/proc/kmsg", "r"); + if (!file) + panic("fopen(kmsg)"); + if (fcntl(fileno(file), F_SETFL, O_NONBLOCK) < 0) + panic("fcntl(kmsg, nonblock)"); + while (fgets(line, sizeof(line), file)) { + start = strstr(line, "wireguard: "); + if (!start) + continue; + start += 11; + *strchrnul(start, '\n') = '\0'; + if (strstr(start, "WireGuard loaded.")) + break; + printf(" \x1b[32m* %s\x1b[0m\n", start); } - close(fd1); - close(fd2); + fclose(file); +} +static void launch_tests(void) +{ + int status, fd; pretty_message("[+] Launching tests..."); switch (fork()) { case -1: @@ -97,14 +144,39 @@ int main(int argc, char *argv[]) panic("wait"); if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { pretty_message("[+] Tests successful! :-)"); - fd1 = open("/dev/vport1p1", O_WRONLY); - if (fd1 < 0) + fd = open("/dev/vport1p1", O_WRONLY); + if (fd < 0) panic("open(vport1p1)"); - if (write(fd1, "success\n", 8) != 8) + if (write(fd, "success\n", 8) != 8) panic("write(success)"); - close(fd1); + close(fd); } else puts("\x1b[31m\x1b[1m[-] Tests failed! :-(\x1b[0m"); +} + +static bool linux_4_8_or_higher(const struct utsname *utsname) +{ + unsigned int maj, min, rel; + if (strcmp(utsname->sysname, "Linux")) + return false; + if (sscanf(utsname->release, "%u.%u.%u", &maj, &min, &rel) != 3) + return false; + return KERNEL_VERSION(maj, min, rel) >= KERNEL_VERSION(4, 8, 0); +} + +int main(int argc, char *argv[]) +{ + struct utsname utsname; + if (uname(&utsname) < 0) + panic("uname"); + + print_banner(&utsname); + mount_filesystems(); + kmod_selftests(); + if (!linux_4_8_or_higher(&utsname)) + seed_rng(); + enable_logging(); + launch_tests(); poweroff(); return 1; } -- cgit v1.2.3-59-g8ed1b