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

systemd / systemd / 13610116813

01 Mar 2025 03:22PM UTC coverage: 71.857% (+0.04%) from 71.822%
13610116813

push

github

DaanDeMeyer
Add a few more bypass environment variables

When we're building ParticleOS images, we don't want the package
manager (or mkosi) to run systemd-sysusers, systemd-tmpfiles or
systemctl preset so let's add a few more bypass environment
variables that we can set to have execution of these skipped like
we already have $SYSTEMD_HWDB_UPDATE_BYPASS and $KERNEL_INSTALL_BYPASS.

12 of 16 new or added lines in 7 files covered. (75.0%)

4203 existing lines in 46 files now uncovered.

294791 of 410246 relevant lines covered (71.86%)

717494.72 hits per line

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

65.06
/src/basic/mountpoint-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <errno.h>
4
#include <fcntl.h>
5
#include <sys/mount.h>
6
#if WANT_LINUX_FS_H
7
#include <linux/fs.h>
8
#endif
9

10
#include "alloc-util.h"
11
#include "chase.h"
12
#include "fd-util.h"
13
#include "fileio.h"
14
#include "filesystems.h"
15
#include "fs-util.h"
16
#include "missing_fcntl.h"
17
#include "missing_fs.h"
18
#include "missing_mount.h"
19
#include "missing_syscall.h"
20
#include "mkdir.h"
21
#include "mountpoint-util.h"
22
#include "nulstr-util.h"
23
#include "parse-util.h"
24
#include "path-util.h"
25
#include "stat-util.h"
26
#include "stdio-util.h"
27
#include "strv.h"
28
#include "user-util.h"
29

30
/* This is the original MAX_HANDLE_SZ definition from the kernel, when the API was introduced. We use that in place of
31
 * any more currently defined value to future-proof things: if the size is increased in the API headers, and our code
32
 * is recompiled then it would cease working on old kernels, as those refuse any sizes larger than this value with
33
 * EINVAL right-away. Hence, let's disconnect ourselves from any such API changes, and stick to the original definition
34
 * from when it was introduced. We use it as a start value only anyway (see below), and hence should be able to deal
35
 * with large file handles anyway. */
36
#define ORIGINAL_MAX_HANDLE_SZ 128
37

38
bool is_name_to_handle_at_fatal_error(int err) {
11,992✔
39
        /* name_to_handle_at() can return "acceptable" errors that are due to the context. For example
40
         * the file system does not support name_to_handle_at() (EOPNOTSUPP), or the syscall was blocked
41
         * (EACCES/EPERM; maybe through seccomp, because we are running inside of a container), or
42
         * the mount point is not triggered yet (EOVERFLOW, think autofs+nfs4), or some general name_to_handle_at()
43
         * flakiness (EINVAL). However other errors are not supposed to happen and therefore are considered
44
         * fatal ones. */
45

46
        assert(err < 0);
11,992✔
47

48
        if (ERRNO_IS_NEG_NOT_SUPPORTED(err))
11,992✔
49
                return false;
UNCOV
50
        if (ERRNO_IS_NEG_PRIVILEGE(err))
×
51
                return false;
52

UNCOV
53
        return !IN_SET(err, -EOVERFLOW, -EINVAL);
×
54
}
55

56
int name_to_handle_at_loop(
27,433✔
57
                int fd,
58
                const char *path,
59
                struct file_handle **ret_handle,
60
                int *ret_mnt_id,
61
                int flags) {
62

63
        size_t n = ORIGINAL_MAX_HANDLE_SZ;
27,433✔
64

65
        assert(fd >= 0 || fd == AT_FDCWD);
27,433✔
66
        assert((flags & ~(AT_SYMLINK_FOLLOW|AT_EMPTY_PATH|AT_HANDLE_FID)) == 0);
27,433✔
67

68
        /* We need to invoke name_to_handle_at() in a loop, given that it might return EOVERFLOW when the specified
69
         * buffer is too small. Note that in contrast to what the docs might suggest, MAX_HANDLE_SZ is only good as a
70
         * start value, it is not an upper bound on the buffer size required.
71
         *
72
         * This improves on raw name_to_handle_at() also in one other regard: ret_handle and ret_mnt_id can be passed
73
         * as NULL if there's no interest in either. */
74

75
        for (;;) {
27,433✔
UNCOV
76
                _cleanup_free_ struct file_handle *h = NULL;
×
77
                int mnt_id = -1;
27,433✔
78

79
                h = malloc0(offsetof(struct file_handle, f_handle) + n);
27,433✔
80
                if (!h)
27,433✔
81
                        return -ENOMEM;
82

83
                h->handle_bytes = n;
27,433✔
84

85
                if (name_to_handle_at(fd, strempty(path), h, &mnt_id, flags) >= 0) {
54,857✔
86

87
                        if (ret_handle)
27,433✔
88
                                *ret_handle = TAKE_PTR(h);
27,433✔
89

90
                        if (ret_mnt_id)
27,433✔
91
                                *ret_mnt_id = mnt_id;
27,433✔
92

93
                        return 0;
27,433✔
94
                }
UNCOV
95
                if (errno != EOVERFLOW)
×
96
                        return -errno;
×
97

UNCOV
98
                if (!ret_handle && ret_mnt_id && mnt_id >= 0) {
×
99

100
                        /* As it appears, name_to_handle_at() fills in mnt_id even when it returns EOVERFLOW when the
101
                         * buffer is too small, but that's undocumented. Hence, let's make use of this if it appears to
102
                         * be filled in, and the caller was interested in only the mount ID an nothing else. */
103

UNCOV
104
                        *ret_mnt_id = mnt_id;
×
105
                        return 0;
×
106
                }
107

108
                /* If name_to_handle_at() didn't increase the byte size, then this EOVERFLOW is caused by
109
                 * something else (apparently EOVERFLOW is returned for untriggered nfs4 autofs mounts
110
                 * sometimes), not by the too small buffer. In that case propagate EOVERFLOW */
UNCOV
111
                if (h->handle_bytes <= n)
×
112
                        return -EOVERFLOW;
113

114
                /* The buffer was too small. Size the new buffer by what name_to_handle_at() returned. */
UNCOV
115
                n = h->handle_bytes;
×
116

117
                /* paranoia: check for overflow (note that .handle_bytes is unsigned only) */
UNCOV
118
                if (n > UINT_MAX - offsetof(struct file_handle, f_handle))
×
119
                        return -EOVERFLOW;
120
        }
121
}
122

