diff options
author | Andre Przywara <andre.przywara@arm.com> | 2023-05-24 12:22:06 +0100 |
---|---|---|
committer | Will Deacon <will@kernel.org> | 2023-06-05 13:18:03 +0100 |
commit | 62ba372b0e67b92efe20f63330fa0d40004d9fdd (patch) | |
tree | f804589dcdc123d573701b595e724bdfb87b5ec9 | |
parent | 77b108c6a6f1c66fb7f60a80d17596bb80bda8ad (diff) | |
download | kvmtool-62ba372b0e67b92efe20f63330fa0d40004d9fdd.tar.gz |
virtio/rng: switch to using /dev/urandom
At the moment we use /dev/random as the backing device to provide random
numbers to our virtio-rng implementation. The downside of doing so is
that it may block indefinitely - or return EAGAIN repeatedly in our case.
On one headless system without ample noise sources (no keyboard, mouse,
or network traffic) I measured 30 seconds to gain one byte of randomness.
At the moment EDK II insists in waiting for all of the requsted random
bytes (for its EFI_RNG_PROTOCOL runtime service) to arrive, that held up
a Linux kernel boot for more than 10 minutes(!).
According to the Internet(TM), on Linux /dev/urandom provides the same
quality random numbers as /dev/random, it just does not block when the
entropy estimation algorithm suggests so. For all practical purposes the
recommendation is to just use /dev/urandom, QEMU did the switch as well
in 2019 [1].
Use /dev/urandom instead of /dev/random when opening the file descriptor
providing the randomness source for the virtio/rng implementation.
Due to a special behaviour documented on the urandom(4) manpage, a read
from /dev/urandom will never block, so we can drop the O_NONBLOCK flag.
[1] https://gitlab.com/qemu-project/qemu/-/commit/a2230bd778d8
Signed-off-by: Andre Przywara <andre.przywara@arm.com>
Reviewed-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Link: https://lore.kernel.org/r/20230524112207.586101-2-andre.przywara@arm.com
Signed-off-by: Will Deacon <will@kernel.org>
-rw-r--r-- | virtio/rng.c | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/virtio/rng.c b/virtio/rng.c index 8f85d5ec..e6e70ced 100644 --- a/virtio/rng.c +++ b/virtio/rng.c @@ -166,7 +166,7 @@ int virtio_rng__init(struct kvm *kvm) if (rdev == NULL) return -ENOMEM; - rdev->fd = open("/dev/random", O_RDONLY | O_NONBLOCK); + rdev->fd = open("/dev/urandom", O_RDONLY); if (rdev->fd < 0) { r = rdev->fd; goto cleanup; |