wireguard: selftests: simplify RNG seeding
authorJason A. Donenfeld <Jason@zx2c4.com>
Wed, 30 Mar 2022 01:31:25 +0000 (21:31 -0400)
committerJakub Kicinski <kuba@kernel.org>
Thu, 31 Mar 2022 02:14:08 +0000 (19:14 -0700)
The seed_rng() function was written to work across lots of old kernels,
back when WireGuard used a big compatibility layer. Now that things have
evolved, we can vastly simplify this, by just marking the RNG as seeded.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
tools/testing/selftests/wireguard/qemu/init.c

index c9698120ac9d8f8e03e68765eb02ba065d016957..0b45055d9de0e1d19a1d84d7e116d3c15670ffdd 100644 (file)
@@ -56,26 +56,14 @@ static void print_banner(void)
 
 static void seed_rng(void)
 {
-       int fd;
-       struct {
-               int entropy_count;
-               int buffer_size;
-               unsigned char buffer[256];
-       } entropy = {
-               .entropy_count = sizeof(entropy.buffer) * 8,
-               .buffer_size = sizeof(entropy.buffer),
-               .buffer = "Adding real entropy is not actually important for these tests. Don't try this at home, kids!"
-       };
+       int bits = 256, fd;
 
-       if (mknod("/dev/urandom", S_IFCHR | 0644, makedev(1, 9)))
-               panic("mknod(/dev/urandom)");
-       fd = open("/dev/urandom", O_WRONLY);
+       pretty_message("[+] Fake seeding RNG...");
+       fd = open("/dev/random", O_WRONLY);
        if (fd < 0)
-               panic("open(urandom)");
-       for (int i = 0; i < 256; ++i) {
-               if (ioctl(fd, RNDADDENTROPY, &entropy) < 0)
-                       panic("ioctl(urandom)");
-       }
+               panic("open(random)");
+       if (ioctl(fd, RNDADDTOENTCNT, &bits) < 0)
+               panic("ioctl(RNDADDTOENTCNT)");
        close(fd);
 }
 
@@ -270,10 +258,10 @@ static void check_leaks(void)
 
 int main(int argc, char *argv[])
 {
-       seed_rng();
        ensure_console();
        print_banner();
        mount_filesystems();
+       seed_rng();
        kmod_selftests();
        enable_logging();
        clear_leaks();