bpf: Clean code with bpf_copy_to_user
PATCH 1/1 bpf: Clean code with bpf_copy_to_user
No logic change, just use bpf_copy_to_user to clean code.
--- kernel/bpf/syscall.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-)
kernel/bpf/syscall.c +3 -14
@@ -5208,21 +5208,10 @@ static int bpf_task_fd_query_copy(const union bpf_attr *attr,if (put_user (zero, ubuf))return -EFAULT ; - }else if (input_len >= len +1 ) { -/* ubuf can hold the string with NULL terminator */ -if (copy_to_user (ubuf, buf, len +1 )) -return -EFAULT ; }else { -/* ubuf cannot hold the string with NULL terminator, -* do a partial copy with NULL terminator. -*/ -char zero ='\0' ; - - err = -ENOSPC ; -if (copy_to_user (ubuf, buf, input_len -1 )) -return -EFAULT ; -if (put_user (zero, ubuf + input_len -1 )) -return -EFAULT ; + err =bpf_copy_to_user (ubuf, buf, input_len, len); +if (err) +return err; } }
Signature · 1 line
2.48.1
Yonghong Song
reviewerQuoted 25 lines · Tao Chen
No logic change, just use
bpf_copy_to_userto clean code.Signed-off-by Tao Chen <[email protected]>--- kernel/bpf/syscall.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-)kernel/bpf/syscall.c +0
-9@@ -5208,21 +5208,10 @@ static int bpf_task_fd_query_copy(const union bpf_attr *attr,if (put_user (zero, ubuf))return -EFAULT ; - }else if (input_len >= len +1 ) { -/* ubuf can hold the string with NULL terminator */ -if (copy_to_user (ubuf, buf, len +1 )) -return -EFAULT ; }else { -/* ubuf cannot hold the string with NULL terminator, -* do a partial copy with NULL terminator. -*/ -char zero ='\0' ; -
kernel/bpf/syscall.c static int bpf_task_fd_query_copy(const union bpf_attr *attr, · line 5216
Quoted 10 lines
kernel/bpf/syscall.c +3
-5- err = -ENOSPC ; -if (copy_to_user (ubuf, buf, input_len -1 )) -return -EFAULT ; -if (put_user (zero, ubuf + input_len -1 )) -return -EFAULT ; + err =bpf_copy_to_user (ubuf, buf, input_len, len); +if (err) +return err; } }
Actually, there is a return value change with this patch. bpf_copy_to_user() return returns -ENOSPC while the original implementation may return -EFAULT due to following code.
if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
put_user(fd_type, &uattr->task_fd_query.fd_type) ||
put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
put_user(probe_addr, &uattr->task_fd_query.probe_addr))
return -EFAULT;
return err;Quoted 43 lines · Yonghong Song
Quoted 25 lines · Tao Chen
No logic change, just use
bpf_copy_to_userto clean code.Signed-off-by Tao Chen <[email protected]>---
kernel/bpf/syscall.c| 17 +++--------------1 file changed, 3 insertions(+), 14 deletions(-)kernel/bpf/syscall.c +0
-9@@ -5208,21 +5208,10 @@ static int bpf_task_fd_query_copy(const unionbpf_attr *attr, if (put_user(zero, ubuf)) return -EFAULT; - } else if (input_len >= len + 1) { - /* ubuf can hold the string with NULL terminator */ - if (copy_to_user(ubuf, buf, len + 1)) - return -EFAULT; } else { - /* ubuf cannot hold the string with NULL terminator, - * do a partial copy with NULL terminator. - */ - char zero = '\0'; -kernel/bpf/syscall.c static int bpf_task_fd_query_copy(const union · line 5216
Quoted 10 lines
kernel/bpf/syscall.c +3
-5- err = -ENOSPC; - if (copy_to_user(ubuf, buf, input_len - 1)) - return -EFAULT; - if (put_user(zero, ubuf + input_len - 1)) - return -EFAULT; + err = bpf_copy_to_user(ubuf, buf, input_len, len); + if (err) + return err; } }Actually, there is a return value change with this patch.
bpf_copy_to_user()return returns-ENOSPCwhile the original implementation may return-EFAULTdue to following code.
Quoted 8 lines
if (
put_user(prog_id, &uattr->task_fd_query.prog_id)||put_user(fd_type, &uattr->task_fd_query.fd_type)||put_user(probe_offset, &uattr->task_fd_query.probe_offset)||put_user(probe_addr, &uattr->task_fd_query.probe_addr)) return-EFAULT;return err;
You are right, maybe we can just use:
err = bpf_copy_to_user(ubuf, buf, input_len, len);
and no return check
or move these put_user code to the front.
Signature · 2 lines
Best Regards Tao Chen
Yonghong Song
reviewerQuoted 50 lines · Tao Chen
Quoted 49 lines · Yonghong Song
Quoted 25 lines · Tao Chen
No logic change, just use
bpf_copy_to_userto clean code.Signed-off-by Tao Chen <[email protected]>---
kernel/bpf/syscall.c| 17 +++--------------1 file changed, 3 insertions(+), 14 deletions(-)kernel/bpf/syscall.c +0
-9@@ -5208,21 +5208,10 @@ static int bpf_task_fd_query_copy(constunion bpf_attr *attr, if (put_user(zero, ubuf)) return -EFAULT; - } else if (input_len >= len + 1) { - /* ubuf can hold the string with NULL terminator */ - if (copy_to_user(ubuf, buf, len + 1)) - return -EFAULT; } else { - /* ubuf cannot hold the string with NULL terminator, - * do a partial copy with NULL terminator. - */ - char zero = '\0'; -kernel/bpf/syscall.c static int bpf_task_fd_query_copy(const · line 5216
Quoted 10 lines
kernel/bpf/syscall.c +3
-5- err = -ENOSPC; - if (copy_to_user(ubuf, buf, input_len - 1)) - return -EFAULT; - if (put_user(zero, ubuf + input_len - 1)) - return -EFAULT; + err = bpf_copy_to_user(ubuf, buf, input_len, len); + if (err) + return err; } }Actually, there is a return value change with this patch.
bpf_copy_to_user()return returns-ENOSPCwhile the original implementation may return-EFAULTdue to following code.if (
put_user(prog_id, &uattr->task_fd_query.prog_id)||put_user(fd_type, &uattr->task_fd_query.fd_type)||put_user(probe_offset, &uattr->task_fd_query.probe_offset)||put_user(probe_addr, &uattr->task_fd_query.probe_addr)) return-EFAULT;
Quoted 7 lines
Quoted 2 lines
return err;
You are right, maybe we can just use:
err =bpf_copy_to_user(ubuf, buf, input_len, len); and no return check
or move theseput_usercode to the front.
Maybe do the following?
err = bpf_copy_to_user(ubuf, buf, input_len, len);
if (err && err != -ENOSPC)
return err;Quoted 62 lines · Yonghong Song
Quoted 51 lines · Tao Chen
Quoted 50 lines · Yonghong Song
Quoted 25 lines · Tao Chen
No logic change, just use
bpf_copy_to_userto clean code.Signed-off-by Tao Chen <[email protected]>---
kernel/bpf/syscall.c| 17 +++--------------1 file changed, 3 insertions(+), 14 deletions(-)kernel/bpf/syscall.c +0
-9@@ -5208,21 +5208,10 @@ static int bpf_task_fd_query_copy(constunion bpf_attr *attr, if (put_user(zero, ubuf)) return -EFAULT; - } else if (input_len >= len + 1) { - /* ubuf can hold the string with NULL terminator */ - if (copy_to_user(ubuf, buf, len + 1)) - return -EFAULT; } else { - /* ubuf cannot hold the string with NULL terminator, - * do a partial copy with NULL terminator. - */ - char zero = '\0'; -kernel/bpf/syscall.c static int bpf_task_fd_query_copy(const · line 5216
Quoted 10 lines
kernel/bpf/syscall.c +3
-5- err = -ENOSPC; - if (copy_to_user(ubuf, buf, input_len - 1)) - return -EFAULT; - if (put_user(zero, ubuf + input_len - 1)) - return -EFAULT; + err = bpf_copy_to_user(ubuf, buf, input_len, len); + if (err) + return err; } }Actually, there is a return value change with this patch.
bpf_copy_to_user()return returns-ENOSPCwhile the original implementation may return-EFAULTdue to following code.if (
put_user(prog_id, &uattr->task_fd_query.prog_id)||put_user(fd_type, &uattr->task_fd_query.fd_type)|| put_user(probe_offset, &uattr-Quoted 1 line
task_fd_query.probe_offset) ||
put_user(probe_addr, &uattr->task_fd_query.probe_addr)) return-EFAULT;Quoted 7 lines
Quoted 2 lines
return err;
You are right, maybe we can just use:
err =bpf_copy_to_user(ubuf, buf, input_len, len); and no return check
or move theseput_usercode to the front.
Quoted 6 lines
Maybe do the following?
err =
bpf_copy_to_user(ubuf, buf, input_len, len); if (err && err !=-ENOSPC)
return err;
Well, it looks better, i will change it in v2, thanks.
Signature · 2 lines
Best Regards Tao Chen