]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/namespace.c
udev: update log messages
[thirdparty/systemd.git] / src / core / namespace.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
15ae422b
LP
2
3#include <errno.h>
e08f94ac 4#include <linux/loop.h>
07630cea 5#include <sched.h>
15ae422b 6#include <stdio.h>
07630cea 7#include <sys/mount.h>
07630cea 8#include <unistd.h>
25e870b5 9#include <linux/fs.h>
15ae422b 10
b5efdb8a 11#include "alloc-util.h"
10404d52 12#include "base-filesystem.h"
7f112f50 13#include "dev-setup.h"
93f59701
LB
14#include "env-util.h"
15#include "escape.h"
16#include "extension-release.h"
3ffd4af2 17#include "fd-util.h"
e5f10caf 18#include "format-util.h"
d944dc95 19#include "fs-util.h"
e908468b 20#include "label.h"
b3d13314 21#include "list.h"
915e6d16 22#include "loop-util.h"
07630cea 23#include "loopback-setup.h"
07630cea 24#include "mkdir.h"
4349cd7c 25#include "mount-util.h"
049af8ad 26#include "mountpoint-util.h"
0cb8e3d1 27#include "namespace-util.h"
3ffd4af2 28#include "namespace.h"
54c2459d 29#include "nsflags.h"
d8b4d14d 30#include "nulstr-util.h"
93f59701 31#include "os-util.h"
07630cea 32#include "path-util.h"
d7b8eec7 33#include "selinux-util.h"
2583fbea 34#include "socket-util.h"
760877e9 35#include "sort-util.h"
36ce7110 36#include "stat-util.h"
8b43440b 37#include "string-table.h"
07630cea
LP
38#include "string-util.h"
39#include "strv.h"
a652f050 40#include "tmpfile-util.h"
affb60b1 41#include "umask-util.h"
ee104e11 42#include "user-util.h"
15ae422b 43
737ba3c8 44#define DEV_MOUNT_OPTIONS (MS_NOSUID|MS_STRICTATIME|MS_NOEXEC)
45
c17ec25e 46typedef enum MountMode {
15ae422b
LP
47 /* This is ordered by priority! */
48 INACCESSIBLE,
93f59701 49 OVERLAY_MOUNT,
b3d13314 50 MOUNT_IMAGES,
d2d6c096
LP
51 BIND_MOUNT,
52 BIND_MOUNT_RECURSIVE,
ac0930c8 53 PRIVATE_TMP,
56a13a49 54 PRIVATE_TMP_READONLY,
7f112f50 55 PRIVATE_DEV,
5d997827 56 BIND_DEV,
6c47cd7d 57 EMPTY_DIR,
5d997827
LP
58 SYSFS,
59 PROCFS,
94293d65 60 RUN,
5d997827 61 READONLY,
59eeb84b 62 READWRITE,
ddc155b2
TM
63 NOEXEC,
64 EXEC,
2abd4e38 65 TMPFS,
93f59701 66 EXTENSION_IMAGES, /* Mounted outside the root directory, and used by subsequent mounts */
80271a44 67 MQUEUEFS,
1e05071d 68 READWRITE_IMPLICIT, /* Should have the lowest priority. */
5beb8688 69 _MOUNT_MODE_MAX,
c17ec25e 70} MountMode;
15ae422b 71
34de407a 72typedef struct MountEntry {
5327c910 73 const char *path_const; /* Memory allocated on stack or static */
cfbeb4ef 74 MountMode mode:5;
5327c910
LP
75 bool ignore:1; /* Ignore if path does not exist? */
76 bool has_prefix:1; /* Already is prefixed by the root dir? */
cfbeb4ef 77 bool read_only:1; /* Shall this mount point be read-only? */
9ce4e4b0 78 bool nosuid:1; /* Shall set MS_NOSUID on the mount itself */
ddc155b2
TM
79 bool noexec:1; /* Shall set MS_NOEXEC on the mount itself */
80 bool exec:1; /* Shall clear MS_NOEXEC on the mount itself */
088696fe 81 bool applied:1; /* Already applied */
55fe7432 82 char *path_malloc; /* Use this instead of 'path_const' if we had to allocate memory */
809ceb82
LB
83 const char *unprefixed_path_const; /* If the path was amended with a prefix, these will save the original */
84 char *unprefixed_path_malloc;
b3d13314 85 const char *source_const; /* The source path, for bind mounts or images */
d2d6c096 86 char *source_malloc;
2abd4e38
YW
87 const char *options_const;/* Mount options for tmpfs */
88 char *options_malloc;
89 unsigned long flags; /* Mount flags used by EMPTY_DIR and TMPFS. Do not include MS_RDONLY here, but please use read_only. */
088696fe 90 unsigned n_followed;
427353f6 91 LIST_HEAD(MountOptions, image_options);
34de407a 92} MountEntry;
15ae422b 93
94293d65 94/* If MountAPIVFS= is used, let's mount /sys, /proc, /dev and /run into the it, but only as a fallback if the user hasn't mounted
3fe91079 95 * something there already. These mounts are hence overridden by any other explicitly configured mounts. */
5d997827
LP
96static const MountEntry apivfs_table[] = {
97 { "/proc", PROCFS, false },
98 { "/dev", BIND_DEV, false },
99 { "/sys", SYSFS, false },
94293d65 100 { "/run", RUN, false, .options_const = "mode=755" TMPFS_LIMITS_RUN, .flags = MS_NOSUID|MS_NODEV|MS_STRICTATIME },
5d997827 101};
f471b2af 102
11a30cec 103/* ProtectKernelTunables= option and the related filesystem APIs */
34de407a 104static const MountEntry protect_kernel_tunables_table[] = {
1e05071d
YW
105 { "/proc/acpi", READONLY, true },
106 { "/proc/apm", READONLY, true }, /* Obsolete API, there's no point in permitting access to this, ever */
107 { "/proc/asound", READONLY, true },
108 { "/proc/bus", READONLY, true },
109 { "/proc/fs", READONLY, true },
110 { "/proc/irq", READONLY, true },
111 { "/proc/kallsyms", INACCESSIBLE, true },
112 { "/proc/kcore", INACCESSIBLE, true },
113 { "/proc/latency_stats", READONLY, true },
114 { "/proc/mtrr", READONLY, true },
115 { "/proc/scsi", READONLY, true },
4e399953 116 { "/proc/sys", READONLY, true },
1e05071d
YW
117 { "/proc/sysrq-trigger", READONLY, true },
118 { "/proc/timer_stats", READONLY, true },
119 { "/sys", READONLY, false },
120 { "/sys/fs/bpf", READONLY, true },
121 { "/sys/fs/cgroup", READWRITE_IMPLICIT, false }, /* READONLY is set by ProtectControlGroups= option */
122 { "/sys/fs/selinux", READWRITE_IMPLICIT, true },
123 { "/sys/kernel/debug", READONLY, true },
124 { "/sys/kernel/tracing", READONLY, true },
11a30cec
DH
125};
126
c575770b 127/* ProtectKernelModules= option */
34de407a 128static const MountEntry protect_kernel_modules_table[] = {
349cc4a5 129#if HAVE_SPLIT_USR
c6232fb0 130 { "/lib/modules", INACCESSIBLE, true },
c575770b 131#endif
c6232fb0 132 { "/usr/lib/modules", INACCESSIBLE, true },
c575770b
DH
133};
134
94a7b275
KK
135/* ProtectKernelLogs= option */
136static const MountEntry protect_kernel_logs_table[] = {
137 { "/proc/kmsg", INACCESSIBLE, true },
138 { "/dev/kmsg", INACCESSIBLE, true },
139};
140
b6c432ca
DH
141/*
142 * ProtectHome=read-only table, protect $HOME and $XDG_RUNTIME_DIR and rest of
143 * system should be protected by ProtectSystem=
144 */
34de407a 145static const MountEntry protect_home_read_only_table[] = {
c6232fb0
LP
146 { "/home", READONLY, true },
147 { "/run/user", READONLY, true },
148 { "/root", READONLY, true },
b6c432ca
DH
149};
150
e4da7d8c
YW
151/* ProtectHome=tmpfs table */
152static const MountEntry protect_home_tmpfs_table[] = {
7d85383e
TM
153 { "/home", TMPFS, true, .read_only = true, .options_const = "mode=0755" TMPFS_LIMITS_EMPTY_OR_ALMOST, .flags = MS_NODEV|MS_STRICTATIME },
154 { "/run/user", TMPFS, true, .read_only = true, .options_const = "mode=0755" TMPFS_LIMITS_EMPTY_OR_ALMOST, .flags = MS_NODEV|MS_STRICTATIME },
155 { "/root", TMPFS, true, .read_only = true, .options_const = "mode=0700" TMPFS_LIMITS_EMPTY_OR_ALMOST, .flags = MS_NODEV|MS_STRICTATIME },
e4da7d8c
YW
156};
157
b6c432ca 158/* ProtectHome=yes table */
34de407a 159static const MountEntry protect_home_yes_table[] = {
c6232fb0
LP
160 { "/home", INACCESSIBLE, true },
161 { "/run/user", INACCESSIBLE, true },
162 { "/root", INACCESSIBLE, true },
b6c432ca
DH
163};
164
f471b2af 165/* ProtectSystem=yes table */
34de407a 166static const MountEntry protect_system_yes_table[] = {
c6232fb0
LP
167 { "/usr", READONLY, false },
168 { "/boot", READONLY, true },
169 { "/efi", READONLY, true },
7486f305
AB
170#if HAVE_SPLIT_USR
171 { "/lib", READONLY, true },
172 { "/lib64", READONLY, true },
173 { "/bin", READONLY, true },
671f0f8d 174# if HAVE_SPLIT_BIN
7486f305 175 { "/sbin", READONLY, true },
671f0f8d 176# endif
7486f305 177#endif
f471b2af
DH
178};
179
180/* ProtectSystem=full includes ProtectSystem=yes */
34de407a 181static const MountEntry protect_system_full_table[] = {
c6232fb0
LP
182 { "/usr", READONLY, false },
183 { "/boot", READONLY, true },
184 { "/efi", READONLY, true },
185 { "/etc", READONLY, false },
7486f305
AB
186#if HAVE_SPLIT_USR
187 { "/lib", READONLY, true },
188 { "/lib64", READONLY, true },
189 { "/bin", READONLY, true },
671f0f8d 190# if HAVE_SPLIT_BIN
7486f305 191 { "/sbin", READONLY, true },
671f0f8d 192# endif
7486f305 193#endif
f471b2af
DH
194};
195
196/*
197 * ProtectSystem=strict table. In this strict mode, we mount everything
198 * read-only, except for /proc, /dev, /sys which are the kernel API VFS,
199 * which are left writable, but PrivateDevices= + ProtectKernelTunables=
200 * protect those, and these options should be fully orthogonal.
201 * (And of course /home and friends are also left writable, as ProtectHome=
202 * shall manage those, orthogonally).
203 */
34de407a 204static const MountEntry protect_system_strict_table[] = {
1e05071d
YW
205 { "/", READONLY, false },
206 { "/proc", READWRITE_IMPLICIT, false }, /* ProtectKernelTunables= */
207 { "/sys", READWRITE_IMPLICIT, false }, /* ProtectKernelTunables= */
208 { "/dev", READWRITE_IMPLICIT, false }, /* PrivateDevices= */
209 { "/home", READWRITE_IMPLICIT, true }, /* ProtectHome= */
210 { "/run/user", READWRITE_IMPLICIT, true }, /* ProtectHome= */
211 { "/root", READWRITE_IMPLICIT, true }, /* ProtectHome= */
f471b2af
DH
212};
213
5beb8688
YW
214static const char * const mount_mode_table[_MOUNT_MODE_MAX] = {
215 [INACCESSIBLE] = "inaccessible",
93f59701 216 [OVERLAY_MOUNT] = "overlay",
5beb8688
YW
217 [BIND_MOUNT] = "bind",
218 [BIND_MOUNT_RECURSIVE] = "rbind",
219 [PRIVATE_TMP] = "private-tmp",
220 [PRIVATE_DEV] = "private-dev",
221 [BIND_DEV] = "bind-dev",
222 [EMPTY_DIR] = "empty",
223 [SYSFS] = "sysfs",
224 [PROCFS] = "procfs",
225 [READONLY] = "read-only",
226 [READWRITE] = "read-write",
227 [TMPFS] = "tmpfs",
b3d13314 228 [MOUNT_IMAGES] = "mount-images",
5beb8688 229 [READWRITE_IMPLICIT] = "rw-implicit",
ddc155b2
TM
230 [EXEC] = "exec",
231 [NOEXEC] = "noexec",
80271a44 232 [MQUEUEFS] = "mqueuefs",
5beb8688
YW
233};
234
235DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(mount_mode, MountMode);
236
34de407a 237static const char *mount_entry_path(const MountEntry *p) {
f0a4feb0
DH
238 assert(p);
239
5327c910
LP
240 /* Returns the path of this bind mount. If the malloc()-allocated ->path_buffer field is set we return that,
241 * otherwise the stack/static ->path field is returned. */
f0a4feb0 242
5327c910 243 return p->path_malloc ?: p->path_const;
f0a4feb0
DH
244}
245
809ceb82
LB
246static const char *mount_entry_unprefixed_path(const MountEntry *p) {
247 assert(p);
248
249 /* Returns the unprefixed path (ie: before prefix_where_needed() ran), if any */
250
251 return p->unprefixed_path_malloc ?: p->unprefixed_path_const ?: mount_entry_path(p);
252}
253
254static void mount_entry_consume_prefix(MountEntry *p, char *new_path) {
255 assert(p);
256 assert(p->path_malloc || p->path_const);
257 assert(new_path);
258
259 /* Saves current path in unprefixed_ variable, and takes over new_path */
260
261 free_and_replace(p->unprefixed_path_malloc, p->path_malloc);
262 /* If we didn't have a path on the heap, then it's a static one */
263 if (!p->unprefixed_path_malloc)
264 p->unprefixed_path_const = p->path_const;
265 p->path_malloc = new_path;
266 p->has_prefix = true;
267}
268
34de407a 269static bool mount_entry_read_only(const MountEntry *p) {
cfbeb4ef
LP
270 assert(p);
271
56a13a49 272 return p->read_only || IN_SET(p->mode, READONLY, INACCESSIBLE, PRIVATE_TMP_READONLY);
cfbeb4ef
LP
273}
274
ddc155b2
TM
275static bool mount_entry_noexec(const MountEntry *p) {
276 assert(p);
277
b33cd6b3 278 return p->noexec || IN_SET(p->mode, NOEXEC, INACCESSIBLE);
ddc155b2
TM
279}
280
281static bool mount_entry_exec(const MountEntry *p) {
282 assert(p);
283
284 return p->exec || p->mode == EXEC;
285}
286
d2d6c096
LP
287static const char *mount_entry_source(const MountEntry *p) {
288 assert(p);
289
290 return p->source_malloc ?: p->source_const;
291}
292
2abd4e38
YW
293static const char *mount_entry_options(const MountEntry *p) {
294 assert(p);
295
296 return p->options_malloc ?: p->options_const;
297}
298
1eb7e08e
LP
299static void mount_entry_done(MountEntry *p) {
300 assert(p);
301
302 p->path_malloc = mfree(p->path_malloc);
809ceb82 303 p->unprefixed_path_malloc = mfree(p->unprefixed_path_malloc);
1eb7e08e 304 p->source_malloc = mfree(p->source_malloc);
2abd4e38 305 p->options_malloc = mfree(p->options_malloc);
427353f6 306 p->image_options = mount_options_free_all(p->image_options);
1eb7e08e
LP
307}
308
d18aff04 309static int append_access_mounts(MountEntry **p, char **strv, MountMode mode, bool forcibly_require_prefix) {
15ae422b
LP
310 char **i;
311
613b411c
LP
312 assert(p);
313
1e05071d 314 /* Adds a list of user-supplied READWRITE/READWRITE_IMPLICIT/READONLY/INACCESSIBLE entries */
5327c910 315
15ae422b 316 STRV_FOREACH(i, strv) {
5327c910
LP
317 bool ignore = false, needs_prefix = false;
318 const char *e = *i;
15ae422b 319
5327c910
LP
320 /* Look for any prefixes */
321 if (startswith(e, "-")) {
322 e++;
9c94d52e 323 ignore = true;
ea92ae33 324 }
5327c910
LP
325 if (startswith(e, "+")) {
326 e++;
327 needs_prefix = true;
328 }
ea92ae33 329
baaa35ad
ZJS
330 if (!path_is_absolute(e))
331 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
332 "Path is not absolute: %s", e);
15ae422b 333
34de407a 334 *((*p)++) = (MountEntry) {
5327c910
LP
335 .path_const = e,
336 .mode = mode,
337 .ignore = ignore,
d18aff04 338 .has_prefix = !needs_prefix && !forcibly_require_prefix,
5327c910 339 };
15ae422b
LP
340 }
341
342 return 0;
343}
344
6c47cd7d
LP
345static int append_empty_dir_mounts(MountEntry **p, char **strv) {
346 char **i;
347
348 assert(p);
349
350 /* Adds tmpfs mounts to provide readable but empty directories. This is primarily used to implement the
351 * "/private/" boundary directories for DynamicUser=1. */
352
353 STRV_FOREACH(i, strv) {
354
355 *((*p)++) = (MountEntry) {
356 .path_const = *i,
357 .mode = EMPTY_DIR,
358 .ignore = false,
6c47cd7d 359 .read_only = true,
7d85383e 360 .options_const = "mode=755" TMPFS_LIMITS_EMPTY_OR_ALMOST,
2abd4e38 361 .flags = MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME,
6c47cd7d
LP
362 };
363 }
364
365 return 0;
366}
367
da6053d0 368static int append_bind_mounts(MountEntry **p, const BindMount *binds, size_t n) {
d2d6c096
LP
369 assert(p);
370
fe96c0f8 371 for (size_t i = 0; i < n; i++) {
d2d6c096
LP
372 const BindMount *b = binds + i;
373
374 *((*p)++) = (MountEntry) {
375 .path_const = b->destination,
376 .mode = b->recursive ? BIND_MOUNT_RECURSIVE : BIND_MOUNT,
377 .read_only = b->read_only,
9ce4e4b0 378 .nosuid = b->nosuid,
d2d6c096 379 .source_const = b->source,
4ca763a9 380 .ignore = b->ignore_enoent,
d2d6c096
LP
381 };
382 }
383
384 return 0;
385}
386
b3d13314
LB
387static int append_mount_images(MountEntry **p, const MountImage *mount_images, size_t n) {
388 assert(p);
389
390 for (size_t i = 0; i < n; i++) {
391 const MountImage *m = mount_images + i;
392
393 *((*p)++) = (MountEntry) {
394 .path_const = m->destination,
395 .mode = MOUNT_IMAGES,
396 .source_const = m->source,
427353f6 397 .image_options = m->mount_options,
b3d13314
LB
398 .ignore = m->ignore_enoent,
399 };
400 }
401
402 return 0;
403}
404
93f59701
LB
405static int append_extension_images(
406 MountEntry **p,
407 const char *root,
408 const char *extension_dir,
409 char **hierarchies,
410 const MountImage *mount_images,
411 size_t n) {
412
413 _cleanup_strv_free_ char **overlays = NULL;
414 char **hierarchy;
415 int r;
416
417 assert(p);
418 assert(extension_dir);
419
420 if (n == 0)
421 return 0;
422
423 /* Prepare a list of overlays, that will have as each element a string suitable for being
7a7d2f16 424 * passed as a lowerdir= parameter, so start with the hierarchy on the root.
93f59701
LB
425 * The overlays vector will have the same number of elements and will correspond to the
426 * hierarchies vector, so they can be iterated upon together. */
427 STRV_FOREACH(hierarchy, hierarchies) {
428 _cleanup_free_ char *prefixed_hierarchy = NULL;
429
430 prefixed_hierarchy = path_join(root, *hierarchy);
431 if (!prefixed_hierarchy)
432 return -ENOMEM;
433
434 r = strv_consume(&overlays, TAKE_PTR(prefixed_hierarchy));
435 if (r < 0)
436 return r;
437 }
438
439 /* First, prepare a mount for each image, but these won't be visible to the unit, instead
440 * they will be mounted in our propagate directory, and used as a source for the overlay. */
441 for (size_t i = 0; i < n; i++) {
442 _cleanup_free_ char *mount_point = NULL;
443 const MountImage *m = mount_images + i;
444
445 r = asprintf(&mount_point, "%s/%zu", extension_dir, i);
446 if (r < 0)
447 return -ENOMEM;
448
449 for (size_t j = 0; hierarchies && hierarchies[j]; ++j) {
450 _cleanup_free_ char *prefixed_hierarchy = NULL, *escaped = NULL, *lowerdir = NULL;
451
452 prefixed_hierarchy = path_join(mount_point, hierarchies[j]);
453 if (!prefixed_hierarchy)
454 return -ENOMEM;
455
456 escaped = shell_escape(prefixed_hierarchy, ",:");
457 if (!escaped)
458 return -ENOMEM;
459
460 /* Note that lowerdir= parameters are in 'reverse' order, so the
461 * top-most directory in the overlay comes first in the list. */
462 lowerdir = strjoin(escaped, ":", overlays[j]);
463 if (!lowerdir)
464 return -ENOMEM;
465
466 free_and_replace(overlays[j], lowerdir);
467 }
468
469 *((*p)++) = (MountEntry) {
470 .path_malloc = TAKE_PTR(mount_point),
471 .image_options = m->mount_options,
472 .ignore = m->ignore_enoent,
473 .source_const = m->source,
474 .mode = EXTENSION_IMAGES,
475 .has_prefix = true,
476 };
477 }
478
479 /* Then, for each hierarchy, prepare an overlay with the list of lowerdir= strings
480 * set up earlier. */
481 for (size_t i = 0; hierarchies && hierarchies[i]; ++i) {
482 _cleanup_free_ char *prefixed_hierarchy = NULL;
483
484 prefixed_hierarchy = path_join(root, hierarchies[i]);
485 if (!prefixed_hierarchy)
486 return -ENOMEM;
487
488 *((*p)++) = (MountEntry) {
489 .path_malloc = TAKE_PTR(prefixed_hierarchy),
490 .options_malloc = TAKE_PTR(overlays[i]),
491 .mode = OVERLAY_MOUNT,
492 .has_prefix = true,
493 .ignore = true, /* If the source image doesn't set the ignore bit it will fail earlier. */
494 };
495 }
496
497 return 0;
498}
499
da6053d0 500static int append_tmpfs_mounts(MountEntry **p, const TemporaryFileSystem *tmpfs, size_t n) {
2abd4e38
YW
501 assert(p);
502
b67ec8e5 503 for (size_t i = 0; i < n; i++) {
2abd4e38
YW
504 const TemporaryFileSystem *t = tmpfs + i;
505 _cleanup_free_ char *o = NULL, *str = NULL;
ad8e66dc 506 unsigned long flags;
2abd4e38 507 bool ro = false;
b67ec8e5 508 int r;
2abd4e38 509
baaa35ad
ZJS
510 if (!path_is_absolute(t->path))
511 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
512 "Path is not absolute: %s",
513 t->path);
2abd4e38 514
b67ec8e5 515 str = strjoin("mode=0755" NESTED_TMPFS_LIMITS ",", t->options);
ad8e66dc
AJ
516 if (!str)
517 return -ENOMEM;
2abd4e38 518
ad8e66dc
AJ
519 r = mount_option_mangle(str, MS_NODEV|MS_STRICTATIME, &flags, &o);
520 if (r < 0)
521 return log_debug_errno(r, "Failed to parse mount option '%s': %m", str);
2abd4e38 522
ad8e66dc
AJ
523 ro = flags & MS_RDONLY;
524 if (ro)
525 flags ^= MS_RDONLY;
2abd4e38
YW
526
527 *((*p)++) = (MountEntry) {
528 .path_const = t->path,
529 .mode = TMPFS,
530 .read_only = ro,
ad8e66dc 531 .options_malloc = TAKE_PTR(o),
2abd4e38
YW
532 .flags = flags,
533 };
2abd4e38
YW
534 }
535
536 return 0;
537}
538
da6053d0 539static int append_static_mounts(MountEntry **p, const MountEntry *mounts, size_t n, bool ignore_protect) {
11a30cec 540 assert(p);
f471b2af 541 assert(mounts);
11a30cec 542
5327c910 543 /* Adds a list of static pre-defined entries */
f471b2af 544
fe96c0f8 545 for (size_t i = 0; i < n; i++)
34de407a
LP
546 *((*p)++) = (MountEntry) {
547 .path_const = mount_entry_path(mounts+i),
5327c910
LP
548 .mode = mounts[i].mode,
549 .ignore = mounts[i].ignore || ignore_protect,
550 };
f471b2af
DH
551
552 return 0;
553}
554
34de407a 555static int append_protect_home(MountEntry **p, ProtectHome protect_home, bool ignore_protect) {
c575770b
DH
556 assert(p);
557
5327c910 558 switch (protect_home) {
b6c432ca 559
5327c910 560 case PROTECT_HOME_NO:
b6c432ca
DH
561 return 0;
562
b6c432ca 563 case PROTECT_HOME_READ_ONLY:
5327c910
LP
564 return append_static_mounts(p, protect_home_read_only_table, ELEMENTSOF(protect_home_read_only_table), ignore_protect);
565
e4da7d8c
YW
566 case PROTECT_HOME_TMPFS:
567 return append_static_mounts(p, protect_home_tmpfs_table, ELEMENTSOF(protect_home_tmpfs_table), ignore_protect);
568
b6c432ca 569 case PROTECT_HOME_YES:
5327c910
LP
570 return append_static_mounts(p, protect_home_yes_table, ELEMENTSOF(protect_home_yes_table), ignore_protect);
571
b6c432ca 572 default:
5327c910 573 assert_not_reached("Unexpected ProtectHome= value");
b6c432ca 574 }
b6c432ca
DH
575}
576
34de407a 577static int append_protect_system(MountEntry **p, ProtectSystem protect_system, bool ignore_protect) {
f471b2af
DH
578 assert(p);
579
5327c910
LP
580 switch (protect_system) {
581
582 case PROTECT_SYSTEM_NO:
f471b2af
DH
583 return 0;
584
f471b2af 585 case PROTECT_SYSTEM_STRICT:
5327c910
LP
586 return append_static_mounts(p, protect_system_strict_table, ELEMENTSOF(protect_system_strict_table), ignore_protect);
587
f471b2af 588 case PROTECT_SYSTEM_YES:
5327c910
LP
589 return append_static_mounts(p, protect_system_yes_table, ELEMENTSOF(protect_system_yes_table), ignore_protect);
590
f471b2af 591 case PROTECT_SYSTEM_FULL:
5327c910
LP
592 return append_static_mounts(p, protect_system_full_table, ELEMENTSOF(protect_system_full_table), ignore_protect);
593
f471b2af 594 default:
5327c910 595 assert_not_reached("Unexpected ProtectSystem= value");
f471b2af 596 }
11a30cec
DH
597}
598
93bab288 599static int mount_path_compare(const MountEntry *a, const MountEntry *b) {
a0827e2b 600 int d;
15ae422b 601
93f59701
LB
602 /* EXTENSION_IMAGES will be used by other mounts as a base, so sort them first
603 * regardless of the prefix - they are set up in the propagate directory anyway */
604 d = -CMP(a->mode == EXTENSION_IMAGES, b->mode == EXTENSION_IMAGES);
605 if (d != 0)
606 return d;
607
6ee1a919 608 /* If the paths are not equal, then order prefixes first */
93bab288 609 d = path_compare(mount_entry_path(a), mount_entry_path(b));
6ee1a919
LP
610 if (d != 0)
611 return d;
15ae422b 612
6ee1a919 613 /* If the paths are equal, check the mode */
93bab288 614 return CMP((int) a->mode, (int) b->mode);
15ae422b
LP
615}
616
da6053d0 617static int prefix_where_needed(MountEntry *m, size_t n, const char *root_directory) {
4a756839 618 /* Prefixes all paths in the bind mount table with the root directory if the entry needs that. */
5327c910 619
fe96c0f8
SS
620 assert(m || n == 0);
621
622 for (size_t i = 0; i < n; i++) {
5327c910
LP
623 char *s;
624
625 if (m[i].has_prefix)
626 continue;
627
c6134d3e 628 s = path_join(root_directory, mount_entry_path(m+i));
5327c910
LP
629 if (!s)
630 return -ENOMEM;
631
809ceb82 632 mount_entry_consume_prefix(&m[i], s);
5327c910
LP
633 }
634
635 return 0;
636}
637
da6053d0 638static void drop_duplicates(MountEntry *m, size_t *n) {
34de407a 639 MountEntry *f, *t, *previous;
15ae422b 640
c17ec25e 641 assert(m);
15ae422b 642 assert(n);
15ae422b 643
fe3c2583
LP
644 /* Drops duplicate entries. Expects that the array is properly ordered already. */
645
1d54cd5d 646 for (f = m, t = m, previous = NULL; f < m + *n; f++) {
15ae422b 647
fe3c2583 648 /* The first one wins (which is the one with the more restrictive mode), see mount_path_compare()
088696fe
LP
649 * above. Note that we only drop duplicates that haven't been mounted yet. */
650 if (previous &&
651 path_equal(mount_entry_path(f), mount_entry_path(previous)) &&
652 !f->applied && !previous->applied) {
5beb8688 653 log_debug("%s (%s) is duplicate.", mount_entry_path(f), mount_mode_to_string(f->mode));
ddc155b2
TM
654 /* Propagate the flags to the remaining entry */
655 previous->read_only = previous->read_only || mount_entry_read_only(f);
656 previous->noexec = previous->noexec || mount_entry_noexec(f);
657 previous->exec = previous->exec || mount_entry_exec(f);
1eb7e08e 658 mount_entry_done(f);
15ae422b 659 continue;
fe3c2583 660 }
15ae422b 661
e2d7c1a0 662 *t = *f;
15ae422b 663 previous = t;
fe3c2583
LP
664 t++;
665 }
666
667 *n = t - m;
668}
669
da6053d0 670static void drop_inaccessible(MountEntry *m, size_t *n) {
34de407a 671 MountEntry *f, *t;
fe3c2583
LP
672 const char *clear = NULL;
673
674 assert(m);
675 assert(n);
676
677 /* Drops all entries obstructed by another entry further up the tree. Expects that the array is properly
678 * ordered already. */
679
1d54cd5d 680 for (f = m, t = m; f < m + *n; f++) {
fe3c2583
LP
681
682 /* If we found a path set for INACCESSIBLE earlier, and this entry has it as prefix we should drop
683 * it, as inaccessible paths really should drop the entire subtree. */
34de407a
LP
684 if (clear && path_startswith(mount_entry_path(f), clear)) {
685 log_debug("%s is masked by %s.", mount_entry_path(f), clear);
1eb7e08e 686 mount_entry_done(f);
fe3c2583
LP
687 continue;
688 }
15ae422b 689
34de407a 690 clear = f->mode == INACCESSIBLE ? mount_entry_path(f) : NULL;
fe3c2583
LP
691
692 *t = *f;
15ae422b
LP
693 t++;
694 }
695
c17ec25e 696 *n = t - m;
15ae422b
LP
697}
698
da6053d0 699static void drop_nop(MountEntry *m, size_t *n) {
34de407a 700 MountEntry *f, *t;
7648a565
LP
701
702 assert(m);
703 assert(n);
704
705 /* Drops all entries which have an immediate parent that has the same type, as they are redundant. Assumes the
706 * list is ordered by prefixes. */
707
1d54cd5d 708 for (f = m, t = m; f < m + *n; f++) {
7648a565 709
1e05071d
YW
710 /* Only suppress such subtrees for READONLY, READWRITE and READWRITE_IMPLICIT entries */
711 if (IN_SET(f->mode, READONLY, READWRITE, READWRITE_IMPLICIT)) {
34de407a 712 MountEntry *p;
7648a565
LP
713 bool found = false;
714
715 /* Now let's find the first parent of the entry we are looking at. */
716 for (p = t-1; p >= m; p--) {
34de407a 717 if (path_startswith(mount_entry_path(f), mount_entry_path(p))) {
7648a565
LP
718 found = true;
719 break;
720 }
721 }
722
723 /* We found it, let's see if it's the same mode, if so, we can drop this entry */
724 if (found && p->mode == f->mode) {
5beb8688
YW
725 log_debug("%s (%s) is made redundant by %s (%s)",
726 mount_entry_path(f), mount_mode_to_string(f->mode),
727 mount_entry_path(p), mount_mode_to_string(p->mode));
1eb7e08e 728 mount_entry_done(f);
7648a565
LP
729 continue;
730 }
731 }
732
733 *t = *f;
734 t++;
735 }
736
737 *n = t - m;
738}
739
da6053d0 740static void drop_outside_root(const char *root_directory, MountEntry *m, size_t *n) {
34de407a 741 MountEntry *f, *t;
cd2902c9
LP
742
743 assert(m);
744 assert(n);
745
1d54cd5d 746 /* Nothing to do */
cd2902c9
LP
747 if (!root_directory)
748 return;
749
750 /* Drops all mounts that are outside of the root directory. */
751
1d54cd5d 752 for (f = m, t = m; f < m + *n; f++) {
cd2902c9 753
93f59701
LB
754 /* ExtensionImages bases are opened in /run/systemd/unit-extensions on the host */
755 if (f->mode != EXTENSION_IMAGES && !path_startswith(mount_entry_path(f), root_directory)) {
34de407a 756 log_debug("%s is outside of root directory.", mount_entry_path(f));
1eb7e08e 757 mount_entry_done(f);
cd2902c9
LP
758 continue;
759 }
760
761 *t = *f;
762 t++;
763 }
764
765 *n = t - m;
766}
767
b2a60844
LP
768static int clone_device_node(
769 const char *d,
770 const char *temporary_mount,
771 bool *make_devnode) {
772
773 _cleanup_free_ char *sl = NULL;
774 const char *dn, *bn, *t;
b5e99f23
ДГ
775 struct stat st;
776 int r;
777
414b304b 778 if (stat(d, &st) < 0) {
b2a60844
LP
779 if (errno == ENOENT) {
780 log_debug_errno(errno, "Device node '%s' to clone does not exist, ignoring.", d);
af984e13 781 return -ENXIO;
b2a60844
LP
782 }
783
784 return log_debug_errno(errno, "Failed to stat() device node '%s' to clone, ignoring: %m", d);
b5e99f23
ДГ
785 }
786
787 if (!S_ISBLK(st.st_mode) &&
baaa35ad
ZJS
788 !S_ISCHR(st.st_mode))
789 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
790 "Device node '%s' to clone is not a device node, ignoring.",
791 d);
b5e99f23 792
6f7f3a33 793 dn = strjoina(temporary_mount, d);
b5e99f23 794
b2a60844 795 /* First, try to create device node properly */
16498617
CB
796 if (*make_devnode) {
797 mac_selinux_create_file_prepare(d, st.st_mode);
798 r = mknod(dn, st.st_mode, st.st_rdev);
799 mac_selinux_create_file_clear();
b2a60844
LP
800 if (r >= 0)
801 goto add_symlink;
16498617
CB
802 if (errno != EPERM)
803 return log_debug_errno(errno, "mknod failed for %s: %m", d);
804
b2a60844 805 /* This didn't work, let's not try this again for the next iterations. */
16498617
CB
806 *make_devnode = false;
807 }
808
2aed63f4 809 /* We're about to fall back to bind-mounting the device
1acf344d
CG
810 * node. So create a dummy bind-mount target.
811 * Do not prepare device-node SELinux label (see issue 13762) */
16498617 812 r = mknod(dn, S_IFREG, 0);
16498617 813 if (r < 0 && errno != EEXIST)
b2a60844 814 return log_debug_errno(errno, "mknod() fallback failed for '%s': %m", d);
16498617 815
21935150
LP
816 /* Fallback to bind-mounting: The assumption here is that all used device nodes carry standard
817 * properties. Specifically, the devices nodes we bind-mount should either be owned by root:root or
818 * root:tty (e.g. /dev/tty, /dev/ptmx) and should not carry ACLs. */
819 r = mount_nofollow_verbose(LOG_DEBUG, d, dn, NULL, MS_BIND, NULL);
820 if (r < 0)
821 return r;
b2a60844
LP
822
823add_symlink:
824 bn = path_startswith(d, "/dev/");
825 if (!bn)
826 return 0;
827
828 /* Create symlinks like /dev/char/1:9 → ../urandom */
cbc056c8
ZJS
829 if (asprintf(&sl, "%s/dev/%s/%u:%u",
830 temporary_mount,
831 S_ISCHR(st.st_mode) ? "char" : "block",
832 major(st.st_rdev), minor(st.st_rdev)) < 0)
b2a60844
LP
833 return log_oom();
834
835 (void) mkdir_parents(sl, 0755);
836
837 t = strjoina("../", bn);
b2a60844 838 if (symlink(t, sl) < 0)
2e4a4fae 839 log_debug_errno(errno, "Failed to symlink '%s' to '%s', ignoring: %m", t, sl);
b5e99f23 840
af984e13 841 return 0;
b5e99f23
ДГ
842}
843
5d997827 844static int mount_private_dev(MountEntry *m) {
7f112f50
LP
845 static const char devnodes[] =
846 "/dev/null\0"
847 "/dev/zero\0"
848 "/dev/full\0"
849 "/dev/random\0"
850 "/dev/urandom\0"
851 "/dev/tty\0";
852
2b85f4e1 853 char temporary_mount[] = "/tmp/namespace-dev-XXXXXX";
63cc4c31 854 const char *d, *dev = NULL, *devpts = NULL, *devshm = NULL, *devhugepages = NULL, *devmqueue = NULL, *devlog = NULL, *devptmx = NULL;
16498617 855 bool can_mknod = true;
7f112f50
LP
856 _cleanup_umask_ mode_t u;
857 int r;
858
859 assert(m);
860
861 u = umask(0000);
862
2b85f4e1 863 if (!mkdtemp(temporary_mount))
2e4a4fae 864 return log_debug_errno(errno, "Failed to create temporary directory '%s': %m", temporary_mount);
2b85f4e1 865
63c372cb 866 dev = strjoina(temporary_mount, "/dev");
dc751688 867 (void) mkdir(dev, 0755);
21935150
LP
868 r = mount_nofollow_verbose(LOG_DEBUG, "tmpfs", dev, "tmpfs", DEV_MOUNT_OPTIONS, "mode=755" TMPFS_LIMITS_DEV);
869 if (r < 0)
2b85f4e1 870 goto fail;
21935150 871
c3151977
TM
872 r = label_fix_container(dev, "/dev", 0);
873 if (r < 0) {
874 log_debug_errno(errno, "Failed to fix label of '%s' as /dev: %m", dev);
875 goto fail;
876 }
2b85f4e1 877
63c372cb 878 devpts = strjoina(temporary_mount, "/dev/pts");
dc751688 879 (void) mkdir(devpts, 0755);
21935150
LP
880 r = mount_nofollow_verbose(LOG_DEBUG, "/dev/pts", devpts, NULL, MS_BIND, NULL);
881 if (r < 0)
2b85f4e1 882 goto fail;
2b85f4e1 883
2e4a4fae
YW
884 /* /dev/ptmx can either be a device node or a symlink to /dev/pts/ptmx.
885 * When /dev/ptmx a device node, /dev/pts/ptmx has 000 permissions making it inaccessible.
886 * Thus, in that case make a clone.
887 * In nspawn and other containers it will be a symlink, in that case make it a symlink. */
36ce7110 888 r = is_symlink("/dev/ptmx");
2e4a4fae
YW
889 if (r < 0) {
890 log_debug_errno(r, "Failed to detect whether /dev/ptmx is a symlink or not: %m");
3164e3cb 891 goto fail;
2e4a4fae 892 } else if (r > 0) {
414b304b
ДГ
893 devptmx = strjoina(temporary_mount, "/dev/ptmx");
894 if (symlink("pts/ptmx", devptmx) < 0) {
2e4a4fae 895 r = log_debug_errno(errno, "Failed to create a symlink '%s' to pts/ptmx: %m", devptmx);
414b304b
ДГ
896 goto fail;
897 }
898 } else {
16498617 899 r = clone_device_node("/dev/ptmx", temporary_mount, &can_mknod);
152c475f
LP
900 if (r < 0)
901 goto fail;
414b304b 902 }
e06b6479 903
63c372cb 904 devshm = strjoina(temporary_mount, "/dev/shm");
8d953682 905 (void) mkdir(devshm, 0755);
21935150
LP
906 r = mount_nofollow_verbose(LOG_DEBUG, "/dev/shm", devshm, NULL, MS_BIND, NULL);
907 if (r < 0)
2b85f4e1 908 goto fail;
2b85f4e1 909
63c372cb 910 devmqueue = strjoina(temporary_mount, "/dev/mqueue");
dc751688 911 (void) mkdir(devmqueue, 0755);
21935150 912 (void) mount_nofollow_verbose(LOG_DEBUG, "/dev/mqueue", devmqueue, NULL, MS_BIND, NULL);
2b85f4e1 913
63c372cb 914 devhugepages = strjoina(temporary_mount, "/dev/hugepages");
dc751688 915 (void) mkdir(devhugepages, 0755);
21935150 916 (void) mount_nofollow_verbose(LOG_DEBUG, "/dev/hugepages", devhugepages, NULL, MS_BIND, NULL);
2b85f4e1 917
63c372cb 918 devlog = strjoina(temporary_mount, "/dev/log");
2e4a4fae
YW
919 if (symlink("/run/systemd/journal/dev-log", devlog) < 0)
920 log_debug_errno(errno, "Failed to create a symlink '%s' to /run/systemd/journal/dev-log, ignoring: %m", devlog);
82d25240 921
7f112f50 922 NULSTR_FOREACH(d, devnodes) {
16498617 923 r = clone_device_node(d, temporary_mount, &can_mknod);
37b22b3b 924 /* ENXIO means the *source* is not a device file, skip creation in that case */
af984e13 925 if (r < 0 && r != -ENXIO)
2b85f4e1 926 goto fail;
7f112f50
LP
927 }
928
2e4a4fae
YW
929 r = dev_setup(temporary_mount, UID_INVALID, GID_INVALID);
930 if (r < 0)
105a1a36 931 log_debug_errno(r, "Failed to set up basic device tree at '%s', ignoring: %m", temporary_mount);
7f112f50 932
ee818b89
AC
933 /* Create the /dev directory if missing. It is more likely to be
934 * missing when the service is started with RootDirectory. This is
935 * consistent with mount units creating the mount points when missing.
936 */
34de407a 937 (void) mkdir_p_label(mount_entry_path(m), 0755);
ee818b89 938
9e5f8252 939 /* Unmount everything in old /dev */
2e4a4fae
YW
940 r = umount_recursive(mount_entry_path(m), 0);
941 if (r < 0)
942 log_debug_errno(r, "Failed to unmount directories below '%s', ignoring: %m", mount_entry_path(m));
943
21935150
LP
944 r = mount_nofollow_verbose(LOG_DEBUG, dev, mount_entry_path(m), NULL, MS_MOVE, NULL);
945 if (r < 0)
2b85f4e1 946 goto fail;
7f112f50 947
1019a48f
LP
948 (void) rmdir(dev);
949 (void) rmdir(temporary_mount);
7f112f50 950
2b85f4e1 951 return 0;
7f112f50 952
2b85f4e1
LP
953fail:
954 if (devpts)
21935150 955 (void) umount_verbose(LOG_DEBUG, devpts, UMOUNT_NOFOLLOW);
7f112f50 956
2b85f4e1 957 if (devshm)
21935150 958 (void) umount_verbose(LOG_DEBUG, devshm, UMOUNT_NOFOLLOW);
7f112f50 959
2b85f4e1 960 if (devhugepages)
21935150 961 (void) umount_verbose(LOG_DEBUG, devhugepages, UMOUNT_NOFOLLOW);
7f112f50 962
2b85f4e1 963 if (devmqueue)
21935150 964 (void) umount_verbose(LOG_DEBUG, devmqueue, UMOUNT_NOFOLLOW);
7f112f50 965
21935150 966 (void) umount_verbose(LOG_DEBUG, dev, UMOUNT_NOFOLLOW);
1019a48f
LP
967 (void) rmdir(dev);
968 (void) rmdir(temporary_mount);
7f112f50 969
2b85f4e1 970 return r;
7f112f50
LP
971}
972
2a2969fd 973static int mount_bind_dev(const MountEntry *m) {
5d997827
LP
974 int r;
975
976 assert(m);
977
978 /* Implements the little brother of mount_private_dev(): simply bind mounts the host's /dev into the service's
979 * /dev. This is only used when RootDirectory= is set. */
980
645767d6
LP
981 (void) mkdir_p_label(mount_entry_path(m), 0755);
982
5d997827
LP
983 r = path_is_mount_point(mount_entry_path(m), NULL, 0);
984 if (r < 0)
985 return log_debug_errno(r, "Unable to determine whether /dev is already mounted: %m");
986 if (r > 0) /* make this a NOP if /dev is already a mount point */
987 return 0;
988
21935150
LP
989 r = mount_nofollow_verbose(LOG_DEBUG, "/dev", mount_entry_path(m), NULL, MS_BIND|MS_REC, NULL);
990 if (r < 0)
991 return r;
5d997827
LP
992
993 return 1;
994}
995
2a2969fd 996static int mount_sysfs(const MountEntry *m) {
5d997827
LP
997 int r;
998
999 assert(m);
1000
645767d6
LP
1001 (void) mkdir_p_label(mount_entry_path(m), 0755);
1002
5d997827
LP
1003 r = path_is_mount_point(mount_entry_path(m), NULL, 0);
1004 if (r < 0)
1005 return log_debug_errno(r, "Unable to determine whether /sys is already mounted: %m");
1006 if (r > 0) /* make this a NOP if /sys is already a mount point */
1007 return 0;
1008
1009 /* Bind mount the host's version so that we get all child mounts of it, too. */
21935150
LP
1010 r = mount_nofollow_verbose(LOG_DEBUG, "/sys", mount_entry_path(m), NULL, MS_BIND|MS_REC, NULL);
1011 if (r < 0)
1012 return r;
5d997827
LP
1013
1014 return 1;
1015}
1016
4e399953 1017static int mount_procfs(const MountEntry *m, const NamespaceInfo *ns_info) {
61f8a7bd 1018 _cleanup_free_ char *opts = NULL;
4e399953 1019 const char *entry_path;
61f8a7bd 1020 int r, n;
5d997827
LP
1021
1022 assert(m);
4e399953 1023 assert(ns_info);
5d997827 1024
4e399953
LP
1025 if (ns_info->protect_proc != PROTECT_PROC_DEFAULT ||
1026 ns_info->proc_subset != PROC_SUBSET_ALL) {
4e399953
LP
1027
1028 /* Starting with kernel 5.8 procfs' hidepid= logic is truly per-instance (previously it
1029 * pretended to be per-instance but actually was per-namespace), hence let's make use of it
1030 * if requested. To make sure this logic succeeds only on kernels where hidepid= is
1031 * per-instance, we'll exclusively use the textual value for hidepid=, since support was
1032 * added in the same commit: if it's supported it is thus also per-instance. */
1033
1034 opts = strjoin("hidepid=",
1035 ns_info->protect_proc == PROTECT_PROC_DEFAULT ? "off" :
1036 protect_proc_to_string(ns_info->protect_proc),
1037 ns_info->proc_subset == PROC_SUBSET_PID ? ",subset=pid" : "");
1038 if (!opts)
1039 return -ENOMEM;
4e399953
LP
1040 }
1041
61f8a7bd
YW
1042 entry_path = mount_entry_path(m);
1043 (void) mkdir_p_label(entry_path, 0755);
1044
1045 /* Mount a new instance, so that we get the one that matches our user namespace, if we are running in
1046 * one. i.e we don't reuse existing mounts here under any condition, we want a new instance owned by
1047 * our user namespace and with our hidepid= settings applied. Hence, let's get rid of everything
1048 * mounted on /proc/ first. */
1049
1050 n = umount_recursive(entry_path, 0);
1051
1052 r = mount_nofollow_verbose(LOG_DEBUG, "proc", entry_path, "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, opts);
1053 if (r == -EINVAL && opts)
1054 /* If this failed with EINVAL then this likely means the textual hidepid= stuff is
1055 * not supported by the kernel, and thus the per-instance hidepid= neither, which
1056 * means we really don't want to use it, since it would affect our host's /proc
1057 * mount. Hence let's gracefully fallback to a classic, unrestricted version. */
1058 r = mount_nofollow_verbose(LOG_DEBUG, "proc", entry_path, "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
1059 if (r == -EPERM) {
24ee0f9d 1060 /* When we do not have enough privileges to mount /proc, fallback to use existing /proc. */
61f8a7bd
YW
1061
1062 if (n > 0)
1063 /* /proc or some of sub-mounts are umounted in the above. Refuse incomplete tree.
1064 * Propagate the original error code returned by mount() in the above. */
1065 return -EPERM;
1066
1067 r = path_is_mount_point(entry_path, NULL, 0);
1068 if (r < 0)
1069 return log_debug_errno(r, "Unable to determine whether /proc is already mounted: %m");
1070 if (r == 0)
1071 /* /proc is not mounted. Propagate the original error code. */
1072 return -EPERM;
ad74f28a
YW
1073 } else if (r < 0)
1074 return r;
5d997827
LP
1075
1076 return 1;
1077}
1078
2abd4e38 1079static int mount_tmpfs(const MountEntry *m) {
df6b900a 1080 const char *entry_path, *inner_path;
abad72be 1081 int r;
abad72be 1082
6c47cd7d
LP
1083 assert(m);
1084
df6b900a 1085 entry_path = mount_entry_path(m);
809ceb82 1086 inner_path = mount_entry_unprefixed_path(m);
df6b900a 1087
2abd4e38 1088 /* First, get rid of everything that is below if there is anything. Then, overmount with our new tmpfs */
6c47cd7d 1089
abad72be
CG
1090 (void) mkdir_p_label(entry_path, 0755);
1091 (void) umount_recursive(entry_path, 0);
6c47cd7d 1092
21935150
LP
1093 r = mount_nofollow_verbose(LOG_DEBUG, "tmpfs", entry_path, "tmpfs", m->flags, mount_entry_options(m));
1094 if (r < 0)
1095 return r;
abad72be 1096
df6b900a 1097 r = label_fix_container(entry_path, inner_path, 0);
abad72be 1098 if (r < 0)
df6b900a 1099 return log_debug_errno(r, "Failed to fix label of '%s' as '%s': %m", entry_path, inner_path);
6c47cd7d
LP
1100
1101 return 1;
1102}
1103
94293d65
LB
1104static int mount_run(const MountEntry *m) {
1105 int r;
1106
1107 assert(m);
1108
1109 r = path_is_mount_point(mount_entry_path(m), NULL, 0);
1110 if (r < 0 && r != -ENOENT)
1111 return log_debug_errno(r, "Unable to determine whether /run is already mounted: %m");
1112 if (r > 0) /* make this a NOP if /run is already a mount point */
1113 return 0;
1114
1115 return mount_tmpfs(m);
1116}
1117
80271a44
XR
1118static int mount_mqueuefs(const MountEntry *m) {
1119 int r;
1120 const char *entry_path;
1121
1122 assert(m);
1123
1124 entry_path = mount_entry_path(m);
1125
1126 (void) mkdir_p_label(entry_path, 0755);
1127 (void) umount_recursive(entry_path, 0);
1128
1129 r = mount_nofollow_verbose(LOG_DEBUG, "mqueue", entry_path, "mqueue", m->flags, mount_entry_options(m));
1130 if (r < 0)
1131 return r;
1132
1133 return 0;
1134}
1135
93f59701
LB
1136static int mount_image(const MountEntry *m, const char *root_directory) {
1137
1138 _cleanup_free_ char *host_os_release_id = NULL, *host_os_release_version_id = NULL,
1139 *host_os_release_sysext_level = NULL;
b3d13314
LB
1140 int r;
1141
89e62e0b
LP
1142 assert(m);
1143
93f59701
LB
1144 if (m->mode == EXTENSION_IMAGES) {
1145 r = parse_os_release(
1146 empty_to_root(root_directory),
1147 "ID", &host_os_release_id,
1148 "VERSION_ID", &host_os_release_version_id,
1149 "SYSEXT_LEVEL", &host_os_release_sysext_level,
1150 NULL);
1151 if (r < 0)
1152 return log_debug_errno(r, "Failed to acquire 'os-release' data of OS tree '%s': %m", empty_to_root(root_directory));
1153 }
1154
1155 r = verity_dissect_and_mount(
1156 mount_entry_source(m), mount_entry_path(m), m->image_options,
1157 host_os_release_id, host_os_release_version_id, host_os_release_sysext_level);
b850a9b2
LB
1158 if (r == -ENOENT && m->ignore)
1159 return 0;
db4c8a25
LB
1160 if (r == -ESTALE && host_os_release_id)
1161 return log_error_errno(r,
1162 "Failed to mount image %s, extension-release metadata does not match the lower layer's: ID=%s%s%s%s%s",
1163 mount_entry_source(m),
1164 host_os_release_id,
1165 host_os_release_version_id ? " VERSION_ID=" : "",
1166 strempty(host_os_release_version_id),
1167 host_os_release_sysext_level ? " SYSEXT_LEVEL=" : "",
1168 strempty(host_os_release_sysext_level));
b3d13314 1169 if (r < 0)
4beda316 1170 return log_debug_errno(r, "Failed to mount image %s on %s: %m", mount_entry_source(m), mount_entry_path(m));
b3d13314
LB
1171
1172 return 1;
1173}
1174
93f59701
LB
1175static int mount_overlay(const MountEntry *m) {
1176 const char *options;
1177 int r;
1178
1179 assert(m);
1180
1181 options = strjoina("lowerdir=", mount_entry_options(m));
1182
1183 (void) mkdir_p_label(mount_entry_path(m), 0755);
1184
1185 r = mount_nofollow_verbose(LOG_DEBUG, "overlay", mount_entry_path(m), "overlay", MS_RDONLY, options);
1186 if (r == -ENOENT && m->ignore)
1187 return 0;
1188 if (r < 0)
1189 return r;
1190
1191 return 1;
1192}
1193
088696fe 1194static int follow_symlink(
d2d6c096 1195 const char *root_directory,
088696fe 1196 MountEntry *m) {
d2d6c096 1197
088696fe 1198 _cleanup_free_ char *target = NULL;
8fceda93
LP
1199 int r;
1200
088696fe
LP
1201 /* Let's chase symlinks, but only one step at a time. That's because depending where the symlink points we
1202 * might need to change the order in which we mount stuff. Hence: let's normalize piecemeal, and do one step at
1203 * a time by specifying CHASE_STEP. This function returns 0 if we resolved one step, and > 0 if we reached the
1204 * end and already have a fully normalized name. */
8fceda93 1205
a5648b80 1206 r = chase_symlinks(mount_entry_path(m), root_directory, CHASE_STEP|CHASE_NONEXISTENT, &target, NULL);
088696fe
LP
1207 if (r < 0)
1208 return log_debug_errno(r, "Failed to chase symlinks '%s': %m", mount_entry_path(m));
1209 if (r > 0) /* Reached the end, nothing more to resolve */
1210 return 1;
8fceda93 1211
baaa35ad
ZJS
1212 if (m->n_followed >= CHASE_SYMLINKS_MAX) /* put a boundary on things */
1213 return log_debug_errno(SYNTHETIC_ERRNO(ELOOP),
1214 "Symlink loop on '%s'.",
1215 mount_entry_path(m));
8fceda93 1216
088696fe 1217 log_debug("Followed mount entry path symlink %s → %s.", mount_entry_path(m), target);
8fceda93 1218
809ceb82 1219 mount_entry_consume_prefix(m, TAKE_PTR(target));
8fceda93 1220
088696fe
LP
1221 m->n_followed ++;
1222
1223 return 0;
8fceda93
LP
1224}
1225
82fb2da2 1226static int apply_one_mount(
8fceda93 1227 const char *root_directory,
4e399953
LP
1228 MountEntry *m,
1229 const NamespaceInfo *ns_info) {
ac0930c8 1230
e5f10caf 1231 _cleanup_free_ char *inaccessible = NULL;
a227a4be 1232 bool rbind = true, make = false;
15ae422b 1233 const char *what;
15ae422b 1234 int r;
15ae422b 1235
c17ec25e 1236 assert(m);
4e399953 1237 assert(ns_info);
15ae422b 1238
34de407a 1239 log_debug("Applying namespace mount on %s", mount_entry_path(m));
fe3c2583 1240
c17ec25e 1241 switch (m->mode) {
15ae422b 1242
160cfdbe 1243 case INACCESSIBLE: {
e5f10caf
AZ
1244 _cleanup_free_ char *tmp = NULL;
1245 const char *runtime_dir;
160cfdbe 1246 struct stat target;
6d313367
LP
1247
1248 /* First, get rid of everything that is below if there
1249 * is anything... Then, overmount it with an
c4b41707 1250 * inaccessible path. */
34de407a 1251 (void) umount_recursive(mount_entry_path(m), 0);
6d313367 1252
088696fe
LP
1253 if (lstat(mount_entry_path(m), &target) < 0) {
1254 if (errno == ENOENT && m->ignore)
1255 return 0;
1256
cbc056c8
ZJS
1257 return log_debug_errno(errno, "Failed to lstat() %s to determine what to mount over it: %m",
1258 mount_entry_path(m));
088696fe 1259 }
15ae422b 1260
e5f10caf 1261 if (geteuid() == 0)
48b747fa 1262 runtime_dir = "/run";
e5f10caf 1263 else {
48b747fa
LP
1264 if (asprintf(&tmp, "/run/user/" UID_FMT, geteuid()) < 0)
1265 return -ENOMEM;
e5f10caf
AZ
1266
1267 runtime_dir = tmp;
1268 }
1269
1270 r = mode_to_inaccessible_node(runtime_dir, target.st_mode, &inaccessible);
1271 if (r < 0)
baaa35ad
ZJS
1272 return log_debug_errno(SYNTHETIC_ERRNO(ELOOP),
1273 "File type not supported for inaccessible mounts. Note that symlinks are not allowed");
e5f10caf 1274 what = inaccessible;
c4b41707 1275 break;
160cfdbe 1276 }
fe3c2583 1277
15ae422b 1278 case READONLY:
15ae422b 1279 case READWRITE:
1e05071d 1280 case READWRITE_IMPLICIT:
ddc155b2
TM
1281 case EXEC:
1282 case NOEXEC:
8fceda93 1283 r = path_is_mount_point(mount_entry_path(m), root_directory, 0);
088696fe
LP
1284 if (r == -ENOENT && m->ignore)
1285 return 0;
d944dc95 1286 if (r < 0)
cbc056c8
ZJS
1287 return log_debug_errno(r, "Failed to determine whether %s is already a mount point: %m",
1288 mount_entry_path(m));
1289 if (r > 0) /* Nothing to do here, it is already a mount. We just later toggle the MS_RDONLY
ddc155b2 1290 * and MS_NOEXEC bits for the mount point if needed. */
6b7c9f8b 1291 return 0;
6b7c9f8b 1292 /* This isn't a mount point yet, let's make it one. */
34de407a 1293 what = mount_entry_path(m);
6b7c9f8b 1294 break;
15ae422b 1295
d2d6c096
LP
1296 case BIND_MOUNT:
1297 rbind = false;
d2d6c096 1298
4831981d 1299 _fallthrough_;
088696fe
LP
1300 case BIND_MOUNT_RECURSIVE: {
1301 _cleanup_free_ char *chased = NULL;
5d997827 1302
cbc056c8
ZJS
1303 /* Since mount() will always follow symlinks we chase the symlinks on our own first. Note
1304 * that bind mount source paths are always relative to the host root, hence we pass NULL as
1305 * root directory to chase_symlinks() here. */
088696fe 1306
a5648b80 1307 r = chase_symlinks(mount_entry_source(m), NULL, CHASE_TRAIL_SLASH, &chased, NULL);
088696fe
LP
1308 if (r == -ENOENT && m->ignore) {
1309 log_debug_errno(r, "Path %s does not exist, ignoring.", mount_entry_source(m));
1310 return 0;
1311 }
1312 if (r < 0)
1313 return log_debug_errno(r, "Failed to follow symlinks on %s: %m", mount_entry_source(m));
1314
1315 log_debug("Followed source symlinks %s → %s.", mount_entry_source(m), chased);
1316
1317 free_and_replace(m->source_malloc, chased);
d2d6c096
LP
1318
1319 what = mount_entry_source(m);
a227a4be 1320 make = true;
d2d6c096 1321 break;
088696fe 1322 }
d2d6c096 1323
6c47cd7d 1324 case EMPTY_DIR:
2abd4e38
YW
1325 case TMPFS:
1326 return mount_tmpfs(m);
6c47cd7d 1327
ac0930c8 1328 case PRIVATE_TMP:
56a13a49 1329 case PRIVATE_TMP_READONLY:
89bd586c 1330 what = mount_entry_source(m);
a227a4be 1331 make = true;
15ae422b 1332 break;
e364ad06 1333
d6797c92 1334 case PRIVATE_DEV:
5d997827
LP
1335 return mount_private_dev(m);
1336
1337 case BIND_DEV:
1338 return mount_bind_dev(m);
1339
1340 case SYSFS:
1341 return mount_sysfs(m);
1342
1343 case PROCFS:
4e399953 1344 return mount_procfs(m, ns_info);
d6797c92 1345
94293d65
LB
1346 case RUN:
1347 return mount_run(m);
1348
80271a44
XR
1349 case MQUEUEFS:
1350 return mount_mqueuefs(m);
1351
b3d13314 1352 case MOUNT_IMAGES:
93f59701
LB
1353 return mount_image(m, NULL);
1354
1355 case EXTENSION_IMAGES:
1356 return mount_image(m, root_directory);
1357
1358 case OVERLAY_MOUNT:
1359 return mount_overlay(m);
b3d13314 1360
e364ad06
LP
1361 default:
1362 assert_not_reached("Unknown mode");
15ae422b
LP
1363 }
1364
ac0930c8 1365 assert(what);
15ae422b 1366
21935150
LP
1367 r = mount_nofollow_verbose(LOG_DEBUG, what, mount_entry_path(m), NULL, MS_BIND|(rbind ? MS_REC : 0), NULL);
1368 if (r < 0) {
a227a4be 1369 bool try_again = false;
a227a4be
LP
1370
1371 if (r == -ENOENT && make) {
8bab8029 1372 int q;
a227a4be 1373
cbc056c8
ZJS
1374 /* Hmm, either the source or the destination are missing. Let's see if we can create
1375 the destination, then try again. */
a227a4be 1376
8bab8029 1377 (void) mkdir_parents(mount_entry_path(m), 0755);
a227a4be 1378
8bab8029
LB
1379 q = make_mount_point_inode_from_path(what, mount_entry_path(m), 0755);
1380 if (q < 0)
1381 log_error_errno(q, "Failed to create destination mount point node '%s': %m",
1382 mount_entry_path(m));
1383 else
1384 try_again = true;
a227a4be
LP
1385 }
1386
21935150
LP
1387 if (try_again)
1388 r = mount_nofollow_verbose(LOG_DEBUG, what, mount_entry_path(m), NULL, MS_BIND|(rbind ? MS_REC : 0), NULL);
a227a4be 1389 if (r < 0)
5dc60faa 1390 return log_error_errno(r, "Failed to mount %s to %s: %m", what, mount_entry_path(m));
a227a4be 1391 }
6b7c9f8b 1392
34de407a 1393 log_debug("Successfully mounted %s to %s", what, mount_entry_path(m));
6b7c9f8b 1394 return 0;
ac0930c8 1395}
15ae422b 1396
6b000af4 1397static int make_read_only(const MountEntry *m, char **deny_list, FILE *proc_self_mountinfo) {
9ce4e4b0 1398 unsigned long new_flags = 0, flags_mask = 0;
763a260a 1399 bool submounts = false;
6b7c9f8b 1400 int r = 0;
15ae422b 1401
c17ec25e 1402 assert(m);
ac9de0b3 1403 assert(proc_self_mountinfo);
ac0930c8 1404
9ce4e4b0
LP
1405 if (mount_entry_read_only(m) || m->mode == PRIVATE_DEV) {
1406 new_flags |= MS_RDONLY;
1407 flags_mask |= MS_RDONLY;
1408 }
1409
1410 if (m->nosuid) {
1411 new_flags |= MS_NOSUID;
1412 flags_mask |= MS_NOSUID;
1413 }
1414
1415 if (flags_mask == 0) /* No Change? */
6b7c9f8b
LP
1416 return 0;
1417
9ce4e4b0
LP
1418 /* We generally apply these changes recursively, except for /dev, and the cases we know there's
1419 * nothing further down. Set /dev readonly, but not submounts like /dev/shm. Also, we only set the
1420 * per-mount read-only flag. We can't set it on the superblock, if we are inside a user namespace
1421 * and running Linux <= 4.17. */
1422 submounts =
1423 mount_entry_read_only(m) &&
1424 !IN_SET(m->mode, EMPTY_DIR, TMPFS);
1425 if (submounts)
6b000af4 1426 r = bind_remount_recursive_with_mountinfo(mount_entry_path(m), new_flags, flags_mask, deny_list, proc_self_mountinfo);
9ce4e4b0 1427 else
7cce68e1 1428 r = bind_remount_one_with_mountinfo(mount_entry_path(m), new_flags, flags_mask, proc_self_mountinfo);
9ce4e4b0 1429
ddc155b2 1430 /* Note that we only turn on the MS_RDONLY flag here, we never turn it off. Something that was marked
867189b5
LP
1431 * read-only already stays this way. This improves compatibility with container managers, where we
1432 * won't attempt to undo read-only mounts already applied. */
ac0930c8 1433
8fceda93 1434 if (r == -ENOENT && m->ignore)
867189b5 1435 return 0;
763a260a 1436 if (r < 0)
9ce4e4b0 1437 return log_debug_errno(r, "Failed to re-mount '%s'%s: %m", mount_entry_path(m),
763a260a 1438 submounts ? " and its submounts" : "");
763a260a 1439 return 0;
d944dc95
LP
1440}
1441
ddc155b2
TM
1442static int make_noexec(const MountEntry *m, char **deny_list, FILE *proc_self_mountinfo) {
1443 unsigned long new_flags = 0, flags_mask = 0;
1444 bool submounts = false;
1445 int r = 0;
1446
1447 assert(m);
1448 assert(proc_self_mountinfo);
1449
1450 if (mount_entry_noexec(m)) {
1451 new_flags |= MS_NOEXEC;
1452 flags_mask |= MS_NOEXEC;
1453 } else if (mount_entry_exec(m)) {
1454 new_flags &= ~MS_NOEXEC;
1455 flags_mask |= MS_NOEXEC;
1456 }
1457
1458 if (flags_mask == 0) /* No Change? */
1459 return 0;
1460
1461 submounts = !IN_SET(m->mode, EMPTY_DIR, TMPFS);
1462
1463 if (submounts)
1464 r = bind_remount_recursive_with_mountinfo(mount_entry_path(m), new_flags, flags_mask, deny_list, proc_self_mountinfo);
1465 else
1466 r = bind_remount_one_with_mountinfo(mount_entry_path(m), new_flags, flags_mask, proc_self_mountinfo);
1467
1468 if (r == -ENOENT && m->ignore)
1469 return 0;
1470 if (r < 0)
1471 return log_debug_errno(r, "Failed to re-mount '%s'%s: %m", mount_entry_path(m),
1472 submounts ? " and its submounts" : "");
1473 return 0;
1474}
1475
6720e356
YW
1476static int make_nosuid(const MountEntry *m, FILE *proc_self_mountinfo) {
1477 bool submounts = false;
1478 int r = 0;
1479
1480 assert(m);
1481 assert(proc_self_mountinfo);
1482
1483 submounts = !IN_SET(m->mode, EMPTY_DIR, TMPFS);
1484
1485 if (submounts)
1486 r = bind_remount_recursive_with_mountinfo(mount_entry_path(m), MS_NOSUID, MS_NOSUID, NULL, proc_self_mountinfo);
1487 else
1488 r = bind_remount_one_with_mountinfo(mount_entry_path(m), MS_NOSUID, MS_NOSUID, proc_self_mountinfo);
1489 if (r == -ENOENT && m->ignore)
1490 return 0;
1491 if (r < 0)
1492 return log_debug_errno(r, "Failed to re-mount '%s'%s: %m", mount_entry_path(m),
1493 submounts ? " and its submounts" : "");
1494 return 0;
1495}
1496
9b68367b 1497static bool namespace_info_mount_apivfs(const NamespaceInfo *ns_info) {
5d997827
LP
1498 assert(ns_info);
1499
9c988f93
DH
1500 /*
1501 * ProtectControlGroups= and ProtectKernelTunables= imply MountAPIVFS=,
1502 * since to protect the API VFS mounts, they need to be around in the
9b68367b 1503 * first place...
9c988f93 1504 */
5d997827 1505
9b68367b
YW
1506 return ns_info->mount_apivfs ||
1507 ns_info->protect_control_groups ||
4e399953
LP
1508 ns_info->protect_kernel_tunables ||
1509 ns_info->protect_proc != PROTECT_PROC_DEFAULT ||
1510 ns_info->proc_subset != PROC_SUBSET_ALL;
5d997827
LP
1511}
1512
da6053d0 1513static size_t namespace_calculate_mounts(
bb0ff3fb 1514 const NamespaceInfo *ns_info,
2652c6c1
DH
1515 char** read_write_paths,
1516 char** read_only_paths,
1517 char** inaccessible_paths,
ddc155b2
TM
1518 char** exec_paths,
1519 char** no_exec_paths,
6c47cd7d 1520 char** empty_directories,
da6053d0
LP
1521 size_t n_bind_mounts,
1522 size_t n_temporary_filesystems,
b3d13314 1523 size_t n_mount_images,
93f59701
LB
1524 size_t n_extension_images,
1525 size_t n_hierarchies,
2652c6c1
DH
1526 const char* tmp_dir,
1527 const char* var_tmp_dir,
bbb4e7f3 1528 const char *creds_path,
5e8deb94 1529 const char* log_namespace,
3bdc25a4
LP
1530 bool setup_propagate,
1531 const char* notify_socket) {
2652c6c1 1532
da6053d0
LP
1533 size_t protect_home_cnt;
1534 size_t protect_system_cnt =
52b3d652 1535 (ns_info->protect_system == PROTECT_SYSTEM_STRICT ?
f471b2af 1536 ELEMENTSOF(protect_system_strict_table) :
52b3d652 1537 ((ns_info->protect_system == PROTECT_SYSTEM_FULL) ?
f471b2af 1538 ELEMENTSOF(protect_system_full_table) :
52b3d652 1539 ((ns_info->protect_system == PROTECT_SYSTEM_YES) ?
f471b2af
DH
1540 ELEMENTSOF(protect_system_yes_table) : 0)));
1541
b6c432ca 1542 protect_home_cnt =
52b3d652 1543 (ns_info->protect_home == PROTECT_HOME_YES ?
b6c432ca 1544 ELEMENTSOF(protect_home_yes_table) :
52b3d652 1545 ((ns_info->protect_home == PROTECT_HOME_READ_ONLY) ?
e4da7d8c 1546 ELEMENTSOF(protect_home_read_only_table) :
52b3d652 1547 ((ns_info->protect_home == PROTECT_HOME_TMPFS) ?
e4da7d8c 1548 ELEMENTSOF(protect_home_tmpfs_table) : 0)));
b6c432ca 1549
2652c6c1
DH
1550 return !!tmp_dir + !!var_tmp_dir +
1551 strv_length(read_write_paths) +
1552 strv_length(read_only_paths) +
1553 strv_length(inaccessible_paths) +
ddc155b2
TM
1554 strv_length(exec_paths) +
1555 strv_length(no_exec_paths) +
6c47cd7d 1556 strv_length(empty_directories) +
d2d6c096 1557 n_bind_mounts +
b3d13314 1558 n_mount_images +
93f59701 1559 (n_extension_images > 0 ? n_hierarchies + n_extension_images : 0) + /* Mount each image plus an overlay per hierarchy */
2abd4e38 1560 n_temporary_filesystems +
c575770b
DH
1561 ns_info->private_dev +
1562 (ns_info->protect_kernel_tunables ? ELEMENTSOF(protect_kernel_tunables_table) : 0) +
c575770b 1563 (ns_info->protect_kernel_modules ? ELEMENTSOF(protect_kernel_modules_table) : 0) +
94a7b275
KK
1564 (ns_info->protect_kernel_logs ? ELEMENTSOF(protect_kernel_logs_table) : 0) +
1565 (ns_info->protect_control_groups ? 1 : 0) +
5d997827 1566 protect_home_cnt + protect_system_cnt +
aecd5ac6 1567 (ns_info->protect_hostname ? 2 : 0) +
91dd5f7c 1568 (namespace_info_mount_apivfs(ns_info) ? ELEMENTSOF(apivfs_table) : 0) +
bbb4e7f3 1569 (creds_path ? 2 : 1) +
3bdc25a4
LP
1570 !!log_namespace +
1571 setup_propagate + /* /run/systemd/incoming */
80271a44
XR
1572 !!notify_socket +
1573 ns_info->private_ipc; /* /dev/mqueue */
2652c6c1
DH
1574}
1575
da6053d0 1576static void normalize_mounts(const char *root_directory, MountEntry *mounts, size_t *n_mounts) {
9b68367b 1577 assert(root_directory);
f8b64b57
LP
1578 assert(n_mounts);
1579 assert(mounts || *n_mounts == 0);
1580
93bab288 1581 typesafe_qsort(mounts, *n_mounts, mount_path_compare);
f8b64b57
LP
1582
1583 drop_duplicates(mounts, n_mounts);
1584 drop_outside_root(root_directory, mounts, n_mounts);
1585 drop_inaccessible(mounts, n_mounts);
1586 drop_nop(mounts, n_mounts);
1587}
1588
82fb2da2
LB
1589static int apply_mounts(
1590 const char *root,
1591 const NamespaceInfo *ns_info,
1592 MountEntry *mounts,
1593 size_t *n_mounts,
1594 char **error_path) {
1595
1596 _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
1597 _cleanup_free_ char **deny_list = NULL;
82fb2da2
LB
1598 int r;
1599
1600 if (n_mounts == 0) /* Shortcut: nothing to do */
1601 return 0;
1602
1603 assert(root);
1604 assert(mounts);
1605 assert(n_mounts);
1606
1607 /* Open /proc/self/mountinfo now as it may become unavailable if we mount anything on top of
1608 * /proc. For example, this is the case with the option: 'InaccessiblePaths=/proc'. */
1609 proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
1610 if (!proc_self_mountinfo) {
d60e3b40
LP
1611 r = -errno;
1612
82fb2da2
LB
1613 if (error_path)
1614 *error_path = strdup("/proc/self/mountinfo");
d60e3b40
LP
1615
1616 return log_debug_errno(r, "Failed to open /proc/self/mountinfo: %m");
82fb2da2
LB
1617 }
1618
1619 /* First round, establish all mounts we need */
1620 for (;;) {
1621 bool again = false;
1622
1623 for (MountEntry *m = mounts; m < mounts + *n_mounts; ++m) {
1624
1625 if (m->applied)
1626 continue;
1627
93f59701
LB
1628 /* ExtensionImages are first opened in the propagate directory, not in the root_directory */
1629 r = follow_symlink(m->mode != EXTENSION_IMAGES ? root : NULL, m);
82fb2da2
LB
1630 if (r < 0) {
1631 if (error_path && mount_entry_path(m))
1632 *error_path = strdup(mount_entry_path(m));
1633 return r;
1634 }
1635 if (r == 0) {
1636 /* We hit a symlinked mount point. The entry got rewritten and might
1637 * point to a very different place now. Let's normalize the changed
1638 * list, and start from the beginning. After all to mount the entry
1639 * at the new location we might need some other mounts first */
1640 again = true;
1641 break;
1642 }
1643
1644 r = apply_one_mount(root, m, ns_info);
1645 if (r < 0) {
1646 if (error_path && mount_entry_path(m))
1647 *error_path = strdup(mount_entry_path(m));
1648 return r;
1649 }
1650
1651 m->applied = true;
1652 }
1653
1654 if (!again)
1655 break;
1656
1657 normalize_mounts(root, mounts, n_mounts);
1658 }
1659
1660 /* Create a deny list we can pass to bind_mount_recursive() */
1661 deny_list = new(char*, (*n_mounts)+1);
1662 if (!deny_list)
1663 return -ENOMEM;
8794e6db 1664 for (size_t j = 0; j < *n_mounts; j++)
82fb2da2 1665 deny_list[j] = (char*) mount_entry_path(mounts+j);
8794e6db 1666 deny_list[*n_mounts] = NULL;
82fb2da2
LB
1667
1668 /* Second round, flip the ro bits if necessary. */
1669 for (MountEntry *m = mounts; m < mounts + *n_mounts; ++m) {
1670 r = make_read_only(m, deny_list, proc_self_mountinfo);
1671 if (r < 0) {
1672 if (error_path && mount_entry_path(m))
1673 *error_path = strdup(mount_entry_path(m));
1674 return r;
1675 }
1676 }
1677
1678 /* Third round, flip the noexec bits with a simplified deny list. */
8794e6db 1679 for (size_t j = 0; j < *n_mounts; j++)
82fb2da2
LB
1680 if (IN_SET((mounts+j)->mode, EXEC, NOEXEC))
1681 deny_list[j] = (char*) mount_entry_path(mounts+j);
8794e6db 1682 deny_list[*n_mounts] = NULL;
82fb2da2
LB
1683
1684 for (MountEntry *m = mounts; m < mounts + *n_mounts; ++m) {
1685 r = make_noexec(m, deny_list, proc_self_mountinfo);
1686 if (r < 0) {
1687 if (error_path && mount_entry_path(m))
1688 *error_path = strdup(mount_entry_path(m));
1689 return r;
1690 }
1691 }
1692
6720e356
YW
1693 /* Fourth round, flip the nosuid bits without a deny list. */
1694 if (ns_info->mount_nosuid)
1695 for (MountEntry *m = mounts; m < mounts + *n_mounts; ++m) {
1696 r = make_nosuid(m, proc_self_mountinfo);
1697 if (r < 0) {
1698 if (error_path && mount_entry_path(m))
1699 *error_path = strdup(mount_entry_path(m));
1700 return r;
1701 }
1702 }
1703
82fb2da2
LB
1704 return 1;
1705}
1706
c8c535d5
LP
1707static bool root_read_only(
1708 char **read_only_paths,
1709 ProtectSystem protect_system) {
1710
1711 /* Determine whether the root directory is going to be read-only given the configured settings. */
1712
1713 if (protect_system == PROTECT_SYSTEM_STRICT)
1714 return true;
1715
de46b2be 1716 if (prefixed_path_strv_contains(read_only_paths, "/"))
c8c535d5
LP
1717 return true;
1718
1719 return false;
1720}
1721
1722static bool home_read_only(
1723 char** read_only_paths,
1724 char** inaccessible_paths,
1725 char** empty_directories,
1726 const BindMount *bind_mounts,
1727 size_t n_bind_mounts,
1728 const TemporaryFileSystem *temporary_filesystems,
1729 size_t n_temporary_filesystems,
1730 ProtectHome protect_home) {
1731
c8c535d5
LP
1732 /* Determine whether the /home directory is going to be read-only given the configured settings. Yes,
1733 * this is a bit sloppy, since we don't bother checking for cases where / is affected by multiple
1734 * settings. */
1735
1736 if (protect_home != PROTECT_HOME_NO)
1737 return true;
1738
de46b2be
TM
1739 if (prefixed_path_strv_contains(read_only_paths, "/home") ||
1740 prefixed_path_strv_contains(inaccessible_paths, "/home") ||
1741 prefixed_path_strv_contains(empty_directories, "/home"))
c8c535d5
LP
1742 return true;
1743
8794e6db 1744 for (size_t i = 0; i < n_temporary_filesystems; i++)
c8c535d5
LP
1745 if (path_equal(temporary_filesystems[i].path, "/home"))
1746 return true;
1747
1748 /* If /home is overmounted with some dir from the host it's not writable. */
8794e6db 1749 for (size_t i = 0; i < n_bind_mounts; i++)
c8c535d5
LP
1750 if (path_equal(bind_mounts[i].destination, "/home"))
1751 return true;
1752
1753 return false;
1754}
1755
89e62e0b
LP
1756static int verity_settings_prepare(
1757 VeritySettings *verity,
1758 const char *root_image,
1759 const void *root_hash,
1760 size_t root_hash_size,
1761 const char *root_hash_path,
1762 const void *root_hash_sig,
1763 size_t root_hash_sig_size,
1764 const char *root_hash_sig_path,
1765 const char *verity_data_path) {
1766
1767 int r;
1768
1769 assert(verity);
1770
1771 if (root_hash) {
1772 void *d;
1773
1774 d = memdup(root_hash, root_hash_size);
1775 if (!d)
1776 return -ENOMEM;
1777
1778 free_and_replace(verity->root_hash, d);
1779 verity->root_hash_size = root_hash_size;
aee36b4e 1780 verity->designator = PARTITION_ROOT;
89e62e0b
LP
1781 }
1782
1783 if (root_hash_sig) {
1784 void *d;
1785
1786 d = memdup(root_hash_sig, root_hash_sig_size);
1787 if (!d)
1788 return -ENOMEM;
1789
1790 free_and_replace(verity->root_hash_sig, d);
1791 verity->root_hash_sig_size = root_hash_sig_size;
aee36b4e 1792 verity->designator = PARTITION_ROOT;
89e62e0b
LP
1793 }
1794
1795 if (verity_data_path) {
1796 r = free_and_strdup(&verity->data_path, verity_data_path);
1797 if (r < 0)
1798 return r;
1799 }
1800
1801 r = verity_settings_load(
1802 verity,
1803 root_image,
1804 root_hash_path,
1805 root_hash_sig_path);
1806 if (r < 0)
1807 return log_debug_errno(r, "Failed to load root hash: %m");
1808
1809 return 0;
1810}
1811
613b411c 1812int setup_namespace(
ee818b89 1813 const char* root_directory,
915e6d16 1814 const char* root_image,
18d73705 1815 const MountOptions *root_image_options,
bb0ff3fb 1816 const NamespaceInfo *ns_info,
2a624c36
AP
1817 char** read_write_paths,
1818 char** read_only_paths,
1819 char** inaccessible_paths,
ddc155b2
TM
1820 char** exec_paths,
1821 char** no_exec_paths,
6c47cd7d 1822 char** empty_directories,
d2d6c096 1823 const BindMount *bind_mounts,
da6053d0 1824 size_t n_bind_mounts,
2abd4e38 1825 const TemporaryFileSystem *temporary_filesystems,
da6053d0 1826 size_t n_temporary_filesystems,
b3d13314
LB
1827 const MountImage *mount_images,
1828 size_t n_mount_images,
a004cb4c
LP
1829 const char* tmp_dir,
1830 const char* var_tmp_dir,
bbb4e7f3 1831 const char *creds_path,
91dd5f7c 1832 const char *log_namespace,
915e6d16 1833 unsigned long mount_flags,
0389f4fa
LB
1834 const void *root_hash,
1835 size_t root_hash_size,
1836 const char *root_hash_path,
d4d55b0d
LB
1837 const void *root_hash_sig,
1838 size_t root_hash_sig_size,
1839 const char *root_hash_sig_path,
89e62e0b 1840 const char *verity_data_path,
93f59701
LB
1841 const MountImage *extension_images,
1842 size_t n_extension_images,
5e8deb94
LB
1843 const char *propagate_dir,
1844 const char *incoming_dir,
3bdc25a4 1845 const char *notify_socket,
7cc5ef5f 1846 char **error_path) {
15ae422b 1847
915e6d16 1848 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
78ebe980 1849 _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL;
915e6d16 1850 _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL;
aee36b4e 1851 _cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT;
93f59701 1852 _cleanup_strv_free_ char **hierarchies = NULL;
5f7a690a 1853 MountEntry *m = NULL, *mounts = NULL;
5e8deb94 1854 bool require_prefix = false, setup_propagate = false;
93f59701 1855 const char *root, *extension_dir = "/run/systemd/unit-extensions";
9cb1709b
LP
1856 DissectImageFlags dissect_image_flags =
1857 DISSECT_IMAGE_GENERIC_ROOT |
1858 DISSECT_IMAGE_REQUIRE_ROOT |
1859 DISSECT_IMAGE_DISCARD_ON_LOOP |
1860 DISSECT_IMAGE_RELAX_VAR_CHECK |
1861 DISSECT_IMAGE_FSCK |
c65f854a
LP
1862 DISSECT_IMAGE_USR_NO_ROOT |
1863 DISSECT_IMAGE_GROWFS;
89e62e0b
LP
1864 size_t n_mounts;
1865 int r;
15ae422b 1866
915e6d16
LP
1867 assert(ns_info);
1868
5e8deb94
LB
1869 if (!isempty(propagate_dir) && !isempty(incoming_dir))
1870 setup_propagate = true;
1871
613b411c 1872 if (mount_flags == 0)
c17ec25e 1873 mount_flags = MS_SHARED;
ac0930c8 1874
915e6d16 1875 if (root_image) {
c8c535d5
LP
1876 /* Make the whole image read-only if we can determine that we only access it in a read-only fashion. */
1877 if (root_read_only(read_only_paths,
52b3d652 1878 ns_info->protect_system) &&
c8c535d5
LP
1879 home_read_only(read_only_paths, inaccessible_paths, empty_directories,
1880 bind_mounts, n_bind_mounts, temporary_filesystems, n_temporary_filesystems,
52b3d652 1881 ns_info->protect_home) &&
c9ef8573 1882 strv_isempty(read_write_paths))
915e6d16
LP
1883 dissect_image_flags |= DISSECT_IMAGE_READ_ONLY;
1884
89e62e0b
LP
1885 r = verity_settings_prepare(
1886 &verity,
1887 root_image,
1888 root_hash, root_hash_size, root_hash_path,
1889 root_hash_sig, root_hash_sig_size, root_hash_sig_path,
1890 verity_data_path);
915e6d16 1891 if (r < 0)
89e62e0b
LP
1892 return r;
1893
1894 SET_FLAG(dissect_image_flags, DISSECT_IMAGE_NO_PARTITION_TABLE, verity.data_path);
915e6d16 1895
89e62e0b
LP
1896 r = loop_device_make_by_path(
1897 root_image,
ef9c184d 1898 FLAGS_SET(dissect_image_flags, DISSECT_IMAGE_DEVICE_READ_ONLY) ? O_RDONLY : -1 /* < 0 means writable if possible, read-only as fallback */,
89e62e0b
LP
1899 FLAGS_SET(dissect_image_flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN,
1900 &loop_device);
78ebe980 1901 if (r < 0)
89e62e0b
LP
1902 return log_debug_errno(r, "Failed to create loop device for root image: %m");
1903
1904 r = dissect_image(
1905 loop_device->fd,
1906 &verity,
1907 root_image_options,
75dc190d 1908 loop_device->uevent_seqnum_not_before,
4a62257d 1909 loop_device->timestamp_not_before,
89e62e0b
LP
1910 dissect_image_flags,
1911 &dissected_image);
78ebe980 1912 if (r < 0)
763a260a 1913 return log_debug_errno(r, "Failed to dissect image: %m");
78ebe980 1914
89e62e0b
LP
1915 r = dissected_image_decrypt(
1916 dissected_image,
1917 NULL,
1918 &verity,
1919 dissect_image_flags,
1920 &decrypted_image);
915e6d16 1921 if (r < 0)
763a260a 1922 return log_debug_errno(r, "Failed to decrypt dissected image: %m");
915e6d16
LP
1923 }
1924
e908468b
LP
1925 if (root_directory)
1926 root = root_directory;
0722b359 1927 else {
77f16dbd
DDM
1928 /* /run/systemd should have been created by PID 1 early on already, but in some cases, like
1929 * when running tests (test-execute), it might not have been created yet so let's make sure
1930 * we create it if it doesn't already exist. */
1931 (void) mkdir_p_label("/run/systemd", 0755);
1932
0722b359
JS
1933 /* Always create the mount namespace in a temporary directory, instead of operating
1934 * directly in the root. The temporary directory prevents any mounts from being
1935 * potentially obscured my other mounts we already applied.
1936 * We use the same mount point for all images, which is safe, since they all live
1937 * in their own namespaces after all, and hence won't see each other. */
e908468b
LP
1938
1939 root = "/run/systemd/unit-root";
1940 (void) mkdir_label(root, 0700);
d18aff04 1941 require_prefix = true;
0722b359 1942 }
e908468b 1943
93f59701
LB
1944 if (n_extension_images > 0) {
1945 r = parse_env_extension_hierarchies(&hierarchies);
1946 if (r < 0)
1947 return r;
1948 }
1949
cfbeb4ef
LP
1950 n_mounts = namespace_calculate_mounts(
1951 ns_info,
1952 read_write_paths,
1953 read_only_paths,
1954 inaccessible_paths,
ddc155b2
TM
1955 exec_paths,
1956 no_exec_paths,
6c47cd7d 1957 empty_directories,
f5c52a77 1958 n_bind_mounts,
2abd4e38 1959 n_temporary_filesystems,
b3d13314 1960 n_mount_images,
93f59701
LB
1961 n_extension_images,
1962 strv_length(hierarchies),
cfbeb4ef 1963 tmp_dir, var_tmp_dir,
bbb4e7f3 1964 creds_path,
5e8deb94 1965 log_namespace,
3bdc25a4
LP
1966 setup_propagate,
1967 notify_socket);
613b411c 1968
f0a4feb0 1969 if (n_mounts > 0) {
5f7a690a
LP
1970 m = mounts = new0(MountEntry, n_mounts);
1971 if (!mounts)
1972 return -ENOMEM;
1973
d18aff04 1974 r = append_access_mounts(&m, read_write_paths, READWRITE, require_prefix);
613b411c 1975 if (r < 0)
f0a4feb0 1976 goto finish;
613b411c 1977
d18aff04 1978 r = append_access_mounts(&m, read_only_paths, READONLY, require_prefix);
613b411c 1979 if (r < 0)
f0a4feb0 1980 goto finish;
613b411c 1981
d18aff04 1982 r = append_access_mounts(&m, inaccessible_paths, INACCESSIBLE, require_prefix);
613b411c 1983 if (r < 0)
f0a4feb0 1984 goto finish;
7ff7394d 1985
ddc155b2
TM
1986 r = append_access_mounts(&m, exec_paths, EXEC, require_prefix);
1987 if (r < 0)
1988 goto finish;
1989
1990 r = append_access_mounts(&m, no_exec_paths, NOEXEC, require_prefix);
1991 if (r < 0)
1992 goto finish;
1993
6c47cd7d
LP
1994 r = append_empty_dir_mounts(&m, empty_directories);
1995 if (r < 0)
1996 goto finish;
1997
d2d6c096
LP
1998 r = append_bind_mounts(&m, bind_mounts, n_bind_mounts);
1999 if (r < 0)
2000 goto finish;
2001
2abd4e38
YW
2002 r = append_tmpfs_mounts(&m, temporary_filesystems, n_temporary_filesystems);
2003 if (r < 0)
2004 goto finish;
2005
613b411c 2006 if (tmp_dir) {
56a13a49
ZJS
2007 bool ro = streq(tmp_dir, RUN_SYSTEMD_EMPTY);
2008
34de407a 2009 *(m++) = (MountEntry) {
5327c910 2010 .path_const = "/tmp",
56a13a49 2011 .mode = ro ? PRIVATE_TMP_READONLY : PRIVATE_TMP,
89bd586c 2012 .source_const = tmp_dir,
5327c910 2013 };
613b411c 2014 }
7ff7394d 2015
613b411c 2016 if (var_tmp_dir) {
56a13a49
ZJS
2017 bool ro = streq(var_tmp_dir, RUN_SYSTEMD_EMPTY);
2018
34de407a 2019 *(m++) = (MountEntry) {
5327c910 2020 .path_const = "/var/tmp",
56a13a49 2021 .mode = ro ? PRIVATE_TMP_READONLY : PRIVATE_TMP,
89bd586c 2022 .source_const = var_tmp_dir,
5327c910 2023 };
7ff7394d 2024 }
ac0930c8 2025
b3d13314
LB
2026 r = append_mount_images(&m, mount_images, n_mount_images);
2027 if (r < 0)
2028 goto finish;
2029
93f59701
LB
2030 r = append_extension_images(&m, root, extension_dir, hierarchies, extension_images, n_extension_images);
2031 if (r < 0)
2032 goto finish;
2033
d46b79bb 2034 if (ns_info->private_dev)
34de407a 2035 *(m++) = (MountEntry) {
5327c910
LP
2036 .path_const = "/dev",
2037 .mode = PRIVATE_DEV,
9ce4e4b0 2038 .flags = DEV_MOUNT_OPTIONS,
5327c910 2039 };
7f112f50 2040
c575770b 2041 if (ns_info->protect_kernel_tunables) {
cbc056c8
ZJS
2042 r = append_static_mounts(&m,
2043 protect_kernel_tunables_table,
2044 ELEMENTSOF(protect_kernel_tunables_table),
2045 ns_info->ignore_protect_paths);
c575770b 2046 if (r < 0)
f0a4feb0 2047 goto finish;
c575770b
DH
2048 }
2049
2050 if (ns_info->protect_kernel_modules) {
cbc056c8
ZJS
2051 r = append_static_mounts(&m,
2052 protect_kernel_modules_table,
2053 ELEMENTSOF(protect_kernel_modules_table),
2054 ns_info->ignore_protect_paths);
c575770b 2055 if (r < 0)
f0a4feb0 2056 goto finish;
c575770b 2057 }
59eeb84b 2058
94a7b275 2059 if (ns_info->protect_kernel_logs) {
cbc056c8
ZJS
2060 r = append_static_mounts(&m,
2061 protect_kernel_logs_table,
2062 ELEMENTSOF(protect_kernel_logs_table),
2063 ns_info->ignore_protect_paths);
94a7b275
KK
2064 if (r < 0)
2065 goto finish;
2066 }
2067
d46b79bb 2068 if (ns_info->protect_control_groups)
34de407a 2069 *(m++) = (MountEntry) {
5327c910
LP
2070 .path_const = "/sys/fs/cgroup",
2071 .mode = READONLY,
2072 };
59eeb84b 2073
52b3d652 2074 r = append_protect_home(&m, ns_info->protect_home, ns_info->ignore_protect_paths);
b6c432ca 2075 if (r < 0)
f0a4feb0 2076 goto finish;
417116f2 2077
52b3d652 2078 r = append_protect_system(&m, ns_info->protect_system, false);
f471b2af 2079 if (r < 0)
f0a4feb0 2080 goto finish;
417116f2 2081
9b68367b 2082 if (namespace_info_mount_apivfs(ns_info)) {
cbc056c8
ZJS
2083 r = append_static_mounts(&m,
2084 apivfs_table,
2085 ELEMENTSOF(apivfs_table),
2086 ns_info->ignore_protect_paths);
5d997827
LP
2087 if (r < 0)
2088 goto finish;
2089 }
2090
aecd5ac6
TM
2091 if (ns_info->protect_hostname) {
2092 *(m++) = (MountEntry) {
2093 .path_const = "/proc/sys/kernel/hostname",
2094 .mode = READONLY,
2095 };
2096 *(m++) = (MountEntry) {
2097 .path_const = "/proc/sys/kernel/domainname",
2098 .mode = READONLY,
2099 };
2100 }
2101
57ac6959 2102 if (ns_info->private_ipc)
80271a44
XR
2103 *(m++) = (MountEntry) {
2104 .path_const = "/dev/mqueue",
2105 .mode = MQUEUEFS,
2106 .flags = MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME,
2107 };
80271a44 2108
bbb4e7f3
LP
2109 if (creds_path) {
2110 /* If our service has a credentials store configured, then bind that one in, but hide
2111 * everything else. */
2112
2113 *(m++) = (MountEntry) {
2114 .path_const = "/run/credentials",
2115 .mode = TMPFS,
2116 .read_only = true,
2117 .options_const = "mode=0755" TMPFS_LIMITS_EMPTY_OR_ALMOST,
2118 .flags = MS_NODEV|MS_STRICTATIME|MS_NOSUID|MS_NOEXEC,
2119 };
2120
2121 *(m++) = (MountEntry) {
2122 .path_const = creds_path,
2123 .mode = BIND_MOUNT,
2124 .read_only = true,
2125 .source_const = creds_path,
2126 };
2127 } else {
2128 /* If our service has no credentials store configured, then make the whole
2129 * credentials tree inaccessible wholesale. */
2130
2131 *(m++) = (MountEntry) {
2132 .path_const = "/run/credentials",
2133 .mode = INACCESSIBLE,
2134 .ignore = true,
2135 };
2136 }
2137
91dd5f7c 2138 if (log_namespace) {
c2b2df60 2139 _cleanup_free_ char *q = NULL;
91dd5f7c
LP
2140
2141 q = strjoin("/run/systemd/journal.", log_namespace);
2142 if (!q) {
2143 r = -ENOMEM;
2144 goto finish;
2145 }
2146
2147 *(m++) = (MountEntry) {
2148 .path_const = "/run/systemd/journal",
2149 .mode = BIND_MOUNT_RECURSIVE,
2150 .read_only = true,
2151 .source_malloc = TAKE_PTR(q),
2152 };
2153 }
2154
5e8deb94
LB
2155 /* Will be used to add bind mounts at runtime */
2156 if (setup_propagate)
2157 *(m++) = (MountEntry) {
2158 .source_const = propagate_dir,
2159 .path_const = incoming_dir,
2160 .mode = BIND_MOUNT,
2161 .read_only = true,
2162 };
2163
3bdc25a4
LP
2164 if (notify_socket)
2165 *(m++) = (MountEntry) {
2166 .path_const = notify_socket,
2167 .source_const = notify_socket,
2168 .mode = BIND_MOUNT,
2169 .read_only = true,
2170 };
2171
f0a4feb0 2172 assert(mounts + n_mounts == m);
ac0930c8 2173
5327c910 2174 /* Prepend the root directory where that's necessary */
e908468b 2175 r = prefix_where_needed(mounts, n_mounts, root);
5327c910
LP
2176 if (r < 0)
2177 goto finish;
2178
839f1877 2179 normalize_mounts(root, mounts, &n_mounts);
15ae422b
LP
2180 }
2181
1beab8b0
LP
2182 /* All above is just preparation, figuring out what to do. Let's now actually start doing something. */
2183
d944dc95 2184 if (unshare(CLONE_NEWNS) < 0) {
763a260a 2185 r = log_debug_errno(errno, "Failed to unshare the mount namespace: %m");
1beab8b0 2186 if (IN_SET(r, -EACCES, -EPERM, -EOPNOTSUPP, -ENOSYS))
cbc056c8
ZJS
2187 /* If the kernel doesn't support namespaces, or when there's a MAC or seccomp filter
2188 * in place that doesn't allow us to create namespaces (or a missing cap), then
2189 * propagate a recognizable error back, which the caller can use to detect this case
2190 * (and only this) and optionally continue without namespacing applied. */
1beab8b0
LP
2191 r = -ENOANO;
2192
d944dc95
LP
2193 goto finish;
2194 }
1e4e94c8 2195
5e8deb94
LB
2196 /* Create the source directory to allow runtime propagation of mounts */
2197 if (setup_propagate)
2198 (void) mkdir_p(propagate_dir, 0600);
2199
57ac6959 2200 if (n_extension_images > 0)
93f59701
LB
2201 /* ExtensionImages mountpoint directories will be created
2202 * while parsing the mounts to create, so have the parent ready */
2203 (void) mkdir_p(extension_dir, 0600);
93f59701 2204
9b68367b
YW
2205 /* Remount / as SLAVE so that nothing now mounted in the namespace
2206 * shows up in the parent */
2207 if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) {
763a260a 2208 r = log_debug_errno(errno, "Failed to remount '/' as SLAVE: %m");
9b68367b 2209 goto finish;
ee818b89
AC
2210 }
2211
915e6d16 2212 if (root_image) {
e908468b 2213 /* A root image is specified, mount it to the right place */
21b61b1d 2214 r = dissected_image_mount(dissected_image, root, UID_INVALID, UID_INVALID, dissect_image_flags);
763a260a
YW
2215 if (r < 0) {
2216 log_debug_errno(r, "Failed to mount root image: %m");
915e6d16 2217 goto finish;
763a260a 2218 }
915e6d16 2219
07ce7407
TM
2220 if (decrypted_image) {
2221 r = decrypted_image_relinquish(decrypted_image);
763a260a
YW
2222 if (r < 0) {
2223 log_debug_errno(r, "Failed to relinquish decrypted image: %m");
07ce7407 2224 goto finish;
763a260a 2225 }
07ce7407 2226 }
78ebe980 2227
915e6d16
LP
2228 loop_device_relinquish(loop_device);
2229
2230 } else if (root_directory) {
2231
e908468b
LP
2232 /* A root directory is specified. Turn its directory into bind mount, if it isn't one yet. */
2233 r = path_is_mount_point(root, NULL, AT_SYMLINK_FOLLOW);
763a260a
YW
2234 if (r < 0) {
2235 log_debug_errno(r, "Failed to detect that %s is a mount point or not: %m", root);
d944dc95 2236 goto finish;
763a260a 2237 }
8f1ad200 2238 if (r == 0) {
21935150
LP
2239 r = mount_nofollow_verbose(LOG_DEBUG, root, root, NULL, MS_BIND|MS_REC, NULL);
2240 if (r < 0)
8f1ad200 2241 goto finish;
d944dc95 2242 }
e908468b 2243
9b68367b 2244 } else {
e908468b 2245 /* Let's mount the main root directory to the root directory to use */
21935150
LP
2246 r = mount_nofollow_verbose(LOG_DEBUG, "/", root, NULL, MS_BIND|MS_REC, NULL);
2247 if (r < 0)
e908468b 2248 goto finish;
ee818b89 2249 }
c2c13f2d 2250
4e0c20de
LP
2251 /* Try to set up the new root directory before mounting anything else there. */
2252 if (root_image || root_directory)
2253 (void) base_filesystem_create(root, UID_INVALID, GID_INVALID);
2254
82fb2da2
LB
2255 /* Now make the magic happen */
2256 r = apply_mounts(root, ns_info, mounts, &n_mounts, error_path);
2257 if (r < 0)
2258 goto finish;
15ae422b 2259
9b68367b
YW
2260 /* MS_MOVE does not work on MS_SHARED so the remount MS_SHARED will be done later */
2261 r = mount_move_root(root);
763a260a
YW
2262 if (r < 0) {
2263 log_debug_errno(r, "Failed to mount root with MS_MOVE: %m");
9b68367b 2264 goto finish;
763a260a 2265 }
ee818b89 2266
55fe7432 2267 /* Remount / as the desired mode. Note that this will not
c2c13f2d
LP
2268 * reestablish propagation from our side to the host, since
2269 * what's disconnected is disconnected. */
d944dc95 2270 if (mount(NULL, "/", NULL, mount_flags | MS_REC, NULL) < 0) {
763a260a 2271 r = log_debug_errno(errno, "Failed to remount '/' with desired mount flags: %m");
d944dc95
LP
2272 goto finish;
2273 }
15ae422b 2274
5e8deb94
LB
2275 /* bind_mount_in_namespace() will MS_MOVE into that directory, and that's only
2276 * supported for non-shared mounts. This needs to happen after remounting / or it will fail. */
2277 if (setup_propagate) {
2278 r = mount(NULL, incoming_dir, NULL, MS_SLAVE, NULL);
2279 if (r < 0) {
2280 log_error_errno(r, "Failed to remount %s with MS_SLAVE: %m", incoming_dir);
2281 goto finish;
2282 }
2283 }
2284
d944dc95 2285 r = 0;
15ae422b 2286
d944dc95 2287finish:
0cd41757
LP
2288 if (n_mounts > 0)
2289 for (m = mounts; m < mounts + n_mounts; m++)
2290 mount_entry_done(m);
613b411c 2291
5f7a690a
LP
2292 free(mounts);
2293
613b411c
LP
2294 return r;
2295}
2296
da6053d0 2297void bind_mount_free_many(BindMount *b, size_t n) {
d2d6c096
LP
2298 assert(b || n == 0);
2299
fe96c0f8 2300 for (size_t i = 0; i < n; i++) {
d2d6c096
LP
2301 free(b[i].source);
2302 free(b[i].destination);
2303 }
2304
2305 free(b);
2306}
2307
da6053d0 2308int bind_mount_add(BindMount **b, size_t *n, const BindMount *item) {
d2d6c096
LP
2309 _cleanup_free_ char *s = NULL, *d = NULL;
2310 BindMount *c;
2311
2312 assert(b);
2313 assert(n);
2314 assert(item);
2315
2316 s = strdup(item->source);
2317 if (!s)
2318 return -ENOMEM;
2319
2320 d = strdup(item->destination);
2321 if (!d)
2322 return -ENOMEM;
2323
aa484f35 2324 c = reallocarray(*b, *n + 1, sizeof(BindMount));
d2d6c096
LP
2325 if (!c)
2326 return -ENOMEM;
2327
2328 *b = c;
2329
2330 c[(*n) ++] = (BindMount) {
1cc6c93a
YW
2331 .source = TAKE_PTR(s),
2332 .destination = TAKE_PTR(d),
d2d6c096 2333 .read_only = item->read_only,
9ce4e4b0 2334 .nosuid = item->nosuid,
d2d6c096
LP
2335 .recursive = item->recursive,
2336 .ignore_enoent = item->ignore_enoent,
2337 };
2338
d2d6c096
LP
2339 return 0;
2340}
2341
b3d13314 2342MountImage* mount_image_free_many(MountImage *m, size_t *n) {
b3d13314
LB
2343 assert(n);
2344 assert(m || *n == 0);
2345
fe96c0f8 2346 for (size_t i = 0; i < *n; i++) {
b3d13314
LB
2347 free(m[i].source);
2348 free(m[i].destination);
427353f6 2349 mount_options_free_all(m[i].mount_options);
b3d13314
LB
2350 }
2351
2352 free(m);
2353 *n = 0;
2354 return NULL;
2355}
2356
2357int mount_image_add(MountImage **m, size_t *n, const MountImage *item) {
2358 _cleanup_free_ char *s = NULL, *d = NULL;
427353f6
LB
2359 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
2360 MountOptions *i;
b3d13314
LB
2361 MountImage *c;
2362
2363 assert(m);
2364 assert(n);
2365 assert(item);
2366
2367 s = strdup(item->source);
2368 if (!s)
2369 return -ENOMEM;
2370
93f59701
LB
2371 if (item->destination) {
2372 d = strdup(item->destination);
2373 if (!d)
2374 return -ENOMEM;
2375 }
b3d13314 2376
427353f6 2377 LIST_FOREACH(mount_options, i, item->mount_options) {
c2b2df60 2378 _cleanup_(mount_options_free_allp) MountOptions *o = NULL;
427353f6
LB
2379
2380 o = new(MountOptions, 1);
2381 if (!o)
2382 return -ENOMEM;
2383
2384 *o = (MountOptions) {
2385 .partition_designator = i->partition_designator,
2386 .options = strdup(i->options),
2387 };
2388 if (!o->options)
2389 return -ENOMEM;
2390
2391 LIST_APPEND(mount_options, options, TAKE_PTR(o));
2392 }
2393
b3d13314
LB
2394 c = reallocarray(*m, *n + 1, sizeof(MountImage));
2395 if (!c)
2396 return -ENOMEM;
2397
2398 *m = c;
2399
2400 c[(*n) ++] = (MountImage) {
2401 .source = TAKE_PTR(s),
2402 .destination = TAKE_PTR(d),
427353f6 2403 .mount_options = TAKE_PTR(options),
b3d13314 2404 .ignore_enoent = item->ignore_enoent,
93f59701 2405 .type = item->type,
b3d13314
LB
2406 };
2407
2408 return 0;
2409}
2410
da6053d0 2411void temporary_filesystem_free_many(TemporaryFileSystem *t, size_t n) {
2abd4e38
YW
2412 assert(t || n == 0);
2413
fe96c0f8 2414 for (size_t i = 0; i < n; i++) {
2abd4e38
YW
2415 free(t[i].path);
2416 free(t[i].options);
2417 }
2418
2419 free(t);
2420}
2421
2422int temporary_filesystem_add(
2423 TemporaryFileSystem **t,
da6053d0 2424 size_t *n,
2abd4e38
YW
2425 const char *path,
2426 const char *options) {
2427
2428 _cleanup_free_ char *p = NULL, *o = NULL;
2429 TemporaryFileSystem *c;
2430
2431 assert(t);
2432 assert(n);
2433 assert(path);
2434
2435 p = strdup(path);
2436 if (!p)
2437 return -ENOMEM;
2438
2439 if (!isempty(options)) {
2440 o = strdup(options);
2441 if (!o)
2442 return -ENOMEM;
2443 }
2444
aa484f35 2445 c = reallocarray(*t, *n + 1, sizeof(TemporaryFileSystem));
2abd4e38
YW
2446 if (!c)
2447 return -ENOMEM;
2448
2449 *t = c;
2450
2451 c[(*n) ++] = (TemporaryFileSystem) {
1cc6c93a
YW
2452 .path = TAKE_PTR(p),
2453 .options = TAKE_PTR(o),
2abd4e38
YW
2454 };
2455
2abd4e38
YW
2456 return 0;
2457}
2458
a652f050
JR
2459static int make_tmp_prefix(const char *prefix) {
2460 _cleanup_free_ char *t = NULL;
2461 int r;
2462
2463 /* Don't do anything unless we know the dir is actually missing */
2464 r = access(prefix, F_OK);
2465 if (r >= 0)
2466 return 0;
2467 if (errno != ENOENT)
2468 return -errno;
2469
2470 r = mkdir_parents(prefix, 0755);
2471 if (r < 0)
2472 return r;
2473
2474 r = tempfn_random(prefix, NULL, &t);
2475 if (r < 0)
2476 return r;
2477
2478 if (mkdir(t, 0777) < 0)
2479 return -errno;
2480
2481 if (chmod(t, 01777) < 0) {
2482 r = -errno;
2483 (void) rmdir(t);
2484 return r;
2485 }
2486
2487 if (rename(t, prefix) < 0) {
2488 r = -errno;
2489 (void) rmdir(t);
2490 return r == -EEXIST ? 0 : r; /* it's fine if someone else created the dir by now */
2491 }
2492
2493 return 0;
2494
2495}
2496
56a13a49 2497static int setup_one_tmp_dir(const char *id, const char *prefix, char **path, char **tmp_path) {
613b411c 2498 _cleanup_free_ char *x = NULL;
19cd4e19 2499 _cleanup_free_ char *y = NULL;
6b46ea73
LP
2500 char bid[SD_ID128_STRING_MAX];
2501 sd_id128_t boot_id;
56a13a49 2502 bool rw = true;
6b46ea73 2503 int r;
613b411c
LP
2504
2505 assert(id);
2506 assert(prefix);
2507 assert(path);
2508
6b46ea73
LP
2509 /* We include the boot id in the directory so that after a
2510 * reboot we can easily identify obsolete directories. */
2511
2512 r = sd_id128_get_boot(&boot_id);
2513 if (r < 0)
2514 return r;
2515
605405c6 2516 x = strjoin(prefix, "/systemd-private-", sd_id128_to_string(boot_id, bid), "-", id, "-XXXXXX");
613b411c
LP
2517 if (!x)
2518 return -ENOMEM;
2519
a652f050
JR
2520 r = make_tmp_prefix(prefix);
2521 if (r < 0)
2522 return r;
2523
613b411c 2524 RUN_WITH_UMASK(0077)
56a13a49
ZJS
2525 if (!mkdtemp(x)) {
2526 if (errno == EROFS || ERRNO_IS_DISK_SPACE(errno))
2527 rw = false;
2528 else
2529 return -errno;
2530 }
613b411c 2531
56a13a49 2532 if (rw) {
19cd4e19 2533 y = strjoin(x, "/tmp");
2534 if (!y)
2535 return -ENOMEM;
2536
2537 RUN_WITH_UMASK(0000) {
2538 if (mkdir(y, 0777 | S_ISVTX) < 0)
2539 return -errno;
2540 }
2541
2542 r = label_fix_container(y, prefix, 0);
56a13a49
ZJS
2543 if (r < 0)
2544 return r;
19cd4e19 2545
2546 if (tmp_path)
2547 *tmp_path = TAKE_PTR(y);
56a13a49
ZJS
2548 } else {
2549 /* Trouble: we failed to create the directory. Instead of failing, let's simulate /tmp being
2550 * read-only. This way the service will get the EROFS result as if it was writing to the real
2551 * file system. */
2552 r = mkdir_p(RUN_SYSTEMD_EMPTY, 0500);
2553 if (r < 0)
2554 return r;
613b411c 2555
3f181262
LP
2556 r = free_and_strdup(&x, RUN_SYSTEMD_EMPTY);
2557 if (r < 0)
2558 return r;
c17ec25e 2559 }
15ae422b 2560
1cc6c93a 2561 *path = TAKE_PTR(x);
613b411c
LP
2562 return 0;
2563}
2564
2565int setup_tmp_dirs(const char *id, char **tmp_dir, char **var_tmp_dir) {
56a13a49
ZJS
2566 _cleanup_(namespace_cleanup_tmpdirp) char *a = NULL;
2567 _cleanup_(rmdir_and_freep) char *a_tmp = NULL;
2568 char *b;
613b411c
LP
2569 int r;
2570
2571 assert(id);
2572 assert(tmp_dir);
2573 assert(var_tmp_dir);
2574
56a13a49 2575 r = setup_one_tmp_dir(id, "/tmp", &a, &a_tmp);
613b411c
LP
2576 if (r < 0)
2577 return r;
2578
56a13a49
ZJS
2579 r = setup_one_tmp_dir(id, "/var/tmp", &b, NULL);
2580 if (r < 0)
613b411c 2581 return r;
613b411c 2582
56a13a49
ZJS
2583 a_tmp = mfree(a_tmp); /* avoid rmdir */
2584 *tmp_dir = TAKE_PTR(a);
2585 *var_tmp_dir = TAKE_PTR(b);
613b411c
LP
2586
2587 return 0;
2588}
2589
54c2459d
XR
2590int setup_shareable_ns(const int ns_storage_socket[static 2], unsigned long nsflag) {
2591 _cleanup_close_ int ns = -1;
3ee897d6 2592 int r, q;
54c2459d 2593 const char *ns_name, *ns_path;
613b411c 2594
54c2459d
XR
2595 assert(ns_storage_socket);
2596 assert(ns_storage_socket[0] >= 0);
2597 assert(ns_storage_socket[1] >= 0);
2598
2599 ns_name = namespace_single_flag_to_string(nsflag);
2600 assert(ns_name);
613b411c
LP
2601
2602 /* We use the passed socketpair as a storage buffer for our
76cd584b
LP
2603 * namespace reference fd. Whatever process runs this first
2604 * shall create a new namespace, all others should just join
2605 * it. To serialize that we use a file lock on the socket
2606 * pair.
613b411c
LP
2607 *
2608 * It's a bit crazy, but hey, works great! */
2609
54c2459d 2610 if (lockf(ns_storage_socket[0], F_LOCK, 0) < 0)
613b411c
LP
2611 return -errno;
2612
54c2459d
XR
2613 ns = receive_one_fd(ns_storage_socket[0], MSG_DONTWAIT);
2614 if (ns == -EAGAIN) {
44ffcbae 2615 /* Nothing stored yet, so let's create a new namespace. */
613b411c 2616
54c2459d 2617 if (unshare(nsflag) < 0) {
613b411c
LP
2618 r = -errno;
2619 goto fail;
2620 }
2621
44ffcbae 2622 (void) loopback_setup();
613b411c 2623
54c2459d
XR
2624 ns_path = strjoina("/proc/self/ns/", ns_name);
2625 ns = open(ns_path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
2626 if (ns < 0) {
613b411c
LP
2627 r = -errno;
2628 goto fail;
2629 }
2630
2631 r = 1;
613b411c 2632
54c2459d
XR
2633 } else if (ns < 0) {
2634 r = ns;
3ee897d6 2635 goto fail;
613b411c 2636
3ee897d6
LP
2637 } else {
2638 /* Yay, found something, so let's join the namespace */
54c2459d 2639 if (setns(ns, nsflag) < 0) {
613b411c
LP
2640 r = -errno;
2641 goto fail;
2642 }
2643
2644 r = 0;
2645 }
2646
54c2459d 2647 q = send_one_fd(ns_storage_socket[1], ns, MSG_DONTWAIT);
3ee897d6
LP
2648 if (q < 0) {
2649 r = q;
613b411c
LP
2650 goto fail;
2651 }
2652
2653fail:
54c2459d 2654 (void) lockf(ns_storage_socket[0], F_ULOCK, 0);
15ae422b
LP
2655 return r;
2656}
417116f2 2657
54c2459d
XR
2658int open_shareable_ns_path(const int ns_storage_socket[static 2], const char *path, unsigned long nsflag) {
2659 _cleanup_close_ int ns = -1;
51af7fb2
LP
2660 int q, r;
2661
54c2459d
XR
2662 assert(ns_storage_socket);
2663 assert(ns_storage_socket[0] >= 0);
2664 assert(ns_storage_socket[1] >= 0);
51af7fb2
LP
2665 assert(path);
2666
54c2459d
XR
2667 /* If the storage socket doesn't contain a ns fd yet, open one via the file system and store it in
2668 * it. This is supposed to be called ahead of time, i.e. before setup_shareable_ns() which will
2669 * allocate a new anonymous ns if needed. */
51af7fb2 2670
54c2459d 2671 if (lockf(ns_storage_socket[0], F_LOCK, 0) < 0)
51af7fb2
LP
2672 return -errno;
2673
54c2459d
XR
2674 ns = receive_one_fd(ns_storage_socket[0], MSG_DONTWAIT);
2675 if (ns == -EAGAIN) {
51af7fb2
LP
2676 /* Nothing stored yet. Open the file from the file system. */
2677
54c2459d
XR
2678 ns = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
2679 if (ns < 0) {
51af7fb2
LP
2680 r = -errno;
2681 goto fail;
2682 }
2683
54c2459d
XR
2684 r = fd_is_ns(ns, nsflag);
2685 if (r == 0) { /* Not a ns of our type? Refuse early. */
51af7fb2
LP
2686 r = -EINVAL;
2687 goto fail;
2688 }
2689 if (r < 0 && r != -EUCLEAN) /* EUCLEAN: we don't know */
2690 goto fail;
2691
2692 r = 1;
2693
54c2459d
XR
2694 } else if (ns < 0) {
2695 r = ns;
51af7fb2
LP
2696 goto fail;
2697 } else
2698 r = 0; /* Already allocated */
2699
54c2459d 2700 q = send_one_fd(ns_storage_socket[1], ns, MSG_DONTWAIT);
51af7fb2
LP
2701 if (q < 0) {
2702 r = q;
2703 goto fail;
2704 }
2705
2706fail:
54c2459d 2707 (void) lockf(ns_storage_socket[0], F_ULOCK, 0);
51af7fb2
LP
2708 return r;
2709}
2710
6e2d7c4f
MS
2711bool ns_type_supported(NamespaceType type) {
2712 const char *t, *ns_proc;
2713
0fa5b831
LP
2714 t = namespace_type_to_string(type);
2715 if (!t) /* Don't know how to translate this? Then it's not supported */
6e2d7c4f
MS
2716 return false;
2717
6e2d7c4f 2718 ns_proc = strjoina("/proc/self/ns/", t);
6e2d7c4f
MS
2719 return access(ns_proc, F_OK) == 0;
2720}
2721
1b8689f9 2722static const char *const protect_home_table[_PROTECT_HOME_MAX] = {
cbc056c8
ZJS
2723 [PROTECT_HOME_NO] = "no",
2724 [PROTECT_HOME_YES] = "yes",
1b8689f9 2725 [PROTECT_HOME_READ_ONLY] = "read-only",
cbc056c8 2726 [PROTECT_HOME_TMPFS] = "tmpfs",
417116f2
LP
2727};
2728
1e8c7bd5 2729DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(protect_home, ProtectHome, PROTECT_HOME_YES);
5e1c6154 2730
1b8689f9 2731static const char *const protect_system_table[_PROTECT_SYSTEM_MAX] = {
cbc056c8
ZJS
2732 [PROTECT_SYSTEM_NO] = "no",
2733 [PROTECT_SYSTEM_YES] = "yes",
2734 [PROTECT_SYSTEM_FULL] = "full",
3f815163 2735 [PROTECT_SYSTEM_STRICT] = "strict",
1b8689f9
LP
2736};
2737
1e8c7bd5 2738DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(protect_system, ProtectSystem, PROTECT_SYSTEM_YES);
03c791aa 2739
6e2d7c4f 2740static const char* const namespace_type_table[] = {
cbc056c8 2741 [NAMESPACE_MOUNT] = "mnt",
6e2d7c4f 2742 [NAMESPACE_CGROUP] = "cgroup",
cbc056c8
ZJS
2743 [NAMESPACE_UTS] = "uts",
2744 [NAMESPACE_IPC] = "ipc",
2745 [NAMESPACE_USER] = "user",
2746 [NAMESPACE_PID] = "pid",
2747 [NAMESPACE_NET] = "net",
6e2d7c4f
MS
2748};
2749
2750DEFINE_STRING_TABLE_LOOKUP(namespace_type, NamespaceType);
4e399953
LP
2751
2752static const char* const protect_proc_table[_PROTECT_PROC_MAX] = {
2753 [PROTECT_PROC_DEFAULT] = "default",
2754 [PROTECT_PROC_NOACCESS] = "noaccess",
2755 [PROTECT_PROC_INVISIBLE] = "invisible",
2756 [PROTECT_PROC_PTRACEABLE] = "ptraceable",
2757};
2758
2759DEFINE_STRING_TABLE_LOOKUP(protect_proc, ProtectProc);
2760
2761static const char* const proc_subset_table[_PROC_SUBSET_MAX] = {
2762 [PROC_SUBSET_ALL] = "all",
2763 [PROC_SUBSET_PID] = "pid",
2764};
2765
2766DEFINE_STRING_TABLE_LOOKUP(proc_subset, ProcSubset);