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

systemd / systemd / 13424846548

19 Feb 2025 10:09PM UTC coverage: 71.768% (+0.02%) from 71.753%
13424846548

push

github

web-flow
tree-wide: tweaks to mount point inode creation (#36308)

Some love for make_mount_point_inode_from_xyz() and ports PID 1 over to
it for mount units.

Alternative to #36290

58 of 91 new or added lines in 7 files covered. (63.74%)

1322 existing lines in 48 files now uncovered.

293681 of 409206 relevant lines covered (71.77%)

717000.31 hits per line

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

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

3
#include <errno.h>
4
#include <stdlib.h>
5
#include <sys/mount.h>
6
#include <sys/stat.h>
7
#include <sys/statvfs.h>
8
#include <unistd.h>
9
#include <linux/loop.h>
10
#if WANT_LINUX_FS_H
11
#include <linux/fs.h>
12
#endif
13

14
#include "alloc-util.h"
15
#include "chase.h"
16
#include "dissect-image.h"
17
#include "exec-util.h"
18
#include "extract-word.h"
19
#include "fd-util.h"
20
#include "fileio.h"
21
#include "fs-util.h"
22
#include "fstab-util.h"
23
#include "glyph-util.h"
24
#include "hashmap.h"
25
#include "initrd-util.h"
26
#include "label-util.h"
27
#include "libmount-util.h"
28
#include "missing_mount.h"
29
#include "missing_syscall.h"
30
#include "mkdir-label.h"
31
#include "mount-util.h"
32
#include "mountpoint-util.h"
33
#include "namespace-util.h"
34
#include "parse-util.h"
35
#include "path-util.h"
36
#include "process-util.h"
37
#include "set.h"
38
#include "sort-util.h"
39
#include "stat-util.h"
40
#include "stdio-util.h"
41
#include "string-table.h"
42
#include "string-util.h"
43
#include "strv.h"
44
#include "tmpfile-util.h"
45
#include "user-util.h"
46

47
int umount_recursive_full(const char *prefix, int flags, char **keep) {
12,746✔
48
        _cleanup_fclose_ FILE *f = NULL;
12,746✔
49
        int n = 0, r;
12,746✔
50

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

54
        f = fopen("/proc/self/mountinfo", "re"); /* Pin the file, in case we unmount /proc/ as part of the logic here */
12,746✔
55
        if (!f)
12,746✔
56
                return log_debug_errno(errno, "Failed to open /proc/self/mountinfo: %m");
×
57

58
        for (;;) {
69,480✔
59
                _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
28,367✔
60
                _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
41,113✔
61
                bool again = false;
41,113✔
62

63
                r = libmount_parse_mountinfo(f, &table, &iter);
41,113✔
64
                if (r < 0)
41,113✔
65
                        return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
×
66

67
                for (;;) {
4,393,273✔
68
                        bool shall_keep = false;
2,217,193✔
69
                        struct libmnt_fs *fs;
2,217,193✔
70
                        const char *path;
2,217,193✔
71

72
                        r = mnt_table_next_fs(table, iter, &fs);
2,217,193✔
73
                        if (r == 1)
2,217,193✔
74
                                break;
75
                        if (r < 0)
2,204,447✔
76
                                return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
×
77

78
                        path = mnt_fs_get_target(fs);
2,204,447✔
79
                        if (!path)
2,204,447✔
80
                                continue;
2,176,080✔
81

82
                        if (prefix && !path_startswith(path, prefix)) {
4,406,898✔
83
                                // FIXME: This is extremely noisy, we're probably doing something very wrong
84
                                // to trigger this so often, needs more investigation.
85
                                // log_trace("Not unmounting %s, outside of prefix: %s", path, prefix);
86
                                continue;
2,158,220✔
87
                        }
88

89
                        STRV_FOREACH(k, keep)
46,609✔
90
                                /* Match against anything in the path to the dirs to keep, or below the dirs to keep */
91
                                if (path_startswith(path, *k) || path_startswith(*k, path)) {
2,011✔
92
                                        shall_keep = true;
1,629✔
93
                                        break;
1,629✔
94
                                }
95
                        if (shall_keep) {
47,856✔
96
                                log_debug("Not unmounting %s, referenced by keep list.", path);
1,629✔
97
                                continue;
1,629✔
98
                        }
99

100
                        if (umount2(path, flags | UMOUNT_NOFOLLOW) < 0) {
44,598✔
101
                                log_debug_errno(errno, "Failed to umount %s, ignoring: %m", path);
16,231✔
102
                                continue;
16,231✔
103
                        }
104

105
                        log_trace("Successfully unmounted %s", path);
28,367✔
106

107
                        again = true;
28,367✔
108
                        n++;
28,367✔
109

110
                        break;
28,367✔
111
                }
112

113
                if (!again)
12,746✔
114
                        break;
115

116
                rewind(f);
28,367✔
117
        }
118

119
        return n;
12,746✔
120
}
121

122
#define MS_CONVERTIBLE_FLAGS (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_NOSYMFOLLOW)
123

124
static uint64_t ms_flags_to_mount_attr(unsigned long a) {
61,746✔
125
        uint64_t f = 0;
61,746✔
126

127
        if (FLAGS_SET(a, MS_RDONLY))
61,746✔
128
                f |= MOUNT_ATTR_RDONLY;
2,095✔
129

130
        if (FLAGS_SET(a, MS_NOSUID))
61,746✔
131
                f |= MOUNT_ATTR_NOSUID;
28,780✔
132

133
        if (FLAGS_SET(a, MS_NODEV))
61,746✔
134
                f |= MOUNT_ATTR_NODEV;
2✔
135

136
        if (FLAGS_SET(a, MS_NOEXEC))
61,746✔
137
                f |= MOUNT_ATTR_NOEXEC;
2✔
138

139
        if (FLAGS_SET(a, MS_NOSYMFOLLOW))
61,746✔
140
                f |= MOUNT_ATTR_NOSYMFOLLOW;
×
141

142
        return f;
61,746✔
143
}
144

145
static bool skip_mount_set_attr = false;
146

147
/* Use this function only if you do not have direct access to /proc/self/mountinfo but the caller can open it
148
 * for you. This is the case when /proc is masked or not mounted. Otherwise, use bind_remount_recursive. */
149
int bind_remount_recursive_with_mountinfo(
58,269✔
150
                const char *prefix,
151
                unsigned long new_flags,
152
                unsigned long flags_mask,
153
                char **deny_list,
154
                FILE *proc_self_mountinfo) {
155

156
        _cleanup_fclose_ FILE *proc_self_mountinfo_opened = NULL;
58,269✔
157
        _cleanup_set_free_ Set *done = NULL;
58,269✔
158
        unsigned n_tries = 0;
58,269✔
159
        int r;
58,269✔
160

161
        assert(prefix);
58,269✔
162

163
        if ((flags_mask & ~MS_CONVERTIBLE_FLAGS) == 0 && strv_isempty(deny_list) && !skip_mount_set_attr) {
86,107✔
164
                /* Let's take a shortcut for all the flags we know how to convert into mount_setattr() flags */
165

166
                if (mount_setattr(AT_FDCWD, prefix, AT_SYMLINK_NOFOLLOW|AT_RECURSIVE,
27,838✔
167
                                  &(struct mount_attr) {
27,838✔
168
                                          .attr_set = ms_flags_to_mount_attr(new_flags & flags_mask),
27,838✔
169
                                          .attr_clr = ms_flags_to_mount_attr(~new_flags & flags_mask),
27,838✔
170
                                  }, MOUNT_ATTR_SIZE_VER0) < 0) {
171

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

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

181
                        if (ERRNO_IS_NOT_SUPPORTED(errno)) /* if not supported, then don't bother at all anymore */
2✔
182
                                skip_mount_set_attr = true;
×
183
                } else
184
                        return 0; /* Nice, this worked! */
27,836✔
185
        }
186

187
        if (!proc_self_mountinfo) {
30,433✔
188
                r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo_opened);
3✔
189
                if (r < 0)
3✔
190
                        return r;
191

192
                proc_self_mountinfo = proc_self_mountinfo_opened;
3✔
193
        }
194

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

209
        for (;;) {
60,869✔
210
                _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
60,869✔
211
                _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
60,869✔
212
                _cleanup_hashmap_free_ Hashmap *todo = NULL;
60,867✔
213
                bool top_autofs = false;
60,869✔
214

215
                if (n_tries++ >= 32) /* Let's not retry this loop forever */
60,869✔
216
                        return -EBUSY;
217

218
                rewind(proc_self_mountinfo);
60,869✔
219

220
                r = libmount_parse_mountinfo(proc_self_mountinfo, &table, &iter);
60,869✔
221
                if (r < 0)
60,869✔
222
                        return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
×
223

224
                for (;;) {
4,479,029✔
225
                        _cleanup_free_ char *d = NULL;
4,418,160✔
226
                        const char *path, *type, *opts;
4,479,029✔
227
                        unsigned long flags = 0;
4,479,029✔
228
                        struct libmnt_fs *fs;
4,479,029✔
229

230
                        r = mnt_table_next_fs(table, iter, &fs);
4,479,029✔
231
                        if (r == 1) /* EOF */
4,479,029✔
232
                                break;
233
                        if (r < 0)
4,418,160✔
234
                                return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
×
235

236
                        path = mnt_fs_get_target(fs);
4,418,160✔
237
                        if (!path)
4,418,160✔
238
                                continue;
×
239

240
                        if (!path_startswith(path, prefix))
4,418,160✔
241
                                continue;
4,305,532✔
242

243
                        type = mnt_fs_get_fstype(fs);
112,628✔
244
                        if (!type)
112,628✔
245
                                continue;
×
246

247
                        /* Let's ignore autofs mounts. If they aren't triggered yet, we want to avoid
248
                         * triggering them, as we don't make any guarantees for future submounts anyway. If
249
                         * they are already triggered, then we will find another entry for this. */
250
                        if (streq(type, "autofs")) {
112,628✔
251
                                top_autofs = top_autofs || path_equal(path, prefix);
8,528✔
252
                                continue;
4,264✔
253
                        }
254

255
                        if (set_contains(done, path))
108,364✔
256
                                continue;
37,399✔
257

258
                        /* Ignore this mount if it is deny-listed, but only if it isn't the top-level mount
259
                         * we shall operate on. */
260
                        if (!path_equal(path, prefix)) {
70,965✔
261
                                bool deny_listed = false;
666,508✔
262

263
                                STRV_FOREACH(i, deny_list) {
666,508✔
264
                                        if (path_equal(*i, prefix))
659,544✔
265
                                                continue;
40,519✔
266

267
                                        if (!path_startswith(*i, prefix))
619,025✔
268
                                                continue;
385,915✔
269

270
                                        if (path_startswith(path, *i)) {
233,110✔
271
                                                deny_listed = true;
272
                                                log_trace("Not remounting %s deny-listed by %s, called for %s", path, *i, prefix);
273
                                                break;
274
                                        }
275
                                }
276

277
                                if (deny_listed)
40,530✔
278
                                        continue;
33,566✔
279
                        }
280

281
                        opts = mnt_fs_get_vfs_options(fs);
37,399✔
282
                        if (opts) {
37,399✔
283
                                r = mnt_optstr_get_flags(opts, &flags, mnt_get_builtin_optmap(MNT_LINUX_MAP));
37,399✔
284
                                if (r < 0)
37,399✔
285
                                        log_debug_errno(r, "Could not get flags for '%s', ignoring: %m", path);
×
286
                        }
287

288
                        d = strdup(path);
37,399✔
289
                        if (!d)
37,399✔
290
                                return -ENOMEM;
291

292
                        r = hashmap_ensure_put(&todo, &path_hash_ops_free, d, ULONG_TO_PTR(flags));
37,399✔
293
                        if (r == -EEXIST)
37,399✔
294
                                /* If the same path was recorded, but with different mount flags, update it:
295
                                 * it means a mount point is overmounted, and libmount returns the "bottom" (or
296
                                 * older one) first, but we want to reapply the flags from the "top" (or newer
297
                                 * one). See: https://github.com/systemd/systemd/issues/20032
298
                                 * Note that this shouldn't really fail, as we were just told that the key
299
                                 * exists, and it's an update so we want 'd' to be freed immediately. */
300
                                r = hashmap_update(todo, d, ULONG_TO_PTR(flags));
8✔
301
                        if (r < 0)
37,399✔
302
                                return r;
303
                        if (r > 0)
37,399✔
304
                                TAKE_PTR(d);
37,357✔
305
                }
306

307
                /* Check if the top-level directory was among what we have seen so far. For that check both
308
                 * 'done' and 'todo'. Also check 'top_autofs' because if the top-level dir is an autofs we'll
309
                 * not include it in either set but will set this bool. */
310
                if (!set_contains(done, prefix) &&
60,869✔
311
                    !(top_autofs || hashmap_contains(todo, prefix))) {
30,435✔
312

313
                        /* The prefix directory itself is not yet a mount, make it one. */
314
                        r = mount_nofollow(prefix, prefix, NULL, MS_BIND|MS_REC, NULL);
2✔
315
                        if (r < 0)
2✔
316
                                return r;
317

318
                        /* Immediately rescan, so that we pick up the new mount's flags */
319
                        continue;
2✔
320
                }
321

322
                /* If we have no submounts to process anymore, we are done */
323
                if (hashmap_isempty(todo))
60,867✔
324
                        return 0;
325

326
                for (;;) {
67,790✔
327
                        unsigned long flags;
67,790✔
328
                        char *x = NULL;
67,790✔
329

330
                        /* Take the first mount from our list of mounts to still process */
331
                        flags = PTR_TO_ULONG(hashmap_steal_first_key_and_value(todo, (void**) &x));
67,790✔
332
                        if (!x)
67,790✔
333
                                break;
334

335
                        r = set_ensure_consume(&done, &path_hash_ops_free, x);
37,356✔
336
                        if (IN_SET(r, 0, -EEXIST))
37,356✔
337
                                continue; /* Already done */
179✔
338
                        if (r < 0)
37,356✔
339
                                return r;
×
340

341
                        /* Now, remount this with the new flags set, but exclude MS_RELATIME from it. (It's
342
                         * the default anyway, thus redundant, and in userns we'll get an error if we try to
343
                         * explicitly enable it) */
344
                        r = mount_nofollow(NULL, x, NULL, ((flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags) & ~MS_RELATIME, NULL);
37,356✔
345
                        if (r < 0) {
37,356✔
346
                                int q;
179✔
347

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

353
                                q = path_is_mount_point(x);
179✔
354
                                if (IN_SET(q, 0, -ENOENT)) {
179✔
355
                                        /* Hmm, whaaaa? The mount point is not actually a mount point? Then
356
                                         * it is either obstructed by a later mount or somebody has been
357
                                         * racing against us and removed it. Either way the mount point
358
                                         * doesn't matter to us, let's ignore it hence. */
359
                                        log_debug_errno(r, "Mount point '%s' to remount is not a mount point anymore, ignoring remount failure: %m", x);
176✔
360
                                        continue;
176✔
361
                                }
362
                                if (q < 0) /* Any other error on this? Just log and continue */
3✔
363
                                        log_debug_errno(q, "Failed to determine whether '%s' is a mount point or not, ignoring: %m", x);
×
364

365
                                if (((flags ^ new_flags) & flags_mask & ~MS_RELATIME) == 0) { /* ignore MS_RELATIME while comparing */
3✔
366
                                        log_debug_errno(r, "Couldn't remount '%s', but the flags already match what we want, hence ignoring: %m", x);
×
367
                                        continue;
×
368
                                }
369

370
                                /* Make this fatal if this is the top-level mount */
371
                                if (path_equal(x, prefix))
3✔
372
                                        return r;
373

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

382
                        log_trace("Remounted %s.", x);
37,177✔
383
                }
384
        }
385
}
386

387
int bind_remount_one_with_mountinfo(
3,034✔
388
                const char *path,
389
                unsigned long new_flags,
390
                unsigned long flags_mask,
391
                FILE *proc_self_mountinfo) {
392

393
        _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
3,034✔
394
        unsigned long flags = 0;
3,034✔
395
        struct libmnt_fs *fs;
3,034✔
396
        const char *opts;
3,034✔
397
        int r;
3,034✔
398

399
        assert(path);
3,034✔
400
        assert(proc_self_mountinfo);
3,034✔
401

402
        if ((flags_mask & ~MS_CONVERTIBLE_FLAGS) == 0 && !skip_mount_set_attr) {
3,034✔
403
                /* Let's take a shortcut for all the flags we know how to convert into mount_setattr() flags */
404

405
                if (mount_setattr(AT_FDCWD, path, AT_SYMLINK_NOFOLLOW,
3,034✔
406
                                  &(struct mount_attr) {
3,034✔
407
                                          .attr_set = ms_flags_to_mount_attr(new_flags & flags_mask),
3,034✔
408
                                          .attr_clr = ms_flags_to_mount_attr(~new_flags & flags_mask),
3,034✔
409
                                  }, MOUNT_ATTR_SIZE_VER0) < 0) {
410

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

413
                        if (ERRNO_IS_NOT_SUPPORTED(errno)) /* if not supported, then don't bother at all anymore */
4✔
414
                                skip_mount_set_attr = true;
×
415
                } else
416
                        return 0; /* Nice, this worked! */
3,030✔
417
        }
418

419
        rewind(proc_self_mountinfo);
4✔
420

421
        table = mnt_new_table();
4✔
422
        if (!table)
4✔
423
                return -ENOMEM;
424

425
        r = mnt_table_parse_stream(table, proc_self_mountinfo, "/proc/self/mountinfo");
4✔
426
        if (r < 0)
4✔
427
                return r;
428

429
        fs = mnt_table_find_target(table, path, MNT_ITER_FORWARD);
4✔
430
        if (!fs) {
4✔
431
                r = access_nofollow(path, F_OK); /* Hmm, it's not in the mount table, but does it exist at all? */
4✔
432
                if (r < 0)
2✔
433
                        return r;
434

435
                return -EINVAL; /* Not a mount point we recognize */
2✔
436
        }
437

438
        opts = mnt_fs_get_vfs_options(fs);
×
439
        if (opts) {
×
440
                r = mnt_optstr_get_flags(opts, &flags, mnt_get_builtin_optmap(MNT_LINUX_MAP));
×
441
                if (r < 0)
×
442
                        log_debug_errno(r, "Could not get flags for '%s', ignoring: %m", path);
×
443
        }
444

445
        r = mount_nofollow(NULL, path, NULL, ((flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags) & ~MS_RELATIME, NULL);
×
446
        if (r < 0) {
×
447
                if (((flags ^ new_flags) & flags_mask & ~MS_RELATIME) != 0) /* Ignore MS_RELATIME again,
×
448
                                                                             * since kernel adds it in
449
                                                                             * everywhere, because it's the
450
                                                                             * default. */
451
                        return r;
452

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

457
        return 0;
458
}
459

460
int bind_remount_one(const char *path, unsigned long new_flags, unsigned long flags_mask) {
53✔
461
        _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
53✔
462

463
        proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
53✔
464
        if (!proc_self_mountinfo)
53✔
465
                return log_debug_errno(errno, "Failed to open /proc/self/mountinfo: %m");
×
466

467
        return bind_remount_one_with_mountinfo(path, new_flags, flags_mask, proc_self_mountinfo);
53✔
468
}
469

470
static int mount_switch_root_pivot(int fd_newroot, const char *path) {
2,808✔
471
        assert(fd_newroot >= 0);
2,808✔
472
        assert(path);
2,808✔
473

474
        /* Let the kernel tuck the new root under the old one. */
475
        if (pivot_root(".", ".") < 0)
2,808✔
476
                return log_debug_errno(errno, "Failed to pivot root to new rootfs '%s': %m", path);
354✔
477

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

483
        return 0;
484
}
485

486
static int mount_switch_root_move(int fd_newroot, const char *path) {
354✔
487
        assert(fd_newroot >= 0);
354✔
488
        assert(path);
354✔
489

490
        /* Move the new root fs */
491
        if (mount(".", "/", NULL, MS_MOVE, NULL) < 0)
354✔
492
                return log_debug_errno(errno, "Failed to move new rootfs '%s': %m", path);
×
493

494
        /* Also change root dir */
495
        if (chroot(".") < 0)
354✔
496
                return log_debug_errno(errno, "Failed to chroot to new rootfs '%s': %m", path);
×
497

498
        return 0;
499
}
500

501
int mount_switch_root_full(const char *path, unsigned long mount_propagation_flag, bool force_ms_move) {
2,810✔
502
        _cleanup_close_ int fd_newroot = -EBADF;
2,810✔
503
        int r, is_current_root;
2,810✔
504

505
        assert(path);
2,810✔
506
        assert(mount_propagation_flag_is_valid(mount_propagation_flag));
2,810✔
507

508
        fd_newroot = open(path, O_PATH|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
2,810✔
509
        if (fd_newroot < 0)
2,810✔
510
                return log_debug_errno(errno, "Failed to open new rootfs '%s': %m", path);
×
511

512
        is_current_root = path_is_root_at(fd_newroot, NULL);
2,810✔
513
        if (is_current_root < 0)
2,810✔
514
                return log_debug_errno(is_current_root, "Failed to determine if target dir is our root already: %m");
×
515

516
        /* Change into the new rootfs. */
517
        if (fchdir(fd_newroot) < 0)
2,810✔
518
                return log_debug_errno(errno, "Failed to chdir into new rootfs '%s': %m", path);
×
519

520
        /* Make this a NOP if we are supposed to switch to our current root fs. After all, both pivot_root()
521
         * and MS_MOVE don't like that. */
522
        if (!is_current_root) {
2,810✔
523
                if (!force_ms_move) {
2,808✔
524
                        r = mount_switch_root_pivot(fd_newroot, path);
2,808✔
525
                        if (r < 0) {
2,808✔
526
                                log_debug_errno(r, "Failed to pivot into new rootfs '%s', will try to use MS_MOVE instead: %m", path);
354✔
527
                                force_ms_move = true;
528
                        }
529
                }
530
                if (force_ms_move) {
531
                        /* Failed to pivot_root() fallback to MS_MOVE. For example, this may happen if the rootfs is
532
                         * an initramfs in which case pivot_root() isn't supported. */
533
                        r = mount_switch_root_move(fd_newroot, path);
354✔
534
                        if (r < 0)
354✔
535
                                return log_debug_errno(r, "Failed to switch to new rootfs '%s' with MS_MOVE: %m", path);
×
536
                }
537
        }
538

539
        log_debug("Successfully switched root to '%s'.", path);
2,810✔
540

541
        /* Finally, let's establish the requested propagation flags. */
542
        if (mount_propagation_flag == 0)
2,810✔
543
                return 0;
544

545
        if (mount(NULL, ".", NULL, mount_propagation_flag | MS_REC, NULL) < 0)
269✔
546
                return log_debug_errno(errno, "Failed to turn new rootfs '%s' into %s mount: %m",
×
547
                                       mount_propagation_flag_to_string(mount_propagation_flag), path);
548

549
        return 0;
550
}
551

552
int repeat_unmount(const char *path, int flags) {
67✔
553
        bool done = false;
67✔
554

555
        assert(path);
67✔
556

557
        /* If there are multiple mounts on a mount point, this
558
         * removes them all */
559

560
        for (;;) {
134✔
561
                if (umount2(path, flags) < 0) {
134✔
562

563
                        if (errno == EINVAL)
67✔
564
                                return done;
67✔
565

566
                        return -errno;
×
567
                }
568

569
                done = true;
570
        }
571
}
572

573
int mode_to_inaccessible_node(
6,917✔
574
                const char *runtime_dir,
575
                mode_t mode,
576
                char **ret) {
577

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

589
        _cleanup_free_ char *d = NULL;
6,917✔
590
        const char *node;
6,917✔
591

592
        assert(ret);
6,917✔
593

594
        if (!runtime_dir)
6,917✔
595
                runtime_dir = "/run";
4✔
596

597
        if (S_ISLNK(mode))
6,917✔
598
                return -EINVAL;
599

600
        node = inode_type_to_string(mode);
6,917✔
601
        if (!node)
6,917✔
602
                return -EINVAL;
603

604
        d = path_join(runtime_dir, "systemd/inaccessible", node);
6,917✔
605
        if (!d)
6,917✔
606
                return -ENOMEM;
607

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

616
        if (S_ISBLK(mode) &&
6,917✔
617
            access(d, F_OK) < 0 && errno == ENOENT) {
×
618
                free(d);
×
619
                d = path_join(runtime_dir, "/systemd/inaccessible/chr");
×
620
                if (!d)
×
621
                        return -ENOMEM;
622
        }
623

624
        if (IN_SET(mode & S_IFMT, S_IFBLK, S_IFCHR) &&
7,363✔
625
            access(d, F_OK) < 0 && errno == ENOENT) {
446✔
626
                free(d);
×
627
                d = path_join(runtime_dir, "/systemd/inaccessible/sock");
×
628
                if (!d)
×
629
                        return -ENOMEM;
630
        }
631

632
        *ret = TAKE_PTR(d);
6,917✔
633
        return 0;
6,917✔
634
}
635

636
int mount_flags_to_string(unsigned long flags, char **ret) {
70,366✔
637
        static const struct {
70,366✔
638
                unsigned long flag;
639
                const char *name;
640
        } map[] = {
641
                { .flag = MS_RDONLY,      .name = "MS_RDONLY",      },
642
                { .flag = MS_NOSUID,      .name = "MS_NOSUID",      },
643
                { .flag = MS_NODEV,       .name = "MS_NODEV",       },
644
                { .flag = MS_NOEXEC,      .name = "MS_NOEXEC",      },
645
                { .flag = MS_SYNCHRONOUS, .name = "MS_SYNCHRONOUS", },
646
                { .flag = MS_REMOUNT,     .name = "MS_REMOUNT",     },
647
                { .flag = MS_MANDLOCK,    .name = "MS_MANDLOCK",    },
648
                { .flag = MS_DIRSYNC,     .name = "MS_DIRSYNC",     },
649
                { .flag = MS_NOSYMFOLLOW, .name = "MS_NOSYMFOLLOW", },
650
                { .flag = MS_NOATIME,     .name = "MS_NOATIME",     },
651
                { .flag = MS_NODIRATIME,  .name = "MS_NODIRATIME",  },
652
                { .flag = MS_BIND,        .name = "MS_BIND",        },
653
                { .flag = MS_MOVE,        .name = "MS_MOVE",        },
654
                { .flag = MS_REC,         .name = "MS_REC",         },
655
                { .flag = MS_SILENT,      .name = "MS_SILENT",      },
656
                { .flag = MS_POSIXACL,    .name = "MS_POSIXACL",    },
657
                { .flag = MS_UNBINDABLE,  .name = "MS_UNBINDABLE",  },
658
                { .flag = MS_PRIVATE,     .name = "MS_PRIVATE",     },
659
                { .flag = MS_SLAVE,       .name = "MS_SLAVE",       },
660
                { .flag = MS_SHARED,      .name = "MS_SHARED",      },
661
                { .flag = MS_RELATIME,    .name = "MS_RELATIME",    },
662
                { .flag = MS_KERNMOUNT,   .name = "MS_KERNMOUNT",   },
663
                { .flag = MS_I_VERSION,   .name = "MS_I_VERSION",   },
664
                { .flag = MS_STRICTATIME, .name = "MS_STRICTATIME", },
665
                { .flag = MS_LAZYTIME,    .name = "MS_LAZYTIME",    },
666
        };
667
        _cleanup_free_ char *str = NULL;
70,366✔
668

669
        assert(ret);
70,366✔
670

671
        FOREACH_ELEMENT(entry, map)
1,829,516✔
672
                if (flags & entry->flag) {
1,759,150✔
673
                        if (!strextend_with_separator(&str, "|", entry->name))
160,832✔
674
                                return -ENOMEM;
675
                        flags &= ~entry->flag;
160,832✔
676
                }
677

678
        if (!str || flags != 0)
70,366✔
679
                if (strextendf_with_separator(&str, "|", "%lx", flags) < 0)
205✔
680
                        return -ENOMEM;
681

682
        *ret = TAKE_PTR(str);
70,366✔
683
        return 0;
70,366✔
684
}
685

686
int mount_verbose_full(
70,336✔
687
                int error_log_level,
688
                const char *what,
689
                const char *where,
690
                const char *type,
691
                unsigned long flags,
692
                const char *options,
693
                bool follow_symlink) {
694

695
        _cleanup_free_ char *fl = NULL, *o = NULL;
70,336✔
696
        unsigned long f;
70,336✔
697
        int r;
70,336✔
698

699
        r = mount_option_mangle(options, flags, &f, &o);
70,336✔
700
        if (r < 0)
70,336✔
701
                return log_full_errno(error_log_level, r,
×
702
                                      "Failed to mangle mount options %s: %m",
703
                                      strempty(options));
704

705
        (void) mount_flags_to_string(f, &fl);
70,336✔
706

707
        if (FLAGS_SET(f, MS_REMOUNT|MS_BIND))
70,336✔
708
                log_debug("Changing mount flags %s (%s \"%s\")...",
12,833✔
709
                          where, strnull(fl), strempty(o));
710
        else if (f & MS_REMOUNT)
63,919✔
711
                log_debug("Remounting superblock %s (%s \"%s\")...",
46✔
712
                          where, strnull(fl), strempty(o));
713
        else if (f & (MS_SHARED|MS_PRIVATE|MS_SLAVE|MS_UNBINDABLE))
63,873✔
714
                log_debug("Changing mount propagation %s (%s \"%s\")",
7,528✔
715
                          where, strnull(fl), strempty(o));
716
        else if (f & MS_BIND)
60,109✔
717
                log_debug("Bind-mounting %s on %s (%s \"%s\")...",
87,013✔
718
                          what, where, strnull(fl), strempty(o));
719
        else if (f & MS_MOVE)
16,539✔
720
                log_debug("Moving mount %s %s %s (%s \"%s\")...",
11,020✔
721
                          what, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), where, strnull(fl), strempty(o));
722
        else
723
                log_debug("Mounting %s (%s) on %s (%s \"%s\")...",
12,836✔
724
                          strna(what), strna(type), where, strnull(fl), strempty(o));
725

726
        if (follow_symlink)
70,336✔
727
                r = RET_NERRNO(mount(what, where, type, f, o));
70,947✔
728
        else
729
                r = mount_nofollow(what, where, type, f, o);
61,593✔
730
        if (r < 0)
62,204✔
731
                return log_full_errno(error_log_level, r,
10,332✔
732
                                      "Failed to mount %s (type %s) on %s (%s \"%s\"): %m",
733
                                      strna(what), strna(type), where, strnull(fl), strempty(o));
734
        return 0;
735
}
736

737
int umount_verbose(
494✔
738
                int error_log_level,
739
                const char *where,
740
                int flags) {
741

742
        assert(where);
494✔
743

744
        log_debug("Unmounting '%s'...", where);
494✔
745

746
        if (umount2(where, flags) < 0)
494✔
747
                return log_full_errno(error_log_level, errno, "Failed to unmount '%s': %m", where);
81✔
748

749
        return 0;
750
}
751

752
int umountat_detach_verbose(
222✔
753
                int error_log_level,
754
                int fd,
755
                const char *where) {
756

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

760
        assert(fd >= 0 || fd == AT_FDCWD);
222✔
761

762
        /* If neither fd nor path are specified take this as reference to the cwd */
763
        if (fd == AT_FDCWD && isempty(where))
222✔
764
                return umount_verbose(error_log_level, ".", MNT_DETACH|UMOUNT_NOFOLLOW);
222✔
765

766
        /* If we don't actually take the fd into consideration for this operation shortcut things, so that we
767
         * don't have to open the inode */
768
        if (fd == AT_FDCWD || path_is_absolute(where))
222✔
769
                return umount_verbose(error_log_level, where, MNT_DETACH|UMOUNT_NOFOLLOW);
×
770

771
        _cleanup_free_ char *prefix = NULL;
444✔
772
        const char *p;
222✔
773
        if (fd_get_path(fd, &prefix) < 0)
222✔
774
                p = "<fd>"; /* if we can't get the path, return something vaguely useful */
775
        else
776
                p = prefix;
222✔
777
        _cleanup_free_ char *joined = isempty(where) ? strdup(p) : path_join(p, where);
564✔
778

779
        log_debug("Unmounting '%s'...", strna(joined));
222✔
780

781
        _cleanup_close_ int inode_fd = -EBADF;
222✔
782
        int mnt_fd;
222✔
783
        if (isempty(where))
222✔
784
                mnt_fd = fd;
785
        else {
786
                inode_fd = openat(fd, where, O_PATH|O_CLOEXEC|O_NOFOLLOW);
120✔
787
                if (inode_fd < 0)
120✔
788
                        return log_full_errno(error_log_level, errno, "Failed to pin '%s': %m", strna(joined));
×
789

790
                mnt_fd = inode_fd;
791
        }
792

793
        if (umount2(FORMAT_PROC_FD_PATH(mnt_fd), MNT_DETACH) < 0)
222✔
794
                return log_full_errno(error_log_level, errno, "Failed to unmount '%s': %m", strna(joined));
9✔
795

796
        return 0;
213✔
797
}
798

799
int mount_exchange_graceful(int fsmount_fd, const char *dest, bool mount_beneath) {
16✔
800
        int r;
16✔
801

802
        assert(fsmount_fd >= 0);
16✔
803
        assert(dest);
16✔
804

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

812
        r = RET_NERRNO(move_mount(fsmount_fd, /* from_path = */ "",
26✔
813
                                  /* to_fd = */ -EBADF, dest,
814
                                  MOVE_MOUNT_F_EMPTY_PATH | (mount_beneath ? MOVE_MOUNT_BENEATH : 0)));
815
        if (mount_beneath) {
16✔
816
                if (r >= 0) /* Mounting beneath worked! Now unmount the upper mount. */
6✔
817
                        return umount_verbose(LOG_DEBUG, dest, UMOUNT_NOFOLLOW|MNT_DETACH);
2✔
818

819
                if (r == -EINVAL) { /* Fallback if mount_beneath is not supported */
4✔
820
                        log_debug_errno(r,
4✔
821
                                        "Cannot mount beneath '%s', falling back to overmount: %m",
822
                                        dest);
823
                        return mount_exchange_graceful(fsmount_fd, dest, /* mount_beneath = */ false);
4✔
824
                }
825
        }
826

827
        return r;
828
}
829

830
int mount_option_mangle(
70,408✔
831
                const char *options,
832
                unsigned long mount_flags,
833
                unsigned long *ret_mount_flags,
834
                char **ret_remaining_options) {
835

836
        const struct libmnt_optmap *map;
70,408✔
837
        _cleanup_free_ char *ret = NULL;
70,408✔
838
        int r;
70,408✔
839

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

853
        assert(ret_mount_flags);
70,408✔
854
        assert(ret_remaining_options);
70,408✔
855

856
        map = mnt_get_builtin_optmap(MNT_LINUX_MAP);
70,408✔
857
        if (!map)
70,408✔
858
                return -EINVAL;
859

860
        for (const char *p = options;;) {
70,408✔
861
                _cleanup_free_ char *word = NULL;
30,326✔
862
                const struct libmnt_optmap *ent;
100,733✔
863

864
                r = extract_first_word(&p, &word, ",", EXTRACT_KEEP_QUOTE);
100,733✔
865
                if (r < 0)
100,733✔
866
                        return r;
867
                if (r == 0)
100,732✔
868
                        break;
869

870
                for (ent = map; ent->name; ent++) {
1,271,067✔
871
                        /* All entries in MNT_LINUX_MAP do not take any argument.
872
                         * Thus, ent->name does not contain "=" or "[=]". */
873
                        if (!streq(word, ent->name))
1,240,833✔
874
                                continue;
1,240,742✔
875

876
                        if (!(ent->mask & MNT_INVERT))
91✔
877
                                mount_flags |= ent->id;
82✔
878
                        else
879
                                mount_flags &= ~ent->id;
9✔
880

881
                        break;
882
                }
883

884
                /* If 'word' is not a mount flag, then store it in '*ret_remaining_options'. */
885
                if (!ent->name &&
60,559✔
886
                    !startswith_no_case(word, "x-") &&
60,466✔
887
                    !strextend_with_separator(&ret, ",", word))
30,232✔
888
                        return -ENOMEM;
889
        }
890

891
        *ret_mount_flags = mount_flags;
70,407✔
892
        *ret_remaining_options = TAKE_PTR(ret);
70,407✔
893

894
        return 0;
70,407✔
895
}
896

897
static int mount_in_namespace_legacy(
×
898
                const char *chased_src_path,
899
                int chased_src_fd,
900
                struct stat *chased_src_st,
901
                const char *propagate_path,
902
                const char *incoming_path,
903
                const char *dest,
904
                int pidns_fd,
905
                int mntns_fd,
906
                int root_fd,
907
                MountInNamespaceFlags flags,
908
                const MountOptions *options,
909
                const ImagePolicy *image_policy) {
910

911
        _cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
×
912
        char mount_slave[] = "/tmp/propagate.XXXXXX", *mount_tmp, *mount_outside, *p;
×
913
        bool mount_slave_created = false, mount_slave_mounted = false,
×
914
                mount_tmp_created = false, mount_tmp_mounted = false,
×
915
                mount_outside_created = false, mount_outside_mounted = false;
×
916
        pid_t child;
×
917
        int r;
×
918

919
        assert(chased_src_path);
×
920
        assert(chased_src_fd >= 0);
×
921
        assert(chased_src_st);
×
922
        assert(propagate_path);
×
923
        assert(incoming_path);
×
924
        assert(dest);
×
925
        assert(pidns_fd >= 0);
×
926
        assert(mntns_fd >= 0);
×
927
        assert(root_fd >= 0);
×
928
        assert(!options || (flags & MOUNT_IN_NAMESPACE_IS_IMAGE));
×
929

930
        p = strjoina(propagate_path, "/");
×
931
        r = access_nofollow(p, F_OK);
×
932
        if (r < 0)
×
933
                return log_debug_errno(r == -ENOENT ? SYNTHETIC_ERRNO(EOPNOTSUPP) : r, "Target does not allow propagation of mount points");
×
934

935
        /* Our goal is to install a new bind mount into the container,
936
           possibly read-only. This is irritatingly complex
937
           unfortunately, currently.
938

939
           First, we start by creating a private playground in /tmp,
940
           that we can mount MS_SLAVE. (Which is necessary, since
941
           MS_MOVE cannot be applied to mounts with MS_SHARED parent
942
           mounts.) */
943

944
        if (!mkdtemp(mount_slave))
×
945
                return log_debug_errno(errno, "Failed to create playground %s: %m", mount_slave);
×
946

947
        mount_slave_created = true;
×
948

949
        r = mount_nofollow_verbose(LOG_DEBUG, mount_slave, mount_slave, NULL, MS_BIND, NULL);
×
950
        if (r < 0)
×
951
                goto finish;
×
952

953
        mount_slave_mounted = true;
×
954

955
        r = mount_nofollow_verbose(LOG_DEBUG, NULL, mount_slave, NULL, MS_SLAVE, NULL);
×
956
        if (r < 0)
×
957
                goto finish;
×
958

959
        /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
960
        mount_tmp = strjoina(mount_slave, "/mount");
×
NEW
961
        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);
×
962
        if (r < 0) {
×
963
                log_debug_errno(r, "Failed to create temporary mount point %s: %m", mount_tmp);
×
964
                goto finish;
×
965
        }
966

967
        mount_tmp_created = true;
×
968

969
        if (flags & MOUNT_IN_NAMESPACE_IS_IMAGE)
×
970
                r = verity_dissect_and_mount(
×
971
                                chased_src_fd,
972
                                chased_src_path,
973
                                mount_tmp,
974
                                options,
975
                                image_policy,
976
                                /* required_host_os_release_id= */ NULL,
977
                                /* required_host_os_release_version_id= */ NULL,
978
                                /* required_host_os_release_sysext_level= */ NULL,
979
                                /* required_host_os_release_confext_level= */ NULL,
980
                                /* required_sysext_scope= */ NULL,
981
                                /* verity= */ NULL,
982
                                /* ret_image= */ NULL);
983
        else
984
                r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(chased_src_fd), mount_tmp, NULL, MS_BIND, NULL);
×
985
        if (r < 0)
×
986
                goto finish;
×
987

988
        mount_tmp_mounted = true;
×
989

990
        /* Third, we remount the new bind mount read-only if requested. */
991
        if (flags & MOUNT_IN_NAMESPACE_READ_ONLY) {
×
992
                r = mount_nofollow_verbose(LOG_DEBUG, NULL, mount_tmp, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL);
×
993
                if (r < 0)
×
994
                        goto finish;
×
995
        }
996

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

1000
        mount_outside = strjoina(propagate_path, "/XXXXXX");
×
1001
        if ((flags & MOUNT_IN_NAMESPACE_IS_IMAGE) || S_ISDIR(chased_src_st->st_mode))
×
1002
                r = mkdtemp(mount_outside) ? 0 : -errno;
×
1003
        else {
1004
                r = mkostemp_safe(mount_outside);
×
1005
                safe_close(r);
×
1006
        }
1007
        if (r < 0) {
×
1008
                log_debug_errno(r, "Cannot create propagation file or directory %s: %m", mount_outside);
×
1009
                goto finish;
×
1010
        }
1011

1012
        mount_outside_created = true;
×
1013

1014
        r = mount_nofollow_verbose(LOG_DEBUG, mount_tmp, mount_outside, NULL, MS_MOVE, NULL);
×
1015
        if (r < 0)
×
1016
                goto finish;
×
1017

1018
        mount_outside_mounted = true;
×
1019
        mount_tmp_mounted = false;
×
1020

1021
        if ((flags & MOUNT_IN_NAMESPACE_IS_IMAGE) || S_ISDIR(chased_src_st->st_mode))
×
1022
                (void) rmdir(mount_tmp);
×
1023
        else
1024
                (void) unlink(mount_tmp);
×
1025
        mount_tmp_created = false;
×
1026

1027
        (void) umount_verbose(LOG_DEBUG, mount_slave, UMOUNT_NOFOLLOW);
×
1028
        mount_slave_mounted = false;
×
1029

1030
        (void) rmdir(mount_slave);
×
1031
        mount_slave_created = false;
×
1032

1033
        if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) {
×
1034
                log_debug_errno(errno, "Failed to create pipe: %m");
×
1035
                goto finish;
×
1036
        }
1037

NEW
1038
        r = namespace_fork(
×
1039
                        "(sd-bindmnt)",
1040
                        "(sd-bindmnt-inner)",
1041
                        /* except_fds= */ NULL,
1042
                        /* n_except_fds= */ 0,
1043
                        FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM,
1044
                        pidns_fd,
1045
                        mntns_fd,
1046
                        /* netns_fd= */ -EBADF,
1047
                        /* userns_fd= */ -EBADF,
1048
                        root_fd,
1049
                        &child);
1050
        if (r < 0)
×
1051
                goto finish;
×
1052
        if (r == 0) {
×
1053
                _cleanup_free_ char *mount_outside_fn = NULL, *mount_inside = NULL;
×
1054

1055
                errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
×
1056

NEW
1057
                _cleanup_close_ int dest_fd = -EBADF;
×
NEW
1058
                _cleanup_free_ char *dest_fn = NULL;
×
NEW
1059
                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);
×
NEW
1060
                if (r < 0)
×
NEW
1061
                        log_debug_errno(r, "Failed to pin parent directory of mount '%s', ignoring: %m", dest);
×
NEW
1062
                else if (flags & MOUNT_IN_NAMESPACE_MAKE_FILE_OR_DIRECTORY) {
×
NEW
1063
                        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);
×
NEW
1064
                        if (r < 0)
×
NEW
1065
                                log_debug_errno(r, "Failed to make mount point inode of mount '%s', ignoring: %m", dest);
×
1066
                }
1067

1068
                /* Fifth, move the mount to the right place inside */
1069
                r = path_extract_filename(mount_outside, &mount_outside_fn);
×
1070
                if (r < 0) {
×
1071
                        log_debug_errno(r, "Failed to extract filename from propagation file or directory '%s': %m", mount_outside);
×
1072
                        report_errno_and_exit(errno_pipe_fd[1], r);
×
1073
                }
1074

1075
                mount_inside = path_join(incoming_path, mount_outside_fn);
×
1076
                if (!mount_inside)
×
1077
                        report_errno_and_exit(errno_pipe_fd[1], log_oom_debug());
×
1078

NEW
1079
                r = mount_nofollow_verbose(LOG_DEBUG, mount_inside, dest_fd >= 0 ? FORMAT_PROC_FD_PATH(dest_fd) : dest, /* fstype= */ NULL, MS_MOVE, /* options= */ NULL);
×
1080
                if (r < 0)
×
1081
                        report_errno_and_exit(errno_pipe_fd[1], r);
×
1082

1083
                _exit(EXIT_SUCCESS);
×
1084
        }
