Skip to contentSkip to conversation

PM: QOS: Always use "kstrtos32_from_user()" in cpu_latency_qos_write()

PATCH In review Xuewen Yan 3 participants 3 messages Aug 7, 2023 – Aug 20, 2023

Subsystems (2)
lore
Share & tools
lore

Xuewen Yan

author
#0 lore raw

PATCH 1/1 PM: QOS: Always use "kstrtos32_from_user()" in cpu_latency_qos_write()

In cpu_latency_qos_write, there is a judgment on whether the count value is equal to sizeof(s32). This means that when user write 100~999, it would get error value because it would call the "copy_from_user()" instead of "kstrtos32".
Just like:

 # echo 500 > /dev/cpu_dma_latency
[T4893] write: qos value=170930229
[T4893] write: count value=4

[T4893] write: qos value=170930226
[T4893] write: count value=4

To prevent this happening, delete the "copy_from_user()" and always use "kstrtos32_from_user()".

---
 kernel/power/qos.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
kernel/power/qos.c +4 -10
index 782d3b41c1f3..21a2f873e921 100644
@@ -379,17 +379,11 @@ static ssize_t cpu_latency_qos_write(struct file *filp, const char __user *buf,
 				     size_t count, loff_t *f_pos)
 {
 	s32 value;
+	int ret;
 
-	if (count == sizeof(s32)) {
-		if (copy_from_user(&value, buf, sizeof(s32)))
-			return -EFAULT;
-	} else {
-		int ret;
-
-		ret = kstrtos32_from_user(buf, count, 16, &value);
-		if (ret)
-			return ret;
-	}
+	ret = kstrtos32_from_user(buf, count, 16, &value);
+	if (ret)
+		return ret;
 
 	cpu_latency_qos_update_request(filp->private_data, value);
 
Signature · 1 line
2.25.1
#1 lore raw

Hi rafael

What do you think of this patch?

 # echo 500 > /dev/cpu_dma_latency
[T4893] write: qos value=170930229
[T4893] write: count value=4

the value is 170930229 is because it's hex is 0x0A303035, It is exactly the "500\0"

 # echo 200> /dev/cpu_dma_latency
[T4893] write: qos value=170930226
[T4893] write: count value=4

the value is 170930226 is because it's hex is 0x0A303032, It is exactly the "200\0"

Thanks!

Quoted 51 lines · Xuewen Yan

In cpu_latency_qos_write, there is a judgment on whether the count value is equal to sizeof(s32). This means that when user write 100~999, it would get error value because it would call the "copy_from_user()" instead of "kstrtos32".
Just like:

 # echo 500 > /dev/cpu_dma_latency
[T4893] write: qos value=170930229
[T4893] write: count value=4

[T4893] write: qos value=170930226
[T4893] write: count value=4

To prevent this happening, delete the "copy_from_user()" and always use "kstrtos32_from_user()".

---
 kernel/power/qos.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
kernel/power/qos.c +4 -11
index 782d3b41c1f3..21a2f873e921 100644
@@ -379,17 +379,11 @@ static ssize_t cpu_latency_qos_write(struct file *filp, const char __user *buf,
                                     size_t count, loff_t *f_pos)
 {
        s32 value;
+       int ret;
 
-       if (count == sizeof(s32)) {
-               if (copy_from_user(&value, buf, sizeof(s32)))
-                       return -EFAULT;
-       } else {
-               int ret;
-
-               ret = kstrtos32_from_user(buf, count, 16, &value);
-               if (ret)
-                       return ret;
-       }
+       ret = kstrtos32_from_user(buf, count, 16, &value);
+       if (ret)
+               return ret;
 
        cpu_latency_qos_update_request(filp->private_data, value);
 
--

2.25.1

#2 lore raw
In reply to Xuewen Yan
Quoted 26 lines · Xuewen Yan

In cpu_latency_qos_write, there is a judgment on whether the count value is equal to sizeof(s32). This means that when user write 100~999, it would get error value because it would call the "copy_from_user()" instead of "kstrtos32".
Just like:

 # echo 500 > /dev/cpu_dma_latency
[T4893] write: qos value=170930229
[T4893] write: count value=4

[T4893] write: qos value=170930226
[T4893] write: count value=4

To prevent this happening, delete the "copy_from_user()" and always use "kstrtos32_from_user()".

---
 kernel/power/qos.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
kernel/power/qos.c +0 -0
index 782d3b41c1f3..21a2f873e921 100644

kernel/power/qos.c static ssize_t cpu_latency_qos_write(struct file *filp, const char __user *buf, · line 385

Quoted 8 lines
kernel/power/qos.c +1 -3
@@ -379,17 +379,11 @@ static ssize_t cpu_latency_qos_write(struct file *filp, const char __user *buf,
 				     size_t count, loff_t *f_pos)
 {
 	s32 value;
+	int ret;
 
-	if (count == sizeof(s32)) {
-		if (copy_from_user(&value, buf, sizeof(s32)))
-			return -EFAULT;

In your test you use echo which produces a string. But from C, someone can write a program where buf contains s32 and not a string, and this will break those programs. If this expectations is no longer valid, then it's fine to remove it.

Maybe we can change the order to call kstrtos32() first, but this is not bullet proof though..

Cheers

--
Qais Yousef

Quoted 16 lines
kernel/power/qos.c +3 -7
-	} else {
-		int ret;
-
-		ret = kstrtos32_from_user(buf, count, 16, &value);
-		if (ret)
-			return ret;
-	}
+	ret = kstrtos32_from_user(buf, count, 16, &value);
+	if (ret)
+		return ret;
 
 	cpu_latency_qos_update_request(filp->private_data, value);
 
Signature · 1 line
2.25.1