public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] thermal: Fix zone lookup by ID
@ 2020-07-24 17:01 Thierry Reding
  2020-07-24 17:12 ` Daniel Lezcano
  0 siblings, 1 reply; 4+ messages in thread
From: Thierry Reding @ 2020-07-24 17:01 UTC (permalink / raw)
  To: Zhang Rui, Daniel Lezcano; +Cc: Amit Kucheria, linux-pm, linux-kernel

From: Thierry Reding <treding@nvidia.com>

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")
Signed-off-by: Thierry Reding <treding@nvidia.com>
---
 drivers/thermal/thermal_core.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index 007f9618e20a..9748fbb9a3a1 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -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,
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] thermal: Fix zone lookup by ID
  2020-07-24 17:01 [PATCH] thermal: Fix zone lookup by ID Thierry Reding
@ 2020-07-24 17:12 ` Daniel Lezcano
  2020-07-27  9:01   ` Thierry Reding
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel Lezcano @ 2020-07-24 17:12 UTC (permalink / raw)
  To: Thierry Reding, Zhang Rui; +Cc: Amit Kucheria, linux-pm, linux-kernel

Hi Thierry,

Applied, thanks for the fix!


On 24/07/2020 19:01, Thierry Reding wrote:
> From: Thierry Reding <treding@nvidia.com>
> 
> 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")
> Signed-off-by: Thierry Reding <treding@nvidia.com>
> ---
>  drivers/thermal/thermal_core.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
> index 007f9618e20a..9748fbb9a3a1 100644
> --- a/drivers/thermal/thermal_core.c
> +++ b/drivers/thermal/thermal_core.c
> @@ -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,
> 


-- 
<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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] thermal: Fix zone lookup by ID
  2020-07-24 17:12 ` Daniel Lezcano
@ 2020-07-27  9:01   ` Thierry Reding
  2020-07-27  9:04     ` Daniel Lezcano
  0 siblings, 1 reply; 4+ messages in thread
From: Thierry Reding @ 2020-07-27  9:01 UTC (permalink / raw)
  To: Daniel Lezcano; +Cc: Zhang Rui, Amit Kucheria, linux-pm, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 425 bytes --]

On Fri, Jul 24, 2020 at 07:12:49PM +0200, Daniel Lezcano wrote:
> 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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] thermal: Fix zone lookup by ID
  2020-07-27  9:01   ` Thierry Reding
@ 2020-07-27  9:04     ` Daniel Lezcano
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel Lezcano @ 2020-07-27  9:04 UTC (permalink / raw)
  To: Thierry Reding; +Cc: Zhang Rui, Amit Kucheria, linux-pm, linux-kernel


Hi Thierry,

thanks for testing the code.

On 27/07/2020 11:01, Thierry Reding wrote:
> On Fri, Jul 24, 2020 at 07:12:49PM +0200, Daniel Lezcano wrote:
>> 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/




-- 
<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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-07-27  9:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-07-24 17:01 [PATCH] thermal: Fix zone lookup by ID Thierry Reding
2020-07-24 17:12 ` Daniel Lezcano
2020-07-27  9:01   ` Thierry Reding
2020-07-27  9:04     ` Daniel Lezcano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox