Skip to contentSkip to conversation

bpf: Check link_create parameter for multi_kprobe

PATCH Superseded Tao Chen 3 participants 9 messages Mar 31, 2025 – Apr 5, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tao Chen

author
#0 lore raw

PATCH 1/2 bpf: Check link_create parameter for multi_kprobe

The target_fd and flags in link_create no used in multi_kprobe , return -EINVAL if they assigned, keep it same as other link attach apis.

Fixes 0dcac2725406 ("bpf: Add multi kprobe link")
---
 kernel/trace/bpf_trace.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/trace/bpf_trace.c +3 -0
index 13bef2462..2f206a2a2 100644
@@ -2993,6 +2993,9 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
 	if (sizeof(u64) != sizeof(void *))
 		return -EOPNOTSUPP;
 
+	if (attr->link_create.target_fd || attr->link_create.flags)
+		return -EINVAL;
+
 	if (!is_kprobe_multi(prog))
 		return -EINVAL;
 
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 2/2 bpf: Check link_create parameter for multi_uprobe

The target_fd and flags in link_create no used in multi_uprobe , return -EINVAL if they assigned, keep it same as other link attach apis.

Fixes 89ae89f53d20 ("bpf: Add multi uprobe link")
---
 kernel/trace/bpf_trace.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/trace/bpf_trace.c +3 -0
index 2f206a2a2..f7ebf17e3 100644
@@ -3385,6 +3385,9 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
 	if (sizeof(u64) != sizeof(void *))
 		return -EOPNOTSUPP;
 
+	if (attr->link_create.target_fd || attr->link_create.flags)
+		return -EINVAL;
+
 	if (!is_uprobe_multi(prog))
 		return -EINVAL;
 
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH bpf-next 2/2] bpf: Check link_create parameter for multi_uprobe

Quoted 15 lines · Tao Chen

The target_fd and flags in link_create no used in multi_uprobe , return -EINVAL if they assigned, keep it same as other link attach apis.

Fixes 89ae89f53d20 ("bpf: Add multi uprobe link")
---
 kernel/trace/bpf_trace.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/trace/bpf_trace.c +0 -0
index 2f206a2a2..f7ebf17e3 100644

kernel/trace/bpf_trace.c int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 3389

Quoted 5 lines
kernel/trace/bpf_trace.c +2 -0
@@ -3385,6 +3385,9 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
 	if (sizeof(u64) != sizeof(void *))
 		return -EOPNOTSUPP;
 
+	if (attr->link_create.target_fd || attr->link_create.flags)
+		return -EINVAL;

I think the CI is failing because usdt code does uprobe multi detection with target_fd = -1 and it fails and perf-uprobe fallback will fail on not having enough file descriptors

but I think at this stage we will brake some user apps by introducing this check, link ebpf go library, which passes 0

jirka

Quoted 7 lines
kernel/trace/bpf_trace.c +1 -0
+
 	if (!is_uprobe_multi(prog))
 		return -EINVAL;
 
Signature · 1 line
2.43.0
author
#3 lore raw

Re: [PATCH bpf-next 2/2] bpf: Check link_create parameter for multi_uprobe

Quoted 19 lines · Jiri Olsa
Quoted 18 lines · Tao Chen

The target_fd and flags in link_create no used in multi_uprobe , return -EINVAL if they assigned, keep it same as other link attach apis.

Fixes 89ae89f53d20 ("bpf: Add multi uprobe link")
---
  kernel/trace/bpf_trace.c | 3 +++
  1 file changed, 3 insertions(+)
kernel/trace/bpf_trace.c +0 -0
index 2f206a2a2..f7ebf17e3 100644
@@ -3385,6 +3385,9 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
  	if (sizeof(u64) != sizeof(void *))
  		return -EOPNOTSUPP;
 
Quoted 7 lines

kernel/trace/bpf_trace.c int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 3389

Quoted 2 lines
kernel/trace/bpf_trace.c +2 -0
+	if (attr->link_create.target_fd || attr->link_create.flags)
+		return -EINVAL;

