1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include <sys/statvfs.h>
9 #include <linux/loop.h>
14 #include "alloc-util.h"
16 #include "dissect-image.h"
17 #include "exec-util.h"
18 #include "extract-word.h"
22 #include "glyph-util.h"
24 #include "initrd-util.h"
25 #include "label-util.h"
26 #include "libmount-util.h"
27 #include "missing_mount.h"
28 #include "missing_syscall.h"
29 #include "mkdir-label.h"
30 #include "mount-util.h"
31 #include "mountpoint-util.h"
32 #include "namespace-util.h"
33 #include "parse-util.h"
34 #include "path-util.h"
35 #include "process-util.h"
37 #include "sort-util.h"
38 #include "stat-util.h"
39 #include "stdio-util.h"
40 #include "string-table.h"
41 #include "string-util.h"
43 #include "tmpfile-util.h"
44 #include "user-util.h"
46 int umount_recursive_full(const char *prefix
, int flags
, char **keep
) {
47 _cleanup_fclose_
FILE *f
= NULL
;
50 /* Try to umount everything recursively below a directory. Also, take care of stacked mounts, and
51 * keep unmounting them until they are gone. */
53 f
= fopen("/proc/self/mountinfo", "re"); /* Pin the file, in case we unmount /proc/ as part of the logic here */
55 return log_debug_errno(errno
, "Failed to open /proc/self/mountinfo: %m");
58 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
59 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
62 r
= libmount_parse("/proc/self/mountinfo", f
, &table
, &iter
);
64 return log_debug_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
67 bool shall_keep
= false;
71 r
= mnt_table_next_fs(table
, iter
, &fs
);
75 return log_debug_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
77 path
= mnt_fs_get_target(fs
);
81 if (prefix
&& !path_startswith(path
, prefix
)) {
82 // FIXME: This is extremely noisy, we're probably doing something very wrong
83 // to trigger this so often, needs more investigation.
84 // log_trace("Not unmounting %s, outside of prefix: %s", path, prefix);
89 /* Match against anything in the path to the dirs to keep, or below the dirs to keep */
90 if (path_startswith(path
, *k
) || path_startswith(*k
, path
)) {
95 log_debug("Not unmounting %s, referenced by keep list.", path
);
99 if (umount2(path
, flags
| UMOUNT_NOFOLLOW
) < 0) {
100 log_debug_errno(errno
, "Failed to umount %s, ignoring: %m", path
);
104 log_trace("Successfully unmounted %s", path
);
121 #define MS_CONVERTIBLE_FLAGS (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_NOSYMFOLLOW)
123 static uint64_t ms_flags_to_mount_attr(unsigned long a
) {
126 if (FLAGS_SET(a
, MS_RDONLY
))
127 f
|= MOUNT_ATTR_RDONLY
;
129 if (FLAGS_SET(a
, MS_NOSUID
))
130 f
|= MOUNT_ATTR_NOSUID
;
132 if (FLAGS_SET(a
, MS_NODEV
))
133 f
|= MOUNT_ATTR_NODEV
;
135 if (FLAGS_SET(a
, MS_NOEXEC
))
136 f
|= MOUNT_ATTR_NOEXEC
;
138 if (FLAGS_SET(a
, MS_NOSYMFOLLOW
))
139 f
|= MOUNT_ATTR_NOSYMFOLLOW
;
144 static bool skip_mount_set_attr
= false;
146 /* Use this function only if you do not have direct access to /proc/self/mountinfo but the caller can open it
147 * for you. This is the case when /proc is masked or not mounted. Otherwise, use bind_remount_recursive. */
148 int bind_remount_recursive_with_mountinfo(
150 unsigned long new_flags
,
151 unsigned long flags_mask
,
153 FILE *proc_self_mountinfo
) {
155 _cleanup_fclose_
FILE *proc_self_mountinfo_opened
= NULL
;
156 _cleanup_set_free_ Set
*done
= NULL
;
157 unsigned n_tries
= 0;
162 if ((flags_mask
& ~MS_CONVERTIBLE_FLAGS
) == 0 && strv_isempty(deny_list
) && !skip_mount_set_attr
) {
163 /* Let's take a shortcut for all the flags we know how to convert into mount_setattr() flags */
165 if (mount_setattr(AT_FDCWD
, prefix
, AT_SYMLINK_NOFOLLOW
|AT_RECURSIVE
,
166 &(struct mount_attr
) {
167 .attr_set
= ms_flags_to_mount_attr(new_flags
& flags_mask
),
168 .attr_clr
= ms_flags_to_mount_attr(~new_flags
& flags_mask
),
169 }, MOUNT_ATTR_SIZE_VER0
) < 0) {
171 log_debug_errno(errno
, "mount_setattr() failed, falling back to classic remounting: %m");
173 /* We fall through to classic behaviour if not supported (i.e. kernel < 5.12). We
174 * also do this for all other kinds of errors since they are so many different, and
175 * mount_setattr() has no graceful mode where it continues despite seeing errors one
176 * some mounts, but we want that. Moreover mount_setattr() only works on the mount
177 * point inode itself, not a non-mount point inode, and we want to support arbitrary
180 if (ERRNO_IS_NOT_SUPPORTED(errno
)) /* if not supported, then don't bother at all anymore */
181 skip_mount_set_attr
= true;
183 return 0; /* Nice, this worked! */
186 if (!proc_self_mountinfo
) {
187 r
= fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo_opened
);
191 proc_self_mountinfo
= proc_self_mountinfo_opened
;
194 /* Recursively remount a directory (and all its submounts) with desired flags (MS_READONLY,
195 * MS_NOSUID, MS_NOEXEC). If the directory is already mounted, we reuse the mount and simply mark it
196 * MS_BIND|MS_RDONLY (or remove the MS_RDONLY for read-write operation), ditto for other flags. If it
197 * isn't we first make it one. Afterwards we apply (or remove) the flags to all submounts we can
198 * access, too. When mounts are stacked on the same mount point we only care for each individual
199 * "top-level" mount on each point, as we cannot influence/access the underlying mounts anyway. We do
200 * not have any effect on future submounts that might get propagated, they might be writable
201 * etc. This includes future submounts that have been triggered via autofs. Also note that we can't
202 * operate atomically here. Mounts established while we process the tree might or might not get
203 * noticed and thus might or might not be covered.
205 * If the "deny_list" parameter is specified it may contain a list of subtrees to exclude from the
206 * remount operation. Note that we'll ignore the deny list for the top-level path. */
209 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
210 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
211 _cleanup_hashmap_free_ Hashmap
*todo
= NULL
;
212 bool top_autofs
= false;
214 if (n_tries
++ >= 32) /* Let's not retry this loop forever */
217 rewind(proc_self_mountinfo
);
219 r
= libmount_parse("/proc/self/mountinfo", proc_self_mountinfo
, &table
, &iter
);
221 return log_debug_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
224 _cleanup_free_
char *d
= NULL
;
225 const char *path
, *type
, *opts
;
226 unsigned long flags
= 0;
227 struct libmnt_fs
*fs
;
229 r
= mnt_table_next_fs(table
, iter
, &fs
);
230 if (r
== 1) /* EOF */
233 return log_debug_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
235 path
= mnt_fs_get_target(fs
);
239 if (!path_startswith(path
, prefix
))
242 type
= mnt_fs_get_fstype(fs
);
246 /* Let's ignore autofs mounts. If they aren't triggered yet, we want to avoid
247 * triggering them, as we don't make any guarantees for future submounts anyway. If
248 * they are already triggered, then we will find another entry for this. */
249 if (streq(type
, "autofs")) {
250 top_autofs
= top_autofs
|| path_equal(path
, prefix
);
254 if (set_contains(done
, path
))
257 /* Ignore this mount if it is deny-listed, but only if it isn't the top-level mount
258 * we shall operate on. */
259 if (!path_equal(path
, prefix
)) {
260 bool deny_listed
= false;
262 STRV_FOREACH(i
, deny_list
) {
263 if (path_equal(*i
, prefix
))
266 if (!path_startswith(*i
, prefix
))
269 if (path_startswith(path
, *i
)) {
271 log_trace("Not remounting %s deny-listed by %s, called for %s", path
, *i
, prefix
);
280 opts
= mnt_fs_get_vfs_options(fs
);
282 r
= mnt_optstr_get_flags(opts
, &flags
, mnt_get_builtin_optmap(MNT_LINUX_MAP
));
284 log_debug_errno(r
, "Could not get flags for '%s', ignoring: %m", path
);
291 r
= hashmap_ensure_put(&todo
, &path_hash_ops_free
, d
, ULONG_TO_PTR(flags
));
293 /* If the same path was recorded, but with different mount flags, update it:
294 * it means a mount point is overmounted, and libmount returns the "bottom" (or
295 * older one) first, but we want to reapply the flags from the "top" (or newer
296 * one). See: https://github.com/systemd/systemd/issues/20032
297 * Note that this shouldn't really fail, as we were just told that the key
298 * exists, and it's an update so we want 'd' to be freed immediately. */
299 r
= hashmap_update(todo
, d
, ULONG_TO_PTR(flags
));
306 /* Check if the top-level directory was among what we have seen so far. For that check both
307 * 'done' and 'todo'. Also check 'top_autofs' because if the top-level dir is an autofs we'll
308 * not include it in either set but will set this bool. */
309 if (!set_contains(done
, prefix
) &&
310 !(top_autofs
|| hashmap_contains(todo
, prefix
))) {
312 /* The prefix directory itself is not yet a mount, make it one. */
313 r
= mount_nofollow(prefix
, prefix
, NULL
, MS_BIND
|MS_REC
, NULL
);
317 /* Immediately rescan, so that we pick up the new mount's flags */
321 /* If we have no submounts to process anymore, we are done */
322 if (hashmap_isempty(todo
))
329 /* Take the first mount from our list of mounts to still process */
330 flags
= PTR_TO_ULONG(hashmap_steal_first_key_and_value(todo
, (void**) &x
));
334 r
= set_ensure_consume(&done
, &path_hash_ops_free
, x
);
335 if (IN_SET(r
, 0, -EEXIST
))
336 continue; /* Already done */
340 /* Now, remount this with the new flags set, but exclude MS_RELATIME from it. (It's
341 * the default anyway, thus redundant, and in userns we'll get an error if we try to
342 * explicitly enable it) */
343 r
= mount_nofollow(NULL
, x
, NULL
, ((flags
& ~flags_mask
)|MS_BIND
|MS_REMOUNT
|new_flags
) & ~MS_RELATIME
, NULL
);
347 /* OK, so the remount of this entry failed. We'll ultimately ignore this in
348 * almost all cases (there are simply so many reasons why this can fail,
349 * think autofs, NFS, FUSE, …), but let's generate useful debug messages at
352 q
= path_is_mount_point(x
);
353 if (IN_SET(q
, 0, -ENOENT
)) {
354 /* Hmm, whaaaa? The mount point is not actually a mount point? Then
355 * it is either obstructed by a later mount or somebody has been
356 * racing against us and removed it. Either way the mount point
357 * doesn't matter to us, let's ignore it hence. */
358 log_debug_errno(r
, "Mount point '%s' to remount is not a mount point anymore, ignoring remount failure: %m", x
);
361 if (q
< 0) /* Any other error on this? Just log and continue */
362 log_debug_errno(q
, "Failed to determine whether '%s' is a mount point or not, ignoring: %m", x
);
364 if (((flags
^ new_flags
) & flags_mask
& ~MS_RELATIME
) == 0) { /* ignore MS_RELATIME while comparing */
365 log_debug_errno(r
, "Couldn't remount '%s', but the flags already match what we want, hence ignoring: %m", x
);
369 /* Make this fatal if this is the top-level mount */
370 if (path_equal(x
, prefix
))
373 /* If this is not the top-level mount, then handle this gracefully: log but
374 * otherwise ignore. With NFS, FUSE, autofs there are just too many reasons
375 * this might fail without a chance for us to do anything about it, let's
376 * hence be strict on the top-level mount and lenient on the inner ones. */
377 log_debug_errno(r
, "Couldn't remount submount '%s' for unexpected reason, ignoring: %m", x
);
381 log_trace("Remounted %s.", x
);
386 int bind_remount_one_with_mountinfo(
388 unsigned long new_flags
,
389 unsigned long flags_mask
,
390 FILE *proc_self_mountinfo
) {
392 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
393 unsigned long flags
= 0;
394 struct libmnt_fs
*fs
;
399 assert(proc_self_mountinfo
);
401 if ((flags_mask
& ~MS_CONVERTIBLE_FLAGS
) == 0 && !skip_mount_set_attr
) {
402 /* Let's take a shortcut for all the flags we know how to convert into mount_setattr() flags */
404 if (mount_setattr(AT_FDCWD
, path
, AT_SYMLINK_NOFOLLOW
,
405 &(struct mount_attr
) {
406 .attr_set
= ms_flags_to_mount_attr(new_flags
& flags_mask
),
407 .attr_clr
= ms_flags_to_mount_attr(~new_flags
& flags_mask
),
408 }, MOUNT_ATTR_SIZE_VER0
) < 0) {
410 log_debug_errno(errno
, "mount_setattr() didn't work, falling back to classic remounting: %m");
412 if (ERRNO_IS_NOT_SUPPORTED(errno
)) /* if not supported, then don't bother at all anymore */
413 skip_mount_set_attr
= true;
415 return 0; /* Nice, this worked! */
418 rewind(proc_self_mountinfo
);
420 table
= mnt_new_table();
424 r
= mnt_table_parse_stream(table
, proc_self_mountinfo
, "/proc/self/mountinfo");
428 fs
= mnt_table_find_target(table
, path
, MNT_ITER_FORWARD
);
430 if (laccess(path
, F_OK
) < 0) /* Hmm, it's not in the mount table, but does it exist at all? */
433 return -EINVAL
; /* Not a mount point we recognize */
436 opts
= mnt_fs_get_vfs_options(fs
);
438 r
= mnt_optstr_get_flags(opts
, &flags
, mnt_get_builtin_optmap(MNT_LINUX_MAP
));
440 log_debug_errno(r
, "Could not get flags for '%s', ignoring: %m", path
);
443 r
= mount_nofollow(NULL
, path
, NULL
, ((flags
& ~flags_mask
)|MS_BIND
|MS_REMOUNT
|new_flags
) & ~MS_RELATIME
, NULL
);
445 if (((flags
^ new_flags
) & flags_mask
& ~MS_RELATIME
) != 0) /* Ignore MS_RELATIME again,
446 * since kernel adds it in
447 * everywhere, because it's the
451 /* Let's handle redundant remounts gracefully */
452 log_debug_errno(r
, "Failed to remount '%s' but flags already match what we want, ignoring: %m", path
);
458 int bind_remount_one(const char *path
, unsigned long new_flags
, unsigned long flags_mask
) {
459 _cleanup_fclose_
FILE *proc_self_mountinfo
= NULL
;
461 proc_self_mountinfo
= fopen("/proc/self/mountinfo", "re");
462 if (!proc_self_mountinfo
)
463 return log_debug_errno(errno
, "Failed to open /proc/self/mountinfo: %m");
465 return bind_remount_one_with_mountinfo(path
, new_flags
, flags_mask
, proc_self_mountinfo
);
468 static int mount_switch_root_pivot(int fd_newroot
, const char *path
) {
469 assert(fd_newroot
>= 0);
472 /* Let the kernel tuck the new root under the old one. */
473 if (pivot_root(".", ".") < 0)
474 return log_debug_errno(errno
, "Failed to pivot root to new rootfs '%s': %m", path
);
476 /* Get rid of the old root and reveal our brand new root. (This will always operate on the top-most
477 * mount on our cwd, regardless what our current directory actually points to.) */
478 if (umount2(".", MNT_DETACH
) < 0)
479 return log_debug_errno(errno
, "Failed to unmount old rootfs: %m");
484 static int mount_switch_root_move(int fd_newroot
, const char *path
) {
485 assert(fd_newroot
>= 0);
488 /* Move the new root fs */
489 if (mount(".", "/", NULL
, MS_MOVE
, NULL
) < 0)
490 return log_debug_errno(errno
, "Failed to move new rootfs '%s': %m", path
);
492 /* Also change root dir */
494 return log_debug_errno(errno
, "Failed to chroot to new rootfs '%s': %m", path
);
499 int mount_switch_root_full(const char *path
, unsigned long mount_propagation_flag
, bool force_ms_move
) {
500 _cleanup_close_
int fd_newroot
= -EBADF
;
501 int r
, is_current_root
;
504 assert(mount_propagation_flag_is_valid(mount_propagation_flag
));
506 fd_newroot
= open(path
, O_PATH
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
);
508 return log_debug_errno(errno
, "Failed to open new rootfs '%s': %m", path
);
510 is_current_root
= path_is_root_at(fd_newroot
, NULL
);
511 if (is_current_root
< 0)
512 return log_debug_errno(is_current_root
, "Failed to determine if target dir is our root already: %m");
514 /* Change into the new rootfs. */
515 if (fchdir(fd_newroot
) < 0)
516 return log_debug_errno(errno
, "Failed to chdir into new rootfs '%s': %m", path
);
518 /* Make this a NOP if we are supposed to switch to our current root fs. After all, both pivot_root()
519 * and MS_MOVE don't like that. */
520 if (!is_current_root
) {
521 if (!force_ms_move
) {
522 r
= mount_switch_root_pivot(fd_newroot
, path
);
524 log_debug_errno(r
, "Failed to pivot into new rootfs '%s', will try to use MS_MOVE instead: %m", path
);
525 force_ms_move
= true;
529 /* Failed to pivot_root() fallback to MS_MOVE. For example, this may happen if the rootfs is
530 * an initramfs in which case pivot_root() isn't supported. */
531 r
= mount_switch_root_move(fd_newroot
, path
);
533 return log_debug_errno(r
, "Failed to switch to new rootfs '%s' with MS_MOVE: %m", path
);
537 /* Finally, let's establish the requested propagation flags. */
538 if (mount_propagation_flag
== 0)
541 if (mount(NULL
, ".", NULL
, mount_propagation_flag
| MS_REC
, 0) < 0)
542 return log_debug_errno(errno
, "Failed to turn new rootfs '%s' into %s mount: %m",
543 mount_propagation_flag_to_string(mount_propagation_flag
), path
);
548 int repeat_unmount(const char *path
, int flags
) {
553 /* If there are multiple mounts on a mount point, this
554 * removes them all */
557 if (umount2(path
, flags
) < 0) {
569 int mode_to_inaccessible_node(
570 const char *runtime_dir
,
574 /* This function maps a node type to a corresponding inaccessible file node. These nodes are created
575 * during early boot by PID 1. In some cases we lacked the privs to create the character and block
576 * devices (maybe because we run in an userns environment, or miss CAP_SYS_MKNOD, or run with a
577 * devices policy that excludes device nodes with major and minor of 0), but that's fine, in that
578 * case we use an AF_UNIX file node instead, which is not the same, but close enough for most
579 * uses. And most importantly, the kernel allows bind mounts from socket nodes to any non-directory
580 * file nodes, and that's the most important thing that matters.
582 * Note that the runtime directory argument shall be the top-level runtime directory, i.e. /run/ if
583 * we operate in system context and $XDG_RUNTIME_DIR if we operate in user context. */
585 _cleanup_free_
char *d
= NULL
;
591 runtime_dir
= "/run";
596 node
= inode_type_to_string(mode
);
600 d
= path_join(runtime_dir
, "systemd/inaccessible", node
);
604 /* On new kernels unprivileged users are permitted to create 0:0 char device nodes (because they also
605 * act as whiteout inode for overlayfs), but no other char or block device nodes. On old kernels no
606 * device node whatsoever may be created by unprivileged processes. Hence, if the caller asks for the
607 * inaccessible block device node let's see if the block device node actually exists, and if not,
608 * fall back to the character device node. From there fall back to the socket device node. This means
609 * in the best case we'll get the right device node type — but if not we'll hopefully at least get a
610 * device node at all. */
613 access(d
, F_OK
) < 0 && errno
== ENOENT
) {
615 d
= path_join(runtime_dir
, "/systemd/inaccessible/chr");
620 if (IN_SET(mode
& S_IFMT
, S_IFBLK
, S_IFCHR
) &&
621 access(d
, F_OK
) < 0 && errno
== ENOENT
) {
623 d
= path_join(runtime_dir
, "/systemd/inaccessible/sock");
632 int mount_flags_to_string(unsigned long flags
, char **ret
) {
633 static const struct {
637 { .flag
= MS_RDONLY
, .name
= "MS_RDONLY", },
638 { .flag
= MS_NOSUID
, .name
= "MS_NOSUID", },
639 { .flag
= MS_NODEV
, .name
= "MS_NODEV", },
640 { .flag
= MS_NOEXEC
, .name
= "MS_NOEXEC", },
641 { .flag
= MS_SYNCHRONOUS
, .name
= "MS_SYNCHRONOUS", },
642 { .flag
= MS_REMOUNT
, .name
= "MS_REMOUNT", },
643 { .flag
= MS_MANDLOCK
, .name
= "MS_MANDLOCK", },
644 { .flag
= MS_DIRSYNC
, .name
= "MS_DIRSYNC", },
645 { .flag
= MS_NOSYMFOLLOW
, .name
= "MS_NOSYMFOLLOW", },
646 { .flag
= MS_NOATIME
, .name
= "MS_NOATIME", },
647 { .flag
= MS_NODIRATIME
, .name
= "MS_NODIRATIME", },
648 { .flag
= MS_BIND
, .name
= "MS_BIND", },
649 { .flag
= MS_MOVE
, .name
= "MS_MOVE", },
650 { .flag
= MS_REC
, .name
= "MS_REC", },
651 { .flag
= MS_SILENT
, .name
= "MS_SILENT", },
652 { .flag
= MS_POSIXACL
, .name
= "MS_POSIXACL", },
653 { .flag
= MS_UNBINDABLE
, .name
= "MS_UNBINDABLE", },
654 { .flag
= MS_PRIVATE
, .name
= "MS_PRIVATE", },
655 { .flag
= MS_SLAVE
, .name
= "MS_SLAVE", },
656 { .flag
= MS_SHARED
, .name
= "MS_SHARED", },
657 { .flag
= MS_RELATIME
, .name
= "MS_RELATIME", },
658 { .flag
= MS_KERNMOUNT
, .name
= "MS_KERNMOUNT", },
659 { .flag
= MS_I_VERSION
, .name
= "MS_I_VERSION", },
660 { .flag
= MS_STRICTATIME
, .name
= "MS_STRICTATIME", },
661 { .flag
= MS_LAZYTIME
, .name
= "MS_LAZYTIME", },
663 _cleanup_free_
char *str
= NULL
;
667 for (size_t i
= 0; i
< ELEMENTSOF(map
); i
++)
668 if (flags
& map
[i
].flag
) {
669 if (!strextend_with_separator(&str
, "|", map
[i
].name
))
671 flags
&= ~map
[i
].flag
;
674 if (!str
|| flags
!= 0)
675 if (strextendf_with_separator(&str
, "|", "%lx", flags
) < 0)
678 *ret
= TAKE_PTR(str
);
682 int mount_verbose_full(
689 bool follow_symlink
) {
691 _cleanup_free_
char *fl
= NULL
, *o
= NULL
;
695 r
= mount_option_mangle(options
, flags
, &f
, &o
);
697 return log_full_errno(error_log_level
, r
,
698 "Failed to mangle mount options %s: %m",
701 (void) mount_flags_to_string(f
, &fl
);
703 if (FLAGS_SET(f
, MS_REMOUNT
|MS_BIND
))
704 log_debug("Changing mount flags %s (%s \"%s\")...",
705 where
, strnull(fl
), strempty(o
));
706 else if (f
& MS_REMOUNT
)
707 log_debug("Remounting superblock %s (%s \"%s\")...",
708 where
, strnull(fl
), strempty(o
));
709 else if (f
& (MS_SHARED
|MS_PRIVATE
|MS_SLAVE
|MS_UNBINDABLE
))
710 log_debug("Changing mount propagation %s (%s \"%s\")",
711 where
, strnull(fl
), strempty(o
));
712 else if (f
& MS_BIND
)
713 log_debug("Bind-mounting %s on %s (%s \"%s\")...",
714 what
, where
, strnull(fl
), strempty(o
));
715 else if (f
& MS_MOVE
)
716 log_debug("Moving mount %s %s %s (%s \"%s\")...",
717 what
, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
), where
, strnull(fl
), strempty(o
));
719 log_debug("Mounting %s (%s) on %s (%s \"%s\")...",
720 strna(what
), strna(type
), where
, strnull(fl
), strempty(o
));
723 r
= RET_NERRNO(mount(what
, where
, type
, f
, o
));
725 r
= mount_nofollow(what
, where
, type
, f
, o
);
727 return log_full_errno(error_log_level
, r
,
728 "Failed to mount %s (type %s) on %s (%s \"%s\"): %m",
729 strna(what
), strna(type
), where
, strnull(fl
), strempty(o
));
740 log_debug("Umounting %s...", what
);
742 if (umount2(what
, flags
) < 0)
743 return log_full_errno(error_log_level
, errno
,
744 "Failed to unmount %s: %m", what
);
749 int mount_exchange_graceful(int fsmount_fd
, const char *dest
, bool mount_beneath
) {
752 assert(fsmount_fd
>= 0);
755 /* First, try to mount beneath an existing mount point, and if that works, umount the old mount,
756 * which is now at the top. This will ensure we can atomically replace a mount. Note that this works
757 * also in the case where there are submounts down the tree. Mount propagation is allowed but
758 * restricted to layouts that don't end up propagation the new mount on top of the mount stack. If
759 * this is not supported (minimum kernel v6.5), or if there is no mount on the mountpoint, we get
760 * -EINVAL and then we fallback to normal mounting. */
762 r
= RET_NERRNO(move_mount(
767 MOVE_MOUNT_F_EMPTY_PATH
| (mount_beneath
? MOVE_MOUNT_BENEATH
: 0)));
769 if (r
== -EINVAL
) { /* Fallback if mount_beneath is not supported */
771 "Failed to mount beneath '%s', falling back to overmount",
773 return RET_NERRNO(move_mount(
778 MOVE_MOUNT_F_EMPTY_PATH
));
781 if (r
>= 0) /* If it is, now remove the old mount */
782 return umount_verbose(LOG_DEBUG
, dest
, UMOUNT_NOFOLLOW
|MNT_DETACH
);
788 int mount_option_mangle(
790 unsigned long mount_flags
,
791 unsigned long *ret_mount_flags
,
792 char **ret_remaining_options
) {
794 const struct libmnt_optmap
*map
;
795 _cleanup_free_
char *ret
= NULL
;
798 /* This extracts mount flags from the mount options, and stores
799 * non-mount-flag options to '*ret_remaining_options'.
801 * "rw,nosuid,nodev,relatime,size=1630748k,mode=0700,uid=1000,gid=1000"
802 * is split to MS_NOSUID|MS_NODEV|MS_RELATIME and
803 * "size=1630748k,mode=0700,uid=1000,gid=1000".
804 * See more examples in test-mount-util.c.
806 * If 'options' does not contain any non-mount-flag options,
807 * then '*ret_remaining_options' is set to NULL instead of empty string.
808 * The validity of options stored in '*ret_remaining_options' is not checked.
809 * If 'options' is NULL, this just copies 'mount_flags' to *ret_mount_flags. */
811 assert(ret_mount_flags
);
812 assert(ret_remaining_options
);
814 map
= mnt_get_builtin_optmap(MNT_LINUX_MAP
);
818 for (const char *p
= options
;;) {
819 _cleanup_free_
char *word
= NULL
;
820 const struct libmnt_optmap
*ent
;
822 r
= extract_first_word(&p
, &word
, ",", EXTRACT_KEEP_QUOTE
);
828 for (ent
= map
; ent
->name
; ent
++) {
829 /* All entries in MNT_LINUX_MAP do not take any argument.
830 * Thus, ent->name does not contain "=" or "[=]". */
831 if (!streq(word
, ent
->name
))
834 if (!(ent
->mask
& MNT_INVERT
))
835 mount_flags
|= ent
->id
;
837 mount_flags
&= ~ent
->id
;
842 /* If 'word' is not a mount flag, then store it in '*ret_remaining_options'. */
844 !startswith_no_case(word
, "x-") &&
845 !strextend_with_separator(&ret
, ",", word
))
849 *ret_mount_flags
= mount_flags
;
850 *ret_remaining_options
= TAKE_PTR(ret
);
855 static int mount_in_namespace_legacy(
856 const char *chased_src_path
,
858 struct stat
*chased_src_st
,
859 const char *propagate_path
,
860 const char *incoming_path
,
866 bool make_file_or_directory
,
867 const MountOptions
*options
,
868 const ImagePolicy
*image_policy
,
871 _cleanup_close_pair_
int errno_pipe_fd
[2] = EBADF_PAIR
;
872 char mount_slave
[] = "/tmp/propagate.XXXXXX", *mount_tmp
, *mount_outside
, *p
;
873 bool mount_slave_created
= false, mount_slave_mounted
= false,
874 mount_tmp_created
= false, mount_tmp_mounted
= false,
875 mount_outside_created
= false, mount_outside_mounted
= false;
879 assert(chased_src_path
);
880 assert(chased_src_fd
>= 0);
881 assert(chased_src_st
);
882 assert(propagate_path
);
883 assert(incoming_path
);
885 assert(pidns_fd
>= 0);
886 assert(mntns_fd
>= 0);
887 assert(root_fd
>= 0);
888 assert(!options
|| is_image
);
890 p
= strjoina(propagate_path
, "/");
891 r
= laccess(p
, F_OK
);
893 return log_debug_errno(r
== -ENOENT
? SYNTHETIC_ERRNO(EOPNOTSUPP
) : r
, "Target does not allow propagation of mount points");
895 /* Our goal is to install a new bind mount into the container,
896 possibly read-only. This is irritatingly complex
897 unfortunately, currently.
899 First, we start by creating a private playground in /tmp,
900 that we can mount MS_SLAVE. (Which is necessary, since
901 MS_MOVE cannot be applied to mounts with MS_SHARED parent
904 if (!mkdtemp(mount_slave
))
905 return log_debug_errno(errno
, "Failed to create playground %s: %m", mount_slave
);
907 mount_slave_created
= true;
909 r
= mount_nofollow_verbose(LOG_DEBUG
, mount_slave
, mount_slave
, NULL
, MS_BIND
, NULL
);
913 mount_slave_mounted
= true;
915 r
= mount_nofollow_verbose(LOG_DEBUG
, NULL
, mount_slave
, NULL
, MS_SLAVE
, NULL
);
919 /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
920 mount_tmp
= strjoina(mount_slave
, "/mount");
922 r
= mkdir_p(mount_tmp
, 0700);
924 r
= make_mount_point_inode_from_stat(chased_src_st
, mount_tmp
, 0700);
926 log_debug_errno(r
, "Failed to create temporary mount point %s: %m", mount_tmp
);
930 mount_tmp_created
= true;
933 r
= verity_dissect_and_mount(
939 /* required_host_os_release_id= */ NULL
,
940 /* required_host_os_release_version_id= */ NULL
,
941 /* required_host_os_release_sysext_level= */ NULL
,
942 /* required_host_os_release_confext_level= */ NULL
,
943 /* required_sysext_scope= */ NULL
,
944 /* ret_image= */ NULL
);
946 r
= mount_follow_verbose(LOG_DEBUG
, FORMAT_PROC_FD_PATH(chased_src_fd
), mount_tmp
, NULL
, MS_BIND
, NULL
);
950 mount_tmp_mounted
= true;
952 /* Third, we remount the new bind mount read-only if requested. */
954 r
= mount_nofollow_verbose(LOG_DEBUG
, NULL
, mount_tmp
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
, NULL
);
959 /* Fourth, we move the new bind mount into the propagation directory. This way it will appear there read-only
962 mount_outside
= strjoina(propagate_path
, "/XXXXXX");
963 if (is_image
|| S_ISDIR(chased_src_st
->st_mode
))
964 r
= mkdtemp(mount_outside
) ? 0 : -errno
;
966 r
= mkostemp_safe(mount_outside
);
970 log_debug_errno(r
, "Cannot create propagation file or directory %s: %m", mount_outside
);
974 mount_outside_created
= true;
976 r
= mount_nofollow_verbose(LOG_DEBUG
, mount_tmp
, mount_outside
, NULL
, MS_MOVE
, NULL
);
980 mount_outside_mounted
= true;
981 mount_tmp_mounted
= false;
983 if (is_image
|| S_ISDIR(chased_src_st
->st_mode
))
984 (void) rmdir(mount_tmp
);
986 (void) unlink(mount_tmp
);
987 mount_tmp_created
= false;
989 (void) umount_verbose(LOG_DEBUG
, mount_slave
, UMOUNT_NOFOLLOW
);
990 mount_slave_mounted
= false;
992 (void) rmdir(mount_slave
);
993 mount_slave_created
= false;
995 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0) {
996 log_debug_errno(errno
, "Failed to create pipe: %m");
1000 r
= namespace_fork("(sd-bindmnt)", "(sd-bindmnt-inner)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG_SIGTERM
,
1001 pidns_fd
, mntns_fd
, -1, -1, root_fd
, &child
);
1005 _cleanup_free_
char *mount_outside_fn
= NULL
, *mount_inside
= NULL
;
1007 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1009 if (make_file_or_directory
) {
1011 (void) mkdir_parents(dest
, 0755);
1012 (void) make_mount_point_inode_from_stat(chased_src_st
, dest
, 0700);
1014 (void) mkdir_p(dest
, 0755);
1017 /* Fifth, move the mount to the right place inside */
1018 r
= path_extract_filename(mount_outside
, &mount_outside_fn
);
1020 log_debug_errno(r
, "Failed to extract filename from propagation file or directory '%s': %m", mount_outside
);
1024 mount_inside
= path_join(incoming_path
, mount_outside_fn
);
1025 if (!mount_inside
) {
1026 r
= log_oom_debug();
1030 r
= mount_nofollow_verbose(LOG_DEBUG
, mount_inside
, dest
, NULL
, MS_MOVE
, NULL
);
1034 _exit(EXIT_SUCCESS
);
1037 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1038 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1040 _exit(EXIT_FAILURE
);
1043 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1045 r
= wait_for_terminate_and_check("(sd-bindmnt)", child
, 0);
1047 log_debug_errno(r
, "Failed to wait for child: %m");
1050 if (r
!= EXIT_SUCCESS
) {
1051 if (read(errno_pipe_fd
[0], &r
, sizeof(r
)) == sizeof(r
))
1052 log_debug_errno(r
, "Failed to mount: %m");
1054 log_debug("Child failed.");
1059 if (mount_outside_mounted
)
1060 (void) umount_verbose(LOG_DEBUG
, mount_outside
, UMOUNT_NOFOLLOW
);
1061 if (mount_outside_created
) {
1062 if (is_image
|| S_ISDIR(chased_src_st
->st_mode
))
1063 (void) rmdir(mount_outside
);
1065 (void) unlink(mount_outside
);
1068 if (mount_tmp_mounted
)
1069 (void) umount_verbose(LOG_DEBUG
, mount_tmp
, UMOUNT_NOFOLLOW
);
1070 if (mount_tmp_created
) {
1071 if (is_image
|| S_ISDIR(chased_src_st
->st_mode
))
1072 (void) rmdir(mount_tmp
);
1074 (void) unlink(mount_tmp
);
1077 if (mount_slave_mounted
)
1078 (void) umount_verbose(LOG_DEBUG
, mount_slave
, UMOUNT_NOFOLLOW
);
1079 if (mount_slave_created
)
1080 (void) rmdir(mount_slave
);
1085 static int mount_in_namespace(
1086 const PidRef
*target
,
1087 const char *propagate_path
,
1088 const char *incoming_path
,
1092 bool make_file_or_directory
,
1093 const MountOptions
*options
,
1094 const ImagePolicy
*image_policy
,
1097 _cleanup_(dissected_image_unrefp
) DissectedImage
*img
= NULL
;
1098 _cleanup_close_pair_
int errno_pipe_fd
[2] = EBADF_PAIR
;
1099 _cleanup_close_
int mntns_fd
= -EBADF
, root_fd
= -EBADF
, pidns_fd
= -EBADF
, chased_src_fd
= -EBADF
,
1100 new_mount_fd
= -EBADF
;
1101 _cleanup_free_
char *chased_src_path
= NULL
;
1106 assert(propagate_path
);
1107 assert(incoming_path
);
1110 assert(!options
|| is_image
);
1112 if (!pidref_is_set(target
))
1115 r
= namespace_open(target
->pid
, &pidns_fd
, &mntns_fd
, /* ret_netns_fd = */ NULL
, /* ret_userns_fd = */ NULL
, &root_fd
);
1117 return log_debug_errno(r
, "Failed to retrieve FDs of the target process' namespace: %m");
1119 r
= in_same_namespace(target
->pid
, 0, NAMESPACE_MOUNT
);
1121 return log_debug_errno(r
, "Failed to determine if mount namespaces are equal: %m");
1122 /* We can't add new mounts at runtime if the process wasn't started in a namespace */
1124 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to activate bind mount in target, not running in a mount namespace");
1126 r
= pidref_verify(target
);
1128 return log_debug_errno(r
, "Failed to verify target process '" PID_FMT
"': %m", target
->pid
);
1130 r
= chase(src
, NULL
, 0, &chased_src_path
, &chased_src_fd
);
1132 return log_debug_errno(r
, "Failed to resolve source path of %s: %m", src
);
1133 log_debug("Chased source path of %s to %s", src
, chased_src_path
);
1135 if (fstat(chased_src_fd
, &st
) < 0)
1136 return log_debug_errno(errno
, "Failed to stat() resolved source path %s: %m", src
);
1137 if (S_ISLNK(st
.st_mode
)) /* This shouldn't really happen, given that we just chased the symlinks above, but let's better be safe… */
1138 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "Source directory %s can't be a symbolic link", src
);
1140 if (!mount_new_api_supported()) /* Fallback if we can't use the new mount API */
1141 return mount_in_namespace_legacy(
1152 make_file_or_directory
,
1158 r
= verity_dissect_and_mount(
1164 /* required_host_os_release_id= */ NULL
,
1165 /* required_host_os_release_version_id= */ NULL
,
1166 /* required_host_os_release_sysext_level= */ NULL
,
1167 /* required_host_os_release_confext_level= */ NULL
,
1168 /* required_sysext_scope= */ NULL
,
1171 return log_debug_errno(
1173 "Failed to dissect and mount image %s: %m",
1176 new_mount_fd
= open_tree(
1179 OPEN_TREE_CLONE
|OPEN_TREE_CLOEXEC
|AT_SYMLINK_NOFOLLOW
|AT_EMPTY_PATH
);
1180 if (new_mount_fd
< 0)
1181 return log_debug_errno(
1183 "Failed to open mount point \"%s\": %m",
1186 if (read_only
&& mount_setattr(new_mount_fd
, "", AT_EMPTY_PATH
,
1187 &(struct mount_attr
) {
1188 .attr_set
= MOUNT_ATTR_RDONLY
,
1189 }, MOUNT_ATTR_SIZE_VER0
) < 0)
1190 return log_debug_errno(
1192 "Failed to set mount flags for \"%s\": %m",
1196 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1197 return log_debug_errno(errno
, "Failed to create pipe: %m");
1199 r
= namespace_fork("(sd-bindmnt)",
1200 "(sd-bindmnt-inner)",
1201 /* except_fds= */ NULL
,
1202 /* n_except_fds= */ 0,
1203 FORK_RESET_SIGNALS
|FORK_DEATHSIG_SIGTERM
,
1207 /* userns_fd= */ -1,
1211 return log_debug_errno(r
, "Failed to fork off: %m");
1213 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1215 if (make_file_or_directory
)
1216 (void) mkdir_parents(dest
, 0755);
1219 DissectImageFlags f
=
1220 DISSECT_IMAGE_TRY_ATOMIC_MOUNT_EXCHANGE
|
1221 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY
;
1223 if (make_file_or_directory
)
1224 f
|= DISSECT_IMAGE_MKDIR
;
1227 f
|= DISSECT_IMAGE_READ_ONLY
;
1229 r
= dissected_image_mount(
1232 /* uid_shift= */ UID_INVALID
,
1233 /* uid_range= */ UID_INVALID
,
1234 /* userns_fd= */ -EBADF
,
1237 if (make_file_or_directory
)
1238 (void) make_mount_point_inode_from_stat(&st
, dest
, 0700);
1240 r
= mount_exchange_graceful(new_mount_fd
, dest
, /* mount_beneath= */ true);
1243 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1244 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1246 _exit(EXIT_FAILURE
);
1249 _exit(EXIT_SUCCESS
);
1252 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1254 r
= wait_for_terminate_and_check("(sd-bindmnt)", child
, 0);
1256 return log_debug_errno(r
, "Failed to wait for child: %m");
1257 if (r
!= EXIT_SUCCESS
) {
1258 if (read(errno_pipe_fd
[0], &r
, sizeof(r
)) == sizeof(r
))
1259 return log_debug_errno(r
, "Failed to mount: %m");
1261 return log_debug_errno(SYNTHETIC_ERRNO(EPROTO
), "Child failed.");
1267 int bind_mount_in_namespace(
1269 const char *propagate_path
,
1270 const char *incoming_path
,
1274 bool make_file_or_directory
) {
1276 return mount_in_namespace(target
, propagate_path
, incoming_path
, src
, dest
, read_only
, make_file_or_directory
, /* options= */ NULL
, /* image_policy= */ NULL
, /* is_image= */ false);
1279 int mount_image_in_namespace(
1281 const char *propagate_path
,
1282 const char *incoming_path
,
1286 bool make_file_or_directory
,
1287 const MountOptions
*options
,
1288 const ImagePolicy
*image_policy
) {
1290 return mount_in_namespace(target
, propagate_path
, incoming_path
, src
, dest
, read_only
, make_file_or_directory
, options
, image_policy
, /* is_image=*/ true);
1293 int make_mount_point(const char *path
) {
1298 /* If 'path' is already a mount point, does nothing and returns 0. If it is not it makes it one, and returns 1. */
1300 r
= path_is_mount_point(path
);
1302 return log_debug_errno(r
, "Failed to determine whether '%s' is a mount point: %m", path
);
1306 r
= mount_nofollow_verbose(LOG_DEBUG
, path
, path
, NULL
, MS_BIND
|MS_REC
, NULL
);
1313 int fd_make_mount_point(int fd
) {
1318 r
= fd_is_mount_point(fd
, NULL
, 0);
1320 return log_debug_errno(r
, "Failed to determine whether file descriptor is a mount point: %m");
1324 r
= mount_follow_verbose(LOG_DEBUG
, FORMAT_PROC_FD_PATH(fd
), FORMAT_PROC_FD_PATH(fd
), NULL
, MS_BIND
|MS_REC
, NULL
);
1331 int make_userns(uid_t uid_shift
, uid_t uid_range
, uid_t source_owner
, uid_t dest_owner
, RemountIdmapping idmapping
) {
1332 _cleanup_close_
int userns_fd
= -EBADF
;
1333 _cleanup_free_
char *line
= NULL
;
1335 /* Allocates a userns file descriptor with the mapping we need. For this we'll fork off a child
1336 * process whose only purpose is to give us a new user namespace. It's killed when we got it. */
1338 if (!userns_shift_range_valid(uid_shift
, uid_range
))
1341 if (IN_SET(idmapping
, REMOUNT_IDMAPPING_NONE
, REMOUNT_IDMAPPING_HOST_ROOT
)) {
1342 if (asprintf(&line
, UID_FMT
" " UID_FMT
" " UID_FMT
"\n", 0u, uid_shift
, uid_range
) < 0)
1343 return log_oom_debug();
1345 /* If requested we'll include an entry in the mapping so that the host root user can make
1346 * changes to the uidmapped mount like it normally would. Specifically, we'll map the user
1347 * with UID_MAPPED_ROOT on the backing fs to UID 0. This is useful, since nspawn code wants
1348 * to create various missing inodes in the OS tree before booting into it, and this becomes
1349 * very easy and straightforward to do if it can just do it under its own regular UID. Note
1350 * that in that case the container's runtime uidmap (i.e. the one the container payload
1351 * processes run in) will leave this UID unmapped, i.e. if we accidentally leave files owned
1352 * by host root in the already uidmapped tree around they'll show up as owned by 'nobody',
1353 * which is safe. (Of course, we shouldn't leave such inodes around, but always chown() them
1354 * to the container's own UID range, but it's good to have a safety net, in case we
1356 if (idmapping
== REMOUNT_IDMAPPING_HOST_ROOT
)
1357 if (strextendf(&line
,
1358 UID_FMT
" " UID_FMT
" " UID_FMT
"\n",
1359 UID_MAPPED_ROOT
, 0u, 1u) < 0)
1360 return log_oom_debug();
1363 if (idmapping
== REMOUNT_IDMAPPING_HOST_OWNER
) {
1364 /* Remap the owner of the bind mounted directory to the root user within the container. This
1365 * way every file written by root within the container to the bind-mounted directory will
1366 * be owned by the original user from the host. All other users will remain unmapped. */
1367 if (asprintf(&line
, UID_FMT
" " UID_FMT
" " UID_FMT
"\n", source_owner
, uid_shift
, 1u) < 0)
1368 return log_oom_debug();
1371 if (idmapping
== REMOUNT_IDMAPPING_HOST_OWNER_TO_TARGET_OWNER
) {
1372 /* Remap the owner of the bind mounted directory to the owner of the target directory
1373 * within the container. This way every file written by target directory owner within the
1374 * container to the bind-mounted directory will be owned by the original host user.
1375 * All other users will remain unmapped. */
1378 UID_FMT
" " UID_FMT
" " UID_FMT
"\n",
1379 source_owner
, dest_owner
, 1u) < 0)
1380 return log_oom_debug();
1383 /* We always assign the same UID and GID ranges */
1384 userns_fd
= userns_acquire(line
, line
);
1386 return log_debug_errno(userns_fd
, "Failed to acquire new userns: %m");
1388 return TAKE_FD(userns_fd
);
1391 int remount_idmap_fd(
1397 assert(userns_fd
>= 0);
1399 /* This remounts all specified paths with the specified userns as idmap. It will do so in the
1400 * order specified in the strv: the expectation is that the top-level directories are at the
1401 * beginning, and nested directories in the right, so that the tree can be built correctly from left
1404 size_t n
= strv_length(paths
);
1405 if (n
== 0) /* Nothing to do? */
1408 int *mount_fds
= NULL
;
1409 size_t n_mounts_fds
= 0;
1411 mount_fds
= new(int, n
);
1413 return log_oom_debug();
1415 CLEANUP_ARRAY(mount_fds
, n_mounts_fds
, close_many_and_free
);
1417 for (size_t i
= 0; i
< n
; i
++) {
1420 /* Clone the mount point */
1421 mntfd
= mount_fds
[n_mounts_fds
] = open_tree(-EBADF
, paths
[i
], OPEN_TREE_CLONE
| OPEN_TREE_CLOEXEC
);
1422 if (mount_fds
[n_mounts_fds
] < 0)
1423 return log_debug_errno(errno
, "Failed to open tree of mounted filesystem '%s': %m", paths
[i
]);
1427 /* Set the user namespace mapping attribute on the cloned mount point */
1428 if (mount_setattr(mntfd
, "", AT_EMPTY_PATH
,
1429 &(struct mount_attr
) {
1430 .attr_set
= MOUNT_ATTR_IDMAP
,
1431 .userns_fd
= userns_fd
,
1432 }, sizeof(struct mount_attr
)) < 0)
1433 return log_debug_errno(errno
, "Failed to change bind mount attributes for clone of '%s': %m", paths
[i
]);
1436 for (size_t i
= n
; i
> 0; i
--) { /* Unmount the paths right-to-left */
1437 /* Remove the old mount points now that we have a idmapped mounts as replacement for all of them */
1438 r
= umount_verbose(LOG_DEBUG
, paths
[i
-1], UMOUNT_NOFOLLOW
);
1443 for (size_t i
= 0; i
< n
; i
++) { /* Mount the replacement mounts left-to-right */
1444 /* And place the cloned version in its place */
1445 log_debug("Mounting idmapped fs to '%s'", paths
[i
]);
1446 if (move_mount(mount_fds
[i
], "", -EBADF
, paths
[i
], MOVE_MOUNT_F_EMPTY_PATH
) < 0)
1447 return log_debug_errno(errno
, "Failed to attach UID mapped mount to '%s': %m", paths
[i
]);
1453 int remount_idmap(char **p
, uid_t uid_shift
, uid_t uid_range
, uid_t source_owner
, uid_t dest_owner
,RemountIdmapping idmapping
) {
1454 _cleanup_close_
int userns_fd
= -EBADF
;
1456 userns_fd
= make_userns(uid_shift
, uid_range
, source_owner
, dest_owner
, idmapping
);
1460 return remount_idmap_fd(p
, userns_fd
);
1463 typedef struct SubMount
{
1468 static void sub_mount_clear(SubMount
*s
) {
1471 s
->path
= mfree(s
->path
);
1472 s
->mount_fd
= safe_close(s
->mount_fd
);
1475 static void sub_mount_array_free(SubMount
*s
, size_t n
) {
1476 assert(s
|| n
== 0);
1478 for (size_t i
= 0; i
< n
; i
++)
1479 sub_mount_clear(s
+ i
);
1484 static int sub_mount_compare(const SubMount
*a
, const SubMount
*b
) {
1490 return path_compare(a
->path
, b
->path
);
1493 static void sub_mount_drop(SubMount
*s
, size_t n
) {
1494 assert(s
|| n
== 0);
1496 for (size_t m
= 0, i
= 1; i
< n
; i
++) {
1497 if (path_startswith(s
[i
].path
, s
[m
].path
))
1498 sub_mount_clear(s
+ i
);
1504 static int get_sub_mounts(
1506 SubMount
**ret_mounts
,
1507 size_t *ret_n_mounts
) {
1508 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
1509 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
1510 SubMount
*mounts
= NULL
;
1514 CLEANUP_ARRAY(mounts
, n
, sub_mount_array_free
);
1518 assert(ret_n_mounts
);
1520 r
= libmount_parse("/proc/self/mountinfo", NULL
, &table
, &iter
);
1522 return log_debug_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
1525 _cleanup_close_
int mount_fd
= -EBADF
;
1526 _cleanup_free_
char *p
= NULL
;
1527 struct libmnt_fs
*fs
;
1531 r
= mnt_table_next_fs(table
, iter
, &fs
);
1535 return log_debug_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
1537 path
= mnt_fs_get_target(fs
);
1541 if (isempty(path_startswith(path
, prefix
)))
1544 id1
= mnt_fs_get_id(fs
);
1545 r
= path_get_mnt_id(path
, &id2
);
1547 log_debug_errno(r
, "Failed to get mount ID of '%s', ignoring: %m", path
);
1551 /* The path may be hidden by another over-mount or already remounted. */
1552 log_debug("The mount IDs of '%s' obtained by libmount and path_get_mnt_id() are different (%i vs %i), ignoring.",
1557 mount_fd
= open(path
, O_CLOEXEC
|O_PATH
);
1559 if (errno
== ENOENT
) /* The path may be hidden by another over-mount or already unmounted. */
1562 return log_debug_errno(errno
, "Failed to open subtree of mounted filesystem '%s': %m", path
);
1567 return log_oom_debug();
1569 if (!GREEDY_REALLOC(mounts
, n
+ 1))
1570 return log_oom_debug();
1572 mounts
[n
++] = (SubMount
) {
1573 .path
= TAKE_PTR(p
),
1574 .mount_fd
= TAKE_FD(mount_fd
),
1578 typesafe_qsort(mounts
, n
, sub_mount_compare
);
1579 sub_mount_drop(mounts
, n
);
1581 *ret_mounts
= TAKE_PTR(mounts
);
1586 int bind_mount_submounts(
1588 const char *target
) {
1590 SubMount
*mounts
= NULL
;
1594 /* Bind mounts all child mounts of 'source' to 'target'. Useful when setting up a new procfs instance
1595 * with new mount options to copy the original submounts over. */
1600 CLEANUP_ARRAY(mounts
, n
, sub_mount_array_free
);
1602 r
= get_sub_mounts(source
, &mounts
, &n
);
1606 FOREACH_ARRAY(m
, mounts
, n
) {
1607 _cleanup_free_
char *t
= NULL
;
1610 if (isempty(m
->path
))
1613 assert_se(suffix
= path_startswith(m
->path
, source
));
1615 t
= path_join(target
, suffix
);
1619 r
= path_is_mount_point(t
);
1621 log_debug_errno(r
, "Failed to detect if '%s' already is a mount point, ignoring: %m", t
);
1625 log_debug("Not bind mounting '%s' from '%s' to '%s', since there's already a mountpoint.", suffix
, source
, target
);
1629 r
= mount_follow_verbose(LOG_DEBUG
, FORMAT_PROC_FD_PATH(m
->mount_fd
), t
, NULL
, MS_BIND
|MS_REC
, NULL
);
1630 if (r
< 0 && ret
== 0)
1637 int make_mount_point_inode_from_stat(const struct stat
*st
, const char *dest
, mode_t mode
) {
1641 if (S_ISDIR(st
->st_mode
))
1642 return mkdir_label(dest
, mode
);
1644 return RET_NERRNO(mknod(dest
, S_IFREG
|(mode
& ~0111), 0));
1647 int make_mount_point_inode_from_path(const char *source
, const char *dest
, mode_t mode
) {
1653 if (stat(source
, &st
) < 0)
1656 return make_mount_point_inode_from_stat(&st
, dest
, mode
);
1659 int trigger_automount_at(int dir_fd
, const char *path
) {
1660 _cleanup_free_
char *nested
= NULL
;
1662 assert(dir_fd
>= 0 || dir_fd
== AT_FDCWD
);
1664 nested
= path_join(path
, "a");
1668 (void) faccessat(dir_fd
, nested
, F_OK
, 0);
1673 unsigned long credentials_fs_mount_flags(bool ro
) {
1674 /* A tight set of mount flags for credentials mounts */
1675 return MS_NODEV
|MS_NOEXEC
|MS_NOSUID
|ms_nosymfollow_supported()|(ro
? MS_RDONLY
: 0);
1678 int mount_credentials_fs(const char *path
, size_t size
, bool ro
) {
1679 _cleanup_free_
char *opts
= NULL
;
1680 int r
, noswap_supported
;
1682 /* Mounts a file system we can place credentials in, i.e. with tight access modes right from the
1683 * beginning, and ideally swapping turned off. In order of preference:
1685 * 1. tmpfs if it supports "noswap"
1687 * 3. tmpfs if it doesn't support "noswap"
1690 noswap_supported
= mount_option_supported("tmpfs", "noswap", NULL
); /* Check explicitly to avoid kmsg noise */
1691 if (noswap_supported
> 0) {
1692 _cleanup_free_
char *noswap_opts
= NULL
;
1694 if (asprintf(&noswap_opts
, "mode=0700,nr_inodes=1024,size=%zu,noswap", size
) < 0)
1697 /* Best case: tmpfs with noswap (needs kernel >= 6.3) */
1699 r
= mount_nofollow_verbose(
1704 credentials_fs_mount_flags(ro
),
1710 r
= mount_nofollow_verbose(
1715 credentials_fs_mount_flags(ro
),
1720 if (asprintf(&opts
, "mode=0700,nr_inodes=1024,size=%zu", size
) < 0)
1723 return mount_nofollow_verbose(
1728 credentials_fs_mount_flags(ro
),
1733 int error_log_level
,
1736 unsigned long flags
,
1737 const char *options
,
1740 _cleanup_close_
int fs_fd
= -EBADF
, mnt_fd
= -EBADF
;
1741 _cleanup_free_
char *o
= NULL
;
1748 r
= mount_option_mangle(options
, flags
, &f
, &o
);
1750 return log_full_errno(
1751 error_log_level
, r
, "Failed to mangle mount options %s: %m",
1754 if (DEBUG_LOGGING
) {
1755 _cleanup_free_
char *fl
= NULL
;
1756 (void) mount_flags_to_string(f
, &fl
);
1758 log_debug("Creating mount fd for %s (%s) (%s \"%s\")...",
1759 strna(what
), strna(type
), strnull(fl
), strempty(o
));
1762 fs_fd
= fsopen(type
, FSOPEN_CLOEXEC
);
1764 return log_full_errno(error_log_level
, errno
, "Failed to open superblock for \"%s\": %m", type
);
1766 if (fsconfig(fs_fd
, FSCONFIG_SET_STRING
, "source", what
, 0) < 0)
1767 return log_full_errno(error_log_level
, errno
, "Failed to set mount source for \"%s\" to \"%s\": %m", type
, what
);
1769 if (FLAGS_SET(f
, MS_RDONLY
))
1770 if (fsconfig(fs_fd
, FSCONFIG_SET_FLAG
, "ro", NULL
, 0) < 0)
1771 return log_full_errno(error_log_level
, errno
, "Failed to set read only mount flag for \"%s\": %m", type
);
1773 for (const char *p
= o
;;) {
1774 _cleanup_free_
char *word
= NULL
;
1777 r
= extract_first_word(&p
, &word
, ",", EXTRACT_KEEP_QUOTE
);
1779 return log_full_errno(error_log_level
, r
, "Failed to parse mount option string \"%s\": %m", o
);
1783 eq
= strchr(word
, '=');
1788 if (fsconfig(fs_fd
, FSCONFIG_SET_STRING
, word
, eq
, 0) < 0)
1789 return log_full_errno(error_log_level
, errno
, "Failed to set mount option \"%s=%s\" for \"%s\": %m", word
, eq
, type
);
1791 if (fsconfig(fs_fd
, FSCONFIG_SET_FLAG
, word
, NULL
, 0) < 0)
1792 return log_full_errno(error_log_level
, errno
, "Failed to set mount flag \"%s\" for \"%s\": %m", word
, type
);
1796 if (fsconfig(fs_fd
, FSCONFIG_CMD_CREATE
, NULL
, NULL
, 0) < 0)
1797 return log_full_errno(error_log_level
, errno
, "Failed to realize fs fd for \"%s\" (\"%s\"): %m", what
, type
);
1799 mnt_fd
= fsmount(fs_fd
, FSMOUNT_CLOEXEC
, 0);
1801 return log_full_errno(error_log_level
, errno
, "Failed to create mount fd for \"%s\" (\"%s\"): %m", what
, type
);
1803 if (mount_setattr(mnt_fd
, "", AT_EMPTY_PATH
|AT_RECURSIVE
,
1804 &(struct mount_attr
) {
1805 .attr_set
= ms_flags_to_mount_attr(f
) | (userns_fd
>= 0 ? MOUNT_ATTR_IDMAP
: 0),
1806 .userns_fd
= userns_fd
,
1807 }, MOUNT_ATTR_SIZE_VER0
) < 0)
1808 return log_full_errno(error_log_level
,
1810 "Failed to set mount flags for \"%s\" (\"%s\"): %m",
1814 return TAKE_FD(mnt_fd
);