1085

1086
        errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
×
1087

1088
        r = wait_for_terminate_and_check("(sd-bindmnt)", child, 0);
×
1089
        if (r < 0) {
×
1090
                log_debug_errno(r, "Failed to wait for child: %m");
×
1091
                goto finish;
×
1092
        }
1093
        if (r != EXIT_SUCCESS) {
×
1094
                if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r))
×
1095
                        log_debug_errno(r, "Failed to mount: %m");
×
1096
                else
1097
                        log_debug("Child failed.");
×
1098
                goto finish;
×
1099
        }
1100

1101
finish:
×
1102
        if (mount_outside_mounted)
×
1103
                (void) umount_verbose(LOG_DEBUG, mount_outside, UMOUNT_NOFOLLOW);
×
1104
        if (mount_outside_created) {
×
1105
                if ((flags & MOUNT_IN_NAMESPACE_IS_IMAGE) || S_ISDIR(chased_src_st->st_mode))
×
1106
                        (void) rmdir(mount_outside);
×
1107
                else
1108
                        (void) unlink(mount_outside);
×
1109
        }
1110

1111
        if (mount_tmp_mounted)
×
1112
                (void) umount_verbose(LOG_DEBUG, mount_tmp, UMOUNT_NOFOLLOW);
×
1113
        if (mount_tmp_created) {
×
1114
                if ((flags & MOUNT_IN_NAMESPACE_IS_IMAGE) || S_ISDIR(chased_src_st->st_mode))
×
1115
                        (void) rmdir(mount_tmp);
×
1116
                else
1117
                        (void) unlink(mount_tmp);
×
1118
        }