I think the CI is failing because usdt code does uprobe multi detection with target_fd = -1 and it fails and perf-uprobe fallback will fail on not having enough file descriptors

Hi jiri

As you said, i found it, thanks.

static int probe_uprobe_multi_link(int token_fd)

{
        LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
                .expected_attach_type = BPF_TRACE_UPROBE_MULTI,
                .token_fd = token_fd,
                .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
        );
        LIBBPF_OPTS(bpf_link_create_opts, link_opts);
        struct bpf_insn insns[] = {
                BPF_MOV64_IMM(BPF_REG_0, 0),
                BPF_EXIT_INSN(),
        };
        int prog_fd, link_fd, err;
        unsigned long offset = 0;

        prog_fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, "GPL",
                                insns, ARRAY_SIZE(insns), &load_opts);
        if (prog_fd < 0)
                return -errno;

        /* Creating uprobe in '/' binary should fail with -EBADF. */
        link_opts.uprobe_multi.path = "/";
        link_opts.uprobe_multi.offsets = &offset;
        link_opts.uprobe_multi.cnt = 1;

        link_fd = bpf_link_create(prog_fd, -1, BPF_TRACE_UPROBE_MULTI, 

&link_opts);

Quoted 3 lines

but I think at this stage we will brake some user apps by introducing this check, link ebpf go library, which passes 0

So is it ok just check the flags?

Quoted 10 lines

jirka

Quoted 7 lines
kernel/trace/bpf_trace.c +1 -0
+
  	if (!is_uprobe_multi(prog))
  		return -EINVAL;
 
Signature · 1 line
2.43.0
Signature · 2 lines
Best Regards
Tao Chen
#4 lore raw

Re: [PATCH bpf-next 2/2] bpf: Check link_create parameter for multi_uprobe

Quoted 59 lines · Tao Chen
Quoted 19 lines · Jiri Olsa
Quoted 18 lines · Tao Chen

The target_fd and flags in link_create no used in multi_uprobe , return -EINVAL if they assigned, keep it same as other link attach apis.

Fixes 89ae89f53d20 ("bpf: Add multi uprobe link")
---
  kernel/trace/bpf_trace.c | 3 +++
  1 file changed, 3 insertions(+)
kernel/trace/bpf_trace.c +0 -0
index 2f206a2a2..f7ebf17e3 100644
@@ -3385,6 +3385,9 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
     if (sizeof(u64) != sizeof(void *))
             return -EOPNOTSUPP;
 
Quoted 7 lines

kernel/trace/bpf_trace.c int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 3389

Quoted 2 lines
kernel/trace/bpf_trace.c +2 -0
+    if (attr->link_create.target_fd || attr->link_create.flags)
+            return -EINVAL;

I think the CI is failing because usdt code does uprobe multi detection with target_fd = -1 and it fails and perf-uprobe fallback will fail on not having enough file descriptors

Hi jiri

As you said, i found it, thanks.

static int probe_uprobe_multi_link(int token_fd)

{
         LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
                 .expected_attach_type = BPF_TRACE_UPROBE_MULTI,
                 .token_fd = token_fd,
                 .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
         );
         LIBBPF_OPTS(bpf_link_create_opts, link_opts);
         struct bpf_insn insns[] = {
                 BPF_MOV64_IMM(BPF_REG_0, 0),
                 BPF_EXIT_INSN(),
         };
         int prog_fd, link_fd, err;
         unsigned long offset = 0;

         prog_fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, "GPL",
                                 insns, ARRAY_SIZE(insns), &load_opts);
         if (prog_fd < 0)
                 return -errno;

         /* Creating uprobe in '/' binary should fail with -EBADF. */
         link_opts.uprobe_multi.path = "/";
         link_opts.uprobe_multi.offsets = &offset;
         link_opts.uprobe_multi.cnt = 1;

         link_fd = bpf_link_create(prog_fd, -1, BPF_TRACE_UPROBE_MULTI,
Quoted 7 lines

&link_opts);

Quoted 3 lines

but I think at this stage we will brake some user apps by introducing this check, link ebpf go library, which passes 0

