Skip to contentSkip to conversation

bpf: Force kprobe multi expected_attach_type for kprobe_multi link

PATCH Superseded Jiri Olsa 4 participants 4 messages Jun 13, 2023 – Jun 17, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH 1/1 bpf: Force kprobe multi expected_attach_type for kprobe_multi link

We currently allow to create perf link for program with expected_attach_type == BPF_TRACE_KPROBE_MULTI.

This will cause crash when we call helpers like get_attach_cookie or get_func_ip in such program, because it will call the kprobe_multi's version (current->bpf_ctx context setup) of those helpers while it expects perf_link's current->bpf_ctx context setup.

Making sure that we use BPF_TRACE_KPROBE_MULTI expected_attach_type only for programs attaching through kprobe_multi link.

Fixes ca74823c6e16 ("bpf: Add cookie support to programs attached with kprobe multi link")
---
 kernel/bpf/syscall.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/bpf/syscall.c +5 -0
index 0c21d0d8efe4..e8fe04a5db93 100644
@@ -4675,6 +4675,11 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
 		ret = bpf_perf_link_attach(attr, prog);
 		break;
 	case BPF_PROG_TYPE_KPROBE:
+		if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI &&
+		    attr->link_create.attach_type != BPF_TRACE_KPROBE_MULTI) {
+			ret = -EINVAL;
+			goto out;
+		}
 		if (attr->link_create.attach_type == BPF_PERF_EVENT)
 			ret = bpf_perf_link_attach(attr, prog);
 		else
Signature · 1 line
2.40.1
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Jiri Olsa

We currently allow to create perf link for program with expected_attach_type == BPF_TRACE_KPROBE_MULTI.

This will cause crash when we call helpers like get_attach_cookie or get_func_ip in such program, because it will call the kprobe_multi's version (current->bpf_ctx context setup) of those helpers while it expects perf_link's current->bpf_ctx context setup.

Quoted 5 lines

Making sure that we use BPF_TRACE_KPROBE_MULTI expected_attach_type only for programs attaching through kprobe_multi link.

Fixes ca74823c6e16 ("bpf: Add cookie support to programs attached with kprobe multi link")
#2 lore raw
In reply to Jiri Olsa
Quoted 23 lines · Jiri Olsa

We currently allow to create perf link for program with expected_attach_type == BPF_TRACE_KPROBE_MULTI.

This will cause crash when we call helpers like get_attach_cookie or get_func_ip in such program, because it will call the kprobe_multi's version (current->bpf_ctx context setup) of those helpers while it expects perf_link's current->bpf_ctx context setup.

Making sure that we use BPF_TRACE_KPROBE_MULTI expected_attach_type only for programs attaching through kprobe_multi link.

Fixes ca74823c6e16 ("bpf: Add cookie support to programs attached with kprobe multi link")
---
 kernel/bpf/syscall.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/bpf/syscall.c +0 -0
index 0c21d0d8efe4..e8fe04a5db93 100644

kernel/bpf/syscall.c static int link_create(union bpf_attr *attr, bpfptr_t uattr) · line 4682

Quoted 8 lines
kernel/bpf/syscall.c +5 -0
@@ -4675,6 +4675,11 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
                ret = bpf_perf_link_attach(attr, prog);
                break;
        case BPF_PROG_TYPE_KPROBE:
+               if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI &&
+                   attr->link_create.attach_type != BPF_TRACE_KPROBE_MULTI) {
+                       ret = -EINVAL;
+                       goto out;
+               }

there is a separate expected attach type validation switch above this, shouldn't this go there? We also have bpf_prog_attach_check_attach_type() call above as well, and tbh by now I'm not sure why we have like three places to check conditions like this... But I'd put this check in either bpf_prog_attach_check_attach_type() or in the dedicated switch for attach_type checks.

Quoted 6 lines
kernel/bpf/syscall.c +0 -1
                if (attr->link_create.attach_type == BPF_PERF_EVENT)
                        ret = bpf_perf_link_attach(attr, prog);
                else
--

2.40.1

#3 lore raw
Quoted 33 lines · Andrii Nakryiko
Quoted 23 lines · Jiri Olsa

We currently allow to create perf link for program with expected_attach_type == BPF_TRACE_KPROBE_MULTI.

This will cause crash when we call helpers like get_attach_cookie or get_func_ip in such program, because it will call the kprobe_multi's version (current->bpf_ctx context setup) of those helpers while it expects perf_link's current->bpf_ctx context setup.

Making sure that we use BPF_TRACE_KPROBE_MULTI expected_attach_type only for programs attaching through kprobe_multi link.

Fixes ca74823c6e16 ("bpf: Add cookie support to programs attached with kprobe multi link")
---
 kernel/bpf/syscall.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/bpf/syscall.c +0 -0
index 0c21d0d8efe4..e8fe04a5db93 100644

kernel/bpf/syscall.c static int link_create(union bpf_attr *attr, bpfptr_t uattr) · line 4682

Quoted 8 lines
kernel/bpf/syscall.c +5 -0
@@ -4675,6 +4675,11 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
                ret = bpf_perf_link_attach(attr, prog);
                break;
        case BPF_PROG_TYPE_KPROBE:
+               if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI &&
+                   attr->link_create.attach_type != BPF_TRACE_KPROBE_MULTI) {
+                       ret = -EINVAL;
+                       goto out;
+               }
Quoted 8 lines

there is a separate expected attach type validation switch above this, shouldn't this go there? We also have bpf_prog_attach_check_attach_type() call above as well, and tbh by now I'm not sure why we have like three places to check conditions like this... But I'd put this check in either bpf_prog_attach_check_attach_type() or in the dedicated switch for attach_type checks.

bpf_prog_attach_check_attach_type looks good, will move it there

thanks,
jirka