virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] vduse: Fix off by one in vduse_dev_mmap()
@ 2024-02-28 18:24 Dan Carpenter
  2024-02-28 19:19 ` Stefan Hajnoczi
  2024-03-19  7:23 ` Michael S. Tsirkin
  0 siblings, 2 replies; 4+ messages in thread
From: Dan Carpenter @ 2024-02-28 18:24 UTC (permalink / raw)
  To: Cindy Lu
  Cc: Michael S. Tsirkin, Jason Wang, Xuan Zhuo, Xie Yongji,
	Maxime Coquelin, Greg Kroah-Hartman, Christian Brauner,
	virtualization, linux-kernel, kernel-janitors

The dev->vqs[] array has "dev->vq_num" elements.  It's allocated in
vduse_dev_init_vqs().  Thus, this > comparison needs to be >= to avoid
reading one element beyond the end of the array.

Add an array_index_nospec() as well to prevent speculation issues.

Fixes: 316ecd1346b0 ("vduse: Add file operation for mmap")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
---
v2: add array_index_nospec()
v3: I accidentally corrupted v2.  Try again.

 drivers/vdpa/vdpa_user/vduse_dev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vduse_dev.c
index b7a1fb88c506..eb914084c650 100644
--- a/drivers/vdpa/vdpa_user/vduse_dev.c
+++ b/drivers/vdpa/vdpa_user/vduse_dev.c
@@ -1532,9 +1532,10 @@ static int vduse_dev_mmap(struct file *file, struct vm_area_struct *vma)
 	if ((vma->vm_flags & VM_SHARED) == 0)
 		return -EINVAL;
 
-	if (index > dev->vq_num)
+	if (index >= dev->vq_num)
 		return -EINVAL;
 
+	index = array_index_nospec(index, dev->vq_num);
 	vq = dev->vqs[index];
 	vaddr = vq->vdpa_reconnect_vaddr;
 	if (vaddr == 0)
-- 
2.43.0


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

* Re: [PATCH v3] vduse: Fix off by one in vduse_dev_mmap()
  2024-02-28 18:24 [PATCH v3] vduse: Fix off by one in vduse_dev_mmap() Dan Carpenter
@ 2024-02-28 19:19 ` Stefan Hajnoczi
  2024-02-29  7:21   ` Cindy Lu
  2024-03-19  7:23 ` Michael S. Tsirkin
  1 sibling, 1 reply; 4+ messages in thread
From: Stefan Hajnoczi @ 2024-02-28 19:19 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Cindy Lu, Michael S. Tsirkin, Jason Wang, Xuan Zhuo, Xie Yongji,
	Maxime Coquelin, Greg Kroah-Hartman, Christian Brauner,
	virtualization, linux-kernel, kernel-janitors

On Wed, 28 Feb 2024 at 13:24, Dan Carpenter <dan.carpenter@linaro.org> wrote:
>
> The dev->vqs[] array has "dev->vq_num" elements.  It's allocated in
> vduse_dev_init_vqs().  Thus, this > comparison needs to be >= to avoid
> reading one element beyond the end of the array.
>
> Add an array_index_nospec() as well to prevent speculation issues.
>
> Fixes: 316ecd1346b0 ("vduse: Add file operation for mmap")
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> ---
> v2: add array_index_nospec()
> v3: I accidentally corrupted v2.  Try again.
>
>  drivers/vdpa/vdpa_user/vduse_dev.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>

> diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vduse_dev.c
> index b7a1fb88c506..eb914084c650 100644
> --- a/drivers/vdpa/vdpa_user/vduse_dev.c
> +++ b/drivers/vdpa/vdpa_user/vduse_dev.c
> @@ -1532,9 +1532,10 @@ static int vduse_dev_mmap(struct file *file, struct vm_area_struct *vma)
>         if ((vma->vm_flags & VM_SHARED) == 0)
>                 return -EINVAL;
>
> -       if (index > dev->vq_num)
> +       if (index >= dev->vq_num)
>                 return -EINVAL;
>
> +       index = array_index_nospec(index, dev->vq_num);
>         vq = dev->vqs[index];
>         vaddr = vq->vdpa_reconnect_vaddr;
>         if (vaddr == 0)
> --
> 2.43.0
>
>

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

* Re: [PATCH v3] vduse: Fix off by one in vduse_dev_mmap()
  2024-02-28 19:19 ` Stefan Hajnoczi