So is it ok just check the flags?

good catch, Jiri! Yep, let's validate just flags?

pw-bot: cr

Quoted 17 lines
Quoted 11 lines

jirka

Quoted 7 lines
kernel/trace/bpf_trace.c +1 -1
+
     if (!is_uprobe_multi(prog))
             return -EINVAL;
 
--

2.43.0

--
Best Regards
Tao Chen

#5 lore raw

Re: [PATCH bpf-next 2/2] bpf: Check link_create parameter for multi_uprobe

Quoted 62 lines · Andrii Nakryiko
Quoted 61 lines · Tao Chen
Quoted 19 lines · Jiri Olsa
Quoted 18 lines · Tao Chen

The target_fd and flags in link_create no used in multi_uprobe , return -EINVAL if they assigned, keep it same as other link attach apis.

Fixes 89ae89f53d20 ("bpf: Add multi uprobe link")
---
  kernel/trace/bpf_trace.c | 3 +++
  1 file changed, 3 insertions(+)
kernel/trace/bpf_trace.c +0 -0
index 2f206a2a2..f7ebf17e3 100644
@@ -3385,6 +3385,9 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
     if (sizeof(u64) != sizeof(void *))
             return -EOPNOTSUPP;
 
Quoted 7 lines

kernel/trace/bpf_trace.c int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 3389

Quoted 2 lines
kernel/trace/bpf_trace.c +2 -0
+    if (attr->link_create.target_fd || attr->link_create.flags)
+            return -EINVAL;

I think the CI is failing because usdt code does uprobe multi detection with target_fd = -1 and it fails and perf-uprobe fallback will fail on not having enough file descriptors

Hi jiri

As you said, i found it, thanks.

static int probe_uprobe_multi_link(int token_fd)

{
         LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
                 .expected_attach_type = BPF_TRACE_UPROBE_MULTI,
                 .token_fd = token_fd,
                 .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
         );
         LIBBPF_OPTS(bpf_link_create_opts, link_opts);
         struct bpf_insn insns[] = {
                 BPF_MOV64_IMM(BPF_REG_0, 0),
                 BPF_EXIT_INSN(),
         };
         int prog_fd, link_fd, err;
         unsigned long offset = 0;

         prog_fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, "GPL",
                                 insns, ARRAY_SIZE(insns), &load_opts);
         if (prog_fd < 0)
                 return -errno;

         /* Creating uprobe in '/' binary should fail with -EBADF. */
         link_opts.uprobe_multi.path = "/";
         link_opts.uprobe_multi.offsets = &offset;
         link_opts.uprobe_multi.cnt = 1;

         link_fd = bpf_link_create(prog_fd, -1, BPF_TRACE_UPROBE_MULTI,

&link_opts);

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

but I think at this stage we will brake some user apps by introducing this check, link ebpf go library, which passes 0

So is it ok just check the flags?

good catch, Jiri! Yep, let's validate just flags?

I think so.. I'll test that with ebpf/go to make sure we are safe at least there ;-) I'll let you know

jirka

Quoted 20 lines

pw-bot: cr

Quoted 17 lines
Quoted 11 lines

jirka

Quoted 7 lines
kernel/trace/bpf_trace.c +1 -1
+
     if (!is_uprobe_multi(prog))
             return -EINVAL;
 
--

2.43.0

--
Best Regards
Tao Chen

#6 lore raw

Re: [PATCH bpf-next 2/2] bpf: Check link_create parameter for multi_uprobe

Quoted 67 lines · Jiri Olsa
Quoted 66 lines · Andrii Nakryiko
Quoted 65 lines · Tao Chen
Quoted 19 lines · Jiri Olsa
Quoted 18 lines · Tao Chen

The target_fd and flags in link_create no used in multi_uprobe , return -EINVAL if they assigned, keep it same as other link attach apis.

Fixes 89ae89f53d20 ("bpf: Add multi uprobe link")
---
  kernel/trace/bpf_trace.c | 3 +++
  1 file changed, 3 insertions(+)
