1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include <sys/statvfs.h>
11 #include "alloc-util.h"
12 #include "extract-word.h"
17 #include "libmount-util.h"
18 #include "mount-util.h"
19 #include "mountpoint-util.h"
20 #include "parse-util.h"
21 #include "path-util.h"
23 #include "stdio-util.h"
24 #include "string-util.h"
27 int umount_recursive(const char *prefix
, int flags
) {
31 /* Try to umount everything recursively below a
32 * directory. Also, take care of stacked mounts, and keep
33 * unmounting them until they are gone. */
36 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
37 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
41 r
= libmount_parse("/proc/self/mountinfo", NULL
, &table
, &iter
);
43 return log_debug_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
49 r
= mnt_table_next_fs(table
, iter
, &fs
);
53 return log_debug_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
55 path
= mnt_fs_get_target(fs
);
59 if (!path_startswith(path
, prefix
))
62 if (umount2(path
, flags
) < 0) {
63 r
= log_debug_errno(errno
, "Failed to umount %s: %m", path
);
67 log_debug("Successfully unmounted %s", path
);
80 static int get_mount_flags(const char *path
, unsigned long *flags
) {
83 if (statvfs(path
, &buf
) < 0)
89 /* Use this function only if you do not have direct access to /proc/self/mountinfo but the caller can open it
90 * for you. This is the case when /proc is masked or not mounted. Otherwise, use bind_remount_recursive. */
91 int bind_remount_recursive_with_mountinfo(
93 unsigned long new_flags
,
94 unsigned long flags_mask
,
96 FILE *proc_self_mountinfo
) {
98 _cleanup_set_free_free_ Set
*done
= NULL
;
99 _cleanup_free_
char *cleaned
= NULL
;
102 assert(proc_self_mountinfo
);
104 /* Recursively remount a directory (and all its submounts) read-only or read-write. If the directory is already
105 * mounted, we reuse the mount and simply mark it MS_BIND|MS_RDONLY (or remove the MS_RDONLY for read-write
106 * operation). If it isn't we first make it one. Afterwards we apply MS_BIND|MS_RDONLY (or remove MS_RDONLY) to
107 * all submounts we can access, too. When mounts are stacked on the same mount point we only care for each
108 * individual "top-level" mount on each point, as we cannot influence/access the underlying mounts anyway. We
109 * do not have any effect on future submounts that might get propagated, they migt be writable. This includes
110 * future submounts that have been triggered via autofs.
112 * If the "blacklist" parameter is specified it may contain a list of subtrees to exclude from the
113 * remount operation. Note that we'll ignore the blacklist for the top-level path. */
115 cleaned
= strdup(prefix
);
119 path_simplify(cleaned
, false);
121 done
= set_new(&path_hash_ops
);
126 _cleanup_set_free_free_ Set
*todo
= NULL
;
127 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
128 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
129 bool top_autofs
= false;
131 unsigned long orig_flags
;
133 todo
= set_new(&path_hash_ops
);
137 rewind(proc_self_mountinfo
);
139 r
= libmount_parse("/proc/self/mountinfo", proc_self_mountinfo
, &table
, &iter
);
141 return log_debug_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
144 struct libmnt_fs
*fs
;
145 const char *path
, *type
;
147 r
= mnt_table_next_fs(table
, iter
, &fs
);
151 return log_debug_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
153 path
= mnt_fs_get_target(fs
);
154 type
= mnt_fs_get_fstype(fs
);
158 if (!path_startswith(path
, cleaned
))
161 /* Ignore this mount if it is blacklisted, but only if it isn't the top-level mount
162 * we shall operate on. */
163 if (!path_equal(path
, cleaned
)) {
164 bool blacklisted
= false;
167 STRV_FOREACH(i
, blacklist
) {
168 if (path_equal(*i
, cleaned
))
171 if (!path_startswith(*i
, cleaned
))
174 if (path_startswith(path
, *i
)) {
176 log_debug("Not remounting %s blacklisted by %s, called for %s",
185 /* Let's ignore autofs mounts. If they aren't
186 * triggered yet, we want to avoid triggering
187 * them, as we don't make any guarantees for
188 * future submounts anyway. If they are
189 * already triggered, then we will find
190 * another entry for this. */
191 if (streq(type
, "autofs")) {
192 top_autofs
= top_autofs
|| path_equal(path
, cleaned
);
196 if (!set_contains(done
, path
)) {
197 r
= set_put_strdup(todo
, path
);
203 /* If we have no submounts to process anymore and if
204 * the root is either already done, or an autofs, we
206 if (set_isempty(todo
) &&
207 (top_autofs
|| set_contains(done
, cleaned
)))
210 if (!set_contains(done
, cleaned
) &&
211 !set_contains(todo
, cleaned
)) {
212 /* The prefix directory itself is not yet a mount, make it one. */
213 if (mount(cleaned
, cleaned
, NULL
, MS_BIND
|MS_REC
, NULL
) < 0)
217 (void) get_mount_flags(cleaned
, &orig_flags
);
218 orig_flags
&= ~MS_RDONLY
;
220 if (mount(NULL
, cleaned
, NULL
, (orig_flags
& ~flags_mask
)|MS_BIND
|MS_REMOUNT
|new_flags
, NULL
) < 0)
223 log_debug("Made top-level directory %s a mount point.", prefix
);
225 r
= set_put_strdup(done
, cleaned
);
230 while ((x
= set_steal_first(todo
))) {
232 r
= set_consume(done
, x
);
233 if (IN_SET(r
, 0, -EEXIST
))
238 /* Deal with mount points that are obstructed by a later mount */
239 r
= path_is_mount_point(x
, NULL
, 0);
240 if (IN_SET(r
, 0, -ENOENT
))
242 if (IN_SET(r
, -EACCES
, -EPERM
)) {
243 /* Even if root user invoke this, submounts under private FUSE or NFS mount points
244 * may not be acceessed. E.g.,
246 * $ bindfs --no-allow-other ~/mnt/mnt ~/mnt/mnt
247 * $ bindfs --no-allow-other ~/mnt ~/mnt
249 * Then, root user cannot access the mount point ~/mnt/mnt.
250 * In such cases, the submounts are ignored, as we have no way to manage them. */
251 log_debug_errno(r
, "Failed to determine '%s' is mount point or not, ignoring: %m", x
);
257 /* Try to reuse the original flag set */
259 (void) get_mount_flags(x
, &orig_flags
);
260 orig_flags
&= ~MS_RDONLY
;
262 if (mount(NULL
, x
, NULL
, (orig_flags
& ~flags_mask
)|MS_BIND
|MS_REMOUNT
|new_flags
, NULL
) < 0)
265 log_debug("Remounted %s read-only.", x
);
270 int bind_remount_recursive(const char *prefix
, unsigned long new_flags
, unsigned long flags_mask
, char **blacklist
) {
271 _cleanup_fclose_
FILE *proc_self_mountinfo
= NULL
;
274 r
= fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo
);
278 return bind_remount_recursive_with_mountinfo(prefix
, new_flags
, flags_mask
, blacklist
, proc_self_mountinfo
);
281 int mount_move_root(const char *path
) {
287 if (mount(path
, "/", NULL
, MS_MOVE
, NULL
) < 0)
299 int repeat_unmount(const char *path
, int flags
) {
304 /* If there are multiple mounts on a mount point, this
305 * removes them all */
308 if (umount2(path
, flags
) < 0) {
320 const char* mode_to_inaccessible_node(mode_t mode
) {
321 /* This function maps a node type to a corresponding inaccessible file node. These nodes are created during
322 * early boot by PID 1. In some cases we lacked the privs to create the character and block devices (maybe
323 * because we run in an userns environment, or miss CAP_SYS_MKNOD, or run with a devices policy that excludes
324 * device nodes with major and minor of 0), but that's fine, in that case we use an AF_UNIX file node instead,
325 * which is not the same, but close enough for most uses. And most importantly, the kernel allows bind mounts
326 * from socket nodes to any non-directory file nodes, and that's the most important thing that matters. */
328 switch(mode
& S_IFMT
) {
330 return "/run/systemd/inaccessible/reg";
333 return "/run/systemd/inaccessible/dir";
336 if (access("/run/systemd/inaccessible/chr", F_OK
) == 0)
337 return "/run/systemd/inaccessible/chr";
338 return "/run/systemd/inaccessible/sock";
341 if (access("/run/systemd/inaccessible/blk", F_OK
) == 0)
342 return "/run/systemd/inaccessible/blk";
343 return "/run/systemd/inaccessible/sock";
346 return "/run/systemd/inaccessible/fifo";
349 return "/run/systemd/inaccessible/sock";
354 #define FLAG(name) (flags & name ? STRINGIFY(name) "|" : "")
355 static char* mount_flags_to_string(long unsigned flags
) {
357 _cleanup_free_
char *y
= NULL
;
358 long unsigned overflow
;
360 overflow
= flags
& ~(MS_RDONLY
|
385 if (flags
== 0 || overflow
!= 0)
386 if (asprintf(&y
, "%lx", overflow
) < 0)
389 x
= strjoin(FLAG(MS_RDONLY
),
393 FLAG(MS_SYNCHRONOUS
),
411 FLAG(MS_STRICTATIME
),
417 x
[strlen(x
) - 1] = '\0'; /* truncate the last | */
427 const char *options
) {
429 _cleanup_free_
char *fl
= NULL
, *o
= NULL
;
433 r
= mount_option_mangle(options
, flags
, &f
, &o
);
435 return log_full_errno(error_log_level
, r
,
436 "Failed to mangle mount options %s: %m",
439 fl
= mount_flags_to_string(f
);
441 if ((f
& MS_REMOUNT
) && !what
&& !type
)
442 log_debug("Remounting %s (%s \"%s\")...",
443 where
, strnull(fl
), strempty(o
));
444 else if (!what
&& !type
)
445 log_debug("Mounting %s (%s \"%s\")...",
446 where
, strnull(fl
), strempty(o
));
447 else if ((f
& MS_BIND
) && !type
)
448 log_debug("Bind-mounting %s on %s (%s \"%s\")...",
449 what
, where
, strnull(fl
), strempty(o
));
450 else if (f
& MS_MOVE
)
451 log_debug("Moving mount %s → %s (%s \"%s\")...",
452 what
, where
, strnull(fl
), strempty(o
));
454 log_debug("Mounting %s on %s (%s \"%s\")...",
455 strna(type
), where
, strnull(fl
), strempty(o
));
456 if (mount(what
, where
, type
, f
, o
) < 0)
457 return log_full_errno(error_log_level
, errno
,
458 "Failed to mount %s (type %s) on %s (%s \"%s\"): %m",
459 strna(what
), strna(type
), where
, strnull(fl
), strempty(o
));
463 int umount_verbose(const char *what
) {
464 log_debug("Umounting %s...", what
);
465 if (umount(what
) < 0)
466 return log_error_errno(errno
, "Failed to unmount %s: %m", what
);
470 int mount_option_mangle(
472 unsigned long mount_flags
,
473 unsigned long *ret_mount_flags
,
474 char **ret_remaining_options
) {
476 const struct libmnt_optmap
*map
;
477 _cleanup_free_
char *ret
= NULL
;
481 /* This extracts mount flags from the mount options, and store
482 * non-mount-flag options to '*ret_remaining_options'.
484 * "rw,nosuid,nodev,relatime,size=1630748k,mode=700,uid=1000,gid=1000"
485 * is split to MS_NOSUID|MS_NODEV|MS_RELATIME and
486 * "size=1630748k,mode=700,uid=1000,gid=1000".
487 * See more examples in test-mount-utils.c.
489 * Note that if 'options' does not contain any non-mount-flag options,
490 * then '*ret_remaining_options' is set to NULL instead of empty string.
491 * Note that this does not check validity of options stored in
492 * '*ret_remaining_options'.
493 * Note that if 'options' is NULL, then this just copies 'mount_flags'
494 * to '*ret_mount_flags'. */
496 assert(ret_mount_flags
);
497 assert(ret_remaining_options
);
499 map
= mnt_get_builtin_optmap(MNT_LINUX_MAP
);
505 _cleanup_free_
char *word
= NULL
;
506 const struct libmnt_optmap
*ent
;
508 r
= extract_first_word(&p
, &word
, ",", EXTRACT_QUOTES
);
514 for (ent
= map
; ent
->name
; ent
++) {
515 /* All entries in MNT_LINUX_MAP do not take any argument.
516 * Thus, ent->name does not contain "=" or "[=]". */
517 if (!streq(word
, ent
->name
))
520 if (!(ent
->mask
& MNT_INVERT
))
521 mount_flags
|= ent
->id
;
522 else if (mount_flags
& ent
->id
)
523 mount_flags
^= ent
->id
;
528 /* If 'word' is not a mount flag, then store it in '*ret_remaining_options'. */
529 if (!ent
->name
&& !strextend_with_separator(&ret
, ",", word
, NULL
))
533 *ret_mount_flags
= mount_flags
;
534 *ret_remaining_options
= TAKE_PTR(ret
);