1119

1120
        if (mount_slave_mounted)
×
1121
                (void) umount_verbose(LOG_DEBUG, mount_slave, UMOUNT_NOFOLLOW);
×
1122
        if (mount_slave_created)
×
1123
                (void) rmdir(mount_slave);
×
1124

1125
        return r;
×
1126
}
1127

1128
static int mount_in_namespace(
6✔
1129
                const PidRef *target,
1130
                const char *propagate_path,
1131
                const char *incoming_path,
1132
                const char *src,
1133
                const char *dest,
1134
                MountInNamespaceFlags flags,
1135
                const MountOptions *options,
1136
                const ImagePolicy *image_policy) {
1137

1138
        _cleanup_close_ int mntns_fd = -EBADF, root_fd = -EBADF, pidns_fd = -EBADF, chased_src_fd = -EBADF;
18✔
1139
        _cleanup_free_ char *chased_src_path = NULL;
6✔
1140
        struct stat st;
6✔
1141
        int r;
6✔
1142

1143
        assert(propagate_path);
6✔
1144
        assert(incoming_path);
6✔
1145
        assert(src);
6✔
1146
        assert(dest);
6✔
1147
        assert((flags & MOUNT_IN_NAMESPACE_IS_IMAGE) || (!options && !image_policy));
6✔
1148

1149
        if (!pidref_is_set(target))
12✔
1150
                return -ESRCH;
1151

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

1156
        r = is_our_namespace(mntns_fd, NAMESPACE_MOUNT);
6✔
1157
        if (r < 0)
6✔
1158
                return log_debug_errno(r, "Failed to determine if mount namespaces are equal: %m");
×
1159
        /* We can't add new mounts at runtime if the process wasn't started in a namespace */
1160
        if (r > 0)
6✔
1161
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to activate bind mount in target, not running in a mount namespace.");
×
1162

1163
        r = chase(src, NULL, 0, &chased_src_path, &chased_src_fd);
6✔
1164
        if (r < 0)
6✔
1165
                return log_debug_errno(r, "Failed to resolve source path '%s': %m", src);
×
1166
        log_debug("Chased source path '%s': %s", src, chased_src_path);
6✔
1167

1168
        if (fstat(chased_src_fd, &st) < 0)
6✔
1169
                return log_debug_errno(errno, "Failed to stat() resolved source path '%s': %m", src);
×
1170
        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✔
1171
                return log_debug_errno(SYNTHETIC_ERRNO(ELOOP), "Source path '%s' can't be a symbolic link.", src);
×
1172

1173
        if (!mount_new_api_supported()) /* Fallback if we can't use the new mount API */
6✔
1174
                return mount_in_namespace_legacy(
×
1175
                                chased_src_path,
1176
                                chased_src_fd,
1177
                                &st,
1178
                                propagate_path,
1179
                                incoming_path,
1180
                                dest,
1181
                                pidns_fd,
1182
                                mntns_fd,
1183
                                root_fd,
1184
                                flags,
1185
                                options,
1186
                                image_policy);
1187

1188
        _cleanup_(dissected_image_unrefp) DissectedImage *img = NULL;
×
1189
        _cleanup_close_ int new_mount_fd = -EBADF;
6✔
1190
        _cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
6✔
1191
        pid_t child;
6✔
1192

1193
        if (flags & MOUNT_IN_NAMESPACE_IS_IMAGE) {
6✔
1194
                r = verity_dissect_and_mount(
2✔
1195
                                chased_src_fd,
1196
                                chased_src_path,
1197
                                /* dest= */ NULL,
1198
                                options,
1199
                                image_policy,
1200
                                /* required_host_os_release_id= */ NULL,
1201
                                /* required_host_os_release_version_id= */ NULL,
1202
                                /* required_host_os_release_sysext_level= */ NULL,
1203
                                /* required_host_os_release_confext_level= */ NULL,
1204
                                /* required_sysext_scope= */ NULL,
1205
                                /* verity= */ NULL,
1206
                                &img);
1207
                if (r < 0)
2✔
1208
                        return log_debug_errno(r,
×
1209
                                               "Failed to dissect and mount image '%s': %m",
1210
                                               chased_src_path);
1211
        } else {
1212
                new_mount_fd = open_tree(
4✔
1213
                                chased_src_fd,
1214
                                "",
1215
                                OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC|AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH);
1216
                if (new_mount_fd < 0)
4✔
1217
                        return log_debug_errno(
×
1218
                                        errno,
1219
                                        "Failed to open mount source '%s': %m",
1220
                                        chased_src_path);
1221

1222
                if ((flags & MOUNT_IN_NAMESPACE_READ_ONLY) && mount_setattr(new_mount_fd, "", AT_EMPTY_PATH,
4✔
1223
                                               &(struct mount_attr) {
×
1224
                                                       .attr_set = MOUNT_ATTR_RDONLY,
1225
                                               }, MOUNT_ATTR_SIZE_VER0) < 0)
1226
                        return log_debug_errno(errno,
×
1227
                                               "Failed to set mount for '%s' to read only: %m",
1228
                                               chased_src_path);
1229
        }
1230

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

1234
        r = namespace_fork("(sd-bindmnt)",
6✔
1235
                           "(sd-bindmnt-inner)",
1236
                           /* except_fds= */ NULL,
1237
                           /* n_except_fds= */ 0,
1238
                           FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM,
1239
                           pidns_fd,
1240
                           mntns_fd,
1241
                           /* netns_fd= */ -EBADF,
1242
                           /* userns_fd= */ -EBADF,
1243
                           root_fd,
1244
                           &child);
1245
        if (r < 0)
12✔
1246
                return log_debug_errno(r, "Failed to fork off mount helper into namespace: %m");
×
1247
        if (r == 0) {
12✔
1248
                errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
6✔
1249

NEW
1250
                _cleanup_close_ int dest_fd = -EBADF;
×
NEW
1251
                _cleanup_free_ char *dest_fn = NULL;
×
1252
                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✔
1253
                if (r < 0)
6✔
NEW
1254
                        report_errno_and_exit(errno_pipe_fd[1], r);
×
1255

1256
                if (flags & MOUNT_IN_NAMESPACE_MAKE_FILE_OR_DIRECTORY)
6✔
1257
                        (void) make_mount_point_inode_from_mode(dest_fd, dest_fn, img ? S_IFDIR : st.st_mode, 0700);
6✔
1258

1259
                if (img) {
6✔
1260
                        DissectImageFlags f =
2✔
1261
                                DISSECT_IMAGE_TRY_ATOMIC_MOUNT_EXCHANGE |
1262
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY;
1263

1264
                        if (flags & MOUNT_IN_NAMESPACE_MAKE_FILE_OR_DIRECTORY)
2✔
1265
                                f |= DISSECT_IMAGE_MKDIR;
2✔
1266

1267
                        if (flags & MOUNT_IN_NAMESPACE_READ_ONLY)
2✔
1268
                                f |= DISSECT_IMAGE_READ_ONLY;
×
1269

1270
                        r = dissected_image_mount(
2✔
1271
                                        img,
1272
                                        dest,
1273
                                        /* uid_shift= */ UID_INVALID,
1274
                                        /* uid_range= */ UID_INVALID,
1275
                                        /* userns_fd= */ -EBADF,
1276
                                        f);
1277
                } else
1278
                        r = mount_exchange_graceful(new_mount_fd, dest, /* mount_beneath= */ true);
4✔
1279

1280
                report_errno_and_exit(errno_pipe_fd[1], r);
6✔
1281
        }
1282

1283
        errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
6✔
1284

1285
        r = wait_for_terminate_and_check("(sd-bindmnt)", child, 0);
6✔
1286
        if (r < 0)
6✔
1287
                return log_debug_errno(r, "Failed to wait for child: %m");
×
1288
        if (r != EXIT_SUCCESS) {
6✔
1289
                if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r))
