bpf: Add check for negative uprobe multi offset
Subsystems (6)
Jiri Olsa
reviewerv2 cover bpf: Add check for negative uprobe multi offset
hi,
adding the check for negative offset for uprobe multi link.
v2 changes:
- add more failure checks [Alan]
- move the offset retrieval/check up in the loop to be done earlier [Song]
thanks,
jirka
---
Jiri Olsa (2):
bpf: Fail uprobe multi link with negative offset selftests/bpf: Add more uprobe multi fail tests
kernel/trace/bpf_trace.c | 8 +++-- tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 152 insertions(+), 5 deletions(-)
Jiri Olsa
reviewerPATCH v2 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.
Also moving the __get_user call and check for the offsets to the top of loop, to fail early without extra __get_user calls for ref_ctr_offset and cookie arrays.
--- kernel/trace/bpf_trace.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/trace/bpf_trace.c +6 -2
@@ -3391,15 +3391,19 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr goto error_free;for (i =0 ; i < cnt; i++) { -if (ucookies &&__get_user (uprobes[i].cookie, ucookies + i)) { +if (__get_user (uprobes[i].offset, uoffsets + i)) { err = -EFAULT ; goto error_free; } +if (uprobes[i].offset <0 ) { + err = -EINVAL ; + goto error_free; + }if (uref_ctr_offsets &&__get_user (uprobes[i].ref_ctr_offset, uref_ctr_offsets + i)) { err = -EFAULT ; goto error_free; } -if (__get_user (uprobes[i].offset, uoffsets + i)) { +if (ucookies &&__get_user (uprobes[i].cookie, ucookies + i)) { err = -EFAULT ; goto error_free; }
Signature · 1 line
2.43.0
Re: [PATCHv2 bpf-next 1/2] bpf: Fail uprobe multi link with negative offset
Quoted 10 lines · Jiri Olsa
Currently the
__uprobe_registerwill return 0 (success) when called with negative offset. The reason is that the call toregister_for_each_vmaand thenbuild_map_infowon'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_registerand fail for negative uprobe offset, because apps might depend on that already.
Quoted 8 lines
But I think we can still make the change and check for it on bpf multi link syscall level.
Also moving the
__get_usercall and check for the offsets to the top of loop, to fail early without extra__get_usercalls forref_ctr_offsetand cookie arrays.Signed-off-by Jiri Olsa <[email protected]>
Re: [PATCHv2 bpf-next 1/2] bpf: Fail uprobe multi link with negative offset
In reply to Jiri OlsaQuoted 29 lines · Jiri Olsa
Currently the
__uprobe_registerwill return 0 (success) when called with negative offset. The reason is that the call toregister_for_each_vmaand thenbuild_map_infowon'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_registerand 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.
Also moving the
__get_usercall and check for the offsets to the top of loop, to fail early without extra__get_usercalls forref_ctr_offsetand cookie arrays.Signed-off-by Jiri Olsa <[email protected]>--- kernel/trace/bpf_trace.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)kernel/trace/bpf_trace.c +0
-0@@ -3391,15 +3391,19 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr goto error_free;
kernel/trace/bpf_trace.c int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 3401
Quoted 10 lines
kernel/trace/bpf_trace.c +5
-1for (i =0 ; i < cnt; i++) { -if (ucookies &&__get_user (uprobes[i].cookie, ucookies + i)) { +if (__get_user (uprobes[i].offset, uoffsets + i)) { err = -EFAULT ; goto error_free; } +if (uprobes[i].offset <0 ) { + err = -EINVAL ; + goto error_free; + }
I applied this because it does fix the problem, but the whole reshuffle of offsets in front of cookies is pointless, because of the common for() loop. You are saving one or two __get_user() calls before failing.
If we really want to do validation first, reading offsets should be in its own for loop, then uref_ctr_offsets in its own, and then cookies in its own loop as well. That way we read and validate the entire array before reading another array. Please consider a follow up, if you think it's important enough.
Quoted 12 lines
kernel/trace/bpf_trace.c +1
-2if (uref_ctr_offsets &&__get_user (uprobes[i].ref_ctr_offset, uref_ctr_offsets + i)) { err = -EFAULT ; goto error_free; } -if (__get_user (uprobes[i].offset, uoffsets + i)) { +if (ucookies &&__get_user (uprobes[i].cookie, ucookies + i)) { err = -EFAULT ; goto error_free; } --2.43.0
Re: [PATCHv2 bpf-next 1/2] bpf: Fail uprobe multi link with negative offset
Quoted 46 lines · Andrii Nakryiko
Quoted 29 lines · Jiri Olsa
Currently the
__uprobe_registerwill return 0 (success) when called with negative offset. The reason is that the call toregister_for_each_vmaand thenbuild_map_infowon'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_registerand 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.
Also moving the
__get_usercall and check for the offsets to the top of loop, to fail early without extra__get_usercalls forref_ctr_offsetand cookie arrays.Signed-off-by Jiri Olsa <[email protected]>--- kernel/trace/bpf_trace.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)kernel/trace/bpf_trace.c +0
-0@@ -3391,15 +3391,19 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr goto error_free;kernel/trace/bpf_trace.c int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 3401
Quoted 10 lines
kernel/trace/bpf_trace.c +5
-1for (i =0 ; i < cnt; i++) { -if (ucookies &&__get_user (uprobes[i].cookie, ucookies + i)) { +if (__get_user (uprobes[i].offset, uoffsets + i)) { err = -EFAULT ; goto error_free; } +if (uprobes[i].offset <0 ) { + err = -EINVAL ; + goto error_free; + }I applied this because it does fix the problem, but the whole reshuffle of offsets in front of cookies is pointless, because of the common
for()loop. You are saving one or two__get_user()calls before failing.
Quoted 5 lines
If we really want to do validation first, reading offsets should be in its own for loop, then
uref_ctr_offsetsin its own, and then cookies in its own loop as well. That way we read and validate the entire array before reading another array. Please consider a follow up, if you think it's important enough.
ok, thanks
jirka
Quoted 14 lines
Quoted 14 lines
kernel/trace/bpf_trace.c +1
-2if (uref_ctr_offsets &&__get_user (uprobes[i].ref_ctr_offset, uref_ctr_offsets + i)) { err = -EFAULT ; goto error_free; } -if (__get_user (uprobes[i].offset, uoffsets + i)) { +if (ucookies &&__get_user (uprobes[i].cookie, ucookies + i)) { err = -EFAULT ; goto error_free; } --2.43.0
Jiri Olsa
reviewerPATCH v2 2/2 selftests/bpf: Add more uprobe multi fail tests
In reply to Jiri OlsaWe fail to create uprobe if we pass negative offset, adding test for that plus some more.
--- .../bpf/prog_tests/uprobe_multi_test.c | 149 +++++++++++++++++- 1 file changed, 146 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +146 -3
@@ -239,23 +239,166 @@ 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 =-1 ;unsigned long offset =0 ; -int link_fd =-1 ; skel =uprobe_multi__open_and_load ();if (!ASSERT_OK_PTR (skel,"uprobe_multi__open_and_load" )) goto cleanup; + prog_fd =bpf_program__fd (skel->progs.uprobe_extra); +/* abnormal cnt */ opts.uprobe_multi.path = path; opts.uprobe_multi.offsets = &offset; opts.uprobe_multi.cnt =INT_MAX ; - link_fd =bpf_link_create (bpf_program__fd (skel->progs.uprobe),0 , -BPF_TRACE_UPROBE_MULTI , &opts); + link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_UPROBE_MULTI , &opts);if (!ASSERT_ERR (link_fd,"link_fd" )) goto cleanup;if (!ASSERT_EQ (link_fd, -E2BIG ,"big cnt" )) goto cleanup; + +/* cnt is 0 */ + LIBBPF_OPTS_RESET(opts, + .uprobe_multi.path = path, + .uprobe_multi.offsets = (unsigned long *) &offset, + ); + + link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_UPROBE_MULTI , &opts); +if (!ASSERT_ERR (link_fd,"link_fd" )) + goto cleanup; +if (!ASSERT_EQ (link_fd, -EINVAL ,"cnt_is_zero" )) + goto cleanup; + +/* negative offset */ + offset =-1 ; + opts.uprobe_multi.path = path; + opts.uprobe_multi.offsets = (unsigned long *) &offset; + opts.uprobe_multi.cnt =1 ; + + link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_UPROBE_MULTI , &opts); +if (!ASSERT_ERR (link_fd,"link_fd" )) + goto cleanup; +if (!ASSERT_EQ (link_fd, -EINVAL ,"offset_is_negative" )) + goto cleanup; + +/* offsets is NULL */ + LIBBPF_OPTS_RESET(opts, + .uprobe_multi.path = path, + .uprobe_multi.cnt = 1, + ); + + link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_UPROBE_MULTI , &opts); +if (!ASSERT_ERR (link_fd,"link_fd" )) + goto cleanup; +if (!ASSERT_EQ (link_fd, -EINVAL ,"offsets_is_null" )) + goto cleanup; + +/* wrong offsets pointer */ + LIBBPF_OPTS_RESET(opts, + .uprobe_multi.path = path, + .uprobe_multi.offsets = (unsigned long *) 1, + .uprobe_multi.cnt = 1, + ); + + link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_UPROBE_MULTI , &opts); +if (!ASSERT_ERR (link_fd,"link_fd" )) + goto cleanup; +if (!ASSERT_EQ (link_fd, -EFAULT ,"offsets_is_wrong" )) + goto cleanup; + +/* path is NULL */ + offset =1 ; + LIBBPF_OPTS_RESET(opts, + .uprobe_multi.offsets = (unsigned long *) &offset, + .uprobe_multi.cnt = 1, + ); + + link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_UPROBE_MULTI , &opts); +if (!ASSERT_ERR (link_fd,"link_fd" )) + goto cleanup; +if (!ASSERT_EQ (link_fd, -EINVAL ,"path_is_null" )) + goto cleanup; + +/* wrong path pointer */ + LIBBPF_OPTS_RESET(opts, + .uprobe_multi.path = (const char *) 1, + .uprobe_multi.offsets = (unsigned long *) &offset, + .uprobe_multi.cnt = 1, + ); + + link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_UPROBE_MULTI , &opts); +if (!ASSERT_ERR (link_fd,"link_fd" )) + goto cleanup; +if (!ASSERT_EQ (link_fd, -EFAULT ,"path_is_wrong" )) + goto cleanup; + +/* wrong path type */ + LIBBPF_OPTS_RESET(opts, + .uprobe_multi.path = "/", + .uprobe_multi.offsets = (unsigned long *) &offset, + .uprobe_multi.cnt = 1, + ); + + link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_UPROBE_MULTI , &opts); +if (!ASSERT_ERR (link_fd,"link_fd" )) + goto cleanup; +if (!ASSERT_EQ (link_fd, -EBADF ,"path_is_wrong_type" )) + goto cleanup; + +/* wrong cookies pointer */ + LIBBPF_OPTS_RESET(opts, + .uprobe_multi.path = path, + .uprobe_multi.offsets = (unsigned long *) &offset, + .uprobe_multi.cookies = (__u64 *) 1ULL, + .uprobe_multi.cnt = 1, + ); + + link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_UPROBE_MULTI , &opts); +if (!ASSERT_ERR (link_fd,"link_fd" )) + goto cleanup; +if (!ASSERT_EQ (link_fd, -EFAULT ,"cookies_is_wrong" )) + goto cleanup; + +/* wrong ref_ctr_offsets pointer */ + LIBBPF_OPTS_RESET(opts, + .uprobe_multi.path = path, + .uprobe_multi.offsets = (unsigned long *) &offset, + .uprobe_multi.cookies = (__u64 *) &offset, + .uprobe_multi.ref_ctr_offsets = (unsigned long *) 1, + .uprobe_multi.cnt = 1, + ); + + link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_UPROBE_MULTI , &opts); +if (!ASSERT_ERR (link_fd,"link_fd" )) + goto cleanup; +if (!ASSERT_EQ (link_fd, -EFAULT ,"ref_ctr_offsets_is_wrong" )) + goto cleanup; + +/* wrong flags */ + LIBBPF_OPTS_RESET(opts, + .uprobe_multi.flags = 1 << 31, + ); + + link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_UPROBE_MULTI , &opts); +if (!ASSERT_ERR (link_fd,"link_fd" )) + goto cleanup; +if (!ASSERT_EQ (link_fd, -EINVAL ,"wrong_flags" )) + goto cleanup; + +/* wrong pid */ + LIBBPF_OPTS_RESET(opts, + .uprobe_multi.path = path, + .uprobe_multi.offsets = (unsigned long *) &offset, + .uprobe_multi.cnt = 1, + .uprobe_multi.pid = -2, + ); + + link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_UPROBE_MULTI , &opts); +if (!ASSERT_ERR (link_fd,"link_fd" )) + goto cleanup; +ASSERT_EQ (link_fd, -ESRCH ,"pid_is_wrong" ); + cleanup:if (link_fd >=0 )close (link_fd);
Signature · 1 line
2.43.0
Re: [PATCHv2 bpf-next 2/2] selftests/bpf: Add more uprobe multi fail tests
Quoted 3 lines · Jiri Olsa
We fail to create uprobe if we pass negative offset, adding test for that plus some more.
nit: "negative offset plus some more" is a little weird to me. This is more like "testing error handling paths of uprobe multi creation".
Quoted 2 lines
Signed-off-by Jiri Olsa <[email protected]>
Other than the nit,
Re: [PATCHv2 bpf-next 2/2] selftests/bpf: Add more uprobe multi fail tests
Quoted 8 lines · Song Liu
Quoted 3 lines · Jiri Olsa
We fail to create uprobe if we pass negative offset, adding test for that plus some more.
nit: "negative offset plus some more" is a little weird to me. This is more like "testing error handling paths of uprobe multi creation".
I fixed it up and generalized a bit while applying.
Quoted 7 lines
Quoted 3 lines
Signed-off-by Jiri Olsa <[email protected]>Other than the nit,
Acked-by Song Liu <[email protected]>
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 8 lines · On Sun, 17 Dec 2023 22:55:36 +0100 you wrote:
hi,
adding the check for negative offset for uprobe multi link.v2 changes:
- add more failure checks [Alan]
- move the offset retrieval/check up in the loop to be done earlier [Song][...]
Here is the summary with links:
- [PATCHv2,bpf-next,1/2] bpf: Fail uprobe multi link with negative offset
https://git.kernel.org/bpf/bpf-next/c/3983c00281d9 - [PATCHv2,bpf-next,2/2] selftests/bpf: Add more uprobe multi fail tests https://git.kernel.org/bpf/bpf-next/c/f17d1a18a3dd
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html