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

systemd / systemd / 12858385481

19 Jan 2025 02:00PM UTC coverage: 0.117%. Remained the same
12858385481

push

github

bluca
capability-util: rename output argument of capability_gain_cap_setpcap()

0 of 1 new or added line in 1 file covered. (0.0%)

3613 existing lines in 15 files now uncovered.

478 of 407820 relevant lines covered (0.12%)

1.46 hits per line

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

0.0
/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) {
×
30
        CustomMount *ret;
×
31

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

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

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

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

47
        return ret;
×
48
}
49

50
void custom_mount_free_all(CustomMount *l, size_t n) {
×
51
        for (size_t i = 0; i < n; i++) {
×
52
                CustomMount *m = l + i;
×
53

54
                free(m->source);
×
55
                free(m->destination);
×
56
                free(m->options);
×
57

58
                if (m->work_dir) {
×
59
                        (void) rm_rf(m->work_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
×
60
                        free(m->work_dir);
×
61
                }
62

63
                if (m->rm_rf_tmpdir) {
×
64
                        (void) rm_rf(m->rm_rf_tmpdir, REMOVE_ROOT|REMOVE_PHYSICAL);
×
65
                        free(m->rm_rf_tmpdir);
×
66
                }
67

68
                strv_free(m->lower);
×
69
                free(m->type_argument);
×
70
        }
71

72
        free(l);
×
73
}
×
74

75
static int custom_mount_compare(const CustomMount *a, const CustomMount *b) {
×
76
        int r;
×
77

78
        r = path_compare(a->destination, b->destination);
×
79
        if (r != 0)
×
80
                return r;
81

82
        return CMP(a->type, b->type);
×
83
}
84

85
static int source_path_parse(const char *p, char **ret) {
×
86
        assert(p);
×
87
        assert(ret);
×
88

89
        if (isempty(p))
×
90
                return -EINVAL;
91

92
        if (*p == '+') {
×
93
                if (!path_is_absolute(p + 1))
×
94
                        return -EINVAL;
×
95

96
                char *s = strdup(p);
×
97
                if (!s)
×
98
                        return -ENOMEM;
99

100
                *ret = TAKE_PTR(s);
×
101
                return 0;
×
102
        }
103

104
        return path_make_absolute_cwd(p, ret);
×
105
}
106

107
static int source_path_parse_nullable(const char *p, char **ret) {
×
108
        assert(p);
×
109
        assert(ret);
×
110

111
        if (isempty(p)) {
×
112
                *ret = NULL;
×
113
                return 0;
×
114
        }
115

116
        return source_path_parse(p, ret);
×
117
}
118

119
static char *resolve_source_path(const char *dest, const char *source) {
×
120
        if (!source)
×
121
                return NULL;
122

123
        if (source[0] == '+')
×
124
                return path_join(dest, source + 1);
×
125

126
        return strdup(source);
×
127
}
128

129
static int allocate_temporary_source(CustomMount *m) {
×
130
        int r;
×
131

132
        assert(m);
×
133
        assert(!m->source);
×
134
        assert(!m->rm_rf_tmpdir);
×
135

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

140
        m->source = path_join(m->rm_rf_tmpdir, "src");
×
141
        if (!m->source)
×
142
                return log_oom();
×
143

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

147
        return 0;
148
}
149

150
int custom_mount_prepare_all(const char *dest, CustomMount *l, size_t n) {
×
151
        int r;
×
152

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

157
        assert(l || n == 0);
×
158

159
        /* Order the custom mounts, and make sure we have a working directory */
160
        typesafe_qsort(l, n, custom_mount_compare);
×
161

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

169
                if (m->type == CUSTOM_MOUNT_BIND) {
×
170
                        if (m->source) {
×
171
                                char *s;
×
172

173
                                s = resolve_source_path(dest, m->source);
×
174
                                if (!s)
×
175
                                        return log_oom();
×
176

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

181
                                r = allocate_temporary_source(m);
×
182
                                if (r < 0)
×
183
                                        return r;
184
                        }
185
                }
186

187
                if (m->type == CUSTOM_MOUNT_OVERLAY) {
×
188
                        STRV_FOREACH(j, m->lower) {
×
189
                                char *s;
×
190

191
                                s = resolve_source_path(dest, *j);
×
192
                                if (!s)
×
193
                                        return log_oom();
×
194

195
                                free_and_replace(*j, s);
×
196
                        }
197

198
                        if (m->source) {
×
199
                                char *s;
×
200

201
                                s = resolve_source_path(dest, m->source);
×
202
                                if (!s)
×
203
                                        return log_oom();
×
204

205
                                free_and_replace(m->source, s);
×
206
                        } else {
207
                                r = allocate_temporary_source(m);
×
208
                                if (r < 0)
×
209
                                        return r;
210
                        }
211

212
                        if (m->work_dir) {
×
213
                                char *s;
×
214

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

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

226
                        (void) mkdir_label(m->work_dir, 0700);
×
227
                }
228
        }
229

230
        return 0;
231
}
232

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

238
        assert(l);
×
239
        assert(n);
×
240

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

257
        r = source_path_parse_nullable(source, &p);
×
258
        if (r < 0)
×
259
                return r;
260

261
        if (!path_is_absolute(destination))
×
262
                return -EINVAL;
263

264
        m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND);
