Skip to contentSkip to conversation

ALSA: control: Synchronize LED mode accesses

PATCH Superseded Sreeraj S Kurup 3 participants 9 messages Nov 6 – Nov 8 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: control: Synchronize LED mode accesses

mode_store() updates led->mode while holding
snd_ctl_led_mutex.

However, snd_ctl_led_set_state() and mode_show() read this field without holding the same mutex.

This can cause a data race when userspace changes the mode while another path reads it.

Read the mode under snd_ctl_led_mutex in snd_ctl_led_set_state() and use the local value after releasing the mutex.

Also protect the mode read in mode_show() with the mutex.

---
 sound/core/control_led.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
sound/core/control_led.c +5 -1
index 501387287ae6..044430b2f1a2 100644
@@ -140,6 +140,7 @@ static void snd_ctl_led_set_state(struct snd_card *card, unsigned int access,
 {
 	struct snd_ctl_led *led;
 	struct snd_ctl_led_ctl *lctl;
+	enum snd_ctl_led_mode mode;
 	int route;
 	bool found;
 
@@ -168,8 +169,9 @@ static void snd_ctl_led_set_state(struct snd_card *card, unsigned int access,
 				UPDATE_ROUTE(route, snd_ctl_led_get(lctl));
 			}
 		}
+		mode = led->mode;
 	}
