Skip to content

Commit b312cf4

Browse files
Asphalttgregkh
authored andcommitted
bpf: Fix abuse of kprobe_write_ctx via freplace
[ Upstream commit 611fe4b ] uprobe programs are allowed to modify struct pt_regs. Since the actual program type of uprobe is KPROBE, it can be abused to modify struct pt_regs via kprobe+freplace when the kprobe attaches to kernel functions. For example, SEC("?kprobe") int kprobe(struct pt_regs *regs) { return 0; } SEC("?freplace") int freplace_kprobe(struct pt_regs *regs) { regs->di = 0; return 0; } freplace_kprobe prog will attach to kprobe prog. kprobe prog will attach to a kernel function. Without this patch, when the kernel function runs, its first arg will always be set as 0 via the freplace_kprobe prog. To fix the abuse of kprobe_write_ctx=true via kprobe+freplace, disallow attaching freplace programs on kprobe programs with different kprobe_write_ctx values. Fixes: 7384893 ("bpf: Allow uprobe program to change context registers") Acked-by: Jiri Olsa <jolsa@kernel.org> Acked-by: Song Liu <song@kernel.org> Signed-off-by: Leon Hwang <leon.hwang@linux.dev> Link: https://lore.kernel.org/r/20260331145353.87606-2-leon.hwang@linux.dev Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent d41508a commit b312cf4

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

kernel/bpf/syscall.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3708,6 +3708,23 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
37083708
tr = prog->aux->dst_trampoline;
37093709
tgt_prog = prog->aux->dst_prog;
37103710
}
3711+
/*
3712+
* It is to prevent modifying struct pt_regs via kprobe_write_ctx=true
3713+
* freplace prog. Without this check, kprobe_write_ctx=true freplace
3714+
* prog is allowed to attach to kprobe_write_ctx=false kprobe prog, and
3715+
* then modify the registers of the kprobe prog's target kernel
3716+
* function.
3717+
*
3718+
* This also blocks the combination of uprobe+freplace, because it is
3719+
* unable to recognize the use of the tgt_prog as an uprobe or a kprobe
3720+
* by tgt_prog itself. At attach time, uprobe/kprobe is recognized by
3721+
* the target perf event flags in __perf_event_set_bpf_prog().
3722+
*/
3723+
if (prog->type == BPF_PROG_TYPE_EXT &&
3724+
prog->aux->kprobe_write_ctx != tgt_prog->aux->kprobe_write_ctx) {
3725+
err = -EINVAL;
3726+
goto out_unlock;
3727+
}
37113728

37123729
err = bpf_link_prime(&link->link.link, &link_primer);
37133730
if (err)

0 commit comments

Comments
 (0)