media: msi2500: fix UAF in the msi2500_disconnect
Subsystems (2)
PATCH 1/1 media: msi2500: fix UAF in the msi2500_disconnect
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
--- drivers/media/usb/msi2500/msi2500.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/media/usb/msi2500/msi2500.c +1 -2
@@ -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; }
Signature · 1 line
2.25.1
Mauro Carvalho Chehab
maintainerEm Wed, 17 Nov 2021 00:32:07 +0800 Dongliang Mu <[email protected]> escreveu:
Quoted 23 lines
In
msi2500_probe, it invokesspi_alloc_masterto allocate master controller andspi_register_masterto register master controller. Then inmsi2500_disconnect, it callsspi_unregister_masterto unregister the master controller. And inspi_unregister_master, it callsput_deviceto decrease the refcount and the device object will be freed then. As a result, the dereference ofdev->lockwill cause a use-after-free bug.Fix it by changing
spi_alloc_mastertodevm_spi_alloc_master, and removingspi_master_putin 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 <[email protected]>--- drivers/media/usb/msi2500/msi2500.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)drivers/media/usb/msi2500/msi2500.c +0
-0
drivers/media/usb/msi2500/msi2500.c static int msi2500_probe(struct usb_interface *intf, · line 1223
Quoted 5 lines
drivers/media/usb/msi2500/msi2500.c +1
-1@@ -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
drivers/media/usb/msi2500/msi2500.c static int msi2500_probe(struct usb_interface *intf, · line 1238
Quoted 11 lines
drivers/media/usb/msi2500/msi2500.c +0
-1if (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
Quoted 37 lines · Mauro Carvalho Chehab
Em Wed, 17 Nov 2021 00:32:07 +0800 Dongliang Mu <[email protected]> escreveu:
Quoted 23 lines
In
msi2500_probe, it invokesspi_alloc_masterto allocate master controller andspi_register_masterto register master controller. Then inmsi2500_disconnect, it callsspi_unregister_masterto unregister the master controller. And inspi_unregister_master, it callsput_deviceto decrease the refcount and the device object will be freed then. As a result, the dereference ofdev->lockwill cause a use-after-free bug.Fix it by changing
spi_alloc_mastertodevm_spi_alloc_master, and removingspi_master_putin 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 <[email protected]>--- drivers/media/usb/msi2500/msi2500.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)drivers/media/usb/msi2500/msi2500.c +0
-0drivers/media/usb/msi2500/msi2500.c static int msi2500_probe(struct usb_interface *intf, · line 1223
Quoted 5 lines
drivers/media/usb/msi2500/msi2500.c +1
-1@@ -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.
Quoted 1 line
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.
Quoted 20 lines
Regards,
Maurodrivers/media/usb/msi2500/msi2500.c static int msi2500_probe(struct usb_interface *intf, · line 1238
Quoted 11 lines
drivers/media/usb/msi2500/msi2500.c +0
-1if (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