123
int name_to_handle_at_try_fid(
27,433✔
124
                int fd,
125
                const char *path,
126
                struct file_handle **ret_handle,
127
                int *ret_mnt_id,
128
                int flags) {
129

130
        int r;
27,433✔
131

132
        assert(fd >= 0 || fd == AT_FDCWD);
27,433✔
133

134
        /* First issues name_to_handle_at() with AT_HANDLE_FID. If this fails and this is not a fatal error
135
         * we'll try without the flag, in order to support older kernels that didn't have AT_HANDLE_FID
136
         * (i.e. older than Linux 6.5). */
137

138
        r = name_to_handle_at_loop(fd, path, ret_handle, ret_mnt_id, flags | AT_HANDLE_FID);
27,433✔
139
        if (r >= 0 || is_name_to_handle_at_fatal_error(r))
27,433✔
140
                return r;
27,433✔
141

UNCOV
142
        return name_to_handle_at_loop(fd, path, ret_handle, ret_mnt_id, flags & ~AT_HANDLE_FID);
×
143
}
144

UNCOV
145
static int fd_fdinfo_mnt_id(int fd, const char *filename, int flags, int *ret_mnt_id) {
×
146
        char path[STRLEN("/proc/self/fdinfo/") + DECIMAL_STR_MAX(int)];
×
147
        _cleanup_free_ char *fdinfo = NULL;
×
148
        _cleanup_close_ int subfd = -EBADF;
×
149
        int r;
×
150

UNCOV
151
        assert((flags & ~(AT_SYMLINK_FOLLOW|AT_EMPTY_PATH)) == 0);
×
152
        assert(ret_mnt_id);
×
153

UNCOV
154
        if ((flags & AT_EMPTY_PATH) && isempty(filename))
×
155
                xsprintf(path, "/proc/self/fdinfo/%i", fd);
×
156
        else {
UNCOV
157
                subfd = openat(fd, filename, O_CLOEXEC|O_PATH|(flags & AT_SYMLINK_FOLLOW ? 0 : O_NOFOLLOW));
×
158
                if (subfd < 0)
×
159
                        return -errno;
×
160

UNCOV
161
                xsprintf(path, "/proc/self/fdinfo/%i", subfd);
×
162
        }
163

UNCOV
164
        r = read_full_virtual_file(path, &fdinfo, NULL);
×
165
        if (r == -ENOENT)
×
166
                return proc_fd_enoent_errno();
×
167
        if (r < 0)
×
168
                return r;
169

UNCOV
170
        char *p = find_line_startswith(fdinfo, "mnt_id:");
×
171
        if (!p)
×
172
                return -EBADMSG;
173

UNCOV
174
        p = skip_leading_chars(p, /* bad = */ NULL);
×
175
        p[strcspn(p, WHITESPACE)] = 0;
×
176

UNCOV
177
        return safe_atoi(p, ret_mnt_id);
×
178
}
179

180
static bool filename_possibly_with_slash_suffix(const char *s) {
129,838✔
181
        const char *slash, *copied;
129,838✔
182

183
        /* Checks whether the specified string is either file name, or a filename with a suffix of
184
         * slashes. But nothing else.
185
         *
186
         * this is OK: foo, bar, foo/, bar/, foo//, bar///
187
         * this is not OK: "", "/", "/foo", "foo/bar", ".", ".." … */
188

189
        slash = strchr(s, '/');
129,838✔
190
        if (!slash)
129,838✔
191
                return filename_is_valid(s);
129,824✔
192

193
        if (slash - s > PATH_MAX) /* We want to allocate on the stack below, hence do a size check first */
14✔
194
                return false;
195

196
        if (slash[strspn(slash, "/")] != 0) /* Check that the suffix consist only of one or more slashes */
14✔
197
                return false;
198

199
        copied = strndupa_safe(s, slash - s);
10✔
200
        return filename_is_valid(copied);
10✔
201
}
202

