• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 16280725298

14 Jul 2025 08:16PM UTC coverage: 72.166% (-0.006%) from 72.172%
16280725298

push

github

web-flow
Two fixlets for coverage test (#38183)

302135 of 418667 relevant lines covered (72.17%)

773261.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.1
/src/shared/mount-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdlib.h>
4
#include <sys/mount.h>
5
#include <sys/stat.h>
6
#include <unistd.h>
7

8
#include "alloc-util.h"
9
#include "chase.h"
10
#include "dissect-image.h"
11
#include "errno-util.h"
12
#include "extract-word.h"
13
#include "fd-util.h"
14
#include "fileio.h"
15
#include "format-util.h"
16
#include "fs-util.h"
17
#include "fstab-util.h"
18
#include "glyph-util.h"
19
#include "hashmap.h"
20
#include "libmount-util.h"
21
#include "log.h"
22
#include "mkdir-label.h"
23
#include "mount-util.h"
24
#include "mountpoint-util.h"
25
#include "namespace-util.h"
26
#include "os-util.h"
27
#include "path-util.h"
28
#include "pidref.h"
29
#include "process-util.h"
30
#include "set.h"
31
#include "sort-util.h"
32
#include "stat-util.h"
33
#include "string-util.h"
34
#include "strv.h"
35
#include "tmpfile-util.h"
36
#include "user-util.h"
37

38
int umount_recursive_full(const char *prefix, int flags, char **keep) {
8,105✔
39
        _cleanup_fclose_ FILE *f = NULL;
8,105✔
40
        int n = 0, r;
8,105✔
41

42
        /* Try to umount everything recursively below a directory. Also, take care of stacked mounts, and
43
         * keep unmounting them until they are gone. */
44

45
        f = fopen("/proc/self/mountinfo", "re"); /* Pin the file, in case we unmount /proc/ as part of the logic here */
8,105✔
46
        if (!f)
8,105✔
47
                return log_debug_errno(errno, "Failed to open %s: %m", "/proc/self/mountinfo");
×
48

49
        for (;;) {
42,871✔
50
                _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
17,383✔
51
                _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
25,488✔
52
                bool again = false;
25,488✔
53

54
                r = libmount_parse_mountinfo(f, &table, &iter);
25,488✔
55
                if (r < 0)
25,488✔
56
                        return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
×
57

58
                for (;;) {
2,770,486✔
59
                        bool shall_keep = false;
1,397,987✔
60
                        struct libmnt_fs *fs;
1,397,987✔
61
                        const char *path;
1,397,987✔
62

63
                        r = mnt_table_next_fs(table, iter, &fs);
1,397,987✔
64
                        if (r == 1)
1,397,987✔
65
                                break;
66
                        if (r < 0)
1,389,882✔
67
                                return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
×
68

69
                        path = mnt_fs_get_target(fs);
1,389,882✔
70
                        if (!path)
1,389,882✔
71
                                continue;
1,372,499✔
72

73
                        if (prefix && !path_startswith(path, prefix)) {
2,779,434✔
74
                                // FIXME: This is extremely noisy, we're probably doing something very wrong
75
                                // to trigger this so often, needs more investigation.
76
                                // log_trace("Not unmounting %s, outside of prefix: %s", path, prefix);
77
                                continue;
1,363,598✔
78
                        }
79

80
                        STRV_FOREACH(k, keep)
26,354✔
81
                                /* Match against anything in the path to the dirs to keep, or below the dirs to keep */
82
                                if (path_startswith(path, *k) || path_startswith(*k, path)) {
345✔
83
                                        shall_keep = true;
275✔
84
                                        break;
275✔
85
                                }
86
                        if (shall_keep) {
26,559✔
87
                                log_debug("Not unmounting %s, referenced by keep list.", path);
275✔
88
                                continue;
275✔
89
                        }
90

91
                        if (umount2(path, flags | UMOUNT_NOFOLLOW) < 0) {
26,009✔
92
                                log_debug_errno(errno, "Failed to umount %s, ignoring: %m", path);
8,626✔
93
                                continue;
8,626✔
94
                        }
95

96
                        log_trace("Successfully unmounted %s", path);
17,383✔
97

98
                        again = true;
17,383✔
99
                        n++;
17,383✔
100

101
                        break;
17,383✔
102
                }
103

104
                if (!again)
8,105✔
105
                        break;
106

107
                rewind(f);
17,383✔
108
        }
109

110
        return n;
8,105✔
111
}
112

113
#define MS_CONVERTIBLE_FLAGS (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_NOSYMFOLLOW)
114

115
static uint64_t ms_flags_to_mount_attr(unsigned long a) {
35,180✔
116
        uint64_t f = 0;
35,180✔
117

118
        if (FLAGS_SET(a, MS_RDONLY))
35,180✔
119
                f |= MOUNT_ATTR_RDONLY;
1,367✔
120

121
        if (FLAGS_SET(a, MS_NOSUID))
35,180✔
122
                f |= MOUNT_ATTR_NOSUID;
16,225✔
123

124
        if (FLAGS_SET(a, MS_NODEV))
35,180✔
125
                f |= MOUNT_ATTR_NODEV;
2✔
126

127
        if (FLAGS_SET(a, MS_NOEXEC))
35,180✔
128
                f |= MOUNT_ATTR_NOEXEC;
2✔
129

130
        if (FLAGS_SET(a, MS_NOSYMFOLLOW))
35,180✔
131
                f |= MOUNT_ATTR_NOSYMFOLLOW;
×
132

133
        return f;
35,180✔
134
}
135

136
static bool skip_mount_set_attr = false;
137

138
/* Use this function only if you do not have direct access to /proc/self/mountinfo but the caller can open it
139
 * for you. This is the case when /proc is masked or not mounted. Otherwise, use bind_remount_recursive. */
140
int bind_remount_recursive_with_mountinfo(
33,980✔
141
                const char *prefix,
142
                unsigned long new_flags,
143
                unsigned long flags_mask,
144
                char **deny_list,
145
                FILE *proc_self_mountinfo) {
146

147
        _cleanup_fclose_ FILE *proc_self_mountinfo_opened = NULL;
33,980✔
148
        _cleanup_set_free_ Set *done = NULL;
33,980✔
149
        unsigned n_tries = 0;
33,980✔
150
        int r;
33,980✔
151

152
        assert(prefix);
33,980✔
153

154
        if ((flags_mask & ~MS_CONVERTIBLE_FLAGS) == 0 && strv_isempty(deny_list) && !skip_mount_set_attr) {
49,494✔
155
                /* Let's take a shortcut for all the flags we know how to convert into mount_setattr() flags */
156

157
                if (mount_setattr(AT_FDCWD, prefix, AT_SYMLINK_NOFOLLOW|AT_RECURSIVE,
15,514✔
158
                                  &(struct mount_attr) {
15,514✔
159
                                          .attr_set = ms_flags_to_mount_attr(new_flags & flags_mask),
15,514✔
160
                                          .attr_clr = ms_flags_to_mount_attr(~new_flags & flags_mask),
15,514✔
161
                                  }, MOUNT_ATTR_SIZE_VER0) < 0) {
162

163
                        log_debug_errno(errno, "mount_setattr() failed, falling back to classic remounting: %m");
2✔
164

165
                        /* We fall through to classic behaviour if not supported (i.e. kernel < 5.12). We
166
                         * also do this for all other kinds of errors since they are so many different, and
167
                         * mount_setattr() has no graceful mode where it continues despite seeing errors one
168
                         * some mounts, but we want that. Moreover mount_setattr() only works on the mount
169
                         * point inode itself, not a non-mount point inode, and we want to support arbitrary
170
                         * prefixes here. */
171

172
                        if (ERRNO_IS_NOT_SUPPORTED(errno)) /* if not supported, then don't bother at all anymore */
2✔
173
                                skip_mount_set_attr = true;
×
174
                } else
175
                        return 0; /* Nice, this worked! */
15,512✔
176
        }
177

178
        if (!proc_self_mountinfo) {
18,468✔
179
                r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo_opened);
3✔
180
                if (r < 0)
3✔
181
                        return r;
182

183
                proc_self_mountinfo = proc_self_mountinfo_opened;
3✔
184
        }
185

186
        /* Recursively remount a directory (and all its submounts) with desired flags (MS_READONLY,
187
         * MS_NOSUID, MS_NOEXEC). If the directory is already mounted, we reuse the mount and simply mark it
188
         * MS_BIND|MS_RDONLY (or remove the MS_RDONLY for read-write operation), ditto for other flags. If it
189
         * isn't we first make it one. Afterwards we apply (or remove) the flags to all submounts we can
190
         * access, too. When mounts are stacked on the same mount point we only care for each individual
191
         * "top-level" mount on each point, as we cannot influence/access the underlying mounts anyway. We do
192
         * not have any effect on future submounts that might get propagated, they might be writable
193
         * etc. This includes future submounts that have been triggered via autofs. Also note that we can't
194
         * operate atomically here. Mounts established while we process the tree might or might not get
195
         * noticed and thus might or might not be covered.
196
         *
197
         * If the "deny_list" parameter is specified it may contain a list of subtrees to exclude from the
198
         * remount operation. Note that we'll ignore the deny list for the top-level path. */
199

200
        for (;;) {
36,942✔
201
                _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
36,942✔
202
                _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
36,942✔
203
                _cleanup_hashmap_free_ Hashmap *todo = NULL;
36,940✔
204
                bool top_autofs = false;
36,942✔
205

206
                if (n_tries++ >= 32) /* Let's not retry this loop forever */
36,942✔
207
                        return -EBUSY;
208

209
                rewind(proc_self_mountinfo);
36,942✔
210

211
                r = libmount_parse_mountinfo(proc_self_mountinfo, &table, &iter);
36,942✔
212
                if (r < 0)
36,942✔
213
                        return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
×
214

215
                for (;;) {
2,610,346✔
216
                        _cleanup_free_ char *d = NULL;
2,573,404✔
217
                        const char *path, *type, *opts;
2,610,346✔
218
                        unsigned long flags = 0;
2,610,346✔
219
                        struct libmnt_fs *fs;
2,610,346✔
220

221
                        r = mnt_table_next_fs(table, iter, &fs);
2,610,346✔
222
                        if (r == 1) /* EOF */
2,610,346✔
223
                                break;
224
                        if (r < 0)
2,573,404✔
225
                                return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
×
226

227
                        path = mnt_fs_get_target(fs);
2,573,404✔
228
                        if (!path)
2,573,404✔
229
                                continue;
×
230

231
                        if (!path_startswith(path, prefix))
2,573,404✔
232
                                continue;
2,509,779✔
233

234
                        type = mnt_fs_get_fstype(fs);
63,625✔
235
                        if (!type)
63,625✔
236
                                continue;
×
237

238
                        /* Let's ignore autofs mounts. If they aren't triggered yet, we want to avoid
239
                         * triggering them, as we don't make any guarantees for future submounts anyway. If
240
                         * they are already triggered, then we will find another entry for this. */
241
                        if (streq(type, "autofs")) {
63,625✔
242
                                top_autofs = top_autofs || path_equal(path, prefix);
5,032✔
243
                                continue;
2,516✔
244
                        }
245

246
                        if (set_contains(done, path))
61,109✔
247
                                continue;
22,304✔
248

249
                        /* Ignore this mount if it is deny-listed, but only if it isn't the top-level mount
250
                         * we shall operate on. */
251
                        if (!path_equal(path, prefix)) {
38,805✔
252
                                bool deny_listed = false;
267,056✔
253

254
                                STRV_FOREACH(i, deny_list) {
267,056✔
255
                                        if (path_equal(*i, prefix))
263,225✔
256
                                                continue;
20,324✔
257

258
                                        if (!path_startswith(*i, prefix))
242,901✔
259
                                                continue;
136,098✔
260

261
                                        if (path_startswith(path, *i)) {
106,803✔
262
                                                deny_listed = true;
263
                                                log_trace("Not remounting %s deny-listed by %s, called for %s", path, *i, prefix);
264
                                                break;
265
                                        }
266
                                }
267

268
                                if (deny_listed)
20,335✔
269
                                        continue;
16,504✔
270
                        }
271

272
                        opts = mnt_fs_get_vfs_options(fs);
22,301✔
273
                        if (opts) {
22,301✔
274
                                r = mnt_optstr_get_flags(opts, &flags, mnt_get_builtin_optmap(MNT_LINUX_MAP));
22,301✔
275
                                if (r < 0)
22,301✔
276
                                        log_debug_errno(r, "Could not get flags for '%s', ignoring: %m", path);
×
277
                        }
278

279
                        d = strdup(path);
22,301✔
280
                        if (!d)
22,301✔
281
                                return -ENOMEM;
282

283
                        r = hashmap_ensure_put(&todo, &path_hash_ops_free, d, ULONG_TO_PTR(flags));
22,301✔
284
                        if (r == -EEXIST)
22,301✔
285
                                /* If the same path was recorded, but with different mount flags, update it:
286
                                 * it means a mount point is overmounted, and libmount returns the "bottom" (or
287
                                 * older one) first, but we want to reapply the flags from the "top" (or newer
288
                                 * one). See: https://github.com/systemd/systemd/issues/20032
289
                                 * Note that this shouldn't really fail, as we were just told that the key
290
                                 * exists, and it's an update so we want 'd' to be freed immediately. */
291
                                r = hashmap_update(todo, d, ULONG_TO_PTR(flags));
9✔
292
                        if (r < 0)
22,301✔
293
                                return r;
294
                        if (r > 0)
22,301✔
295
                                TAKE_PTR(d);
22,258✔
296
                }
297

298
                /* Check if the top-level directory was among what we have seen so far. For that check both
299
                 * 'done' and 'todo'. Also check 'top_autofs' because if the top-level dir is an autofs we'll
300
                 * not include it in either set but will set this bool. */
301
                if (!set_contains(done, prefix) &&
36,942✔
302
                    !(top_autofs || hashmap_contains(todo, prefix))) {
18,470✔
303

304
                        /* The prefix directory itself is not yet a mount, make it one. */
305
                        r = mount_nofollow(prefix, prefix, NULL, MS_BIND|MS_REC, NULL);
2✔
306
                        if (r < 0)
2✔
307
                                return r;
308

309
                        /* Immediately rescan, so that we pick up the new mount's flags */
310
                        continue;
2✔
311
                }
312

313
                /* If we have no submounts to process anymore, we are done */
314
                if (hashmap_isempty(todo))
36,940✔
315
                        return 0;
316

317
                for (;;) {
40,729✔
318
                        unsigned long flags;
40,729✔
319
                        char *x = NULL;
40,729✔
320

321
                        /* Take the first mount from our list of mounts to still process */
322
                        flags = PTR_TO_ULONG(hashmap_steal_first_key_and_value(todo, (void**) &x));
40,729✔
323
                        if (!x)
40,729✔
324
                                break;
325

326
                        r = set_ensure_consume(&done, &path_hash_ops_free, x);
22,257✔
327
                        if (IN_SET(r, 0, -EEXIST))
22,257✔
328
                                continue; /* Already done */
284✔
329
                        if (r < 0)
22,257✔
330
                                return r;
×
331

332
                        /* Now, remount this with the new flags set, but exclude MS_RELATIME from it. (It's
333
                         * the default anyway, thus redundant, and in userns we'll get an error if we try to
334
                         * explicitly enable it) */
335
                        r = mount_nofollow(NULL, x, NULL, ((flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags) & ~MS_RELATIME, NULL);
22,257✔
336
                        if (r < 0) {
22,257✔
337
                                int q;
284✔
338

339
                                /* OK, so the remount of this entry failed. We'll ultimately ignore this in
340
                                 * almost all cases (there are simply so many reasons why this can fail,
341
                                 * think autofs, NFS, FUSE, …), but let's generate useful debug messages at
342
                                 * the very least. */
343

344
                                q = path_is_mount_point(x);
284✔
345
                                if (IN_SET(q, 0, -ENOENT)) {
284✔
346
                                        /* Hmm, whaaaa? The mount point is not actually a mount point? Then
347
                                         * it is either obstructed by a later mount or somebody has been
348
                                         * racing against us and removed it. Either way the mount point
349
                                         * doesn't matter to us, let's ignore it hence. */
350
                                        log_debug_errno(r, "Mount point '%s' to remount is not a mount point anymore, ignoring remount failure: %m", x);
280✔
351
                                        continue;
280✔
352
                                }
353
                                if (q < 0) /* Any other error on this? Just log and continue */
4✔
354
                                        log_debug_errno(q, "Failed to determine whether '%s' is a mount point or not, ignoring: %m", x);
×
355

356
                                if (((flags ^ new_flags) & flags_mask & ~MS_RELATIME) == 0) { /* ignore MS_RELATIME while comparing */
4✔
357
                                        log_debug_errno(r, "Couldn't remount '%s', but the flags already match what we want, hence ignoring: %m", x);
×
358
                                        continue;
×
359
                                }
360

361
                                /* Make this fatal if this is the top-level mount */
362
                                if (path_equal(x, prefix))
4✔
363
                                        return r;
364

365
                                /* If this is not the top-level mount, then handle this gracefully: log but
366
                                 * otherwise ignore. With NFS, FUSE, autofs there are just too many reasons
367
                                 * this might fail without a chance for us to do anything about it, let's
368
                                 * hence be strict on the top-level mount and lenient on the inner ones. */
369
                                log_debug_errno(r, "Couldn't remount submount '%s' for unexpected reason, ignoring: %m", x);
4✔
370
                                continue;
4✔
371
                        }
372

373
                        log_trace("Remounted %s.", x);
21,973✔
374
                }
375
        }
376
}
377

378
int bind_remount_one_with_mountinfo(
2,075✔
379
                const char *path,
380
                unsigned long new_flags,
381
                unsigned long flags_mask,
382
                FILE *proc_self_mountinfo) {
383

384
        _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
2,075✔
385
        unsigned long flags = 0;
2,075✔
386
        struct libmnt_fs *fs;
2,075✔
387
        const char *opts;
2,075✔
388
        int r;
2,075✔
389

390
        assert(path);
2,075✔
391
        assert(proc_self_mountinfo);
2,075✔
392

393
        if ((flags_mask & ~MS_CONVERTIBLE_FLAGS) == 0 && !skip_mount_set_attr) {
2,075✔
394
                /* Let's take a shortcut for all the flags we know how to convert into mount_setattr() flags */
395

396
                if (mount_setattr(AT_FDCWD, path, AT_SYMLINK_NOFOLLOW,
2,075✔
397
                                  &(struct mount_attr) {
2,075✔
398
                                          .attr_set = ms_flags_to_mount_attr(new_flags & flags_mask),
2,075✔
399
                                          .attr_clr = ms_flags_to_mount_attr(~new_flags & flags_mask),
2,075✔
400
                                  }, MOUNT_ATTR_SIZE_VER0) < 0) {
401

402
                        log_debug_errno(errno, "mount_setattr() didn't work, falling back to classic remounting: %m");
4✔
403

404
                        if (ERRNO_IS_NOT_SUPPORTED(errno)) /* if not supported, then don't bother at all anymore */
4✔
405
                                skip_mount_set_attr = true;
×
406
                } else
407
                        return 0; /* Nice, this worked! */
2,071✔
408
        }
409

410
        rewind(proc_self_mountinfo);
4✔
411

412
        table = mnt_new_table();
4✔
413
        if (!table)
4✔
414
                return -ENOMEM;
415

416
        r = mnt_table_parse_stream(table, proc_self_mountinfo, "/proc/self/mountinfo");
4✔
417
        if (r < 0)
4✔
418
                return r;
419

420
        fs = mnt_table_find_target(table, path, MNT_ITER_FORWARD);
4✔
421
        if (!fs) {
4✔
422
                r = access_nofollow(path, F_OK); /* Hmm, it's not in the mount table, but does it exist at all? */
4✔
423
                if (r < 0)
4✔
424
                        return r;
425

426
                return -EINVAL; /* Not a mount point we recognize */
2✔
427
        }
428

429
        opts = mnt_fs_get_vfs_options(fs);
×
430
        if (opts) {
×
431
                r = mnt_optstr_get_flags(opts, &flags, mnt_get_builtin_optmap(MNT_LINUX_MAP));
×
432
                if (r < 0)
×
433
                        log_debug_errno(r, "Could not get flags for '%s', ignoring: %m", path);
×
434
        }
435

436
        r = mount_nofollow(NULL, path, NULL, ((flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags) & ~MS_RELATIME, NULL);
×
437
        if (r < 0) {
×
438
                if (((flags ^ new_flags) & flags_mask & ~MS_RELATIME) != 0) /* Ignore MS_RELATIME again,
×
439
                                                                             * since kernel adds it in
440
                                                                             * everywhere, because it's the
441
                                                                             * default. */
442
                        return r;
443

444
                /* Let's handle redundant remounts gracefully */
445
                log_debug_errno(r, "Failed to remount '%s' but flags already match what we want, ignoring: %m", path);
4✔
446
        }
447

448
        return 0;
449
}
450

451
int bind_remount_one(const char *path, unsigned long new_flags, unsigned long flags_mask) {
53✔
452
        _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
53✔
453

454
        proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
53✔
455
        if (!proc_self_mountinfo)
53✔
456
                return log_debug_errno(errno, "Failed to open %s: %m", "/proc/self/mountinfo");
×
457

458
        return bind_remount_one_with_mountinfo(path, new_flags, flags_mask, proc_self_mountinfo);
53✔
459
}
460

461
static int mount_switch_root_pivot(int fd_newroot, const char *path) {
2,174✔
462
        assert(fd_newroot >= 0);
2,174✔
463
        assert(path);
2,174✔
464

465
        /* Let the kernel tuck the new root under the old one. */
466
        if (pivot_root(".", ".") < 0)
2,174✔
467
                return log_debug_errno(errno, "Failed to pivot root to new rootfs '%s': %m", path);
×
468

469
        /* Get rid of the old root and reveal our brand new root. (This will always operate on the top-most
470
         * mount on our cwd, regardless what our current directory actually points to.) */
471
        if (umount2(".", MNT_DETACH) < 0)
2,174✔
472
                return log_debug_errno(errno, "Failed to unmount old rootfs: %m");
×
473

474
        return 0;
475
}
476

477
static int mount_switch_root_move(int fd_newroot, const char *path) {
×
478
        assert(fd_newroot >= 0);
×
479
        assert(path);
×
480

481
        /* Move the new root fs */
482
        if (mount(".", "/", NULL, MS_MOVE, NULL) < 0)
×
483
                return log_debug_errno(errno, "Failed to move new rootfs '%s': %m", path);
×
484

485
        /* Also change root dir */
486
        if (chroot(".") < 0)
×
487
                return log_debug_errno(errno, "Failed to chroot to new rootfs '%s': %m", path);
×
488

489
        return 0;
490
}
491

492
int mount_switch_root_full(const char *path, unsigned long mount_propagation_flag, bool force_ms_move) {
2,176✔
493
        _cleanup_close_ int fd_newroot = -EBADF;
2,176✔
494
        int r, is_current_root;
2,176✔
495

496
        assert(path);
2,176✔
497
        assert(mount_propagation_flag_is_valid(mount_propagation_flag));
2,176✔
498

499
        fd_newroot = open(path, O_PATH|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
2,176✔
500
        if (fd_newroot < 0)
2,176✔
501
                return log_debug_errno(errno, "Failed to open new rootfs '%s': %m", path);
×
502

503
        is_current_root = path_is_root_at(fd_newroot, NULL);
2,176✔
504
        if (is_current_root < 0)
2,176✔
505
                return log_debug_errno(is_current_root, "Failed to determine if target dir is our root already: %m");
×
506

507
        /* Change into the new rootfs. */
508
        if (fchdir(fd_newroot) < 0)
2,176✔
509
                return log_debug_errno(errno, "Failed to chdir into new rootfs '%s': %m", path);
×
510

511
        /* Make this a NOP if we are supposed to switch to our current root fs. After all, both pivot_root()
512
         * and MS_MOVE don't like that. */
513
        if (!is_current_root) {
2,176✔
514
                if (!force_ms_move) {
2,174✔
515
                        r = mount_switch_root_pivot(fd_newroot, path);
2,174✔
516
                        if (r < 0) {
2,174✔
517
                                log_debug_errno(r, "Failed to pivot into new rootfs '%s', will try to use MS_MOVE instead: %m", path);
×
518
                                force_ms_move = true;
519
                        }
520
                }
521
                if (force_ms_move) {
522
                        /* Failed to pivot_root() fallback to MS_MOVE. For example, this may happen if the rootfs is
523
                         * an initramfs in which case pivot_root() isn't supported. */
524
                        r = mount_switch_root_move(fd_newroot, path);
×
525
                        if (r < 0)
×
526
                                return log_debug_errno(r, "Failed to switch to new rootfs '%s' with MS_MOVE: %m", path);
×
527
                }
528
        }
529

530
        log_debug("Successfully switched root to '%s'.", path);
2,176✔
531

532
        /* Finally, let's establish the requested propagation flags. */
533
        if (mount_propagation_flag == 0)
2,176✔
534
                return 0;
535

536
        if (mount(NULL, ".", NULL, mount_propagation_flag | MS_REC, NULL) < 0)
226✔
537
                return log_debug_errno(errno, "Failed to turn new rootfs '%s' into %s mount: %m",
×
538
                                       mount_propagation_flag_to_string(mount_propagation_flag), path);
539

540
        return 0;
541
}
542

543
int repeat_unmount(const char *path, int flags) {
19✔
544
        bool done = false;
19✔
545

546
        assert(path);
19✔
547

548
        /* If there are multiple mounts on a mount point, this
549
         * removes them all */
550

551
        for (;;) {
38✔
552
                if (umount2(path, flags) < 0) {
38✔
553

554
                        if (errno == EINVAL)
19✔
555
                                return done;
19✔
556

557
                        return -errno;
×
558
                }
559

560
                done = true;
561
        }
562
}
563

564
int mode_to_inaccessible_node(
4,565✔
565
                const char *runtime_dir,
566
                mode_t mode,
567
                char **ret) {
568

569
        /* This function maps a node type to a corresponding inaccessible file node. These nodes are created
570
         * during early boot by PID 1. In some cases we lacked the privs to create the character and block
571
         * devices (maybe because we run in an userns environment, or miss CAP_SYS_MKNOD, or run with a
572
         * devices policy that excludes device nodes with major and minor of 0), but that's fine, in that
573
         * case we use an AF_UNIX file node instead, which is not the same, but close enough for most
574
         * uses. And most importantly, the kernel allows bind mounts from socket nodes to any non-directory
575
         * file nodes, and that's the most important thing that matters.
576
         *
577
         * Note that the runtime directory argument shall be the top-level runtime directory, i.e. /run/ if
578
         * we operate in system context and $XDG_RUNTIME_DIR if we operate in user context. */
579

580
        _cleanup_free_ char *d = NULL;
4,565✔
581
        const char *node;
4,565✔
582

583
        assert(ret);
4,565✔
584

585
        if (!runtime_dir)
4,565✔
586
                runtime_dir = "/run";
4✔
587

588
        if (S_ISLNK(mode))
4,565✔
589
                return -EINVAL;
590

591
        node = inode_type_to_string(mode);
4,565✔
592
        if (!node)
4,565✔
593
                return -EINVAL;
594

595
        d = path_join(runtime_dir, "systemd/inaccessible", node);
4,565✔
596
        if (!d)
4,565✔
597
                return -ENOMEM;
598

599
        /* On new kernels unprivileged users are permitted to create 0:0 char device nodes (because they also
600
         * act as whiteout inode for overlayfs), but no other char or block device nodes. On old kernels no
601
         * device node whatsoever may be created by unprivileged processes. Hence, if the caller asks for the
602
         * inaccessible block device node let's see if the block device node actually exists, and if not,
603
         * fall back to the character device node. From there fall back to the socket device node. This means
604
         * in the best case we'll get the right device node type — but if not we'll hopefully at least get a
605
         * device node at all. */
606

607
        if (S_ISBLK(mode) &&
4,565✔
608
            access(d, F_OK) < 0 && errno == ENOENT) {
×
609
                free(d);
×
610
                d = path_join(runtime_dir, "/systemd/inaccessible/chr");
×
611
                if (!d)
×
612
                        return -ENOMEM;
613
        }
614

615
        if (IN_SET(mode & S_IFMT, S_IFBLK, S_IFCHR) &&
5,094✔
616
            access(d, F_OK) < 0 && errno == ENOENT) {
529✔
617
                free(d);
×
618
                d = path_join(runtime_dir, "/systemd/inaccessible/sock");
×
619
                if (!d)
×
620
                        return -ENOMEM;
621
        }
622

623
        *ret = TAKE_PTR(d);
4,565✔
624
        return 0;
4,565✔
625
}
626

627
int mount_flags_to_string(unsigned long flags, char **ret) {
46,891✔
628
        static const struct {
46,891✔
629
                unsigned long flag;
630
                const char *name;
631
        } map[] = {
632
                { .flag = MS_RDONLY,      .name = "MS_RDONLY",      },
633
                { .flag = MS_NOSUID,      .name = "MS_NOSUID",      },
634
                { .flag = MS_NODEV,       .name = "MS_NODEV",       },
635
                { .flag = MS_NOEXEC,      .name = "MS_NOEXEC",      },
636
                { .flag = MS_SYNCHRONOUS, .name = "MS_SYNCHRONOUS", },
637
                { .flag = MS_REMOUNT,     .name = "MS_REMOUNT",     },
638
                { .flag = MS_MANDLOCK,    .name = "MS_MANDLOCK",    },
639
                { .flag = MS_DIRSYNC,     .name = "MS_DIRSYNC",     },
640
                { .flag = MS_NOSYMFOLLOW, .name = "MS_NOSYMFOLLOW", },
641
                { .flag = MS_NOATIME,     .name = "MS_NOATIME",     },
642
                { .flag = MS_NODIRATIME,  .name = "MS_NODIRATIME",  },
643
                { .flag = MS_BIND,        .name = "MS_BIND",        },
644
                { .flag = MS_MOVE,        .name = "MS_MOVE",        },
645
                { .flag = MS_REC,         .name = "MS_REC",         },
646
                { .flag = MS_SILENT,      .name = "MS_SILENT",      },
647
                { .flag = MS_POSIXACL,    .name = "MS_POSIXACL",    },
648
                { .flag = MS_UNBINDABLE,  .name = "MS_UNBINDABLE",  },
649
                { .flag = MS_PRIVATE,     .name = "MS_PRIVATE",     },
650
                { .flag = MS_SLAVE,       .name = "MS_SLAVE",       },
651
                { .flag = MS_SHARED,      .name = "MS_SHARED",      },
652
                { .flag = MS_RELATIME,    .name = "MS_RELATIME",    },
653
                { .flag = MS_KERNMOUNT,   .name = "MS_KERNMOUNT",   },
654
                { .flag = MS_I_VERSION,   .name = "MS_I_VERSION",   },
655
                { .flag = MS_STRICTATIME, .name = "MS_STRICTATIME", },
656
                { .flag = MS_LAZYTIME,    .name = "MS_LAZYTIME",    },
657
        };
658
        _cleanup_free_ char *str = NULL;
46,891✔
659

660
        assert(ret);
46,891✔
661

662
        FOREACH_ELEMENT(entry, map)
1,219,166✔
663
                if (flags & entry->flag) {
1,172,275✔
664
                        if (!strextend_with_separator(&str, "|", entry->name))
112,725✔
665
                                return -ENOMEM;
666
                        flags &= ~entry->flag;
112,725✔
667
                }
668

669
        if (!str || flags != 0)
46,891✔
670
                if (strextendf_with_separator(&str, "|", "%lx", flags) < 0)
162✔
671
                        return -ENOMEM;
672

673
        *ret = TAKE_PTR(str);
46,891✔
674
        return 0;
46,891✔
675
}
676

677
int mount_verbose_full(
46,861✔
678
                int error_log_level,
679
                const char *what,
680
                const char *where,
681
                const char *type,
682
                unsigned long flags,
683
                const char *options,
684
                bool follow_symlink) {
685

686
        _cleanup_free_ char *fl = NULL, *o = NULL;
46,861✔
687
        unsigned long f;
46,861✔
688
        int r;
46,861✔
689

690
        r = mount_option_mangle(options, flags, &f, &o);
46,861✔
691
        if (r < 0)
46,861✔
692
                return log_full_errno(error_log_level, r,
×
693
                                      "Failed to mangle mount options %s: %m",
694
                                      strempty(options));
695

696
        (void) mount_flags_to_string(f, &fl);
46,861✔
697

698
        if (FLAGS_SET(f, MS_REMOUNT|MS_BIND))
46,861✔
699
                log_debug("Changing mount flags %s (%s \"%s\")...",
10,639✔
700
                          where, strnull(fl), strempty(o));
701
        else if (f & MS_REMOUNT)
41,541✔
702
                log_debug("Remounting superblock %s (%s \"%s\")...",
4✔
703
                          where, strnull(fl), strempty(o));
704
        else if (f & (MS_SHARED|MS_PRIVATE|MS_SLAVE|MS_UNBINDABLE))
41,537✔
705
                log_debug("Changing mount propagation %s (%s \"%s\")",
6,450✔
706
                          where, strnull(fl), strempty(o));
707
        else if (f & MS_BIND)
38,312✔
708
                log_debug("Bind-mounting %s on %s (%s \"%s\")...",
53,209✔
709
                          what, where, strnull(fl), strempty(o));
710
        else if (f & MS_MOVE)
11,634✔
711
                log_debug("Moving mount %s %s %s (%s \"%s\")...",
7,300✔
712
                          what, glyph(GLYPH_ARROW_RIGHT), where, strnull(fl), strempty(o));
713
        else
714
                log_debug("Mounting %s (%s) on %s (%s \"%s\")...",
9,309✔
715
                          strna(what), strna(type), where, strnull(fl), strempty(o));
716

717
        if (follow_symlink)
46,861✔
718
                r = RET_NERRNO(mount(what, where, type, f, o));
47,251✔
719
        else
720
                r = mount_nofollow(what, where, type, f, o);
41,787✔
721
        if (r < 0)
42,177✔
722
                return log_full_errno(error_log_level, r,
8,914✔
723
                                      "Failed to mount %s (type %s) on %s (%s \"%s\"): %m",
724
                                      strna(what), strna(type), where, strnull(fl), strempty(o));
725
        return 0;
726
}
727

728
int umount_verbose(
514✔
729
                int error_log_level,
730
                const char *where,
731
                int flags) {
732

733
        assert(where);
514✔
734

735
        log_debug("Unmounting '%s'...", where);
514✔
736

737
        if (umount2(where, flags) < 0)
514✔
738
                return log_full_errno(error_log_level, errno, "Failed to unmount '%s': %m", where);
89✔
739

740
        return 0;
741
}
742

743
int umountat_detach_verbose(
228✔
744
                int error_log_level,
745
                int fd,
746
                const char *where) {
747

748
        /* Similar to umountat_verbose(), but goes by fd + path. This implies MNT_DETACH, since to do this we
749
         * must pin the inode in question via an fd. */
750

751
        assert(fd >= 0 || fd == AT_FDCWD);
228✔
752

753
        /* If neither fd nor path are specified take this as reference to the cwd */
754
        if (fd == AT_FDCWD && isempty(where))
228✔
755
                return umount_verbose(error_log_level, ".", MNT_DETACH|UMOUNT_NOFOLLOW);
228✔
756

757
        /* If we don't actually take the fd into consideration for this operation shortcut things, so that we
758
         * don't have to open the inode */
759
        if (fd == AT_FDCWD || path_is_absolute(where))
228✔
760
                return umount_verbose(error_log_level, where, MNT_DETACH|UMOUNT_NOFOLLOW);
×
761

762
        _cleanup_free_ char *prefix = NULL;
456✔
763
        const char *p;
228✔
764
        if (fd_get_path(fd, &prefix) < 0)
228✔
765
                p = "<fd>"; /* if we can't get the path, return something vaguely useful */
766
        else
767
                p = prefix;
228✔
768
        _cleanup_free_ char *joined = isempty(where) ? strdup(p) : path_join(p, where);
579✔
769

770
        log_debug("Unmounting '%s'...", strna(joined));
228✔
771

772
        _cleanup_close_ int inode_fd = -EBADF;
228✔
773
        int mnt_fd;
228✔
774
        if (isempty(where))
228✔
775
                mnt_fd = fd;
776
        else {
777
                inode_fd = openat(fd, where, O_PATH|O_CLOEXEC|O_NOFOLLOW);
123✔
778
                if (inode_fd < 0)
123✔
779
                        return log_full_errno(error_log_level, errno, "Failed to pin '%s': %m", strna(joined));
×
780

781
                mnt_fd = inode_fd;
782
        }
783

784
        if (umount2(FORMAT_PROC_FD_PATH(mnt_fd), MNT_DETACH) < 0)
228✔
785
                return log_full_errno(error_log_level, errno, "Failed to unmount '%s': %m", strna(joined));
9✔
786

787
        return 0;
219✔
788
}
789

790
int mount_exchange_graceful(int fsmount_fd, const char *dest, bool mount_beneath) {
25✔
791
        int r;
25✔
792

793
        assert(fsmount_fd >= 0);
25✔
794
        assert(dest);
25✔
795

796
        /* First, try to mount beneath an existing mount point, and if that works, umount the old mount,
797
         * which is now at the top. This will ensure we can atomically replace a mount. Note that this works
798
         * also in the case where there are submounts down the tree. Mount propagation is allowed but
799
         * restricted to layouts that don't end up propagation the new mount on top of the mount stack.  If
800
         * this is not supported (minimum kernel v6.5), or if there is no mount on the mountpoint, we get
801
         * -EINVAL and then we fallback to normal mounting. */
802

803
        r = RET_NERRNO(move_mount(fsmount_fd, /* from_path = */ "",
35✔
804
                                  /* to_fd = */ -EBADF, dest,
805
                                  MOVE_MOUNT_F_EMPTY_PATH | (mount_beneath ? MOVE_MOUNT_BENEATH : 0)));
806
        if (mount_beneath) {
25✔
807
                if (r >= 0) /* Mounting beneath worked! Now unmount the upper mount. */
15✔
808
                        return umount_verbose(LOG_DEBUG, dest, UMOUNT_NOFOLLOW|MNT_DETACH);
11✔
809

810
                if (r == -EINVAL) { /* Fallback if mount_beneath is not supported */
4✔
811
                        log_debug_errno(r,
4✔
812
                                        "Cannot mount beneath '%s', falling back to overmount: %m",
813
                                        dest);
814
                        return mount_exchange_graceful(fsmount_fd, dest, /* mount_beneath = */ false);
4✔
815
                }
816
        }
817

818
        return r;
819
}
820

821
int mount_option_mangle(
46,933✔
822
                const char *options,
823
                unsigned long mount_flags,
824
                unsigned long *ret_mount_flags,
825
                char **ret_remaining_options) {
826

827
        const struct libmnt_optmap *map;
46,933✔
828
        _cleanup_free_ char *ret = NULL;
46,933✔
829
        int r;
46,933✔
830

831
        /* This extracts mount flags from the mount options, and stores
832
         * non-mount-flag options to '*ret_remaining_options'.
833
         * E.g.,
834
         * "rw,nosuid,nodev,relatime,size=1630748k,mode=0700,uid=1000,gid=1000"
835
         * is split to MS_NOSUID|MS_NODEV|MS_RELATIME and
836
         * "size=1630748k,mode=0700,uid=1000,gid=1000".
837
         * See more examples in test-mount-util.c.
838
         *
839
         * If 'options' does not contain any non-mount-flag options,
840
         * then '*ret_remaining_options' is set to NULL instead of empty string.
841
         * The validity of options stored in '*ret_remaining_options' is not checked.
842
         * If 'options' is NULL, this just copies 'mount_flags' to *ret_mount_flags. */
843

844
        assert(ret_mount_flags);
46,933✔
845
        assert(ret_remaining_options);
46,933✔
846

847
        map = mnt_get_builtin_optmap(MNT_LINUX_MAP);
46,933✔
848
        if (!map)
46,933✔
849
                return -EINVAL;
850

851
        for (const char *p = options;;) {
46,933✔
852
                _cleanup_free_ char *word = NULL;
23,071✔
853
                const struct libmnt_optmap *ent;
70,003✔
854

855
                r = extract_first_word(&p, &word, ",", EXTRACT_KEEP_QUOTE);
70,003✔
856
                if (r < 0)
70,003✔
857
                        return r;
858
                if (r == 0)
70,002✔
859
                        break;
860

861
                for (ent = map; ent->name; ent++) {
966,357✔
862
                        /* All entries in MNT_LINUX_MAP do not take any argument.
863
                         * Thus, ent->name does not contain "=" or "[=]". */
864
                        if (!streq(word, ent->name))
943,378✔
865
                                continue;
943,287✔
866

867
                        if (!(ent->mask & MNT_INVERT))
91✔
868
                                mount_flags |= ent->id;
82✔
869
                        else
870
                                mount_flags &= ~ent->id;
9✔
871

872
                        break;
873
                }
874

875
                /* If 'word' is not a mount flag, then store it in '*ret_remaining_options'. */
876
                if (!ent->name &&
46,049✔
877
                    !startswith_no_case(word, "x-") &&
45,956✔
878
                    !strextend_with_separator(&ret, ",", word))
22,977✔
879
                        return -ENOMEM;
880
        }
881

882
        *ret_mount_flags = mount_flags;
46,932✔
883
        *ret_remaining_options = TAKE_PTR(ret);
46,932✔
884

885
        return 0;
46,932✔
886
}
887

888
static int mount_in_namespace_legacy(
×
889
                const char *chased_src_path,
890
                int chased_src_fd,
891
                struct stat *chased_src_st,
892
                const char *propagate_path,
893
                const char *incoming_path,
894
                const char *dest,
895
                int pidns_fd,
896
                int mntns_fd,
897
                int root_fd,
898
                MountInNamespaceFlags flags,
899
                const MountOptions *options,
900
                const ImagePolicy *image_policy) {
901

902
        _cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
×
903
        char mount_slave[] = "/tmp/propagate.XXXXXX", *mount_tmp, *mount_outside, *p;
×
904
        bool mount_slave_created = false, mount_slave_mounted = false,
×
905
                mount_tmp_created = false, mount_tmp_mounted = false,
×
906
                mount_outside_created = false, mount_outside_mounted = false;
×
907
        pid_t child;
×
908
        int r;
×
909

910
        assert(chased_src_path);
×
911
        assert(chased_src_fd >= 0);
×
912
        assert(chased_src_st);
×
913
        assert(propagate_path);
×
914
        assert(incoming_path);
×
915
        assert(dest);
×
916
        assert(pidns_fd >= 0);
×
917
        assert(mntns_fd >= 0);
×
918
        assert(root_fd >= 0);
×
919
        assert(!options || (flags & MOUNT_IN_NAMESPACE_IS_IMAGE));
×
920

921
        p = strjoina(propagate_path, "/");
×
922
        r = access_nofollow(p, F_OK);
×
923
        if (r < 0)
×
924
                return log_debug_errno(r == -ENOENT ? SYNTHETIC_ERRNO(EOPNOTSUPP) : r, "Target does not allow propagation of mount points");
×
925

926
        /* Our goal is to install a new bind mount into the container,
927
           possibly read-only. This is irritatingly complex
928
           unfortunately, currently.
929

930
           First, we start by creating a private playground in /tmp,
931
           that we can mount MS_SLAVE. (Which is necessary, since
932
           MS_MOVE cannot be applied to mounts with MS_SHARED parent
933
           mounts.) */
934

935
        if (!mkdtemp(mount_slave))
×
936
                return log_debug_errno(errno, "Failed to create playground %s: %m", mount_slave);
×
937

938
        mount_slave_created = true;
×
939

940
        r = mount_nofollow_verbose(LOG_DEBUG, mount_slave, mount_slave, NULL, MS_BIND, NULL);
×
941
        if (r < 0)
×
942
                goto finish;
×
943

944
        mount_slave_mounted = true;
×
945

946
        r = mount_nofollow_verbose(LOG_DEBUG, NULL, mount_slave, NULL, MS_SLAVE, NULL);
×
947
        if (r < 0)
×
948
                goto finish;
×
949

950
        /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
951
        mount_tmp = strjoina(mount_slave, "/mount");
×
952
        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);
×
953
        if (r < 0) {
×
954
                log_debug_errno(r, "Failed to create temporary mount point %s: %m", mount_tmp);
×
955
                goto finish;
×
956
        }
957

958
        mount_tmp_created = true;
×
959

960
        if (flags & MOUNT_IN_NAMESPACE_IS_IMAGE)
×
961
                r = verity_dissect_and_mount(
×
962
                                chased_src_fd,
963
                                chased_src_path,
964
                                mount_tmp,
965
                                options,
966
                                image_policy,
967
                                /* image_filter= */ NULL,
968
                                /* extension_release_data= */ NULL,
969
                                /* required_class= */ _IMAGE_CLASS_INVALID,
970
                                /* verity= */ NULL,
971
                                /* ret_image= */ NULL);
972
        else
973
                r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(chased_src_fd), mount_tmp, NULL, MS_BIND, NULL);
×
974
        if (r < 0)
×
975
                goto finish;
×
976

977
        mount_tmp_mounted = true;
×
978

979
        /* Third, we remount the new bind mount read-only if requested. */
980
        if (flags & MOUNT_IN_NAMESPACE_READ_ONLY) {
×
981
                r = mount_nofollow_verbose(LOG_DEBUG, NULL, mount_tmp, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL);
×
982
                if (r < 0)
×
983
                        goto finish;
×
984
        }
985

986
        /* Fourth, we move the new bind mount into the propagation directory. This way it will appear there read-only
987
         * right-away. */
988

989
        mount_outside = strjoina(propagate_path, "/XXXXXX");
×
990
        if ((flags & MOUNT_IN_NAMESPACE_IS_IMAGE) || S_ISDIR(chased_src_st->st_mode))
×
991
                r = mkdtemp(mount_outside) ? 0 : -errno;
×
992
        else {
993
                r = mkostemp_safe(mount_outside);
×
994
                safe_close(r);
×
995
        }
996
        if (r < 0) {
×
997
                log_debug_errno(r, "Cannot create propagation file or directory %s: %m", mount_outside);
×
998
                goto finish;
×
999
        }
1000

1001
        mount_outside_created = true;
×
1002

1003
        r = mount_nofollow_verbose(LOG_DEBUG, mount_tmp, mount_outside, NULL, MS_MOVE, NULL);
×
1004
        if (r < 0)
×
1005
                goto finish;
×
1006

1007
        mount_outside_mounted = true;
×
1008
        mount_tmp_mounted = false;
×
1009

1010
        if ((flags & MOUNT_IN_NAMESPACE_IS_IMAGE) || S_ISDIR(chased_src_st->st_mode))
×
1011
                (void) rmdir(mount_tmp);
×
1012
        else
1013
                (void) unlink(mount_tmp);
×
1014
        mount_tmp_created = false;
×
1015

1016
        (void) umount_verbose(LOG_DEBUG, mount_slave, UMOUNT_NOFOLLOW);
×
1017
        mount_slave_mounted = false;
×
1018

1019
        (void) rmdir(mount_slave);
×
1020
        mount_slave_created = false;
×
1021

1022
        if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) {
×
1023
                log_debug_errno(errno, "Failed to create pipe: %m");
×
1024
                goto finish;
×
1025
        }
1026

1027
        r = namespace_fork(
×
1028
                        "(sd-bindmnt)",
1029
                        "(sd-bindmnt-inner)",
1030
                        /* except_fds= */ NULL,
1031
                        /* n_except_fds= */ 0,
1032
                        FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM,
1033
                        pidns_fd,
1034
                        mntns_fd,
1035
                        /* netns_fd= */ -EBADF,
1036
                        /* userns_fd= */ -EBADF,
1037
                        root_fd,
1038
                        &child);
1039
        if (r < 0)
×
1040
                goto finish;
×
1041
        if (r == 0) {
×
1042
                _cleanup_free_ char *mount_outside_fn = NULL, *mount_inside = NULL;
×
1043

1044
                errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
×
1045

1046
                _cleanup_close_ int dest_fd = -EBADF;
×
1047
                _cleanup_free_ char *dest_fn = NULL;
×
1048
                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);
×
1049
                if (r < 0)
×
1050
                        log_debug_errno(r, "Failed to pin parent directory of mount '%s', ignoring: %m", dest);
×
1051
                else if (flags & MOUNT_IN_NAMESPACE_MAKE_FILE_OR_DIRECTORY) {
×
1052
                        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);
×
1053
                        if (r < 0)
×
1054
                                log_debug_errno(r, "Failed to make mount point inode of mount '%s', ignoring: %m", dest);
×
1055
                }
