mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Sverdlin, Alexander" <alexander.sverdlin@siemens.com>
To: Andrew Lunn <andrew@lunn.ch>
Cc: "andrew@lunn.ch" <andrew@lunn.ch>,
	"pavel@kernel.org" <pavel@kernel.org>,
	"mike.marciniszyn@gmail.com" <mike.marciniszyn@gmail.com>,
	"lee@kernel.org" <lee@kernel.org>,
	"kees@kernel.org" <kees@kernel.org>,
	"kabel@kernel.org" <kabel@kernel.org>,
	"ansuelsmth@gmail.com" <ansuelsmth@gmail.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"kuba@kernel.org" <kuba@kernel.org>,
	"linux-leds@vger.kernel.org" <linux-leds@vger.kernel.org>,
	"stable@vger.kernel.org" <stable@vger.kernel.org>
Subject: Re: [PATCH v2 3/4] leds: trigger: netdev: Fix sysfs_update_group() races
Date: Mon, 28 Sep 2026 09:10:01 +0000	[thread overview]
Message-ID: <076481c222a8e5016b44a77137e02a6571fd6fe4.camel@siemens.com> (raw)
In-Reply-To: <20260921093249.3481413-4-alexander.sverdlin@siemens.com>

Hi Andrew,

On Mon, 2026-09-21 at 11:32 +0200, A. Sverdlin wrote:
> From: Alexander Sverdlin <alexander.sverdlin@siemens.com>
> 
> The link_speed attribute group was listed in netdev_led_trigger.groups,
> so the LED core creates and destroys it via device_add_groups() /
> device_remove_groups() in led_trigger_set() (under trigger_lock), while
> the trigger also refreshes it with sysfs_update_group() from
> netdev_trig_notify() and device_name writes.  With no shared lock this was
> observed as a sysfs splat when the trigger is re-armed during PHY link-up:
> 
>   sysfs: cannot create duplicate filename '...green:lan/link_10'
> 
>     CPU 0 (led_trigger_set)        CPU 1 (linkwatch workqueue)
>     -----------------------        ---------------------------
>     activate():
>       register_netdevice_notifier()
>     .                              netdev_trig_notify(NETDEV_CHANGE):
>     .                                sysfs_update_group() creates "link_10"
>     device_add_groups()
>       creates "link_10"  <- EEXIST!
> 
> The mirror case (device_remove_groups() racing the notifier) leaves an
> orphaned sysfs file pointing at freed trigger_data, i.e. a UAF.
> 
> Manage the group in the trigger: drop it from netdev_led_trigger.groups
> and create/destroy it in activate()/deactivate(), so the LED core never
> touches it.  The two remaining refreshers, netdev_trig_notify() and
> device_name_store(), are serialized against each other by a dedicated
> attr_lock, and the is_visible callback takes trigger_data->lock for its
> supported_link_modes read.  sysfs_update_group() is never called under
> trigger_data->lock, so it cannot deadlock against a concurrent link_*
> store that takes that lock.
> 
> Cc: stable@vger.kernel.org
> Fixes: 06cdca014eca ("leds: trigger: netdev: Display only supported link speed attribute")
> Signed-off-by: Alexander Sverdlin <alexander.sverdlin@siemens.com>
> ---
> Changelog:
> v2:
> - this patch is a combined rework of patches 1&2 from v1
> 
>  drivers/leds/trigger/ledtrig-netdev.c | 42 ++++++++++++++++++++-------
>  1 file changed, 31 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/leds/trigger/ledtrig-netdev.c b/drivers/leds/trigger/ledtrig-netdev.c
> index 8b807451b642d..54c6913f3afe3 100644
> --- a/drivers/leds/trigger/ledtrig-netdev.c
> +++ b/drivers/leds/trigger/ledtrig-netdev.c
> @@ -57,6 +57,8 @@
>  
>  struct led_netdev_data {
>  	struct mutex lock;
> +	/* Serializes link_speed group refreshes; never taken by attr stores */
> +	struct mutex attr_lock;
>  
>  	struct delayed_work work;
>  	struct notifier_block notifier;
> @@ -336,8 +338,9 @@ static ssize_t device_name_store(struct device *dev,
>  	if (ret < 0)
>  		return ret;
>  
> -	/* Refresh link_speed visibility */
> -	sysfs_update_group(&dev->kobj, &netdev_trig_link_speed_attrs_group);
> +	/* Serialize the link_speed visibility refresh against netdev_trig_notify() */
> +	scoped_guard(mutex, &trigger_data->attr_lock)
> +		sysfs_update_group(&dev->kobj, &netdev_trig_link_speed_attrs_group);
>  
>  	return size;
>  }
> @@ -547,6 +550,7 @@ static umode_t netdev_trig_link_speed_visible(struct kobject *kobj,
>  	 * Stop at the first matching entry as we care only to check if a particular
>  	 * speed is supported and not the kind.
>  	 */
> +	guard(mutex)(&trigger_data->lock);
>  	for_each_set_bit(mode, supported_link_modes, __ETHTOOL_LINK_MODE_MASK_NBITS) {
>  		struct ethtool_link_ksettings link_ksettings;
>  

I can change this to scoped_guard() as it "is considered more readable" [1],
but switching to explicit mutex_unlock() makes things quite ugly because
CHECK_LINK_MODE_ATTR() would  either do mutex_unlock() (of a lock outside of
macro scope) or a goto (outside of macro).

Therefore at least a scoped_guard() really makes sense here IMO.

[1] https://www.kernel.org/doc/html/latest/process/maintainer-netdev.html

-- 
Alexander Sverdlin
Siemens AG
www.siemens.com

  reply	other threads:[~2026-09-28  9:10 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-21  9:32 [PATCH v2 0/4] leds: trigger: netdev: fix " A. Sverdlin
2026-09-21  9:32 ` [PATCH v2 1/4] leds: trigger: netdev: Put netdev on activate error path A. Sverdlin
2026-09-27 15:02   ` Andrew Lunn
2026-09-28  8:27     ` Sverdlin, Alexander
2026-09-21  9:32 ` [PATCH v2 2/4] leds: trigger: netdev: Access net_dev under trigger_data->lock in the worker A. Sverdlin
2026-09-27 15:05   ` Andrew Lunn
2026-09-28  8:27     ` Sverdlin, Alexander
2026-09-21  9:32 ` [PATCH v2 3/4] leds: trigger: netdev: Fix sysfs_update_group() races A. Sverdlin
2026-09-28  9:10   ` Sverdlin, Alexander [this message]
2026-09-21  9:32 ` [PATCH v2 4/4] leds: trigger: netdev: Serialize mode/interval stores with trigger lock A. Sverdlin

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=076481c222a8e5016b44a77137e02a6571fd6fe4.camel@siemens.com \
    --to=alexander.sverdlin@siemens.com \
    --cc=andrew@lunn.ch \
    --cc=ansuelsmth@gmail.com \
    --cc=kabel@kernel.org \
    --cc=kees@kernel.org \
    --cc=kuba@kernel.org \
    --cc=lee@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=mike.marciniszyn@gmail.com \
    --cc=pavel@kernel.org \
    --cc=stable@vger.kernel.org \
    /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®