[PATCH] media: cxusb: avoid double free on radio register failure

Guangshuo Li posted 1 patch 6 days, 17 hours ago
drivers/media/usb/dvb-usb/cxusb-analog.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH] media: cxusb: avoid double free on radio register failure
Posted by Guangshuo Li 6 days, 17 hours ago
cxusb_medion_register_analog_radio() allocates a video_device with
video_device_alloc() and releases it if video_register_device() fails.

This can double free the video_device when __video_register_device()
reaches device_register() and that call fails:

  video_register_device()
    -> __video_register_device()
       -> device_register() fails
          -> put_device(&vdev->dev)
             -> v4l2_device_release()
                -> vdev->release(vdev)
                   -> video_device_release(vdev)

  cxusb_medion_register_analog_radio()
    -> video_device_release(cxdev->radiodev)

Use video_device_release_empty() while registering the device so that
registration failure paths do not free cxdev->radiodev through
vdev->release(). cxusb_medion_register_analog_radio() then releases
cxdev->radiodev exactly once on failure. Restore video_device_release()
after successful registration so the registered device keeps its normal
lifetime handling.

This issue was found by a static analysis tool I am developing.

Fixes: e478d4054054 ("media: cxusb: add analog mode support for Medion MD95700")
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 drivers/media/usb/dvb-usb/cxusb-analog.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/media/usb/dvb-usb/cxusb-analog.c b/drivers/media/usb/dvb-usb/cxusb-analog.c
index 2d899af0d5c3..c31a46e59495 100644
--- a/drivers/media/usb/dvb-usb/cxusb-analog.c
+++ b/drivers/media/usb/dvb-usb/cxusb-analog.c
@@ -1690,7 +1690,7 @@ static int cxusb_medion_register_analog_radio(struct dvb_usb_device *dvbdev)
 	strscpy(cxdev->radiodev->name, "cxusb", sizeof(cxdev->radiodev->name));
 	cxdev->radiodev->vfl_dir = VFL_DIR_RX;
 	cxdev->radiodev->ioctl_ops = &cxusb_radio_ioctl;
-	cxdev->radiodev->release = video_device_release;
+	cxdev->radiodev->release = video_device_release_empty;
 	cxdev->radiodev->lock = &cxdev->dev_lock;
 	video_set_drvdata(cxdev->radiodev, dvbdev);
 
@@ -1702,6 +1702,7 @@ static int cxusb_medion_register_analog_radio(struct dvb_usb_device *dvbdev)
 		return ret;
 	}
 
+	cxdev->radiodev->release = video_device_release;
 	return 0;
 }
 
-- 
2.43.0