virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hwrng: virtio - fix copy_data() handling of partial reads
@ 2026-11-05  7:17 Xiang Lei
  2026-11-05  7:24 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Xiang Lei @ 2026-11-05  7:17 UTC (permalink / raw)
  Cc: Xiang Lei, Wei Liu, stable, Laurent Vivier, Olivia Mackall,
	Herbert Xu, Michael S. Tsirkin, Michael Bommarito, virtualization,
	linux-crypto, linux-kernel

vi->data_avail is decremented on each read, so it holds the remaining
bytes rather than the total length. Subtracting vi->data_idx from it
again in commit e3046eeada29 ("hwrng: virtio: clamp device-reported
used.len at copy_data()") undercounts the bytes left after a partial
read. This can leave vi->data_avail nonzero without queuing a new
request, causing virtio_read() to hang on have_data.

Clamp vi->data_avail to sizeof(vi->data) - vi->data_idx instead.

Fixes: e3046eeada29 ("hwrng: virtio: clamp device-reported used.len at copy_data()")
Tested-by: Wei Liu <liuwei@kylinos.cn>
Cc: stable@vger.kernel.org
Signed-off-by: Xiang Lei <leixiang@kylinos.cn>
---
 drivers/char/hw_random/virtio-rng.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c
index f9982217247d..a3392f7e2d8c 100644
--- a/drivers/char/hw_random/virtio-rng.c
+++ b/drivers/char/hw_random/virtio-rng.c
@@ -70,7 +70,7 @@ static void request_entropy(struct virtrng_info *vi)
 static unsigned int copy_data(struct virtrng_info *vi, void *buf,
 			      unsigned int size)
 {
-	unsigned int idx, avail;
+	unsigned int idx;
 
 	/*
 	 * vi->data_avail was set from the device-reported used.len and
@@ -81,13 +81,13 @@ static unsigned int copy_data(struct virtrng_info *vi, void *buf,
 	 * steered into adjacent slab memory, including under
 	 * speculation.
 	 */
-	avail = min(vi->data_avail, sizeof(vi->data));
-	if (vi->data_idx >= avail) {
+	if (vi->data_idx >= sizeof(vi->data)) {
 		vi->data_avail = 0;
 		request_entropy(vi);
 		return 0;
 	}
-	size = min(size, avail - vi->data_idx);
+	vi->data_avail = min(vi->data_avail, sizeof(vi->data) - vi->data_idx);
+	size = min(size, vi->data_avail);
 	idx = array_index_nospec(vi->data_idx, sizeof(vi->data));
 	memcpy(buf, vi->data + idx, size);
 	vi->data_idx += size;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] hwrng: virtio - fix copy_data() handling of partial reads
  2026-11-05  7:17 [PATCH] hwrng: virtio - fix copy_data() handling of partial reads Xiang Lei
@ 2026-11-05  7:24 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-05  7:24 UTC (permalink / raw)
  To: Xiang Lei; +Cc: virtualization, Michael S. Tsirkin, Eugenio Perez

> vi->data_avail is decremented on each read, so it holds the remaining
> bytes rather than the total length. Subtracting vi->data_idx from it
> again in commit e3046eeada29 ("hwrng: virtio: clamp device-reported
> used.len at copy_data()") undercounts the bytes left after a partial
> read. This can leave vi->data_avail nonzero without queuing a new
> request, causing virtio_read() to hang on have_data.
> 
> Clamp vi->data_avail to sizeof(vi->data) - vi->data_idx instead.
> 
> Fixes: e3046eeada29 ("hwrng: virtio: clamp device-reported used.len at copy_data()")
> Tested-by: Wei Liu <liuwei@kylinos.cn>
> Cc: stable@vger.kernel.org
> Signed-off-by: Xiang Lei <leixiang@kylinos.cn>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261105071736.65932-1-leixiang@kylinos.cn?part=1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-05  7:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-05  7:17 [PATCH] hwrng: virtio - fix copy_data() handling of partial reads Xiang Lei
2026-11-05  7:24 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).