Skip to content

Commit d5849e6

Browse files
olsajiriKernel Patches Daemon
authored andcommitted
selftests/bpf: Add uprobe context ip register change test
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 <[email protected]>
1 parent c72b57b commit d5849e6

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

tools/testing/selftests/bpf/prog_tests/uprobe.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,58 @@ static void regs_common(void)
190190
test_uprobe__destroy(skel);
191191
}
192192

193+
static __naked unsigned long uprobe_regs_change_ip_1(void)
194+
{
195+
asm volatile (
196+
"movq $0xc0ffee, %rax\n"
197+
"ret\n"
198+
);
199+
}
200+
201+
static __naked unsigned long uprobe_regs_change_ip_2(void)
202+
{
203+
asm volatile (
204+
"movq $0xdeadbeef, %rax\n"
205+
"ret\n"
206+
);
207+
}
208+
209+
static void regs_ip(void)
210+
{
211+
LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts);
212+
struct test_uprobe *skel;
213+
unsigned long ret;
214+
215+
skel = test_uprobe__open_and_load();
216+
if (!ASSERT_OK_PTR(skel, "skel_open"))
217+
return;
218+
219+
skel->bss->my_pid = getpid();
220+
skel->bss->ip = (unsigned long) uprobe_regs_change_ip_2;
221+
222+
uprobe_opts.func_name = "uprobe_regs_change_ip_1";
223+
skel->links.test_regs_change_ip = bpf_program__attach_uprobe_opts(
224+
skel->progs.test_regs_change_ip,
225+
-1,
226+
"/proc/self/exe",
227+
0 /* offset */,
228+
&uprobe_opts);
229+
if (!ASSERT_OK_PTR(skel->links.test_regs_change_ip, "bpf_program__attach_uprobe_opts"))
230+
goto cleanup;
231+
232+
ret = uprobe_regs_change_ip_1();
233+
ASSERT_EQ(ret, 0xdeadbeef, "ret");
234+
235+
cleanup:
236+
test_uprobe__destroy(skel);
237+
}
238+
193239
static void test_uprobe_regs_change(void)
194240
{
195241
if (test__start_subtest("regs_change_common"))
196242
regs_common();
243+
if (test__start_subtest("regs_change_ip"))
244+
regs_ip();
197245
}
198246
#else
199247
static void test_uprobe_regs_change(void) { }

tools/testing/selftests/bpf/progs/test_uprobe.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,18 @@ int BPF_UPROBE(test_regs_change)
8282
ctx->si = regs.si;
8383
return 0;
8484
}
85+
86+
unsigned long ip;
87+
88+
SEC("uprobe")
89+
int BPF_UPROBE(test_regs_change_ip)
90+
{
91+
pid_t pid = bpf_get_current_pid_tgid() >> 32;
92+
93+
if (pid != my_pid)
94+
return 0;
95+
96+
ctx->ip = ip;
97+
return 0;
98+
}
8599
#endif

0 commit comments

Comments
 (0)