Debian Codegraph @

src/shared/mount-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <sys/mount.h>
5 #include <sys/socket.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8
9 #include "alloc-util.h"
10 #include "chase.h"
11 #include "creds-util.h"
12 #include "dissect-image.h"
13 #include "errno-util.h"
14 #include "extract-word.h"
15 #include "fd-util.h"
16 #include "fileio.h"
17 #include "format-util.h"
18 #include "fs-util.h"
19 #include "fstab-util.h"
20 #include "glyph-util.h"
21 #include "hashmap.h"
22 #include "libmount-util.h"
23 #include "log.h"
24 #include "mkdir.h"
25 #include "mount-util.h"
26 #include "mountpoint-util.h"
27 #include "namespace-util.h"
28 #include "os-util.h"
29 #include "path-util.h"
30 #include "pidref.h"
31 #include "process-util.h"
32 #include "runtime-scope.h"
33 #include "set.h"
34 #include "socket-util.h"
35 #include "sort-util.h"
36 #include "stat-util.h"
37 #include "string-util.h"
38 #include "strv.h"
39 #include "tmpfile-util.h"
40 #include "user-util.h"
41
42 int umount_recursive_full(const char *prefix, int flags, char **keep) {
43 #if HAVE_LIBMOUNT
44 _cleanup_fclose_ FILE *f = NULL;
45 int n = 0, r;
46
47 /* Try to umount everything recursively below a directory. Also, take care of stacked mounts, and
48 * keep unmounting them until they are gone. */
49
50 f = fopen("/proc/self/mountinfo", "re"); /* Pin the file, in case we unmount /proc/ as part of the logic here */
51 if (!f)
52 return log_debug_errno(errno, "Failed to open %s: %m", "/proc/self/mountinfo");
53
54 for (;;) {
55 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
56 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
57 bool again = false;
58
59 r = libmount_parse_full("/proc/self/mountinfo", f, MNT_ITER_BACKWARD, &table, &iter);
60 if (r < 0)
61 return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
62
63 for (;;) {
64 bool shall_keep = false;
65 struct libmnt_fs *fs;
66 const char *path;
67
68 r = sym_mnt_table_next_fs(table, iter, &fs);
69 if (r == 1)
70 break;
71 if (r < 0)
72 return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
73
74 path = sym_mnt_fs_get_target(fs);
75 if (!path)
76 continue;
77
78 if (prefix && !path_startswith(path, prefix)) {
79 // FIXME: This is extremely noisy, we're probably doing something very wrong
80 // to trigger this so often, needs more investigation.
81 // log_trace("Not unmounting %s, outside of prefix: %s", path, prefix);
82 continue;
83 }
84
85 STRV_FOREACH(k, keep)
86 /* Match against anything in the path to the dirs to keep, or below the dirs to keep */
87 if (path_startswith(path, *k) || path_startswith(*k, path)) {
88 shall_keep = true;
89 break;
90 }
91 if (shall_keep) {
92 log_debug("Not unmounting %s, referenced by keep list.", path);
93 continue;
94 }
95
96 if (umount2(path, flags | UMOUNT_NOFOLLOW) < 0) {
97 log_debug_errno(errno, "Failed to umount %s, ignoring: %m", path);
98 continue;
99 }
100
101 log_trace("Successfully unmounted %s", path);
102
103 again = true;
104 n++;
105
106 break;
107 }
108
109 if (!again)
110 break;
111
112 rewind(f);
113 }
114
115 return n;
116 #else
117 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libmount support not compiled in");
118 #endif
119 }
120
121 #define MS_CONVERTIBLE_FLAGS (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_NOSYMFOLLOW|MS_RELATIME|MS_NOATIME|MS_STRICTATIME|MS_NODIRATIME)
122
123 static uint64_t ms_flags_to_mount_attr(unsigned long a) {
124 uint64_t f = 0;
125
126 if (FLAGS_SET(a, MS_RDONLY))
127 f |= MOUNT_ATTR_RDONLY;
128
129 if (FLAGS_SET(a, MS_NOSUID))
130 f |= MOUNT_ATTR_NOSUID;
131
132 if (FLAGS_SET(a, MS_NODEV))
133 f |= MOUNT_ATTR_NODEV;
134
135 if (FLAGS_SET(a, MS_NOEXEC))
136 f |= MOUNT_ATTR_NOEXEC;
137
138 if (FLAGS_SET(a, MS_NOSYMFOLLOW))
139 f |= MOUNT_ATTR_NOSYMFOLLOW;
140
141 if (FLAGS_SET(a, MS_RELATIME))
142 f |= MOUNT_ATTR_RELATIME;
143
144 if (FLAGS_SET(a, MS_NOATIME))
145 f |= MOUNT_ATTR_NOATIME;
146
147 if (FLAGS_SET(a, MS_STRICTATIME))
148 f |= MOUNT_ATTR_STRICTATIME;
149
150 if (FLAGS_SET(a, MS_NODIRATIME))
151 f |= MOUNT_ATTR_NODIRATIME;
152
153 return f;
154 }
155
156 static uint64_t ms_flags_to_mount_attr_clr(unsigned long a) {
157 uint64_t f = 0;
158
159 /* As per documentation, if relatime/noatime/strictatime are set, we need to clear the atime flag
160 * too, otherwise -EINVAL will be returned by the kernel. */
161 if (FLAGS_SET(a, MS_RELATIME))
162 f |= MOUNT_ATTR__ATIME;
163
164 if (FLAGS_SET(a, MS_NOATIME))
165 f |= MOUNT_ATTR__ATIME;
166
167 if (FLAGS_SET(a, MS_STRICTATIME))
168 f |= MOUNT_ATTR__ATIME;
169
170 return f;
171 }
172
173 static bool skip_mount_set_attr = false;
174
175 /* Use this function only if you do not have direct access to /proc/self/mountinfo but the caller can open it
176 * for you. This is the case when /proc is masked or not mounted. Otherwise, use bind_remount_recursive. */
177 int bind_remount_recursive_with_mountinfo(
178 const char *prefix,
179 unsigned long new_flags,
180 unsigned long flags_mask,
181 char **deny_list,
182 FILE *proc_self_mountinfo) {
183
184 assert(prefix);
185
186 if ((flags_mask & ~MS_CONVERTIBLE_FLAGS) == 0 && strv_isempty(deny_list) && !skip_mount_set_attr) {
187 /* Let's take a shortcut for all the flags we know how to convert into mount_setattr() flags */
188
189 if (mount_setattr(AT_FDCWD, prefix, AT_SYMLINK_NOFOLLOW|AT_RECURSIVE,
190 &(struct mount_attr) {
191 .attr_set = ms_flags_to_mount_attr(new_flags & flags_mask),
192 .attr_clr = ms_flags_to_mount_attr(~new_flags & flags_mask),
193 }, MOUNT_ATTR_SIZE_VER0) < 0) {
194
195 log_debug_errno(errno, "mount_setattr() failed, falling back to classic remounting: %m");
196
197 /* We fall through to classic behaviour if not supported (i.e. kernel < 5.12). We
198 * also do this for all other kinds of errors since they are so many different, and
199 * mount_setattr() has no graceful mode where it continues despite seeing errors one
200 * some mounts, but we want that. Moreover mount_setattr() only works on the mount
201 * point inode itself, not a non-mount point inode, and we want to support arbitrary
202 * prefixes here. */
203
204 if (ERRNO_IS_NOT_SUPPORTED(errno)) /* if not supported, then don't bother at all anymore */
205 skip_mount_set_attr = true;
206 } else
207 return 0; /* Nice, this worked! */
208 }
209
210 #if HAVE_LIBMOUNT
211 _cleanup_fclose_ FILE *proc_self_mountinfo_opened = NULL;
212 _cleanup_set_free_ Set *done = NULL;
213 unsigned n_tries = 0;
214 int r;
215
216 if (!proc_self_mountinfo) {
217 r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo_opened);
218 if (r < 0)
219 return r;
220
221 proc_self_mountinfo = proc_self_mountinfo_opened;
222 }
223
224 /* Recursively remount a directory (and all its submounts) with desired flags (MS_READONLY,
225 * MS_NOSUID, MS_NOEXEC). If the directory is already mounted, we reuse the mount and simply mark it
226 * MS_BIND|MS_RDONLY (or remove the MS_RDONLY for read-write operation), ditto for other flags. If it
227 * isn't we first make it one. Afterwards we apply (or remove) the flags to all submounts we can
228 * access, too. When mounts are stacked on the same mount point we only care for each individual
229 * "top-level" mount on each point, as we cannot influence/access the underlying mounts anyway. We do
230 * not have any effect on future submounts that might get propagated, they might be writable
231 * etc. This includes future submounts that have been triggered via autofs. Also note that we can't
232 * operate atomically here. Mounts established while we process the tree might or might not get
233 * noticed and thus might or might not be covered.
234 *
235 * If the "deny_list" parameter is specified it may contain a list of subtrees to exclude from the
236 * remount operation. Note that we'll ignore the deny list for the top-level path. */
237
238 for (;;) {
239 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
240 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
241 _cleanup_hashmap_free_ Hashmap *todo = NULL;
242 bool top_autofs = false;
243
244 if (n_tries++ >= 32) /* Let's not retry this loop forever */
245 return -EBUSY;
246
247 rewind(proc_self_mountinfo);
248
249 r = libmount_parse_mountinfo(proc_self_mountinfo, &table, &iter);
250 if (r < 0)
251 return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
252
253 for (;;) {
254 _cleanup_free_ char *d = NULL;
255 const char *path, *type, *opts;
256 unsigned long flags = 0;
257 struct libmnt_fs *fs;
258
259 r = sym_mnt_table_next_fs(table, iter, &fs);
260 if (r == 1) /* EOF */
261 break;
262 if (r < 0)
263 return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
264
265 path = sym_mnt_fs_get_target(fs);
266 if (!path)
267 continue;
268
269 if (!path_startswith(path, prefix))
270 continue;
271
272 type = sym_mnt_fs_get_fstype(fs);
273 if (!type)
274 continue;
275
276 /* Let's ignore autofs mounts. If they aren't triggered yet, we want to avoid
277 * triggering them, as we don't make any guarantees for future submounts anyway. If
278 * they are already triggered, then we will find another entry for this. */
279 if (streq(type, "autofs")) {
280 top_autofs = top_autofs || path_equal(path, prefix);
281 continue;
282 }
283
284 if (set_contains(done, path))
285 continue;
286
287 /* Ignore this mount if it is deny-listed, but only if it isn't the top-level mount
288 * we shall operate on. */
289 if (!path_equal(path, prefix)) {
290 bool deny_listed = false;
291
292 STRV_FOREACH(i, deny_list) {
293 if (path_equal(*i, prefix))
294 continue;
295
296 if (!path_startswith(*i, prefix))
297 continue;
298
299 if (path_startswith(path, *i)) {
300 deny_listed = true;
301 log_trace("Not remounting %s deny-listed by %s, called for %s", path, *i, prefix);
302 break;
303 }
304 }
305
306 if (deny_listed)
307 continue;
308 }
309
310 opts = sym_mnt_fs_get_vfs_options(fs);
311 if (opts) {
312 r = sym_mnt_optstr_get_flags(opts, &flags, sym_mnt_get_builtin_optmap(MNT_LINUX_MAP));
313 if (r < 0)
314 log_debug_errno(r, "Could not get flags for '%s', ignoring: %m", path);
315 }
316
317 d = strdup(path);
318 if (!d)
319 return -ENOMEM;
320
321 r = hashmap_ensure_put(&todo, &path_hash_ops_free, d, ULONG_TO_PTR(flags));
322 if (r == -EEXIST)
323 /* If the same path was recorded, but with different mount flags, update it:
324 * it means a mount point is overmounted, and libmount returns the "bottom" (or
325 * older one) first, but we want to reapply the flags from the "top" (or newer
326 * one). See: https://github.com/systemd/systemd/issues/20032
327 * Note that this shouldn't really fail, as we were just told that the key
328 * exists, and it's an update so we want 'd' to be freed immediately. */
329 r = hashmap_update(todo, d, ULONG_TO_PTR(flags));
330 if (r < 0)
331 return r;
332 if (r > 0)
333 TAKE_PTR(d);
334 }
335
336 /* Check if the top-level directory was among what we have seen so far. For that check both
337 * 'done' and 'todo'. Also check 'top_autofs' because if the top-level dir is an autofs we'll
338 * not include it in either set but will set this bool. */
339 if (!set_contains(done, prefix) &&
340 !(top_autofs || hashmap_contains(todo, prefix))) {
341
342 /* The prefix directory itself is not yet a mount, make it one. */
343 r = mount_nofollow(prefix, prefix, NULL, MS_BIND|MS_REC, NULL);
344 if (r < 0)
345 return r;
346
347 /* Immediately rescan, so that we pick up the new mount's flags */
348 continue;
349 }
350
351 /* If we have no submounts to process anymore, we are done */
352 if (hashmap_isempty(todo))
353 return 0;
354
355 for (;;) {
356 unsigned long flags;
357 char *x = NULL;
358
359 /* Take the first mount from our list of mounts to still process */
360 flags = PTR_TO_ULONG(hashmap_steal_first_key_and_value(todo, (void**) &x));
361 if (!x)
362 break;
363
364 r = set_ensure_consume(&done, &path_hash_ops_free, x);
365 if (IN_SET(r, 0, -EEXIST))
366 continue; /* Already done */
367 if (r < 0)
368 return r;
369
370 /* Now, remount this with the new flags set, but exclude MS_RELATIME from it. (It's
371 * the default anyway, thus redundant, and in userns we'll get an error if we try to
372 * explicitly enable it) */
373 r = mount_nofollow(NULL, x, NULL, ((flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags) & ~MS_RELATIME, NULL);
374 if (r < 0) {
375 int q;
376
377 /* OK, so the remount of this entry failed. We'll ultimately ignore this in
378 * almost all cases (there are simply so many reasons why this can fail,
379 * think autofs, NFS, FUSE, …), but let's generate useful debug messages at
380 * the very least. */
381
382 q = path_is_mount_point(x);
383 if (IN_SET(q, 0, -ENOENT)) {
384 /* Hmm, whaaaa? The mount point is not actually a mount point? Then
385 * it is either obstructed by a later mount or somebody has been
386 * racing against us and removed it. Either way the mount point
387 * doesn't matter to us, let's ignore it hence. */
388 log_debug_errno(r, "Mount point '%s' to remount is not a mount point anymore, ignoring remount failure: %m", x);
389 continue;
390 }
391 if (q < 0) /* Any other error on this? Just log and continue */
392 log_debug_errno(q, "Failed to determine whether '%s' is a mount point or not, ignoring: %m", x);
393
394 if (((flags ^ new_flags) & flags_mask & ~MS_RELATIME) == 0) { /* ignore MS_RELATIME while comparing */
395 log_debug_errno(r, "Couldn't remount '%s', but the flags already match what we want, hence ignoring: %m", x);
396 continue;
397 }
398
399 /* Make this fatal if this is the top-level mount */
400 if (path_equal(x, prefix))
401 return r;
402
403 /* If this is not the top-level mount, then handle this gracefully: log but
404 * otherwise ignore. With NFS, FUSE, autofs there are just too many reasons
405 * this might fail without a chance for us to do anything about it, let's
406 * hence be strict on the top-level mount and lenient on the inner ones. */
407 log_debug_errno(r, "Couldn't remount submount '%s' for unexpected reason, ignoring: %m", x);
408 continue;
409 }
410
411 log_trace("Remounted %s.", x);
412 }
413 }
414 #else
415 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libmount support not compiled in");
416 #endif
417 }
418
419 int bind_remount_one_with_mountinfo(
420 const char *path,
421 unsigned long new_flags,
422 unsigned long flags_mask,
423 FILE *proc_self_mountinfo) {
424
425 assert(path);
426 assert(proc_self_mountinfo);
427
428 if ((flags_mask & ~MS_CONVERTIBLE_FLAGS) == 0 && !skip_mount_set_attr) {
429 /* Let's take a shortcut for all the flags we know how to convert into mount_setattr() flags */
430
431 if (mount_setattr(AT_FDCWD, path, AT_SYMLINK_NOFOLLOW,
432 &(struct mount_attr) {
433 .attr_set = ms_flags_to_mount_attr(new_flags & flags_mask),
434 .attr_clr = ms_flags_to_mount_attr(~new_flags & flags_mask),
435 }, MOUNT_ATTR_SIZE_VER0) < 0) {
436
437 log_debug_errno(errno, "mount_setattr() didn't work, falling back to classic remounting: %m");
438
439 if (ERRNO_IS_NOT_SUPPORTED(errno)) /* if not supported, then don't bother at all anymore */
440 skip_mount_set_attr = true;
441 } else
442 return 0; /* Nice, this worked! */
443 }
444
445 #if HAVE_LIBMOUNT
446 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
447 unsigned long flags = 0;
448 struct libmnt_fs *fs;
449 const char *opts;
450 int r;
451
452 rewind(proc_self_mountinfo);
453
454 r = dlopen_libmount(LOG_DEBUG);
455 if (r < 0)
456 return r;
457
458 table = sym_mnt_new_table();
459 if (!table)
460 return -ENOMEM;
461
462 r = sym_mnt_table_parse_stream(table, proc_self_mountinfo, "/proc/self/mountinfo");
463 if (r < 0)
464 return r;
465
466 fs = sym_mnt_table_find_target(table, path, MNT_ITER_FORWARD);
467 if (!fs) {
468 r = access_nofollow(path, F_OK); /* Hmm, it's not in the mount table, but does it exist at all? */
469 if (r < 0)
470 return r;
471
472 return -EINVAL; /* Not a mount point we recognize */
473 }
474
475 opts = sym_mnt_fs_get_vfs_options(fs);
476 if (opts) {
477 r = sym_mnt_optstr_get_flags(opts, &flags, sym_mnt_get_builtin_optmap(MNT_LINUX_MAP));
478 if (r < 0)
479 log_debug_errno(r, "Could not get flags for '%s', ignoring: %m", path);
480 }
481
482 r = mount_nofollow(NULL, path, NULL, ((flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags) & ~MS_RELATIME, NULL);
483 if (r < 0) {
484 if (((flags ^ new_flags) & flags_mask & ~MS_RELATIME) != 0) /* Ignore MS_RELATIME again,
485 * since kernel adds it in
486 * everywhere, because it's the
487 * default. */
488 return r;
489
490 /* Let's handle redundant remounts gracefully */
491 log_debug_errno(r, "Failed to remount '%s' but flags already match what we want, ignoring: %m", path);
492 }
493
494 return 0;
495 #else
496 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libmount support not compiled in");
497 #endif
498 }
499
500 int bind_remount_one(const char *path, unsigned long new_flags, unsigned long flags_mask) {
501 _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
502
503 proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
504 if (!proc_self_mountinfo)
505 return log_debug_errno(errno, "Failed to open %s: %m", "/proc/self/mountinfo");
506
507 return bind_remount_one_with_mountinfo(path, new_flags, flags_mask, proc_self_mountinfo);
508 }
509
510 static int mount_switch_root_pivot(int fd_newroot, const char *path) {
511 assert(fd_newroot >= 0);
512 assert(path);
513
514 /* Let the kernel tuck the new root under the old one. */
515 if (pivot_root(".", ".") < 0)
516 return log_debug_errno(errno, "Failed to pivot root to new rootfs '%s': %m", path);
517
518 /* Get rid of the old root and reveal our brand new root. (This will always operate on the top-most
519 * mount on our cwd, regardless what our current directory actually points to.) */
520 if (umount2(".", MNT_DETACH) < 0)
521 return log_debug_errno(errno, "Failed to unmount old rootfs: %m");
522
523 return 0;
524 }
525
526 static int mount_switch_root_move(int fd_newroot, const char *path) {
527 assert(fd_newroot >= 0);
528 assert(path);
529
530 /* Move the new root fs */
531 if (mount(".", "/", NULL, MS_MOVE, NULL) < 0)
532 return log_debug_errno(errno, "Failed to move new rootfs '%s': %m", path);
533
534 /* Also change root dir */
535 if (chroot(".") < 0)
536 return log_debug_errno(errno, "Failed to chroot to new rootfs '%s': %m", path);
537
538 return 0;
539 }
540
541 int mount_switch_root_full(const char *path, unsigned long mount_propagation_flag, bool force_ms_move) {
542 _cleanup_close_ int fd_newroot = -EBADF;
543 int r, is_current_root;
544
545 assert(path);
546 assert(mount_propagation_flag_is_valid(mount_propagation_flag));
547
548 fd_newroot = open(path, O_PATH|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
549 if (fd_newroot < 0)
550 return log_debug_errno(errno, "Failed to open new rootfs '%s': %m", path);
551
552 is_current_root = path_is_root_at(fd_newroot, NULL);
553 if (is_current_root < 0)
554 return log_debug_errno(is_current_root, "Failed to determine if target dir is our root already: %m");
555
556 /* Change into the new rootfs. */
557 if (fchdir(fd_newroot) < 0)
558 return log_debug_errno(errno, "Failed to chdir into new rootfs '%s': %m", path);
559
560 /* Make this a NOP if we are supposed to switch to our current root fs. After all, both pivot_root()
561 * and MS_MOVE don't like that. */
562 if (!is_current_root) {
563 if (!force_ms_move) {
564 r = mount_switch_root_pivot(fd_newroot, path);
565 if (r < 0) {
566 log_debug_errno(r, "Failed to pivot into new rootfs '%s', will try to use MS_MOVE instead: %m", path);
567 force_ms_move = true;
568 }
569 }
570 if (force_ms_move) {
571 /* Failed to pivot_root() fallback to MS_MOVE. For example, this may happen if the rootfs is
572 * an initramfs in which case pivot_root() isn't supported. */
573 r = mount_switch_root_move(fd_newroot, path);
574 if (r < 0)
575 return log_debug_errno(r, "Failed to switch to new rootfs '%s' with MS_MOVE: %m", path);
576 }
577 }
578
579 log_debug("Successfully switched root to '%s'.", path);
580
581 /* Finally, let's establish the requested propagation flags. */
582 if (mount_propagation_flag == 0)
583 return 0;
584
585 if (mount(NULL, ".", NULL, mount_propagation_flag | MS_REC, NULL) < 0)
586 return log_debug_errno(errno, "Failed to turn new rootfs '%s' into %s mount: %m",
587 mount_propagation_flag_to_string(mount_propagation_flag), path);
588
589 return 0;
590 }
591
592 int repeat_unmount(const char *path, int flags) {
593 bool done = false;
594
595 assert(path);
596
597 /* If there are multiple mounts on a mount point, this
598 * removes them all */
599
600 for (;;) {
601 if (umount2(path, flags) < 0) {
602
603 if (errno == EINVAL)
604 return done;
605
606 return -errno;
607 }
608
609 done = true;
610 }
611 }
612
613 int mode_to_inaccessible_node(
614 const char *runtime_dir,
615 mode_t mode,
616 char **ret) {
617
618 /* This function maps a node type to a corresponding inaccessible file node. These nodes are created
619 * during early boot by PID 1. In some cases we lacked the privs to create the character and block
620 * devices (maybe because we run in an userns environment, or miss CAP_SYS_MKNOD, or run with a
621 * devices policy that excludes device nodes with major and minor of 0), but that's fine, in that
622 * case we use an AF_UNIX file node instead, which is not the same, but close enough for most
623 * uses. And most importantly, the kernel allows bind mounts from socket nodes to any non-directory
624 * file nodes, and that's the most important thing that matters.
625 *
626 * Note that the runtime directory argument shall be the top-level runtime directory, i.e. /run/ if
627 * we operate in system context and $XDG_RUNTIME_DIR if we operate in user context. */
628
629 _cleanup_free_ char *d = NULL;
630 const char *node;
631
632 assert(ret);
633
634 if (!runtime_dir)
635 runtime_dir = "/run";
636
637 if (S_ISLNK(mode))
638 return -EINVAL;
639
640 node = inode_type_to_string(mode);
641 if (!node)
642 return -EINVAL;
643
644 d = path_join(runtime_dir, "systemd/inaccessible", node);
645 if (!d)
646 return -ENOMEM;
647
648 /* On new kernels unprivileged users are permitted to create 0:0 char device nodes (because they also
649 * act as whiteout inode for overlayfs), but no other char or block device nodes. On old kernels no
650 * device node whatsoever may be created by unprivileged processes. Hence, if the caller asks for the
651 * inaccessible block device node let's see if the block device node actually exists, and if not,
652 * fall back to the character device node. From there fall back to the socket device node. This means
653 * in the best case we'll get the right device node type — but if not we'll hopefully at least get a
654 * device node at all. */
655
656 if (S_ISBLK(mode) &&
657 access(d, F_OK) < 0 && errno == ENOENT) {
658 free(d);
659 d = path_join(runtime_dir, "/systemd/inaccessible/chr");
660 if (!d)
661 return -ENOMEM;
662 }
663
664 if (IN_SET(mode & S_IFMT, S_IFBLK, S_IFCHR) &&
665 access(d, F_OK) < 0 && errno == ENOENT) {
666 free(d);
667 d = path_join(runtime_dir, "/systemd/inaccessible/sock");
668 if (!d)
669 return -ENOMEM;
670 }
671
672 *ret = TAKE_PTR(d);
673 return 0;
674 }
675
676 int mount_flags_to_string(unsigned long flags, char **ret) {
677 static const struct {
678 unsigned long flag;
679 const char *name;
680 } map[] = {
681 { .flag = MS_RDONLY, .name = "MS_RDONLY", },
682 { .flag = MS_NOSUID, .name = "MS_NOSUID", },
683 { .flag = MS_NODEV, .name = "MS_NODEV", },
684 { .flag = MS_NOEXEC, .name = "MS_NOEXEC", },
685 { .flag = MS_SYNCHRONOUS, .name = "MS_SYNCHRONOUS", },
686 { .flag = MS_REMOUNT, .name = "MS_REMOUNT", },
687 { .flag = MS_MANDLOCK, .name = "MS_MANDLOCK", },
688 { .flag = MS_DIRSYNC, .name = "MS_DIRSYNC", },
689 { .flag = MS_NOSYMFOLLOW, .name = "MS_NOSYMFOLLOW", },
690 { .flag = MS_NOATIME, .name = "MS_NOATIME", },
691 { .flag = MS_NODIRATIME, .name = "MS_NODIRATIME", },
692 { .flag = MS_BIND, .name = "MS_BIND", },
693 { .flag = MS_MOVE, .name = "MS_MOVE", },
694 { .flag = MS_REC, .name = "MS_REC", },
695 { .flag = MS_SILENT, .name = "MS_SILENT", },
696 { .flag = MS_POSIXACL, .name = "MS_POSIXACL", },
697 { .flag = MS_UNBINDABLE, .name = "MS_UNBINDABLE", },
698 { .flag = MS_PRIVATE, .name = "MS_PRIVATE", },
699 { .flag = MS_SLAVE, .name = "MS_SLAVE", },
700 { .flag = MS_SHARED, .name = "MS_SHARED", },
701 { .flag = MS_RELATIME, .name = "MS_RELATIME", },
702 { .flag = MS_KERNMOUNT, .name = "MS_KERNMOUNT", },
703 { .flag = MS_I_VERSION, .name = "MS_I_VERSION", },
704 { .flag = MS_STRICTATIME, .name = "MS_STRICTATIME", },
705 { .flag = MS_LAZYTIME, .name = "MS_LAZYTIME", },
706 };
707 _cleanup_free_ char *str = NULL;
708
709 assert(ret);
710
711 FOREACH_ELEMENT(entry, map)
712 if (flags & entry->flag) {
713 if (!strextend_with_separator(&str, "|", entry->name))
714 return -ENOMEM;
715 flags &= ~entry->flag;
716 }
717
718 if (!str || flags != 0)
719 if (strextendf_with_separator(&str, "|", "%lx", flags) < 0)
720 return -ENOMEM;
721
722 *ret = TAKE_PTR(str);
723 return 0;
724 }
725
726 int mount_verbose_full(
727 int error_log_level,
728 const char *what,
729 const char *where,
730 const char *type,
731 unsigned long flags,
732 const char *options,
733 bool follow_symlink) {
734
735 _cleanup_free_ char *fl = NULL, *o = NULL;
736 unsigned long f;
737 int r;
738
739 r = mount_option_mangle(options, flags, &f, &o);
740 if (r < 0)
741 return log_full_errno(error_log_level, r,
742 "Failed to mangle mount options %s: %m",
743 strempty(options));
744
745 (void) mount_flags_to_string(f, &fl);
746
747 if (FLAGS_SET(f, MS_REMOUNT|MS_BIND))
748 log_debug("Changing mount flags %s (%s \"%s\")...",
749 where, strnull(fl), strempty(o));
750 else if (f & MS_REMOUNT)
751 log_debug("Remounting superblock %s (%s \"%s\")...",
752 where, strnull(fl), strempty(o));
753 else if (f & (MS_SHARED|MS_PRIVATE|MS_SLAVE|MS_UNBINDABLE))
754 log_debug("Changing mount propagation %s (%s \"%s\")",
755 where, strnull(fl), strempty(o));
756 else if (f & MS_BIND)
757 log_debug("Bind-mounting %s on %s (%s \"%s\")...",
758 what, where, strnull(fl), strempty(o));
759 else if (f & MS_MOVE)
760 log_debug("Moving mount %s %s %s (%s \"%s\")...",
761 what, glyph(GLYPH_ARROW_RIGHT), where, strnull(fl), strempty(o));
762 else
763 log_debug("Mounting %s (%s) on %s (%s \"%s\")...",
764 strna(what), strna(type), where, strnull(fl), strempty(o));
765
766 if (follow_symlink)
767 r = RET_NERRNO(mount(what, where, type, f, o));
768 else
769 r = mount_nofollow(what, where, type, f, o);
770 if (r < 0)
771 return log_full_errno(error_log_level, r,
772 "Failed to mount %s (type %s) on %s (%s \"%s\"): %m",
773 strna(what), strna(type), where, strnull(fl), strempty(o));
774 return 0;
775 }
776
777 int umount_verbose(
778 int error_log_level,
779 const char *where,
780 int flags) {
781
782 assert(where);
783
784 log_debug("Unmounting '%s'...", where);
785
786 if (umount2(where, flags) < 0)
787 return log_full_errno(error_log_level, errno, "Failed to unmount '%s': %m", where);
788
789 return 0;
790 }
791
792 int umountat_detach_verbose(
793 int error_log_level,
794 int fd,
795 const char *where) {
796
797 /* Similar to umountat_verbose(), but goes by fd + path. This implies MNT_DETACH, since to do this we
798 * must pin the inode in question via an fd. */
799
800 assert(fd >= 0 || fd == AT_FDCWD);
801
802 /* If neither fd nor path are specified take this as reference to the cwd */
803 if (fd == AT_FDCWD && isempty(where))
804 return umount_verbose(error_log_level, ".", MNT_DETACH|UMOUNT_NOFOLLOW);
805
806 /* If we don't actually take the fd into consideration for this operation shortcut things, so that we
807 * don't have to open the inode */
808 if (fd == AT_FDCWD || path_is_absolute(where))
809 return umount_verbose(error_log_level, where, MNT_DETACH|UMOUNT_NOFOLLOW);
810
811 _cleanup_free_ char *prefix = NULL;
812 const char *p;
813 if (fd_get_path(fd, &prefix) < 0)
814 p = "<fd>"; /* if we can't get the path, return something vaguely useful */
815 else
816 p = prefix;
817 _cleanup_free_ char *joined = isempty(where) ? strdup(p) : path_join(p, where);
818
819 log_debug("Unmounting '%s'...", strna(joined));
820
821 _cleanup_close_ int inode_fd = -EBADF;
822 int mnt_fd;
823 if (isempty(where))
824 mnt_fd = fd;
825 else {
826 inode_fd = openat(fd, where, O_PATH|O_CLOEXEC|O_NOFOLLOW);
827 if (inode_fd < 0)
828 return log_full_errno(error_log_level, errno, "Failed to pin '%s': %m", strna(joined));
829
830 mnt_fd = inode_fd;
831 }
832
833 if (umount2(FORMAT_PROC_FD_PATH(mnt_fd), MNT_DETACH) < 0)
834 return log_full_errno(error_log_level, errno, "Failed to unmount '%s': %m", strna(joined));
835
836 return 0;
837 }
838
839 int mount_exchange_graceful(int fsmount_fd, const char *dest, bool mount_beneath) {
840 int r;
841
842 assert(fsmount_fd >= 0);
843 assert(dest);
844
845 /* First, try to mount beneath an existing mount point, and if that works, umount the old mount,
846 * which is now at the top. This will ensure we can atomically replace a mount. Note that this works
847 * also in the case where there are submounts down the tree. Mount propagation is allowed but
848 * restricted to layouts that don't end up propagation the new mount on top of the mount stack. If
849 * this is not supported (minimum kernel v6.5), or if there is no mount on the mountpoint, we get
850 * -EINVAL and then we fallback to normal mounting. */
851
852 r = RET_NERRNO(move_mount(fsmount_fd, /* from_path= */ "",
853 /* to_fd= */ -EBADF, dest,
854 MOVE_MOUNT_F_EMPTY_PATH | (mount_beneath ? MOVE_MOUNT_BENEATH : 0)));
855 if (mount_beneath) {
856 if (r >= 0) /* Mounting beneath worked! Now unmount the upper mount. */
857 return umount_verbose(LOG_DEBUG, dest, UMOUNT_NOFOLLOW|MNT_DETACH);
858
859 if (r == -EINVAL) { /* Fallback if mount_beneath is not supported */
860 log_debug_errno(r,
861 "Cannot mount beneath '%s', falling back to overmount: %m",
862 dest);
863 return mount_exchange_graceful(fsmount_fd, dest, /* mount_beneath= */ false);
864 }
865 }
866
867 return r;
868 }
869
870 int mount_option_mangle(
871 const char *options,
872 unsigned long mount_flags,
873 unsigned long *ret_mount_flags,
874 char **ret_remaining_options) {
875
876 assert(ret_mount_flags);
877 assert(ret_remaining_options);
878
879 if (!options) {
880 *ret_mount_flags = mount_flags;
881 *ret_remaining_options = NULL;
882 return 0;
883 }
884
885 #if HAVE_LIBMOUNT
886 const struct libmnt_optmap *map;
887 _cleanup_free_ char *ret = NULL;
888 int r;
889
890 /* This extracts mount flags from the mount options, and stores
891 * non-mount-flag options to '*ret_remaining_options'.
892 * E.g.,
893 * "rw,nosuid,nodev,relatime,size=1630748k,mode=0700,uid=1000,gid=1000"
894 * is split to MS_NOSUID|MS_NODEV|MS_RELATIME and
895 * "size=1630748k,mode=0700,uid=1000,gid=1000".
896 * See more examples in test-mount-util.c.
897 *
898 * If 'options' does not contain any non-mount-flag options,
899 * then '*ret_remaining_options' is set to NULL instead of empty string.
900 * The validity of options stored in '*ret_remaining_options' is not checked.
901 * If 'options' is NULL, this just copies 'mount_flags' to *ret_mount_flags. */
902
903 r = dlopen_libmount(LOG_DEBUG);
904 if (r < 0)
905 return r;
906
907 map = sym_mnt_get_builtin_optmap(MNT_LINUX_MAP);
908 if (!map)
909 return -EINVAL;
910
911 for (const char *p = options;;) {
912 _cleanup_free_ char *word = NULL;
913 const struct libmnt_optmap *ent;
914
915 r = extract_first_word(&p, &word, ",", EXTRACT_KEEP_QUOTE);
916 if (r < 0)
917 return r;
918 if (r == 0)
919 break;
920
921 for (ent = map; ent->name; ent++) {
922 /* All entries in MNT_LINUX_MAP do not take any argument.
923 * Thus, ent->name does not contain "=" or "[=]". */
924 if (!streq(word, ent->name))
925 continue;
926
927 if (!(ent->mask & MNT_INVERT))
928 mount_flags |= ent->id;
929 else
930 mount_flags &= ~ent->id;
931
932 break;
933 }
934
935 /* If 'word' is not a mount flag, then store it in '*ret_remaining_options'. */
936 if (!ent->name &&
937 !startswith_no_case(word, "x-") &&
938 !strextend_with_separator(&ret, ",", word))
939 return -ENOMEM;
940 }
941
942 *ret_mount_flags = mount_flags;
943 *ret_remaining_options = TAKE_PTR(ret);
944
945 return 0;
946 #else
947 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libmount support not compiled in");
948 #endif
949 }
950
951 static int mount_in_namespace_legacy(
952 const char *chased_src_path,
953 int chased_src_fd,
954 struct stat *chased_src_st,
955 const char *propagate_path,
956 const char *incoming_path,
957 const char *dest,
958 int pidns_fd,
959 int mntns_fd,
960 int root_fd,
961 MountInNamespaceFlags flags,
962 const MountOptions *options,
963 const ImagePolicy *image_policy) {
964
965 _cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
966 char mount_slave[] = "/tmp/propagate.XXXXXX", *mount_tmp, *mount_outside, *p;
967 bool mount_slave_created = false, mount_slave_mounted = false,
968 mount_tmp_created = false, mount_tmp_mounted = false,
969 mount_outside_created = false, mount_outside_mounted = false;
970 _cleanup_(pidref_done) PidRef child = PIDREF_NULL;
971 int r;
972
973 assert(chased_src_path);
974 assert(chased_src_fd >= 0);
975 assert(chased_src_st);
976 assert(propagate_path);
977 assert(incoming_path);
978 assert(dest);
979 assert(pidns_fd >= 0);
980 assert(mntns_fd >= 0);
981 assert(root_fd >= 0);
982 assert(!options || (flags & MOUNT_IN_NAMESPACE_IS_IMAGE));
983
984 p = strjoina(propagate_path, "/");
985 r = access_nofollow(p, F_OK);
986 if (r < 0)
987 return log_debug_errno(r == -ENOENT ? SYNTHETIC_ERRNO(EOPNOTSUPP) : r, "Target does not allow propagation of mount points");
988
989 /* Our goal is to install a new bind mount into the container,
990 possibly read-only. This is irritatingly complex
991 unfortunately, currently.
992
993 First, we start by creating a private playground in /tmp,
994 that we can mount MS_SLAVE. (Which is necessary, since
995 MS_MOVE cannot be applied to mounts with MS_SHARED parent
996 mounts.) */
997
998 if (!mkdtemp(mount_slave))
999 return log_debug_errno(errno, "Failed to create playground %s: %m", mount_slave);
1000
1001 mount_slave_created = true;
1002
1003 r = mount_nofollow_verbose(LOG_DEBUG, mount_slave, mount_slave, NULL, MS_BIND, NULL);
1004 if (r < 0)
1005 goto finish;
1006
1007 mount_slave_mounted = true;
1008
1009 r = mount_nofollow_verbose(LOG_DEBUG, NULL, mount_slave, NULL, MS_SLAVE, NULL);
1010 if (r < 0)
1011 goto finish;
1012
1013 /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
1014 mount_tmp = strjoina(mount_slave, "/mount");
1015 r = make_mount_point_inode_from_mode(AT_FDCWD, mount_tmp, (flags & MOUNT_IN_NAMESPACE_IS_IMAGE) ? S_IFDIR : chased_src_st->st_mode, 0700);
1016 if (r < 0) {
1017 log_debug_errno(r, "Failed to create temporary mount point %s: %m", mount_tmp);
1018 goto finish;
1019 }
1020
1021 mount_tmp_created = true;
1022
1023 if (flags & MOUNT_IN_NAMESPACE_IS_IMAGE)
1024 r = verity_dissect_and_mount(
1025 chased_src_fd,
1026 chased_src_path,
1027 mount_tmp,
1028 options,
1029 image_policy,
1030 /* image_filter= */ NULL,
1031 /* extension_release_data= */ NULL,
1032 /* required_class= */ _IMAGE_CLASS_INVALID,
1033 /* verity= */ NULL,
1034 RUNTIME_SCOPE_SYSTEM,
1035 /* ret_image= */ NULL);
1036 else
1037 r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(chased_src_fd), mount_tmp, NULL, MS_BIND, NULL);
1038 if (r < 0)
1039 goto finish;
1040
1041 mount_tmp_mounted = true;
1042
1043 /* Third, we remount the new bind mount read-only if requested. */
1044 if (flags & MOUNT_IN_NAMESPACE_READ_ONLY) {
1045 r = mount_nofollow_verbose(LOG_DEBUG, NULL, mount_tmp, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL);
1046 if (r < 0)
1047 goto finish;
1048 }
1049
1050 /* Fourth, we move the new bind mount into the propagation directory. This way it will appear there read-only
1051 * right-away. */
1052
1053 mount_outside = strjoina(propagate_path, "/XXXXXX");
1054 if ((flags & MOUNT_IN_NAMESPACE_IS_IMAGE) || S_ISDIR(chased_src_st->st_mode))
1055 r = mkdtemp(mount_outside) ? 0 : -errno;
1056 else {
1057 r = mkostemp_safe(mount_outside);
1058 safe_close(r);
1059 }
1060 if (r < 0) {
1061 log_debug_errno(r, "Cannot create propagation file or directory %s: %m", mount_outside);
1062 goto finish;
1063 }
1064
1065 mount_outside_created = true;
1066
1067 r = mount_nofollow_verbose(LOG_DEBUG, mount_tmp, mount_outside, NULL, MS_MOVE, NULL);
1068 if (r < 0)
1069 goto finish;
1070
1071 mount_outside_mounted = true;
1072 mount_tmp_mounted = false;
1073
1074 if ((flags & MOUNT_IN_NAMESPACE_IS_IMAGE) || S_ISDIR(chased_src_st->st_mode))
1075 (void) rmdir(mount_tmp);
1076 else
1077 (void) unlink(mount_tmp);
1078 mount_tmp_created = false;
1079
1080 (void) umount_verbose(LOG_DEBUG, mount_slave, UMOUNT_NOFOLLOW);
1081 mount_slave_mounted = false;
1082
1083 (void) rmdir(mount_slave);
1084 mount_slave_created = false;
1085
1086 if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) {
1087 log_debug_errno(errno, "Failed to create pipe: %m");
1088 goto finish;
1089 }
1090
1091 r = namespace_fork(
1092 "(sd-bindmnt)",
1093 "(sd-bindmnt-inner)",
1094 FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM,
1095 pidns_fd,
1096 mntns_fd,
1097 /* netns_fd= */ -EBADF,
1098 /* userns_fd= */ -EBADF,
1099 root_fd,
1100 &child);
1101 if (r < 0)
1102 goto finish;
1103 if (r == 0) {
1104 _cleanup_free_ char *mount_outside_fn = NULL, *mount_inside = NULL;
1105
1106 errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
1107
1108 _cleanup_close_ int dest_fd = -EBADF;
1109 _cleanup_free_ char *dest_fn = NULL;
1110 r = chase(dest, /* root= */ NULL, CHASE_PARENT|CHASE_EXTRACT_FILENAME|((flags & MOUNT_IN_NAMESPACE_MAKE_FILE_OR_DIRECTORY) ? CHASE_MKDIR_0755 : 0), &dest_fn, &dest_fd);
1111 if (r < 0)
1112 log_debug_errno(r, "Failed to pin parent directory of mount '%s', ignoring: %m", dest);
1113 else if (flags & MOUNT_IN_NAMESPACE_MAKE_FILE_OR_DIRECTORY) {
1114 r = make_mount_point_inode_from_mode(dest_fd, dest_fn, (flags & MOUNT_IN_NAMESPACE_IS_IMAGE) ? S_IFDIR : chased_src_st->st_mode, 0700);
1115 if (r < 0)
1116 log_debug_errno(r, "Failed to make mount point inode of mount '%s', ignoring: %m", dest);
1117 }
1118
1119 /* Fifth, move the mount to the right place inside */
1120 r = path_extract_filename(mount_outside, &mount_outside_fn);
1121 if (r < 0) {
1122 log_debug_errno(r, "Failed to extract filename from propagation file or directory '%s': %m", mount_outside);
1123 report_errno_and_exit(errno_pipe_fd[1], r);
1124 }
1125
1126 mount_inside = path_join(incoming_path, mount_outside_fn);
1127 if (!mount_inside)
1128 report_errno_and_exit(errno_pipe_fd[1], log_oom_debug());
1129
1130 r = mount_nofollow_verbose(LOG_DEBUG, mount_inside, dest_fd >= 0 ? FORMAT_PROC_FD_PATH(dest_fd) : dest, /* fstype= */ NULL, MS_MOVE, /* options= */ NULL);
1131 if (r < 0)
1132 report_errno_and_exit(errno_pipe_fd[1], r);
1133
1134 _exit(EXIT_SUCCESS);
1135 }
1136
1137 errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
1138
1139 r = pidref_wait_for_terminate_and_check("(sd-bindmnt)", &child, 0);
1140 if (r < 0) {
1141 log_debug_errno(r, "Failed to wait for child: %m");
1142 goto finish;
1143 }
1144 if (r != EXIT_SUCCESS) {
1145 if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r))
1146 log_debug_errno(r, "Failed to mount: %m");
1147 else
1148 log_debug("Child failed.");
1149 goto finish;
1150 }
1151
1152 finish:
1153 if (mount_outside_mounted)
1154 (void) umount_verbose(LOG_DEBUG, mount_outside, UMOUNT_NOFOLLOW);
1155 if (mount_outside_created) {
1156 if ((flags & MOUNT_IN_NAMESPACE_IS_IMAGE) || S_ISDIR(chased_src_st->st_mode))
1157 (void) rmdir(mount_outside);
1158 else
1159 (void) unlink(mount_outside);
1160 }
1161
1162 if (mount_tmp_mounted)
1163 (void) umount_verbose(LOG_DEBUG, mount_tmp, UMOUNT_NOFOLLOW);
1164 if (mount_tmp_created) {
1165 if ((flags & MOUNT_IN_NAMESPACE_IS_IMAGE) || S_ISDIR(chased_src_st->st_mode))
1166 (void) rmdir(mount_tmp);
1167 else
1168 (void) unlink(mount_tmp);
1169 }
1170
1171 if (mount_slave_mounted)
1172 (void) umount_verbose(LOG_DEBUG, mount_slave, UMOUNT_NOFOLLOW);
1173 if (mount_slave_created)
1174 (void) rmdir(mount_slave);
1175
1176 return r;
1177 }
1178
1179 static int mount_in_namespace(
1180 const PidRef *target,
1181 const char *propagate_path,
1182 const char *incoming_path,
1183 const char *src,
1184 const char *dest,
1185 MountInNamespaceFlags flags,
1186 const MountOptions *options,
1187 const ImagePolicy *image_policy) {
1188
1189 _cleanup_close_ int mntns_fd = -EBADF, root_fd = -EBADF, pidns_fd = -EBADF, chased_src_fd = -EBADF;
1190 _cleanup_free_ char *chased_src_path = NULL;
1191 struct stat st;
1192 int r;
1193
1194 assert(propagate_path);
1195 assert(incoming_path);
1196 assert(src);
1197 assert(dest);
1198 assert((flags & MOUNT_IN_NAMESPACE_IS_IMAGE) || (!options && !image_policy));
1199
1200 if (!pidref_is_set(target))
1201 return -ESRCH;
1202
1203 r = pidref_namespace_open(target, &pidns_fd, &mntns_fd, /* ret_netns_fd= */ NULL, /* ret_userns_fd= */ NULL, &root_fd);
1204 if (r < 0)
1205 return log_debug_errno(r, "Failed to retrieve FDs of the target process' namespace: %m");
1206
1207 r = is_our_namespace(mntns_fd, NAMESPACE_MOUNT);
1208 if (r < 0)
1209 return log_debug_errno(r, "Failed to determine if mount namespaces are equal: %m");
1210 /* We can't add new mounts at runtime if the process wasn't started in a namespace */
1211 if (r > 0)
1212 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to activate bind mount in target, not running in a mount namespace.");
1213
1214 r = chase(src, NULL, 0, &chased_src_path, &chased_src_fd);
1215 if (r < 0)
1216 return log_debug_errno(r, "Failed to resolve source path '%s': %m", src);
1217 log_debug("Chased source path '%s': %s", src, chased_src_path);
1218
1219 if (fstat(chased_src_fd, &st) < 0)
1220 return log_debug_errno(errno, "Failed to stat() resolved source path '%s': %m", src);
1221 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… */
1222 return log_debug_errno(SYNTHETIC_ERRNO(ELOOP), "Source path '%s' can't be a symbolic link.", src);
1223
1224 if (!mount_new_api_supported()) /* Fallback if we can't use the new mount API */
1225 return mount_in_namespace_legacy(
1226 chased_src_path,
1227 chased_src_fd,
1228 &st,
1229 propagate_path,
1230 incoming_path,
1231 dest,
1232 pidns_fd,
1233 mntns_fd,
1234 root_fd,
1235 flags,
1236 options,
1237 image_policy);
1238
1239 _cleanup_(dissected_image_unrefp) DissectedImage *img = NULL;
1240 _cleanup_close_ int new_mount_fd = -EBADF;
1241 _cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
1242 _cleanup_(pidref_done) PidRef child = PIDREF_NULL;
1243
1244 if (flags & MOUNT_IN_NAMESPACE_IS_IMAGE) {
1245 r = verity_dissect_and_mount(
1246 chased_src_fd,
1247 chased_src_path,
1248 /* dest= */ NULL,
1249 options,
1250 image_policy,
1251 /* image_filter= */ NULL,
1252 /* extension_release_data= */ NULL,
1253 /* required_class= */ _IMAGE_CLASS_INVALID,
1254 /* verity= */ NULL,
1255 RUNTIME_SCOPE_SYSTEM,
1256 &img);
1257 if (r < 0)
1258 return log_debug_errno(r,
1259 "Failed to dissect and mount image '%s': %m",
1260 chased_src_path);
1261 } else {
1262 new_mount_fd = open_tree(
1263 chased_src_fd,
1264 "",
1265 OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC|AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH);
1266 if (new_mount_fd < 0)
1267 return log_debug_errno(
1268 errno,
1269 "Failed to open mount source '%s': %m",
1270 chased_src_path);
1271
1272 if ((flags & MOUNT_IN_NAMESPACE_READ_ONLY) && mount_setattr(new_mount_fd, "", AT_EMPTY_PATH,
1273 &(struct mount_attr) {
1274 .attr_set = MOUNT_ATTR_RDONLY,
1275 }, MOUNT_ATTR_SIZE_VER0) < 0)
1276 return log_debug_errno(errno,
1277 "Failed to set mount for '%s' to read only: %m",
1278 chased_src_path);
1279 }
1280
1281 if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
1282 return log_debug_errno(errno, "Failed to create pipe: %m");
1283
1284 r = namespace_fork("(sd-bindmnt)",
1285 "(sd-bindmnt-inner)",
1286 FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM,
1287 pidns_fd,
1288 mntns_fd,
1289 /* netns_fd= */ -EBADF,
1290 /* userns_fd= */ -EBADF,
1291 root_fd,
1292 &child);
1293 if (r < 0)
1294 return log_debug_errno(r, "Failed to fork off mount helper into namespace: %m");
1295 if (r == 0) {
1296 errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
1297
1298 _cleanup_close_ int dest_fd = -EBADF;
1299 _cleanup_free_ char *dest_fn = NULL;
1300 r = chase(dest, /* root= */ NULL, CHASE_PARENT|CHASE_EXTRACT_FILENAME|((flags & MOUNT_IN_NAMESPACE_MAKE_FILE_OR_DIRECTORY) ? CHASE_MKDIR_0755 : 0), &dest_fn, &dest_fd);
1301 if (r < 0)
1302 report_errno_and_exit(errno_pipe_fd[1], r);
1303
1304 if (flags & MOUNT_IN_NAMESPACE_MAKE_FILE_OR_DIRECTORY)
1305 (void) make_mount_point_inode_from_mode(dest_fd, dest_fn, img ? S_IFDIR : st.st_mode, 0700);
1306
1307 if (img) {
1308 DissectImageFlags f =
1309 DISSECT_IMAGE_TRY_ATOMIC_MOUNT_EXCHANGE |
1310 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY;
1311
1312 if (flags & MOUNT_IN_NAMESPACE_MAKE_FILE_OR_DIRECTORY)
1313 f |= DISSECT_IMAGE_MKDIR;
1314
1315 if (flags & MOUNT_IN_NAMESPACE_READ_ONLY)
1316 f |= DISSECT_IMAGE_READ_ONLY;
1317
1318 r = dissected_image_mount(
1319 img,
1320 dest,
1321 /* uid_shift= */ UID_INVALID,
1322 /* uid_range= */ UID_INVALID,
1323 /* userns_fd= */ -EBADF,
1324 f);
1325 } else
1326 r = mount_exchange_graceful(new_mount_fd, dest, /* mount_beneath= */ true);
1327
1328 report_errno_and_exit(errno_pipe_fd[1], r);
1329 }
1330
1331 errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
1332
1333 r = pidref_wait_for_terminate_and_check("(sd-bindmnt)", &child, 0);
1334 if (r < 0)
1335 return log_debug_errno(r, "Failed to wait for child: %m");
1336 if (r != EXIT_SUCCESS) {
1337 if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r))
1338 return log_debug_errno(r, "Failed to mount into namespace: %m");
1339
1340 return log_debug_errno(SYNTHETIC_ERRNO(EPROTO), "Child failed.");
1341 }
1342
1343 return 0;
1344 }
1345
1346 int bind_mount_in_namespace(
1347 const PidRef *target,
1348 const char *propagate_path,
1349 const char *incoming_path,
1350 const char *src,
1351 const char *dest,
1352 MountInNamespaceFlags flags) {
1353
1354 return mount_in_namespace(target,
1355 propagate_path,
1356 incoming_path,
1357 src,
1358 dest,
1359 flags & ~MOUNT_IN_NAMESPACE_IS_IMAGE,
1360 /* options= */ NULL,
1361 /* image_policy= */ NULL);
1362 }
1363
1364 int mount_image_in_namespace(
1365 const PidRef *target,
1366 const char *propagate_path,
1367 const char *incoming_path,
1368 const char *src,
1369 const char *dest,
1370 MountInNamespaceFlags flags,
1371 const MountOptions *options,
1372 const ImagePolicy *image_policy) {
1373
1374 return mount_in_namespace(target,
1375 propagate_path,
1376 incoming_path,
1377 src,
1378 dest,
1379 flags | MOUNT_IN_NAMESPACE_IS_IMAGE,
1380 options,
1381 image_policy);
1382 }
1383
1384 int make_mount_point(const char *path) {
1385 int r;
1386
1387 assert(path);
1388
1389 /* If 'path' is already a mount point, does nothing and returns 0. If it is not it makes it one, and returns 1. */
1390
1391 r = path_is_mount_point(path);
1392 if (r < 0)
1393 return log_debug_errno(r, "Failed to determine whether '%s' is a mount point: %m", path);
1394 if (r > 0)
1395 return 0;
1396
1397 r = mount_nofollow_verbose(LOG_DEBUG, path, path, NULL, MS_BIND|MS_REC, NULL);
1398 if (r < 0)
1399 return r;
1400
1401 return 1;
1402 }
1403
1404 int fd_make_mount_point(int fd) {
1405 int r;
1406
1407 assert(fd >= 0);
1408
1409 r = is_mount_point_at(fd, NULL, 0);
1410 if (r < 0)
1411 return log_debug_errno(r, "Failed to determine whether file descriptor is a mount point: %m");
1412 if (r > 0)
1413 return 0;
1414
1415 r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(fd), FORMAT_PROC_FD_PATH(fd), NULL, MS_BIND|MS_REC, NULL);
1416 if (r < 0)
1417 return r;
1418
1419 return 1;
1420 }
1421
1422 int mount_fd_clone(int mount_fd, bool recursive, int *replacement_fd) {
1423 const int flags = OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC|AT_EMPTY_PATH|(recursive ? AT_RECURSIVE : 0);
1424 int r;
1425
1426 assert(mount_fd >= 0);
1427
1428 /* If the input mount fd is supposed to remain cloneable after calling this function, call it as
1429 * follows: mount_fd_clone(mount_fd, recursive, &mount_fd). */
1430
1431 /* Clone a detached mount (that may be owned by a foreign mountns, e.g. mountfsd's). For this to
1432 * work on older kernels, we have to jump through some hoops, because the kernel currently doesn't
1433 * allow us to just call open_tree(OPEN_TREE_CLONE) directly to get a clone of a mount that is
1434 * detached and owned by another mountns. Hence here's what we do: we clone short-lived child in a
1435 * new mount namespace owned by our userns. There, we attach the mount (invisible to anyone else).
1436 * This is sufficient to pass the kernel check, so next we use open_tree(OPEN_TREE_CLONE) to get our
1437 * own detached mount. This we send back to the parent, which then can use it. */
1438
1439 r = RET_NERRNO(open_tree(mount_fd, "", flags));
1440 if (r != -EINVAL)
1441 /* The straightforward path just works? Yay! Don't bother with the complex logic below. No
1442 * need to put a replacement fd in replacement_fd as the original fd is still usable. */
1443 return r;
1444
1445 _cleanup_close_pair_ int transfer_fds[2] = EBADF_PAIR;
1446 r = socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, transfer_fds);
1447 if (r < 0)
1448 return log_debug_errno(errno, "Failed to create socket pair: %m");
1449
1450 _cleanup_close_pair_ int errno_pipe_fds[2] = EBADF_PAIR;
1451 if (pipe2(errno_pipe_fds, O_CLOEXEC|O_NONBLOCK) < 0)
1452 return log_debug_errno(errno, "Failed to open pipe: %m");
1453
1454 /* Fork a child. Note that we set FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE here, i.e. get a new mount namespace */
1455 r = pidref_safe_fork_full(
1456 "(sd-clonemnt)",
1457 /* stdio_fds= */ NULL,
1458 (int[]) { mount_fd, transfer_fds[1], errno_pipe_fds[1] }, 3,
1459 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGKILL|FORK_REOPEN_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE,
1460 /* ret= */ NULL);
1461 if (r < 0) {
1462 errno_pipe_fds[1] = safe_close(errno_pipe_fds[1]);
1463
1464 int q = read_errno(errno_pipe_fds[0]);
1465 if (q < 0 && q != -EIO)
1466 return q;
1467
1468 return r;
1469 }
1470 if (r == 0) { /* Child */
1471
1472 /* Attach mount */
1473 if (move_mount(mount_fd, "", -EBADF, "/", MOVE_MOUNT_F_EMPTY_PATH) < 0) {
1474 log_debug_errno(errno, "Failed to move mount file descriptor to '/': %m");
1475 report_errno_and_exit(errno_pipe_fds[1], -errno);
1476 }
1477
1478 /* If requested by the caller, we clone the fd twice. Why? After move_mount(), the input file
1479 * descriptor can't be move_mount()'ed again, which means we can't clone it again if it comes
1480 * from a different mount namespace. To ensure they can clone the same fd multiple times,
1481 * callers can pass a pointer to the input fd which will be replaced with a second clone,
1482 * which can be move_mount()'ed and thus can be cloned again. */
1483
1484 for (int i = 0; i < 1 + !!replacement_fd; i++) {
1485 /* And now clone the attached mount that is now ours. */
1486 _cleanup_close_ int cloned_fd = open_tree(mount_fd, "", flags);
1487 if (cloned_fd < 0) {
1488 log_debug_errno(errno, "Failed to clone mount file descriptor: %m");
1489 report_errno_and_exit(errno_pipe_fds[1], -errno);
1490 }
1491
1492 /* And send it to the parent. */
1493 r = send_one_fd(transfer_fds[1], cloned_fd, /* flags= */ 0);
1494 if (r < 0)
1495 report_errno_and_exit(errno_pipe_fds[1], r);
1496 }
1497
1498 _exit(EXIT_SUCCESS);
1499 }
1500
1501 transfer_fds[1] = safe_close(transfer_fds[1]);
1502
1503 /* Accept the new cloned mount */
1504 _cleanup_close_ int fd1 = receive_one_fd(transfer_fds[0], 0);
1505 if (fd1 < 0)
1506 return fd1;
1507
1508 if (replacement_fd) {
1509 int fd2 = receive_one_fd(transfer_fds[0], 0);
1510 if (fd2 < 0)
1511 return fd2;
1512
1513 close_and_replace(*replacement_fd, fd2);
1514 }
1515
1516 return TAKE_FD(fd1);
1517 }
1518
1519 int make_userns(uid_t uid_shift,
1520 uid_t uid_range,
1521 uid_t source_owner,
1522 uid_t dest_owner,
1523 RemountIdmapping idmapping) {
1524
1525 _cleanup_close_ int userns_fd = -EBADF;
1526 _cleanup_free_ char *line = NULL;
1527 uid_t source_base = 0;
1528
1529 /* Allocates a userns file descriptor with the mapping we need. For this we'll fork off a child
1530 * process whose only purpose is to give us a new user namespace. It's killed when we got it. */
1531
1532 if (!userns_shift_range_valid(uid_shift, uid_range))
1533 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid UID range for user namespace.");
1534
1535 switch (idmapping) {
1536
1537 case REMOUNT_IDMAPPING_FOREIGN_WITH_HOST_ROOT:
1538 source_base = FOREIGN_UID_BASE;
1539 _fallthrough_;
1540
1541 case REMOUNT_IDMAPPING_NONE:
1542 case REMOUNT_IDMAPPING_HOST_ROOT:
1543
1544 if (asprintf(&line,
1545 UID_FMT " " UID_FMT " " UID_FMT "\n",
1546 source_base, uid_shift, uid_range) < 0)
1547 return log_oom_debug();
1548
1549 /* If requested we'll include an entry in the mapping so that the host root user can make
1550 * changes to the uidmapped mount like it normally would. Specifically, we'll map the user
1551 * with UID_MAPPED_ROOT on the backing fs to UID 0. This is useful, since nspawn code wants
1552 * to create various missing inodes in the OS tree before booting into it, and this becomes
1553 * very easy and straightforward to do if it can just do it under its own regular UID. Note
1554 * that in that case the container's runtime uidmap (i.e. the one the container payload
1555 * processes run in) will leave this UID unmapped, i.e. if we accidentally leave files owned
1556 * by host root in the already uidmapped tree around they'll show up as owned by 'nobody',
1557 * which is safe. (Of course, we shouldn't leave such inodes around, but always chown() them
1558 * to the container's own UID range, but it's good to have a safety net, in case we
1559 * forget it.) */
1560 if (idmapping == REMOUNT_IDMAPPING_HOST_ROOT)
1561 if (strextendf(&line,
1562 UID_FMT " " UID_FMT " " UID_FMT "\n",
1563 UID_MAPPED_ROOT, (uid_t) 0u, (uid_t) 1u) < 0)
1564 return log_oom_debug();
1565
1566 break;
1567
1568 case REMOUNT_IDMAPPING_HOST_OWNER:
1569 /* Remap the owner of the bind mounted directory to the root user within the container. This
1570 * way every file written by root within the container to the bind-mounted directory will
1571 * be owned by the original user from the host. All other users will remain unmapped. */
1572 if (asprintf(&line,
1573 UID_FMT " " UID_FMT " " UID_FMT "\n",
1574 source_owner, uid_shift, (uid_t) 1u) < 0)
1575 return log_oom_debug();
1576 break;
1577
1578 case REMOUNT_IDMAPPING_HOST_OWNER_TO_TARGET_OWNER:
1579 /* Remap the owner of the bind mounted directory to the owner of the target directory
1580 * within the container. This way every file written by target directory owner within the
1581 * container to the bind-mounted directory will be owned by the original host user.
1582 * All other users will remain unmapped. */
1583 if (asprintf(&line,
1584 UID_FMT " " UID_FMT " " UID_FMT "\n",
1585 source_owner, dest_owner, (uid_t) 1u) < 0)
1586 return log_oom_debug();
1587 break;
1588
1589 default:
1590 assert_not_reached();
1591 }
1592
1593 /* We always assign the same UID and GID ranges */
1594 userns_fd = userns_acquire(line, line, /* setgroups_deny= */ true);
1595 if (userns_fd < 0)
1596 return log_debug_errno(userns_fd, "Failed to acquire new userns: %m");
1597
1598 return TAKE_FD(userns_fd);
1599 }
1600
1601 int open_tree_attr_with_fallback(int dir_fd, const char *path, unsigned flags, struct mount_attr *attr) {
1602 _cleanup_close_ int fd = -EBADF;
1603
1604 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
1605 assert(attr);
1606
1607 if (isempty(path)) {
1608 path = "";
1609 flags |= AT_EMPTY_PATH;
1610 }
1611
1612 fd = open_tree_attr(dir_fd, path, flags, attr, sizeof(struct mount_attr));
1613 if (fd >= 0)
1614 return TAKE_FD(fd);
1615 if (!ERRNO_IS_NOT_SUPPORTED(errno))
1616 return log_debug_errno(errno, "Failed to open tree and set mount attributes: %m");
1617
1618 if (attr->attr_clr & MOUNT_ATTR_IDMAP)
1619 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Cannot clear idmap from mount without open_tree_attr()");
1620
1621 fd = open_tree(dir_fd, path, flags);
1622 if (fd < 0)
1623 return log_debug_errno(errno, "Failed to open tree: %m");
1624
1625 if (mount_setattr(fd, "", AT_EMPTY_PATH | (flags & AT_RECURSIVE), attr, sizeof(struct mount_attr)) < 0)
1626 return log_debug_errno(errno, "Failed to change mount attributes: %m");
1627
1628 return TAKE_FD(fd);
1629 }
1630
1631 int open_tree_try_drop_idmap(int dir_fd, const char *path, unsigned flags) {
1632 /* Tries to drop MOUNT_ATTR_IDMAP while calling open_tree_attr(), but if that doesn't work just uses
1633 * a regular open_tree() */
1634
1635 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
1636
1637 if (isempty(path)) {
1638 path = "";
1639 flags |= AT_EMPTY_PATH;
1640 }
1641
1642 _cleanup_close_ int fd = open_tree_attr_with_fallback(
1643 dir_fd,
1644 path,
1645 flags,
1646 &(struct mount_attr) {
1647 .attr_clr = MOUNT_ATTR_IDMAP,
1648 });
1649 if (fd < 0) {
1650 if (!ERRNO_IS_NEG_NOT_SUPPORTED(fd))
1651 return log_debug_errno(fd, "Failed to clear idmap of directory with open_tree_attr(): %m");
1652
1653 log_debug_errno(fd, "Failed to clear idmap with open_tree_attr(), retrying open_tree() without clearing idmap: %m");
1654
1655 fd = RET_NERRNO(open_tree(dir_fd, path, flags));
1656 if (fd < 0)
1657 return log_debug_errno(fd, "Both open_tree() and open_tree_attr() failed, giving up: %m");
1658
1659 log_debug("open_tree() without clearing idmap worked.");
1660 return TAKE_FD(fd);
1661 }
1662
1663 log_debug("Successfully acquired mount fd with cleared idmap.");
1664 return TAKE_FD(fd);
1665 }
1666
1667 int remount_idmap_fd(
1668 char **paths,
1669 int userns_fd,
1670 uint64_t extra_mount_attr_set) {
1671
1672 int r;
1673
1674 assert(userns_fd >= 0);
1675
1676 /* This remounts all specified paths with the specified userns as idmap. It will do so in the
1677 * order specified in the strv: the expectation is that the top-level directories are at the
1678 * beginning, and nested directories in the right, so that the tree can be built correctly from left
1679 * to right. */
1680
1681 size_t n = strv_length(paths);
1682 if (n == 0) /* Nothing to do? */
1683 return 0;
1684
1685 int *mount_fds = NULL;
1686 size_t n_mounts_fds = 0;
1687
1688 mount_fds = new(int, n);
1689 if (!mount_fds)
1690 return log_oom_debug();
1691
1692 CLEANUP_ARRAY(mount_fds, n_mounts_fds, close_many_and_free);
1693
1694 for (size_t i = 0; i < n; i++) {
1695 /* Clone the mount point and et the user namespace mapping attribute on the cloned mount point. */
1696 mount_fds[n_mounts_fds] = open_tree_attr_with_fallback(
1697 AT_FDCWD,
1698 paths[i],
1699 OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC,
1700 &(struct mount_attr) {
1701 .attr_set = MOUNT_ATTR_IDMAP | extra_mount_attr_set,
1702 .userns_fd = userns_fd,
1703 });
1704 if (mount_fds[n_mounts_fds] < 0)
1705 return mount_fds[n_mounts_fds];
1706
1707 n_mounts_fds++;
1708 }
1709
1710 for (size_t i = n; i > 0; i--) { /* Unmount the paths right-to-left */
1711 /* Remove the old mount points now that we have a idmapped mounts as replacement for all of them */
1712 r = umount_verbose(LOG_DEBUG, paths[i-1], UMOUNT_NOFOLLOW);
1713 if (r < 0)
1714 return r;
1715 }
1716
1717 for (size_t i = 0; i < n; i++) { /* Mount the replacement mounts left-to-right */
1718 /* And place the cloned version in its place */
1719 log_debug("Mounting idmapped fs to '%s'", paths[i]);
1720 if (move_mount(mount_fds[i], "", -EBADF, paths[i], MOVE_MOUNT_F_EMPTY_PATH) < 0)
1721 return log_debug_errno(errno, "Failed to attach UID mapped mount to '%s': %m", paths[i]);
1722 }
1723
1724 return 0;
1725 }
1726
1727 int remount_idmap(
1728 char **p,
1729 uid_t uid_shift,
1730 uid_t uid_range,
1731 uid_t source_owner,
1732 uid_t dest_owner,
1733 RemountIdmapping idmapping) {
1734
1735 _cleanup_close_ int userns_fd = -EBADF;
1736
1737 userns_fd = make_userns(uid_shift, uid_range, source_owner, dest_owner, idmapping);
1738 if (userns_fd < 0)
1739 return userns_fd;
1740
1741 return remount_idmap_fd(p, userns_fd, /* extra_mount_attr_set= */ 0);
1742 }
1743
1744 static void sub_mount_clear(SubMount *s) {
1745 assert(s);
1746
1747 s->path = mfree(s->path);
1748 s->mount_fd = safe_close(s->mount_fd);
1749 }
1750
1751 DEFINE_ARRAY_FREE_FUNC(sub_mount_array_free, SubMount, sub_mount_clear);
1752
1753 #if HAVE_LIBMOUNT
1754 static int sub_mount_compare(const SubMount *a, const SubMount *b) {
1755 assert(a);
1756 assert(b);
1757 assert(a->path);
1758 assert(b->path);
1759
1760 return path_compare(a->path, b->path);
1761 }
1762
1763 static void sub_mount_drop(SubMount *s, size_t n) {
1764 assert(s || n == 0);
1765
1766 for (size_t m = 0, i = 1; i < n; i++) {
1767 if (path_startswith(s[i].path, s[m].path))
1768 sub_mount_clear(s + i);
1769 else
1770 m = i;
1771 }
1772 }
1773 #endif
1774
1775 int get_sub_mounts(const char *prefix, SubMount **ret_mounts, size_t *ret_n_mounts) {
1776 #if HAVE_LIBMOUNT
1777 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
1778 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
1779 SubMount *mounts = NULL;
1780 size_t n = 0;
1781 int r;
1782
1783 CLEANUP_ARRAY(mounts, n, sub_mount_array_free);
1784
1785 assert(prefix);
1786 assert(ret_mounts);
1787 assert(ret_n_mounts);
1788
1789 r = libmount_parse_mountinfo(/* source= */ NULL, &table, &iter);
1790 if (r < 0)
1791 return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
1792
1793 for (;;) {
1794 _cleanup_close_ int mount_fd = -EBADF;
1795 _cleanup_free_ char *p = NULL;
1796 struct libmnt_fs *fs;
1797 const char *path;
1798 int id1, id2;
1799
1800 r = sym_mnt_table_next_fs(table, iter, &fs);
1801 if (r == 1)
1802 break; /* EOF */
1803 if (r < 0)
1804 return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
1805
1806 path = sym_mnt_fs_get_target(fs);
1807 if (!path)
1808 continue;
1809
1810 if (isempty(path_startswith(path, prefix)))
1811 continue;
1812
1813 id1 = sym_mnt_fs_get_id(fs);
1814 r = path_get_mnt_id(path, &id2);
1815 if (r < 0) {
1816 log_debug_errno(r, "Failed to get mount ID of '%s', ignoring: %m", path);
1817 continue;
1818 }
1819 if (id1 != id2) {
1820 /* The path may be hidden by another over-mount or already remounted. */
1821 log_debug("The mount IDs of '%s' obtained by libmount and path_get_mnt_id() are different (%i vs %i), ignoring.",
1822 path, id1, id2);
1823 continue;
1824 }
1825
1826 mount_fd = RET_NERRNO(open_tree(AT_FDCWD, path, OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC|AT_RECURSIVE));
1827 if (mount_fd == -ENOENT) /* The path may be hidden by another over-mount or already unmounted. */
1828 continue;
1829 if (mount_fd < 0)
1830 return log_debug_errno(mount_fd, "Failed to open subtree of mounted filesystem '%s': %m", path);
1831
1832 p = strdup(path);
1833 if (!p)
1834 return log_oom_debug();
1835
1836 if (!GREEDY_REALLOC(mounts, n + 1))
1837 return log_oom_debug();
1838
1839 mounts[n++] = (SubMount) {
1840 .path = TAKE_PTR(p),
1841 .mount_fd = TAKE_FD(mount_fd),
1842 };
1843 }
1844
1845 typesafe_qsort(mounts, n, sub_mount_compare);
1846 sub_mount_drop(mounts, n);
1847
1848 *ret_mounts = TAKE_PTR(mounts);
1849 *ret_n_mounts = n;
1850 return 0;
1851 #else
1852 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libmount support not compiled in");
1853 #endif
1854 }
1855
1856 int bind_mount_submounts(
1857 const char *source,
1858 const char *target) {
1859
1860 SubMount *mounts = NULL;
1861 size_t n = 0;
1862 int ret = 0, r;
1863
1864 /* Bind mounts all child mounts of 'source' to 'target'. Useful when setting up a new procfs instance
1865 * with new mount options to copy the original submounts over. */
1866
1867 assert(source);
1868 assert(target);
1869
1870 CLEANUP_ARRAY(mounts, n, sub_mount_array_free);
1871
1872 r = get_sub_mounts(source, &mounts, &n);
1873 if (r < 0)
1874 return r;
1875
1876 FOREACH_ARRAY(m, mounts, n) {
1877 _cleanup_free_ char *t = NULL;
1878 const char *suffix;
1879
1880 if (isempty(m->path))
1881 continue;
1882
1883 assert_se(suffix = path_startswith(m->path, source));
1884
1885 t = path_join(target, suffix);
1886 if (!t)
1887 return -ENOMEM;
1888
1889 r = path_is_mount_point(t);
1890 if (r < 0) {
1891 log_debug_errno(r, "Failed to detect if '%s' already is a mount point, ignoring: %m", t);
1892 continue;
1893 }
1894 if (r > 0) {
1895 log_debug("Not bind mounting '%s' from '%s' to '%s', since there's already a mountpoint.", suffix, source, target);
1896 continue;
1897 }
1898
1899 RET_GATHER(ret, RET_NERRNO(move_mount(m->mount_fd, "", AT_FDCWD, t, MOVE_MOUNT_F_EMPTY_PATH)));
1900 }
1901
1902 return ret;
1903 }
1904
1905 int make_mount_point_inode_from_mode(int dir_fd, const char *dest, mode_t source_mode, mode_t target_mode) {
1906 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
1907 assert(dest);
1908
1909 if (S_ISDIR(source_mode))
1910 return mkdirat_label(dir_fd, dest, target_mode & 07777);
1911 else
1912 return RET_NERRNO(mknodat(dir_fd, dest, S_IFREG|(target_mode & 07666), 0)); /* Mask off X bit */
1913 }
1914
1915 int make_mount_point_inode_from_path(const char *source, const char *dest, mode_t access_mode) {
1916 struct stat st;
1917
1918 assert(source);
1919 assert(dest);
1920
1921 if (stat(source, &st) < 0)
1922 return -errno;
1923
1924 return make_mount_point_inode_from_mode(AT_FDCWD, dest, st.st_mode, access_mode);
1925 }
1926
1927 int trigger_automount_at(int dir_fd, const char *path) {
1928 _cleanup_free_ char *nested = NULL;
1929
1930 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
1931
1932 nested = path_join(path, "a");
1933 if (!nested)
1934 return -ENOMEM;
1935
1936 (void) faccessat(dir_fd, nested, F_OK, 0);
1937
1938 return 0;
1939 }
1940
1941 unsigned long credentials_fs_mount_flags(bool ro) {
1942 /* A tight set of mount flags for credentials mounts */
1943 return MS_NODEV|MS_NOEXEC|MS_NOSUID|MS_NOSYMFOLLOW|(ro ? MS_RDONLY : 0);
1944 }
1945
1946 int fsmount_credentials_fs(int *ret_fsfd) {
1947 _cleanup_close_ int fs_fd = -EBADF;
1948 char size_str[DECIMAL_STR_MAX(uint64_t)];
1949
1950 /* Mounts a file system we can place credentials in, i.e. with tight access modes right from the
1951 * beginning, and ideally swapping turned off. In order of preference:
1952 *
1953 * 1. tmpfs if it supports "noswap" (needs kernel >= 6.3)
1954 * 2. ramfs
1955 * 3. tmpfs without "noswap"
1956 */
1957
1958 fs_fd = fsopen("tmpfs", FSOPEN_CLOEXEC);
1959 if (fs_fd < 0)
1960 return -errno;
1961
1962 if (fsconfig(fs_fd, FSCONFIG_SET_STRING, "nr_inodes", "1024", 0) < 0)
1963 return -errno;
1964
1965 xsprintf(size_str, "%" PRIu64, CREDENTIALS_TOTAL_SIZE_MAX);
1966 if (fsconfig(fs_fd, FSCONFIG_SET_STRING, "size", size_str, 0) < 0)
1967 return -errno;
1968
1969 if (fsconfig(fs_fd, FSCONFIG_SET_FLAG, "noswap", NULL, 0) < 0) {
1970 if (errno != EINVAL)
1971 return -errno;
1972
1973 int ramfs_fd = fsopen("ramfs", FSOPEN_CLOEXEC);
1974 if (ramfs_fd >= 0)
1975 close_and_replace(fs_fd, ramfs_fd);
1976 }
1977
1978 if (fsconfig(fs_fd, FSCONFIG_SET_STRING, "mode", "0700", 0) < 0)
1979 return -errno;
1980
1981 if (fsconfig(fs_fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) < 0)
1982 return -errno;
1983
1984 unsigned mount_attrs = ms_flags_to_mount_attr(credentials_fs_mount_flags(/* ro = */ false));
1985
1986 int mfd = RET_NERRNO(fsmount(fs_fd, FSMOUNT_CLOEXEC, mount_attrs));
1987 if (mfd == -EINVAL) {
1988 /* MS_NOSYMFOLLOW was added in kernel 5.10, but the new mount API counterpart was missing
1989 * until 5.14 (c.f. https://github.com/torvalds/linux/commit/dd8b477f9a3d8edb136207acb3652e1a34a661b7).
1990 *
1991 * TODO: drop this once our baseline is raised to 5.14 */
1992 assert(FLAGS_SET(mount_attrs, MOUNT_ATTR_NOSYMFOLLOW));
1993 mfd = RET_NERRNO(fsmount(fs_fd, FSMOUNT_CLOEXEC, mount_attrs & ~MOUNT_ATTR_NOSYMFOLLOW));
1994 }
1995 if (mfd < 0)
1996 return mfd;
1997
1998 if (ret_fsfd)
1999 *ret_fsfd = TAKE_FD(fs_fd);
2000
2001 return mfd;
2002 }
2003
2004 int mount_credentials_fs(const char *path) {
2005 _cleanup_close_ int mfd = -EBADF;
2006
2007 assert(path);
2008
2009 mfd = fsmount_credentials_fs(/* ret_fsfd= */ NULL);
2010 if (mfd < 0)
2011 return mfd;
2012
2013 return RET_NERRNO(move_mount(mfd, "", AT_FDCWD, path, MOVE_MOUNT_F_EMPTY_PATH));
2014 }
2015
2016 int make_fsmount(
2017 int error_log_level,
2018 const char *what,
2019 const char *type,
2020 unsigned long flags,
2021 const char *options,
2022 int userns_fd) {
2023
2024 _cleanup_close_ int fs_fd = -EBADF, mnt_fd = -EBADF;
2025 _cleanup_free_ char *o = NULL;
2026 unsigned long f;
2027 int r;
2028
2029 assert(type);
2030 assert(what);
2031
2032 r = mount_option_mangle(options, flags, &f, &o);
2033 if (r < 0)
2034 return log_full_errno(
2035 error_log_level, r, "Failed to mangle mount options %s: %m",
2036 strempty(options));
2037
2038 if (DEBUG_LOGGING) {
2039 _cleanup_free_ char *fl = NULL;
2040 (void) mount_flags_to_string(f, &fl);
2041
2042 log_debug("Creating mount fd for %s (%s) (%s \"%s\")...",
2043 strna(what), strna(type), strnull(fl), strempty(o));
2044 }
2045
2046 fs_fd = fsopen(type, FSOPEN_CLOEXEC);
2047 if (fs_fd < 0)
2048 return log_full_errno(error_log_level, errno, "Failed to open superblock for \"%s\": %m", type);
2049
2050 if (fsconfig(fs_fd, FSCONFIG_SET_STRING, "source", what, 0) < 0)
2051 return log_full_errno(error_log_level, errno, "Failed to set mount source for \"%s\" to \"%s\": %m", type, what);
2052
2053 if (FLAGS_SET(f, MS_RDONLY))
2054 if (fsconfig(fs_fd, FSCONFIG_SET_FLAG, "ro", NULL, 0) < 0)
2055 return log_full_errno(error_log_level, errno, "Failed to set read only mount flag for \"%s\": %m", type);
2056
2057 for (const char *p = o;;) {
2058 _cleanup_free_ char *word = NULL;
2059 char *eq;
2060
2061 r = extract_first_word(&p, &word, ",", EXTRACT_KEEP_QUOTE);
2062 if (r < 0)
2063 return log_full_errno(error_log_level, r, "Failed to parse mount option string \"%s\": %m", strempty(o));
2064 if (r == 0)
2065 break;
2066
2067 eq = strchr(word, '=');
2068 if (eq) {
2069 *eq = 0;
2070 eq++;
2071
2072 if (fsconfig(fs_fd, FSCONFIG_SET_STRING, word, eq, 0) < 0)
2073 return log_full_errno(error_log_level, errno, "Failed to set mount option \"%s=%s\" for \"%s\": %m", word, eq, type);
2074 } else {
2075 if (fsconfig(fs_fd, FSCONFIG_SET_FLAG, word, NULL, 0) < 0)
2076 return log_full_errno(error_log_level, errno, "Failed to set mount flag \"%s\" for \"%s\": %m", word, type);
2077 }
2078 }
2079
2080 if (fsconfig(fs_fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) < 0)
2081 return log_full_errno(error_log_level, errno, "Failed to realize fs fd for \"%s\" (\"%s\"): %m", what, type);
2082
2083 mnt_fd = fsmount(fs_fd, FSMOUNT_CLOEXEC, 0);
2084 if (mnt_fd < 0)
2085 return log_full_errno(error_log_level, errno, "Failed to create mount fd for \"%s\" (\"%s\"): %m", what, type);
2086
2087 struct mount_attr ma = {
2088 .attr_clr = ms_flags_to_mount_attr_clr(f),
2089 .attr_set = ms_flags_to_mount_attr(f) | (userns_fd >= 0 ? MOUNT_ATTR_IDMAP : 0),
2090 .userns_fd = userns_fd,
2091 };
2092 if (ma.attr_set != 0 && mount_setattr(mnt_fd, "", AT_EMPTY_PATH|AT_RECURSIVE, &ma, MOUNT_ATTR_SIZE_VER0) < 0)
2093 return log_full_errno(error_log_level,
2094 errno,
2095 "Failed to set mount flags for \"%s\" (\"%s\"): %m",
2096 what,
2097 type);
2098
2099 return TAKE_FD(mnt_fd);
2100 }
2101
2102 char* umount_and_rmdir_and_free(char *p) {
2103 if (!p)
2104 return NULL;
2105
2106 PROTECT_ERRNO;
2107 (void) umount_recursive(p, 0);
2108 (void) rmdir(p);
2109 return mfree(p);
2110 }
2111
2112 char* umount_and_free(char *p) {
2113 if (!p)
2114 return NULL;
2115
2116 PROTECT_ERRNO;
2117 (void) umount_recursive(p, 0);
2118 return mfree(p);
2119 }
2120
2121 char* umount_and_unlink_and_free(char *p) {
2122 if (!p)
2123 return NULL;
2124
2125 PROTECT_ERRNO;
2126 (void) umount2(p, 0);
2127 (void) unlink(p);
2128 return mfree(p);
2129 }
2130
2131 int path_get_mount_info_at(
2132 int dir_fd,
2133 const char *path,
2134 char **ret_fstype,
2135 char **ret_options,
2136 char **ret_source) {
2137
2138 #if HAVE_LIBMOUNT
2139 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
2140 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
2141 int r, mnt_id;
2142
2143 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
2144
2145 r = path_get_mnt_id_at(dir_fd, path, &mnt_id);
2146 if (r < 0)
2147 return log_debug_errno(r, "Failed to get mount ID: %m");
2148
2149 /* When getting options is requested, we also need to parse utab, otherwise userspace options like
2150 * "_netdev" will be lost. */
2151 if (ret_options)
2152 r = libmount_parse_with_utab(&table, &iter);
2153 else
2154 r = libmount_parse_mountinfo(/* source= */ NULL, &table, &iter);
2155 if (r < 0)
2156 return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
2157
2158 for (;;) {
2159 struct libmnt_fs *fs;
2160
2161 r = sym_mnt_table_next_fs(table, iter, &fs);
2162 if (r == 1)
2163 break; /* EOF */
2164 if (r < 0)
2165 return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
2166
2167 if (sym_mnt_fs_get_id(fs) != mnt_id)
2168 continue;
2169
2170 _cleanup_free_ char *fstype = NULL, *options = NULL, *source = NULL;
2171
2172 if (ret_fstype) {
2173 fstype = strdup(strempty(sym_mnt_fs_get_fstype(fs)));
2174 if (!fstype)
2175 return log_oom_debug();
2176 }
2177
2178 if (ret_options) {
2179 options = strdup(strempty(sym_mnt_fs_get_options(fs)));
2180 if (!options)
2181 return log_oom_debug();
2182 }
2183
2184 if (ret_source) {
2185 source = strdup(strempty(sym_mnt_fs_get_source(fs)));
2186 if (!source)
2187 return log_oom_debug();
2188 }
2189
2190 if (ret_fstype)
2191 *ret_fstype = TAKE_PTR(fstype);
2192 if (ret_options)
2193 *ret_options = TAKE_PTR(options);
2194 if (ret_source)
2195 *ret_source = TAKE_PTR(source);
2196
2197 return 0;
2198 }
2199
2200 return log_debug_errno(SYNTHETIC_ERRNO(ESTALE), "Cannot find mount ID %i from /proc/self/mountinfo.", mnt_id);
2201 #else
2202 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libmount support not compiled in");
2203 #endif
2204 }
2205
2206 int path_is_network_fs_harder_at(int dir_fd, const char *path) {
2207 _cleanup_close_ int fd = -EBADF;
2208 int r;
2209
2210 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
2211
2212 fd = xopenat(dir_fd, path, O_PATH | O_CLOEXEC | O_NOFOLLOW);
2213 if (fd < 0)
2214 return fd;
2215
2216 r = fd_is_network_fs(fd);
2217 if (r != 0)
2218 return r;
2219
2220 _cleanup_free_ char *fstype = NULL, *options = NULL;
2221 r = path_get_mount_info_at(fd, /* path= */ NULL, &fstype, &options, /* ret_source= */ NULL);
2222 if (r < 0)
2223 return r;
2224
2225 if (fstype_is_network(fstype))
2226 return true;
2227
2228 if (fstab_test_option(options, "_netdev\0"))
2229 return true;
2230
2231 return false;
2232 }