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

systemd / systemd / 13402545581

18 Feb 2025 11:10PM UTC coverage: 71.753% (-0.07%) from 71.822%
13402545581

push

github

bluca
obs: trigger systemd-suse instead of systemd-fedora

The suse recipe is the odd one out, so now fedora builds with
arch/debian/ubuntu and suse is in its own package

293534 of 409088 relevant lines covered (71.75%)

717819.06 hits per line

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

76.93
/src/nspawn/nspawn-mount.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <sys/mount.h>
4
#include <linux/magic.h>
5

6
#include "alloc-util.h"
7
#include "chase.h"
8
#include "escape.h"
9
#include "fd-util.h"
10
#include "format-util.h"
11
#include "fs-util.h"
12
#include "label-util.h"
13
#include "mkdir-label.h"
14
#include "mount-util.h"
15
#include "mountpoint-util.h"
16
#include "namespace-util.h"
17
#include "nspawn-mount.h"
18
#include "parse-util.h"
19
#include "path-util.h"
20
#include "rm-rf.h"
21
#include "set.h"
22
#include "sort-util.h"
23
#include "stat-util.h"
24
#include "string-util.h"
25
#include "strv.h"
26
#include "tmpfile-util.h"
27
#include "user-util.h"
28

29
CustomMount* custom_mount_add(CustomMount **l, size_t *n, CustomMountType t) {
632✔
30
        CustomMount *ret;
632✔
31

32
        assert(l);
632✔
33
        assert(n);
632✔
34
        assert(t >= 0);
632✔
35
        assert(t < _CUSTOM_MOUNT_TYPE_MAX);
632✔
36

37
        if (!GREEDY_REALLOC(*l, *n + 1))
632✔
38
                return NULL;
39

40
        ret = *l + *n;
632✔
41
        (*n)++;
632✔
42

43
        *ret = (CustomMount) {
632✔
44
                .type = t
45
        };
46

47
        return ret;
632✔
48
}
49

50
void custom_mount_free_all(CustomMount *l, size_t n) {
1,451✔
51
        FOREACH_ARRAY(m, l, n) {
1,769✔
52
                free(m->source);
318✔
53
                free(m->destination);
318✔
54
                free(m->options);
318✔
55

56
                if (m->work_dir) {
318✔
57
                        (void) rm_rf(m->work_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
3✔
58
                        free(m->work_dir);
3✔
59
                }
60

61
                if (m->rm_rf_tmpdir) {
318✔
62
                        (void) rm_rf(m->rm_rf_tmpdir, REMOVE_ROOT|REMOVE_PHYSICAL);
2✔
63
                        free(m->rm_rf_tmpdir);
2✔
64
                }
65

66
                strv_free(m->lower);
318✔
67
                free(m->type_argument);
318✔
68
        }
69

70
        free(l);
1,451✔
71
}
1,451✔
72

73
static int custom_mount_compare(const CustomMount *a, const CustomMount *b) {
103✔
74
        int r;
103✔
75

76
        r = path_compare(a->destination, b->destination);
103✔
77
        if (r != 0)
103✔
78
                return r;
79

80
        return CMP(a->type, b->type);
×
81
}
82

83
static int source_path_parse(const char *p, char **ret) {
599✔
84
        assert(p);
599✔
85
        assert(ret);
599✔
86

87
        if (isempty(p))
599✔
88
                return -EINVAL;
89

90
        if (*p == '+') {
599✔
91
                if (!path_is_absolute(p + 1))
7✔
92
                        return -EINVAL;
599✔
93

94
                char *s = strdup(p);
7✔
95
                if (!s)
7✔
96
                        return -ENOMEM;
97

98
                *ret = TAKE_PTR(s);
7✔
99
                return 0;
7✔
100
        }
101

102
        return path_make_absolute_cwd(p, ret);
592✔
103
}
104

105
static int source_path_parse_nullable(const char *p, char **ret) {
595✔
106
        assert(p);
595✔
107
        assert(ret);
595✔
108

109
        if (isempty(p)) {
595✔
110
                *ret = NULL;
7✔
111
                return 0;
7✔
112
        }
113

114
        return source_path_parse(p, ret);
588✔
115
}
116

117
static char *resolve_source_path(const char *dest, const char *source) {
447✔
118
        if (!source)
447✔
119
                return NULL;
120

121
        if (source[0] == '+')
447✔
122
                return path_join(dest, source + 1);
8✔
123

124
        return strdup(source);
439✔
125
}
126

127
static int allocate_temporary_source(CustomMount *m) {
8✔
128
        int r;
8✔
129

130
        assert(m);
8✔
131
        assert(!m->source);
8✔
132
        assert(!m->rm_rf_tmpdir);
8✔
133

134
        r = mkdtemp_malloc("/var/tmp/nspawn-temp-XXXXXX", &m->rm_rf_tmpdir);
8✔
135
        if (r < 0)
8✔
136
                return log_error_errno(r, "Failed to acquire temporary directory: %m");
×
137

138
        m->source = path_join(m->rm_rf_tmpdir, "src");
8✔
139
        if (!m->source)
8✔
140
                return log_oom();
×
141

142
        if (mkdir(m->source, 0755) < 0)
8✔
143
                return log_error_errno(errno, "Failed to create %s: %m", m->source);
×
144

145
        return 0;
146
}
147

148
int custom_mount_prepare_all(const char *dest, CustomMount *l, size_t n) {
448✔
149
        int r;
448✔
150

151
        /* Prepare all custom mounts. This will make sure we know all temporary directories. This is called in the
152
         * parent process, so that we know the temporary directories to remove on exit before we fork off the
153
         * children. */
154

155
        assert(l || n == 0);
448✔
156

157
        /* Order the custom mounts, and make sure we have a working directory */
158
        typesafe_qsort(l, n, custom_mount_compare);
448✔
159

160
        FOREACH_ARRAY(m, l, n) {
927✔
161
                /* /proc we mount in the inner child, i.e. when we acquired CLONE_NEWPID. All other mounts we mount
162
                 * already in the outer child, so that the mounts are already established before CLONE_NEWPID and in
163
                 * particular CLONE_NEWUSER. This also means any custom mounts below /proc also need to be mounted in
164
                 * the inner child, not the outer one. Determine this here. */
165
                m->in_userns = path_startswith(m->destination, "/proc");
479✔
166

167
                if (m->type == CUSTOM_MOUNT_BIND) {
479✔
168
                        if (m->source) {
435✔
169
                                char *s;
434✔
170

171
                                s = resolve_source_path(dest, m->source);
434✔
172
                                if (!s)
434✔
173
                                        return log_oom();
×
174

175
                                free_and_replace(m->source, s);
434✔
176
                        } else {
177
                                /* No source specified? In that case, use a throw-away temporary directory in /var/tmp */
178

179
                                r = allocate_temporary_source(m);
1✔
180
                                if (r < 0)
1✔
181
                                        return r;
182
                        }
183
                }
184

185
                if (m->type == CUSTOM_MOUNT_OVERLAY) {
479✔
186
                        STRV_FOREACH(j, m->lower) {
20✔
187
                                char *s;
11✔
188

189
                                s = resolve_source_path(dest, *j);
11✔
190
                                if (!s)
11✔
191
                                        return log_oom();
×
192

193
                                free_and_replace(*j, s);
11✔
194
                        }
195

196
                        if (m->source) {
9✔
197
                                char *s;
2✔
198

199
                                s = resolve_source_path(dest, m->source);
2✔
200
                                if (!s)
2✔
201
                                        return log_oom();
×
202

203
                                free_and_replace(m->source, s);
2✔
204
                        } else {
205
                                r = allocate_temporary_source(m);
7✔
206
                                if (r < 0)
7✔
207
                                        return r;
208
                        }
209

210
                        if (m->work_dir) {
9✔
211
                                char *s;
×
212

213
                                s = resolve_source_path(dest, m->work_dir);
×
214
                                if (!s)
×
215
                                        return log_oom();
×
216

217
                                free_and_replace(m->work_dir, s);
×
218
                        } else {
219
                                r = tempfn_random(m->source, NULL, &m->work_dir);
9✔
220
                                if (r < 0)
9✔
221
                                        return log_error_errno(r, "Failed to acquire working directory: %m");
×
222
                        }
223

224
                        (void) mkdir_label(m->work_dir, 0700);
9✔
225
                }
226
        }
227

228
        return 0;
229
}
230

231
int bind_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) {
586✔
232
        _cleanup_free_ char *source = NULL, *destination = NULL, *opts = NULL, *p = NULL;
586✔
233
        CustomMount *m;
586✔
234
        int r;
586✔
235

236
        assert(l);
586✔
237
        assert(n);
586✔
238

239
        r = extract_many_words(&s, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &source, &destination);
586✔
240
        if (r < 0)
586✔
241
                return r;
242
        if (r == 0)
586✔
243
                return -EINVAL;
244
        if (r == 1) {
586✔
245
                destination = strdup(source[0] == '+' ? source+1 : source);
558✔
246
                if (!destination)
558✔
247
                        return -ENOMEM;
248
        }
249
        if (r == 2 && !isempty(s)) {
586✔
250
                opts = strdup(s);
9✔
251
                if (!opts)
9✔
252
                        return -ENOMEM;
253
        }
254

255
        r = source_path_parse_nullable(source, &p);
586✔
256
        if (r < 0)
586✔
257
                return r;
258

259
        if (!path_is_absolute(destination))
1,168✔
260
                return -EINVAL;
261

262
        m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND);
