]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-mount.c
nspawn: add nosuid and nodev to /tmp mount (#6004)
[thirdparty/systemd.git] / src / nspawn / nspawn-mount.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2015 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
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
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
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <sys/mount.h>
21 #include <linux/magic.h>
22
23 #include "alloc-util.h"
24 #include "escape.h"
25 #include "fd-util.h"
26 #include "fileio.h"
27 #include "fs-util.h"
28 #include "label.h"
29 #include "mkdir.h"
30 #include "mount-util.h"
31 #include "nspawn-mount.h"
32 #include "parse-util.h"
33 #include "path-util.h"
34 #include "rm-rf.h"
35 #include "set.h"
36 #include "stat-util.h"
37 #include "string-util.h"
38 #include "strv.h"
39 #include "user-util.h"
40 #include "util.h"
41
42 CustomMount* custom_mount_add(CustomMount **l, unsigned *n, CustomMountType t) {
43 CustomMount *c, *ret;
44
45 assert(l);
46 assert(n);
47 assert(t >= 0);
48 assert(t < _CUSTOM_MOUNT_TYPE_MAX);
49
50 c = realloc_multiply(*l, (*n + 1), sizeof(CustomMount));
51 if (!c)
52 return NULL;
53
54 *l = c;
55 ret = *l + *n;
56 (*n)++;
57
58 *ret = (CustomMount) { .type = t };
59
60 return ret;
61 }
62
63 void custom_mount_free_all(CustomMount *l, unsigned n) {
64 unsigned i;
65
66 for (i = 0; i < n; i++) {
67 CustomMount *m = l + i;
68
69 free(m->source);
70 free(m->destination);
71 free(m->options);
72
73 if (m->work_dir) {
74 (void) rm_rf(m->work_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
75 free(m->work_dir);
76 }
77
78 if (m->rm_rf_tmpdir) {
79 (void) rm_rf(m->rm_rf_tmpdir, REMOVE_ROOT|REMOVE_PHYSICAL);
80 free(m->rm_rf_tmpdir);
81 }
82
83 strv_free(m->lower);
84 }
85
86 free(l);
87 }
88
89 static int custom_mount_compare(const void *a, const void *b) {
90 const CustomMount *x = a, *y = b;
91 int r;
92
93 r = path_compare(x->destination, y->destination);
94 if (r != 0)
95 return r;
96
97 if (x->type < y->type)
98 return -1;
99 if (x->type > y->type)
100 return 1;
101
102 return 0;
103 }
104
105 static bool source_path_is_valid(const char *p) {
106 assert(p);
107
108 if (*p == '+')
109 p++;
110
111 return path_is_absolute(p);
112 }
113
114 static char *resolve_source_path(const char *dest, const char *source) {
115
116 if (!source)
117 return NULL;
118
119 if (source[0] == '+')
120 return prefix_root(dest, source + 1);
121
122 return strdup(source);
123 }
124
125 int custom_mount_prepare_all(const char *dest, CustomMount *l, unsigned n) {
126 unsigned i;
127 int r;
128
129 /* Prepare all custom mounts. This will make source we know all temporary directories. This is called in the
130 * parent process, so that we know the temporary directories to remove on exit before we fork off the
131 * children. */
132
133 assert(l || n == 0);
134
135 /* Order the custom mounts, and make sure we have a working directory */
136 qsort_safe(l, n, sizeof(CustomMount), custom_mount_compare);
137
138 for (i = 0; i < n; i++) {
139 CustomMount *m = l + i;
140
141 if (m->source) {
142 char *s;
143
144 s = resolve_source_path(dest, m->source);
145 if (!s)
146 return log_oom();
147
148 free(m->source);
149 m->source = s;
150 } else {
151 /* No source specified? In that case, use a throw-away temporary directory in /var/tmp */
152
153 m->rm_rf_tmpdir = strdup("/var/tmp/nspawn-temp-XXXXXX");
154 if (!m->rm_rf_tmpdir)
155 return log_oom();
156
157 if (!mkdtemp(m->rm_rf_tmpdir)) {
158 m->rm_rf_tmpdir = mfree(m->rm_rf_tmpdir);
159 return log_error_errno(errno, "Failed to acquire temporary directory: %m");
160 }
161
162 m->source = strjoin(m->rm_rf_tmpdir, "/src");
163 if (!m->source)
164 return log_oom();
165
166 if (mkdir(m->source, 0755) < 0)
167 return log_error_errno(errno, "Failed to create %s: %m", m->source);
168 }
169
170 if (m->type == CUSTOM_MOUNT_OVERLAY) {
171 char **j;
172
173 STRV_FOREACH(j, m->lower) {
174 char *s;
175
176 s = resolve_source_path(dest, *j);
177 if (!s)
178 return log_oom();
179
180 free(*j);
181 *j = s;
182 }
183
184 if (m->work_dir) {
185 char *s;
186
187 s = resolve_source_path(dest, m->work_dir);
188 if (!s)
189 return log_oom();
190
191 free(m->work_dir);
192 m->work_dir = s;
193 } else {
194 assert(m->source);
195
196 r = tempfn_random(m->source, NULL, &m->work_dir);
197 if (r < 0)
198 return log_error_errno(r, "Failed to acquire working directory: %m");
199 }
200
201 (void) mkdir_label(m->work_dir, 0700);
202 }
203 }
204
205 return 0;
206 }
207
208 int bind_mount_parse(CustomMount **l, unsigned *n, const char *s, bool read_only) {
209 _cleanup_free_ char *source = NULL, *destination = NULL, *opts = NULL;
210 const char *p = s;
211 CustomMount *m;
212 int r;
213
214 assert(l);
215 assert(n);
216
217 r = extract_many_words(&p, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &source, &destination, NULL);
218 if (r < 0)
219 return r;
220 if (r == 0)
221 return -EINVAL;
222 if (r == 1) {
223 destination = strdup(source[0] == '+' ? source+1 : source);
224 if (!destination)
225 return -ENOMEM;
226 }
227 if (r == 2 && !isempty(p)) {
228 opts = strdup(p);
229 if (!opts)
230 return -ENOMEM;
231 }
232
233 if (isempty(source))
234 source = NULL;
235 else if (!source_path_is_valid(source))
236 return -EINVAL;
237
238 if (!path_is_absolute(destination))
239 return -EINVAL;
240
241 m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND);
242 if (!m)
243 return -ENOMEM;
244
245 m->source = source;
246 m->destination = destination;
247 m->read_only = read_only;
248 m->options = opts;
249
250 source = destination = opts = NULL;
251 return 0;
252 }
253
254 int tmpfs_mount_parse(CustomMount **l, unsigned *n, const char *s) {
255 _cleanup_free_ char *path = NULL, *opts = NULL;
256 const char *p = s;
257 CustomMount *m;
258 int r;
259
260 assert(l);
261 assert(n);
262 assert(s);
263
264 r = extract_first_word(&p, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
265 if (r < 0)
266 return r;
267 if (r == 0)
268 return -EINVAL;
269
270 if (isempty(p))
271 opts = strdup("mode=0755");
272 else
273 opts = strdup(p);
274 if (!opts)
275 return -ENOMEM;
276
277 if (!path_is_absolute(path))
278 return -EINVAL;
279
280 m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS);
281 if (!m)
282 return -ENOMEM;
283
284 m->destination = path;
285 m->options = opts;
286
287 path = opts = NULL;
288 return 0;
289 }
290
291 int overlay_mount_parse(CustomMount **l, unsigned *n, const char *s, bool read_only) {
292 _cleanup_free_ char *upper = NULL, *destination = NULL;
293 _cleanup_strv_free_ char **lower = NULL;
294 CustomMount *m;
295 int k;
296
297 k = strv_split_extract(&lower, s, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
298 if (k < 0)
299 return k;
300 if (k < 2)
301 return -EADDRNOTAVAIL;
302 if (k == 2) {
303 /* If two parameters are specified, the first one is the lower, the second one the upper directory. And
304 * we'll also define the destination mount point the same as the upper. */
305
306 if (!source_path_is_valid(lower[0]) ||
307 !source_path_is_valid(lower[1]))
308 return -EINVAL;
309
310 upper = lower[1];
311 lower[1] = NULL;
312
313 destination = strdup(upper[0] == '+' ? upper+1 : upper); /* take the destination without "+" prefix */
314 if (!destination)
315 return -ENOMEM;
316 } else {
317 char **i;
318
319 /* If more than two parameters are specified, the last one is the destination, the second to last one
320 * the "upper", and all before that the "lower" directories. */
321
322 destination = lower[k - 1];
323 upper = lower[k - 2];
324 lower[k - 2] = NULL;
325
326 STRV_FOREACH(i, lower)
327 if (!source_path_is_valid(*i))
328 return -EINVAL;
329
330 /* If the upper directory is unspecified, then let's create it automatically as a throw-away directory
331 * in /var/tmp */
332 if (isempty(upper))
333 upper = NULL;
334 else if (!source_path_is_valid(upper))
335 return -EINVAL;
336
337 if (!path_is_absolute(destination))
338 return -EINVAL;
339 }
340
341 m = custom_mount_add(l, n, CUSTOM_MOUNT_OVERLAY);
342 if (!m)
343 return -ENOMEM;
344
345 m->destination = destination;
346 m->source = upper;
347 m->lower = lower;
348 m->read_only = read_only;
349
350 upper = destination = NULL;
351 lower = NULL;
352
353 return 0;
354 }
355
356 static int tmpfs_patch_options(
357 const char *options,
358 bool userns,
359 uid_t uid_shift, uid_t uid_range,
360 bool patch_ids,
361 const char *selinux_apifs_context,
362 char **ret) {
363
364 char *buf = NULL;
365
366 if ((userns && uid_shift != 0) || patch_ids) {
367 assert(uid_shift != UID_INVALID);
368
369 if (asprintf(&buf, "%s%suid=" UID_FMT ",gid=" UID_FMT,
370 options ?: "", options ? "," : "",
371 uid_shift, uid_shift) < 0)
372 return -ENOMEM;
373
374 options = buf;
375 }
376
377 #ifdef HAVE_SELINUX
378 if (selinux_apifs_context) {
379 char *t;
380
381 t = strjoin(options ?: "", options ? "," : "",
382 "context=\"", selinux_apifs_context, "\"");
383 free(buf);
384 if (!t)
385 return -ENOMEM;
386
387 buf = t;
388 }
389 #endif
390
391 if (!buf && options) {
392 buf = strdup(options);
393 if (!buf)
394 return -ENOMEM;
395 }
396 *ret = buf;
397
398 return !!buf;
399 }
400
401 int mount_sysfs(const char *dest, MountSettingsMask mount_settings) {
402 const char *full, *top, *x;
403 int r;
404 unsigned long extra_flags = 0;
405
406 top = prefix_roota(dest, "/sys");
407 r = path_check_fstype(top, SYSFS_MAGIC);
408 if (r < 0)
409 return log_error_errno(r, "Failed to determine filesystem type of %s: %m", top);
410 /* /sys might already be mounted as sysfs by the outer child in the
411 * !netns case. In this case, it's all good. Don't touch it because we
412 * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555.
413 */
414 if (r > 0)
415 return 0;
416
417 full = prefix_roota(top, "/full");
418
419 (void) mkdir(full, 0755);
420
421 if (mount_settings & MOUNT_APPLY_APIVFS_RO)
422 extra_flags |= MS_RDONLY;
423
424 r = mount_verbose(LOG_ERR, "sysfs", full, "sysfs",
425 MS_NOSUID|MS_NOEXEC|MS_NODEV|extra_flags, NULL);
426 if (r < 0)
427 return r;
428
429 FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") {
430 _cleanup_free_ char *from = NULL, *to = NULL;
431
432 from = prefix_root(full, x);
433 if (!from)
434 return log_oom();
435
436 to = prefix_root(top, x);
437 if (!to)
438 return log_oom();
439
440 (void) mkdir(to, 0755);
441
442 r = mount_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
443 if (r < 0)
444 return r;
445
446 r = mount_verbose(LOG_ERR, NULL, to, NULL,
447 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
448 if (r < 0)
449 return r;
450 }
451
452 r = umount_verbose(full);
453 if (r < 0)
454 return r;
455
456 if (rmdir(full) < 0)
457 return log_error_errno(errno, "Failed to remove %s: %m", full);
458
459 x = prefix_roota(top, "/fs/kdbus");
460 (void) mkdir_p(x, 0755);
461
462 /* Create mountpoint for cgroups. Otherwise we are not allowed since we
463 * remount /sys read-only.
464 */
465 if (cg_ns_supported()) {
466 x = prefix_roota(top, "/fs/cgroup");
467 (void) mkdir_p(x, 0755);
468 }
469
470 return mount_verbose(LOG_ERR, NULL, top, NULL,
471 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
472 }
473
474 static int mkdir_userns(const char *path, mode_t mode, MountSettingsMask mask, uid_t uid_shift) {
475 int r;
476
477 assert(path);
478
479 r = mkdir(path, mode);
480 if (r < 0 && errno != EEXIST)
481 return -errno;
482
483 if ((mask & MOUNT_USE_USERNS) == 0)
484 return 0;
485
486 if (mask & MOUNT_IN_USERNS)
487 return 0;
488
489 r = lchown(path, uid_shift, uid_shift);
490 if (r < 0)
491 return -errno;
492
493 return 0;
494 }
495
496 static int mkdir_userns_p(const char *prefix, const char *path, mode_t mode, MountSettingsMask mask, uid_t uid_shift) {
497 const char *p, *e;
498 int r;
499
500 assert(path);
501
502 if (prefix && !path_startswith(path, prefix))
503 return -ENOTDIR;
504
505 /* create every parent directory in the path, except the last component */
506 p = path + strspn(path, "/");
507 for (;;) {
508 char t[strlen(path) + 1];
509
510 e = p + strcspn(p, "/");
511 p = e + strspn(e, "/");
512
513 /* Is this the last component? If so, then we're done */
514 if (*p == 0)
515 break;
516
517 memcpy(t, path, e - path);
518 t[e-path] = 0;
519
520 if (prefix && path_startswith(prefix, t))
521 continue;
522
523 r = mkdir_userns(t, mode, mask, uid_shift);
524 if (r < 0)
525 return r;
526 }
527
528 return mkdir_userns(path, mode, mask, uid_shift);
529 }
530
531 int mount_all(const char *dest,
532 MountSettingsMask mount_settings,
533 uid_t uid_shift, uid_t uid_range,
534 const char *selinux_apifs_context) {
535
536 typedef struct MountPoint {
537 const char *what;
538 const char *where;
539 const char *type;
540 const char *options;
541 unsigned long flags;
542 MountSettingsMask mount_settings;
543 } MountPoint;
544
545 static const MountPoint mount_table[] = {
546 /* inner child mounts */
547 { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL|MOUNT_IN_USERNS },
548 { "/proc/sys", "/proc/sys", NULL, NULL, MS_BIND, MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */
549 { "/proc/sys/net", "/proc/sys/net", NULL, NULL, MS_BIND, MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_APIVFS_NETNS }, /* (except for this) */
550 { NULL, "/proc/sys", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* ... then, make it r/o */
551 { "/proc/sysrq-trigger", "/proc/sysrq-trigger", NULL, NULL, MS_BIND, MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */
552 { NULL, "/proc/sysrq-trigger", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* ... then, make it r/o */
553
554 /* outer child mounts */
555 { "tmpfs", "/tmp", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME, MOUNT_FATAL },
556 { "tmpfs", "/sys", "tmpfs", "mode=755", MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL|MOUNT_APPLY_APIVFS_NETNS },
557 { "sysfs", "/sys", "sysfs", NULL, MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL|MOUNT_APPLY_APIVFS_RO }, /* skipped if above was mounted */
558 { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL }, /* skipped if above was mounted */
559
560 { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID|MS_STRICTATIME, MOUNT_FATAL },
561 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME, MOUNT_FATAL },
562 { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID|MS_NODEV|MS_STRICTATIME, MOUNT_FATAL },
563 #ifdef HAVE_SELINUX
564 { "/sys/fs/selinux", "/sys/fs/selinux", NULL, NULL, MS_BIND, 0 }, /* Bind mount first */
565 { NULL, "/sys/fs/selinux", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, 0 }, /* Then, make it r/o */
566 #endif
567 };
568
569 unsigned k;
570 int r;
571 bool use_userns = (mount_settings & MOUNT_USE_USERNS);
572 bool netns = (mount_settings & MOUNT_APPLY_APIVFS_NETNS);
573 bool ro = (mount_settings & MOUNT_APPLY_APIVFS_RO);
574 bool in_userns = (mount_settings & MOUNT_IN_USERNS);
575
576 for (k = 0; k < ELEMENTSOF(mount_table); k++) {
577 _cleanup_free_ char *where = NULL, *options = NULL;
578 const char *o;
579 bool fatal = (mount_table[k].mount_settings & MOUNT_FATAL);
580
581 if (in_userns != (bool)(mount_table[k].mount_settings & MOUNT_IN_USERNS))
582 continue;
583
584 if (!netns && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_APIVFS_NETNS))
585 continue;
586
587 if (!ro && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_APIVFS_RO))
588 continue;
589
590 r = chase_symlinks(mount_table[k].where, dest, CHASE_NONEXISTENT|CHASE_PREFIX_ROOT, &where);
591 if (r < 0)
592 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, mount_table[k].where);
593
594 r = path_is_mount_point(where, NULL, 0);
595 if (r < 0 && r != -ENOENT)
596 return log_error_errno(r, "Failed to detect whether %s is a mount point: %m", where);
597
598 /* Skip this entry if it is not a remount. */
599 if (mount_table[k].what && r > 0)
600 continue;
601
602 r = mkdir_userns_p(dest, where, 0755, mount_settings, uid_shift);
603 if (r < 0 && r != -EEXIST) {
604 if (fatal)
605 return log_error_errno(r, "Failed to create directory %s: %m", where);
606
607 log_debug_errno(r, "Failed to create directory %s: %m", where);
608 continue;
609 }
610
611 o = mount_table[k].options;
612 if (streq_ptr(mount_table[k].type, "tmpfs")) {
613 if (in_userns)
614 r = tmpfs_patch_options(o, use_userns, 0, uid_range, true, selinux_apifs_context, &options);
615 else
616 r = tmpfs_patch_options(o, use_userns, uid_shift, uid_range, false, selinux_apifs_context, &options);
617 if (r < 0)
618 return log_oom();
619 if (r > 0)
620 o = options;
621 }
622
623 r = mount_verbose(fatal ? LOG_ERR : LOG_DEBUG,
624 mount_table[k].what,
625 where,
626 mount_table[k].type,
627 mount_table[k].flags,
628 o);
629 if (r < 0 && fatal)
630 return r;
631 }
632
633 return 0;
634 }
635
636 static int parse_mount_bind_options(const char *options, unsigned long *mount_flags, char **mount_opts) {
637 const char *p = options;
638 unsigned long flags = *mount_flags;
639 char *opts = NULL;
640 int r;
641
642 assert(options);
643
644 for (;;) {
645 _cleanup_free_ char *word = NULL;
646
647 r = extract_first_word(&p, &word, ",", 0);
648 if (r < 0)
649 return log_error_errno(r, "Failed to extract mount option: %m");
650 if (r == 0)
651 break;
652
653 if (streq(word, "rbind"))
654 flags |= MS_REC;
655 else if (streq(word, "norbind"))
656 flags &= ~MS_REC;
657 else {
658 log_error("Invalid bind mount option: %s", word);
659 return -EINVAL;
660 }
661 }
662
663 *mount_flags = flags;
664 /* in the future mount_opts will hold string options for mount(2) */
665 *mount_opts = opts;
666
667 return 0;
668 }
669
670 static int mount_bind(const char *dest, CustomMount *m) {
671
672 _cleanup_free_ char *mount_opts = NULL, *where = NULL;
673 unsigned long mount_flags = MS_BIND | MS_REC;
674 struct stat source_st, dest_st;
675 int r;
676
677 assert(dest);
678 assert(m);
679
680 if (m->options) {
681 r = parse_mount_bind_options(m->options, &mount_flags, &mount_opts);
682 if (r < 0)
683 return r;
684 }
685
686 if (stat(m->source, &source_st) < 0)
687 return log_error_errno(errno, "Failed to stat %s: %m", m->source);
688
689 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where);
690 if (r < 0)
691 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
692 if (r > 0) { /* Path exists already? */
693
694 if (stat(where, &dest_st) < 0)
695 return log_error_errno(errno, "Failed to stat %s: %m", where);
696
697 if (S_ISDIR(source_st.st_mode) && !S_ISDIR(dest_st.st_mode)) {
698 log_error("Cannot bind mount directory %s on file %s.", m->source, where);
699 return -EINVAL;
700 }
701
702 if (!S_ISDIR(source_st.st_mode) && S_ISDIR(dest_st.st_mode)) {
703 log_error("Cannot bind mount file %s on directory %s.", m->source, where);
704 return -EINVAL;
705 }
706
707 } else { /* Path doesn't exist yet? */
708 r = mkdir_parents_label(where, 0755);
709 if (r < 0)
710 return log_error_errno(r, "Failed to make parents of %s: %m", where);
711
712 /* Create the mount point. Any non-directory file can be
713 * mounted on any non-directory file (regular, fifo, socket,
714 * char, block).
715 */
716 if (S_ISDIR(source_st.st_mode))
717 r = mkdir_label(where, 0755);
718 else
719 r = touch(where);
720 if (r < 0)
721 return log_error_errno(r, "Failed to create mount point %s: %m", where);
722
723 }
724
725 r = mount_verbose(LOG_ERR, m->source, where, NULL, mount_flags, mount_opts);
726 if (r < 0)
727 return r;
728
729 if (m->read_only) {
730 r = bind_remount_recursive(where, true, NULL);
731 if (r < 0)
732 return log_error_errno(r, "Read-only bind mount failed: %m");
733 }
734
735 return 0;
736 }
737
738 static int mount_tmpfs(
739 const char *dest,
740 CustomMount *m,
741 bool userns, uid_t uid_shift, uid_t uid_range,
742 const char *selinux_apifs_context) {
743
744 const char *options;
745 _cleanup_free_ char *buf = NULL, *where = NULL;
746 int r;
747
748 assert(dest);
749 assert(m);
750
751 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where);
752 if (r < 0)
753 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
754 if (r == 0) { /* Doesn't exist yet? */
755 r = mkdir_p_label(where, 0755);
756 if (r < 0)
757 return log_error_errno(r, "Creating mount point for tmpfs %s failed: %m", where);
758 }
759
760 r = tmpfs_patch_options(m->options, userns, uid_shift, uid_range, false, selinux_apifs_context, &buf);
761 if (r < 0)
762 return log_oom();
763 options = r > 0 ? buf : m->options;
764
765 return mount_verbose(LOG_ERR, "tmpfs", where, "tmpfs", MS_NODEV|MS_STRICTATIME, options);
766 }
767
768 static char *joined_and_escaped_lower_dirs(char **lower) {
769 _cleanup_strv_free_ char **sv = NULL;
770
771 sv = strv_copy(lower);
772 if (!sv)
773 return NULL;
774
775 strv_reverse(sv);
776
777 if (!strv_shell_escape(sv, ",:"))
778 return NULL;
779
780 return strv_join(sv, ":");
781 }
782
783 static int mount_overlay(const char *dest, CustomMount *m) {
784
785 _cleanup_free_ char *lower = NULL, *where = NULL, *escaped_source = NULL;
786 const char *options;
787 int r;
788
789 assert(dest);
790 assert(m);
791
792 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where);
793 if (r < 0)
794 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
795 if (r == 0) { /* Doesn't exist yet? */
796 r = mkdir_label(where, 0755);
797 if (r < 0)
798 return log_error_errno(r, "Creating mount point for overlay %s failed: %m", where);
799 }
800
801 (void) mkdir_p_label(m->source, 0755);
802
803 lower = joined_and_escaped_lower_dirs(m->lower);
804 if (!lower)
805 return log_oom();
806
807 escaped_source = shell_escape(m->source, ",:");
808 if (!escaped_source)
809 return log_oom();
810
811 if (m->read_only)
812 options = strjoina("lowerdir=", escaped_source, ":", lower);
813 else {
814 _cleanup_free_ char *escaped_work_dir = NULL;
815
816 escaped_work_dir = shell_escape(m->work_dir, ",:");
817 if (!escaped_work_dir)
818 return log_oom();
819
820 options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir);
821 }
822
823 return mount_verbose(LOG_ERR, "overlay", where, "overlay", m->read_only ? MS_RDONLY : 0, options);
824 }
825
826 int mount_custom(
827 const char *dest,
828 CustomMount *mounts, unsigned n,
829 bool userns, uid_t uid_shift, uid_t uid_range,
830 const char *selinux_apifs_context) {
831
832 unsigned i;
833 int r;
834
835 assert(dest);
836
837 for (i = 0; i < n; i++) {
838 CustomMount *m = mounts + i;
839
840 switch (m->type) {
841
842 case CUSTOM_MOUNT_BIND:
843 r = mount_bind(dest, m);
844 break;
845
846 case CUSTOM_MOUNT_TMPFS:
847 r = mount_tmpfs(dest, m, userns, uid_shift, uid_range, selinux_apifs_context);
848 break;
849
850 case CUSTOM_MOUNT_OVERLAY:
851 r = mount_overlay(dest, m);
852 break;
853
854 default:
855 assert_not_reached("Unknown custom mount type");
856 }
857
858 if (r < 0)
859 return r;
860 }
861
862 return 0;
863 }
864
865 /* Retrieve existing subsystems. This function is called in a new cgroup
866 * namespace.
867 */
868 static int get_controllers(Set *subsystems) {
869 _cleanup_fclose_ FILE *f = NULL;
870 char line[LINE_MAX];
871
872 assert(subsystems);
873
874 f = fopen("/proc/self/cgroup", "re");
875 if (!f)
876 return errno == ENOENT ? -ESRCH : -errno;
877
878 FOREACH_LINE(line, f, return -errno) {
879 int r;
880 char *e, *l, *p;
881
882 l = strchr(line, ':');
883 if (!l)
884 continue;
885
886 l++;
887 e = strchr(l, ':');
888 if (!e)
889 continue;
890
891 *e = 0;
892
893 if (STR_IN_SET(l, "", "name=systemd", "name=unified"))
894 continue;
895
896 p = strdup(l);
897 if (!p)
898 return -ENOMEM;
899
900 r = set_consume(subsystems, p);
901 if (r < 0)
902 return r;
903 }
904
905 return 0;
906 }
907
908 static int mount_legacy_cgroup_hierarchy(
909 const char *dest,
910 const char *controller,
911 const char *hierarchy,
912 bool read_only) {
913
914 const char *to, *fstype, *opts;
915 int r;
916
917 to = strjoina(strempty(dest), "/sys/fs/cgroup/", hierarchy);
918
919 r = path_is_mount_point(to, dest, 0);
920 if (r < 0 && r != -ENOENT)
921 return log_error_errno(r, "Failed to determine if %s is mounted already: %m", to);
922 if (r > 0)
923 return 0;
924
925 mkdir_p(to, 0755);
926
927 /* The superblock mount options of the mount point need to be
928 * identical to the hosts', and hence writable... */
929 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER_HYBRID)) {
930 fstype = "cgroup2";
931 opts = NULL;
932 } else if (streq(controller, SYSTEMD_CGROUP_CONTROLLER_LEGACY)) {
933 fstype = "cgroup";
934 opts = "none,name=systemd,xattr";
935 } else {
936 fstype = "cgroup";
937 opts = controller;
938 }
939
940 r = mount_verbose(LOG_ERR, "cgroup", to, fstype, MS_NOSUID|MS_NOEXEC|MS_NODEV, opts);
941 if (r < 0)
942 return r;
943
944 /* ... hence let's only make the bind mount read-only, not the superblock. */
945 if (read_only) {
946 r = mount_verbose(LOG_ERR, NULL, to, NULL,
947 MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, NULL);
948 if (r < 0)
949 return r;
950 }
951
952 return 1;
953 }
954
955 /* Mount a legacy cgroup hierarchy when cgroup namespaces are supported. */
956 static int mount_legacy_cgns_supported(
957 const char *dest,
958 CGroupUnified unified_requested,
959 bool userns,
960 uid_t uid_shift,
961 uid_t uid_range,
962 const char *selinux_apifs_context) {
963
964 _cleanup_set_free_free_ Set *controllers = NULL;
965 const char *cgroup_root = "/sys/fs/cgroup", *c;
966 int r;
967
968 (void) mkdir_p(cgroup_root, 0755);
969
970 /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
971 r = path_is_mount_point(cgroup_root, dest, AT_SYMLINK_FOLLOW);
972 if (r < 0)
973 return log_error_errno(r, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
974 if (r == 0) {
975 _cleanup_free_ char *options = NULL;
976
977 /* When cgroup namespaces are enabled and user namespaces are
978 * used then the mount of the cgroupfs is done *inside* the new
979 * user namespace. We're root in the new user namespace and the
980 * kernel will happily translate our uid/gid to the correct
981 * uid/gid as seen from e.g. /proc/1/mountinfo. So we simply
982 * pass uid 0 and not uid_shift to tmpfs_patch_options().
983 */
984 r = tmpfs_patch_options("mode=755", userns, 0, uid_range, true, selinux_apifs_context, &options);
985 if (r < 0)
986 return log_oom();
987
988 r = mount_verbose(LOG_ERR, "tmpfs", cgroup_root, "tmpfs",
989 MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, options);
990 if (r < 0)
991 return r;
992 }
993
994 r = cg_all_unified();
995 if (r < 0)
996 return r;
997 if (r > 0)
998 goto skip_controllers;
999
1000 controllers = set_new(&string_hash_ops);
1001 if (!controllers)
1002 return log_oom();
1003
1004 r = get_controllers(controllers);
1005 if (r < 0)
1006 return log_error_errno(r, "Failed to determine cgroup controllers: %m");
1007
1008 for (;;) {
1009 _cleanup_free_ const char *controller = NULL;
1010
1011 controller = set_steal_first(controllers);
1012 if (!controller)
1013 break;
1014
1015 r = mount_legacy_cgroup_hierarchy("", controller, controller, !userns);
1016 if (r < 0)
1017 return r;
1018
1019 /* When multiple hierarchies are co-mounted, make their
1020 * constituting individual hierarchies a symlink to the
1021 * co-mount.
1022 */
1023 c = controller;
1024 for (;;) {
1025 _cleanup_free_ char *target = NULL, *tok = NULL;
1026
1027 r = extract_first_word(&c, &tok, ",", 0);
1028 if (r < 0)
1029 return log_error_errno(r, "Failed to extract co-mounted cgroup controller: %m");
1030 if (r == 0)
1031 break;
1032
1033 target = prefix_root("/sys/fs/cgroup", tok);
1034 if (!target)
1035 return log_oom();
1036
1037 if (streq(controller, tok))
1038 break;
1039
1040 r = symlink_idempotent(controller, target);
1041 if (r == -EINVAL)
1042 return log_error_errno(r, "Invalid existing symlink for combined hierarchy: %m");
1043 if (r < 0)
1044 return log_error_errno(r, "Failed to create symlink for combined hierarchy: %m");
1045 }
1046 }
1047
1048 skip_controllers:
1049 if (unified_requested >= CGROUP_UNIFIED_SYSTEMD) {
1050 r = mount_legacy_cgroup_hierarchy("", SYSTEMD_CGROUP_CONTROLLER_HYBRID, "unified", false);
1051 if (r < 0)
1052 return r;
1053 }
1054
1055 r = mount_legacy_cgroup_hierarchy("", SYSTEMD_CGROUP_CONTROLLER_LEGACY, "systemd", false);
1056 if (r < 0)
1057 return r;
1058
1059 if (!userns)
1060 return mount_verbose(LOG_ERR, NULL, cgroup_root, NULL,
1061 MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755");
1062
1063 return 0;
1064 }
1065
1066 /* Mount legacy cgroup hierarchy when cgroup namespaces are unsupported. */
1067 static int mount_legacy_cgns_unsupported(
1068 const char *dest,
1069 CGroupUnified unified_requested,
1070 bool userns,
1071 uid_t uid_shift,
1072 uid_t uid_range,
1073 const char *selinux_apifs_context) {
1074
1075 _cleanup_set_free_free_ Set *controllers = NULL;
1076 const char *cgroup_root;
1077 int r;
1078
1079 cgroup_root = prefix_roota(dest, "/sys/fs/cgroup");
1080
1081 (void) mkdir_p(cgroup_root, 0755);
1082
1083 /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
1084 r = path_is_mount_point(cgroup_root, dest, AT_SYMLINK_FOLLOW);
1085 if (r < 0)
1086 return log_error_errno(r, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
1087 if (r == 0) {
1088 _cleanup_free_ char *options = NULL;
1089
1090 r = tmpfs_patch_options("mode=755", userns, uid_shift, uid_range, false, selinux_apifs_context, &options);
1091 if (r < 0)
1092 return log_oom();
1093
1094 r = mount_verbose(LOG_ERR, "tmpfs", cgroup_root, "tmpfs",
1095 MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, options);
1096 if (r < 0)
1097 return r;
1098 }
1099
1100 r = cg_all_unified();
1101 if (r < 0)
1102 return r;
1103 if (r > 0)
1104 goto skip_controllers;
1105
1106 controllers = set_new(&string_hash_ops);
1107 if (!controllers)
1108 return log_oom();
1109
1110 r = cg_kernel_controllers(controllers);
1111 if (r < 0)
1112 return log_error_errno(r, "Failed to determine cgroup controllers: %m");
1113
1114 for (;;) {
1115 _cleanup_free_ char *controller = NULL, *origin = NULL, *combined = NULL;
1116
1117 controller = set_steal_first(controllers);
1118 if (!controller)
1119 break;
1120
1121 origin = prefix_root("/sys/fs/cgroup/", controller);
1122 if (!origin)
1123 return log_oom();
1124
1125 r = readlink_malloc(origin, &combined);
1126 if (r == -EINVAL) {
1127 /* Not a symbolic link, but directly a single cgroup hierarchy */
1128
1129 r = mount_legacy_cgroup_hierarchy(dest, controller, controller, true);
1130 if (r < 0)
1131 return r;
1132
1133 } else if (r < 0)
1134 return log_error_errno(r, "Failed to read link %s: %m", origin);
1135 else {
1136 _cleanup_free_ char *target = NULL;
1137
1138 target = prefix_root(dest, origin);
1139 if (!target)
1140 return log_oom();
1141
1142 /* A symbolic link, a combination of controllers in one hierarchy */
1143
1144 if (!filename_is_valid(combined)) {
1145 log_warning("Ignoring invalid combined hierarchy %s.", combined);
1146 continue;
1147 }
1148
1149 r = mount_legacy_cgroup_hierarchy(dest, combined, combined, true);
1150 if (r < 0)
1151 return r;
1152
1153 r = symlink_idempotent(combined, target);
1154 if (r == -EINVAL)
1155 return log_error_errno(r, "Invalid existing symlink for combined hierarchy: %m");
1156 if (r < 0)
1157 return log_error_errno(r, "Failed to create symlink for combined hierarchy: %m");
1158 }
1159 }
1160
1161 skip_controllers:
1162 if (unified_requested >= CGROUP_UNIFIED_SYSTEMD) {
1163 r = mount_legacy_cgroup_hierarchy(dest, SYSTEMD_CGROUP_CONTROLLER_HYBRID, "unified", false);
1164 if (r < 0)
1165 return r;
1166 }
1167
1168 r = mount_legacy_cgroup_hierarchy(dest, SYSTEMD_CGROUP_CONTROLLER_LEGACY, "systemd", false);
1169 if (r < 0)
1170 return r;
1171
1172 return mount_verbose(LOG_ERR, NULL, cgroup_root, NULL,
1173 MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755");
1174 }
1175
1176 static int mount_unified_cgroups(const char *dest) {
1177 const char *p;
1178 int r;
1179
1180 assert(dest);
1181
1182 p = prefix_roota(dest, "/sys/fs/cgroup");
1183
1184 (void) mkdir_p(p, 0755);
1185
1186 r = path_is_mount_point(p, dest, AT_SYMLINK_FOLLOW);
1187 if (r < 0)
1188 return log_error_errno(r, "Failed to determine if %s is mounted already: %m", p);
1189 if (r > 0) {
1190 p = prefix_roota(dest, "/sys/fs/cgroup/cgroup.procs");
1191 if (access(p, F_OK) >= 0)
1192 return 0;
1193 if (errno != ENOENT)
1194 return log_error_errno(errno, "Failed to determine if mount point %s contains the unified cgroup hierarchy: %m", p);
1195
1196 log_error("%s is already mounted but not a unified cgroup hierarchy. Refusing.", p);
1197 return -EINVAL;
1198 }
1199
1200 return mount_verbose(LOG_ERR, "cgroup", p, "cgroup2", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
1201 }
1202
1203 int mount_cgroups(
1204 const char *dest,
1205 CGroupUnified unified_requested,
1206 bool userns,
1207 uid_t uid_shift,
1208 uid_t uid_range,
1209 const char *selinux_apifs_context,
1210 bool use_cgns) {
1211
1212 if (unified_requested >= CGROUP_UNIFIED_ALL)
1213 return mount_unified_cgroups(dest);
1214 else if (use_cgns)
1215 return mount_legacy_cgns_supported(dest, unified_requested, userns, uid_shift, uid_range, selinux_apifs_context);
1216
1217 return mount_legacy_cgns_unsupported(dest, unified_requested, userns, uid_shift, uid_range, selinux_apifs_context);
1218 }
1219
1220 static int mount_systemd_cgroup_writable_one(const char *systemd_own, const char *systemd_root)
1221 {
1222 int r;
1223
1224 /* Make our own cgroup a (writable) bind mount */
1225 r = mount_verbose(LOG_ERR, systemd_own, systemd_own, NULL, MS_BIND, NULL);
1226 if (r < 0)
1227 return r;
1228
1229 /* And then remount the systemd cgroup root read-only */
1230 return mount_verbose(LOG_ERR, NULL, systemd_root, NULL,
1231 MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, NULL);
1232 }
1233
1234 int mount_systemd_cgroup_writable(
1235 const char *dest,
1236 CGroupUnified unified_requested) {
1237
1238 _cleanup_free_ char *own_cgroup_path = NULL;
1239 int r;
1240
1241 assert(dest);
1242
1243 r = cg_pid_get_path(NULL, 0, &own_cgroup_path);
1244 if (r < 0)
1245 return log_error_errno(r, "Failed to determine our own cgroup path: %m");
1246
1247 /* If we are living in the top-level, then there's nothing to do... */
1248 if (path_equal(own_cgroup_path, "/"))
1249 return 0;
1250
1251 if (unified_requested >= CGROUP_UNIFIED_ALL)
1252 return mount_systemd_cgroup_writable_one(strjoina(dest, "/sys/fs/cgroup", own_cgroup_path),
1253 prefix_roota(dest, "/sys/fs/cgroup"));
1254
1255 if (unified_requested >= CGROUP_UNIFIED_SYSTEMD) {
1256 r = mount_systemd_cgroup_writable_one(strjoina(dest, "/sys/fs/cgroup/unified", own_cgroup_path),
1257 prefix_roota(dest, "/sys/fs/cgroup/unified"));
1258 if (r < 0)
1259 return r;
1260 }
1261
1262 return mount_systemd_cgroup_writable_one(strjoina(dest, "/sys/fs/cgroup/systemd", own_cgroup_path),
1263 prefix_roota(dest, "/sys/fs/cgroup/systemd"));
1264 }
1265
1266 int setup_volatile_state(
1267 const char *directory,
1268 VolatileMode mode,
1269 bool userns, uid_t uid_shift, uid_t uid_range,
1270 const char *selinux_apifs_context) {
1271
1272 _cleanup_free_ char *buf = NULL;
1273 const char *p, *options;
1274 int r;
1275
1276 assert(directory);
1277
1278 if (mode != VOLATILE_STATE)
1279 return 0;
1280
1281 /* --volatile=state means we simply overmount /var
1282 with a tmpfs, and the rest read-only. */
1283
1284 r = bind_remount_recursive(directory, true, NULL);
1285 if (r < 0)
1286 return log_error_errno(r, "Failed to remount %s read-only: %m", directory);
1287
1288 p = prefix_roota(directory, "/var");
1289 r = mkdir(p, 0755);
1290 if (r < 0 && errno != EEXIST)
1291 return log_error_errno(errno, "Failed to create %s: %m", directory);
1292
1293 options = "mode=755";
1294 r = tmpfs_patch_options(options, userns, uid_shift, uid_range, false, selinux_apifs_context, &buf);
1295 if (r < 0)
1296 return log_oom();
1297 if (r > 0)
1298 options = buf;
1299
1300 return mount_verbose(LOG_ERR, "tmpfs", p, "tmpfs", MS_STRICTATIME, options);
1301 }
1302
1303 int setup_volatile(
1304 const char *directory,
1305 VolatileMode mode,
1306 bool userns, uid_t uid_shift, uid_t uid_range,
1307 const char *selinux_apifs_context) {
1308
1309 bool tmpfs_mounted = false, bind_mounted = false;
1310 char template[] = "/tmp/nspawn-volatile-XXXXXX";
1311 _cleanup_free_ char *buf = NULL;
1312 const char *f, *t, *options;
1313 int r;
1314
1315 assert(directory);
1316
1317 if (mode != VOLATILE_YES)
1318 return 0;
1319
1320 /* --volatile=yes means we mount a tmpfs to the root dir, and
1321 the original /usr to use inside it, and that read-only. */
1322
1323 if (!mkdtemp(template))
1324 return log_error_errno(errno, "Failed to create temporary directory: %m");
1325
1326 options = "mode=755";
1327 r = tmpfs_patch_options(options, userns, uid_shift, uid_range, false, selinux_apifs_context, &buf);
1328 if (r < 0)
1329 return log_oom();
1330 if (r > 0)
1331 options = buf;
1332
1333 r = mount_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
1334 if (r < 0)
1335 goto fail;
1336
1337 tmpfs_mounted = true;
1338
1339 f = prefix_roota(directory, "/usr");
1340 t = prefix_roota(template, "/usr");
1341
1342 r = mkdir(t, 0755);
1343 if (r < 0 && errno != EEXIST) {
1344 r = log_error_errno(errno, "Failed to create %s: %m", t);
1345 goto fail;
1346 }
1347
1348 r = mount_verbose(LOG_ERR, f, t, NULL, MS_BIND|MS_REC, NULL);
1349 if (r < 0)
1350 goto fail;
1351
1352 bind_mounted = true;
1353
1354 r = bind_remount_recursive(t, true, NULL);
1355 if (r < 0) {
1356 log_error_errno(r, "Failed to remount %s read-only: %m", t);
1357 goto fail;
1358 }
1359
1360 r = mount_verbose(LOG_ERR, template, directory, NULL, MS_MOVE, NULL);
1361 if (r < 0)
1362 goto fail;
1363
1364 (void) rmdir(template);
1365
1366 return 0;
1367
1368 fail:
1369 if (bind_mounted)
1370 (void) umount_verbose(t);
1371
1372 if (tmpfs_mounted)
1373 (void) umount_verbose(template);
1374 (void) rmdir(template);
1375 return r;
1376 }
1377
1378 /* Expects *pivot_root_new and *pivot_root_old to be initialised to allocated memory or NULL. */
1379 int pivot_root_parse(char **pivot_root_new, char **pivot_root_old, const char *s) {
1380 _cleanup_free_ char *root_new = NULL, *root_old = NULL;
1381 const char *p = s;
1382 int r;
1383
1384 assert(pivot_root_new);
1385 assert(pivot_root_old);
1386
1387 r = extract_first_word(&p, &root_new, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
1388 if (r < 0)
1389 return r;
1390 if (r == 0)
1391 return -EINVAL;
1392
1393 if (isempty(p))
1394 root_old = NULL;
1395 else {
1396 root_old = strdup(p);
1397 if (!root_old)
1398 return -ENOMEM;
1399 }
1400
1401 if (!path_is_absolute(root_new))
1402 return -EINVAL;
1403 if (root_old && !path_is_absolute(root_old))
1404 return -EINVAL;
1405
1406 free_and_replace(*pivot_root_new, root_new);
1407 free_and_replace(*pivot_root_old, root_old);
1408
1409 return 0;
1410 }
1411
1412 int setup_pivot_root(const char *directory, const char *pivot_root_new, const char *pivot_root_old) {
1413 _cleanup_free_ char *directory_pivot_root_new = NULL;
1414 _cleanup_free_ char *pivot_tmp_pivot_root_old = NULL;
1415 char pivot_tmp[] = "/tmp/nspawn-pivot-XXXXXX";
1416 bool remove_pivot_tmp = false;
1417 int r;
1418
1419 assert(directory);
1420
1421 if (!pivot_root_new)
1422 return 0;
1423
1424 /* Pivot pivot_root_new to / and the existing / to pivot_root_old.
1425 * If pivot_root_old is NULL, the existing / disappears.
1426 * This requires a temporary directory, pivot_tmp, which is
1427 * not a child of either.
1428 *
1429 * This is typically used for OSTree-style containers, where
1430 * the root partition contains several sysroots which could be
1431 * run. Normally, one would be chosen by the bootloader and
1432 * pivoted to / by initramfs.
1433 *
1434 * For example, for an OSTree deployment, pivot_root_new
1435 * would be: /ostree/deploy/$os/deploy/$checksum. Note that this
1436 * code doesn’t do the /var mount which OSTree expects: use
1437 * --bind +/sysroot/ostree/deploy/$os/var:/var for that.
1438 *
1439 * So in the OSTree case, we’ll end up with something like:
1440 * - directory = /tmp/nspawn-root-123456
1441 * - pivot_root_new = /ostree/deploy/os/deploy/123abc
1442 * - pivot_root_old = /sysroot
1443 * - directory_pivot_root_new =
1444 * /tmp/nspawn-root-123456/ostree/deploy/os/deploy/123abc
1445 * - pivot_tmp = /tmp/nspawn-pivot-123456
1446 * - pivot_tmp_pivot_root_old = /tmp/nspawn-pivot-123456/sysroot
1447 *
1448 * Requires all file systems at directory and below to be mounted
1449 * MS_PRIVATE or MS_SLAVE so they can be moved.
1450 */
1451 directory_pivot_root_new = prefix_root(directory, pivot_root_new);
1452
1453 /* Remount directory_pivot_root_new to make it movable. */
1454 r = mount_verbose(LOG_ERR, directory_pivot_root_new, directory_pivot_root_new, NULL, MS_BIND, NULL);
1455 if (r < 0)
1456 goto done;
1457
1458 if (pivot_root_old) {
1459 if (!mkdtemp(pivot_tmp)) {
1460 r = log_error_errno(errno, "Failed to create temporary directory: %m");
1461 goto done;
1462 }
1463
1464 remove_pivot_tmp = true;
1465 pivot_tmp_pivot_root_old = prefix_root(pivot_tmp, pivot_root_old);
1466
1467 r = mount_verbose(LOG_ERR, directory_pivot_root_new, pivot_tmp, NULL, MS_MOVE, NULL);
1468 if (r < 0)
1469 goto done;
1470
1471 r = mount_verbose(LOG_ERR, directory, pivot_tmp_pivot_root_old, NULL, MS_MOVE, NULL);
1472 if (r < 0)
1473 goto done;
1474
1475 r = mount_verbose(LOG_ERR, pivot_tmp, directory, NULL, MS_MOVE, NULL);
1476 if (r < 0)
1477 goto done;
1478 } else {
1479 r = mount_verbose(LOG_ERR, directory_pivot_root_new, directory, NULL, MS_MOVE, NULL);
1480 if (r < 0)
1481 goto done;
1482 }
1483
1484 done:
1485 if (remove_pivot_tmp)
1486 (void) rmdir(pivot_tmp);
1487
1488 return r;
1489 }