linux-leds.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] leds: sgm3140: Put the child node reference on success
@ 2026-11-25 13:40 Cong Nguyen
  2026-11-25 13:45 ` sashiko-bot
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Cong Nguyen @ 2026-11-25 13:40 UTC (permalink / raw)
  To: Lee Jones, Pavel Machek; +Cc: Luca Weiss, linux-leds, linux-kernel

sgm3140_probe() gets the LED child node with device_get_next_child_node(),
which returns it with its reference count raised. The error path puts the
reference, but a successful probe returns before reaching that code, so the
reference is never dropped. The v4l2 flash sub-device takes its own
reference on the node, and the LED class device only stores the pointer,
so nothing else needs the probe's reference after probe returns.

Put the reference before returning success.

Fixes: cef8ec8cbd21 ("leds: add sgm3140 driver")
Signed-off-by: Cong Nguyen <congnt264@gmail.com>
---
 drivers/leds/flash/leds-sgm3140.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/leds/flash/leds-sgm3140.c b/drivers/leds/flash/leds-sgm3140.c
index 107d02c3137f..5bc21b192d74 100644
--- a/drivers/leds/flash/leds-sgm3140.c
+++ b/drivers/leds/flash/leds-sgm3140.c
@@ -273,7 +273,8 @@ static int sgm3140_probe(struct platform_device *pdev)
 		goto err;
 	}
 
-	return ret;
+	fwnode_handle_put(child_node);
+	return 0;
 
 err:
 	fwnode_handle_put(child_node);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] leds: sgm3140: Put the child node reference on success
  2026-11-25 13:40 [PATCH] leds: sgm3140: Put the child node reference on success Cong Nguyen
@ 2026-11-25 13:45 ` sashiko-bot
  2026-11-25 15:15 ` Markus Elfring
  2026-11-26 12:41 ` Lee Jones
  2 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-25 13:45 UTC (permalink / raw)
  To: Cong Nguyen; +Cc: linux-leds, lee

> sgm3140_probe() gets the LED child node with device_get_next_child_node(),
> which returns it with its reference count raised. The error path puts the
> reference, but a successful probe returns before reaching that code, so the
> reference is never dropped. The v4l2 flash sub-device takes its own
> reference on the node, and the LED class device only stores the pointer,
> so nothing else needs the probe's reference after probe returns.
> 
> Put the reference before returning success.
> 
> Fixes: cef8ec8cbd21 ("leds: add sgm3140 driver")
> Signed-off-by: Cong Nguyen <congnt264@gmail.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125134013.2692510-1-congnt264@gmail.com?part=1

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] leds: sgm3140: Put the child node reference on success
  2026-11-25 13:40 [PATCH] leds: sgm3140: Put the child node reference on success Cong Nguyen
  2026-11-25 13:45 ` sashiko-bot
@ 2026-11-25 15:15 ` Markus Elfring
  2026-11-26 12:28   ` Nguyễn Công
  2026-11-26 12:41 ` Lee Jones
  2 siblings, 1 reply; 6+ messages in thread
From: Markus Elfring @ 2026-11-25 15:15 UTC (permalink / raw)
  To: Cong Nguyen, linux-leds, Lee Jones, Pavel Machek; +Cc: LKML, Luca Weiss

…
> Put the reference before returning success.

Please avoid a bit of duplicate source.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v7.4-rc4#n526


How do you think about to apply the attribute “__free(fwnode_handle_put)”
also in the implementation of the function “sgm3140_probe”?
https://elixir.bootlin.com/linux/v7.4-rc1/source/include/linux/property.h#L229
https://elixir.bootlin.com/linux/v7.4-rc1/source/drivers/leds/flash/leds-sgm3140.c#L185-L281

Regards,
Markus

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] leds: sgm3140: Put the child node reference on success
  2026-11-25 15:15 ` Markus Elfring
@ 2026-11-26 12:28   ` Nguyễn Công
  0 siblings, 0 replies; 6+ messages in thread