×
1290
                        return log_debug_errno(r, "Failed to mount into namespace: %m");
×
1291

1292
                return log_debug_errno(SYNTHETIC_ERRNO(EPROTO), "Child failed.");
×
1293
        }
1294

1295
        return 0;
1296
}
1297

1298
int bind_mount_in_namespace(
4✔
1299
                const PidRef *target,
1300
                const char *propagate_path,
1301
                const char *incoming_path,
1302
                const char *src,
1303
                const char *dest,
1304
                MountInNamespaceFlags flags) {
1305

1306
        return mount_in_namespace(target,
8✔
1307
                                  propagate_path,
1308
                                  incoming_path,
1309
                                  src,
1310
                                  dest,
1311
                                  flags & ~MOUNT_IN_NAMESPACE_IS_IMAGE,
4✔
1312
                                  /* options = */ NULL,
1313
                                  /* image_policy = */ NULL);
1314
}
1315

1316
int mount_image_in_namespace(
2✔
1317
                const PidRef *target,
1318
                const char *propagate_path,
1319
                const char *incoming_path,
1320
                const char *src,
1321
                const char *dest,
1322
                MountInNamespaceFlags flags,
1323
                const MountOptions *options,
1324
                const ImagePolicy *image_policy) {
1325

1326
        return mount_in_namespace(target,
4✔
1327
                                  propagate_path,
1328
                                  incoming_path,
1329
                                  src,
1330
                                  dest,
1331
                                  flags | MOUNT_IN_NAMESPACE_IS_IMAGE,
2✔
1332
                                  options,
1333
                                  image_policy);
1334
}
1335

