Skip to contentSkip to conversation

Fix virtio/rng handling in low entropy situations

PATCH Superseded Andre Przywara 3 participants 6 messages Apr 19, 2023 – May 23, 2023 KVM

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Fix virtio/rng handling in low entropy situations

At the moment kvmtool uses the /dev/random device to back the randomness provided by our virtio/rng implementation. We run it in non-blocking mode, so are not affected by the nasty "can block indefinitely" behaviour of that file. However:
- If /dev/random WOULD block, it returns EAGAIN, and we reflect that by adding 0 bytes of entropy to the virtio queue. However the virtio 1.x spec clearly says this is not allowed, and that we should always provide at least one random byte.
- If the guest is waiting for the random numbers, we still run into an effective blocking situation, because the buffer will only be filled very slowly, effectively stalling or blocking the guest. EDK II shows that behaviour, when servicing the EFI_RNG_PROTOCOL runtime service call, called by the kernel very early on boot.

Those two patches fix those problems, and allow to boot a Linux kernel MUCH quicker when the host lacks good entropy sources. On a particular system the kernel took 10 minutes to boot because of /dev/random effectively blocking, this runs now at full speed.

The block is avoided by using /dev/urandom, there is a proper rabbit hole in the internet out there why this is safe, even for cryptographic applications.

Patch 2 aims to fix the corner case when the /dev/urandom read fails for whatever reason: we just try once more in this case, since it should only happen when the call is interrupted by a signal. This is not 100% bullet proof, I am happy to hear any suggestions or whether we just don't care about that very rare case.

Please have a look!

Cheers,
Andre

Changelog v1 ... v2:
- Drop O_NONBLOCK from the /dev/urandom open() call
- Drop block/unblock sequence after failed read, just retry once

Andre Przywara (2):
virtio/rng: switch to using /dev/urandom
virtio/rng: return at least one byte of entropy

 virtio/rng.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/2 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

---
 virtio/rng.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
virtio/rng.c +1 -1
index 8f85d5ec1..e6e70ced3 100644
@@ -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;
Signature · 1 line
2.25.1
#2 lore raw

PATCH v2 2/2 virtio/rng: return at least one byte of entropy

In contrast to the original v0.9 virtio spec (which was rather vague), the virtio 1.0+ spec demands that a RNG request returns at least one byte:
"The device MUST place one or more random bytes into the buffer, but it MAY use less than the entire buffer length."

Our current implementation does not prevent returning zero bytes, which upsets an assert in EDK II. /dev/urandom should always return at least 256 bytes of entropy, unless interrupted by a signal.

Repeat the read if that happens, and give up if that fails as well. This makes sure we return some entropy and become spec compliant.

---
 virtio/rng.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
virtio/rng.c +12 -2
index e6e70ced3..d5959d358 100644
@@ -66,8 +66,18 @@ static bool virtio_rng_do_io_request(struct kvm *kvm, struct rng_dev *rdev, stru
 
 	head	= virt_queue__get_iov(queue, iov, &out, &in, kvm);
 	len	= readv(rdev->fd, iov, in);
-	if (len < 0 && errno == EAGAIN)
-		len = 0;
+	if (len < 0 && (errno == EAGAIN || errno == EINTR)) {
+		/*
+		 * The virtio 1.0 spec demands at least one byte of entropy,
+		 * so we cannot just return with 0 if something goes wrong.
+		 * The urandom(4) manpage mentions that a read from /dev/urandom
+		 * should always return at least 256 bytes of randomness, so
+		 * just retry here in case we were interrupted by a signal.
+		 */
+		len = readv(rdev->fd, iov, in);
+		if (len < 1)
+			return false;
+	}
 
 	virt_queue__set_used_elem(queue, head, len);
 
Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH kvmtool v2 2/2] virtio/rng: return at least one byte of entropy

Quoted 25 lines · Andre Przywara

In contrast to the original v0.9 virtio spec (which was rather vague), the virtio 1.0+ spec demands that a RNG request returns at least one byte:
"The device MUST place one or more random bytes into the buffer, but it MAY use less than the entire buffer length."

Our current implementation does not prevent returning zero bytes, which upsets an assert in EDK II. /dev/urandom should always return at least 256 bytes of entropy, unless interrupted by a signal.

Repeat the read if that happens, and give up if that fails as well. This makes sure we return some entropy and become spec compliant.

---
 virtio/rng.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
