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