Skip to contentSkip to conversation

led: triggers: Fix NULL dereference in led_trigger_set() error handling

PATCH In review Dan Carpenter 4 participants 10 messages Sep 5, 2019 – Sep 8, 2019

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

PATCH 1/1 led: triggers: Fix NULL dereference in led_trigger_set() error handling

The problem is we set "led_cdev->trigger = NULL;" and then dereference it when we call write_lock_irqsave():

	write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
                            ^^^^^^^^^^^^^^^^^
Fixes 2282e125a406 ("leds: triggers: let struct led_trigger::activate() return an error code")
---
 drivers/leds/led-triggers.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/leds/led-triggers.c +2 -2
index eff1bda8b520..13cea227277c 100644
@@ -167,12 +167,12 @@ int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
 		trig->deactivate(led_cdev);
 err_activate:
 
-	led_cdev->trigger = NULL;
-	led_cdev->trigger_data = NULL;
 	write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
 	list_del(&led_cdev->trig_list);
 	write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock, flags);
+	led_cdev->trigger = NULL;
+	led_cdev->trigger_data = NULL;
 	led_set_brightness(led_cdev, LED_OFF);
 	kfree(event);
 
 	return ret;
Signature · 1 line
2.20.1
#1 lore raw

Hello Dan,
If you amend my patch, please keep my Signed-of-by.

Quoted 36 lines

5 вер. 2019 р. о 12:57 пп Dan Carpenter <[email protected]> написав(ла):

The problem is we set "led_cdev->trigger = NULL;" and then dereference it when we call write_lock_irqsave():

	write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
                           ^^^^^^^^^^^^^^^^^
Fixes 2282e125a406 ("leds: triggers: let struct led_trigger::activate() return an error code")

---
drivers/leds/led-triggers.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
drivers/leds/led-triggers.c +0 -0
index eff1bda8b520..13cea227277c 100644
@@ -167,12 +167,12 @@ int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
 		trig->deactivate(led_cdev);

err_activate:

drivers/leds/led-triggers.c +0 -2
-	led_cdev->trigger = NULL;
-	led_cdev->trigger_data = NULL;
	write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
	list_del(&led_cdev->trig_list);
	write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock, flags);
drivers/leds/led-triggers.c +2 -0
+	led_cdev->trigger = NULL;
+	led_cdev->trigger_data = NULL;
	led_set_brightness(led_cdev, LED_OFF);
	kfree(event);

	return ret;
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Dan Carpenter

Hello,

Quoted 8 lines · Dan Carpenter

The problem is we set "led_cdev->trigger = NULL;" and then dereference it when we call write_lock_irqsave():

	write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
                            ^^^^^^^^^^^^^^^^^
Fixes 2282e125a406 ("leds: triggers: let struct led_trigger::activate() return an error code")

Obviously right. Thanks for catching.

Did you find this at runtime or by using some static checker?

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#3 lore raw

Hello Jacek,

Quoted 11 lines

5 вер. 2019 р. о 3:06 пп Uwe Kleine-König <[email protected]> написав(ла):

Hello,

Quoted 6 lines · Dan Carpenter

The problem is we set "led_cdev->trigger = NULL;" and then dereference it when we call write_lock_irqsave():

	write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
                           ^^^^^^^^^^^^^^^^^
Quoted 8 lines
Quoted 2 lines
Fixes 2282e125a406 ("leds: triggers: let struct led_trigger::activate() return an error code")

Obviously right. Thanks for catching.

Did you find this at runtime or by using some static checker?

Let me summarize the chronology of the last activities below:
1. I have sent the patch for the bugs that I have found by static analyzer at PVS-Studio

    Date: Wed, 4 Sep 2019 00:18:19 +0300
    https://www.spinics.net/lists/linux-leds/msg13181.html

2. At 5 Sep 2019 12:57:19 +0300 Time Dan Cartpen has sent the patch with the same proposal
3. Uwe Kleine-König started to discuss his results of review by asking Dan on how he was found it.

Would you mine if you will keep me as a Original author of this patch based on fact 1?

Best regards,
Oleh Kravchenko

