2021-09-04 06:22:35

by Christophe JAILLET

[permalink] [raw]
Subject: [PATCH] media: uvcvideo: Fix a memory leak in error handling code in 'uvc_gpio_parse()'

Memory allocated in 'uvc_alloc_entity()' should be freed if an error
occurs after it.

Reorder the code in order to avoid the leak.

Fixes: 2886477ff987 ("media: uvcvideo: Implement UVC_EXT_GPIO_UNIT")
Signed-off-by: Christophe JAILLET <[email protected]>
---
drivers/media/usb/uvc/uvc_driver.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c
index b1b055784f8d..a4c45424ba7e 100644
--- a/drivers/media/usb/uvc/uvc_driver.c
+++ b/drivers/media/usb/uvc/uvc_driver.c
@@ -1533,10 +1533,6 @@ static int uvc_gpio_parse(struct uvc_device *dev)
if (IS_ERR_OR_NULL(gpio_privacy))
return PTR_ERR_OR_ZERO(gpio_privacy);

- unit = uvc_alloc_entity(UVC_EXT_GPIO_UNIT, UVC_EXT_GPIO_UNIT_ID, 0, 1);
- if (!unit)
- return -ENOMEM;
-
irq = gpiod_to_irq(gpio_privacy);
if (irq < 0) {
if (irq != EPROBE_DEFER)
@@ -1545,6 +1541,10 @@ static int uvc_gpio_parse(struct uvc_device *dev)
return irq;
}

+ unit = uvc_alloc_entity(UVC_EXT_GPIO_UNIT, UVC_EXT_GPIO_UNIT_ID, 0, 1);
+ if (!unit)
+ return -ENOMEM;
+
unit->gpio.gpio_privacy = gpio_privacy;
unit->gpio.irq = irq;
unit->gpio.bControlSize = 1;
--
2.30.2


2021-09-06 07:10:33

by Ricardo Ribalda

[permalink] [raw]
Subject: Re: [PATCH] media: uvcvideo: Fix a memory leak in error handling code in 'uvc_gpio_parse()'

Hi Christophe

Indeed, there is a bug, uvc_delete will not free the entity because it
has not been added yet.

Thanks for catching it up.

Reviewed by: Ricardo Ribalda <[email protected]>

On Sat, 4 Sept 2021 at 08:20, Christophe JAILLET
<[email protected]> wrote:
>
> Memory allocated in 'uvc_alloc_entity()' should be freed if an error
> occurs after it.
>
> Reorder the code in order to avoid the leak.
>
> Fixes: 2886477ff987 ("media: uvcvideo: Implement UVC_EXT_GPIO_UNIT")
> Signed-off-by: Christophe JAILLET <[email protected]>
> ---
> drivers/media/usb/uvc/uvc_driver.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c
> index b1b055784f8d..a4c45424ba7e 100644
> --- a/drivers/media/usb/uvc/uvc_driver.c
> +++ b/drivers/media/usb/uvc/uvc_driver.c
> @@ -1533,10 +1533,6 @@ static int uvc_gpio_parse(struct uvc_device *dev)
> if (IS_ERR_OR_NULL(gpio_privacy))
> return PTR_ERR_OR_ZERO(gpio_privacy);
>
> - unit = uvc_alloc_entity(UVC_EXT_GPIO_UNIT, UVC_EXT_GPIO_UNIT_ID, 0, 1);
> - if (!unit)
> - return -ENOMEM;
> -
> irq = gpiod_to_irq(gpio_privacy);
> if (irq < 0) {
> if (irq != EPROBE_DEFER)
> @@ -1545,6 +1541,10 @@ static int uvc_gpio_parse(struct uvc_device *dev)
> return irq;
> }
>
> + unit = uvc_alloc_entity(UVC_EXT_GPIO_UNIT, UVC_EXT_GPIO_UNIT_ID, 0, 1);
> + if (!unit)
> + return -ENOMEM;
> +
> unit->gpio.gpio_privacy = gpio_privacy;
> unit->gpio.irq = irq;
> unit->gpio.bControlSize = 1;
> --
> 2.30.2
>


--
Ricardo Ribalda