media: platform: sun4i_csi: Add missing media_entity_cleanup() + fix UAF
Subsystems (6)
PATCH v2 1/3 media: platform: sun4i_csi: Add missing media_entity_cleanup() + fix UAF
This patch adds missing calls to media_entity_cleanup() in the probe error paths and the remove function to properly release media entities.
Additionally, this patch completely restructures the error rollback cascades in sun4i_csi_probe() and sun4i_csi_notify_complete(). Previously, early initialization failures in probe would return directly, permanently leaking IDA internal structures initialized by media_device_init(). Furthermore, failures in the async notifier complete callback would return without unregistering the exposed video device, leading to a Use-After-Free. A v4l2_async_nf_cleanup() leak was also fixed in the probe path.
--- drivers/media/platform/sunxi/sun4i-csi/sun4i_csi.c | 56 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 19 deletions(-)
drivers/media/platform/sunxi/sun4i-csi/sun4i_csi.c +37 -19
@@ -85,11 +85,11 @@ static int sun4i_csi_notify_complete(struct v4l2_async_notifier *notifier) ret =sun4i_csi_v4l2_register (csi);if (ret <0 ) -return ret; + goto err_unregister_subdev; ret =media_device_register (&csi->mdev);if (ret) -return ret; + goto err_unregister_vdev;/* Create link from subdev to main device */ ret = media_create_pad_link(&subdev->entity, CSI_SUBDEV_SOURCE,
@@ -114,6 +114,10 @@ static int sun4i_csi_notify_complete(struct v4l2_async_notifier *notifier) err_clean_media:media_device_unregister (&csi->mdev); +err_unregister_vdev: +vb2_video_unregister_device (&csi->vdev); +err_unregister_subdev: +v4l2_device_unregister_subdev (subdev);return ret; }
@@ -187,37 +191,45 @@ static int sun4i_csi_probe(struct platform_device *pdev) csi->v4l.mdev = &csi->mdev; csi->regs =devm_platform_ioremap_resource (pdev,0 ); -if (IS_ERR (csi->regs)) -return PTR_ERR (csi->regs); +if (IS_ERR (csi->regs)) { + ret =PTR_ERR (csi->regs); + goto err_clean_media_dev; + } irq =platform_get_irq (pdev,0 ); -if (irq <0 ) -return irq; +if (irq <0 ) { + ret = irq; + goto err_clean_media_dev; + } csi->bus_clk =devm_clk_get (&pdev->dev,"bus" );if (IS_ERR (csi->bus_clk)) {dev_err (&pdev->dev,"Couldn't get our bus clock\n" ); -return PTR_ERR (csi->bus_clk); + ret =PTR_ERR (csi->bus_clk); + goto err_clean_media_dev; }if (csi->traits->has_isp) { csi->isp_clk =devm_clk_get (&pdev->dev,"isp" );if (IS_ERR (csi->isp_clk)) {dev_err (&pdev->dev,"Couldn't get our ISP clock\n" ); -return PTR_ERR (csi->isp_clk); + ret =PTR_ERR (csi->isp_clk); + goto err_clean_media_dev; } } csi->ram_clk =devm_clk_get (&pdev->dev,"ram" );if (IS_ERR (csi->ram_clk)) {dev_err (&pdev->dev,"Couldn't get our ram clock\n" ); -return PTR_ERR (csi->ram_clk); + ret =PTR_ERR (csi->ram_clk); + goto err_clean_media_dev; } csi->rst =devm_reset_control_get (&pdev->dev,NULL );if (IS_ERR (csi->rst)) {dev_err (&pdev->dev,"Couldn't get our reset line\n" ); -return PTR_ERR (csi->rst); + ret =PTR_ERR (csi->rst); + goto err_clean_media_dev; }/* Initialize subdev */
@@ -236,37 +248,41 @@ static int sun4i_csi_probe(struct platform_device *pdev) ret =media_entity_pads_init (&subdev->entity,CSI_SUBDEV_PADS , csi->subdev_pads);if (ret <0 ) -return ret; + goto err_clean_media_dev; csi->vdev_pad.flags =MEDIA_PAD_FL_SINK |MEDIA_PAD_FL_MUST_CONNECT ; vdev->entity.ops = &sun4i_csi_video_entity_ops; ret =media_entity_pads_init (&vdev->entity,1 , &csi->vdev_pad);if (ret <0 ) -return ret; + goto err_clean_subdev_entity; ret =sun4i_csi_dma_register (csi, irq);if (ret) - goto err_clean_pad; + goto err_clean_vdev_entity; ret =sun4i_csi_notifier_init (csi);if (ret) - goto err_unregister_media; + goto err_unregister_dma; ret =v4l2_async_nf_register (&csi->notifier);if (ret) {dev_err (csi->dev,"Couldn't register our notifier.\n" ); - goto err_unregister_media; + goto err_clean_notifier; }pm_runtime_enable (&pdev->dev);return 0 ; -err_unregister_media: -media_device_unregister (&csi->mdev); +err_clean_notifier: +v4l2_async_nf_cleanup (&csi->notifier); +err_unregister_dma:sun4i_csi_dma_unregister (csi); - -err_clean_pad: +err_clean_vdev_entity: +media_entity_cleanup (&vdev->entity); +err_clean_subdev_entity: +media_entity_cleanup (&subdev->entity); +err_clean_media_dev:media_device_cleanup (&csi->mdev);return ret;
@@ -282,6 +298,8 @@ static void sun4i_csi_remove(struct platform_device *pdev)vb2_video_unregister_device (&csi->vdev);media_device_unregister (&csi->mdev);sun4i_csi_dma_unregister (csi); +media_entity_cleanup (&csi->subdev.entity); +media_entity_cleanup (&csi->vdev.entity);media_device_cleanup (&csi->mdev); }
PATCH v2 2/3 media: platform: rcar-vin: Add missing media_entity_cleanup() + fix UAF
This patch adds a missing call to media_entity_cleanup() in the probe error path and remove function to properly release media entities.
Additionally, it fixes a severe Use-After-Free vulnerability in rvin_group_notify_complete(). If the registration of one of the video nodes (or the subsequent link setup) failed, the function returned directly. This skipped the cleanup of previously registered video nodes and the media_device, exposing them to userspace.
If the probe function then aborted and tore down the parent structures, any subsequent userspace access to the stranded /dev/videoX nodes would result in a Use-After-Free.
This patch adds a proper cascading rollback block to unregister the video devices and the media device before returning the error.
--- drivers/media/platform/renesas/rcar-vin/rcar-core.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)
drivers/media/platform/renesas/rcar-vin/rcar-core.c +18 -3
@@ -219,7 +219,7 @@ static int rvin_group_notify_complete(struct v4l2_async_notifier *notifier) ret =v4l2_device_register_subdev_nodes (&vin->v4l2_dev);if (ret) {vin_err (vin,"Failed to register subdev nodes\n" ); -return ret; + goto err_unregister_media; }/* Register all video nodes for the group. */
@@ -228,11 +228,24 @@ static int rvin_group_notify_complete(struct v4l2_async_notifier *notifier) !video_is_registered(&vin->group->vin[i]->vdev)) { ret =rvin_v4l2_register (vin->group->vin[i]);if (ret) -return ret; + goto err_unregister_vdevs; } } -return vin->group->link_setup (vin->group); + ret = vin->group->link_setup (vin->group); +if (ret) + goto err_unregister_vdevs; + +return 0 ; + +err_unregister_vdevs: +for (i =0 ; i <RCAR_VIN_NUM ; i++) { +if (vin->group->vin[i] &&video_is_registered (&vin->group->vin[i]->vdev)) +rvin_v4l2_unregister (vin->group->vin[i]); + } +err_unregister_media: +media_device_unregister (&vin->group->mdev); +return ret; } static void rvin_group_notify_unbind(struct v4l2_async_notifier *notifier,
@@ -1250,6 +1263,7 @@ static int rcar_vin_probe(struct platform_device *pdev)rvin_id_put (vin); err_dma:rvin_dma_unregister (vin); +media_entity_cleanup (&vin->vdev.entity);return ret; }
@@ -1274,6 +1288,7 @@ static void rcar_vin_remove(struct platform_device *pdev)rvin_id_put (vin);rvin_dma_unregister (vin); +media_entity_cleanup (&vin->vdev.entity); } static DEFINE_SIMPLE_DEV_PM_OPS(rvin_pm_ops, rvin_suspend, rvin_resume);
PATCH v2 3/3 media: platform: rzg2l-cru: Add missing media_entity_cleanup() + fix UAF
In reply to Biren PandyaThis patch adds a missing call to media_entity_cleanup() in the probe error path and remove function to properly release media entities.
Additionally, it fixes a severe Use-After-Free vulnerability in rzg2l_cru_group_notify_complete(). If media_create_pad_link() failed after successfully registering the video device and IP subdev, the function returned directly without unregistering them.
If the probe function then tore down the parent structures, any subsequent userspace access to the stranded video node would result in a Use-After-Free.
This patch adds a cascading rollback block to unregister the video device and IP subdev on failure.
--- drivers/media/platform/renesas/rzg2l-cru/rzg2l-core.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/media/platform/renesas/rzg2l-cru/rzg2l-core.c +12 -4
@@ -50,12 +50,12 @@ static int rzg2l_cru_group_notify_complete(struct v4l2_async_notifier *notifier) ret =v4l2_device_register_subdev_nodes (&cru->v4l2_dev);if (ret) {dev_err (cru->dev,"Failed to register subdev nodes\n" ); -return ret; + goto err_unregister_ip_subdev; } ret =rzg2l_cru_video_register (cru);if (ret) -return ret; + goto err_unregister_ip_subdev;/* * CRU can be connected either to CSI2 or PARALLEL device
@@ -71,7 +71,7 @@ static int rzg2l_cru_group_notify_complete(struct v4l2_async_notifier *notifier)if (ret) {dev_err (cru->dev,"Error creating link from %s to %s\n" , source->name, sink->name); -return ret; + goto err_unregister_video; } cru->ip.remote = cru->csi.subdev;
@@ -84,10 +84,16 @@ static int rzg2l_cru_group_notify_complete(struct v4l2_async_notifier *notifier)if (ret) {dev_err (cru->dev,"Error creating link from %s to %s\n" , source->name, sink->name); -return ret; + goto err_unregister_video; }return 0 ; + +err_unregister_video: +rzg2l_cru_video_unregister (cru); +err_unregister_ip_subdev: +rzg2l_cru_ip_subdev_unregister (cru); +return ret; } static void rzg2l_cru_group_notify_unbind(struct v4l2_async_notifier *notifier,
@@ -300,6 +306,7 @@ static int rzg2l_cru_probe(struct platform_device *pdev) error_dma_unregister:rzg2l_cru_dma_unregister (cru); +media_entity_cleanup (&cru->vdev.entity);return ret; }
@@ -316,6 +323,7 @@ static void rzg2l_cru_remove(struct platform_device *pdev)mutex_destroy (&cru->mdev_lock);rzg2l_cru_dma_unregister (cru); +media_entity_cleanup (&cru->vdev.entity); }static const u16 rzg3e_cru_regs[] = {