582✔
263
        if (!m)
582✔
264
                return -ENOMEM;
265

266
        m->source = TAKE_PTR(p);
582✔
267
        m->destination = TAKE_PTR(destination);
582✔
268
        m->read_only = read_only;
582✔
269
        m->options = TAKE_PTR(opts);
582✔
270

271
        return 0;
582✔
272
}
273

274
int tmpfs_mount_parse(CustomMount **l, size_t *n, const char *s) {
17✔
275
        _cleanup_free_ char *path = NULL, *opts = NULL;
17✔
276
        const char *p = ASSERT_PTR(s);
17✔
277
        CustomMount *m;
17✔
278
        int r;
17✔
279

280
        assert(l);
17✔
281
        assert(n);
17✔
282

283
        r = extract_first_word(&p, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
17✔
284
        if (r < 0)
17✔
285
                return r;
286
        if (r == 0)
17✔
287
                return -EINVAL;
288

289
        if (isempty(p))
17✔
290
                opts = strdup("mode=0755");
16✔
291
        else
292
                opts = strdup(p);
1✔
293
        if (!opts)
17✔
294
                return -ENOMEM;
295

296
        if (!path_is_absolute(path))
32✔
297
                return -EINVAL;
298

299
        m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS);
15✔
300
        if (!m)
15✔
301
                return -ENOMEM;
302

303
        m->destination = TAKE_PTR(path);
15✔
304
        m->options = TAKE_PTR(opts);
15✔
305

306
        return 0;
15✔
307
}
308