From: Nguyễn Công @ 2026-11-26 12:28 UTC (permalink / raw)
  To: Markus Elfring; +Cc: linux-leds, Lee Jones, Pavel Machek, LKML, Luca Weiss

On Wed, Nov 25, 2026 at 10:15 PM Markus Elfring <Markus.Elfring@web.de> wrote:
>
> …
> > Put the reference before returning success.
>
> Please avoid a bit of duplicate source.
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v7.4-rc4#n526
>
Hi Markus,

Agreed on the duplicate, v2 drops it and uses the common exit:
https://lore.kernel.org/r/20261126122516.1198461-1-congnt264@gmail.com

>
> How do you think about to apply the attribute “__free(fwnode_handle_put)”
> also in the implementation of the function “sgm3140_probe”?
> https://elixir.bootlin.com/linux/v7.4-rc1/source/include/linux/property.h#L229
> https://elixir.bootlin.com/linux/v7.4-rc1/source/drivers/leds/flash/leds-sgm3140.c#L185-L281

I kept fwnode_handle_put() rather than __free(fwnode_handle) because
this has a Fixes tag and may be backported, and the __free() helper
only exists since v6.10.

Thanks,
Cong

>
> Regards,
> Markus

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] leds: sgm3140: Put the child node reference on success
  2026-11-25 13:40 [PATCH] leds: sgm3140: Put the child node reference on success Cong Nguyen
  2026-11-25 13:45 ` sashiko-bot
  2026-11-25 15:15 ` Markus Elfring
@ 2026-11-26 12:41 ` Lee Jones
  2026-11-26 13:33   ` Nguyễn Công
  2 siblings, 1 reply; 6+ messages in thread
From: Lee Jones @ 2026-11-26 12:41 UTC (permalink / raw)
  To: Cong Nguyen; +Cc: Pavel Machek, Luca Weiss, linux-leds, linux-kernel

On Wed, 25 Nov 2026, Cong Nguyen wrote:

> sgm3140_probe() gets the LED child node with device_get_next_child_node(),
> which returns it with its reference count raised. The error path puts the
> reference, but a successful probe returns before reaching that code, so the
> reference is never dropped. The v4l2 flash sub-device takes its own
> reference on the node, and the LED class device only stores the pointer,

As the commit message notes, devm_led_classdev_flash_register_ext()
stores 'init_data->fwnode' in 'led_cdev->dev->fwnode' without taking a
reference on it and led_classdev_unregister() subsequently calls
fwnode_handle_put(led_cdev->dev->fwnode) when the device is removed.

So 'led_cdev->dev->fwnode' takes ownership of the caller's 'child_node'
reference on successful registration.  Dropping 'child_node' here on the
success path causes a reference underflow / use-after-free while the LED
device is registered and a double-put on unbind.

In fact, there is already a bug on the error path if v4l2_flash_init()
fails after devm_led_classdev_flash_register_ext() has succeeded: 'goto
err' drops 'child_node' once, and then devres teardown calls
devm_led_classdev_release() -> led_classdev_unregister() which calls
fwnode_handle_put() a second time!

> so nothing else needs the probe's reference after probe returns.
>
> Put the reference before returning success.
>
> Fixes: cef8ec8cbd21 ("leds: add sgm3140 driver")
> Signed-off-by: Cong Nguyen <congnt264@gmail.com>
> ---
>  drivers/leds/flash/leds-sgm3140.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/leds/flash/leds-sgm3140.c b/drivers/leds/flash/leds-sgm3140.c
> index 107d02c3137f..5bc21b192d74 100644
> --- a/drivers/leds/flash/leds-sgm3140.c
> +++ b/drivers/leds/flash/leds-sgm3140.c
> @@ -273,7 +273,8 @@ static int sgm3140_probe(struct platform_device *pdev)
>  		goto err;
>  	}
>
> -	return ret;
> +	fwnode_handle_put(child_node);
> +	return 0;
>
>  err:
>  	fwnode_handle_put(child_node);
> -- 
> 2.25.1
>
>

