From nobody Wed Apr 1 09:43:21 2026 Received: from out-178.mta0.migadu.com (out-178.mta0.migadu.com [91.218.175.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9EA5F3DD525 for ; Tue, 31 Mar 2026 14:54:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774968879; cv=none; b=RLdTmkZichSewYfGxNzhwJ0gEySzkf2SrAGdaC00K/fIpKx6TbiCek3RQ/EwoKY7EfXVrHjSDT5sI0JtDbQ7uba+q+qRyTz69KtsYo/qEgTo+TcjHT+69ABIcdkcWJEauJcXSeSpFGLm1iH0AonB+9886cRG7wYl7Q/epf3bqF4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774968879; c=relaxed/simple; bh=wsizVL+qipW1oS+MnNqgqJiUL1L1zQMvNsK9MqvXbWo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZpjunLg6+NnSKcHf93dHuRQqP4Wc3QRidYcWxLbJ8RCfART3o3jEFbxXJx+/zt/FiX3v9/SRwelVvZ24mpRE62xHC+vCc/IreR7rJrdTXzzRLGaa0FHG6xDpgzJsyJlVaiHzI3b3aN9d+ae/b3AAS8pg1h4jPktDzx49rAh/I+c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=HGephv0u; arc=none smtp.client-ip=91.218.175.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="HGephv0u" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1774968875; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KDMByIchSdvZ1GTL3+7haZlQgAA8E2m7T8HepMwViZs=; b=HGephv0urUlmqblzp/1whguYJEFghFQPlCd2lLvJHYsxfi6q6lJ7GiUfhx78y8dwWJdJZA utb7zIxdcYJqZk3nCWKyQvc3eUcFebY3Xa4c05zmz+7kttgv15ZyUDdseSQZ9cNje5Nd2K hqhCefjrGJpHoG3f6mU6pSNSEvmC+hk= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Kumar Kartikeya Dwivedi , Song Liu , Yonghong Song , Jiri Olsa , Shuah Khan , Feng Yang , Leon Hwang , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [PATCH bpf-next v3 1/2] bpf: Fix abuse of kprobe_write_ctx via freplace Date: Tue, 31 Mar 2026 22:53:52 +0800 Message-ID: <20260331145353.87606-2-leon.hwang@linux.dev> In-Reply-To: <20260331145353.87606-1-leon.hwang@linux.dev> References: <20260331145353.87606-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" 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 =3D 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=3Dtrue via kprobe+freplace, disallow attaching freplace programs on kprobe programs with different kprobe_write_ctx values. Fixes: 7384893d970e ("bpf: Allow uprobe program to change context registers= ") Acked-by: Jiri Olsa Acked-by: Song Liu Signed-off-by: Leon Hwang --- kernel/bpf/syscall.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 51ade3cde8bb..e1505c9cd09e 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3733,6 +3733,23 @@ static int bpf_tracing_prog_attach(struct bpf_prog *= prog, tr =3D prog->aux->dst_trampoline; tgt_prog =3D prog->aux->dst_prog; } + /* + * It is to prevent modifying struct pt_regs via kprobe_write_ctx=3Dtrue + * freplace prog. Without this check, kprobe_write_ctx=3Dtrue freplace + * prog is allowed to attach to kprobe_write_ctx=3Dfalse kprobe prog, and + * then modify the registers of the kprobe prog's target kernel + * function. + * + * This also blocks the combination of uprobe+freplace, because it is + * unable to recognize the use of the tgt_prog as an uprobe or a kprobe + * by tgt_prog itself. At attach time, uprobe/kprobe is recognized by + * the target perf event flags in __perf_event_set_bpf_prog(). + */ + if (prog->type =3D=3D BPF_PROG_TYPE_EXT && + prog->aux->kprobe_write_ctx !=3D tgt_prog->aux->kprobe_write_ctx) { + err =3D -EINVAL; + goto out_unlock; + } =20 err =3D bpf_link_prime(&link->link.link, &link_primer); if (err) --=20 2.53.0 From nobody Wed Apr 1 09:43:21 2026 Received: from out-178.mta0.migadu.com (out-178.mta0.migadu.com [91.218.175.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2BAB31197B; Tue, 31 Mar 2026 14:54:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774968886; cv=none; b=c64W/JUMCFqfc/8HBeZQBtxI/T7+FNV+mLkiOp5KEPst9SkXXVmrNAY5Z13NpXOxQ4JmthtRo748osvi4gy2EuZh39zOXR6Sixd8Nb/vT6L5xhYoOgU+TG1Gf0sCjYLRJkBZsL8jN7dBxbjf4VQ8H/HzFQEHUOrIl9LcYi3bNuQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774968886; c=relaxed/simple; bh=x27V+IEw1MBBIM1+TXEfu6ygBI0xSx84AHvLZ+62gd4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=h5wedZVlPEUWFPEYQB0yCYOuHXqSyVwMncaY8iVs/xdIhg8rwSM82RZvqBMpWpnVubF3qIltcR87kY4r7G5FnZ5gQKOsCenboqupqtScQLOiVOehPryaUmhonBY5mG2ol4MRo9GG5psNKK9Dvskz8aaRVzP76BjaWnvZ0apZN4c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=vGSBbDxM; arc=none smtp.client-ip=91.218.175.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="vGSBbDxM" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1774968883; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SM+QVdZlIbNSt11DNnuXfk71/Inw0TohdIdeU6+Lf4A=; b=vGSBbDxM7oqt8qD6RQde/FOMv/QvHJqz9gony6PWjXtWWzkEUS5LCiBTzbjsag5TGDyrrj loZ+PoF79Gm2gFjl2i4ezhqRjqY60bOox9kQuhOa9/XJccVQtfJ4ci71TNYMbSU2SjixAt AONSrzKd9n1sZslaS3VGcK29A6x+Kqw= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Kumar Kartikeya Dwivedi , Song Liu , Yonghong Song , Jiri Olsa , Shuah Khan , Feng Yang , Leon Hwang , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [PATCH bpf-next v3 2/2] selftests/bpf: Add test to verify the fix of kprobe_write_ctx abuse Date: Tue, 31 Mar 2026 22:53:53 +0800 Message-ID: <20260331145353.87606-3-leon.hwang@linux.dev> In-Reply-To: <20260331145353.87606-1-leon.hwang@linux.dev> References: <20260331145353.87606-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Add a test to verify the issue: kprobe_write_ctx can be abused to modify struct pt_regs of kernel functions via kprobe_write_ctx=3Dtrue freplace progs. Without the fix, the issue is verified: kprobe_write_ctx=3Dtrue freplace prog is allowed to attach to kprobe_write_ctx=3Dfalse kprobe prog. Then, the first arg of bpf_fentry_test1 will be set as 0, and bpf_prog_test_run_opts() gets -EFAULT instead of 0. With the fix, the issue is rejected at attach time. Acked-by: Jiri Olsa Signed-off-by: Leon Hwang --- .../selftests/bpf/prog_tests/attach_probe.c | 64 +++++++++++++++++++ .../selftests/bpf/progs/kprobe_write_ctx.c | 19 ++++++ 2 files changed, 83 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/attach_probe.c b/tools/= testing/selftests/bpf/prog_tests/attach_probe.c index 9e77e5da7097..38852df70c0d 100644 --- a/tools/testing/selftests/bpf/prog_tests/attach_probe.c +++ b/tools/testing/selftests/bpf/prog_tests/attach_probe.c @@ -220,11 +220,73 @@ static void test_attach_kprobe_write_ctx(void) =20 kprobe_write_ctx__destroy(skel); } + +static void test_freplace_kprobe_write_ctx(void) +{ + struct bpf_program *prog_kprobe, *prog_ext, *prog_fentry; + struct kprobe_write_ctx *skel_kprobe, *skel_ext =3D NULL; + struct bpf_link *link_kprobe =3D NULL, *link_ext =3D NULL; + int err, prog_fd; + LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts); + LIBBPF_OPTS(bpf_test_run_opts, topts); + + skel_kprobe =3D kprobe_write_ctx__open(); + if (!ASSERT_OK_PTR(skel_kprobe, "kprobe_write_ctx__open kprobe")) + return; + + prog_kprobe =3D skel_kprobe->progs.kprobe_dummy; + bpf_program__set_autoload(prog_kprobe, true); + + prog_fentry =3D skel_kprobe->progs.fentry; + bpf_program__set_autoload(prog_fentry, true); + + err =3D kprobe_write_ctx__load(skel_kprobe); + if (!ASSERT_OK(err, "kprobe_write_ctx__load kprobe")) + goto out; + + skel_ext =3D kprobe_write_ctx__open(); + if (!ASSERT_OK_PTR(skel_ext, "kprobe_write_ctx__open ext")) + goto out; + + prog_ext =3D skel_ext->progs.freplace_kprobe; + bpf_program__set_autoload(prog_ext, true); + + prog_fd =3D bpf_program__fd(skel_kprobe->progs.kprobe_write_ctx); + bpf_program__set_attach_target(prog_ext, prog_fd, "kprobe_write_ctx"); + + err =3D kprobe_write_ctx__load(skel_ext); + if (!ASSERT_OK(err, "kprobe_write_ctx__load ext")) + goto out; + + prog_fd =3D bpf_program__fd(prog_kprobe); + link_ext =3D bpf_program__attach_freplace(prog_ext, prog_fd, "kprobe_dumm= y"); + ASSERT_ERR_PTR(link_ext, "bpf_program__attach_freplace link"); + ASSERT_EQ(libbpf_get_error(link_ext), -EINVAL, "bpf_program__attach_frepl= ace error"); + + link_kprobe =3D bpf_program__attach_kprobe_opts(prog_kprobe, "bpf_fentry_= test1", + &kprobe_opts); + if (!ASSERT_OK_PTR(link_kprobe, "bpf_program__attach_kprobe_opts")) + goto out; + + err =3D bpf_prog_test_run_opts(bpf_program__fd(prog_fentry), &topts); + ASSERT_OK(err, "bpf_prog_test_run_opts"); + +out: + bpf_link__destroy(link_ext); + bpf_link__destroy(link_kprobe); + kprobe_write_ctx__destroy(skel_ext); + kprobe_write_ctx__destroy(skel_kprobe); +} #else static void test_attach_kprobe_write_ctx(void) { test__skip(); } + +static void test_freplace_kprobe_write_ctx(void) +{ + test__skip(); +} #endif =20 static void test_attach_probe_auto(struct test_attach_probe *skel) @@ -434,6 +496,8 @@ void test_attach_probe(void) test_attach_kprobe_long_event_name(); if (test__start_subtest("kprobe-write-ctx")) test_attach_kprobe_write_ctx(); + if (test__start_subtest("freplace-kprobe-write-ctx")) + test_freplace_kprobe_write_ctx(); =20 cleanup: test_attach_probe__destroy(skel); diff --git a/tools/testing/selftests/bpf/progs/kprobe_write_ctx.c b/tools/t= esting/selftests/bpf/progs/kprobe_write_ctx.c index f77aef0474d3..adbf52afe490 100644 --- a/tools/testing/selftests/bpf/progs/kprobe_write_ctx.c +++ b/tools/testing/selftests/bpf/progs/kprobe_write_ctx.c @@ -19,4 +19,23 @@ int kprobe_multi_write_ctx(struct pt_regs *ctx) ctx->ax =3D 0; return 0; } + +SEC("?kprobe") +int kprobe_dummy(struct pt_regs *regs) +{ + return 0; +} + +SEC("?freplace") +int freplace_kprobe(struct pt_regs *regs) +{ + regs->di =3D 0; + return 0; +} + +SEC("?fentry/bpf_fentry_test1") +int BPF_PROG(fentry) +{ + return 0; +} #endif --=20 2.53.0