309
int overlay_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) {
13✔
310
        _cleanup_free_ char *upper = NULL, *destination = NULL;
13✔
311
        _cleanup_strv_free_ char **lower = NULL;
13✔
312
        CustomMount *m;
13✔
313
        int r, k;
13✔
314

315
        k = strv_split_full(&lower, s, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
13✔
316
        if (k < 0)
13✔
317
                return k;
318
        if (k < 2)
13✔
319
                return -EADDRNOTAVAIL;
320
        if (k == 2) {
9✔
321
                _cleanup_free_ char *p = NULL;
×
322

323
                /* If two parameters are specified, the first one is the lower, the second one the upper directory. And
324
                 * we'll also define the destination mount point the same as the upper. */
325

326
                r = source_path_parse(lower[0], &p);
×
327
                if (r < 0)
×
328
                        return r;
329

330
                free_and_replace(lower[0], p);
×
331

332
                r = source_path_parse(lower[1], &p);
×
333
                if (r < 0)
×
334
                        return r;
335

336
                free_and_replace(lower[1], p);
×
337

338
                upper = TAKE_PTR(lower[1]);
×
339

340
                destination = strdup(upper[0] == '+' ? upper+1 : upper); /* take the destination without "+" prefix */
×
341
                if (!destination)
×
342
                        return -ENOMEM;
343
        } else {
344
                _cleanup_free_ char *p = NULL;
9✔
345

346
                /* If more than two parameters are specified, the last one is the destination, the second to last one
347
                 * the "upper", and all before that the "lower" directories. */
348

349
                destination = lower[k - 1];
9✔
350
                upper = TAKE_PTR(lower[k - 2]);
9✔
351

352
                STRV_FOREACH(i, lower) {
20✔
353
                        r = source_path_parse(*i, &p);
11✔
354
                        if (r < 0)
11✔
355
                                return r;
356

357
                        free_and_replace(*i, p);
11✔
358
                }
359

360
                /* If the upper directory is unspecified, then let's create it automatically as a throw-away directory
361
                 * in /var/tmp */
362
                r = source_path_parse_nullable(upper, &p);
9✔
363
                if (r < 0)
9✔
364
                        return r;
365

366
                free_and_replace(upper, p);
9✔
367

368
                if (!path_is_absolute(destination))
9✔
369
                        return -EINVAL;
370
        }
371

372
        m = custom_mount_add(l, n, CUSTOM_MOUNT_OVERLAY);
9✔
373
        if (!m)
9✔
374
                return -ENOMEM;
375

376
        m->destination = TAKE_PTR(destination);
9✔
377
        m->source = TAKE_PTR(upper);
9✔
378
        m->lower = TAKE_PTR(lower);
9✔
379
        m->read_only = read_only;
9✔
380

381
        return 0;
9✔
382
}
383

384
int inaccessible_mount_parse(CustomMount **l, size_t *n, const char *s) {
18✔
385
        _cleanup_free_ char *path = NULL;
18✔
386
        CustomMount *m;
18✔
387

388
        assert(l);
18✔
389
        assert(n);
18✔
390
        assert(s);
18✔
391

392
        if (!path_is_absolute(s))
18✔
393
                return -EINVAL;
394

395
        path = strdup(s);
16✔
396
        if (!path)
16✔
397
                return -ENOMEM;
398

399
        m = custom_mount_add(l, n, CUSTOM_MOUNT_INACCESSIBLE);
16✔
400
        if (!m)
16✔
401
                return -ENOMEM;
402

403
        m->destination = TAKE_PTR(path);
16✔
404
        return 0;
16✔
405
}
406

407
int tmpfs_patch_options(
1,307✔
408
                const char *options,
409
                uid_t uid_shift,
410
                const char *selinux_apifs_context,
411
                char **ret) {
412

413
        _cleanup_free_ char *buf = NULL;
1,307✔
414

415
        assert(ret);
1,307✔
416

417
        if (options) {
1,307✔
418
                buf = strdup(options);
1,307✔
419
                if (!buf)
1,307✔
420
                        return -ENOMEM;
421
        }
422

423
        if (uid_shift != UID_INVALID)
1,307✔
424
                if (strextendf_with_separator(&buf, ",", "uid=" UID_FMT ",gid=" UID_FMT, uid_shift, uid_shift) < 0)
1,271✔
425
                        return -ENOMEM;
426

427
#if HAVE_SELINUX
428
        if (selinux_apifs_context)
429
                if (strextendf_with_separator(&buf, ",", "context=\"%s\"", selinux_apifs_context) < 0)
430
                        return -ENOMEM;
431
#endif
432

433
        *ret = TAKE_PTR(buf);
1,307✔
434
        return !!*ret;
1,307✔
435
}
436

437
int mount_sysfs(const char *dest, MountSettingsMask mount_settings) {
135✔
438
        _cleanup_free_ char *top = NULL, *full = NULL;;
135✔
439
        unsigned long extra_flags = 0;
135✔
440
        int r;
135✔
441

442
        top = path_join(dest, "/sys");
135✔
443
        if (!top)
135✔
444
                return log_oom();
×
445

446
        r = path_is_mount_point(top);
135✔
447
        if (r < 0)
135✔
448
                return log_error_errno(r, "Failed to determine if '%s' is a mountpoint: %m", top);
×
449
        if (r == 0) {
135✔
450
                /* If this is not a mount point yet, then mount a tmpfs there */
451
                r = mount_nofollow_verbose(LOG_ERR, "tmpfs", top, "tmpfs", MS_NOSUID|MS_NOEXEC|MS_NODEV, "mode=0555" TMPFS_LIMITS_SYS);
1✔
452
                if (r < 0)
1✔
453
                        return r;
454
        } else {
455
                r = path_is_fs_type(top, SYSFS_MAGIC);
134✔
456
                if (r < 0)
134✔
457
                        return log_error_errno(r, "Failed to determine filesystem type of %s: %m", top);
×
458

459
                /* /sys/ might already be mounted as sysfs by the outer child in the !netns case. In this case, it's
460
                 * all good. Don't touch it because we don't have the right to do so, see
461
                 * https://github.com/systemd/systemd/issues/1555.
462
                 */
463
                if (r > 0)
134✔
464
                        return 0;
465
        }
466

467
        full = path_join(top, "/full");
70✔
468
        if (!full)
70✔
469
                return log_oom();
×
470

471
        if (mkdir(full, 0755) < 0 && errno != EEXIST)
70✔
472
                return log_error_errno(errno, "Failed to create directory '%s': %m", full);
×
473

474
        if (FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_RO))
70✔
475
                extra_flags |= MS_RDONLY;
62✔
476

477
        r = mount_nofollow_verbose(LOG_ERR, "sysfs", full, "sysfs",
70✔
478
                                   MS_NOSUID|MS_NOEXEC|MS_NODEV|extra_flags, NULL);
479
        if (r < 0)
70✔
480
                return r;
481

482
        FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") {
490✔
483
                _cleanup_free_ char *from = NULL, *to = NULL;
420✔
484

485
                from = path_join(full, x);
420✔
486
                if (!from)
420✔
487
                        return log_oom();
×
488

489
                to = path_join(top, x);
420✔
490
                if (!to)
420✔
491
                        return log_oom();
×
492

493
                (void) mkdir(to, 0755);
420✔
494

495
                r = mount_nofollow_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
420✔
496
                if (r < 0)
420✔
497
                        return r;
498

499
                r = mount_nofollow_verbose(LOG_ERR, NULL, to, NULL,
420✔
500
                                           MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
501
                if (r < 0)
420✔
502
                        return r;
503
        }
504

505
        r = umount_verbose(LOG_ERR, full, UMOUNT_NOFOLLOW);
70✔
506
        if (r < 0)
70✔
507
                return r;
508

509
        if (rmdir(full) < 0)
70✔
510
                return log_error_errno(errno, "Failed to remove %s: %m", full);
×
511

512
        /* Create mountpoint for cgroups. Otherwise we are not allowed since we remount /sys/ read-only. */
513
        _cleanup_free_ char *x = path_join(top, "/fs/cgroup");
140✔
514
        if (!x)
70✔
515
                return log_oom();
×
516

517
        (void) mkdir_p(x, 0755);
70✔
518

519
        return mount_nofollow_verbose(LOG_ERR, NULL, top, NULL,
70✔
520
                                      MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
521
}
522

523
#define PROC_DEFAULT_MOUNT_FLAGS (MS_NOSUID|MS_NOEXEC|MS_NODEV)
524
#define SYS_DEFAULT_MOUNT_FLAGS  (MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV)
525

526
int mount_all(const char *dest,
411✔
527
              MountSettingsMask mount_settings,
528
              uid_t uid_shift,
529
              const char *selinux_apifs_context) {
530

531
#define PROC_INACCESSIBLE_REG(path)                                     \
532
        { "/run/systemd/inaccessible/reg", (path), NULL, NULL, MS_BIND, \
533
          MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ \
534
        { NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \
535
          MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */
536

537
#define PROC_READ_ONLY(path)                                            \
538
        { (path), (path), NULL, NULL, MS_BIND,                          \
539
          MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ \
540
        { NULL,   (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \
541
          MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */
542

543
        typedef struct MountPoint {
411✔
544
                const char *what;
545
                const char *where;
546
                const char *type;
547
                const char *options;
548
                unsigned long flags;
549
                MountSettingsMask mount_settings;
550
        } MountPoint;
551

552
        static const MountPoint mount_table[] = {
411✔
553
                /* First we list inner child mounts (i.e. mounts applied *after* entering user namespacing when we are privileged) */
554
                { "proc",            "/proc",           "proc",  NULL,        PROC_DEFAULT_MOUNT_FLAGS,
555
                  MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_MKDIR|MOUNT_FOLLOW_SYMLINKS }, /* we follow symlinks here since not following them requires /proc/ already being mounted, which we don't have here. */
556

557
                { "/proc/sys",       "/proc/sys",       NULL,    NULL,        MS_BIND,
558
                  MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO },                          /* Bind mount first ... */
559

560
                { "/proc/sys/net",   "/proc/sys/net",   NULL,    NULL,        MS_BIND,
561
                  MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_APIVFS_NETNS }, /* (except for this) */
562

563
                { NULL,              "/proc/sys",       NULL,    NULL,        MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
564
                  MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO },                          /* ... then, make it r/o */
565

566
                /* Make these files inaccessible to container payloads: they potentially leak information about kernel
567
                 * internals or the host's execution environment to the container */
568
                PROC_INACCESSIBLE_REG("/proc/kallsyms"),
569
                PROC_INACCESSIBLE_REG("/proc/kcore"),
570
                PROC_INACCESSIBLE_REG("/proc/keys"),
571
                PROC_INACCESSIBLE_REG("/proc/sysrq-trigger"),
572
                PROC_INACCESSIBLE_REG("/proc/timer_list"),
573

574
                /* Make these directories read-only to container payloads: they show hardware information, and in some
575
                 * cases contain tunables the container really shouldn't have access to. */
576
                PROC_READ_ONLY("/proc/acpi"),
577
                PROC_READ_ONLY("/proc/apm"),
578
                PROC_READ_ONLY("/proc/asound"),
579
                PROC_READ_ONLY("/proc/bus"),
580
                PROC_READ_ONLY("/proc/fs"),
581
                PROC_READ_ONLY("/proc/irq"),
582
                PROC_READ_ONLY("/proc/scsi"),
583

584
                { "mqueue",                 "/dev/mqueue",                  "mqueue", NULL,                            MS_NOSUID|MS_NOEXEC|MS_NODEV,
585
                  MOUNT_IN_USERNS|MOUNT_MKDIR },
586

587
                /* Then we list outer child mounts (i.e. mounts applied *before* entering user namespacing when we are privileged) */
588
                { "tmpfs",                  "/tmp",                         "tmpfs", "mode=01777" NESTED_TMPFS_LIMITS, MS_NOSUID|MS_NODEV|MS_STRICTATIME,
589
                  MOUNT_FATAL|MOUNT_APPLY_TMPFS_TMP|MOUNT_MKDIR|MOUNT_USRQUOTA_GRACEFUL },
590
                { "tmpfs",                  "/sys",                         "tmpfs", "mode=0555" TMPFS_LIMITS_SYS,     MS_NOSUID|MS_NOEXEC|MS_NODEV,
591
                  MOUNT_FATAL|MOUNT_APPLY_APIVFS_NETNS|MOUNT_MKDIR|MOUNT_UNMANAGED },
592
                { "sysfs",                  "/sys",                         "sysfs", NULL,                             SYS_DEFAULT_MOUNT_FLAGS,
593
                  MOUNT_FATAL|MOUNT_APPLY_APIVFS_RO|MOUNT_MKDIR|MOUNT_UNMANAGED },    /* skipped if above was mounted */
594
                { "sysfs",                  "/sys",                         "sysfs", NULL,                             MS_NOSUID|MS_NOEXEC|MS_NODEV,
595
                  MOUNT_FATAL|MOUNT_MKDIR|MOUNT_UNMANAGED },                          /* skipped if above was mounted */
596
                { "tmpfs",                  "/dev",                         "tmpfs", "mode=0755" TMPFS_LIMITS_PRIVATE_DEV, MS_NOSUID|MS_STRICTATIME,
597
                  MOUNT_FATAL|MOUNT_MKDIR },
598
                { "tmpfs",                  "/dev/shm",                     "tmpfs", "mode=01777" NESTED_TMPFS_LIMITS, MS_NOSUID|MS_NODEV|MS_STRICTATIME,
599
                  MOUNT_FATAL|MOUNT_MKDIR|MOUNT_USRQUOTA_GRACEFUL },
600
                { "tmpfs",                  "/run",                         "tmpfs", "mode=0755" TMPFS_LIMITS_RUN,     MS_NOSUID|MS_NODEV|MS_STRICTATIME,
601
                  MOUNT_FATAL|MOUNT_MKDIR },
602
                { "/run/host",              "/run/host",                    NULL,    NULL,                             MS_BIND,
603
                  MOUNT_FATAL|MOUNT_MKDIR|MOUNT_PREFIX_ROOT }, /* Prepare this so that we can make it read-only when we are done */
604
                { "/etc/os-release",        "/run/host/os-release",         NULL,    NULL,                             MS_BIND,
605
                  MOUNT_TOUCH }, /* As per kernel interface requirements, bind mount first (creating mount points) and make read-only later */
606
                { "/usr/lib/os-release",    "/run/host/os-release",         NULL,    NULL,                             MS_BIND,
607
                  MOUNT_FATAL }, /* If /etc/os-release doesn't exist use the version in /usr/lib as fallback */
608
                { NULL,                     "/run/host/os-release",         NULL,    NULL,                             MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
609
                  MOUNT_FATAL },
610
                { NULL,                     "/run/host/os-release",         NULL,    NULL,                             MS_PRIVATE,
611
                  MOUNT_FATAL },  /* Turn off propagation (we only want that for the mount propagation tunnel dir) */
612
                { NULL,                     "/run/host",                    NULL,    NULL,                             MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
613
                  MOUNT_FATAL|MOUNT_IN_USERNS },
614
#if HAVE_SELINUX
615
                { "/sys/fs/selinux",        "/sys/fs/selinux",              NULL,    NULL,                             MS_BIND,
616
                  MOUNT_MKDIR|MOUNT_PRIVILEGED },  /* Bind mount first (mkdir/chown the mount point in case /sys/ is mounted as minimal skeleton tmpfs) */
617
                { NULL,                     "/sys/fs/selinux",              NULL,    NULL,                             MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
618
                  MOUNT_UNMANAGED|MOUNT_PRIVILEGED },  /* Then, make it r/o (don't mkdir/chown the mount point here, the previous entry already did that) */
619
                { NULL,                     "/sys/fs/selinux",              NULL,    NULL,                             MS_PRIVATE,
620
                  MOUNT_UNMANAGED|MOUNT_PRIVILEGED },  /* Turn off propagation (we only want that for the mount propagation tunnel dir) */
621
#endif
622
        };
623

624
        bool use_userns = FLAGS_SET(mount_settings, MOUNT_USE_USERNS);
411✔
625
        bool netns = FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_NETNS);
411✔
626
        bool ro = FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_RO);
411✔
627
        bool in_userns = FLAGS_SET(mount_settings, MOUNT_IN_USERNS);
411✔
628
        bool tmpfs_tmp = FLAGS_SET(mount_settings, MOUNT_APPLY_TMPFS_TMP);
411✔
629
        bool unmanaged = FLAGS_SET(mount_settings, MOUNT_UNMANAGED);
411✔
630
        bool privileged = FLAGS_SET(mount_settings, MOUNT_PRIVILEGED);
411✔
631
        int r;
411✔
632

633
        FOREACH_ELEMENT(m, mount_table) {
17,673✔
634
                _cleanup_free_ char *where = NULL, *options = NULL, *prefixed = NULL;
17,262✔
635
                bool fatal = FLAGS_SET(m->mount_settings, MOUNT_FATAL);
17,262✔
636
                const char *o;
17,262✔
637

638
                /* If we are in managed user namespace mode but the entry is marked for mount outside of
639
                 * managed user namespace mode, and to be mounted outside the user namespace, then skip it */
640
                if (!unmanaged && FLAGS_SET(m->mount_settings, MOUNT_UNMANAGED) && !FLAGS_SET(m->mount_settings, MOUNT_IN_USERNS))
17,262✔
641
                        continue;
27✔
642

643
                if (in_userns != FLAGS_SET(m->mount_settings, MOUNT_IN_USERNS))
17,235✔
644
                        continue;
9,897✔
645

646
                if (!netns && FLAGS_SET(m->mount_settings, MOUNT_APPLY_APIVFS_NETNS))
7,338✔
647
                        continue;
195✔
648

649
                if (!ro && FLAGS_SET(m->mount_settings, MOUNT_APPLY_APIVFS_RO))
7,143✔
650
                        continue;
456✔
651

652
                if (!tmpfs_tmp && FLAGS_SET(m->mount_settings, MOUNT_APPLY_TMPFS_TMP))
6,687✔
653
                        continue;
×
654

655
                if (!privileged && FLAGS_SET(m->mount_settings, MOUNT_PRIVILEGED))
6,687✔
656
                        continue;
×
657

658
                r = chase(m->where, dest, CHASE_NONEXISTENT|CHASE_PREFIX_ROOT, &where, NULL);
6,687✔
659
                if (r < 0)
6,687✔
660
                        return log_error_errno(r, "Failed to resolve %s%s: %m", strempty(dest), m->where);
×
661

662
                /* Skip this entry if it is not a remount. */
663
                if (m->what) {
6,687✔
664
                        r = path_is_mount_point(where);
4,453✔
665
                        if (r < 0 && r != -ENOENT)
4,453✔
666
                                return log_error_errno(r, "Failed to detect whether %s is a mount point: %m", where);
×
667
                        if (r > 0)
4,453✔
668
                                continue;
651✔
669
                }
670

671
                if ((m->mount_settings & (MOUNT_MKDIR|MOUNT_TOUCH)) != 0) {
6,036✔
672
                        uid_t u = (use_userns && !in_userns) ? uid_shift : UID_INVALID;
2,193✔
673

674
                        if (FLAGS_SET(m->mount_settings, MOUNT_TOUCH))
2,193✔
675
                                r = mkdir_parents_safe(dest, where, 0755, u, u, 0);
276✔
676
                        else
677
                                r = mkdir_p_safe(dest, where, 0755, u, u, 0);
1,917✔
678
                        if (r < 0 && r != -EEXIST) {
2,193✔
679
                                if (fatal && r != -EROFS)
×
680
                                        return log_error_errno(r, "Failed to create directory %s: %m", where);
×
681

682
                                log_debug_errno(r, "Failed to create directory %s: %m", where);
×
683

684
                                /* If we failed mkdir() or chown() due to the root directory being read only,
685
                                 * attempt to mount this fs anyway and let mount_verbose log any errors */
686
                                if (r != -EROFS)
×
687
                                        continue;
×
688
                        }
689
                }
690

691
                if (FLAGS_SET(m->mount_settings, MOUNT_TOUCH)) {
6,036✔
692
                        r = touch(where);
276✔
693
                        if (r < 0 && r != -EEXIST) {
276✔
694
                                if (fatal && r != -EROFS)
×
695
                                        return log_error_errno(r, "Failed to create file %s: %m", where);
×
696

697
                                log_debug_errno(r, "Failed to create file %s: %m", where);
×
698
                                if (r != -EROFS)
×
699
                                        continue;
×
700
                        }
701
                }
702

703
                o = m->options;
6,036✔
704
                if (streq_ptr(m->type, "tmpfs")) {
6,036✔
705
                        r = tmpfs_patch_options(o, in_userns ? 0 : uid_shift, selinux_apifs_context, &options);
2,484✔
706
                        if (r < 0)
1,242✔
707
                                return log_oom();
×
708
                        if (r > 0)
1,242✔
709
                                o = options;
1,242✔
710
                }
711

712
                if (FLAGS_SET(m->mount_settings, MOUNT_USRQUOTA_GRACEFUL)) {
6,036✔
713
                        r = mount_option_supported(m->type, /* key= */ "usrquota", /* value= */ NULL);
552✔
714
                        if (r < 0)
552✔
715
                                log_warning_errno(r, "Failed to determine if '%s' supports 'usrquota', assuming it doesn't: %m", m->type);
×
716
                        else if (r == 0)
552✔
717
                                log_debug("Kernel doesn't support 'usrquota' on '%s', not including in mount options for '%s'.", m->type, m->where);
16✔
718
                        else {
719
                                _cleanup_free_ char *joined = NULL;
×
720

721
                                if (!strextend_with_separator(&joined, ",", o ?: POINTER_MAX, "usrquota"))
536✔
722
                                        return log_oom();
×
723

724
                                free_and_replace(options, joined);
536✔
725
                                o = options;
536✔
726
                        }
727
                }
728

729
                if (FLAGS_SET(m->mount_settings, MOUNT_PREFIX_ROOT)) {
6,036✔
730
                        /* Optionally prefix the mount source with the root dir. This is useful in bind
731
                         * mounts to be created within the container image before we transition into it. Note
732
                         * that MOUNT_IN_USERNS is run after we transitioned hence prefixing is not necessary
733
                         * for those. */
734
                        r = chase(m->what, dest, CHASE_PREFIX_ROOT, &prefixed, NULL);
276✔
735
                        if (r < 0)
276✔
736
                                return log_error_errno(r, "Failed to resolve %s%s: %m", strempty(dest), m->what);
×
737
                }
738

739
                r = mount_verbose_full(
9,303✔
740
                                fatal ? LOG_ERR : LOG_DEBUG,
741
                                prefixed ?: m->what,
6,036✔
742
                                where,
743
                                m->type,
6,036✔
744
                                m->flags,
6,036✔
745
                                o,
746
                                FLAGS_SET(m->mount_settings, MOUNT_FOLLOW_SYMLINKS));
6,036✔
747
                if (r < 0 && fatal)
6,036✔
748
                        return r;
749
        }
750

751
        return 0;
752
}
753

754
static int parse_mount_bind_options(const char *options, unsigned long *mount_flags, char **mount_opts, RemountIdmapping *idmapping) {
4✔
755
        unsigned long flags = *mount_flags;
4✔
756
        char *opts = NULL;
4✔
757
        RemountIdmapping new_idmapping = *idmapping;
4✔
758
        int r;
4✔
759

760
        assert(options);
4✔
761

762
        for (;;) {
16✔
763
                _cleanup_free_ char *word = NULL;
6✔
764

765
                r = extract_first_word(&options, &word, ",", 0);
10✔
766
                if (r < 0)
10✔
767
                        return log_error_errno(r, "Failed to extract mount option: %m");
×
768
                if (r == 0)
10✔
769
                        break;
770

771
                if (streq(word, "rbind"))
6✔
772
                        flags |= MS_REC;
×
773
                else if (streq(word, "norbind"))
6✔
774
                        flags &= ~MS_REC;
2✔
775
                else if (streq(word, "idmap"))
4✔
776
                        new_idmapping = REMOUNT_IDMAPPING_HOST_ROOT;
777
                else if (streq(word, "noidmap"))
4✔
778
                        new_idmapping = REMOUNT_IDMAPPING_NONE;
779
                else if (streq(word, "rootidmap"))
2✔
780
                        new_idmapping = REMOUNT_IDMAPPING_HOST_OWNER;
781
                else if (streq(word, "owneridmap"))
2✔
782
                        new_idmapping = REMOUNT_IDMAPPING_HOST_OWNER_TO_TARGET_OWNER;
783
                else
784
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
785
                                               "Invalid bind mount option: %s", word);
786
        }
787

788
        *mount_flags = flags;
4✔
789
        *idmapping = new_idmapping;
4✔
790
        /* in the future mount_opts will hold string options for mount(2) */
791
        *mount_opts = opts;
4✔
792

793
        return 0;
4✔
794
}
795

796
static int mount_bind(const char *dest, CustomMount *m, uid_t uid_shift, uid_t uid_range) {
288✔
797
        _cleanup_free_ char *mount_opts = NULL, *where = NULL;
288✔
798
        unsigned long mount_flags = MS_BIND | MS_REC;
288✔
799
        struct stat source_st, dest_st;
288✔
800
        uid_t dest_uid = UID_INVALID;
288✔
801
        int r;
288✔
802
        RemountIdmapping idmapping = REMOUNT_IDMAPPING_NONE;
288✔
803

804
        assert(dest);
288✔
805
        assert(m);
288✔
806

807
        if (m->options) {
288✔
808
                r = parse_mount_bind_options(m->options, &mount_flags, &mount_opts, &idmapping);
4✔
809
                if (r < 0)
4✔
810
                        return r;
811
        }
812

813
        /* If this is a bind mount from a temporary sources change ownership of the source to the container's
814
         * root UID. Otherwise it would always show up as "nobody" if user namespacing is used. */
815
        if (m->rm_rf_tmpdir && chown(m->source, uid_shift, uid_shift) < 0)
288✔
816
                return log_error_errno(errno, "Failed to chown %s: %m", m->source);
×
817

818
        if (stat(m->source, &source_st) < 0)
288✔
819
                return log_error_errno(errno, "Failed to stat %s: %m", m->source);
×
820

821
        r = chase(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
288✔
822
        if (r < 0)
288✔
823
                return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
×
824
        if (r > 0) { /* Path exists already? */
288✔
825

826
                if (stat(where, &dest_st) < 0)
182✔
827
                        return log_error_errno(errno, "Failed to stat %s: %m", where);
×
828

829
                dest_uid = dest_st.st_uid;
182✔
830

831
                if (S_ISDIR(source_st.st_mode) && !S_ISDIR(dest_st.st_mode))
182✔
832
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
833
                                               "Cannot bind mount directory %s on file %s.",
834
                                               m->source, where);
835

836
                if (!S_ISDIR(source_st.st_mode) && S_ISDIR(dest_st.st_mode))
182✔
837
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
838
                                               "Cannot bind mount file %s on directory %s.",
839
                                               m->source, where);
840

841
        } else { /* Path doesn't exist yet? */
842
                r = mkdir_parents_safe_label(dest, where, 0755, uid_shift, uid_shift, MKDIR_IGNORE_EXISTING);
106✔
843
                if (r < 0)
106✔
844
                        return log_error_errno(r, "Failed to make parents of %s: %m", where);
×
845

846
                /* Create the mount point. Any non-directory file can be
847
                * mounted on any non-directory file (regular, fifo, socket,
848
                * char, block).
849
                */
850
                if (S_ISDIR(source_st.st_mode))
106✔
851
                        r = mkdir_label(where, 0755);
104✔
852
                else
853
                        r = touch(where);
2✔
854
                if (r < 0)
106✔
855
                        return log_error_errno(r, "Failed to create mount point %s: %m", where);
×
856

857
                if (chown(where, uid_shift, uid_shift) < 0)
106✔
858
                        return log_error_errno(errno, "Failed to chown %s: %m", where);
×
859

860
                dest_uid = uid_shift;
861
        }
862

863
        r = mount_nofollow_verbose(LOG_ERR, m->source, where, NULL, mount_flags, mount_opts);
288✔
864
        if (r < 0)
288✔
865
                return r;
866

867
        if (m->read_only) {
288✔
868
                r = bind_remount_recursive(where, MS_RDONLY, MS_RDONLY, NULL);
2✔
869
                if (r < 0)
2✔
870
                        return log_error_errno(r, "Read-only bind mount failed: %m");
×
871
        }
872

873
        if (idmapping != REMOUNT_IDMAPPING_NONE) {
288✔
874
                r = remount_idmap(STRV_MAKE(where), uid_shift, uid_range, source_st.st_uid, dest_uid, idmapping);
2✔
875
                if (r < 0)
2✔
876
                        return log_error_errno(r, "Failed to map ids for bind mount %s: %m", where);
×
877
        }
878

879
        return 0;
880
}
881

882
static int mount_tmpfs(const char *dest, CustomMount *m, uid_t uid_shift, const char *selinux_apifs_context) {
4✔
883
        const char *options;
4✔
884
        _cleanup_free_ char *buf = NULL, *where = NULL;
4✔
885
        int r;
4✔
886

887
        assert(dest);
4✔
888
        assert(m);
4✔
889

890
        r = chase(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
4✔
891
        if (r < 0)
4✔
892
                return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
×
893
        if (r == 0) { /* Doesn't exist yet? */
4✔
894
                r = mkdir_p_label(where, 0755);
×
895
                if (r < 0)
×
896
                        return log_error_errno(r, "Creating mount point for tmpfs %s failed: %m", where);
×
897
        }
898

899
        r = tmpfs_patch_options(m->options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
8✔
900
        if (r < 0)
4✔
901
                return log_oom();
×
902
        options = r > 0 ? buf : m->options;
4✔
903

904
        return mount_nofollow_verbose(LOG_ERR, "tmpfs", where, "tmpfs", MS_NODEV|MS_STRICTATIME, options);
4✔
905
}
906

907
static char *joined_and_escaped_lower_dirs(char **lower) {
2✔
908
        _cleanup_strv_free_ char **sv = NULL;
×
909

910
        sv = strv_copy(lower);
2✔
911
        if (!sv)
2✔
912
                return NULL;
913

914
        strv_reverse(sv);
2✔
915

916
        if (!strv_shell_escape(sv, ",:"))
2✔
917
                return NULL;
918

919
        return strv_join(sv, ":");
2✔
920
}
921

922
static int mount_overlay(const char *dest, CustomMount *m) {
2✔
923
        _cleanup_free_ char *lower = NULL, *where = NULL, *escaped_source = NULL;
2✔
924
        const char *options;
2✔
925
        int r;
2✔
926

927
        assert(dest);
2✔
928
        assert(m);
2✔
929

930
        r = chase(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
2✔
931
        if (r < 0)
2✔
932
                return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
×
933
        if (r == 0) { /* Doesn't exist yet? */
2✔
934
                r = mkdir_label(where, 0755);
×
935
                if (r < 0)
×
936
                        return log_error_errno(r, "Creating mount point for overlay %s failed: %m", where);
×
937
        }
938

939
        (void) mkdir_p_label(m->source, 0755);
2✔
940

941
        lower = joined_and_escaped_lower_dirs(m->lower);
2✔
942
        if (!lower)
2✔
943
                return log_oom();
×
944

945
        escaped_source = shell_escape(m->source, ",:");
2✔
946
        if (!escaped_source)
2✔
947
                return log_oom();
×
948

949
        if (m->read_only)
2✔
950
                options = strjoina("lowerdir=", escaped_source, ":", lower);
×
951
        else {
952
                _cleanup_free_ char *escaped_work_dir = NULL;
2✔
953

954
                escaped_work_dir = shell_escape(m->work_dir, ",:");
2✔
955
                if (!escaped_work_dir)
2✔
956
                        return log_oom();
×
957

958
                options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir);
26✔
959
        }
960

961
        return mount_nofollow_verbose(LOG_ERR, "overlay", where, "overlay", m->read_only ? MS_RDONLY : 0, options);
2✔
962
}
963

964
static int mount_inaccessible(const char *dest, CustomMount *m) {
4✔
965
        _cleanup_free_ char *where = NULL, *source = NULL;
4✔
966
        struct stat st;
4✔
967
        int r;
4✔
968

969
        assert(dest);
4✔
970
        assert(m);
4✔
971

972
        r = chase_and_stat(m->destination, dest, CHASE_PREFIX_ROOT, &where, &st);
4✔
973
        if (r < 0) {
4✔
974
                log_full_errno(m->graceful ? LOG_DEBUG : LOG_ERR, r, "Failed to resolve %s/%s: %m", dest, m->destination);
×
975
                return m->graceful ? 0 : r;
×
976
        }
977

978
        r = mode_to_inaccessible_node(NULL, st.st_mode, &source);
4✔
979
        if (r < 0)
4✔
980
                return m->graceful ? 0 : r;
×
981

982
        r = mount_nofollow_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, source, where, NULL, MS_BIND, NULL);
4✔
983
        if (r < 0)
4✔
984
                return m->graceful ? 0 : r;
×
985

986
        r = mount_nofollow_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, NULL, where, NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, NULL);
4✔
987
        if (r < 0) {
4✔
988
                (void) umount_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, where, UMOUNT_NOFOLLOW);
×
989
                return m->graceful ? 0 : r;
×
990
        }
991

992
        return 0;
993
}
994

995
static int mount_arbitrary(const char *dest, CustomMount *m) {
×
996
        _cleanup_free_ char *where = NULL;
×
997
        int r;
×
998

999
        assert(dest);
×
1000
        assert(m);
×
1001

1002
        r = chase(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
×
1003
        if (r < 0)
×
1004
                return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
×
1005
        if (r == 0) { /* Doesn't exist yet? */
×
1006
                r = mkdir_p_label(where, 0755);
×
1007
                if (r < 0)
×
1008
                        return log_error_errno(r, "Creating mount point for mount %s failed: %m", where);
×
1009
        }
1010

1011
        return mount_nofollow_verbose(LOG_ERR, m->source, where, m->type_argument, 0, m->options);
×
1012
}
1013

1014
int mount_custom(
687✔
1015
                const char *dest,
1016
                CustomMount *mounts, size_t n,
1017
                uid_t uid_shift,
1018
                uid_t uid_range,
1019
                const char *selinux_apifs_context,
1020
                MountSettingsMask mount_settings) {
1021
        int r;
687✔
1022

1023
        assert(dest);
687✔
1024

1025
        FOREACH_ARRAY(m, mounts, n) {
1,432✔
1026
                if (FLAGS_SET(mount_settings, MOUNT_IN_USERNS) != m->in_userns)
745✔
1027
                        continue;
149✔
1028

1029
                if (FLAGS_SET(mount_settings, MOUNT_ROOT_ONLY) && !path_equal(m->destination, "/"))
596✔
1030
                        continue;
298✔
1031

1032
                if (FLAGS_SET(mount_settings, MOUNT_NON_ROOT_ONLY) && path_equal(m->destination, "/"))
298✔
1033
                        continue;
×
1034

1035
                switch (m->type) {
298✔
1036

1037
                case CUSTOM_MOUNT_BIND:
288✔
1038
                        r = mount_bind(dest, m, uid_shift, uid_range);
288✔
1039
                        break;
288✔
1040

1041
                case CUSTOM_MOUNT_TMPFS:
4✔
1042
                        r = mount_tmpfs(dest, m, uid_shift, selinux_apifs_context);
4✔
1043
                        break;
4✔
1044

1045
                case CUSTOM_MOUNT_OVERLAY:
2✔
1046
                        r = mount_overlay(dest, m);
2✔
1047
                        break;
2✔
1048

1049
                case CUSTOM_MOUNT_INACCESSIBLE:
4✔
1050
                        r = mount_inaccessible(dest, m);
4✔
1051
                        break;
4✔
1052

1053
                case CUSTOM_MOUNT_ARBITRARY:
×
1054
                        r = mount_arbitrary(dest, m);
×
1055
                        break;
×
1056

1057
                default:
×
1058
                        assert_not_reached();
×
1059
                }
1060

1061
                if (r < 0)
298✔
1062
                        return r;
1063
        }
1064

1065
        return 0;
1066
}
1067

1068
bool has_custom_root_mount(const CustomMount *mounts, size_t n) {
559✔
1069
        FOREACH_ARRAY(m, mounts, n)
1,147✔
1070
                if (path_equal(m->destination, "/"))
588✔
1071
                        return true;
1072

1073
        return false;
1074
}
1075

1076
static int setup_volatile_state(const char *directory) {
4✔
1077
        int r;
4✔
1078

1079
        assert(directory);
4✔
1080

1081
        /* --volatile=state means we simply overmount /var with a tmpfs, and the rest read-only. */
1082

1083
        /* First, remount the root directory. */
1084
        r = bind_remount_recursive(directory, MS_RDONLY, MS_RDONLY, NULL);
4✔
1085
        if (r < 0)
4✔
1086
                return log_error_errno(r, "Failed to remount %s read-only: %m", directory);
×
1087

1088
        return 0;
1089
}
1090

1091
static int setup_volatile_state_after_remount_idmap(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
4✔
1092
        _cleanup_free_ char *buf = NULL;
4✔
1093
        const char *p, *options;
4✔
1094
        int r;
4✔
1095

1096
        assert(directory);
4✔
1097

1098
        /* Then, after remount_idmap(), overmount /var/ with a tmpfs. */
1099

1100
        p = prefix_roota(directory, "/var");
4✔
1101
        r = mkdir(p, 0755);
4✔
1102
        if (r < 0 && errno != EEXIST)
4✔
1103
                return log_error_errno(errno, "Failed to create %s: %m", directory);
×
1104

1105
        options = "mode=0755" TMPFS_LIMITS_VOLATILE_STATE;
4✔
1106
        r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
6✔
1107
        if (r < 0)
4✔
1108
                return log_oom();
×
1109
        if (r > 0)
4✔
1110
                options = buf;
4✔
1111

1112
        return mount_nofollow_verbose(LOG_ERR, "tmpfs", p, "tmpfs", MS_STRICTATIME, options);
4✔
1113
}
1114

1115
static int setup_volatile_yes(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
8✔
1116
        bool tmpfs_mounted = false, bind_mounted = false;
8✔
1117
        _cleanup_(rmdir_and_freep) char *template = NULL;
×
1118
        _cleanup_free_ char *buf = NULL, *bindir = NULL;
8✔
1119
        const char *f, *t, *options;
8✔
1120
        struct stat st;
8✔
1121
        int r;
8✔
1122

1123
        assert(directory);
8✔
1124

1125
        /* --volatile=yes means we mount a tmpfs to the root dir, and the original /usr to use inside it, and
1126
         * that read-only. Before we start setting this up let's validate if the image has the /usr merge
1127
         * implemented, and let's output a friendly log message if it hasn't. */
1128

1129
        bindir = path_join(directory, "/bin");
8✔
1130
        if (!bindir)
8✔
1131
                return log_oom();
×
1132
        if (lstat(bindir, &st) < 0) {
8✔
1133
                if (errno != ENOENT)
×
1134
                        return log_error_errno(errno, "Failed to stat /bin directory below image: %m");
×
1135

1136
                /* ENOENT is fine, just means the image is probably just a naked /usr and we can create the
1137
                 * rest. */
1138
        } else if (S_ISDIR(st.st_mode))
8✔
1139
                return log_error_errno(SYNTHETIC_ERRNO(EISDIR),
×
1140
                                       "Sorry, --volatile=yes mode is not supported with OS images that have not merged /bin/, /sbin/, /lib/, /lib64/ into /usr/. "
1141
                                       "Please work with your distribution and help them adopt the merged /usr scheme.");
1142
        else if (!S_ISLNK(st.st_mode))
8✔
1143
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1144
                                       "Error starting image: if --volatile=yes is used /bin must be a symlink (for merged /usr support) or non-existent (in which case a symlink is created automatically).");
1145

1146
        r = mkdtemp_malloc("/tmp/nspawn-volatile-XXXXXX", &template);
8✔
1147
        if (r < 0)
8✔
1148
                return log_error_errno(r, "Failed to create temporary directory: %m");
×
1149

1150
        options = "mode=0755" TMPFS_LIMITS_ROOTFS;
8✔
1151
        r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
12✔
1152
        if (r < 0)
8✔
1153
                goto fail;
×
1154
        if (r > 0)
8✔
1155
                options = buf;
8✔
1156

1157
        r = mount_nofollow_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
8✔
1158
        if (r < 0)
8✔
1159
                goto fail;
×
1160

1161
        tmpfs_mounted = true;
8✔
1162

1163
        f = prefix_roota(directory, "/usr");
16✔
1164
        t = prefix_roota(template, "/usr");
16✔
1165

1166
        r = mkdir(t, 0755);
8✔
1167
        if (r < 0 && errno != EEXIST) {
8✔
1168
                r = log_error_errno(errno, "Failed to create %s: %m", t);
×
1169
                goto fail;
×
1170
        }
1171

1172
        r = mount_nofollow_verbose(LOG_ERR, f, t, NULL, MS_BIND|MS_REC, NULL);
8✔
1173
        if (r < 0)
8✔
1174
                goto fail;
×
1175

1176
        bind_mounted = true;
8✔
1177

1178
        r = bind_remount_recursive(t, MS_RDONLY, MS_RDONLY, NULL);
8✔
1179
        if (r < 0) {
8✔
1180
                log_error_errno(r, "Failed to remount %s read-only: %m", t);
×
1181
                goto fail;
×
1182
        }
1183

1184
        r = mount_nofollow_verbose(LOG_ERR, template, directory, NULL, MS_MOVE, NULL);
8✔
1185
        if (r < 0)
8✔
1186
                goto fail;
×
1187

1188
        (void) rmdir(template);
8✔
1189

1190
        return 0;
8✔
1191

1192
fail:
1193
        if (bind_mounted)
×
1194
                (void) umount_verbose(LOG_ERR, t, UMOUNT_NOFOLLOW);
×
1195

1196
        if (tmpfs_mounted)
×
1197
                (void) umount_verbose(LOG_ERR, template, UMOUNT_NOFOLLOW);
×
1198

1199
        return r;
1200
}
1201

1202
static int setup_volatile_overlay(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
4✔
1203
        _cleanup_free_ char *buf = NULL, *escaped_directory = NULL, *escaped_upper = NULL, *escaped_work = NULL;
4✔
1204
        _cleanup_(rmdir_and_freep) char *template = NULL;
4✔
1205
        const char *upper, *work, *options;
4✔
1206
        bool tmpfs_mounted = false;
4✔
1207
        int r;
4✔
1208

1209
        assert(directory);
4✔
1210

1211
        /* --volatile=overlay means we mount an overlayfs to the root dir. */
1212

1213
        r = mkdtemp_malloc("/tmp/nspawn-volatile-XXXXXX", &template);
4✔
1214
        if (r < 0)
4✔
1215
                return log_error_errno(r, "Failed to create temporary directory: %m");
×
1216

1217
        options = "mode=0755" TMPFS_LIMITS_ROOTFS;
4✔
1218
        r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
6✔
1219
        if (r < 0)
4✔
1220
                goto finish;
×
1221
        if (r > 0)
4✔
1222
                options = buf;
4✔
1223

1224
        r = mount_nofollow_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
4✔
1225
        if (r < 0)
4✔
1226
                goto finish;
×
1227

1228
        tmpfs_mounted = true;
4✔
1229

1230
        upper = strjoina(template, "/upper");
20✔
1231
        work = strjoina(template, "/work");
20✔
1232

1233
        if (mkdir(upper, 0755) < 0) {
4✔
1234
                r = log_error_errno(errno, "Failed to create %s: %m", upper);
×
1235
                goto finish;
×
1236
        }
1237
        if (mkdir(work, 0755) < 0) {
4✔
1238
                r = log_error_errno(errno, "Failed to create %s: %m", work);
×
1239
                goto finish;
×
1240
        }
1241

1242
        /* And now, let's overmount the root dir with an overlayfs that uses the root dir as lower dir. It's kinda nice
1243
         * that the kernel allows us to do that without going through some mount point rearrangements. */
1244

1245
        escaped_directory = shell_escape(directory, ",:");
4✔
1246
        escaped_upper = shell_escape(upper, ",:");
4✔
1247
        escaped_work = shell_escape(work, ",:");
4✔
1248
        if (!escaped_directory || !escaped_upper || !escaped_work) {
4✔
1249
                r = -ENOMEM;
×
1250
                goto finish;
×
1251
        }
1252

1253
        options = strjoina("lowerdir=", escaped_directory, ",upperdir=", escaped_upper, ",workdir=", escaped_work);
52✔
1254
        r = mount_nofollow_verbose(LOG_ERR, "overlay", directory, "overlay", 0, options);
4✔
1255

1256
finish:
1257
        if (tmpfs_mounted)
×
1258
                (void) umount_verbose(LOG_ERR, template, UMOUNT_NOFOLLOW);
4✔
1259

1260
        return r;
1261
}
1262

1263
int setup_volatile_mode(
278✔
1264
                const char *directory,
1265
                VolatileMode mode,
1266
                uid_t uid_shift,
1267
                const char *selinux_apifs_context) {
1268

1269
        switch (mode) {
278✔
1270

1271
        case VOLATILE_YES:
8✔
1272
                return setup_volatile_yes(directory, uid_shift, selinux_apifs_context);
8✔
1273

1274
        case VOLATILE_STATE:
4✔
1275
                return setup_volatile_state(directory);
4✔
1276

1277
        case VOLATILE_OVERLAY:
4✔
1278
                return setup_volatile_overlay(directory, uid_shift, selinux_apifs_context);
4✔
1279

1280
        default:
1281
                return 0;
1282
        }
1283
}
1284

1285
int setup_volatile_mode_after_remount_idmap(
276✔
1286
                const char *directory,
1287
                VolatileMode mode,
1288
                uid_t uid_shift,
1289
                const char *selinux_apifs_context) {
1290

1291
        switch (mode) {
276✔
1292

1293
        case VOLATILE_STATE:
4✔
1294
                return setup_volatile_state_after_remount_idmap(directory, uid_shift, selinux_apifs_context);
4✔
1295

1296
        default:
1297
                return 0;
1298
        }
1299
}
1300

1301
/* Expects *pivot_root_new and *pivot_root_old to be initialised to allocated memory or NULL. */
1302
int pivot_root_parse(char **pivot_root_new, char **pivot_root_old, const char *s) {
2✔
1303
        _cleanup_free_ char *root_new = NULL, *root_old = NULL;
2✔
1304
        const char *p = s;
2✔
1305
        int r;
2✔
1306

1307
        assert(pivot_root_new);
2✔
1308
        assert(pivot_root_old);
2✔
1309

1310
        r = extract_first_word(&p, &root_new, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
2✔
1311
        if (r < 0)
2✔
1312
                return r;
1313
        if (r == 0)
2✔
1314
                return -EINVAL;
1315

1316
        if (isempty(p))
2✔
1317
                root_old = NULL;
1318
        else {
1319
                root_old = strdup(p);
×
1320
                if (!root_old)
×
1321
                        return -ENOMEM;
1322
        }
1323

1324
        if (!path_is_absolute(root_new))
2✔
1325
                return -EINVAL;
1326
        if (root_old && !path_is_absolute(root_old))
×
1327
                return -EINVAL;
1328

1329
        free_and_replace(*pivot_root_new, root_new);
×
1330
        free_and_replace(*pivot_root_old, root_old);
×
1331

1332
        return 0;
×
1333
}
1334

1335
int setup_pivot_root(const char *directory, const char *pivot_root_new, const char *pivot_root_old) {
278✔
1336
        _cleanup_free_ char *directory_pivot_root_new = NULL;
556✔
1337
        _cleanup_free_ char *pivot_tmp_pivot_root_old = NULL;
278✔
1338
        _cleanup_(rmdir_and_freep) char *pivot_tmp = NULL;
278✔
1339
        int r;
278✔
1340

1341
        assert(directory);
278✔
1342

1343
        if (!pivot_root_new)
278✔
1344
                return 0;
1345

1346
        /* Pivot pivot_root_new to / and the existing / to pivot_root_old.
1347
         * If pivot_root_old is NULL, the existing / disappears.
1348
         * This requires a temporary directory, pivot_tmp, which is
1349
         * not a child of either.
1350
         *
1351
         * This is typically used for OSTree-style containers, where the root partition contains several
1352
         * sysroots which could be run. Normally, one would be chosen by the bootloader and pivoted to / by
1353
         * initrd.
1354
         *
1355
         * For example, for an OSTree deployment, pivot_root_new
1356
         * would be: /ostree/deploy/$os/deploy/$checksum. Note that this
1357
         * code doesn’t do the /var mount which OSTree expects: use
1358
         * --bind +/sysroot/ostree/deploy/$os/var:/var for that.
1359
         *
1360
         * So in the OSTree case, we’ll end up with something like:
1361
         *  - directory = /tmp/nspawn-root-123456
1362
         *  - pivot_root_new = /ostree/deploy/os/deploy/123abc
1363
         *  - pivot_root_old = /sysroot
1364
         *  - directory_pivot_root_new =
1365
         *       /tmp/nspawn-root-123456/ostree/deploy/os/deploy/123abc
1366
         *  - pivot_tmp = /tmp/nspawn-pivot-123456
1367
         *  - pivot_tmp_pivot_root_old = /tmp/nspawn-pivot-123456/sysroot
1368
         *
1369
         * Requires all file systems at directory and below to be mounted
1370
         * MS_PRIVATE or MS_SLAVE so they can be moved.
1371
         */
1372
        directory_pivot_root_new = path_join(directory, pivot_root_new);
×
1373
        if (!directory_pivot_root_new)
×
1374
                return log_oom();
×
1375

1376
        /* Remount directory_pivot_root_new to make it movable. */
1377
        r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, directory_pivot_root_new, NULL, MS_BIND, NULL);
×
1378
        if (r < 0)
×
1379
                return r;
1380

1381
        if (pivot_root_old) {
×
1382
                r = mkdtemp_malloc("/tmp/nspawn-pivot-XXXXXX", &pivot_tmp);
×
1383
                if (r < 0)
×
1384
                        return log_error_errno(r, "Failed to create temporary directory: %m");
×
1385

1386
                pivot_tmp_pivot_root_old = path_join(pivot_tmp, pivot_root_old);
×
1387
                if (!pivot_tmp_pivot_root_old)
×
1388
                        return log_oom();
×
1389

1390
                r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, pivot_tmp, NULL, MS_MOVE, NULL);
×
1391
                if (r < 0)
×
1392
                        return r;
1393

1394
                r = mount_nofollow_verbose(LOG_ERR, directory, pivot_tmp_pivot_root_old, NULL, MS_MOVE, NULL);
×
1395
                if (r < 0)
×
1396
                        return r;
1397

1398
                r = mount_nofollow_verbose(LOG_ERR, pivot_tmp, directory, NULL, MS_MOVE, NULL);
×
1399
        } else
1400
                r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, directory, NULL, MS_MOVE, NULL);
