Skip to contentSkip to conversation

bpf: Fail uprobe multi link with negative offset

RFC Superseded Jiri Olsa 5 participants 10 messages Dec 13, 2023 – Dec 14, 2023

Subsystems (6)
Kselftest BPF Tracing BPF selftests +2 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

RFC 1/2 bpf: Fail uprobe multi link with negative offset

Currently the __uprobe_register will return 0 (success) when called with negative offset. The reason is that the call to register_for_each_vma and then build_map_info won't return error for negative offset. They just won't do anything - no matching vma is found so there's no registered breakpoint for the uprobe.

I don't think we can change the behaviour of __uprobe_register and fail for negative uprobe offset, because apps might depend on that already.

But I think we can still make the change and check for it on bpf multi link syscall level.

---
 kernel/trace/bpf_trace.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/trace/bpf_trace.c +5 -0
index 774cf476a892..0dbf8d9b3ace 100644
@@ -3397,6 +3397,11 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
 			goto error_free;
 		}
 
+		if (uprobes[i].offset < 0) {
+			err = -EINVAL;
+			goto error_free;
+		}
+
 		uprobes[i].link = link;
 
 		if (flags & BPF_F_UPROBE_MULTI_RETURN)
Signature · 1 line
2.43.0
author reviewer
#1 lore raw

RFC 2/2 selftests/bpf: Add uprobe multi fail test

We fail to create uprobe if we pass negative offset, adding test for that.

---
 .../bpf/prog_tests/uprobe_multi_test.c        | 27 +++++++++++++++++++
 1 file changed, 27 insertions(+)
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +27 -0
index ece260cf2c0b..aebfa7e6bfd6 100644
@@ -326,6 +326,31 @@ void test_link_api(void)
 	__test_link_api(child);
 }
 
+static void test_attach_api_fails(void)
+{
+	LIBBPF_OPTS(bpf_link_create_opts, opts);
+	const char *path = "/proc/self/exe";
+	struct uprobe_multi *skel = NULL;
+	int prog_fd, link_fd;
+	long offset = -1;
+
+	skel = uprobe_multi__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "uprobe_multi__open_and_load"))
+		return;
+
+	/* fail_1 - attach on negative offset */
+	opts.uprobe_multi.path = path;
+	opts.uprobe_multi.offsets = (unsigned long *) &offset;
+	opts.uprobe_multi.cnt = 1;
+
+	prog_fd = bpf_program__fd(skel->progs.uprobe_extra);
+	link_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_UPROBE_MULTI, &opts);
+	if (!ASSERT_EQ(link_fd, -EINVAL, "link_fd"))
+		close(link_fd);
+
+	uprobe_multi__destroy(skel);
+}
+
 static void test_bench_attach_uprobe(void)
 {
 	long attach_start_ns = 0, attach_end_ns = 0;
@@ -412,4 +437,6 @@ void test_uprobe_multi_test(void)
 		test_bench_attach_uprobe();
 	if (test__start_subtest("bench_usdt"))
 		test_bench_attach_usdt();
+	if (test__start_subtest("attach_api_fails"))
+		test_attach_api_fails();
 }
Signature · 1 line
2.43.0
Reviewed-by on v1 2/2
#2 lore raw

Re: [RFC bpf-next 2/2] selftests/bpf: Add uprobe multi fail test

Quoted 4 lines · Jiri Olsa

We fail to create uprobe if we pass negative offset, adding test for that.

small suggestion below..

Quoted 24 lines
---
 .../bpf/prog_tests/uprobe_multi_test.c        | 27 +++++++++++++++++++
 1 file changed, 27 insertions(+)
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +12 -0
index ece260cf2c0b..aebfa7e6bfd6 100644
@@ -326,6 +326,31 @@ void test_link_api(void)
 	__test_link_api(child);
 }
 
+static void test_attach_api_fails(void)
+{
+	LIBBPF_OPTS(bpf_link_create_opts, opts);
+	const char *path = "/proc/self/exe";
+	struct uprobe_multi *skel = NULL;
+	int prog_fd, link_fd;
+	long offset = -1;
+
+	skel = uprobe_multi__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "uprobe_multi__open_and_load"))
+		return;
+

tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c void test_link_api(void) · line 350

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +10 -0
+	/* fail_1 - attach on negative offset */
+	opts.uprobe_multi.path = path;
+	opts.uprobe_multi.offsets = (unsigned long *) &offset;
+	opts.uprobe_multi.cnt = 1;
+
+	prog_fd = bpf_program__fd(skel->progs.uprobe_extra);
+	link_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_UPROBE_MULTI, &opts);
+	if (!ASSERT_EQ(link_fd, -EINVAL, "link_fd"))
+		close(link_fd);
+

would it be worth exercising a few additional error cases here? not specifying offsets/cnt triggers an EINVAL too.

#3 lore raw

Re: [RFC bpf-next 2/2] selftests/bpf: Add uprobe multi fail test

