BPF_PROG_RUN for RAW_TRACEPOINT compares ctx_size_in with the
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 = *(u64 *)(ctx + 96) 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.
This is the raw_tp instance of commit 08c987904970 ("bpf: Give a
syscall program the largest context when it does tail calls"), which
applied the same fix to the syscall test_run path only. With KASAN
and BPF_JIT_KASAN the raw_tp path reports:
BUG: KASAN: slab-out-of-bounds in bpf_prog_..._target+0x2a/0x259
Read of size 8 at addr ffff88810486a818 by task rawtp_chain_leak/285
bpf_prog_..._target+0x2a/0x259
__bpf_prog_test_run_raw_tp+0x5b8/0x900
bpf_prog_test_run_raw_tp+0x42f/0x7e0
The buggy address belongs to the cache memdup_user-8 of size 8
The copy is accounted now. It comes from kmalloc-cg or vmalloc, not
from the caches of memdup_user().
Fixes: 04fd61ab36ec ("bpf: allow bpf programs to tail-call other bpf programs")
Signed-off-by: Pragyansh Chaturvedi <r41k…@ubuntu.com>
---
net/bpf/test_run.c | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)
diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
index 8b8c4e9ded9..31c4c656d39 100644
--- a/net/bpf/test_run.c
+++ b/net/bpf/test_run.c
@@ -782,6 +782,7 @@ int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
__u32 ctx_size_in = kattr->test.ctx_size_in;
struct bpf_raw_tp_test_run_info info;
int cpu = kattr->test.cpu, err = 0;
+ u32 size = ctx_size_in;
int current_cpu;
/* doesn't support data_in/out, ctx_out, duration, or repeat */
@@ -804,10 +805,18 @@ int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
if (prog->sleepable && (kattr->test.flags & BPF_F_TEST_RUN_ON_CPU))
return -EINVAL;
- if (ctx_size_in) {
- info.ctx = memdup_user(ctx_in, ctx_size_in);
- if (IS_ERR(info.ctx))
- return PTR_ERR(info.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) {
+ info.ctx = kvzalloc(size, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!info.ctx)
+ return -ENOMEM;
+ if (copy_from_user(info.ctx, ctx_in, ctx_size_in)) {
+ err = -EFAULT;
+ goto out;
+ }
} else {
info.ctx = NULL;
}
@@ -842,7 +851,8 @@ int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32)))
err = -EFAULT;
- kfree(info.ctx);
+out:
+ kvfree(info.ctx);
return err;
}
--
2.53.0