mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: Riccardo Oliva <oliva.scorza@gmail.com>
Cc: linux-kernel@vger.kernel.org, mfd@lists.linux.dev
Subject: Re: [PATCH] mfd: dln2: Fix use-after-free risk in list iterator
Date: Tue, 20 Oct 2026 10:59:39 +0100	[thread overview]
Message-ID: <179198398105.839288.1873130512029656566@kernel.org> (raw)
In-Reply-To: <20261012234230.17966-1-oliva.scorza@gmail.com>

On Mon, 12 Oct 2026, Riccardo Oliva wrote:

> In dln2_unregister_event_cb(), the iterator variable 'i' is used after
> the list_for_each_entry() loop terminates to free memory.

How is this a use-after-free risk?  'i' is only ever used after the loop
if 'found' is true, which means we broke out of the loop early while 'i'
still points to a valid entry.

Is this an actual bug or are we just silencing a static checker false
positive?

>
> Fix this by introducing a dedicated 'target' pointer to hold the entry,
> isolating the iterator to the loop scope and removing the 'found' bool.
>
> Assisted-by: coccinelle sparse
> Signed-off-by: Riccardo Oliva <oliva.scorza@gmail.com>
> ---
>  drivers/mfd/dln2.c | 9 ++++-----
>  1 file changed, 4 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/mfd/dln2.c b/drivers/mfd/dln2.c
> index 0b1e7fd7f..7cd64aad1 100644
> --- a/drivers/mfd/dln2.c
> +++ b/drivers/mfd/dln2.c
> @@ -157,25 +157,24 @@ EXPORT_SYMBOL(dln2_register_event_cb);
>  void dln2_unregister_event_cb(struct platform_device *pdev, u16 id)
>  {
>  	struct dln2_dev *dln2 = dev_get_drvdata(pdev->dev.parent);
> -	struct dln2_event_cb_entry *i;
> +	struct dln2_event_cb_entry *i, *target = NULL;

If we do keep this change as a cleanup, does it make sense to use
'entry' instead of 'target' to match 'dln2_register_event_cb()' above?

>  	unsigned long flags;
> -	bool found = false;
>
>  	spin_lock_irqsave(&dln2->event_cb_lock, flags);
>
>  	list_for_each_entry(i, &dln2->event_cb_list, list) {
>  		if (i->id == id) {
>  			list_del_rcu(&i->list);
> -			found = true;
> +			target = i;
>  			break;
>  		}
>  	}
>
>  	spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
>
> -	if (found) {
> +	if (target) {
>  		synchronize_rcu();
> -		kfree(i);
> +		kfree(target);
>  	}
>  }
>  EXPORT_SYMBOL(dln2_unregister_event_cb);
> -- 
> 2.43.0
>

-- 
Lee Jones

      reply	other threads:[~2026-10-20  9:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-12 23:42 Riccardo Oliva
2026-10-20  9:59 ` Lee Jones [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=179198398105.839288.1873130512029656566@kernel.org \
    --to=lee@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mfd@lists.linux.dev \
    --cc=oliva.scorza@gmail.com \
    /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

all inboxes | Powered by JetHome®