Debian Codegraph @

src/core/bpf-restrict-fsaccess.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <sys/mman.h>
5 #include <sys/stat.h>
6
7 #include "bpf-restrict-fsaccess.h"
8 #include "fd-util.h"
9 #include "fileio.h"
10 #include "initrd-util.h"
11 #include "log.h"
12 #include "lsm-util.h"
13 #include "manager.h"
14 #include "memory-util.h"
15 #include "serialize.h"
16 #include "string-table.h"
17
18 /* DMVERITY_DEVICES_MAX lives in bpf-restrict-fsaccess.h for sharing with tests. */
19
20 static const char* const restrict_filesystem_access_table[_RESTRICT_FILESYSTEM_ACCESS_MAX] = {
21 [RESTRICT_FILESYSTEM_ACCESS_NO] = "no",
22 [RESTRICT_FILESYSTEM_ACCESS_EXEC] = "exec",
23 };
24
25 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(restrict_filesystem_access, RestrictFileSystemAccess, RESTRICT_FILESYSTEM_ACCESS_EXEC);
26
27 const char* const restrict_fsaccess_link_names[_RESTRICT_FILESYSTEM_ACCESS_LINK_MAX] = {
28 [RESTRICT_FILESYSTEM_ACCESS_LINK_BDEV_SETINTEGRITY] = "restrict-fsaccess-bdev-setintegrity-link",
29 [RESTRICT_FILESYSTEM_ACCESS_LINK_BDEV_FREE] = "restrict-fsaccess-bdev-free-link",
30 [RESTRICT_FILESYSTEM_ACCESS_LINK_BPRM_CHECK] = "restrict-fsaccess-bprm-check-link",
31 [RESTRICT_FILESYSTEM_ACCESS_LINK_MMAP_FILE] = "restrict-fsaccess-mmap-file-link",
32 [RESTRICT_FILESYSTEM_ACCESS_LINK_FILE_MPROTECT] = "restrict-fsaccess-file-mprotect-link",
33 [RESTRICT_FILESYSTEM_ACCESS_LINK_PTRACE_GUARD] = "restrict-fsaccess-ptrace-guard-link",
34 [RESTRICT_FILESYSTEM_ACCESS_LINK_BPF_MAP_GUARD] = "restrict-fsaccess-bpf-map-guard-link",
35 [RESTRICT_FILESYSTEM_ACCESS_LINK_BPF_PROG_GUARD] = "restrict-fsaccess-bpf-prog-guard-link",
36 [RESTRICT_FILESYSTEM_ACCESS_LINK_BPF_GUARD] = "restrict-fsaccess-bpf-guard-link",
37 };
38
39 #if BPF_FRAMEWORK && HAVE_LSM_INTEGRITY_TYPE
40 #include "bpf-util.h"
41 #include "bpf-link.h"
42 #include "restrict-fsaccess-skel.h"
43
44 static struct restrict_fsaccess_bpf *restrict_fsaccess_bpf_free(struct restrict_fsaccess_bpf *obj) {
45 restrict_fsaccess_bpf__destroy(obj);
46 return NULL;
47 }
48
49 DEFINE_TRIVIAL_CLEANUP_FUNC(struct restrict_fsaccess_bpf *, restrict_fsaccess_bpf_free);
50
51 /* Verify that restrict_fsaccess_bss matches the skeleton's .bss layout. The sizeof
52 * check catches field additions/removals; the offsetof checks catch field
53 * reordering. Field order in restrict_fsaccess_bss must match the BPF global
54 * declaration order in restrict-fsaccess.bpf.c — this is what bpftool uses for the
55 * generated struct. The read-modify-write in restrict_fsaccess_clear_initramfs_trust()
56 * depends on this layout. */
57 assert_cc(sizeof(struct restrict_fsaccess_bss) == sizeof_field(struct restrict_fsaccess_bpf, bss[0]));
58 assert_cc(offsetof(struct restrict_fsaccess_bss, initramfs_s_dev) ==
59 offsetof(typeof_field(struct restrict_fsaccess_bpf, bss[0]), initramfs_s_dev));
60 assert_cc(offsetof(struct restrict_fsaccess_bss, protected_map_id_verity) ==
61 offsetof(typeof_field(struct restrict_fsaccess_bpf, bss[0]), protected_map_id_verity));
62 assert_cc(offsetof(struct restrict_fsaccess_bss, protected_map_id_bss) ==
63 offsetof(typeof_field(struct restrict_fsaccess_bpf, bss[0]), protected_map_id_bss));
64
65 /* Build the skeleton links array indexed by the link enum.
66 * For BDEV_SETINTEGRITY, use whichever variant was loaded (full or compat).
67 * This compat logic can be removed once the kernel baseline includes
68 * 1271a40eeafa ("bpf: Allow access to const void pointer arguments"). */
69 #define RESTRICT_FSACCESS_LINKS(obj) { \
70 [RESTRICT_FILESYSTEM_ACCESS_LINK_BDEV_SETINTEGRITY] = (obj)->links.restrict_fsaccess_bdev_setintegrity ?: \
71 (obj)->links.restrict_fsaccess_bdev_setintegrity_compat, \
72 [RESTRICT_FILESYSTEM_ACCESS_LINK_BDEV_FREE] = (obj)->links.restrict_fsaccess_bdev_free, \
73 [RESTRICT_FILESYSTEM_ACCESS_LINK_BPRM_CHECK] = (obj)->links.restrict_fsaccess_bprm_check, \
74 [RESTRICT_FILESYSTEM_ACCESS_LINK_MMAP_FILE] = (obj)->links.restrict_fsaccess_mmap_file, \
75 [RESTRICT_FILESYSTEM_ACCESS_LINK_FILE_MPROTECT] = (obj)->links.restrict_fsaccess_file_mprotect, \
76 [RESTRICT_FILESYSTEM_ACCESS_LINK_PTRACE_GUARD] = (obj)->links.restrict_fsaccess_ptrace_guard, \
77 [RESTRICT_FILESYSTEM_ACCESS_LINK_BPF_MAP_GUARD] = (obj)->links.restrict_fsaccess_bpf_map_guard, \
78 [RESTRICT_FILESYSTEM_ACCESS_LINK_BPF_PROG_GUARD] = (obj)->links.restrict_fsaccess_bpf_prog_guard, \
79 [RESTRICT_FILESYSTEM_ACCESS_LINK_BPF_GUARD] = (obj)->links.restrict_fsaccess_bpf_guard, \
80 }
81
82 bool dm_verity_require_signatures(void) {
83 int r;
84
85 r = read_boolean_file("/sys/module/dm_verity/parameters/require_signatures");
86 if (r < 0) {
87 if (r != -ENOENT)
88 log_warning_errno(r, "bpf-restrict-fsaccess: Failed to read dm-verity require_signatures: %m");
89 return false;
90 }
91
92 return r > 0;
93 }
94
95 static int get_root_s_dev(uint32_t *ret) {
96 struct stat st;
97
98 assert(ret);
99
100 /* Stat /usr/ rather than / — executable code lives in /usr/ and we push toward
101 * a writable non-executable /. On systems with a separate /usr partition this
102 * means / is intentionally not trusted. */
103 if (stat("/usr/", &st) < 0)
104 return log_error_errno(errno, "bpf-restrict-fsaccess: Failed to stat /usr/ filesystem: %m");
105
106 *ret = STAT_DEV_TO_KERNEL(st.st_dev);
107 return 0;
108 }
109
110 int bpf_restrict_fsaccess_prepare(struct restrict_fsaccess_bpf **ret) {
111 _cleanup_(restrict_fsaccess_bpf_freep) struct restrict_fsaccess_bpf *obj = NULL;
112 int r;
113
114 assert(ret);
115
116 /* Try the preferred version first — it reads the const void *value
117 * argument for defense-in-depth. On kernels before v6.16 (missing
118 * 1271a40eeafa) the verifier rejects loads from const void * context
119 * arguments, so we fall back to the _compat variant that only reads
120 * the size argument via raw ctx access. */
121 obj = restrict_fsaccess_bpf__open();
122 if (!obj)
123 return log_error_errno(errno, "bpf-restrict-fsaccess: Failed to open BPF object: %m");
124
125 r = sym_bpf_map__set_max_entries(obj->maps.verity_devices, DMVERITY_DEVICES_MAX);
126 if (r < 0)
127 return log_error_errno(r, "bpf-restrict-fsaccess: Failed to size hash table: %m");
128
129 r = sym_bpf_program__set_autoload(obj->progs.restrict_fsaccess_bdev_setintegrity_compat, false);
130 if (r < 0)
131 return log_error_errno(r, "bpf-restrict-fsaccess: Failed to disable compat program: %m");
132
133 r = restrict_fsaccess_bpf__load(obj);
134 if (r >= 0) {
135 log_debug("bpf-restrict-fsaccess: Loaded with full const void * access.");
136 *ret = TAKE_PTR(obj);
137 return 0;
138 }
139
140 log_debug_errno(r, "bpf-restrict-fsaccess: Full version failed to load (%m), trying compat variant.");
141 obj = restrict_fsaccess_bpf_free(obj);
142
143 obj = restrict_fsaccess_bpf__open();
144 if (!obj)
145 return log_error_errno(errno, "bpf-restrict-fsaccess: Failed to reopen BPF object: %m");
146
147 r = sym_bpf_map__set_max_entries(obj->maps.verity_devices, DMVERITY_DEVICES_MAX);
148 if (r < 0)
149 return log_error_errno(r, "bpf-restrict-fsaccess: Failed to size hash table: %m");
150
151 r = sym_bpf_program__set_autoload(obj->progs.restrict_fsaccess_bdev_setintegrity, false);
152 if (r < 0)
153 return log_error_errno(r, "bpf-restrict-fsaccess: Failed to disable full program: %m");
154
155 r = restrict_fsaccess_bpf__load(obj);
156 if (r < 0)
157 return log_error_errno(r, "bpf-restrict-fsaccess: Failed to load BPF object (compat): %m");
158
159 log_debug("bpf-restrict-fsaccess: Loaded with compat bdev_setintegrity.");
160 *ret = TAKE_PTR(obj);
161 return 0;
162 }
163
164 bool bpf_restrict_fsaccess_supported(void) {
165 _cleanup_(restrict_fsaccess_bpf_freep) struct restrict_fsaccess_bpf *obj = NULL;
166 static int supported = -1;
167 int r;
168
169 if (supported >= 0)
170 return supported;
171 if (DLOPEN_BPF(LOG_WARNING, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED) < 0)
172 return (supported = false);
173
174 r = lsm_supported("bpf");
175 if (r == -ENOPKG) {
176 log_debug_errno(r, "bpf-restrict-fsaccess: securityfs not mounted, BPF LSM not available.");
177 return (supported = false);
178 }
179 if (r < 0) {
180 log_warning_errno(r, "bpf-restrict-fsaccess: Can't determine whether the BPF LSM module is used: %m");
181 return (supported = false);
182 }
183 if (r == 0) {
184 log_info("bpf-restrict-fsaccess: BPF LSM hook not enabled in the kernel, not supported.");
185 return (supported = false);
186 }
187
188 r = bpf_restrict_fsaccess_prepare(&obj);
189 if (r < 0)
190 return (supported = false);
191
192 if (!bpf_can_link_lsm_program(obj->progs.restrict_fsaccess_bprm_check)) {
193 log_warning("bpf-restrict-fsaccess: Failed to link program; assuming BPF LSM is not available.");
194 return (supported = false);
195 }
196
197 return (supported = true);
198 }
199
200 /* Partial deserialization (some FDs but not all) is fatal: continuing
201 * would leave enforcement incomplete. */
202 static int restrict_fsaccess_have_deserialized_fds(Manager *m) {
203 size_t count = 0;
204
205 assert(m);
206
207 FOREACH_ELEMENT(fd, m->restrict_fsaccess_link_fds)
208 if (*fd >= 0)
209 count++;
210
211 if (count == 0)
212 return 0;
213 if (count == ELEMENTSOF(m->restrict_fsaccess_link_fds))
214 return 1;
215
216 return log_error_errno(SYNTHETIC_ERRNO(EBADFD),
217 "bpf-restrict-fsaccess: Only %zu of %zu link FDs deserialized, refusing to continue with partial enforcement.",
218 count, ELEMENTSOF(m->restrict_fsaccess_link_fds));
219 }
220
221 /* Close the initramfs trust window after switch_root by clearing initramfs_s_dev
222 * in the BPF .bss map. The .bss is a BPF_F_MMAPABLE array map — mmap it and do
223 * a single aligned 4-byte store instead of a full-value read-modify-write via
224 * bpf_map_update_elem, which would needlessly rewrite the guard globals too. */
225 static int restrict_fsaccess_clear_initramfs_trust(int bss_map_fd) {
226 void *p;
227
228 assert(bss_map_fd >= 0);
229 assert_cc(offsetof(struct restrict_fsaccess_bss, initramfs_s_dev) == 0);
230
231 p = mmap(NULL, page_size(), PROT_READ | PROT_WRITE, MAP_SHARED, bss_map_fd, 0);
232 if (p == MAP_FAILED)
233 return log_error_errno(errno, "bpf-restrict-fsaccess: Failed to mmap .bss map: %m");
234
235 /* initramfs_s_dev is at offset 0 in the .bss layout. Single aligned
236 * 32-bit store is atomic — BPF programs see either the old or new value,
237 * no torn reads possible. Guard globals are untouched. */
238 *(uint32_t *) p = 0;
239
240 /* munmap failure here is harmless: the clear above already landed in
241 * the kernel, and the mapping is discarded by exec anyway. */
242 if (munmap(p, page_size()) < 0)
243 log_warning_errno(errno, "bpf-restrict-fsaccess: Failed to munmap .bss map, ignoring: %m");
244
245 log_info("bpf-restrict-fsaccess: Cleared initramfs trust window after switch_root.");
246 return 0;
247 }
248
249 static int bpf_get_map_id(int fd, uint32_t *ret_id) {
250 struct bpf_map_info info = {};
251 uint32_t len = sizeof(info);
252 int r;
253
254 if (fd < 0)
255 return -EBADF;
256
257 assert(ret_id);
258
259 r = sym_bpf_obj_get_info_by_fd(fd, &info, &len);
260 if (r < 0)
261 return r;
262
263 *ret_id = info.id;
264 return 0;
265 }
266
267 static int bpf_get_link_ids(int fd, uint32_t *ret_link_id, uint32_t *ret_prog_id) {
268 struct bpf_link_info info = {};
269 uint32_t len = sizeof(info);
270 int r;
271
272 if (fd < 0)
273 return -EBADF;
274
275 r = sym_bpf_obj_get_info_by_fd(fd, &info, &len);
276 if (r < 0)
277 return r;
278
279 if (ret_link_id)
280 *ret_link_id = info.id;
281 if (ret_prog_id)
282 *ret_prog_id = info.prog_id;
283
284 return 0;
285 }
286
287 /* Populate guard globals with kernel-assigned IDs so the guard hooks block
288 * non-PID1 access to our maps/progs/links via the bpf() syscall. */
289 int bpf_restrict_fsaccess_populate_guard(struct restrict_fsaccess_bpf *obj) {
290 int r;
291
292 assert(obj);
293
294 struct bpf_link *links[] = RESTRICT_FSACCESS_LINKS(obj);
295 assert_cc(ELEMENTSOF(links) == _RESTRICT_FILESYSTEM_ACCESS_LINK_MAX);
296
297 /* Map IDs */
298 r = bpf_get_map_id(sym_bpf_map__fd(obj->maps.verity_devices), &obj->bss->protected_map_id_verity);
299 if (r < 0)
300 return log_error_errno(r, "bpf-restrict-fsaccess: Failed to get verity_devices map ID: %m");
301
302 r = bpf_get_map_id(sym_bpf_map__fd(obj->maps.bss), &obj->bss->protected_map_id_bss);
303 if (r < 0)
304 return log_error_errno(r, "bpf-restrict-fsaccess: Failed to get .bss map ID: %m");
305
306 /* Link and program IDs (each link knows its associated program) */
307 FOREACH_ELEMENT(link, links) {
308 size_t idx = link - links;
309
310 /* BDEV_SETINTEGRITY slot resolves via ?: between full and compat
311 * variants; assert at least one was attached. */
312 if (!*link)
313 return log_error_errno(SYNTHETIC_ERRNO(ENODATA),
314 "bpf-restrict-fsaccess: %s link missing after attach.",
315 restrict_fsaccess_link_names[idx]);
316
317 r = bpf_get_link_ids(sym_bpf_link__fd(*link),
318 &obj->bss->protected_link_ids[idx],
319 &obj->bss->protected_prog_ids[idx]);
320 if (r < 0)
321 return log_error_errno(r, "bpf-restrict-fsaccess: Failed to get link/prog IDs for %s: %m",
322 restrict_fsaccess_link_names[idx]);
323 }
324
325 log_info("bpf-restrict-fsaccess: Guard globals populated (verity_map=%u, bss_map=%u)",
326 (unsigned) obj->bss->protected_map_id_verity,
327 (unsigned) obj->bss->protected_map_id_bss);
328 return 0;
329 }
330
331 /* Validate that deserialized FDs actually reference our LSM BPF links. A
332 * corrupted serialization file could leave FDs pointing at arbitrary kernel
333 * objects; a stale FD could point at a BPF link of an entirely different type
334 * (e.g. kprobe-multi). Verify both link type and attach type so a substituted
335 * FD that happens to be a BPF link still fails the check. */
336 static int restrict_fsaccess_validate_deserialized_fds(Manager *m) {
337 int r;
338
339 assert(m);
340
341 r = DLOPEN_BPF(LOG_WARNING, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
342 if (r < 0)
343 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
344 "bpf-restrict-fsaccess: Failed to load libbpf for FD validation, aborting.");
345
346 FOREACH_ELEMENT(fd, m->restrict_fsaccess_link_fds) {
347 struct bpf_link_info info = {};
348 uint32_t len = sizeof(info);
349 const char *name = restrict_fsaccess_link_names[fd - m->restrict_fsaccess_link_fds];
350
351 r = sym_bpf_obj_get_info_by_fd(*fd, &info, &len);
352 if (r < 0)
353 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
354 "bpf-restrict-fsaccess: Deserialized FD for %s is not a valid BPF object, aborting.",
355 name);
356
357 if (info.type != BPF_LINK_TYPE_TRACING || info.tracing.attach_type != BPF_LSM_MAC)
358 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
359 "bpf-restrict-fsaccess: Deserialized FD for %s is not an LSM tracing link (type=%u attach=%u), aborting.",
360 name, info.type, info.tracing.attach_type);
361 }
362
363 if (m->restrict_fsaccess_bss_map_fd >= 0) {
364 uint32_t id;
365
366 r = bpf_get_map_id(m->restrict_fsaccess_bss_map_fd, &id);
367 if (r < 0)
368 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
369 "bpf-restrict-fsaccess: Deserialized FD for .bss map is not a valid BPF map, aborting.");
370 }
371
372 return 0;
373 }
374
375 int bpf_restrict_fsaccess_setup(Manager *m) {
376 _cleanup_(restrict_fsaccess_bpf_freep) struct restrict_fsaccess_bpf *obj = NULL;
377 int r;
378
379 assert(m);
380
381 if (!MANAGER_IS_SYSTEM(m) || m->restrict_filesystem_access <= RESTRICT_FILESYSTEM_ACCESS_NO)
382 return 0;
383
384 r = restrict_fsaccess_have_deserialized_fds(m);
385 if (r < 0)
386 return r;
387 if (r > 0) {
388 log_info("bpf-restrict-fsaccess: Recovered link FDs from previous exec, programs still attached.");
389
390 r = restrict_fsaccess_validate_deserialized_fds(m);
391 if (r < 0)
392 return r;
393 if (m->switching_root) {
394 if (m->restrict_fsaccess_bss_map_fd < 0)
395 return log_error_errno(SYNTHETIC_ERRNO(EBADF),
396 "bpf-restrict-fsaccess: Cannot clear initramfs trust after switch_root.");
397 r = restrict_fsaccess_clear_initramfs_trust(m->restrict_fsaccess_bss_map_fd);
398 if (r < 0)
399 return r;
400 }
401
402 return 0;
403 }
404
405 /* Fresh setup: verify BPF LSM is available */
406 if (!bpf_restrict_fsaccess_supported())
407 return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
408 "bpf-restrict-fsaccess: BPF LSM is not available.");
409
410 /* Require dm-verity signature enforcement */
411 if (!dm_verity_require_signatures())
412 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY),
413 "bpf-restrict-fsaccess: dm-verity require_signatures is not enabled. "
414 "RestrictFileSystemAccess= requires the kernel to enforce dm-verity signatures. "
415 "Set dm_verity.require_signatures=1 on the kernel command line.");
416
417 r = bpf_restrict_fsaccess_prepare(&obj);
418 if (r < 0)
419 return r;
420
421 /* If we're still in the initramfs, allow execution from it by recording
422 * its s_dev. After switch_root, PID1 re-execs and in_initrd() returns
423 * false — initramfs_s_dev stays at 0 (its default), closing the trust
424 * window. */
425 if (in_initrd()) {
426 uint32_t root_dev;
427
428 r = get_root_s_dev(&root_dev);
429 if (r < 0)
430 return r;
431
432 obj->bss->initramfs_s_dev = root_dev;
433 log_info("bpf-restrict-fsaccess: Initramfs trusted (s_dev=%" PRIu32 ":%" PRIu32 ")",
434 root_dev >> 20, root_dev & 0xFFFFF);
435 }
436
437 r = restrict_fsaccess_bpf__attach(obj);
438 if (r < 0)
439 return log_error_errno(r, "bpf-restrict-fsaccess: Failed to attach BPF programs: %m");
440
441 log_info("bpf-restrict-fsaccess: LSM BPF programs attached");
442
443 /* Now that all programs are attached, populate the guard's globals with
444 * the kernel-assigned IDs of our maps, programs, and links. From this
445 * point on, non-PID1 processes cannot obtain FDs to our BPF objects. */
446 r = bpf_restrict_fsaccess_populate_guard(obj);
447 if (r < 0)
448 return r;
449
450 /* Extract owned FDs from the skeleton. These keep the kernel BPF objects
451 * alive after the skeleton is destroyed. Destroying the skeleton unmaps
452 * the .bss page from our address space so no BPF state (guard globals,
453 * map IDs, initramfs_s_dev) is reachable via /proc/1/mem. */
454 struct bpf_link *links[] = RESTRICT_FSACCESS_LINKS(obj);
455 FOREACH_ELEMENT(link, links) {
456 size_t idx = link - links;
457
458 if (!*link) {
459 r = log_error_errno(SYNTHETIC_ERRNO(ENODATA),
460 "bpf-restrict-fsaccess: %s link missing after attach.",
461 restrict_fsaccess_link_names[idx]);
462 goto fail;
463 }
464
465 m->restrict_fsaccess_link_fds[idx] = fcntl(sym_bpf_link__fd(*link), F_DUPFD_CLOEXEC, 3);
466 if (m->restrict_fsaccess_link_fds[idx] < 0) {
467 r = log_error_errno(errno, "bpf-restrict-fsaccess: Failed to dup link FD for %s: %m",
468 restrict_fsaccess_link_names[idx]);
469 goto fail;
470 }
471 }
472
473 m->restrict_fsaccess_bss_map_fd = fcntl(sym_bpf_map__fd(obj->maps.bss), F_DUPFD_CLOEXEC, 3);
474 if (m->restrict_fsaccess_bss_map_fd < 0) {
475 r = log_error_errno(errno, "bpf-restrict-fsaccess: Failed to dup .bss map FD: %m");
476 goto fail;
477 }
478
479 return 0;
480
481 fail:
482 /* Close partial FDs so we don't leave a half-baked policy attached
483 * once the skeleton is destroyed by _cleanup_. */
484 FOREACH_ELEMENT(fd, m->restrict_fsaccess_link_fds)
485 *fd = safe_close(*fd);
486 m->restrict_fsaccess_bss_map_fd = safe_close(m->restrict_fsaccess_bss_map_fd);
487 return r;
488 }
489
490 int bpf_restrict_fsaccess_close_initramfs_trust(Manager *m) {
491 assert(m);
492
493 /* Clear initramfs_s_dev in the BPF .bss map BEFORE switch_root unmounts
494 * the initramfs. This eliminates the dev_t recycling window: the anonymous
495 * dev_t is still held by the mounted initramfs superblock, so no other
496 * filesystem can recycle it yet. Anonymous dev_t recycling is immediate
497 * and lowest-first, so a stale initramfs_s_dev is a near-certain trust
498 * bypass — fail closed. */
499 if (!in_initrd() || m->restrict_fsaccess_bss_map_fd < 0)
500 return 0;
501
502 return restrict_fsaccess_clear_initramfs_trust(m->restrict_fsaccess_bss_map_fd);
503 }
504
505 int bpf_restrict_fsaccess_serialize(Manager *m, FILE *f, FDSet *fds) {
506 int r;
507
508 assert(m);
509 assert(f);
510 assert(fds);
511
512 if (!MANAGER_IS_SYSTEM(m) || m->restrict_filesystem_access <= RESTRICT_FILESYSTEM_ACCESS_NO)
513 return 0;
514
515 FOREACH_ELEMENT(fd, m->restrict_fsaccess_link_fds) {
516 r = serialize_fd(f, fds, restrict_fsaccess_link_names[fd - m->restrict_fsaccess_link_fds], *fd);
517 if (r < 0)
518 return r;
519 }
520
521 r = serialize_fd(f, fds, "restrict-fsaccess-bss-map", m->restrict_fsaccess_bss_map_fd);
522 if (r < 0)
523 return r;
524
525 return 0;
526 }
527
528 #else /* ! BPF_FRAMEWORK || ! HAVE_LSM_INTEGRITY_TYPE */
529
530 bool dm_verity_require_signatures(void) {
531 return false;
532 }
533
534 bool bpf_restrict_fsaccess_supported(void) {
535 return false;
536 }
537
538 int bpf_restrict_fsaccess_setup(Manager *m) {
539 if (!MANAGER_IS_SYSTEM(m) || m->restrict_filesystem_access <= RESTRICT_FILESYSTEM_ACCESS_NO)
540 return 0;
541
542 return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
543 "bpf-restrict-fsaccess: RestrictFileSystemAccess= requested but BPF framework is not compiled in.");
544 }
545
546 int bpf_restrict_fsaccess_prepare(struct restrict_fsaccess_bpf **ret) {
547 return -EOPNOTSUPP;
548 }
549
550 int bpf_restrict_fsaccess_populate_guard(struct restrict_fsaccess_bpf *obj) {
551 return 0;
552 }
553
554 int bpf_restrict_fsaccess_close_initramfs_trust(Manager *m) {
555 return 0;
556 }
557
558 int bpf_restrict_fsaccess_serialize(Manager *m, FILE *f, FDSet *fds) {
559 return 0;
560 }
561
562 #endif