1336
int make_mount_point(const char *path) {
124✔
1337
        int r;
124✔
1338

1339
        assert(path);
124✔
1340

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

1343
        r = path_is_mount_point(path);
124✔
1344
        if (r < 0)
124✔
1345
                return log_debug_errno(r, "Failed to determine whether '%s' is a mount point: %m", path);
×
1346
        if (r > 0)
124✔
1347
                return 0;
1348

1349
        r = mount_nofollow_verbose(LOG_DEBUG, path, path, NULL, MS_BIND|MS_REC, NULL);
61✔
1350
        if (r < 0)
61✔
1351
                return r;
×
1352

1353
        return 1;
1354
}
1355

1356
int fd_make_mount_point(int fd) {
63✔
1357
        int r;
63✔
1358

1359
        assert(fd >= 0);
63✔
1360

1361
        r = is_mount_point_at(fd, NULL, 0);
63✔
1362
        if (r < 0)
63✔
1363
                return log_debug_errno(r, "Failed to determine whether file descriptor is a mount point: %m");
×
1364
        if (r > 0)
63✔
1365
                return 0;
1366

1367
        r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(fd), FORMAT_PROC_FD_PATH(fd), NULL, MS_BIND|MS_REC, NULL);
1✔
1368
        if (r < 0)
