]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/nspawn/nspawn-mount.c
Split out part of mount-util.c into mountpoint-util.c
[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
CB
8#include "fd-util.h"
9#include "fileio.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"
8fcde012 20#include "stat-util.h"
07630cea 21#include "string-util.h"
4f5dd394 22#include "strv.h"
ee104e11 23#include "user-util.h"
4f5dd394 24#include "util.h"
e83bebef 25
88614c8a 26CustomMount* custom_mount_add(CustomMount **l, size_t *n, CustomMountType t) {
e83bebef
LP
27 CustomMount *c, *ret;
28
29 assert(l);
30 assert(n);
31 assert(t >= 0);
32 assert(t < _CUSTOM_MOUNT_TYPE_MAX);
33
aa484f35 34 c = reallocarray(*l, *n + 1, sizeof(CustomMount));
e83bebef
LP
35 if (!c)
36 return NULL;
37
38 *l = c;
39 ret = *l + *n;
40 (*n)++;
41
42 *ret = (CustomMount) { .type = t };
43
44 return ret;
45}
46
88614c8a
LP
47void custom_mount_free_all(CustomMount *l, size_t n) {
48 size_t i;
e83bebef
LP
49
50 for (i = 0; i < n; i++) {
51 CustomMount *m = l + i;
52
53 free(m->source);
54 free(m->destination);
55 free(m->options);
56
57 if (m->work_dir) {
58 (void) rm_rf(m->work_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
59 free(m->work_dir);
60 }
61
c7a4890c
LP
62 if (m->rm_rf_tmpdir) {
63 (void) rm_rf(m->rm_rf_tmpdir, REMOVE_ROOT|REMOVE_PHYSICAL);
64 free(m->rm_rf_tmpdir);
65 }
66
e83bebef
LP
67 strv_free(m->lower);
68 }
69
70 free(l);
71}
72
93bab288 73static int custom_mount_compare(const CustomMount *a, const CustomMount *b) {
e83bebef
LP
74 int r;
75
93bab288 76 r = path_compare(a->destination, b->destination);
e83bebef
LP
77 if (r != 0)
78 return r;
79
93bab288 80 return CMP(a->type, b->type);
e83bebef
LP
81}
82
86c0dd4a
LP
83static bool source_path_is_valid(const char *p) {
84 assert(p);
85
86 if (*p == '+')
87 p++;
88
89 return path_is_absolute(p);
90}
91
92static char *resolve_source_path(const char *dest, const char *source) {
93
94 if (!source)
95 return NULL;
96
97 if (source[0] == '+')
98 return prefix_root(dest, source + 1);
99
100 return strdup(source);
101}
102
88614c8a
LP
103int custom_mount_prepare_all(const char *dest, CustomMount *l, size_t n) {
104 size_t i;
86c0dd4a
LP
105 int r;
106
107 /* Prepare all custom mounts. This will make source we know all temporary directories. This is called in the
108 * parent process, so that we know the temporary directories to remove on exit before we fork off the
109 * children. */
110
111 assert(l || n == 0);
112
113 /* Order the custom mounts, and make sure we have a working directory */
93bab288 114 typesafe_qsort(l, n, custom_mount_compare);
86c0dd4a
LP
115
116 for (i = 0; i < n; i++) {
117 CustomMount *m = l + i;
118
119 if (m->source) {
120 char *s;
121
122 s = resolve_source_path(dest, m->source);
123 if (!s)
124 return log_oom();
125
10af01a5 126 free_and_replace(m->source, s);
c7a4890c
LP
127 } else {
128 /* No source specified? In that case, use a throw-away temporary directory in /var/tmp */
129
130 m->rm_rf_tmpdir = strdup("/var/tmp/nspawn-temp-XXXXXX");
131 if (!m->rm_rf_tmpdir)
132 return log_oom();
133
134 if (!mkdtemp(m->rm_rf_tmpdir)) {
135 m->rm_rf_tmpdir = mfree(m->rm_rf_tmpdir);
136 return log_error_errno(errno, "Failed to acquire temporary directory: %m");
137 }
138
139 m->source = strjoin(m->rm_rf_tmpdir, "/src");
140 if (!m->source)
141 return log_oom();
142
143 if (mkdir(m->source, 0755) < 0)
144 return log_error_errno(errno, "Failed to create %s: %m", m->source);
86c0dd4a
LP
145 }
146
147 if (m->type == CUSTOM_MOUNT_OVERLAY) {
148 char **j;
149
150 STRV_FOREACH(j, m->lower) {
151 char *s;
152
153 s = resolve_source_path(dest, *j);
154 if (!s)
155 return log_oom();
156
10af01a5 157 free_and_replace(*j, s);
86c0dd4a
LP
158 }
159
160 if (m->work_dir) {
161 char *s;
162
163 s = resolve_source_path(dest, m->work_dir);
164 if (!s)
165 return log_oom();
166
10af01a5 167 free_and_replace(m->work_dir, s);
86c0dd4a
LP
168 } else {
169 assert(m->source);
170
171 r = tempfn_random(m->source, NULL, &m->work_dir);
172 if (r < 0)
173 return log_error_errno(r, "Failed to acquire working directory: %m");
174 }
175
176 (void) mkdir_label(m->work_dir, 0700);
177 }
178 }
179
180 return 0;
181}
182
88614c8a 183int bind_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) {
e83bebef
LP
184 _cleanup_free_ char *source = NULL, *destination = NULL, *opts = NULL;
185 const char *p = s;
186 CustomMount *m;
187 int r;
188
189 assert(l);
190 assert(n);
191
192 r = extract_many_words(&p, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &source, &destination, NULL);
193 if (r < 0)
194 return r;
195 if (r == 0)
196 return -EINVAL;
e83bebef 197 if (r == 1) {
86c0dd4a 198 destination = strdup(source[0] == '+' ? source+1 : source);
e83bebef
LP
199 if (!destination)
200 return -ENOMEM;
201 }
e83bebef
LP
202 if (r == 2 && !isempty(p)) {
203 opts = strdup(p);
204 if (!opts)
205 return -ENOMEM;
206 }
207
c7a4890c
LP
208 if (isempty(source))
209 source = NULL;
210 else if (!source_path_is_valid(source))
e83bebef 211 return -EINVAL;
c7a4890c 212
e83bebef
LP
213 if (!path_is_absolute(destination))
214 return -EINVAL;
215
216 m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND);
217 if (!m)
48cbe5f8 218 return -ENOMEM;
e83bebef
LP
219
220 m->source = source;
221 m->destination = destination;
222 m->read_only = read_only;
223 m->options = opts;
224
225 source = destination = opts = NULL;
226 return 0;
227}
228
88614c8a 229int tmpfs_mount_parse(CustomMount **l, size_t *n, const char *s) {
e83bebef
LP
230 _cleanup_free_ char *path = NULL, *opts = NULL;
231 const char *p = s;
232 CustomMount *m;
233 int r;
234
235 assert(l);
236 assert(n);
237 assert(s);
238
239 r = extract_first_word(&p, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
240 if (r < 0)
241 return r;
242 if (r == 0)
243 return -EINVAL;
244
245 if (isempty(p))
246 opts = strdup("mode=0755");
247 else
248 opts = strdup(p);
249 if (!opts)
250 return -ENOMEM;
251
252 if (!path_is_absolute(path))
253 return -EINVAL;
254
255 m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS);
256 if (!m)
257 return -ENOMEM;
258
1cc6c93a
YW
259 m->destination = TAKE_PTR(path);
260 m->options = TAKE_PTR(opts);
e83bebef 261
e83bebef
LP
262 return 0;
263}
264
88614c8a 265int overlay_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) {
ad85779a
LP
266 _cleanup_free_ char *upper = NULL, *destination = NULL;
267 _cleanup_strv_free_ char **lower = NULL;
268 CustomMount *m;
86c0dd4a 269 int k;
ad85779a 270
86c0dd4a
LP
271 k = strv_split_extract(&lower, s, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
272 if (k < 0)
273 return k;
ad85779a
LP
274 if (k < 2)
275 return -EADDRNOTAVAIL;
276 if (k == 2) {
86c0dd4a
LP
277 /* If two parameters are specified, the first one is the lower, the second one the upper directory. And
278 * we'll also define the destination mount point the same as the upper. */
279
280 if (!source_path_is_valid(lower[0]) ||
281 !source_path_is_valid(lower[1]))
282 return -EINVAL;
283
ae2a15bc 284 upper = TAKE_PTR(lower[1]);
ad85779a 285
86c0dd4a 286 destination = strdup(upper[0] == '+' ? upper+1 : upper); /* take the destination without "+" prefix */
ad85779a
LP
287 if (!destination)
288 return -ENOMEM;
ad85779a 289 } else {
c7a4890c 290 char **i;
86c0dd4a
LP
291
292 /* If more than two parameters are specified, the last one is the destination, the second to last one
293 * the "upper", and all before that the "lower" directories. */
294
ad85779a 295 destination = lower[k - 1];
ae2a15bc 296 upper = TAKE_PTR(lower[k - 2]);
86c0dd4a 297
c7a4890c
LP
298 STRV_FOREACH(i, lower)
299 if (!source_path_is_valid(*i))
300 return -EINVAL;
301
302 /* If the upper directory is unspecified, then let's create it automatically as a throw-away directory
303 * in /var/tmp */
304 if (isempty(upper))
305 upper = NULL;
306 else if (!source_path_is_valid(upper))
307 return -EINVAL;
308
86c0dd4a
LP
309 if (!path_is_absolute(destination))
310 return -EINVAL;
ad85779a
LP
311 }
312
313 m = custom_mount_add(l, n, CUSTOM_MOUNT_OVERLAY);
314 if (!m)
315 return -ENOMEM;
316
1cc6c93a
YW
317 m->destination = TAKE_PTR(destination);
318 m->source = TAKE_PTR(upper);
319 m->lower = TAKE_PTR(lower);
ad85779a
LP
320 m->read_only = read_only;
321
ad85779a
LP
322 return 0;
323}
324
04029482 325int tmpfs_patch_options(
e83bebef 326 const char *options,
2fa017f1 327 uid_t uid_shift,
e83bebef
LP
328 const char *selinux_apifs_context,
329 char **ret) {
330
331 char *buf = NULL;
332
2fa017f1 333 if (uid_shift != UID_INVALID) {
9aa2169e 334 if (asprintf(&buf, "%s%suid=" UID_FMT ",gid=" UID_FMT,
87e4e28d 335 strempty(options), options ? "," : "",
9aa2169e 336 uid_shift, uid_shift) < 0)
e83bebef
LP
337 return -ENOMEM;
338
339 options = buf;
340 }
341
349cc4a5 342#if HAVE_SELINUX
e83bebef
LP
343 if (selinux_apifs_context) {
344 char *t;
345
87e4e28d 346 t = strjoin(strempty(options), options ? "," : "",
9aa2169e
ZJS
347 "context=\"", selinux_apifs_context, "\"");
348 free(buf);
349 if (!t)
e83bebef 350 return -ENOMEM;
e83bebef 351
e83bebef
LP
352 buf = t;
353 }
354#endif
355
0996ef00
CB
356 if (!buf && options) {
357 buf = strdup(options);
358 if (!buf)
359 return -ENOMEM;
360 }
e83bebef 361 *ret = buf;
0996ef00 362
e83bebef
LP
363 return !!buf;
364}
365
4f086aab 366int mount_sysfs(const char *dest, MountSettingsMask mount_settings) {
d8fc6a00 367 const char *full, *top, *x;
d1678248 368 int r;
4f086aab 369 unsigned long extra_flags = 0;
d8fc6a00
LP
370
371 top = prefix_roota(dest, "/sys");
40fd52f2 372 r = path_is_fs_type(top, SYSFS_MAGIC);
d1678248
ILG
373 if (r < 0)
374 return log_error_errno(r, "Failed to determine filesystem type of %s: %m", top);
375 /* /sys might already be mounted as sysfs by the outer child in the
376 * !netns case. In this case, it's all good. Don't touch it because we
377 * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555.
378 */
379 if (r > 0)
380 return 0;
381
d8fc6a00
LP
382 full = prefix_roota(top, "/full");
383
384 (void) mkdir(full, 0755);
385
4f086aab
SU
386 if (mount_settings & MOUNT_APPLY_APIVFS_RO)
387 extra_flags |= MS_RDONLY;
388
60e76d48 389 r = mount_verbose(LOG_ERR, "sysfs", full, "sysfs",
4f086aab 390 MS_NOSUID|MS_NOEXEC|MS_NODEV|extra_flags, NULL);
60e76d48
ZJS
391 if (r < 0)
392 return r;
d8fc6a00
LP
393
394 FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") {
395 _cleanup_free_ char *from = NULL, *to = NULL;
396
397 from = prefix_root(full, x);
398 if (!from)
399 return log_oom();
400
401 to = prefix_root(top, x);
402 if (!to)
403 return log_oom();
404
405 (void) mkdir(to, 0755);
406
60e76d48
ZJS
407 r = mount_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
408 if (r < 0)
409 return r;
d8fc6a00 410
60e76d48 411 r = mount_verbose(LOG_ERR, NULL, to, NULL,
4f086aab 412 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
60e76d48
ZJS
413 if (r < 0)
414 return r;
d8fc6a00
LP
415 }
416
60e76d48
ZJS
417 r = umount_verbose(full);
418 if (r < 0)
419 return r;
d8fc6a00
LP
420
421 if (rmdir(full) < 0)
422 return log_error_errno(errno, "Failed to remove %s: %m", full);
423
0996ef00
CB
424 /* Create mountpoint for cgroups. Otherwise we are not allowed since we
425 * remount /sys read-only.
426 */
677a72cd
LS
427 x = prefix_roota(top, "/fs/cgroup");
428 (void) mkdir_p(x, 0755);
d8fc6a00 429
60e76d48 430 return mount_verbose(LOG_ERR, NULL, top, NULL,
4f086aab 431 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
d8fc6a00
LP
432}
433
9c0fad5f 434static int mkdir_userns(const char *path, mode_t mode, uid_t uid_shift) {
63eae723
EV
435 int r;
436
437 assert(path);
438
dae8b82e
ZJS
439 r = mkdir_errno_wrapper(path, mode);
440 if (r < 0 && r != -EEXIST)
441 return r;
63eae723 442
9c0fad5f 443 if (uid_shift == UID_INVALID)
acbbf69b
LP
444 return 0;
445
dae8b82e 446 if (lchown(path, uid_shift, uid_shift) < 0)
acbbf69b 447 return -errno;
63eae723
EV
448
449 return 0;
450}
451
9c0fad5f 452static int mkdir_userns_p(const char *prefix, const char *path, mode_t mode, uid_t uid_shift) {
63eae723
EV
453 const char *p, *e;
454 int r;
455
456 assert(path);
457
458 if (prefix && !path_startswith(path, prefix))
459 return -ENOTDIR;
460
461 /* create every parent directory in the path, except the last component */
462 p = path + strspn(path, "/");
463 for (;;) {
464 char t[strlen(path) + 1];
465
466 e = p + strcspn(p, "/");
467 p = e + strspn(e, "/");
468
469 /* Is this the last component? If so, then we're done */
470 if (*p == 0)
471 break;
472
473 memcpy(t, path, e - path);
474 t[e-path] = 0;
475
476 if (prefix && path_startswith(prefix, t))
477 continue;
478
9c0fad5f 479 r = mkdir_userns(t, mode, uid_shift);
63eae723
EV
480 if (r < 0)
481 return r;
482 }
483
9c0fad5f 484 return mkdir_userns(path, mode, uid_shift);
63eae723
EV
485}
486
e83bebef 487int mount_all(const char *dest,
4f086aab 488 MountSettingsMask mount_settings,
2fa017f1 489 uid_t uid_shift,
e83bebef
LP
490 const char *selinux_apifs_context) {
491
d4b653c5
LP
492#define PROC_INACCESSIBLE(path) \
493 { NULL, (path), NULL, NULL, MS_BIND, \
494 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO|MOUNT_INACCESSIBLE_REG }, /* Bind mount first ... */ \
495 { NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \
496 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */
497
498#define PROC_READ_ONLY(path) \
499 { (path), (path), NULL, NULL, MS_BIND, \
500 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ \
501 { NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \
502 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */
503
e83bebef
LP
504 typedef struct MountPoint {
505 const char *what;
506 const char *where;
507 const char *type;
508 const char *options;
509 unsigned long flags;
4f086aab 510 MountSettingsMask mount_settings;
e83bebef
LP
511 } MountPoint;
512
513 static const MountPoint mount_table[] = {
d4b653c5
LP
514 /* First we list inner child mounts (i.e. mounts applied *after* entering user namespacing) */
515 { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
516 MOUNT_FATAL|MOUNT_IN_USERNS },
517
518 { "/proc/sys", "/proc/sys", NULL, NULL, MS_BIND,
519 MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */
520
521 { "/proc/sys/net", "/proc/sys/net", NULL, NULL, MS_BIND,
522 MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_APIVFS_NETNS }, /* (except for this) */
523
524 { NULL, "/proc/sys", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
525 MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* ... then, make it r/o */
526
527 /* Make these files inaccessible to container payloads: they potentially leak information about kernel
528 * internals or the host's execution environment to the container */
529 PROC_INACCESSIBLE("/proc/kallsyms"),
530 PROC_INACCESSIBLE("/proc/kcore"),
531 PROC_INACCESSIBLE("/proc/keys"),
532 PROC_INACCESSIBLE("/proc/sysrq-trigger"),
533 PROC_INACCESSIBLE("/proc/timer_list"),
534
535 /* Make these directories read-only to container payloads: they show hardware information, and in some
536 * cases contain tunables the container really shouldn't have access to. */
537 PROC_READ_ONLY("/proc/acpi"),
538 PROC_READ_ONLY("/proc/apm"),
539 PROC_READ_ONLY("/proc/asound"),
540 PROC_READ_ONLY("/proc/bus"),
541 PROC_READ_ONLY("/proc/fs"),
542 PROC_READ_ONLY("/proc/irq"),
543 PROC_READ_ONLY("/proc/scsi"),
544
545 /* Then we list outer child mounts (i.e. mounts applied *before* entering user namespacing) */
546 { "tmpfs", "/tmp", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
1099ceeb 547 MOUNT_FATAL|MOUNT_APPLY_TMPFS_TMP },
03d0f4b5 548 { "tmpfs", "/sys", "tmpfs", "mode=555", MS_NOSUID|MS_NOEXEC|MS_NODEV,
d4b653c5
LP
549 MOUNT_FATAL|MOUNT_APPLY_APIVFS_NETNS },
550 { "sysfs", "/sys", "sysfs", NULL, MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV,
551 MOUNT_FATAL|MOUNT_APPLY_APIVFS_RO }, /* skipped if above was mounted */
552 { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
553 MOUNT_FATAL }, /* skipped if above was mounted */
554 { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID|MS_STRICTATIME,
555 MOUNT_FATAL },
556 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
557 MOUNT_FATAL },
558 { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
559 MOUNT_FATAL },
560
349cc4a5 561#if HAVE_SELINUX
d4b653c5
LP
562 { "/sys/fs/selinux", "/sys/fs/selinux", NULL, NULL, MS_BIND,
563 0 }, /* Bind mount first */
564 { NULL, "/sys/fs/selinux", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT,
565 0 }, /* Then, make it r/o */
e83bebef
LP
566#endif
567 };
568
d4b653c5 569 _cleanup_(unlink_and_freep) char *inaccessible = NULL;
4f086aab
SU
570 bool use_userns = (mount_settings & MOUNT_USE_USERNS);
571 bool netns = (mount_settings & MOUNT_APPLY_APIVFS_NETNS);
572 bool ro = (mount_settings & MOUNT_APPLY_APIVFS_RO);
573 bool in_userns = (mount_settings & MOUNT_IN_USERNS);
1099ceeb 574 bool tmpfs_tmp = (mount_settings & MOUNT_APPLY_TMPFS_TMP);
d4b653c5 575 size_t k;
88614c8a 576 int r;
e83bebef
LP
577
578 for (k = 0; k < ELEMENTSOF(mount_table); k++) {
579 _cleanup_free_ char *where = NULL, *options = NULL;
d4b653c5 580 const char *o, *what;
4f086aab
SU
581 bool fatal = (mount_table[k].mount_settings & MOUNT_FATAL);
582
583 if (in_userns != (bool)(mount_table[k].mount_settings & MOUNT_IN_USERNS))
584 continue;
e83bebef 585
4f086aab 586 if (!netns && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_APIVFS_NETNS))
d1678248
ILG
587 continue;
588
4f086aab 589 if (!ro && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_APIVFS_RO))
e83bebef
LP
590 continue;
591
1099ceeb
LP
592 if (!tmpfs_tmp && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_TMPFS_TMP))
593 continue;
594
cb638b5e 595 r = chase_symlinks(mount_table[k].where, dest, CHASE_NONEXISTENT|CHASE_PREFIX_ROOT, &where);
8ce48cf0 596 if (r < 0)
ec57bd42 597 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, mount_table[k].where);
e83bebef 598
d4b653c5
LP
599 if (mount_table[k].mount_settings & MOUNT_INACCESSIBLE_REG) {
600
601 if (!inaccessible) {
602 _cleanup_free_ char *np = NULL;
603
604 r = tempfn_random_child(NULL, "inaccessible", &np);
605 if (r < 0)
606 return log_error_errno(r, "Failed to generate inaccessible file node path: %m");
607
608 r = touch_file(np, false, USEC_INFINITY, UID_INVALID, GID_INVALID, 0000);
609 if (r < 0)
610 return log_error_errno(r, "Failed to create inaccessible file node '%s': %m", np);
611
612 inaccessible = TAKE_PTR(np);
613 }
614
615 what = inaccessible;
616 } else
617 what = mount_table[k].what;
618
8ce48cf0 619 r = path_is_mount_point(where, NULL, 0);
e83bebef
LP
620 if (r < 0 && r != -ENOENT)
621 return log_error_errno(r, "Failed to detect whether %s is a mount point: %m", where);
622
623 /* Skip this entry if it is not a remount. */
d4b653c5 624 if (what && r > 0)
e83bebef
LP
625 continue;
626
9c0fad5f 627 r = mkdir_userns_p(dest, where, 0755, (use_userns && !in_userns) ? uid_shift : UID_INVALID);
920a7899 628 if (r < 0 && r != -EEXIST) {
4f13e534 629 if (fatal && r != -EROFS)
e83bebef
LP
630 return log_error_errno(r, "Failed to create directory %s: %m", where);
631
201b13c8 632 log_debug_errno(r, "Failed to create directory %s: %m", where);
4f13e534
LT
633 /* If we failed mkdir() or chown() due to the root
634 * directory being read only, attempt to mount this fs
635 * anyway and let mount_verbose log any errors */
636 if (r != -EROFS)
637 continue;
e83bebef
LP
638 }
639
640 o = mount_table[k].options;
641 if (streq_ptr(mount_table[k].type, "tmpfs")) {
2fa017f1 642 r = tmpfs_patch_options(o, in_userns ? 0 : uid_shift, selinux_apifs_context, &options);
e83bebef
LP
643 if (r < 0)
644 return log_oom();
645 if (r > 0)
646 o = options;
647 }
648
4f086aab 649 r = mount_verbose(fatal ? LOG_ERR : LOG_DEBUG,
d4b653c5 650 what,
60e76d48
ZJS
651 where,
652 mount_table[k].type,
653 mount_table[k].flags,
654 o);
4f086aab 655 if (r < 0 && fatal)
60e76d48 656 return r;
e83bebef
LP
657 }
658
659 return 0;
660}
661
e83bebef 662static int mount_bind(const char *dest, CustomMount *m) {
68cf43c3 663
72d967df 664 _cleanup_free_ char *where = NULL;
68cf43c3 665 struct stat source_st, dest_st;
e83bebef
LP
666 int r;
667
86c0dd4a 668 assert(dest);
e83bebef
LP
669 assert(m);
670
e83bebef
LP
671 if (stat(m->source, &source_st) < 0)
672 return log_error_errno(errno, "Failed to stat %s: %m", m->source);
673
cb638b5e 674 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where);
68cf43c3 675 if (r < 0)
ec57bd42 676 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
8ce48cf0
LP
677 if (r > 0) { /* Path exists already? */
678
679 if (stat(where, &dest_st) < 0)
680 return log_error_errno(errno, "Failed to stat %s: %m", where);
e83bebef 681
baaa35ad
ZJS
682 if (S_ISDIR(source_st.st_mode) && !S_ISDIR(dest_st.st_mode))
683 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
684 "Cannot bind mount directory %s on file %s.",
685 m->source, where);
686
687 if (!S_ISDIR(source_st.st_mode) && S_ISDIR(dest_st.st_mode))
688 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
689 "Cannot bind mount file %s on directory %s.",
690 m->source, where);
e83bebef 691
8ce48cf0 692 } else { /* Path doesn't exist yet? */
e83bebef
LP
693 r = mkdir_parents_label(where, 0755);
694 if (r < 0)
695 return log_error_errno(r, "Failed to make parents of %s: %m", where);
b97e83cb
BN
696
697 /* Create the mount point. Any non-directory file can be
698 * mounted on any non-directory file (regular, fifo, socket,
699 * char, block).
700 */
701 if (S_ISDIR(source_st.st_mode))
702 r = mkdir_label(where, 0755);
703 else
704 r = touch(where);
705 if (r < 0)
706 return log_error_errno(r, "Failed to create mount point %s: %m", where);
707
8ce48cf0 708 }
e83bebef 709
72d967df 710 r = mount_verbose(LOG_ERR, m->source, where, NULL, MS_BIND | MS_REC, m->options);
60e76d48
ZJS
711 if (r < 0)
712 return r;
e83bebef
LP
713
714 if (m->read_only) {
6b7c9f8b 715 r = bind_remount_recursive(where, true, NULL);
e83bebef
LP
716 if (r < 0)
717 return log_error_errno(r, "Read-only bind mount failed: %m");
718 }
719
720 return 0;
721}
722
723static int mount_tmpfs(
724 const char *dest,
725 CustomMount *m,
726 bool userns, uid_t uid_shift, uid_t uid_range,
727 const char *selinux_apifs_context) {
728
68cf43c3
LP
729 const char *options;
730 _cleanup_free_ char *buf = NULL, *where = NULL;
e83bebef
LP
731 int r;
732
733 assert(dest);
734 assert(m);
735
cb638b5e 736 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where);
68cf43c3 737 if (r < 0)
ec57bd42 738 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
8ce48cf0
LP
739 if (r == 0) { /* Doesn't exist yet? */
740 r = mkdir_p_label(where, 0755);
741 if (r < 0)
742 return log_error_errno(r, "Creating mount point for tmpfs %s failed: %m", where);
743 }
e83bebef 744
2fa017f1 745 r = tmpfs_patch_options(m->options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
e83bebef
LP
746 if (r < 0)
747 return log_oom();
748 options = r > 0 ? buf : m->options;
749
60e76d48 750 return mount_verbose(LOG_ERR, "tmpfs", where, "tmpfs", MS_NODEV|MS_STRICTATIME, options);
e83bebef
LP
751}
752
86c0dd4a 753static char *joined_and_escaped_lower_dirs(char **lower) {
e83bebef
LP
754 _cleanup_strv_free_ char **sv = NULL;
755
756 sv = strv_copy(lower);
757 if (!sv)
758 return NULL;
759
760 strv_reverse(sv);
761
762 if (!strv_shell_escape(sv, ",:"))
763 return NULL;
764
765 return strv_join(sv, ":");
766}
767
768static int mount_overlay(const char *dest, CustomMount *m) {
68cf43c3 769
86c0dd4a 770 _cleanup_free_ char *lower = NULL, *where = NULL, *escaped_source = NULL;
68cf43c3 771 const char *options;
e83bebef
LP
772 int r;
773
774 assert(dest);
775 assert(m);
776
cb638b5e 777 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where);
68cf43c3 778 if (r < 0)
ec57bd42 779 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
8ce48cf0
LP
780 if (r == 0) { /* Doesn't exist yet? */
781 r = mkdir_label(where, 0755);
782 if (r < 0)
783 return log_error_errno(r, "Creating mount point for overlay %s failed: %m", where);
784 }
e83bebef
LP
785
786 (void) mkdir_p_label(m->source, 0755);
787
788 lower = joined_and_escaped_lower_dirs(m->lower);
789 if (!lower)
790 return log_oom();
791
86c0dd4a
LP
792 escaped_source = shell_escape(m->source, ",:");
793 if (!escaped_source)
794 return log_oom();
e83bebef 795
86c0dd4a 796 if (m->read_only)
e83bebef 797 options = strjoina("lowerdir=", escaped_source, ":", lower);
86c0dd4a
LP
798 else {
799 _cleanup_free_ char *escaped_work_dir = NULL;
e83bebef 800
e83bebef
LP
801 escaped_work_dir = shell_escape(m->work_dir, ",:");
802 if (!escaped_work_dir)
803 return log_oom();
804
805 options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir);
806 }
807
60e76d48 808 return mount_verbose(LOG_ERR, "overlay", where, "overlay", m->read_only ? MS_RDONLY : 0, options);
e83bebef
LP
809}
810
811int mount_custom(
812 const char *dest,
88614c8a 813 CustomMount *mounts, size_t n,
e83bebef
LP
814 bool userns, uid_t uid_shift, uid_t uid_range,
815 const char *selinux_apifs_context) {
816
88614c8a 817 size_t i;
e83bebef
LP
818 int r;
819
820 assert(dest);
821
822 for (i = 0; i < n; i++) {
823 CustomMount *m = mounts + i;
824
825 switch (m->type) {
826
827 case CUSTOM_MOUNT_BIND:
828 r = mount_bind(dest, m);
829 break;
830
831 case CUSTOM_MOUNT_TMPFS:
832 r = mount_tmpfs(dest, m, userns, uid_shift, uid_range, selinux_apifs_context);
833 break;
834
835 case CUSTOM_MOUNT_OVERLAY:
836 r = mount_overlay(dest, m);
837 break;
838
839 default:
840 assert_not_reached("Unknown custom mount type");
841 }
842
843 if (r < 0)
844 return r;
845 }
846
847 return 0;
848}
849
e83bebef
LP
850int setup_volatile_state(
851 const char *directory,
852 VolatileMode mode,
853 bool userns, uid_t uid_shift, uid_t uid_range,
854 const char *selinux_apifs_context) {
855
856 _cleanup_free_ char *buf = NULL;
857 const char *p, *options;
858 int r;
859
860 assert(directory);
861
862 if (mode != VOLATILE_STATE)
863 return 0;
864
865 /* --volatile=state means we simply overmount /var
866 with a tmpfs, and the rest read-only. */
867
6b7c9f8b 868 r = bind_remount_recursive(directory, true, NULL);
e83bebef
LP
869 if (r < 0)
870 return log_error_errno(r, "Failed to remount %s read-only: %m", directory);
871
872 p = prefix_roota(directory, "/var");
873 r = mkdir(p, 0755);
874 if (r < 0 && errno != EEXIST)
875 return log_error_errno(errno, "Failed to create %s: %m", directory);
876
877 options = "mode=755";
2fa017f1 878 r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
e83bebef
LP
879 if (r < 0)
880 return log_oom();
881 if (r > 0)
882 options = buf;
883
60e76d48 884 return mount_verbose(LOG_ERR, "tmpfs", p, "tmpfs", MS_STRICTATIME, options);
e83bebef
LP
885}
886
887int setup_volatile(
888 const char *directory,
889 VolatileMode mode,
890 bool userns, uid_t uid_shift, uid_t uid_range,
891 const char *selinux_apifs_context) {
892
893 bool tmpfs_mounted = false, bind_mounted = false;
894 char template[] = "/tmp/nspawn-volatile-XXXXXX";
895 _cleanup_free_ char *buf = NULL;
896 const char *f, *t, *options;
897 int r;
898
899 assert(directory);
900
901 if (mode != VOLATILE_YES)
902 return 0;
903
904 /* --volatile=yes means we mount a tmpfs to the root dir, and
905 the original /usr to use inside it, and that read-only. */
906
907 if (!mkdtemp(template))
908 return log_error_errno(errno, "Failed to create temporary directory: %m");
909
910 options = "mode=755";
2fa017f1 911 r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
e83bebef
LP
912 if (r < 0)
913 return log_oom();
914 if (r > 0)
915 options = buf;
916
60e76d48
ZJS
917 r = mount_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
918 if (r < 0)
e83bebef 919 goto fail;
e83bebef
LP
920
921 tmpfs_mounted = true;
922
923 f = prefix_roota(directory, "/usr");
924 t = prefix_roota(template, "/usr");
925
926 r = mkdir(t, 0755);
927 if (r < 0 && errno != EEXIST) {
928 r = log_error_errno(errno, "Failed to create %s: %m", t);
929 goto fail;
930 }
931
60e76d48
ZJS
932 r = mount_verbose(LOG_ERR, f, t, NULL, MS_BIND|MS_REC, NULL);
933 if (r < 0)
e83bebef 934 goto fail;
e83bebef
LP
935
936 bind_mounted = true;
937
6b7c9f8b 938 r = bind_remount_recursive(t, true, NULL);
e83bebef
LP
939 if (r < 0) {
940 log_error_errno(r, "Failed to remount %s read-only: %m", t);
941 goto fail;
942 }
943
60e76d48
ZJS
944 r = mount_verbose(LOG_ERR, template, directory, NULL, MS_MOVE, NULL);
945 if (r < 0)
e83bebef 946 goto fail;
e83bebef
LP
947
948 (void) rmdir(template);
949
950 return 0;
951
952fail:
953 if (bind_mounted)
60e76d48 954 (void) umount_verbose(t);
e83bebef
LP
955
956 if (tmpfs_mounted)
60e76d48 957 (void) umount_verbose(template);
e83bebef
LP
958 (void) rmdir(template);
959 return r;
960}
b53ede69
PW
961
962/* Expects *pivot_root_new and *pivot_root_old to be initialised to allocated memory or NULL. */
963int pivot_root_parse(char **pivot_root_new, char **pivot_root_old, const char *s) {
964 _cleanup_free_ char *root_new = NULL, *root_old = NULL;
965 const char *p = s;
966 int r;
967
968 assert(pivot_root_new);
969 assert(pivot_root_old);
970
971 r = extract_first_word(&p, &root_new, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
972 if (r < 0)
973 return r;
974 if (r == 0)
975 return -EINVAL;
976
977 if (isempty(p))
978 root_old = NULL;
979 else {
980 root_old = strdup(p);
981 if (!root_old)
982 return -ENOMEM;
983 }
984
985 if (!path_is_absolute(root_new))
986 return -EINVAL;
987 if (root_old && !path_is_absolute(root_old))
988 return -EINVAL;
989
990 free_and_replace(*pivot_root_new, root_new);
991 free_and_replace(*pivot_root_old, root_old);
992
993 return 0;
994}
995
996int setup_pivot_root(const char *directory, const char *pivot_root_new, const char *pivot_root_old) {
997 _cleanup_free_ char *directory_pivot_root_new = NULL;
998 _cleanup_free_ char *pivot_tmp_pivot_root_old = NULL;
999 char pivot_tmp[] = "/tmp/nspawn-pivot-XXXXXX";
1000 bool remove_pivot_tmp = false;
1001 int r;
1002
1003 assert(directory);
1004
1005 if (!pivot_root_new)
1006 return 0;
1007
1008 /* Pivot pivot_root_new to / and the existing / to pivot_root_old.
1009 * If pivot_root_old is NULL, the existing / disappears.
1010 * This requires a temporary directory, pivot_tmp, which is
1011 * not a child of either.
1012 *
1013 * This is typically used for OSTree-style containers, where
1014 * the root partition contains several sysroots which could be
1015 * run. Normally, one would be chosen by the bootloader and
1016 * pivoted to / by initramfs.
1017 *
1018 * For example, for an OSTree deployment, pivot_root_new
1019 * would be: /ostree/deploy/$os/deploy/$checksum. Note that this
1020 * code doesn’t do the /var mount which OSTree expects: use
1021 * --bind +/sysroot/ostree/deploy/$os/var:/var for that.
1022 *
1023 * So in the OSTree case, we’ll end up with something like:
1024 * - directory = /tmp/nspawn-root-123456
1025 * - pivot_root_new = /ostree/deploy/os/deploy/123abc
1026 * - pivot_root_old = /sysroot
1027 * - directory_pivot_root_new =
1028 * /tmp/nspawn-root-123456/ostree/deploy/os/deploy/123abc
1029 * - pivot_tmp = /tmp/nspawn-pivot-123456
1030 * - pivot_tmp_pivot_root_old = /tmp/nspawn-pivot-123456/sysroot
1031 *
1032 * Requires all file systems at directory and below to be mounted
1033 * MS_PRIVATE or MS_SLAVE so they can be moved.
1034 */
1035 directory_pivot_root_new = prefix_root(directory, pivot_root_new);
1036
1037 /* Remount directory_pivot_root_new to make it movable. */
1038 r = mount_verbose(LOG_ERR, directory_pivot_root_new, directory_pivot_root_new, NULL, MS_BIND, NULL);
1039 if (r < 0)
1040 goto done;
1041
1042 if (pivot_root_old) {
1043 if (!mkdtemp(pivot_tmp)) {
1044 r = log_error_errno(errno, "Failed to create temporary directory: %m");
1045 goto done;
1046 }
1047
1048 remove_pivot_tmp = true;
1049 pivot_tmp_pivot_root_old = prefix_root(pivot_tmp, pivot_root_old);
1050
1051 r = mount_verbose(LOG_ERR, directory_pivot_root_new, pivot_tmp, NULL, MS_MOVE, NULL);
1052 if (r < 0)
1053 goto done;
1054
1055 r = mount_verbose(LOG_ERR, directory, pivot_tmp_pivot_root_old, NULL, MS_MOVE, NULL);
1056 if (r < 0)
1057 goto done;
1058
1059 r = mount_verbose(LOG_ERR, pivot_tmp, directory, NULL, MS_MOVE, NULL);
1060 if (r < 0)
1061 goto done;
1062 } else {
1063 r = mount_verbose(LOG_ERR, directory_pivot_root_new, directory, NULL, MS_MOVE, NULL);
1064 if (r < 0)
1065 goto done;
1066 }
1067
1068done:
1069 if (remove_pivot_tmp)
1070 (void) rmdir(pivot_tmp);
1071
1072 return r;
1073}