Debian Codegraph @

src/nsresourced/userns-restrict.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/stat.h>
4
5 #if HAVE_VMLINUX_H
6 #include "userns-restrict-skel.h"
7 #endif
8
9 #include "bpf-util.h"
10 #include "bpf-link.h"
11 #include "fd-util.h"
12 #include "log.h"
13 #include "lsm-util.h"
14 #include "mkdir.h"
15 #include "mountpoint-util.h"
16 #include "namespace-util.h"
17 #include "path-util.h"
18 #include "string-util.h"
19 #include "userns-restrict.h"
20
21 #define USERNS_MAX (16U*1024U)
22 #define MOUNTS_MAX 4096U
23
24 #define PROGRAM_LINK_PREFIX "/sys/fs/bpf/systemd/userns-restrict/programs"
25 #define MAP_LINK_PREFIX "/sys/fs/bpf/systemd/userns-restrict/maps"
26
27 struct userns_restrict_bpf *userns_restrict_bpf_free(struct userns_restrict_bpf *obj) {
28 #if HAVE_VMLINUX_H
29 userns_restrict_bpf__destroy(obj); /* this call is fine with NULL */
30 #endif
31 return NULL;
32
33 }
34
35 #if HAVE_VMLINUX_H
36 static int make_inner_hash_map(void) {
37 int fd;
38
39 fd = compat_bpf_map_create(
40 BPF_MAP_TYPE_HASH,
41 NULL,
42 sizeof(int),
43 sizeof(uint32_t),
44 MOUNTS_MAX,
45 NULL);
46 if (fd < 0)
47 return log_debug_errno(errno, "Failed to allocate inner BPF map: %m");
48
49 return fd;
50 }
51 #endif
52
53 int userns_restrict_install(
54 bool pin,
55 struct userns_restrict_bpf **ret) {
56
57 #if HAVE_VMLINUX_H
58 _cleanup_(userns_restrict_bpf_freep) struct userns_restrict_bpf *obj = NULL;
59 _cleanup_close_ int dummy_mnt_id_hash_fd = -EBADF;
60 int r;
61
62 r = lsm_supported("bpf");
63 if (r == -ENOPKG)
64 /* We propagate this as EOPNOTSUPP! */
65 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "bpf-lsm support not available, as securityfs is not mounted.");
66 if (r < 0)
67 return log_error_errno(r, "Failed to check if bpf-lsm support is available: %m");
68 if (r == 0)
69 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "bpf-lsm not supported, can't lock down user namespace.");
70
71 r = DLOPEN_BPF(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
72 if (r < 0)
73 return r;
74
75 /* bpf_object__next_map() is not available in libbpf pre-0.7.0, and we want to use it. */
76 if (!sym_bpf_object__next_map)
77 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libbpf too old for locking down user namespace.");
78
79 obj = userns_restrict_bpf__open();
80 if (!obj)
81 return log_error_errno(errno, "Failed to open userns_restrict BPF object: %m");
82
83 if (pin) {
84 struct bpf_map *map;
85
86 /* libbpf will only create one level of dirs. Let's create the rest */
87 (void) mkdir_p(MAP_LINK_PREFIX, 0755);
88 (void) mkdir_p(PROGRAM_LINK_PREFIX, 0755);
89
90 map = sym_bpf_object__next_map(obj->obj, NULL);
91 while (map) {
92 _cleanup_free_ char *fn = NULL;
93
94 fn = path_join(MAP_LINK_PREFIX, sym_bpf_map__name(map));
95 if (!fn)
96 return log_oom();
97
98 r = sym_bpf_map__set_pin_path(map, fn);
99 if (r < 0)
100 return log_error_errno(r, "Failed to set pin path to '%s': %m", fn);
101
102 map = sym_bpf_object__next_map(obj->obj, map);
103 }
104 }
105
106 r = sym_bpf_map__set_max_entries(obj->maps.userns_mnt_id_hash, USERNS_MAX);
107 if (r < 0)
108 return log_error_errno(r, "Failed to size userns/mnt_id hash table: %m");
109
110 r = sym_bpf_map__set_max_entries(obj->maps.userns_ringbuf, USERNS_MAX * sizeof(unsigned));
111 if (r < 0)
112 return log_error_errno(r, "Failed to size userns ring buffer: %m");
113
114 r = sym_bpf_map__set_max_entries(obj->maps.userns_setgroups_deny, USERNS_MAX);
115 if (r < 0)
116 return log_error_errno(r, "Failed to size userns setgroups deny hash table: %m");
117
118 /* Dummy map to satisfy the verifier */
119 dummy_mnt_id_hash_fd = make_inner_hash_map();
120 if (dummy_mnt_id_hash_fd < 0)
121 return dummy_mnt_id_hash_fd;
122
123 r = sym_bpf_map__set_inner_map_fd(obj->maps.userns_mnt_id_hash, dummy_mnt_id_hash_fd);
124 if (r < 0)
125 return log_error_errno(r, "Failed to set inner BPF map: %m");
126
127 r = userns_restrict_bpf__load(obj);
128 if (r < 0)
129 return log_error_errno(r, "Failed to load BPF object: %m");
130
131 for (int i = 0; i < obj->skeleton->prog_cnt; i++) {
132 _cleanup_(bpf_link_freep) struct bpf_link *link = NULL;
133 struct bpf_prog_skeleton *ps = obj->skeleton->progs + i;
134 _cleanup_free_ char *fn = NULL;
135 bool linked = false;
136 const char *e;
137
138 e = startswith(ps->name, "userns_restrict_");
139 assert(e);
140
141 if (pin) {
142 fn = path_join(PROGRAM_LINK_PREFIX, e);
143 if (!fn)
144 return log_oom();
145
146 link = sym_bpf_link__open(fn);
147 r = bpf_get_error_translated(link);
148 if (r < 0) {
149 if (r != -ENOENT)
150 return log_error_errno(r, "Unable to open pinned program link: %m");
151 link = NULL;
152 } else {
153 linked = true;
154 log_debug("userns-restrict BPF-LSM program %s already attached.", ps->name);
155 }
156 }
157
158 if (!link) {
159 link = sym_bpf_program__attach(*ps->prog);
160 r = bpf_get_error_translated(link);
161 if (r < 0)
162 return log_error_errno(r, "Failed to attach LSM BPF program: %m");
163
164 log_info("userns-restrict BPF-LSM program %s now attached.", ps->name);
165 }
166
167 if (pin && !linked) {
168 assert(fn);
169
170 r = sym_bpf_link__pin(link, fn);
171 if (r < 0)
172 return log_error_errno(r, "Failed to pin LSM attachment: %m");
173 }
174
175 *ps->link = TAKE_PTR(link);
176 }
177
178 if (pin) {
179 r = sym_bpf_object__pin_maps(obj->obj, NULL);
180 if (r < 0)
181 return log_error_errno(r, "Failed to pin BPF maps: %m");
182 }
183
184 if (ret)
185 *ret = TAKE_PTR(obj);
186
187 return 0;
188 #else
189 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "User Namespace Restriction BPF support disabled.");
190 #endif
191 }
192
193 int userns_restrict_put_by_inode(
194 struct userns_restrict_bpf *obj,
195 uint64_t userns_inode,
196 bool replace,
197 const int mount_fds[],
198 size_t n_mount_fds) {
199
200 #if HAVE_VMLINUX_H
201 _cleanup_close_ int inner_map_fd = -EBADF;
202 _cleanup_free_ int *mnt_ids = NULL;
203 uint64_t ino = userns_inode;
204 int r, outer_map_fd;
205
206 assert(obj);
207 assert(userns_inode != 0);
208 assert(n_mount_fds == 0 || mount_fds);
209
210 /* The BPF map type BPF_MAP_TYPE_HASH_OF_MAPS only supports 32bit keys, and user namespace inode
211 * numbers are 32bit too, even though ino_t is 64bit these days. Should we ever run into a 64bit
212 * inode let's refuse early, we can't support this with the current BPF code for now. */
213 if (userns_inode > UINT32_MAX)
214 return -EINVAL;
215
216 mnt_ids = new(int, n_mount_fds);
217 if (!mnt_ids)
218 return -ENOMEM;
219
220 for (size_t i = 0; i < n_mount_fds; i++) {
221 r = path_get_mnt_id_at(mount_fds[i], "", mnt_ids + i);
222 if (r < 0)
223 return log_debug_errno(r, "Failed to get mount ID: %m");
224 }
225
226 outer_map_fd = sym_bpf_map__fd(obj->maps.userns_mnt_id_hash);
227 if (outer_map_fd < 0)
228 return log_debug_errno(outer_map_fd, "Failed to get outer BPF map fd: %m");
229
230 if (replace) {
231 /* Add if missing, replace if already exists */
232 inner_map_fd = make_inner_hash_map();
233 if (inner_map_fd < 0)
234 return inner_map_fd;
235
236 r = sym_bpf_map_update_elem(outer_map_fd, &ino, &inner_map_fd, BPF_ANY);
237 if (r < 0)
238 return log_debug_errno(r, "Failed to replace map in inode hash: %m");
239 } else {
240 /* Let's add an entry for this userns inode if missing. If it exists just extend the existing map. We
241 * might race against each other, hence we try a couple of times */
242 for (size_t n_try = 10;; n_try--) {
243 uint32_t innermap_id;
244
245 if (n_try == 0)
246 return log_debug_errno(SYNTHETIC_ERRNO(EEXIST),
247 "Still cannot create inode entry in BPF map after 10 tries.");
248
249 r = sym_bpf_map_lookup_elem(outer_map_fd, &ino, &innermap_id);
250 if (r >= 0) {
251 inner_map_fd = sym_bpf_map_get_fd_by_id(innermap_id);
252 if (inner_map_fd < 0)
253 return log_debug_errno(inner_map_fd, "Failed to get file descriptor for inner map: %m");
254
255 break;
256 }
257 if (errno != ENOENT)
258 return log_debug_errno(errno, "Failed to look up inode hash entry: %m");
259
260 /* No entry for this user namespace yet. Let's create one */
261 inner_map_fd = make_inner_hash_map();
262 if (inner_map_fd < 0)
263 return inner_map_fd;
264
265 r = sym_bpf_map_update_elem(outer_map_fd, &ino, &inner_map_fd, BPF_NOEXIST);
266 if (r >= 0)
267 break;
268 if (errno != EEXIST)
269 return log_debug_errno(errno, "Failed to add mount ID list to inode hash: %m");
270 }
271 }
272
273 FOREACH_ARRAY(mntid, mnt_ids, n_mount_fds) {
274 uint32_t dummy_value = 1;
275
276 r = sym_bpf_map_update_elem(inner_map_fd, mntid, &dummy_value, BPF_ANY);
277 if (r < 0)
278 return log_debug_errno(r, "Failed to add mount ID to map: %m");
279
280 log_debug("Allowing mount %i on userns inode %" PRIu64, *mntid, ino);
281 }
282
283 return 0;
284 #else
285 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "User Namespace Restriction BPF support disabled.");
286 #endif
287 }
288
289 int userns_restrict_put_by_fd(
290 struct userns_restrict_bpf *obj,
291 int userns_fd,
292 bool replace,
293 const int mount_fds[],
294 size_t n_mount_fds) {
295
296 #if HAVE_VMLINUX_H
297 struct stat st;
298 int r;
299
300 assert(obj);
301 assert(userns_fd >= 0);
302 assert(n_mount_fds == 0 || mount_fds);
303
304 r = fd_is_namespace(userns_fd, NAMESPACE_USER);
305 if (r < 0)
306 return log_debug_errno(r, "Failed to determine if file descriptor is user namespace: %m");
307 if (r == 0)
308 return log_debug_errno(SYNTHETIC_ERRNO(EBADF), "User namespace fd is not actually a user namespace fd.");
309
310 if (fstat(userns_fd, &st) < 0)
311 return log_debug_errno(errno, "Failed to fstat() user namespace: %m");
312
313 return userns_restrict_put_by_inode(
314 obj,
315 st.st_ino,
316 replace,
317 mount_fds,
318 n_mount_fds);
319 #else
320 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "User Namespace Restriction BPF support disabled.");
321 #endif
322 }
323
324 int userns_restrict_reset_by_inode(struct userns_restrict_bpf *obj, uint64_t userns_inode) {
325
326 #if HAVE_VMLINUX_H
327 int r, outer_map_fd, setgroups_deny_fd;
328 unsigned u;
329
330 assert(obj);
331 assert(userns_inode != 0);
332
333 if (userns_inode > UINT32_MAX) /* inodes larger than 32bit are definitely not included in our map, exit early */
334 return 0;
335
336 outer_map_fd = sym_bpf_map__fd(obj->maps.userns_mnt_id_hash);
337 if (outer_map_fd < 0)
338 return log_debug_errno(outer_map_fd, "Failed to get outer BPF map fd: %m");
339
340 u = (uint32_t) userns_inode;
341
342 r = sym_bpf_map_delete_elem(outer_map_fd, &u);
343 if (r < 0)
344 return log_debug_errno(r, "Failed to remove entry for inode %" PRIu64 " from outer map: %m", userns_inode);
345
346 setgroups_deny_fd = sym_bpf_map__fd(obj->maps.userns_setgroups_deny);
347 if (setgroups_deny_fd < 0)
348 return log_debug_errno(setgroups_deny_fd, "Failed to get setgroups deny BPF map fd: %m");
349
350 r = sym_bpf_map_delete_elem(setgroups_deny_fd, &u);
351 if (r < 0 && r != -ENOENT)
352 return log_debug_errno(r, "Failed to remove entry for inode %" PRIu64 " from setgroups deny map: %m", userns_inode);
353
354 return 0;
355 #else
356 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "User Namespace Restriction BPF support disabled.");
357 #endif
358 }
359
360 int userns_restrict_setgroups_deny_by_inode(
361 struct userns_restrict_bpf *obj,
362 uint64_t userns_inode) {
363
364 #if HAVE_VMLINUX_H
365 int map_fd, r;
366 uint32_t dummy = 1;
367 unsigned ino;
368
369 assert(obj);
370 assert(userns_inode != 0);
371
372 /* The BPF map only supports 32bit keys, and user namespace inode numbers are 32bit too, even though
373 * ino_t is 64bit these days. Should we ever run into a 64bit inode let's refuse early. */
374 if (userns_inode > UINT32_MAX)
375 return -EINVAL;
376
377 ino = (unsigned) userns_inode;
378
379 map_fd = sym_bpf_map__fd(obj->maps.userns_setgroups_deny);
380 if (map_fd < 0)
381 return log_debug_errno(map_fd, "Failed to get setgroups deny BPF map fd: %m");
382
383 r = sym_bpf_map_update_elem(map_fd, &ino, &dummy, BPF_ANY);
384 if (r < 0)
385 return log_debug_errno(r, "Failed to add userns inode to setgroups deny map: %m");
386
387 log_debug("Denying setgroups() on userns inode %" PRIu64, userns_inode);
388
389 return 0;
390 #else
391 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "User Namespace Restriction BPF support disabled.");
392 #endif
393 }
394
395 int userns_restrict_setgroups_deny_by_fd(
396 struct userns_restrict_bpf *obj,
397 int userns_fd) {
398
399 #if HAVE_VMLINUX_H
400 struct stat st;
401 int r;
402
403 assert(obj);
404 assert(userns_fd >= 0);
405
406 r = fd_is_namespace(userns_fd, NAMESPACE_USER);
407 if (r < 0)
408 return log_debug_errno(r, "Failed to determine if file descriptor is user namespace: %m");
409 if (r == 0)
410 return log_debug_errno(SYNTHETIC_ERRNO(EBADF), "User namespace fd is not actually a user namespace fd.");
411
412 if (fstat(userns_fd, &st) < 0)
413 return log_debug_errno(errno, "Failed to fstat() user namespace: %m");
414
415 return userns_restrict_setgroups_deny_by_inode(obj, st.st_ino);
416 #else
417 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "User Namespace Restriction BPF support disabled.");
418 #endif
419 }