-- 
Lee Jones

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] leds: sgm3140: Put the child node reference on success
  2026-11-26 12:41 ` Lee Jones
@ 2026-11-26 13:33   ` Nguyễn Công
  0 siblings, 0 replies; 6+ messages in thread
From: Nguyễn Công @ 2026-11-26 13:33 UTC (permalink / raw)
  To: Lee Jones; +Cc: Pavel Machek, Luca Weiss, linux-leds, linux-kernel

On Thu, Nov 26, 2026 at 7:41 PM Lee Jones <lee@kernel.org> wrote:
>
> On Wed, 25 Nov 2026, Cong Nguyen wrote:
>
> > sgm3140_probe() gets the LED child node with device_get_next_child_node(),
> > which returns it with its reference count raised. The error path puts the
> > reference, but a successful probe returns before reaching that code, so the
> > reference is never dropped. The v4l2 flash sub-device takes its own
> > reference on the node, and the LED class device only stores the pointer,
>
> As the commit message notes, devm_led_classdev_flash_register_ext()
> stores 'init_data->fwnode' in 'led_cdev->dev->fwnode' without taking a
> reference on it and led_classdev_unregister() subsequently calls
> fwnode_handle_put(led_cdev->dev->fwnode) when the device is removed.

Hi Lee,
I could not find that call. led_classdev_unregister() only stops
triggers and blinking, turns the LED off and calls device_unregister(),
and device_create_release() just does kfree(). The only
fwnode_handle_put() in led-class.c is in fwnode_led_get(), for the node
it looked up itself.

>
> So 'led_cdev->dev->fwnode' takes ownership of the caller's 'child_node'
> reference on successful registration.  Dropping 'child_node' here on the
> success path causes a reference underflow / use-after-free while the LED
> device is registered and a double-put on unbind.
>
> In fact, there is already a bug on the error path if v4l2_flash_init()
> fails after devm_led_classdev_flash_register_ext() has succeeded: 'goto
> err' drops 'child_node' once, and then devres teardown calls
> devm_led_classdev_release() -> led_classdev_unregister() which calls
> fwnode_handle_put() a second time!

v4l2_flash_init() takes its own reference on the node and puts it on
its error path, so the one 'goto err' drops is the probe's. Other
drivers drop their reference right after registering, for example
leds-lt3593:

    init_data.fwnode = child;
    ret = devm_led_classdev_register_ext(dev, &led_data->cdev, &init_data);
    fwnode_handle_put(child);

Is there a path I am missing?

Thanks,
Cong

>
> > so nothing else needs the probe's reference after probe returns.
> >
> > Put the reference before returning success.
> >
> > Fixes: cef8ec8cbd21 ("leds: add sgm3140 driver")
> > Signed-off-by: Cong Nguyen <congnt264@gmail.com>
> > ---
> >  drivers/leds/flash/leds-sgm3140.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/leds/flash/leds-sgm3140.c b/drivers/leds/flash/leds-sgm3140.c
> > index 107d02c3137f..5bc21b192d74 100644
> > --- a/drivers/leds/flash/leds-sgm3140.c
> > +++ b/drivers/leds/flash/leds-sgm3140.c
> > @@ -273,7 +273,8 @@ static int sgm3140_probe(struct platform_device *pdev)
> >               goto err;
> >       }
> >
> > -     return ret;
> > +     fwnode_handle_put(child_node);
> > +     return 0;
> >
> >  err:
> >       fwnode_handle_put(child_node);
> > --
> > 2.25.1
> >
> >
>
> --
> Lee Jones

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-26 13:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 13:40 [PATCH] leds: sgm3140: Put the child node reference on success Cong Nguyen
2026-11-25 13:45 ` sashiko-bot
2026-11-25 15:15 ` Markus Elfring
2026-11-26 12:28   ` Nguyễn Công
2026-11-26 12:41 ` Lee Jones
2026-11-26 13:33   ` Nguyễn Công

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).