Skip to content

Commit 09905b8

Browse files
liu-song-6Kernel Patches Daemon
authored andcommitted
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 2aa07b3 commit 09905b8

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-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: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
const char *livepatch_paths[] = {
11+
"../../../../samples/livepatch/livepatch-sample.ko",
12+
/* This is the path used by CI */
13+
"/tmp/work/bpf/bpf/kbuild-output/samples/livepatch/livepatch-sample.ko",
14+
};
15+
int ret, i;
16+
17+
for (i = 0; i < sizeof(livepatch_paths) / sizeof(char *); i++) {
18+
ret = load_module(livepatch_paths[i], env_verbosity > VERBOSE_NONE);
19+
if (!ret)
20+
break;
21+
}
22+
return ret;
23+
}
24+
25+
static void unload_livepatch(void)
26+
{
27+
/* Disable the livepatch before unloading the module */
28+
system("echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled");
29+
30+
unload_module("livepatch_sample", env_verbosity > VERBOSE_NONE);
31+
}
32+
33+
static void read_proc_cmdline(void)
34+
{
35+
char buf[4096];
36+
int fd, ret;
37+
38+
fd = open("/proc/cmdline", O_RDONLY);
39+
if (!ASSERT_OK_FD(fd, "open /proc/cmdline"))
40+
return;
41+
42+
ret = read(fd, buf, sizeof(buf));
43+
if (!ASSERT_GT(ret, 0, "read /proc/cmdline"))
44+
goto out;
45+
46+
ASSERT_OK(strncmp(buf, "this has been live patched", 26), "strncmp");
47+
48+
out:
49+
close(fd);
50+
}
51+
52+
static void __test_livepatch_trampoline(bool fexit_first)
53+
{
54+
struct livepatch_trampoline *skel = NULL;
55+
int err;
56+
57+
skel = livepatch_trampoline__open_and_load();
58+
if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
59+
goto out;
60+
61+
skel->bss->my_pid = getpid();
62+
63+
if (!fexit_first) {
64+
/* fentry program is loaded first by default */
65+
err = livepatch_trampoline__attach(skel);
66+
if (!ASSERT_OK(err, "skel_attach"))
67+
goto out;
68+
} else {
69+
/* Manually load fexit program first. */
70+
skel->links.fexit_cmdline = bpf_program__attach(skel->progs.fexit_cmdline);
71+
if (!ASSERT_OK_PTR(skel->links.fexit_cmdline, "attach_fexit"))
72+
goto out;
73+
74+
skel->links.fentry_cmdline = bpf_program__attach(skel->progs.fentry_cmdline);
75+
if (!ASSERT_OK_PTR(skel->links.fentry_cmdline, "attach_fentry"))
76+
goto out;
77+
}
78+
79+
read_proc_cmdline();
80+
81+
ASSERT_EQ(skel->bss->fentry_hit, 1, "fentry_hit");
82+
ASSERT_EQ(skel->bss->fexit_hit, 1, "fexit_hit");
83+
out:
84+
livepatch_trampoline__destroy(skel);
85+
}
86+
87+
void test_livepatch_trampoline(void)
88+
{
89+
int retry_cnt = 0;
90+
91+
retry:
92+
if (load_livepatch()) {
93+
if (retry_cnt) {
94+
ASSERT_OK(1, "load_livepatch");
95+
goto out;
96+
}
97+
/*
98+
* Something else (previous run of the same test?) loaded
99+
* the KLP module. Unload the KLP module and retry.
100+
*/
101+
unload_livepatch();
102+
retry_cnt++;
103+
goto retry;
104+
}
105+
106+
if (test__start_subtest("fentry_first"))
107+
__test_livepatch_trampoline(false);
108+
109+
if (test__start_subtest("fexit_first"))
110+
__test_livepatch_trampoline(true);
111+
out:
112+
unload_livepatch();
113+
}
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)