]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/nspawn/nspawn-mount.c
repart: Allow target directory excludes
[thirdparty/systemd.git] / src / nspawn / nspawn-mount.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
e83bebef 2
4f5dd394 3#include <sys/mount.h>
07630cea 4#include <linux/magic.h>
e83bebef 5
b5efdb8a 6#include "alloc-util.h"
f461a28d 7#include "chase.h"
4f5dd394 8#include "escape.h"
0996ef00 9#include "fd-util.h"
ca78ad1d 10#include "format-util.h"
f4f15635 11#include "fs-util.h"
e83bebef 12#include "label.h"
35cd0ba5 13#include "mkdir-label.h"
4349cd7c 14#include "mount-util.h"
049af8ad 15#include "mountpoint-util.h"
b71a0192 16#include "namespace-util.h"
6bedfcbb
LP
17#include "nspawn-mount.h"
18#include "parse-util.h"
4f5dd394
LP
19#include "path-util.h"
20#include "rm-rf.h"
e83bebef 21#include "set.h"
760877e9 22#include "sort-util.h"
8fcde012 23#include "stat-util.h"
07630cea 24#include "string-util.h"
4f5dd394 25#include "strv.h"
e4de7287 26#include "tmpfile-util.h"
ee104e11 27#include "user-util.h"
e83bebef 28
88614c8a 29CustomMount* custom_mount_add(CustomMount **l, size_t *n, CustomMountType t) {
e83bebef
LP
30 CustomMount *c, *ret;
31
32 assert(l);
33 assert(n);
34 assert(t >= 0);
35 assert(t < _CUSTOM_MOUNT_TYPE_MAX);
36
aa484f35 37 c = reallocarray(*l, *n + 1, sizeof(CustomMount));
e83bebef
LP
38 if (!c)
39 return NULL;
40
41 *l = c;
42 ret = *l + *n;
43 (*n)++;
44
511a8cfe
LP
45 *ret = (CustomMount) {
46 .type = t
47 };
e83bebef
LP
48
49 return ret;
50}
51
88614c8a 52void custom_mount_free_all(CustomMount *l, size_t n) {
d2b99ed7 53 for (size_t i = 0; i < n; i++) {
e83bebef
LP
54 CustomMount *m = l + i;
55
56 free(m->source);
57 free(m->destination);
58 free(m->options);
59
60 if (m->work_dir) {
61 (void) rm_rf(m->work_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
62 free(m->work_dir);
63 }
64
c7a4890c
LP
65 if (m->rm_rf_tmpdir) {
66 (void) rm_rf(m->rm_rf_tmpdir, REMOVE_ROOT|REMOVE_PHYSICAL);
67 free(m->rm_rf_tmpdir);
68 }
69
e83bebef 70 strv_free(m->lower);
de40a303 71 free(m->type_argument);
e83bebef
LP
72 }
73
74 free(l);
75}
76
93bab288 77static int custom_mount_compare(const CustomMount *a, const CustomMount *b) {
e83bebef
LP
78 int r;
79
93bab288 80 r = path_compare(a->destination, b->destination);
e83bebef
LP
81 if (r != 0)
82 return r;
83
93bab288 84 return CMP(a->type, b->type);
e83bebef
LP
85}
86
448f7377 87static int source_path_parse(const char *p, char **ret) {
86c0dd4a 88 assert(p);
448f7377 89 assert(ret);
86c0dd4a 90
448f7377
DDM
91 if (isempty(p))
92 return -EINVAL;
93
94 if (*p == '+') {
95 if (!path_is_absolute(p + 1))
96 return -EINVAL;
97
98 char *s = strdup(p);
99 if (!s)
100 return -ENOMEM;
101
102 *ret = TAKE_PTR(s);
103 return 0;
104 }
105
106 return path_make_absolute_cwd(p, ret);
107}
108
109static int source_path_parse_nullable(const char *p, char **ret) {
110 assert(p);
111 assert(ret);
112
113 if (isempty(p)) {
114 *ret = NULL;
115 return 0;
116 }
86c0dd4a 117
448f7377 118 return source_path_parse(p, ret);
86c0dd4a
LP
119}
120
121static char *resolve_source_path(const char *dest, const char *source) {
86c0dd4a
LP
122 if (!source)
123 return NULL;
124
125 if (source[0] == '+')
c6134d3e 126 return path_join(dest, source + 1);
86c0dd4a
LP
127
128 return strdup(source);
129}
130
d0556c55
LP
131static int allocate_temporary_source(CustomMount *m) {
132 assert(m);
133 assert(!m->source);
134 assert(!m->rm_rf_tmpdir);
135
136 m->rm_rf_tmpdir = strdup("/var/tmp/nspawn-temp-XXXXXX");
137 if (!m->rm_rf_tmpdir)
138 return log_oom();
139
140 if (!mkdtemp(m->rm_rf_tmpdir)) {
141 m->rm_rf_tmpdir = mfree(m->rm_rf_tmpdir);
142 return log_error_errno(errno, "Failed to acquire temporary directory: %m");
143 }
144
145 m->source = path_join(m->rm_rf_tmpdir, "src");
146 if (!m->source)
147 return log_oom();
148
149 if (mkdir(m->source, 0755) < 0)
150 return log_error_errno(errno, "Failed to create %s: %m", m->source);
151
152 return 0;
153}
154
88614c8a 155int custom_mount_prepare_all(const char *dest, CustomMount *l, size_t n) {
86c0dd4a
LP
156 int r;
157
158 /* Prepare all custom mounts. This will make source we know all temporary directories. This is called in the
159 * parent process, so that we know the temporary directories to remove on exit before we fork off the
160 * children. */
161
162 assert(l || n == 0);
163
164 /* Order the custom mounts, and make sure we have a working directory */
93bab288 165 typesafe_qsort(l, n, custom_mount_compare);
86c0dd4a 166
d2b99ed7 167 for (size_t i = 0; i < n; i++) {
86c0dd4a
LP
168 CustomMount *m = l + i;
169
de40a303
LP
170 /* /proc we mount in the inner child, i.e. when we acquired CLONE_NEWPID. All other mounts we mount
171 * already in the outer child, so that the mounts are already established before CLONE_NEWPID and in
172 * particular CLONE_NEWUSER. This also means any custom mounts below /proc also need to be mounted in
173 * the inner child, not the outer one. Determine this here. */
174 m->in_userns = path_startswith(m->destination, "/proc");
86c0dd4a 175
de40a303
LP
176 if (m->type == CUSTOM_MOUNT_BIND) {
177 if (m->source) {
178 char *s;
86c0dd4a 179
de40a303
LP
180 s = resolve_source_path(dest, m->source);
181 if (!s)
182 return log_oom();
c7a4890c 183
de40a303
LP
184 free_and_replace(m->source, s);
185 } else {
186 /* No source specified? In that case, use a throw-away temporary directory in /var/tmp */
c7a4890c 187
d0556c55
LP
188 r = allocate_temporary_source(m);
189 if (r < 0)
190 return r;
de40a303 191 }
86c0dd4a
LP
192 }
193
194 if (m->type == CUSTOM_MOUNT_OVERLAY) {
86c0dd4a
LP
195 STRV_FOREACH(j, m->lower) {
196 char *s;
197
198 s = resolve_source_path(dest, *j);
199 if (!s)
200 return log_oom();
201
10af01a5 202 free_and_replace(*j, s);
86c0dd4a
LP
203 }
204
d0556c55
LP
205 if (m->source) {
206 char *s;
207
208 s = resolve_source_path(dest, m->source);
209 if (!s)
210 return log_oom();
211
212 free_and_replace(m->source, s);
213 } else {
214 r = allocate_temporary_source(m);
215 if (r < 0)
216 return r;
217 }
218
86c0dd4a
LP
219 if (m->work_dir) {
220 char *s;
221
222 s = resolve_source_path(dest, m->work_dir);
223 if (!s)
224 return log_oom();
225
10af01a5 226 free_and_replace(m->work_dir, s);
86c0dd4a 227 } else {
86c0dd4a
LP
228 r = tempfn_random(m->source, NULL, &m->work_dir);
229 if (r < 0)
230 return log_error_errno(r, "Failed to acquire working directory: %m");
231 }
232
233 (void) mkdir_label(m->work_dir, 0700);
234 }
235 }
236
237 return 0;
238}
239
88614c8a 240int bind_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) {
448f7377 241 _cleanup_free_ char *source = NULL, *destination = NULL, *opts = NULL, *p = NULL;
e83bebef
LP
242 CustomMount *m;
243 int r;
244
245 assert(l);
246 assert(n);
247
a50947c5 248 r = extract_many_words(&s, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &source, &destination, NULL);
e83bebef
LP
249 if (r < 0)
250 return r;
251 if (r == 0)
252 return -EINVAL;
e83bebef 253 if (r == 1) {
86c0dd4a 254 destination = strdup(source[0] == '+' ? source+1 : source);
e83bebef
LP
255 if (!destination)
256 return -ENOMEM;
257 }
a50947c5
DDM
258 if (r == 2 && !isempty(s)) {
259 opts = strdup(s);
e83bebef
LP
260 if (!opts)
261 return -ENOMEM;
262 }
263
448f7377
DDM
264 r = source_path_parse_nullable(source, &p);
265 if (r < 0)
266 return r;
c7a4890c 267
e83bebef
LP
268 if (!path_is_absolute(destination))
269 return -EINVAL;
270
271 m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND);
272 if (!m)
48cbe5f8 273 return -ENOMEM;
e83bebef 274
448f7377 275 m->source = TAKE_PTR(p);
0e636bf5 276 m->destination = TAKE_PTR(destination);
e83bebef 277 m->read_only = read_only;
0e636bf5 278 m->options = TAKE_PTR(opts);
de40a303 279
e83bebef
LP
280 return 0;
281}
282
88614c8a 283int tmpfs_mount_parse(CustomMount **l, size_t *n, const char *s) {
e83bebef 284 _cleanup_free_ char *path = NULL, *opts = NULL;
99534007 285 const char *p = ASSERT_PTR(s);
e83bebef
LP
286 CustomMount *m;
287 int r;
288
289 assert(l);
290 assert(n);
e83bebef
LP
291
292 r = extract_first_word(&p, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
293 if (r < 0)
294 return r;
295 if (r == 0)
296 return -EINVAL;
297
298 if (isempty(p))
299 opts = strdup("mode=0755");
300 else
301 opts = strdup(p);
302 if (!opts)
303 return -ENOMEM;
304
305 if (!path_is_absolute(path))
306 return -EINVAL;
307
308 m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS);
309 if (!m)
310 return -ENOMEM;
311
1cc6c93a
YW
312 m->destination = TAKE_PTR(path);
313 m->options = TAKE_PTR(opts);
e83bebef 314
e83bebef
LP
315 return 0;
316}
317
88614c8a 318int overlay_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) {
ad85779a
LP
319 _cleanup_free_ char *upper = NULL, *destination = NULL;
320 _cleanup_strv_free_ char **lower = NULL;
321 CustomMount *m;
448f7377 322 int r, k;
ad85779a 323
90e30d76 324 k = strv_split_full(&lower, s, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
86c0dd4a
LP
325 if (k < 0)
326 return k;
ad85779a
LP
327 if (k < 2)
328 return -EADDRNOTAVAIL;
329 if (k == 2) {
448f7377
DDM
330 _cleanup_free_ char *p = NULL;
331
86c0dd4a
LP
332 /* If two parameters are specified, the first one is the lower, the second one the upper directory. And
333 * we'll also define the destination mount point the same as the upper. */
334
448f7377
DDM
335 r = source_path_parse(lower[0], &p);
336 if (r < 0)
337 return r;
338
339 free_and_replace(lower[0], p);
340
341 r = source_path_parse(lower[1], &p);
342 if (r < 0)
343 return r;
344
345 free_and_replace(lower[1], p);
86c0dd4a 346
ae2a15bc 347 upper = TAKE_PTR(lower[1]);
ad85779a 348
86c0dd4a 349 destination = strdup(upper[0] == '+' ? upper+1 : upper); /* take the destination without "+" prefix */
ad85779a
LP
350 if (!destination)
351 return -ENOMEM;
ad85779a 352 } else {
448f7377
DDM
353 _cleanup_free_ char *p = NULL;
354
86c0dd4a
LP
355 /* If more than two parameters are specified, the last one is the destination, the second to last one
356 * the "upper", and all before that the "lower" directories. */
357
ad85779a 358 destination = lower[k - 1];
ae2a15bc 359 upper = TAKE_PTR(lower[k - 2]);
86c0dd4a 360
448f7377
DDM
361 STRV_FOREACH(i, lower) {
362 r = source_path_parse(*i, &p);
363 if (r < 0)
364 return r;
365
366 free_and_replace(*i, p);
367 }
c7a4890c
LP
368
369 /* If the upper directory is unspecified, then let's create it automatically as a throw-away directory
370 * in /var/tmp */
448f7377
DDM
371 r = source_path_parse_nullable(upper, &p);
372 if (r < 0)
373 return r;
374
375 free_and_replace(upper, p);
c7a4890c 376
86c0dd4a
LP
377 if (!path_is_absolute(destination))
378 return -EINVAL;
ad85779a
LP
379 }
380
381 m = custom_mount_add(l, n, CUSTOM_MOUNT_OVERLAY);
382 if (!m)
383 return -ENOMEM;
384
1cc6c93a
YW
385 m->destination = TAKE_PTR(destination);
386 m->source = TAKE_PTR(upper);
387 m->lower = TAKE_PTR(lower);
ad85779a
LP
388 m->read_only = read_only;
389
ad85779a
LP
390 return 0;
391}
392
de40a303
LP
393int inaccessible_mount_parse(CustomMount **l, size_t *n, const char *s) {
394 _cleanup_free_ char *path = NULL;
395 CustomMount *m;
396
397 assert(l);
398 assert(n);
399 assert(s);
400
401 if (!path_is_absolute(s))
402 return -EINVAL;
403
404 path = strdup(s);
405 if (!path)
406 return -ENOMEM;
407
408 m = custom_mount_add(l, n, CUSTOM_MOUNT_INACCESSIBLE);
409 if (!m)
410 return -ENOMEM;
411
412 m->destination = TAKE_PTR(path);
413 return 0;
414}
415
04029482 416int tmpfs_patch_options(
e83bebef 417 const char *options,
2fa017f1 418 uid_t uid_shift,
e83bebef
LP
419 const char *selinux_apifs_context,
420 char **ret) {
421
cfea7618 422 _cleanup_free_ char *buf = NULL;
e83bebef 423
cfea7618 424 assert(ret);
e83bebef 425
cfea7618
YW
426 if (options) {
427 buf = strdup(options);
428 if (!buf)
429 return -ENOMEM;
e83bebef
LP
430 }
431
cfea7618
YW
432 if (uid_shift != UID_INVALID)
433 if (strextendf_with_separator(&buf, ",", "uid=" UID_FMT ",gid=" UID_FMT, uid_shift, uid_shift) < 0)
e83bebef 434 return -ENOMEM;
e83bebef 435
cfea7618
YW
436#if HAVE_SELINUX
437 if (selinux_apifs_context)
d3d15594 438 if (strextendf_with_separator(&buf, ",", "context=\"%s\"", selinux_apifs_context) < 0)
0996ef00 439 return -ENOMEM;
cfea7618 440#endif
0996ef00 441
cfea7618
YW
442 *ret = TAKE_PTR(buf);
443 return !!*ret;
e83bebef
LP
444}
445
4f086aab 446int mount_sysfs(const char *dest, MountSettingsMask mount_settings) {
5980d463 447 const char *full, *top;
d1678248 448 int r;
4f086aab 449 unsigned long extra_flags = 0;
d8fc6a00
LP
450
451 top = prefix_roota(dest, "/sys");
40fd52f2 452 r = path_is_fs_type(top, SYSFS_MAGIC);
d1678248
ILG
453 if (r < 0)
454 return log_error_errno(r, "Failed to determine filesystem type of %s: %m", top);
455 /* /sys might already be mounted as sysfs by the outer child in the
456 * !netns case. In this case, it's all good. Don't touch it because we
457 * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555.
458 */
459 if (r > 0)
460 return 0;
461
d8fc6a00
LP
462 full = prefix_roota(top, "/full");
463
464 (void) mkdir(full, 0755);
465
bd6609eb 466 if (FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_RO))
4f086aab
SU
467 extra_flags |= MS_RDONLY;
468
511a8cfe
LP
469 r = mount_nofollow_verbose(LOG_ERR, "sysfs", full, "sysfs",
470 MS_NOSUID|MS_NOEXEC|MS_NODEV|extra_flags, NULL);
60e76d48
ZJS
471 if (r < 0)
472 return r;
d8fc6a00
LP
473
474 FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") {
475 _cleanup_free_ char *from = NULL, *to = NULL;
476
c6134d3e 477 from = path_join(full, x);
d8fc6a00
LP
478 if (!from)
479 return log_oom();
480
c6134d3e 481 to = path_join(top, x);
d8fc6a00
LP
482 if (!to)
483 return log_oom();
484
485 (void) mkdir(to, 0755);
486
511a8cfe 487 r = mount_nofollow_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
60e76d48
ZJS
488 if (r < 0)
489 return r;
d8fc6a00 490
511a8cfe
LP
491 r = mount_nofollow_verbose(LOG_ERR, NULL, to, NULL,
492 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
60e76d48
ZJS
493 if (r < 0)
494 return r;
d8fc6a00
LP
495 }
496
30f5d104 497 r = umount_verbose(LOG_ERR, full, UMOUNT_NOFOLLOW);
60e76d48
ZJS
498 if (r < 0)
499 return r;
d8fc6a00
LP
500
501 if (rmdir(full) < 0)
502 return log_error_errno(errno, "Failed to remove %s: %m", full);
503
0996ef00
CB
504 /* Create mountpoint for cgroups. Otherwise we are not allowed since we
505 * remount /sys read-only.
506 */
5980d463 507 const char *x = prefix_roota(top, "/fs/cgroup");
677a72cd 508 (void) mkdir_p(x, 0755);
d8fc6a00 509
511a8cfe
LP
510 return mount_nofollow_verbose(LOG_ERR, NULL, top, NULL,
511 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
d8fc6a00
LP
512}
513
b71a0192
CB
514#define PROC_DEFAULT_MOUNT_FLAGS (MS_NOSUID|MS_NOEXEC|MS_NODEV)
515#define SYS_DEFAULT_MOUNT_FLAGS (MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV)
516
e83bebef 517int mount_all(const char *dest,
4f086aab 518 MountSettingsMask mount_settings,
2fa017f1 519 uid_t uid_shift,
e83bebef
LP
520 const char *selinux_apifs_context) {
521
de40a303
LP
522#define PROC_INACCESSIBLE_REG(path) \
523 { "/run/systemd/inaccessible/reg", (path), NULL, NULL, MS_BIND, \
524 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ \
d4b653c5
LP
525 { NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \
526 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */
527
528#define PROC_READ_ONLY(path) \
529 { (path), (path), NULL, NULL, MS_BIND, \
530 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ \
531 { NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \
532 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */
533
e83bebef
LP
534 typedef struct MountPoint {
535 const char *what;
536 const char *where;
537 const char *type;
538 const char *options;
539 unsigned long flags;
4f086aab 540 MountSettingsMask mount_settings;
e83bebef
LP
541 } MountPoint;
542
543 static const MountPoint mount_table[] = {
d4b653c5 544 /* First we list inner child mounts (i.e. mounts applied *after* entering user namespacing) */
b71a0192 545 { "proc", "/proc", "proc", NULL, PROC_DEFAULT_MOUNT_FLAGS,
511a8cfe 546 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. */
d4b653c5
LP
547
548 { "/proc/sys", "/proc/sys", NULL, NULL, MS_BIND,
549 MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */
550
551 { "/proc/sys/net", "/proc/sys/net", NULL, NULL, MS_BIND,
552 MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_APIVFS_NETNS }, /* (except for this) */
553
554 { NULL, "/proc/sys", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
555 MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* ... then, make it r/o */
556
557 /* Make these files inaccessible to container payloads: they potentially leak information about kernel
558 * internals or the host's execution environment to the container */
de40a303
LP
559 PROC_INACCESSIBLE_REG("/proc/kallsyms"),
560 PROC_INACCESSIBLE_REG("/proc/kcore"),
561 PROC_INACCESSIBLE_REG("/proc/keys"),
562 PROC_INACCESSIBLE_REG("/proc/sysrq-trigger"),
563 PROC_INACCESSIBLE_REG("/proc/timer_list"),
d4b653c5
LP
564
565 /* Make these directories read-only to container payloads: they show hardware information, and in some
566 * cases contain tunables the container really shouldn't have access to. */
567 PROC_READ_ONLY("/proc/acpi"),
568 PROC_READ_ONLY("/proc/apm"),
569 PROC_READ_ONLY("/proc/asound"),
570 PROC_READ_ONLY("/proc/bus"),
571 PROC_READ_ONLY("/proc/fs"),
572 PROC_READ_ONLY("/proc/irq"),
573 PROC_READ_ONLY("/proc/scsi"),
574
e1bb4b0d 575 { "mqueue", "/dev/mqueue", "mqueue", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
dcff2fa5 576 MOUNT_IN_USERNS|MOUNT_MKDIR },
849b9b85 577
d4b653c5 578 /* Then we list outer child mounts (i.e. mounts applied *before* entering user namespacing) */
9f563f27 579 { "tmpfs", "/tmp", "tmpfs", "mode=01777" NESTED_TMPFS_LIMITS, MS_NOSUID|MS_NODEV|MS_STRICTATIME,
dcff2fa5 580 MOUNT_FATAL|MOUNT_APPLY_TMPFS_TMP|MOUNT_MKDIR },
9f563f27 581 { "tmpfs", "/sys", "tmpfs", "mode=0555" TMPFS_LIMITS_SYS, MS_NOSUID|MS_NOEXEC|MS_NODEV,
dcff2fa5 582 MOUNT_FATAL|MOUNT_APPLY_APIVFS_NETNS|MOUNT_MKDIR },
b71a0192 583 { "sysfs", "/sys", "sysfs", NULL, SYS_DEFAULT_MOUNT_FLAGS,
dcff2fa5 584 MOUNT_FATAL|MOUNT_APPLY_APIVFS_RO|MOUNT_MKDIR }, /* skipped if above was mounted */
e1bb4b0d 585 { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
dcff2fa5 586 MOUNT_FATAL|MOUNT_MKDIR }, /* skipped if above was mounted */
9f563f27 587 { "tmpfs", "/dev", "tmpfs", "mode=0755" TMPFS_LIMITS_PRIVATE_DEV, MS_NOSUID|MS_STRICTATIME,
dcff2fa5 588 MOUNT_FATAL|MOUNT_MKDIR },
9f563f27 589 { "tmpfs", "/dev/shm", "tmpfs", "mode=01777" NESTED_TMPFS_LIMITS, MS_NOSUID|MS_NODEV|MS_STRICTATIME,
dcff2fa5 590 MOUNT_FATAL|MOUNT_MKDIR },
9f563f27 591 { "tmpfs", "/run", "tmpfs", "mode=0755" TMPFS_LIMITS_RUN, MS_NOSUID|MS_NODEV|MS_STRICTATIME,
dcff2fa5 592 MOUNT_FATAL|MOUNT_MKDIR },
d64e32c2
LP
593 { "/run/host", "/run/host", NULL, NULL, MS_BIND,
594 MOUNT_FATAL|MOUNT_MKDIR|MOUNT_PREFIX_ROOT }, /* Prepare this so that we can make it read-only when we are done */
595 { "/etc/os-release", "/run/host/os-release", NULL, NULL, MS_BIND,
596 MOUNT_TOUCH }, /* As per kernel interface requirements, bind mount first (creating mount points) and make read-only later */
597 { "/usr/lib/os-release", "/run/host/os-release", NULL, NULL, MS_BIND,
598 MOUNT_FATAL }, /* If /etc/os-release doesn't exist use the version in /usr/lib as fallback */
599 { NULL, "/run/host/os-release", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
600 MOUNT_FATAL },
56339a10
LP
601 { NULL, "/run/host/os-release", NULL, NULL, MS_PRIVATE,
602 MOUNT_FATAL }, /* Turn off propagation (we only want that for the mount propagation tunnel dir) */
d64e32c2
LP
603 { NULL, "/run/host", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
604 MOUNT_FATAL|MOUNT_IN_USERNS },
349cc4a5 605#if HAVE_SELINUX
e1bb4b0d 606 { "/sys/fs/selinux", "/sys/fs/selinux", NULL, NULL, MS_BIND,
6fe01ced 607 MOUNT_MKDIR }, /* Bind mount first (mkdir/chown the mount point in case /sys/ is mounted as minimal skeleton tmpfs) */
e1bb4b0d 608 { NULL, "/sys/fs/selinux", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
6fe01ced 609 0 }, /* Then, make it r/o (don't mkdir/chown the mount point here, the previous entry already did that) */
56339a10
LP
610 { NULL, "/sys/fs/selinux", NULL, NULL, MS_PRIVATE,
611 0 }, /* Turn off propagation (we only want that for the mount propagation tunnel dir) */
e83bebef
LP
612#endif
613 };
614
bd6609eb
DDM
615 bool use_userns = FLAGS_SET(mount_settings, MOUNT_USE_USERNS);
616 bool netns = FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_NETNS);
617 bool ro = FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_RO);
618 bool in_userns = FLAGS_SET(mount_settings, MOUNT_IN_USERNS);
619 bool tmpfs_tmp = FLAGS_SET(mount_settings, MOUNT_APPLY_TMPFS_TMP);
88614c8a 620 int r;
e83bebef 621
d2b99ed7 622 for (size_t k = 0; k < ELEMENTSOF(mount_table); k++) {
d64e32c2 623 _cleanup_free_ char *where = NULL, *options = NULL, *prefixed = NULL;
bd6609eb 624 bool fatal = FLAGS_SET(mount_table[k].mount_settings, MOUNT_FATAL);
d64e32c2 625 const char *o;
4f086aab 626
bd6609eb 627 if (in_userns != FLAGS_SET(mount_table[k].mount_settings, MOUNT_IN_USERNS))
4f086aab 628 continue;
e83bebef 629
bd6609eb 630 if (!netns && FLAGS_SET(mount_table[k].mount_settings, MOUNT_APPLY_APIVFS_NETNS))
d1678248
ILG
631 continue;
632
bd6609eb 633 if (!ro && FLAGS_SET(mount_table[k].mount_settings, MOUNT_APPLY_APIVFS_RO))
e83bebef
LP
634 continue;
635
bd6609eb 636 if (!tmpfs_tmp && FLAGS_SET(mount_table[k].mount_settings, MOUNT_APPLY_TMPFS_TMP))
1099ceeb
LP
637 continue;
638
f461a28d 639 r = chase(mount_table[k].where, dest, CHASE_NONEXISTENT|CHASE_PREFIX_ROOT, &where, NULL);
8ce48cf0 640 if (r < 0)
e85c644e 641 return log_error_errno(r, "Failed to resolve %s%s: %m", strempty(dest), mount_table[k].where);
e83bebef 642
e83bebef 643 /* Skip this entry if it is not a remount. */
de40a303
LP
644 if (mount_table[k].what) {
645 r = path_is_mount_point(where, NULL, 0);
646 if (r < 0 && r != -ENOENT)
647 return log_error_errno(r, "Failed to detect whether %s is a mount point: %m", where);
648 if (r > 0)
649 continue;
650 }
e83bebef 651
d64e32c2 652 if ((mount_table[k].mount_settings & (MOUNT_MKDIR|MOUNT_TOUCH)) != 0) {
b3b1a08a 653 uid_t u = (use_userns && !in_userns) ? uid_shift : UID_INVALID;
e1bb4b0d
LB
654
655 if (FLAGS_SET(mount_table[k].mount_settings, MOUNT_TOUCH))
656 r = mkdir_parents_safe(dest, where, 0755, u, u, 0);
657 else
658 r = mkdir_p_safe(dest, where, 0755, u, u, 0);
dcff2fa5
LP
659 if (r < 0 && r != -EEXIST) {
660 if (fatal && r != -EROFS)
661 return log_error_errno(r, "Failed to create directory %s: %m", where);
e83bebef 662
dcff2fa5
LP
663 log_debug_errno(r, "Failed to create directory %s: %m", where);
664
665 /* If we failed mkdir() or chown() due to the root directory being read only,
666 * attempt to mount this fs anyway and let mount_verbose log any errors */
667 if (r != -EROFS)
668 continue;
669 }
d64e32c2
LP
670 }
671
672 if (FLAGS_SET(mount_table[k].mount_settings, MOUNT_TOUCH)) {
673 r = touch(where);
674 if (r < 0 && r != -EEXIST) {
675 if (fatal && r != -EROFS)
676 return log_error_errno(r, "Failed to create file %s: %m", where);
677
678 log_debug_errno(r, "Failed to create file %s: %m", where);
679 if (r != -EROFS)
680 continue;
e1bb4b0d 681 }
e83bebef
LP
682 }
683
684 o = mount_table[k].options;
685 if (streq_ptr(mount_table[k].type, "tmpfs")) {
2fa017f1 686 r = tmpfs_patch_options(o, in_userns ? 0 : uid_shift, selinux_apifs_context, &options);
e83bebef
LP
687 if (r < 0)
688 return log_oom();
689 if (r > 0)
690 o = options;
691 }
692
d64e32c2
LP
693 if (FLAGS_SET(mount_table[k].mount_settings, MOUNT_PREFIX_ROOT)) {
694 /* Optionally prefix the mount source with the root dir. This is useful in bind
695 * mounts to be created within the container image before we transition into it. Note
696 * that MOUNT_IN_USERNS is run after we transitioned hence prefixing is not ncessary
697 * for those. */
f461a28d 698 r = chase(mount_table[k].what, dest, CHASE_PREFIX_ROOT, &prefixed, NULL);
d64e32c2 699 if (r < 0)
e85c644e 700 return log_error_errno(r, "Failed to resolve %s%s: %m", strempty(dest), mount_table[k].what);
d64e32c2
LP
701 }
702
511a8cfe
LP
703 r = mount_verbose_full(
704 fatal ? LOG_ERR : LOG_DEBUG,
705 prefixed ?: mount_table[k].what,
706 where,
707 mount_table[k].type,
708 mount_table[k].flags,
709 o,
710 FLAGS_SET(mount_table[k].mount_settings, MOUNT_FOLLOW_SYMLINKS));
4f086aab 711 if (r < 0 && fatal)
60e76d48 712 return r;
e83bebef
LP
713 }
714
715 return 0;
716}
717
1aa18710 718static int parse_mount_bind_options(const char *options, unsigned long *mount_flags, char **mount_opts, RemountIdmapping *idmapping) {
a11fd406
ILG
719 unsigned long flags = *mount_flags;
720 char *opts = NULL;
1aa18710 721 RemountIdmapping new_idmapping = *idmapping;
a11fd406
ILG
722 int r;
723
724 assert(options);
725
726 for (;;) {
727 _cleanup_free_ char *word = NULL;
728
d2b99ed7 729 r = extract_first_word(&options, &word, ",", 0);
a11fd406
ILG
730 if (r < 0)
731 return log_error_errno(r, "Failed to extract mount option: %m");
732 if (r == 0)
733 break;
734
735 if (streq(word, "rbind"))
736 flags |= MS_REC;
737 else if (streq(word, "norbind"))
738 flags &= ~MS_REC;
c0c8f718 739 else if (streq(word, "idmap"))
1aa18710 740 new_idmapping = REMOUNT_IDMAPPING_HOST_ROOT;
c0c8f718 741 else if (streq(word, "noidmap"))
1aa18710 742 new_idmapping = REMOUNT_IDMAPPING_NONE;
2b2777ed
QD
743 else if (streq(word, "rootidmap"))
744 new_idmapping = REMOUNT_IDMAPPING_HOST_OWNER;
d2b99ed7 745 else
38288f0b 746 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
d2b99ed7 747 "Invalid bind mount option: %s", word);
a11fd406
ILG
748 }
749
750 *mount_flags = flags;
1aa18710 751 *idmapping = new_idmapping;
a11fd406
ILG
752 /* in the future mount_opts will hold string options for mount(2) */
753 *mount_opts = opts;
754
755 return 0;
756}
757
c0c8f718 758static int mount_bind(const char *dest, CustomMount *m, uid_t uid_shift, uid_t uid_range) {
a11fd406
ILG
759 _cleanup_free_ char *mount_opts = NULL, *where = NULL;
760 unsigned long mount_flags = MS_BIND | MS_REC;
68cf43c3 761 struct stat source_st, dest_st;
e83bebef 762 int r;
1aa18710 763 RemountIdmapping idmapping = REMOUNT_IDMAPPING_NONE;
e83bebef 764
86c0dd4a 765 assert(dest);
e83bebef
LP
766 assert(m);
767
a11fd406 768 if (m->options) {
1aa18710 769 r = parse_mount_bind_options(m->options, &mount_flags, &mount_opts, &idmapping);
a11fd406
ILG
770 if (r < 0)
771 return r;
772 }
773
07bca16f
LP
774 /* If this is a bind mount from a temporary sources change ownership of the source to the container's
775 * root UID. Otherwise it would always show up as "nobody" if user namespacing is used. */
776 if (m->rm_rf_tmpdir && chown(m->source, uid_shift, uid_shift) < 0)
777 return log_error_errno(errno, "Failed to chown %s: %m", m->source);
778
e83bebef
LP
779 if (stat(m->source, &source_st) < 0)
780 return log_error_errno(errno, "Failed to stat %s: %m", m->source);
781
f461a28d 782 r = chase(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
68cf43c3 783 if (r < 0)
ec57bd42 784 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
8ce48cf0
LP
785 if (r > 0) { /* Path exists already? */
786
787 if (stat(where, &dest_st) < 0)
788 return log_error_errno(errno, "Failed to stat %s: %m", where);
e83bebef 789
baaa35ad
ZJS
790 if (S_ISDIR(source_st.st_mode) && !S_ISDIR(dest_st.st_mode))
791 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
792 "Cannot bind mount directory %s on file %s.",
793 m->source, where);
794
795 if (!S_ISDIR(source_st.st_mode) && S_ISDIR(dest_st.st_mode))
796 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
797 "Cannot bind mount file %s on directory %s.",
798 m->source, where);
e83bebef 799
8ce48cf0 800 } else { /* Path doesn't exist yet? */
0a67965f 801 r = mkdir_parents_safe_label(dest, where, 0755, uid_shift, uid_shift, MKDIR_IGNORE_EXISTING);
e83bebef
LP
802 if (r < 0)
803 return log_error_errno(r, "Failed to make parents of %s: %m", where);
b97e83cb
BN
804
805 /* Create the mount point. Any non-directory file can be
806 * mounted on any non-directory file (regular, fifo, socket,
807 * char, block).
808 */
809 if (S_ISDIR(source_st.st_mode))
810 r = mkdir_label(where, 0755);
811 else
812 r = touch(where);
813 if (r < 0)
814 return log_error_errno(r, "Failed to create mount point %s: %m", where);
0a67965f
DDM
815
816 if (chown(where, uid_shift, uid_shift) < 0)
817 return log_error_errno(errno, "Failed to chown %s: %m", where);
8ce48cf0 818 }
e83bebef 819
511a8cfe 820 r = mount_nofollow_verbose(LOG_ERR, m->source, where, NULL, mount_flags, mount_opts);
60e76d48
ZJS
821 if (r < 0)
822 return r;
e83bebef
LP
823
824 if (m->read_only) {
64e82c19 825 r = bind_remount_recursive(where, MS_RDONLY, MS_RDONLY, NULL);
e83bebef
LP
826 if (r < 0)
827 return log_error_errno(r, "Read-only bind mount failed: %m");
828 }
829
1aa18710 830 if (idmapping != REMOUNT_IDMAPPING_NONE) {
2b2777ed 831 r = remount_idmap(where, uid_shift, uid_range, source_st.st_uid, idmapping);
c0c8f718
AV
832 if (r < 0)
833 return log_error_errno(r, "Failed to map ids for bind mount %s: %m", where);
834 }
835
e83bebef
LP
836 return 0;
837}
838
e091a5df 839static int mount_tmpfs(const char *dest, CustomMount *m, uid_t uid_shift, const char *selinux_apifs_context) {
68cf43c3
LP
840 const char *options;
841 _cleanup_free_ char *buf = NULL, *where = NULL;
e83bebef
LP
842 int r;
843
844 assert(dest);
845 assert(m);
846
f461a28d 847 r = chase(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
68cf43c3 848 if (r < 0)
ec57bd42 849 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
8ce48cf0
LP
850 if (r == 0) { /* Doesn't exist yet? */
851 r = mkdir_p_label(where, 0755);
852 if (r < 0)
853 return log_error_errno(r, "Creating mount point for tmpfs %s failed: %m", where);
854 }
e83bebef 855
2fa017f1 856 r = tmpfs_patch_options(m->options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
e83bebef
LP
857 if (r < 0)
858 return log_oom();
859 options = r > 0 ? buf : m->options;
860
511a8cfe 861 return mount_nofollow_verbose(LOG_ERR, "tmpfs", where, "tmpfs", MS_NODEV|MS_STRICTATIME, options);
e83bebef
LP
862}
863
86c0dd4a 864static char *joined_and_escaped_lower_dirs(char **lower) {
e83bebef
LP
865 _cleanup_strv_free_ char **sv = NULL;
866
867 sv = strv_copy(lower);
868 if (!sv)
869 return NULL;
870
871 strv_reverse(sv);
872
873 if (!strv_shell_escape(sv, ",:"))
874 return NULL;
875
876 return strv_join(sv, ":");
877}
878
879static int mount_overlay(const char *dest, CustomMount *m) {
86c0dd4a 880 _cleanup_free_ char *lower = NULL, *where = NULL, *escaped_source = NULL;
68cf43c3 881 const char *options;
e83bebef
LP
882 int r;
883
884 assert(dest);
885 assert(m);
886
f461a28d 887 r = chase(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
68cf43c3 888 if (r < 0)
ec57bd42 889 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
8ce48cf0
LP
890 if (r == 0) { /* Doesn't exist yet? */
891 r = mkdir_label(where, 0755);
892 if (r < 0)
893 return log_error_errno(r, "Creating mount point for overlay %s failed: %m", where);
894 }
e83bebef
LP
895
896 (void) mkdir_p_label(m->source, 0755);
897
898 lower = joined_and_escaped_lower_dirs(m->lower);
899 if (!lower)
900 return log_oom();
901
86c0dd4a
LP
902 escaped_source = shell_escape(m->source, ",:");
903 if (!escaped_source)
904 return log_oom();
e83bebef 905
86c0dd4a 906 if (m->read_only)
e83bebef 907 options = strjoina("lowerdir=", escaped_source, ":", lower);
86c0dd4a
LP
908 else {
909 _cleanup_free_ char *escaped_work_dir = NULL;
e83bebef 910
e83bebef
LP
911 escaped_work_dir = shell_escape(m->work_dir, ",:");
912 if (!escaped_work_dir)
913 return log_oom();
914
915 options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir);
916 }
917
511a8cfe 918 return mount_nofollow_verbose(LOG_ERR, "overlay", where, "overlay", m->read_only ? MS_RDONLY : 0, options);
e83bebef
LP
919}
920
de40a303 921static int mount_inaccessible(const char *dest, CustomMount *m) {
e5f10caf 922 _cleanup_free_ char *where = NULL, *source = NULL;
de40a303
LP
923 struct stat st;
924 int r;
925
926 assert(dest);
927 assert(m);
928
f461a28d 929 r = chase_and_stat(m->destination, dest, CHASE_PREFIX_ROOT, &where, &st);
de40a303
LP
930 if (r < 0) {
931 log_full_errno(m->graceful ? LOG_DEBUG : LOG_ERR, r, "Failed to resolve %s/%s: %m", dest, m->destination);
932 return m->graceful ? 0 : r;
933 }
934
48b747fa 935 r = mode_to_inaccessible_node(NULL, st.st_mode, &source);
e5f10caf
AZ
936 if (r < 0)
937 return m->graceful ? 0 : r;
de40a303 938
511a8cfe 939 r = mount_nofollow_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, source, where, NULL, MS_BIND, NULL);
de40a303
LP
940 if (r < 0)
941 return m->graceful ? 0 : r;
942
511a8cfe 943 r = mount_nofollow_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, NULL, where, NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, NULL);
2c9b7a7e 944 if (r < 0) {
30f5d104 945 (void) umount_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, where, UMOUNT_NOFOLLOW);
de40a303 946 return m->graceful ? 0 : r;
2c9b7a7e 947 }
de40a303
LP
948
949 return 0;
950}
951
952static int mount_arbitrary(const char *dest, CustomMount *m) {
953 _cleanup_free_ char *where = NULL;
954 int r;
955
956 assert(dest);
957 assert(m);
958
f461a28d 959 r = chase(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
de40a303
LP
960 if (r < 0)
961 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
962 if (r == 0) { /* Doesn't exist yet? */
963 r = mkdir_p_label(where, 0755);
964 if (r < 0)
965 return log_error_errno(r, "Creating mount point for mount %s failed: %m", where);
966 }
967
511a8cfe 968 return mount_nofollow_verbose(LOG_ERR, m->source, where, m->type_argument, 0, m->options);
de40a303
LP
969}
970
e83bebef
LP
971int mount_custom(
972 const char *dest,
88614c8a 973 CustomMount *mounts, size_t n,
e091a5df 974 uid_t uid_shift,
c0c8f718 975 uid_t uid_range,
de40a303 976 const char *selinux_apifs_context,
5f0a6347 977 MountSettingsMask mount_settings) {
e83bebef
LP
978 int r;
979
980 assert(dest);
981
d2b99ed7 982 for (size_t i = 0; i < n; i++) {
e83bebef
LP
983 CustomMount *m = mounts + i;
984
bd6609eb 985 if (FLAGS_SET(mount_settings, MOUNT_IN_USERNS) != m->in_userns)
5f0a6347
DDM
986 continue;
987
bd6609eb 988 if (FLAGS_SET(mount_settings, MOUNT_ROOT_ONLY) && !path_equal(m->destination, "/"))
5f0a6347
DDM
989 continue;
990
bd6609eb 991 if (FLAGS_SET(mount_settings, MOUNT_NON_ROOT_ONLY) && path_equal(m->destination, "/"))
de40a303
LP
992 continue;
993
e83bebef
LP
994 switch (m->type) {
995
996 case CUSTOM_MOUNT_BIND:
c0c8f718 997 r = mount_bind(dest, m, uid_shift, uid_range);
e83bebef
LP
998 break;
999
1000 case CUSTOM_MOUNT_TMPFS:
e091a5df 1001 r = mount_tmpfs(dest, m, uid_shift, selinux_apifs_context);
e83bebef
LP
1002 break;
1003
1004 case CUSTOM_MOUNT_OVERLAY:
1005 r = mount_overlay(dest, m);
1006 break;
1007
de40a303
LP
1008 case CUSTOM_MOUNT_INACCESSIBLE:
1009 r = mount_inaccessible(dest, m);
1010 break;
1011
1012 case CUSTOM_MOUNT_ARBITRARY:
1013 r = mount_arbitrary(dest, m);
1014 break;
1015
e83bebef 1016 default:
04499a70 1017 assert_not_reached();
e83bebef
LP
1018 }
1019
1020 if (r < 0)
1021 return r;
1022 }
1023
1024 return 0;
1025}
1026
bbd407ea 1027bool has_custom_root_mount(const CustomMount *mounts, size_t n) {
d2b99ed7
ZJS
1028 for (size_t i = 0; i < n; i++)
1029 if (path_equal(mounts[i].destination, "/"))
bbd407ea 1030 return true;
bbd407ea
DDM
1031
1032 return false;
1033}
1034
e091a5df 1035static int setup_volatile_state(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
e83bebef
LP
1036 _cleanup_free_ char *buf = NULL;
1037 const char *p, *options;
1038 int r;
1039
1040 assert(directory);
1041
e5b43a04 1042 /* --volatile=state means we simply overmount /var with a tmpfs, and the rest read-only. */
e83bebef 1043
64e82c19 1044 r = bind_remount_recursive(directory, MS_RDONLY, MS_RDONLY, NULL);
e83bebef
LP
1045 if (r < 0)
1046 return log_error_errno(r, "Failed to remount %s read-only: %m", directory);
1047
1048 p = prefix_roota(directory, "/var");
1049 r = mkdir(p, 0755);
1050 if (r < 0 && errno != EEXIST)
1051 return log_error_errno(errno, "Failed to create %s: %m", directory);
1052
9f563f27 1053 options = "mode=0755" TMPFS_LIMITS_VOLATILE_STATE;
2fa017f1 1054 r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
e83bebef
LP
1055 if (r < 0)
1056 return log_oom();
1057 if (r > 0)
1058 options = buf;
1059
511a8cfe 1060 return mount_nofollow_verbose(LOG_ERR, "tmpfs", p, "tmpfs", MS_STRICTATIME, options);
e83bebef
LP
1061}
1062
e091a5df 1063static int setup_volatile_yes(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
e83bebef
LP
1064 bool tmpfs_mounted = false, bind_mounted = false;
1065 char template[] = "/tmp/nspawn-volatile-XXXXXX";
07b9f3f0 1066 _cleanup_free_ char *buf = NULL, *bindir = NULL;
e83bebef 1067 const char *f, *t, *options;
07b9f3f0 1068 struct stat st;
e83bebef
LP
1069 int r;
1070
1071 assert(directory);
1072
07b9f3f0
LP
1073 /* --volatile=yes means we mount a tmpfs to the root dir, and the original /usr to use inside it, and
1074 * that read-only. Before we start setting this up let's validate if the image has the /usr merge
1075 * implemented, and let's output a friendly log message if it hasn't. */
1076
1077 bindir = path_join(directory, "/bin");
1078 if (!bindir)
1079 return log_oom();
1080 if (lstat(bindir, &st) < 0) {
1081 if (errno != ENOENT)
1082 return log_error_errno(errno, "Failed to stat /bin directory below image: %m");
1083
1084 /* ENOENT is fine, just means the image is probably just a naked /usr and we can create the
1085 * rest. */
1086 } else if (S_ISDIR(st.st_mode))
1087 return log_error_errno(SYNTHETIC_ERRNO(EISDIR),
1088 "Sorry, --volatile=yes mode is not supported with OS images that have not merged /bin/, /sbin/, /lib/, /lib64/ into /usr/. "
1089 "Please work with your distribution and help them adopt the merged /usr scheme.");
1090 else if (!S_ISLNK(st.st_mode))
1091 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1092 "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).");
e83bebef
LP
1093
1094 if (!mkdtemp(template))
1095 return log_error_errno(errno, "Failed to create temporary directory: %m");
1096
9f563f27 1097 options = "mode=0755" TMPFS_LIMITS_ROOTFS;
2fa017f1 1098 r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
e83bebef 1099 if (r < 0)
c55d0ae7 1100 goto fail;
e83bebef
LP
1101 if (r > 0)
1102 options = buf;
1103
511a8cfe 1104 r = mount_nofollow_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
60e76d48 1105 if (r < 0)
e83bebef 1106 goto fail;
e83bebef
LP
1107
1108 tmpfs_mounted = true;
1109
1110 f = prefix_roota(directory, "/usr");
1111 t = prefix_roota(template, "/usr");
1112
1113 r = mkdir(t, 0755);
1114 if (r < 0 && errno != EEXIST) {
1115 r = log_error_errno(errno, "Failed to create %s: %m", t);
1116 goto fail;
1117 }
1118
511a8cfe 1119 r = mount_nofollow_verbose(LOG_ERR, f, t, NULL, MS_BIND|MS_REC, NULL);
60e76d48 1120 if (r < 0)
e83bebef 1121 goto fail;
e83bebef
LP
1122
1123 bind_mounted = true;
1124
64e82c19 1125 r = bind_remount_recursive(t, MS_RDONLY, MS_RDONLY, NULL);
e83bebef
LP
1126 if (r < 0) {
1127 log_error_errno(r, "Failed to remount %s read-only: %m", t);
1128 goto fail;
1129 }
1130
511a8cfe 1131 r = mount_nofollow_verbose(LOG_ERR, template, directory, NULL, MS_MOVE, NULL);
60e76d48 1132 if (r < 0)
e83bebef 1133 goto fail;
e83bebef
LP
1134
1135 (void) rmdir(template);
1136
1137 return 0;
1138
1139fail:
1140 if (bind_mounted)
30f5d104 1141 (void) umount_verbose(LOG_ERR, t, UMOUNT_NOFOLLOW);
e83bebef
LP
1142
1143 if (tmpfs_mounted)
30f5d104
LP
1144 (void) umount_verbose(LOG_ERR, template, UMOUNT_NOFOLLOW);
1145
e83bebef
LP
1146 (void) rmdir(template);
1147 return r;
1148}
b53ede69 1149
e091a5df 1150static int setup_volatile_overlay(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
6c610aca
LP
1151 _cleanup_free_ char *buf = NULL, *escaped_directory = NULL, *escaped_upper = NULL, *escaped_work = NULL;
1152 char template[] = "/tmp/nspawn-volatile-XXXXXX";
1153 const char *upper, *work, *options;
1154 bool tmpfs_mounted = false;
1155 int r;
1156
1157 assert(directory);
1158
1159 /* --volatile=overlay means we mount an overlayfs to the root dir. */
1160
1161 if (!mkdtemp(template))
1162 return log_error_errno(errno, "Failed to create temporary directory: %m");
1163
9f563f27 1164 options = "mode=0755" TMPFS_LIMITS_ROOTFS;
6c610aca
LP
1165 r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
1166 if (r < 0)
1167 goto finish;
1168 if (r > 0)
1169 options = buf;
1170
511a8cfe 1171 r = mount_nofollow_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
6c610aca
LP
1172 if (r < 0)
1173 goto finish;
1174
1175 tmpfs_mounted = true;
1176
1177 upper = strjoina(template, "/upper");
1178 work = strjoina(template, "/work");
1179
1180 if (mkdir(upper, 0755) < 0) {
1181 r = log_error_errno(errno, "Failed to create %s: %m", upper);
1182 goto finish;
1183 }
1184 if (mkdir(work, 0755) < 0) {
1185 r = log_error_errno(errno, "Failed to create %s: %m", work);
1186 goto finish;
1187 }
1188
1189 /* And now, let's overmount the root dir with an overlayfs that uses the root dir as lower dir. It's kinda nice
1190 * that the kernel allows us to do that without going through some mount point rearrangements. */
1191
1192 escaped_directory = shell_escape(directory, ",:");
1193 escaped_upper = shell_escape(upper, ",:");
1194 escaped_work = shell_escape(work, ",:");
1195 if (!escaped_directory || !escaped_upper || !escaped_work) {
1196 r = -ENOMEM;
1197 goto finish;
1198 }
1199
1200 options = strjoina("lowerdir=", escaped_directory, ",upperdir=", escaped_upper, ",workdir=", escaped_work);
511a8cfe 1201 r = mount_nofollow_verbose(LOG_ERR, "overlay", directory, "overlay", 0, options);
6c610aca
LP
1202
1203finish:
1204 if (tmpfs_mounted)
30f5d104 1205 (void) umount_verbose(LOG_ERR, template, UMOUNT_NOFOLLOW);
6c610aca
LP
1206
1207 (void) rmdir(template);
1208 return r;
1209}
1210
e5b43a04
LP
1211int setup_volatile_mode(
1212 const char *directory,
1213 VolatileMode mode,
e091a5df 1214 uid_t uid_shift,
e5b43a04
LP
1215 const char *selinux_apifs_context) {
1216
1217 switch (mode) {
1218
1219 case VOLATILE_YES:
e091a5df 1220 return setup_volatile_yes(directory, uid_shift, selinux_apifs_context);
e5b43a04
LP
1221
1222 case VOLATILE_STATE:
e091a5df 1223 return setup_volatile_state(directory, uid_shift, selinux_apifs_context);
e5b43a04 1224
6c610aca 1225 case VOLATILE_OVERLAY:
e091a5df 1226 return setup_volatile_overlay(directory, uid_shift, selinux_apifs_context);
6c610aca 1227
e5b43a04
LP
1228 default:
1229 return 0;
1230 }
1231}
1232
b53ede69
PW
1233/* Expects *pivot_root_new and *pivot_root_old to be initialised to allocated memory or NULL. */
1234int pivot_root_parse(char **pivot_root_new, char **pivot_root_old, const char *s) {
1235 _cleanup_free_ char *root_new = NULL, *root_old = NULL;
1236 const char *p = s;
1237 int r;
1238
1239 assert(pivot_root_new);
1240 assert(pivot_root_old);
1241
1242 r = extract_first_word(&p, &root_new, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
1243 if (r < 0)
1244 return r;
1245 if (r == 0)
1246 return -EINVAL;
1247
1248 if (isempty(p))
1249 root_old = NULL;
1250 else {
1251 root_old = strdup(p);
1252 if (!root_old)
1253 return -ENOMEM;
1254 }
1255
1256 if (!path_is_absolute(root_new))
1257 return -EINVAL;
1258 if (root_old && !path_is_absolute(root_old))
1259 return -EINVAL;
1260
1261 free_and_replace(*pivot_root_new, root_new);
1262 free_and_replace(*pivot_root_old, root_old);
1263
1264 return 0;
1265}
1266
1267int setup_pivot_root(const char *directory, const char *pivot_root_new, const char *pivot_root_old) {
1268 _cleanup_free_ char *directory_pivot_root_new = NULL;
1269 _cleanup_free_ char *pivot_tmp_pivot_root_old = NULL;
1270 char pivot_tmp[] = "/tmp/nspawn-pivot-XXXXXX";
1271 bool remove_pivot_tmp = false;
1272 int r;
1273
1274 assert(directory);
1275
1276 if (!pivot_root_new)
1277 return 0;
1278
1279 /* Pivot pivot_root_new to / and the existing / to pivot_root_old.
1280 * If pivot_root_old is NULL, the existing / disappears.
1281 * This requires a temporary directory, pivot_tmp, which is
1282 * not a child of either.
1283 *
32e27670
LP
1284 * This is typically used for OSTree-style containers, where the root partition contains several
1285 * sysroots which could be run. Normally, one would be chosen by the bootloader and pivoted to / by
1286 * initrd.
b53ede69
PW
1287 *
1288 * For example, for an OSTree deployment, pivot_root_new
1289 * would be: /ostree/deploy/$os/deploy/$checksum. Note that this
1290 * code doesn’t do the /var mount which OSTree expects: use
1291 * --bind +/sysroot/ostree/deploy/$os/var:/var for that.
1292 *
1293 * So in the OSTree case, we’ll end up with something like:
1294 * - directory = /tmp/nspawn-root-123456
1295 * - pivot_root_new = /ostree/deploy/os/deploy/123abc
1296 * - pivot_root_old = /sysroot
1297 * - directory_pivot_root_new =
1298 * /tmp/nspawn-root-123456/ostree/deploy/os/deploy/123abc
1299 * - pivot_tmp = /tmp/nspawn-pivot-123456
1300 * - pivot_tmp_pivot_root_old = /tmp/nspawn-pivot-123456/sysroot
1301 *
1302 * Requires all file systems at directory and below to be mounted
1303 * MS_PRIVATE or MS_SLAVE so they can be moved.
1304 */
c6134d3e
LP
1305 directory_pivot_root_new = path_join(directory, pivot_root_new);
1306 if (!directory_pivot_root_new)
1307 return log_oom();
b53ede69
PW
1308
1309 /* Remount directory_pivot_root_new to make it movable. */
511a8cfe 1310 r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, directory_pivot_root_new, NULL, MS_BIND, NULL);
b53ede69
PW
1311 if (r < 0)
1312 goto done;
1313
1314 if (pivot_root_old) {
1315 if (!mkdtemp(pivot_tmp)) {
1316 r = log_error_errno(errno, "Failed to create temporary directory: %m");
1317 goto done;
1318 }
1319
1320 remove_pivot_tmp = true;
c6134d3e
LP
1321 pivot_tmp_pivot_root_old = path_join(pivot_tmp, pivot_root_old);
1322 if (!pivot_tmp_pivot_root_old) {
1323 r = log_oom();
1324 goto done;
1325 }
b53ede69 1326
511a8cfe 1327 r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, pivot_tmp, NULL, MS_MOVE, NULL);
b53ede69
PW
1328 if (r < 0)
1329 goto done;
1330
511a8cfe 1331 r = mount_nofollow_verbose(LOG_ERR, directory, pivot_tmp_pivot_root_old, NULL, MS_MOVE, NULL);
b53ede69
PW
1332 if (r < 0)
1333 goto done;
1334
511a8cfe 1335 r = mount_nofollow_verbose(LOG_ERR, pivot_tmp, directory, NULL, MS_MOVE, NULL);
b53ede69
PW
1336 if (r < 0)
1337 goto done;
1338 } else {
511a8cfe 1339 r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, directory, NULL, MS_MOVE, NULL);
b53ede69
PW
1340 if (r < 0)
1341 goto done;
1342 }
1343
1344done:
1345 if (remove_pivot_tmp)
1346 (void) rmdir(pivot_tmp);
1347
1348 return r;
1349}
b71a0192
CB
1350
1351#define NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS "/run/host/proc"
1352#define NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS "/run/host/sys"
1353
1354int pin_fully_visible_fs(void) {
1355 int r;
1356
1357 (void) mkdir_p(NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS, 0755);
1358 (void) mkdir_p(NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS, 0755);
1359
1360 r = mount_follow_verbose(LOG_ERR, "proc", NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS, "proc", PROC_DEFAULT_MOUNT_FLAGS, NULL);
1361 if (r < 0)
1362 return r;
1363
1364 r = mount_follow_verbose(LOG_ERR, "sysfs", NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS, "sysfs", SYS_DEFAULT_MOUNT_FLAGS, NULL);
1365 if (r < 0)
1366 return r;
1367
1368 return 0;
1369}
1370
1371static int do_wipe_fully_visible_fs(void) {
1372 if (umount2(NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS, MNT_DETACH) < 0)
1373 return log_error_errno(errno, "Failed to unmount temporary proc: %m");
1374
1375 if (rmdir(NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS) < 0)
1376 return log_error_errno(errno, "Failed to remove temporary proc mountpoint: %m");
1377
1378 if (umount2(NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS, MNT_DETACH) < 0)
1379 return log_error_errno(errno, "Failed to unmount temporary sys: %m");
1380
1381 if (rmdir(NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS) < 0)
1382 return log_error_errno(errno, "Failed to remove temporary sys mountpoint: %m");
1383
1384 return 0;
1385}
1386
1387int wipe_fully_visible_fs(int mntns_fd) {
1388 _cleanup_close_ int orig_mntns_fd = -EBADF;
1389 int r, rr;
1390
1391 r = namespace_open(0, NULL, &orig_mntns_fd, NULL, NULL, NULL);
1392 if (r < 0)
1393 return log_error_errno(r, "Failed to pin originating mount namespace: %m");
1394
1395 r = namespace_enter(-EBADF, mntns_fd, -EBADF, -EBADF, -EBADF);
1396 if (r < 0)
1397 return log_error_errno(r, "Failed to enter mount namespace: %m");
1398
1399 rr = do_wipe_fully_visible_fs();
1400
1401 r = namespace_enter(-EBADF, orig_mntns_fd, -EBADF, -EBADF, -EBADF);
1402 if (r < 0)
1403 return log_error_errno(r, "Failed to enter original mount namespace: %m");
1404
1405 return rr;
1406}