diff mbox series

[v6,bpf-next,08/11] bpf: Add bpf_perf_link_fill_common()

Message ID 20230628115329.248450-9-laoar.shao@gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpf: Support ->fill_link_info for kprobe_multi and perf_event links | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 10 this patch: 11
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/build_clang fail Errors and warnings before: 8 this patch: 10
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn fail Errors and warnings before: 10 this patch: 11
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 40 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-6 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on s390x with gcc

Commit Message

Yafang Shao June 28, 2023, 11:53 a.m. UTC
Add a new helper bpf_perf_link_fill_common(), which will be used by
perf_link based tracepoint, kprobe and uprobe.

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

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 new helper bpf_perf_link_fill_common(), which will be used by
> perf_link based tracepoint, kprobe and uprobe.
>
> Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> ---
>  kernel/bpf/syscall.c | 34 ++++++++++++++++++++++++++++++++++
>  1 file changed, 34 insertions(+)
>
> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 4aa6e5776a04..72de91beabbc 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -3364,6 +3364,40 @@ static void bpf_perf_link_dealloc(struct bpf_link *link)
>         kfree(perf_link);
>  }
>
> +static int bpf_perf_link_fill_common(const struct perf_event *event,
> +                                    char __user *uname, u32 ulen,
> +                                    u64 *probe_offset, u64 *probe_addr,
> +                                    u32 *fd_type)
> +{
> +       const char *buf;
> +       u32 prog_id;
> +       size_t len;
> +       int err;
> +
> +       if (!ulen ^ !uname)
> +               return -EINVAL;
> +       if (!uname)
> +               return 0;
> +
> +       err = bpf_get_perf_event_info(event, &prog_id, fd_type, &buf,
> +                                     probe_offset, probe_addr);
> +       if (err)
> +               return err;
> +
> +       len = strlen(buf);
> +       if (buf) {

if buf is NULL, strlen above will crash, so you need to calculate len
inside this if branch


> +               err = bpf_copy_to_user(uname, buf, ulen, len);
> +               if (err)
> +                       return err;
> +       } else {
> +               char zero = '\0';
> +
> +               if (put_user(zero, uname))
> +                       return -EFAULT;
> +       }
> +       return 0;
> +}
> +
>  static const struct bpf_link_ops bpf_perf_link_lops = {
>         .release = bpf_perf_link_release,
>         .dealloc = bpf_perf_link_dealloc,
> --
> 2.39.3
>
Yafang Shao July 7, 2023, 1:49 a.m. UTC | #2
On Fri, Jul 7, 2023 at 6:00 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Wed, Jun 28, 2023 at 4:53 AM Yafang Shao <laoar.shao@gmail.com> wrote:
> >
> > Add a new helper bpf_perf_link_fill_common(), which will be used by
> > perf_link based tracepoint, kprobe and uprobe.
> >
> > Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> > ---
> >  kernel/bpf/syscall.c | 34 ++++++++++++++++++++++++++++++++++
> >  1 file changed, 34 insertions(+)
> >
> > diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> > index 4aa6e5776a04..72de91beabbc 100644
> > --- a/kernel/bpf/syscall.c
> > +++ b/kernel/bpf/syscall.c
> > @@ -3364,6 +3364,40 @@ static void bpf_perf_link_dealloc(struct bpf_link *link)
> >         kfree(perf_link);
> >  }
> >
> > +static int bpf_perf_link_fill_common(const struct perf_event *event,
> > +                                    char __user *uname, u32 ulen,
> > +                                    u64 *probe_offset, u64 *probe_addr,
> > +                                    u32 *fd_type)
> > +{
> > +       const char *buf;
> > +       u32 prog_id;
> > +       size_t len;
> > +       int err;
> > +
> > +       if (!ulen ^ !uname)
> > +               return -EINVAL;
> > +       if (!uname)
> > +               return 0;
> > +
> > +       err = bpf_get_perf_event_info(event, &prog_id, fd_type, &buf,
> > +                                     probe_offset, probe_addr);
> > +       if (err)
> > +               return err;
> > +
> > +       len = strlen(buf);
> > +       if (buf) {
>
> if buf is NULL, strlen above will crash, so you need to calculate len
> inside this if branch

Nice catch. Will fix it.
diff mbox series

Patch

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 4aa6e5776a04..72de91beabbc 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3364,6 +3364,40 @@  static void bpf_perf_link_dealloc(struct bpf_link *link)
 	kfree(perf_link);
 }
 
+static int bpf_perf_link_fill_common(const struct perf_event *event,
+				     char __user *uname, u32 ulen,
+				     u64 *probe_offset, u64 *probe_addr,
+				     u32 *fd_type)
+{
+	const char *buf;
+	u32 prog_id;
+	size_t len;
+	int err;
+
+	if (!ulen ^ !uname)
+		return -EINVAL;
+	if (!uname)
+		return 0;
+
+	err = bpf_get_perf_event_info(event, &prog_id, fd_type, &buf,
+				      probe_offset, probe_addr);
+	if (err)
+		return err;
+
+	len = strlen(buf);
+	if (buf) {
+		err = bpf_copy_to_user(uname, buf, ulen, len);
+		if (err)
+			return err;
+	} else {
+		char zero = '\0';
+
+		if (put_user(zero, uname))
+			return -EFAULT;
+	}
+	return 0;
+}
+
 static const struct bpf_link_ops bpf_perf_link_lops = {
 	.release = bpf_perf_link_release,
 	.dealloc = bpf_perf_link_dealloc,