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 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 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,