media: davinci: vpif: fix use-after-free on driver unbind
Subsystems (2)
cover media: davinci: vpif: fix use-after-free on driver unbind
When scanning the tree for drivers that did not handle platform-device registration failures properly I stumbled upon this driver, which also failed to deregister device structures before freeing them.
Included are also some related runtime PM imbalance fixes and a printk clean up.
Johan
Johan Hovold (4):
media: davinci: vpif: fix unbalanced runtime PM get
media: davinci: vpif: fix unbalanced runtime PM enable
media: davinci: vpif: fix use-after-free on driver unbind
media: davinci: vpif: drop probe printk
drivers/media/platform/davinci/vpif.c | 111 +++++++++++++++++++------- 1 file changed, 81 insertions(+), 30 deletions(-)
Signature · 1 line
2.32.0
PATCH 1/4 media: davinci: vpif: fix unbalanced runtime PM get
Make sure to balance the runtime PM usage counter on driver unbind.
--- drivers/media/platform/davinci/vpif.c | 1 + 1 file changed, 1 insertion(+)
drivers/media/platform/davinci/vpif.c +1 -0
@@ -495,6 +495,7 @@ static int vpif_probe(struct platform_device *pdev)static int vpif_remove (struct platform_device *pdev) { +pm_runtime_put (&pdev->dev);pm_runtime_disable (&pdev->dev);return 0 ; }
Signature · 1 line
2.32.0
Re: [PATCH 1/4] media: davinci: vpif: fix unbalanced runtime PM get
Hi Johan,
Thank you for the patch.
Quoted 11 lines · Johan Hovold
Make sure to balance the runtime PM usage counter on driver unbind.
Fixes 407ccc65bfd2 ("[media] davinci: vpif: add pm_runtime support")Cc [email protected] # 3.9Cc Lad, Prabhakar <[email protected]>Signed-off-by Johan Hovold <[email protected]>--- drivers/media/platform/davinci/vpif.c | 1 + 1 file changed, 1 insertion(+)
Quoted 13 lines
drivers/media/platform/davinci/vpif.c +1
-1@@ -495,6 +495,7 @@ static int vpif_probe(struct platform_device *pdev)static int vpif_remove (struct platform_device *pdev) { +pm_runtime_put (&pdev->dev);pm_runtime_disable (&pdev->dev);return 0 ; } --
Quoted 2 lines
2.32.0
Cheers,
Prabhakar
PATCH 2/4 media: davinci: vpif: fix unbalanced runtime PM enable
In reply to Johan HovoldMake sure to disable runtime PM before returning on probe errors.
--- drivers/media/platform/davinci/vpif.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/media/platform/davinci/vpif.c +9 -2
@@ -428,6 +428,7 @@ static int vpif_probe(struct platform_device *pdev)static struct resource *res_irq;struct platform_device *pdev_capture, *pdev_display;struct device_node *endpoint =NULL ; +int ret; vpif_base =devm_platform_ioremap_resource (pdev,0 );if (IS_ERR (vpif_base))
@@ -456,8 +457,8 @@ static int vpif_probe(struct platform_device *pdev) res_irq =platform_get_resource (pdev,IORESOURCE_IRQ ,0 );if (!res_irq) {dev_warn (&pdev->dev,"Missing IRQ resource.\n" ); -pm_runtime_put (&pdev->dev); -return -EINVAL ; + ret = -EINVAL ; + goto err_put_rpm; } pdev_capture = devm_kzalloc(&pdev->dev, sizeof(*pdev_capture),
@@ -491,6 +492,12 @@ static int vpif_probe(struct platform_device *pdev) }return 0 ; + +err_put_rpm: +pm_runtime_put (&pdev->dev); +pm_runtime_disable (&pdev->dev); + +return ret; }static int vpif_remove (struct platform_device *pdev)
Signature · 1 line
2.32.0
Re: [PATCH 2/4] media: davinci: vpif: fix unbalanced runtime PM enable
Hi Johan,
Thank you for the patch.
Quoted 24 lines · Johan Hovold
Make sure to disable runtime PM before returning on probe errors.
Fixes 479f7a118105 ("[media] davinci: vpif: adaptions for DT support")Cc [email protected] # 4.12: 4024d6f601e3cCc Kevin Hilman <[email protected]>Signed-off-by Johan Hovold <[email protected]>--- drivers/media/platform/davinci/vpif.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)drivers/media/platform/davinci/vpif.c +1
-0@@ -428,6 +428,7 @@ static int vpif_probe(struct platform_device *pdev)static struct resource *res_irq;struct platform_device *pdev_capture, *pdev_display;struct device_node *endpoint =NULL ; +int ret; vpif_base =devm_platform_ioremap_resource (pdev,0 );if (IS_ERR (vpif_base))
drivers/media/platform/davinci/vpif.c static int vpif_probe(struct platform_device *pdev) · line 459
Quoted 4 lines
drivers/media/platform/davinci/vpif.c +0
-1@@ -456,8 +457,8 @@ static int vpif_probe(struct platform_device *pdev) res_irq =platform_get_resource (pdev,IORESOURCE_IRQ ,0 );if (!res_irq) {dev_warn (&pdev->dev,"Missing IRQ resource.\n" ); -pm_runtime_put (&pdev->dev);
Maybe just add pm_runtime_disable(&pdev->dev); here, rest diff won't be required.
Cheers,
Prabhakar
Quoted 22 lines
drivers/media/platform/davinci/vpif.c +8
-2-return -EINVAL ; + ret = -EINVAL ; + goto err_put_rpm; } pdev_capture = devm_kzalloc(&pdev->dev, sizeof(*pdev_capture),@@ -491,6 +492,12 @@ static int vpif_probe(struct platform_device *pdev) }return 0 ; + +err_put_rpm: +pm_runtime_put (&pdev->dev); +pm_runtime_disable (&pdev->dev); + +return ret; }static int vpif_remove (struct platform_device *pdev) --2.32.0
Re: [PATCH 2/4] media: davinci: vpif: fix unbalanced runtime PM enable
Hi Lad,
and sorry about the late reply.
Quoted 26 lines · Prabhakar
Hi Johan,
Thank you for the patch.
Quoted 21 lines · Johan Hovold
Make sure to disable runtime PM before returning on probe errors.
Fixes 479f7a118105 ("[media] davinci: vpif: adaptions for DT support")Cc [email protected] # 4.12: 4024d6f601e3cCc Kevin Hilman <[email protected]>Signed-off-by Johan Hovold <[email protected]>--- drivers/media/platform/davinci/vpif.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)drivers/media/platform/davinci/vpif.c +1
-0@@ -428,6 +428,7 @@ static int vpif_probe(struct platform_device *pdev)static struct resource *res_irq;struct platform_device *pdev_capture, *pdev_display;struct device_node *endpoint =NULL ; +int ret;
Quoted 9 lines
drivers/media/platform/davinci/vpif.c static int vpif_probe(struct platform_device *pdev) · line 459
Quoted 7 lines
drivers/media/platform/davinci/vpif.c +0
-1vpif_base =devm_platform_ioremap_resource (pdev,0 );if (IS_ERR (vpif_base))@@ -456,8 +457,8 @@ static int vpif_probe(struct platform_device *pdev) res_irq =platform_get_resource (pdev,IORESOURCE_IRQ ,0 );if (!res_irq) {dev_warn (&pdev->dev,"Missing IRQ resource.\n" ); -pm_runtime_put (&pdev->dev);Maybe just add
pm_runtime_disable(&pdev->dev); here, rest diff won't be required.
I chose to do it this way in order to make the following patches smaller and easier to review.
Thanks for reviewing.
Johan
PATCH 3/4 media: davinci: vpif: fix use-after-free on driver unbind
In reply to Johan HovoldThe driver allocates and registers two platform device structures during probe, but the devices were never deregistered on driver unbind.
This results in a use-after-free on driver unbind as the device structures were allocated using devres and would be freed by driver core when remove() returns.
Fix this by adding the missing deregistration calls to the remove() callback and failing probe on registration errors.
Note that the platform device structures must be freed using a proper release callback to avoid leaking associated resources like device names.
--- drivers/media/platform/davinci/vpif.c | 97 ++++++++++++++++++++------- 1 file changed, 71 insertions(+), 26 deletions(-)
drivers/media/platform/davinci/vpif.c +71 -26
@@ -41,6 +41,11 @@ MODULE_ALIAS("platform:" VPIF_DRIVER_NAME);#define VPIF_CH2_MAX_MODES 15#define VPIF_CH3_MAX_MODES 2 +struct vpif_data { +struct platform_device *capture; +struct platform_device *display; +}; +DEFINE_SPINLOCK (vpif_lock);EXPORT_SYMBOL_GPL (vpif_lock);
@@ -423,17 +428,31 @@ int vpif_channel_getfid(u8 channel_id) }EXPORT_SYMBOL (vpif_channel_getfid); +static void vpif_pdev_release (struct device *dev) +{ +struct platform_device *pdev =to_platform_device (dev); + +kfree (pdev); +} +static int vpif_probe (struct platform_device *pdev) {static struct resource *res_irq;struct platform_device *pdev_capture, *pdev_display;struct device_node *endpoint =NULL ; +struct vpif_data *data;int ret; vpif_base =devm_platform_ioremap_resource (pdev,0 );if (IS_ERR (vpif_base))return PTR_ERR (vpif_base); + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + +platform_set_drvdata (pdev, data); +pm_runtime_enable (&pdev->dev);pm_runtime_get (&pdev->dev);
@@ -461,49 +480,75 @@ static int vpif_probe(struct platform_device *pdev) goto err_put_rpm; } - pdev_capture =devm_kzalloc (&pdev->dev,sizeof (*pdev_capture), -GFP_KERNEL ); -if (pdev_capture) { - pdev_capture->name ="vpif_capture" ; - pdev_capture->id =-1 ; - pdev_capture->resource = res_irq; - pdev_capture->num_resources =1 ; - pdev_capture->dev.dma_mask = pdev->dev.dma_mask; - pdev_capture->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask; - pdev_capture->dev.parent = &pdev->dev; -platform_device_register (pdev_capture); - }else { -dev_warn (&pdev->dev,"Unable to allocate memory for pdev_capture.\n" ); + pdev_capture =kzalloc (sizeof (*pdev_capture),GFP_KERNEL ); +if (!pdev_capture) { + ret = -ENOMEM ; + goto err_put_rpm; } - pdev_display =devm_kzalloc (&pdev->dev,sizeof (*pdev_display), -GFP_KERNEL ); -if (pdev_display) { - pdev_display->name ="vpif_display" ; - pdev_display->id =-1 ; - pdev_display->resource = res_irq; - pdev_display->num_resources =1 ; - pdev_display->dev.dma_mask = pdev->dev.dma_mask; - pdev_display->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask; - pdev_display->dev.parent = &pdev->dev; -platform_device_register (pdev_display); - }else { -dev_warn (&pdev->dev,"Unable to allocate memory for pdev_display.\n" ); + pdev_capture->name ="vpif_capture" ; + pdev_capture->id =-1 ; + pdev_capture->resource = res_irq; + pdev_capture->num_resources =1 ; + pdev_capture->dev.dma_mask = pdev->dev.dma_mask; + pdev_capture->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask; + pdev_capture->dev.parent = &pdev->dev; + pdev_capture->dev.release = vpif_pdev_release; + + ret =platform_device_register (pdev_capture); +if (ret) + goto err_put_pdev_capture; + + pdev_display =kzalloc (sizeof (*pdev_display),GFP_KERNEL ); +if (!pdev_display) { + ret = -ENOMEM ; + goto err_put_pdev_capture; } + pdev_display->name ="vpif_display" ; + pdev_display->id =-1 ; + pdev_display->resource = res_irq; + pdev_display->num_resources =1 ; + pdev_display->dev.dma_mask = pdev->dev.dma_mask; + pdev_display->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask; + pdev_display->dev.parent = &pdev->dev; + pdev_display->dev.release = vpif_pdev_release; + + ret =platform_device_register (pdev_display); +if (ret) + goto err_put_pdev_display; + + data->capture = pdev_capture; + data->display = pdev_display; +return 0 ; +err_put_pdev_display: +platform_device_put (pdev_display); +err_put_pdev_capture: +platform_device_put (pdev_capture); err_put_rpm:pm_runtime_put (&pdev->dev);pm_runtime_disable (&pdev->dev); +kfree (data);return ret; }static int vpif_remove (struct platform_device *pdev) { +struct vpif_data *data =platform_get_drvdata (pdev); + +if (data->capture) +platform_device_unregister (data->capture); +if (data->display) +platform_device_unregister (data->display); +pm_runtime_put (&pdev->dev);pm_runtime_disable (&pdev->dev); + +kfree (data); +return 0 ; }
Signature · 1 line
2.32.0
Hi Johan,
Thank you for the patch.
Quoted 15 lines · Johan Hovold
The driver allocates and registers two platform device structures during probe, but the devices were never deregistered on driver unbind.
This results in a use-after-free on driver unbind as the device structures were allocated using devres and would be freed by driver core when
remove()returns.Fix this by adding the missing deregistration calls to the
remove()callback and failing probe on registration errors.Note that the platform device structures must be freed using a proper release callback to avoid leaking associated resources like device names.
Quoted 8 lines
Fixes 479f7a118105 ("[media] davinci: vpif: adaptions for DT support")Cc [email protected] # 4.12Cc Kevin Hilman <[email protected]>Signed-off-by Johan Hovold <[email protected]>--- drivers/media/platform/davinci/vpif.c | 97 ++++++++++++++++++++------- 1 file changed, 71 insertions(+), 26 deletions(-)
Cheers,
Prabhakar
Quoted 153 lines
drivers/media/platform/davinci/vpif.c +71
-27@@ -41,6 +41,11 @@ MODULE_ALIAS("platform:" VPIF_DRIVER_NAME);#define VPIF_CH2_MAX_MODES 15#define VPIF_CH3_MAX_MODES 2 +struct vpif_data { +struct platform_device *capture; +struct platform_device *display; +}; +DEFINE_SPINLOCK (vpif_lock);EXPORT_SYMBOL_GPL (vpif_lock);@@ -423,17 +428,31 @@ int vpif_channel_getfid(u8 channel_id) }EXPORT_SYMBOL (vpif_channel_getfid); +static void vpif_pdev_release (struct device *dev) +{ +struct platform_device *pdev =to_platform_device (dev); + +kfree (pdev); +} +static int vpif_probe (struct platform_device *pdev) {static struct resource *res_irq;struct platform_device *pdev_capture, *pdev_display;struct device_node *endpoint =NULL ; +struct vpif_data *data;int ret; vpif_base =devm_platform_ioremap_resource (pdev,0 );if (IS_ERR (vpif_base))return PTR_ERR (vpif_base); + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + +platform_set_drvdata (pdev, data); +pm_runtime_enable (&pdev->dev);pm_runtime_get (&pdev->dev);@@ -461,49 +480,75 @@ static int vpif_probe(struct platform_device *pdev) goto err_put_rpm; } - pdev_capture =devm_kzalloc (&pdev->dev,sizeof (*pdev_capture), -GFP_KERNEL ); -if (pdev_capture) { - pdev_capture->name ="vpif_capture" ; - pdev_capture->id =-1 ; - pdev_capture->resource = res_irq; - pdev_capture->num_resources =1 ; - pdev_capture->dev.dma_mask = pdev->dev.dma_mask; - pdev_capture->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask; - pdev_capture->dev.parent = &pdev->dev; -platform_device_register (pdev_capture); - }else { -dev_warn (&pdev->dev,"Unable to allocate memory for pdev_capture.\n" ); + pdev_capture =kzalloc (sizeof (*pdev_capture),GFP_KERNEL ); +if (!pdev_capture) { + ret = -ENOMEM ; + goto err_put_rpm; } - pdev_display =devm_kzalloc (&pdev->dev,sizeof (*pdev_display), -GFP_KERNEL ); -if (pdev_display) { - pdev_display->name ="vpif_display" ; - pdev_display->id =-1 ; - pdev_display->resource = res_irq; - pdev_display->num_resources =1 ; - pdev_display->dev.dma_mask = pdev->dev.dma_mask; - pdev_display->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask; - pdev_display->dev.parent = &pdev->dev; -platform_device_register (pdev_display); - }else { -dev_warn (&pdev->dev,"Unable to allocate memory for pdev_display.\n" ); + pdev_capture->name ="vpif_capture" ; + pdev_capture->id =-1 ; + pdev_capture->resource = res_irq; + pdev_capture->num_resources =1 ; + pdev_capture->dev.dma_mask = pdev->dev.dma_mask; + pdev_capture->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask; + pdev_capture->dev.parent = &pdev->dev; + pdev_capture->dev.release = vpif_pdev_release; + + ret =platform_device_register (pdev_capture); +if (ret) + goto err_put_pdev_capture; + + pdev_display =kzalloc (sizeof (*pdev_display),GFP_KERNEL ); +if (!pdev_display) { + ret = -ENOMEM ; + goto err_put_pdev_capture; } + pdev_display->name ="vpif_display" ; + pdev_display->id =-1 ; + pdev_display->resource = res_irq; + pdev_display->num_resources =1 ; + pdev_display->dev.dma_mask = pdev->dev.dma_mask; + pdev_display->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask; + pdev_display->dev.parent = &pdev->dev; + pdev_display->dev.release = vpif_pdev_release; + + ret =platform_device_register (pdev_display); +if (ret) + goto err_put_pdev_display; + + data->capture = pdev_capture; + data->display = pdev_display; +return 0 ; +err_put_pdev_display: +platform_device_put (pdev_display); +err_put_pdev_capture: +platform_device_put (pdev_capture); err_put_rpm:pm_runtime_put (&pdev->dev);pm_runtime_disable (&pdev->dev); +kfree (data);return ret; }static int vpif_remove (struct platform_device *pdev) { +struct vpif_data *data =platform_get_drvdata (pdev); + +if (data->capture) +platform_device_unregister (data->capture); +if (data->display) +platform_device_unregister (data->display); +pm_runtime_put (&pdev->dev);pm_runtime_disable (&pdev->dev); + +kfree (data); +return 0 ; } --2.32.0
PATCH 4/4 media: davinci: vpif: drop probe printk
In reply to Johan HovoldDrivers should generally not print anything for a successful probe, and printing "success" before probe is done makes no sense.
Drop the unnecessary and misleading dev_info() call from probe.
--- drivers/media/platform/davinci/vpif.c | 2 -- 1 file changed, 2 deletions(-)
drivers/media/platform/davinci/vpif.c +0 -2
@@ -456,8 +456,6 @@ static int vpif_probe(struct platform_device *pdev)pm_runtime_enable (&pdev->dev);pm_runtime_get (&pdev->dev); -dev_info (&pdev->dev,"vpif probe success\n" ); -/* * If VPIF Node has endpoints, assume "new" DT support, * where capture and display drivers don't have DT nodes
Signature · 1 line
2.32.0
Re: [PATCH 4/4] media: davinci: vpif: drop probe printk
Hi Johan,
Thank you for the patch.
Quoted 11 lines · Johan Hovold
Drivers should generally not print anything for a successful probe, and printing "success" before probe is done makes no sense.
Drop the unnecessary and misleading
dev_info()call from probe.Signed-off-by Johan Hovold <[email protected]>--- drivers/media/platform/davinci/vpif.c | 2 -- 1 file changed, 2 deletions(-)
Cheers,
Prabhakar
Quoted 16 lines
drivers/media/platform/davinci/vpif.c +0
-3@@ -456,8 +456,6 @@ static int vpif_probe(struct platform_device *pdev)pm_runtime_enable (&pdev->dev);pm_runtime_get (&pdev->dev); -dev_info (&pdev->dev,"vpif probe success\n" ); -/* * If VPIF Node has endpoints, assume "new" DT support, * where capture and display drivers don't have DT nodes -- 2.32.0