Skip to content

Commit 7cd1d2e

Browse files
committed
qemu: simplify fake entropy adding on init
We don't actualy need to write anything in the pool. Instead, we just force the total over 128, and we should be good to go for all old kernels. We also only need this on getrandom() kernels, which simplifies things too. Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 4eff63d commit 7cd1d2e

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

src/tests/qemu/init.c

+8-18
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,17 @@ static void print_banner(void)
5858

5959
static void seed_rng(void)
6060
{
61-
int fd;
62-
struct {
63-
int entropy_count;
64-
int buffer_size;
65-
unsigned char buffer[256];
66-
} entropy = {
67-
.entropy_count = sizeof(entropy.buffer) * 8,
68-
.buffer_size = sizeof(entropy.buffer),
69-
.buffer = "Adding real entropy is not actually important for these tests. Don't try this at home, kids!"
70-
};
61+
int bits = 4096, fd;
7162

72-
if (mknod("/dev/urandom", S_IFCHR | 0644, makedev(1, 9)))
73-
panic("mknod(/dev/urandom)");
74-
fd = open("/dev/urandom", O_WRONLY);
63+
pretty_message("[+] Fake seeding RNG...");
64+
fd = open("/dev/random", O_WRONLY);
7565
if (fd < 0)
76-
panic("open(urandom)");
66+
panic("open(random)");
7767
for (;;) {
78-
if (getrandom(entropy.buffer, sizeof(entropy.buffer), GRND_NONBLOCK) != -1 || errno != EAGAIN)
68+
if (!getrandom(NULL, 0, GRND_NONBLOCK) || errno == ENOSYS)
7969
break;
80-
if (ioctl(fd, RNDADDENTROPY, &entropy) < 0)
81-
panic("ioctl(urandom)");
70+
if (ioctl(fd, RNDADDTOENTCNT, &bits) < 0)
71+
panic("ioctl(RNDADDTOENTCNT)");
8272
}
8373
close(fd);
8474
}
@@ -274,10 +264,10 @@ static void check_leaks(void)
274264

275265
int main(int argc, char *argv[])
276266
{
277-
seed_rng();
278267
ensure_console();
279268
print_banner();
280269
mount_filesystems();
270+
seed_rng();
281271
kmod_selftests();
282272
enable_logging();
283273
clear_leaks();

0 commit comments

Comments
 (0)