203
bool file_handle_equal(const struct file_handle *a, const struct file_handle *b) {
13,712✔
204
        if (a == b)
13,712✔
205
                return true;
206
        if (!a != !b)
13,712✔
207
                return false;
208
        if (a->handle_type != b->handle_type)
13,712✔
209
                return false;
210

211
        return memcmp_nn(a->f_handle, a->handle_bytes, b->f_handle, b->handle_bytes) == 0;
13,698✔
212
}
213

214
int is_mount_point_at(int fd, const char *filename, int flags) {
129,999✔
215
        bool fd_is_self;
129,999✔
216
        int r;
129,999✔
217

218
        assert(fd >= 0 || fd == AT_FDCWD);
129,999✔
219
        assert((flags & ~AT_SYMLINK_FOLLOW) == 0);
129,999✔
220

221
        if (isempty(filename)) {
129,999✔
222
                if (fd == AT_FDCWD)
113✔
223
                        filename = ".";
224
                else {
225
                        /* If the file name is empty we'll see if the specified 'fd' is a mount point.
226
                         * That's only supported by statx(), or if the inode specified via 'fd' refers to a
227
                         * directory. Otherwise, we'll have to fail (ENOTDIR), because we have no kernel API
228
                         * to query the information we need. */
229
                        flags |= AT_EMPTY_PATH;
111✔
230
                        filename = "";
111✔
231
                }
232

233
                fd_is_self = true;
234
        } else if (STR_IN_SET(filename, ".", "./"))
129,886✔
235
                fd_is_self = true;
236
        else {
237
                /* Insist that the specified filename is actually a filename, and not a path, i.e. some inode
238
                 * further up or down the tree then immediately below the specified directory fd. */
239
                if (!filename_possibly_with_slash_suffix(filename))
129,838✔
240
                        return -EINVAL;
7✔
241

242
                fd_is_self = false;
243
        }
244

245
        /* First we will try statx()' STATX_ATTR_MOUNT_ROOT attribute, which is our ideal API, available
246
         * since kernel 5.8.
247
         *
248
         * If that fails, our second try is the name_to_handle_at() syscall, which tells us the mount id and
249
         * an opaque file "handle". It is not supported everywhere though (kernel compile-time option, not
250
         * all file systems are hooked up). If it works the mount id is usually good enough to tell us
251
         * whether something is a mount point.
252
         *
253
         * If that didn't work we will try to read the mount id from /proc/self/fdinfo/<fd>. This is almost
254
         * as good as name_to_handle_at(), however, does not return the opaque file handle. The opaque file
255
         * handle is pretty useful to detect the root directory, which we should always consider a mount
256
         * point. Hence we use this only as fallback.
257
         *
258
         * Note that traditionally the check is done via fstat()-based st_dev comparisons. However, various
259
         * file systems don't guarantee same st_dev across single fs anymore, e.g. unionfs exposes file systems
260
         * with a variety of st_dev reported. Also, btrfs subvolumes have different st_dev, even though
261
         * they aren't real mounts of their own. */
262

263
        struct statx sx = {}; /* explicitly initialize the struct to make msan silent. */
129,992✔
264
        if (statx(fd, filename,
129,992✔
265
                  at_flags_normalize_nofollow(flags) |
129,992✔
266
                  AT_NO_AUTOMOUNT |            /* don't trigger automounts – mounts are a local concept, hence no need to trigger automounts to determine STATX_ATTR_MOUNT_ROOT */
267
                  AT_STATX_DONT_SYNC,          /* don't go to the network for this – for similar reasons */
268
                  STATX_TYPE,
269
                  &sx) < 0)
270
                return -errno;
5,815✔
271

272
        if (FLAGS_SET(sx.stx_attributes_mask, STATX_ATTR_MOUNT_ROOT)) /* yay! */
124,177✔
273
                return FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT);
124,177✔
274

UNCOV
275
        _cleanup_free_ struct file_handle *h = NULL, *h_parent = NULL;
×
UNCOV
276
        int mount_id = -1, mount_id_parent = -1;
×
UNCOV
277
        bool nosupp = false;
×
278

UNCOV
279
        r = name_to_handle_at_try_fid(fd, filename, &h, &mount_id, flags);
×
UNCOV
280
        if (r < 0) {
×
281
                if (is_name_to_handle_at_fatal_error(r))
×
282
                        return r;
283
                if (!ERRNO_IS_NOT_SUPPORTED(r))
×
UNCOV
284
                        goto fallback_fdinfo;
×
285

286
                /* This file system does not support name_to_handle_at(), hence let's see if the upper fs
287
                 * supports it (in which case it is a mount point), otherwise fall back to the fdinfo logic. */
288
                nosupp = true;
289
        }
290

UNCOV
291
        if (fd_is_self)
×
UNCOV
292
                r = name_to_handle_at_try_fid(fd, "..", &h_parent, &mount_id_parent, 0); /* can't work for non-directories 😢 */
×
293
        else
UNCOV
294
                r = name_to_handle_at_try_fid(fd, "", &h_parent, &mount_id_parent, AT_EMPTY_PATH);
×
UNCOV
295
        if (r < 0) {
×
UNCOV
296
                if (is_name_to_handle_at_fatal_error(r))
×
297
                        return r;
298
                if (!ERRNO_IS_NOT_SUPPORTED(r))
×
UNCOV
299
                        goto fallback_fdinfo;
×
300
                if (nosupp)
×
301
                        /* Both the parent and the directory can't do name_to_handle_at() */
302
                        goto fallback_fdinfo;
×
303

304
                /* The parent can't do name_to_handle_at() but the directory we are
305
                 * interested in can?  If so, it must be a mount point. */
306
                return 1;
307
        }
308

309
        /* The parent can do name_to_handle_at() but the directory we are interested in can't? If
310
         * so, it must be a mount point. */
UNCOV
311
        if (nosupp)
×
312
                return 1;
313

314
        /* If the file handle for the directory we are interested in and its parent are identical,
315
         * we assume this is the root directory, which is a mount point. */
UNCOV
316
        if (file_handle_equal(h_parent, h))
×
317
                return 1;
318

UNCOV
319
        return mount_id != mount_id_parent;
×
320

UNCOV
321
fallback_fdinfo:
×
322
        r = fd_fdinfo_mnt_id(fd, filename, flags, &mount_id);
×
UNCOV
323
        if (r < 0)
×
324
                return r;
325

UNCOV
326
        if (fd_is_self)
×
327
                r = fd_fdinfo_mnt_id(fd, "..", 0, &mount_id_parent); /* can't work for non-directories 😢 */
×
328
        else
329
                r = fd_fdinfo_mnt_id(fd, "", AT_EMPTY_PATH, &mount_id_parent);
×
UNCOV
330
        if (r < 0)
×
331
                return r;
332

333
        if (mount_id != mount_id_parent)
×
334
                return 1;
335

336
        /* Hmm, so, the mount ids are the same. This leaves one special case though for the root file
337
         * system. For that, let's see if the parent directory has the same inode as we are interested
338
         * in. */
339

UNCOV
340
        struct stat a, b;
×
341

342
        /* yay for fstatat() taking a different set of flags than the other _at() above */
UNCOV
343
        if (fstatat(fd, filename, &a, at_flags_normalize_nofollow(flags)) < 0)
×
UNCOV
344
                return -errno;
×
345

346
        if (fd_is_self)
×
UNCOV
347
                r = fstatat(fd, "..", &b, 0);
×
348
        else
349
                r = fstatat(fd, "", &b, AT_EMPTY_PATH);
×
350
        if (r < 0)
×
UNCOV
351
                return -errno;
×
352

353
        /* A directory with same device and inode as its parent must be the root directory. Otherwise
354
         * not a mount point.
355
         *
356
         * NB: we avoid inode_same_at() here because it internally attempts name_to_handle_at_try_fid() first,
357
         * which is redundant. */
UNCOV
358
        return stat_inode_same(&a, &b);
×
359
}
360

361
/* flags can be AT_SYMLINK_FOLLOW or 0 */
362
int path_is_mount_point_full(const char *path, const char *root, int flags) {
41,071✔
363
        _cleanup_close_ int dfd = -EBADF;
41,071✔
364
        _cleanup_free_ char *fn = NULL;
41,071✔
365

366
        assert(path);
41,071✔
367
        assert((flags & ~AT_SYMLINK_FOLLOW) == 0);
41,071✔
368

369
        if (path_equal(path, "/"))
41,071✔
370
                return 1;
371

372
        /* we need to resolve symlinks manually, we can't just rely on is_mount_point_at() to do that for us;
373
         * if we have a structure like /bin -> /usr/bin/ and /usr is a mount point, then the parent that we
374
         * look at needs to be /usr, not /. */
375
        dfd = chase_and_open_parent(path, root,
41,064✔
376
                                    CHASE_TRAIL_SLASH|(FLAGS_SET(flags, AT_SYMLINK_FOLLOW) ? 0 : CHASE_NOFOLLOW),
41,064✔
377
                                    &fn);
378
        if (dfd < 0)
41,064✔
379
                return dfd;
380

381
        return is_mount_point_at(dfd, fn, flags);
40,814✔
382
}
383

