1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
// SPDX-License-Identifier: GPL-2.0
#include <test_progs.h>
#include <bpf/btf.h>
#include "btf_module_allowlist.skel.h"
static void test_load_with_list(const char **list, size_t count,
int expected_ret)
{
struct btf_module_allowlist *skel;
int ret;
LIBBPF_OPTS(bpf_object_open_opts, opts,
.btf_module_allowlist = list,
.btf_module_allowlist_cnt = count,
);
skel = btf_module_allowlist__open_opts(&opts);
if (!ASSERT_OK_PTR(skel, "btf_module_allowlist__open_opts"))
return;
ret = btf_module_allowlist__load(skel);
ASSERT_EQ(ret, expected_ret, "btf_module_allowlist__load");
btf_module_allowlist__destroy(skel);
}
static void btf_module_allowlist_default(void)
{
struct btf_module_allowlist *skel;
int ret;
skel = btf_module_allowlist__open();
if (!ASSERT_OK_PTR(skel, "btf_module_allowlist__open"))
return;
ret = btf_module_allowlist__load(skel);
ASSERT_OK(ret, "btf_module_allowlist__load");
btf_module_allowlist__destroy(skel);
}
static void btf_module_allowlist_allow(void)
{
const char *mod_list[] = { "bpf_testmod" };
test_load_with_list(mod_list, 1, 0);
}
/*
* A non-NULL allowlist with a count of 0 explicitly disables module BTFs.
* The pointer must be non-NULL to distinguish this from an unspecified
* allowlist.
*/
static void btf_module_allowlist_emptylist(void)
{
const char *mod_list[] = { NULL };
test_load_with_list(mod_list, 0, -ESRCH);
}
/*
* bpf_testmod is not in the allowlist, so its BTF should not be loaded.
*/
static void btf_module_allowlist_skipunlisted(void)
{
const char *mod_list[] = { "module_nonexist" };
struct btf_module_allowlist *skel;
int ret;
LIBBPF_OPTS(bpf_object_open_opts, opts,
.btf_module_allowlist = mod_list,
.btf_module_allowlist_cnt = 1,
);
skel = btf_module_allowlist__open_opts(&opts);
if (!ASSERT_OK_PTR(skel, "btf_module_allowlist__open_opts"))
return;
ret = bpf_program__set_attach_target(skel->progs.test_btf_module_allowlist, 0,
"bpf_testmod:bpf_testmod_fentry_test1");
ASSERT_EQ(ret, -ESRCH, "bpf_program__set_attach_target");
btf_module_allowlist__destroy(skel);
}
static void test_invalid_input(const char **list, size_t count,
const char *test_name)
{
struct btf_module_allowlist *skel;
char assert_name[64];
int err;
LIBBPF_OPTS(bpf_object_open_opts, opts,
.btf_module_allowlist = list,
.btf_module_allowlist_cnt = count,
);
snprintf(assert_name, sizeof(assert_name), "%s: open_opts", test_name);
skel = btf_module_allowlist__open_opts(&opts);
err = errno;
if (!ASSERT_NULL(skel, assert_name)) {
btf_module_allowlist__destroy(skel);
return;
}
snprintf(assert_name, sizeof(assert_name), "%s: open_opts err", test_name);
ASSERT_EQ(err, EINVAL, assert_name);
}
static void btf_module_allowlist_invalidinput(void)
{
const char *names[] = { NULL };
const char *empty_names[] = { "" };
const char *duplicate_names[] = { "bpf_testmod", "bpf_testmod" };
test_invalid_input(NULL, 1, "null_list");
test_invalid_input(names, 1, "null_name");
test_invalid_input(empty_names, 1, "empty_name");
test_invalid_input(duplicate_names, 2, "duplicate_name");
test_invalid_input(names, (size_t)SSIZE_MAX + 1, "large_count");
}
void test_btf_module_allowlist(void)
{
struct btf *vmlinux_btf = NULL;
struct btf *module_btf = NULL;
if (!env.has_testmod) {
printf("%s:SKIP: bpf_testmod is not available\n", __func__);
test__skip();
return;
}
vmlinux_btf = btf__load_vmlinux_btf();
if (!vmlinux_btf) {
printf("%s:SKIP: vmlinux_btf is not available\n", __func__);
test__skip();
return;
}
/* Ensure bpf_testmod BTF is available. */
module_btf = btf__load_module_btf("bpf_testmod", vmlinux_btf);
if (!module_btf) {
printf("%s:SKIP: bpf_testmod's BTF is not available\n", __func__);
btf__free(vmlinux_btf);
test__skip();
return;
}
btf__free(module_btf);
btf__free(vmlinux_btf);
if (test__start_subtest("btf_module_allowlist_default"))
btf_module_allowlist_default();
if (test__start_subtest("btf_module_allowlist_allow"))
btf_module_allowlist_allow();
if (test__start_subtest("btf_module_allowlist_skipunlisted"))
btf_module_allowlist_skipunlisted();
if (test__start_subtest("btf_module_allowlist_emptylist"))
btf_module_allowlist_emptylist();
if (test__start_subtest("btf_module_allowlist_invalidinput"))
btf_module_allowlist_invalidinput();
}
|