]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/nspawn/nspawn-mount.c
nspawn: use chase_symlinks() on all paths specified via --tmpfs=, --bind= and so on
[thirdparty/systemd.git] / src / nspawn / nspawn-mount.c
CommitLineData
e83bebef
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2015 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
4f5dd394 20#include <sys/mount.h>
07630cea 21#include <linux/magic.h>
e83bebef 22
b5efdb8a 23#include "alloc-util.h"
4f5dd394 24#include "escape.h"
0996ef00
CB
25#include "fd-util.h"
26#include "fileio.h"
f4f15635 27#include "fs-util.h"
e83bebef 28#include "label.h"
4f5dd394 29#include "mkdir.h"
4349cd7c 30#include "mount-util.h"
6bedfcbb
LP
31#include "nspawn-mount.h"
32#include "parse-util.h"
4f5dd394
LP
33#include "path-util.h"
34#include "rm-rf.h"
e83bebef 35#include "set.h"
8fcde012 36#include "stat-util.h"
07630cea 37#include "string-util.h"
4f5dd394 38#include "strv.h"
ee104e11 39#include "user-util.h"
4f5dd394 40#include "util.h"
e83bebef
LP
41
42CustomMount* custom_mount_add(CustomMount **l, unsigned *n, CustomMountType t) {
43 CustomMount *c, *ret;
44
45 assert(l);
46 assert(n);
47 assert(t >= 0);
48 assert(t < _CUSTOM_MOUNT_TYPE_MAX);
49
56391931 50 c = realloc_multiply(*l, (*n + 1), sizeof(CustomMount));
e83bebef
LP
51 if (!c)
52 return NULL;
53
54 *l = c;
55 ret = *l + *n;
56 (*n)++;
57
58 *ret = (CustomMount) { .type = t };
59
60 return ret;
61}
62
63void custom_mount_free_all(CustomMount *l, unsigned n) {
64 unsigned i;
65
66 for (i = 0; i < n; i++) {
67 CustomMount *m = l + i;
68
69 free(m->source);
70 free(m->destination);
71 free(m->options);
72
73 if (m->work_dir) {
74 (void) rm_rf(m->work_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
75 free(m->work_dir);
76 }
77
78 strv_free(m->lower);
79 }
80
81 free(l);
82}
83
84int custom_mount_compare(const void *a, const void *b) {
85 const CustomMount *x = a, *y = b;
86 int r;
87
88 r = path_compare(x->destination, y->destination);
89 if (r != 0)
90 return r;
91
92 if (x->type < y->type)
93 return -1;
94 if (x->type > y->type)
95 return 1;
96
97 return 0;
98}
99
100int bind_mount_parse(CustomMount **l, unsigned *n, const char *s, bool read_only) {
101 _cleanup_free_ char *source = NULL, *destination = NULL, *opts = NULL;
102 const char *p = s;
103 CustomMount *m;
104 int r;
105
106 assert(l);
107 assert(n);
108
109 r = extract_many_words(&p, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &source, &destination, NULL);
110 if (r < 0)
111 return r;
112 if (r == 0)
113 return -EINVAL;
114
115 if (r == 1) {
116 destination = strdup(source);
117 if (!destination)
118 return -ENOMEM;
119 }
120
121 if (r == 2 && !isempty(p)) {
122 opts = strdup(p);
123 if (!opts)
124 return -ENOMEM;
125 }
126
127 if (!path_is_absolute(source))
128 return -EINVAL;
129
130 if (!path_is_absolute(destination))
131 return -EINVAL;
132
133 m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND);
134 if (!m)
135 return log_oom();
136
137 m->source = source;
138 m->destination = destination;
139 m->read_only = read_only;
140 m->options = opts;
141
142 source = destination = opts = NULL;
143 return 0;
144}
145
146int tmpfs_mount_parse(CustomMount **l, unsigned *n, const char *s) {
147 _cleanup_free_ char *path = NULL, *opts = NULL;
148 const char *p = s;
149 CustomMount *m;
150 int r;
151
152 assert(l);
153 assert(n);
154 assert(s);
155
156 r = extract_first_word(&p, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
157 if (r < 0)
158 return r;
159 if (r == 0)
160 return -EINVAL;
161
162 if (isempty(p))
163 opts = strdup("mode=0755");
164 else
165 opts = strdup(p);
166 if (!opts)
167 return -ENOMEM;
168
169 if (!path_is_absolute(path))
170 return -EINVAL;
171
172 m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS);
173 if (!m)
174 return -ENOMEM;
175
176 m->destination = path;
177 m->options = opts;
178
179 path = opts = NULL;
180 return 0;
181}
182
183static int tmpfs_patch_options(
184 const char *options,
0996ef00
CB
185 bool userns,
186 uid_t uid_shift, uid_t uid_range,
187 bool patch_ids,
e83bebef
LP
188 const char *selinux_apifs_context,
189 char **ret) {
190
191 char *buf = NULL;
192
0996ef00 193 if ((userns && uid_shift != 0) || patch_ids) {
e83bebef
LP
194 assert(uid_shift != UID_INVALID);
195
9aa2169e
ZJS
196 if (asprintf(&buf, "%s%suid=" UID_FMT ",gid=" UID_FMT,
197 options ?: "", options ? "," : "",
198 uid_shift, uid_shift) < 0)
e83bebef
LP
199 return -ENOMEM;
200
201 options = buf;
202 }
203
204#ifdef HAVE_SELINUX
205 if (selinux_apifs_context) {
206 char *t;
207
9aa2169e
ZJS
208 t = strjoin(options ?: "", options ? "," : "",
209 "context=\"", selinux_apifs_context, "\"");
210 free(buf);
211 if (!t)
e83bebef 212 return -ENOMEM;
e83bebef 213
e83bebef
LP
214 buf = t;
215 }
216#endif
217
0996ef00
CB
218 if (!buf && options) {
219 buf = strdup(options);
220 if (!buf)
221 return -ENOMEM;
222 }
e83bebef 223 *ret = buf;
0996ef00 224
e83bebef
LP
225 return !!buf;
226}
227
4f086aab 228int mount_sysfs(const char *dest, MountSettingsMask mount_settings) {
d8fc6a00 229 const char *full, *top, *x;
d1678248 230 int r;
4f086aab 231 unsigned long extra_flags = 0;
d8fc6a00
LP
232
233 top = prefix_roota(dest, "/sys");
d1678248
ILG
234 r = path_check_fstype(top, SYSFS_MAGIC);
235 if (r < 0)
236 return log_error_errno(r, "Failed to determine filesystem type of %s: %m", top);
237 /* /sys might already be mounted as sysfs by the outer child in the
238 * !netns case. In this case, it's all good. Don't touch it because we
239 * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555.
240 */
241 if (r > 0)
242 return 0;
243
d8fc6a00
LP
244 full = prefix_roota(top, "/full");
245
246 (void) mkdir(full, 0755);
247
4f086aab
SU
248 if (mount_settings & MOUNT_APPLY_APIVFS_RO)
249 extra_flags |= MS_RDONLY;
250
60e76d48 251 r = mount_verbose(LOG_ERR, "sysfs", full, "sysfs",
4f086aab 252 MS_NOSUID|MS_NOEXEC|MS_NODEV|extra_flags, NULL);
60e76d48
ZJS
253 if (r < 0)
254 return r;
d8fc6a00
LP
255
256 FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") {
257 _cleanup_free_ char *from = NULL, *to = NULL;
258
259 from = prefix_root(full, x);
260 if (!from)
261 return log_oom();
262
263 to = prefix_root(top, x);
264 if (!to)
265 return log_oom();
266
267 (void) mkdir(to, 0755);
268
60e76d48
ZJS
269 r = mount_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
270 if (r < 0)
271 return r;
d8fc6a00 272
60e76d48 273 r = mount_verbose(LOG_ERR, NULL, to, NULL,
4f086aab 274 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
60e76d48
ZJS
275 if (r < 0)
276 return r;
d8fc6a00
LP
277 }
278
60e76d48
ZJS
279 r = umount_verbose(full);
280 if (r < 0)
281 return r;
d8fc6a00
LP
282
283 if (rmdir(full) < 0)
284 return log_error_errno(errno, "Failed to remove %s: %m", full);
285
286 x = prefix_roota(top, "/fs/kdbus");
0996ef00
CB
287 (void) mkdir_p(x, 0755);
288
289 /* Create mountpoint for cgroups. Otherwise we are not allowed since we
290 * remount /sys read-only.
291 */
292 if (cg_ns_supported()) {
293 x = prefix_roota(top, "/fs/cgroup");
294 (void) mkdir_p(x, 0755);
295 }
d8fc6a00 296
60e76d48 297 return mount_verbose(LOG_ERR, NULL, top, NULL,
4f086aab 298 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
d8fc6a00
LP
299}
300
acbbf69b 301static int mkdir_userns(const char *path, mode_t mode, MountSettingsMask mask, uid_t uid_shift) {
63eae723
EV
302 int r;
303
304 assert(path);
305
306 r = mkdir(path, mode);
307 if (r < 0 && errno != EEXIST)
308 return -errno;
309
acbbf69b
LP
310 if ((mask & MOUNT_USE_USERNS) == 0)
311 return 0;
312
313 if (mask & MOUNT_IN_USERNS)
314 return 0;
315
316 r = lchown(path, uid_shift, uid_shift);
317 if (r < 0)
318 return -errno;
63eae723
EV
319
320 return 0;
321}
322
acbbf69b 323static int mkdir_userns_p(const char *prefix, const char *path, mode_t mode, MountSettingsMask mask, uid_t uid_shift) {
63eae723
EV
324 const char *p, *e;
325 int r;
326
327 assert(path);
328
329 if (prefix && !path_startswith(path, prefix))
330 return -ENOTDIR;
331
332 /* create every parent directory in the path, except the last component */
333 p = path + strspn(path, "/");
334 for (;;) {
335 char t[strlen(path) + 1];
336
337 e = p + strcspn(p, "/");
338 p = e + strspn(e, "/");
339
340 /* Is this the last component? If so, then we're done */
341 if (*p == 0)
342 break;
343
344 memcpy(t, path, e - path);
345 t[e-path] = 0;
346
347 if (prefix && path_startswith(prefix, t))
348 continue;
349
acbbf69b 350 r = mkdir_userns(t, mode, mask, uid_shift);
63eae723
EV
351 if (r < 0)
352 return r;
353 }
354
acbbf69b 355 return mkdir_userns(path, mode, mask, uid_shift);
63eae723
EV
356}
357
e83bebef 358int mount_all(const char *dest,
4f086aab 359 MountSettingsMask mount_settings,
403af78c 360 uid_t uid_shift, uid_t uid_range,
e83bebef
LP
361 const char *selinux_apifs_context) {
362
363 typedef struct MountPoint {
364 const char *what;
365 const char *where;
366 const char *type;
367 const char *options;
368 unsigned long flags;
4f086aab 369 MountSettingsMask mount_settings;
e83bebef
LP
370 } MountPoint;
371
372 static const MountPoint mount_table[] = {
4f086aab
SU
373 /* inner child mounts */
374 { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL|MOUNT_IN_USERNS },
375 { "/proc/sys", "/proc/sys", NULL, NULL, MS_BIND, MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ...*/
376 { "/proc/sys/net", "/proc/sys/net", NULL, NULL, MS_BIND, MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_APIVFS_NETNS }, /* (except for this) */
377 { NULL, "/proc/sys", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* ... then, make it r/o */
378 { "/proc/sysrq-trigger", "/proc/sysrq-trigger", NULL, NULL, MS_BIND, MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ...*/
379 { NULL, "/proc/sysrq-trigger", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* ... then, make it r/o */
380 { "tmpfs", "/tmp", "tmpfs", "mode=1777", MS_STRICTATIME, MOUNT_FATAL|MOUNT_IN_USERNS },
381
382 /* outer child mounts */
383 { "tmpfs", "/sys", "tmpfs", "mode=755", MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL|MOUNT_APPLY_APIVFS_NETNS },
384 { "sysfs", "/sys", "sysfs", NULL, MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL|MOUNT_APPLY_APIVFS_RO }, /* skipped if above was mounted */
385 { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL }, /* skipped if above was mounted */
386
387 { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID|MS_STRICTATIME, MOUNT_FATAL },
388 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME, MOUNT_FATAL },
389 { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID|MS_NODEV|MS_STRICTATIME, MOUNT_FATAL },
e83bebef 390#ifdef HAVE_SELINUX
4f086aab
SU
391 { "/sys/fs/selinux", "/sys/fs/selinux", NULL, NULL, MS_BIND, 0 }, /* Bind mount first */
392 { NULL, "/sys/fs/selinux", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, 0 }, /* Then, make it r/o */
e83bebef
LP
393#endif
394 };
395
396 unsigned k;
397 int r;
4f086aab
SU
398 bool use_userns = (mount_settings & MOUNT_USE_USERNS);
399 bool netns = (mount_settings & MOUNT_APPLY_APIVFS_NETNS);
400 bool ro = (mount_settings & MOUNT_APPLY_APIVFS_RO);
401 bool in_userns = (mount_settings & MOUNT_IN_USERNS);
e83bebef
LP
402
403 for (k = 0; k < ELEMENTSOF(mount_table); k++) {
404 _cleanup_free_ char *where = NULL, *options = NULL;
405 const char *o;
4f086aab
SU
406 bool fatal = (mount_table[k].mount_settings & MOUNT_FATAL);
407
408 if (in_userns != (bool)(mount_table[k].mount_settings & MOUNT_IN_USERNS))
409 continue;
e83bebef 410
4f086aab 411 if (!netns && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_APIVFS_NETNS))
d1678248
ILG
412 continue;
413
4f086aab 414 if (!ro && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_APIVFS_RO))
e83bebef
LP
415 continue;
416
417 where = prefix_root(dest, mount_table[k].where);
418 if (!where)
419 return log_oom();
420
e1873695 421 r = path_is_mount_point(where, dest, AT_SYMLINK_FOLLOW);
e83bebef
LP
422 if (r < 0 && r != -ENOENT)
423 return log_error_errno(r, "Failed to detect whether %s is a mount point: %m", where);
424
425 /* Skip this entry if it is not a remount. */
426 if (mount_table[k].what && r > 0)
427 continue;
428
acbbf69b 429 r = mkdir_userns_p(dest, where, 0755, mount_settings, uid_shift);
920a7899 430 if (r < 0 && r != -EEXIST) {
4f086aab 431 if (fatal)
e83bebef
LP
432 return log_error_errno(r, "Failed to create directory %s: %m", where);
433
201b13c8 434 log_debug_errno(r, "Failed to create directory %s: %m", where);
e83bebef
LP
435 continue;
436 }
437
438 o = mount_table[k].options;
439 if (streq_ptr(mount_table[k].type, "tmpfs")) {
8492849e
EV
440 if (in_userns)
441 r = tmpfs_patch_options(o, use_userns, 0, uid_range, true, selinux_apifs_context, &options);
442 else
443 r = tmpfs_patch_options(o, use_userns, uid_shift, uid_range, false, selinux_apifs_context, &options);
e83bebef
LP
444 if (r < 0)
445 return log_oom();
446 if (r > 0)
447 o = options;
448 }
449
4f086aab 450 r = mount_verbose(fatal ? LOG_ERR : LOG_DEBUG,
60e76d48
ZJS
451 mount_table[k].what,
452 where,
453 mount_table[k].type,
454 mount_table[k].flags,
455 o);
4f086aab 456 if (r < 0 && fatal)
60e76d48 457 return r;
e83bebef
LP
458 }
459
460 return 0;
461}
462
463static int parse_mount_bind_options(const char *options, unsigned long *mount_flags, char **mount_opts) {
464 const char *p = options;
465 unsigned long flags = *mount_flags;
466 char *opts = NULL;
4da92e58 467 int r;
e83bebef
LP
468
469 assert(options);
470
471 for (;;) {
472 _cleanup_free_ char *word = NULL;
4da92e58
LP
473
474 r = extract_first_word(&p, &word, ",", 0);
e83bebef
LP
475 if (r < 0)
476 return log_error_errno(r, "Failed to extract mount option: %m");
477 if (r == 0)
478 break;
479
480 if (streq(word, "rbind"))
481 flags |= MS_REC;
482 else if (streq(word, "norbind"))
483 flags &= ~MS_REC;
484 else {
485 log_error("Invalid bind mount option: %s", word);
486 return -EINVAL;
487 }
488 }
489
490 *mount_flags = flags;
491 /* in the future mount_opts will hold string options for mount(2) */
492 *mount_opts = opts;
493
494 return 0;
495}
496
497static int mount_bind(const char *dest, CustomMount *m) {
68cf43c3
LP
498
499 _cleanup_free_ char *mount_opts = NULL, *where = NULL;
e83bebef 500 unsigned long mount_flags = MS_BIND | MS_REC;
68cf43c3 501 struct stat source_st, dest_st;
e83bebef
LP
502 int r;
503
504 assert(m);
505
506 if (m->options) {
507 r = parse_mount_bind_options(m->options, &mount_flags, &mount_opts);
508 if (r < 0)
509 return r;
510 }
511
512 if (stat(m->source, &source_st) < 0)
513 return log_error_errno(errno, "Failed to stat %s: %m", m->source);
514
68cf43c3
LP
515 r = chase_symlinks_prefix(m->destination, dest, &where);
516 if (r < 0)
517 return log_error_errno(r, "Failed to resolve %s: %m", m->destination);
e83bebef
LP
518
519 if (stat(where, &dest_st) >= 0) {
520 if (S_ISDIR(source_st.st_mode) && !S_ISDIR(dest_st.st_mode)) {
521 log_error("Cannot bind mount directory %s on file %s.", m->source, where);
522 return -EINVAL;
523 }
524
525 if (!S_ISDIR(source_st.st_mode) && S_ISDIR(dest_st.st_mode)) {
526 log_error("Cannot bind mount file %s on directory %s.", m->source, where);
527 return -EINVAL;
528 }
529
530 } else if (errno == ENOENT) {
531 r = mkdir_parents_label(where, 0755);
532 if (r < 0)
533 return log_error_errno(r, "Failed to make parents of %s: %m", where);
b97e83cb
BN
534
535 /* Create the mount point. Any non-directory file can be
536 * mounted on any non-directory file (regular, fifo, socket,
537 * char, block).
538 */
539 if (S_ISDIR(source_st.st_mode))
540 r = mkdir_label(where, 0755);
541 else
542 r = touch(where);
543 if (r < 0)
544 return log_error_errno(r, "Failed to create mount point %s: %m", where);
545
60e76d48 546 } else
e1427b13 547 return log_error_errno(errno, "Failed to stat %s: %m", where);
e83bebef 548
60e76d48
ZJS
549 r = mount_verbose(LOG_ERR, m->source, where, NULL, mount_flags, mount_opts);
550 if (r < 0)
551 return r;
e83bebef
LP
552
553 if (m->read_only) {
6b7c9f8b 554 r = bind_remount_recursive(where, true, NULL);
e83bebef
LP
555 if (r < 0)
556 return log_error_errno(r, "Read-only bind mount failed: %m");
557 }
558
559 return 0;
560}
561
562static int mount_tmpfs(
563 const char *dest,
564 CustomMount *m,
565 bool userns, uid_t uid_shift, uid_t uid_range,
566 const char *selinux_apifs_context) {
567
68cf43c3
LP
568 const char *options;
569 _cleanup_free_ char *buf = NULL, *where = NULL;
e83bebef
LP
570 int r;
571
572 assert(dest);
573 assert(m);
574
68cf43c3
LP
575 r = chase_symlinks_prefix(m->destination, dest, &where);
576 if (r < 0)
577 return log_error_errno(r, "Failed to resolve %s: %m", m->destination);
e83bebef
LP
578
579 r = mkdir_p_label(where, 0755);
580 if (r < 0 && r != -EEXIST)
581 return log_error_errno(r, "Creating mount point for tmpfs %s failed: %m", where);
582
0996ef00 583 r = tmpfs_patch_options(m->options, userns, uid_shift, uid_range, false, selinux_apifs_context, &buf);
e83bebef
LP
584 if (r < 0)
585 return log_oom();
586 options = r > 0 ? buf : m->options;
587
60e76d48 588 return mount_verbose(LOG_ERR, "tmpfs", where, "tmpfs", MS_NODEV|MS_STRICTATIME, options);
e83bebef
LP
589}
590
591static char *joined_and_escaped_lower_dirs(char * const *lower) {
592 _cleanup_strv_free_ char **sv = NULL;
593
594 sv = strv_copy(lower);
595 if (!sv)
596 return NULL;
597
598 strv_reverse(sv);
599
600 if (!strv_shell_escape(sv, ",:"))
601 return NULL;
602
603 return strv_join(sv, ":");
604}
605
606static int mount_overlay(const char *dest, CustomMount *m) {
68cf43c3
LP
607
608 _cleanup_free_ char *lower = NULL, *where = NULL;
609 const char *options;
e83bebef
LP
610 int r;
611
612 assert(dest);
613 assert(m);
614
68cf43c3
LP
615 r = chase_symlinks_prefix(m->destination, dest, &where);
616 if (r < 0)
617 return log_error_errno(r, "Failed to resolve %s: %m", m->destination);
e83bebef
LP
618
619 r = mkdir_label(where, 0755);
620 if (r < 0 && r != -EEXIST)
621 return log_error_errno(r, "Creating mount point for overlay %s failed: %m", where);
622
623 (void) mkdir_p_label(m->source, 0755);
624
625 lower = joined_and_escaped_lower_dirs(m->lower);
626 if (!lower)
627 return log_oom();
628
629 if (m->read_only) {
630 _cleanup_free_ char *escaped_source = NULL;
631
632 escaped_source = shell_escape(m->source, ",:");
633 if (!escaped_source)
634 return log_oom();
635
636 options = strjoina("lowerdir=", escaped_source, ":", lower);
637 } else {
638 _cleanup_free_ char *escaped_source = NULL, *escaped_work_dir = NULL;
639
640 assert(m->work_dir);
641 (void) mkdir_label(m->work_dir, 0700);
642
643 escaped_source = shell_escape(m->source, ",:");
644 if (!escaped_source)
645 return log_oom();
646 escaped_work_dir = shell_escape(m->work_dir, ",:");
647 if (!escaped_work_dir)
648 return log_oom();
649
650 options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir);
651 }
652
60e76d48 653 return mount_verbose(LOG_ERR, "overlay", where, "overlay", m->read_only ? MS_RDONLY : 0, options);
e83bebef
LP
654}
655
656int mount_custom(
657 const char *dest,
658 CustomMount *mounts, unsigned n,
659 bool userns, uid_t uid_shift, uid_t uid_range,
660 const char *selinux_apifs_context) {
661
662 unsigned i;
663 int r;
664
665 assert(dest);
666
667 for (i = 0; i < n; i++) {
668 CustomMount *m = mounts + i;
669
670 switch (m->type) {
671
672 case CUSTOM_MOUNT_BIND:
673 r = mount_bind(dest, m);
674 break;
675
676 case CUSTOM_MOUNT_TMPFS:
677 r = mount_tmpfs(dest, m, userns, uid_shift, uid_range, selinux_apifs_context);
678 break;
679
680 case CUSTOM_MOUNT_OVERLAY:
681 r = mount_overlay(dest, m);
682 break;
683
684 default:
685 assert_not_reached("Unknown custom mount type");
686 }
687
688 if (r < 0)
689 return r;
690 }
691
692 return 0;
693}
694
0996ef00
CB
695/* Retrieve existing subsystems. This function is called in a new cgroup
696 * namespace.
697 */
698static int get_controllers(Set *subsystems) {
699 _cleanup_fclose_ FILE *f = NULL;
700 char line[LINE_MAX];
701
702 assert(subsystems);
703
704 f = fopen("/proc/self/cgroup", "re");
705 if (!f)
706 return errno == ENOENT ? -ESRCH : -errno;
707
708 FOREACH_LINE(line, f, return -errno) {
709 int r;
710 char *e, *l, *p;
711
0996ef00
CB
712 l = strchr(line, ':');
713 if (!l)
714 continue;
715
716 l++;
717 e = strchr(l, ':');
718 if (!e)
719 continue;
720
721 *e = 0;
722
add554f4 723 if (STR_IN_SET(l, "", "name=systemd"))
0996ef00
CB
724 continue;
725
726 p = strdup(l);
add554f4
ZJS
727 if (!p)
728 return -ENOMEM;
729
0996ef00
CB
730 r = set_consume(subsystems, p);
731 if (r < 0)
732 return r;
733 }
734
735 return 0;
736}
737
e1873695
LP
738static int mount_legacy_cgroup_hierarchy(
739 const char *dest,
740 const char *controller,
741 const char *hierarchy,
742 CGroupUnified unified_requested,
743 bool read_only) {
744
60e76d48 745 const char *to, *fstype, *opts;
e83bebef
LP
746 int r;
747
ee30f6ac 748 to = strjoina(strempty(dest), "/sys/fs/cgroup/", hierarchy);
e83bebef 749
e1873695 750 r = path_is_mount_point(to, dest, 0);
e83bebef
LP
751 if (r < 0 && r != -ENOENT)
752 return log_error_errno(r, "Failed to determine if %s is mounted already: %m", to);
753 if (r > 0)
754 return 0;
755
756 mkdir_p(to, 0755);
757
758 /* The superblock mount options of the mount point need to be
759 * identical to the hosts', and hence writable... */
5da38d07 760 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
60e76d48
ZJS
761 if (unified_requested >= CGROUP_UNIFIED_SYSTEMD) {
762 fstype = "cgroup2";
763 opts = NULL;
764 } else {
765 fstype = "cgroup";
766 opts = "none,name=systemd,xattr";
767 }
768 } else {
769 fstype = "cgroup";
770 opts = controller;
771 }
5da38d07 772
60e76d48 773 r = mount_verbose(LOG_ERR, "cgroup", to, fstype, MS_NOSUID|MS_NOEXEC|MS_NODEV, opts);
5da38d07 774 if (r < 0)
60e76d48 775 return r;
e83bebef 776
60e76d48 777 /* ... hence let's only make the bind mount read-only, not the superblock. */
e83bebef 778 if (read_only) {
60e76d48
ZJS
779 r = mount_verbose(LOG_ERR, NULL, to, NULL,
780 MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, NULL);
781 if (r < 0)
782 return r;
e83bebef 783 }
60e76d48 784
e83bebef
LP
785 return 1;
786}
787
0996ef00
CB
788/* Mount a legacy cgroup hierarchy when cgroup namespaces are supported. */
789static int mount_legacy_cgns_supported(
e1873695
LP
790 const char *dest,
791 CGroupUnified unified_requested,
792 bool userns,
793 uid_t uid_shift,
794 uid_t uid_range,
795 const char *selinux_apifs_context) {
796
0996ef00
CB
797 _cleanup_set_free_free_ Set *controllers = NULL;
798 const char *cgroup_root = "/sys/fs/cgroup", *c;
799 int r;
e83bebef 800
0996ef00
CB
801 (void) mkdir_p(cgroup_root, 0755);
802
803 /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
e1873695 804 r = path_is_mount_point(cgroup_root, dest, AT_SYMLINK_FOLLOW);
0996ef00
CB
805 if (r < 0)
806 return log_error_errno(r, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
807 if (r == 0) {
808 _cleanup_free_ char *options = NULL;
809
810 /* When cgroup namespaces are enabled and user namespaces are
811 * used then the mount of the cgroupfs is done *inside* the new
812 * user namespace. We're root in the new user namespace and the
813 * kernel will happily translate our uid/gid to the correct
814 * uid/gid as seen from e.g. /proc/1/mountinfo. So we simply
815 * pass uid 0 and not uid_shift to tmpfs_patch_options().
816 */
817 r = tmpfs_patch_options("mode=755", userns, 0, uid_range, true, selinux_apifs_context, &options);
818 if (r < 0)
819 return log_oom();
820
60e76d48
ZJS
821 r = mount_verbose(LOG_ERR, "tmpfs", cgroup_root, "tmpfs",
822 MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, options);
823 if (r < 0)
824 return r;
0996ef00
CB
825 }
826
ca2f6384 827 if (cg_all_unified() > 0)
0996ef00
CB
828 goto skip_controllers;
829
830 controllers = set_new(&string_hash_ops);
831 if (!controllers)
832 return log_oom();
833
834 r = get_controllers(controllers);
835 if (r < 0)
836 return log_error_errno(r, "Failed to determine cgroup controllers: %m");
837
838 for (;;) {
839 _cleanup_free_ const char *controller = NULL;
840
841 controller = set_steal_first(controllers);
842 if (!controller)
843 break;
844
5da38d07 845 r = mount_legacy_cgroup_hierarchy("", controller, controller, unified_requested, !userns);
0996ef00
CB
846 if (r < 0)
847 return r;
848
849 /* When multiple hierarchies are co-mounted, make their
850 * constituting individual hierarchies a symlink to the
851 * co-mount.
852 */
853 c = controller;
854 for (;;) {
855 _cleanup_free_ char *target = NULL, *tok = NULL;
856
857 r = extract_first_word(&c, &tok, ",", 0);
858 if (r < 0)
859 return log_error_errno(r, "Failed to extract co-mounted cgroup controller: %m");
860 if (r == 0)
861 break;
862
863 target = prefix_root("/sys/fs/cgroup", tok);
864 if (!target)
865 return log_oom();
866
867 if (streq(controller, tok))
868 break;
869
870 r = symlink_idempotent(controller, target);
871 if (r == -EINVAL)
872 return log_error_errno(r, "Invalid existing symlink for combined hierarchy: %m");
873 if (r < 0)
874 return log_error_errno(r, "Failed to create symlink for combined hierarchy: %m");
875 }
876 }
877
878skip_controllers:
5da38d07 879 r = mount_legacy_cgroup_hierarchy("", SYSTEMD_CGROUP_CONTROLLER, "systemd", unified_requested, false);
0996ef00
CB
880 if (r < 0)
881 return r;
882
60e76d48
ZJS
883 if (!userns)
884 return mount_verbose(LOG_ERR, NULL, cgroup_root, NULL,
885 MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755");
0996ef00
CB
886
887 return 0;
888}
889
890/* Mount legacy cgroup hierarchy when cgroup namespaces are unsupported. */
891static int mount_legacy_cgns_unsupported(
892 const char *dest,
e1873695
LP
893 CGroupUnified unified_requested,
894 bool userns,
895 uid_t uid_shift,
896 uid_t uid_range,
0996ef00 897 const char *selinux_apifs_context) {
e1873695 898
e83bebef
LP
899 _cleanup_set_free_free_ Set *controllers = NULL;
900 const char *cgroup_root;
901 int r;
902
903 cgroup_root = prefix_roota(dest, "/sys/fs/cgroup");
904
d8fc6a00
LP
905 (void) mkdir_p(cgroup_root, 0755);
906
e83bebef 907 /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
e1873695 908 r = path_is_mount_point(cgroup_root, dest, AT_SYMLINK_FOLLOW);
e83bebef
LP
909 if (r < 0)
910 return log_error_errno(r, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
911 if (r == 0) {
912 _cleanup_free_ char *options = NULL;
913
0996ef00 914 r = tmpfs_patch_options("mode=755", userns, uid_shift, uid_range, false, selinux_apifs_context, &options);
e83bebef
LP
915 if (r < 0)
916 return log_oom();
917
60e76d48
ZJS
918 r = mount_verbose(LOG_ERR, "tmpfs", cgroup_root, "tmpfs",
919 MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, options);
920 if (r < 0)
921 return r;
e83bebef
LP
922 }
923
ca2f6384 924 if (cg_all_unified() > 0)
e83bebef
LP
925 goto skip_controllers;
926
927 controllers = set_new(&string_hash_ops);
928 if (!controllers)
929 return log_oom();
930
931 r = cg_kernel_controllers(controllers);
932 if (r < 0)
933 return log_error_errno(r, "Failed to determine cgroup controllers: %m");
934
935 for (;;) {
936 _cleanup_free_ char *controller = NULL, *origin = NULL, *combined = NULL;
937
938 controller = set_steal_first(controllers);
939 if (!controller)
940 break;
941
942 origin = prefix_root("/sys/fs/cgroup/", controller);
943 if (!origin)
944 return log_oom();
945
946 r = readlink_malloc(origin, &combined);
947 if (r == -EINVAL) {
948 /* Not a symbolic link, but directly a single cgroup hierarchy */
949
5da38d07 950 r = mount_legacy_cgroup_hierarchy(dest, controller, controller, unified_requested, true);
e83bebef
LP
951 if (r < 0)
952 return r;
953
954 } else if (r < 0)
955 return log_error_errno(r, "Failed to read link %s: %m", origin);
956 else {
957 _cleanup_free_ char *target = NULL;
958
959 target = prefix_root(dest, origin);
960 if (!target)
961 return log_oom();
962
963 /* A symbolic link, a combination of controllers in one hierarchy */
964
965 if (!filename_is_valid(combined)) {
966 log_warning("Ignoring invalid combined hierarchy %s.", combined);
967 continue;
968 }
969
5da38d07 970 r = mount_legacy_cgroup_hierarchy(dest, combined, combined, unified_requested, true);
e83bebef
LP
971 if (r < 0)
972 return r;
973
974 r = symlink_idempotent(combined, target);
0996ef00
CB
975 if (r == -EINVAL)
976 return log_error_errno(r, "Invalid existing symlink for combined hierarchy: %m");
e83bebef
LP
977 if (r < 0)
978 return log_error_errno(r, "Failed to create symlink for combined hierarchy: %m");
979 }
980 }
981
982skip_controllers:
5da38d07 983 r = mount_legacy_cgroup_hierarchy(dest, SYSTEMD_CGROUP_CONTROLLER, "systemd", unified_requested, false);
e83bebef
LP
984 if (r < 0)
985 return r;
986
60e76d48
ZJS
987 return mount_verbose(LOG_ERR, NULL, cgroup_root, NULL,
988 MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755");
e83bebef
LP
989}
990
991static int mount_unified_cgroups(const char *dest) {
992 const char *p;
993 int r;
994
995 assert(dest);
996
88e10572
MT
997 p = prefix_roota(dest, "/sys/fs/cgroup");
998
999 (void) mkdir_p(p, 0755);
e83bebef 1000
e1873695 1001 r = path_is_mount_point(p, dest, AT_SYMLINK_FOLLOW);
e83bebef
LP
1002 if (r < 0)
1003 return log_error_errno(r, "Failed to determine if %s is mounted already: %m", p);
1004 if (r > 0) {
88e10572 1005 p = prefix_roota(dest, "/sys/fs/cgroup/cgroup.procs");
e83bebef
LP
1006 if (access(p, F_OK) >= 0)
1007 return 0;
1008 if (errno != ENOENT)
1009 return log_error_errno(errno, "Failed to determine if mount point %s contains the unified cgroup hierarchy: %m", p);
1010
1011 log_error("%s is already mounted but not a unified cgroup hierarchy. Refusing.", p);
1012 return -EINVAL;
1013 }
1014
60e76d48 1015 return mount_verbose(LOG_ERR, "cgroup", p, "cgroup2", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
e83bebef
LP
1016}
1017
1018int mount_cgroups(
1019 const char *dest,
5da38d07 1020 CGroupUnified unified_requested,
e1873695
LP
1021 bool userns,
1022 uid_t uid_shift,
1023 uid_t uid_range,
5a8ff0e6
CB
1024 const char *selinux_apifs_context,
1025 bool use_cgns) {
e83bebef 1026
5da38d07 1027 if (unified_requested >= CGROUP_UNIFIED_ALL)
e83bebef 1028 return mount_unified_cgroups(dest);
ada54120 1029 else if (use_cgns)
e1873695 1030 return mount_legacy_cgns_supported(dest, unified_requested, userns, uid_shift, uid_range, selinux_apifs_context);
0996ef00 1031
5da38d07 1032 return mount_legacy_cgns_unsupported(dest, unified_requested, userns, uid_shift, uid_range, selinux_apifs_context);
e83bebef
LP
1033}
1034
1035int mount_systemd_cgroup_writable(
1036 const char *dest,
5da38d07 1037 CGroupUnified unified_requested) {
e83bebef
LP
1038
1039 _cleanup_free_ char *own_cgroup_path = NULL;
1040 const char *systemd_root, *systemd_own;
1041 int r;
1042
1043 assert(dest);
1044
1045 r = cg_pid_get_path(NULL, 0, &own_cgroup_path);
1046 if (r < 0)
1047 return log_error_errno(r, "Failed to determine our own cgroup path: %m");
1048
1049 /* If we are living in the top-level, then there's nothing to do... */
1050 if (path_equal(own_cgroup_path, "/"))
1051 return 0;
1052
5da38d07 1053 if (unified_requested >= CGROUP_UNIFIED_ALL) {
e83bebef
LP
1054 systemd_own = strjoina(dest, "/sys/fs/cgroup", own_cgroup_path);
1055 systemd_root = prefix_roota(dest, "/sys/fs/cgroup");
1056 } else {
1057 systemd_own = strjoina(dest, "/sys/fs/cgroup/systemd", own_cgroup_path);
1058 systemd_root = prefix_roota(dest, "/sys/fs/cgroup/systemd");
1059 }
1060
1061 /* Make our own cgroup a (writable) bind mount */
60e76d48
ZJS
1062 r = mount_verbose(LOG_ERR, systemd_own, systemd_own, NULL, MS_BIND, NULL);
1063 if (r < 0)
1064 return r;
e83bebef
LP
1065
1066 /* And then remount the systemd cgroup root read-only */
60e76d48
ZJS
1067 return mount_verbose(LOG_ERR, NULL, systemd_root, NULL,
1068 MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, NULL);
e83bebef
LP
1069}
1070
1071int setup_volatile_state(
1072 const char *directory,
1073 VolatileMode mode,
1074 bool userns, uid_t uid_shift, uid_t uid_range,
1075 const char *selinux_apifs_context) {
1076
1077 _cleanup_free_ char *buf = NULL;
1078 const char *p, *options;
1079 int r;
1080
1081 assert(directory);
1082
1083 if (mode != VOLATILE_STATE)
1084 return 0;
1085
1086 /* --volatile=state means we simply overmount /var
1087 with a tmpfs, and the rest read-only. */
1088
6b7c9f8b 1089 r = bind_remount_recursive(directory, true, NULL);
e83bebef
LP
1090 if (r < 0)
1091 return log_error_errno(r, "Failed to remount %s read-only: %m", directory);
1092
1093 p = prefix_roota(directory, "/var");
1094 r = mkdir(p, 0755);
1095 if (r < 0 && errno != EEXIST)
1096 return log_error_errno(errno, "Failed to create %s: %m", directory);
1097
1098 options = "mode=755";
0996ef00 1099 r = tmpfs_patch_options(options, userns, uid_shift, uid_range, false, selinux_apifs_context, &buf);
e83bebef
LP
1100 if (r < 0)
1101 return log_oom();
1102 if (r > 0)
1103 options = buf;
1104
60e76d48 1105 return mount_verbose(LOG_ERR, "tmpfs", p, "tmpfs", MS_STRICTATIME, options);
e83bebef
LP
1106}
1107
1108int setup_volatile(
1109 const char *directory,
1110 VolatileMode mode,
1111 bool userns, uid_t uid_shift, uid_t uid_range,
1112 const char *selinux_apifs_context) {
1113
1114 bool tmpfs_mounted = false, bind_mounted = false;
1115 char template[] = "/tmp/nspawn-volatile-XXXXXX";
1116 _cleanup_free_ char *buf = NULL;
1117 const char *f, *t, *options;
1118 int r;
1119
1120 assert(directory);
1121
1122 if (mode != VOLATILE_YES)
1123 return 0;
1124
1125 /* --volatile=yes means we mount a tmpfs to the root dir, and
1126 the original /usr to use inside it, and that read-only. */
1127
1128 if (!mkdtemp(template))
1129 return log_error_errno(errno, "Failed to create temporary directory: %m");
1130
1131 options = "mode=755";
0996ef00 1132 r = tmpfs_patch_options(options, userns, uid_shift, uid_range, false, selinux_apifs_context, &buf);
e83bebef
LP
1133 if (r < 0)
1134 return log_oom();
1135 if (r > 0)
1136 options = buf;
1137
60e76d48
ZJS
1138 r = mount_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
1139 if (r < 0)
e83bebef 1140 goto fail;
e83bebef
LP
1141
1142 tmpfs_mounted = true;
1143
1144 f = prefix_roota(directory, "/usr");
1145 t = prefix_roota(template, "/usr");
1146
1147 r = mkdir(t, 0755);
1148 if (r < 0 && errno != EEXIST) {
1149 r = log_error_errno(errno, "Failed to create %s: %m", t);
1150 goto fail;
1151 }
1152
60e76d48
ZJS
1153 r = mount_verbose(LOG_ERR, f, t, NULL, MS_BIND|MS_REC, NULL);
1154 if (r < 0)
e83bebef 1155 goto fail;
e83bebef
LP
1156
1157 bind_mounted = true;
1158
6b7c9f8b 1159 r = bind_remount_recursive(t, true, NULL);
e83bebef
LP
1160 if (r < 0) {
1161 log_error_errno(r, "Failed to remount %s read-only: %m", t);
1162 goto fail;
1163 }
1164
60e76d48
ZJS
1165 r = mount_verbose(LOG_ERR, template, directory, NULL, MS_MOVE, NULL);
1166 if (r < 0)
e83bebef 1167 goto fail;
e83bebef
LP
1168
1169 (void) rmdir(template);
1170
1171 return 0;
1172
1173fail:
1174 if (bind_mounted)
60e76d48 1175 (void) umount_verbose(t);
e83bebef
LP
1176
1177 if (tmpfs_mounted)
60e76d48 1178 (void) umount_verbose(template);
e83bebef
LP
1179 (void) rmdir(template);
1180 return r;
1181}
1182
1183VolatileMode volatile_mode_from_string(const char *s) {
1184 int b;
1185
1186 if (isempty(s))
1187 return _VOLATILE_MODE_INVALID;
1188
1189 b = parse_boolean(s);
1190 if (b > 0)
1191 return VOLATILE_YES;
1192 if (b == 0)
1193 return VOLATILE_NO;
1194
1195 if (streq(s, "state"))
1196 return VOLATILE_STATE;
1197
1198 return _VOLATILE_MODE_INVALID;
1199}