]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/mount-util.c
b65416b03bdf2eb13e55f6cbcdd3eddd2ffba4f2
[thirdparty/systemd.git] / src / shared / mount-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <sys/mount.h>
6 #include <sys/stat.h>
7 #include <sys/statvfs.h>
8 #include <unistd.h>
9 #include <linux/loop.h>
10 #if WANT_LINUX_FS_H
11 #include <linux/fs.h>
12 #endif
13
14 #include "alloc-util.h"
15 #include "chase.h"
16 #include "dissect-image.h"
17 #include "exec-util.h"
18 #include "extract-word.h"
19 #include "fd-util.h"
20 #include "fileio.h"
21 #include "fs-util.h"
22 #include "glyph-util.h"
23 #include "hashmap.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"
36 #include "set.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"
42 #include "strv.h"
43 #include "tmpfile-util.h"
44 #include "user-util.h"
45
46 int umount_recursive_full(const char *prefix, int flags, char **keep) {
47 _cleanup_fclose_ FILE *f = NULL;
48 int n = 0, r;
49
50 /* Try to umount everything recursively below a directory. Also, take care of stacked mounts, and
51 * keep unmounting them until they are gone. */
52
53 f = fopen("/proc/self/mountinfo", "re"); /* Pin the file, in case we unmount /proc/ as part of the logic here */
54 if (!f)
55 return log_debug_errno(errno, "Failed to open /proc/self/mountinfo: %m");
56
57 for (;;) {
58 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
59 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
60 bool again = false;
61
62 r = libmount_parse("/proc/self/mountinfo", f, &table, &iter);
63 if (r < 0)
64 return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
65
66 for (;;) {
67 bool shall_keep = false;
68 struct libmnt_fs *fs;
69 const char *path;
70
71 r = mnt_table_next_fs(table, iter, &fs);
72 if (r == 1)
73 break;
74 if (r < 0)
75 return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
76
77 path = mnt_fs_get_target(fs);
78 if (!path)
79 continue;
80
81 if (prefix && !path_startswith(path, prefix)) {
82 log_trace("Not unmounting %s, outside of prefix: %s", path, prefix);
83 continue;
84 }
85
86 STRV_FOREACH(k, keep)
87 /* Match against anything in the path to the dirs to keep, or below the dirs to keep */
88 if (path_startswith(path, *k) || path_startswith(*k, path)) {
89 shall_keep = true;
90 break;
91 }
92 if (shall_keep) {
93 log_debug("Not unmounting %s, referenced by keep list.", path);
94 continue;
95 }
96
97 if (umount2(path, flags | UMOUNT_NOFOLLOW) < 0) {
98 log_debug_errno(errno, "Failed to umount %s, ignoring: %m", path);
99 continue;
100 }
101
102 log_trace("Successfully unmounted %s", path);
103
104 again = true;
105 n++;
106
107 break;
108 }
109
110 if (!again)
111 break;
112
113 rewind(f);
114 }
115
116 return n;
117 }
118
119 #define MS_CONVERTIBLE_FLAGS (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_NOSYMFOLLOW)
120
121 static uint64_t ms_flags_to_mount_attr(unsigned long a) {
122 uint64_t f = 0;
123
124 if (FLAGS_SET(a, MS_RDONLY))
125 f |= MOUNT_ATTR_RDONLY;
126
127 if (FLAGS_SET(a, MS_NOSUID))
128 f |= MOUNT_ATTR_NOSUID;
129
130 if (FLAGS_SET(a, MS_NODEV))
131 f |= MOUNT_ATTR_NODEV;
132
133 if (FLAGS_SET(a, MS_NOEXEC))
134 f |= MOUNT_ATTR_NOEXEC;
135
136 if (FLAGS_SET(a, MS_NOSYMFOLLOW))
137 f |= MOUNT_ATTR_NOSYMFOLLOW;
138
139 return f;
140 }
141
142 static bool skip_mount_set_attr = false;
143
144 /* Use this function only if you do not have direct access to /proc/self/mountinfo but the caller can open it
145 * for you. This is the case when /proc is masked or not mounted. Otherwise, use bind_remount_recursive. */
146 int bind_remount_recursive_with_mountinfo(
147 const char *prefix,
148 unsigned long new_flags,
149 unsigned long flags_mask,
150 char **deny_list,
151 FILE *proc_self_mountinfo) {
152
153 _cleanup_fclose_ FILE *proc_self_mountinfo_opened = NULL;
154 _cleanup_set_free_ Set *done = NULL;
155 unsigned n_tries = 0;
156 int r;
157
158 assert(prefix);
159
160 if ((flags_mask & ~MS_CONVERTIBLE_FLAGS) == 0 && strv_isempty(deny_list) && !skip_mount_set_attr) {
161 /* Let's take a shortcut for all the flags we know how to convert into mount_setattr() flags */
162
163 if (mount_setattr(AT_FDCWD, prefix, AT_SYMLINK_NOFOLLOW|AT_RECURSIVE,
164 &(struct mount_attr) {
165 .attr_set = ms_flags_to_mount_attr(new_flags & flags_mask),
166 .attr_clr = ms_flags_to_mount_attr(~new_flags & flags_mask),
167 }, MOUNT_ATTR_SIZE_VER0) < 0) {
168
169 log_debug_errno(errno, "mount_setattr() failed, falling back to classic remounting: %m");
170
171 /* We fall through to classic behaviour if not supported (i.e. kernel < 5.12). We
172 * also do this for all other kinds of errors since they are so many different, and
173 * mount_setattr() has no graceful mode where it continues despite seeing errors one
174 * some mounts, but we want that. Moreover mount_setattr() only works on the mount
175 * point inode itself, not a non-mount point inode, and we want to support arbitrary
176 * prefixes here. */
177
178 if (ERRNO_IS_NOT_SUPPORTED(errno)) /* if not supported, then don't bother at all anymore */
179 skip_mount_set_attr = true;
180 } else
181 return 0; /* Nice, this worked! */
182 }
183
184 if (!proc_self_mountinfo) {
185 r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo_opened);
186 if (r < 0)
187 return r;
188
189 proc_self_mountinfo = proc_self_mountinfo_opened;
190 }
191
192 /* Recursively remount a directory (and all its submounts) with desired flags (MS_READONLY,
193 * MS_NOSUID, MS_NOEXEC). If the directory is already mounted, we reuse the mount and simply mark it
194 * MS_BIND|MS_RDONLY (or remove the MS_RDONLY for read-write operation), ditto for other flags. If it
195 * isn't we first make it one. Afterwards we apply (or remove) the flags to all submounts we can
196 * access, too. When mounts are stacked on the same mount point we only care for each individual
197 * "top-level" mount on each point, as we cannot influence/access the underlying mounts anyway. We do
198 * not have any effect on future submounts that might get propagated, they might be writable
199 * etc. This includes future submounts that have been triggered via autofs. Also note that we can't
200 * operate atomically here. Mounts established while we process the tree might or might not get
201 * noticed and thus might or might not be covered.
202 *
203 * If the "deny_list" parameter is specified it may contain a list of subtrees to exclude from the
204 * remount operation. Note that we'll ignore the deny list for the top-level path. */
205
206 for (;;) {
207 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
208 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
209 _cleanup_hashmap_free_ Hashmap *todo = NULL;
210 bool top_autofs = false;
211
212 if (n_tries++ >= 32) /* Let's not retry this loop forever */
213 return -EBUSY;
214
215 rewind(proc_self_mountinfo);
216
217 r = libmount_parse("/proc/self/mountinfo", proc_self_mountinfo, &table, &iter);
218 if (r < 0)
219 return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
220
221 for (;;) {
222 _cleanup_free_ char *d = NULL;
223 const char *path, *type, *opts;
224 unsigned long flags = 0;
225 struct libmnt_fs *fs;
226
227 r = mnt_table_next_fs(table, iter, &fs);
228 if (r == 1) /* EOF */
229 break;
230 if (r < 0)
231 return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
232
233 path = mnt_fs_get_target(fs);
234 if (!path)
235 continue;
236
237 if (!path_startswith(path, prefix))
238 continue;
239
240 type = mnt_fs_get_fstype(fs);
241 if (!type)
242 continue;
243
244 /* Let's ignore autofs mounts. If they aren't triggered yet, we want to avoid
245 * triggering them, as we don't make any guarantees for future submounts anyway. If
246 * they are already triggered, then we will find another entry for this. */
247 if (streq(type, "autofs")) {
248 top_autofs = top_autofs || path_equal(path, prefix);
249 continue;
250 }
251
252 if (set_contains(done, path))
253 continue;
254
255 /* Ignore this mount if it is deny-listed, but only if it isn't the top-level mount
256 * we shall operate on. */
257 if (!path_equal(path, prefix)) {
258 bool deny_listed = false;
259
260 STRV_FOREACH(i, deny_list) {
261 if (path_equal(*i, prefix))
262 continue;
263
264 if (!path_startswith(*i, prefix))
265 continue;
266
267 if (path_startswith(path, *i)) {
268 deny_listed = true;
269 log_trace("Not remounting %s deny-listed by %s, called for %s", path, *i, prefix);
270 break;
271 }
272 }
273
274 if (deny_listed)
275 continue;
276 }
277
278 opts = mnt_fs_get_vfs_options(fs);
279 if (opts) {
280 r = mnt_optstr_get_flags(opts, &flags, mnt_get_builtin_optmap(MNT_LINUX_MAP));
281 if (r < 0)
282 log_debug_errno(r, "Could not get flags for '%s', ignoring: %m", path);
283 }
284
285 d = strdup(path);
286 if (!d)
287 return -ENOMEM;
288
289 r = hashmap_ensure_put(&todo, &path_hash_ops_free, d, ULONG_TO_PTR(flags));
290 if (r == -EEXIST)
291 /* If the same path was recorded, but with different mount flags, update it:
292 * it means a mount point is overmounted, and libmount returns the "bottom" (or
293 * older one) first, but we want to reapply the flags from the "top" (or newer
294 * one). See: https://github.com/systemd/systemd/issues/20032
295 * Note that this shouldn't really fail, as we were just told that the key
296 * exists, and it's an update so we want 'd' to be freed immediately. */
297 r = hashmap_update(todo, d, ULONG_TO_PTR(flags));
298 if (r < 0)
299 return r;
300 if (r > 0)
301 TAKE_PTR(d);
302 }
303
304 /* Check if the top-level directory was among what we have seen so far. For that check both
305 * 'done' and 'todo'. Also check 'top_autofs' because if the top-level dir is an autofs we'll
306 * not include it in either set but will set this bool. */
307 if (!set_contains(done, prefix) &&
308 !(top_autofs || hashmap_contains(todo, prefix))) {
309
310 /* The prefix directory itself is not yet a mount, make it one. */
311 r = mount_nofollow(prefix, prefix, NULL, MS_BIND|MS_REC, NULL);
312 if (r < 0)
313 return r;
314
315 /* Immediately rescan, so that we pick up the new mount's flags */
316 continue;
317 }
318
319 /* If we have no submounts to process anymore, we are done */
320 if (hashmap_isempty(todo))
321 return 0;
322
323 for (;;) {
324 unsigned long flags;
325 char *x = NULL;
326
327 /* Take the first mount from our list of mounts to still process */
328 flags = PTR_TO_ULONG(hashmap_steal_first_key_and_value(todo, (void**) &x));
329 if (!x)
330 break;
331
332 r = set_ensure_consume(&done, &path_hash_ops_free, x);
333 if (IN_SET(r, 0, -EEXIST))
334 continue; /* Already done */
335 if (r < 0)
336 return r;
337
338 /* Now, remount this with the new flags set, but exclude MS_RELATIME from it. (It's
339 * the default anyway, thus redundant, and in userns we'll get an error if we try to
340 * explicitly enable it) */
341 r = mount_nofollow(NULL, x, NULL, ((flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags) & ~MS_RELATIME, NULL);
342 if (r < 0) {
343 int q;
344
345 /* OK, so the remount of this entry failed. We'll ultimately ignore this in
346 * almost all cases (there are simply so many reasons why this can fail,
347 * think autofs, NFS, FUSE, …), but let's generate useful debug messages at
348 * the very least. */
349
350 q = path_is_mount_point(x, NULL, 0);
351 if (IN_SET(q, 0, -ENOENT)) {
352 /* Hmm, whaaaa? The mount point is not actually a mount point? Then
353 * it is either obstructed by a later mount or somebody has been
354 * racing against us and removed it. Either way the mount point
355 * doesn't matter to us, let's ignore it hence. */
356 log_debug_errno(r, "Mount point '%s' to remount is not a mount point anymore, ignoring remount failure: %m", x);
357 continue;
358 }
359 if (q < 0) /* Any other error on this? Just log and continue */
360 log_debug_errno(q, "Failed to determine whether '%s' is a mount point or not, ignoring: %m", x);
361
362 if (((flags ^ new_flags) & flags_mask & ~MS_RELATIME) == 0) { /* ignore MS_RELATIME while comparing */
363 log_debug_errno(r, "Couldn't remount '%s', but the flags already match what we want, hence ignoring: %m", x);
364 continue;
365 }
366
367 /* Make this fatal if this is the top-level mount */
368 if (path_equal(x, prefix))
369 return r;
370
371 /* If this is not the top-level mount, then handle this gracefully: log but
372 * otherwise ignore. With NFS, FUSE, autofs there are just too many reasons
373 * this might fail without a chance for us to do anything about it, let's
374 * hence be strict on the top-level mount and lenient on the inner ones. */
375 log_debug_errno(r, "Couldn't remount submount '%s' for unexpected reason, ignoring: %m", x);
376 continue;
377 }
378
379 log_trace("Remounted %s.", x);
380 }
381 }
382 }
383
384 int bind_remount_one_with_mountinfo(
385 const char *path,
386 unsigned long new_flags,
387 unsigned long flags_mask,
388 FILE *proc_self_mountinfo) {
389
390 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
391 unsigned long flags = 0;
392 struct libmnt_fs *fs;
393 const char *opts;
394 int r;
395
396 assert(path);
397 assert(proc_self_mountinfo);
398
399 if ((flags_mask & ~MS_CONVERTIBLE_FLAGS) == 0 && !skip_mount_set_attr) {
400 /* Let's take a shortcut for all the flags we know how to convert into mount_setattr() flags */
401
402 if (mount_setattr(AT_FDCWD, path, AT_SYMLINK_NOFOLLOW,
403 &(struct mount_attr) {
404 .attr_set = ms_flags_to_mount_attr(new_flags & flags_mask),
405 .attr_clr = ms_flags_to_mount_attr(~new_flags & flags_mask),
406 }, MOUNT_ATTR_SIZE_VER0) < 0) {
407
408 log_debug_errno(errno, "mount_setattr() didn't work, falling back to classic remounting: %m");
409
410 if (ERRNO_IS_NOT_SUPPORTED(errno)) /* if not supported, then don't bother at all anymore */
411 skip_mount_set_attr = true;
412 } else
413 return 0; /* Nice, this worked! */
414 }
415
416 rewind(proc_self_mountinfo);
417
418 table = mnt_new_table();
419 if (!table)
420 return -ENOMEM;
421
422 r = mnt_table_parse_stream(table, proc_self_mountinfo, "/proc/self/mountinfo");
423 if (r < 0)
424 return r;
425
426 fs = mnt_table_find_target(table, path, MNT_ITER_FORWARD);
427 if (!fs) {
428 if (laccess(path, F_OK) < 0) /* Hmm, it's not in the mount table, but does it exist at all? */
429 return -errno;
430
431 return -EINVAL; /* Not a mount point we recognize */
432 }
433
434 opts = mnt_fs_get_vfs_options(fs);
435 if (opts) {
436 r = mnt_optstr_get_flags(opts, &flags, mnt_get_builtin_optmap(MNT_LINUX_MAP));
437 if (r < 0)
438 log_debug_errno(r, "Could not get flags for '%s', ignoring: %m", path);
439 }
440
441 r = mount_nofollow(NULL, path, NULL, ((flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags) & ~MS_RELATIME, NULL);
442 if (r < 0) {
443 if (((flags ^ new_flags) & flags_mask & ~MS_RELATIME) != 0) /* Ignore MS_RELATIME again,
444 * since kernel adds it in
445 * everywhere, because it's the
446 * default. */
447 return r;
448
449 /* Let's handle redundant remounts gracefully */
450 log_debug_errno(r, "Failed to remount '%s' but flags already match what we want, ignoring: %m", path);
451 }
452
453 return 0;
454 }
455
456 static int mount_switch_root_pivot(int fd_newroot, const char *path) {
457 assert(fd_newroot >= 0);
458 assert(path);
459
460 /* Change into the new rootfs. */
461 if (fchdir(fd_newroot) < 0)
462 return log_debug_errno(errno, "Failed to chdir into new rootfs '%s': %m", path);
463
464 /* Let the kernel tuck the new root under the old one. */
465 if (pivot_root(".", ".") < 0)
466 return log_debug_errno(errno, "Failed to pivot root to new rootfs '%s': %m", path);
467
468 /* Get rid of the old root and reveal our brand new root. (This will always operate on the top-most
469 * mount on our cwd, regardless what our current directory actually points to.) */
470 if (umount2(".", MNT_DETACH) < 0)
471 return log_debug_errno(errno, "Failed to unmount old rootfs: %m");
472
473 return 0;
474 }
475
476 static int mount_switch_root_move(int fd_newroot, const char *path) {
477 assert(fd_newroot >= 0);
478 assert(path);
479
480 /* Change into the new rootfs. */
481 if (fchdir(fd_newroot) < 0)
482 return log_debug_errno(errno, "Failed to chdir into new rootfs '%s': %m", path);
483
484 /* Move the new root fs */
485 if (mount(".", "/", NULL, MS_MOVE, NULL) < 0)
486 return log_debug_errno(errno, "Failed to move new rootfs '%s': %m", path);
487
488 /* Also change root dir */
489 if (chroot(".") < 0)
490 return log_debug_errno(errno, "Failed to chroot to new rootfs '%s': %m", path);
491
492 return 0;
493 }
494
495 int mount_switch_root_full(const char *path, unsigned long mount_propagation_flag, bool force_ms_move) {
496 _cleanup_close_ int fd_newroot = -EBADF;
497 int r;
498
499 assert(path);
500 assert(mount_propagation_flag_is_valid(mount_propagation_flag));
501
502 fd_newroot = open(path, O_PATH|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
503 if (fd_newroot < 0)
504 return log_debug_errno(errno, "Failed to open new rootfs '%s': %m", path);
505
506 if (!force_ms_move) {
507 r = mount_switch_root_pivot(fd_newroot, path);
508 if (r < 0) {
509 log_debug_errno(r, "Failed to pivot into new rootfs '%s', will try to use MS_MOVE instead: %m", path);
510 force_ms_move = true;
511 }
512 }
513 if (force_ms_move) {
514 /* Failed to pivot_root() fallback to MS_MOVE. For example, this may happen if the rootfs is
515 * an initramfs in which case pivot_root() isn't supported. */
516 r = mount_switch_root_move(fd_newroot, path);
517 if (r < 0)
518 return log_debug_errno(r, "Failed to switch to new rootfs '%s' with MS_MOVE: %m", path);
519 }
520
521 /* Finally, let's establish the requested propagation flags. */
522 if (mount_propagation_flag == 0)
523 return 0;
524
525 if (mount(NULL, ".", NULL, mount_propagation_flag | MS_REC, 0) < 0)
526 return log_debug_errno(errno, "Failed to turn new rootfs '%s' into %s mount: %m",
527 mount_propagation_flag_to_string(mount_propagation_flag), path);
528
529 return 0;
530 }
531
532 int repeat_unmount(const char *path, int flags) {
533 bool done = false;
534
535 assert(path);
536
537 /* If there are multiple mounts on a mount point, this
538 * removes them all */
539
540 for (;;) {
541 if (umount2(path, flags) < 0) {
542
543 if (errno == EINVAL)
544 return done;
545
546 return -errno;
547 }
548
549 done = true;
550 }
551 }
552
553 int mode_to_inaccessible_node(
554 const char *runtime_dir,
555 mode_t mode,
556 char **ret) {
557
558 /* This function maps a node type to a corresponding inaccessible file node. These nodes are created
559 * during early boot by PID 1. In some cases we lacked the privs to create the character and block
560 * devices (maybe because we run in an userns environment, or miss CAP_SYS_MKNOD, or run with a
561 * devices policy that excludes device nodes with major and minor of 0), but that's fine, in that
562 * case we use an AF_UNIX file node instead, which is not the same, but close enough for most
563 * uses. And most importantly, the kernel allows bind mounts from socket nodes to any non-directory
564 * file nodes, and that's the most important thing that matters.
565 *
566 * Note that the runtime directory argument shall be the top-level runtime directory, i.e. /run/ if
567 * we operate in system context and $XDG_RUNTIME_DIR if we operate in user context. */
568
569 _cleanup_free_ char *d = NULL;
570 const char *node;
571
572 assert(ret);
573
574 if (!runtime_dir)
575 runtime_dir = "/run";
576
577 if (S_ISLNK(mode))
578 return -EINVAL;
579
580 node = inode_type_to_string(mode);
581 if (!node)
582 return -EINVAL;
583
584 d = path_join(runtime_dir, "systemd/inaccessible", node);
585 if (!d)
586 return -ENOMEM;
587
588 /* On new kernels unprivileged users are permitted to create 0:0 char device nodes (because they also
589 * act as whiteout inode for overlayfs), but no other char or block device nodes. On old kernels no
590 * device node whatsoever may be created by unprivileged processes. Hence, if the caller asks for the
591 * inaccessible block device node let's see if the block device node actually exists, and if not,
592 * fall back to the character device node. From there fall back to the socket device node. This means
593 * in the best case we'll get the right device node type — but if not we'll hopefully at least get a
594 * device node at all. */
595
596 if (S_ISBLK(mode) &&
597 access(d, F_OK) < 0 && errno == ENOENT) {
598 free(d);
599 d = path_join(runtime_dir, "/systemd/inaccessible/chr");
600 if (!d)
601 return -ENOMEM;
602 }
603
604 if (IN_SET(mode & S_IFMT, S_IFBLK, S_IFCHR) &&
605 access(d, F_OK) < 0 && errno == ENOENT) {
606 free(d);
607 d = path_join(runtime_dir, "/systemd/inaccessible/sock");
608 if (!d)
609 return -ENOMEM;
610 }
611
612 *ret = TAKE_PTR(d);
613 return 0;
614 }
615
616 int mount_flags_to_string(unsigned long flags, char **ret) {
617 static const struct {
618 unsigned long flag;
619 const char *name;
620 } map[] = {
621 { .flag = MS_RDONLY, .name = "MS_RDONLY", },
622 { .flag = MS_NOSUID, .name = "MS_NOSUID", },
623 { .flag = MS_NODEV, .name = "MS_NODEV", },
624 { .flag = MS_NOEXEC, .name = "MS_NOEXEC", },
625 { .flag = MS_SYNCHRONOUS, .name = "MS_SYNCHRONOUS", },
626 { .flag = MS_REMOUNT, .name = "MS_REMOUNT", },
627 { .flag = MS_MANDLOCK, .name = "MS_MANDLOCK", },
628 { .flag = MS_DIRSYNC, .name = "MS_DIRSYNC", },
629 { .flag = MS_NOSYMFOLLOW, .name = "MS_NOSYMFOLLOW", },
630 { .flag = MS_NOATIME, .name = "MS_NOATIME", },
631 { .flag = MS_NODIRATIME, .name = "MS_NODIRATIME", },
632 { .flag = MS_BIND, .name = "MS_BIND", },
633 { .flag = MS_MOVE, .name = "MS_MOVE", },
634 { .flag = MS_REC, .name = "MS_REC", },
635 { .flag = MS_SILENT, .name = "MS_SILENT", },
636 { .flag = MS_POSIXACL, .name = "MS_POSIXACL", },
637 { .flag = MS_UNBINDABLE, .name = "MS_UNBINDABLE", },
638 { .flag = MS_PRIVATE, .name = "MS_PRIVATE", },
639 { .flag = MS_SLAVE, .name = "MS_SLAVE", },
640 { .flag = MS_SHARED, .name = "MS_SHARED", },
641 { .flag = MS_RELATIME, .name = "MS_RELATIME", },
642 { .flag = MS_KERNMOUNT, .name = "MS_KERNMOUNT", },
643 { .flag = MS_I_VERSION, .name = "MS_I_VERSION", },
644 { .flag = MS_STRICTATIME, .name = "MS_STRICTATIME", },
645 { .flag = MS_LAZYTIME, .name = "MS_LAZYTIME", },
646 };
647 _cleanup_free_ char *str = NULL;
648
649 assert(ret);
650
651 for (size_t i = 0; i < ELEMENTSOF(map); i++)
652 if (flags & map[i].flag) {
653 if (!strextend_with_separator(&str, "|", map[i].name))
654 return -ENOMEM;
655 flags &= ~map[i].flag;
656 }
657
658 if (!str || flags != 0)
659 if (strextendf_with_separator(&str, "|", "%lx", flags) < 0)
660 return -ENOMEM;
661
662 *ret = TAKE_PTR(str);
663 return 0;
664 }
665
666 int mount_verbose_full(
667 int error_log_level,
668 const char *what,
669 const char *where,
670 const char *type,
671 unsigned long flags,
672 const char *options,
673 bool follow_symlink) {
674
675 _cleanup_free_ char *fl = NULL, *o = NULL;
676 unsigned long f;
677 int r;
678
679 r = mount_option_mangle(options, flags, &f, &o);
680 if (r < 0)
681 return log_full_errno(error_log_level, r,
682 "Failed to mangle mount options %s: %m",
683 strempty(options));
684
685 (void) mount_flags_to_string(f, &fl);
686
687 if (FLAGS_SET(f, MS_REMOUNT|MS_BIND))
688 log_debug("Changing mount flags %s (%s \"%s\")...",
689 where, strnull(fl), strempty(o));
690 else if (f & MS_REMOUNT)
691 log_debug("Remounting superblock %s (%s \"%s\")...",
692 where, strnull(fl), strempty(o));
693 else if (f & (MS_SHARED|MS_PRIVATE|MS_SLAVE|MS_UNBINDABLE))
694 log_debug("Changing mount propagation %s (%s \"%s\")",
695 where, strnull(fl), strempty(o));
696 else if (f & MS_BIND)
697 log_debug("Bind-mounting %s on %s (%s \"%s\")...",
698 what, where, strnull(fl), strempty(o));
699 else if (f & MS_MOVE)
700 log_debug("Moving mount %s %s %s (%s \"%s\")...",
701 what, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), where, strnull(fl), strempty(o));
702 else
703 log_debug("Mounting %s (%s) on %s (%s \"%s\")...",
704 strna(what), strna(type), where, strnull(fl), strempty(o));
705
706 if (follow_symlink)
707 r = RET_NERRNO(mount(what, where, type, f, o));
708 else
709 r = mount_nofollow(what, where, type, f, o);
710 if (r < 0)
711 return log_full_errno(error_log_level, r,
712 "Failed to mount %s (type %s) on %s (%s \"%s\"): %m",
713 strna(what), strna(type), where, strnull(fl), strempty(o));
714 return 0;
715 }
716
717 int umount_verbose(
718 int error_log_level,
719 const char *what,
720 int flags) {
721
722 assert(what);
723
724 log_debug("Umounting %s...", what);
725
726 if (umount2(what, flags) < 0)
727 return log_full_errno(error_log_level, errno,
728 "Failed to unmount %s: %m", what);
729
730 return 0;
731 }
732
733 int mount_option_mangle(
734 const char *options,
735 unsigned long mount_flags,
736 unsigned long *ret_mount_flags,
737 char **ret_remaining_options) {
738
739 const struct libmnt_optmap *map;
740 _cleanup_free_ char *ret = NULL;
741 int r;
742
743 /* This extracts mount flags from the mount options, and stores
744 * non-mount-flag options to '*ret_remaining_options'.
745 * E.g.,
746 * "rw,nosuid,nodev,relatime,size=1630748k,mode=0700,uid=1000,gid=1000"
747 * is split to MS_NOSUID|MS_NODEV|MS_RELATIME and
748 * "size=1630748k,mode=0700,uid=1000,gid=1000".
749 * See more examples in test-mount-util.c.
750 *
751 * If 'options' does not contain any non-mount-flag options,
752 * then '*ret_remaining_options' is set to NULL instead of empty string.
753 * The validity of options stored in '*ret_remaining_options' is not checked.
754 * If 'options' is NULL, this just copies 'mount_flags' to *ret_mount_flags. */
755
756 assert(ret_mount_flags);
757 assert(ret_remaining_options);
758
759 map = mnt_get_builtin_optmap(MNT_LINUX_MAP);
760 if (!map)
761 return -EINVAL;
762
763 for (const char *p = options;;) {
764 _cleanup_free_ char *word = NULL;
765 const struct libmnt_optmap *ent;
766
767 r = extract_first_word(&p, &word, ",", EXTRACT_KEEP_QUOTE);
768 if (r < 0)
769 return r;
770 if (r == 0)
771 break;
772
773 for (ent = map; ent->name; ent++) {
774 /* All entries in MNT_LINUX_MAP do not take any argument.
775 * Thus, ent->name does not contain "=" or "[=]". */
776 if (!streq(word, ent->name))
777 continue;
778
779 if (!(ent->mask & MNT_INVERT))
780 mount_flags |= ent->id;
781 else if (mount_flags & ent->id)
782 mount_flags ^= ent->id;
783
784 break;
785 }
786
787 /* If 'word' is not a mount flag, then store it in '*ret_remaining_options'. */
788 if (!ent->name &&
789 !startswith_no_case(word, "x-") &&
790 !strextend_with_separator(&ret, ",", word))
791 return -ENOMEM;
792 }
793
794 *ret_mount_flags = mount_flags;
795 *ret_remaining_options = TAKE_PTR(ret);
796
797 return 0;
798 }
799
800 static int mount_in_namespace(
801 pid_t target,
802 const char *propagate_path,
803 const char *incoming_path,
804 const char *src,
805 const char *dest,
806 bool read_only,
807 bool make_file_or_directory,
808 const MountOptions *options,
809 const ImagePolicy *image_policy,
810 bool is_image) {
811
812 _cleanup_close_pair_ int errno_pipe_fd[2] = PIPE_EBADF;
813 _cleanup_close_ int mntns_fd = -EBADF, root_fd = -EBADF, pidns_fd = -EBADF, chased_src_fd = -EBADF;
814 char mount_slave[] = "/tmp/propagate.XXXXXX", *mount_tmp, *mount_outside, *p;
815 bool mount_slave_created = false, mount_slave_mounted = false,
816 mount_tmp_created = false, mount_tmp_mounted = false,
817 mount_outside_created = false, mount_outside_mounted = false;
818 _cleanup_free_ char *chased_src_path = NULL;
819 struct stat st;
820 pid_t child;
821 int r;
822
823 assert(target > 0);
824 assert(propagate_path);
825 assert(incoming_path);
826 assert(src);
827 assert(dest);
828 assert(!options || is_image);
829
830 r = namespace_open(target, &pidns_fd, &mntns_fd, NULL, NULL, &root_fd);
831 if (r < 0)
832 return log_debug_errno(r, "Failed to retrieve FDs of the target process' namespace: %m");
833
834 r = in_same_namespace(target, 0, NAMESPACE_MOUNT);
835 if (r < 0)
836 return log_debug_errno(r, "Failed to determine if mount namespaces are equal: %m");
837 /* We can't add new mounts at runtime if the process wasn't started in a namespace */
838 if (r > 0)
839 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to activate bind mount in target, not running in a mount namespace");
840
841 /* One day, when bind mounting /proc/self/fd/n works across namespace boundaries we should rework
842 * this logic to make use of it... */
843
844 p = strjoina(propagate_path, "/");
845 r = laccess(p, F_OK);
846 if (r < 0)
847 return log_debug_errno(r == -ENOENT ? SYNTHETIC_ERRNO(EOPNOTSUPP) : r, "Target does not allow propagation of mount points");
848
849 r = chase(src, NULL, 0, &chased_src_path, &chased_src_fd);
850 if (r < 0)
851 return log_debug_errno(r, "Failed to resolve source path of %s: %m", src);
852 log_debug("Chased source path of %s to %s", src, chased_src_path);
853
854 if (fstat(chased_src_fd, &st) < 0)
855 return log_debug_errno(errno, "Failed to stat() resolved source path %s: %m", src);
856 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… */
857 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Source directory %s can't be a symbolic link", src);
858
859 /* Our goal is to install a new bind mount into the container,
860 possibly read-only. This is irritatingly complex
861 unfortunately, currently.
862
863 First, we start by creating a private playground in /tmp,
864 that we can mount MS_SLAVE. (Which is necessary, since
865 MS_MOVE cannot be applied to mounts with MS_SHARED parent
866 mounts.) */
867
868 if (!mkdtemp(mount_slave))
869 return log_debug_errno(errno, "Failed to create playground %s: %m", mount_slave);
870
871 mount_slave_created = true;
872
873 r = mount_nofollow_verbose(LOG_DEBUG, mount_slave, mount_slave, NULL, MS_BIND, NULL);
874 if (r < 0)
875 goto finish;
876
877 mount_slave_mounted = true;
878
879 r = mount_nofollow_verbose(LOG_DEBUG, NULL, mount_slave, NULL, MS_SLAVE, NULL);
880 if (r < 0)
881 goto finish;
882
883 /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
884 mount_tmp = strjoina(mount_slave, "/mount");
885 if (is_image)
886 r = mkdir_p(mount_tmp, 0700);
887 else
888 r = make_mount_point_inode_from_stat(&st, mount_tmp, 0700);
889 if (r < 0) {
890 log_debug_errno(r, "Failed to create temporary mount point %s: %m", mount_tmp);
891 goto finish;
892 }
893
894 mount_tmp_created = true;
895
896 if (is_image)
897 r = verity_dissect_and_mount(chased_src_fd, chased_src_path, mount_tmp, options, image_policy, NULL, NULL, NULL, NULL);
898 else
899 r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(chased_src_fd), mount_tmp, NULL, MS_BIND, NULL);
900 if (r < 0)
901 goto finish;
902
903 mount_tmp_mounted = true;
904
905 /* Third, we remount the new bind mount read-only if requested. */
906 if (read_only) {
907 r = mount_nofollow_verbose(LOG_DEBUG, NULL, mount_tmp, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL);
908 if (r < 0)
909 goto finish;
910 }
911
912 /* Fourth, we move the new bind mount into the propagation directory. This way it will appear there read-only
913 * right-away. */
914
915 mount_outside = strjoina(propagate_path, "/XXXXXX");
916 if (is_image || S_ISDIR(st.st_mode))
917 r = mkdtemp(mount_outside) ? 0 : -errno;
918 else {
919 r = mkostemp_safe(mount_outside);
920 safe_close(r);
921 }
922 if (r < 0) {
923 log_debug_errno(r, "Cannot create propagation file or directory %s: %m", mount_outside);
924 goto finish;
925 }
926
927 mount_outside_created = true;
928
929 r = mount_nofollow_verbose(LOG_DEBUG, mount_tmp, mount_outside, NULL, MS_MOVE, NULL);
930 if (r < 0)
931 goto finish;
932
933 mount_outside_mounted = true;
934 mount_tmp_mounted = false;
935
936 if (is_image || S_ISDIR(st.st_mode))
937 (void) rmdir(mount_tmp);
938 else
939 (void) unlink(mount_tmp);
940 mount_tmp_created = false;
941
942 (void) umount_verbose(LOG_DEBUG, mount_slave, UMOUNT_NOFOLLOW);
943 mount_slave_mounted = false;
944
945 (void) rmdir(mount_slave);
946 mount_slave_created = false;
947
948 if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) {
949 log_debug_errno(errno, "Failed to create pipe: %m");
950 goto finish;
951 }
952
953 r = namespace_fork("(sd-bindmnt)", "(sd-bindmnt-inner)", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG,
954 pidns_fd, mntns_fd, -1, -1, root_fd, &child);
955 if (r < 0)
956 goto finish;
957 if (r == 0) {
958 _cleanup_free_ char *mount_outside_fn = NULL, *mount_inside = NULL;
959
960 errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
961
962 if (make_file_or_directory) {
963 if (!is_image) {
964 (void) mkdir_parents(dest, 0755);
965 (void) make_mount_point_inode_from_stat(&st, dest, 0700);
966 } else
967 (void) mkdir_p(dest, 0755);
968 }
969
970 /* Fifth, move the mount to the right place inside */
971 r = path_extract_filename(mount_outside, &mount_outside_fn);
972 if (r < 0) {
973 log_debug_errno(r, "Failed to extract filename from propagation file or directory '%s': %m", mount_outside);
974 goto child_fail;
975 }
976
977 mount_inside = path_join(incoming_path, mount_outside_fn);
978 if (!mount_inside) {
979 r = log_oom_debug();
980 goto child_fail;
981 }
982
983 r = mount_nofollow_verbose(LOG_DEBUG, mount_inside, dest, NULL, MS_MOVE, NULL);
984 if (r < 0)
985 goto child_fail;
986
987 _exit(EXIT_SUCCESS);
988
989 child_fail:
990 (void) write(errno_pipe_fd[1], &r, sizeof(r));
991 errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
992
993 _exit(EXIT_FAILURE);
994 }
995
996 errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
997
998 r = wait_for_terminate_and_check("(sd-bindmnt)", child, 0);
999 if (r < 0) {
1000 log_debug_errno(r, "Failed to wait for child: %m");
1001 goto finish;
1002 }
1003 if (r != EXIT_SUCCESS) {
1004 if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r))
1005 log_debug_errno(r, "Failed to mount: %m");
1006 else
1007 log_debug("Child failed.");
1008 goto finish;
1009 }
1010
1011 finish:
1012 if (mount_outside_mounted)
1013 (void) umount_verbose(LOG_DEBUG, mount_outside, UMOUNT_NOFOLLOW);
1014 if (mount_outside_created) {
1015 if (is_image || S_ISDIR(st.st_mode))
1016 (void) rmdir(mount_outside);
1017 else
1018 (void) unlink(mount_outside);
1019 }
1020
1021 if (mount_tmp_mounted)
1022 (void) umount_verbose(LOG_DEBUG, mount_tmp, UMOUNT_NOFOLLOW);
1023 if (mount_tmp_created) {
1024 if (is_image || S_ISDIR(st.st_mode))
1025 (void) rmdir(mount_tmp);
1026 else
1027 (void) unlink(mount_tmp);
1028 }
1029
1030 if (mount_slave_mounted)
1031 (void) umount_verbose(LOG_DEBUG, mount_slave, UMOUNT_NOFOLLOW);
1032 if (mount_slave_created)
1033 (void) rmdir(mount_slave);
1034
1035 return r;
1036 }
1037
1038 int bind_mount_in_namespace(
1039 pid_t target,
1040 const char *propagate_path,
1041 const char *incoming_path,
1042 const char *src,
1043 const char *dest,
1044 bool read_only,
1045 bool make_file_or_directory) {
1046
1047 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);
1048 }
1049
1050 int mount_image_in_namespace(
1051 pid_t target,
1052 const char *propagate_path,
1053 const char *incoming_path,
1054 const char *src,
1055 const char *dest,
1056 bool read_only,
1057 bool make_file_or_directory,
1058 const MountOptions *options,
1059 const ImagePolicy *image_policy) {
1060
1061 return mount_in_namespace(target, propagate_path, incoming_path, src, dest, read_only, make_file_or_directory, options, image_policy, /* is_image=*/ true);
1062 }
1063
1064 int make_mount_point(const char *path) {
1065 int r;
1066
1067 assert(path);
1068
1069 /* If 'path' is already a mount point, does nothing and returns 0. If it is not it makes it one, and returns 1. */
1070
1071 r = path_is_mount_point(path, NULL, 0);
1072 if (r < 0)
1073 return log_debug_errno(r, "Failed to determine whether '%s' is a mount point: %m", path);
1074 if (r > 0)
1075 return 0;
1076
1077 r = mount_nofollow_verbose(LOG_DEBUG, path, path, NULL, MS_BIND|MS_REC, NULL);
1078 if (r < 0)
1079 return r;
1080
1081 return 1;
1082 }
1083
1084 int fd_make_mount_point(int fd) {
1085 int r;
1086
1087 assert(fd >= 0);
1088
1089 r = fd_is_mount_point(fd, NULL, 0);
1090 if (r < 0)
1091 return log_debug_errno(r, "Failed to determine whether file descriptor is a mount point: %m");
1092 if (r > 0)
1093 return 0;
1094
1095 r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(fd), FORMAT_PROC_FD_PATH(fd), NULL, MS_BIND|MS_REC, NULL);
1096 if (r < 0)
1097 return r;
1098
1099 return 1;
1100 }
1101
1102 int make_userns(uid_t uid_shift, uid_t uid_range, uid_t owner, RemountIdmapping idmapping) {
1103 _cleanup_close_ int userns_fd = -EBADF;
1104 _cleanup_free_ char *line = NULL;
1105
1106 /* Allocates a userns file descriptor with the mapping we need. For this we'll fork off a child
1107 * process whose only purpose is to give us a new user namespace. It's killed when we got it. */
1108
1109 if (!userns_shift_range_valid(uid_shift, uid_range))
1110 return -EINVAL;
1111
1112 if (IN_SET(idmapping, REMOUNT_IDMAPPING_NONE, REMOUNT_IDMAPPING_HOST_ROOT)) {
1113 if (asprintf(&line, UID_FMT " " UID_FMT " " UID_FMT "\n", 0u, uid_shift, uid_range) < 0)
1114 return log_oom_debug();
1115
1116 /* If requested we'll include an entry in the mapping so that the host root user can make
1117 * changes to the uidmapped mount like it normally would. Specifically, we'll map the user
1118 * with UID_MAPPED_ROOT on the backing fs to UID 0. This is useful, since nspawn code wants
1119 * to create various missing inodes in the OS tree before booting into it, and this becomes
1120 * very easy and straightforward to do if it can just do it under its own regular UID. Note
1121 * that in that case the container's runtime uidmap (i.e. the one the container payload
1122 * processes run in) will leave this UID unmapped, i.e. if we accidentally leave files owned
1123 * by host root in the already uidmapped tree around they'll show up as owned by 'nobody',
1124 * which is safe. (Of course, we shouldn't leave such inodes around, but always chown() them
1125 * to the container's own UID range, but it's good to have a safety net, in case we
1126 * forget it.) */
1127 if (idmapping == REMOUNT_IDMAPPING_HOST_ROOT)
1128 if (strextendf(&line,
1129 UID_FMT " " UID_FMT " " UID_FMT "\n",
1130 UID_MAPPED_ROOT, 0u, 1u) < 0)
1131 return log_oom_debug();
1132 }
1133
1134 if (idmapping == REMOUNT_IDMAPPING_HOST_OWNER) {
1135 /* Remap the owner of the bind mounted directory to the root user within the container. This
1136 * way every file written by root within the container to the bind-mounted directory will
1137 * be owned by the original user. All other user will remain unmapped. */
1138 if (asprintf(&line, UID_FMT " " UID_FMT " " UID_FMT "\n", owner, uid_shift, 1u) < 0)
1139 return log_oom_debug();
1140 }
1141
1142 /* We always assign the same UID and GID ranges */
1143 userns_fd = userns_acquire(line, line);
1144 if (userns_fd < 0)
1145 return log_debug_errno(userns_fd, "Failed to acquire new userns: %m");
1146
1147 return TAKE_FD(userns_fd);
1148 }
1149
1150 int remount_idmap_fd(
1151 const char *p,
1152 int userns_fd) {
1153
1154 _cleanup_close_ int mount_fd = -EBADF;
1155 int r;
1156
1157 assert(p);
1158 assert(userns_fd >= 0);
1159
1160 /* Clone the mount point */
1161 mount_fd = open_tree(-1, p, OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
1162 if (mount_fd < 0)
1163 return log_debug_errno(errno, "Failed to open tree of mounted filesystem '%s': %m", p);
1164
1165 /* Set the user namespace mapping attribute on the cloned mount point */
1166 if (mount_setattr(mount_fd, "", AT_EMPTY_PATH | AT_RECURSIVE,
1167 &(struct mount_attr) {
1168 .attr_set = MOUNT_ATTR_IDMAP,
1169 .userns_fd = userns_fd,
1170 }, sizeof(struct mount_attr)) < 0)
1171 return log_debug_errno(errno, "Failed to change bind mount attributes for '%s': %m", p);
1172
1173 /* Remove the old mount point */
1174 r = umount_verbose(LOG_DEBUG, p, UMOUNT_NOFOLLOW);
1175 if (r < 0)
1176 return r;
1177
1178 /* And place the cloned version in its place */
1179 if (move_mount(mount_fd, "", -1, p, MOVE_MOUNT_F_EMPTY_PATH) < 0)
1180 return log_debug_errno(errno, "Failed to attach UID mapped mount to '%s': %m", p);
1181
1182 return 0;
1183 }
1184
1185 int remount_idmap(const char *p, uid_t uid_shift, uid_t uid_range, uid_t owner, RemountIdmapping idmapping) {
1186 _cleanup_close_ int userns_fd = -EBADF;
1187
1188 userns_fd = make_userns(uid_shift, uid_range, owner, idmapping);
1189 if (userns_fd < 0)
1190 return userns_fd;
1191
1192 return remount_idmap_fd(p, userns_fd);
1193 }
1194
1195 typedef struct SubMount {
1196 char *path;
1197 int mount_fd;
1198 } SubMount;
1199
1200 static void sub_mount_clear(SubMount *s) {
1201 assert(s);
1202
1203 s->path = mfree(s->path);
1204 s->mount_fd = safe_close(s->mount_fd);
1205 }
1206
1207 static void sub_mount_array_free(SubMount *s, size_t n) {
1208 assert(s || n == 0);
1209
1210 for (size_t i = 0; i < n; i++)
1211 sub_mount_clear(s + i);
1212
1213 free(s);
1214 }
1215
1216 static int sub_mount_compare(const SubMount *a, const SubMount *b) {
1217 assert(a);
1218 assert(b);
1219 assert(a->path);
1220 assert(b->path);
1221
1222 return path_compare(a->path, b->path);
1223 }
1224
1225 static void sub_mount_drop(SubMount *s, size_t n) {
1226 assert(s || n == 0);
1227
1228 for (size_t m = 0, i = 1; i < n; i++) {
1229 if (path_startswith(s[i].path, s[m].path))
1230 sub_mount_clear(s + i);
1231 else
1232 m = i;
1233 }
1234 }
1235
1236 static int get_sub_mounts(
1237 const char *prefix,
1238 bool clone_tree,
1239 SubMount **ret_mounts,
1240 size_t *ret_n_mounts) {
1241 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
1242 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
1243 SubMount *mounts = NULL;
1244 size_t n = 0;
1245 int r;
1246
1247 CLEANUP_ARRAY(mounts, n, sub_mount_array_free);
1248
1249 assert(prefix);
1250 assert(ret_mounts);
1251 assert(ret_n_mounts);
1252
1253 r = libmount_parse("/proc/self/mountinfo", NULL, &table, &iter);
1254 if (r < 0)
1255 return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
1256
1257 for (;;) {
1258 _cleanup_close_ int mount_fd = -EBADF;
1259 _cleanup_free_ char *p = NULL;
1260 struct libmnt_fs *fs;
1261 const char *path;
1262 int id1, id2;
1263
1264 r = mnt_table_next_fs(table, iter, &fs);
1265 if (r == 1)
1266 break; /* EOF */
1267 if (r < 0)
1268 return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
1269
1270 path = mnt_fs_get_target(fs);
1271 if (!path)
1272 continue;
1273
1274 if (isempty(path_startswith(path, prefix)))
1275 continue;
1276
1277 id1 = mnt_fs_get_id(fs);
1278 r = path_get_mnt_id(path, &id2);
1279 if (r < 0) {
1280 log_debug_errno(r, "Failed to get mount ID of '%s', ignoring: %m", path);
1281 continue;
1282 }
1283 if (id1 != id2) {
1284 /* The path may be hidden by another over-mount or already remounted. */
1285 log_debug("The mount IDs of '%s' obtained by libmount and path_get_mnt_id() are different (%i vs %i), ignoring.",
1286 path, id1, id2);
1287 continue;
1288 }
1289
1290 if (clone_tree)
1291 mount_fd = open_tree(AT_FDCWD, path, OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC | AT_RECURSIVE);
1292 else
1293 mount_fd = open(path, O_CLOEXEC|O_PATH);
1294 if (mount_fd < 0) {
1295 if (errno == ENOENT) /* The path may be hidden by another over-mount or already unmounted. */
1296 continue;
1297
1298 return log_debug_errno(errno, "Failed to open subtree of mounted filesystem '%s': %m", path);
1299 }
1300
1301 p = strdup(path);
1302 if (!p)
1303 return log_oom_debug();
1304
1305 if (!GREEDY_REALLOC(mounts, n + 1))
1306 return log_oom_debug();
1307
1308 mounts[n++] = (SubMount) {
1309 .path = TAKE_PTR(p),
1310 .mount_fd = TAKE_FD(mount_fd),
1311 };
1312 }
1313
1314 typesafe_qsort(mounts, n, sub_mount_compare);
1315 sub_mount_drop(mounts, n);
1316
1317 *ret_mounts = TAKE_PTR(mounts);
1318 *ret_n_mounts = n;
1319 return 0;
1320 }
1321
1322 static int move_sub_mounts(SubMount *mounts, size_t n) {
1323 assert(mounts || n == 0);
1324
1325 for (size_t i = 0; i < n; i++) {
1326 if (!mounts[i].path || mounts[i].mount_fd < 0)
1327 continue;
1328
1329 (void) mkdir_p_label(mounts[i].path, 0755);
1330
1331 if (move_mount(mounts[i].mount_fd, "", AT_FDCWD, mounts[i].path, MOVE_MOUNT_F_EMPTY_PATH) < 0)
1332 return log_debug_errno(errno, "Failed to move mount_fd to '%s': %m", mounts[i].path);
1333 }
1334
1335 return 0;
1336 }
1337
1338 int remount_and_move_sub_mounts(
1339 const char *what,
1340 const char *where,
1341 const char *type,
1342 unsigned long flags,
1343 const char *options) {
1344
1345 SubMount *mounts = NULL;
1346 size_t n = 0;
1347 int r;
1348
1349 CLEANUP_ARRAY(mounts, n, sub_mount_array_free);
1350
1351 assert(where);
1352
1353 /* This is useful when creating a new network namespace. Unlike procfs, we need to remount sysfs,
1354 * otherwise properties of the network interfaces in the main network namespace are still accessible
1355 * through the old sysfs, e.g. /sys/class/net/eth0. All sub-mounts previously mounted on the sysfs
1356 * are moved onto the new sysfs mount. */
1357
1358 r = path_is_mount_point(where, NULL, 0);
1359 if (r < 0)
1360 return log_debug_errno(r, "Failed to determine if '%s' is a mountpoint: %m", where);
1361 if (r == 0)
1362 /* Shortcut. Simply mount the requested filesystem. */
1363 return mount_nofollow_verbose(LOG_DEBUG, what, where, type, flags, options);
1364
1365 /* Get the list of sub-mounts and duplicate them. */
1366 r = get_sub_mounts(where, /* clone_tree= */ true, &mounts, &n);
1367 if (r < 0)
1368 return r;
1369
1370 /* Then, remount the mount and its sub-mounts. */
1371 (void) umount_recursive(where, 0);
1372
1373 /* Remount the target filesystem. */
1374 r = mount_nofollow_verbose(LOG_DEBUG, what, where, type, flags, options);
1375 if (r < 0)
1376 return r;
1377
1378 /* Finally, move the all sub-mounts on the new target mount point. */
1379 return move_sub_mounts(mounts, n);
1380 }
1381
1382 int bind_mount_submounts(
1383 const char *source,
1384 const char *target) {
1385
1386 SubMount *mounts = NULL;
1387 size_t n = 0;
1388 int ret = 0, r;
1389
1390 /* Bind mounts all child mounts of 'source' to 'target'. Useful when setting up a new procfs instance
1391 * with new mount options to copy the original submounts over. */
1392
1393 assert(source);
1394 assert(target);
1395
1396 CLEANUP_ARRAY(mounts, n, sub_mount_array_free);
1397
1398 r = get_sub_mounts(source, /* clone_tree= */ false, &mounts, &n);
1399 if (r < 0)
1400 return r;
1401
1402 FOREACH_ARRAY(m, mounts, n) {
1403 _cleanup_free_ char *t = NULL;
1404 const char *suffix;
1405
1406 if (isempty(m->path))
1407 continue;
1408
1409 assert_se(suffix = path_startswith(m->path, source));
1410
1411 t = path_join(target, suffix);
1412 if (!t)
1413 return -ENOMEM;
1414
1415 r = path_is_mount_point(t, NULL, 0);
1416 if (r < 0) {
1417 log_debug_errno(r, "Failed to detect if '%s' already is a mount point, ignoring: %m", t);
1418 continue;
1419 }
1420 if (r > 0) {
1421 log_debug("Not bind mounting '%s' from '%s' to '%s', since there's already a mountpoint.", suffix, source, target);
1422 continue;
1423 }
1424
1425 r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(m->mount_fd), t, NULL, MS_BIND|MS_REC, NULL);
1426 if (r < 0 && ret == 0)
1427 ret = r;
1428 }
1429
1430 return ret;
1431 }
1432
1433 int remount_sysfs(const char *where) {
1434 return remount_and_move_sub_mounts("sysfs", where, "sysfs", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
1435 }
1436
1437 int make_mount_point_inode_from_stat(const struct stat *st, const char *dest, mode_t mode) {
1438 assert(st);
1439 assert(dest);
1440
1441 if (S_ISDIR(st->st_mode))
1442 return mkdir_label(dest, mode);
1443 else
1444 return RET_NERRNO(mknod(dest, S_IFREG|(mode & ~0111), 0));
1445 }
1446
1447 int make_mount_point_inode_from_path(const char *source, const char *dest, mode_t mode) {
1448 struct stat st;
1449
1450 assert(source);
1451 assert(dest);
1452
1453 if (stat(source, &st) < 0)
1454 return -errno;
1455
1456 return make_mount_point_inode_from_stat(&st, dest, mode);
1457 }
1458
1459 int trigger_automount_at(int dir_fd, const char *path) {
1460 _cleanup_free_ char *nested = NULL;
1461
1462 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
1463
1464 nested = path_join(path, "a");
1465 if (!nested)
1466 return -ENOMEM;
1467
1468 (void) faccessat(dir_fd, nested, F_OK, 0);
1469
1470 return 0;
1471 }