1✔
1369
                return r;
×
1370

1371
        return 1;
1372
}
1373

1374
int make_userns(uid_t uid_shift,
86✔
1375
                uid_t uid_range,
1376
                uid_t source_owner,
1377
                uid_t dest_owner,
1378
                RemountIdmapping idmapping) {
1379

1380
        _cleanup_close_ int userns_fd = -EBADF;
86✔
1381
        _cleanup_free_ char *line = NULL;
86✔
1382
        uid_t source_base = 0;
86✔
1383

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

1387
        if (!userns_shift_range_valid(uid_shift, uid_range))
86✔
1388
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid UID range for user namespace.");
×
1389

1390
        switch (idmapping) {
86✔
1391

1392
        case REMOUNT_IDMAPPING_FOREIGN_WITH_HOST_ROOT:
2✔
1393
                source_base = FOREIGN_UID_BASE;
2✔
1394
                _fallthrough_;
84✔
1395

1396
        case REMOUNT_IDMAPPING_NONE:
84✔
1397
        case REMOUNT_IDMAPPING_HOST_ROOT:
1398

1399
                if (asprintf(&line,
84✔
1400
                             UID_FMT " " UID_FMT " " UID_FMT "\n",
1401
                             source_base, uid_shift, uid_range) < 0)
1402
                        return log_oom_debug();
×
1403

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

1421
                break;
1422

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

1433
        case REMOUNT_IDMAPPING_HOST_OWNER_TO_TARGET_OWNER:
2✔
1434
                /* Remap the owner of the bind mounted directory to the owner of the target directory
1435
                 * within the container. This way every file written by target directory owner within the
1436
                 * container to the bind-mounted directory will be owned by the original host user.
1437
                 * All other users will remain unmapped. */
1438
                if (asprintf(&line,
2✔
1439
                             UID_FMT " " UID_FMT " " UID_FMT "\n",
1440
                             source_owner, dest_owner, (uid_t) 1u) < 0)
1441
                        return log_oom_debug();
×
1442
                break;
1443

1444
        default:
×
1445
                assert_not_reached();
×
1446
        }
1447

1448
        /* We always assign the same UID and GID ranges */
1449
        userns_fd = userns_acquire(line, line);
86✔
1450
        if (userns_fd < 0)
86✔
1451
                return log_debug_errno(userns_fd, "Failed to acquire new userns: %m");
×
1452

1453
        return TAKE_FD(userns_fd);
1454
}
1455

