Skip to contentSkip to conversation

media: gspca: don't call stopN/stop0 after device disconnect

PATCH In review Deepanshu Kartikey 2 participants 2 messages Aug 30 – Sep 8

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: gspca: don't call stopN/stop0 after device disconnect

gspca_stream_off() calls the sub-driver's stopN() and stop0() callbacks unconditionally. These issue USB requests using gspca_dev->dev, which becomes a dangling pointer once the USB device has been disconnected and freed, causing a use-after-free when the video device fd is closed after disconnect.

Add a gspca_dev->present check before calling stopN() and stop0(), matching the existing check already used for gspca_input_create_urb() in the same function.

Fixes 1f5965c4dfd7 ("media: gspca: convert to vb2")
Reported-by [email protected]
---
 drivers/media/usb/gspca/gspca.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/gspca/gspca.c +2 -2
index 594d73e50b9f..5c33cf397f3d 100644
@@ -733,14 +733,14 @@ static void gspca_stream_off(struct gspca_dev *gspca_dev)
 {
 	gspca_dev->streaming = false;
 	gspca_dev->usb_err = 0;
-	if (gspca_dev->sd_desc->stopN)
+	if (gspca_dev->present && gspca_dev->sd_desc->stopN)
 		gspca_dev->sd_desc->stopN(gspca_dev);
 	destroy_urbs(gspca_dev);
 	gspca_input_destroy_urb(gspca_dev);
 	gspca_set_alt0(gspca_dev);
 	if (gspca_dev->present)
 		gspca_input_create_urb(gspca_dev);
-	if (gspca_dev->sd_desc->stop0)
+	if (gspca_dev->present && gspca_dev->sd_desc->stop0)
 		gspca_dev->sd_desc->stop0(gspca_dev);
 	gspca_dbg(gspca_dev, D_STREAM, "stream off OK\n");
 }
Signature · 1 line
2.43.0
#1 lore raw
Quoted 10 lines · Deepanshu Kartikey

gspca_stream_off() calls the sub-driver's stopN() and stop0() callbacks unconditionally. These issue USB requests using gspca_dev->dev, which becomes a dangling pointer once the USB device has been disconnected and freed, causing a use-after-free when the video device fd is closed after disconnect.

Add a gspca_dev->present check before calling stopN() and stop0(), matching the existing check already used for gspca_input_create_urb() in the same function.

Quoted 5 lines
Fixes 1f5965c4dfd7 ("media: gspca: convert to vb2")
Reported-by [email protected]

This is superseded by:

https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/

That's the correct fix since it forces streaming off before freeing anything.

Regards,

Hans

Quoted 25 lines
---
 drivers/media/usb/gspca/gspca.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/gspca/gspca.c +2 -2
index 594d73e50b9f..5c33cf397f3d 100644
@@ -733,14 +733,14 @@ static void gspca_stream_off(struct gspca_dev *gspca_dev)
 {
 	gspca_dev->streaming = false;
 	gspca_dev->usb_err = 0;
-	if (gspca_dev->sd_desc->stopN)
+	if (gspca_dev->present && gspca_dev->sd_desc->stopN)
 		gspca_dev->sd_desc->stopN(gspca_dev);
 	destroy_urbs(gspca_dev);
 	gspca_input_destroy_urb(gspca_dev);
 	gspca_set_alt0(gspca_dev);
 	if (gspca_dev->present)
 		gspca_input_create_urb(gspca_dev);
-	if (gspca_dev->sd_desc->stop0)
+	if (gspca_dev->present && gspca_dev->sd_desc->stop0)
 		gspca_dev->sd_desc->stop0(gspca_dev);
 	gspca_dbg(gspca_dev, D_STREAM, "stream off OK\n");
 }