public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: William Breathitt Gray <wbg@kernel.org>
To: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
Cc: William Breathitt Gray <wbg@kernel.org>,
	Guangshuo Li <lgs201920130244@gmail.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH] counter: Fix refcount leak in counter_alloc() error path
Date: Sat, 18 Apr 2026 18:42:37 +0900	[thread overview]
Message-ID: <20260418094249.305435-1-wbg@kernel.org> (raw)
In-Reply-To: <adyb3Cdr1p3p76h6@monoceros>

On Mon, Apr 13, 2026 at 09:47:04AM +0200, Uwe Kleine-König wrote:
> On Sat, Apr 11, 2026 at 09:35:11PM +0800, Guangshuo Li wrote:
> > After device_initialize(), the lifetime of the embedded struct device
> > is expected to be managed through the device core reference counting.
> > 
> > In counter_alloc(), if dev_set_name() fails after device_initialize(),
> > the error path removes the chrdev, frees the ID, and frees the backing
> > allocation directly instead of releasing the device reference with
> > put_device(). This bypasses the normal device lifetime rules and may
> > leave the reference count of the embedded struct device unbalanced,
> > resulting in a refcount leak and potentially leading to a use-after-free.
> > 
> > Fix this by using put_device() in the dev_set_name() failure path and
> > let counter_device_release() handle the final cleanup.
> > 
> > Fixes: 4da08477ea1f ("counter: Set counter device name")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
> > ---
> >  drivers/counter/counter-core.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
> > index 50bd30ba3d03..12dc18c78672 100644
> > --- a/drivers/counter/counter-core.c
> > +++ b/drivers/counter/counter-core.c
> > @@ -123,10 +123,10 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
> >  	return counter;
> >  
> >  err_dev_set_name:
> > +	put_device(dev);
> > +	return NULL;
> >  
> > -	counter_chrdev_remove(counter);
> >  err_chrdev_add:
> > -
> >  	ida_free(&counter_ida, dev->id);
> >  err_ida_alloc:
> 
> This patch is technically correct. Looking in more detail however I
> wonder why 4da08477ea1f ("counter: Set counter device name") was created
> in the presence of
> 
> 	static const struct bus_type counter_bus_type = {
> 		...
> 		.dev_name = "counter",
> 	};
> 
> 	int device_add(struct device *dev)
> 	{
> 		...
> 		if (dev->bus && dev->bus->dev_name)
> 			error = dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
> 		...
> 	}
> 
> The only upside I can see is that the name is already set before
> device_add() is called. 

Looking at the current code, the only Counter subsystem specific call
occurring after device_initialize() but before device_add() is
counter_sysfs_add() which does perform a number devm_kcalloc() and other
such devres operations. I suspect the reason we added the dev_set_name()
call is to make those operations in counter_sysfs_add() clearer in the
devres_log events.

> Assuming the dev_set_name() call should be kept, I think that
> 
> diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
> index 50bd30ba3d03..69f042ce4418 100644
> --- a/drivers/counter/counter-core.c
> +++ b/drivers/counter/counter-core.c
> @@ -114,12 +114,12 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
>  	if (err < 0)
>  		goto err_chrdev_add;
>  
> -	device_initialize(dev);
> -
>  	err = dev_set_name(dev, COUNTER_NAME "%d", dev->id);
>  	if (err)
>  		goto err_dev_set_name;
>  
> +	device_initialize(dev);
> +
>  	return counter;
>  
>  err_dev_set_name:
> 
> also fixes the issue.

We moved dev_set_name() after device_initialize() so that the device
core takes care of freeing the memory allocated for the name.[^1] I
think that's the behavior expected for dev_set_name(), so it's probably
best to keep it after device_initialize() so that there is no need to
manually free memory on failure.

William Breathitt Gray

[^1] https://lkml.iu.edu/hypermail/linux/kernel/2202.0/03859.html

      reply	other threads:[~2026-04-18  9:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-11 13:35 [PATCH] counter: Fix refcount leak in counter_alloc() error path Guangshuo Li
2026-04-13  7:47 ` Uwe Kleine-König
2026-04-18  9:42   ` William Breathitt Gray [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260418094249.305435-1-wbg@kernel.org \
    --to=wbg@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=lgs201920130244@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=u.kleine-koenig@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox