Skip to contentSkip to conversation

ALSA: control - fix a leak in snd_ctl_led_init()

PATCH In review Dan Carpenter 3 participants 8 messages Apr 2, 2021 – Apr 9, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ALSA: control - fix a leak in snd_ctl_led_init()

This unwind loop needs to free snd_ctl_leds[0] as well.

Fixes cb17fe0045aa ("ALSA: control - add sysfs support to the LED trigger module")
---
 sound/core/control_led.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
sound/core/control_led.c +2 -2
index d4fb8b873f34..202b475d0bf3 100644
@@ -712,7 +712,7 @@ static struct snd_ctl_layer_ops snd_ctl_led_lops = {
 static int __init snd_ctl_led_init(void)
 {
 	struct snd_ctl_led *led;
-	unsigned int group;
+	int group;
 
 	device_initialize(&snd_ctl_led_dev);
 	snd_ctl_led_dev.class = sound_class;
@@ -730,7 +730,7 @@ static int __init snd_ctl_led_init(void)
 		dev_set_name(&led->dev, led->name);
 		if (device_add(&led->dev)) {
 			put_device(&led->dev);
-			for (; group > 0; group--) {
+			for (; group >= 0; group--) {
 				led = &snd_ctl_leds[group];
 				device_del(&led->dev);
 			}
Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/2 ALSA: control - off by one in store_mode()

If count is 16 then this will put the NUL terminator one element beyond the end of the array.

Fixes cb17fe0045aa ("ALSA: control - add sysfs support to the LED trigger module")
---
 sound/core/control_led.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/control_led.c +1 -1
index 202b475d0bf3..ab5a455723c8 100644
@@ -391,7 +391,7 @@ static ssize_t store_mode(struct device *dev, struct device_attribute *attr,
 {
 	struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
 	char _buf[16];
-	size_t l = min(count, sizeof(_buf) - 1) + 1;
+	size_t l = min(count, sizeof(_buf) - 1);
 	enum snd_ctl_led_mode mode;
 
 	memcpy(_buf, buf, l);
Signature · 1 line
2.30.2
maintainer Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] ALSA: control - off by one in store_mode()

Dne 02. 04. 21 v 13:42 Dan Carpenter napsal(a):

Quoted 5 lines

If count is 16 then this will put the NUL terminator one element beyond the end of the array.

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

No idea why I added + 1... Thanks for your correction.

Quoted 18 lines
---
 sound/core/control_led.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/control_led.c +1 -1
index 202b475d0bf3..ab5a455723c8 100644
@@ -391,7 +391,7 @@ static ssize_t store_mode(struct device *dev, struct device_attribute *attr,
 {
 	struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
 	char _buf[16];
-	size_t l = min(count, sizeof(_buf) - 1) + 1;
+	size_t l = min(count, sizeof(_buf) - 1);
 	enum snd_ctl_led_mode mode;
 
 	memcpy(_buf, buf, l);
Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#3 lore raw

Re: [PATCH 2/2] ALSA: control - off by one in store_mode()

Quoted 11 lines · Jaroslav Kysela

Dne 02. 04. 21 v 13:42 Dan Carpenter napsal(a):

Quoted 5 lines

If count is 16 then this will put the NUL terminator one element beyond the end of the array.

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

No idea why I added + 1... Thanks for your correction.

Applied now. Thanks.

Takashi

maintainer
#4 lore raw
In reply to Dan Carpenter

Dne 02. 04. 21 v 13:42 Dan Carpenter napsal(a):

Quoted 22 lines

This unwind loop needs to free snd_ctl_leds[0] as well.

Fixes cb17fe0045aa ("ALSA: control - add sysfs support to the LED trigger module")
---
 sound/core/control_led.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
sound/core/control_led.c +1 -1
index d4fb8b873f34..202b475d0bf3 100644
@@ -712,7 +712,7 @@ static struct snd_ctl_layer_ops snd_ctl_led_lops = {
 static int __init snd_ctl_led_init(void)
 {
 	struct snd_ctl_led *led;
-	unsigned int group;
+	int group;
 
 	device_initialize(&snd_ctl_led_dev);
 	snd_ctl_led_dev.class = sound_class;

sound/core/control_led.c static int __init snd_ctl_led_init(void) · line 734

Quoted 6 lines
sound/core/control_led.c +1 -1
@@ -730,7 +730,7 @@ static int __init snd_ctl_led_init(void)
 		dev_set_name(&led->dev, led->name);
 		if (device_add(&led->dev)) {
 			put_device(&led->dev);
-			for (; group > 0; group--) {
+			for (; group >= 0; group--) {
 				led = &snd_ctl_leds[group];

It's not correct. This assignent should be 'led = &snd_ctl_leds[group - 1];' without other changes, because the put_device() is enough when device_add() fails.

Could you resend the correction?

Jaroslav

Quoted 3 lines
sound/core/control_led.c +0 -0
 				device_del(&led->dev);
 			}
Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#5 lore raw

PATCH v2 1/2 ALSA: control - double free in snd_ctl_led_init()

"group - 1" was intended here instead of "group". The current error handling will double free the first item in the array and leak the last item.

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

---
v2: The first patch wasn't right. It fixed the leak but left the double free.

 sound/core/control_led.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/control_led.c +1 -1
index d756a52e58db..93b201063c7d 100644
@@ -734,7 +734,7 @@ static int __init snd_ctl_led_init(void)
 		if (device_add(&led->dev)) {
 			put_device(&led->dev);
 			for (; group > 0; group--) {
-				led = &snd_ctl_leds[group];
+				led = &snd_ctl_leds[group - 1];
 				device_del(&led->dev);
 			}
 			device_del(&snd_ctl_led_dev);
Signature · 1 line
2.30.2
maintainer Reviewed-by on v2 1/2
#6 lore raw

Re: [PATCH 1/2 v2] ALSA: control - double free in snd_ctl_led_init()

Dne 09. 04. 21 v 14:34 Dan Carpenter napsal(a):

Quoted 6 lines

"group - 1" was intended here instead of "group". The current error handling will double free the first item in the array and leak the last item.

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

---
v2: The first patch wasn't right. It fixed the leak but left the double free.

 sound/core/control_led.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/control_led.c +1 -1
index d756a52e58db..93b201063c7d 100644
@@ -734,7 +734,7 @@ static int __init snd_ctl_led_init(void)
 		if (device_add(&led->dev)) {
 			put_device(&led->dev);
 			for (; group > 0; group--) {
-				led = &snd_ctl_leds[group];
+				led = &snd_ctl_leds[group - 1];
 				device_del(&led->dev);
 			}
 			device_del(&snd_ctl_led_dev);
Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#7 lore raw

Re: [PATCH 1/2 v2] ALSA: control - double free in snd_ctl_led_init()

In reply to Dan Carpenter
Quoted 10 lines · Dan Carpenter

"group - 1" was intended here instead of "group". The current error handling will double free the first item in the array and leak the last item.

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

---
v2: The first patch wasn't right. It fixed the leak but left the double free.

Applied now. Thanks.

Takashi