]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/namespace.c
nspawn: drop spurious newline
[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"
07630cea
LP
32#include "loopback-setup.h"
33#include "missing.h"
34#include "mkdir.h"
4349cd7c 35#include "mount-util.h"
3ffd4af2 36#include "namespace.h"
07630cea 37#include "path-util.h"
d7b8eec7 38#include "selinux-util.h"
2583fbea 39#include "socket-util.h"
8b43440b 40#include "string-table.h"
07630cea
LP
41#include "string-util.h"
42#include "strv.h"
affb60b1 43#include "umask-util.h"
ee104e11 44#include "user-util.h"
07630cea 45#include "util.h"
15ae422b 46
c17ec25e 47typedef enum MountMode {
15ae422b
LP
48 /* This is ordered by priority! */
49 INACCESSIBLE,
50 READONLY,
ac0930c8
LP
51 PRIVATE_TMP,
52 PRIVATE_VAR_TMP,
7f112f50 53 PRIVATE_DEV,
15ae422b 54 READWRITE
c17ec25e 55} MountMode;
15ae422b 56
c17ec25e 57typedef struct BindMount {
15ae422b 58 const char *path;
c17ec25e 59 MountMode mode;
ac0930c8 60 bool done;
ea92ae33 61 bool ignore;
c17ec25e 62} BindMount;
15ae422b 63
c17ec25e 64static int append_mounts(BindMount **p, char **strv, MountMode mode) {
15ae422b
LP
65 char **i;
66
613b411c
LP
67 assert(p);
68
15ae422b
LP
69 STRV_FOREACH(i, strv) {
70
ea92ae33 71 (*p)->ignore = false;
002b2268 72 (*p)->done = false;
ea92ae33 73
94828d2d 74 if ((mode == INACCESSIBLE || mode == READONLY || mode == READWRITE) && (*i)[0] == '-') {
ea92ae33
MW
75 (*p)->ignore = true;
76 (*i)++;
77 }
78
15ae422b
LP
79 if (!path_is_absolute(*i))
80 return -EINVAL;
81
82 (*p)->path = *i;
83 (*p)->mode = mode;
84 (*p)++;
85 }
86
87 return 0;
88}
89
c17ec25e
MS
90static int mount_path_compare(const void *a, const void *b) {
91 const BindMount *p = a, *q = b;
a0827e2b 92 int d;
15ae422b 93
a0827e2b 94 d = path_compare(p->path, q->path);
15ae422b 95
5a8af538 96 if (d == 0) {
15ae422b
LP
97 /* If the paths are equal, check the mode */
98 if (p->mode < q->mode)
99 return -1;
100
101 if (p->mode > q->mode)
102 return 1;
103
104 return 0;
105 }
106
107 /* If the paths are not equal, then order prefixes first */
a0827e2b 108 return d;
15ae422b
LP
109}
110
c17ec25e
MS
111static void drop_duplicates(BindMount *m, unsigned *n) {
112 BindMount *f, *t, *previous;
15ae422b 113
c17ec25e 114 assert(m);
15ae422b 115 assert(n);
15ae422b 116
c17ec25e 117 for (f = m, t = m, previous = NULL; f < m+*n; f++) {
15ae422b 118
ac0930c8 119 /* The first one wins */
15ae422b
LP
120 if (previous && path_equal(f->path, previous->path))
121 continue;
122
e2d7c1a0 123 *t = *f;
15ae422b 124
15ae422b
LP
125 previous = t;
126
127 t++;
128 }
129
c17ec25e 130 *n = t - m;
15ae422b
LP
131}
132
7f112f50
LP
133static int mount_dev(BindMount *m) {
134 static const char devnodes[] =
135 "/dev/null\0"
136 "/dev/zero\0"
137 "/dev/full\0"
138 "/dev/random\0"
139 "/dev/urandom\0"
140 "/dev/tty\0";
141
2b85f4e1 142 char temporary_mount[] = "/tmp/namespace-dev-XXXXXX";
63cc4c31 143 const char *d, *dev = NULL, *devpts = NULL, *devshm = NULL, *devhugepages = NULL, *devmqueue = NULL, *devlog = NULL, *devptmx = NULL;
7f112f50
LP
144 _cleanup_umask_ mode_t u;
145 int r;
146
147 assert(m);
148
149 u = umask(0000);
150
2b85f4e1
LP
151 if (!mkdtemp(temporary_mount))
152 return -errno;
153
63c372cb 154 dev = strjoina(temporary_mount, "/dev");
dc751688 155 (void) mkdir(dev, 0755);
2b85f4e1
LP
156 if (mount("tmpfs", dev, "tmpfs", MS_NOSUID|MS_STRICTATIME, "mode=755") < 0) {
157 r = -errno;
158 goto fail;
159 }
160
63c372cb 161 devpts = strjoina(temporary_mount, "/dev/pts");
dc751688 162 (void) mkdir(devpts, 0755);
2b85f4e1
LP
163 if (mount("/dev/pts", devpts, NULL, MS_BIND, NULL) < 0) {
164 r = -errno;
165 goto fail;
166 }
167
63c372cb 168 devptmx = strjoina(temporary_mount, "/dev/ptmx");
3164e3cb
ZJS
169 if (symlink("pts/ptmx", devptmx) < 0) {
170 r = -errno;
171 goto fail;
172 }
e06b6479 173
63c372cb 174 devshm = strjoina(temporary_mount, "/dev/shm");
dc751688 175 (void) mkdir(devshm, 01777);
2b85f4e1
LP
176 r = mount("/dev/shm", devshm, NULL, MS_BIND, NULL);
177 if (r < 0) {
178 r = -errno;
179 goto fail;
180 }
181
63c372cb 182 devmqueue = strjoina(temporary_mount, "/dev/mqueue");
dc751688 183 (void) mkdir(devmqueue, 0755);
3164e3cb 184 (void) mount("/dev/mqueue", devmqueue, NULL, MS_BIND, NULL);
2b85f4e1 185
63c372cb 186 devhugepages = strjoina(temporary_mount, "/dev/hugepages");
dc751688 187 (void) mkdir(devhugepages, 0755);
3164e3cb 188 (void) mount("/dev/hugepages", devhugepages, NULL, MS_BIND, NULL);
2b85f4e1 189
63c372cb 190 devlog = strjoina(temporary_mount, "/dev/log");
3164e3cb 191 (void) symlink("/run/systemd/journal/dev-log", devlog);
82d25240 192
7f112f50 193 NULSTR_FOREACH(d, devnodes) {
2b85f4e1
LP
194 _cleanup_free_ char *dn = NULL;
195 struct stat st;
196
197 r = stat(d, &st);
7f112f50 198 if (r < 0) {
2b85f4e1
LP
199
200 if (errno == ENOENT)
201 continue;
202
203 r = -errno;
204 goto fail;
7f112f50
LP
205 }
206
2b85f4e1
LP
207 if (!S_ISBLK(st.st_mode) &&
208 !S_ISCHR(st.st_mode)) {
209 r = -EINVAL;
210 goto fail;
211 }
212
213 if (st.st_rdev == 0)
214 continue;
215
216 dn = strappend(temporary_mount, d);
217 if (!dn) {
218 r = -ENOMEM;
219 goto fail;
220 }
221
ecabcf8b 222 mac_selinux_create_file_prepare(d, st.st_mode);
2b85f4e1 223 r = mknod(dn, st.st_mode, st.st_rdev);
ecabcf8b 224 mac_selinux_create_file_clear();
dd078a1e 225
2b85f4e1
LP
226 if (r < 0) {
227 r = -errno;
228 goto fail;
229 }
7f112f50
LP
230 }
231
03cfe0d5 232 dev_setup(temporary_mount, UID_INVALID, GID_INVALID);
7f112f50 233
ee818b89
AC
234 /* Create the /dev directory if missing. It is more likely to be
235 * missing when the service is started with RootDirectory. This is
236 * consistent with mount units creating the mount points when missing.
237 */
238 (void) mkdir_p_label(m->path, 0755);
239
240 if (mount(dev, m->path, NULL, MS_MOVE, NULL) < 0) {
2b85f4e1
LP
241 r = -errno;
242 goto fail;
243 }
7f112f50 244
2b85f4e1
LP
245 rmdir(dev);
246 rmdir(temporary_mount);
7f112f50 247
2b85f4e1 248 return 0;
7f112f50 249
2b85f4e1
LP
250fail:
251 if (devpts)
252 umount(devpts);
7f112f50 253
2b85f4e1
LP
254 if (devshm)
255 umount(devshm);
7f112f50 256
2b85f4e1
LP
257 if (devhugepages)
258 umount(devhugepages);
7f112f50 259
2b85f4e1
LP
260 if (devmqueue)
261 umount(devmqueue);
7f112f50 262
d267c5aa
ZJS
263 umount(dev);
264 rmdir(dev);
2b85f4e1 265 rmdir(temporary_mount);
7f112f50 266
2b85f4e1 267 return r;
7f112f50
LP
268}
269
ac0930c8 270static int apply_mount(
c17ec25e 271 BindMount *m,
ac0930c8 272 const char *tmp_dir,
c17ec25e 273 const char *var_tmp_dir) {
ac0930c8 274
15ae422b 275 const char *what;
15ae422b 276 int r;
15ae422b 277
c17ec25e 278 assert(m);
15ae422b 279
c17ec25e 280 switch (m->mode) {
15ae422b
LP
281
282 case INACCESSIBLE:
6d313367
LP
283
284 /* First, get rid of everything that is below if there
285 * is anything... Then, overmount it with an
286 * inaccessible directory. */
287 umount_recursive(m->path, 0);
288
c17ec25e 289 what = "/run/systemd/inaccessible";
15ae422b
LP
290 break;
291
292 case READONLY:
15ae422b 293 case READWRITE:
d6797c92
LP
294 /* Nothing to mount here, we just later toggle the
295 * MS_RDONLY bit for the mount point */
296 return 0;
15ae422b 297
ac0930c8
LP
298 case PRIVATE_TMP:
299 what = tmp_dir;
300 break;
301
302 case PRIVATE_VAR_TMP:
303 what = var_tmp_dir;
15ae422b 304 break;
e364ad06 305
d6797c92
LP
306 case PRIVATE_DEV:
307 return mount_dev(m);
308
e364ad06
LP
309 default:
310 assert_not_reached("Unknown mode");
15ae422b
LP
311 }
312
ac0930c8 313 assert(what);
15ae422b 314
c17ec25e 315 r = mount(what, m->path, NULL, MS_BIND|MS_REC, NULL);
ac0930c8 316 if (r >= 0)
c17ec25e 317 log_debug("Successfully mounted %s to %s", what, m->path);
ea92ae33 318 else if (m->ignore && errno == ENOENT)
d6797c92 319 return 0;
15ae422b 320
ac0930c8
LP
321 return r;
322}
15ae422b 323
c17ec25e 324static int make_read_only(BindMount *m) {
ac0930c8 325 int r;
15ae422b 326
c17ec25e 327 assert(m);
ac0930c8 328
d6797c92
LP
329 if (IN_SET(m->mode, INACCESSIBLE, READONLY))
330 r = bind_remount_recursive(m->path, true);
664064d6 331 else if (IN_SET(m->mode, READWRITE, PRIVATE_TMP, PRIVATE_VAR_TMP, PRIVATE_DEV))
d6797c92
LP
332 r = bind_remount_recursive(m->path, false);
333 else
334 r = 0;
ac0930c8 335
d6797c92
LP
336 if (m->ignore && r == -ENOENT)
337 return 0;
ac0930c8 338
d6797c92 339 return r;
15ae422b
LP
340}
341
613b411c 342int setup_namespace(
ee818b89 343 const char* root_directory,
613b411c
LP
344 char** read_write_dirs,
345 char** read_only_dirs,
346 char** inaccessible_dirs,
a004cb4c
LP
347 const char* tmp_dir,
348 const char* var_tmp_dir,
7f112f50 349 bool private_dev,
1b8689f9
LP
350 ProtectHome protect_home,
351 ProtectSystem protect_system,
e6547662 352 unsigned long mount_flags) {
15ae422b 353
7ff7394d 354 BindMount *m, *mounts = NULL;
613b411c 355 unsigned n;
c17ec25e 356 int r = 0;
15ae422b 357
613b411c 358 if (mount_flags == 0)
c17ec25e 359 mount_flags = MS_SHARED;
ac0930c8 360
d5a3f0ea
ZJS
361 if (unshare(CLONE_NEWNS) < 0)
362 return -errno;
15ae422b 363
9ca6ff50 364 n = !!tmp_dir + !!var_tmp_dir +
613b411c
LP
365 strv_length(read_write_dirs) +
366 strv_length(read_only_dirs) +
7f112f50 367 strv_length(inaccessible_dirs) +
417116f2 368 private_dev +
c8835999 369 (protect_home != PROTECT_HOME_NO ? 3 : 0) +
051be1f7 370 (protect_system != PROTECT_SYSTEM_NO ? 2 : 0) +
1b8689f9 371 (protect_system == PROTECT_SYSTEM_FULL ? 1 : 0);
613b411c
LP
372
373 if (n > 0) {
002b2268 374 m = mounts = (BindMount *) alloca0(n * sizeof(BindMount));
613b411c
LP
375 r = append_mounts(&m, read_write_dirs, READWRITE);
376 if (r < 0)
377 return r;
378
379 r = append_mounts(&m, read_only_dirs, READONLY);
380 if (r < 0)
381 return r;
382
383 r = append_mounts(&m, inaccessible_dirs, INACCESSIBLE);
384 if (r < 0)
7ff7394d
ZJS
385 return r;
386
613b411c 387 if (tmp_dir) {
ee818b89 388 m->path = prefix_roota(root_directory, "/tmp");
7ff7394d
ZJS
389 m->mode = PRIVATE_TMP;
390 m++;
613b411c 391 }
7ff7394d 392
613b411c 393 if (var_tmp_dir) {
ee818b89 394 m->path = prefix_roota(root_directory, "/var/tmp");
7ff7394d
ZJS
395 m->mode = PRIVATE_VAR_TMP;
396 m++;
397 }
ac0930c8 398
7f112f50 399 if (private_dev) {
ee818b89 400 m->path = prefix_roota(root_directory, "/dev");
7f112f50
LP
401 m->mode = PRIVATE_DEV;
402 m++;
403 }
404
1b8689f9 405 if (protect_home != PROTECT_HOME_NO) {
ee818b89
AC
406 const char *home_dir, *run_user_dir, *root_dir;
407
408 home_dir = prefix_roota(root_directory, "/home");
409 home_dir = strjoina("-", home_dir);
410 run_user_dir = prefix_roota(root_directory, "/run/user");
411 run_user_dir = strjoina("-", run_user_dir);
412 root_dir = prefix_roota(root_directory, "/root");
413 root_dir = strjoina("-", root_dir);
414
415 r = append_mounts(&m, STRV_MAKE(home_dir, run_user_dir, root_dir),
416 protect_home == PROTECT_HOME_READ_ONLY ? READONLY : INACCESSIBLE);
417116f2
LP
417 if (r < 0)
418 return r;
419 }
420
1b8689f9 421 if (protect_system != PROTECT_SYSTEM_NO) {
ee818b89
AC
422 const char *usr_dir, *boot_dir, *etc_dir;
423
d38e01dc 424 usr_dir = prefix_roota(root_directory, "/usr");
ee818b89
AC
425 boot_dir = prefix_roota(root_directory, "/boot");
426 boot_dir = strjoina("-", boot_dir);
427 etc_dir = prefix_roota(root_directory, "/etc");
428
429 r = append_mounts(&m, protect_system == PROTECT_SYSTEM_FULL
430 ? STRV_MAKE(usr_dir, boot_dir, etc_dir)
431 : STRV_MAKE(usr_dir, boot_dir), READONLY);
417116f2
LP
432 if (r < 0)
433 return r;
434 }
435
7ff7394d 436 assert(mounts + n == m);
ac0930c8 437
7ff7394d
ZJS
438 qsort(mounts, n, sizeof(BindMount), mount_path_compare);
439 drop_duplicates(mounts, &n);
15ae422b
LP
440 }
441
ee818b89 442 if (n > 0 || root_directory) {
c2c13f2d
LP
443 /* Remount / as SLAVE so that nothing now mounted in the namespace
444 shows up in the parent */
445 if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0)
446 return -errno;
ee818b89
AC
447 }
448
449 if (root_directory) {
450 /* Turn directory into bind mount */
451 if (mount(root_directory, root_directory, NULL, MS_BIND|MS_REC, NULL) < 0)
452 return -errno;
453 }
c2c13f2d 454
ee818b89 455 if (n > 0) {
c2c13f2d
LP
456 for (m = mounts; m < mounts + n; ++m) {
457 r = apply_mount(m, tmp_dir, var_tmp_dir);
458 if (r < 0)
459 goto fail;
460 }
15ae422b 461
c2c13f2d
LP
462 for (m = mounts; m < mounts + n; ++m) {
463 r = make_read_only(m);
464 if (r < 0)
465 goto fail;
466 }
15ae422b
LP
467 }
468
ee818b89
AC
469 if (root_directory) {
470 /* MS_MOVE does not work on MS_SHARED so the remount MS_SHARED will be done later */
471 r = mount_move_root(root_directory);
472
473 /* at this point, we cannot rollback */
474 if (r < 0)
475 return r;
476 }
477
c2c13f2d
LP
478 /* Remount / as the desired mode. Not that this will not
479 * reestablish propagation from our side to the host, since
480 * what's disconnected is disconnected. */
1f6b4113 481 if (mount(NULL, "/", NULL, mount_flags | MS_REC, NULL) < 0)
ee818b89
AC
482 /* at this point, we cannot rollback */
483 return -errno;
15ae422b 484
15ae422b
LP
485 return 0;
486
613b411c 487fail:
c2c13f2d
LP
488 if (n > 0) {
489 for (m = mounts; m < mounts + n; ++m)
490 if (m->done)
42b1b990 491 (void) umount2(m->path, MNT_DETACH);
c2c13f2d 492 }
613b411c
LP
493
494 return r;
495}
496
497static int setup_one_tmp_dir(const char *id, const char *prefix, char **path) {
498 _cleanup_free_ char *x = NULL;
6b46ea73
LP
499 char bid[SD_ID128_STRING_MAX];
500 sd_id128_t boot_id;
501 int r;
613b411c
LP
502
503 assert(id);
504 assert(prefix);
505 assert(path);
506
6b46ea73
LP
507 /* We include the boot id in the directory so that after a
508 * reboot we can easily identify obsolete directories. */
509
510 r = sd_id128_get_boot(&boot_id);
511 if (r < 0)
512 return r;
513
514 x = strjoin(prefix, "/systemd-private-", sd_id128_to_string(boot_id, bid), "-", id, "-XXXXXX", NULL);
613b411c
LP
515 if (!x)
516 return -ENOMEM;
517
518 RUN_WITH_UMASK(0077)
519 if (!mkdtemp(x))
520 return -errno;
521
522 RUN_WITH_UMASK(0000) {
523 char *y;
524
63c372cb 525 y = strjoina(x, "/tmp");
613b411c
LP
526
527 if (mkdir(y, 0777 | S_ISVTX) < 0)
528 return -errno;
c17ec25e 529 }
15ae422b 530
613b411c
LP
531 *path = x;
532 x = NULL;
533
534 return 0;
535}
536
537int setup_tmp_dirs(const char *id, char **tmp_dir, char **var_tmp_dir) {
538 char *a, *b;
539 int r;
540
541 assert(id);
542 assert(tmp_dir);
543 assert(var_tmp_dir);
544
545 r = setup_one_tmp_dir(id, "/tmp", &a);
546 if (r < 0)
547 return r;
548
549 r = setup_one_tmp_dir(id, "/var/tmp", &b);
550 if (r < 0) {
551 char *t;
552
63c372cb 553 t = strjoina(a, "/tmp");
613b411c
LP
554 rmdir(t);
555 rmdir(a);
556
557 free(a);
558 return r;
559 }
560
561 *tmp_dir = a;
562 *var_tmp_dir = b;
563
564 return 0;
565}
566
567int setup_netns(int netns_storage_socket[2]) {
568 _cleanup_close_ int netns = -1;
3ee897d6 569 int r, q;
613b411c
LP
570
571 assert(netns_storage_socket);
572 assert(netns_storage_socket[0] >= 0);
573 assert(netns_storage_socket[1] >= 0);
574
575 /* We use the passed socketpair as a storage buffer for our
76cd584b
LP
576 * namespace reference fd. Whatever process runs this first
577 * shall create a new namespace, all others should just join
578 * it. To serialize that we use a file lock on the socket
579 * pair.
613b411c
LP
580 *
581 * It's a bit crazy, but hey, works great! */
582
583 if (lockf(netns_storage_socket[0], F_LOCK, 0) < 0)
584 return -errno;
585
3ee897d6
LP
586 netns = receive_one_fd(netns_storage_socket[0], MSG_DONTWAIT);
587 if (netns == -EAGAIN) {
613b411c
LP
588 /* Nothing stored yet, so let's create a new namespace */
589
590 if (unshare(CLONE_NEWNET) < 0) {
591 r = -errno;
592 goto fail;
593 }
594
595 loopback_setup();
596
597 netns = open("/proc/self/ns/net", O_RDONLY|O_CLOEXEC|O_NOCTTY);
598 if (netns < 0) {
599 r = -errno;
600 goto fail;
601 }
602
603 r = 1;
613b411c 604
3ee897d6
LP
605 } else if (netns < 0) {
606 r = netns;
607 goto fail;
613b411c 608
3ee897d6
LP
609 } else {
610 /* Yay, found something, so let's join the namespace */
613b411c
LP
611 if (setns(netns, CLONE_NEWNET) < 0) {
612 r = -errno;
613 goto fail;
614 }
615
616 r = 0;
617 }
618
3ee897d6
LP
619 q = send_one_fd(netns_storage_socket[1], netns, MSG_DONTWAIT);
620 if (q < 0) {
621 r = q;
613b411c
LP
622 goto fail;
623 }
624
625fail:
626 lockf(netns_storage_socket[0], F_ULOCK, 0);
15ae422b
LP
627 return r;
628}
417116f2 629
1b8689f9
LP
630static const char *const protect_home_table[_PROTECT_HOME_MAX] = {
631 [PROTECT_HOME_NO] = "no",
632 [PROTECT_HOME_YES] = "yes",
633 [PROTECT_HOME_READ_ONLY] = "read-only",
417116f2
LP
634};
635
1b8689f9
LP
636DEFINE_STRING_TABLE_LOOKUP(protect_home, ProtectHome);
637
638static const char *const protect_system_table[_PROTECT_SYSTEM_MAX] = {
639 [PROTECT_SYSTEM_NO] = "no",
640 [PROTECT_SYSTEM_YES] = "yes",
641 [PROTECT_SYSTEM_FULL] = "full",
642};
643
644DEFINE_STRING_TABLE_LOOKUP(protect_system, ProtectSystem);