@ 2024-02-29  7:21   ` Cindy Lu
  0 siblings, 0 replies; 4+ messages in thread
From: Cindy Lu @ 2024-02-29  7:21 UTC (permalink / raw)
  To: Stefan Hajnoczi
  Cc: Dan Carpenter, Michael S. Tsirkin, Jason Wang, Xuan Zhuo,
	Xie Yongji, Maxime Coquelin, Greg Kroah-Hartman,
	Christian Brauner, virtualization, linux-kernel, kernel-janitors

On Thu, Feb 29, 2024 at 3:19 AM Stefan Hajnoczi <stefanha@gmail.com> wrote:
>
> On Wed, 28 Feb 2024 at 13:24, Dan Carpenter <dan.carpenter@linaro.org> wrote:
> >
> > The dev->vqs[] array has "dev->vq_num" elements.  It's allocated in
> > vduse_dev_init_vqs().  Thus, this > comparison needs to be >= to avoid
> > reading one element beyond the end of the array.
> >
> > Add an array_index_nospec() as well to prevent speculation issues.
> >
> > Fixes: 316ecd1346b0 ("vduse: Add file operation for mmap")
> > Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> > ---
> > v2: add array_index_nospec()
> > v3: I accidentally corrupted v2.  Try again.
> >
Thanks for fix this
> >  drivers/vdpa/vdpa_user/vduse_dev.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
>
> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by:  Cindy Lu <lulu@redhat.com>
> > diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vduse_dev.c
> > index b7a1fb88c506..eb914084c650 100644
> > --- a/drivers/vdpa/vdpa_user/vduse_dev.c
> > +++ b/drivers/vdpa/vdpa_user/vduse_dev.c
> > @@ -1532,9 +1532,10 @@ static int vduse_dev_mmap(struct file *file, struct vm_area_struct *vma)
> >         if ((vma->vm_flags & VM_SHARED) == 0)
> >                 return -EINVAL;
> >
> > -       if (index > dev->vq_num)
> > +       if (index >= dev->vq_num)
> >                 return -EINVAL;
> >
> > +       index = array_index_nospec(index, dev->vq_num);
> >         vq = dev->vqs[index];
> >         vaddr = vq->vdpa_reconnect_vaddr;
> >         if (vaddr == 0)
> > --
> > 2.43.0
> >
> >
>


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

* Re: [PATCH v3] vduse: Fix off by one in vduse_dev_mmap()
  2024-02-28 18:24 [PATCH v3] vduse: Fix off by one in vduse_dev_mmap() Dan Carpenter
  2024-02-28 19:19 ` Stefan Hajnoczi
@ 2024-03-19  7:23 ` Michael S. Tsirkin
  1 sibling, 0 replies; 4+ messages in thread
From: Michael S. Tsirkin @ 2024-03-19  7:23 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Cindy Lu, Jason Wang, Xuan Zhuo, Xie Yongji, Maxime Coquelin,
	Greg Kroah-Hartman, Christian Brauner, virtualization,
	linux-kernel, kernel-janitors

On Wed, Feb 28, 2024 at 09:24:07PM +0300, Dan Carpenter wrote:
> The dev->vqs[] array has "dev->vq_num" elements.  It's allocated in
> vduse_dev_init_vqs().  Thus, this > comparison needs to be >= to avoid
> reading one element beyond the end of the array.
> 
> Add an array_index_nospec() as well to prevent speculation issues.
> 
> Fixes: 316ecd1346b0 ("vduse: Add file operation for mmap")
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>

Thanks a lot!
I assume this will be squashed in the relevant patch when that is
re-spun.

> ---
> v2: add array_index_nospec()
> v3: I accidentally corrupted v2.  Try again.
> 
>  drivers/vdpa/vdpa_user/vduse_dev.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vduse_dev.c
> index b7a1fb88c506..eb914084c650 100644
> --- a/drivers/vdpa/vdpa_user/vduse_dev.c
> +++ b/drivers/vdpa/vdpa_user/vduse_dev.c
> @@ -1532,9 +1532,10 @@ static int vduse_dev_mmap(struct file *file, struct vm_area_struct *vma)
>  	if ((vma->vm_flags & VM_SHARED) == 0)
>  		return -EINVAL;
>  
> -	if (index > dev->vq_num)
> +	if (index >= dev->vq_num)
>  		return -EINVAL;
>  
> +	index = array_index_nospec(index, dev->vq_num);
>  	vq = dev->vqs[index];
>  	vaddr = vq->vdpa_reconnect_vaddr;
>  	if (vaddr == 0)
> -- 
> 2.43.0


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

end of thread, other threads:[~2024-03-19  7:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-28 18:24 [PATCH v3] vduse: Fix off by one in vduse_dev_mmap() Dan Carpenter
2024-02-28 19:19 ` Stefan Hajnoczi
2024-02-29  7:21   ` Cindy Lu
2024-03-19  7:23 ` Michael S. Tsirkin

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).