All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] media: msi2500: fix UAF in the msi2500_disconnect
@ 2021-11-16 16:32 Dongliang Mu
  2021-12-14 13:32 ` Mauro Carvalho Chehab
  0 siblings, 1 reply; 3+ messages in thread
From: Dongliang Mu @ 2021-11-16 16:32 UTC (permalink / raw)
  To: Antti Palosaari, Mauro Carvalho Chehab
  Cc: Dongliang Mu, linux-media, linux-kernel

In msi2500_probe, it invokes spi_alloc_master to allocate master
controller and spi_register_master to register master controller.
Then in msi2500_disconnect, it calls spi_unregister_master to unregister
the master controller. And in spi_unregister_master, it calls put_device
to decrease the refcount and the device object will be freed then. As a
result, the dereference of dev->lock will cause a use-after-free bug.

Fix it by changing spi_alloc_master to devm_spi_alloc_master, and
removing spi_master_put in the error handling code.

Note that this patch can prevent UAF occurring any more

Fixes: fd8b5f502929 ("msi2500: move msi3101 out of staging and rename")
Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>
---
 drivers/media/usb/msi2500/msi2500.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/media/usb/msi2500/msi2500.c b/drivers/media/usb/msi2500/msi2500.c
index 71de6b4c4e4c..64c3ec9f1d0c 100644
--- a/drivers/media/usb/msi2500/msi2500.c
+++ b/drivers/media/usb/msi2500/msi2500.c
@@ -1220,7 +1220,7 @@ static int msi2500_probe(struct usb_interface *intf,
 	}
 
 	/* SPI master adapter */
-	master = spi_alloc_master(dev->dev, 0);
+	master = devm_spi_alloc_master(dev->dev, 0);
 	if (master == NULL) {
 		ret = -ENOMEM;
 		goto err_unregister_v4l2_dev;
@@ -1233,7 +1233,6 @@ static int msi2500_probe(struct usb_interface *intf,
 	spi_master_set_devdata(master, dev);
 	ret = spi_register_master(master);
 	if (ret) {
-		spi_master_put(master);
 		goto err_unregister_v4l2_dev;
 	}
 
-- 
2.25.1


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

* Re: [PATCH] media: msi2500: fix UAF in the msi2500_disconnect
  2021-11-16 16:32 [PATCH] media: msi2500: fix UAF in the msi2500_disconnect Dongliang Mu
@ 2021-12-14 13:32 ` Mauro Carvalho Chehab
  2021-12-16  1:56   ` Dongliang Mu
  0 siblings, 1 reply; 3+ messages in thread
From: Mauro Carvalho Chehab @ 2021-12-14 13:32 UTC (permalink / raw)
  To: Dongliang Mu; +Cc: Antti Palosaari, linux-media, linux-kernel

Em Wed, 17 Nov 2021 00:32:07 +0800
Dongliang Mu <mudongliangabcd@gmail.com> escreveu:

> In msi2500_probe, it invokes spi_alloc_master to allocate master
> controller and spi_register_master to register master controller.
> Then in msi2500_disconnect, it calls spi_unregister_master to unregister
> the master controller. And in spi_unregister_master, it calls put_device
> to decrease the refcount and the device object will be freed then. As a
> result, the dereference of dev->lock will cause a use-after-free bug.
> 
> Fix it by changing spi_alloc_master to devm_spi_alloc_master, and
> removing spi_master_put in the error handling code.
> 
> Note that this patch can prevent UAF occurring any more
> 
> Fixes: fd8b5f502929 ("msi2500: move msi3101 out of staging and rename")
> Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>
> ---
>  drivers/media/usb/msi2500/msi2500.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/media/usb/msi2500/msi2500.c b/drivers/media/usb/msi2500/msi2500.c
> index 71de6b4c4e4c..64c3ec9f1d0c 100644
> --- a/drivers/media/usb/msi2500/msi2500.c
> +++ b/drivers/media/usb/msi2500/msi2500.c
> @@ -1220,7 +1220,7 @@ static int msi2500_probe(struct usb_interface *intf,
>  	}
>  
>  	/* SPI master adapter */
> -	master = spi_alloc_master(dev->dev, 0);
> +	master = devm_spi_alloc_master(dev->dev, 0);

We had some bad past experiences on using devm_* on USB media devices,
as there are almost always multiple drivers loaded and multiple
interfaces.

Could you please fix the free logic instead of using devm_*?

Regards,
Mauro

>  	if (master == NULL) {
>  		ret = -ENOMEM;
>  		goto err_unregister_v4l2_dev;
> @@ -1233,7 +1233,6 @@ static int msi2500_probe(struct usb_interface *intf,
>  	spi_master_set_devdata(master, dev);
>  	ret = spi_register_master(master);
>  	if (ret) {
> -		spi_master_put(master);
>  		goto err_unregister_v4l2_dev;
>  	}
>  



Thanks,
Mauro

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

* Re: [PATCH] media: msi2500: fix UAF in the msi2500_disconnect
  2021-12-14 13:32 ` Mauro Carvalho Chehab