1056

1057
                /* Fifth, move the mount to the right place inside */
1058
                r = path_extract_filename(mount_outside, &mount_outside_fn);
×
1059
                if (r < 0) {
×
1060
                        log_debug_errno(r, "Failed to extract filename from propagation file or directory '%s': %m", mount_outside);
×
1061
                        report_errno_and_exit(errno_pipe_fd[1], r);
×
1062
                }
1063

1064
                mount_inside = path_join(incoming_path, mount_outside_fn);
×
1065
                if (!mount_inside)
×
1066
                        report_errno_and_exit(errno_pipe_fd[1], log_oom_debug());
×
1067

1068
                r = mount_nofollow_verbose(LOG_DEBUG, mount_inside, dest_fd >= 0 ? FORMAT_PROC_FD_PATH(dest_fd) : dest, /* fstype= */ NULL, MS_MOVE, /* options= */ NULL);
×
1069
                if (r < 0)
×
1070
                        report_errno_and_exit(errno_pipe_fd[1], r);
×
1071

1072
                _exit(EXIT_SUCCESS);
×
1073
        }
1074

1075
        errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
×
1076

1077
        r = wait_for_terminate_and_check("(sd-bindmnt)", child, 0);
×
1078
        if (r < 0) {
×
1079
                log_debug_errno(r, "Failed to wait for child: %m");
×
1080
                goto finish;
×
1081
        }
