[PATCH] media: sti: delta: avoid double free on video register failure

Guangshuo Li posted 1 patch 6 days, 15 hours ago
drivers/media/platform/st/sti/delta/delta-v4l2.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH] media: sti: delta: avoid double free on video register failure
Posted by Guangshuo Li 6 days, 15 hours ago
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")
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 drivers/media/platform/st/sti/delta/delta-v4l2.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/media/platform/st/sti/delta/delta-v4l2.c b/drivers/media/platform/st/sti/delta/delta-v4l2.c
index 7dd27de37b78..0c6ac4bdaa9d 100644
--- a/drivers/media/platform/st/sti/delta/delta-v4l2.c
+++ b/drivers/media/platform/st/sti/delta/delta-v4l2.c
@@ -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);
-- 
2.43.0