Quoted 39 lines · Alan Maguire
Quoted 4 lines · Jiri Olsa

We fail to create uprobe if we pass negative offset, adding test for that.

small suggestion below..

Quoted 29 lines
---
 .../bpf/prog_tests/uprobe_multi_test.c        | 27 +++++++++++++++++++
 1 file changed, 27 insertions(+)
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +17 -0
index ece260cf2c0b..aebfa7e6bfd6 100644
@@ -326,6 +326,31 @@ void test_link_api(void)
 	__test_link_api(child);
 }
 
+static void test_attach_api_fails(void)
+{
+	LIBBPF_OPTS(bpf_link_create_opts, opts);
+	const char *path = "/proc/self/exe";
+	struct uprobe_multi *skel = NULL;
+	int prog_fd, link_fd;
+	long offset = -1;
+
+	skel = uprobe_multi__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "uprobe_multi__open_and_load"))
+		return;
+
+	/* fail_1 - attach on negative offset */
+	opts.uprobe_multi.path = path;
+	opts.uprobe_multi.offsets = (unsigned long *) &offset;
+	opts.uprobe_multi.cnt = 1;
+
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c void test_link_api(void) · line 350

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +5 -0
+	prog_fd = bpf_program__fd(skel->progs.uprobe_extra);
+	link_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_UPROBE_MULTI, &opts);
+	if (!ASSERT_EQ(link_fd, -EINVAL, "link_fd"))
+		close(link_fd);
+

would it be worth exercising a few additional error cases here? not specifying offsets/cnt triggers an EINVAL too.

yes, I think we can add more tests in here, will check

thanks,
jirka

Reviewed-by on v1 1/2
#4 lore raw
In reply to Jiri Olsa
Quoted 9 lines · Jiri Olsa

Currently the __uprobe_register will return 0 (success) when called with negative offset. The reason is that the call to register_for_each_vma and then build_map_info won't return error for negative offset. They just won't do anything - no matching vma is found so there's no registered breakpoint for the uprobe.

I don't think we can change the behaviour of __uprobe_register and fail for negative uprobe offset, because apps might depend on that already.

just my view, but since passing negative offsets never made sense, I wouldn't be as worried about breaking existing consumers. Regardless of what a user thought would happen passing a negative value, nothing did, so that can't have been their intent.

Quoted 4 lines

But I think we can still make the change and check for it on bpf multi link syscall level.

Quoted 20 lines
---
 kernel/trace/bpf_trace.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/trace/bpf_trace.c +5 -0
index 774cf476a892..0dbf8d9b3ace 100644
@@ -3397,6 +3397,11 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
 			goto error_free;
 		}
 
+		if (uprobes[i].offset < 0) {
+			err = -EINVAL;
+			goto error_free;
+		}
+
 		uprobes[i].link = link;
 
 		if (flags & BPF_F_UPROBE_MULTI_RETURN)
maintainer
#5 lore raw
In reply to Jiri Olsa
Quoted 23 lines · Jiri Olsa

Currently the __uprobe_register will return 0 (success) when called with negative offset. The reason is that the call to register_for_each_vma and then build_map_info won't return error for negative offset. They just won't do anything - no matching vma is found so there's no registered breakpoint for the uprobe.

I don't think we can change the behaviour of __uprobe_register and fail for negative uprobe offset, because apps might depend on that already.

But I think we can still make the change and check for it on bpf multi link syscall level.

---
 kernel/trace/bpf_trace.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/trace/bpf_trace.c +0 -0
index 774cf476a892..0dbf8d9b3ace 100644

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

Quoted 8 lines
kernel/trace/bpf_trace.c +5 -0
@@ -3397,6 +3397,11 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
                        goto error_free;
                }
 
+               if (uprobes[i].offset < 0) {
+                       err = -EINVAL;
+                       goto error_free;
+               }
+

nit: We have 3 __get_user() here. How about we move __get_user(offset) to the first
and check offset immediately after it? This will save us a few __get_user() in the
error path.

Thanks,
Song

Quoted 7 lines
kernel/trace/bpf_trace.c +0 -1
                uprobes[i].link = link;
 
                if (flags & BPF_F_UPROBE_MULTI_RETURN)
--

2.43.0

#6 lore raw
Quoted 27 lines · Song Liu
Quoted 26 lines · Jiri Olsa

Currently the __uprobe_register will return 0 (success) when called with negative offset. The reason is that the call to register_for_each_vma and then build_map_info won't return error for negative offset. They just won't do anything - no matching vma is found so there's no registered breakpoint for the uprobe.

I don't think we can change the behaviour of __uprobe_register and fail for negative uprobe offset, because apps might depend on that already.

But I think we can still make the change and check for it on bpf multi link syscall level.

---
 kernel/trace/bpf_trace.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/trace/bpf_trace.c +0 -0
index 774cf476a892..0dbf8d9b3ace 100644
@@ -3397,6 +3397,11 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
                        goto error_free;
                }
 