@ 2021-12-16  1:56   ` Dongliang Mu
  0 siblings, 0 replies; 3+ messages in thread
From: Dongliang Mu @ 2021-12-16  1:56 UTC (permalink / raw)
  To: Mauro Carvalho Chehab; +Cc: Antti Palosaari, linux-media, linux-kernel

On Tue, Dec 14, 2021 at 9:32 PM Mauro Carvalho Chehab
<mchehab@kernel.org> wrote:
>
> Em Wed, 17 Nov 2021 00:32:07 +0800
> Dongliang Mu <mudongliangabcd@gmail.com> escreveu:
>
> > In msi2500_probe, it invokes spi_alloc_master to allocate master
> > controller and spi_register_master to register master controller.
> > Then in msi2500_disconnect, it calls spi_unregister_master to unregister
> > the master controller. And in spi_unregister_master, it calls put_device
> > to decrease the refcount and the device object will be freed then. As a
> > result, the dereference of dev->lock will cause a use-after-free bug.
> >
> > Fix it by changing spi_alloc_master to devm_spi_alloc_master, and
> > removing spi_master_put in the error handling code.
> >
> > Note that this patch can prevent UAF occurring any more
> >
> > Fixes: fd8b5f502929 ("msi2500: move msi3101 out of staging and rename")
> > Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>
> > ---
> >  drivers/media/usb/msi2500/msi2500.c | 3 +--
> >  1 file changed, 1 insertion(+), 2 deletions(-)
> >
> > diff --git a/drivers/media/usb/msi2500/msi2500.c b/drivers/media/usb/msi2500/msi2500.c
> > index 71de6b4c4e4c..64c3ec9f1d0c 100644
> > --- a/drivers/media/usb/msi2500/msi2500.c
> > +++ b/drivers/media/usb/msi2500/msi2500.c
> > @@ -1220,7 +1220,7 @@ static int msi2500_probe(struct usb_interface *intf,
> >       }
> >
> >       /* SPI master adapter */
> > -     master = spi_alloc_master(dev->dev, 0);
> > +     master = devm_spi_alloc_master(dev->dev, 0);
>
> We had some bad past experiences on using devm_* on USB media devices,
> as there are almost always multiple drivers loaded and multiple
> interfaces.
>
> Could you please fix the free logic instead of using devm_*?

Hi Mauro,

When I resume working on this patch, I found this UAF bug is already
fixed in the spi part and merged to the upstream.

The correct fix is spi: fix use-after-free of the add_lock mutex [1].
So let's forget this patch.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c53b45c71b4920b5e62f0ea8079a1da382b9434


>
> Regards,
> Mauro
>
> >       if (master == NULL) {
> >               ret = -ENOMEM;
> >               goto err_unregister_v4l2_dev;
> > @@ -1233,7 +1233,6 @@ static int msi2500_probe(struct usb_interface *intf,
> >       spi_master_set_devdata(master, dev);
> >       ret = spi_register_master(master);
> >       if (ret) {
> > -             spi_master_put(master);
> >               goto err_unregister_v4l2_dev;
> >       }
> >
>
>
>
> Thanks,
> Mauro

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

end of thread, other threads:[~2021-12-16  1:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-16 16:32 [PATCH] media: msi2500: fix UAF in the msi2500_disconnect Dongliang Mu
2021-12-14 13:32 ` Mauro Carvalho Chehab
2021-12-16  1:56   ` Dongliang Mu

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.