×
265
        if (!m)
×
266
                return -ENOMEM;
267

268
        m->source = TAKE_PTR(p);
×
269
        m->destination = TAKE_PTR(destination);
×
270
        m->read_only = read_only;
×
271
        m->options = TAKE_PTR(opts);
×
272

273
        return 0;
×
274
}
275

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

282
        assert(l);
×
283
        assert(n);
×
284

285
        r = extract_first_word(&p, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
×
286
        if (r < 0)
×
287
                return r;
288
        if (r == 0)
×
289
                return -EINVAL;
290

291
        if (isempty(p))
×
292
                opts = strdup("mode=0755");
×
293
        else
294
                opts = strdup(p);
×
295
        if (!opts)
×
296
                return -ENOMEM;
297

298
        if (!path_is_absolute(path))
×
299
                return -EINVAL;
300

301
        m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS);
×
302
        if (!m)
×
303
                return -ENOMEM;
304

305
        m->destination = TAKE_PTR(path);
×
306
        m->options = TAKE_PTR(opts);
×
307

308
        return 0;
×
309
}
310

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

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

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

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

332
                free_and_replace(lower[0], p);
×
333

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

338
                free_and_replace(lower[1], p);
×
339

340
                upper = TAKE_PTR(lower[1]);
×
341

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

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

351
                destination = lower[k - 1];
×
352
                upper = TAKE_PTR(lower[k - 2]);
×
353

354
                STRV_FOREACH(i, lower) {
×
355
                        r = source_path_parse(*i, &p);
×
356
                        if (r < 0)
×
357
                                return r;
358

359
                        free_and_replace(*i, p);
×
360
                }
361

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

368
                free_and_replace(upper, p);
×
369

370
                if (!path_is_absolute(destination))
×
371
                        return -EINVAL;
372
        }
373

374
        m = custom_mount_add(l, n, CUSTOM_MOUNT_OVERLAY);
×
375
        if (!m)
×
376
                return -ENOMEM;
377

378
        m->destination = TAKE_PTR(destination);
×
379
        m->source = TAKE_PTR(upper);
×
380
        m->lower = TAKE_PTR(lower);
×
381
        m->read_only = read_only;
×
382

383
        return 0;
×
384
}
385

386
int inaccessible_mount_parse(CustomMount **l, size_t *n, const char *s) {
×
387
        _cleanup_free_ char *path = NULL;
×
388
        CustomMount *m;
×
389

390
        assert(l);
×
391
        assert(n);
×
392
        assert(s);
×
393

394
        if (!path_is_absolute(s))
×
395
                return -EINVAL;
396

397
        path = strdup(s);
×
398
        if (!path)
×
399
                return -ENOMEM;
400

401
        m = custom_mount_add(l, n, CUSTOM_MOUNT_INACCESSIBLE);
×
402
        if (!m)
×
403
                return -ENOMEM;
404

405
        m->destination = TAKE_PTR(path);
×
406
        return 0;
×
407
}
408

