Patch "hwrng: virtio - Fix race on data_avail and actual data" has been added to the 5.4-stable tree

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is a note to let you know that I've just added the patch titled

    hwrng: virtio - Fix race on data_avail and actual data

to the 5.4-stable tree which can be found at:
    http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
     hwrng-virtio-fix-race-on-data_avail-and-actual-data.patch
and it can be found in the queue-5.4 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree,
please let <stable@xxxxxxxxxxxxxxx> know about it.



commit 8a4a549b7f39f7e6fa22594774a47da073037e9a
Author: Herbert Xu <herbert@xxxxxxxxxxxxxxxxxxx>
Date:   Thu May 4 11:59:32 2023 +0800

    hwrng: virtio - Fix race on data_avail and actual data
    
    [ Upstream commit ac52578d6e8d300dd50f790f29a24169b1edd26c ]
    
    The virtio rng device kicks off a new entropy request whenever the
    data available reaches zero.  When a new request occurs at the end
    of a read operation, that is, when the result of that request is
    only needed by the next reader, then there is a race between the
    writing of the new data and the next reader.
    
    This is because there is no synchronisation whatsoever between the
    writer and the reader.
    
    Fix this by writing data_avail with smp_store_release and reading
    it with smp_load_acquire when we first enter read.  The subsequent
    reads are safe because they're either protected by the first load
    acquire, or by the completion mechanism.
    
    Also remove the redundant zeroing of data_idx in random_recv_done
    (data_idx must already be zero at this point) and data_avail in
    request_entropy (ditto).
    
    Reported-by: syzbot+726dc8c62c3536431ceb@xxxxxxxxxxxxxxxxxxxxxxxxx
    Fixes: f7f510ec1957 ("virtio: An entropy device, as suggested by hpa.")
    Signed-off-by: Herbert Xu <herbert@xxxxxxxxxxxxxxxxxxx>
    Acked-by: Michael S. Tsirkin <mst@xxxxxxxxxx>
    Signed-off-by: Herbert Xu <herbert@xxxxxxxxxxxxxxxxxxx>
    Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>

diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c
index f98e3ee5f8b03..145d7b1055c07 100644
--- a/drivers/char/hw_random/virtio-rng.c
+++ b/drivers/char/hw_random/virtio-rng.c
@@ -4,6 +4,7 @@
  *  Copyright (C) 2007, 2008 Rusty Russell IBM Corporation
  */
 
+#include <asm/barrier.h>
 #include <linux/err.h>
 #include <linux/hw_random.h>
 #include <linux/scatterlist.h>
@@ -36,13 +37,13 @@ struct virtrng_info {
 static void random_recv_done(struct virtqueue *vq)
 {
 	struct virtrng_info *vi = vq->vdev->priv;
+	unsigned int len;
 
 	/* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */
-	if (!virtqueue_get_buf(vi->vq, &vi->data_avail))
+	if (!virtqueue_get_buf(vi->vq, &len))
 		return;
 
-	vi->data_idx = 0;
-
+	smp_store_release(&vi->data_avail, len);
 	complete(&vi->have_data);
 }
 
@@ -51,7 +52,6 @@ static void request_entropy(struct virtrng_info *vi)
 	struct scatterlist sg;
 
 	reinit_completion(&vi->have_data);
-	vi->data_avail = 0;
 	vi->data_idx = 0;
 
 	sg_init_one(&sg, vi->data, sizeof(vi->data));
@@ -87,7 +87,7 @@ static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait)
 	read = 0;
 
 	/* copy available data */
-	if (vi->data_avail) {
+	if (smp_load_acquire(&vi->data_avail)) {
 		chunk = copy_data(vi, buf, size);
 		size -= chunk;
 		read += chunk;



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux