All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] thermal/core: Fix parameter check when setting trip point temperatures
@ 2022-10-04 17:18 Guenter Roeck
  2022-10-04 17:30 ` Daniel Lezcano
  0 siblings, 1 reply; 2+ messages in thread
From: Guenter Roeck @ 2022-10-04 17:18 UTC (permalink / raw)
  To: linux-pm
  Cc: Rafael J . Wysocki, Amit Kucheria, Zhang Rui, linux-kernel,
	Guenter Roeck, Daniel Lezcano

Commit 9326167058e8a ("thermal/core: Move set_trip_temp ops to the sysfs
code") changed the parameter check in trip_point_temp_store() from

	if (!tz->ops->set_trip_temp)

to
	if (!tz->ops->set_trip_temp && !tz->trips)

That means the condition will pass if either tz->ops->set_trip_temp
or tz->trips is not NULL. Subsequently, access to tz->trips is
checked again, but tz->ops->set_trip_temp is called unconditionally.
This will result in a crash if the set_trip_temp callback is not set.
Add check if tz->ops->set_trip_temp is NULL before trying to call it.

Fixes: 9326167058e8a ("thermal/core: Move set_trip_temp ops to the sysfs code")
Cc: Daniel Lezcano <daniel.lezcano@linaro.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/thermal/thermal_sysfs.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
index 78c5841bdfae..ec495c7dff03 100644
--- a/drivers/thermal/thermal_sysfs.c
+++ b/drivers/thermal/thermal_sysfs.c
@@ -128,9 +128,11 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
 	if (kstrtoint(buf, 10, &temperature))
 		return -EINVAL;
 
-	ret = tz->ops->set_trip_temp(tz, trip, temperature);
-	if (ret)
-		return ret;
+	if (tz->ops->set_trip_temp) {
+		ret = tz->ops->set_trip_temp(tz, trip, temperature);
+		if (ret)
+			return ret;
+	}
 
 	if (tz->trips)
 		tz->trips[trip].temperature = temperature;
-- 
2.36.2


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

* Re: [PATCH] thermal/core: Fix parameter check when setting trip point temperatures
  2022-10-04 17:18 [PATCH] thermal/core: Fix parameter check when setting trip point temperatures Guenter Roeck
@ 2022-10-04 17:30 ` Daniel Lezcano
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Lezcano @ 2022-10-04 17:30 UTC (permalink / raw)
  To: Guenter Roeck, linux-pm
  Cc: Rafael J . Wysocki, Amit Kucheria, Zhang Rui, linux-kernel


Hi Guenter,

this has been already fixed and the PR has been sent to Rafael:

https://git.kernel.org/pub/scm/linux/kernel/git/thermal/linux.git/tag/?h=thermal-v6.1-rc1-2

Thanks for sending the fix anyway

   -- Daniel

On 04/10/2022 19:18, Guenter Roeck wrote:
> Commit 9326167058e8a ("thermal/core: Move set_trip_temp ops to the sysfs
> code") changed the parameter check in trip_point_temp_store() from
> 
> 	if (!tz->ops->set_trip_temp)
> 
> to
> 	if (!tz->ops->set_trip_temp && !tz->trips)
> 
> That means the condition will pass if either tz->ops->set_trip_temp
> or tz->trips is not NULL. Subsequently, access to tz->trips is
> checked again, but tz->ops->set_trip_temp is called unconditionally.
> This will result in a crash if the set_trip_temp callback is not set.
> Add check if tz->ops->set_trip_temp is NULL before trying to call it.
> 
> Fixes: 9326167058e8a ("thermal/core: Move set_trip_temp ops to the sysfs code")
> Cc: Daniel Lezcano <daniel.lezcano@linaro.org>
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
>   drivers/thermal/thermal_sysfs.c | 8 +++++---
>   1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
> index 78c5841bdfae..ec495c7dff03 100644
> --- a/drivers/thermal/thermal_sysfs.c
> +++ b/drivers/thermal/thermal_sysfs.c
> @@ -128,9 +128,11 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
>   	if (kstrtoint(buf, 10, &temperature))
>   		return -EINVAL;
>   
> -	ret = tz->ops->set_trip_temp(tz, trip, temperature);
> -	if (ret)
> -		return ret;
> +	if (tz->ops->set_trip_temp) {
> +		ret = tz->ops->set_trip_temp(tz, trip, temperature);
> +		if (ret)
> +			return ret;
> +	}
>   
>   	if (tz->trips)
>   		tz->trips[trip].temperature = temperature;


-- 
<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] 2+ messages in thread

end of thread, other threads:[~2022-10-04 17:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-04 17:18 [PATCH] thermal/core: Fix parameter check when setting trip point temperatures Guenter Roeck
2022-10-04 17:30 ` Daniel Lezcano

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.