UNCOV
384
int path_get_mnt_id_at_fallback(int dir_fd, const char *path, int *ret) {
×
UNCOV
385
        int r;
×
386

UNCOV
387
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
×
UNCOV
388
        assert(ret);
×
389

390
        r = name_to_handle_at_loop(dir_fd, path, NULL, ret, isempty(path) ? AT_EMPTY_PATH : 0);
×
391
        if (r >= 0 || is_name_to_handle_at_fatal_error(r))
×
UNCOV
392
                return r;
×
393

394
        return fd_fdinfo_mnt_id(dir_fd, path, isempty(path) ? AT_EMPTY_PATH : 0, ret);
×
395
}
396

397
int path_get_mnt_id_at(int dir_fd, const char *path, int *ret) {
10,894✔
398
        struct statx sx;
10,894✔
399

400
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
10,894✔
401
        assert(ret);
10,894✔
402

403
        if (statx(dir_fd,
21,788✔
404
                  strempty(path),
10,894✔
405
                  (isempty(path) ? AT_EMPTY_PATH : AT_SYMLINK_NOFOLLOW) |
10,894✔
406
                  AT_NO_AUTOMOUNT |    /* don't trigger automounts, mnt_id is a local concept */
407
                  AT_STATX_DONT_SYNC,  /* don't go to the network, mnt_id is a local concept */
408
                  STATX_MNT_ID,
409
                  &sx) < 0)
410
                return -errno;
1✔
411

412
        if (FLAGS_SET(sx.stx_mask, STATX_MNT_ID)) {
10,893✔
413
                *ret = sx.stx_mnt_id;
10,893✔
414
                return 0;
10,893✔
415
        }
416

UNCOV
417
        return path_get_mnt_id_at_fallback(dir_fd, path, ret);
×
418
}
419

420
bool fstype_is_network(const char *fstype) {
2,044✔
421
        const char *x;
2,044✔
422

423
        x = startswith(fstype, "fuse.");
2,044✔
424
        if (x)
2,044✔
UNCOV
425
                fstype = x;
×
426

427
        if (nulstr_contains(filesystem_sets[FILESYSTEM_SET_NETWORK].value, fstype))
2,044✔
428
                return true;
2,044✔
429

430
        /* Filesystems not present in the internal database */
431
        return STR_IN_SET(fstype,
2,040✔
432
                          "davfs",
433
                          "glusterfs",
434
                          "lustre",
435
                          "sshfs");
436
}
437

UNCOV
438
bool fstype_needs_quota(const char *fstype) {
×
439
       /* 1. quotacheck needs to be run for some filesystems after they are mounted
440
        *    if the filesystem was not unmounted cleanly.
441
        * 2. You may need to run quotaon to enable quota usage tracking and/or
442
        *    enforcement.
443
        * ext2     - needs 1) and 2)
444
        * ext3     - needs 2) if configured using usrjquota/grpjquota mount options
445
        * ext4     - needs 1) if created without journal, needs 2) if created without QUOTA
446
        *            filesystem feature
447
        * reiserfs - needs 2).
448
        * jfs      - needs 2)
449
        * f2fs     - needs 2) if configured using usrjquota/grpjquota/prjjquota mount options
450
        * xfs      - nothing needed
451
        * gfs2     - nothing needed
452
        * ocfs2    - nothing needed
453
        * btrfs    - nothing needed
454
        * for reference see filesystem and quota manpages */
UNCOV
455
        return STR_IN_SET(fstype,
×
456
                          "ext2",
457
                          "ext3",
458
                          "ext4",
459
                          "reiserfs",
460
                          "jfs",
461
                          "f2fs");
462
}
463

464
bool fstype_is_api_vfs(const char *fstype) {
40✔
465
        assert(fstype);
40✔
466

467
        const FilesystemSet *fs;
40✔
468
        FOREACH_ARGUMENT(fs,
181✔
469
                         filesystem_sets + FILESYSTEM_SET_BASIC_API,
470
                         filesystem_sets + FILESYSTEM_SET_AUXILIARY_API,
471
                         filesystem_sets + FILESYSTEM_SET_PRIVILEGED_API,
472
                         filesystem_sets + FILESYSTEM_SET_TEMPORARY)
473
                if (nulstr_contains(fs->value, fstype))
160✔
474
                    return true;
19✔
475

476
        /* Filesystems not present in the internal database */
477
        return STR_IN_SET(fstype,
21✔
478
                          "autofs",
479
                          "cpuset",
480
                          "devtmpfs");
481
}
482

483
bool fstype_is_blockdev_backed(const char *fstype) {
10✔
484
        const char *x;
10✔
485

486
        x = startswith(fstype, "fuse.");
10✔
487
        if (x)
10✔
UNCOV
488
                fstype = x;
×
489

490
        return !streq(fstype, "9p") && !fstype_is_network(fstype) && !fstype_is_api_vfs(fstype);
10✔
491
}
492

493
bool fstype_is_ro(const char *fstype) {
2,463✔
494
        /* All Linux file systems that are necessarily read-only */
495
        return STR_IN_SET(fstype,
2,463✔
496
                          "DM_verity_hash",
497
                          "cramfs",
498
                          "erofs",
499
                          "iso9660",
500
                          "squashfs");
501
}
502