1082
        if (r != EXIT_SUCCESS) {
×
1083
                if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r))
×
1084
                        log_debug_errno(r, "Failed to mount: %m");
×
1085
                else
1086
                        log_debug("Child failed.");
×
1087
                goto finish;
×
1088
        }
1089

1090
finish:
×
1091
        if (mount_outside_mounted)
×
1092
                (void) umount_verbose(LOG_DEBUG, mount_outside, UMOUNT_NOFOLLOW);
×
1093
        if (mount_outside_created) {
×
1094
                if ((flags & MOUNT_IN_NAMESPACE_IS_IMAGE) || S_ISDIR(chased_src_st->st_mode))
×
1095
                        (void) rmdir(mount_outside);
×
1096
                else
1097
                        (void) unlink(mount_outside);
×
1098
        }
1099

1100
        if (mount_tmp_mounted)
×
1101
                (void) umount_verbose(LOG_DEBUG, mount_tmp, UMOUNT_NOFOLLOW);
×
1102
        if (mount_tmp_created) {
×
1103
                if ((flags & MOUNT_IN_NAMESPACE_IS_IMAGE) || S_ISDIR(chased_src_st->st_mode))
×
1104
                        (void) rmdir(mount_tmp);
×
1105
                else
1106
                        (void) unlink(mount_tmp);
×
1107
        }
