Skip to contentSkip to conversation

thermal: sysfs: Perform bounds check when storing thermal states

PATCH In review Varad Gautam 3 participants 11 messages Jul 5, 2022 – Jul 6, 2022

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

PATCH 1/1 thermal: sysfs: Perform bounds check when storing thermal states

Check that a user-provided thermal state is within the maximum thermal states supported by a given driver before attempting to apply it. This prevents a subsequent OOB access in thermal_cooling_device_stats_update() while performing state-transition accounting on drivers that do not have this check in their set_cur_state() handle.

---
 drivers/thermal/thermal_sysfs.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/thermal/thermal_sysfs.c +11 -1
index 1c4aac8464a7..0c6b0223b133 100644
@@ -607,7 +607,7 @@ cur_state_store(struct device *dev, struct device_attribute *attr,
 		const char *buf, size_t count)
 {
 	struct thermal_cooling_device *cdev = to_cooling_device(dev);
-	unsigned long state;
+	unsigned long state, max_state;
 	int result;
 
 	if (sscanf(buf, "%ld\n", &state) != 1)
@@ -618,10 +618,20 @@ cur_state_store(struct device *dev, struct device_attribute *attr,
 
 	mutex_lock(&cdev->lock);
 
+	result = cdev->ops->get_max_state(cdev, &max_state);
+	if (result)
+		goto unlock;
+
+	if (state > max_state) {
+		result = -EINVAL;
+		goto unlock;
+	}
+
 	result = cdev->ops->set_cur_state(cdev, state);
 	if (!result)
 		thermal_cooling_device_stats_update(cdev, state);
 
+unlock:
 	mutex_unlock(&cdev->lock);
 	return result ? result : count;
 }
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
maintainer
#1 lore raw
Quoted 30 lines · Varad Gautam

Check that a user-provided thermal state is within the maximum thermal states supported by a given driver before attempting to apply it. This prevents a subsequent OOB access in thermal_cooling_device_stats_update() while performing state-transition accounting on drivers that do not have this check in their set_cur_state() handle.

---
 drivers/thermal/thermal_sysfs.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/thermal/thermal_sysfs.c +1 -1
index 1c4aac8464a7..0c6b0223b133 100644
@@ -607,7 +607,7 @@ cur_state_store(struct device *dev, struct device_attribute *attr,
 		const char *buf, size_t count)
 {
 	struct thermal_cooling_device *cdev = to_cooling_device(dev);
-	unsigned long state;
+	unsigned long state, max_state;
 	int result;
 
 	if (sscanf(buf, "%ld\n", &state) != 1)
@@ -618,10 +618,20 @@ cur_state_store(struct device *dev, struct device_attribute *attr,
 
 	mutex_lock(&cdev->lock);
 

drivers/thermal/thermal_sysfs.c cur_state_store(struct device *dev, struct device_attribute *attr, · line 630

Quoted 10 lines
drivers/thermal/thermal_sysfs.c +9 -0
+	result = cdev->ops->get_max_state(cdev, &max_state);
+	if (result)
+		goto unlock;
+
+	if (state > max_state) {
+		result = -EINVAL;
+		goto unlock;
+	}
+
 	result = cdev->ops->set_cur_state(cdev, state);

Why doesn't set_cur_state() check the max state before setting it? Why are the callers forced to always check it before? That feels wrong...

thanks,

greg k-h

#2 lore raw
Quoted 32 lines · Greg KH
Quoted 30 lines · Varad Gautam

Check that a user-provided thermal state is within the maximum thermal states supported by a given driver before attempting to apply it. This prevents a subsequent OOB access in thermal_cooling_device_stats_update() while performing state-transition accounting on drivers that do not have this check in their set_cur_state() handle.

---
 drivers/thermal/thermal_sysfs.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/thermal/thermal_sysfs.c +1 -1
index 1c4aac8464a7..0c6b0223b133 100644
@@ -607,7 +607,7 @@ cur_state_store(struct device *dev, struct device_attribute *attr,
              const char *buf, size_t count)
 {
      struct thermal_cooling_device *cdev = to_cooling_device(dev);
-     unsigned long state;
+     unsigned long state, max_state;
      int result;
 
      if (sscanf(buf, "%ld\n", &state) != 1)
@@ -618,10 +618,20 @@ cur_state_store(struct device *dev, struct device_attribute *attr,
 
      mutex_lock(&cdev->lock);
 
Quoted 14 lines

drivers/thermal/thermal_sysfs.c cur_state_store(struct device *dev, struct device_attribute *attr, · line 630

Quoted 10 lines
drivers/thermal/thermal_sysfs.c +9 -0
+     result = cdev->ops->get_max_state(cdev, &max_state);
+     if (result)
+             goto unlock;
+
+     if (state > max_state) {
+             result = -EINVAL;
+             goto unlock;
+     }
+
      result = cdev->ops->set_cur_state(cdev, state);

Why doesn't set_cur_state() check the max state before setting it? Why are the callers forced to always check it before? That feels wrong...

The problem lies in thermal_cooling_device_stats_update(), not set_cur_state().

If ->set_cur_state() doesn't error out on invalid state, thermal_cooling_device_stats_update() does a:

stats->trans_table[stats->state * stats->max_states + new_state]++;

stats->trans_table reserves space depending on max_states, but we'd end up reading/writing outside it. cur_state_store() can prevent this regardless of the driver's ->set_cur_state() implementation.

Regards,
Varad

Quoted 3 lines

thanks,

greg k-h

maintainer
#3 lore raw
Quoted 50 lines · Varad Gautam
Quoted 32 lines · Greg KH
Quoted 30 lines · Varad Gautam

Check that a user-provided thermal state is within the maximum thermal states supported by a given driver before attempting to apply it. This prevents a subsequent OOB access in thermal_cooling_device_stats_update() while performing state-transition accounting on drivers that do not have this check in their set_cur_state() handle.

---
 drivers/thermal/thermal_sysfs.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/thermal/thermal_sysfs.c +1 -1
index 1c4aac8464a7..0c6b0223b133 100644
@@ -607,7 +607,7 @@ cur_state_store(struct device *dev, struct device_attribute *attr,
              const char *buf, size_t count)
 {
      struct thermal_cooling_device *cdev = to_cooling_device(dev);
-     unsigned long state;
+     unsigned long state, max_state;
      int result;
 
      if (sscanf(buf, "%ld\n", &state) != 1)
@@ -618,10 +618,20 @@ cur_state_store(struct device *dev, struct device_attribute *attr,
 
      mutex_lock(&cdev->lock);
 
Quoted 14 lines

drivers/thermal/thermal_sysfs.c cur_state_store(struct device *dev, struct device_attribute *attr, · line 630

Quoted 10 lines
drivers/thermal/thermal_sysfs.c +9 -0
+     result = cdev->ops->get_max_state(cdev, &max_state);
+     if (result)
+             goto unlock;
+
+     if (state > max_state) {
+             result = -EINVAL;
+             goto unlock;
+     }
+
      result = cdev->ops->set_cur_state(cdev, state);

Why doesn't set_cur_state() check the max state before setting it? Why are the callers forced to always check it before? That feels wrong...

The problem lies in thermal_cooling_device_stats_update(), not set_cur_state().

Quoted 8 lines

If ->set_cur_state() doesn't error out on invalid state, thermal_cooling_device_stats_update() does a:

stats->trans_table[stats->state * stats->max_states + new_state]++;

stats->trans_table reserves space depending on max_states, but we'd end up reading/writing outside it. cur_state_store() can prevent this regardless of the driver's ->set_cur_state() implementation.

Why wouldn't cur_state_store() check for an out-of-bounds condition by calling get_max_state() and then return an error if it is invalid, preventing thermal_cooling_device_stats_update() from ever being called?

thanks,

greg k-h

#4 lore raw
Quoted 57 lines · Greg KH
Quoted 55 lines · Varad Gautam
Quoted 32 lines · Greg KH
Quoted 30 lines · Varad Gautam

Check that a user-provided thermal state is within the maximum thermal states supported by a given driver before attempting to apply it. This prevents a subsequent OOB access in thermal_cooling_device_stats_update() while performing state-transition accounting on drivers that do not have this check in their set_cur_state() handle.

---
 drivers/thermal/thermal_sysfs.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/thermal/thermal_sysfs.c +1 -1
index 1c4aac8464a7..0c6b0223b133 100644
@@ -607,7 +607,7 @@ cur_state_store(struct device *dev, struct device_attribute *attr,
              const char *buf, size_t count)
 {
      struct thermal_cooling_device *cdev = to_cooling_device(dev);
-     unsigned long state;
+     unsigned long state, max_state;
      int result;
 
      if (sscanf(buf, "%ld\n", &state) != 1)
@@ -618,10 +618,20 @@ cur_state_store(struct device *dev, struct device_attribute *attr,
 
      mutex_lock(&cdev->lock);
 
Quoted 14 lines

drivers/thermal/thermal_sysfs.c cur_state_store(struct device *dev, struct device_attribute *attr, · line 630

Quoted 10 lines
drivers/thermal/thermal_sysfs.c +9 -0
+     result = cdev->ops->get_max_state(cdev, &max_state);
+     if (result)
+             goto unlock;
+
+     if (state > max_state) {
+             result = -EINVAL;
+             goto unlock;
+     }
+
      result = cdev->ops->set_cur_state(cdev, state);

Why doesn't set_cur_state() check the max state before setting it? Why are the callers forced to always check it before? That feels wrong...

The problem lies in thermal_cooling_device_stats_update(), not set_cur_state().

If ->set_cur_state() doesn't error out on invalid state, thermal_cooling_device_stats_update() does a:

stats->trans_table[stats->state * stats->max_states + new_state]++;

Quoted 8 lines
Quoted 3 lines

stats->trans_table reserves space depending on max_states, but we'd end up reading/writing outside it. cur_state_store() can prevent this regardless of the driver's ->set_cur_state() implementation.

Why wouldn't cur_state_store() check for an out-of-bounds condition by calling get_max_state() and then return an error if it is invalid, preventing thermal_cooling_device_stats_update() from ever being called?

That's what this patch does, it adds the out-of-bounds check.

Quoted 3 lines

thanks,

greg k-h

reviewer
#5 lore raw
Quoted 77 lines · Varad Gautam
Quoted 75 lines · Greg KH
Quoted 72 lines · Varad Gautam
Quoted 39 lines · Greg KH
Quoted 37 lines · Varad Gautam

Check that a user-provided thermal state is within the maximum
thermal states supported by a given driver before attempting to
apply it. This prevents a subsequent OOB access in thermal_cooling_device_stats_update() while performing state-transition accounting on drivers that do not have this check
in their set_cur_state() handle.

---
 drivers/thermal/thermal_sysfs.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/thermal/thermal_sysfs.c +1 -1
index 1c4aac8464a7..0c6b0223b133 100644
@@ -607,7 +607,7 @@ cur_state_store(struct device *dev,
 struct device_attribute *attr,
              const char *buf, size_t count)
 {
      struct thermal_cooling_device *cdev =
 to_cooling_device(dev);
-     unsigned long state;
+     unsigned long state, max_state;
      int result;
      if (sscanf(buf, "%ld\n", &state) != 1)
drivers/thermal/thermal_sysfs.c +0 -0
@@ -618,10 +618,20 @@ cur_state_store(struct device *dev,
 struct device_attribute *attr,
 
      mutex_lock(&cdev->lock);
 
Quoted 16 lines

drivers/thermal/thermal_sysfs.c cur_state_store(struct device *dev, struct device_attribute *attr, · line 630

Quoted 10 lines
drivers/thermal/thermal_sysfs.c +9 -0
+     result = cdev->ops->get_max_state(cdev, &max_state);
+     if (result)
+             goto unlock;
+
+     if (state > max_state) {
+             result = -EINVAL;
+             goto unlock;
+     }
+
      result = cdev->ops->set_cur_state(cdev, state);

Why doesn't set_cur_state() check the max state before setting it? Why
are the callers forced to always check it before? That feels wrong...

The problem lies in thermal_cooling_device_stats_update(), not set_cur_state().

If ->set_cur_state() doesn't error out on invalid state, thermal_cooling_device_stats_update() does a:

stats->trans_table[stats->state * stats->max_states + new_state]++;

stats->trans_table reserves space depending on max_states, but we'd end up
reading/writing outside it. cur_state_store() can prevent this regardless of
the driver's ->set_cur_state() implementation.

Quoted 8 lines
Quoted 6 lines

Why wouldn't cur_state_store() check for an out-of-bounds condition by
calling get_max_state() and then return an error if it is invalid, preventing thermal_cooling_device_stats_update() from ever being called?

That's what this patch does, it adds the out-of-bounds check.

No, I think Greg' question is
why cdev->ops->set_cur_state() return 0 when setting a cooling state that exceeds the maximum cooling state?

thanks,
rui

Quoted 4 lines
Quoted 4 lines

thanks,

greg k-h

maintainer
#6 lore raw
Quoted 85 lines · Zhang Rui
Quoted 84 lines · Varad Gautam
Quoted 81 lines · Greg KH
Quoted 64 lines · Varad Gautam
Quoted 39 lines · Greg KH
Quoted 37 lines · Varad Gautam

Check that a user-provided thermal state is within the maximum
thermal states supported by a given driver before attempting to
apply it. This prevents a subsequent OOB access in thermal_cooling_device_stats_update() while performing state-transition accounting on drivers that do not have this check
in their set_cur_state() handle.

---
 drivers/thermal/thermal_sysfs.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/thermal/thermal_sysfs.c +1 -1
index 1c4aac8464a7..0c6b0223b133 100644
@@ -607,7 +607,7 @@ cur_state_store(struct device *dev,
 struct device_attribute *attr,
              const char *buf, size_t count)
 {
      struct thermal_cooling_device *cdev =
 to_cooling_device(dev);
-     unsigned long state;
+     unsigned long state, max_state;
      int result;
      if (sscanf(buf, "%ld\n", &state) != 1)
drivers/thermal/thermal_sysfs.c +0 -0
@@ -618,10 +618,20 @@ cur_state_store(struct device *dev,
 struct device_attribute *attr,
 
      mutex_lock(&cdev->lock);
 
Quoted 16 lines

drivers/thermal/thermal_sysfs.c cur_state_store(struct device *dev, struct device_attribute *attr, · line 630

Quoted 10 lines
drivers/thermal/thermal_sysfs.c +9 -0
+     result = cdev->ops->get_max_state(cdev, &max_state);
+     if (result)
+             goto unlock;
+
+     if (state > max_state) {
+             result = -EINVAL;
+             goto unlock;
+     }
+
      result = cdev->ops->set_cur_state(cdev, state);

Why doesn't set_cur_state() check the max state before setting it? Why
are the callers forced to always check it before? That feels wrong...

The problem lies in thermal_cooling_device_stats_update(), not set_cur_state().

If ->set_cur_state() doesn't error out on invalid state, thermal_cooling_device_stats_update() does a:

Quoted 8 lines

stats->trans_table[stats->state * stats->max_states + new_state]++;

stats->trans_table reserves space depending on max_states, but we'd end up
reading/writing outside it. cur_state_store() can prevent this regardless of
the driver's ->set_cur_state() implementation.

Why wouldn't cur_state_store() check for an out-of-bounds condition by
calling get_max_state() and then return an error if it is invalid, preventing thermal_cooling_device_stats_update() from ever being called?

Quoted 5 lines
Quoted 1 line

That's what this patch does, it adds the out-of-bounds check.

No, I think Greg' question is
why cdev->ops->set_cur_state() return 0 when setting a cooling state that exceeds the maximum cooling state?

Yes, that is what I am asking, it should not allow a state to be exceeded.

thanks,

greg k-h

#7 lore raw
Quoted 87 lines · Greg KH
Quoted 85 lines · Zhang Rui
Quoted 84 lines · Varad Gautam
Quoted 81 lines · Greg KH
Quoted 64 lines · Varad Gautam
Quoted 39 lines · Greg KH
Quoted 37 lines · Varad Gautam

Check that a user-provided thermal state is within the maximum
thermal states supported by a given driver before attempting to
apply it. This prevents a subsequent OOB access in thermal_cooling_device_stats_update() while performing state-transition accounting on drivers that do not have this check
in their set_cur_state() handle.

---
 drivers/thermal/thermal_sysfs.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/thermal/thermal_sysfs.c +1 -1
index 1c4aac8464a7..0c6b0223b133 100644
@@ -607,7 +607,7 @@ cur_state_store(struct device *dev,
 struct device_attribute *attr,
              const char *buf, size_t count)
 {
      struct thermal_cooling_device *cdev =
 to_cooling_device(dev);
-     unsigned long state;
+     unsigned long state, max_state;
      int result;
      if (sscanf(buf, "%ld\n", &state) != 1)
drivers/thermal/thermal_sysfs.c +0 -0
@@ -618,10 +618,20 @@ cur_state_store(struct device *dev,
 struct device_attribute *attr,
 
      mutex_lock(&cdev->lock);
 
Quoted 16 lines

drivers/thermal/thermal_sysfs.c cur_state_store(struct device *dev, · line 631

Quoted 10 lines
drivers/thermal/thermal_sysfs.c +9 -0
+     result = cdev->ops->get_max_state(cdev, &max_state);
+     if (result)
+             goto unlock;
+
+     if (state > max_state) {
+             result = -EINVAL;
+             goto unlock;
+     }
+
      result = cdev->ops->set_cur_state(cdev, state);

Why doesn't set_cur_state() check the max state before setting it? Why
are the callers forced to always check it before? That feels wrong...

The problem lies in thermal_cooling_device_stats_update(), not set_cur_state().

If ->set_cur_state() doesn't error out on invalid state, thermal_cooling_device_stats_update() does a:

Quoted 8 lines

stats->trans_table[stats->state * stats->max_states + new_state]++;

stats->trans_table reserves space depending on max_states, but we'd end up
reading/writing outside it. cur_state_store() can prevent this regardless of
the driver's ->set_cur_state() implementation.

Why wouldn't cur_state_store() check for an out-of-bounds condition by
calling get_max_state() and then return an error if it is invalid, preventing thermal_cooling_device_stats_update() from ever being called?

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

That's what this patch does, it adds the out-of-bounds check.

No, I think Greg' question is
why cdev->ops->set_cur_state() return 0 when setting a cooling state that exceeds the maximum cooling state?

Yes, that is what I am asking, it should not allow a state to be exceeded.

Indeed, it is upto the driver to return !0 from cdev->ops->set_cur_state() when setting state > max - and it is a driver bug for not doing so.

But a buggy driver should not lead to cur_state_store() performing an OOB access.

Quoted 3 lines

thanks,

greg k-h

maintainer
#8 lore raw
Quoted 98 lines · Varad Gautam
Quoted 87 lines · Greg KH
Quoted 85 lines · Zhang Rui
Quoted 84 lines · Varad Gautam
Quoted 81 lines · Greg KH
Quoted 64 lines · Varad Gautam
Quoted 39 lines · Greg KH
Quoted 37 lines · Varad Gautam

Check that a user-provided thermal state is within the maximum
thermal states supported by a given driver before attempting to
apply it. This prevents a subsequent OOB access in thermal_cooling_device_stats_update() while performing state-transition accounting on drivers that do not have this check
in their set_cur_state() handle.

---
 drivers/thermal/thermal_sysfs.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/thermal/thermal_sysfs.c +1 -1
index 1c4aac8464a7..0c6b0223b133 100644
@@ -607,7 +607,7 @@ cur_state_store(struct device *dev,
 struct device_attribute *attr,
              const char *buf, size_t count)
 {
      struct thermal_cooling_device *cdev =
 to_cooling_device(dev);
-     unsigned long state;
+     unsigned long state, max_state;
      int result;
      if (sscanf(buf, "%ld\n", &state) != 1)
drivers/thermal/thermal_sysfs.c +0 -0
@@ -618,10 +618,20 @@ cur_state_store(struct device *dev,
 struct device_attribute *attr,
 
      mutex_lock(&cdev->lock);
 
Quoted 16 lines

drivers/thermal/thermal_sysfs.c cur_state_store(struct device *dev, · line 631

Quoted 10 lines
drivers/thermal/thermal_sysfs.c +9 -0
+     result = cdev->ops->get_max_state(cdev, &max_state);
+     if (result)
+             goto unlock;
+
+     if (state > max_state) {
+             result = -EINVAL;
+             goto unlock;
+     }
+
      result = cdev->ops->set_cur_state(cdev, state);

Why doesn't set_cur_state() check the max state before setting it? Why
are the callers forced to always check it before? That feels wrong...

The problem lies in thermal_cooling_device_stats_update(), not set_cur_state().

If ->set_cur_state() doesn't error out on invalid state, thermal_cooling_device_stats_update() does a:

Quoted 8 lines

stats->trans_table[stats->state * stats->max_states + new_state]++;

stats->trans_table reserves space depending on max_states, but we'd end up
reading/writing outside it. cur_state_store() can prevent this regardless of
the driver's ->set_cur_state() implementation.

Why wouldn't cur_state_store() check for an out-of-bounds condition by
calling get_max_state() and then return an error if it is invalid, preventing thermal_cooling_device_stats_update() from ever being called?

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

That's what this patch does, it adds the out-of-bounds check.

No, I think Greg' question is
why cdev->ops->set_cur_state() return 0 when setting a cooling state that exceeds the maximum cooling state?

Yes, that is what I am asking, it should not allow a state to be exceeded.

Quoted 5 lines

Indeed, it is upto the driver to return !0 from cdev->ops->set_cur_state() when setting state > max - and it is a driver bug for not doing so.

But a buggy driver should not lead to cur_state_store() performing an OOB access.

Agreed, which is why the code that does the access should check before it does so. Right now you are relying on the sysfs code to do so, which seems very wrong.

thanks,

greg k-h

#9 lore raw
Quoted 103 lines · Greg KH
Quoted 101 lines · Varad Gautam
Quoted 87 lines · Greg KH
Quoted 85 lines · Zhang Rui
Quoted 84 lines · Varad Gautam
Quoted 81 lines · Greg KH
Quoted 64 lines · Varad Gautam
Quoted 39 lines · Greg KH
Quoted 37 lines · Varad Gautam

Check that a user-provided thermal state is within the maximum
thermal states supported by a given driver before attempting to
apply it. This prevents a subsequent OOB access in thermal_cooling_device_stats_update() while performing state-transition accounting on drivers that do not have this check
in their set_cur_state() handle.

---
 drivers/thermal/thermal_sysfs.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/thermal/thermal_sysfs.c +1 -1
index 1c4aac8464a7..0c6b0223b133 100644
@@ -607,7 +607,7 @@ cur_state_store(struct device *dev,
 struct device_attribute *attr,
              const char *buf, size_t count)
 {
      struct thermal_cooling_device *cdev =
 to_cooling_device(dev);
-     unsigned long state;
+     unsigned long state, max_state;
      int result;
      if (sscanf(buf, "%ld\n", &state) != 1)
drivers/thermal/thermal_sysfs.c +0 -0
@@ -618,10 +618,20 @@ cur_state_store(struct device *dev,
 struct device_attribute *attr,
 
      mutex_lock(&cdev->lock);
 
Quoted 16 lines

drivers/thermal/thermal_sysfs.c cur_state_store(struct device *dev, · line 631

Quoted 10 lines
drivers/thermal/thermal_sysfs.c +9 -0
+     result = cdev->ops->get_max_state(cdev, &max_state);
+     if (result)
+             goto unlock;
+
+     if (state > max_state) {
+             result = -EINVAL;
+             goto unlock;
+     }
+
      result = cdev->ops->set_cur_state(cdev, state);

Why doesn't set_cur_state() check the max state before setting it? Why
are the callers forced to always check it before? That feels wrong...

The problem lies in thermal_cooling_device_stats_update(), not set_cur_state().

If ->set_cur_state() doesn't error out on invalid state, thermal_cooling_device_stats_update() does a:

Quoted 8 lines

stats->trans_table[stats->state * stats->max_states + new_state]++;

stats->trans_table reserves space depending on max_states, but we'd end up
reading/writing outside it. cur_state_store() can prevent this regardless of
the driver's ->set_cur_state() implementation.

Why wouldn't cur_state_store() check for an out-of-bounds condition by
calling get_max_state() and then return an error if it is invalid, preventing thermal_cooling_device_stats_update() from ever being called?

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

That's what this patch does, it adds the out-of-bounds check.

No, I think Greg' question is
why cdev->ops->set_cur_state() return 0 when setting a cooling state that exceeds the maximum cooling state?

Yes, that is what I am asking, it should not allow a state to be exceeded.

Indeed, it is upto the driver to return !0 from cdev->ops->set_cur_state() when setting state > max - and it is a driver bug for not doing so.

Quoted 7 lines
Quoted 2 lines

But a buggy driver should not lead to cur_state_store() performing an OOB access.

Agreed, which is why the code that does the access should check before it does so. Right now you are relying on the sysfs code to do so, which seems very wrong.

I see the point.

The OOB access happens in thermal_cooling_device_stats_update().

By placing the check in cur_state_store(), I'm trying to ensure two things for a buggy driver:
1. The driver's cdev->ops->set_cur_state() doesn't get called if the new state is > max state. This is to prevent the driver from storing the new (invalid) state internally. If the driver didn't realise/reject an invalid state, chances are it will try to propagate it internally and take actions according to that, which can have side effects on system stability.
2. The kernel doesn't do an OOB access in thermal_cooling_device_stats_update().

Quoted 3 lines

thanks,

greg k-h

maintainer
#10 lore raw
Quoted 112 lines · Varad Gautam
Quoted 103 lines · Greg KH
Quoted 101 lines · Varad Gautam
Quoted 87 lines · Greg KH
Quoted 85 lines · Zhang Rui
Quoted 84 lines · Varad Gautam
Quoted 81 lines · Greg KH
Quoted 64 lines · Varad Gautam
Quoted 39 lines · Greg KH
Quoted 37 lines · Varad Gautam

Check that a user-provided thermal state is within the maximum
thermal states supported by a given driver before attempting to
apply it. This prevents a subsequent OOB access in thermal_cooling_device_stats_update() while performing state-transition accounting on drivers that do not have this check
in their set_cur_state() handle.

---
 drivers/thermal/thermal_sysfs.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/thermal/thermal_sysfs.c +1 -1
index 1c4aac8464a7..0c6b0223b133 100644
@@ -607,7 +607,7 @@ cur_state_store(struct device *dev,
 struct device_attribute *attr,
              const char *buf, size_t count)
 {
      struct thermal_cooling_device *cdev =
 to_cooling_device(dev);
-     unsigned long state;
+     unsigned long state, max_state;
      int result;
      if (sscanf(buf, "%ld\n", &state) != 1)
drivers/thermal/thermal_sysfs.c +0 -0
@@ -618,10 +618,20 @@ cur_state_store(struct device *dev,
 struct device_attribute *attr,
 
      mutex_lock(&cdev->lock);
 
Quoted 16 lines

drivers/thermal/thermal_sysfs.c cur_state_store(struct device *dev, · line 631

Quoted 10 lines
drivers/thermal/thermal_sysfs.c +9 -0
+     result = cdev->ops->get_max_state(cdev, &max_state);
+     if (result)
+             goto unlock;
+
+     if (state > max_state) {
+             result = -EINVAL;
+             goto unlock;
+     }
+
      result = cdev->ops->set_cur_state(cdev, state);

Why doesn't set_cur_state() check the max state before setting it? Why
are the callers forced to always check it before? That feels wrong...

The problem lies in thermal_cooling_device_stats_update(), not set_cur_state().

If ->set_cur_state() doesn't error out on invalid state, thermal_cooling_device_stats_update() does a:

Quoted 8 lines

stats->trans_table[stats->state * stats->max_states + new_state]++;

stats->trans_table reserves space depending on max_states, but we'd end up
reading/writing outside it. cur_state_store() can prevent this regardless of
the driver's ->set_cur_state() implementation.

Why wouldn't cur_state_store() check for an out-of-bounds condition by
calling get_max_state() and then return an error if it is invalid, preventing thermal_cooling_device_stats_update() from ever being called?

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

That's what this patch does, it adds the out-of-bounds check.

No, I think Greg' question is
why cdev->ops->set_cur_state() return 0 when setting a cooling state that exceeds the maximum cooling state?

Yes, that is what I am asking, it should not allow a state to be exceeded.

Indeed, it is upto the driver to return !0 from cdev->ops->set_cur_state() when setting state > max - and it is a driver bug for not doing so.

Quoted 7 lines
Quoted 2 lines

But a buggy driver should not lead to cur_state_store() performing an OOB access.

Agreed, which is why the code that does the access should check before it does so. Right now you are relying on the sysfs code to do so, which seems very wrong.

Quoted 6 lines

I see the point.

The OOB access happens in thermal_cooling_device_stats_update().

By placing the check in cur_state_store(), I'm trying to ensure two things for a buggy driver:

What in-kernel driver has this problem, and why not just fix it there?

Quoted 6 lines

1. The driver's cdev->ops->set_cur_state() doesn't get called if the new state is > max state. This is to prevent the driver from storing the new (invalid) state internally. If the driver didn't realise/reject an invalid state, chances are it will try to propagate it internally and take actions according to that, which can have side effects on system stability.

Again, set_cur_state() should check for max values, if not, it is broken and that needs to be fixed in the driver.

Quoted 2 lines

2. The kernel doesn't do an OOB access in
thermal_cooling_device_stats_update().

Then don't allow thermal_cooling_device_stats_update() to do an out of band access by fixing it there too. But again, your patch does not solve that directly.

thanks,

greg k-h