Skip to contentSkip to conversation

media: sti: delta: avoid double free on video register failure

PATCH New Guangshuo Li 1 participant 1 message May 18

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

PATCH 1/1 media: sti: delta: avoid double free on video register failure

delta_register_device() allocates a video_device with video_device_alloc() and releases it from the err_vdev_release error path 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)

delta_register_device()

    -> err_vdev_release
       -> video_device_release(vdev)

Use video_device_release_empty() while registering the device so that registration failure paths do not free vdev through vdev->release(). delta_register_device() then releases vdev exactly once from err_vdev_release. 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 f386509e4959 ("[media] st-delta: STiH4xx multi-format video decoder v4l2 driver")
---
 drivers/media/platform/st/sti/delta/delta-v4l2.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/media/platform/st/sti/delta/delta-v4l2.c +2 -1
index 7dd27de37b78..0c6ac4bdaa9d 100644
@@ -1767,7 +1767,7 @@ static int delta_register_device(struct delta_dev *delta)
 
 	vdev->fops = &delta_fops;
 	vdev->ioctl_ops = &delta_ioctl_ops;
-	vdev->release = video_device_release;
+	vdev->release = video_device_release_empty;
 	vdev->lock = &delta->lock;
 	vdev->vfl_dir = VFL_DIR_M2M;
 	vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M;
@@ -1781,6 +1781,7 @@ static int delta_register_device(struct delta_dev *delta)
 			DELTA_PREFIX);
 		goto err_vdev_release;
 	}
+	vdev->release = video_device_release;
 
 	delta->vdev = vdev;
 	video_set_drvdata(vdev, delta);
Signature · 1 line
2.43.0