Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
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
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
@@ -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
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 ofcopy_from_user()+kstrtouint()makes the code simpler and less error-prone.Signed-off-by Fushuai Wang <[email protected]>--- kernel/sched/debug.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)kernel/sched/debug.c +1
-3@@ -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
-0if (scaling >=SCHED_TUNABLESCALING_END )return -EINVAL ;Signature · 1 line
2.36.1
PATCH 2/2 x86/tlb: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
In reply to Fushuai WangFrom: 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
@@ -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