409
int tmpfs_patch_options(
×
410
                const char *options,
411
                uid_t uid_shift,
412
                const char *selinux_apifs_context,
413
                char **ret) {
414

415
        _cleanup_free_ char *buf = NULL;
×
416

417
        assert(ret);
×
418

419
        if (options) {
×
420
                buf = strdup(options);
×
421
                if (!buf)
×
422
                        return -ENOMEM;
423
        }
424

425
        if (uid_shift != UID_INVALID)
×
426
                if (strextendf_with_separator(&buf, ",", "uid=" UID_FMT ",gid=" UID_FMT, uid_shift, uid_shift) < 0)
×
427
                        return -ENOMEM;
428

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

435
        *ret = TAKE_PTR(buf);
×
436
        return !!*ret;
×
437
}
438

439
int mount_sysfs(const char *dest, MountSettingsMask mount_settings) {
×
440
        _cleanup_free_ char *top = NULL, *full = NULL;;
×
441
        unsigned long extra_flags = 0;
×
442
        int r;
×
443

444
        top = path_join(dest, "/sys");
×
445
        if (!top)
×
446
                return log_oom();
×
447

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

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

469
        full = path_join(top, "/full");
×
470
        if (!full)
×
471
                return log_oom();
×
472

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

476
        if (FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_RO))
×
477
                extra_flags |= MS_RDONLY;
×
478

479
        r = mount_nofollow_verbose(LOG_ERR, "sysfs", full, "sysfs",
×
480
                                   MS_NOSUID|MS_NOEXEC|MS_NODEV|extra_flags, NULL);
481
        if (r < 0)
×
482
                return r;
483

484
        FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") {
×
485
                _cleanup_free_ char *from = NULL, *to = NULL;
×
486

487
                from = path_join(full, x);
×
488
                if (!from)
×
489
                        return log_oom();
×
490

491
                to = path_join(top, x);
×
492
                if (!to)
×
493
                        return log_oom();
×
494

495
                (void) mkdir(to, 0755);
×
496

497
                r = mount_nofollow_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
×
498
                if (r < 0)
×
499
                        return r;
500

501
                r = mount_nofollow_verbose(LOG_ERR, NULL, to, NULL,
×
502
                                           MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
503
                if (r < 0)
×
504
                        return r;
505
        }
506

507
        r = umount_verbose(LOG_ERR, full, UMOUNT_NOFOLLOW);
×
508
        if (r < 0)
×
509
                return r;
510

511
        if (rmdir(full) < 0)
×
512
                return log_error_errno(errno, "Failed to remove %s: %m", full);
×
513

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

519
        (void) mkdir_p(x, 0755);
×
520

521
        return mount_nofollow_verbose(LOG_ERR, NULL, top, NULL,
×
522
                                      MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
523
}
524

525
#define PROC_DEFAULT_MOUNT_FLAGS (MS_NOSUID|MS_NOEXEC|MS_NODEV)
526
#define SYS_DEFAULT_MOUNT_FLAGS  (MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV)
527