-	switch (led->mode) {
+	switch (mode) {
 	case MODE_OFF:		route = 1; break;
 	case MODE_ON:		route = 0; break;
 	case MODE_FOLLOW_ROUTE:	if (route >= 0) route ^= 1; break;
@@ -382,6 +384,8 @@ static ssize_t mode_show(struct device *dev,
 	struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
 	const char *str = NULL;
 
+	guard(mutex)(&snd_ctl_led_mutex);
+
 	switch (led->mode) {
 	case MODE_FOLLOW_MUTE:	str = "follow-mute"; break;
 	case MODE_FOLLOW_ROUTE:	str = "follow-route"; break;
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] ALSA: control: missing Fixes trailer for led->mode data race fix It also noticed 1 related…

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] ALSA: control: missing Fixes trailer for led->mode data race fix It also noticed 1 related pre-existing issue this patch does not introduce:
- [Medium] ALSA: control_led: data race in snd_ctl_led_reset() [1] --

commit 2868f72034945ef393b8a3a1a9c6b9d772455395
Author: Sreeraj S Kurup <[email protected]>

Quoted 11 lines

ALSA: control: Synchronize LED mode accesses

mode_store() updates led->mode while holding
snd_ctl_led_mutex.

However, snd_ctl_led_set_state() and mode_show() read this field without holding the same mutex.

This can cause a data race when userspace changes the mode while another path reads it.

Quoted 7 lines

Read the mode under snd_ctl_led_mutex in snd_ctl_led_set_state() and use the local value after releasing the mutex.

Also protect the mode read in mode_show() with the mutex.

[Severity: Low]
Since this fixes the unlocked reads of led->mode introduced when sysfs support was added, should this include a Fixes tag?

Fixes cb17fe0045aa ("ALSA: control - add sysfs support to the LED trigger module")
Quoted 4 lines
sound/core/control_led.c +0 -0
index 501387287ae69..044430b2f1a22 100644

[ ... ]

sound/core/control_led.c static ssize_t mode_show(struct device *dev, · line 387

Quoted 5 lines
sound/core/control_led.c +1 -0
@@ -382,6 +384,8 @@ static ssize_t mode_show(struct device *dev,
 	struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
 	const char *str = NULL;
 
+	guard(mutex)(&snd_ctl_led_mutex);

[Severity: Medium]
This problem wasn't introduced by this patch, but can snd_ctl_led_reset() and snd_ctl_led_set_id() race on vd->access [1]?

Concurrent sysfs writes to the reset and attach or detach attributes modify vd->access under different locks: snd_ctl_led_reset() holds only snd_ctl_led_mutex, whereas snd_ctl_led_set_id() holds only card->controls_rwsem.

CPU0 CPU1
snd_ctl_led_reset() snd_ctl_led_set_id()
scoped_guard(mutex,

               &snd_ctl_led_mutex)
                                        guard(rwsem_write)(
                                          &card->controls_rwsem)
                                        new_access = vd->access & ~mask;
    vd->access &= ~group_to_access();
                                        vd->access = new_access;
                                          <-- overwrites reset

The lost update leaves the control's LED access flags out of sync with the LED group's control list.

[1] https://sashiko.dev/#/bug/linux-55c9a300-c6f3-4df1-9060-c4bb4df43d19

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

PATCH v2 1/1 ALSA: control: Synchronize LED mode accesses

In reply to Sreeraj S Kurup

mode_store() updates led->mode while holding
snd_ctl_led_mutex.

However, snd_ctl_led_set_state() and mode_show() read this field without holding the same mutex.

This can cause a data race when userspace changes the mode while another path reads it.

Read the mode under snd_ctl_led_mutex in snd_ctl_led_set_state() and use the local value after releasing the mutex.

Also protect the mode read in mode_show() with the mutex.

Fixes cb17fe0045aa ("ALSA: control - add sysfs support to the LED trigger module")

---
v2:
- Add the Fixes trailer.

 sound/core/control_led.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
sound/core/control_led.c +5 -1
index 501387287ae6..044430b2f1a2 100644
@@ -140,6 +140,7 @@ static void snd_ctl_led_set_state(struct snd_card *card, unsigned int access,
 {
 	struct snd_ctl_led *led;
 	struct snd_ctl_led_ctl *lctl;
+	enum snd_ctl_led_mode mode;
 	int route;
 	bool found;
 
@@ -168,8 +169,9 @@ static void snd_ctl_led_set_state(struct snd_card *card, unsigned int access,
 				UPDATE_ROUTE(route, snd_ctl_led_get(lctl));
 			}
 		}
+		mode = led->mode;
 	}
-	switch (led->mode) {
+	switch (mode) {
 	case MODE_OFF:		route = 1; break;
 	case MODE_ON:		route = 0; break;
 	case MODE_FOLLOW_ROUTE:	if (route >= 0) route ^= 1; break;
@@ -382,6 +384,8 @@ static ssize_t mode_show(struct device *dev,
 	struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
 	const char *str = NULL;
 
+	guard(mutex)(&snd_ctl_led_mutex);
+
 	switch (led->mode) {
 	case MODE_FOLLOW_MUTE:	str = "follow-mute"; break;
 	case MODE_FOLLOW_ROUTE:	str = "follow-route"; break;
Signature · 1 line
2.55.0
#3 lore raw
Quoted 10 lines · Sreeraj S Kurup

mode_store() updates led->mode while holding
snd_ctl_led_mutex.

However, snd_ctl_led_set_state() and mode_show() read this field without holding the same mutex.

This can cause a data race when userspace changes the mode while another path reads it.

Quoted 8 lines

Read the mode under snd_ctl_led_mutex in snd_ctl_led_set_state() and use the local value after releasing the mutex.

Also protect the mode read in mode_show() with the mutex.

Fixes cb17fe0045aa ("ALSA: control - add sysfs support to the LED trigger module")

As this sort of data race for a single value has no much impact on the actual behavior, it's more or less just for consistency.
So I applied to for-next branch. Thanks.

Takashi

#7 lore raw
Quoted 9 lines · Sreeraj S Kurup

Hi Takashi,

I would like to kindly follow up on my first patch.

Could you please let me know if you have had a chance to review it?

For reference, here is the patch link:
https://lore.kernel.org/all/[email protected]/

I'm still considering what's the best way.

Your patch replaces and adds some open-code, which isn't nice. OTOH, it's a small change, so it's acceptable.

An alternative would be to use simple_strtoul() that can return the end pointer. Or, we may reorganize the whole code a bit, so that to get the delimiter at first (instead of the head of the next word), then clear and parse. But not sure whether it'd be worth.

Takashi

#8 lore raw

Hi Takashi,

I looked into using simple_strtoul(), but noticed that it is deprecated. kstrtoul() does not provide an end pointer either.

Would it be reasonable to retain the delimiter-first approach with kstrtouint(), as in v2?

Thanks,
Sreeraj