kernel/trace/bpf_trace.c +0 -0
index 2f206a2a2..f7ebf17e3 100644
@@ -3385,6 +3385,9 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
     if (sizeof(u64) != sizeof(void *))
             return -EOPNOTSUPP;
 
Quoted 7 lines

kernel/trace/bpf_trace.c int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 3389

Quoted 2 lines
kernel/trace/bpf_trace.c +2 -0
+    if (attr->link_create.target_fd || attr->link_create.flags)
+            return -EINVAL;

I think the CI is failing because usdt code does uprobe multi detection with target_fd = -1 and it fails and perf-uprobe fallback will fail on not having enough file descriptors

Hi jiri

As you said, i found it, thanks.

static int probe_uprobe_multi_link(int token_fd)

{
         LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
                 .expected_attach_type = BPF_TRACE_UPROBE_MULTI,
                 .token_fd = token_fd,
                 .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
         );
         LIBBPF_OPTS(bpf_link_create_opts, link_opts);
         struct bpf_insn insns[] = {
                 BPF_MOV64_IMM(BPF_REG_0, 0),
                 BPF_EXIT_INSN(),
         };
         int prog_fd, link_fd, err;
         unsigned long offset = 0;

         prog_fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, "GPL",
                                 insns, ARRAY_SIZE(insns), &load_opts);
         if (prog_fd < 0)
                 return -errno;

         /* Creating uprobe in '/' binary should fail with -EBADF. */
         link_opts.uprobe_multi.path = "/";
         link_opts.uprobe_multi.offsets = &offset;
         link_opts.uprobe_multi.cnt = 1;

         link_fd = bpf_link_create(prog_fd, -1, BPF_TRACE_UPROBE_MULTI,

&link_opts);

Quoted 3 lines

but I think at this stage we will brake some user apps by introducing this check, link ebpf go library, which passes 0

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

So is it ok just check the flags?

good catch, Jiri! Yep, let's validate just flags?

I think so.. I'll test that with ebpf/go to make sure we are safe at least there ;-) I'll let you know

sorry, got stuck.. link_create.flags are initialized to zero, so I think flags check should be fine (at least for ebpf/go)

jirka

#7 lore raw

Re: [PATCH bpf-next 2/2] bpf: Check link_create parameter for multi_uprobe

Quoted 70 lines · Jiri Olsa
Quoted 69 lines · Jiri Olsa
Quoted 68 lines · Andrii Nakryiko
Quoted 66 lines · Tao Chen
Quoted 19 lines · Jiri Olsa
Quoted 18 lines · Tao Chen

The target_fd and flags in link_create no used in multi_uprobe , return -EINVAL if they assigned, keep it same as other link attach apis.

Fixes 89ae89f53d20 ("bpf: Add multi uprobe link")
---
  kernel/trace/bpf_trace.c | 3 +++
  1 file changed, 3 insertions(+)
kernel/trace/bpf_trace.c +0 -0
index 2f206a2a2..f7ebf17e3 100644
@@ -3385,6 +3385,9 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
     if (sizeof(u64) != sizeof(void *))
             return -EOPNOTSUPP;
 
Quoted 7 lines

kernel/trace/bpf_trace.c int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 3389

Quoted 2 lines
kernel/trace/bpf_trace.c +2 -0
+    if (attr->link_create.target_fd || attr->link_create.flags)
+            return -EINVAL;

I think the CI is failing because usdt code does uprobe multi detection with target_fd = -1 and it fails and perf-uprobe fallback will fail on not having enough file descriptors

Hi jiri

As you said, i found it, thanks.

static int probe_uprobe_multi_link(int token_fd)

