Skip to contentSkip to conversation

thermal: Fix zone lookup by ID

PATCH Applied Thierry Reding 2 participants 4 messages Jul 24, 2020 – Jul 27, 2020 Thermal

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 thermal: Fix zone lookup by ID

From: Thierry Reding <[email protected]>

When a thermal zone is looked up by an ID and no zone is found matching that ID, the thermal_zone_get_by_id() function will return a pointer to the thermal zone list head which isn't actually a valid thermal zone.

This can lead to a subsequent crash because a valid pointer is returned to the called, but dereferencing that pointer as struct thermal_zone is not safe.

Fixes 329b064fbd13 ("thermal: core: Get thermal zone by id")
---
 drivers/thermal/thermal_core.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/thermal/thermal_core.c +5 -3
index 007f9618e20a..9748fbb9a3a1 100644
@@ -751,16 +751,18 @@ int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
 
 struct thermal_zone_device *thermal_zone_get_by_id(int id)
 {
-	struct thermal_zone_device *tz = NULL;
+	struct thermal_zone_device *tz, *match = NULL;
 
 	mutex_lock(&thermal_list_lock);
 	list_for_each_entry(tz, &thermal_tz_list, node) {
-		if (tz->id == id)
+		if (tz->id == id) {
+			match = tz;
 			break;
+		}
 	}
 	mutex_unlock(&thermal_list_lock);
 
-	return tz;
+	return match;
 }
 
 void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
Signature · 1 line
2.27.0
#1 lore raw

Hi Thierry,

Applied, thanks for the fix!

Quoted 43 lines · Thierry Reding

From: Thierry Reding <[email protected]>

When a thermal zone is looked up by an ID and no zone is found matching that ID, the thermal_zone_get_by_id() function will return a pointer to the thermal zone list head which isn't actually a valid thermal zone.

This can lead to a subsequent crash because a valid pointer is returned to the called, but dereferencing that pointer as struct thermal_zone is not safe.

Fixes 329b064fbd13 ("thermal: core: Get thermal zone by id")
---
 drivers/thermal/thermal_core.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/thermal/thermal_core.c +5 -3
index 007f9618e20a..9748fbb9a3a1 100644
@@ -751,16 +751,18 @@ int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
 
 struct thermal_zone_device *thermal_zone_get_by_id(int id)
 {
-	struct thermal_zone_device *tz = NULL;
+	struct thermal_zone_device *tz, *match = NULL;
 
 	mutex_lock(&thermal_list_lock);
 	list_for_each_entry(tz, &thermal_tz_list, node) {
-		if (tz->id == id)
+		if (tz->id == id) {
+			match = tz;
 			break;
+		}
 	}
 	mutex_unlock(&thermal_list_lock);
 
-	return tz;
+	return match;
 }
 
 void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#2 lore raw
Quoted 3 lines · Daniel Lezcano

Hi Thierry,

Applied, thanks for the fix!

I ran into this as I was experimenting with the new netlink interface and got things wrong. Do you have any userspace that you use for testing this that I can use as a reference?

I have managed to get it to work now, but I'm still interested in any userspace that you might have for this.

Thierry

#3 lore raw

Hi Thierry,

thanks for testing the code.

Quoted 11 lines · Thierry Reding
Quoted 3 lines · Daniel Lezcano

Hi Thierry,

Applied, thanks for the fix!

I ran into this as I was experimenting with the new netlink interface and got things wrong. Do you have any userspace that you use for testing this that I can use as a reference?

I have managed to get it to work now, but I'm still interested in any userspace that you might have for this.

https://git.linaro.org/people/daniel.lezcano/thermal-genl.git/

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog