| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
|
| 5 |
#include "bpf-restrict-fs.h" |
| 6 |
#include "fd-util.h" |
| 7 |
#include "filesystems.h" |
| 8 |
#include "log.h" |
| 9 |
#include "lsm-util.h" |
| 10 |
#include "manager.h" |
| 11 |
#include "nulstr-util.h" |
| 12 |
#include "set.h" |
| 13 |
#include "stat-util.h" |
| 14 |
#include "unit.h" |
| 15 |
|
| 16 |
#if BPF_FRAMEWORK |
| 17 |
/* libbpf, clang and llc compile time dependencies are satisfied */ |
| 18 |
#include "bpf-util.h" |
| 19 |
#include "bpf-link.h" |
| 20 |
#include "restrict-fs-skel.h" |
| 21 |
|
| 22 |
#define CGROUP_HASH_SIZE_MAX 2048 |
| 23 |
|
| 24 |
static struct restrict_fs_bpf *restrict_fs_bpf_free(struct restrict_fs_bpf *obj) { |
| 25 |
/* restrict_fs_bpf__destroy handles object == NULL case */ |
| 26 |
restrict_fs_bpf__destroy(obj); |
| 27 |
|
| 28 |
return NULL; |
| 29 |
} |
| 30 |
|
| 31 |
DEFINE_TRIVIAL_CLEANUP_FUNC(struct restrict_fs_bpf *, restrict_fs_bpf_free); |
| 32 |
|
| 33 |
static int prepare_restrict_fs_bpf(struct restrict_fs_bpf **ret_obj) { |
| 34 |
_cleanup_(restrict_fs_bpf_freep) struct restrict_fs_bpf *obj = NULL; |
| 35 |
_cleanup_close_ int inner_map_fd = -EBADF; |
| 36 |
int r; |
| 37 |
|
| 38 |
assert(ret_obj); |
| 39 |
|
| 40 |
obj = restrict_fs_bpf__open(); |
| 41 |
if (!obj) |
| 42 |
return log_error_errno(errno, "bpf-restrict-fs: Failed to open BPF object: %m"); |
| 43 |
|
| 44 |
/* TODO Maybe choose a number based on runtime information? */ |
| 45 |
r = sym_bpf_map__set_max_entries(obj->maps.cgroup_hash, CGROUP_HASH_SIZE_MAX); |
| 46 |
assert(r <= 0); |
| 47 |
if (r < 0) |
| 48 |
return log_error_errno(r, "bpf-restrict-fs: Failed to resize BPF map '%s': %m", |
| 49 |
sym_bpf_map__name(obj->maps.cgroup_hash)); |
| 50 |
|
| 51 |
/* Dummy map to satisfy the verifier */ |
| 52 |
inner_map_fd = compat_bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(uint32_t), sizeof(uint32_t), 128U, NULL); |
| 53 |
if (inner_map_fd < 0) |
| 54 |
return log_error_errno(errno, "bpf-restrict-fs: Failed to create BPF map: %m"); |
| 55 |
|
| 56 |
r = sym_bpf_map__set_inner_map_fd(obj->maps.cgroup_hash, inner_map_fd); |
| 57 |
assert(r <= 0); |
| 58 |
if (r < 0) |
| 59 |
return log_error_errno(r, "bpf-restrict-fs: Failed to set inner map fd: %m"); |
| 60 |
|
| 61 |
r = restrict_fs_bpf__load(obj); |
| 62 |
assert(r <= 0); |
| 63 |
if (r < 0) |
| 64 |
return log_error_errno(r, "bpf-restrict-fs: Failed to load BPF object: %m"); |
| 65 |
|
| 66 |
*ret_obj = TAKE_PTR(obj); |
| 67 |
|
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
bool bpf_restrict_fs_supported(bool initialize) { |
| 72 |
_cleanup_(restrict_fs_bpf_freep) struct restrict_fs_bpf *obj = NULL; |
| 73 |
static int supported = -1; |
| 74 |
int r; |
| 75 |
|
| 76 |
if (supported >= 0) |
| 77 |
return supported; |
| 78 |
if (!initialize) |
| 79 |
return false; |
| 80 |
|
| 81 |
if (DLOPEN_BPF(LOG_WARNING, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED) < 0) |
| 82 |
return (supported = false); |
| 83 |
|
| 84 |
r = lsm_supported("bpf"); |
| 85 |
if (r == -ENOPKG) { |
| 86 |
log_debug_errno(r, "bpf-restrict-fs: securityfs not mounted, BPF LSM support not available."); |
| 87 |
return (supported = false); |
| 88 |
} |
| 89 |
if (r < 0) { |
| 90 |
log_warning_errno(r, "bpf-restrict-fs: Can't determine whether the BPF LSM module is used: %m"); |
| 91 |
return (supported = false); |
| 92 |
} |
| 93 |
if (r == 0) { |
| 94 |
log_info("bpf-restrict-fs: BPF LSM hook not enabled in the kernel, BPF LSM not supported."); |
| 95 |
return (supported = false); |
| 96 |
} |
| 97 |
|
| 98 |
r = prepare_restrict_fs_bpf(&obj); |
| 99 |
if (r < 0) |
| 100 |
return (supported = false); |
| 101 |
|
| 102 |
if (!bpf_can_link_lsm_program(obj->progs.restrict_filesystems)) { |
| 103 |
log_warning("bpf-restrict-fs: Failed to link program; assuming BPF LSM is not available."); |
| 104 |
return (supported = false); |
| 105 |
} |
| 106 |
|
| 107 |
return (supported = true); |
| 108 |
} |
| 109 |
|
| 110 |
int bpf_restrict_fs_setup(Manager *m) { |
| 111 |
_cleanup_(restrict_fs_bpf_freep) struct restrict_fs_bpf *obj = NULL; |
| 112 |
_cleanup_(bpf_link_freep) struct bpf_link *link = NULL; |
| 113 |
int r; |
| 114 |
|
| 115 |
assert(m); |
| 116 |
|
| 117 |
r = prepare_restrict_fs_bpf(&obj); |
| 118 |
if (r < 0) |
| 119 |
return r; |
| 120 |
|
| 121 |
link = sym_bpf_program__attach_lsm(obj->progs.restrict_filesystems); |
| 122 |
r = bpf_get_error_translated(link); |
| 123 |
if (r != 0) |
| 124 |
return log_error_errno(r, "bpf-restrict-fs: Failed to link '%s' LSM BPF program: %m", |
| 125 |
sym_bpf_program__name(obj->progs.restrict_filesystems)); |
| 126 |
|
| 127 |
log_info("bpf-restrict-fs: LSM BPF program attached"); |
| 128 |
|
| 129 |
obj->links.restrict_filesystems = TAKE_PTR(link); |
| 130 |
m->restrict_fs = TAKE_PTR(obj); |
| 131 |
|
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
int bpf_restrict_fs_update(const Set *filesystems, uint64_t cgroup_id, int outer_map_fd, bool allow_list) { |
| 136 |
uint32_t dummy_value = 1, zero = 0; |
| 137 |
const char *fs; |
| 138 |
const statfs_f_type_t *magic; |
| 139 |
int r; |
| 140 |
|
| 141 |
assert(filesystems); |
| 142 |
assert(outer_map_fd >= 0); |
| 143 |
|
| 144 |
int inner_map_fd = compat_bpf_map_create( |
| 145 |
BPF_MAP_TYPE_HASH, |
| 146 |
NULL, |
| 147 |
sizeof(uint32_t), |
| 148 |
sizeof(uint32_t), |
| 149 |
128U, /* Should be enough for all filesystem types */ |
| 150 |
NULL); |
| 151 |
if (inner_map_fd < 0) |
| 152 |
return log_error_errno(errno, "bpf-restrict-fs: Failed to create inner BPF map: %m"); |
| 153 |
|
| 154 |
if (sym_bpf_map_update_elem(outer_map_fd, &cgroup_id, &inner_map_fd, BPF_ANY) != 0) |
| 155 |
return log_error_errno(errno, "bpf-restrict-fs: Error populating BPF map: %m"); |
| 156 |
|
| 157 |
uint32_t allow = allow_list; |
| 158 |
|
| 159 |
/* Use key 0 to store whether this is an allow list or a deny list */ |
| 160 |
if (sym_bpf_map_update_elem(inner_map_fd, &zero, &allow, BPF_ANY) != 0) |
| 161 |
return log_error_errno(errno, "bpf-restrict-fs: Error initializing map: %m"); |
| 162 |
|
| 163 |
SET_FOREACH(fs, filesystems) { |
| 164 |
r = fs_type_from_string(fs, &magic); |
| 165 |
if (r < 0) { |
| 166 |
log_warning("bpf-restrict-fs: Invalid filesystem name '%s', ignoring.", fs); |
| 167 |
continue; |
| 168 |
} |
| 169 |
|
| 170 |
log_debug("bpf-restrict-fs: Restricting filesystem access to '%s'", fs); |
| 171 |
|
| 172 |
for (int i = 0; i < FILESYSTEM_MAGIC_MAX; i++) { |
| 173 |
if (magic[i] == 0) |
| 174 |
break; |
| 175 |
|
| 176 |
if (sym_bpf_map_update_elem(inner_map_fd, &magic[i], &dummy_value, BPF_ANY) != 0) { |
| 177 |
r = log_error_errno(errno, "bpf-restrict-fs: Failed to update BPF map: %m"); |
| 178 |
|
| 179 |
if (sym_bpf_map_delete_elem(outer_map_fd, &cgroup_id) != 0) |
| 180 |
log_debug_errno(errno, "bpf-restrict-fs: Failed to delete cgroup entry from BPF map: %m"); |
| 181 |
|
| 182 |
return r; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
int bpf_restrict_fs_cleanup(Unit *u) { |
| 191 |
CGroupRuntime *crt; |
| 192 |
|
| 193 |
assert(u); |
| 194 |
assert(u->manager); |
| 195 |
|
| 196 |
/* If we never successfully detected support, there is nothing to clean up. */ |
| 197 |
if (!bpf_restrict_fs_supported(/* initialize= */ false)) |
| 198 |
return 0; |
| 199 |
|
| 200 |
if (!u->manager->restrict_fs) |
| 201 |
return 0; |
| 202 |
|
| 203 |
crt = unit_get_cgroup_runtime(u); |
| 204 |
if (!crt) |
| 205 |
return 0; |
| 206 |
|
| 207 |
if (crt->cgroup_id == 0) |
| 208 |
return 0; |
| 209 |
|
| 210 |
int fd = sym_bpf_map__fd(u->manager->restrict_fs->maps.cgroup_hash); |
| 211 |
if (fd < 0) |
| 212 |
return log_unit_error_errno(u, errno, "bpf-restrict-fs: Failed to get BPF map fd: %m"); |
| 213 |
|
| 214 |
if (sym_bpf_map_delete_elem(fd, &crt->cgroup_id) != 0 && errno != ENOENT) |
| 215 |
return log_unit_debug_errno(u, errno, "bpf-restrict-fs: Failed to delete cgroup entry from LSM BPF map: %m"); |
| 216 |
|
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
int bpf_restrict_fs_map_fd(Unit *unit) { |
| 221 |
assert(unit); |
| 222 |
assert(unit->manager); |
| 223 |
|
| 224 |
if (!unit->manager->restrict_fs) |
| 225 |
return -ENOMEDIUM; |
| 226 |
|
| 227 |
return sym_bpf_map__fd(unit->manager->restrict_fs->maps.cgroup_hash); |
| 228 |
} |
| 229 |
|
| 230 |
void bpf_restrict_fs_destroy(struct restrict_fs_bpf *prog) { |
| 231 |
restrict_fs_bpf__destroy(prog); |
| 232 |
} |
| 233 |
#else /* ! BPF_FRAMEWORK */ |
| 234 |
bool bpf_restrict_fs_supported(bool initialize) { |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
int bpf_restrict_fs_setup(Manager *m) { |
| 239 |
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "bpf-restrict-fs: BPF framework is not supported."); |
| 240 |
} |
| 241 |
|
| 242 |
int bpf_restrict_fs_update(const Set *filesystems, uint64_t cgroup_id, int outer_map_fd, const bool allow_list) { |
| 243 |
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "bpf-restrict-fs: BPF framework is not supported."); |
| 244 |
} |
| 245 |
|
| 246 |
int bpf_restrict_fs_cleanup(Unit *u) { |
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
int bpf_restrict_fs_map_fd(Unit *unit) { |
| 251 |
return -ENOMEDIUM; |
| 252 |
} |
| 253 |
|
| 254 |
void bpf_restrict_fs_destroy(struct restrict_fs_bpf *prog) { |
| 255 |
return; |
| 256 |
} |
| 257 |
#endif |
| 258 |
|
| 259 |
int bpf_restrict_fs_parse_filesystem( |
| 260 |
const char *name, |
| 261 |
Set **filesystems, |
| 262 |
FilesystemParseFlags flags, |
| 263 |
const char *unit, |
| 264 |
const char *filename, |
| 265 |
unsigned line) { |
| 266 |
int r; |
| 267 |
|
| 268 |
assert(name); |
| 269 |
assert(filesystems); |
| 270 |
|
| 271 |
if (name[0] == '@') { |
| 272 |
const FilesystemSet *set; |
| 273 |
|
| 274 |
set = filesystem_set_find(name); |
| 275 |
if (!set) { |
| 276 |
log_syntax(unit, flags & FILESYSTEM_PARSE_LOG ? LOG_WARNING : LOG_DEBUG, filename, line, 0, |
| 277 |
"bpf-restrict-fs: Unknown filesystem group, ignoring: %s", name); |
| 278 |
return 0; |
| 279 |
} |
| 280 |
|
| 281 |
NULSTR_FOREACH(i, set->value) { |
| 282 |
/* Call ourselves again, for the group to parse. Note that we downgrade logging here |
| 283 |
* (i.e. take away the FILESYSTEM_PARSE_LOG flag) since any issues in the group table |
| 284 |
* are our own problem, not a problem in user configuration data and we shouldn't |
| 285 |
* pretend otherwise by complaining about them. */ |
| 286 |
r = bpf_restrict_fs_parse_filesystem(i, filesystems, flags &~ FILESYSTEM_PARSE_LOG, unit, filename, line); |
| 287 |
if (r < 0) |
| 288 |
return r; |
| 289 |
} |
| 290 |
} else { |
| 291 |
/* If we previously wanted to forbid access to a filesystem and now |
| 292 |
* we want to allow it, then remove it from the list. */ |
| 293 |
if (!(flags & FILESYSTEM_PARSE_INVERT) == !!(flags & FILESYSTEM_PARSE_ALLOW_LIST)) { |
| 294 |
r = set_put_strdup(filesystems, name); |
| 295 |
if (r == -ENOMEM) |
| 296 |
return flags & FILESYSTEM_PARSE_LOG ? log_oom() : -ENOMEM; |
| 297 |
if (r < 0 && r != -EEXIST) /* When already in set, ignore */ |
| 298 |
return r; |
| 299 |
} else |
| 300 |
free(set_remove(*filesystems, name)); |
| 301 |
} |
| 302 |
|
| 303 |
return 0; |
| 304 |
} |