528
int mount_all(const char *dest,
×
529
              MountSettingsMask mount_settings,
530
              uid_t uid_shift,
531
              const char *selinux_apifs_context) {
532

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

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

545
        typedef struct MountPoint {
×
546
                const char *what;
547
                const char *where;
548
                const char *type;
549
                const char *options;
550
                unsigned long flags;
551
                MountSettingsMask mount_settings;
552
        } MountPoint;
553

554
        static const MountPoint mount_table[] = {
×
555
                /* First we list inner child mounts (i.e. mounts applied *after* entering user namespacing when we are privileged) */
556
                { "proc",            "/proc",           "proc",  NULL,        PROC_DEFAULT_MOUNT_FLAGS,
557
                  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. */
558

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

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

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

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

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

586
                { "mqueue",                 "/dev/mqueue",                  "mqueue", NULL,                            MS_NOSUID|MS_NOEXEC|MS_NODEV,
587
                  MOUNT_IN_USERNS|MOUNT_MKDIR },
588

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

626
        bool use_userns = FLAGS_SET(mount_settings, MOUNT_USE_USERNS);
×
627
        bool netns = FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_NETNS);
×
628
        bool ro = FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_RO);
×
629
        bool in_userns = FLAGS_SET(mount_settings, MOUNT_IN_USERNS);
×
630
        bool tmpfs_tmp = FLAGS_SET(mount_settings, MOUNT_APPLY_TMPFS_TMP);
×
631
        bool privileged = FLAGS_SET(mount_settings, MOUNT_PRIVILEGED);
×
632
        int r;
×
633

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

639
                /* If we are not privileged but the entry is marked as privileged and to be mounted outside the user namespace, then skip it */
640
                if (!privileged && FLAGS_SET(m->mount_settings, MOUNT_PRIVILEGED) && !FLAGS_SET(m->mount_settings, MOUNT_IN_USERNS))
×
641
                        continue;
×
642

643
                if (in_userns != FLAGS_SET(m->mount_settings, MOUNT_IN_USERNS))
×
644
                        continue;
×
645

646
                if (!netns && FLAGS_SET(m->mount_settings, MOUNT_APPLY_APIVFS_NETNS))
×
647
                        continue;
×
648

649
                if (!ro && FLAGS_SET(m->mount_settings, MOUNT_APPLY_APIVFS_RO))
×
650
                        continue;
×
651

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

655
                r = chase(m->where, dest, CHASE_NONEXISTENT|CHASE_PREFIX_ROOT, &where, NULL);
×
656
                if (r < 0)
×
657
                        return log_error_errno(r, "Failed to resolve %s%s: %m", strempty(dest), m->where);
×
658

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

668
                if ((m->mount_settings & (MOUNT_MKDIR|MOUNT_TOUCH)) != 0) {
×
669
                        uid_t u = (use_userns && !in_userns) ? uid_shift : UID_INVALID;
×
670

671
                        if (FLAGS_SET(m->mount_settings, MOUNT_TOUCH))
×
672
                                r = mkdir_parents_safe(dest, where, 0755, u, u, 0);
×
673
                        else
674
                                r = mkdir_p_safe(dest, where, 0755, u, u, 0);
×
675
                        if (r < 0 && r != -EEXIST) {
×
676
                                if (fatal && r != -EROFS)
×
677
                                        return log_error_errno(r, "Failed to create directory %s: %m", where);
×
678

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

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

688
                if (FLAGS_SET(m->mount_settings, MOUNT_TOUCH)) {
×
689
                        r = touch(where);
×
690
                        if (r < 0 && r != -EEXIST) {
×
691
                                if (fatal && r != -EROFS)
×
692
                                        return log_error_errno(r, "Failed to create file %s: %m", where);
×
693

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

700
                o = m->options;
×
701
                if (streq_ptr(m->type, "tmpfs")) {
×
702
                        r = tmpfs_patch_options(o, in_userns ? 0 : uid_shift, selinux_apifs_context, &options);
×
703
                        if (r < 0)
×
704
                                return log_oom();
×
705
                        if (r > 0)
×
706
                                o = options;
×
707
                }
708

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

UNCOV
718
                                if (!strextend_with_separator(&joined, ",", o ?: POINTER_MAX, "usrquota"))
×
719
                                        return log_oom();
×
720

721
                                free_and_replace(options, joined);
×
UNCOV
722
                                o = options;
×
723
                        }
724
                }
725

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

736
                r = mount_verbose_full(
×
737
                                fatal ? LOG_ERR : LOG_DEBUG,
738
                                prefixed ?: m->what,
×
739
                                where,
740
                                m->type,
×
UNCOV
741
                                m->flags,
×
742
                                o,
743
                                FLAGS_SET(m->mount_settings, MOUNT_FOLLOW_SYMLINKS));
×
UNCOV
744
                if (r < 0 && fatal)
×
745
                        return r;
746
        }
747

748
        return 0;
749
}
750

751
static int parse_mount_bind_options(const char *options, unsigned long *mount_flags, char **mount_opts, RemountIdmapping *idmapping) {
×
752
        unsigned long flags = *mount_flags;
×
753
        char *opts = NULL;
×
754
        RemountIdmapping new_idmapping = *idmapping;
×
755
        int r;
×
756

757
        assert(options);
×
758

759
        for (;;) {
×
UNCOV
760
                _cleanup_free_ char *word = NULL;
×
761

UNCOV
762
                r = extract_first_word(&options, &word, ",", 0);
×
UNCOV
763
                if (r < 0)
×
764
                        return log_error_errno(r, "Failed to extract mount option: %m");
×
UNCOV
765
                if (r == 0)
×
766
                        break;
767

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

785
        *mount_flags = flags;
×
UNCOV
786
        *idmapping = new_idmapping;
×
787
        /* in the future mount_opts will hold string options for mount(2) */
788
        *mount_opts = opts;
×
789

UNCOV
790
        return 0;
×
791
}
792

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

801
        assert(dest);
×
802
        assert(m);
×
803

804
        if (m->options) {
×
UNCOV
805
                r = parse_mount_bind_options(m->options, &mount_flags, &mount_opts, &idmapping);
×
806
                if (r < 0)
×
807
                        return r;
808
        }
809

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

UNCOV
815
        if (stat(m->source, &source_st) < 0)
×
816
                return log_error_errno(errno, "Failed to stat %s: %m", m->source);
×
817

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

823
                if (stat(where, &dest_st) < 0)
×
824
                        return log_error_errno(errno, "Failed to stat %s: %m", where);
×
825

UNCOV
826
                dest_uid = dest_st.st_uid;
×
827

UNCOV
828
                if (S_ISDIR(source_st.st_mode) && !S_ISDIR(dest_st.st_mode))
×
UNCOV
829
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
830
                                               "Cannot bind mount directory %s on file %s.",
831
                                               m->source, where);
832

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

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

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

854
                if (chown(where, uid_shift, uid_shift) < 0)
×
855
                        return log_error_errno(errno, "Failed to chown %s: %m", where);
×
856

857
                dest_uid = uid_shift;
858
        }