1108

1109
        if (mount_slave_mounted)
×
1110
                (void) umount_verbose(LOG_DEBUG, mount_slave, UMOUNT_NOFOLLOW);
×
1111
        if (mount_slave_created)
×
1112
                (void) rmdir(mount_slave);
×
1113

1114
        return r;
×
1115
}
1116

1117
static int mount_in_namespace(
6✔
1118
                const PidRef *target,
1119
                const char *propagate_path,
1120
                const char *incoming_path,
1121
                const char *src,
1122
                const char *dest,
1123
                MountInNamespaceFlags flags,
1124
                const MountOptions *options,
1125
                const ImagePolicy *image_policy) {
1126

1127
        _cleanup_close_ int mntns_fd = -EBADF, root_fd = -EBADF, pidns_fd = -EBADF, chased_src_fd = -EBADF;
18✔
1128
        _cleanup_free_ char *chased_src_path = NULL;
6✔
1129
        struct stat st;
6✔
1130
        int r;
6✔
1131

1132
        assert(propagate_path);
6✔
1133
        assert(incoming_path);
6✔
1134
        assert(src);
6✔
1135
        assert(dest);
6✔
1136
        assert((flags & MOUNT_IN_NAMESPACE_IS_IMAGE) || (!options && !image_policy));
6✔
1137

1138
        if (!pidref_is_set(target))
12✔
1139
                return -ESRCH;
1140

1141
        r = pidref_namespace_open(target, &pidns_fd, &mntns_fd, /* ret_netns_fd = */ NULL, /* ret_userns_fd = */ NULL, &root_fd);
6✔
1142
        if (r < 0)
6✔
1143
                return log_debug_errno(r, "Failed to retrieve FDs of the target process' namespace: %m");
×
1144

1145
        r = is_our_namespace(mntns_fd, NAMESPACE_MOUNT);
6✔
1146
        if (r < 0)
6✔
1147
                return log_debug_errno(r, "Failed to determine if mount namespaces are equal: %m");
×
1148
        /* We can't add new mounts at runtime if the process wasn't started in a namespace */
1149
        if (r > 0)
6✔
1150
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to activate bind mount in target, not running in a mount namespace.");
×
1151

1152
        r = chase(src, NULL, 0, &chased_src_path, &chased_src_fd);
6✔
1153
        if (r < 0)
6✔
1154
                return log_debug_errno(r, "Failed to resolve source path '%s': %m", src);
×
1155
        log_debug("Chased source path '%s': %s", src, chased_src_path);
6✔
1156

1157
        if (fstat(chased_src_fd, &st) < 0)
6✔
1158
                return log_debug_errno(errno, "Failed to stat() resolved source path '%s': %m", src);
×
1159
        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… */
6✔
1160
                return log_debug_errno(SYNTHETIC_ERRNO(ELOOP), "Source path '%s' can't be a symbolic link.", src);
×
1161

1162
        if (!mount_new_api_supported()) /* Fallback if we can't use the new mount API */
6✔
1163
                return mount_in_namespace_legacy(
×
1164
                                chased_src_path,
1165
                                chased_src_fd,
1166
                                &st,
1167
                                propagate_path,
1168
                                incoming_path,
1169
                                dest,
1170
                                pidns_fd,
1171
                                mntns_fd,
1172
                                root_fd,
1173
                                flags,
1174
                                options,
1175
                                image_policy);
1176

1177
        _cleanup_(dissected_image_unrefp) DissectedImage *img = NULL;
×
1178
        _cleanup_close_ int new_mount_fd = -EBADF;
6✔
1179
        _cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
6✔
1180
        pid_t child;
6✔
1181

1182
        if (flags & MOUNT_IN_NAMESPACE_IS_IMAGE) {
6✔
1183
                r = verity_dissect_and_mount(
2✔
1184
                                chased_src_fd,
1185
                                chased_src_path,
1186
                                /* dest= */ NULL,
1187
                                options,
1188
                                image_policy,
1189
                                /* image_filter= */ NULL,
1190
                                /* extension_release_data= */ NULL,
1191
                                /* required_class= */ _IMAGE_CLASS_INVALID,
1192
                                /* verity= */ NULL,
1193
                                &img);
1194
                if (r < 0)
2✔
1195
                        return log_debug_errno(r,
×
1196
                                               "Failed to dissect and mount image '%s': %m",
1197
                                               chased_src_path);
1198
        } else {
1199
                new_mount_fd = open_tree(
4✔
1200
                                chased_src_fd,
1201
                                "",
1202
                                OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC|AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH);
1203
                if (new_mount_fd < 0)
4✔
1204
                        return log_debug_errno(
×
1205
                                        errno,
1206
                                        "Failed to open mount source '%s': %m",
1207
                                        chased_src_path);
1208

1209
                if ((flags & MOUNT_IN_NAMESPACE_READ_ONLY) && mount_setattr(new_mount_fd, "", AT_EMPTY_PATH,
4✔
1210
                                               &(struct mount_attr) {
×
1211
                                                       .attr_set = MOUNT_ATTR_RDONLY,
1212
                                               }, MOUNT_ATTR_SIZE_VER0) < 0)
1213
                        return log_debug_errno(errno,
×
1214
                                               "Failed to set mount for '%s' to read only: %m",
1215
                                               chased_src_path);
1216
        }
1217

1218
        if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
6✔
1219
                return log_debug_errno(errno, "Failed to create pipe: %m");
×
1220

1221
        r = namespace_fork("(sd-bindmnt)",
6✔
1222
                           "(sd-bindmnt-inner)",
1223
                           /* except_fds= */ NULL,
1224
                           /* n_except_fds= */ 0,
1225
                           FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM,
1226
                           pidns_fd,
1227
                           mntns_fd,
1228
                           /* netns_fd= */ -EBADF,
1229
                           /* userns_fd= */ -EBADF,
1230
                           root_fd,
1231
                           &child);
1232
        if (r < 0)
12✔
1233
                return log_debug_errno(r, "Failed to fork off mount helper into namespace: %m");
×
1234
        if (r == 0) {
12✔
1235
                errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
6✔
1236

1237
                _cleanup_close_ int dest_fd = -EBADF;
×
1238
                _cleanup_free_ char *dest_fn = NULL;
×
1239
                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);
6✔
1240
                if (r < 0)
6✔
1241
                        report_errno_and_exit(errno_pipe_fd[1], r);
×
1242

1243
                if (flags & MOUNT_IN_NAMESPACE_MAKE_FILE_OR_DIRECTORY)
6✔
1244
                        (void) make_mount_point_inode_from_mode(dest_fd, dest_fn, img ? S_IFDIR : st.st_mode, 0700);
6✔
1245

1246
                if (img) {
6✔
1247
                        DissectImageFlags f =
2✔
1248
                                DISSECT_IMAGE_TRY_ATOMIC_MOUNT_EXCHANGE |
1249
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY;
1250

1251
                        if (flags & MOUNT_IN_NAMESPACE_MAKE_FILE_OR_DIRECTORY)
2✔
1252
                                f |= DISSECT_IMAGE_MKDIR;
2✔
1253

1254
                        if (flags & MOUNT_IN_NAMESPACE_READ_ONLY)
2✔
1255
                                f |= DISSECT_IMAGE_READ_ONLY;
×
1256

1257
                        r = dissected_image_mount(
2✔
1258
                                        img,
1259
                                        dest,
1260
                                        /* uid_shift= */ UID_INVALID,
1261
                                        /* uid_range= */ UID_INVALID,
1262
                                        /* userns_fd= */ -EBADF,
1263
                                        f);
1264
                } else
1265
                        r = mount_exchange_graceful(new_mount_fd, dest, /* mount_beneath= */ true);
4✔
1266

1267
                report_errno_and_exit(errno_pipe_fd[1], r);
6✔
1268
        }