Quoted 7 lines

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#4 lore raw
Quoted 24 lines · Oleh Kravchenko

Hello Jacek,

Quoted 11 lines

5 вер. 2019 р. о 3:06 пп Uwe Kleine-König <[email protected]> написав(ла):

Hello,

Quoted 6 lines · Dan Carpenter

The problem is we set "led_cdev->trigger = NULL;" and then dereference it when we call write_lock_irqsave():

	write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
                           ^^^^^^^^^^^^^^^^^
Quoted 8 lines
Quoted 2 lines
Fixes 2282e125a406 ("leds: triggers: let struct led_trigger::activate() return an error code")

Obviously right. Thanks for catching.

Did you find this at runtime or by using some static checker?

Let me summarize the chronology of the last activities below:
1. I have sent the patch for the bugs that I have found by static analyzer at PVS-Studio

Quoted 7 lines
    Date: Wed, 4 Sep 2019 00:18:19 +0300
    https://www.spinics.net/lists/linux-leds/msg13181.html

2. At 5 Sep 2019 12:57:19 +0300 Time Dan Cartpen has sent the patch with the same proposal
3. Uwe Kleine-König started to discuss his results of review by asking Dan on how he was found it.

Would you mine if you will keep me as a Original author of this patch based on fact 1?

I don't care much personally but it seems fair to take Oleh's version. I didn't see Oleh's patch before as only Dan's was Cc:d to me.

Feel free to add my Reviewed-by also to Oleh's patch of course.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#5 lore raw
Quoted 31 lines · Uwe Kleine-König
Quoted 30 lines · Oleh Kravchenko

Hello Jacek,

Quoted 11 lines

5 вер. 2019 р. о 3:06 пп Uwe Kleine-König <[email protected]> написав(ла):

Hello,

Quoted 6 lines · Dan Carpenter

The problem is we set "led_cdev->trigger = NULL;" and then dereference it when we call write_lock_irqsave():

	write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
                           ^^^^^^^^^^^^^^^^^
Quoted 8 lines
Quoted 2 lines
Fixes 2282e125a406 ("leds: triggers: let struct led_trigger::activate() return an error code")

Obviously right. Thanks for catching.

Did you find this at runtime or by using some static checker?

Let me summarize the chronology of the last activities below:
1. I have sent the patch for the bugs that I have found by static analyzer at PVS-Studio

    Date: Wed, 4 Sep 2019 00:18:19 +0300
    https://www.spinics.net/lists/linux-leds/msg13181.html

2. At 5 Sep 2019 12:57:19 +0300 Time Dan Cartpen has sent the patch with the same proposal
3. Uwe Kleine-König started to discuss his results of review by asking Dan on how he was found it.

Quoted 6 lines
Quoted 1 line

Would you mine if you will keep me as a Original author of this patch based on fact 1?

I don't care much personally but it seems fair to take Oleh's version. I didn't see Oleh's patch before as only Dan's was Cc:d to me.

Feel free to add my Reviewed-by also to Oleh's patch of course.

I've applied the patch from Oleh then, and added your Reviewed-by.

Signature · 2 lines
Best regards,
Jacek Anaszewski
#6 lore raw
In reply to Oleh Kravchenko
Quoted 9 lines · Oleh Kravchenko

Let me summarize the chronology of the last activities below:
1. I have sent the patch for the bugs that I have found by static analyzer at PVS-Studio

    Date: Wed, 4 Sep 2019 00:18:19 +0300
    https://www.spinics.net/lists/linux-leds/msg13181.html

2. At 5 Sep 2019 12:57:19 +0300 Time Dan Cartpen has sent the patch with the same proposal
3. Uwe Kleine-König started to discuss his results of review by asking Dan on how he was found it.

Would you mine if you will keep me as a Original author of this patch based on fact 1?

Heh.

No, I didn't steal your patch. :P I am the author of the Smatch static analysis tool and mostly fix things found by Smatch. I don't use other static analysis tools except to do a final QC of my patches.

It's super common for people to send duplicate fixes when it's based on static analysis. Most of the static analysis people hang out on kernel-janitors so we don't send duplicate patches. For a while people were getting annoyed by all the duplicates but now they accept it as their punishment for introducing a bug in the first place.