859

UNCOV
860
        r = mount_nofollow_verbose(LOG_ERR, m->source, where, NULL, mount_flags, mount_opts);
×
UNCOV
861
        if (r < 0)
×
862
                return r;
863

864
        if (m->read_only) {
×
865
                r = bind_remount_recursive(where, MS_RDONLY, MS_RDONLY, NULL);
×
UNCOV
866
                if (r < 0)
×
867
                        return log_error_errno(r, "Read-only bind mount failed: %m");
×
868
        }
869

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

876
        return 0;
877
}
878

879
static int mount_tmpfs(const char *dest, CustomMount *m, uid_t uid_shift, const char *selinux_apifs_context) {
×
880
        const char *options;
×
881
        _cleanup_free_ char *buf = NULL, *where = NULL;
×
882
        int r;
×
883

884
        assert(dest);
×
UNCOV
885
        assert(m);
×
886

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

896
        r = tmpfs_patch_options(m->options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
×
UNCOV
897
        if (r < 0)
×
UNCOV
898
                return log_oom();
×
899
        options = r > 0 ? buf : m->options;
×
900

UNCOV
901
        return mount_nofollow_verbose(LOG_ERR, "tmpfs", where, "tmpfs", MS_NODEV|MS_STRICTATIME, options);
×
902
}
903

904
static char *joined_and_escaped_lower_dirs(char **lower) {
×
905
        _cleanup_strv_free_ char **sv = NULL;
×
906

907
        sv = strv_copy(lower);
×
908
        if (!sv)
×
909
                return NULL;
910

911
        strv_reverse(sv);
×
912

913
        if (!strv_shell_escape(sv, ",:"))
×
914
                return NULL;
915

916
        return strv_join(sv, ":");
×
917
}
918

919
static int mount_overlay(const char *dest, CustomMount *m) {
×
UNCOV
920
        _cleanup_free_ char *lower = NULL, *where = NULL, *escaped_source = NULL;
×
921
        const char *options;
×
922
        int r;
×
923

UNCOV
924
        assert(dest);
×
925
        assert(m);
×
926

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

936
        (void) mkdir_p_label(m->source, 0755);
×
937

938
        lower = joined_and_escaped_lower_dirs(m->lower);
×
UNCOV
939
        if (!lower)
×
UNCOV
940
                return log_oom();
×
941

UNCOV
942
        escaped_source = shell_escape(m->source, ",:");
×
UNCOV
943
        if (!escaped_source)
×
944
                return log_oom();
×
945

946
        if (m->read_only)
×
947
                options = strjoina("lowerdir=", escaped_source, ":", lower);
×
948
        else {
949
                _cleanup_free_ char *escaped_work_dir = NULL;
×
950

UNCOV
951
                escaped_work_dir = shell_escape(m->work_dir, ",:");
×
952
                if (!escaped_work_dir)
×
953
                        return log_oom();
×
954

955
                options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir);
×
956
        }
957

958
        return mount_nofollow_verbose(LOG_ERR, "overlay", where, "overlay", m->read_only ? MS_RDONLY : 0, options);
×
959
}
960

