All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Dmitry Vyukov <dvyukov@google.com>,
	syzbot <syzbot+726dc8c62c3536431ceb@syzkaller.appspotmail.com>,
	davem@davemloft.net, linux-crypto@vger.kernel.org,
	linux-kernel@vger.kernel.org, olivia@selenic.com,
	syzkaller-bugs@googlegroups.com, Jason Wang <jasowang@redhat.com>,
	Laurent Vivier <lvivier@redhat.com>,
	Rusty Russell <rusty@rustcorp.com.au>
Subject: Re: [v2 PATCH] hwrng: virtio - Fix race on data_avail and actual data
Date: Thu, 4 May 2023 01:28:30 -0400	[thread overview]
Message-ID: <20230504012732-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <ZFMtpC18ykLd/jf2@gondor.apana.org.au>

On Thu, May 04, 2023 at 11:59:32AM +0800, Herbert Xu wrote:
> On Wed, May 03, 2023 at 07:37:00AM -0400, Michael S. Tsirkin wrote:
> >
> > On the surface of it, it looks like you removed this store
> > which isn't described in the commit log.
> > I do not, offhand, remember why we stored 0 in data_idx here
> > when we also zero it in request_entropy.
> > It was added with
> 
> Yes I removed because it's redundant.  But you're right I'll add
> a note about it in the log:
> 
> ---8<---
> 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@syzkaller.appspotmail.com
> Fixes: f7f510ec1957 ("virtio: An entropy device, as suggested by hpa.")
> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>


Acked-by: Michael S. Tsirkin <mst@redhat.com>

feel free ro merge, thanks!

> 
> diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c
> index f7690e0f92ed..e41a84e6b4b5 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>
> @@ -37,13 +38,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);
>  }
>  
> @@ -52,7 +53,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));
> @@ -88,7 +88,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;
> -- 
> Email: Herbert Xu <herbert@gondor.apana.org.au>
> Home Page: http://gondor.apana.org.au/~herbert/
> PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt


      reply	other threads:[~2023-05-04  5:29 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-21 14:35 [syzbot] [crypto?] KCSAN: data-race in random_recv_done / virtio_read (3) syzbot
2023-04-21 14:52 ` Dmitry Vyukov
2023-05-03 10:22   ` Herbert Xu
2023-05-03 10:54   ` [PATCH] hwrng: virtio - Fix race on data_avail and actual data Herbert Xu
2023-05-03 11:19     ` Tudor Ambarus
2023-05-04  3:55       ` Herbert Xu
2023-05-04  8:10         ` Tudor Ambarus
2023-05-05  4:01           ` Theodore Ts'o
2023-05-08  5:33             ` Dmitry Vyukov
2023-05-08  8:55               ` Theodore Ts'o
2023-05-11 15:11                 ` Aleksandr Nogikh
2023-05-03 11:37     ` Michael S. Tsirkin
2023-05-04  3:59       ` [v2 PATCH] " Herbert Xu
2023-05-04  5:28         ` Michael S. Tsirkin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230504012732-mutt-send-email-mst@kernel.org \
    --to=mst@redhat.com \
    --cc=davem@davemloft.net \
    --cc=dvyukov@google.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=jasowang@redhat.com \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lvivier@redhat.com \
    --cc=olivia@selenic.com \
    --cc=rusty@rustcorp.com.au \
    --cc=syzbot+726dc8c62c3536431ceb@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.