{
         LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
                 .expected_attach_type = BPF_TRACE_UPROBE_MULTI,
                 .token_fd = token_fd,
                 .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
         );
         LIBBPF_OPTS(bpf_link_create_opts, link_opts);
         struct bpf_insn insns[] = {
                 BPF_MOV64_IMM(BPF_REG_0, 0),
                 BPF_EXIT_INSN(),
         };
         int prog_fd, link_fd, err;
         unsigned long offset = 0;

         prog_fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, "GPL",
                                 insns, ARRAY_SIZE(insns), &load_opts);
         if (prog_fd < 0)
                 return -errno;

         /* Creating uprobe in '/' binary should fail with -EBADF. */
         link_opts.uprobe_multi.path = "/";
         link_opts.uprobe_multi.offsets = &offset;
         link_opts.uprobe_multi.cnt = 1;

         link_fd = bpf_link_create(prog_fd, -1, BPF_TRACE_UPROBE_MULTI,

&link_opts);

Quoted 3 lines

but I think at this stage we will brake some user apps by introducing this check, link ebpf go library, which passes 0

So is it ok just check the flags?

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

good catch, Jiri! Yep, let's validate just flags?

I think so.. I'll test that with ebpf/go to make sure we are safe at least there ;-) I'll let you know

sorry, got stuck.. link_create.flags are initialized to zero, so I think flags check should be fine (at least for ebpf/go)

sry forgot.. adding Timo to the loop (ebpf/go)

jirka

author
#8 lore raw

Re: [PATCH bpf-next 2/2] bpf: Check link_create parameter for multi_uprobe

In reply to Jiri Olsa
Quoted 70 lines · Jiri Olsa
Quoted 69 lines · Jiri Olsa
Quoted 68 lines · Andrii Nakryiko
Quoted 66 lines · Tao Chen
Quoted 19 lines · Jiri Olsa
Quoted 18 lines · Tao Chen

The target_fd and flags in link_create no used in multi_uprobe , return -EINVAL if they assigned, keep it same as other link attach apis.

Fixes 89ae89f53d20 ("bpf: Add multi uprobe link")
---
   kernel/trace/bpf_trace.c | 3 +++
   1 file changed, 3 insertions(+)
kernel/trace/bpf_trace.c +0 -0
index 2f206a2a2..f7ebf17e3 100644
@@ -3385,6 +3385,9 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
      if (sizeof(u64) != sizeof(void *))
              return -EOPNOTSUPP;
 
Quoted 7 lines

kernel/trace/bpf_trace.c int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 3389

Quoted 2 lines
kernel/trace/bpf_trace.c +2 -0
+    if (attr->link_create.target_fd || attr->link_create.flags)
+            return -EINVAL;

I think the CI is failing because usdt code does uprobe multi detection with target_fd = -1 and it fails and perf-uprobe fallback will fail on not having enough file descriptors

Hi jiri

As you said, i found it, thanks.

static int probe_uprobe_multi_link(int token_fd)

{
          LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
                  .expected_attach_type = BPF_TRACE_UPROBE_MULTI,
                  .token_fd = token_fd,
                  .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
          );
          LIBBPF_OPTS(bpf_link_create_opts, link_opts);
          struct bpf_insn insns[] = {
                  BPF_MOV64_IMM(BPF_REG_0, 0),
                  BPF_EXIT_INSN(),
          };
          int prog_fd, link_fd, err;
          unsigned long offset = 0;

          prog_fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, "GPL",
                                  insns, ARRAY_SIZE(insns), &load_opts);
          if (prog_fd < 0)
                  return -errno;

          /* Creating uprobe in '/' binary should fail with -EBADF. */
          link_opts.uprobe_multi.path = "/";
          link_opts.uprobe_multi.offsets = &offset;
          link_opts.uprobe_multi.cnt = 1;

          link_fd = bpf_link_create(prog_fd, -1, BPF_TRACE_UPROBE_MULTI,

&link_opts);

Quoted 3 lines

but I think at this stage we will brake some user apps by introducing this check, link ebpf go library, which passes 0

So is it ok just check the flags?

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

good catch, Jiri! Yep, let's validate just flags?

I think so.. I'll test that with ebpf/go to make sure we are safe at least there ;-) I'll let you know

sorry, got stuck.. link_create.flags are initialized to zero, so I think flags check should be fine (at least for ebpf/go)

Thank you very much for your detailed check. I will send it v2.

Quoted 2 lines

jirka

Signature · 2 lines
Best Regards
Tao Chen