1269

1270
        errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
6✔
1271

1272
        r = wait_for_terminate_and_check("(sd-bindmnt)", child, 0);
6✔
1273
        if (r < 0)
6✔
1274
                return log_debug_errno(r, "Failed to wait for child: %m");
×
1275
        if (r != EXIT_SUCCESS) {
6✔
1276
                if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r))
×
1277
                        return log_debug_errno(r, "Failed to mount into namespace: %m");
×
1278

1279
                return log_debug_errno(SYNTHETIC_ERRNO(EPROTO), "Child failed.");
×
1280
        }
1281

1282
        return 0;
1283
}
1284

1285
int bind_mount_in_namespace(
4✔
1286
                const PidRef *target,
1287
                const char *propagate_path,
1288
                const char *incoming_path,
1289
                const char *src,
1290
                const char *dest,
1291
                MountInNamespaceFlags flags) {
1292

1293
        return mount_in_namespace(target,
8✔
1294
                                  propagate_path,
1295
                                  incoming_path,
1296
                                  src,
1297
                                  dest,
1298
                                  flags & ~MOUNT_IN_NAMESPACE_IS_IMAGE,
4✔
1299
                                  /* options = */ NULL,
1300
                                  /* image_policy = */ NULL);
1301
}
1302

1303
int mount_image_in_namespace(
2✔
1304
                const PidRef *target,
1305
                const char *propagate_path,
1306
                const char *incoming_path,
1307
                const char *src,
1308
                const char *dest,
1309
                MountInNamespaceFlags flags,
1310
                const MountOptions *options,
1311
                const ImagePolicy *image_policy) {
1312

1313
        return mount_in_namespace(target,
4✔
1314
                                  propagate_path,
1315
                                  incoming_path,
1316
                                  src,
1317
                                  dest,
1318
                                  flags | MOUNT_IN_NAMESPACE_IS_IMAGE,
2✔
1319
                                  options,
1320
                                  image_policy);
1321
}
1322