503
bool fstype_can_discard(const char *fstype) {
4✔
504
        assert(fstype);
4✔
505

506
        /* Use a curated list as first check, to avoid calling fsopen() which might load kmods, which might
507
         * not be allowed in our MAC context. */
508
        if (STR_IN_SET(fstype, "btrfs", "f2fs", "ext4", "vfat", "xfs"))
4✔
509
                return true;
1✔
510

511
        /* On new kernels we can just ask the kernel */
512
        return mount_option_supported(fstype, "discard", NULL) > 0;
3✔
513
}
514

515
const char* fstype_norecovery_option(const char *fstype) {
162✔
516
        int r;
162✔
517

518
        assert(fstype);
162✔
519

520
        /* Use a curated list as first check, to avoid calling fsopen() which might load kmods, which might
521
         * not be allowed in our MAC context. */
522
        if (STR_IN_SET(fstype, "ext3", "ext4", "xfs"))
162✔
523
                return "norecovery";
17✔
524

525
        /* btrfs dropped support for the "norecovery" option in 6.8
526
         * (https://github.com/torvalds/linux/commit/a1912f712188291f9d7d434fba155461f1ebef66) and replaced
527
         * it with rescue=nologreplay, so we check for the new name first and fall back to checking for the
528
         * old name if the new name doesn't work. */
529
        if (streq(fstype, "btrfs")) {
145✔
UNCOV
530
                r = mount_option_supported(fstype, "rescue=nologreplay", NULL);
×
UNCOV
531
                if (r == -EAGAIN) {
×
UNCOV
532
                        log_debug_errno(r, "Failed to check for btrfs 'rescue=nologreplay' option, assuming old kernel with 'norecovery': %m");
×
UNCOV
533
                        return "norecovery";
×
534
                }
UNCOV
535
                if (r < 0)
×
UNCOV
536
                        log_debug_errno(r, "Failed to check for btrfs 'rescue=nologreplay' option, assuming it is not supported: %m");
×
UNCOV
537
                if (r > 0)
×
538
                        return "rescue=nologreplay";
539
        }
540

541
        /* On new kernels we can just ask the kernel */
542
        return mount_option_supported(fstype, "norecovery", NULL) > 0 ? "norecovery" : NULL;
145✔
543
}
544

545
bool fstype_can_fmask_dmask(const char *fstype) {
56✔
546
        assert(fstype);
56✔
547

548
        /* Use a curated list as first check, to avoid calling fsopen() which might load kmods, which might
549
         * not be allowed in our MAC context. If we don't know ourselves, on new kernels we can just ask the
550
         * kernel. */
551
        return streq(fstype, "vfat") || (mount_option_supported(fstype, "fmask", "0177") > 0 && mount_option_supported(fstype, "dmask", "0077") > 0);
56✔
552
}
553

554
bool fstype_can_uid_gid(const char *fstype) {
1✔
555
        /* All file systems that have a uid=/gid= mount option that fixates the owners of all files and
556
         * directories, current and future. Note that this does *not* ask the kernel via
557
         * mount_option_supported() here because the uid=/gid= setting of various file systems mean different
558
         * things: some apply it only to the root dir inode, others to all inodes in the file system. Thus we
559
         * maintain the curated list below. 😢 */
560

561
        return STR_IN_SET(fstype,
1✔
562
                          "adfs",
563
                          "exfat",
564
                          "fat",
565
                          "hfs",
566
                          "hpfs",
567
                          "iso9660",
568
                          "msdos",
569
                          "ntfs",
570
                          "vfat");
571
}
572

573
int dev_is_devtmpfs(void) {
298✔
574
        _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
298✔
575
        int mount_id, r;
298✔
576
        char *e;
298✔
577

578
        r = path_get_mnt_id("/dev", &mount_id);
298✔
579
        if (r < 0)
298✔
580
                return r;
581

582
        r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo);
298✔
583
        if (r == -ENOENT)
298✔
UNCOV
584
                return proc_mounted() > 0 ? -ENOENT : -ENOSYS;
×
585
        if (r < 0)
298✔
586
                return r;
587

588
        for (;;) {
11,113✔
589
                _cleanup_free_ char *line = NULL;
10,882✔
590
                int mid;
11,113✔
591

592
                r = read_line(proc_self_mountinfo, LONG_LINE_MAX, &line);
11,113✔
593
                if (r < 0)
11,113✔
594
                        return r;
595
                if (r == 0)
11,113✔
596
                        break;
597

598
                if (sscanf(line, "%i", &mid) != 1)
10,882✔
UNCOV
599
                        continue;
×
600

601
                if (mid != mount_id)
10,882✔
602
                        continue;
10,584✔
603

604
                e = strstrafter(line, " - ");
298✔
605
                if (!e)
298✔
UNCOV
606
                        continue;
×
607

608
                /* accept any name that starts with the currently expected type */
609
                if (startswith(e, "devtmpfs"))
298✔
610
                        return true;
611
        }
612

613
        return false;
231✔
614
}
615