1456
int remount_idmap_fd(
96✔
1457
                char **paths,
1458
                int userns_fd,
1459
                uint64_t extra_mount_attr_set) {
1460

1461
        int r;
96✔
1462

1463
        assert(userns_fd >= 0);
96✔
1464

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

1470
        size_t n = strv_length(paths);
96✔
1471
        if (n == 0) /* Nothing to do? */
96✔
1472
                return 0;
96✔
1473

1474
        int *mount_fds = NULL;
96✔
1475
        size_t n_mounts_fds = 0;
96✔
1476

1477
        mount_fds = new(int, n);
96✔
1478
        if (!mount_fds)
96✔
1479
                return log_oom_debug();
×
1480

1481
        CLEANUP_ARRAY(mount_fds, n_mounts_fds, close_many_and_free);
96✔
1482

1483
        for (size_t i = 0; i < n; i++) {
190✔
1484
                int mntfd;
96✔
1485

1486
                /* Clone the mount point */
1487
                mntfd = mount_fds[n_mounts_fds] = open_tree(-EBADF, paths[i], OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
96✔
1488
                if (mount_fds[n_mounts_fds] < 0)
96✔
1489
                        return log_debug_errno(errno, "Failed to open tree of mounted filesystem '%s': %m", paths[i]);
×
1490

1491
                n_mounts_fds++;
96✔
1492

1493
                /* Set the user namespace mapping attribute on the cloned mount point */
1494
                if (mount_setattr(mntfd, "", AT_EMPTY_PATH,
96✔
1495
                                  &(struct mount_attr) {
96✔
1496
                                          .attr_set = MOUNT_ATTR_IDMAP | extra_mount_attr_set,
96✔
1497
                                          .userns_fd = userns_fd,
1498
                                  }, sizeof(struct mount_attr)) < 0)
1499
                        return log_debug_errno(errno, "Failed to change bind mount attributes for clone of '%s': %m", paths[i]);
2✔
1500
        }
1501

1502
        for (size_t i = n; i > 0; i--) { /* Unmount the paths right-to-left */
188✔
1503
                /* Remove the old mount points now that we have a idmapped mounts as replacement for all of them */
1504
                r = umount_verbose(LOG_DEBUG, paths[i-1], UMOUNT_NOFOLLOW);
94✔
1505
                if (r < 0)
94✔
1506
                        return r;
1507
        }
1508

1509
        for (size_t i = 0; i < n; i++) { /* Mount the replacement mounts left-to-right */
188✔
1510
                /* And place the cloned version in its place */
1511
                log_debug("Mounting idmapped fs to '%s'", paths[i]);
94✔
1512
                if (move_mount(mount_fds[i], "", -EBADF, paths[i], MOVE_MOUNT_F_EMPTY_PATH) < 0)
94✔
1513
                        return log_debug_errno(errno, "Failed to attach UID mapped mount to '%s': %m", paths[i]);
×
1514
        }
1515

1516
        return 0;
1517
}
1518

1519
int remount_idmap(
84✔
1520
                char **p,
1521
                uid_t uid_shift,
1522
                uid_t uid_range,
1523
                uid_t source_owner,
1524
                uid_t dest_owner,
1525
                RemountIdmapping idmapping) {
1526

1527
        _cleanup_close_ int userns_fd = -EBADF;
84✔
1528

1529
        userns_fd = make_userns(uid_shift, uid_range, source_owner, dest_owner, idmapping);
84✔
1530
        if (userns_fd < 0)
84✔
1531
                return userns_fd;
1532

1533
        return remount_idmap_fd(p, userns_fd, /* extra_mount_attr_set= */ 0);
84✔
1534
}
1535

1536
static void sub_mount_clear(SubMount *s) {
9,865✔
1537
        assert(s);
9,865✔
1538

1539
        s->path = mfree(s->path);
9,865✔
1540
        s->mount_fd = safe_close(s->mount_fd);
9,865✔
1541
}
9,865✔
1542

1543
void sub_mount_array_free(SubMount *s, size_t n) {
1,938✔
1544
        assert(s || n == 0);
1,938✔
1545

1546
        for (size_t i = 0; i < n; i++)
10,576✔
1547
                sub_mount_clear(s + i);
8,638✔
1548

1549
        free(s);
1,938✔
1550
}
1,938✔
1551

1552
static int sub_mount_compare(const SubMount *a, const SubMount *b) {
12,634✔
1553
        assert(a);
12,634✔
1554
        assert(b);
12,634✔
1555
        assert(a->path);
12,634✔
1556
        assert(b->path);
12,634✔
1557

1558
        return path_compare(a->path, b->path);
12,634✔
1559
}
1560

1561
static void sub_mount_drop(SubMount *s, size_t n) {
2,480✔
1562
        assert(s || n == 0);
2,480✔
1563

1564
        for (size_t m = 0, i = 1; i < n; i++) {
9,180✔
1565
                if (path_startswith(s[i].path, s[m].path))
6,700✔
1566
                        sub_mount_clear(s + i);
1,227✔
1567
                else
1568
                        m = i;
1569
        }
1570
}
2,480✔
1571

1572
int get_sub_mounts(const char *prefix, SubMount **ret_mounts, size_t *ret_n_mounts) {
2,480✔
1573

1574
        _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
2,480✔
1575
        _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
2,480✔
1576
        SubMount *mounts = NULL;
2,480✔
1577
        size_t n = 0;
2,480✔
1578
        int r;
2,480✔
1579

1580
        CLEANUP_ARRAY(mounts, n, sub_mount_array_free);
2,480✔
1581

1582
        assert(prefix);
2,480✔
1583
        assert(ret_mounts);
2,480✔
1584
        assert(ret_n_mounts);
2,480✔
1585

1586
        r = libmount_parse_mountinfo(/* source = */ NULL, &table, &iter);
2,480✔
1587
        if (r < 0)
2,480✔
1588
                return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
×
1589

1590
        for (;;) {
135,977✔
1591
                _cleanup_close_ int mount_fd = -EBADF;
133,497✔
1592
                _cleanup_free_ char *p = NULL;
135,977✔
1593
                struct libmnt_fs *fs;
135,977✔
1594
                const char *path;
135,977✔
1595
                int id1, id2;
135,977✔
1596

1597
                r = mnt_table_next_fs(table, iter, &fs);
135,977✔
1598
                if (r == 1)
135,977✔
1599
                        break; /* EOF */
1600
                if (r < 0)
133,497✔
1601
                        return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
×
1602

1603
                path = mnt_fs_get_target(fs);
133,497✔
1604
                if (!path)
133,497✔
1605
                        continue;
×
1606

1607
                if (isempty(path_startswith(path, prefix)))
133,497✔
1608
                        continue;
123,635✔
1609

1610
                id1 = mnt_fs_get_id(fs);
9,862✔
1611
                r = path_get_mnt_id(path, &id2);
9,862✔
1612
                if (r < 0) {
9,862✔
1613
                        log_debug_errno(r, "Failed to get mount ID of '%s', ignoring: %m", path);
×
1614
                        continue;
×
1615
                }
1616
                if (id1 != id2) {
9,862✔
1617
                        /* The path may be hidden by another over-mount or already remounted. */
1618
                        log_debug("The mount IDs of '%s' obtained by libmount and path_get_mnt_id() are different (%i vs %i), ignoring.",
1,224✔
1619
                                  path, id1, id2);
1620
                        continue;
1,224✔
1621
                }
1622

1623
                mount_fd = open(path, O_CLOEXEC|O_PATH);
8,638✔
1624
                if (mount_fd < 0) {
8,638✔
1625
                        if (errno == ENOENT) /* The path may be hidden by another over-mount or already unmounted. */
×
1626
                                continue;
×
1627

1628
                        return log_debug_errno(errno, "Failed to open subtree of mounted filesystem '%s': %m", path);
×
1629
                }
1630

1631
                p = strdup(path);
8,638✔
1632
                if (!p)
8,638✔
1633
                        return log_oom_debug();
×
1634

1635
                if (!GREEDY_REALLOC(mounts, n + 1))
8,638✔
1636
                        return log_oom_debug();
×
1637

1638
                mounts[n++] = (SubMount) {
8,638✔
1639
                        .path = TAKE_PTR(p),
8,638✔
1640
                        .mount_fd = TAKE_FD(mount_fd),
8,638✔
1641
                };
1642
        }
1643

1644
        typesafe_qsort(mounts, n, sub_mount_compare);
2,480✔
1645
        sub_mount_drop(mounts, n);
2,480✔
1646

1647
        *ret_mounts = TAKE_PTR(mounts);
2,480✔
1648
        *ret_n_mounts = n;
2,480✔
1649
        return 0;
2,480✔
1650
}
1651

1652
int bind_mount_submounts(
1,942✔
1653
                const char *source,
1654
                const char *target) {
1655

1656
        SubMount *mounts = NULL;
1,942✔
1657
        size_t n = 0;
1,942✔
1658
        int ret = 0, r;
1,942✔
1659

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

1663
        assert(source);
1,942✔
1664
        assert(target);
1,942✔
1665

1666
        CLEANUP_ARRAY(mounts, n, sub_mount_array_free);
1,942✔
1667

1668
        r = get_sub_mounts(source, &mounts, &n);
1,942✔
1669
        if (r < 0)
1,942✔
1670
                return r;
1671

1672
        FOREACH_ARRAY(m, mounts, n) {
10,576✔
1673
                _cleanup_free_ char *t = NULL;
8,634✔
1674
                const char *suffix;
8,634✔
1675

1676
                if (isempty(m->path))
8,634✔
1677
                        continue;
1,227✔
1678

1679
                assert_se(suffix = path_startswith(m->path, source));
7,407✔
1680

1681
                t = path_join(target, suffix);
7,407✔
1682
                if (!t)
7,407✔
1683
                        return -ENOMEM;
×
1684

1685
                r = path_is_mount_point(t);
7,407✔
1686
                if (r < 0) {
7,407✔
1687
                        log_debug_errno(r, "Failed to detect if '%s' already is a mount point, ignoring: %m", t);
7✔
1688
                        continue;
7✔
1689
                }
1690
                if (r > 0) {
7,400✔
1691
                        log_debug("Not bind mounting '%s' from '%s' to '%s', since there's already a mountpoint.", suffix, source, target);
×
1692
                        continue;
×
1693
                }
1694

1695
                r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(m->mount_fd), t, NULL, MS_BIND|MS_REC, NULL);
7,400✔
1696
                if (r < 0 && ret == 0)
7,400✔
1697
                        ret = r;
611✔
1698
        }
1699

1700
        return ret;
1701
}
1702

1703
int make_mount_point_inode_from_mode(int dir_fd, const char *dest, mode_t source_mode, mode_t target_mode) {
1,433✔
1704
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
1,433✔
1705
        assert(dest);
1,433✔
1706

1707
        if (S_ISDIR(source_mode))
1,433✔
1708
                return mkdirat_label(dir_fd, dest, target_mode & 07777);
1,416✔
1709
        else
1710
                return RET_NERRNO(mknodat(dir_fd, dest, S_IFREG|(target_mode & 07666), 0)); /* Mask off X bit */
18✔
1711
}
1712

1713
int make_mount_point_inode_from_path(const char *source, const char *dest, mode_t access_mode) {
889✔
1714
        struct stat st;
889✔
1715

1716
        assert(source);
889✔
1717
        assert(dest);
889✔
1718

1719
        if (stat(source, &st) < 0)
889✔
1720
                return -errno;
×
1721

1722
        return make_mount_point_inode_from_mode(AT_FDCWD, dest, st.st_mode, access_mode);
889✔
1723
}
1724

