From: Alexei Starovoitov BPF_PROG_RUN compares ctx_size_in with max_ctx_offset of the program that it runs. The target of a tail call gets the same context: first: bpf_tail_call(ctx, ...) max_ctx_offset 0, ctx_size_in 8 target: r0 = *(u32 *)(ctx + 60000) out of bounds The target is not known before the run. Pad the copy of the context with zeros up to U16_MAX, no program can use more. kern_sys_bpf() makes no copy, so its caller has to pass that much. An extension cannot add tail calls to a program that has none. The copy is accounted now. It comes from kmalloc-cg or vmalloc, not from the caches of memdup_user(). Fixes: 79a7f8bdb159 ("bpf: Introduce bpf_sys_bpf() helper and program type.") Signed-off-by: Alexei Starovoitov --- kernel/bpf/syscall.c | 7 +++++++ net/bpf/test_run.c | 20 ++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index bf4a9fb8e2e0..86e5c9c38026 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3859,6 +3859,12 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, err = -EINVAL; goto out_unlock; } + /* The same for tail calls, see bpf_prog_test_run_syscall(). */ + if (prog->type == BPF_PROG_TYPE_EXT && tgt_prog->type == BPF_PROG_TYPE_SYSCALL && + prog->aux->tail_call_reachable && !tgt_prog->aux->tail_call_reachable) { + err = -EINVAL; + goto out_unlock; + } err = bpf_link_prime(&link->link.link, &link_primer); if (err) @@ -6668,6 +6674,7 @@ int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size) return PTR_ERR(prog); if (attr->test.ctx_size_in < prog->aux->max_ctx_offset || + (prog->aux->tail_call_reachable && attr->test.ctx_size_in < U16_MAX) || attr->test.ctx_size_in > U16_MAX) { bpf_prog_put(prog); return -EINVAL; diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c index d6c9dc62182e..cc3236e3c0c4 100644 --- a/net/bpf/test_run.c +++ b/net/bpf/test_run.c @@ -1687,8 +1687,8 @@ int bpf_prog_test_run_syscall(struct bpf_prog *prog, { void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in); __u32 ctx_size_in = kattr->test.ctx_size_in; + u32 retval, size = ctx_size_in; void *ctx = NULL; - u32 retval; int err = 0; /* doesn't support data_in/out, ctx_out, duration, or repeat or flags */ @@ -1702,10 +1702,18 @@ int bpf_prog_test_run_syscall(struct bpf_prog *prog, ctx_size_in > U16_MAX) return -EINVAL; - if (ctx_size_in) { - ctx = memdup_user(ctx_in, ctx_size_in); - if (IS_ERR(ctx)) - return PTR_ERR(ctx); + /* the target of a tail call can use that much */ + if (prog->aux->tail_call_reachable) + size = max_t(u32, size, U16_MAX); + + if (size) { + ctx = kvzalloc(size, GFP_KERNEL_ACCOUNT | __GFP_NOWARN); + if (!ctx) + return -ENOMEM; + if (copy_from_user(ctx, ctx_in, ctx_size_in)) { + err = -EFAULT; + goto out; + } } rcu_read_lock_trace(); @@ -1720,7 +1728,7 @@ int bpf_prog_test_run_syscall(struct bpf_prog *prog, if (copy_to_user(ctx_in, ctx, ctx_size_in)) err = -EFAULT; out: - kfree(ctx); + kvfree(ctx); return err; } -- 2.55.0