1323
int make_mount_point(const char *path) {
25✔
1324
        int r;
25✔
1325

1326
        assert(path);
25✔
1327

1328
        /* If 'path' is already a mount point, does nothing and returns 0. If it is not it makes it one, and returns 1. */
1329

1330
        r = path_is_mount_point(path);
25✔
1331
        if (r < 0)
25✔
1332
                return log_debug_errno(r, "Failed to determine whether '%s' is a mount point: %m", path);
×
1333
        if (r > 0)
25✔
1334
                return 0;
1335

1336
        r = mount_nofollow_verbose(LOG_DEBUG, path, path, NULL, MS_BIND|MS_REC, NULL);
9✔
1337
        if (r < 0)
9✔
1338
                return r;
×
1339

1340
        return 1;
1341
}
1342

1343
int fd_make_mount_point(int fd) {
11✔
1344
        int r;
11✔
1345

1346
        assert(fd >= 0);
11✔
1347

1348
        r = is_mount_point_at(fd, NULL, 0);
11✔
1349
        if (r < 0)
11✔
1350
                return log_debug_errno(r, "Failed to determine whether file descriptor is a mount point: %m");
×
1351
        if (r > 0)
11✔
1352
                return 0;
1353

1354
        r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(fd), FORMAT_PROC_FD_PATH(fd), NULL, MS_BIND|MS_REC, NULL);
1✔
1355
        if (r < 0)
1✔
1356
                return r;
×
1357

1358
        return 1;
1359
}
1360

1361
int make_userns(uid_t uid_shift,
120✔
1362
                uid_t uid_range,
1363
                uid_t source_owner,
1364
                uid_t dest_owner,
1365
                RemountIdmapping idmapping) {
1366

1367
        _cleanup_close_ int userns_fd = -EBADF;
120✔
1368
        _cleanup_free_ char *line = NULL;
120✔
1369
        uid_t source_base = 0;
120✔
1370

1371
        /* Allocates a userns file descriptor with the mapping we need. For this we'll fork off a child
1372
         * process whose only purpose is to give us a new user namespace. It's killed when we got it. */
1373

1374
        if (!userns_shift_range_valid(uid_shift, uid_range))
120✔
1375
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid UID range for user namespace.");
×
1376

1377
        switch (idmapping) {
120✔
1378

1379
        case REMOUNT_IDMAPPING_FOREIGN_WITH_HOST_ROOT:
2✔
1380
                source_base = FOREIGN_UID_BASE;
2✔
1381
                _fallthrough_;
88✔
1382

1383
        case REMOUNT_IDMAPPING_NONE:
88✔
1384
        case REMOUNT_IDMAPPING_HOST_ROOT:
1385

1386
                if (asprintf(&line,
88✔
1387
                             UID_FMT " " UID_FMT " " UID_FMT "\n",
1388
                             source_base, uid_shift, uid_range) < 0)
1389
                        return log_oom_debug();
×
1390

1391
                /* If requested we'll include an entry in the mapping so that the host root user can make
1392
                 * changes to the uidmapped mount like it normally would. Specifically, we'll map the user
1393
                 * with UID_MAPPED_ROOT on the backing fs to UID 0. This is useful, since nspawn code wants
1394
                 * to create various missing inodes in the OS tree before booting into it, and this becomes
1395
                 * very easy and straightforward to do if it can just do it under its own regular UID. Note
1396
                 * that in that case the container's runtime uidmap (i.e. the one the container payload
1397
                 * processes run in) will leave this UID unmapped, i.e. if we accidentally leave files owned
1398
                 * by host root in the already uidmapped tree around they'll show up as owned by 'nobody',
1399
                 * which is safe. (Of course, we shouldn't leave such inodes around, but always chown() them
1400
                 * to the container's own UID range, but it's good to have a safety net, in case we
1401
                 * forget it.) */
1402
                if (idmapping == REMOUNT_IDMAPPING_HOST_ROOT)
88✔
1403
                        if (strextendf(&line,
86✔
1404
                                       UID_FMT " " UID_FMT " " UID_FMT "\n",
1405
                                       UID_MAPPED_ROOT, (uid_t) 0u, (uid_t) 1u) < 0)
1406
                                return log_oom_debug();
×
1407

1408
                break;
1409

1410
        case REMOUNT_IDMAPPING_HOST_OWNER:
×
1411
                /* Remap the owner of the bind mounted directory to the root user within the container. This
1412
                 * way every file written by root within the container to the bind-mounted directory will
1413
                 * be owned by the original user from the host. All other users will remain unmapped. */
1414
                if (asprintf(&line,
×
1415
                             UID_FMT " " UID_FMT " " UID_FMT "\n",
1416
                             source_owner, uid_shift, (uid_t) 1u) < 0)
1417
                        return log_oom_debug();
×
1418
                break;
1419

1420
        case REMOUNT_IDMAPPING_HOST_OWNER_TO_TARGET_OWNER:
32✔
1421
                /* Remap the owner of the bind mounted directory to the owner of the target directory
1422
                 * within the container. This way every file written by target directory owner within the
1423
                 * container to the bind-mounted directory will be owned by the original host user.
1424
                 * All other users will remain unmapped. */
1425
                if (asprintf(&line,
32✔
1426
                             UID_FMT " " UID_FMT " " UID_FMT "\n",
1427
                             source_owner, dest_owner, (uid_t) 1u) < 0)
1428
                        return log_oom_debug();
×
1429
                break;
1430

1431
        default:
×
1432
                assert_not_reached();
×
1433
        }
1434

1435
        /* We always assign the same UID and GID ranges */
1436
        userns_fd = userns_acquire(line, line, /* setgroups_deny= */ true);
120✔
1437
        if (userns_fd < 0)
120✔
1438
                return log_debug_errno(userns_fd, "Failed to acquire new userns: %m");
×
1439

1440
        return TAKE_FD(userns_fd);
1441
}
1442

1443
int open_tree_attr_with_fallback(int dir_fd, const char *path, unsigned int flags, struct mount_attr *attr) {
395✔
1444
        _cleanup_close_ int fd = -EBADF;
395✔
1445

1446
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
395✔
1447
        assert(attr);
395✔
1448

1449
        if (isempty(path)) {
395✔
1450
                path = "";
×
1451
                flags |= AT_EMPTY_PATH;
×
1452
        }
1453

1454
        fd = open_tree_attr(dir_fd, path, flags, attr, sizeof(struct mount_attr));
395✔
1455
        if (fd >= 0)
395✔
1456
                return TAKE_FD(fd);
395✔
1457
        if (!ERRNO_IS_NOT_SUPPORTED(errno))
15✔
1458
                return log_debug_errno(errno, "Failed to open tree and set mount attributes: %m");
2✔
1459

1460
        if (attr->attr_clr & MOUNT_ATTR_IDMAP)
13✔
1461
                return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Cannot clear idmap from mount without open_tree_attr()");
×
1462

1463
        fd = open_tree(dir_fd, path, flags);
13✔
1464
        if (fd < 0)
13✔
1465
                return log_debug_errno(errno, "Failed to open tree: %m");
×
1466

1467
        if (mount_setattr(fd, "", AT_EMPTY_PATH | (flags & AT_RECURSIVE), attr, sizeof(struct mount_attr)) < 0)
13✔
1468
                return log_debug_errno(errno, "Failed to change mount attributes: %m");
×
1469

1470
        return TAKE_FD(fd);
1471
}
1472

