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