616
static int mount_fd(
97,729✔
617
                const char *source,
618
                int target_fd,
619
                const char *filesystemtype,
620
                unsigned long mountflags,
621
                const void *data) {
622

623
        assert(target_fd >= 0);
97,729✔
624

625
        if (mount(source, FORMAT_PROC_FD_PATH(target_fd), filesystemtype, mountflags, data) < 0) {
97,729✔
626
                if (errno != ENOENT)
1,200✔
627
                        return -errno;
1,200✔
628

629
                /* ENOENT can mean two things: either that the source is missing, or that /proc/ isn't
630
                 * mounted. Check for the latter to generate better error messages. */
631
                if (proc_mounted() == 0)
595✔
632
                        return -ENOSYS;
633

634
                return -ENOENT;
595✔
635
        }
636

637
        return 0;
96,529✔
638
}
639

640
int mount_nofollow(
99,253✔
641
                const char *source,
642
                const char *target,
643
                const char *filesystemtype,
644
                unsigned long mountflags,
645
                const void *data) {
646

647
        _cleanup_close_ int fd = -EBADF;
99,253✔
648

649
        assert(target);
99,253✔
650

651
        /* In almost all cases we want to manipulate the mount table without following symlinks, hence
652
         * mount_nofollow() is usually the way to go. The only exceptions are environments where /proc/ is
653
         * not available yet, since we need /proc/self/fd/ for this logic to work. i.e. during the early
654
         * initialization of namespacing/container stuff where /proc is not yet mounted (and maybe even the
655
         * fs to mount) we can only use traditional mount() directly.
656
         *
657
         * Note that this disables following only for the final component of the target, i.e symlinks within
658
         * the path of the target are honoured, as are symlinks in the source path everywhere. */
659

660
        fd = open(target, O_PATH|O_CLOEXEC|O_NOFOLLOW);
99,253✔
661
        if (fd < 0)
99,253✔
662
                return -errno;
1,524✔
663

664
        return mount_fd(source, fd, filesystemtype, mountflags, data);
97,729✔
665
}
666

667
const char* mount_propagation_flag_to_string(unsigned long flags) {
9✔
668

669
        switch (flags & (MS_SHARED|MS_SLAVE|MS_PRIVATE)) {
9✔
670
        case 0:
671
                return "";
672
        case MS_SHARED:
1✔
673
                return "shared";
1✔
674
        case MS_SLAVE:
1✔
675
                return "slave";
1✔
676
        case MS_PRIVATE:
3✔
677
                return "private";
3✔
678
        }
679

UNCOV
680
        return NULL;
×
681
}
682

683
int mount_propagation_flag_from_string(const char *name, unsigned long *ret) {
8✔
684

685
        if (isempty(name))
8✔
686
                *ret = 0;
2✔
687
        else if (streq(name, "shared"))
6✔
688
                *ret = MS_SHARED;
1✔
689
        else if (streq(name, "slave"))
5✔
690
                *ret = MS_SLAVE;
1✔
691
        else if (streq(name, "private"))
4✔
692
                *ret = MS_PRIVATE;
2✔
693
        else
694
                return -EINVAL;
695
        return 0;
696
}
697

698
bool mount_propagation_flag_is_valid(unsigned long flag) {
2,819✔
699
        return IN_SET(flag, 0, MS_SHARED, MS_PRIVATE, MS_SLAVE);
2,819✔
700
}
701

702
bool mount_new_api_supported(void) {
6,914✔
703
        static int cache = -1;
6,914✔
704
        int r;
6,914✔
705

706
        if (cache >= 0)
6,914✔
707
                return cache;
1,757✔
708

709
        /* This is the newer API among the ones we use, so use it as boundary */
710
        r = RET_NERRNO(mount_setattr(-EBADF, NULL, 0, NULL, 0));
5,157✔
711
        if (r == 0 || ERRNO_IS_NOT_SUPPORTED(r)) /* This should return an error if it is working properly */
5,157✔
UNCOV
712
                return (cache = false);
×
713

714
        return (cache = true);
5,157✔
715
}
716

717
unsigned long ms_nosymfollow_supported(void) {
6,115✔
718
        _cleanup_close_ int fsfd = -EBADF, mntfd = -EBADF;
6,115✔
719
        static int cache = -1;
6,115✔
720

721
        /* Returns MS_NOSYMFOLLOW if it is supported, zero otherwise. */
722

723
        if (cache >= 0)
6,115✔
724
                return cache ? MS_NOSYMFOLLOW : 0;
3,189✔
725

726
        if (!mount_new_api_supported())
2,926✔
UNCOV
727
                goto not_supported;
×
728

729
        /* Checks if MS_NOSYMFOLLOW is supported (which was added in 5.10). We use the new mount API's
730
         * mount_setattr() call for that, which was added in 5.12, which is close enough. */
731

732
        fsfd = fsopen("tmpfs", FSOPEN_CLOEXEC);
2,926✔
733
        if (fsfd < 0) {
2,926✔
734
                if (ERRNO_IS_NOT_SUPPORTED(errno))
2✔
UNCOV
735
                        goto not_supported;
×
736

737
                log_debug_errno(errno, "Failed to open superblock context for tmpfs: %m");
2✔
738
                return 0;
2✔
739
        }
740

741
        if (fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) < 0) {
2,924✔
UNCOV
742
                if (ERRNO_IS_NOT_SUPPORTED(errno))
×
UNCOV
743
                        goto not_supported;
×
744

UNCOV
745
                log_debug_errno(errno, "Failed to create tmpfs superblock: %m");
×
UNCOV
746
                return 0;
×
747
        }
