]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
bpf: Clean code with bpf_copy_to_user()
authorTao Chen <chen.dylane@linux.dev>
Thu, 3 Jul 2025 16:37:00 +0000 (00:37 +0800)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 7 Jul 2025 15:53:59 +0000 (08:53 -0700)
No logic change, use bpf_copy_to_user() to clean code.

Signed-off-by: Tao Chen <chen.dylane@linux.dev>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/r/20250703163700.677628-1-chen.dylane@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/syscall.c

index 7db7182a3057739eca6bf245e6f547414ac1e175..3f36bfe132660886d53c964628c6b65bd8b33016 100644 (file)
@@ -5344,21 +5344,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 == -EFAULT)
+                               return err;
                }
        }