Skip to contentSkip to conversation

Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()

PATCH Superseded Fushuai Wang 2 participants 4 messages Jan 16

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()

From: Fushuai Wang <[email protected]>

Hi,

There are several places use copy_from_user() to copy a string from userspase, followed by kstrtoXXX() to convert it to a desired type. This approach requires manually handling NUL-termination and proper buffer sizing, which is error-prone and unnecessarily complex. The kernel provides kstrtoXXX_from_user() helpers to copy a string from userspace and perform the conversion in one step. So use a kstrtoXXX_from_user() instead of copy_from_user() + kstrtoXXX().

Recent discussion:
https://lore.kernel.org/all/[email protected]/T/

Fushuai Wang (2):
sched/debug: Convert copy_from_user() + kstrtouint() to
kstrtouint_from_user()
x86/tlb: Convert copy_from_user() + kstrtouint() to
kstrtouint_from_user()

 arch/x86/mm/tlb.c    | 13 ++++---------
 kernel/sched/debug.c | 14 ++++----------
 2 files changed, 8 insertions(+), 19 deletions(-)
Signature · 1 line
2.36.1
#1 lore raw

PATCH 1/2 sched/debug: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()

From: Fushuai Wang <[email protected]>

Using kstrtouint_from_user() instead of copy_from_user() + kstrtouint() makes the code simpler and less error-prone.

---
 kernel/sched/debug.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
kernel/sched/debug.c +4 -10
index 41caa22e0680..1091f9046260 100644
@@ -172,18 +172,12 @@ static const struct file_operations sched_feat_fops = {
 static ssize_t sched_scaling_write(struct file *filp, const char __user *ubuf,
 				   size_t cnt, loff_t *ppos)
 {
-	char buf[16];
 	unsigned int scaling;
+	int ret;
 
-	if (cnt > 15)
-		cnt = 15;
-
-	if (copy_from_user(&buf, ubuf, cnt))
-		return -EFAULT;
-	buf[cnt] = '\0';
-
-	if (kstrtouint(buf, 10, &scaling))
-		return -EINVAL;
+	ret = kstrtouint_from_user(ubuf, cnt, 10, &scaling);
+	if (ret < 0)
+		return ret;
 
 	if (scaling >= SCHED_TUNABLESCALING_END)
 		return -EINVAL;
Signature · 1 line
2.36.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] sched/debug: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()

Quoted 24 lines · Fushuai Wang

From: Fushuai Wang <[email protected]>

Using kstrtouint_from_user() instead of copy_from_user() + kstrtouint() makes the code simpler and less error-prone.

---
 kernel/sched/debug.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
kernel/sched/debug.c +1 -3
index 41caa22e0680..1091f9046260 100644
@@ -172,18 +172,12 @@ static const struct file_operations sched_feat_fops = {
 static ssize_t sched_scaling_write(struct file *filp, const char __user *ubuf,
 				   size_t cnt, loff_t *ppos)
 {
-	char buf[16];
 	unsigned int scaling;
+	int ret;
 
-	if (cnt > 15)
-		cnt = 15;

kernel/sched/debug.c static const struct file_operations sched_feat_fops = { · line 180

Quoted 10 lines
kernel/sched/debug.c +3 -7
-
-	if (copy_from_user(&buf, ubuf, cnt))
-		return -EFAULT;
-	buf[cnt] = '\0';
-
-	if (kstrtouint(buf, 10, &scaling))
-		return -EINVAL;
+	ret = kstrtouint_from_user(ubuf, cnt, 10, &scaling);
+	if (ret < 0)
+		return ret;

This thing returns 0 on success, so I think you could better do:

        if (ret)
                return ret;

With that, for the series:

Thanks,
Yury

Quoted 6 lines
kernel/sched/debug.c +0 -0
 	if (scaling >= SCHED_TUNABLESCALING_END)
 		return -EINVAL;
Signature · 1 line
2.36.1
#3 lore raw

PATCH 2/2 x86/tlb: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()

In reply to Fushuai Wang

From: Fushuai Wang <[email protected]>

Using kstrtouint_from_user() instead of copy_from_user() + kstrtouint() makes the code simpler and less error-prone.

---
 arch/x86/mm/tlb.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
arch/x86/mm/tlb.c +4 -9
index f5b93e01e347..5e6a07306214 100644
@@ -1802,17 +1802,12 @@ static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf,
 static ssize_t tlbflush_write_file(struct file *file,
 		 const char __user *user_buf, size_t count, loff_t *ppos)
 {
-	char buf[32];
-	ssize_t len;
 	int ceiling;
+	int err;
 
-	len = min(count, sizeof(buf) - 1);
-	if (copy_from_user(buf, user_buf, len))
-		return -EFAULT;
-
-	buf[len] = '\0';
-	if (kstrtoint(buf, 0, &ceiling))
-		return -EINVAL;
+	err = kstrtoint_from_user(user_buf, count, 0, &ceiling);
+	if (err)
+		return err;
 
 	if (ceiling < 0)
 		return -EINVAL;
Signature · 1 line
2.36.1