diff mbox series

[v6,bpf-next,07/11] bpf: Add a common helper bpf_copy_to_user()

Message ID 20230628115329.248450-8-laoar.shao@gmail.com (mailing list archive)
State Superseded
Headers show
Series bpf: Support ->fill_link_info for kprobe_multi and perf_event links | expand

Commit Message

Yafang Shao June 28, 2023, 11:53 a.m. UTC
Add a common helper bpf_copy_to_user(), which will be used at multiple
places.
No functional change.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 kernel/bpf/syscall.c | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)

Comments

Andrii Nakryiko July 6, 2023, 10 p.m. UTC | #1
On Wed, Jun 28, 2023 at 4:53 AM Yafang Shao <laoar.shao@gmail.com> wrote:
>
> Add a common helper bpf_copy_to_user(), which will be used at multiple
> places.
> No functional change.
>
> Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> ---
>  kernel/bpf/syscall.c | 34 ++++++++++++++++++++--------------
>  1 file changed, 20 insertions(+), 14 deletions(-)
>

Acked-by: Andrii Nakryiko <andrii@kernel.org>



> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index a2aef900519c..4aa6e5776a04 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -3295,6 +3295,25 @@ static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
>                    raw_tp_link->btp->tp->name);
>  }
>
> +static int bpf_copy_to_user(char __user *ubuf, const char *buf, u32 ulen,
> +                           u32 len)
> +{
> +       if (ulen >= len + 1) {
> +               if (copy_to_user(ubuf, buf, len + 1))
> +                       return -EFAULT;
> +       } else {
> +               char zero = '\0';
> +
> +               if (copy_to_user(ubuf, buf, ulen - 1))
> +                       return -EFAULT;
> +               if (put_user(zero, ubuf + ulen - 1))
> +                       return -EFAULT;
> +               return -ENOSPC;
> +       }
> +
> +       return 0;
> +}
> +
>  static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
>                                           struct bpf_link_info *info)
>  {
> @@ -3313,20 +3332,7 @@ static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
>         if (!ubuf)
>                 return 0;
>
> -       if (ulen >= tp_len + 1) {
> -               if (copy_to_user(ubuf, tp_name, tp_len + 1))
> -                       return -EFAULT;
> -       } else {
> -               char zero = '\0';
> -
> -               if (copy_to_user(ubuf, tp_name, ulen - 1))
> -                       return -EFAULT;
> -               if (put_user(zero, ubuf + ulen - 1))
> -                       return -EFAULT;
> -               return -ENOSPC;
> -       }
> -
> -       return 0;
> +       return bpf_copy_to_user(ubuf, tp_name, ulen, tp_len);
>  }
>
>  static const struct bpf_link_ops bpf_raw_tp_link_lops = {
> --
> 2.39.3
>
diff mbox series

Patch

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index a2aef900519c..4aa6e5776a04 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3295,6 +3295,25 @@  static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
 		   raw_tp_link->btp->tp->name);
 }
 
+static int bpf_copy_to_user(char __user *ubuf, const char *buf, u32 ulen,
+			    u32 len)
+{
+	if (ulen >= len + 1) {
+		if (copy_to_user(ubuf, buf, len + 1))
+			return -EFAULT;
+	} else {
+		char zero = '\0';
+
+		if (copy_to_user(ubuf, buf, ulen - 1))
+			return -EFAULT;
+		if (put_user(zero, ubuf + ulen - 1))
+			return -EFAULT;
+		return -ENOSPC;
+	}
+
+	return 0;
+}
+
 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
 					  struct bpf_link_info *info)
 {
@@ -3313,20 +3332,7 @@  static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
 	if (!ubuf)
 		return 0;
 
-	if (ulen >= tp_len + 1) {
-		if (copy_to_user(ubuf, tp_name, tp_len + 1))
-			return -EFAULT;
-	} else {
-		char zero = '\0';
-
-		if (copy_to_user(ubuf, tp_name, ulen - 1))
-			return -EFAULT;
-		if (put_user(zero, ubuf + ulen - 1))
-			return -EFAULT;
-		return -ENOSPC;
-	}
-
-	return 0;
+	return bpf_copy_to_user(ubuf, tp_name, ulen, tp_len);
 }
 
 static const struct bpf_link_ops bpf_raw_tp_link_lops = {