Skip to contentSkip to conversation

driver core: restrict buffer length in online_store()

PATCH In review Tiezhu Yang 2 participants 2 messages Jul 21, 2017 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 driver core: restrict buffer length in online_store()

According to Documentation/ABI/testing/sysfs-devices-online, in order to control CPU N's hotplug state, we should write one of 'Yy1Nn0' to the file /sys/devices/system/cpu/cpuN/online, other values should be invalid. so the buffer length should be 2, buf[0] is one of 'Yy1Nn0' and buf[1] is '\n'.

without patch:
[root@localhost home]# echo 0test > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 0
[root@localhost home]# echo 1test > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 1
[root@localhost home]# echo ntest > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 0
[root@localhost home]# echo ytest > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 1
[root@localhost home]# echo Ntest > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 0
[root@localhost home]# echo Ytest > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 1

with patch:
[root@localhost home]# echo 0test > /sys/devices/system/cpu/cpu1/online
bash: echo: write error: Invalid argument
[root@localhost home]# echo 1test > /sys/devices/system/cpu/cpu1/online
bash: echo: write error: Invalid argument
[root@localhost home]# echo ntest > /sys/devices/system/cpu/cpu1/online
bash: echo: write error: Invalid argument
[root@localhost home]# echo ytest > /sys/devices/system/cpu/cpu1/online
bash: echo: write error: Invalid argument
[root@localhost home]# echo Ntest > /sys/devices/system/cpu/cpu1/online
bash: echo: write error: Invalid argument
[root@localhost home]# echo Ytest > /sys/devices/system/cpu/cpu1/online
bash: echo: write error: Invalid argument

---
 drivers/base/core.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/base/core.c +6 -0
index 755451f..6588ed5 100644
@@ -1005,6 +1005,12 @@ static ssize_t online_store(struct device *dev, struct device_attribute *attr,
 	bool val;
 	int ret;
 
+	/* According to Documentation/ABI/testing/sysfs-devices-online,
+	 * the buf length should be 2, buf[0]: one of 'Yy1Nn0', buf[1]: '\n'.
+	 */
+	if (strlen(buf) != 2)
+		return -EINVAL;
+
 	ret = strtobool(buf, &val);
 	if (ret < 0)
 		return ret;
Signature · 1 line
1.8.3.1
maintainer
#1 lore raw
Quoted 49 lines · Tiezhu Yang

According to Documentation/ABI/testing/sysfs-devices-online, in order to control CPU N's hotplug state, we should write one of 'Yy1Nn0' to the file /sys/devices/system/cpu/cpuN/online, other values should be invalid. so the buffer length should be 2, buf[0] is one of 'Yy1Nn0' and buf[1] is '\n'.

without patch:
[root@localhost home]# echo 0test > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 0
[root@localhost home]# echo 1test > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 1
[root@localhost home]# echo ntest > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 0
[root@localhost home]# echo ytest > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 1
[root@localhost home]# echo Ntest > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 0
[root@localhost home]# echo Ytest > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 1

with patch:
[root@localhost home]# echo 0test > /sys/devices/system/cpu/cpu1/online
bash: echo: write error: Invalid argument
[root@localhost home]# echo 1test > /sys/devices/system/cpu/cpu1/online
bash: echo: write error: Invalid argument
[root@localhost home]# echo ntest > /sys/devices/system/cpu/cpu1/online
bash: echo: write error: Invalid argument
[root@localhost home]# echo ytest > /sys/devices/system/cpu/cpu1/online
bash: echo: write error: Invalid argument
[root@localhost home]# echo Ntest > /sys/devices/system/cpu/cpu1/online
bash: echo: write error: Invalid argument
[root@localhost home]# echo Ytest > /sys/devices/system/cpu/cpu1/online
bash: echo: write error: Invalid argument

---
 drivers/base/core.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/base/core.c +0 -0
index 755451f..6588ed5 100644

drivers/base/core.c static ssize_t online_store(struct device *dev, struct device_attribute *attr, · line 1014

Quoted 10 lines
drivers/base/core.c +6 -0
@@ -1005,6 +1005,12 @@ static ssize_t online_store(struct device *dev, struct device_attribute *attr,
 	bool val;
 	int ret;
 
+	/* According to Documentation/ABI/testing/sysfs-devices-online,
+	 * the buf length should be 2, buf[0]: one of 'Yy1Nn0', buf[1]: '\n'.
+	 */
+	if (strlen(buf) != 2)
+		return -EINVAL;
+
 	ret = strtobool(buf, &val);

strtobool should handle all of this, so let's not force every individual user of it to check the "length".

What is the problem that this patch is trying to solve? Who is writing odd values to this file that is not working properly? Who writes "0testfoo" to the file and expect it to reject the value?

thanks,

greg k-h