Skip to content

Commit 62a4a54

Browse files
committed
selftests/bpf: Add tests for livepatch + bpf trampoline
Both livepatch and BPF trampoline use ftrace. Special attention is needed when livepatch and fexit program touch the same function at the same time, because livepatch updates a kernel function and the BPF trampoline need to call into the right version of the kernel function. Use samples/livepatch/livepatch-sample.ko for the test. The test covers two cases: 1) When a fentry program is loaded first. This exercises the modify_ftrace_direct code path. 2) When a fentry program is loaded first. This exercises the register_ftrace_direct code path. Signed-off-by: Song Liu <[email protected]>
1 parent a50439c commit 62a4a54

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

tools/testing/selftests/bpf/config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ CONFIG_IPV6_SIT=y
5050
CONFIG_IPV6_TUNNEL=y
5151
CONFIG_KEYS=y
5252
CONFIG_LIRC=y
53+
CONFIG_LIVEPATCH=y
5354
CONFIG_LWTUNNEL=y
5455
CONFIG_MODULE_SIG=y
5556
CONFIG_MODULE_SRCVERSION_ALL=y
@@ -111,6 +112,8 @@ CONFIG_IP6_NF_FILTER=y
111112
CONFIG_NF_NAT=y
112113
CONFIG_PACKET=y
113114
CONFIG_RC_CORE=y
115+
CONFIG_SAMPLES=y
116+
CONFIG_SAMPLE_LIVEPATCH=m
114117
CONFIG_SECURITY=y
115118
CONFIG_SECURITYFS=y
116119
CONFIG_SYN_COOKIES=y
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <test_progs.h>
5+
#include "testing_helpers.h"
6+
#include "livepatch_trampoline.skel.h"
7+
8+
static int load_livepatch(void)
9+
{
10+
return load_module("../../../../samples/livepatch/livepatch-sample.ko", false);
11+
}
12+
13+
static void unload_livepatch(void)
14+
{
15+
/* Disable the livepatch before unloading the module */
16+
system("echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled");
17+
18+
unload_module("livepatch_sample", false);
19+
}
20+
21+
static void read_proc_cmdline(void)
22+
{
23+
char buf[4096];
24+
int fd, ret;
25+
26+
fd = open("/proc/cmdline", O_RDONLY);
27+
if (!ASSERT_OK_FD(fd, "open /proc/cmdline"))
28+
return;
29+
30+
ret = read(fd, buf, sizeof(buf));
31+
if (!ASSERT_GT(ret, 0, "read /proc/cmdline"))
32+
goto out;
33+
34+
ASSERT_OK(strncmp(buf, "this has been live patched", 26), "strncmp");
35+
36+
out:
37+
close(fd);
38+
}
39+
40+
static void __test_livepatch_trampoline(bool fexit_first)
41+
{
42+
struct livepatch_trampoline *skel = NULL;
43+
int err;
44+
45+
skel = livepatch_trampoline__open_and_load();
46+
if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
47+
goto out;
48+
49+
skel->bss->my_pid = getpid();
50+
51+
if (!fexit_first) {
52+
/* fentry program is loaded first by default */
53+
err = livepatch_trampoline__attach(skel);
54+
if (!ASSERT_OK(err, "skel_attach"))
55+
goto out;
56+
} else {
57+
/* Manually load fexit program first. */
58+
skel->links.fexit_cmdline = bpf_program__attach(skel->progs.fexit_cmdline);
59+
if (!ASSERT_OK_PTR(skel->links.fexit_cmdline, "attach_fexit"))
60+
goto out;
61+
62+
skel->links.fentry_cmdline = bpf_program__attach(skel->progs.fentry_cmdline);
63+
if (!ASSERT_OK_PTR(skel->links.fentry_cmdline, "attach_fentry"))
64+
goto out;
65+
}
66+
67+
read_proc_cmdline();
68+
69+
ASSERT_EQ(skel->bss->fentry_hit, 1, "fentry_hit");
70+
ASSERT_EQ(skel->bss->fexit_hit, 1, "fexit_hit");
71+
out:
72+
livepatch_trampoline__destroy(skel);
73+
}
74+
75+
void test_livepatch_trampoline(void)
76+
{
77+
int retry_cnt = 0;
78+
79+
retry:
80+
if (load_livepatch()) {
81+
if (retry_cnt) {
82+
ASSERT_OK(1, "load_livepatch");
83+
goto out;
84+
}
85+
/*
86+
* Something else (previous run of the same test?) loaded
87+
* the KLP module. Unload the KLP module and retry.
88+
*/
89+
unload_livepatch();
90+
retry_cnt++;
91+
goto retry;
92+
}
93+
94+
if (test__start_subtest("fentry_first"))
95+
__test_livepatch_trampoline(false);
96+
97+
if (test__start_subtest("fexit_first"))
98+
__test_livepatch_trampoline(true);
99+
out:
100+
unload_livepatch();
101+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <linux/bpf.h>
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
7+
8+
int fentry_hit;
9+
int fexit_hit;
10+
int my_pid;
11+
12+
SEC("fentry/cmdline_proc_show")
13+
int BPF_PROG(fentry_cmdline)
14+
{
15+
if (my_pid != (bpf_get_current_pid_tgid() >> 32))
16+
return 0;
17+
18+
fentry_hit = 1;
19+
return 0;
20+
}
21+
22+
SEC("fexit/cmdline_proc_show")
23+
int BPF_PROG(fexit_cmdline)
24+
{
25+
if (my_pid != (bpf_get_current_pid_tgid() >> 32))
26+
return 0;
27+
28+
fexit_hit = 1;
29+
return 0;
30+
}

0 commit comments

Comments
 (0)