UNCOV
961
static int mount_inaccessible(const char *dest, CustomMount *m) {
×
962
        _cleanup_free_ char *where = NULL, *source = NULL;
×
963
        struct stat st;
×
964
        int r;
×
965

966
        assert(dest);
×
967
        assert(m);
×
968

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

975
        r = mode_to_inaccessible_node(NULL, st.st_mode, &source);
×
976
        if (r < 0)
×
977
                return m->graceful ? 0 : r;
×
978

979
        r = mount_nofollow_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, source, where, NULL, MS_BIND, NULL);
×
980
        if (r < 0)
×
UNCOV
981
                return m->graceful ? 0 : r;
×
982

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

989
        return 0;
990
}
991

UNCOV
992
static int mount_arbitrary(const char *dest, CustomMount *m) {
×
UNCOV
993
        _cleanup_free_ char *where = NULL;
×
994
        int r;
×
995

UNCOV
996
        assert(dest);
×
UNCOV
997
        assert(m);
×
998

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

1008
        return mount_nofollow_verbose(LOG_ERR, m->source, where, m->type_argument, 0, m->options);
×
1009
}
1010

1011
int mount_custom(
×
1012
                const char *dest,
1013
                CustomMount *mounts, size_t n,
1014
                uid_t uid_shift,
1015
                uid_t uid_range,
1016
                const char *selinux_apifs_context,
1017
                MountSettingsMask mount_settings) {
UNCOV
1018
        int r;
×
1019

1020
        assert(dest);
×
1021

UNCOV
1022
        for (size_t i = 0; i < n; i++) {
×
1023
                CustomMount *m = mounts + i;
×
1024

1025
                if (FLAGS_SET(mount_settings, MOUNT_IN_USERNS) != m->in_userns)
×
UNCOV
1026
                        continue;
×
1027

1028
                if (FLAGS_SET(mount_settings, MOUNT_ROOT_ONLY) && !path_equal(m->destination, "/"))
×
1029
                        continue;
×
1030

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

UNCOV
1034
                switch (m->type) {
×
1035

1036
                case CUSTOM_MOUNT_BIND:
×
1037
                        r = mount_bind(dest, m, uid_shift, uid_range);
×
UNCOV
1038
                        break;
×
1039

1040
                case CUSTOM_MOUNT_TMPFS:
×
UNCOV
1041
                        r = mount_tmpfs(dest, m, uid_shift, selinux_apifs_context);
×
UNCOV
1042
                        break;
×
1043

UNCOV
1044
                case CUSTOM_MOUNT_OVERLAY:
×
UNCOV
1045
                        r = mount_overlay(dest, m);
×
UNCOV
1046
                        break;
×
1047

UNCOV
1048
                case CUSTOM_MOUNT_INACCESSIBLE:
×
UNCOV
1049
                        r = mount_inaccessible(dest, m);
×
1050
                        break;
×
1051

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

UNCOV
1056
                default:
×
UNCOV
1057
                        assert_not_reached();
×
1058
                }
1059

UNCOV
1060
                if (r < 0)
×
1061
                        return r;
1062
        }
1063

1064
        return 0;
1065
}
1066

1067
bool has_custom_root_mount(const CustomMount *mounts, size_t n) {
×
1068
        for (size_t i = 0; i < n; i++)
×
UNCOV
1069
                if (path_equal(mounts[i].destination, "/"))
×
1070
                        return true;
1071

1072
        return false;
1073
}
1074

1075
static int setup_volatile_state(const char *directory) {
×
1076
        int r;
×
1077

1078
        assert(directory);
×
1079

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

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

1087
        return 0;
1088
}
1089

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

UNCOV
1095
        assert(directory);
×
1096

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

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

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

1111
        return mount_nofollow_verbose(LOG_ERR, "tmpfs", p, "tmpfs", MS_STRICTATIME, options);
×
1112
}
1113

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

UNCOV
1122
        assert(directory);
×
1123

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

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

1135
                /* ENOENT is fine, just means the image is probably just a naked /usr and we can create the
1136
                 * rest. */
1137
        } else if (S_ISDIR(st.st_mode))
