Skip to content

Commit 77afdde

Browse files
committed
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 3a805ae commit 77afdde

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,60 @@ 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+
"nop\n"
197+
"movq $0xc0ffee, %rax\n"
198+
"ret\n"
199+
);
200+
}
201+
202+
static __naked unsigned long uprobe_regs_change_ip_2(void)
203+
{
204+
asm volatile (
205+
"nop\n"
206+
"movq $0xdeadbeef, %rax\n"
207+
"ret\n"
208+
);
209+
}
210+
211+
static void regs_ip(void)
212+
{
213+
LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts);
214+
struct test_uprobe *skel;
215+
unsigned long ret;
216+
217+
skel = test_uprobe__open_and_load();
218+
if (!ASSERT_OK_PTR(skel, "skel_open"))
219+
return;
220+
221+
skel->bss->my_pid = getpid();
222+
skel->bss->ip = (unsigned long) uprobe_regs_change_ip_2;
223+
224+
uprobe_opts.func_name = "uprobe_regs_change_ip_1";
225+
skel->links.test_regs_change_ip = bpf_program__attach_uprobe_opts(
226+
skel->progs.test_regs_change_ip,
227+
-1,
228+
"/proc/self/exe",
229+
0 /* offset */,
230+
&uprobe_opts);
231+
if (!ASSERT_OK_PTR(skel->links.test_regs_change_ip, "bpf_program__attach_uprobe_opts"))
232+
goto cleanup;
233+
234+
ret = uprobe_regs_change_ip_1();
235+
ASSERT_EQ(ret, 0xdeadbeef, "ret");
236+
237+
cleanup:
238+
test_uprobe__destroy(skel);
239+
}
240+
193241
static void test_uprobe_regs_change(void)
194242
{
195243
if (test__start_subtest("regs_change_common"))
196244
regs_common();
245+
if (test__start_subtest("regs_change_ip"))
246+
regs_ip();
197247
}
198248
#else
199249
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)