]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-mount.c
tree-wide: use ASSERT_PTR more
[thirdparty/systemd.git] / src / nspawn / nspawn-mount.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/mount.h>
4 #include <linux/magic.h>
5
6 #include "alloc-util.h"
7 #include "chase-symlinks.h"
8 #include "escape.h"
9 #include "fd-util.h"
10 #include "format-util.h"
11 #include "fs-util.h"
12 #include "label.h"
13 #include "mkdir-label.h"
14 #include "mount-util.h"
15 #include "mountpoint-util.h"
16 #include "nspawn-mount.h"
17 #include "parse-util.h"
18 #include "path-util.h"
19 #include "rm-rf.h"
20 #include "set.h"
21 #include "sort-util.h"
22 #include "stat-util.h"
23 #include "string-util.h"
24 #include "strv.h"
25 #include "tmpfile-util.h"
26 #include "user-util.h"
27
28 CustomMount* custom_mount_add(CustomMount **l, size_t *n, CustomMountType t) {
29 CustomMount *c, *ret;
30
31 assert(l);
32 assert(n);
33 assert(t >= 0);
34 assert(t < _CUSTOM_MOUNT_TYPE_MAX);
35
36 c = reallocarray(*l, *n + 1, sizeof(CustomMount));
37 if (!c)
38 return NULL;
39
40 *l = c;
41 ret = *l + *n;
42 (*n)++;
43
44 *ret = (CustomMount) {
45 .type = t
46 };
47
48 return ret;
49 }
50
51 void custom_mount_free_all(CustomMount *l, size_t n) {
52 for (size_t i = 0; i < n; i++) {
53 CustomMount *m = l + i;
54
55 free(m->source);
56 free(m->destination);
57 free(m->options);
58
59 if (m->work_dir) {
60 (void) rm_rf(m->work_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
61 free(m->work_dir);
62 }
63
64 if (m->rm_rf_tmpdir) {
65 (void) rm_rf(m->rm_rf_tmpdir, REMOVE_ROOT|REMOVE_PHYSICAL);
66 free(m->rm_rf_tmpdir);
67 }
68
69 strv_free(m->lower);
70 free(m->type_argument);
71 }
72
73 free(l);
74 }
75
76 static int custom_mount_compare(const CustomMount *a, const CustomMount *b) {
77 int r;
78
79 r = path_compare(a->destination, b->destination);
80 if (r != 0)
81 return r;
82
83 return CMP(a->type, b->type);
84 }
85
86 static int source_path_parse(const char *p, char **ret) {
87 assert(p);
88 assert(ret);
89
90 if (isempty(p))
91 return -EINVAL;
92
93 if (*p == '+') {
94 if (!path_is_absolute(p + 1))
95 return -EINVAL;
96
97 char *s = strdup(p);
98 if (!s)
99 return -ENOMEM;
100
101 *ret = TAKE_PTR(s);
102 return 0;
103 }
104
105 return path_make_absolute_cwd(p, ret);
106 }
107
108 static int source_path_parse_nullable(const char *p, char **ret) {
109 assert(p);
110 assert(ret);
111
112 if (isempty(p)) {
113 *ret = NULL;
114 return 0;
115 }
116
117 return source_path_parse(p, ret);
118 }
119
120 static char *resolve_source_path(const char *dest, const char *source) {
121 if (!source)
122 return NULL;
123
124 if (source[0] == '+')
125 return path_join(dest, source + 1);
126
127 return strdup(source);
128 }
129
130 static int allocate_temporary_source(CustomMount *m) {
131 assert(m);
132 assert(!m->source);
133 assert(!m->rm_rf_tmpdir);
134
135 m->rm_rf_tmpdir = strdup("/var/tmp/nspawn-temp-XXXXXX");
136 if (!m->rm_rf_tmpdir)
137 return log_oom();
138
139 if (!mkdtemp(m->rm_rf_tmpdir)) {
140 m->rm_rf_tmpdir = mfree(m->rm_rf_tmpdir);
141 return log_error_errno(errno, "Failed to acquire temporary directory: %m");
142 }
143
144 m->source = path_join(m->rm_rf_tmpdir, "src");
145 if (!m->source)
146 return log_oom();
147
148 if (mkdir(m->source, 0755) < 0)
149 return log_error_errno(errno, "Failed to create %s: %m", m->source);
150
151 return 0;
152 }
153
154 int custom_mount_prepare_all(const char *dest, CustomMount *l, size_t n) {
155 int r;
156
157 /* Prepare all custom mounts. This will make source we know all temporary directories. This is called in the
158 * parent process, so that we know the temporary directories to remove on exit before we fork off the
159 * children. */
160
161 assert(l || n == 0);
162
163 /* Order the custom mounts, and make sure we have a working directory */
164 typesafe_qsort(l, n, custom_mount_compare);
165
166 for (size_t i = 0; i < n; i++) {
167 CustomMount *m = l + i;
168
169 /* /proc we mount in the inner child, i.e. when we acquired CLONE_NEWPID. All other mounts we mount
170 * already in the outer child, so that the mounts are already established before CLONE_NEWPID and in
171 * particular CLONE_NEWUSER. This also means any custom mounts below /proc also need to be mounted in
172 * the inner child, not the outer one. Determine this here. */
173 m->in_userns = path_startswith(m->destination, "/proc");
174
175 if (m->type == CUSTOM_MOUNT_BIND) {
176 if (m->source) {
177 char *s;
178
179 s = resolve_source_path(dest, m->source);
180 if (!s)
181 return log_oom();
182
183 free_and_replace(m->source, s);
184 } else {
185 /* No source specified? In that case, use a throw-away temporary directory in /var/tmp */
186
187 r = allocate_temporary_source(m);
188 if (r < 0)
189 return r;
190 }
191 }
192
193 if (m->type == CUSTOM_MOUNT_OVERLAY) {
194 STRV_FOREACH(j, m->lower) {
195 char *s;
196
197 s = resolve_source_path(dest, *j);
198 if (!s)
199 return log_oom();
200
201 free_and_replace(*j, s);
202 }
203
204 if (m->source) {
205 char *s;
206
207 s = resolve_source_path(dest, m->source);
208 if (!s)
209 return log_oom();
210
211 free_and_replace(m->source, s);
212 } else {
213 r = allocate_temporary_source(m);
214 if (r < 0)
215 return r;
216 }
217
218 if (m->work_dir) {
219 char *s;
220
221 s = resolve_source_path(dest, m->work_dir);
222 if (!s)
223 return log_oom();
224
225 free_and_replace(m->work_dir, s);
226 } else {
227 r = tempfn_random(m->source, NULL, &m->work_dir);
228 if (r < 0)
229 return log_error_errno(r, "Failed to acquire working directory: %m");
230 }
231
232 (void) mkdir_label(m->work_dir, 0700);
233 }
234 }
235
236 return 0;
237 }
238
239 int bind_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) {
240 _cleanup_free_ char *source = NULL, *destination = NULL, *opts = NULL, *p = NULL;
241 CustomMount *m;
242 int r;
243
244 assert(l);
245 assert(n);
246
247 r = extract_many_words(&s, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &source, &destination, NULL);
248 if (r < 0)
249 return r;
250 if (r == 0)
251 return -EINVAL;
252 if (r == 1) {
253 destination = strdup(source[0] == '+' ? source+1 : source);
254 if (!destination)
255 return -ENOMEM;
256 }
257 if (r == 2 && !isempty(s)) {
258 opts = strdup(s);
259 if (!opts)
260 return -ENOMEM;
261 }
262
263 r = source_path_parse_nullable(source, &p);
264 if (r < 0)
265 return r;
266
267 if (!path_is_absolute(destination))
268 return -EINVAL;
269
270 m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND);
271 if (!m)
272 return -ENOMEM;
273
274 m->source = TAKE_PTR(p);
275 m->destination = TAKE_PTR(destination);
276 m->read_only = read_only;
277 m->options = TAKE_PTR(opts);
278
279 return 0;
280 }
281
282 int tmpfs_mount_parse(CustomMount **l, size_t *n, const char *s) {
283 _cleanup_free_ char *path = NULL, *opts = NULL;
284 const char *p = ASSERT_PTR(s);
285 CustomMount *m;
286 int r;
287
288 assert(l);
289 assert(n);
290
291 r = extract_first_word(&p, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
292 if (r < 0)
293 return r;
294 if (r == 0)
295 return -EINVAL;
296
297 if (isempty(p))
298 opts = strdup("mode=0755");
299 else
300 opts = strdup(p);
301 if (!opts)
302 return -ENOMEM;
303
304 if (!path_is_absolute(path))
305 return -EINVAL;
306
307 m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS);
308 if (!m)
309 return -ENOMEM;
310
311 m->destination = TAKE_PTR(path);
312 m->options = TAKE_PTR(opts);
313
314 return 0;
315 }
316
317 int overlay_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) {
318 _cleanup_free_ char *upper = NULL, *destination = NULL;
319 _cleanup_strv_free_ char **lower = NULL;
320 CustomMount *m;
321 int r, k;
322
323 k = strv_split_full(&lower, s, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
324 if (k < 0)
325 return k;
326 if (k < 2)
327 return -EADDRNOTAVAIL;
328 if (k == 2) {
329 _cleanup_free_ char *p = NULL;
330
331 /* If two parameters are specified, the first one is the lower, the second one the upper directory. And
332 * we'll also define the destination mount point the same as the upper. */
333
334 r = source_path_parse(lower[0], &p);
335 if (r < 0)
336 return r;
337
338 free_and_replace(lower[0], p);
339
340 r = source_path_parse(lower[1], &p);
341 if (r < 0)
342 return r;
343
344 free_and_replace(lower[1], p);
345
346 upper = TAKE_PTR(lower[1]);
347
348 destination = strdup(upper[0] == '+' ? upper+1 : upper); /* take the destination without "+" prefix */
349 if (!destination)
350 return -ENOMEM;
351 } else {
352 _cleanup_free_ char *p = NULL;
353
354 /* If more than two parameters are specified, the last one is the destination, the second to last one
355 * the "upper", and all before that the "lower" directories. */
356
357 destination = lower[k - 1];
358 upper = TAKE_PTR(lower[k - 2]);
359
360 STRV_FOREACH(i, lower) {
361 r = source_path_parse(*i, &p);
362 if (r < 0)
363 return r;
364
365 free_and_replace(*i, p);
366 }
367
368 /* If the upper directory is unspecified, then let's create it automatically as a throw-away directory
369 * in /var/tmp */
370 r = source_path_parse_nullable(upper, &p);
371 if (r < 0)
372 return r;
373
374 free_and_replace(upper, p);
375
376 if (!path_is_absolute(destination))
377 return -EINVAL;
378 }
379
380 m = custom_mount_add(l, n, CUSTOM_MOUNT_OVERLAY);
381 if (!m)
382 return -ENOMEM;
383
384 m->destination = TAKE_PTR(destination);
385 m->source = TAKE_PTR(upper);
386 m->lower = TAKE_PTR(lower);
387 m->read_only = read_only;
388
389 return 0;
390 }
391
392 int inaccessible_mount_parse(CustomMount **l, size_t *n, const char *s) {
393 _cleanup_free_ char *path = NULL;
394 CustomMount *m;
395
396 assert(l);
397 assert(n);
398 assert(s);
399
400 if (!path_is_absolute(s))
401 return -EINVAL;
402
403 path = strdup(s);
404 if (!path)
405 return -ENOMEM;
406
407 m = custom_mount_add(l, n, CUSTOM_MOUNT_INACCESSIBLE);
408 if (!m)
409 return -ENOMEM;
410
411 m->destination = TAKE_PTR(path);
412 return 0;
413 }
414
415 int tmpfs_patch_options(
416 const char *options,
417 uid_t uid_shift,
418 const char *selinux_apifs_context,
419 char **ret) {
420
421 _cleanup_free_ char *buf = NULL;
422
423 assert(ret);
424
425 if (options) {
426 buf = strdup(options);
427 if (!buf)
428 return -ENOMEM;
429 }
430
431 if (uid_shift != UID_INVALID)
432 if (strextendf_with_separator(&buf, ",", "uid=" UID_FMT ",gid=" UID_FMT, uid_shift, uid_shift) < 0)
433 return -ENOMEM;
434
435 #if HAVE_SELINUX
436 if (selinux_apifs_context)
437 if (strextendf_with_separator(&buf, ",", "context=\"%s\"", selinux_apifs_context) < 0)
438 return -ENOMEM;
439 #endif
440
441 *ret = TAKE_PTR(buf);
442 return !!*ret;
443 }
444
445 int mount_sysfs(const char *dest, MountSettingsMask mount_settings) {
446 const char *full, *top;
447 int r;
448 unsigned long extra_flags = 0;
449
450 top = prefix_roota(dest, "/sys");
451 r = path_is_fs_type(top, SYSFS_MAGIC);
452 if (r < 0)
453 return log_error_errno(r, "Failed to determine filesystem type of %s: %m", top);
454 /* /sys might already be mounted as sysfs by the outer child in the
455 * !netns case. In this case, it's all good. Don't touch it because we
456 * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555.
457 */
458 if (r > 0)
459 return 0;
460
461 full = prefix_roota(top, "/full");
462
463 (void) mkdir(full, 0755);
464
465 if (FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_RO))
466 extra_flags |= MS_RDONLY;
467
468 r = mount_nofollow_verbose(LOG_ERR, "sysfs", full, "sysfs",
469 MS_NOSUID|MS_NOEXEC|MS_NODEV|extra_flags, NULL);
470 if (r < 0)
471 return r;
472
473 FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") {
474 _cleanup_free_ char *from = NULL, *to = NULL;
475
476 from = path_join(full, x);
477 if (!from)
478 return log_oom();
479
480 to = path_join(top, x);
481 if (!to)
482 return log_oom();
483
484 (void) mkdir(to, 0755);
485
486 r = mount_nofollow_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
487 if (r < 0)
488 return r;
489
490 r = mount_nofollow_verbose(LOG_ERR, NULL, to, NULL,
491 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
492 if (r < 0)
493 return r;
494 }
495
496 r = umount_verbose(LOG_ERR, full, UMOUNT_NOFOLLOW);
497 if (r < 0)
498 return r;
499
500 if (rmdir(full) < 0)
501 return log_error_errno(errno, "Failed to remove %s: %m", full);
502
503 /* Create mountpoint for cgroups. Otherwise we are not allowed since we
504 * remount /sys read-only.
505 */
506 const char *x = prefix_roota(top, "/fs/cgroup");
507 (void) mkdir_p(x, 0755);
508
509 return mount_nofollow_verbose(LOG_ERR, NULL, top, NULL,
510 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
511 }
512
513 int mount_all(const char *dest,
514 MountSettingsMask mount_settings,
515 uid_t uid_shift,
516 const char *selinux_apifs_context) {
517
518 #define PROC_INACCESSIBLE_REG(path) \
519 { "/run/systemd/inaccessible/reg", (path), NULL, NULL, MS_BIND, \
520 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ \
521 { NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \
522 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */
523
524 #define PROC_READ_ONLY(path) \
525 { (path), (path), NULL, NULL, MS_BIND, \
526 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ \
527 { NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \
528 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */
529
530 typedef struct MountPoint {
531 const char *what;
532 const char *where;
533 const char *type;
534 const char *options;
535 unsigned long flags;
536 MountSettingsMask mount_settings;
537 } MountPoint;
538
539 static const MountPoint mount_table[] = {
540 /* First we list inner child mounts (i.e. mounts applied *after* entering user namespacing) */
541 { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
542 MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_MKDIR|MOUNT_FOLLOW_SYMLINKS }, /* we follow symlinks here since not following them requires /proc/ already being mounted, which we don't have here. */
543
544 { "/proc/sys", "/proc/sys", NULL, NULL, MS_BIND,
545 MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */
546
547 { "/proc/sys/net", "/proc/sys/net", NULL, NULL, MS_BIND,
548 MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_APIVFS_NETNS }, /* (except for this) */
549
550 { NULL, "/proc/sys", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
551 MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* ... then, make it r/o */
552
553 /* Make these files inaccessible to container payloads: they potentially leak information about kernel
554 * internals or the host's execution environment to the container */
555 PROC_INACCESSIBLE_REG("/proc/kallsyms"),
556 PROC_INACCESSIBLE_REG("/proc/kcore"),
557 PROC_INACCESSIBLE_REG("/proc/keys"),
558 PROC_INACCESSIBLE_REG("/proc/sysrq-trigger"),
559 PROC_INACCESSIBLE_REG("/proc/timer_list"),
560
561 /* Make these directories read-only to container payloads: they show hardware information, and in some
562 * cases contain tunables the container really shouldn't have access to. */
563 PROC_READ_ONLY("/proc/acpi"),
564 PROC_READ_ONLY("/proc/apm"),
565 PROC_READ_ONLY("/proc/asound"),
566 PROC_READ_ONLY("/proc/bus"),
567 PROC_READ_ONLY("/proc/fs"),
568 PROC_READ_ONLY("/proc/irq"),
569 PROC_READ_ONLY("/proc/scsi"),
570
571 { "mqueue", "/dev/mqueue", "mqueue", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
572 MOUNT_IN_USERNS|MOUNT_MKDIR },
573
574 /* Then we list outer child mounts (i.e. mounts applied *before* entering user namespacing) */
575 { "tmpfs", "/tmp", "tmpfs", "mode=1777" NESTED_TMPFS_LIMITS, MS_NOSUID|MS_NODEV|MS_STRICTATIME,
576 MOUNT_FATAL|MOUNT_APPLY_TMPFS_TMP|MOUNT_MKDIR },
577 { "tmpfs", "/sys", "tmpfs", "mode=555" TMPFS_LIMITS_SYS, MS_NOSUID|MS_NOEXEC|MS_NODEV,
578 MOUNT_FATAL|MOUNT_APPLY_APIVFS_NETNS|MOUNT_MKDIR },
579 { "sysfs", "/sys", "sysfs", NULL, MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV,
580 MOUNT_FATAL|MOUNT_APPLY_APIVFS_RO|MOUNT_MKDIR }, /* skipped if above was mounted */
581 { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
582 MOUNT_FATAL|MOUNT_MKDIR }, /* skipped if above was mounted */
583 { "tmpfs", "/dev", "tmpfs", "mode=755" TMPFS_LIMITS_PRIVATE_DEV, MS_NOSUID|MS_STRICTATIME,
584 MOUNT_FATAL|MOUNT_MKDIR },
585 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777" NESTED_TMPFS_LIMITS, MS_NOSUID|MS_NODEV|MS_STRICTATIME,
586 MOUNT_FATAL|MOUNT_MKDIR },
587 { "tmpfs", "/run", "tmpfs", "mode=755" TMPFS_LIMITS_RUN, MS_NOSUID|MS_NODEV|MS_STRICTATIME,
588 MOUNT_FATAL|MOUNT_MKDIR },
589 { "/run/host", "/run/host", NULL, NULL, MS_BIND,
590 MOUNT_FATAL|MOUNT_MKDIR|MOUNT_PREFIX_ROOT }, /* Prepare this so that we can make it read-only when we are done */
591 { "/etc/os-release", "/run/host/os-release", NULL, NULL, MS_BIND,
592 MOUNT_TOUCH }, /* As per kernel interface requirements, bind mount first (creating mount points) and make read-only later */
593 { "/usr/lib/os-release", "/run/host/os-release", NULL, NULL, MS_BIND,
594 MOUNT_FATAL }, /* If /etc/os-release doesn't exist use the version in /usr/lib as fallback */
595 { NULL, "/run/host/os-release", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
596 MOUNT_FATAL },
597 { NULL, "/run/host", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
598 MOUNT_FATAL|MOUNT_IN_USERNS },
599 #if HAVE_SELINUX
600 { "/sys/fs/selinux", "/sys/fs/selinux", NULL, NULL, MS_BIND,
601 MOUNT_MKDIR }, /* Bind mount first (mkdir/chown the mount point in case /sys/ is mounted as minimal skeleton tmpfs) */
602 { NULL, "/sys/fs/selinux", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
603 0 }, /* Then, make it r/o (don't mkdir/chown the mount point here, the previous entry already did that) */
604 #endif
605 };
606
607 bool use_userns = FLAGS_SET(mount_settings, MOUNT_USE_USERNS);
608 bool netns = FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_NETNS);
609 bool ro = FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_RO);
610 bool in_userns = FLAGS_SET(mount_settings, MOUNT_IN_USERNS);
611 bool tmpfs_tmp = FLAGS_SET(mount_settings, MOUNT_APPLY_TMPFS_TMP);
612 int r;
613
614 for (size_t k = 0; k < ELEMENTSOF(mount_table); k++) {
615 _cleanup_free_ char *where = NULL, *options = NULL, *prefixed = NULL;
616 bool fatal = FLAGS_SET(mount_table[k].mount_settings, MOUNT_FATAL);
617 const char *o;
618
619 if (in_userns != FLAGS_SET(mount_table[k].mount_settings, MOUNT_IN_USERNS))
620 continue;
621
622 if (!netns && FLAGS_SET(mount_table[k].mount_settings, MOUNT_APPLY_APIVFS_NETNS))
623 continue;
624
625 if (!ro && FLAGS_SET(mount_table[k].mount_settings, MOUNT_APPLY_APIVFS_RO))
626 continue;
627
628 if (!tmpfs_tmp && FLAGS_SET(mount_table[k].mount_settings, MOUNT_APPLY_TMPFS_TMP))
629 continue;
630
631 r = chase_symlinks(mount_table[k].where, dest, CHASE_NONEXISTENT|CHASE_PREFIX_ROOT, &where, NULL);
632 if (r < 0)
633 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, mount_table[k].where);
634
635 /* Skip this entry if it is not a remount. */
636 if (mount_table[k].what) {
637 r = path_is_mount_point(where, NULL, 0);
638 if (r < 0 && r != -ENOENT)
639 return log_error_errno(r, "Failed to detect whether %s is a mount point: %m", where);
640 if (r > 0)
641 continue;
642 }
643
644 if ((mount_table[k].mount_settings & (MOUNT_MKDIR|MOUNT_TOUCH)) != 0) {
645 uid_t u = (use_userns && !in_userns) ? uid_shift : UID_INVALID;
646
647 if (FLAGS_SET(mount_table[k].mount_settings, MOUNT_TOUCH))
648 r = mkdir_parents_safe(dest, where, 0755, u, u, 0);
649 else
650 r = mkdir_p_safe(dest, where, 0755, u, u, 0);
651 if (r < 0 && r != -EEXIST) {
652 if (fatal && r != -EROFS)
653 return log_error_errno(r, "Failed to create directory %s: %m", where);
654
655 log_debug_errno(r, "Failed to create directory %s: %m", where);
656
657 /* If we failed mkdir() or chown() due to the root directory being read only,
658 * attempt to mount this fs anyway and let mount_verbose log any errors */
659 if (r != -EROFS)
660 continue;
661 }
662 }
663
664 if (FLAGS_SET(mount_table[k].mount_settings, MOUNT_TOUCH)) {
665 r = touch(where);
666 if (r < 0 && r != -EEXIST) {
667 if (fatal && r != -EROFS)
668 return log_error_errno(r, "Failed to create file %s: %m", where);
669
670 log_debug_errno(r, "Failed to create file %s: %m", where);
671 if (r != -EROFS)
672 continue;
673 }
674 }
675
676 o = mount_table[k].options;
677 if (streq_ptr(mount_table[k].type, "tmpfs")) {
678 r = tmpfs_patch_options(o, in_userns ? 0 : uid_shift, selinux_apifs_context, &options);
679 if (r < 0)
680 return log_oom();
681 if (r > 0)
682 o = options;
683 }
684
685 if (FLAGS_SET(mount_table[k].mount_settings, MOUNT_PREFIX_ROOT)) {
686 /* Optionally prefix the mount source with the root dir. This is useful in bind
687 * mounts to be created within the container image before we transition into it. Note
688 * that MOUNT_IN_USERNS is run after we transitioned hence prefixing is not ncessary
689 * for those. */
690 r = chase_symlinks(mount_table[k].what, dest, CHASE_PREFIX_ROOT, &prefixed, NULL);
691 if (r < 0)
692 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, mount_table[k].what);
693 }
694
695 r = mount_verbose_full(
696 fatal ? LOG_ERR : LOG_DEBUG,
697 prefixed ?: mount_table[k].what,
698 where,
699 mount_table[k].type,
700 mount_table[k].flags,
701 o,
702 FLAGS_SET(mount_table[k].mount_settings, MOUNT_FOLLOW_SYMLINKS));
703 if (r < 0 && fatal)
704 return r;
705 }
706
707 return 0;
708 }
709
710 static int parse_mount_bind_options(const char *options, unsigned long *mount_flags, char **mount_opts, RemountIdmapping *idmapping) {
711 unsigned long flags = *mount_flags;
712 char *opts = NULL;
713 RemountIdmapping new_idmapping = *idmapping;
714 int r;
715
716 assert(options);
717
718 for (;;) {
719 _cleanup_free_ char *word = NULL;
720
721 r = extract_first_word(&options, &word, ",", 0);
722 if (r < 0)
723 return log_error_errno(r, "Failed to extract mount option: %m");
724 if (r == 0)
725 break;
726
727 if (streq(word, "rbind"))
728 flags |= MS_REC;
729 else if (streq(word, "norbind"))
730 flags &= ~MS_REC;
731 else if (streq(word, "idmap"))
732 new_idmapping = REMOUNT_IDMAPPING_HOST_ROOT;
733 else if (streq(word, "noidmap"))
734 new_idmapping = REMOUNT_IDMAPPING_NONE;
735 else if (streq(word, "rootidmap"))
736 new_idmapping = REMOUNT_IDMAPPING_HOST_OWNER;
737 else
738 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
739 "Invalid bind mount option: %s", word);
740 }
741
742 *mount_flags = flags;
743 *idmapping = new_idmapping;
744 /* in the future mount_opts will hold string options for mount(2) */
745 *mount_opts = opts;
746
747 return 0;
748 }
749
750 static int mount_bind(const char *dest, CustomMount *m, uid_t uid_shift, uid_t uid_range) {
751 _cleanup_free_ char *mount_opts = NULL, *where = NULL;
752 unsigned long mount_flags = MS_BIND | MS_REC;
753 struct stat source_st, dest_st;
754 int r;
755 RemountIdmapping idmapping = REMOUNT_IDMAPPING_NONE;
756
757 assert(dest);
758 assert(m);
759
760 if (m->options) {
761 r = parse_mount_bind_options(m->options, &mount_flags, &mount_opts, &idmapping);
762 if (r < 0)
763 return r;
764 }
765
766 /* If this is a bind mount from a temporary sources change ownership of the source to the container's
767 * root UID. Otherwise it would always show up as "nobody" if user namespacing is used. */
768 if (m->rm_rf_tmpdir && chown(m->source, uid_shift, uid_shift) < 0)
769 return log_error_errno(errno, "Failed to chown %s: %m", m->source);
770
771 if (stat(m->source, &source_st) < 0)
772 return log_error_errno(errno, "Failed to stat %s: %m", m->source);
773
774 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
775 if (r < 0)
776 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
777 if (r > 0) { /* Path exists already? */
778
779 if (stat(where, &dest_st) < 0)
780 return log_error_errno(errno, "Failed to stat %s: %m", where);
781
782 if (S_ISDIR(source_st.st_mode) && !S_ISDIR(dest_st.st_mode))
783 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
784 "Cannot bind mount directory %s on file %s.",
785 m->source, where);
786
787 if (!S_ISDIR(source_st.st_mode) && S_ISDIR(dest_st.st_mode))
788 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
789 "Cannot bind mount file %s on directory %s.",
790 m->source, where);
791
792 } else { /* Path doesn't exist yet? */
793 r = mkdir_parents_label(where, 0755);
794 if (r < 0)
795 return log_error_errno(r, "Failed to make parents of %s: %m", where);
796
797 /* Create the mount point. Any non-directory file can be
798 * mounted on any non-directory file (regular, fifo, socket,
799 * char, block).
800 */
801 if (S_ISDIR(source_st.st_mode))
802 r = mkdir_label(where, 0755);
803 else
804 r = touch(where);
805 if (r < 0)
806 return log_error_errno(r, "Failed to create mount point %s: %m", where);
807 }
808
809 r = mount_nofollow_verbose(LOG_ERR, m->source, where, NULL, mount_flags, mount_opts);
810 if (r < 0)
811 return r;
812
813 if (m->read_only) {
814 r = bind_remount_recursive(where, MS_RDONLY, MS_RDONLY, NULL);
815 if (r < 0)
816 return log_error_errno(r, "Read-only bind mount failed: %m");
817 }
818
819 if (idmapping != REMOUNT_IDMAPPING_NONE) {
820 r = remount_idmap(where, uid_shift, uid_range, source_st.st_uid, idmapping);
821 if (r < 0)
822 return log_error_errno(r, "Failed to map ids for bind mount %s: %m", where);
823 }
824
825 return 0;
826 }
827
828 static int mount_tmpfs(const char *dest, CustomMount *m, uid_t uid_shift, const char *selinux_apifs_context) {
829 const char *options;
830 _cleanup_free_ char *buf = NULL, *where = NULL;
831 int r;
832
833 assert(dest);
834 assert(m);
835
836 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
837 if (r < 0)
838 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
839 if (r == 0) { /* Doesn't exist yet? */
840 r = mkdir_p_label(where, 0755);
841 if (r < 0)
842 return log_error_errno(r, "Creating mount point for tmpfs %s failed: %m", where);
843 }
844
845 r = tmpfs_patch_options(m->options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
846 if (r < 0)
847 return log_oom();
848 options = r > 0 ? buf : m->options;
849
850 return mount_nofollow_verbose(LOG_ERR, "tmpfs", where, "tmpfs", MS_NODEV|MS_STRICTATIME, options);
851 }
852
853 static char *joined_and_escaped_lower_dirs(char **lower) {
854 _cleanup_strv_free_ char **sv = NULL;
855
856 sv = strv_copy(lower);
857 if (!sv)
858 return NULL;
859
860 strv_reverse(sv);
861
862 if (!strv_shell_escape(sv, ",:"))
863 return NULL;
864
865 return strv_join(sv, ":");
866 }
867
868 static int mount_overlay(const char *dest, CustomMount *m) {
869 _cleanup_free_ char *lower = NULL, *where = NULL, *escaped_source = NULL;
870 const char *options;
871 int r;
872
873 assert(dest);
874 assert(m);
875
876 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
877 if (r < 0)
878 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
879 if (r == 0) { /* Doesn't exist yet? */
880 r = mkdir_label(where, 0755);
881 if (r < 0)
882 return log_error_errno(r, "Creating mount point for overlay %s failed: %m", where);
883 }
884
885 (void) mkdir_p_label(m->source, 0755);
886
887 lower = joined_and_escaped_lower_dirs(m->lower);
888 if (!lower)
889 return log_oom();
890
891 escaped_source = shell_escape(m->source, ",:");
892 if (!escaped_source)
893 return log_oom();
894
895 if (m->read_only)
896 options = strjoina("lowerdir=", escaped_source, ":", lower);
897 else {
898 _cleanup_free_ char *escaped_work_dir = NULL;
899
900 escaped_work_dir = shell_escape(m->work_dir, ",:");
901 if (!escaped_work_dir)
902 return log_oom();
903
904 options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir);
905 }
906
907 return mount_nofollow_verbose(LOG_ERR, "overlay", where, "overlay", m->read_only ? MS_RDONLY : 0, options);
908 }
909
910 static int mount_inaccessible(const char *dest, CustomMount *m) {
911 _cleanup_free_ char *where = NULL, *source = NULL;
912 struct stat st;
913 int r;
914
915 assert(dest);
916 assert(m);
917
918 r = chase_symlinks_and_stat(m->destination, dest, CHASE_PREFIX_ROOT, &where, &st, NULL);
919 if (r < 0) {
920 log_full_errno(m->graceful ? LOG_DEBUG : LOG_ERR, r, "Failed to resolve %s/%s: %m", dest, m->destination);
921 return m->graceful ? 0 : r;
922 }
923
924 r = mode_to_inaccessible_node(NULL, st.st_mode, &source);
925 if (r < 0)
926 return m->graceful ? 0 : r;
927
928 r = mount_nofollow_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, source, where, NULL, MS_BIND, NULL);
929 if (r < 0)
930 return m->graceful ? 0 : r;
931
932 r = mount_nofollow_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, NULL, where, NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, NULL);
933 if (r < 0) {
934 (void) umount_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, where, UMOUNT_NOFOLLOW);
935 return m->graceful ? 0 : r;
936 }
937
938 return 0;
939 }
940
941 static int mount_arbitrary(const char *dest, CustomMount *m) {
942 _cleanup_free_ char *where = NULL;
943 int r;
944
945 assert(dest);
946 assert(m);
947
948 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
949 if (r < 0)
950 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
951 if (r == 0) { /* Doesn't exist yet? */
952 r = mkdir_p_label(where, 0755);
953 if (r < 0)
954 return log_error_errno(r, "Creating mount point for mount %s failed: %m", where);
955 }
956
957 return mount_nofollow_verbose(LOG_ERR, m->source, where, m->type_argument, 0, m->options);
958 }
959
960 int mount_custom(
961 const char *dest,
962 CustomMount *mounts, size_t n,
963 uid_t uid_shift,
964 uid_t uid_range,
965 const char *selinux_apifs_context,
966 MountSettingsMask mount_settings) {
967 int r;
968
969 assert(dest);
970
971 for (size_t i = 0; i < n; i++) {
972 CustomMount *m = mounts + i;
973
974 if (FLAGS_SET(mount_settings, MOUNT_IN_USERNS) != m->in_userns)
975 continue;
976
977 if (FLAGS_SET(mount_settings, MOUNT_ROOT_ONLY) && !path_equal(m->destination, "/"))
978 continue;
979
980 if (FLAGS_SET(mount_settings, MOUNT_NON_ROOT_ONLY) && path_equal(m->destination, "/"))
981 continue;
982
983 switch (m->type) {
984
985 case CUSTOM_MOUNT_BIND:
986 r = mount_bind(dest, m, uid_shift, uid_range);
987 break;
988
989 case CUSTOM_MOUNT_TMPFS:
990 r = mount_tmpfs(dest, m, uid_shift, selinux_apifs_context);
991 break;
992
993 case CUSTOM_MOUNT_OVERLAY:
994 r = mount_overlay(dest, m);
995 break;
996
997 case CUSTOM_MOUNT_INACCESSIBLE:
998 r = mount_inaccessible(dest, m);
999 break;
1000
1001 case CUSTOM_MOUNT_ARBITRARY:
1002 r = mount_arbitrary(dest, m);
1003 break;
1004
1005 default:
1006 assert_not_reached();
1007 }
1008
1009 if (r < 0)
1010 return r;
1011 }
1012
1013 return 0;
1014 }
1015
1016 bool has_custom_root_mount(const CustomMount *mounts, size_t n) {
1017 for (size_t i = 0; i < n; i++)
1018 if (path_equal(mounts[i].destination, "/"))
1019 return true;
1020
1021 return false;
1022 }
1023
1024 static int setup_volatile_state(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
1025 _cleanup_free_ char *buf = NULL;
1026 const char *p, *options;
1027 int r;
1028
1029 assert(directory);
1030
1031 /* --volatile=state means we simply overmount /var with a tmpfs, and the rest read-only. */
1032
1033 r = bind_remount_recursive(directory, MS_RDONLY, MS_RDONLY, NULL);
1034 if (r < 0)
1035 return log_error_errno(r, "Failed to remount %s read-only: %m", directory);
1036
1037 p = prefix_roota(directory, "/var");
1038 r = mkdir(p, 0755);
1039 if (r < 0 && errno != EEXIST)
1040 return log_error_errno(errno, "Failed to create %s: %m", directory);
1041
1042 options = "mode=755" TMPFS_LIMITS_VOLATILE_STATE;
1043 r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
1044 if (r < 0)
1045 return log_oom();
1046 if (r > 0)
1047 options = buf;
1048
1049 return mount_nofollow_verbose(LOG_ERR, "tmpfs", p, "tmpfs", MS_STRICTATIME, options);
1050 }
1051
1052 static int setup_volatile_yes(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
1053 bool tmpfs_mounted = false, bind_mounted = false;
1054 char template[] = "/tmp/nspawn-volatile-XXXXXX";
1055 _cleanup_free_ char *buf = NULL, *bindir = NULL;
1056 const char *f, *t, *options;
1057 struct stat st;
1058 int r;
1059
1060 assert(directory);
1061
1062 /* --volatile=yes means we mount a tmpfs to the root dir, and the original /usr to use inside it, and
1063 * that read-only. Before we start setting this up let's validate if the image has the /usr merge
1064 * implemented, and let's output a friendly log message if it hasn't. */
1065
1066 bindir = path_join(directory, "/bin");
1067 if (!bindir)
1068 return log_oom();
1069 if (lstat(bindir, &st) < 0) {
1070 if (errno != ENOENT)
1071 return log_error_errno(errno, "Failed to stat /bin directory below image: %m");
1072
1073 /* ENOENT is fine, just means the image is probably just a naked /usr and we can create the
1074 * rest. */
1075 } else if (S_ISDIR(st.st_mode))
1076 return log_error_errno(SYNTHETIC_ERRNO(EISDIR),
1077 "Sorry, --volatile=yes mode is not supported with OS images that have not merged /bin/, /sbin/, /lib/, /lib64/ into /usr/. "
1078 "Please work with your distribution and help them adopt the merged /usr scheme.");
1079 else if (!S_ISLNK(st.st_mode))
1080 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1081 "Error starting image: if --volatile=yes is used /bin must be a symlink (for merged /usr support) or non-existent (in which case a symlink is created automatically).");
1082
1083 if (!mkdtemp(template))
1084 return log_error_errno(errno, "Failed to create temporary directory: %m");
1085
1086 options = "mode=755" TMPFS_LIMITS_ROOTFS;
1087 r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
1088 if (r < 0)
1089 goto fail;
1090 if (r > 0)
1091 options = buf;
1092
1093 r = mount_nofollow_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
1094 if (r < 0)
1095 goto fail;
1096
1097 tmpfs_mounted = true;
1098
1099 f = prefix_roota(directory, "/usr");
1100 t = prefix_roota(template, "/usr");
1101
1102 r = mkdir(t, 0755);
1103 if (r < 0 && errno != EEXIST) {
1104 r = log_error_errno(errno, "Failed to create %s: %m", t);
1105 goto fail;
1106 }
1107
1108 r = mount_nofollow_verbose(LOG_ERR, f, t, NULL, MS_BIND|MS_REC, NULL);
1109 if (r < 0)
1110 goto fail;
1111
1112 bind_mounted = true;
1113
1114 r = bind_remount_recursive(t, MS_RDONLY, MS_RDONLY, NULL);
1115 if (r < 0) {
1116 log_error_errno(r, "Failed to remount %s read-only: %m", t);
1117 goto fail;
1118 }
1119
1120 r = mount_nofollow_verbose(LOG_ERR, template, directory, NULL, MS_MOVE, NULL);
1121 if (r < 0)
1122 goto fail;
1123
1124 (void) rmdir(template);
1125
1126 return 0;
1127
1128 fail:
1129 if (bind_mounted)
1130 (void) umount_verbose(LOG_ERR, t, UMOUNT_NOFOLLOW);
1131
1132 if (tmpfs_mounted)
1133 (void) umount_verbose(LOG_ERR, template, UMOUNT_NOFOLLOW);
1134
1135 (void) rmdir(template);
1136 return r;
1137 }
1138
1139 static int setup_volatile_overlay(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
1140 _cleanup_free_ char *buf = NULL, *escaped_directory = NULL, *escaped_upper = NULL, *escaped_work = NULL;
1141 char template[] = "/tmp/nspawn-volatile-XXXXXX";
1142 const char *upper, *work, *options;
1143 bool tmpfs_mounted = false;
1144 int r;
1145
1146 assert(directory);
1147
1148 /* --volatile=overlay means we mount an overlayfs to the root dir. */
1149
1150 if (!mkdtemp(template))
1151 return log_error_errno(errno, "Failed to create temporary directory: %m");
1152
1153 options = "mode=755" TMPFS_LIMITS_ROOTFS;
1154 r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
1155 if (r < 0)
1156 goto finish;
1157 if (r > 0)
1158 options = buf;
1159
1160 r = mount_nofollow_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
1161 if (r < 0)
1162 goto finish;
1163
1164 tmpfs_mounted = true;
1165
1166 upper = strjoina(template, "/upper");
1167 work = strjoina(template, "/work");
1168
1169 if (mkdir(upper, 0755) < 0) {
1170 r = log_error_errno(errno, "Failed to create %s: %m", upper);
1171 goto finish;
1172 }
1173 if (mkdir(work, 0755) < 0) {
1174 r = log_error_errno(errno, "Failed to create %s: %m", work);
1175 goto finish;
1176 }
1177
1178 /* And now, let's overmount the root dir with an overlayfs that uses the root dir as lower dir. It's kinda nice
1179 * that the kernel allows us to do that without going through some mount point rearrangements. */
1180
1181 escaped_directory = shell_escape(directory, ",:");
1182 escaped_upper = shell_escape(upper, ",:");
1183 escaped_work = shell_escape(work, ",:");
1184 if (!escaped_directory || !escaped_upper || !escaped_work) {
1185 r = -ENOMEM;
1186 goto finish;
1187 }
1188
1189 options = strjoina("lowerdir=", escaped_directory, ",upperdir=", escaped_upper, ",workdir=", escaped_work);
1190 r = mount_nofollow_verbose(LOG_ERR, "overlay", directory, "overlay", 0, options);
1191
1192 finish:
1193 if (tmpfs_mounted)
1194 (void) umount_verbose(LOG_ERR, template, UMOUNT_NOFOLLOW);
1195
1196 (void) rmdir(template);
1197 return r;
1198 }
1199
1200 int setup_volatile_mode(
1201 const char *directory,
1202 VolatileMode mode,
1203 uid_t uid_shift,
1204 const char *selinux_apifs_context) {
1205
1206 switch (mode) {
1207
1208 case VOLATILE_YES:
1209 return setup_volatile_yes(directory, uid_shift, selinux_apifs_context);
1210
1211 case VOLATILE_STATE:
1212 return setup_volatile_state(directory, uid_shift, selinux_apifs_context);
1213
1214 case VOLATILE_OVERLAY:
1215 return setup_volatile_overlay(directory, uid_shift, selinux_apifs_context);
1216
1217 default:
1218 return 0;
1219 }
1220 }
1221
1222 /* Expects *pivot_root_new and *pivot_root_old to be initialised to allocated memory or NULL. */
1223 int pivot_root_parse(char **pivot_root_new, char **pivot_root_old, const char *s) {
1224 _cleanup_free_ char *root_new = NULL, *root_old = NULL;
1225 const char *p = s;
1226 int r;
1227
1228 assert(pivot_root_new);
1229 assert(pivot_root_old);
1230
1231 r = extract_first_word(&p, &root_new, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
1232 if (r < 0)
1233 return r;
1234 if (r == 0)
1235 return -EINVAL;
1236
1237 if (isempty(p))
1238 root_old = NULL;
1239 else {
1240 root_old = strdup(p);
1241 if (!root_old)
1242 return -ENOMEM;
1243 }
1244
1245 if (!path_is_absolute(root_new))
1246 return -EINVAL;
1247 if (root_old && !path_is_absolute(root_old))
1248 return -EINVAL;
1249
1250 free_and_replace(*pivot_root_new, root_new);
1251 free_and_replace(*pivot_root_old, root_old);
1252
1253 return 0;
1254 }
1255
1256 int setup_pivot_root(const char *directory, const char *pivot_root_new, const char *pivot_root_old) {
1257 _cleanup_free_ char *directory_pivot_root_new = NULL;
1258 _cleanup_free_ char *pivot_tmp_pivot_root_old = NULL;
1259 char pivot_tmp[] = "/tmp/nspawn-pivot-XXXXXX";
1260 bool remove_pivot_tmp = false;
1261 int r;
1262
1263 assert(directory);
1264
1265 if (!pivot_root_new)
1266 return 0;
1267
1268 /* Pivot pivot_root_new to / and the existing / to pivot_root_old.
1269 * If pivot_root_old is NULL, the existing / disappears.
1270 * This requires a temporary directory, pivot_tmp, which is
1271 * not a child of either.
1272 *
1273 * This is typically used for OSTree-style containers, where
1274 * the root partition contains several sysroots which could be
1275 * run. Normally, one would be chosen by the bootloader and
1276 * pivoted to / by initramfs.
1277 *
1278 * For example, for an OSTree deployment, pivot_root_new
1279 * would be: /ostree/deploy/$os/deploy/$checksum. Note that this
1280 * code doesn’t do the /var mount which OSTree expects: use
1281 * --bind +/sysroot/ostree/deploy/$os/var:/var for that.
1282 *
1283 * So in the OSTree case, we’ll end up with something like:
1284 * - directory = /tmp/nspawn-root-123456
1285 * - pivot_root_new = /ostree/deploy/os/deploy/123abc
1286 * - pivot_root_old = /sysroot
1287 * - directory_pivot_root_new =
1288 * /tmp/nspawn-root-123456/ostree/deploy/os/deploy/123abc
1289 * - pivot_tmp = /tmp/nspawn-pivot-123456
1290 * - pivot_tmp_pivot_root_old = /tmp/nspawn-pivot-123456/sysroot
1291 *
1292 * Requires all file systems at directory and below to be mounted
1293 * MS_PRIVATE or MS_SLAVE so they can be moved.
1294 */
1295 directory_pivot_root_new = path_join(directory, pivot_root_new);
1296 if (!directory_pivot_root_new)
1297 return log_oom();
1298
1299 /* Remount directory_pivot_root_new to make it movable. */
1300 r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, directory_pivot_root_new, NULL, MS_BIND, NULL);
1301 if (r < 0)
1302 goto done;
1303
1304 if (pivot_root_old) {
1305 if (!mkdtemp(pivot_tmp)) {
1306 r = log_error_errno(errno, "Failed to create temporary directory: %m");
1307 goto done;
1308 }
1309
1310 remove_pivot_tmp = true;
1311 pivot_tmp_pivot_root_old = path_join(pivot_tmp, pivot_root_old);
1312 if (!pivot_tmp_pivot_root_old) {
1313 r = log_oom();
1314 goto done;
1315 }
1316
1317 r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, pivot_tmp, NULL, MS_MOVE, NULL);
1318 if (r < 0)
1319 goto done;
1320
1321 r = mount_nofollow_verbose(LOG_ERR, directory, pivot_tmp_pivot_root_old, NULL, MS_MOVE, NULL);
1322 if (r < 0)
1323 goto done;
1324
1325 r = mount_nofollow_verbose(LOG_ERR, pivot_tmp, directory, NULL, MS_MOVE, NULL);
1326 if (r < 0)
1327 goto done;
1328 } else {
1329 r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, directory, NULL, MS_MOVE, NULL);
1330 if (r < 0)
1331 goto done;
1332 }
1333
1334 done:
1335 if (remove_pivot_tmp)
1336 (void) rmdir(pivot_tmp);
1337
1338 return r;
1339 }