Skip to contentSkip to conversation

cpufreq: add NULL check to the store method of cpufreq

PATCH In review Shen Kai 4 participants 10 messages Nov 6, 2019 – Nov 8, 2019 CPU frequency scaling framework

lore
Share & tools
lore

Shen Kai

author
#0 lore raw

PATCH 1/1 cpufreq: add NULL check to the store method of cpufreq

From: shenkai <[email protected]>

Add NULL check in the store function here to avoid NULL callback invoking. Though some interfaces of cpufreq are set as read-only, user can still get write permission using chmod which can lead to a kernel crash.

The following operations can lead to a kernel crash.

chmod +w /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq echo 1 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

This bug was found on linux 4.19

---
 drivers/cpufreq/cpufreq.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/cpufreq/cpufreq.c +3 -0
index bffc11b..4ccaa96 100644
@@ -947,6 +947,9 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr,
 	struct freq_attr *fattr = to_attr(attr);
 	ssize_t ret = -EINVAL;
 
+	if (!fattr->store)
+		return ret;
+
 	/*
 	 * cpus_read_trylock() is used here to work around a circular lock
 	 * dependency problem with respect to the cpufreq_register_driver().
Signature · 1 line
2.6.4.windows.1
#1 lore raw
Quoted 2 lines · Shen Kai

From: shenkai <[email protected]>

Can you please use the proper spelling of your name here?

Quoted 5 lines

Add NULL check in the store function here to avoid NULL callback invoking. Though some interfaces of cpufreq are set as read-only, user can still get write permission using chmod which can lead to a kernel crash.

Quoted 8 lines

The following operations can lead to a kernel crash.

chmod +w /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq echo 1 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

This bug was found on linux 4.19

And here?

Quoted 2 lines

And the proper spelling of the reporter/reviewer name here too?

Quoted 23 lines
---
 drivers/cpufreq/cpufreq.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/cpufreq/cpufreq.c +3 -1
index bffc11b..4ccaa96 100644
@@ -947,6 +947,9 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr,
        struct freq_attr *fattr = to_attr(attr);
        ssize_t ret = -EINVAL;
 
+       if (!fattr->store)
+               return ret;
+
        /*
         * cpus_read_trylock() is used here to work around a circular lock
         * dependency problem with respect to the cpufreq_register_driver().
--

2.6.4.windows.1

author
#2 lore raw

PATCH 1/1 cpufreq: add NULL check to the store method of cpufreq

From: Kai Shen <[email protected]>

Add NULL check in the store function here to avoid NULL callback invoking. Though some interfaces of cpufreq are set as read-only, user can still get write permission using chmod which can lead to a kernel crash.

The following operations can lead to a kernel crash.

chmod +w /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq echo 1 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

This bug was found on linux 4.19

---
 drivers/cpufreq/cpufreq.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/cpufreq/cpufreq.c +3 -0
index bffc11b..4ccaa96 100644
@@ -947,6 +947,9 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr,
 	struct freq_attr *fattr = to_attr(attr);
 	ssize_t ret = -EINVAL;
 
+	if (!fattr->store)
+		return ret;
+
 	/*
 	 * cpus_read_trylock() is used here to work around a circular lock
 	 * dependency problem with respect to the cpufreq_register_driver().
Signature · 1 line
2.6.4.windows.1
maintainer
#3 lore raw
Quoted 26 lines · Shen Kai

From: Kai Shen <[email protected]>

Add NULL check in the store function here to avoid NULL callback invoking. Though some interfaces of cpufreq are set as read-only, user can still get write permission using chmod which can lead to a kernel crash.

The following operations can lead to a kernel crash.

chmod +w /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq echo 1 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

This bug was found on linux 4.19

---
 drivers/cpufreq/cpufreq.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/cpufreq/cpufreq.c +0 -0
index bffc11b..4ccaa96 100644

drivers/cpufreq/cpufreq.c static ssize_t store(struct kobject *kobj, struct attribute *attr, · line 951

Quoted 5 lines
drivers/cpufreq/cpufreq.c +2 -0
@@ -947,6 +947,9 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr,
 	struct freq_attr *fattr = to_attr(attr);
 	ssize_t ret = -EINVAL;
 
+	if (!fattr->store)
+		return ret;

The return error shouldn't be -EINVAL here, maybe ENOPERM or ENOENT ?

Quoted 7 lines
drivers/cpufreq/cpufreq.c +1 -0
+
 	/*
 	 * cpus_read_trylock() is used here to work around a circular lock
 	 * dependency problem with respect to the cpufreq_register_driver().
Signature · 1 line
2.6.4.windows.1
Signature · 1 line
viresh
maintainer
#4 lore raw
Quoted 30 lines · Viresh Kumar
Quoted 29 lines · Shen Kai

From: Kai Shen <[email protected]>

Add NULL check in the store function here to avoid NULL callback invoking. Though some interfaces of cpufreq are set as read-only, user can still get write permission using chmod which can lead to a kernel crash.

The following operations can lead to a kernel crash.

chmod +w /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq echo 1 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

This bug was found on linux 4.19

---
 drivers/cpufreq/cpufreq.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/cpufreq/cpufreq.c +0 -0
index bffc11b..4ccaa96 100644
@@ -947,6 +947,9 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr,
 	struct freq_attr *fattr = to_attr(attr);
 	ssize_t ret = -EINVAL;
 
Quoted 4 lines

drivers/cpufreq/cpufreq.c static ssize_t store(struct kobject *kobj, struct attribute *attr, · line 951

Quoted 2 lines
drivers/cpufreq/cpufreq.c +2 -0
+	if (!fattr->store)
+		return ret;

The return error shouldn't be -EINVAL here, maybe ENOPERM or ENOENT ?

s/ENOPERM/EPERM/

Signature · 1 line
viresh
author
#5 lore raw

PATCH 1/1 cpufreq: add NULL check to the store method of cpufreq

From: Kai Shen <[email protected]>

Add NULL check in the store function here to avoid NULL callback invoking. Though some interfaces of cpufreq are set as read-only, user can still get write permission using chmod which can lead to a kernel crash.

The following operations can lead to a kernel crash.

chmod +w /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq echo 1 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

This bug was found on linux 4.19

---
 drivers/cpufreq/cpufreq.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/cpufreq/cpufreq.c +3 -0
index bffc11b..4ccaa96 100644
@@ -947,6 +947,9 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr,
 	struct freq_attr *fattr = to_attr(attr);
 	ssize_t ret = -EINVAL;
 
+	if (!fattr->store)
+		return -EPERM;
+
 	/*
 	 * cpus_read_trylock() is used here to work around a circular lock
 	 * dependency problem with respect to the cpufreq_register_driver().
Signature · 1 line
2.6.4.windows.1
maintainer
#6 lore raw
Quoted 26 lines · Shen Kai

From: Kai Shen <[email protected]>

Add NULL check in the store function here to avoid NULL callback invoking. Though some interfaces of cpufreq are set as read-only, user can still get write permission using chmod which can lead to a kernel crash.

The following operations can lead to a kernel crash.

chmod +w /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq echo 1 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

This bug was found on linux 4.19

---
 drivers/cpufreq/cpufreq.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/cpufreq/cpufreq.c +0 -0
index bffc11b..4ccaa96 100644

drivers/cpufreq/cpufreq.c static ssize_t store(struct kobject *kobj, struct attribute *attr, · line 952

Quoted 6 lines
drivers/cpufreq/cpufreq.c +3 -0
@@ -947,6 +947,9 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr,
 	struct freq_attr *fattr = to_attr(attr);
 	ssize_t ret = -EINVAL;
 
+	if (!fattr->store)
+		return -EPERM;
+

And this should be -EIO, I found that after looking at all the instances of struct kobj_type in the kernel :(

Also there is another change you need to make.

drivers/cpufreq/cpufreq.c +3 -0
index dd1628192310..4bfaafde9083 100644
@@ -933,6 +933,9 @@ static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
        struct freq_attr *fattr = to_attr(attr);
        ssize_t ret;
 
+       if (!fattr->show)
+               return -EIO;
+
        down_read(&policy->rwsem);
        ret = fattr->show(policy, buf);
        up_read(&policy->rwsem);

as there is a write-only attribute (reset) in cpufreq stats.

Signature · 1 line
viresh
author
#7 lore raw

PATCH v3 1/1 cpufreq: add NULL check to the store method of cpufreq

From: Kai Shen <[email protected]>

Add NULL check in the store function here to avoid NULL callback invoking. Though some interfaces of cpufreq are set as read-only, user can still get write permission using chmod which can lead to a kernel crash.

The following operations can lead to a kernel crash.

chmod +w /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq echo 1 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

This bug was found on linux 4.19

---
V2->V3
- use return value -EIO instead.
- do NULL check in show method too.

 drivers/cpufreq/cpufreq.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/cpufreq/cpufreq.c +6 -0
index 48a224a..bc19d6c 100644
@@ -933,6 +933,9 @@ static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
 	struct freq_attr *fattr = to_attr(attr);
 	ssize_t ret;
 
+	if (!fattr->show)
+		return -EIO;
+
 	down_read(&policy->rwsem);
 	ret = fattr->show(policy, buf);
 	up_read(&policy->rwsem);
@@ -947,6 +950,9 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr,
 	struct freq_attr *fattr = to_attr(attr);
 	ssize_t ret = -EINVAL;
 
+	if (!fattr->store)
+		return -EIO;
+
 	/*
 	 * cpus_read_trylock() is used here to work around a circular lock
 	 * dependency problem with respect to the cpufreq_register_driver().
Signature · 1 line
2.6.4.windows.1
maintainer Acked-by on v3 1/1
#8 lore raw
Quoted 40 lines · Shen Kai

From: Kai Shen <[email protected]>

Add NULL check in the store function here to avoid NULL callback invoking. Though some interfaces of cpufreq are set as read-only, user can still get write permission using chmod which can lead to a kernel crash.

The following operations can lead to a kernel crash.

chmod +w /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq echo 1 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

This bug was found on linux 4.19

---
V2->V3
- use return value -EIO instead.
- do NULL check in show method too.

 drivers/cpufreq/cpufreq.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/cpufreq/cpufreq.c +3 -0
index 48a224a..bc19d6c 100644
@@ -933,6 +933,9 @@ static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
 	struct freq_attr *fattr = to_attr(attr);
 	ssize_t ret;
 
+	if (!fattr->show)
+		return -EIO;
+
 	down_read(&policy->rwsem);
 	ret = fattr->show(policy, buf);
 	up_read(&policy->rwsem);

drivers/cpufreq/cpufreq.c static ssize_t store(struct kobject *kobj, struct attribute *attr, · line 958

Quoted 9 lines
drivers/cpufreq/cpufreq.c +3 -0
@@ -947,6 +950,9 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr,
 	struct freq_attr *fattr = to_attr(attr);
 	ssize_t ret = -EINVAL;
 
+	if (!fattr->store)
+		return -EIO;
+
 	/*
 	 * cpus_read_trylock() is used here to work around a circular lock
 	 * dependency problem with respect to the cpufreq_register_driver().
Signature · 1 line
viresh
#9 lore raw
In reply to Shen Kai
Quoted 41 lines · CET Shen Kai

From: Kai Shen <[email protected]>

Add NULL check in the store function here to avoid NULL callback invoking. Though some interfaces of cpufreq are set as read-only, user can still get write permission using chmod which can lead to a kernel crash.

The following operations can lead to a kernel crash.

chmod +w /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq echo 1 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

This bug was found on linux 4.19

---
V2->V3
- use return value -EIO instead.
- do NULL check in show method too.

 drivers/cpufreq/cpufreq.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/cpufreq/cpufreq.c +3 -0
index 48a224a..bc19d6c 100644
@@ -933,6 +933,9 @@ static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
 	struct freq_attr *fattr = to_attr(attr);
 	ssize_t ret;
 
+	if (!fattr->show)
+		return -EIO;
+
 	down_read(&policy->rwsem);
 	ret = fattr->show(policy, buf);
 	up_read(&policy->rwsem);

drivers/cpufreq/cpufreq.c static ssize_t store(struct kobject *kobj, struct attribute *attr, · line 958

Quoted 9 lines
drivers/cpufreq/cpufreq.c +3 -0
@@ -947,6 +950,9 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr,
 	struct freq_attr *fattr = to_attr(attr);
 	ssize_t ret = -EINVAL;
 
+	if (!fattr->store)
+		return -EIO;
+
 	/*
 	 * cpus_read_trylock() is used here to work around a circular lock
 	 * dependency problem with respect to the cpufreq_register_driver().

Applying as 5.5 material with updated subject and changelog (the original ones did not mention the show() function).

Thanks!