From nobody Wed Sep 10 01:43:01 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 7FF4E2DC34B; Tue, 9 Sep 2025 12:39:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757421561; cv=none; b=gdS1Tqr4wo9naVSqWbLGIa8CItbQIw5BZYdWBPwYWZ+f6Z9uLg5ywgGrWsA8fh4isyXhWMYx7VBEnnElf1Puifjm+VizcdSjgsr4g5Ga1cfU1nE4WfyR+yq7G4Wd30FtqF2dt5QfddYGEG/PNhN46C64cMfg7y1tZI6asyJXs+A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757421561; c=relaxed/simple; bh=QoVbv3Gt4+8mnAmP5Qd017V/bTQvkbHe423VRjfq6Z4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=srvk0IvEAtCWcG74k7j3mh5lA93PIrVVuubrvROn8SX6wpQIYOIqfxOVBmzuxXkVUFW3QpsE1HPeQjY8nthVXjHdtgK0ZXS2Jda/1B9n/o7URJKP4uN0rF4pKJhGVrJubol3e82sXTcWsyYEAuzEn1jZHCtzM0J0QuJ589err1g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Cf6cGQ2u; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Cf6cGQ2u" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EB777C4CEF4; Tue, 9 Sep 2025 12:39:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1757421561; bh=QoVbv3Gt4+8mnAmP5Qd017V/bTQvkbHe423VRjfq6Z4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Cf6cGQ2uNa4dIlsaDafSOxvdSDJTd2RsLEU9IyvqwnqpOa9YEBT8F5hv4faVq0AAz pcu/hCGdKfPRGUmaCjUsfwweD7be+IruIPxs+76u+KX3cF1qn3tSgcOGMEL+AdNfw3 CcbRSxv/JLlkIxvRSbJvYreEijfYLa4wffjytXF+NGIpw1JpLrnIeFMFGuHFUnU1zt nV3RA+BhiRk5DQ8wTFy78ae4WUQ1xQ+P614mZ8YxUD68iNRlGCLWJ/dRqcPv0ltwGT FZiQd9a2iK3seQAubRAjtyO48IbQvzK3a15qnP+9iSVGPTgGVy2OegpFLWtdcYCCcM hYF1i2DRvDISg== From: Jiri Olsa To: Oleg Nesterov , Masami Hiramatsu , Peter Zijlstra , Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org, Song Liu , Yonghong Song , John Fastabend , Hao Luo , Steven Rostedt , Ingo Molnar Subject: [PATCHv3 perf/core 1/6] bpf: Allow uprobe program to change context registers Date: Tue, 9 Sep 2025 14:38:52 +0200 Message-ID: <20250909123857.315599-2-jolsa@kernel.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250909123857.315599-1-jolsa@kernel.org> References: <20250909123857.315599-1-jolsa@kernel.org> 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 Content-Type: text/plain; charset="utf-8" Currently uprobe (BPF_PROG_TYPE_KPROBE) program can't write to the context registers data. While this makes sense for kprobe attachments, for uprobe attachment it might make sense to be able to change user space registers to alter application execution. Since uprobe and kprobe programs share the same type (BPF_PROG_TYPE_KPROBE), we can't deny write access to context during the program load. We need to check on it during program attachment to see if it's going to be kprobe or uprobe. Storing the program's write attempt to context and checking on it during the attachment. Signed-off-by: Jiri Olsa Acked-by: Andrii Nakryiko --- include/linux/bpf.h | 1 + kernel/events/core.c | 4 ++++ kernel/trace/bpf_trace.c | 7 +++++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index cc700925b802..404a30cde84e 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1619,6 +1619,7 @@ struct bpf_prog_aux { bool priv_stack_requested; bool changes_pkt_data; bool might_sleep; + bool kprobe_write_ctx; u64 prog_array_member_cnt; /* counts how many times as member of prog_arr= ay */ struct mutex ext_mutex; /* mutex for is_extended and prog_array_member_cn= t */ struct bpf_arena *arena; diff --git a/kernel/events/core.c b/kernel/events/core.c index 28de3baff792..c3f37b266fc4 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -11238,6 +11238,10 @@ static int __perf_event_set_bpf_prog(struct perf_e= vent *event, if (prog->kprobe_override && !is_kprobe) return -EINVAL; =20 + /* Writing to context allowed only for uprobes. */ + if (prog->aux->kprobe_write_ctx && !is_uprobe) + return -EINVAL; + if (is_tracepoint || is_syscall_tp) { int off =3D trace_event_get_offsets(event->tp_event); =20 diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 3ae52978cae6..dfb19e773afa 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1521,8 +1521,6 @@ static bool kprobe_prog_is_valid_access(int off, int = size, enum bpf_access_type { if (off < 0 || off >=3D sizeof(struct pt_regs)) return false; - if (type !=3D BPF_READ) - return false; if (off % size !=3D 0) return false; /* @@ -1532,6 +1530,7 @@ static bool kprobe_prog_is_valid_access(int off, int = size, enum bpf_access_type if (off + size > sizeof(struct pt_regs)) return false; =20 + prog->aux->kprobe_write_ctx |=3D type =3D=3D BPF_WRITE; return true; } =20 @@ -2913,6 +2912,10 @@ int bpf_kprobe_multi_link_attach(const union bpf_att= r *attr, struct bpf_prog *pr if (!is_kprobe_multi(prog)) return -EINVAL; =20 + /* Writing to context is not allowed for kprobes. */ + if (prog->aux->kprobe_write_ctx) + return -EINVAL; + flags =3D attr->link_create.kprobe_multi.flags; if (flags & ~BPF_F_KPROBE_MULTI_RETURN) return -EINVAL; --=20 2.51.0 From nobody Wed Sep 10 01:43:01 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 AFAE533439A; Tue, 9 Sep 2025 12:39:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757421571; cv=none; b=O8DsAP87ilkbZyyM4A0QBL/RfkK74hCAEdpxF7YWiAAKlKk0kQs4dWYxTSnIGH0S7hieBK87cgJUitrxOI1WnbCU4sjqkATI0LvpcJxJO7QEKmIeYHpWRmuHUnTs7E9663gnWZWl//se3ZcdogJ5bhFNrB/BgKec5K+Dn9HJW/U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757421571; c=relaxed/simple; bh=TS50jOjzvt6IH8eaBLVaBZO88XVBkMIDXW/tc9a1LfM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pH02QZL6iyJ0Ju2EPodkP2QnDBZE8z1L0rWVHh1QIzJGPNPOBBmPW067XIW0ZuU//4+b8JMMXsnojWM/CmiQ8fqSM3zt0B0uoEn2XwmALGeDi8pkkiitPw8IsKLWcL0Kx0GreFHqrEDrh+PKDEJdkv2EQJYmMHtqsVNzYLu9WRk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DTPKekDM; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DTPKekDM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 64DFDC4CEF4; Tue, 9 Sep 2025 12:39:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1757421571; bh=TS50jOjzvt6IH8eaBLVaBZO88XVBkMIDXW/tc9a1LfM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DTPKekDMA6rEHhwfa6DytnkJTyObiVmz72S+eJuJsN9fpe73rE+8+ibXPxXCYUKFr cv/ORGCZFtyW1z9WFL7MIn5CnVblx0u4odGnf5lddpt/hWHCCe14bGmNbl6W1qehMA 4aB9E8Gh9FkKk0maGSr9lysAloWJ5tlDA0NenPWp8misvqjpgwUtIWOZyASekQZGEs iuwd0wxlMhUjLBfKpM/10JpJQ1GbOovk8UUzVDWP2U42/tW8KR+TTnYyC2T7XfBR1K 8iP6vFC1JVcEVxrBjg/POKKCFn81m+H285d4GOioUqE8kfhk7RkDXefwFtNDhz9Mf7 1tAG3eAycUSaQ== From: Jiri Olsa To: Oleg Nesterov , Masami Hiramatsu , Peter Zijlstra , Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org, Song Liu , Yonghong Song , John Fastabend , Hao Luo , Steven Rostedt , Ingo Molnar Subject: [PATCHv3 perf/core 2/6] uprobe: Do not emulate/sstep original instruction when ip is changed Date: Tue, 9 Sep 2025 14:38:53 +0200 Message-ID: <20250909123857.315599-3-jolsa@kernel.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250909123857.315599-1-jolsa@kernel.org> References: <20250909123857.315599-1-jolsa@kernel.org> 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 Content-Type: text/plain; charset="utf-8" If uprobe handler changes instruction pointer we still execute single step) or emulate the original instruction and increment the (new) ip with its length. This makes the new instruction pointer bogus and application will likely crash on illegal instruction execution. If user decided to take execution elsewhere, it makes little sense to execute the original instruction, so let's skip it. Acked-by: Oleg Nesterov Signed-off-by: Jiri Olsa Acked-by: Andrii Nakryiko --- kernel/events/uprobes.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 996a81080d56..4f46018e507e 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -2768,6 +2768,13 @@ static void handle_swbp(struct pt_regs *regs) /* Try to optimize after first hit. */ arch_uprobe_optimize(&uprobe->arch, bp_vaddr); =20 + /* + * If user decided to take execution elsewhere, it makes little sense + * to execute the original instruction, so let's skip it. + */ + if (instruction_pointer(regs) !=3D bp_vaddr) + goto out; + if (arch_uprobe_skip_sstep(&uprobe->arch, regs)) goto out; =20 --=20 2.51.0 From nobody Wed Sep 10 01:43:01 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 4F30A32CF9E; Tue, 9 Sep 2025 12:39:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757421582; cv=none; b=eb2uYRRR5R7WtFDZzGVvXKpcd+WLH24lgUpgVOfH+CFzLL3Di3XanNZnTUnQh68w5LWG8LUF8g1tGWvzqV/EyMIEmwbG3JZDYhU50a9XL6Catjd8wR/V9jk9dmWGA+qOHHgFyiv52JYJjs2aNjXk2t3sgS7dJavfpRSwN+SDeiY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757421582; c=relaxed/simple; bh=3NANpKXl+pfp3BrIntDBj/EVTt9o2TzdBlkEWj5GkFY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=g+rOtGjRQAdj1X5KBCSDxjS1yMFR+NpH3YeI0zaS3CBssfz1jvWLI4xL48d/FEt2IckdaW0ZnElnHe9wXxpCdCpmujf9v2BJrdAujBECxr9ZVfF8O0jOryi2yDt22Z7uwRKfHcpadhUli0hCq8XO4gBoZd3x5t/MI2ZA5un/Osk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=O6CSxpcy; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="O6CSxpcy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B1060C4CEF5; Tue, 9 Sep 2025 12:39:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1757421581; bh=3NANpKXl+pfp3BrIntDBj/EVTt9o2TzdBlkEWj5GkFY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O6CSxpcyHCtn7Q0cGRJaPvX1I+pT75V5hGmNgTnGfGIqMSkXLeqLz5M+0OTM31pIv OEPXDNU//QvGgQl2R4mRT+SaRUcqURyKwz/QJ/Ro7S5uqPkWT0LlL5SlPVEaGvtg+x YDTKFI54quew+sIDd0WglYhj6sVUujicwKzgDWZPrtPy7Td+CPHvs8iAqfxI3HLVYR ioAG5nNLpUpWqKESnztrt9Mjh7ka+gaWYuQPY46q/z241Lx+tMaCb44zTX+0N6nKZZ sp5Cph4SXCTVhCUdBQDMmBqt7ZeBa6kpwrrIl7LgeZcqk4+zuq8biBk9pQXi5/Ruub 76hSXofoZfi+Q== From: Jiri Olsa To: Oleg Nesterov , Masami Hiramatsu , Peter Zijlstra , Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org, Song Liu , Yonghong Song , John Fastabend , Hao Luo , Steven Rostedt , Ingo Molnar Subject: [PATCHv3 perf/core 3/6] selftests/bpf: Add uprobe context registers changes test Date: Tue, 9 Sep 2025 14:38:54 +0200 Message-ID: <20250909123857.315599-4-jolsa@kernel.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250909123857.315599-1-jolsa@kernel.org> References: <20250909123857.315599-1-jolsa@kernel.org> 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 Content-Type: text/plain; charset="utf-8" Adding test to check we can change common register values through uprobe program. It's x86_64 specific test. Signed-off-by: Jiri Olsa Acked-by: Andrii Nakryiko --- .../testing/selftests/bpf/prog_tests/uprobe.c | 114 +++++++++++++++++- .../testing/selftests/bpf/progs/test_uprobe.c | 24 ++++ 2 files changed, 137 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe.c b/tools/testin= g/selftests/bpf/prog_tests/uprobe.c index cf3e0e7a64fa..19dd900df188 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe.c @@ -2,6 +2,7 @@ /* Copyright (c) 2023 Hengqi Chen */ =20 #include +#include #include "test_uprobe.skel.h" =20 static FILE *urand_spawn(int *pid) @@ -33,7 +34,7 @@ static int urand_trigger(FILE **urand_pipe) return exit_code; } =20 -void test_uprobe(void) +static void test_uprobe_attach(void) { LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts); struct test_uprobe *skel; @@ -93,3 +94,114 @@ void test_uprobe(void) pclose(urand_pipe); test_uprobe__destroy(skel); } + +#ifdef __x86_64__ +__naked __maybe_unused unsigned long uprobe_regs_change_trigger(void) +{ + asm volatile ( + "ret\n" + ); +} + +static __naked void uprobe_regs_change(struct pt_regs *before, struct pt_r= egs *after) +{ + asm volatile ( + "movq %r11, 48(%rdi)\n" + "movq %r10, 56(%rdi)\n" + "movq %r9, 64(%rdi)\n" + "movq %r8, 72(%rdi)\n" + "movq %rax, 80(%rdi)\n" + "movq %rcx, 88(%rdi)\n" + "movq %rdx, 96(%rdi)\n" + "movq %rsi, 104(%rdi)\n" + "movq %rdi, 112(%rdi)\n" + + /* save 2nd argument */ + "pushq %rsi\n" + "call uprobe_regs_change_trigger\n" + + /* save return value and load 2nd argument pointer to rax */ + "pushq %rax\n" + "movq 8(%rsp), %rax\n" + + "movq %r11, 48(%rax)\n" + "movq %r10, 56(%rax)\n" + "movq %r9, 64(%rax)\n" + "movq %r8, 72(%rax)\n" + "movq %rcx, 88(%rax)\n" + "movq %rdx, 96(%rax)\n" + "movq %rsi, 104(%rax)\n" + "movq %rdi, 112(%rax)\n" + + /* restore return value and 2nd argument */ + "pop %rax\n" + "pop %rsi\n" + + "movq %rax, 80(%rsi)\n" + "ret\n" + ); +} + +static void regs_common(void) +{ + struct pt_regs before =3D {}, after =3D {}, expected =3D { + .rax =3D 0xc0ffe, + .rcx =3D 0xbad, + .rdx =3D 0xdead, + .r8 =3D 0x8, + .r9 =3D 0x9, + .r10 =3D 0x10, + .r11 =3D 0x11, + .rdi =3D 0x12, + .rsi =3D 0x13, + }; + LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts); + struct test_uprobe *skel; + + skel =3D test_uprobe__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + skel->bss->my_pid =3D getpid(); + skel->bss->regs =3D expected; + + uprobe_opts.func_name =3D "uprobe_regs_change_trigger"; + skel->links.test_regs_change =3D bpf_program__attach_uprobe_opts(skel->pr= ogs.test_regs_change, + -1, + "/proc/self/exe", + 0 /* offset */, + &uprobe_opts); + if (!ASSERT_OK_PTR(skel->links.test_regs_change, "bpf_program__attach_upr= obe_opts")) + goto cleanup; + + uprobe_regs_change(&before, &after); + + ASSERT_EQ(after.rax, expected.rax, "ax"); + ASSERT_EQ(after.rcx, expected.rcx, "cx"); + ASSERT_EQ(after.rdx, expected.rdx, "dx"); + ASSERT_EQ(after.r8, expected.r8, "r8"); + ASSERT_EQ(after.r9, expected.r9, "r9"); + ASSERT_EQ(after.r10, expected.r10, "r10"); + ASSERT_EQ(after.r11, expected.r11, "r11"); + ASSERT_EQ(after.rdi, expected.rdi, "rdi"); + ASSERT_EQ(after.rsi, expected.rsi, "rsi"); + +cleanup: + test_uprobe__destroy(skel); +} + +static void test_uprobe_regs_change(void) +{ + if (test__start_subtest("regs_change_common")) + regs_common(); +} +#else +static void test_uprobe_regs_change(void) { } +#endif + +void test_uprobe(void) +{ + if (test__start_subtest("attach")) + test_uprobe_attach(); + test_uprobe_regs_change(); +} diff --git a/tools/testing/selftests/bpf/progs/test_uprobe.c b/tools/testin= g/selftests/bpf/progs/test_uprobe.c index 896c88a4960d..9437bd76a437 100644 --- a/tools/testing/selftests/bpf/progs/test_uprobe.c +++ b/tools/testing/selftests/bpf/progs/test_uprobe.c @@ -59,3 +59,27 @@ int BPF_UPROBE(test4) test4_result =3D 1; return 0; } + +#if defined(__TARGET_ARCH_x86) +struct pt_regs regs; + +SEC("uprobe") +int BPF_UPROBE(test_regs_change) +{ + pid_t pid =3D bpf_get_current_pid_tgid() >> 32; + + if (pid !=3D my_pid) + return 0; + + ctx->ax =3D regs.ax; + ctx->cx =3D regs.cx; + ctx->dx =3D regs.dx; + ctx->r8 =3D regs.r8; + ctx->r9 =3D regs.r9; + ctx->r10 =3D regs.r10; + ctx->r11 =3D regs.r11; + ctx->di =3D regs.di; + ctx->si =3D regs.si; + return 0; +} +#endif --=20 2.51.0 From nobody Wed Sep 10 01:43:01 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 56294322A0C; Tue, 9 Sep 2025 12:39:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757421592; cv=none; b=YM2ZwjYtI9F2Hf/KRWNjkn8lGe1NAvNu6V9pjN5TAbrML2pNAWWb785oik+U51ed57+TGulRonH6arPw7VfLBtT0y4whVfaXa0N02UJKfjpKn5YLgrcR1QW/Fy2LxEvs4HWCK815GMBVFHeWNFyQZw09KUs3uqVZZyu7A4ZlCJU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757421592; c=relaxed/simple; bh=AVpuHvvS5I5LtGxHyezfUbjP3WODclgFHrgBZgJ6/k0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=V+ygXvmAEqdsllRehkH4g8yv6+MqqwbiShmQuH9USbIPzURF0x59c4hlBBJnjc86rkLiS4GPEdTGxP3eTbxuzTghfIDt3PLw2qI5bIiGwfNtYqgEI3JT4Bk213Y3tp6kIyrJffSduRFd1JQXAbliqrXn+szqD+9fXSFc7qg6q8M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bSbqfGNj; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bSbqfGNj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C35FBC4CEF4; Tue, 9 Sep 2025 12:39:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1757421591; bh=AVpuHvvS5I5LtGxHyezfUbjP3WODclgFHrgBZgJ6/k0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bSbqfGNjVgnjBNay74JFJXdt4hR220qTaB+i61gQeMT7TCkPnRB7s9ORfSA8/zDKg UOGOD59ySe07cmQ2hfI3rCKhqdPQKCDj1P8+eIWHIKtOuQjmbeC0/LEdw+IH2wUmX5 qFrdZdZ7thC8KkpnBvSkeG4c0gCgs0tvHqtM5FB1kr5JPrH5BX0NqehL2dJ9hIiMup Ob7xS7hNm1KokVHidibVy0G86rzDgzvJmGM1QOL4Pyo3DCghqAQ2aCfh5P0bxzI5Jl XuC/kyRv4IERQe3wU0StBGGUVeUz+4n94bucFaFDGTQIM6eLrrpiyuv+4hMrcScEzW l0UAdGiU/butQ== From: Jiri Olsa To: Oleg Nesterov , Masami Hiramatsu , Peter Zijlstra , Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org, Song Liu , Yonghong Song , John Fastabend , Hao Luo , Steven Rostedt , Ingo Molnar Subject: [PATCHv3 perf/core 4/6] selftests/bpf: Add uprobe context ip register change test Date: Tue, 9 Sep 2025 14:38:55 +0200 Message-ID: <20250909123857.315599-5-jolsa@kernel.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250909123857.315599-1-jolsa@kernel.org> References: <20250909123857.315599-1-jolsa@kernel.org> 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 Content-Type: text/plain; charset="utf-8" Adding test to check we can change the application execution through instruction pointer change through uprobe program. It's x86_64 specific test. Signed-off-by: Jiri Olsa Acked-by: Andrii Nakryiko --- .../testing/selftests/bpf/prog_tests/uprobe.c | 42 +++++++++++++++++++ .../testing/selftests/bpf/progs/test_uprobe.c | 14 +++++++ 2 files changed, 56 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe.c b/tools/testin= g/selftests/bpf/prog_tests/uprobe.c index 19dd900df188..86404476c1da 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe.c @@ -190,10 +190,52 @@ static void regs_common(void) test_uprobe__destroy(skel); } =20 +static noinline unsigned long uprobe_regs_change_ip_1(void) +{ + return 0xc0ffee; +} + +static noinline unsigned long uprobe_regs_change_ip_2(void) +{ + return 0xdeadbeef; +} + +static void regs_ip(void) +{ + LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts); + struct test_uprobe *skel; + unsigned long ret; + + skel =3D test_uprobe__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + skel->bss->my_pid =3D getpid(); + skel->bss->ip =3D (unsigned long) uprobe_regs_change_ip_2; + + uprobe_opts.func_name =3D "uprobe_regs_change_ip_1"; + skel->links.test_regs_change_ip =3D bpf_program__attach_uprobe_opts( + skel->progs.test_regs_change_ip, + -1, + "/proc/self/exe", + 0 /* offset */, + &uprobe_opts); + if (!ASSERT_OK_PTR(skel->links.test_regs_change_ip, "bpf_program__attach_= uprobe_opts")) + goto cleanup; + + ret =3D uprobe_regs_change_ip_1(); + ASSERT_EQ(ret, 0xdeadbeef, "ret"); + +cleanup: + test_uprobe__destroy(skel); +} + static void test_uprobe_regs_change(void) { if (test__start_subtest("regs_change_common")) regs_common(); + if (test__start_subtest("regs_change_ip")) + regs_ip(); } #else static void test_uprobe_regs_change(void) { } diff --git a/tools/testing/selftests/bpf/progs/test_uprobe.c b/tools/testin= g/selftests/bpf/progs/test_uprobe.c index 9437bd76a437..12f4065fca20 100644 --- a/tools/testing/selftests/bpf/progs/test_uprobe.c +++ b/tools/testing/selftests/bpf/progs/test_uprobe.c @@ -82,4 +82,18 @@ int BPF_UPROBE(test_regs_change) ctx->si =3D regs.si; return 0; } + +unsigned long ip; + +SEC("uprobe") +int BPF_UPROBE(test_regs_change_ip) +{ + pid_t pid =3D bpf_get_current_pid_tgid() >> 32; + + if (pid !=3D my_pid) + return 0; + + ctx->ip =3D ip; + return 0; +} #endif --=20 2.51.0 From nobody Wed Sep 10 01:43:01 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 A0A5433471A; Tue, 9 Sep 2025 12:40:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757421602; cv=none; b=pqRXcUoeyuvN0K5UcIZ4rUrj37phBx1Hnw4R57wKsqml4yrJy6rXC7yXWQOkbc2Jif/aB3dCij1hrzQQVYBHDpDniPTymUwRI0NMVgpBXbfwuBvlv+AKdPMDxr3ZDAvj1VYOQZGgW/oiKniOsURu+TLhQOghvwGaRBy3vjcKhw0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757421602; c=relaxed/simple; bh=DcRm/QuIJ29/54z6kYgqyNeqOZ05ZpUkyeEHfEwnpc4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tlN0EfMnox2w0nXm9Mz590iHtcIoqNYQmpJg0MiE5RJhicudmmZCG9+dh5o3Mi92pwXWglVDmFtY251LTrJ5Tnfd5o9jKRpeSm8gFmyIrHBVSRhi7PLfuE3SpdVm6tyjSE8oRD+L6FKk+gHzxcQ46t5qcdDZrGMzxcFMUhW27aM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hVmlTsvs; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hVmlTsvs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 17217C4CEF4; Tue, 9 Sep 2025 12:39:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1757421602; bh=DcRm/QuIJ29/54z6kYgqyNeqOZ05ZpUkyeEHfEwnpc4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hVmlTsvssKVPqgCV3pY2c2IMxY6p7Bk65CBfDMopoTpW5bm04o8SYkJ6Uc1SGwFn6 O0BLqJhoSsvr06GLmNuslZhLE1+phByuLLBqDe8/BbU+r26grCc/XNtjkV/0sVIgc+ oVOryjY7aHSXGjTaM/Vh/D9fayIsRRjXbKbquqfaI9HhTIXoa4yMXorIUHwUY3txS+ E4mnP3yQ2f+X0hRdzboi67sRMcGnxKyS5f0N5exbYU5Rw1xbw/WdgzOJr+G1uu3I76 dkUaxsYo2CtQw0QU4CUnInvxd/39jWXuvr+f0PeOLKB9LsWmbM8Qqc0VV0fQ87wXKq CnMEepYqbHS9g== From: Jiri Olsa To: Oleg Nesterov , Masami Hiramatsu , Peter Zijlstra , Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org, Song Liu , Yonghong Song , John Fastabend , Hao Luo , Steven Rostedt , Ingo Molnar Subject: [PATCHv3 perf/core 5/6] selftests/bpf: Add kprobe write ctx attach test Date: Tue, 9 Sep 2025 14:38:56 +0200 Message-ID: <20250909123857.315599-6-jolsa@kernel.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250909123857.315599-1-jolsa@kernel.org> References: <20250909123857.315599-1-jolsa@kernel.org> 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 Content-Type: text/plain; charset="utf-8" Adding test to check we can't attach standard kprobe program that writes to the context. It's x86_64 specific test. Signed-off-by: Jiri Olsa Acked-by: Andrii Nakryiko --- .../selftests/bpf/prog_tests/attach_probe.c | 28 +++++++++++++++++++ .../selftests/bpf/progs/kprobe_write_ctx.c | 15 ++++++++++ 2 files changed, 43 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/kprobe_write_ctx.c diff --git a/tools/testing/selftests/bpf/prog_tests/attach_probe.c b/tools/= testing/selftests/bpf/prog_tests/attach_probe.c index cabc51c2ca6b..9e77e5da7097 100644 --- a/tools/testing/selftests/bpf/prog_tests/attach_probe.c +++ b/tools/testing/selftests/bpf/prog_tests/attach_probe.c @@ -3,6 +3,7 @@ #include "test_attach_kprobe_sleepable.skel.h" #include "test_attach_probe_manual.skel.h" #include "test_attach_probe.skel.h" +#include "kprobe_write_ctx.skel.h" =20 /* this is how USDT semaphore is actually defined, except volatile modifie= r */ volatile unsigned short uprobe_ref_ctr __attribute__((unused)) __attribute= ((section(".probes"))); @@ -201,6 +202,31 @@ static void test_attach_kprobe_long_event_name(void) test_attach_probe_manual__destroy(skel); } =20 +#ifdef __x86_64__ +/* attach kprobe/kretprobe long event name testings */ +static void test_attach_kprobe_write_ctx(void) +{ + struct kprobe_write_ctx *skel =3D NULL; + struct bpf_link *link =3D NULL; + + skel =3D kprobe_write_ctx__open_and_load(); + if (!ASSERT_OK_PTR(skel, "kprobe_write_ctx__open_and_load")) + return; + + link =3D bpf_program__attach_kprobe_opts(skel->progs.kprobe_write_ctx, + "bpf_fentry_test1", NULL); + if (!ASSERT_ERR_PTR(link, "bpf_program__attach_kprobe_opts")) + bpf_link__destroy(link); + + kprobe_write_ctx__destroy(skel); +} +#else +static void test_attach_kprobe_write_ctx(void) +{ + test__skip(); +} +#endif + static void test_attach_probe_auto(struct test_attach_probe *skel) { struct bpf_link *uprobe_err_link; @@ -406,6 +432,8 @@ void test_attach_probe(void) test_attach_uprobe_long_event_name(); if (test__start_subtest("kprobe-long_name")) test_attach_kprobe_long_event_name(); + if (test__start_subtest("kprobe-write-ctx")) + test_attach_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 new file mode 100644 index 000000000000..4621a5bef4e2 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/kprobe_write_ctx.c @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vmlinux.h" +#include +#include + +char _license[] SEC("license") =3D "GPL"; + +#if defined(__TARGET_ARCH_x86) +SEC("kprobe") +int kprobe_write_ctx(struct pt_regs *ctx) +{ + ctx->ax =3D 0; + return 0; +} +#endif --=20 2.51.0 From nobody Wed Sep 10 01:43:01 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 1E44132A3D7; Tue, 9 Sep 2025 12:40:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757421613; cv=none; b=upQYzFJXJAUM81PL2Oln+JGy6y8Cg9HXcB8/gileAPdaS6qKqbjIxKUjpVJfduZrMRm+XuUS813viUaR6K9SXUCIV+atRDm9f8VBrGKkhAn+gNQpEHJPb8mD/wuMxmZKGqLswP71wCEeJ6Oe/aE3q6jjcsh/rIrFV+oZU7ZxxUs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757421613; c=relaxed/simple; bh=E0pyzzXc5ywisIehqLd9QVqn8ohz0R6l3t8a5bN6GPQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gjuqdS9xrcK+Ysv1OpfDwdxgtPo7Zh5JYsC+RCCc35k0CMa+u4E2O9xwQ7mlO5HadeJQhGC6DIcBfRU4xqk948zUoRNYBdLkyE5C7U2gO8duGLOZzt98qUYUtFHXiqniYK5tKximFgE9cHRq25TCpt4wR9Gs3Azan3bEwJmr22s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NdpnaDqS; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NdpnaDqS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7B11DC4CEF4; Tue, 9 Sep 2025 12:40:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1757421612; bh=E0pyzzXc5ywisIehqLd9QVqn8ohz0R6l3t8a5bN6GPQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NdpnaDqSV+cIrUEa0xp48dPO9/HLwy2WjTn/gdyoiBwiV03w8my8dnDSlWtX5pcy0 tKUuU78gwaXUDoL5kHeSumgsXw5rGiWb8FuRqjOqQFHQypt/ZsAF87cu0iLh4XPMVB oKatSJuVKW3KvmOzHN+T0xqYen9FVEstV4fWndAFoyoGhh76EcmAnEp44sbGMeDd6W NmwCYWVpnoV6tTCJdlWXWxnqhmEX88MW3bCXhH4y8I4bjUsDdR37Ssaeb5R/uRrqd+ vFkW7+0oxAhr9y7tHWSEQpF/u42j5BVrfki6M31XSlMEkTUi6RX0QQpp7FH+KfpWPH 1uyWd42IlBOqw== From: Jiri Olsa To: Oleg Nesterov , Masami Hiramatsu , Peter Zijlstra , Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org, Song Liu , Yonghong Song , John Fastabend , Hao Luo , Steven Rostedt , Ingo Molnar Subject: [PATCHv3 perf/core 6/6] selftests/bpf: Add kprobe multi write ctx attach test Date: Tue, 9 Sep 2025 14:38:57 +0200 Message-ID: <20250909123857.315599-7-jolsa@kernel.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250909123857.315599-1-jolsa@kernel.org> References: <20250909123857.315599-1-jolsa@kernel.org> 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 Content-Type: text/plain; charset="utf-8" Adding test to check we can't attach kprobe multi program that writes to the context. It's x86_64 specific test. Signed-off-by: Jiri Olsa Acked-by: Andrii Nakryiko --- .../bpf/prog_tests/kprobe_multi_test.c | 27 +++++++++++++++++++ .../selftests/bpf/progs/kprobe_write_ctx.c | 7 +++++ 2 files changed, 34 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c b/t= ools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c index e19ef509ebf8..bc52389217e2 100644 --- a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +++ b/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c @@ -7,6 +7,7 @@ #include "kprobe_multi_session.skel.h" #include "kprobe_multi_session_cookie.skel.h" #include "kprobe_multi_verifier.skel.h" +#include "kprobe_write_ctx.skel.h" #include "bpf/libbpf_internal.h" #include "bpf/hashmap.h" =20 @@ -753,6 +754,30 @@ static void test_attach_override(void) kprobe_multi_override__destroy(skel); } =20 +#ifdef __x86_64__ +static void test_attach_write_ctx(void) +{ + struct kprobe_write_ctx *skel =3D NULL; + struct bpf_link *link =3D NULL; + + skel =3D kprobe_write_ctx__open_and_load(); + if (!ASSERT_OK_PTR(skel, "kprobe_write_ctx__open_and_load")) + return; + + link =3D bpf_program__attach_kprobe_opts(skel->progs.kprobe_multi_write_c= tx, + "bpf_fentry_test1", NULL); + if (!ASSERT_ERR_PTR(link, "bpf_program__attach_kprobe_opts")) + bpf_link__destroy(link); + + kprobe_write_ctx__destroy(skel); +} +#else +static void test_attach_write_ctx(void) +{ + test__skip(); +} +#endif + void serial_test_kprobe_multi_bench_attach(void) { if (test__start_subtest("kernel")) @@ -792,5 +817,7 @@ void test_kprobe_multi_test(void) test_session_cookie_skel_api(); if (test__start_subtest("unique_match")) test_unique_match(); + if (test__start_subtest("attach_write_ctx")) + test_attach_write_ctx(); RUN_TESTS(kprobe_multi_verifier); } diff --git a/tools/testing/selftests/bpf/progs/kprobe_write_ctx.c b/tools/t= esting/selftests/bpf/progs/kprobe_write_ctx.c index 4621a5bef4e2..f77aef0474d3 100644 --- a/tools/testing/selftests/bpf/progs/kprobe_write_ctx.c +++ b/tools/testing/selftests/bpf/progs/kprobe_write_ctx.c @@ -12,4 +12,11 @@ int kprobe_write_ctx(struct pt_regs *ctx) ctx->ax =3D 0; return 0; } + +SEC("kprobe.multi") +int kprobe_multi_write_ctx(struct pt_regs *ctx) +{ + ctx->ax =3D 0; + return 0; +} #endif --=20 2.51.0