bpf: Fail uprobe multi link with negative offset
authorJiri Olsa <jolsa@kernel.org>
Sun, 17 Dec 2023 21:55:37 +0000 (22:55 +0100)
committerAndrii Nakryiko <andrii@kernel.org>
Mon, 18 Dec 2023 17:51:30 +0000 (09:51 -0800)
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.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Song Liu <song@kernel.org>
Link: https://lore.kernel.org/bpf/20231217215538.3361991-2-jolsa@kernel.org
kernel/trace/bpf_trace.c

index 97c0c49c40a020f8ac23d6d33fca6b31a9702468..492d60e9c48000ca4fec9ca5e8e8236c729ea089 100644 (file)
@@ -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;
                }