×
1401

1402
        if (r < 0)
×
1403
                return r;
×
1404

1405
        return 0;
1406
}
1407

1408
#define NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS "/run/host/proc"
1409
#define NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS "/run/host/sys"
1410

1411
int pin_fully_visible_api_fs(void) {
100✔
1412
        int r;
100✔
1413

1414
        log_debug("Pinning fully visible API FS");
100✔
1415

1416
        (void) mkdir_p(NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS, 0755);
100✔
1417
        (void) mkdir_p(NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS, 0755);
100✔
1418

1419
        r = mount_follow_verbose(LOG_ERR, "proc", NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS, "proc", PROC_DEFAULT_MOUNT_FLAGS, NULL);
100✔
1420
        if (r < 0)
100✔
1421
                return r;
1422

1423
        r = mount_follow_verbose(LOG_ERR, "sysfs", NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS, "sysfs", SYS_DEFAULT_MOUNT_FLAGS, NULL);
100✔
1424
        if (r < 0)
100✔
1425
                return r;
×
1426

1427
        return 0;
1428
}
1429

1430
static int do_wipe_fully_visible_api_fs(void) {
48✔
1431
        if (umount2(NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS, MNT_DETACH) < 0)
48✔
1432
                return log_error_errno(errno, "Failed to unmount temporary proc: %m");
×
1433

1434
        if (rmdir(NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS) < 0)
48✔
1435
                return log_error_errno(errno, "Failed to remove temporary proc mountpoint: %m");
×
1436

1437
        if (umount2(NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS, MNT_DETACH) < 0)
48✔
1438
                return log_error_errno(errno, "Failed to unmount temporary sys: %m");
×
1439

1440
        if (rmdir(NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS) < 0)
48✔
1441
                return log_error_errno(errno, "Failed to remove temporary sys mountpoint: %m");
×
1442

1443
        return 0;
1444
}
1445

1446
int wipe_fully_visible_api_fs(int mntns_fd) {
48✔
1447
        _cleanup_close_ int orig_mntns_fd = -EBADF;
48✔
1448
        int r, rr;
48✔
1449

1450
        log_debug("Wiping fully visible API FS");
48✔
1451

1452
        orig_mntns_fd = namespace_open_by_type(NAMESPACE_MOUNT);
48✔
1453
        if (orig_mntns_fd < 0)
48✔
1454
                return log_error_errno(orig_mntns_fd, "Failed to pin originating mount namespace: %m");
×
1455

1456
        r = namespace_enter(/* pidns_fd = */ -EBADF,
48✔
1457
                            mntns_fd,
1458
                            /* netns_fd = */ -EBADF,
1459
                            /* userns_fd = */ -EBADF,
1460
                            /* root_fd = */ -EBADF);
1461
        if (r < 0)
48✔
1462
                return log_error_errno(r, "Failed to enter mount namespace: %m");
×
1463

1464
        rr = do_wipe_fully_visible_api_fs();
48✔
1465

1466
        r = namespace_enter(/* pidns_fd = */ -EBADF,
48✔
1467
                            orig_mntns_fd,
1468
                            /* netns_fd = */ -EBADF,
1469
                            /* userns_fd = */ -EBADF,
1470
                            /* root_fd = */ -EBADF);
1471
        if (r < 0)
48✔
1472
                return log_error_errno(r, "Failed to enter original mount namespace: %m");
×
1473

1474
        return rr;
1475
}
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