1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <linux/magic.h>
6 #include "alloc-util.h"
7 #include "chase-symlinks.h"
10 #include "format-util.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"
21 #include "sort-util.h"
22 #include "stat-util.h"
23 #include "string-util.h"
25 #include "tmpfile-util.h"
26 #include "user-util.h"
28 CustomMount
* custom_mount_add(CustomMount
**l
, size_t *n
, CustomMountType t
) {
34 assert(t
< _CUSTOM_MOUNT_TYPE_MAX
);
36 c
= reallocarray(*l
, *n
+ 1, sizeof(CustomMount
));
44 *ret
= (CustomMount
) {
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
;
60 (void) rm_rf(m
->work_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
64 if (m
->rm_rf_tmpdir
) {
65 (void) rm_rf(m
->rm_rf_tmpdir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
66 free(m
->rm_rf_tmpdir
);
70 free(m
->type_argument
);
76 static int custom_mount_compare(const CustomMount
*a
, const CustomMount
*b
) {
79 r
= path_compare(a
->destination
, b
->destination
);
83 return CMP(a
->type
, b
->type
);
86 static int source_path_parse(const char *p
, char **ret
) {
94 if (!path_is_absolute(p
+ 1))
105 return path_make_absolute_cwd(p
, ret
);
108 static int source_path_parse_nullable(const char *p
, char **ret
) {
117 return source_path_parse(p
, ret
);
120 static char *resolve_source_path(const char *dest
, const char *source
) {
124 if (source
[0] == '+')
125 return path_join(dest
, source
+ 1);
127 return strdup(source
);
130 static int allocate_temporary_source(CustomMount
*m
) {
133 assert(!m
->rm_rf_tmpdir
);
135 m
->rm_rf_tmpdir
= strdup("/var/tmp/nspawn-temp-XXXXXX");
136 if (!m
->rm_rf_tmpdir
)
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");
144 m
->source
= path_join(m
->rm_rf_tmpdir
, "src");
148 if (mkdir(m
->source
, 0755) < 0)
149 return log_error_errno(errno
, "Failed to create %s: %m", m
->source
);
154 int custom_mount_prepare_all(const char *dest
, CustomMount
*l
, size_t n
) {
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
163 /* Order the custom mounts, and make sure we have a working directory */
164 typesafe_qsort(l
, n
, custom_mount_compare
);
166 for (size_t i
= 0; i
< n
; i
++) {
167 CustomMount
*m
= l
+ i
;
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");
175 if (m
->type
== CUSTOM_MOUNT_BIND
) {
179 s
= resolve_source_path(dest
, m
->source
);
183 free_and_replace(m
->source
, s
);
185 /* No source specified? In that case, use a throw-away temporary directory in /var/tmp */
187 r
= allocate_temporary_source(m
);
193 if (m
->type
== CUSTOM_MOUNT_OVERLAY
) {
194 STRV_FOREACH(j
, m
->lower
) {
197 s
= resolve_source_path(dest
, *j
);
201 free_and_replace(*j
, s
);
207 s
= resolve_source_path(dest
, m
->source
);
211 free_and_replace(m
->source
, s
);
213 r
= allocate_temporary_source(m
);
221 s
= resolve_source_path(dest
, m
->work_dir
);
225 free_and_replace(m
->work_dir
, s
);
227 r
= tempfn_random(m
->source
, NULL
, &m
->work_dir
);
229 return log_error_errno(r
, "Failed to acquire working directory: %m");
232 (void) mkdir_label(m
->work_dir
, 0700);
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
;
247 r
= extract_many_words(&s
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
, &source
, &destination
, NULL
);
253 destination
= strdup(source
[0] == '+' ? source
+1 : source
);
257 if (r
== 2 && !isempty(s
)) {
263 r
= source_path_parse_nullable(source
, &p
);
267 if (!path_is_absolute(destination
))
270 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_BIND
);
274 m
->source
= TAKE_PTR(p
);
275 m
->destination
= TAKE_PTR(destination
);
276 m
->read_only
= read_only
;
277 m
->options
= TAKE_PTR(opts
);
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
);
291 r
= extract_first_word(&p
, &path
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
298 opts
= strdup("mode=0755");
304 if (!path_is_absolute(path
))
307 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_TMPFS
);
311 m
->destination
= TAKE_PTR(path
);
312 m
->options
= TAKE_PTR(opts
);
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
;
323 k
= strv_split_full(&lower
, s
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
327 return -EADDRNOTAVAIL
;
329 _cleanup_free_
char *p
= NULL
;
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. */
334 r
= source_path_parse(lower
[0], &p
);
338 free_and_replace(lower
[0], p
);
340 r
= source_path_parse(lower
[1], &p
);
344 free_and_replace(lower
[1], p
);
346 upper
= TAKE_PTR(lower
[1]);
348 destination
= strdup(upper
[0] == '+' ? upper
+1 : upper
); /* take the destination without "+" prefix */
352 _cleanup_free_
char *p
= NULL
;
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. */
357 destination
= lower
[k
- 1];
358 upper
= TAKE_PTR(lower
[k
- 2]);
360 STRV_FOREACH(i
, lower
) {
361 r
= source_path_parse(*i
, &p
);
365 free_and_replace(*i
, p
);
368 /* If the upper directory is unspecified, then let's create it automatically as a throw-away directory
370 r
= source_path_parse_nullable(upper
, &p
);
374 free_and_replace(upper
, p
);
376 if (!path_is_absolute(destination
))
380 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_OVERLAY
);
384 m
->destination
= TAKE_PTR(destination
);
385 m
->source
= TAKE_PTR(upper
);
386 m
->lower
= TAKE_PTR(lower
);
387 m
->read_only
= read_only
;
392 int inaccessible_mount_parse(CustomMount
**l
, size_t *n
, const char *s
) {
393 _cleanup_free_
char *path
= NULL
;
400 if (!path_is_absolute(s
))
407 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_INACCESSIBLE
);
411 m
->destination
= TAKE_PTR(path
);
415 int tmpfs_patch_options(
418 const char *selinux_apifs_context
,
421 _cleanup_free_
char *buf
= NULL
;
426 buf
= strdup(options
);
431 if (uid_shift
!= UID_INVALID
)
432 if (strextendf_with_separator(&buf
, ",", "uid=" UID_FMT
",gid=" UID_FMT
, uid_shift
, uid_shift
) < 0)
436 if (selinux_apifs_context
)
437 if (strextendf_with_separator(&buf
, ",", "context=\"%s\"", selinux_apifs_context
) < 0)
441 *ret
= TAKE_PTR(buf
);
445 int mount_sysfs(const char *dest
, MountSettingsMask mount_settings
) {
446 const char *full
, *top
;
448 unsigned long extra_flags
= 0;
450 top
= prefix_roota(dest
, "/sys");
451 r
= path_is_fs_type(top
, SYSFS_MAGIC
);
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.
461 full
= prefix_roota(top
, "/full");
463 (void) mkdir(full
, 0755);
465 if (FLAGS_SET(mount_settings
, MOUNT_APPLY_APIVFS_RO
))
466 extra_flags
|= MS_RDONLY
;
468 r
= mount_nofollow_verbose(LOG_ERR
, "sysfs", full
, "sysfs",
469 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|extra_flags
, NULL
);
473 FOREACH_STRING(x
, "block", "bus", "class", "dev", "devices", "kernel") {
474 _cleanup_free_
char *from
= NULL
, *to
= NULL
;
476 from
= path_join(full
, x
);
480 to
= path_join(top
, x
);
484 (void) mkdir(to
, 0755);
486 r
= mount_nofollow_verbose(LOG_ERR
, from
, to
, NULL
, MS_BIND
, NULL
);
490 r
= mount_nofollow_verbose(LOG_ERR
, NULL
, to
, NULL
,
491 MS_BIND
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
|extra_flags
, NULL
);
496 r
= umount_verbose(LOG_ERR
, full
, UMOUNT_NOFOLLOW
);
501 return log_error_errno(errno
, "Failed to remove %s: %m", full
);
503 /* Create mountpoint for cgroups. Otherwise we are not allowed since we
504 * remount /sys read-only.
506 const char *x
= prefix_roota(top
, "/fs/cgroup");
507 (void) mkdir_p(x
, 0755);
509 return mount_nofollow_verbose(LOG_ERR
, NULL
, top
, NULL
,
510 MS_BIND
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
|extra_flags
, NULL
);
513 int mount_all(const char *dest
,
514 MountSettingsMask mount_settings
,
516 const char *selinux_apifs_context
) {
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 */
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 */
530 typedef struct MountPoint
{
536 MountSettingsMask mount_settings
;
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. */
544 { "/proc/sys", "/proc/sys", NULL
, NULL
, MS_BIND
,
545 MOUNT_FATAL
|MOUNT_IN_USERNS
|MOUNT_APPLY_APIVFS_RO
}, /* Bind mount first ... */
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) */
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 */
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"),
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"),
571 { "mqueue", "/dev/mqueue", "mqueue", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
572 MOUNT_IN_USERNS
|MOUNT_MKDIR
},
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
,
597 { NULL
, "/run/host", NULL
, NULL
, MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
,
598 MOUNT_FATAL
|MOUNT_IN_USERNS
},
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) */
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
);
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
);
619 if (in_userns
!= FLAGS_SET(mount_table
[k
].mount_settings
, MOUNT_IN_USERNS
))
622 if (!netns
&& FLAGS_SET(mount_table
[k
].mount_settings
, MOUNT_APPLY_APIVFS_NETNS
))
625 if (!ro
&& FLAGS_SET(mount_table
[k
].mount_settings
, MOUNT_APPLY_APIVFS_RO
))
628 if (!tmpfs_tmp
&& FLAGS_SET(mount_table
[k
].mount_settings
, MOUNT_APPLY_TMPFS_TMP
))
631 r
= chase_symlinks(mount_table
[k
].where
, dest
, CHASE_NONEXISTENT
|CHASE_PREFIX_ROOT
, &where
, NULL
);
633 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, mount_table
[k
].where
);
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
);
644 if ((mount_table
[k
].mount_settings
& (MOUNT_MKDIR
|MOUNT_TOUCH
)) != 0) {
645 uid_t u
= (use_userns
&& !in_userns
) ? uid_shift
: UID_INVALID
;
647 if (FLAGS_SET(mount_table
[k
].mount_settings
, MOUNT_TOUCH
))
648 r
= mkdir_parents_safe(dest
, where
, 0755, u
, u
, 0);
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
);
655 log_debug_errno(r
, "Failed to create directory %s: %m", where
);
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 */
664 if (FLAGS_SET(mount_table
[k
].mount_settings
, MOUNT_TOUCH
)) {
666 if (r
< 0 && r
!= -EEXIST
) {
667 if (fatal
&& r
!= -EROFS
)
668 return log_error_errno(r
, "Failed to create file %s: %m", where
);
670 log_debug_errno(r
, "Failed to create file %s: %m", where
);
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
);
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
690 r
= chase_symlinks(mount_table
[k
].what
, dest
, CHASE_PREFIX_ROOT
, &prefixed
, NULL
);
692 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, mount_table
[k
].what
);
695 r
= mount_verbose_full(
696 fatal
? LOG_ERR
: LOG_DEBUG
,
697 prefixed
?: mount_table
[k
].what
,
700 mount_table
[k
].flags
,
702 FLAGS_SET(mount_table
[k
].mount_settings
, MOUNT_FOLLOW_SYMLINKS
));
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
;
713 RemountIdmapping new_idmapping
= *idmapping
;
719 _cleanup_free_
char *word
= NULL
;
721 r
= extract_first_word(&options
, &word
, ",", 0);
723 return log_error_errno(r
, "Failed to extract mount option: %m");
727 if (streq(word
, "rbind"))
729 else if (streq(word
, "norbind"))
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
;
738 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
739 "Invalid bind mount option: %s", word
);
742 *mount_flags
= flags
;
743 *idmapping
= new_idmapping
;
744 /* in the future mount_opts will hold string options for mount(2) */
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
;
755 RemountIdmapping idmapping
= REMOUNT_IDMAPPING_NONE
;
761 r
= parse_mount_bind_options(m
->options
, &mount_flags
, &mount_opts
, &idmapping
);
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
);
771 if (stat(m
->source
, &source_st
) < 0)
772 return log_error_errno(errno
, "Failed to stat %s: %m", m
->source
);
774 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NONEXISTENT
, &where
, NULL
);
776 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, m
->destination
);
777 if (r
> 0) { /* Path exists already? */
779 if (stat(where
, &dest_st
) < 0)
780 return log_error_errno(errno
, "Failed to stat %s: %m", where
);
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.",
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.",
792 } else { /* Path doesn't exist yet? */
793 r
= mkdir_parents_label(where
, 0755);
795 return log_error_errno(r
, "Failed to make parents of %s: %m", where
);
797 /* Create the mount point. Any non-directory file can be
798 * mounted on any non-directory file (regular, fifo, socket,
801 if (S_ISDIR(source_st
.st_mode
))
802 r
= mkdir_label(where
, 0755);
806 return log_error_errno(r
, "Failed to create mount point %s: %m", where
);
809 r
= mount_nofollow_verbose(LOG_ERR
, m
->source
, where
, NULL
, mount_flags
, mount_opts
);
814 r
= bind_remount_recursive(where
, MS_RDONLY
, MS_RDONLY
, NULL
);
816 return log_error_errno(r
, "Read-only bind mount failed: %m");
819 if (idmapping
!= REMOUNT_IDMAPPING_NONE
) {
820 r
= remount_idmap(where
, uid_shift
, uid_range
, source_st
.st_uid
, idmapping
);
822 return log_error_errno(r
, "Failed to map ids for bind mount %s: %m", where
);
828 static int mount_tmpfs(const char *dest
, CustomMount
*m
, uid_t uid_shift
, const char *selinux_apifs_context
) {
830 _cleanup_free_
char *buf
= NULL
, *where
= NULL
;
836 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NONEXISTENT
, &where
, NULL
);
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);
842 return log_error_errno(r
, "Creating mount point for tmpfs %s failed: %m", where
);
845 r
= tmpfs_patch_options(m
->options
, uid_shift
== 0 ? UID_INVALID
: uid_shift
, selinux_apifs_context
, &buf
);
848 options
= r
> 0 ? buf
: m
->options
;
850 return mount_nofollow_verbose(LOG_ERR
, "tmpfs", where
, "tmpfs", MS_NODEV
|MS_STRICTATIME
, options
);
853 static char *joined_and_escaped_lower_dirs(char **lower
) {
854 _cleanup_strv_free_
char **sv
= NULL
;
856 sv
= strv_copy(lower
);
862 if (!strv_shell_escape(sv
, ",:"))
865 return strv_join(sv
, ":");
868 static int mount_overlay(const char *dest
, CustomMount
*m
) {
869 _cleanup_free_
char *lower
= NULL
, *where
= NULL
, *escaped_source
= NULL
;
876 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NONEXISTENT
, &where
, NULL
);
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);
882 return log_error_errno(r
, "Creating mount point for overlay %s failed: %m", where
);
885 (void) mkdir_p_label(m
->source
, 0755);
887 lower
= joined_and_escaped_lower_dirs(m
->lower
);
891 escaped_source
= shell_escape(m
->source
, ",:");
896 options
= strjoina("lowerdir=", escaped_source
, ":", lower
);
898 _cleanup_free_
char *escaped_work_dir
= NULL
;
900 escaped_work_dir
= shell_escape(m
->work_dir
, ",:");
901 if (!escaped_work_dir
)
904 options
= strjoina("lowerdir=", lower
, ",upperdir=", escaped_source
, ",workdir=", escaped_work_dir
);
907 return mount_nofollow_verbose(LOG_ERR
, "overlay", where
, "overlay", m
->read_only
? MS_RDONLY
: 0, options
);
910 static int mount_inaccessible(const char *dest
, CustomMount
*m
) {
911 _cleanup_free_
char *where
= NULL
, *source
= NULL
;
918 r
= chase_symlinks_and_stat(m
->destination
, dest
, CHASE_PREFIX_ROOT
, &where
, &st
, NULL
);
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
;
924 r
= mode_to_inaccessible_node(NULL
, st
.st_mode
, &source
);
926 return m
->graceful
? 0 : r
;
928 r
= mount_nofollow_verbose(m
->graceful
? LOG_DEBUG
: LOG_ERR
, source
, where
, NULL
, MS_BIND
, NULL
);
930 return m
->graceful
? 0 : r
;
932 r
= mount_nofollow_verbose(m
->graceful
? LOG_DEBUG
: LOG_ERR
, NULL
, where
, NULL
, MS_BIND
|MS_RDONLY
|MS_REMOUNT
, NULL
);
934 (void) umount_verbose(m
->graceful
? LOG_DEBUG
: LOG_ERR
, where
, UMOUNT_NOFOLLOW
);
935 return m
->graceful
? 0 : r
;
941 static int mount_arbitrary(const char *dest
, CustomMount
*m
) {
942 _cleanup_free_
char *where
= NULL
;
948 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NONEXISTENT
, &where
, NULL
);
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);
954 return log_error_errno(r
, "Creating mount point for mount %s failed: %m", where
);
957 return mount_nofollow_verbose(LOG_ERR
, m
->source
, where
, m
->type_argument
, 0, m
->options
);
962 CustomMount
*mounts
, size_t n
,
965 const char *selinux_apifs_context
,
966 MountSettingsMask mount_settings
) {
971 for (size_t i
= 0; i
< n
; i
++) {
972 CustomMount
*m
= mounts
+ i
;
974 if (FLAGS_SET(mount_settings
, MOUNT_IN_USERNS
) != m
->in_userns
)
977 if (FLAGS_SET(mount_settings
, MOUNT_ROOT_ONLY
) && !path_equal(m
->destination
, "/"))
980 if (FLAGS_SET(mount_settings
, MOUNT_NON_ROOT_ONLY
) && path_equal(m
->destination
, "/"))
985 case CUSTOM_MOUNT_BIND
:
986 r
= mount_bind(dest
, m
, uid_shift
, uid_range
);
989 case CUSTOM_MOUNT_TMPFS
:
990 r
= mount_tmpfs(dest
, m
, uid_shift
, selinux_apifs_context
);
993 case CUSTOM_MOUNT_OVERLAY
:
994 r
= mount_overlay(dest
, m
);
997 case CUSTOM_MOUNT_INACCESSIBLE
:
998 r
= mount_inaccessible(dest
, m
);
1001 case CUSTOM_MOUNT_ARBITRARY
:
1002 r
= mount_arbitrary(dest
, m
);
1006 assert_not_reached();
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
, "/"))
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
;
1031 /* --volatile=state means we simply overmount /var with a tmpfs, and the rest read-only. */
1033 r
= bind_remount_recursive(directory
, MS_RDONLY
, MS_RDONLY
, NULL
);
1035 return log_error_errno(r
, "Failed to remount %s read-only: %m", directory
);
1037 p
= prefix_roota(directory
, "/var");
1039 if (r
< 0 && errno
!= EEXIST
)
1040 return log_error_errno(errno
, "Failed to create %s: %m", directory
);
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
);
1049 return mount_nofollow_verbose(LOG_ERR
, "tmpfs", p
, "tmpfs", MS_STRICTATIME
, options
);
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
;
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. */
1066 bindir
= path_join(directory
, "/bin");
1069 if (lstat(bindir
, &st
) < 0) {
1070 if (errno
!= ENOENT
)
1071 return log_error_errno(errno
, "Failed to stat /bin directory below image: %m");
1073 /* ENOENT is fine, just means the image is probably just a naked /usr and we can create the
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).");
1083 if (!mkdtemp(template))
1084 return log_error_errno(errno
, "Failed to create temporary directory: %m");
1086 options
= "mode=755" TMPFS_LIMITS_ROOTFS
;
1087 r
= tmpfs_patch_options(options
, uid_shift
== 0 ? UID_INVALID
: uid_shift
, selinux_apifs_context
, &buf
);
1093 r
= mount_nofollow_verbose(LOG_ERR
, "tmpfs", template, "tmpfs", MS_STRICTATIME
, options
);
1097 tmpfs_mounted
= true;
1099 f
= prefix_roota(directory
, "/usr");
1100 t
= prefix_roota(template, "/usr");
1103 if (r
< 0 && errno
!= EEXIST
) {
1104 r
= log_error_errno(errno
, "Failed to create %s: %m", t
);
1108 r
= mount_nofollow_verbose(LOG_ERR
, f
, t
, NULL
, MS_BIND
|MS_REC
, NULL
);
1112 bind_mounted
= true;
1114 r
= bind_remount_recursive(t
, MS_RDONLY
, MS_RDONLY
, NULL
);
1116 log_error_errno(r
, "Failed to remount %s read-only: %m", t
);
1120 r
= mount_nofollow_verbose(LOG_ERR
, template, directory
, NULL
, MS_MOVE
, NULL
);
1124 (void) rmdir(template);
1130 (void) umount_verbose(LOG_ERR
, t
, UMOUNT_NOFOLLOW
);
1133 (void) umount_verbose(LOG_ERR
, template, UMOUNT_NOFOLLOW
);
1135 (void) rmdir(template);
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;
1148 /* --volatile=overlay means we mount an overlayfs to the root dir. */
1150 if (!mkdtemp(template))
1151 return log_error_errno(errno
, "Failed to create temporary directory: %m");
1153 options
= "mode=755" TMPFS_LIMITS_ROOTFS
;
1154 r
= tmpfs_patch_options(options
, uid_shift
== 0 ? UID_INVALID
: uid_shift
, selinux_apifs_context
, &buf
);
1160 r
= mount_nofollow_verbose(LOG_ERR
, "tmpfs", template, "tmpfs", MS_STRICTATIME
, options
);
1164 tmpfs_mounted
= true;
1166 upper
= strjoina(template, "/upper");
1167 work
= strjoina(template, "/work");
1169 if (mkdir(upper
, 0755) < 0) {
1170 r
= log_error_errno(errno
, "Failed to create %s: %m", upper
);
1173 if (mkdir(work
, 0755) < 0) {
1174 r
= log_error_errno(errno
, "Failed to create %s: %m", work
);
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. */
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
) {
1189 options
= strjoina("lowerdir=", escaped_directory
, ",upperdir=", escaped_upper
, ",workdir=", escaped_work
);
1190 r
= mount_nofollow_verbose(LOG_ERR
, "overlay", directory
, "overlay", 0, options
);
1194 (void) umount_verbose(LOG_ERR
, template, UMOUNT_NOFOLLOW
);
1196 (void) rmdir(template);
1200 int setup_volatile_mode(
1201 const char *directory
,
1204 const char *selinux_apifs_context
) {
1209 return setup_volatile_yes(directory
, uid_shift
, selinux_apifs_context
);
1211 case VOLATILE_STATE
:
1212 return setup_volatile_state(directory
, uid_shift
, selinux_apifs_context
);
1214 case VOLATILE_OVERLAY
:
1215 return setup_volatile_overlay(directory
, uid_shift
, selinux_apifs_context
);
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
;
1228 assert(pivot_root_new
);
1229 assert(pivot_root_old
);
1231 r
= extract_first_word(&p
, &root_new
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
1240 root_old
= strdup(p
);
1245 if (!path_is_absolute(root_new
))
1247 if (root_old
&& !path_is_absolute(root_old
))
1250 free_and_replace(*pivot_root_new
, root_new
);
1251 free_and_replace(*pivot_root_old
, root_old
);
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;
1265 if (!pivot_root_new
)
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.
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.
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.
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
1292 * Requires all file systems at directory and below to be mounted
1293 * MS_PRIVATE or MS_SLAVE so they can be moved.
1295 directory_pivot_root_new
= path_join(directory
, pivot_root_new
);
1296 if (!directory_pivot_root_new
)
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
);
1304 if (pivot_root_old
) {
1305 if (!mkdtemp(pivot_tmp
)) {
1306 r
= log_error_errno(errno
, "Failed to create temporary directory: %m");
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
) {
1317 r
= mount_nofollow_verbose(LOG_ERR
, directory_pivot_root_new
, pivot_tmp
, NULL
, MS_MOVE
, NULL
);
1321 r
= mount_nofollow_verbose(LOG_ERR
, directory
, pivot_tmp_pivot_root_old
, NULL
, MS_MOVE
, NULL
);
1325 r
= mount_nofollow_verbose(LOG_ERR
, pivot_tmp
, directory
, NULL
, MS_MOVE
, NULL
);
1329 r
= mount_nofollow_verbose(LOG_ERR
, directory_pivot_root_new
, directory
, NULL
, MS_MOVE
, NULL
);
1335 if (remove_pivot_tmp
)
1336 (void) rmdir(pivot_tmp
);