virtio/rng.c +0 -0
index e6e70ced3..d5959d358 100644
@@ -66,8 +66,18 @@ static bool virtio_rng_do_io_request(struct kvm *kvm, struct rng_dev *rdev, stru
 

virtio/rng.c static bool virtio_rng_do_io_request(struct kvm *kvm, struct rng_dev *rdev, stru · line 74

Quoted 10 lines
virtio/rng.c +6 -2
 	head	= virt_queue__get_iov(queue, iov, &out, &in, kvm);
 	len	= readv(rdev->fd, iov, in);
-	if (len < 0 && errno == EAGAIN)
-		len = 0;
+	if (len < 0 && (errno == EAGAIN || errno == EINTR)) {
+		/*
+		 * The virtio 1.0 spec demands at least one byte of entropy,
+		 * so we cannot just return with 0 if something goes wrong.
+		 * The urandom(4) manpage mentions that a read from /dev/urandom
+		 * should always return at least 256 bytes of randomness, so

I guess that's implied, but strictly speaking the manpage only states that reads of <=256 bytes succeed. Larger reads may return an error again or (if you read the man naively) zero bytes. We could increase the chance of this succeeding by setting in = 1 and iov_len = min(iov_len, 256)

Thanks,
Jean

Quoted 13 lines
virtio/rng.c +6 -0
+		 * just retry here in case we were interrupted by a signal.
+		 */
 
+		len = readv(rdev->fd, iov, in);
+		if (len < 1)
+			return false;
+	}
 
 	virt_queue__set_used_elem(queue, head, len);
Signature · 2 lines
-- 
2.25.1
#4 lore raw

Re: [PATCH kvmtool v2 2/2] virtio/rng: return at least one byte of entropy

Quoted 26 lines · Jean-Philippe Brucker
Quoted 25 lines · Andre Przywara

In contrast to the original v0.9 virtio spec (which was rather vague), the virtio 1.0+ spec demands that a RNG request returns at least one byte:
"The device MUST place one or more random bytes into the buffer, but it MAY use less than the entire buffer length."

Our current implementation does not prevent returning zero bytes, which upsets an assert in EDK II. /dev/urandom should always return at least 256 bytes of entropy, unless interrupted by a signal.

Repeat the read if that happens, and give up if that fails as well. This makes sure we return some entropy and become spec compliant.

---
 virtio/rng.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
virtio/rng.c +0 -0
index e6e70ced3..d5959d358 100644
@@ -66,8 +66,18 @@ static bool virtio_rng_do_io_request(struct kvm *kvm, struct rng_dev *rdev, stru
 
Quoted 15 lines

virtio/rng.c static bool virtio_rng_do_io_request(struct kvm *kvm, struct rng_dev *rdev, stru · line 74

Quoted 10 lines
virtio/rng.c +6 -2
 	head	= virt_queue__get_iov(queue, iov, &out, &in, kvm);
 	len	= readv(rdev->fd, iov, in);
-	if (len < 0 && errno == EAGAIN)
-		len = 0;
+	if (len < 0 && (errno == EAGAIN || errno == EINTR)) {
+		/*
+		 * The virtio 1.0 spec demands at least one byte of entropy,
+		 * so we cannot just return with 0 if something goes wrong.
+		 * The urandom(4) manpage mentions that a read from /dev/urandom
+		 * should always return at least 256 bytes of randomness, so

I guess that's implied, but strictly speaking the manpage only states that reads of <=256 bytes succeed. Larger reads may return an error again or (if you read the man naively) zero bytes. We could increase the chance of this succeeding by setting in = 1 and iov_len = min(iov_len, 256)

Andre -- do you plan to respin with Jean's suggestion above?

Will

#5 lore raw

Re: [PATCH kvmtool v2 2/2] virtio/rng: return at least one byte of entropy

On Tue, 23 May 2023 11:45:03 +0100
Will Deacon <[email protected]> wrote:

Hi Will,

Quoted 43 lines · Jean-Philippe Brucker
Quoted 26 lines · Andre Przywara
Quoted 25 lines

In contrast to the original v0.9 virtio spec (which was rather vague), the virtio 1.0+ spec demands that a RNG request returns at least one byte:
"The device MUST place one or more random bytes into the buffer, but it MAY use less than the entire buffer length."

Our current implementation does not prevent returning zero bytes, which upsets an assert in EDK II. /dev/urandom should always return at least 256 bytes of entropy, unless interrupted by a signal.

Repeat the read if that happens, and give up if that fails as well. This makes sure we return some entropy and become spec compliant.

---
 virtio/rng.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
virtio/rng.c +0 -0
index e6e70ced3..d5959d358 100644
@@ -66,8 +66,18 @@ static bool virtio_rng_do_io_request(struct kvm *kvm, struct rng_dev *rdev, stru
 
Quoted 15 lines

virtio/rng.c static bool virtio_rng_do_io_request(struct kvm *kvm, struct rng_dev *rdev, stru · line 74

Quoted 10 lines
virtio/rng.c +6 -2
 	head	= virt_queue__get_iov(queue, iov, &out, &in, kvm);
 	len	= readv(rdev->fd, iov, in);
-	if (len < 0 && errno == EAGAIN)
-		len = 0;
+	if (len < 0 && (errno == EAGAIN || errno == EINTR)) {
+		/*
+		 * The virtio 1.0 spec demands at least one byte of entropy,
+		 * so we cannot just return with 0 if something goes wrong.
+		 * The urandom(4) manpage mentions that a read from /dev/urandom
+		 * should always return at least 256 bytes of randomness, so  

I guess that's implied, but strictly speaking the manpage only states that reads of <=256 bytes succeed. Larger reads may return an error again or (if you read the man naively) zero bytes. We could increase the chance of this succeeding by setting in = 1 and iov_len = min(iov_len, 256)

Quoted 1 line

Andre -- do you plan to respin with Jean's suggestion above?

Yes, sorry, I read too much into the suggestion at first, so it got pushed off the table.
But reading that again now and looking at the code I realise that it's indeed easy to implement. I will post something shortly.

Thanks,
Andre.