thermal: add boundary check to set_cur_state
Srikar Srimath Tirumala [Tue, 12 Sep 2017 19:27:13 +0000 (12:27 -0700)]
Prevent sysfs from setting a cur_state that exceeds the max cur_state
of the cooling device.

Bug 200334223
Bug 200331706
Bug 1968660
Bug 1968616

Change-Id: I935be6166a9e184683abfcdce70cb08cbe4a1350
Signed-off-by: Srikar Srimath Tirumala <srikars@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/1558407
(cherry picked from commit 142cf9d96ed221124ea2b778dc37cf5db8d5702c)
Reviewed-on: https://git-master.nvidia.com/r/1661413
Reviewed-on: https://git-master.nvidia.com/r/1662626
GVS: Gerrit_Virtual_Submit
Tested-by: Amulya Yarlagadda <ayarlagadda@nvidia.com>
Reviewed-by: Winnie Hsu <whsu@nvidia.com>

drivers/thermal/thermal_core.c

index ffb4b9c..68e6e09 100644 (file)
@@ -944,8 +944,8 @@ thermal_cooling_device_cur_state_store(struct device *dev,
                                       const char *buf, size_t count)
 {
        struct thermal_cooling_device *cdev = to_cooling_device(dev);
-       unsigned long state;
-       int result;
+       unsigned long state, max_state;
+       int result, ret;
 
        if (!sscanf(buf, "%ld\n", &state))
                return -EINVAL;
@@ -953,6 +953,13 @@ thermal_cooling_device_cur_state_store(struct device *dev,
        if ((long)state < 0)
                return -EINVAL;
 
+       ret = cdev->ops->get_max_state(cdev, &max_state);
+       if (ret)
+               return ret;
+
+       if (state > max_state)
+               return -EINVAL;
+
        result = cdev->ops->set_cur_state(cdev, state);
        if (result)
                return result;