1473
int remount_idmap_fd(
131✔
1474
                char **paths,
1475
                int userns_fd,
1476
                uint64_t extra_mount_attr_set) {
1477

1478
        int r;
131✔
1479

1480
        assert(userns_fd >= 0);
131✔
1481

1482
        /* This remounts all specified paths with the specified userns as idmap. It will do so in the
1483
         * order specified in the strv: the expectation is that the top-level directories are at the
1484
         * beginning, and nested directories in the right, so that the tree can be built correctly from left
1485
         * to right. */
1486

1487
        size_t n = strv_length(paths);
131✔
1488
        if (n == 0) /* Nothing to do? */
131✔
1489
                return 0;
131✔
1490

1491
        int *mount_fds = NULL;
131✔
1492
        size_t n_mounts_fds = 0;
131✔
1493

1494
        mount_fds = new(int, n);
131✔
1495
        if (!mount_fds)
131✔
1496
                return log_oom_debug();
×
1497

1498
        CLEANUP_ARRAY(mount_fds, n_mounts_fds, close_many_and_free);
131✔
1499

1500
        for (size_t i = 0; i < n; i++) {
260✔
1501
                /* Clone the mount point and et the user namespace mapping attribute on the cloned mount point. */
1502
                mount_fds[n_mounts_fds] = open_tree_attr_with_fallback(
262✔
1503
                                AT_FDCWD,
1504
                                paths[i],
131✔
1505
                                OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC,
1506
                                &(struct mount_attr) {
131✔
1507
                                          .attr_set = MOUNT_ATTR_IDMAP | extra_mount_attr_set,
131✔
1508
                                          .userns_fd = userns_fd,
1509
                                });
1510
                if (mount_fds[n_mounts_fds] < 0)
131✔
1511
                        return mount_fds[n_mounts_fds];
2✔
1512

1513
                n_mounts_fds++;
129✔
1514
        }
1515

1516
        for (size_t i = n; i > 0; i--) { /* Unmount the paths right-to-left */
258✔
1517
                /* Remove the old mount points now that we have a idmapped mounts as replacement for all of them */
1518
                r = umount_verbose(LOG_DEBUG, paths[i-1], UMOUNT_NOFOLLOW);
129✔
1519
                if (r < 0)
129✔
1520
                        return r;
1521
        }
1522

1523
        for (size_t i = 0; i < n; i++) { /* Mount the replacement mounts left-to-right */
258✔
1524
                /* And place the cloned version in its place */
1525
                log_debug("Mounting idmapped fs to '%s'", paths[i]);
129✔
1526
                if (move_mount(mount_fds[i], "", -EBADF, paths[i], MOVE_MOUNT_F_EMPTY_PATH) < 0)
129✔
1527
                        return log_debug_errno(errno, "Failed to attach UID mapped mount to '%s': %m", paths[i]);
×
1528
        }
1529

1530
        return 0;
1531
}
1532

1533
int remount_idmap(
118✔
1534
                char **p,
1535
                uid_t uid_shift,
1536
                uid_t uid_range,
1537
                uid_t source_owner,
1538
                uid_t dest_owner,
1539
                RemountIdmapping idmapping) {
1540

1541
        _cleanup_close_ int userns_fd = -EBADF;
118✔
1542

1543
        userns_fd = make_userns(uid_shift, uid_range, source_owner, dest_owner, idmapping);
118✔
1544
        if (userns_fd < 0)
118✔
1545
                return userns_fd;
1546

1547
        return remount_idmap_fd(p, userns_fd, /* extra_mount_attr_set= */ 0);
118✔
1548
}
1549

1550
static void sub_mount_clear(SubMount *s) {
5,489✔
1551
        assert(s);
5,489✔
1552

1553
        s->path = mfree(s->path);
5,489✔
1554
        s->mount_fd = safe_close(s->mount_fd);
5,489✔
1555
}
5,489✔
1556

1557
void sub_mount_array_free(SubMount *s, size_t n) {
1,195✔
1558
        assert(s || n == 0);
1,195✔
1559

1560
        for (size_t i = 0; i < n; i++)
5,897✔
1561
                sub_mount_clear(s + i);
4,702✔
1562

1563
        free(s);
1,195✔
1564
}
1,195✔
1565

1566
static int sub_mount_compare(const SubMount *a, const SubMount *b) {
6,960✔
1567
        assert(a);
6,960✔
1568
        assert(b);
6,960✔
1569
        assert(a->path);
6,960✔
1570
        assert(b->path);
6,960✔
1571

1572
        return path_compare(a->path, b->path);
6,960✔
1573
}
1574

1575
static void sub_mount_drop(SubMount *s, size_t n) {
1,773✔
1576
        assert(s || n == 0);
1,773✔
1577

1578
        for (size_t m = 0, i = 1; i < n; i++) {
5,280✔
1579
                if (path_startswith(s[i].path, s[m].path))
3,507✔
1580
                        sub_mount_clear(s + i);
787✔
1581
                else
1582
                        m = i;
1583
        }
1584
}
1,773✔
1585

1586
int get_sub_mounts(const char *prefix, SubMount **ret_mounts, size_t *ret_n_mounts) {
1,773✔
1587

1588
        _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
1,773✔
1589
        _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
1,773✔
1590
        SubMount *mounts = NULL;
1,773✔
1591
        size_t n = 0;
1,773✔
1592
        int r;
1,773✔
1593

1594
        CLEANUP_ARRAY(mounts, n, sub_mount_array_free);
1,773✔
1595

1596
        assert(prefix);
1,773✔
1597
        assert(ret_mounts);
1,773✔
1598
        assert(ret_n_mounts);
1,773✔
1599

1600
        r = libmount_parse_mountinfo(/* source = */ NULL, &table, &iter);
1,773✔
1601
        if (r < 0)
1,773✔
1602
                return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
×
1603

1604
        for (;;) {
90,120✔
1605
                _cleanup_close_ int mount_fd = -EBADF;
88,347✔
1606
                _cleanup_free_ char *p = NULL;
90,120✔
1607
                struct libmnt_fs *fs;
90,120✔
1608
                const char *path;
90,120✔
1609
                int id1, id2;
90,120✔
1610

1611
                r = mnt_table_next_fs(table, iter, &fs);
90,120✔
1612
                if (r == 1)
90,120✔
1613
                        break; /* EOF */
1614
                if (r < 0)
88,347✔
1615
                        return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
×
1616

1617
                path = mnt_fs_get_target(fs);
88,347✔
1618
                if (!path)
88,347✔
1619
                        continue;
×
1620

1621
                if (isempty(path_startswith(path, prefix)))
88,347✔
1622
                        continue;
82,863✔
1623

1624
                id1 = mnt_fs_get_id(fs);
5,484✔
1625
                r = path_get_mnt_id(path, &id2);
5,484✔
1626
                if (r < 0) {
5,484✔
1627
                        log_debug_errno(r, "Failed to get mount ID of '%s', ignoring: %m", path);
×
1628
                        continue;
×
1629
                }
1630
                if (id1 != id2) {
5,484✔
1631
                        /* The path may be hidden by another over-mount or already remounted. */
1632
                        log_debug("The mount IDs of '%s' obtained by libmount and path_get_mnt_id() are different (%i vs %i), ignoring.",
782✔
1633
                                  path, id1, id2);
1634
                        continue;
782✔
1635
                }
1636

1637
                mount_fd = open(path, O_CLOEXEC|O_PATH);
4,702✔
1638
                if (mount_fd < 0) {
4,702✔
1639
                        if (errno == ENOENT) /* The path may be hidden by another over-mount or already unmounted. */
×
1640
                                continue;
×
1641

1642
                        return log_debug_errno(errno, "Failed to open subtree of mounted filesystem '%s': %m", path);
×
1643
                }
1644

1645
                p = strdup(path);
4,702✔
1646
                if (!p)
4,702✔
1647
                        return log_oom_debug();
×
1648

1649
                if (!GREEDY_REALLOC(mounts, n + 1))
4,702✔
1650
                        return log_oom_debug();
×
1651

1652
                mounts[n++] = (SubMount) {
4,702✔
1653
                        .path = TAKE_PTR(p),
4,702✔
1654
                        .mount_fd = TAKE_FD(mount_fd),
4,702✔
1655
                };
1656
        }
1657

1658
        typesafe_qsort(mounts, n, sub_mount_compare);
1,773✔
1659
        sub_mount_drop(mounts, n);
1,773✔
1660

1661
        *ret_mounts = TAKE_PTR(mounts);
1,773✔
1662
        *ret_n_mounts = n;
1,773✔
1663
        return 0;
1,773✔
1664
}
1665

1666
int bind_mount_submounts(
1,203✔
1667
                const char *source,
1668
                const char *target) {
1669

1670
        SubMount *mounts = NULL;
1,203✔
1671
        size_t n = 0;
1,203✔
1672
        int ret = 0, r;
1,203✔
1673

1674
        /* Bind mounts all child mounts of 'source' to 'target'. Useful when setting up a new procfs instance
1675
         * with new mount options to copy the original submounts over. */
1676

1677
        assert(source);
1,203✔
1678
        assert(target);
1,203✔
1679

1680
        CLEANUP_ARRAY(mounts, n, sub_mount_array_free);
1,203✔
1681

1682
        r = get_sub_mounts(source, &mounts, &n);
1,203✔
1683
        if (r < 0)
1,203✔
1684
                return r;
1685

1686
        FOREACH_ARRAY(m, mounts, n) {
5,901✔
1687
                _cleanup_free_ char *t = NULL;
4,698✔
1688
                const char *suffix;
4,698✔
1689

1690
                if (isempty(m->path))
4,698✔
1691
                        continue;
787✔
1692

1693
                assert_se(suffix = path_startswith(m->path, source));
3,911✔
1694

1695
                t = path_join(target, suffix);
3,911✔
1696
                if (!t)
3,911✔
1697
                        return -ENOMEM;
×
1698

1699
                r = path_is_mount_point(t);
3,911✔
1700
                if (r < 0) {
3,911✔
1701
                        log_debug_errno(r, "Failed to detect if '%s' already is a mount point, ignoring: %m", t);
9✔
1702
                        continue;
9✔
1703
                }
1704
                if (r > 0) {
3,902✔
1705
                        log_debug("Not bind mounting '%s' from '%s' to '%s', since there's already a mountpoint.", suffix, source, target);
×
1706
                        continue;
×
1707
                }
1708

1709
                r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(m->mount_fd), t, NULL, MS_BIND|MS_REC, NULL);
3,902✔
1710
                if (r < 0 && ret == 0)
3,902✔
1711
                        ret = r;
390✔
1712
        }
1713

1714
        return ret;
1715
}
1716

1717
int make_mount_point_inode_from_mode(int dir_fd, const char *dest, mode_t source_mode, mode_t target_mode) {
1,022✔
1718
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
1,022✔
1719
        assert(dest);
1,022✔
1720

1721
        if (S_ISDIR(source_mode))
1,022✔
1722
                return mkdirat_label(dir_fd, dest, target_mode & 07777);
1,005✔
1723
        else
1724
                return RET_NERRNO(mknodat(dir_fd, dest, S_IFREG|(target_mode & 07666), 0)); /* Mask off X bit */
18✔
1725
}
1726

1727
int make_mount_point_inode_from_path(const char *source, const char *dest, mode_t access_mode) {
860✔
1728
        struct stat st;
860✔
1729

1730
        assert(source);
860✔
1731
        assert(dest);
860✔
1732

1733
        if (stat(source, &st) < 0)
860✔
1734
                return -errno;
×
1735

1736
        return make_mount_point_inode_from_mode(AT_FDCWD, dest, st.st_mode, access_mode);
860✔
1737
}
1738

1739
int trigger_automount_at(int dir_fd, const char *path) {
392✔
1740
        _cleanup_free_ char *nested = NULL;
784✔
1741

1742
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
392✔
1743

1744
        nested = path_join(path, "a");
392✔
1745
        if (!nested)
392✔
1746
                return -ENOMEM;
1747

1748
        (void) faccessat(dir_fd, nested, F_OK, 0);
392✔
1749

1750
        return 0;
392✔
1751
}
1752

1753
unsigned long credentials_fs_mount_flags(bool ro) {
3,978✔
1754
        /* A tight set of mount flags for credentials mounts */
1755
        return MS_NODEV|MS_NOEXEC|MS_NOSUID|ms_nosymfollow_supported()|(ro ? MS_RDONLY : 0);
3,978✔
1756
}
1757

