All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] hwrng: virtio - prevent use-after-free after a failed restore
@ 2026-10-16 10:36 Thorsten Blum
  2026-10-16 10:47 ` sashiko-bot
  2026-10-18 17:00 ` PrasannaKumar Muralidharan
  0 siblings, 2 replies; 5+ messages in thread
From: Thorsten Blum @ 2026-10-16 10:36 UTC (permalink / raw)
  To: Laurent Vivier, Olivia Mackall, Herbert Xu, Rusty Russell,
	Amos Kong
  Cc: Thorsten Blum, virtualization, linux-crypto, linux-kernel

remove_common() frees the driver data during freeze, but does not clear
vdev->priv, which points to freed memory. If probe_common() then fails
during restore, a later remove or freeze can dereference the dangling
pointer.

Clear vdev->priv on the probe_common() error cleanup path and before
freeing the driver data in remove_common(). Also reset the device and
return early from remove_common() if vdev->priv is already NULL for a
safe teardown after a failed restore.

Fixes: 08e53fbdb85c ("virtio-rng: support multiple virtio-rng devices")
Cc: stable@vger.kernel.org
Signed-off-by: Thorsten Blum <blum@kernel.org>
---
 drivers/char/hw_random/virtio-rng.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c
index 5e83ffa105e4..a9ff520975a7 100644
--- a/drivers/char/hw_random/virtio-rng.c
+++ b/drivers/char/hw_random/virtio-rng.c
@@ -190,6 +190,7 @@ static int probe_common(struct virtio_device *vdev)
 err_find:
 	ida_free(&rng_index_ida, index);
 err_ida:
+	vdev->priv = NULL;
 	kfree(vi);
 	return err;
 }
@@ -198,6 +199,11 @@ static void remove_common(struct virtio_device *vdev)
 {
 	struct virtrng_info *vi = vdev->priv;
 
+	if (!vi) {
+		virtio_reset_device(vdev);
+		return;
+	}
+
 	vi->hwrng_removed = true;
 	vi->data_avail = 0;
 	vi->data_idx = 0;
@@ -207,6 +213,7 @@ static void remove_common(struct virtio_device *vdev)
 	virtio_reset_device(vdev);
 	vdev->config->del_vqs(vdev);
 	ida_free(&rng_index_ida, vi->index);
+	vdev->priv = NULL;
 	kfree(vi);
 }
 

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

* Re: [PATCH] hwrng: virtio - prevent use-after-free after a failed restore
  2026-10-16 10:36 [PATCH] hwrng: virtio - prevent use-after-free after a failed restore Thorsten Blum
@ 2026-10-16 10:47 ` sashiko-bot
  2026-10-18 17:00 ` PrasannaKumar Muralidharan
  1 sibling, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-10-16 10:47 UTC (permalink / raw)
  To: Thorsten Blum; +Cc: virtualization, Michael S. Tsirkin, Eugenio Perez

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] virtio-rng: data race on data_avail leads to deadlock
--

commit 2904ec0356e25704559279a6336e7fed895c58d2
Author: Thorsten Blum <blum@kernel.org>

hwrng: virtio - prevent use-after-free after a failed restore

remove_common() frees the driver data during freeze, but does not clear
vdev->priv, which points to freed memory. Clear vdev->priv on the error
cleanup path and before freeing the driver data in remove_common(). Also
reset the device and return early from remove_common() if vdev->priv is
already NULL.

> diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c
> index f9982217247dd..45d87c0614a5e 100644
> --- a/drivers/char/hw_random/virtio-rng.c
> +++ b/drivers/char/hw_random/virtio-rng.c
[ ... ]
> @@ -198,6 +199,11 @@ static void remove_common(struct virtio_device *vdev)
>  {
>  	struct virtrng_info *vi = vdev->priv;
>  
> +	if (!vi) {
> +		virtio_reset_device(vdev);
> +		return;
> +	}
> +
>  	vi->hwrng_removed = true;
>  	vi->data_avail = 0;

[Severity: High]
Does this code race with a reader and cause a deadlock?

In copy_data() in drivers/char/hw_random/virtio-rng.c, vi->data_avail is read,
memcpy() is called, and then size is subtracted:

drivers/char/hw_random/virtio-rng.c:copy_data() {
	...
	memcpy(buf, vi->data + idx, size);
	vi->data_idx += size;
	vi->data_avail -= size;
	if (vi->data_avail == 0)
		request_entropy(vi);
	return size;
}

If remove_common() sets vi->data_avail to 0 before the subtraction, will this
cause an unsigned integer underflow, leaving vi->data_avail as a huge value?

Since vi->data_avail != 0, copy_data() would skip calling request_entropy().
virtio_read() would then consume the single completion from remove_common().

Seeing a huge data_avail instead of 0, virtio_read() would continue looping
and eventually block forever in wait_for_completion_killable() because no
pending entropy request will trigger a new completion:

drivers/char/hw_random/virtio-rng.c:virtio_read() {
	...
	while (size != 0) {
		/* data_avail is 0 but a request is pending */
		ret = wait_for_completion_killable(&vi->have_data);
	...
}

Could this cause hwrng_unregister() in remove_common() to block forever
waiting for the reader to drop its reference, deadlocking device removal?

>  	vi->data_idx = 0;
>  	complete(&vi->have_data);
[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261016103640.4989-2-blum@kernel.org?part=1

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

* Re: [PATCH] hwrng: virtio - prevent use-after-free after a failed restore
  2026-10-16 10:36 [PATCH] hwrng: virtio - prevent use-after-free after a failed restore Thorsten Blum
  2026-10-16 10:47 ` sashiko-bot
@ 2026-10-18 17:00 ` PrasannaKumar Muralidharan
  2026-10-18 20:37   ` Thorsten Blum
  1 sibling, 1 reply; 5+ messages in thread
From: PrasannaKumar Muralidharan @ 2026-10-18 17:00 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Laurent Vivier, Olivia Mackall, Herbert Xu, Rusty Russell,
	Amos Kong, virtualization, linux-crypto, linux-kernel

Hi Thorsten,

On Fri, 16 Oct 2026 at 16:16, Thorsten Blum <blum@kernel.org> wrote:
>
> remove_common() frees the driver data during freeze, but does not clear
> vdev->priv, which points to freed memory. If probe_common() then fails
> during restore, a later remove or freeze can dereference the dangling
> pointer.
>
> Clear vdev->priv on the probe_common() error cleanup path and before
> freeing the driver data in remove_common(). Also reset the device and
> return early from remove_common() if vdev->priv is already NULL for a
> safe teardown after a failed restore.
>
> Fixes: 08e53fbdb85c ("virtio-rng: support multiple virtio-rng devices")
> Cc: stable@vger.kernel.org
> Signed-off-by: Thorsten Blum <blum@kernel.org>
> ---
>  drivers/char/hw_random/virtio-rng.c | 7 +++++++
>  1 file changed, 7 insertions(+)
>
> diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c
> index 5e83ffa105e4..a9ff520975a7 100644
> --- a/drivers/char/hw_random/virtio-rng.c
> +++ b/drivers/char/hw_random/virtio-rng.c
> @@ -190,6 +190,7 @@ static int probe_common(struct virtio_device *vdev)
>  err_find:
>         ida_free(&rng_index_ida, index);
>  err_ida:
> +       vdev->priv = NULL;
>         kfree(vi);
>         return err;
>  }
> @@ -198,6 +199,11 @@ static void remove_common(struct virtio_device *vdev)
>  {
>         struct virtrng_info *vi = vdev->priv;
>
> +       if (!vi) {
> +               virtio_reset_device(vdev);
> +               return;
> +       }
> +

The virtio_reset_device() is always called before setting vdev->priv
to NULL. So the virtio device is already quiesced, no need to do early
return. Isn't it?

>         vi->hwrng_removed = true;
>         vi->data_avail = 0;
>         vi->data_idx = 0;
> @@ -207,6 +213,7 @@ static void remove_common(struct virtio_device *vdev)
>         virtio_reset_device(vdev);
>         vdev->config->del_vqs(vdev);
>         ida_free(&rng_index_ida, vi->index);
> +       vdev->priv = NULL;
>         kfree(vi);
>  }
>
>

The rest of the hunks are fine.

Thanks,
PrasannaKumar

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

* Re: [PATCH] hwrng: virtio - prevent use-after-free after a failed restore
  2026-10-18 17:00 ` PrasannaKumar Muralidharan
@ 2026-10-18 20:37   ` Thorsten Blum
  2026-10-19 13:00     ` PrasannaKumar Muralidharan
  0 siblings, 1 reply; 5+ messages in thread
From: Thorsten Blum @ 2026-10-18 20:37 UTC (permalink / raw)
  To: PrasannaKumar Muralidharan
  Cc: Laurent Vivier, Olivia Mackall, Herbert Xu, Rusty Russell,
	Amos Kong, virtualization, linux-crypto, linux-kernel

Hi PrasannaKumar,

On Sun, Oct 18, 2026 at 10:30:08PM +0530, PrasannaKumar Muralidharan wrote:
> Hi Thorsten,
> 
> On Fri, 16 Oct 2026 at 16:16, Thorsten Blum <blum@kernel.org> wrote:
> >
> > remove_common() frees the driver data during freeze, but does not clear
> > vdev->priv, which points to freed memory. If probe_common() then fails
> > during restore, a later remove or freeze can dereference the dangling
> > pointer.
> >
> > Clear vdev->priv on the probe_common() error cleanup path and before
> > freeing the driver data in remove_common(). Also reset the device and
> > return early from remove_common() if vdev->priv is already NULL for a
> > safe teardown after a failed restore.
> >
> > Fixes: 08e53fbdb85c ("virtio-rng: support multiple virtio-rng devices")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Thorsten Blum <blum@kernel.org>
> > ---
> >  drivers/char/hw_random/virtio-rng.c | 7 +++++++
> >  1 file changed, 7 insertions(+)
> >
> > diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c
> > index 5e83ffa105e4..a9ff520975a7 100644
> > --- a/drivers/char/hw_random/virtio-rng.c
> > +++ b/drivers/char/hw_random/virtio-rng.c
> > @@ -190,6 +190,7 @@ static int probe_common(struct virtio_device *vdev)
> >  err_find:
> >         ida_free(&rng_index_ida, index);
> >  err_ida:
> > +       vdev->priv = NULL;
> >         kfree(vi);
> >         return err;
> >  }
> > @@ -198,6 +199,11 @@ static void remove_common(struct virtio_device *vdev)
> >  {
> >         struct virtrng_info *vi = vdev->priv;
> >
> > +       if (!vi) {
> > +               virtio_reset_device(vdev);
> > +               return;
> > +       }
> > +
> 
> The virtio_reset_device() is always called before setting vdev->priv
> to NULL. So the virtio device is already quiesced, no need to do early
> return. Isn't it?

The check is still needed.

For example, during hibernation or suspend, .freeze (virtrng_freeze() ->
remove_common()) frees vi and sets vdev->priv to NULL.

On resume, .restore (virtrng_restore()) calls probe_common(), which
could fail and leave vdev->priv NULL. A later .freeze or .remove calls
remove_common() again, which would dereference NULL without the check.

Best regards,
Thorsten

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

* Re: [PATCH] hwrng: virtio - prevent use-after-free after a failed restore
  2026-10-18 20:37   ` Thorsten Blum
@ 2026-10-19 13:00     ` PrasannaKumar Muralidharan
  0 siblings, 0 replies; 5+ messages in thread
From: PrasannaKumar Muralidharan @ 2026-10-19 13:00 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Laurent Vivier, Olivia Mackall, Herbert Xu, Rusty Russell,
	Amos Kong, virtualization, linux-crypto, linux-kernel

On Mon, 19 Oct 2026 at 02:07, Thorsten Blum <blum@kernel.org> wrote:
>
> Hi PrasannaKumar,
>
> On Sun, Oct 18, 2026 at 10:30:08PM +0530, PrasannaKumar Muralidharan wrote:
> > Hi Thorsten,
> >
> > On Fri, 16 Oct 2026 at 16:16, Thorsten Blum <blum@kernel.org> wrote:
> > >
> > > remove_common() frees the driver data during freeze, but does not clear
> > > vdev->priv, which points to freed memory. If probe_common() then fails
> > > during restore, a later remove or freeze can dereference the dangling
> > > pointer.
> > >
> > > Clear vdev->priv on the probe_common() error cleanup path and before
> > > freeing the driver data in remove_common(). Also reset the device and
> > > return early from remove_common() if vdev->priv is already NULL for a
> > > safe teardown after a failed restore.
> > >
> > > Fixes: 08e53fbdb85c ("virtio-rng: support multiple virtio-rng devices")
> > > Cc: stable@vger.kernel.org
> > > Signed-off-by: Thorsten Blum <blum@kernel.org>
> > > ---
> > >  drivers/char/hw_random/virtio-rng.c | 7 +++++++
> > >  1 file changed, 7 insertions(+)
> > >
> > > diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c
> > > index 5e83ffa105e4..a9ff520975a7 100644
> > > --- a/drivers/char/hw_random/virtio-rng.c
> > > +++ b/drivers/char/hw_random/virtio-rng.c
> > > @@ -190,6 +190,7 @@ static int probe_common(struct virtio_device *vdev)
> > >  err_find:
> > >         ida_free(&rng_index_ida, index);
> > >  err_ida:
> > > +       vdev->priv = NULL;
> > >         kfree(vi);
> > >         return err;
> > >  }
> > > @@ -198,6 +199,11 @@ static void remove_common(struct virtio_device *vdev)
> > >  {
> > >         struct virtrng_info *vi = vdev->priv;
> > >
> > > +       if (!vi) {
> > > +               virtio_reset_device(vdev);
> > > +               return;
> > > +       }
> > > +
> >
> > The virtio_reset_device() is always called before setting vdev->priv
> > to NULL. So the virtio device is already quiesced, no need to do early
> > return. Isn't it?
>
> The check is still needed.
>
> For example, during hibernation or suspend, .freeze (virtrng_freeze() ->
> remove_common()) frees vi and sets vdev->priv to NULL.
>
> On resume, .restore (virtrng_restore()) calls probe_common(), which
> could fail and leave vdev->priv NULL. A later .freeze or .remove calls
> remove_common() again, which would dereference NULL without the check.

Yes, it makes sense.

> Best regards,
> Thorsten

Change looks good to me.

Feel free to add:
Reviewed-by: PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>.

Thanks,
PrasannaKumar

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

end of thread, other threads:[~2026-10-19 13:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-16 10:36 [PATCH] hwrng: virtio - prevent use-after-free after a failed restore Thorsten Blum
2026-10-16 10:47 ` sashiko-bot
2026-10-18 17:00 ` PrasannaKumar Muralidharan
2026-10-18 20:37   ` Thorsten Blum
2026-10-19 13:00     ` PrasannaKumar Muralidharan

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.