Debian Codegraph @

src/test/test-bpf-restrict-fsaccess.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 /*
4 * Test helper for RestrictFileSystemAccess= BPF enforcement tests.
5 *
6 * Usage:
7 * test-bpf-restrict-fsaccess attach — Load, attach, print IDs, then block.
8 * Kill the process to detach (synchronous
9 * via bpf_link_put_direct on last FD close).
10 * test-bpf-restrict-fsaccess check — Check BPF LSM + require_signatures preconditions
11 * test-bpf-restrict-fsaccess mmap-exec PATH — Attempt PROT_READ|PROT_EXEC mmap of PATH
12 * test-bpf-restrict-fsaccess anon-mmap-exec — Attempt anonymous PROT_READ|PROT_EXEC mmap
13 * test-bpf-restrict-fsaccess mprotect-exec PATH — mmap PATH PROT_READ, then mprotect to PROT_EXEC
14 *
15 * When "attach" is used, the BPF LSM program is loaded with initramfs_s_dev
16 * set to the current rootfs s_dev, so the calling test script (running from
17 * the rootfs) continues to work. The process holds all link FDs and blocks;
18 * when killed, close() drops the last reference synchronously.
19 */
20
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <sys/mman.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 #include "bpf-restrict-fsaccess.h"
28 #include "fd-util.h"
29 #include "log.h"
30 #include "string-util.h"
31 #include "tests.h"
32
33 /* ---- mmap/mprotect probe commands (no BPF dependency) ----
34 *
35 * These exercise the mmap_file, file_mprotect, and anonymous-mmap LSM hooks.
36 * The test script copies a file to tmpfs and passes its path here.
37 * Returns 0 if the operation was allowed, negative errno if denied. */
38
39 static int do_mmap_exec(const char *path) {
40 _cleanup_close_ int fd = -EBADF;
41 void *addr;
42
43 fd = open(path, O_RDONLY | O_CLOEXEC);
44 if (fd < 0)
45 return log_error_errno(errno, "Failed to open %s: %m", path);
46
47 addr = mmap(NULL, 4096, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
48 if (addr == MAP_FAILED)
49 return log_info_errno(errno, "PROT_EXEC mmap of %s denied: %m", path);
50
51 (void) munmap(addr, 4096);
52 log_info("PROT_EXEC mmap of %s succeeded", path);
53 return 0;
54 }
55
56 static int do_anon_mmap_exec(void) {
57 void *addr;
58
59 addr = mmap(NULL, 4096, PROT_READ | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
60 if (addr == MAP_FAILED)
61 return log_info_errno(errno, "Anonymous PROT_EXEC mmap denied: %m");
62
63 (void) munmap(addr, 4096);
64 log_info("Anonymous PROT_EXEC mmap succeeded");
65 return 0;
66 }
67
68 static int do_mprotect_exec(const char *path) {
69 _cleanup_close_ int fd = -EBADF;
70 void *addr;
71 int r;
72
73 fd = open(path, O_RDONLY | O_CLOEXEC);
74 if (fd < 0)
75 return log_error_errno(errno, "Failed to open %s: %m", path);
76
77 addr = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, fd, 0);
78 if (addr == MAP_FAILED)
79 return log_error_errno(errno, "PROT_READ mmap of %s failed: %m", path);
80
81 r = mprotect(addr, 4096, PROT_READ | PROT_EXEC);
82 if (r < 0)
83 r = -errno;
84
85 (void) munmap(addr, 4096);
86
87 if (r < 0)
88 return log_info_errno(r, "mprotect PROT_EXEC on %s denied: %m", path);
89
90 log_info("mprotect PROT_EXEC on %s succeeded", path);
91 return 0;
92 }
93
94 #if BPF_FRAMEWORK && HAVE_LSM_INTEGRITY_TYPE
95 #include "bpf-util.h"
96 #include "restrict-fsaccess-skel.h"
97
98 static struct restrict_fsaccess_bpf *restrict_fsaccess_bpf_free(struct restrict_fsaccess_bpf *obj) {
99 restrict_fsaccess_bpf__destroy(obj);
100 return NULL;
101 }
102
103 DEFINE_TRIVIAL_CLEANUP_FUNC(struct restrict_fsaccess_bpf *, restrict_fsaccess_bpf_free);
104
105 static int do_attach(void) {
106 _cleanup_(restrict_fsaccess_bpf_freep) struct restrict_fsaccess_bpf *obj = NULL;
107 struct stat st;
108 int r;
109
110 r = DLOPEN_BPF(LOG_ERR, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
111 if (r < 0)
112 return log_error_errno(r, "Failed to dlopen libbpf: %m");
113
114 r = bpf_restrict_fsaccess_prepare(&obj);
115 if (r < 0)
116 return r;
117
118 /* Set initramfs_s_dev to rootfs s_dev so the test script keeps running */
119 if (stat("/", &st) < 0)
120 return log_error_errno(errno, "Failed to stat /: %m");
121
122 obj->bss->initramfs_s_dev = STAT_DEV_TO_KERNEL(st.st_dev);
123 log_info("Set initramfs_s_dev to %u:%u (kernel dev_t=0x%x)",
124 major(st.st_dev), minor(st.st_dev), obj->bss->initramfs_s_dev);
125
126 r = restrict_fsaccess_bpf__attach(obj);
127 if (r < 0)
128 return log_error_errno(r, "Failed to attach BPF programs: %m");
129
130 /* Populate guard globals so the guard protects our BPF objects */
131 r = bpf_restrict_fsaccess_populate_guard(obj);
132 if (r < 0)
133 return log_error_errno(r, "Failed to populate guard globals: %m");
134
135 printf("VERITY_MAP_ID=%u\n", (unsigned) obj->bss->protected_map_id_verity);
136 printf("BSS_MAP_ID=%u\n", (unsigned) obj->bss->protected_map_id_bss);
137
138 /* Print comma-separated prog and link IDs for guard tests */
139 printf("PROG_IDS=\"");
140 for (size_t i = 0; i < _RESTRICT_FILESYSTEM_ACCESS_LINK_MAX; i++)
141 printf("%s%u", i > 0 ? "," : "", (unsigned) obj->bss->protected_prog_ids[i]);
142 printf("\"\n");
143
144 printf("LINK_IDS=\"");
145 for (size_t i = 0; i < _RESTRICT_FILESYSTEM_ACCESS_LINK_MAX; i++)
146 printf("%s%u", i > 0 ? "," : "", (unsigned) obj->bss->protected_link_ids[i]);
147 printf("\"\n");
148
149 fflush(stdout);
150
151 /* Block until killed. The _cleanup_ destructor holds all link FDs via
152 * the skeleton. When this process is killed, close() on the FDs goes
153 * through bpf_link_put_direct() which synchronously detaches the
154 * trampoline before the process exits. No bpffs pins needed. */
155 log_info("BPF programs attached, waiting for signal to detach...");
156 for (;;)
157 pause();
158
159 /* unreachable — cleanup happens via signal/exit */
160 }
161
162 static int do_check(void) {
163 if (!bpf_restrict_fsaccess_supported()) {
164 log_error("BPF LSM is not available");
165 return -EOPNOTSUPP;
166 }
167 log_info("BPF LSM: supported");
168
169 if (!dm_verity_require_signatures()) {
170 log_error("dm-verity require_signatures is not enabled");
171 return -ENOKEY;
172 }
173 log_info("dm-verity require_signatures: enabled");
174
175 return 0;
176 }
177
178 int main(int argc, char *argv[]) {
179 test_setup_logging(LOG_DEBUG);
180
181 if (argc < 2) {
182 log_error("Usage: %s attach|check|mmap-exec|anon-mmap-exec|mprotect-exec",
183 program_invocation_short_name);
184 return EXIT_FAILURE;
185 }
186
187 if (streq(argv[1], "attach"))
188 return do_attach() < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
189 if (streq(argv[1], "check"))
190 return do_check() < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
191 if (streq(argv[1], "mmap-exec") && argc == 3)
192 return do_mmap_exec(argv[2]) < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
193 if (streq(argv[1], "anon-mmap-exec"))
194 return do_anon_mmap_exec() < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
195 if (streq(argv[1], "mprotect-exec") && argc == 3)
196 return do_mprotect_exec(argv[2]) < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
197
198 log_error("Usage: %s attach|check|mmap-exec PATH|anon-mmap-exec|mprotect-exec PATH",
199 program_invocation_short_name);
200 return EXIT_FAILURE;
201 }
202
203 #else /* ! BPF_FRAMEWORK || ! HAVE_LSM_INTEGRITY_TYPE */
204
205 int main(int argc, char *argv[]) {
206 test_setup_logging(LOG_DEBUG);
207
208 /* mmap/mprotect probes work without BPF */
209 if (argc >= 2) {
210 if (streq(argv[1], "mmap-exec") && argc == 3)
211 return do_mmap_exec(argv[2]) < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
212 if (streq(argv[1], "anon-mmap-exec"))
213 return do_anon_mmap_exec() < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
214 if (streq(argv[1], "mprotect-exec") && argc == 3)
215 return do_mprotect_exec(argv[2]) < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
216 }
217
218 log_info("BPF framework not available, attach/check not supported");
219 return 77; /* skip */
220 }
221
222 #endif