1758
int mount_credentials_fs(const char *path, size_t size, bool ro) {
1,989✔
1759
        _cleanup_free_ char *opts = NULL;
1,989✔
1760
        int r, noswap_supported;
1,989✔
1761

1762
        /* Mounts a file system we can place credentials in, i.e. with tight access modes right from the
1763
         * beginning, and ideally swapping turned off. In order of preference:
1764
         *
1765
         *      1. tmpfs if it supports "noswap"
1766
         *      2. ramfs
1767
         *      3. tmpfs if it doesn't support "noswap"
1768
         */
1769

1770
        noswap_supported = mount_option_supported("tmpfs", "noswap", NULL); /* Check explicitly to avoid kmsg noise */
1,989✔
1771
        if (noswap_supported > 0) {
1,989✔
1772
                _cleanup_free_ char *noswap_opts = NULL;
1,988✔
1773

1774
                if (asprintf(&noswap_opts, "mode=0700,nr_inodes=1024,size=%zu,noswap", size) < 0)
1,988✔
1775
                        return -ENOMEM;
1776

1777
                /* Best case: tmpfs with noswap (needs kernel >= 6.3) */
1778

1779
                r = mount_nofollow_verbose(
1,988✔
1780
                                LOG_DEBUG,
1781
                                "tmpfs",
1782
                                path,
1783
                                "tmpfs",
1784
                                credentials_fs_mount_flags(ro),
1785
                                noswap_opts);
1786
                if (r >= 0)
1,988✔
1787
                        return r;
1788
        }
1789

1790
        r = mount_nofollow_verbose(
1✔
1791
                        LOG_DEBUG,
1792
                        "ramfs",
1793
                        path,
1794
                        "ramfs",
1795
                        credentials_fs_mount_flags(ro),
1796
                        "mode=0700");
1797
        if (r >= 0)
1✔
1798
                return r;
1799

1800
        if (asprintf(&opts, "mode=0700,nr_inodes=1024,size=%zu", size) < 0)
1✔
1801
                return -ENOMEM;
1802

1803
        return mount_nofollow_verbose(
1,989✔
1804
                        LOG_DEBUG,
1805
                        "tmpfs",
1806
                        path,
1807
                        "tmpfs",
1808
                        credentials_fs_mount_flags(ro),
1809
                        opts);
1810
}
1811

1812
int make_fsmount(
2✔
1813
                int error_log_level,
1814
                const char *what,
1815
                const char *type,
1816
                unsigned long flags,
1817
                const char *options,
1818
                int userns_fd) {
1819

1820
        _cleanup_close_ int fs_fd = -EBADF, mnt_fd = -EBADF;
2✔
1821
        _cleanup_free_ char *o = NULL;
2✔
1822
        unsigned long f;
2✔
1823
        int r;
2✔
1824

1825
        assert(type);
2✔
1826
        assert(what);
2✔
1827

1828
        r = mount_option_mangle(options, flags, &f, &o);
2✔
1829
        if (r < 0)
2✔
1830
                return log_full_errno(
×
1831
                                error_log_level, r, "Failed to mangle mount options %s: %m",
1832
                                strempty(options));
1833

1834
        if (DEBUG_LOGGING) {
2✔
1835
                _cleanup_free_ char *fl = NULL;
2✔
1836
                (void) mount_flags_to_string(f, &fl);
2✔
1837

1838
                log_debug("Creating mount fd for %s (%s) (%s \"%s\")...",
4✔
1839
                        strna(what), strna(type), strnull(fl), strempty(o));
1840
        }
1841

1842
        fs_fd = fsopen(type, FSOPEN_CLOEXEC);
2✔
1843
        if (fs_fd < 0)
2✔
1844
                return log_full_errno(error_log_level, errno, "Failed to open superblock for \"%s\": %m", type);
×
1845

1846
        if (fsconfig(fs_fd, FSCONFIG_SET_STRING, "source", what, 0) < 0)
2✔
1847
                return log_full_errno(error_log_level, errno, "Failed to set mount source for \"%s\" to \"%s\": %m", type, what);
×
1848

1849
        if (FLAGS_SET(f, MS_RDONLY))
2✔
1850
                if (fsconfig(fs_fd, FSCONFIG_SET_FLAG, "ro", NULL, 0) < 0)
2✔
1851
                        return log_full_errno(error_log_level, errno, "Failed to set read only mount flag for \"%s\": %m", type);
×
1852

1853
        for (const char *p = o;;) {
2✔
1854
                _cleanup_free_ char *word = NULL;
×
1855
                char *eq;
2✔
1856

1857
                r = extract_first_word(&p, &word, ",", EXTRACT_KEEP_QUOTE);
2✔
1858
                if (r < 0)
2✔
1859
                        return log_full_errno(error_log_level, r, "Failed to parse mount option string \"%s\": %m", o);
×
1860
                if (r == 0)
2✔
1861
                        break;
1862

1863
                eq = strchr(word, '=');
×
1864
                if (eq) {
×
1865
                        *eq = 0;
×
1866
                        eq++;
×
1867

1868
                        if (fsconfig(fs_fd, FSCONFIG_SET_STRING, word, eq, 0) < 0)
×
1869
                                return log_full_errno(error_log_level, errno, "Failed to set mount option \"%s=%s\" for \"%s\": %m", word, eq, type);
×
1870
                } else {
1871
                        if (fsconfig(fs_fd, FSCONFIG_SET_FLAG, word, NULL, 0) < 0)
×
1872
                                return log_full_errno(error_log_level, errno, "Failed to set mount flag \"%s\" for \"%s\": %m", word, type);
×
1873
                }
1874
        }
1875

1876
        if (fsconfig(fs_fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) < 0)
2✔
1877
                return log_full_errno(error_log_level, errno, "Failed to realize fs fd for \"%s\" (\"%s\"): %m", what, type);
×
1878

1879
        mnt_fd = fsmount(fs_fd, FSMOUNT_CLOEXEC, 0);
2✔
1880
        if (mnt_fd < 0)
2✔
1881
                return log_full_errno(error_log_level, errno, "Failed to create mount fd for \"%s\" (\"%s\"): %m", what, type);
×
1882

1883
        struct mount_attr ma = {
4✔
1884
                .attr_set = ms_flags_to_mount_attr(f) | (userns_fd >= 0 ? MOUNT_ATTR_IDMAP : 0),
2✔
1885
                .userns_fd = userns_fd,
1886
        };
1887
        if (ma.attr_set != 0 && mount_setattr(mnt_fd, "", AT_EMPTY_PATH|AT_RECURSIVE, &ma, MOUNT_ATTR_SIZE_VER0) < 0)
2✔
1888
                return log_full_errno(error_log_level,
×
1889
                                      errno,
1890
                                      "Failed to set mount flags for \"%s\" (\"%s\"): %m",
1891
                                      what,
1892
                                      type);
1893

1894
        return TAKE_FD(mnt_fd);
1895
}
1896

1897
char* umount_and_rmdir_and_free(char *p) {
149✔
1898
        if (!p)
149✔
1899
                return NULL;
149✔
1900

1901
        PROTECT_ERRNO;
×
1902
        (void) umount_recursive(p, 0);
149✔
1903
        (void) rmdir(p);
149✔
1904
        return mfree(p);
149✔
1905
}
1906

1907
char* umount_and_free(char *p) {
39✔
1908
        if (!p)
39✔
1909
                return NULL;
39✔
1910

1911
        PROTECT_ERRNO;
×
1912
        (void) umount_recursive(p, 0);
39✔
1913
        return mfree(p);
39✔
1914
}
1915

1916
char* umount_and_unlink_and_free(char *p) {
1✔
1917
        if (!p)
1✔
1918
                return NULL;
1✔
1919

1920
        PROTECT_ERRNO;
2✔
1921
        (void) umount2(p, 0);
1✔
1922
        (void) unlink(p);
1✔
1923
        return mfree(p);
1✔
1924
}
1925

1926
int path_get_mount_info_at(
646✔
1927
                int dir_fd,
1928
                const char *path,
1929
                char **ret_fstype,
1930
                char **ret_options,
1931
                char **ret_source) {
1932

1933
        _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
646✔
1934
        _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
646✔
1935
        int r, mnt_id;
646✔
1936

1937
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
646✔
1938

1939
        r = path_get_mnt_id_at(dir_fd, path, &mnt_id);
646✔
1940
        if (r < 0)
646✔
1941
                return log_debug_errno(r, "Failed to get mount ID: %m");
×
1942

1943
        /* When getting options is requested, we also need to parse utab, otherwise userspace options like
1944
         * "_netdev" will be lost. */
1945
        if (ret_options)
646✔
1946
                r = libmount_parse_with_utab(&table, &iter);
636✔
1947
        else
1948
                r = libmount_parse_mountinfo(/* source = */ NULL, &table, &iter);
10✔
1949
        if (r < 0)
646✔
1950
                return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
×
1951

1952
        for (;;) {
3,454✔
1953
                struct libmnt_fs *fs;
2,050✔
1954

1955
                r = mnt_table_next_fs(table, iter, &fs);
2,050✔
1956
                if (r == 1)
2,050✔
1957
                        break; /* EOF */
1958
                if (r < 0)
2,050✔
1959
                        return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
646✔
1960

1961
                if (mnt_fs_get_id(fs) != mnt_id)
2,050✔
1962
                        continue;
1,404✔
1963

1964
                _cleanup_free_ char *fstype = NULL, *options = NULL, *source = NULL;
646✔
1965

1966
                if (ret_fstype) {
646✔
1967
                        fstype = strdup(strempty(mnt_fs_get_fstype(fs)));
636✔
1968
                        if (!fstype)
636✔
1969
                                return log_oom_debug();
×
1970
                }
1971

1972
                if (ret_options) {
646✔
1973
                        options = strdup(strempty(mnt_fs_get_options(fs)));
636✔
1974
                        if (!options)
636✔
1975
                                return log_oom_debug();
×
1976
                }
1977

1978
                if (ret_source) {
646✔
1979
                        source = strdup(strempty(mnt_fs_get_source(fs)));
10✔
1980
                        if (!source)
10✔
1981
                                return log_oom_debug();
×
1982
                }
1983

1984
                if (ret_fstype)
646✔
1985
                        *ret_fstype = TAKE_PTR(fstype);
636✔
1986
                if (ret_options)
646✔
1987
                        *ret_options = TAKE_PTR(options);
636✔
1988
                if (ret_source)
646✔
1989
                        *ret_source = TAKE_PTR(source);
10✔
1990

1991
                return 0;
1992
        }
1993

1994
        return log_debug_errno(SYNTHETIC_ERRNO(ESTALE), "Cannot find mount ID %i from /proc/self/mountinfo.", mnt_id);
×
1995
}
1996

1997
int path_is_network_fs_harder_at(int dir_fd, const char *path) {
654✔
1998
        _cleanup_close_ int fd = -EBADF;
654✔
1999
        int r;
654✔
2000

2001
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
654✔
2002

2003
        fd = xopenat(dir_fd, path, O_PATH | O_CLOEXEC | O_NOFOLLOW);
654✔
2004
        if (fd < 0)
654✔
2005
                return fd;
2006

2007
        r = fd_is_network_fs(fd);
636✔
2008
        if (r != 0)
636✔
2009
                return r;
2010

2011
        _cleanup_free_ char *fstype = NULL, *options = NULL;
636✔
2012
        r = path_get_mount_info_at(fd, /* path = */ NULL, &fstype, &options, /* ret_source = */ NULL);
636✔
2013
        if (r < 0)
636✔
2014
                return r;
2015

2016
        if (fstype_is_network(fstype))
636✔
2017
                return true;
2018

2019
        if (fstab_test_option(options, "_netdev\0"))
636✔
2020
                return true;
×
2021

2022
        return false;
2023
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc