All of lore.kernel.org
 help / color / mirror / Atom feed
From: Charles Keepax <ckeepax@opensource.cirrus.com>
To: sparkhuang <huangshaobo3@xiaomi.com>
Cc: Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>,
	Charles Keepax <ckeepax@opensource.wolfsonmicro.com>,
	linux-kernel@vger.kernel.org, weipengliang@xiaomi.com,
	wengjinfei@xiaomi.com, caodan1@xiaomi.com
Subject: Re: [PATCH] regulator: core: Protect regulator_supply_alias_list with regulator_list_mutex
Date: Wed, 26 Nov 2025 14:44:06 +0000	[thread overview]
Message-ID: <aScSNtqQ01cKJ7gW@opensource.cirrus.com> (raw)
In-Reply-To: <20251126061542.3849-1-huangshaobo3@xiaomi.com>

On Wed, Nov 26, 2025 at 02:15:42PM +0800, sparkhuang wrote:
> regulator_supply_alias_list was accessed without any locking in
> regulator_supply_alias(), regulator_register_supply_alias(), and
> regulator_unregister_supply_alias(). Concurrent registration,
> unregistration and lookups can race, leading to:
> 
> 1 use-after-free if an alias entry is removed while being read,
> 2 duplicate entries when two threads register the same alias,
> 3 inconsistent alias mappings observed by consumers.
> 
> Protect all traversals, insertions and deletions on
> regulator_supply_alias_list with the existing regulator_list_mutex.
> 
> Fixes: a06ccd9c3785f ("regulator: core: Add ability to create a lookup alias for supply")
> Signed-off-by: sparkhuang <huangshaobo3@xiaomi.com>
> ---
>  {
>  	struct regulator_supply_alias *map;
> +	struct regulator_supply_alias *new_map;
>  
> -	map = regulator_find_supply_alias(dev, id);
> -	if (map)
> -		return -EEXIST;
> -
> -	map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
> -	if (!map)
> +	new_map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
> +	if (!new_map)
>  		return -ENOMEM;
>  
> -	map->src_dev = dev;
> -	map->src_supply = id;
> -	map->alias_dev = alias_dev;
> -	map->alias_supply = alias_id;
> +	mutex_lock(&regulator_list_mutex);
> +	map = regulator_find_supply_alias(dev, id);
> +	if (map) {
> +		mutex_unlock(&regulator_list_mutex);
> +		kfree(new_map);
> +		return -EEXIST;
> +	}
>  
> +	new_map->src_dev = dev;
> +	new_map->src_supply = id;
> +	new_map->alias_dev = alias_dev;
> +	new_map->alias_supply = alias_id;
>  	list_add(&map->list, &regulator_supply_alias_list);

In addition to Mark's comment the first argument here should be
the new item to add. Personally I would be inclined to just leave
the allocation inside the lock and not require the extra variable
but up to you.

Thanks,
Charles

  parent reply	other threads:[~2025-11-26 14:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-26  6:15 [PATCH] regulator: core: Protect regulator_supply_alias_list with regulator_list_mutex sparkhuang
2025-11-26 12:58 ` Mark Brown
2025-11-26 14:44 ` Charles Keepax [this message]
2025-11-26 14:51   ` Mark Brown
2025-11-27  2:57     ` [PATCH v2] " sparkhuang
2025-11-27 16:55       ` Charles Keepax
2025-11-27 21:52       ` Mark Brown

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=aScSNtqQ01cKJ7gW@opensource.cirrus.com \
    --to=ckeepax@opensource.cirrus.com \
    --cc=broonie@kernel.org \
    --cc=caodan1@xiaomi.com \
    --cc=ckeepax@opensource.wolfsonmicro.com \
    --cc=huangshaobo3@xiaomi.com \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=weipengliang@xiaomi.com \
    --cc=wengjinfei@xiaomi.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.