Anyway, the rule for kernel development is that normally the first person's patch goes in, so we will take your patch.

regards,
dan carpenter

#7 lore raw

Hello Dan,

Quoted 11 lines

5 вер. 2019 р. о 4:32 пп Dan Carpenter <[email protected]> написав(ла):

Quoted 8 lines · Oleh Kravchenko

Let me summarize the chronology of the last activities below:
1. I have sent the patch for the bugs that I have found by static analyzer at PVS-Studio
Date: Wed, 4 Sep 2019 00:18:19 +0300
https://www.spinics.net/lists/linux-leds/msg13181.html

2. At 5 Sep 2019 12:57:19 +0300 Time Dan Cartpen has sent the patch with the same proposal
3. Uwe Kleine-König started to discuss his results of review by asking Dan on how he was found it.

Quoted 8 lines
Quoted 1 line

Would you mine if you will keep me as a Original author of this patch based on fact 1?

Heh.

No, I didn't steal your patch. :P I am the author of the Smatch static analysis tool and mostly fix things found by Smatch. I don't use other static analysis tools except to do a final QC of my patches.

Thanks!
I didn’t know this tool. Will take a look on it.

By the way, you can use PVS-Studio freely
https://www.viva64.com/en/b/0600/
They provide free license for open source projects.

Quoted 6 lines

It's super common for people to send duplicate fixes when it's based on static analysis. Most of the static analysis people hang out on kernel-janitors so we don't send duplicate patches. For a while people were getting annoyed by all the duplicates but now they accept it as their punishment for introducing a bug in the first place.

No problem.
We all doing good things!

Quoted 3 lines

Anyway, the rule for kernel development is that normally the first person's patch goes in, so we will take your patch.

I think next time you will take a look at mail list before sending patches ;-)

Quoted 3 lines

regards,
dan carpenter

P.S.:
resent because was in non plain-text format.

Signature · 2 lines
Best regards,
Oleh Kravchenko
#8 lore raw
In reply to Uwe Kleine-König

Hello Jacek,

Quoted 11 lines

5 вер. 2019 р. о 3:06 пп Uwe Kleine-König <[email protected]> написав(ла):

Hello,

Quoted 6 lines · Dan Carpenter

The problem is we set "led_cdev->trigger = NULL;" and then dereference it when we call write_lock_irqsave():

	write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
                           ^^^^^^^^^^^^^^^^^
Quoted 8 lines
Quoted 2 lines
Fixes 2282e125a406 ("leds: triggers: let struct led_trigger::activate() return an error code")

Obviously right. Thanks for catching.

Did you find this at runtime or by using some static checker?

Let me summarize the chronology of the last activities below:
1. I have sent the patch for the bugs that I have found by static analyzer at PVS-Studio

    Date: Wed, 4 Sep 2019 00:18:19 +0300
    https://www.spinics.net/lists/linux-leds/msg13181.html

2. At 5 Sep 2019 12:57:19 +0300 Time Dan Cartpen has sent the patch with the same proposal

3. Uwe Kleine-König started to discuss his results of review by asking Dan on how he was found it.

Would you mine if you will keep me as a Original author of this patch based on fact 1?

Best regards,
Oleh Kravchenko

Quoted 7 lines

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#9 lore raw
In reply to Uwe Kleine-König
Quoted 9 lines · Uwe Kleine-König

Hello,

Quoted 6 lines · Dan Carpenter

The problem is we set "led_cdev->trigger = NULL;" and then dereference it when we call write_lock_irqsave():

	write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
                            ^^^^^^^^^^^^^^^^^
Quoted 8 lines
Quoted 2 lines
Fixes 2282e125a406 ("leds: triggers: let struct led_trigger::activate() return an error code")

Obviously right. Thanks for catching.

Did you find this at runtime or by using some static checker?

Yes. It's a new one that I'm working on.

It's a tricky thing because it turns out Smatch thinks a whole lot of pointers are definitely NULL when they aren't. For example, if the struct is allocated with kzalloc() and Smatch doesn't see where the pointer is assigned.

regards,
dan carpenter