Tracing BPF_LINK_CREATE returns an error without explaining why attachment failed. Pass the BPF_LINK_CREATE log to the tracing attach path and report invalid attach parameters. Also pass it to bpf_check_attach_target() so freplace attachment to a new target can report target-check failures. Signed-off-by: Leon Hwang --- kernel/bpf/syscall.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 04a3b3c80c74..cf6bf3765b3d 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3674,7 +3674,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, int tgt_prog_fd, u32 btf_id, u64 bpf_cookie, - enum bpf_attach_type attach_type) + enum bpf_attach_type attach_type, + struct bpf_verifier_log *log) { struct bpf_link_primer link_primer; struct bpf_prog *tgt_prog = NULL; @@ -3711,6 +3712,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, } if (!!tgt_prog_fd != !!btf_id) { + bpf_log(log, "tgt_prog_fd and btf_id must be both set or both unset.\n"); err = -EINVAL; goto out_put_prog; } @@ -3723,12 +3725,14 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, * attach_tracing_prog flag is set. */ if (prog->type != BPF_PROG_TYPE_EXT) { + bpf_log(log, "tgt_prog_fd is only allowed for freplace prog.\n"); err = -EINVAL; goto out_put_prog; } tgt_prog = bpf_prog_get(tgt_prog_fd); if (IS_ERR(tgt_prog)) { + bpf_log(log, "Invalid tgt_prog_fd.\n"); err = PTR_ERR(tgt_prog); tgt_prog = NULL; goto out_put_prog; @@ -3783,11 +3787,13 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, */ if (prog->type != BPF_PROG_TYPE_TRACING && prog->type != BPF_PROG_TYPE_LSM) { + bpf_log(log, "Only allow re-attach for tracing and lsm progs.\n"); err = -EINVAL; goto out_unlock; } /* We can allow re-attach only if we have valid attach_btf. */ if (!prog->aux->attach_btf) { + bpf_log(log, "Invalid attach_btf.\n"); err = -EINVAL; goto out_unlock; } @@ -3803,7 +3809,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, */ struct bpf_attach_target_info tgt_info = {}; - err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id, + err = bpf_check_attach_target(log, prog, tgt_prog, btf_id, &tgt_info); if (err) goto out_unlock; @@ -3843,6 +3849,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, */ if (prog->type == BPF_PROG_TYPE_EXT && prog->aux->kprobe_write_ctx != tgt_prog->aux->kprobe_write_ctx) { + bpf_log(log, "Invalid kprobe_write_ctx.\n"); err = -EINVAL; goto out_unlock; } @@ -3856,6 +3863,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, tgt_prog->type == BPF_PROG_TYPE_TRACEPOINT || tgt_prog->type == BPF_PROG_TYPE_SYSCALL) && prog->aux->max_ctx_offset > tgt_prog->aux->max_ctx_offset) { + bpf_log(log, "Freplace prog accesses more ctx than its target.\n"); err = -EINVAL; goto out_unlock; } @@ -4374,7 +4382,7 @@ static int bpf_raw_tp_link_attach(struct bpf_prog *prog, tp_name = prog->aux->attach_func_name; break; } - return bpf_tracing_prog_attach(prog, 0, 0, 0, attach_type); + return bpf_tracing_prog_attach(prog, 0, 0, 0, attach_type, NULL); case BPF_PROG_TYPE_RAW_TRACEPOINT: case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0) @@ -5907,11 +5915,13 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a attr->link_create.target_fd, attr->link_create.target_btf_id, attr->link_create.tracing.cookie, - attr->link_create.attach_type); + attr->link_create.attach_type, + log); break; case BPF_PROG_TYPE_LSM: case BPF_PROG_TYPE_TRACING: if (attr->link_create.attach_type != prog->expected_attach_type) { + bpf_log(log, "Invalid tracing attach_type.\n"); ret = -EINVAL; goto out; } @@ -5929,7 +5939,8 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a attr->link_create.target_fd, attr->link_create.target_btf_id, attr->link_create.tracing.cookie, - attr->link_create.attach_type); + attr->link_create.attach_type, + log); break; case BPF_PROG_TYPE_FLOW_DISSECTOR: case BPF_PROG_TYPE_SK_LOOKUP: -- 2.55.0