×
UNCOV
1138
                return log_error_errno(SYNTHETIC_ERRNO(EISDIR),
×
1139
                                       "Sorry, --volatile=yes mode is not supported with OS images that have not merged /bin/, /sbin/, /lib/, /lib64/ into /usr/. "
1140
                                       "Please work with your distribution and help them adopt the merged /usr scheme.");
1141
        else if (!S_ISLNK(st.st_mode))
×
UNCOV
1142
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1143
                                       "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).");
1144

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

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

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

1160
        tmpfs_mounted = true;
×
1161

1162
        f = prefix_roota(directory, "/usr");
×
1163
        t = prefix_roota(template, "/usr");
×
1164

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

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

1175
        bind_mounted = true;
×
1176

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

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

1187
        (void) rmdir(template);
×
1188

1189
        return 0;
×
1190

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

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

1198
        return r;
1199
}
1200

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

1208
        assert(directory);
×
1209

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

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

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

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

1227
        tmpfs_mounted = true;
×
1228

1229
        upper = strjoina(template, "/upper");
×
1230
        work = strjoina(template, "/work");
×
1231

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

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

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

UNCOV
1252
        options = strjoina("lowerdir=", escaped_directory, ",upperdir=", escaped_upper, ",workdir=", escaped_work);
×
1253
        r = mount_nofollow_verbose(LOG_ERR, "overlay", directory, "overlay", 0, options);
×
1254

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

1259
        return r;
1260
}
1261

UNCOV
1262
int setup_volatile_mode(
×
1263
                const char *directory,
1264
                VolatileMode mode,
1265
                uid_t uid_shift,
1266
                const char *selinux_apifs_context) {
1267

UNCOV
1268
        switch (mode) {
×
1269

UNCOV
1270
        case VOLATILE_YES:
×
UNCOV
1271
                return setup_volatile_yes(directory, uid_shift, selinux_apifs_context);
×
1272

1273
        case VOLATILE_STATE:
×
UNCOV
1274
                return setup_volatile_state(directory);
×
1275

1276
        case VOLATILE_OVERLAY:
×
UNCOV
1277
                return setup_volatile_overlay(directory, uid_shift, selinux_apifs_context);
×
1278

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

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

1290
        switch (mode) {
×
1291

1292
        case VOLATILE_STATE:
×
1293
                return setup_volatile_state_after_remount_idmap(directory, uid_shift, selinux_apifs_context);
×
1294

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

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

1306
        assert(pivot_root_new);
×
UNCOV
1307
        assert(pivot_root_old);
×
1308

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

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

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

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

UNCOV
1331
        return 0;
×
1332
}
1333

UNCOV
1334
int setup_pivot_root(const char *directory, const char *pivot_root_new, const char *pivot_root_old) {
×
UNCOV
1335
        _cleanup_free_ char *directory_pivot_root_new = NULL;
×
UNCOV
1336
        _cleanup_free_ char *pivot_tmp_pivot_root_old = NULL;
×
UNCOV
1337
        _cleanup_(rmdir_and_freep) char *pivot_tmp = NULL;
×
UNCOV
1338
        int r;
×
1339

UNCOV
1340
        assert(directory);
×
1341

UNCOV
1342
        if (!pivot_root_new)
×
1343
                return 0;
1344

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

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

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

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

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

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

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

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

1404
        return 0;
1405
}
1406

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

UNCOV
1410
int pin_fully_visible_api_fs(void) {
×
UNCOV
1411
        int r;
×
1412

1413
        log_debug("Pinning fully visible API FS");
×
1414

UNCOV
1415
        (void) mkdir_p(NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS, 0755);
×
1416
        (void) mkdir_p(NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS, 0755);
×
1417

UNCOV
1418
        r = mount_follow_verbose(LOG_ERR, "proc", NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS, "proc", PROC_DEFAULT_MOUNT_FLAGS, NULL);
×
1419
        if (r < 0)
×
1420
                return r;
1421

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

1426
        return 0;
1427
}
1428

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

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

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

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

1442
        return 0;
1443
}
1444

UNCOV
1445
int wipe_fully_visible_api_fs(int mntns_fd) {
×
1446
        _cleanup_close_ int orig_mntns_fd = -EBADF;
×
UNCOV
1447
        int r, rr;
×
1448

UNCOV
1449
        log_debug("Wiping fully visible API FS");
×
1450

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

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

UNCOV
1463
        rr = do_wipe_fully_visible_api_fs();
×
1464

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

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