748

749
        mntfd = fsmount(fsfd, FSMOUNT_CLOEXEC, 0);
2,924✔
750
        if (mntfd < 0) {
2,924✔
UNCOV
751
                if (ERRNO_IS_NOT_SUPPORTED(errno))
×
UNCOV
752
                        goto not_supported;
×
753

754
                log_debug_errno(errno, "Failed to turn superblock fd into mount fd: %m");
×
755
                return 0;
×
756
        }
757

758
        if (mount_setattr(mntfd, "", AT_EMPTY_PATH|AT_RECURSIVE,
2,924✔
759
                          &(struct mount_attr) {
2,924✔
760
                                  .attr_set = MOUNT_ATTR_NOSYMFOLLOW,
761
                          }, sizeof(struct mount_attr)) < 0) {
UNCOV
762
                if (ERRNO_IS_NOT_SUPPORTED(errno))
×
763
                        goto not_supported;
×
764

UNCOV
765
                log_debug_errno(errno, "Failed to set MOUNT_ATTR_NOSYMFOLLOW mount attribute: %m");
×
766
                return 0;
×
767
        }
768

769
        cache = true;
2,924✔
770
        return MS_NOSYMFOLLOW;
2,924✔
771

UNCOV
772
not_supported:
×
UNCOV
773
        cache = false;
×
774
        return 0;
×
775
}
776

777
int mount_option_supported(const char *fstype, const char *key, const char *value) {
4,693✔
778
        _cleanup_close_ int fd = -EBADF;
4,693✔
779
        int r;
4,693✔
780

781
        /* Checks if the specified file system supports a mount option. Returns > 0 if it supports it, == 0 if
782
         * it does not. Return -EAGAIN if we can't determine it. And any other error otherwise. */
783

784
        assert(fstype);
4,693✔
785
        assert(key);
4,693✔
786

787
        fd = fsopen(fstype, FSOPEN_CLOEXEC);
4,693✔
788
        if (fd < 0)
4,693✔
789
                return log_debug_errno(errno, "Failed to open superblock context for '%s': %m", fstype);
1✔
790

791
        /* Various file systems support fs context only in recent kernels (e.g. btrfs). For older kernels
792
         * fsconfig() with FSCONFIG_SET_STRING/FSCONFIG_SET_FLAG never fail. Which sucks, because we want to
793
         * use it for testing support, after all. Let's hence do a check if the file system got converted yet
794
         * first. */
795
        if (fsconfig(fd, FSCONFIG_SET_FD, "adefinitelynotexistingmountoption", NULL, fd) < 0) {
4,692✔
796
                /* If FSCONFIG_SET_FD is not supported for the fs, then the file system was not converted to
797
                 * the new mount API yet. If it returns EINVAL the mount option doesn't exist, but the fstype
798
                 * is converted. */
799
                if (errno == EOPNOTSUPP)
4,692✔
800
                        return -EAGAIN; /* fs not converted to new mount API → don't know */
801
                if (errno != EINVAL)
4,690✔
UNCOV
802
                        return log_debug_errno(errno, "Failed to check if file system '%s' has been converted to new mount API: %m", fstype);
×
803

804
                /* So FSCONFIG_SET_FD worked, but the option didn't exist (we got EINVAL), this means the fs
805
                 * is converted. Let's now ask the actual question we wonder about. */
806
        } else
UNCOV
807
                return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), "FSCONFIG_SET_FD worked unexpectedly for '%s', whoa!", fstype);
×
808

809
        if (value)
4,690✔
810
                r = fsconfig(fd, FSCONFIG_SET_STRING, key, value, 0);
1,280✔
811
        else
812
                r = fsconfig(fd, FSCONFIG_SET_FLAG, key, NULL, 0);
3,410✔
813
        if (r < 0) {
4,690✔
814
                if (errno == EINVAL)
191✔
815
                        return false; /* EINVAL means option not supported. */
816

UNCOV
817
                return log_debug_errno(errno, "Failed to set '%s%s%s' on '%s' superblock context: %m",
×
818
                                       key, value ? "=" : "", strempty(value), fstype);
819
        }
820

821
        return true; /* works! */
822
}
823

824
bool path_below_api_vfs(const char *p) {
7,752✔
825
        assert(p);
7,752✔
826

827
        /* API VFS are either directly mounted on any of these three paths, or below it. */
828
        return PATH_STARTSWITH_SET(p, "/dev", "/sys", "/proc");
7,752✔
829
}
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