1725
int trigger_automount_at(int dir_fd, const char *path) {
384✔
1726
        _cleanup_free_ char *nested = NULL;
768✔
1727

1728
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
384✔
1729

1730
        nested = path_join(path, "a");
384✔
1731
        if (!nested)
384✔
1732
                return -ENOMEM;
1733

1734
        (void) faccessat(dir_fd, nested, F_OK, 0);
384✔
1735

1736
        return 0;
384✔
1737
}
1738

1739
unsigned long credentials_fs_mount_flags(bool ro) {
4,888✔
1740
        /* A tight set of mount flags for credentials mounts */
1741
        return MS_NODEV|MS_NOEXEC|MS_NOSUID|ms_nosymfollow_supported()|(ro ? MS_RDONLY : 0);
4,888✔
1742
}
1743

1744
int mount_credentials_fs(const char *path, size_t size, bool ro) {
2,444✔
1745
        _cleanup_free_ char *opts = NULL;
2,444✔
1746
        int r, noswap_supported;
2,444✔
1747

1748
        /* Mounts a file system we can place credentials in, i.e. with tight access modes right from the
1749
         * beginning, and ideally swapping turned off. In order of preference:
1750
         *
1751
         *      1. tmpfs if it supports "noswap"
1752
         *      2. ramfs
1753
         *      3. tmpfs if it doesn't support "noswap"
1754
         */
1755

1756
        noswap_supported = mount_option_supported("tmpfs", "noswap", NULL); /* Check explicitly to avoid kmsg noise */
2,444✔
1757
        if (noswap_supported > 0) {
2,444✔
1758
                _cleanup_free_ char *noswap_opts = NULL;
2,443✔
1759

1760
                if (asprintf(&noswap_opts, "mode=0700,nr_inodes=1024,size=%zu,noswap", size) < 0)
2,443✔
1761
                        return -ENOMEM;
1762

1763
                /* Best case: tmpfs with noswap (needs kernel >= 6.3) */
1764

1765
                r = mount_nofollow_verbose(
2,443✔
1766
                                LOG_DEBUG,
1767
                                "tmpfs",
1768
                                path,
1769
                                "tmpfs",
1770
                                credentials_fs_mount_flags(ro),
1771
                                noswap_opts);
1772
                if (r >= 0)
2,443✔
1773
                        return r;
1774
        }
1775

1776
        r = mount_nofollow_verbose(
1✔
1777
                        LOG_DEBUG,
1778
                        "ramfs",
1779
                        path,
1780
                        "ramfs",
1781
                        credentials_fs_mount_flags(ro),
1782
                        "mode=0700");
1783
        if (r >= 0)
1✔
1784
                return r;
1785

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

1789
        return mount_nofollow_verbose(
2,444✔
1790
                        LOG_DEBUG,
1791
                        "tmpfs",
1792
                        path,
1793
                        "tmpfs",
1794
                        credentials_fs_mount_flags(ro),
1795
                        opts);
1796
}
1797

1798
int make_fsmount(
2✔
1799
                int error_log_level,
1800
                const char *what,
1801
                const char *type,
1802
                unsigned long flags,
1803
                const char *options,
1804
                int userns_fd) {
1805

1806
        _cleanup_close_ int fs_fd = -EBADF, mnt_fd = -EBADF;
2✔
1807
        _cleanup_free_ char *o = NULL;
2✔
1808
        unsigned long f;
2✔
1809
        int r;
2✔
1810

1811
        assert(type);
2✔
1812
        assert(what);
2✔
1813

1814
        r = mount_option_mangle(options, flags, &f, &o);
2✔
1815
        if (r < 0)
2✔
1816
                return log_full_errno(
×
1817
                                error_log_level, r, "Failed to mangle mount options %s: %m",
1818
                                strempty(options));
1819

1820
        if (DEBUG_LOGGING) {
2✔
1821
                _cleanup_free_ char *fl = NULL;
2✔
1822
                (void) mount_flags_to_string(f, &fl);
2✔
1823

1824
                log_debug("Creating mount fd for %s (%s) (%s \"%s\")...",
4✔
1825
                        strna(what), strna(type), strnull(fl), strempty(o));
1826
        }
1827

1828
        fs_fd = fsopen(type, FSOPEN_CLOEXEC);
2✔
1829
        if (fs_fd < 0)
2✔
1830
                return log_full_errno(error_log_level, errno, "Failed to open superblock for \"%s\": %m", type);
×
1831

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

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

1839
        for (const char *p = o;;) {
2✔
1840
                _cleanup_free_ char *word = NULL;
×
1841
                char *eq;
2✔
1842

1843
                r = extract_first_word(&p, &word, ",", EXTRACT_KEEP_QUOTE);
2✔
1844
                if (r < 0)
2✔
1845
                        return log_full_errno(error_log_level, r, "Failed to parse mount option string \"%s\": %m", o);
×
1846
                if (r == 0)
2✔
1847
                        break;
1848

1849
                eq = strchr(word, '=');
×
1850
                if (eq) {
×
1851
                        *eq = 0;
×
1852
                        eq++;
×
1853

1854
                        if (fsconfig(fs_fd, FSCONFIG_SET_STRING, word, eq, 0) < 0)
×
1855
                                return log_full_errno(error_log_level, errno, "Failed to set mount option \"%s=%s\" for \"%s\": %m", word, eq, type);
×
1856
                } else {
1857
                        if (fsconfig(fs_fd, FSCONFIG_SET_FLAG, word, NULL, 0) < 0)
×
1858
                                return log_full_errno(error_log_level, errno, "Failed to set mount flag \"%s\" for \"%s\": %m", word, type);
×
1859
                }
1860
        }
1861

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

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

1869
        if (mount_setattr(mnt_fd, "", AT_EMPTY_PATH|AT_RECURSIVE,
2✔
1870
                          &(struct mount_attr) {
4✔
1871
                                  .attr_set = ms_flags_to_mount_attr(f) | (userns_fd >= 0 ? MOUNT_ATTR_IDMAP : 0),
4✔
1872
                                  .userns_fd = userns_fd,
1873
                          }, MOUNT_ATTR_SIZE_VER0) < 0)
1874
                return log_full_errno(error_log_level,
×
1875
                                      errno,
1876
                                      "Failed to set mount flags for \"%s\" (\"%s\"): %m",
1877
                                      what,
1878
                                      type);
1879

1880
        return TAKE_FD(mnt_fd);
2✔
1881
}
1882

1883
char* umount_and_unlink_and_free(char *p) {
1✔
1884
        if (!p)
1✔
1885
                return NULL;
1✔
1886

1887
        PROTECT_ERRNO;
2✔
1888
        (void) umount2(p, 0);
1✔
1889
        (void) unlink(p);
1✔
1890
        return mfree(p);
1✔
1891
}
1892

1893
static int path_get_mount_info_at(
614✔
1894
                int dir_fd,
1895
                const char *path,
1896
                char **ret_fstype,
1897
                char **ret_options) {
1898

1899
        _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
614✔
1900
        _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
614✔
1901
        int r, mnt_id;
614✔
1902

1903
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
614✔
1904

1905
        r = path_get_mnt_id_at(dir_fd, path, &mnt_id);
614✔
1906
        if (r < 0)
614✔
1907
                return log_debug_errno(r, "Failed to get mount ID: %m");
×
1908

1909
        /* When getting options is requested, we also need to parse utab, otherwise userspace options like
1910
         * "_netdev" will be lost. */
1911
        if (ret_options)
614✔
1912
                r = libmount_parse_with_utab(&table, &iter);
614✔
1913
        else
1914
                r = libmount_parse_mountinfo(/* source = */ NULL, &table, &iter);
×
1915
        if (r < 0)
614✔
1916
                return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
×
1917

1918
        for (;;) {
2,826✔
1919
                struct libmnt_fs *fs;
1,720✔
1920

1921
                r = mnt_table_next_fs(table, iter, &fs);
1,720✔
1922
                if (r == 1)
1,720✔
1923
                        break; /* EOF */
1924
                if (r < 0)
1,720✔
1925
                        return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
614✔
1926

1927
                if (mnt_fs_get_id(fs) != mnt_id)
1,720✔
1928
                        continue;
1,106✔
1929

1930
                _cleanup_free_ char *fstype = NULL, *options = NULL;
614✔
1931

1932
                if (ret_fstype) {
614✔
1933
                        fstype = strdup(strempty(mnt_fs_get_fstype(fs)));
614✔
1934
                        if (!fstype)
614✔
1935
                                return log_oom_debug();
×
1936
                }
1937

1938
                if (ret_options) {
614✔
1939
                        options = strdup(strempty(mnt_fs_get_options(fs)));
614✔
1940
                        if (!options)
614✔
1941
                                return log_oom_debug();
×
1942
                }
1943

1944
                if (ret_fstype)
614✔
1945
                        *ret_fstype = TAKE_PTR(fstype);
614✔
1946
                if (ret_options)
614✔
1947
                        *ret_options = TAKE_PTR(options);
614✔
1948

1949
                return 0;
1950
        }
1951

1952
        return log_debug_errno(SYNTHETIC_ERRNO(ESTALE), "Cannot find mount ID %i from /proc/self/mountinfo.", mnt_id);
614✔
1953
}
1954

1955
int path_is_network_fs_harder_at(int dir_fd, const char *path) {
632✔
1956
        _cleanup_close_ int fd = -EBADF;
632✔
1957
        int r;
632✔
1958

1959
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
632✔
1960

1961
        fd = xopenat(dir_fd, path, O_PATH | O_CLOEXEC | O_NOFOLLOW);
632✔
1962
        if (fd < 0)
632✔
1963
                return fd;
1964

1965
        r = fd_is_network_fs(fd);
614✔
1966
        if (r != 0)
614✔
1967
                return r;
1968

1969
        _cleanup_free_ char *fstype = NULL, *options = NULL;
614✔
1970
        r = path_get_mount_info_at(fd, /* path = */ NULL, &fstype, &options);
614✔
1971
        if (r < 0)
614✔
1972
                return r;
1973

1974
        if (fstype_is_network(fstype))
614✔
1975
                return true;
1976

1977
        if (fstab_test_option(options, "_netdev\0"))
614✔
1978
                return true;
×
1979

1980
        return false;
1981
}
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