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