]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/namespace.c
Fix missing space in comments (#5439)
[thirdparty/systemd.git] / src / core / namespace.c
CommitLineData
15ae422b
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
15ae422b
LP
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 14 Lesser General Public License for more details.
15ae422b 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
15ae422b
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include <errno.h>
07630cea 21#include <sched.h>
15ae422b 22#include <stdio.h>
07630cea
LP
23#include <string.h>
24#include <sys/mount.h>
15ae422b 25#include <sys/stat.h>
07630cea 26#include <unistd.h>
25e870b5 27#include <linux/fs.h>
15ae422b 28
b5efdb8a 29#include "alloc-util.h"
7f112f50 30#include "dev-setup.h"
3ffd4af2 31#include "fd-util.h"
d944dc95 32#include "fs-util.h"
915e6d16 33#include "loop-util.h"
07630cea
LP
34#include "loopback-setup.h"
35#include "missing.h"
36#include "mkdir.h"
4349cd7c 37#include "mount-util.h"
3ffd4af2 38#include "namespace.h"
07630cea 39#include "path-util.h"
d7b8eec7 40#include "selinux-util.h"
2583fbea 41#include "socket-util.h"
8b43440b 42#include "string-table.h"
07630cea
LP
43#include "string-util.h"
44#include "strv.h"
affb60b1 45#include "umask-util.h"
ee104e11 46#include "user-util.h"
07630cea 47#include "util.h"
15ae422b 48
737ba3c8 49#define DEV_MOUNT_OPTIONS (MS_NOSUID|MS_STRICTATIME|MS_NOEXEC)
50
c17ec25e 51typedef enum MountMode {
15ae422b
LP
52 /* This is ordered by priority! */
53 INACCESSIBLE,
d2d6c096
LP
54 BIND_MOUNT,
55 BIND_MOUNT_RECURSIVE,
ac0930c8
LP
56 PRIVATE_TMP,
57 PRIVATE_VAR_TMP,
7f112f50 58 PRIVATE_DEV,
5d997827
LP
59 BIND_DEV,
60 SYSFS,
61 PROCFS,
62 READONLY,
59eeb84b 63 READWRITE,
c17ec25e 64} MountMode;
15ae422b 65
34de407a 66typedef struct MountEntry {
5327c910 67 const char *path_const; /* Memory allocated on stack or static */
cfbeb4ef 68 MountMode mode:5;
5327c910
LP
69 bool ignore:1; /* Ignore if path does not exist? */
70 bool has_prefix:1; /* Already is prefixed by the root dir? */
cfbeb4ef 71 bool read_only:1; /* Shall this mount point be read-only? */
5327c910 72 char *path_malloc; /* Use this instead of 'path' if we had to allocate memory */
d2d6c096
LP
73 const char *source_const; /* The source path, for bind mounts */
74 char *source_malloc;
34de407a 75} MountEntry;
15ae422b 76
5d997827
LP
77/* If MountAPIVFS= is used, let's mount /sys and /proc into the it, but only as a fallback if the user hasn't mounted
78 * something there already. These mounts are hence overriden by any other explicitly configured mounts. */
79static const MountEntry apivfs_table[] = {
80 { "/proc", PROCFS, false },
81 { "/dev", BIND_DEV, false },
82 { "/sys", SYSFS, false },
83};
f471b2af 84
11a30cec 85/* ProtectKernelTunables= option and the related filesystem APIs */
34de407a 86static const MountEntry protect_kernel_tunables_table[] = {
c6232fb0
LP
87 { "/proc/sys", READONLY, false },
88 { "/proc/sysrq-trigger", READONLY, true },
89 { "/proc/latency_stats", READONLY, true },
90 { "/proc/mtrr", READONLY, true },
aa70f38b 91 { "/proc/apm", READONLY, true }, /* Obsolete API, there's no point in permitting access to this, ever */
c6232fb0
LP
92 { "/proc/acpi", READONLY, true },
93 { "/proc/timer_stats", READONLY, true },
94 { "/proc/asound", READONLY, true },
95 { "/proc/bus", READONLY, true },
96 { "/proc/fs", READONLY, true },
97 { "/proc/irq", READONLY, true },
98 { "/sys", READONLY, false },
99 { "/sys/kernel/debug", READONLY, true },
100 { "/sys/kernel/tracing", READONLY, true },
101 { "/sys/fs/cgroup", READWRITE, false }, /* READONLY is set by ProtectControlGroups= option */
11a30cec
DH
102};
103
c575770b 104/* ProtectKernelModules= option */
34de407a 105static const MountEntry protect_kernel_modules_table[] = {
c575770b 106#ifdef HAVE_SPLIT_USR
c6232fb0 107 { "/lib/modules", INACCESSIBLE, true },
c575770b 108#endif
c6232fb0 109 { "/usr/lib/modules", INACCESSIBLE, true },
c575770b
DH
110};
111
b6c432ca
DH
112/*
113 * ProtectHome=read-only table, protect $HOME and $XDG_RUNTIME_DIR and rest of
114 * system should be protected by ProtectSystem=
115 */
34de407a 116static const MountEntry protect_home_read_only_table[] = {
c6232fb0
LP
117 { "/home", READONLY, true },
118 { "/run/user", READONLY, true },
119 { "/root", READONLY, true },
b6c432ca
DH
120};
121
122/* ProtectHome=yes table */
34de407a 123static const MountEntry protect_home_yes_table[] = {
c6232fb0
LP
124 { "/home", INACCESSIBLE, true },
125 { "/run/user", INACCESSIBLE, true },
126 { "/root", INACCESSIBLE, true },
b6c432ca
DH
127};
128
f471b2af 129/* ProtectSystem=yes table */
34de407a 130static const MountEntry protect_system_yes_table[] = {
c6232fb0
LP
131 { "/usr", READONLY, false },
132 { "/boot", READONLY, true },
133 { "/efi", READONLY, true },
f471b2af
DH
134};
135
136/* ProtectSystem=full includes ProtectSystem=yes */
34de407a 137static const MountEntry protect_system_full_table[] = {
c6232fb0
LP
138 { "/usr", READONLY, false },
139 { "/boot", READONLY, true },
140 { "/efi", READONLY, true },
141 { "/etc", READONLY, false },
f471b2af
DH
142};
143
144/*
145 * ProtectSystem=strict table. In this strict mode, we mount everything
146 * read-only, except for /proc, /dev, /sys which are the kernel API VFS,
147 * which are left writable, but PrivateDevices= + ProtectKernelTunables=
148 * protect those, and these options should be fully orthogonal.
149 * (And of course /home and friends are also left writable, as ProtectHome=
150 * shall manage those, orthogonally).
151 */
34de407a 152static const MountEntry protect_system_strict_table[] = {
ddbe0412
LP
153 { "/", READONLY, false },
154 { "/proc", READWRITE, false }, /* ProtectKernelTunables= */
155 { "/sys", READWRITE, false }, /* ProtectKernelTunables= */
156 { "/dev", READWRITE, false }, /* PrivateDevices= */
157 { "/home", READWRITE, true }, /* ProtectHome= */
158 { "/run/user", READWRITE, true }, /* ProtectHome= */
159 { "/root", READWRITE, true }, /* ProtectHome= */
f471b2af
DH
160};
161
34de407a 162static const char *mount_entry_path(const MountEntry *p) {
f0a4feb0
DH
163 assert(p);
164
5327c910
LP
165 /* Returns the path of this bind mount. If the malloc()-allocated ->path_buffer field is set we return that,
166 * otherwise the stack/static ->path field is returned. */
f0a4feb0 167
5327c910 168 return p->path_malloc ?: p->path_const;
f0a4feb0
DH
169}
170
34de407a 171static bool mount_entry_read_only(const MountEntry *p) {
cfbeb4ef
LP
172 assert(p);
173
174 return p->read_only || IN_SET(p->mode, READONLY, INACCESSIBLE);
175}
176
d2d6c096
LP
177static const char *mount_entry_source(const MountEntry *p) {
178 assert(p);
179
180 return p->source_malloc ?: p->source_const;
181}
182
1eb7e08e
LP
183static void mount_entry_done(MountEntry *p) {
184 assert(p);
185
186 p->path_malloc = mfree(p->path_malloc);
187 p->source_malloc = mfree(p->source_malloc);
188}
189
34de407a 190static int append_access_mounts(MountEntry **p, char **strv, MountMode mode) {
15ae422b
LP
191 char **i;
192
613b411c
LP
193 assert(p);
194
5327c910
LP
195 /* Adds a list of user-supplied READWRITE/READONLY/INACCESSIBLE entries */
196
15ae422b 197 STRV_FOREACH(i, strv) {
5327c910
LP
198 bool ignore = false, needs_prefix = false;
199 const char *e = *i;
15ae422b 200
5327c910
LP
201 /* Look for any prefixes */
202 if (startswith(e, "-")) {
203 e++;
9c94d52e 204 ignore = true;
ea92ae33 205 }
5327c910
LP
206 if (startswith(e, "+")) {
207 e++;
208 needs_prefix = true;
209 }
ea92ae33 210
5327c910 211 if (!path_is_absolute(e))
15ae422b
LP
212 return -EINVAL;
213
34de407a 214 *((*p)++) = (MountEntry) {
5327c910
LP
215 .path_const = e,
216 .mode = mode,
217 .ignore = ignore,
218 .has_prefix = !needs_prefix,
219 };
15ae422b
LP
220 }
221
222 return 0;
223}
224
d2d6c096
LP
225static int append_bind_mounts(MountEntry **p, const BindMount *binds, unsigned n) {
226 unsigned i;
227
228 assert(p);
229
230 for (i = 0; i < n; i++) {
231 const BindMount *b = binds + i;
232
233 *((*p)++) = (MountEntry) {
234 .path_const = b->destination,
235 .mode = b->recursive ? BIND_MOUNT_RECURSIVE : BIND_MOUNT,
236 .read_only = b->read_only,
237 .source_const = b->source,
238 };
239 }
240
241 return 0;
242}
243
34de407a 244static int append_static_mounts(MountEntry **p, const MountEntry *mounts, unsigned n, bool ignore_protect) {
f471b2af 245 unsigned i;
11a30cec
DH
246
247 assert(p);
f471b2af 248 assert(mounts);
11a30cec 249
5327c910 250 /* Adds a list of static pre-defined entries */
f471b2af 251
5327c910 252 for (i = 0; i < n; i++)
34de407a
LP
253 *((*p)++) = (MountEntry) {
254 .path_const = mount_entry_path(mounts+i),
5327c910
LP
255 .mode = mounts[i].mode,
256 .ignore = mounts[i].ignore || ignore_protect,
257 };
f471b2af
DH
258
259 return 0;
260}
261
34de407a 262static int append_protect_home(MountEntry **p, ProtectHome protect_home, bool ignore_protect) {
c575770b
DH
263 assert(p);
264
5327c910 265 switch (protect_home) {
b6c432ca 266
5327c910 267 case PROTECT_HOME_NO:
b6c432ca
DH
268 return 0;
269
b6c432ca 270 case PROTECT_HOME_READ_ONLY:
5327c910
LP
271 return append_static_mounts(p, protect_home_read_only_table, ELEMENTSOF(protect_home_read_only_table), ignore_protect);
272
b6c432ca 273 case PROTECT_HOME_YES:
5327c910
LP
274 return append_static_mounts(p, protect_home_yes_table, ELEMENTSOF(protect_home_yes_table), ignore_protect);
275
b6c432ca 276 default:
5327c910 277 assert_not_reached("Unexpected ProtectHome= value");
b6c432ca 278 }
b6c432ca
DH
279}
280
34de407a 281static int append_protect_system(MountEntry **p, ProtectSystem protect_system, bool ignore_protect) {
f471b2af
DH
282 assert(p);
283
5327c910
LP
284 switch (protect_system) {
285
286 case PROTECT_SYSTEM_NO:
f471b2af
DH
287 return 0;
288
f471b2af 289 case PROTECT_SYSTEM_STRICT:
5327c910
LP
290 return append_static_mounts(p, protect_system_strict_table, ELEMENTSOF(protect_system_strict_table), ignore_protect);
291
f471b2af 292 case PROTECT_SYSTEM_YES:
5327c910
LP
293 return append_static_mounts(p, protect_system_yes_table, ELEMENTSOF(protect_system_yes_table), ignore_protect);
294
f471b2af 295 case PROTECT_SYSTEM_FULL:
5327c910
LP
296 return append_static_mounts(p, protect_system_full_table, ELEMENTSOF(protect_system_full_table), ignore_protect);
297
f471b2af 298 default:
5327c910 299 assert_not_reached("Unexpected ProtectSystem= value");
f471b2af 300 }
11a30cec
DH
301}
302
c17ec25e 303static int mount_path_compare(const void *a, const void *b) {
34de407a 304 const MountEntry *p = a, *q = b;
a0827e2b 305 int d;
15ae422b 306
6ee1a919 307 /* If the paths are not equal, then order prefixes first */
34de407a 308 d = path_compare(mount_entry_path(p), mount_entry_path(q));
6ee1a919
LP
309 if (d != 0)
310 return d;
15ae422b 311
6ee1a919
LP
312 /* If the paths are equal, check the mode */
313 if (p->mode < q->mode)
314 return -1;
15ae422b 315
6ee1a919
LP
316 if (p->mode > q->mode)
317 return 1;
15ae422b 318
6ee1a919 319 return 0;
15ae422b
LP
320}
321
34de407a 322static int prefix_where_needed(MountEntry *m, unsigned n, const char *root_directory) {
5327c910
LP
323 unsigned i;
324
325 /* Prefixes all paths in the bind mount table with the root directory if it is specified and the entry needs
326 * that. */
327
328 if (!root_directory)
329 return 0;
330
331 for (i = 0; i < n; i++) {
332 char *s;
333
334 if (m[i].has_prefix)
335 continue;
336
34de407a 337 s = prefix_root(root_directory, mount_entry_path(m+i));
5327c910
LP
338 if (!s)
339 return -ENOMEM;
340
341 free(m[i].path_malloc);
342 m[i].path_malloc = s;
343
344 m[i].has_prefix = true;
345 }
346
347 return 0;
348}
349
34de407a
LP
350static void drop_duplicates(MountEntry *m, unsigned *n) {
351 MountEntry *f, *t, *previous;
15ae422b 352
c17ec25e 353 assert(m);
15ae422b 354 assert(n);
15ae422b 355
fe3c2583
LP
356 /* Drops duplicate entries. Expects that the array is properly ordered already. */
357
1d54cd5d 358 for (f = m, t = m, previous = NULL; f < m + *n; f++) {
15ae422b 359
fe3c2583
LP
360 /* The first one wins (which is the one with the more restrictive mode), see mount_path_compare()
361 * above. */
34de407a
LP
362 if (previous && path_equal(mount_entry_path(f), mount_entry_path(previous))) {
363 log_debug("%s is duplicate.", mount_entry_path(f));
364 previous->read_only = previous->read_only || mount_entry_read_only(f); /* Propagate the read-only flag to the remaining entry */
1eb7e08e 365 mount_entry_done(f);
15ae422b 366 continue;
fe3c2583 367 }
15ae422b 368
e2d7c1a0 369 *t = *f;
15ae422b 370 previous = t;
fe3c2583
LP
371 t++;
372 }
373
374 *n = t - m;
375}
376
34de407a
LP
377static void drop_inaccessible(MountEntry *m, unsigned *n) {
378 MountEntry *f, *t;
fe3c2583
LP
379 const char *clear = NULL;
380
381 assert(m);
382 assert(n);
383
384 /* Drops all entries obstructed by another entry further up the tree. Expects that the array is properly
385 * ordered already. */
386
1d54cd5d 387 for (f = m, t = m; f < m + *n; f++) {
fe3c2583
LP
388
389 /* If we found a path set for INACCESSIBLE earlier, and this entry has it as prefix we should drop
390 * it, as inaccessible paths really should drop the entire subtree. */
34de407a
LP
391 if (clear && path_startswith(mount_entry_path(f), clear)) {
392 log_debug("%s is masked by %s.", mount_entry_path(f), clear);
1eb7e08e 393 mount_entry_done(f);
fe3c2583
LP
394 continue;
395 }
15ae422b 396
34de407a 397 clear = f->mode == INACCESSIBLE ? mount_entry_path(f) : NULL;
fe3c2583
LP
398
399 *t = *f;
15ae422b
LP
400 t++;
401 }
402
c17ec25e 403 *n = t - m;
15ae422b
LP
404}
405
34de407a
LP
406static void drop_nop(MountEntry *m, unsigned *n) {
407 MountEntry *f, *t;
7648a565
LP
408
409 assert(m);
410 assert(n);
411
412 /* Drops all entries which have an immediate parent that has the same type, as they are redundant. Assumes the
413 * list is ordered by prefixes. */
414
1d54cd5d 415 for (f = m, t = m; f < m + *n; f++) {
7648a565
LP
416
417 /* Only suppress such subtrees for READONLY and READWRITE entries */
418 if (IN_SET(f->mode, READONLY, READWRITE)) {
34de407a 419 MountEntry *p;
7648a565
LP
420 bool found = false;
421
422 /* Now let's find the first parent of the entry we are looking at. */
423 for (p = t-1; p >= m; p--) {
34de407a 424 if (path_startswith(mount_entry_path(f), mount_entry_path(p))) {
7648a565
LP
425 found = true;
426 break;
427 }
428 }
429
430 /* We found it, let's see if it's the same mode, if so, we can drop this entry */
431 if (found && p->mode == f->mode) {
34de407a 432 log_debug("%s is redundant by %s", mount_entry_path(f), mount_entry_path(p));
1eb7e08e 433 mount_entry_done(f);
7648a565
LP
434 continue;
435 }
436 }
437
438 *t = *f;
439 t++;
440 }
441
442 *n = t - m;
443}
444
34de407a
LP
445static void drop_outside_root(const char *root_directory, MountEntry *m, unsigned *n) {
446 MountEntry *f, *t;
cd2902c9
LP
447
448 assert(m);
449 assert(n);
450
1d54cd5d 451 /* Nothing to do */
cd2902c9
LP
452 if (!root_directory)
453 return;
454
455 /* Drops all mounts that are outside of the root directory. */
456
1d54cd5d 457 for (f = m, t = m; f < m + *n; f++) {
cd2902c9 458
34de407a
LP
459 if (!path_startswith(mount_entry_path(f), root_directory)) {
460 log_debug("%s is outside of root directory.", mount_entry_path(f));
1eb7e08e 461 mount_entry_done(f);
cd2902c9
LP
462 continue;
463 }
464
465 *t = *f;
466 t++;
467 }
468
469 *n = t - m;
470}
471
5d997827 472static int mount_private_dev(MountEntry *m) {
7f112f50
LP
473 static const char devnodes[] =
474 "/dev/null\0"
475 "/dev/zero\0"
476 "/dev/full\0"
477 "/dev/random\0"
478 "/dev/urandom\0"
479 "/dev/tty\0";
480
2b85f4e1 481 char temporary_mount[] = "/tmp/namespace-dev-XXXXXX";
63cc4c31 482 const char *d, *dev = NULL, *devpts = NULL, *devshm = NULL, *devhugepages = NULL, *devmqueue = NULL, *devlog = NULL, *devptmx = NULL;
7f112f50
LP
483 _cleanup_umask_ mode_t u;
484 int r;
485
486 assert(m);
487
488 u = umask(0000);
489
2b85f4e1
LP
490 if (!mkdtemp(temporary_mount))
491 return -errno;
492
63c372cb 493 dev = strjoina(temporary_mount, "/dev");
dc751688 494 (void) mkdir(dev, 0755);
737ba3c8 495 if (mount("tmpfs", dev, "tmpfs", DEV_MOUNT_OPTIONS, "mode=755") < 0) {
2b85f4e1
LP
496 r = -errno;
497 goto fail;
498 }
499
63c372cb 500 devpts = strjoina(temporary_mount, "/dev/pts");
dc751688 501 (void) mkdir(devpts, 0755);
2b85f4e1
LP
502 if (mount("/dev/pts", devpts, NULL, MS_BIND, NULL) < 0) {
503 r = -errno;
504 goto fail;
505 }
506
63c372cb 507 devptmx = strjoina(temporary_mount, "/dev/ptmx");
3164e3cb
ZJS
508 if (symlink("pts/ptmx", devptmx) < 0) {
509 r = -errno;
510 goto fail;
511 }
e06b6479 512
63c372cb 513 devshm = strjoina(temporary_mount, "/dev/shm");
dc751688 514 (void) mkdir(devshm, 01777);
2b85f4e1
LP
515 r = mount("/dev/shm", devshm, NULL, MS_BIND, NULL);
516 if (r < 0) {
517 r = -errno;
518 goto fail;
519 }
520
63c372cb 521 devmqueue = strjoina(temporary_mount, "/dev/mqueue");
dc751688 522 (void) mkdir(devmqueue, 0755);
3164e3cb 523 (void) mount("/dev/mqueue", devmqueue, NULL, MS_BIND, NULL);
2b85f4e1 524
63c372cb 525 devhugepages = strjoina(temporary_mount, "/dev/hugepages");
dc751688 526 (void) mkdir(devhugepages, 0755);
3164e3cb 527 (void) mount("/dev/hugepages", devhugepages, NULL, MS_BIND, NULL);
2b85f4e1 528
63c372cb 529 devlog = strjoina(temporary_mount, "/dev/log");
3164e3cb 530 (void) symlink("/run/systemd/journal/dev-log", devlog);
82d25240 531
7f112f50 532 NULSTR_FOREACH(d, devnodes) {
2b85f4e1
LP
533 _cleanup_free_ char *dn = NULL;
534 struct stat st;
535
536 r = stat(d, &st);
7f112f50 537 if (r < 0) {
2b85f4e1
LP
538
539 if (errno == ENOENT)
540 continue;
541
542 r = -errno;
543 goto fail;
7f112f50
LP
544 }
545
2b85f4e1
LP
546 if (!S_ISBLK(st.st_mode) &&
547 !S_ISCHR(st.st_mode)) {
548 r = -EINVAL;
549 goto fail;
550 }
551
552 if (st.st_rdev == 0)
553 continue;
554
555 dn = strappend(temporary_mount, d);
556 if (!dn) {
557 r = -ENOMEM;
558 goto fail;
559 }
560
ecabcf8b 561 mac_selinux_create_file_prepare(d, st.st_mode);
2b85f4e1 562 r = mknod(dn, st.st_mode, st.st_rdev);
ecabcf8b 563 mac_selinux_create_file_clear();
dd078a1e 564
2b85f4e1
LP
565 if (r < 0) {
566 r = -errno;
567 goto fail;
568 }
7f112f50
LP
569 }
570
03cfe0d5 571 dev_setup(temporary_mount, UID_INVALID, GID_INVALID);
7f112f50 572
ee818b89
AC
573 /* Create the /dev directory if missing. It is more likely to be
574 * missing when the service is started with RootDirectory. This is
575 * consistent with mount units creating the mount points when missing.
576 */
34de407a 577 (void) mkdir_p_label(mount_entry_path(m), 0755);
ee818b89 578
9e5f8252 579 /* Unmount everything in old /dev */
34de407a
LP
580 umount_recursive(mount_entry_path(m), 0);
581 if (mount(dev, mount_entry_path(m), NULL, MS_MOVE, NULL) < 0) {
2b85f4e1
LP
582 r = -errno;
583 goto fail;
584 }
7f112f50 585
2b85f4e1
LP
586 rmdir(dev);
587 rmdir(temporary_mount);
7f112f50 588
2b85f4e1 589 return 0;
7f112f50 590
2b85f4e1
LP
591fail:
592 if (devpts)
593 umount(devpts);
7f112f50 594
2b85f4e1
LP
595 if (devshm)
596 umount(devshm);
7f112f50 597
2b85f4e1
LP
598 if (devhugepages)
599 umount(devhugepages);
7f112f50 600
2b85f4e1
LP
601 if (devmqueue)
602 umount(devmqueue);
7f112f50 603
d267c5aa
ZJS
604 umount(dev);
605 rmdir(dev);
2b85f4e1 606 rmdir(temporary_mount);
7f112f50 607
2b85f4e1 608 return r;
7f112f50
LP
609}
610
5d997827
LP
611static int mount_bind_dev(MountEntry *m) {
612 int r;
613
614 assert(m);
615
616 /* Implements the little brother of mount_private_dev(): simply bind mounts the host's /dev into the service's
617 * /dev. This is only used when RootDirectory= is set. */
618
619 r = path_is_mount_point(mount_entry_path(m), NULL, 0);
620 if (r < 0)
621 return log_debug_errno(r, "Unable to determine whether /dev is already mounted: %m");
622 if (r > 0) /* make this a NOP if /dev is already a mount point */
623 return 0;
624
625 if (mount("/dev", mount_entry_path(m), NULL, MS_BIND|MS_REC, NULL) < 0)
626 return log_debug_errno(errno, "Failed to bind mount %s: %m", mount_entry_path(m));
627
628 return 1;
629}
630
631static int mount_sysfs(MountEntry *m) {
632 int r;
633
634 assert(m);
635
636 r = path_is_mount_point(mount_entry_path(m), NULL, 0);
637 if (r < 0)
638 return log_debug_errno(r, "Unable to determine whether /sys is already mounted: %m");
639 if (r > 0) /* make this a NOP if /sys is already a mount point */
640 return 0;
641
642 /* Bind mount the host's version so that we get all child mounts of it, too. */
643 if (mount("/sys", mount_entry_path(m), NULL, MS_BIND|MS_REC, NULL) < 0)
644 return log_debug_errno(errno, "Failed to mount %s: %m", mount_entry_path(m));
645
646 return 1;
647}
648
649static int mount_procfs(MountEntry *m) {
650 int r;
651
652 assert(m);
653
654 r = path_is_mount_point(mount_entry_path(m), NULL, 0);
655 if (r < 0)
656 return log_debug_errno(r, "Unable to determine whether /proc is already mounted: %m");
657 if (r > 0) /* make this a NOP if /proc is already a mount point */
658 return 0;
659
660 /* Mount a new instance, so that we get the one that matches our user namespace, if we are running in one */
661 if (mount("proc", mount_entry_path(m), "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL) < 0)
662 return log_debug_errno(errno, "Failed to mount %s: %m", mount_entry_path(m));
663
664 return 1;
665}
666
d2d6c096
LP
667static int mount_entry_chase(
668 const char *root_directory,
669 MountEntry *m,
670 const char *path,
671 char **location) {
672
8fceda93
LP
673 char *chased;
674 int r;
675
676 assert(m);
677
678 /* Since mount() will always follow symlinks and we need to take the different root directory into account we
d2d6c096
LP
679 * chase the symlinks on our own first. This is called for the destination path, as well as the source path (if
680 * that applies). The result is stored in "location". */
8fceda93 681
d2d6c096 682 r = chase_symlinks(path, root_directory, 0, &chased);
8fceda93 683 if (r == -ENOENT && m->ignore) {
d2d6c096 684 log_debug_errno(r, "Path %s does not exist, ignoring.", path);
8fceda93
LP
685 return 0;
686 }
687 if (r < 0)
d2d6c096 688 return log_debug_errno(r, "Failed to follow symlinks on %s: %m", path);
8fceda93 689
d2d6c096 690 log_debug("Followed symlinks %s → %s.", path, chased);
8fceda93 691
d2d6c096
LP
692 free(*location);
693 *location = chased;
8fceda93
LP
694
695 return 1;
696}
697
ac0930c8 698static int apply_mount(
8fceda93 699 const char *root_directory,
34de407a 700 MountEntry *m,
ac0930c8 701 const char *tmp_dir,
c17ec25e 702 const char *var_tmp_dir) {
ac0930c8 703
15ae422b 704 const char *what;
d2d6c096 705 bool rbind = true;
15ae422b 706 int r;
15ae422b 707
c17ec25e 708 assert(m);
15ae422b 709
d2d6c096 710 r = mount_entry_chase(root_directory, m, mount_entry_path(m), &m->path_malloc);
8fceda93
LP
711 if (r <= 0)
712 return r;
713
34de407a 714 log_debug("Applying namespace mount on %s", mount_entry_path(m));
fe3c2583 715
c17ec25e 716 switch (m->mode) {
15ae422b 717
160cfdbe
LP
718 case INACCESSIBLE: {
719 struct stat target;
6d313367
LP
720
721 /* First, get rid of everything that is below if there
722 * is anything... Then, overmount it with an
c4b41707 723 * inaccessible path. */
34de407a 724 (void) umount_recursive(mount_entry_path(m), 0);
6d313367 725
34de407a
LP
726 if (lstat(mount_entry_path(m), &target) < 0)
727 return log_debug_errno(errno, "Failed to lstat() %s to determine what to mount over it: %m", mount_entry_path(m));
15ae422b 728
c4b41707 729 what = mode_to_inaccessible_node(target.st_mode);
5fd7cf6f
LP
730 if (!what) {
731 log_debug("File type not supported for inaccessible mounts. Note that symlinks are not allowed");
c4b41707
AP
732 return -ELOOP;
733 }
734 break;
160cfdbe 735 }
fe3c2583 736
15ae422b 737 case READONLY:
15ae422b 738 case READWRITE:
8fceda93 739 r = path_is_mount_point(mount_entry_path(m), root_directory, 0);
d944dc95 740 if (r < 0)
34de407a 741 return log_debug_errno(r, "Failed to determine whether %s is already a mount point: %m", mount_entry_path(m));
6b7c9f8b
LP
742 if (r > 0) /* Nothing to do here, it is already a mount. We just later toggle the MS_RDONLY bit for the mount point if needed. */
743 return 0;
6b7c9f8b 744 /* This isn't a mount point yet, let's make it one. */
34de407a 745 what = mount_entry_path(m);
6b7c9f8b 746 break;
15ae422b 747
d2d6c096
LP
748 case BIND_MOUNT:
749 rbind = false;
750 /* fallthrough */
751
752 case BIND_MOUNT_RECURSIVE:
753 /* Also chase the source mount */
5d997827 754
d2d6c096
LP
755 r = mount_entry_chase(root_directory, m, mount_entry_source(m), &m->source_malloc);
756 if (r <= 0)
757 return r;
758
759 what = mount_entry_source(m);
760 break;
761
ac0930c8
LP
762 case PRIVATE_TMP:
763 what = tmp_dir;
764 break;
765
766 case PRIVATE_VAR_TMP:
767 what = var_tmp_dir;
15ae422b 768 break;
e364ad06 769
d6797c92 770 case PRIVATE_DEV:
5d997827
LP
771 return mount_private_dev(m);
772
773 case BIND_DEV:
774 return mount_bind_dev(m);
775
776 case SYSFS:
777 return mount_sysfs(m);
778
779 case PROCFS:
780 return mount_procfs(m);
d6797c92 781
e364ad06
LP
782 default:
783 assert_not_reached("Unknown mode");
15ae422b
LP
784 }
785
ac0930c8 786 assert(what);
15ae422b 787
d2d6c096 788 if (mount(what, mount_entry_path(m), NULL, MS_BIND|(rbind ? MS_REC : 0), NULL) < 0)
34de407a 789 return log_debug_errno(errno, "Failed to mount %s to %s: %m", what, mount_entry_path(m));
6b7c9f8b 790
34de407a 791 log_debug("Successfully mounted %s to %s", what, mount_entry_path(m));
6b7c9f8b 792 return 0;
ac0930c8 793}
15ae422b 794
34de407a 795static int make_read_only(MountEntry *m, char **blacklist) {
6b7c9f8b 796 int r = 0;
15ae422b 797
c17ec25e 798 assert(m);
ac0930c8 799
34de407a
LP
800 if (mount_entry_read_only(m))
801 r = bind_remount_recursive(mount_entry_path(m), true, blacklist);
13e785f7 802 else if (m->mode == PRIVATE_DEV) { /* Superblock can be readonly but the submounts can't */
34de407a 803 if (mount(NULL, mount_entry_path(m), NULL, MS_REMOUNT|DEV_MOUNT_OPTIONS|MS_RDONLY, NULL) < 0)
6b7c9f8b 804 r = -errno;
737ba3c8 805 } else
6b7c9f8b
LP
806 return 0;
807
808 /* Not that we only turn on the MS_RDONLY flag here, we never turn it off. Something that was marked read-only
809 * already stays this way. This improves compatibility with container managers, where we won't attempt to undo
810 * read-only mounts already applied. */
ac0930c8 811
8fceda93
LP
812 if (r == -ENOENT && m->ignore)
813 r = 0;
5327c910 814
1d54cd5d 815 return r;
d944dc95
LP
816}
817
5d997827
LP
818static bool namespace_info_mount_apivfs(const NameSpaceInfo *ns_info) {
819 assert(ns_info);
820
821 /* ProtectControlGroups= and ProtectKernelTunables= imply MountAPIVFS=, since to protect the API VFS mounts,
822 * they need to be around in the first place... */
823
824 return ns_info->mount_apivfs ||
825 ns_info->protect_control_groups ||
826 ns_info->protect_kernel_tunables;
827}
828
2652c6c1 829static unsigned namespace_calculate_mounts(
c575770b 830 const NameSpaceInfo *ns_info,
2652c6c1
DH
831 char** read_write_paths,
832 char** read_only_paths,
833 char** inaccessible_paths,
d2d6c096
LP
834 const BindMount *bind_mounts,
835 unsigned n_bind_mounts,
2652c6c1
DH
836 const char* tmp_dir,
837 const char* var_tmp_dir,
2652c6c1
DH
838 ProtectHome protect_home,
839 ProtectSystem protect_system) {
840
b6c432ca 841 unsigned protect_home_cnt;
f471b2af
DH
842 unsigned protect_system_cnt =
843 (protect_system == PROTECT_SYSTEM_STRICT ?
844 ELEMENTSOF(protect_system_strict_table) :
845 ((protect_system == PROTECT_SYSTEM_FULL) ?
846 ELEMENTSOF(protect_system_full_table) :
847 ((protect_system == PROTECT_SYSTEM_YES) ?
848 ELEMENTSOF(protect_system_yes_table) : 0)));
849
b6c432ca
DH
850 protect_home_cnt =
851 (protect_home == PROTECT_HOME_YES ?
852 ELEMENTSOF(protect_home_yes_table) :
853 ((protect_home == PROTECT_HOME_READ_ONLY) ?
854 ELEMENTSOF(protect_home_read_only_table) : 0));
855
2652c6c1
DH
856 return !!tmp_dir + !!var_tmp_dir +
857 strv_length(read_write_paths) +
858 strv_length(read_only_paths) +
859 strv_length(inaccessible_paths) +
d2d6c096 860 n_bind_mounts +
c575770b
DH
861 ns_info->private_dev +
862 (ns_info->protect_kernel_tunables ? ELEMENTSOF(protect_kernel_tunables_table) : 0) +
863 (ns_info->protect_control_groups ? 1 : 0) +
864 (ns_info->protect_kernel_modules ? ELEMENTSOF(protect_kernel_modules_table) : 0) +
5d997827
LP
865 protect_home_cnt + protect_system_cnt +
866 (namespace_info_mount_apivfs(ns_info) ? ELEMENTSOF(apivfs_table) : 0);
2652c6c1
DH
867}
868
613b411c 869int setup_namespace(
ee818b89 870 const char* root_directory,
915e6d16 871 const char* root_image,
c575770b 872 const NameSpaceInfo *ns_info,
2a624c36
AP
873 char** read_write_paths,
874 char** read_only_paths,
875 char** inaccessible_paths,
d2d6c096
LP
876 const BindMount *bind_mounts,
877 unsigned n_bind_mounts,
a004cb4c
LP
878 const char* tmp_dir,
879 const char* var_tmp_dir,
1b8689f9
LP
880 ProtectHome protect_home,
881 ProtectSystem protect_system,
915e6d16
LP
882 unsigned long mount_flags,
883 DissectImageFlags dissect_image_flags) {
15ae422b 884
915e6d16 885 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
78ebe980 886 _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL;
915e6d16 887 _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL;
78ebe980 888 _cleanup_free_ void *root_hash = NULL;
34de407a 889 MountEntry *m, *mounts = NULL;
78ebe980 890 size_t root_hash_size = 0;
d944dc95 891 bool make_slave = false;
f0a4feb0 892 unsigned n_mounts;
c17ec25e 893 int r = 0;
15ae422b 894
915e6d16
LP
895 assert(ns_info);
896
613b411c 897 if (mount_flags == 0)
c17ec25e 898 mount_flags = MS_SHARED;
ac0930c8 899
915e6d16
LP
900 if (root_image) {
901 dissect_image_flags |= DISSECT_IMAGE_REQUIRE_ROOT;
902
903 if (protect_system == PROTECT_SYSTEM_STRICT && strv_isempty(read_write_paths))
904 dissect_image_flags |= DISSECT_IMAGE_READ_ONLY;
905
906 r = loop_device_make_by_path(root_image,
907 dissect_image_flags & DISSECT_IMAGE_READ_ONLY ? O_RDONLY : O_RDWR,
908 &loop_device);
909 if (r < 0)
910 return r;
911
78ebe980
LP
912 r = root_hash_load(root_image, &root_hash, &root_hash_size);
913 if (r < 0)
914 return r;
915
916 r = dissect_image(loop_device->fd, root_hash, root_hash_size, dissect_image_flags, &dissected_image);
917 if (r < 0)
918 return r;
919
920 r = dissected_image_decrypt(dissected_image, NULL, root_hash, root_hash_size, dissect_image_flags, &decrypted_image);
915e6d16
LP
921 if (r < 0)
922 return r;
923
924 if (!root_directory) {
925 /* Create a mount point for the image, if it's still missing. We use the same mount point for
926 * all images, which is safe, since they all live in their own namespaces after all, and hence
927 * won't see each other. */
928 root_directory = "/run/systemd/unit-root";
929 (void) mkdir(root_directory, 0700);
930 }
931 }
932
cfbeb4ef
LP
933 n_mounts = namespace_calculate_mounts(
934 ns_info,
935 read_write_paths,
936 read_only_paths,
937 inaccessible_paths,
d2d6c096 938 bind_mounts, n_bind_mounts,
cfbeb4ef
LP
939 tmp_dir, var_tmp_dir,
940 protect_home, protect_system);
613b411c 941
2652c6c1 942 /* Set mount slave mode */
f0a4feb0 943 if (root_directory || n_mounts > 0)
d944dc95
LP
944 make_slave = true;
945
f0a4feb0 946 if (n_mounts > 0) {
34de407a 947 m = mounts = (MountEntry *) alloca0(n_mounts * sizeof(MountEntry));
5327c910 948 r = append_access_mounts(&m, read_write_paths, READWRITE);
613b411c 949 if (r < 0)
f0a4feb0 950 goto finish;
613b411c 951
5327c910 952 r = append_access_mounts(&m, read_only_paths, READONLY);
613b411c 953 if (r < 0)
f0a4feb0 954 goto finish;
613b411c 955
5327c910 956 r = append_access_mounts(&m, inaccessible_paths, INACCESSIBLE);
613b411c 957 if (r < 0)
f0a4feb0 958 goto finish;
7ff7394d 959
d2d6c096
LP
960 r = append_bind_mounts(&m, bind_mounts, n_bind_mounts);
961 if (r < 0)
962 goto finish;
963
613b411c 964 if (tmp_dir) {
34de407a 965 *(m++) = (MountEntry) {
5327c910
LP
966 .path_const = "/tmp",
967 .mode = PRIVATE_TMP,
968 };
613b411c 969 }
7ff7394d 970
613b411c 971 if (var_tmp_dir) {
34de407a 972 *(m++) = (MountEntry) {
5327c910
LP
973 .path_const = "/var/tmp",
974 .mode = PRIVATE_VAR_TMP,
975 };
7ff7394d 976 }
ac0930c8 977
c575770b 978 if (ns_info->private_dev) {
34de407a 979 *(m++) = (MountEntry) {
5327c910
LP
980 .path_const = "/dev",
981 .mode = PRIVATE_DEV,
982 };
7f112f50
LP
983 }
984
c575770b 985 if (ns_info->protect_kernel_tunables) {
5327c910 986 r = append_static_mounts(&m, protect_kernel_tunables_table, ELEMENTSOF(protect_kernel_tunables_table), ns_info->ignore_protect_paths);
c575770b 987 if (r < 0)
f0a4feb0 988 goto finish;
c575770b
DH
989 }
990
991 if (ns_info->protect_kernel_modules) {
5327c910 992 r = append_static_mounts(&m, protect_kernel_modules_table, ELEMENTSOF(protect_kernel_modules_table), ns_info->ignore_protect_paths);
c575770b 993 if (r < 0)
f0a4feb0 994 goto finish;
c575770b 995 }
59eeb84b 996
c575770b 997 if (ns_info->protect_control_groups) {
34de407a 998 *(m++) = (MountEntry) {
5327c910
LP
999 .path_const = "/sys/fs/cgroup",
1000 .mode = READONLY,
1001 };
59eeb84b
LP
1002 }
1003
5327c910 1004 r = append_protect_home(&m, protect_home, ns_info->ignore_protect_paths);
b6c432ca 1005 if (r < 0)
f0a4feb0 1006 goto finish;
417116f2 1007
5327c910 1008 r = append_protect_system(&m, protect_system, false);
f471b2af 1009 if (r < 0)
f0a4feb0 1010 goto finish;
417116f2 1011
5d997827
LP
1012 if (namespace_info_mount_apivfs(ns_info)) {
1013 r = append_static_mounts(&m, apivfs_table, ELEMENTSOF(apivfs_table), ns_info->ignore_protect_paths);
1014 if (r < 0)
1015 goto finish;
1016 }
1017
f0a4feb0 1018 assert(mounts + n_mounts == m);
ac0930c8 1019
5327c910
LP
1020 /* Prepend the root directory where that's necessary */
1021 r = prefix_where_needed(mounts, n_mounts, root_directory);
1022 if (r < 0)
1023 goto finish;
1024
34de407a 1025 qsort(mounts, n_mounts, sizeof(MountEntry), mount_path_compare);
fe3c2583 1026
f0a4feb0
DH
1027 drop_duplicates(mounts, &n_mounts);
1028 drop_outside_root(root_directory, mounts, &n_mounts);
1029 drop_inaccessible(mounts, &n_mounts);
1030 drop_nop(mounts, &n_mounts);
15ae422b
LP
1031 }
1032
d944dc95
LP
1033 if (unshare(CLONE_NEWNS) < 0) {
1034 r = -errno;
1035 goto finish;
1036 }
1e4e94c8 1037
d944dc95 1038 if (make_slave) {
c2c13f2d
LP
1039 /* Remount / as SLAVE so that nothing now mounted in the namespace
1040 shows up in the parent */
d944dc95
LP
1041 if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) {
1042 r = -errno;
1043 goto finish;
1044 }
ee818b89
AC
1045 }
1046
915e6d16
LP
1047 if (root_image) {
1048 r = dissected_image_mount(dissected_image, root_directory, dissect_image_flags);
1049 if (r < 0)
1050 goto finish;
1051
78ebe980
LP
1052 r = decrypted_image_relinquish(decrypted_image);
1053 if (r < 0)
1054 goto finish;
1055
915e6d16
LP
1056 loop_device_relinquish(loop_device);
1057
1058 } else if (root_directory) {
1059
8f1ad200 1060 /* Turn directory into bind mount, if it isn't one yet */
e1873695 1061 r = path_is_mount_point(root_directory, NULL, AT_SYMLINK_FOLLOW);
8f1ad200 1062 if (r < 0)
d944dc95 1063 goto finish;
8f1ad200
LP
1064 if (r == 0) {
1065 if (mount(root_directory, root_directory, NULL, MS_BIND|MS_REC, NULL) < 0) {
1066 r = -errno;
1067 goto finish;
1068 }
d944dc95 1069 }
ee818b89 1070 }
c2c13f2d 1071
f0a4feb0 1072 if (n_mounts > 0) {
6b7c9f8b
LP
1073 char **blacklist;
1074 unsigned j;
1075
1076 /* First round, add in all special mounts we need */
f0a4feb0 1077 for (m = mounts; m < mounts + n_mounts; ++m) {
8fceda93 1078 r = apply_mount(root_directory, m, tmp_dir, var_tmp_dir);
c2c13f2d 1079 if (r < 0)
d944dc95 1080 goto finish;
c2c13f2d 1081 }
15ae422b 1082
6b7c9f8b 1083 /* Create a blacklist we can pass to bind_mount_recursive() */
f0a4feb0
DH
1084 blacklist = newa(char*, n_mounts+1);
1085 for (j = 0; j < n_mounts; j++)
34de407a 1086 blacklist[j] = (char*) mount_entry_path(mounts+j);
6b7c9f8b
LP
1087 blacklist[j] = NULL;
1088
1089 /* Second round, flip the ro bits if necessary. */
f0a4feb0 1090 for (m = mounts; m < mounts + n_mounts; ++m) {
6b7c9f8b 1091 r = make_read_only(m, blacklist);
c2c13f2d 1092 if (r < 0)
d944dc95 1093 goto finish;
c2c13f2d 1094 }
15ae422b
LP
1095 }
1096
ee818b89
AC
1097 if (root_directory) {
1098 /* MS_MOVE does not work on MS_SHARED so the remount MS_SHARED will be done later */
1099 r = mount_move_root(root_directory);
d944dc95
LP
1100 if (r < 0)
1101 goto finish;
ee818b89
AC
1102 }
1103
c2c13f2d
LP
1104 /* Remount / as the desired mode. Not that this will not
1105 * reestablish propagation from our side to the host, since
1106 * what's disconnected is disconnected. */
d944dc95
LP
1107 if (mount(NULL, "/", NULL, mount_flags | MS_REC, NULL) < 0) {
1108 r = -errno;
1109 goto finish;
1110 }
15ae422b 1111
d944dc95 1112 r = 0;
15ae422b 1113
d944dc95 1114finish:
f0a4feb0 1115 for (m = mounts; m < mounts + n_mounts; m++)
1eb7e08e 1116 mount_entry_done(m);
613b411c
LP
1117
1118 return r;
1119}
1120
d2d6c096
LP
1121void bind_mount_free_many(BindMount *b, unsigned n) {
1122 unsigned i;
1123
1124 assert(b || n == 0);
1125
1126 for (i = 0; i < n; i++) {
1127 free(b[i].source);
1128 free(b[i].destination);
1129 }
1130
1131 free(b);
1132}
1133
1134int bind_mount_add(BindMount **b, unsigned *n, const BindMount *item) {
1135 _cleanup_free_ char *s = NULL, *d = NULL;
1136 BindMount *c;
1137
1138 assert(b);
1139 assert(n);
1140 assert(item);
1141
1142 s = strdup(item->source);
1143 if (!s)
1144 return -ENOMEM;
1145
1146 d = strdup(item->destination);
1147 if (!d)
1148 return -ENOMEM;
1149
1150 c = realloc_multiply(*b, sizeof(BindMount), *n + 1);
1151 if (!c)
1152 return -ENOMEM;
1153
1154 *b = c;
1155
1156 c[(*n) ++] = (BindMount) {
1157 .source = s,
1158 .destination = d,
1159 .read_only = item->read_only,
1160 .recursive = item->recursive,
1161 .ignore_enoent = item->ignore_enoent,
1162 };
1163
1164 s = d = NULL;
1165 return 0;
1166}
1167
613b411c
LP
1168static int setup_one_tmp_dir(const char *id, const char *prefix, char **path) {
1169 _cleanup_free_ char *x = NULL;
6b46ea73
LP
1170 char bid[SD_ID128_STRING_MAX];
1171 sd_id128_t boot_id;
1172 int r;
613b411c
LP
1173
1174 assert(id);
1175 assert(prefix);
1176 assert(path);
1177
6b46ea73
LP
1178 /* We include the boot id in the directory so that after a
1179 * reboot we can easily identify obsolete directories. */
1180
1181 r = sd_id128_get_boot(&boot_id);
1182 if (r < 0)
1183 return r;
1184
605405c6 1185 x = strjoin(prefix, "/systemd-private-", sd_id128_to_string(boot_id, bid), "-", id, "-XXXXXX");
613b411c
LP
1186 if (!x)
1187 return -ENOMEM;
1188
1189 RUN_WITH_UMASK(0077)
1190 if (!mkdtemp(x))
1191 return -errno;
1192
1193 RUN_WITH_UMASK(0000) {
1194 char *y;
1195
63c372cb 1196 y = strjoina(x, "/tmp");
613b411c
LP
1197
1198 if (mkdir(y, 0777 | S_ISVTX) < 0)
1199 return -errno;
c17ec25e 1200 }
15ae422b 1201
613b411c
LP
1202 *path = x;
1203 x = NULL;
1204
1205 return 0;
1206}
1207
1208int setup_tmp_dirs(const char *id, char **tmp_dir, char **var_tmp_dir) {
1209 char *a, *b;
1210 int r;
1211
1212 assert(id);
1213 assert(tmp_dir);
1214 assert(var_tmp_dir);
1215
1216 r = setup_one_tmp_dir(id, "/tmp", &a);
1217 if (r < 0)
1218 return r;
1219
1220 r = setup_one_tmp_dir(id, "/var/tmp", &b);
1221 if (r < 0) {
1222 char *t;
1223
63c372cb 1224 t = strjoina(a, "/tmp");
613b411c
LP
1225 rmdir(t);
1226 rmdir(a);
1227
1228 free(a);
1229 return r;
1230 }
1231
1232 *tmp_dir = a;
1233 *var_tmp_dir = b;
1234
1235 return 0;
1236}
1237
1238int setup_netns(int netns_storage_socket[2]) {
1239 _cleanup_close_ int netns = -1;
3ee897d6 1240 int r, q;
613b411c
LP
1241
1242 assert(netns_storage_socket);
1243 assert(netns_storage_socket[0] >= 0);
1244 assert(netns_storage_socket[1] >= 0);
1245
1246 /* We use the passed socketpair as a storage buffer for our
76cd584b
LP
1247 * namespace reference fd. Whatever process runs this first
1248 * shall create a new namespace, all others should just join
1249 * it. To serialize that we use a file lock on the socket
1250 * pair.
613b411c
LP
1251 *
1252 * It's a bit crazy, but hey, works great! */
1253
1254 if (lockf(netns_storage_socket[0], F_LOCK, 0) < 0)
1255 return -errno;
1256
3ee897d6
LP
1257 netns = receive_one_fd(netns_storage_socket[0], MSG_DONTWAIT);
1258 if (netns == -EAGAIN) {
613b411c
LP
1259 /* Nothing stored yet, so let's create a new namespace */
1260
1261 if (unshare(CLONE_NEWNET) < 0) {
1262 r = -errno;
1263 goto fail;
1264 }
1265
1266 loopback_setup();
1267
1268 netns = open("/proc/self/ns/net", O_RDONLY|O_CLOEXEC|O_NOCTTY);
1269 if (netns < 0) {
1270 r = -errno;
1271 goto fail;
1272 }
1273
1274 r = 1;
613b411c 1275
3ee897d6
LP
1276 } else if (netns < 0) {
1277 r = netns;
1278 goto fail;
613b411c 1279
3ee897d6
LP
1280 } else {
1281 /* Yay, found something, so let's join the namespace */
613b411c
LP
1282 if (setns(netns, CLONE_NEWNET) < 0) {
1283 r = -errno;
1284 goto fail;
1285 }
1286
1287 r = 0;
1288 }
1289
3ee897d6
LP
1290 q = send_one_fd(netns_storage_socket[1], netns, MSG_DONTWAIT);
1291 if (q < 0) {
1292 r = q;
613b411c
LP
1293 goto fail;
1294 }
1295
1296fail:
fe048ce5 1297 (void) lockf(netns_storage_socket[0], F_ULOCK, 0);
15ae422b
LP
1298 return r;
1299}
417116f2 1300
1b8689f9
LP
1301static const char *const protect_home_table[_PROTECT_HOME_MAX] = {
1302 [PROTECT_HOME_NO] = "no",
1303 [PROTECT_HOME_YES] = "yes",
1304 [PROTECT_HOME_READ_ONLY] = "read-only",
417116f2
LP
1305};
1306
1b8689f9
LP
1307DEFINE_STRING_TABLE_LOOKUP(protect_home, ProtectHome);
1308
1309static const char *const protect_system_table[_PROTECT_SYSTEM_MAX] = {
1310 [PROTECT_SYSTEM_NO] = "no",
1311 [PROTECT_SYSTEM_YES] = "yes",
1312 [PROTECT_SYSTEM_FULL] = "full",
3f815163 1313 [PROTECT_SYSTEM_STRICT] = "strict",
1b8689f9
LP
1314};
1315
1316DEFINE_STRING_TABLE_LOOKUP(protect_system, ProtectSystem);