Skip to contentSkip to conversation

bpf: Clean code with bpf_copy_to_user

PATCH Superseded Tao Chen 2 participants 5 messages Jul 3, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tao Chen

author
#0 lore raw

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
index e6eea594f1c..ca152d36312 100644
@@ -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
reviewer
#1 lore raw
Quoted 25 lines · Tao Chen

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 +0 -9
index e6eea594f1c..ca152d36312 100644
@@ -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;
author
#2 lore raw
Quoted 43 lines · Yonghong Song
Quoted 25 lines · Tao Chen

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 +0 -9
index e6eea594f1c..ca152d36312 100644
@@ -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 · 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.

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
reviewer
#3 lore raw
Quoted 50 lines · Tao Chen
Quoted 49 lines · Yonghong Song
Quoted 25 lines · Tao Chen

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 +0 -9
index e6eea594f1c..ca152d36312 100644
@@ -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 · 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;

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 these put_user code to the front.

Maybe do the following?

   err = bpf_copy_to_user(ubuf, buf, input_len, len);
   if (err && err != -ENOSPC)
     return err;
author
#4 lore raw
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_user to clean code.

---
  kernel/bpf/syscall.c | 17 +++--------------

  1 file changed, 3 insertions(+), 14 deletions(-)
kernel/bpf/syscall.c +0 -9
index e6eea594f1c..ca152d36312 100644
@@ -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 · 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-

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 these put_user code 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