Quoted 11 lines

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

Quoted 5 lines
kernel/trace/bpf_trace.c +5 -0
+               if (uprobes[i].offset < 0) {
+                       err = -EINVAL;
+                       goto error_free;
+               }
+

nit: We have 3 __get_user() here. How about we move __get_user(offset) to the first
and check offset immediately after it? This will save us a few __get_user() in the
error path.

right, we can move it up

thanks,
jirka

Quoted 11 lines

Thanks,
Song

Quoted 7 lines
kernel/trace/bpf_trace.c +0 -1
                uprobes[i].link = link;
 
                if (flags & BPF_F_UPROBE_MULTI_RETURN)
--

2.43.0

#7 lore raw
In reply to Jiri Olsa
Quoted 23 lines · Jiri Olsa

Currently the __uprobe_register will return 0 (success) when called with negative offset. The reason is that the call to register_for_each_vma and then build_map_info won't return error for negative offset. They just won't do anything - no matching vma is found so there's no registered breakpoint for the uprobe.

I don't think we can change the behaviour of __uprobe_register and fail for negative uprobe offset, because apps might depend on that already.

But I think we can still make the change and check for it on bpf multi link syscall level.

---
 kernel/trace/bpf_trace.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/trace/bpf_trace.c +0 -0
index 774cf476a892..0dbf8d9b3ace 100644

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

Quoted 4 lines
kernel/trace/bpf_trace.c +1 -0
@@ -3397,6 +3397,11 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
                        goto error_free;
                }
 
+               if (uprobes[i].offset < 0) {

offset in UAPI is defined as unsigned, so how can it be negative?

Quoted 10 lines
kernel/trace/bpf_trace.c +4 -1
+                       err = -EINVAL;
+                       goto error_free;
+               }
+
                uprobes[i].link = link;
 
                if (flags & BPF_F_UPROBE_MULTI_RETURN)
--

2.43.0

#8 lore raw
Quoted 27 lines · Andrii Nakryiko
Quoted 26 lines · Jiri Olsa

Currently the __uprobe_register will return 0 (success) when called with negative offset. The reason is that the call to register_for_each_vma and then build_map_info won't return error for negative offset. They just won't do anything - no matching vma is found so there's no registered breakpoint for the uprobe.

I don't think we can change the behaviour of __uprobe_register and fail for negative uprobe offset, because apps might depend on that already.

But I think we can still make the change and check for it on bpf multi link syscall level.

---
 kernel/trace/bpf_trace.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/trace/bpf_trace.c +0 -0
index 774cf476a892..0dbf8d9b3ace 100644
@@ -3397,6 +3397,11 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
                        goto error_free;
                }
 
Quoted 3 lines

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

Quoted 1 line
kernel/trace/bpf_trace.c +1 -0
+               if (uprobes[i].offset < 0) {

offset in UAPI is defined as unsigned, so how can it be negative?

right, but then it's passed to uprobe_register_refctr as loff_t which is 'long long'

jirka

Quoted 11 lines
Quoted 11 lines
kernel/trace/bpf_trace.c +4 -1
+                       err = -EINVAL;
+                       goto error_free;
+               }
+
                uprobes[i].link = link;
 
                if (flags & BPF_F_UPROBE_MULTI_RETURN)
--

2.43.0

#9 lore raw
Quoted 29 lines · Jiri Olsa
Quoted 27 lines · Andrii Nakryiko
Quoted 26 lines · Jiri Olsa

Currently the __uprobe_register will return 0 (success) when called with negative offset. The reason is that the call to register_for_each_vma and then build_map_info won't return error for negative offset. They just won't do anything - no matching vma is found so there's no registered breakpoint for the uprobe.

I don't think we can change the behaviour of __uprobe_register and fail for negative uprobe offset, because apps might depend on that already.

But I think we can still make the change and check for it on bpf multi link syscall level.

---
 kernel/trace/bpf_trace.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/trace/bpf_trace.c +0 -0
index 774cf476a892..0dbf8d9b3ace 100644
@@ -3397,6 +3397,11 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
                        goto error_free;
                }
 
Quoted 5 lines
Quoted 3 lines

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

Quoted 1 line
kernel/trace/bpf_trace.c +1 -0
+               if (uprobes[i].offset < 0) {

offset in UAPI is defined as unsigned, so how can it be negative?

right, but then it's passed to uprobe_register_refctr as loff_t which is 'long long'

ah, so it's not rejected because uprobe_register expects signed offset (for some reason...) and it only does

if (offset > i_size_read(inode))

got it, thanks.

Quoted 14 lines

jirka

Quoted 11 lines
Quoted 11 lines
kernel/trace/bpf_trace.c +4 -1
+                       err = -EINVAL;
+                       goto error_free;
+               }
+
                uprobes[i].link = link;
 
                if (flags & BPF_F_UPROBE_MULTI_RETURN)
--

2.43.0