]>
Commit | Line | Data |
---|---|---|
15ae422b LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
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 | |
15ae422b LP |
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 | |
5430f7f2 | 14 | Lesser General Public License for more details. |
15ae422b | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
15ae422b LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
20 | #include <errno.h> | |
07630cea | 21 | #include <sched.h> |
15ae422b | 22 | #include <stdio.h> |
07630cea LP |
23 | #include <string.h> |
24 | #include <sys/mount.h> | |
15ae422b | 25 | #include <sys/stat.h> |
07630cea | 26 | #include <unistd.h> |
25e870b5 | 27 | #include <linux/fs.h> |
15ae422b | 28 | |
b5efdb8a | 29 | #include "alloc-util.h" |
10404d52 | 30 | #include "base-filesystem.h" |
7f112f50 | 31 | #include "dev-setup.h" |
3ffd4af2 | 32 | #include "fd-util.h" |
d944dc95 | 33 | #include "fs-util.h" |
e908468b | 34 | #include "label.h" |
915e6d16 | 35 | #include "loop-util.h" |
07630cea LP |
36 | #include "loopback-setup.h" |
37 | #include "missing.h" | |
38 | #include "mkdir.h" | |
4349cd7c | 39 | #include "mount-util.h" |
3ffd4af2 | 40 | #include "namespace.h" |
07630cea | 41 | #include "path-util.h" |
d7b8eec7 | 42 | #include "selinux-util.h" |
2583fbea | 43 | #include "socket-util.h" |
8b43440b | 44 | #include "string-table.h" |
07630cea LP |
45 | #include "string-util.h" |
46 | #include "strv.h" | |
affb60b1 | 47 | #include "umask-util.h" |
ee104e11 | 48 | #include "user-util.h" |
07630cea | 49 | #include "util.h" |
15ae422b | 50 | |
737ba3c8 | 51 | #define DEV_MOUNT_OPTIONS (MS_NOSUID|MS_STRICTATIME|MS_NOEXEC) |
52 | ||
c17ec25e | 53 | typedef enum MountMode { |
15ae422b LP |
54 | /* This is ordered by priority! */ |
55 | INACCESSIBLE, | |
d2d6c096 LP |
56 | BIND_MOUNT, |
57 | BIND_MOUNT_RECURSIVE, | |
ac0930c8 LP |
58 | PRIVATE_TMP, |
59 | PRIVATE_VAR_TMP, | |
7f112f50 | 60 | PRIVATE_DEV, |
5d997827 | 61 | BIND_DEV, |
6c47cd7d | 62 | EMPTY_DIR, |
5d997827 LP |
63 | SYSFS, |
64 | PROCFS, | |
65 | READONLY, | |
59eeb84b | 66 | READWRITE, |
c17ec25e | 67 | } MountMode; |
15ae422b | 68 | |
34de407a | 69 | typedef struct MountEntry { |
5327c910 | 70 | const char *path_const; /* Memory allocated on stack or static */ |
cfbeb4ef | 71 | MountMode mode:5; |
5327c910 LP |
72 | bool ignore:1; /* Ignore if path does not exist? */ |
73 | bool has_prefix:1; /* Already is prefixed by the root dir? */ | |
cfbeb4ef | 74 | bool read_only:1; /* Shall this mount point be read-only? */ |
5327c910 | 75 | char *path_malloc; /* Use this instead of 'path' if we had to allocate memory */ |
d2d6c096 LP |
76 | const char *source_const; /* The source path, for bind mounts */ |
77 | char *source_malloc; | |
34de407a | 78 | } MountEntry; |
15ae422b | 79 | |
5d997827 LP |
80 | /* If MountAPIVFS= is used, let's mount /sys and /proc into the it, but only as a fallback if the user hasn't mounted |
81 | * something there already. These mounts are hence overriden by any other explicitly configured mounts. */ | |
82 | static const MountEntry apivfs_table[] = { | |
83 | { "/proc", PROCFS, false }, | |
84 | { "/dev", BIND_DEV, false }, | |
85 | { "/sys", SYSFS, false }, | |
86 | }; | |
f471b2af | 87 | |
11a30cec | 88 | /* ProtectKernelTunables= option and the related filesystem APIs */ |
34de407a | 89 | static const MountEntry protect_kernel_tunables_table[] = { |
c6232fb0 LP |
90 | { "/proc/sys", READONLY, false }, |
91 | { "/proc/sysrq-trigger", READONLY, true }, | |
92 | { "/proc/latency_stats", READONLY, true }, | |
93 | { "/proc/mtrr", READONLY, true }, | |
aa70f38b | 94 | { "/proc/apm", READONLY, true }, /* Obsolete API, there's no point in permitting access to this, ever */ |
c6232fb0 LP |
95 | { "/proc/acpi", READONLY, true }, |
96 | { "/proc/timer_stats", READONLY, true }, | |
97 | { "/proc/asound", READONLY, true }, | |
98 | { "/proc/bus", READONLY, true }, | |
99 | { "/proc/fs", READONLY, true }, | |
100 | { "/proc/irq", READONLY, true }, | |
101 | { "/sys", READONLY, false }, | |
102 | { "/sys/kernel/debug", READONLY, true }, | |
103 | { "/sys/kernel/tracing", READONLY, true }, | |
104 | { "/sys/fs/cgroup", READWRITE, false }, /* READONLY is set by ProtectControlGroups= option */ | |
3a0bf6d6 | 105 | { "/sys/fs/selinux", READWRITE, true }, |
11a30cec DH |
106 | }; |
107 | ||
c575770b | 108 | /* ProtectKernelModules= option */ |
34de407a | 109 | static const MountEntry protect_kernel_modules_table[] = { |
c575770b | 110 | #ifdef HAVE_SPLIT_USR |
c6232fb0 | 111 | { "/lib/modules", INACCESSIBLE, true }, |
c575770b | 112 | #endif |
c6232fb0 | 113 | { "/usr/lib/modules", INACCESSIBLE, true }, |
c575770b DH |
114 | }; |
115 | ||
b6c432ca DH |
116 | /* |
117 | * ProtectHome=read-only table, protect $HOME and $XDG_RUNTIME_DIR and rest of | |
118 | * system should be protected by ProtectSystem= | |
119 | */ | |
34de407a | 120 | static const MountEntry protect_home_read_only_table[] = { |
c6232fb0 LP |
121 | { "/home", READONLY, true }, |
122 | { "/run/user", READONLY, true }, | |
123 | { "/root", READONLY, true }, | |
b6c432ca DH |
124 | }; |
125 | ||
126 | /* ProtectHome=yes table */ | |
34de407a | 127 | static const MountEntry protect_home_yes_table[] = { |
c6232fb0 LP |
128 | { "/home", INACCESSIBLE, true }, |
129 | { "/run/user", INACCESSIBLE, true }, | |
130 | { "/root", INACCESSIBLE, true }, | |
b6c432ca DH |
131 | }; |
132 | ||
f471b2af | 133 | /* ProtectSystem=yes table */ |
34de407a | 134 | static const MountEntry protect_system_yes_table[] = { |
c6232fb0 LP |
135 | { "/usr", READONLY, false }, |
136 | { "/boot", READONLY, true }, | |
137 | { "/efi", READONLY, true }, | |
f471b2af DH |
138 | }; |
139 | ||
140 | /* ProtectSystem=full includes ProtectSystem=yes */ | |
34de407a | 141 | static const MountEntry protect_system_full_table[] = { |
c6232fb0 LP |
142 | { "/usr", READONLY, false }, |
143 | { "/boot", READONLY, true }, | |
144 | { "/efi", READONLY, true }, | |
145 | { "/etc", READONLY, false }, | |
f471b2af DH |
146 | }; |
147 | ||
148 | /* | |
149 | * ProtectSystem=strict table. In this strict mode, we mount everything | |
150 | * read-only, except for /proc, /dev, /sys which are the kernel API VFS, | |
151 | * which are left writable, but PrivateDevices= + ProtectKernelTunables= | |
152 | * protect those, and these options should be fully orthogonal. | |
153 | * (And of course /home and friends are also left writable, as ProtectHome= | |
154 | * shall manage those, orthogonally). | |
155 | */ | |
34de407a | 156 | static const MountEntry protect_system_strict_table[] = { |
ddbe0412 LP |
157 | { "/", READONLY, false }, |
158 | { "/proc", READWRITE, false }, /* ProtectKernelTunables= */ | |
159 | { "/sys", READWRITE, false }, /* ProtectKernelTunables= */ | |
160 | { "/dev", READWRITE, false }, /* PrivateDevices= */ | |
161 | { "/home", READWRITE, true }, /* ProtectHome= */ | |
162 | { "/run/user", READWRITE, true }, /* ProtectHome= */ | |
163 | { "/root", READWRITE, true }, /* ProtectHome= */ | |
f471b2af DH |
164 | }; |
165 | ||
34de407a | 166 | static const char *mount_entry_path(const MountEntry *p) { |
f0a4feb0 DH |
167 | assert(p); |
168 | ||
5327c910 LP |
169 | /* Returns the path of this bind mount. If the malloc()-allocated ->path_buffer field is set we return that, |
170 | * otherwise the stack/static ->path field is returned. */ | |
f0a4feb0 | 171 | |
5327c910 | 172 | return p->path_malloc ?: p->path_const; |
f0a4feb0 DH |
173 | } |
174 | ||
34de407a | 175 | static bool mount_entry_read_only(const MountEntry *p) { |
cfbeb4ef LP |
176 | assert(p); |
177 | ||
178 | return p->read_only || IN_SET(p->mode, READONLY, INACCESSIBLE); | |
179 | } | |
180 | ||
d2d6c096 LP |
181 | static const char *mount_entry_source(const MountEntry *p) { |
182 | assert(p); | |
183 | ||
184 | return p->source_malloc ?: p->source_const; | |
185 | } | |
186 | ||
1eb7e08e LP |
187 | static void mount_entry_done(MountEntry *p) { |
188 | assert(p); | |
189 | ||
190 | p->path_malloc = mfree(p->path_malloc); | |
191 | p->source_malloc = mfree(p->source_malloc); | |
192 | } | |
193 | ||
34de407a | 194 | static int append_access_mounts(MountEntry **p, char **strv, MountMode mode) { |
15ae422b LP |
195 | char **i; |
196 | ||
613b411c LP |
197 | assert(p); |
198 | ||
5327c910 LP |
199 | /* Adds a list of user-supplied READWRITE/READONLY/INACCESSIBLE entries */ |
200 | ||
15ae422b | 201 | STRV_FOREACH(i, strv) { |
5327c910 LP |
202 | bool ignore = false, needs_prefix = false; |
203 | const char *e = *i; | |
15ae422b | 204 | |
5327c910 LP |
205 | /* Look for any prefixes */ |
206 | if (startswith(e, "-")) { | |
207 | e++; | |
9c94d52e | 208 | ignore = true; |
ea92ae33 | 209 | } |
5327c910 LP |
210 | if (startswith(e, "+")) { |
211 | e++; | |
212 | needs_prefix = true; | |
213 | } | |
ea92ae33 | 214 | |
5327c910 | 215 | if (!path_is_absolute(e)) |
15ae422b LP |
216 | return -EINVAL; |
217 | ||
34de407a | 218 | *((*p)++) = (MountEntry) { |
5327c910 LP |
219 | .path_const = e, |
220 | .mode = mode, | |
221 | .ignore = ignore, | |
222 | .has_prefix = !needs_prefix, | |
223 | }; | |
15ae422b LP |
224 | } |
225 | ||
226 | return 0; | |
227 | } | |
228 | ||
6c47cd7d LP |
229 | static int append_empty_dir_mounts(MountEntry **p, char **strv) { |
230 | char **i; | |
231 | ||
232 | assert(p); | |
233 | ||
234 | /* Adds tmpfs mounts to provide readable but empty directories. This is primarily used to implement the | |
235 | * "/private/" boundary directories for DynamicUser=1. */ | |
236 | ||
237 | STRV_FOREACH(i, strv) { | |
238 | ||
239 | *((*p)++) = (MountEntry) { | |
240 | .path_const = *i, | |
241 | .mode = EMPTY_DIR, | |
242 | .ignore = false, | |
243 | .has_prefix = false, | |
244 | .read_only = true, | |
245 | }; | |
246 | } | |
247 | ||
248 | return 0; | |
249 | } | |
250 | ||
d2d6c096 LP |
251 | static int append_bind_mounts(MountEntry **p, const BindMount *binds, unsigned n) { |
252 | unsigned i; | |
253 | ||
254 | assert(p); | |
255 | ||
256 | for (i = 0; i < n; i++) { | |
257 | const BindMount *b = binds + i; | |
258 | ||
259 | *((*p)++) = (MountEntry) { | |
260 | .path_const = b->destination, | |
261 | .mode = b->recursive ? BIND_MOUNT_RECURSIVE : BIND_MOUNT, | |
262 | .read_only = b->read_only, | |
263 | .source_const = b->source, | |
264 | }; | |
265 | } | |
266 | ||
267 | return 0; | |
268 | } | |
269 | ||
34de407a | 270 | static int append_static_mounts(MountEntry **p, const MountEntry *mounts, unsigned n, bool ignore_protect) { |
f471b2af | 271 | unsigned i; |
11a30cec DH |
272 | |
273 | assert(p); | |
f471b2af | 274 | assert(mounts); |
11a30cec | 275 | |
5327c910 | 276 | /* Adds a list of static pre-defined entries */ |
f471b2af | 277 | |
5327c910 | 278 | for (i = 0; i < n; i++) |
34de407a LP |
279 | *((*p)++) = (MountEntry) { |
280 | .path_const = mount_entry_path(mounts+i), | |
5327c910 LP |
281 | .mode = mounts[i].mode, |
282 | .ignore = mounts[i].ignore || ignore_protect, | |
283 | }; | |
f471b2af DH |
284 | |
285 | return 0; | |
286 | } | |
287 | ||
34de407a | 288 | static int append_protect_home(MountEntry **p, ProtectHome protect_home, bool ignore_protect) { |
c575770b DH |
289 | assert(p); |
290 | ||
5327c910 | 291 | switch (protect_home) { |
b6c432ca | 292 | |
5327c910 | 293 | case PROTECT_HOME_NO: |
b6c432ca DH |
294 | return 0; |
295 | ||
b6c432ca | 296 | case PROTECT_HOME_READ_ONLY: |
5327c910 LP |
297 | return append_static_mounts(p, protect_home_read_only_table, ELEMENTSOF(protect_home_read_only_table), ignore_protect); |
298 | ||
b6c432ca | 299 | case PROTECT_HOME_YES: |
5327c910 LP |
300 | return append_static_mounts(p, protect_home_yes_table, ELEMENTSOF(protect_home_yes_table), ignore_protect); |
301 | ||
b6c432ca | 302 | default: |
5327c910 | 303 | assert_not_reached("Unexpected ProtectHome= value"); |
b6c432ca | 304 | } |
b6c432ca DH |
305 | } |
306 | ||
34de407a | 307 | static int append_protect_system(MountEntry **p, ProtectSystem protect_system, bool ignore_protect) { |
f471b2af DH |
308 | assert(p); |
309 | ||
5327c910 LP |
310 | switch (protect_system) { |
311 | ||
312 | case PROTECT_SYSTEM_NO: | |
f471b2af DH |
313 | return 0; |
314 | ||
f471b2af | 315 | case PROTECT_SYSTEM_STRICT: |
5327c910 LP |
316 | return append_static_mounts(p, protect_system_strict_table, ELEMENTSOF(protect_system_strict_table), ignore_protect); |
317 | ||
f471b2af | 318 | case PROTECT_SYSTEM_YES: |
5327c910 LP |
319 | return append_static_mounts(p, protect_system_yes_table, ELEMENTSOF(protect_system_yes_table), ignore_protect); |
320 | ||
f471b2af | 321 | case PROTECT_SYSTEM_FULL: |
5327c910 LP |
322 | return append_static_mounts(p, protect_system_full_table, ELEMENTSOF(protect_system_full_table), ignore_protect); |
323 | ||
f471b2af | 324 | default: |
5327c910 | 325 | assert_not_reached("Unexpected ProtectSystem= value"); |
f471b2af | 326 | } |
11a30cec DH |
327 | } |
328 | ||
c17ec25e | 329 | static int mount_path_compare(const void *a, const void *b) { |
34de407a | 330 | const MountEntry *p = a, *q = b; |
a0827e2b | 331 | int d; |
15ae422b | 332 | |
6ee1a919 | 333 | /* If the paths are not equal, then order prefixes first */ |
34de407a | 334 | d = path_compare(mount_entry_path(p), mount_entry_path(q)); |
6ee1a919 LP |
335 | if (d != 0) |
336 | return d; | |
15ae422b | 337 | |
6ee1a919 LP |
338 | /* If the paths are equal, check the mode */ |
339 | if (p->mode < q->mode) | |
340 | return -1; | |
15ae422b | 341 | |
6ee1a919 LP |
342 | if (p->mode > q->mode) |
343 | return 1; | |
15ae422b | 344 | |
6ee1a919 | 345 | return 0; |
15ae422b LP |
346 | } |
347 | ||
34de407a | 348 | static int prefix_where_needed(MountEntry *m, unsigned n, const char *root_directory) { |
5327c910 LP |
349 | unsigned i; |
350 | ||
351 | /* Prefixes all paths in the bind mount table with the root directory if it is specified and the entry needs | |
352 | * that. */ | |
353 | ||
354 | if (!root_directory) | |
355 | return 0; | |
356 | ||
357 | for (i = 0; i < n; i++) { | |
358 | char *s; | |
359 | ||
360 | if (m[i].has_prefix) | |
361 | continue; | |
362 | ||
34de407a | 363 | s = prefix_root(root_directory, mount_entry_path(m+i)); |
5327c910 LP |
364 | if (!s) |
365 | return -ENOMEM; | |
366 | ||
367 | free(m[i].path_malloc); | |
368 | m[i].path_malloc = s; | |
369 | ||
370 | m[i].has_prefix = true; | |
371 | } | |
372 | ||
373 | return 0; | |
374 | } | |
375 | ||
34de407a LP |
376 | static void drop_duplicates(MountEntry *m, unsigned *n) { |
377 | MountEntry *f, *t, *previous; | |
15ae422b | 378 | |
c17ec25e | 379 | assert(m); |
15ae422b | 380 | assert(n); |
15ae422b | 381 | |
fe3c2583 LP |
382 | /* Drops duplicate entries. Expects that the array is properly ordered already. */ |
383 | ||
1d54cd5d | 384 | for (f = m, t = m, previous = NULL; f < m + *n; f++) { |
15ae422b | 385 | |
fe3c2583 LP |
386 | /* The first one wins (which is the one with the more restrictive mode), see mount_path_compare() |
387 | * above. */ | |
34de407a LP |
388 | if (previous && path_equal(mount_entry_path(f), mount_entry_path(previous))) { |
389 | log_debug("%s is duplicate.", mount_entry_path(f)); | |
390 | previous->read_only = previous->read_only || mount_entry_read_only(f); /* Propagate the read-only flag to the remaining entry */ | |
1eb7e08e | 391 | mount_entry_done(f); |
15ae422b | 392 | continue; |
fe3c2583 | 393 | } |
15ae422b | 394 | |
e2d7c1a0 | 395 | *t = *f; |
15ae422b | 396 | previous = t; |
fe3c2583 LP |
397 | t++; |
398 | } | |
399 | ||
400 | *n = t - m; | |
401 | } | |
402 | ||
34de407a LP |
403 | static void drop_inaccessible(MountEntry *m, unsigned *n) { |
404 | MountEntry *f, *t; | |
fe3c2583 LP |
405 | const char *clear = NULL; |
406 | ||
407 | assert(m); | |
408 | assert(n); | |
409 | ||
410 | /* Drops all entries obstructed by another entry further up the tree. Expects that the array is properly | |
411 | * ordered already. */ | |
412 | ||
1d54cd5d | 413 | for (f = m, t = m; f < m + *n; f++) { |
fe3c2583 LP |
414 | |
415 | /* If we found a path set for INACCESSIBLE earlier, and this entry has it as prefix we should drop | |
416 | * it, as inaccessible paths really should drop the entire subtree. */ | |
34de407a LP |
417 | if (clear && path_startswith(mount_entry_path(f), clear)) { |
418 | log_debug("%s is masked by %s.", mount_entry_path(f), clear); | |
1eb7e08e | 419 | mount_entry_done(f); |
fe3c2583 LP |
420 | continue; |
421 | } | |
15ae422b | 422 | |
34de407a | 423 | clear = f->mode == INACCESSIBLE ? mount_entry_path(f) : NULL; |
fe3c2583 LP |
424 | |
425 | *t = *f; | |
15ae422b LP |
426 | t++; |
427 | } | |
428 | ||
c17ec25e | 429 | *n = t - m; |
15ae422b LP |
430 | } |
431 | ||
34de407a LP |
432 | static void drop_nop(MountEntry *m, unsigned *n) { |
433 | MountEntry *f, *t; | |
7648a565 LP |
434 | |
435 | assert(m); | |
436 | assert(n); | |
437 | ||
438 | /* Drops all entries which have an immediate parent that has the same type, as they are redundant. Assumes the | |
439 | * list is ordered by prefixes. */ | |
440 | ||
1d54cd5d | 441 | for (f = m, t = m; f < m + *n; f++) { |
7648a565 LP |
442 | |
443 | /* Only suppress such subtrees for READONLY and READWRITE entries */ | |
444 | if (IN_SET(f->mode, READONLY, READWRITE)) { | |
34de407a | 445 | MountEntry *p; |
7648a565 LP |
446 | bool found = false; |
447 | ||
448 | /* Now let's find the first parent of the entry we are looking at. */ | |
449 | for (p = t-1; p >= m; p--) { | |
34de407a | 450 | if (path_startswith(mount_entry_path(f), mount_entry_path(p))) { |
7648a565 LP |
451 | found = true; |
452 | break; | |
453 | } | |
454 | } | |
455 | ||
456 | /* We found it, let's see if it's the same mode, if so, we can drop this entry */ | |
457 | if (found && p->mode == f->mode) { | |
34de407a | 458 | log_debug("%s is redundant by %s", mount_entry_path(f), mount_entry_path(p)); |
1eb7e08e | 459 | mount_entry_done(f); |
7648a565 LP |
460 | continue; |
461 | } | |
462 | } | |
463 | ||
464 | *t = *f; | |
465 | t++; | |
466 | } | |
467 | ||
468 | *n = t - m; | |
469 | } | |
470 | ||
34de407a LP |
471 | static void drop_outside_root(const char *root_directory, MountEntry *m, unsigned *n) { |
472 | MountEntry *f, *t; | |
cd2902c9 LP |
473 | |
474 | assert(m); | |
475 | assert(n); | |
476 | ||
1d54cd5d | 477 | /* Nothing to do */ |
cd2902c9 LP |
478 | if (!root_directory) |
479 | return; | |
480 | ||
481 | /* Drops all mounts that are outside of the root directory. */ | |
482 | ||
1d54cd5d | 483 | for (f = m, t = m; f < m + *n; f++) { |
cd2902c9 | 484 | |
34de407a LP |
485 | if (!path_startswith(mount_entry_path(f), root_directory)) { |
486 | log_debug("%s is outside of root directory.", mount_entry_path(f)); | |
1eb7e08e | 487 | mount_entry_done(f); |
cd2902c9 LP |
488 | continue; |
489 | } | |
490 | ||
491 | *t = *f; | |
492 | t++; | |
493 | } | |
494 | ||
495 | *n = t - m; | |
496 | } | |
497 | ||
5d997827 | 498 | static int mount_private_dev(MountEntry *m) { |
7f112f50 LP |
499 | static const char devnodes[] = |
500 | "/dev/null\0" | |
501 | "/dev/zero\0" | |
502 | "/dev/full\0" | |
503 | "/dev/random\0" | |
504 | "/dev/urandom\0" | |
505 | "/dev/tty\0"; | |
506 | ||
2b85f4e1 | 507 | char temporary_mount[] = "/tmp/namespace-dev-XXXXXX"; |
63cc4c31 | 508 | const char *d, *dev = NULL, *devpts = NULL, *devshm = NULL, *devhugepages = NULL, *devmqueue = NULL, *devlog = NULL, *devptmx = NULL; |
7f112f50 LP |
509 | _cleanup_umask_ mode_t u; |
510 | int r; | |
511 | ||
512 | assert(m); | |
513 | ||
514 | u = umask(0000); | |
515 | ||
2b85f4e1 LP |
516 | if (!mkdtemp(temporary_mount)) |
517 | return -errno; | |
518 | ||
63c372cb | 519 | dev = strjoina(temporary_mount, "/dev"); |
dc751688 | 520 | (void) mkdir(dev, 0755); |
737ba3c8 | 521 | if (mount("tmpfs", dev, "tmpfs", DEV_MOUNT_OPTIONS, "mode=755") < 0) { |
2b85f4e1 LP |
522 | r = -errno; |
523 | goto fail; | |
524 | } | |
525 | ||
63c372cb | 526 | devpts = strjoina(temporary_mount, "/dev/pts"); |
dc751688 | 527 | (void) mkdir(devpts, 0755); |
2b85f4e1 LP |
528 | if (mount("/dev/pts", devpts, NULL, MS_BIND, NULL) < 0) { |
529 | r = -errno; | |
530 | goto fail; | |
531 | } | |
532 | ||
63c372cb | 533 | devptmx = strjoina(temporary_mount, "/dev/ptmx"); |
3164e3cb ZJS |
534 | if (symlink("pts/ptmx", devptmx) < 0) { |
535 | r = -errno; | |
536 | goto fail; | |
537 | } | |
e06b6479 | 538 | |
63c372cb | 539 | devshm = strjoina(temporary_mount, "/dev/shm"); |
dc751688 | 540 | (void) mkdir(devshm, 01777); |
2b85f4e1 LP |
541 | r = mount("/dev/shm", devshm, NULL, MS_BIND, NULL); |
542 | if (r < 0) { | |
543 | r = -errno; | |
544 | goto fail; | |
545 | } | |
546 | ||
63c372cb | 547 | devmqueue = strjoina(temporary_mount, "/dev/mqueue"); |
dc751688 | 548 | (void) mkdir(devmqueue, 0755); |
3164e3cb | 549 | (void) mount("/dev/mqueue", devmqueue, NULL, MS_BIND, NULL); |
2b85f4e1 | 550 | |
63c372cb | 551 | devhugepages = strjoina(temporary_mount, "/dev/hugepages"); |
dc751688 | 552 | (void) mkdir(devhugepages, 0755); |
3164e3cb | 553 | (void) mount("/dev/hugepages", devhugepages, NULL, MS_BIND, NULL); |
2b85f4e1 | 554 | |
63c372cb | 555 | devlog = strjoina(temporary_mount, "/dev/log"); |
3164e3cb | 556 | (void) symlink("/run/systemd/journal/dev-log", devlog); |
82d25240 | 557 | |
7f112f50 | 558 | NULSTR_FOREACH(d, devnodes) { |
2b85f4e1 LP |
559 | _cleanup_free_ char *dn = NULL; |
560 | struct stat st; | |
561 | ||
562 | r = stat(d, &st); | |
7f112f50 | 563 | if (r < 0) { |
2b85f4e1 LP |
564 | |
565 | if (errno == ENOENT) | |
566 | continue; | |
567 | ||
568 | r = -errno; | |
569 | goto fail; | |
7f112f50 LP |
570 | } |
571 | ||
2b85f4e1 LP |
572 | if (!S_ISBLK(st.st_mode) && |
573 | !S_ISCHR(st.st_mode)) { | |
574 | r = -EINVAL; | |
575 | goto fail; | |
576 | } | |
577 | ||
578 | if (st.st_rdev == 0) | |
579 | continue; | |
580 | ||
581 | dn = strappend(temporary_mount, d); | |
582 | if (!dn) { | |
583 | r = -ENOMEM; | |
584 | goto fail; | |
585 | } | |
586 | ||
ecabcf8b | 587 | mac_selinux_create_file_prepare(d, st.st_mode); |
2b85f4e1 | 588 | r = mknod(dn, st.st_mode, st.st_rdev); |
ecabcf8b | 589 | mac_selinux_create_file_clear(); |
dd078a1e | 590 | |
2b85f4e1 LP |
591 | if (r < 0) { |
592 | r = -errno; | |
593 | goto fail; | |
594 | } | |
7f112f50 LP |
595 | } |
596 | ||
03cfe0d5 | 597 | dev_setup(temporary_mount, UID_INVALID, GID_INVALID); |
7f112f50 | 598 | |
ee818b89 AC |
599 | /* Create the /dev directory if missing. It is more likely to be |
600 | * missing when the service is started with RootDirectory. This is | |
601 | * consistent with mount units creating the mount points when missing. | |
602 | */ | |
34de407a | 603 | (void) mkdir_p_label(mount_entry_path(m), 0755); |
ee818b89 | 604 | |
9e5f8252 | 605 | /* Unmount everything in old /dev */ |
34de407a LP |
606 | umount_recursive(mount_entry_path(m), 0); |
607 | if (mount(dev, mount_entry_path(m), NULL, MS_MOVE, NULL) < 0) { | |
2b85f4e1 LP |
608 | r = -errno; |
609 | goto fail; | |
610 | } | |
7f112f50 | 611 | |
2b85f4e1 LP |
612 | rmdir(dev); |
613 | rmdir(temporary_mount); | |
7f112f50 | 614 | |
2b85f4e1 | 615 | return 0; |
7f112f50 | 616 | |
2b85f4e1 LP |
617 | fail: |
618 | if (devpts) | |
619 | umount(devpts); | |
7f112f50 | 620 | |
2b85f4e1 LP |
621 | if (devshm) |
622 | umount(devshm); | |
7f112f50 | 623 | |
2b85f4e1 LP |
624 | if (devhugepages) |
625 | umount(devhugepages); | |
7f112f50 | 626 | |
2b85f4e1 LP |
627 | if (devmqueue) |
628 | umount(devmqueue); | |
7f112f50 | 629 | |
d267c5aa ZJS |
630 | umount(dev); |
631 | rmdir(dev); | |
2b85f4e1 | 632 | rmdir(temporary_mount); |
7f112f50 | 633 | |
2b85f4e1 | 634 | return r; |
7f112f50 LP |
635 | } |
636 | ||
5d997827 LP |
637 | static int mount_bind_dev(MountEntry *m) { |
638 | int r; | |
639 | ||
640 | assert(m); | |
641 | ||
642 | /* Implements the little brother of mount_private_dev(): simply bind mounts the host's /dev into the service's | |
643 | * /dev. This is only used when RootDirectory= is set. */ | |
644 | ||
645767d6 LP |
645 | (void) mkdir_p_label(mount_entry_path(m), 0755); |
646 | ||
5d997827 LP |
647 | r = path_is_mount_point(mount_entry_path(m), NULL, 0); |
648 | if (r < 0) | |
649 | return log_debug_errno(r, "Unable to determine whether /dev is already mounted: %m"); | |
650 | if (r > 0) /* make this a NOP if /dev is already a mount point */ | |
651 | return 0; | |
652 | ||
653 | if (mount("/dev", mount_entry_path(m), NULL, MS_BIND|MS_REC, NULL) < 0) | |
654 | return log_debug_errno(errno, "Failed to bind mount %s: %m", mount_entry_path(m)); | |
655 | ||
656 | return 1; | |
657 | } | |
658 | ||
659 | static int mount_sysfs(MountEntry *m) { | |
660 | int r; | |
661 | ||
662 | assert(m); | |
663 | ||
645767d6 LP |
664 | (void) mkdir_p_label(mount_entry_path(m), 0755); |
665 | ||
5d997827 LP |
666 | r = path_is_mount_point(mount_entry_path(m), NULL, 0); |
667 | if (r < 0) | |
668 | return log_debug_errno(r, "Unable to determine whether /sys is already mounted: %m"); | |
669 | if (r > 0) /* make this a NOP if /sys is already a mount point */ | |
670 | return 0; | |
671 | ||
672 | /* Bind mount the host's version so that we get all child mounts of it, too. */ | |
673 | if (mount("/sys", mount_entry_path(m), NULL, MS_BIND|MS_REC, NULL) < 0) | |
674 | return log_debug_errno(errno, "Failed to mount %s: %m", mount_entry_path(m)); | |
675 | ||
676 | return 1; | |
677 | } | |
678 | ||
679 | static int mount_procfs(MountEntry *m) { | |
680 | int r; | |
681 | ||
682 | assert(m); | |
683 | ||
645767d6 LP |
684 | (void) mkdir_p_label(mount_entry_path(m), 0755); |
685 | ||
5d997827 LP |
686 | r = path_is_mount_point(mount_entry_path(m), NULL, 0); |
687 | if (r < 0) | |
688 | return log_debug_errno(r, "Unable to determine whether /proc is already mounted: %m"); | |
689 | if (r > 0) /* make this a NOP if /proc is already a mount point */ | |
690 | return 0; | |
691 | ||
692 | /* Mount a new instance, so that we get the one that matches our user namespace, if we are running in one */ | |
693 | if (mount("proc", mount_entry_path(m), "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL) < 0) | |
694 | return log_debug_errno(errno, "Failed to mount %s: %m", mount_entry_path(m)); | |
695 | ||
696 | return 1; | |
697 | } | |
698 | ||
6c47cd7d LP |
699 | static int mount_empty_dir(MountEntry *m) { |
700 | assert(m); | |
701 | ||
702 | /* First, get rid of everything that is below if there is anything. Then, overmount with our new empty dir */ | |
703 | ||
704 | (void) mkdir_p_label(mount_entry_path(m), 0755); | |
705 | (void) umount_recursive(mount_entry_path(m), 0); | |
706 | ||
707 | if (mount("tmpfs", mount_entry_path(m), "tmpfs", MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, "mode=755") < 0) | |
708 | return log_debug_errno(errno, "Failed to mount %s: %m", mount_entry_path(m)); | |
709 | ||
710 | return 1; | |
711 | } | |
712 | ||
d2d6c096 LP |
713 | static int mount_entry_chase( |
714 | const char *root_directory, | |
715 | MountEntry *m, | |
716 | const char *path, | |
717 | char **location) { | |
718 | ||
8fceda93 LP |
719 | char *chased; |
720 | int r; | |
721 | ||
722 | assert(m); | |
723 | ||
724 | /* Since mount() will always follow symlinks and we need to take the different root directory into account we | |
d2d6c096 LP |
725 | * chase the symlinks on our own first. This is called for the destination path, as well as the source path (if |
726 | * that applies). The result is stored in "location". */ | |
8fceda93 | 727 | |
a227a4be LP |
728 | r = chase_symlinks(path, root_directory, |
729 | IN_SET(m->mode, BIND_MOUNT, BIND_MOUNT_RECURSIVE, PRIVATE_TMP, PRIVATE_VAR_TMP, PRIVATE_DEV, BIND_DEV, EMPTY_DIR, SYSFS, PROCFS) ? CHASE_NONEXISTENT : 0, | |
730 | &chased); | |
8fceda93 | 731 | if (r == -ENOENT && m->ignore) { |
d2d6c096 | 732 | log_debug_errno(r, "Path %s does not exist, ignoring.", path); |
8fceda93 LP |
733 | return 0; |
734 | } | |
735 | if (r < 0) | |
d2d6c096 | 736 | return log_debug_errno(r, "Failed to follow symlinks on %s: %m", path); |
8fceda93 | 737 | |
d2d6c096 | 738 | log_debug("Followed symlinks %s → %s.", path, chased); |
8fceda93 | 739 | |
d2d6c096 LP |
740 | free(*location); |
741 | *location = chased; | |
8fceda93 LP |
742 | |
743 | return 1; | |
744 | } | |
745 | ||
ac0930c8 | 746 | static int apply_mount( |
8fceda93 | 747 | const char *root_directory, |
34de407a | 748 | MountEntry *m, |
ac0930c8 | 749 | const char *tmp_dir, |
c17ec25e | 750 | const char *var_tmp_dir) { |
ac0930c8 | 751 | |
a227a4be | 752 | bool rbind = true, make = false; |
15ae422b | 753 | const char *what; |
15ae422b | 754 | int r; |
15ae422b | 755 | |
c17ec25e | 756 | assert(m); |
15ae422b | 757 | |
d2d6c096 | 758 | r = mount_entry_chase(root_directory, m, mount_entry_path(m), &m->path_malloc); |
8fceda93 LP |
759 | if (r <= 0) |
760 | return r; | |
761 | ||
34de407a | 762 | log_debug("Applying namespace mount on %s", mount_entry_path(m)); |
fe3c2583 | 763 | |
c17ec25e | 764 | switch (m->mode) { |
15ae422b | 765 | |
160cfdbe LP |
766 | case INACCESSIBLE: { |
767 | struct stat target; | |
6d313367 LP |
768 | |
769 | /* First, get rid of everything that is below if there | |
770 | * is anything... Then, overmount it with an | |
c4b41707 | 771 | * inaccessible path. */ |
34de407a | 772 | (void) umount_recursive(mount_entry_path(m), 0); |
6d313367 | 773 | |
34de407a LP |
774 | if (lstat(mount_entry_path(m), &target) < 0) |
775 | return log_debug_errno(errno, "Failed to lstat() %s to determine what to mount over it: %m", mount_entry_path(m)); | |
15ae422b | 776 | |
c4b41707 | 777 | what = mode_to_inaccessible_node(target.st_mode); |
5fd7cf6f LP |
778 | if (!what) { |
779 | log_debug("File type not supported for inaccessible mounts. Note that symlinks are not allowed"); | |
c4b41707 AP |
780 | return -ELOOP; |
781 | } | |
782 | break; | |
160cfdbe | 783 | } |
fe3c2583 | 784 | |
15ae422b | 785 | case READONLY: |
15ae422b | 786 | case READWRITE: |
8fceda93 | 787 | r = path_is_mount_point(mount_entry_path(m), root_directory, 0); |
d944dc95 | 788 | if (r < 0) |
34de407a | 789 | return log_debug_errno(r, "Failed to determine whether %s is already a mount point: %m", mount_entry_path(m)); |
6b7c9f8b LP |
790 | if (r > 0) /* Nothing to do here, it is already a mount. We just later toggle the MS_RDONLY bit for the mount point if needed. */ |
791 | return 0; | |
6b7c9f8b | 792 | /* This isn't a mount point yet, let's make it one. */ |
34de407a | 793 | what = mount_entry_path(m); |
6b7c9f8b | 794 | break; |
15ae422b | 795 | |
d2d6c096 LP |
796 | case BIND_MOUNT: |
797 | rbind = false; | |
798 | /* fallthrough */ | |
799 | ||
800 | case BIND_MOUNT_RECURSIVE: | |
801 | /* Also chase the source mount */ | |
5d997827 | 802 | |
d2d6c096 LP |
803 | r = mount_entry_chase(root_directory, m, mount_entry_source(m), &m->source_malloc); |
804 | if (r <= 0) | |
805 | return r; | |
806 | ||
807 | what = mount_entry_source(m); | |
a227a4be | 808 | make = true; |
d2d6c096 LP |
809 | break; |
810 | ||
6c47cd7d LP |
811 | case EMPTY_DIR: |
812 | return mount_empty_dir(m); | |
813 | ||
ac0930c8 LP |
814 | case PRIVATE_TMP: |
815 | what = tmp_dir; | |
a227a4be | 816 | make = true; |
ac0930c8 LP |
817 | break; |
818 | ||
819 | case PRIVATE_VAR_TMP: | |
820 | what = var_tmp_dir; | |
a227a4be | 821 | make = true; |
15ae422b | 822 | break; |
e364ad06 | 823 | |
d6797c92 | 824 | case PRIVATE_DEV: |
5d997827 LP |
825 | return mount_private_dev(m); |
826 | ||
827 | case BIND_DEV: | |
828 | return mount_bind_dev(m); | |
829 | ||
830 | case SYSFS: | |
831 | return mount_sysfs(m); | |
832 | ||
833 | case PROCFS: | |
834 | return mount_procfs(m); | |
d6797c92 | 835 | |
e364ad06 LP |
836 | default: |
837 | assert_not_reached("Unknown mode"); | |
15ae422b LP |
838 | } |
839 | ||
ac0930c8 | 840 | assert(what); |
15ae422b | 841 | |
a227a4be LP |
842 | if (mount(what, mount_entry_path(m), NULL, MS_BIND|(rbind ? MS_REC : 0), NULL) < 0) { |
843 | bool try_again = false; | |
844 | r = -errno; | |
845 | ||
846 | if (r == -ENOENT && make) { | |
847 | struct stat st; | |
848 | ||
849 | /* Hmm, either the source or the destination are missing. Let's see if we can create the destination, then try again */ | |
850 | ||
851 | if (stat(what, &st) >= 0) { | |
852 | ||
853 | (void) mkdir_parents(mount_entry_path(m), 0755); | |
854 | ||
855 | if (S_ISDIR(st.st_mode)) | |
856 | try_again = mkdir(mount_entry_path(m), 0755) >= 0; | |
857 | else | |
858 | try_again = touch(mount_entry_path(m)) >= 0; | |
859 | } | |
860 | } | |
861 | ||
862 | if (try_again) { | |
863 | if (mount(what, mount_entry_path(m), NULL, MS_BIND|(rbind ? MS_REC : 0), NULL) < 0) | |
864 | r = -errno; | |
865 | else | |
866 | r = 0; | |
867 | } | |
868 | ||
869 | if (r < 0) | |
870 | return log_debug_errno(r, "Failed to mount %s to %s: %m", what, mount_entry_path(m)); | |
871 | } | |
6b7c9f8b | 872 | |
34de407a | 873 | log_debug("Successfully mounted %s to %s", what, mount_entry_path(m)); |
6b7c9f8b | 874 | return 0; |
ac0930c8 | 875 | } |
15ae422b | 876 | |
ac9de0b3 | 877 | static int make_read_only(MountEntry *m, char **blacklist, FILE *proc_self_mountinfo) { |
6b7c9f8b | 878 | int r = 0; |
15ae422b | 879 | |
c17ec25e | 880 | assert(m); |
ac9de0b3 | 881 | assert(proc_self_mountinfo); |
ac0930c8 | 882 | |
34de407a | 883 | if (mount_entry_read_only(m)) |
ac9de0b3 | 884 | r = bind_remount_recursive_with_mountinfo(mount_entry_path(m), true, blacklist, proc_self_mountinfo); |
13e785f7 | 885 | else if (m->mode == PRIVATE_DEV) { /* Superblock can be readonly but the submounts can't */ |
34de407a | 886 | if (mount(NULL, mount_entry_path(m), NULL, MS_REMOUNT|DEV_MOUNT_OPTIONS|MS_RDONLY, NULL) < 0) |
6b7c9f8b | 887 | r = -errno; |
737ba3c8 | 888 | } else |
6b7c9f8b LP |
889 | return 0; |
890 | ||
891 | /* Not that we only turn on the MS_RDONLY flag here, we never turn it off. Something that was marked read-only | |
892 | * already stays this way. This improves compatibility with container managers, where we won't attempt to undo | |
893 | * read-only mounts already applied. */ | |
ac0930c8 | 894 | |
8fceda93 LP |
895 | if (r == -ENOENT && m->ignore) |
896 | r = 0; | |
5327c910 | 897 | |
1d54cd5d | 898 | return r; |
d944dc95 LP |
899 | } |
900 | ||
9c988f93 | 901 | static bool namespace_info_mount_apivfs(const char *root_directory, const NameSpaceInfo *ns_info) { |
5d997827 LP |
902 | assert(ns_info); |
903 | ||
9c988f93 DH |
904 | /* |
905 | * ProtectControlGroups= and ProtectKernelTunables= imply MountAPIVFS=, | |
906 | * since to protect the API VFS mounts, they need to be around in the | |
907 | * first place... and RootDirectory= or RootImage= need to be set. | |
908 | */ | |
5d997827 | 909 | |
9c988f93 DH |
910 | /* root_directory should point to a mount point */ |
911 | return root_directory && | |
912 | (ns_info->mount_apivfs || | |
913 | ns_info->protect_control_groups || | |
914 | ns_info->protect_kernel_tunables); | |
5d997827 LP |
915 | } |
916 | ||
2652c6c1 | 917 | static unsigned namespace_calculate_mounts( |
9c988f93 | 918 | const char* root_directory, |
c575770b | 919 | const NameSpaceInfo *ns_info, |
2652c6c1 DH |
920 | char** read_write_paths, |
921 | char** read_only_paths, | |
922 | char** inaccessible_paths, | |
6c47cd7d | 923 | char** empty_directories, |
d2d6c096 LP |
924 | const BindMount *bind_mounts, |
925 | unsigned n_bind_mounts, | |
2652c6c1 DH |
926 | const char* tmp_dir, |
927 | const char* var_tmp_dir, | |
2652c6c1 DH |
928 | ProtectHome protect_home, |
929 | ProtectSystem protect_system) { | |
930 | ||
b6c432ca | 931 | unsigned protect_home_cnt; |
f471b2af DH |
932 | unsigned protect_system_cnt = |
933 | (protect_system == PROTECT_SYSTEM_STRICT ? | |
934 | ELEMENTSOF(protect_system_strict_table) : | |
935 | ((protect_system == PROTECT_SYSTEM_FULL) ? | |
936 | ELEMENTSOF(protect_system_full_table) : | |
937 | ((protect_system == PROTECT_SYSTEM_YES) ? | |
938 | ELEMENTSOF(protect_system_yes_table) : 0))); | |
939 | ||
b6c432ca DH |
940 | protect_home_cnt = |
941 | (protect_home == PROTECT_HOME_YES ? | |
942 | ELEMENTSOF(protect_home_yes_table) : | |
943 | ((protect_home == PROTECT_HOME_READ_ONLY) ? | |
944 | ELEMENTSOF(protect_home_read_only_table) : 0)); | |
945 | ||
2652c6c1 DH |
946 | return !!tmp_dir + !!var_tmp_dir + |
947 | strv_length(read_write_paths) + | |
948 | strv_length(read_only_paths) + | |
949 | strv_length(inaccessible_paths) + | |
6c47cd7d | 950 | strv_length(empty_directories) + |
d2d6c096 | 951 | n_bind_mounts + |
c575770b DH |
952 | ns_info->private_dev + |
953 | (ns_info->protect_kernel_tunables ? ELEMENTSOF(protect_kernel_tunables_table) : 0) + | |
954 | (ns_info->protect_control_groups ? 1 : 0) + | |
955 | (ns_info->protect_kernel_modules ? ELEMENTSOF(protect_kernel_modules_table) : 0) + | |
5d997827 | 956 | protect_home_cnt + protect_system_cnt + |
9c988f93 | 957 | (namespace_info_mount_apivfs(root_directory, ns_info) ? ELEMENTSOF(apivfs_table) : 0); |
2652c6c1 DH |
958 | } |
959 | ||
613b411c | 960 | int setup_namespace( |
ee818b89 | 961 | const char* root_directory, |
915e6d16 | 962 | const char* root_image, |
c575770b | 963 | const NameSpaceInfo *ns_info, |
2a624c36 AP |
964 | char** read_write_paths, |
965 | char** read_only_paths, | |
966 | char** inaccessible_paths, | |
6c47cd7d | 967 | char** empty_directories, |
d2d6c096 LP |
968 | const BindMount *bind_mounts, |
969 | unsigned n_bind_mounts, | |
a004cb4c LP |
970 | const char* tmp_dir, |
971 | const char* var_tmp_dir, | |
1b8689f9 LP |
972 | ProtectHome protect_home, |
973 | ProtectSystem protect_system, | |
915e6d16 LP |
974 | unsigned long mount_flags, |
975 | DissectImageFlags dissect_image_flags) { | |
15ae422b | 976 | |
915e6d16 | 977 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
78ebe980 | 978 | _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL; |
915e6d16 | 979 | _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL; |
78ebe980 | 980 | _cleanup_free_ void *root_hash = NULL; |
34de407a | 981 | MountEntry *m, *mounts = NULL; |
78ebe980 | 982 | size_t root_hash_size = 0; |
d944dc95 | 983 | bool make_slave = false; |
e908468b | 984 | const char *root; |
f0a4feb0 | 985 | unsigned n_mounts; |
c17ec25e | 986 | int r = 0; |
15ae422b | 987 | |
915e6d16 LP |
988 | assert(ns_info); |
989 | ||
613b411c | 990 | if (mount_flags == 0) |
c17ec25e | 991 | mount_flags = MS_SHARED; |
ac0930c8 | 992 | |
915e6d16 LP |
993 | if (root_image) { |
994 | dissect_image_flags |= DISSECT_IMAGE_REQUIRE_ROOT; | |
995 | ||
996 | if (protect_system == PROTECT_SYSTEM_STRICT && strv_isempty(read_write_paths)) | |
997 | dissect_image_flags |= DISSECT_IMAGE_READ_ONLY; | |
998 | ||
999 | r = loop_device_make_by_path(root_image, | |
1000 | dissect_image_flags & DISSECT_IMAGE_READ_ONLY ? O_RDONLY : O_RDWR, | |
1001 | &loop_device); | |
1002 | if (r < 0) | |
1003 | return r; | |
1004 | ||
78ebe980 LP |
1005 | r = root_hash_load(root_image, &root_hash, &root_hash_size); |
1006 | if (r < 0) | |
1007 | return r; | |
1008 | ||
1009 | r = dissect_image(loop_device->fd, root_hash, root_hash_size, dissect_image_flags, &dissected_image); | |
1010 | if (r < 0) | |
1011 | return r; | |
1012 | ||
1013 | r = dissected_image_decrypt(dissected_image, NULL, root_hash, root_hash_size, dissect_image_flags, &decrypted_image); | |
915e6d16 LP |
1014 | if (r < 0) |
1015 | return r; | |
915e6d16 LP |
1016 | } |
1017 | ||
e908468b LP |
1018 | if (root_directory) |
1019 | root = root_directory; | |
1020 | else if (root_image || n_bind_mounts > 0) { | |
1021 | ||
1022 | /* If we are booting from an image, create a mount point for the image, if it's still missing. We use | |
1023 | * the same mount point for all images, which is safe, since they all live in their own namespaces | |
1024 | * after all, and hence won't see each other. We also use such a root directory whenever there are bind | |
1025 | * mounts configured, so that their source mounts are never obstructed by mounts we already applied | |
1026 | * while we are applying them. */ | |
1027 | ||
1028 | root = "/run/systemd/unit-root"; | |
1029 | (void) mkdir_label(root, 0700); | |
1030 | } else | |
1031 | root = NULL; | |
1032 | ||
cfbeb4ef | 1033 | n_mounts = namespace_calculate_mounts( |
e908468b | 1034 | root, |
cfbeb4ef LP |
1035 | ns_info, |
1036 | read_write_paths, | |
1037 | read_only_paths, | |
1038 | inaccessible_paths, | |
6c47cd7d | 1039 | empty_directories, |
d2d6c096 | 1040 | bind_mounts, n_bind_mounts, |
cfbeb4ef LP |
1041 | tmp_dir, var_tmp_dir, |
1042 | protect_home, protect_system); | |
613b411c | 1043 | |
2652c6c1 | 1044 | /* Set mount slave mode */ |
e908468b | 1045 | if (root || n_mounts > 0) |
d944dc95 LP |
1046 | make_slave = true; |
1047 | ||
f0a4feb0 | 1048 | if (n_mounts > 0) { |
34de407a | 1049 | m = mounts = (MountEntry *) alloca0(n_mounts * sizeof(MountEntry)); |
5327c910 | 1050 | r = append_access_mounts(&m, read_write_paths, READWRITE); |
613b411c | 1051 | if (r < 0) |
f0a4feb0 | 1052 | goto finish; |
613b411c | 1053 | |
5327c910 | 1054 | r = append_access_mounts(&m, read_only_paths, READONLY); |
613b411c | 1055 | if (r < 0) |
f0a4feb0 | 1056 | goto finish; |
613b411c | 1057 | |
5327c910 | 1058 | r = append_access_mounts(&m, inaccessible_paths, INACCESSIBLE); |
613b411c | 1059 | if (r < 0) |
f0a4feb0 | 1060 | goto finish; |
7ff7394d | 1061 | |
6c47cd7d LP |
1062 | r = append_empty_dir_mounts(&m, empty_directories); |
1063 | if (r < 0) | |
1064 | goto finish; | |
1065 | ||
d2d6c096 LP |
1066 | r = append_bind_mounts(&m, bind_mounts, n_bind_mounts); |
1067 | if (r < 0) | |
1068 | goto finish; | |
1069 | ||
613b411c | 1070 | if (tmp_dir) { |
34de407a | 1071 | *(m++) = (MountEntry) { |
5327c910 LP |
1072 | .path_const = "/tmp", |
1073 | .mode = PRIVATE_TMP, | |
1074 | }; | |
613b411c | 1075 | } |
7ff7394d | 1076 | |
613b411c | 1077 | if (var_tmp_dir) { |
34de407a | 1078 | *(m++) = (MountEntry) { |
5327c910 LP |
1079 | .path_const = "/var/tmp", |
1080 | .mode = PRIVATE_VAR_TMP, | |
1081 | }; | |
7ff7394d | 1082 | } |
ac0930c8 | 1083 | |
c575770b | 1084 | if (ns_info->private_dev) { |
34de407a | 1085 | *(m++) = (MountEntry) { |
5327c910 LP |
1086 | .path_const = "/dev", |
1087 | .mode = PRIVATE_DEV, | |
1088 | }; | |
7f112f50 LP |
1089 | } |
1090 | ||
c575770b | 1091 | if (ns_info->protect_kernel_tunables) { |
5327c910 | 1092 | r = append_static_mounts(&m, protect_kernel_tunables_table, ELEMENTSOF(protect_kernel_tunables_table), ns_info->ignore_protect_paths); |
c575770b | 1093 | if (r < 0) |
f0a4feb0 | 1094 | goto finish; |
c575770b DH |
1095 | } |
1096 | ||
1097 | if (ns_info->protect_kernel_modules) { | |
5327c910 | 1098 | r = append_static_mounts(&m, protect_kernel_modules_table, ELEMENTSOF(protect_kernel_modules_table), ns_info->ignore_protect_paths); |
c575770b | 1099 | if (r < 0) |
f0a4feb0 | 1100 | goto finish; |
c575770b | 1101 | } |
59eeb84b | 1102 | |
c575770b | 1103 | if (ns_info->protect_control_groups) { |
34de407a | 1104 | *(m++) = (MountEntry) { |
5327c910 LP |
1105 | .path_const = "/sys/fs/cgroup", |
1106 | .mode = READONLY, | |
1107 | }; | |
59eeb84b LP |
1108 | } |
1109 | ||
5327c910 | 1110 | r = append_protect_home(&m, protect_home, ns_info->ignore_protect_paths); |
b6c432ca | 1111 | if (r < 0) |
f0a4feb0 | 1112 | goto finish; |
417116f2 | 1113 | |
5327c910 | 1114 | r = append_protect_system(&m, protect_system, false); |
f471b2af | 1115 | if (r < 0) |
f0a4feb0 | 1116 | goto finish; |
417116f2 | 1117 | |
e908468b | 1118 | if (namespace_info_mount_apivfs(root, ns_info)) { |
5d997827 LP |
1119 | r = append_static_mounts(&m, apivfs_table, ELEMENTSOF(apivfs_table), ns_info->ignore_protect_paths); |
1120 | if (r < 0) | |
1121 | goto finish; | |
1122 | } | |
1123 | ||
f0a4feb0 | 1124 | assert(mounts + n_mounts == m); |
ac0930c8 | 1125 | |
5327c910 | 1126 | /* Prepend the root directory where that's necessary */ |
e908468b | 1127 | r = prefix_where_needed(mounts, n_mounts, root); |
5327c910 LP |
1128 | if (r < 0) |
1129 | goto finish; | |
1130 | ||
34de407a | 1131 | qsort(mounts, n_mounts, sizeof(MountEntry), mount_path_compare); |
fe3c2583 | 1132 | |
f0a4feb0 | 1133 | drop_duplicates(mounts, &n_mounts); |
e908468b | 1134 | drop_outside_root(root, mounts, &n_mounts); |
f0a4feb0 DH |
1135 | drop_inaccessible(mounts, &n_mounts); |
1136 | drop_nop(mounts, &n_mounts); | |
15ae422b LP |
1137 | } |
1138 | ||
d944dc95 LP |
1139 | if (unshare(CLONE_NEWNS) < 0) { |
1140 | r = -errno; | |
1141 | goto finish; | |
1142 | } | |
1e4e94c8 | 1143 | |
d944dc95 | 1144 | if (make_slave) { |
c2c13f2d LP |
1145 | /* Remount / as SLAVE so that nothing now mounted in the namespace |
1146 | shows up in the parent */ | |
d944dc95 LP |
1147 | if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) { |
1148 | r = -errno; | |
1149 | goto finish; | |
1150 | } | |
ee818b89 AC |
1151 | } |
1152 | ||
10404d52 | 1153 | /* Try to set up the new root directory before mounting anything there */ |
e908468b LP |
1154 | if (root) |
1155 | (void) base_filesystem_create(root, UID_INVALID, GID_INVALID); | |
10404d52 | 1156 | |
915e6d16 | 1157 | if (root_image) { |
e908468b LP |
1158 | /* A root image is specified, mount it to the right place */ |
1159 | r = dissected_image_mount(dissected_image, root, dissect_image_flags); | |
915e6d16 LP |
1160 | if (r < 0) |
1161 | goto finish; | |
1162 | ||
07ce7407 TM |
1163 | if (decrypted_image) { |
1164 | r = decrypted_image_relinquish(decrypted_image); | |
1165 | if (r < 0) | |
1166 | goto finish; | |
1167 | } | |
78ebe980 | 1168 | |
915e6d16 LP |
1169 | loop_device_relinquish(loop_device); |
1170 | ||
1171 | } else if (root_directory) { | |
1172 | ||
e908468b LP |
1173 | /* A root directory is specified. Turn its directory into bind mount, if it isn't one yet. */ |
1174 | r = path_is_mount_point(root, NULL, AT_SYMLINK_FOLLOW); | |
8f1ad200 | 1175 | if (r < 0) |
d944dc95 | 1176 | goto finish; |
8f1ad200 | 1177 | if (r == 0) { |
e908468b | 1178 | if (mount(root, root, NULL, MS_BIND|MS_REC, NULL) < 0) { |
8f1ad200 LP |
1179 | r = -errno; |
1180 | goto finish; | |
1181 | } | |
d944dc95 | 1182 | } |
e908468b LP |
1183 | |
1184 | } else if (root) { | |
1185 | ||
1186 | /* Let's mount the main root directory to the root directory to use */ | |
1187 | if (mount("/", root, NULL, MS_BIND|MS_REC, NULL) < 0) { | |
1188 | r = -errno; | |
1189 | goto finish; | |
1190 | } | |
ee818b89 | 1191 | } |
c2c13f2d | 1192 | |
f0a4feb0 | 1193 | if (n_mounts > 0) { |
ac9de0b3 | 1194 | _cleanup_fclose_ FILE *proc_self_mountinfo = NULL; |
6b7c9f8b LP |
1195 | char **blacklist; |
1196 | unsigned j; | |
1197 | ||
ac9de0b3 TR |
1198 | /* Open /proc/self/mountinfo now as it may become unavailable if we mount anything on top of /proc. |
1199 | * For example, this is the case with the option: 'InaccessiblePaths=/proc' */ | |
1200 | proc_self_mountinfo = fopen("/proc/self/mountinfo", "re"); | |
1201 | if (!proc_self_mountinfo) { | |
1202 | r = -errno; | |
1203 | goto finish; | |
1204 | } | |
1205 | ||
6b7c9f8b | 1206 | /* First round, add in all special mounts we need */ |
f0a4feb0 | 1207 | for (m = mounts; m < mounts + n_mounts; ++m) { |
e908468b | 1208 | r = apply_mount(root, m, tmp_dir, var_tmp_dir); |
c2c13f2d | 1209 | if (r < 0) |
d944dc95 | 1210 | goto finish; |
c2c13f2d | 1211 | } |
15ae422b | 1212 | |
6b7c9f8b | 1213 | /* Create a blacklist we can pass to bind_mount_recursive() */ |
f0a4feb0 DH |
1214 | blacklist = newa(char*, n_mounts+1); |
1215 | for (j = 0; j < n_mounts; j++) | |
34de407a | 1216 | blacklist[j] = (char*) mount_entry_path(mounts+j); |
6b7c9f8b LP |
1217 | blacklist[j] = NULL; |
1218 | ||
1219 | /* Second round, flip the ro bits if necessary. */ | |
f0a4feb0 | 1220 | for (m = mounts; m < mounts + n_mounts; ++m) { |
ac9de0b3 | 1221 | r = make_read_only(m, blacklist, proc_self_mountinfo); |
c2c13f2d | 1222 | if (r < 0) |
d944dc95 | 1223 | goto finish; |
c2c13f2d | 1224 | } |
15ae422b LP |
1225 | } |
1226 | ||
e908468b | 1227 | if (root) { |
ee818b89 | 1228 | /* MS_MOVE does not work on MS_SHARED so the remount MS_SHARED will be done later */ |
e908468b | 1229 | r = mount_move_root(root); |
d944dc95 LP |
1230 | if (r < 0) |
1231 | goto finish; | |
ee818b89 AC |
1232 | } |
1233 | ||
c2c13f2d LP |
1234 | /* Remount / as the desired mode. Not that this will not |
1235 | * reestablish propagation from our side to the host, since | |
1236 | * what's disconnected is disconnected. */ | |
d944dc95 LP |
1237 | if (mount(NULL, "/", NULL, mount_flags | MS_REC, NULL) < 0) { |
1238 | r = -errno; | |
1239 | goto finish; | |
1240 | } | |
15ae422b | 1241 | |
d944dc95 | 1242 | r = 0; |
15ae422b | 1243 | |
d944dc95 | 1244 | finish: |
f0a4feb0 | 1245 | for (m = mounts; m < mounts + n_mounts; m++) |
1eb7e08e | 1246 | mount_entry_done(m); |
613b411c LP |
1247 | |
1248 | return r; | |
1249 | } | |
1250 | ||
d2d6c096 LP |
1251 | void bind_mount_free_many(BindMount *b, unsigned n) { |
1252 | unsigned i; | |
1253 | ||
1254 | assert(b || n == 0); | |
1255 | ||
1256 | for (i = 0; i < n; i++) { | |
1257 | free(b[i].source); | |
1258 | free(b[i].destination); | |
1259 | } | |
1260 | ||
1261 | free(b); | |
1262 | } | |
1263 | ||
1264 | int bind_mount_add(BindMount **b, unsigned *n, const BindMount *item) { | |
1265 | _cleanup_free_ char *s = NULL, *d = NULL; | |
1266 | BindMount *c; | |
1267 | ||
1268 | assert(b); | |
1269 | assert(n); | |
1270 | assert(item); | |
1271 | ||
1272 | s = strdup(item->source); | |
1273 | if (!s) | |
1274 | return -ENOMEM; | |
1275 | ||
1276 | d = strdup(item->destination); | |
1277 | if (!d) | |
1278 | return -ENOMEM; | |
1279 | ||
1280 | c = realloc_multiply(*b, sizeof(BindMount), *n + 1); | |
1281 | if (!c) | |
1282 | return -ENOMEM; | |
1283 | ||
1284 | *b = c; | |
1285 | ||
1286 | c[(*n) ++] = (BindMount) { | |
1287 | .source = s, | |
1288 | .destination = d, | |
1289 | .read_only = item->read_only, | |
1290 | .recursive = item->recursive, | |
1291 | .ignore_enoent = item->ignore_enoent, | |
1292 | }; | |
1293 | ||
1294 | s = d = NULL; | |
1295 | return 0; | |
1296 | } | |
1297 | ||
613b411c LP |
1298 | static int setup_one_tmp_dir(const char *id, const char *prefix, char **path) { |
1299 | _cleanup_free_ char *x = NULL; | |
6b46ea73 LP |
1300 | char bid[SD_ID128_STRING_MAX]; |
1301 | sd_id128_t boot_id; | |
1302 | int r; | |
613b411c LP |
1303 | |
1304 | assert(id); | |
1305 | assert(prefix); | |
1306 | assert(path); | |
1307 | ||
6b46ea73 LP |
1308 | /* We include the boot id in the directory so that after a |
1309 | * reboot we can easily identify obsolete directories. */ | |
1310 | ||
1311 | r = sd_id128_get_boot(&boot_id); | |
1312 | if (r < 0) | |
1313 | return r; | |
1314 | ||
605405c6 | 1315 | x = strjoin(prefix, "/systemd-private-", sd_id128_to_string(boot_id, bid), "-", id, "-XXXXXX"); |
613b411c LP |
1316 | if (!x) |
1317 | return -ENOMEM; | |
1318 | ||
1319 | RUN_WITH_UMASK(0077) | |
1320 | if (!mkdtemp(x)) | |
1321 | return -errno; | |
1322 | ||
1323 | RUN_WITH_UMASK(0000) { | |
1324 | char *y; | |
1325 | ||
63c372cb | 1326 | y = strjoina(x, "/tmp"); |
613b411c LP |
1327 | |
1328 | if (mkdir(y, 0777 | S_ISVTX) < 0) | |
1329 | return -errno; | |
c17ec25e | 1330 | } |
15ae422b | 1331 | |
613b411c LP |
1332 | *path = x; |
1333 | x = NULL; | |
1334 | ||
1335 | return 0; | |
1336 | } | |
1337 | ||
1338 | int setup_tmp_dirs(const char *id, char **tmp_dir, char **var_tmp_dir) { | |
1339 | char *a, *b; | |
1340 | int r; | |
1341 | ||
1342 | assert(id); | |
1343 | assert(tmp_dir); | |
1344 | assert(var_tmp_dir); | |
1345 | ||
1346 | r = setup_one_tmp_dir(id, "/tmp", &a); | |
1347 | if (r < 0) | |
1348 | return r; | |
1349 | ||
1350 | r = setup_one_tmp_dir(id, "/var/tmp", &b); | |
1351 | if (r < 0) { | |
1352 | char *t; | |
1353 | ||
63c372cb | 1354 | t = strjoina(a, "/tmp"); |
613b411c LP |
1355 | rmdir(t); |
1356 | rmdir(a); | |
1357 | ||
1358 | free(a); | |
1359 | return r; | |
1360 | } | |
1361 | ||
1362 | *tmp_dir = a; | |
1363 | *var_tmp_dir = b; | |
1364 | ||
1365 | return 0; | |
1366 | } | |
1367 | ||
1368 | int setup_netns(int netns_storage_socket[2]) { | |
1369 | _cleanup_close_ int netns = -1; | |
3ee897d6 | 1370 | int r, q; |
613b411c LP |
1371 | |
1372 | assert(netns_storage_socket); | |
1373 | assert(netns_storage_socket[0] >= 0); | |
1374 | assert(netns_storage_socket[1] >= 0); | |
1375 | ||
1376 | /* We use the passed socketpair as a storage buffer for our | |
76cd584b LP |
1377 | * namespace reference fd. Whatever process runs this first |
1378 | * shall create a new namespace, all others should just join | |
1379 | * it. To serialize that we use a file lock on the socket | |
1380 | * pair. | |
613b411c LP |
1381 | * |
1382 | * It's a bit crazy, but hey, works great! */ | |
1383 | ||
1384 | if (lockf(netns_storage_socket[0], F_LOCK, 0) < 0) | |
1385 | return -errno; | |
1386 | ||
3ee897d6 LP |
1387 | netns = receive_one_fd(netns_storage_socket[0], MSG_DONTWAIT); |
1388 | if (netns == -EAGAIN) { | |
613b411c LP |
1389 | /* Nothing stored yet, so let's create a new namespace */ |
1390 | ||
1391 | if (unshare(CLONE_NEWNET) < 0) { | |
1392 | r = -errno; | |
1393 | goto fail; | |
1394 | } | |
1395 | ||
1396 | loopback_setup(); | |
1397 | ||
1398 | netns = open("/proc/self/ns/net", O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
1399 | if (netns < 0) { | |
1400 | r = -errno; | |
1401 | goto fail; | |
1402 | } | |
1403 | ||
1404 | r = 1; | |
613b411c | 1405 | |
3ee897d6 LP |
1406 | } else if (netns < 0) { |
1407 | r = netns; | |
1408 | goto fail; | |
613b411c | 1409 | |
3ee897d6 LP |
1410 | } else { |
1411 | /* Yay, found something, so let's join the namespace */ | |
613b411c LP |
1412 | if (setns(netns, CLONE_NEWNET) < 0) { |
1413 | r = -errno; | |
1414 | goto fail; | |
1415 | } | |
1416 | ||
1417 | r = 0; | |
1418 | } | |
1419 | ||
3ee897d6 LP |
1420 | q = send_one_fd(netns_storage_socket[1], netns, MSG_DONTWAIT); |
1421 | if (q < 0) { | |
1422 | r = q; | |
613b411c LP |
1423 | goto fail; |
1424 | } | |
1425 | ||
1426 | fail: | |
fe048ce5 | 1427 | (void) lockf(netns_storage_socket[0], F_ULOCK, 0); |
15ae422b LP |
1428 | return r; |
1429 | } | |
417116f2 | 1430 | |
1b8689f9 LP |
1431 | static const char *const protect_home_table[_PROTECT_HOME_MAX] = { |
1432 | [PROTECT_HOME_NO] = "no", | |
1433 | [PROTECT_HOME_YES] = "yes", | |
1434 | [PROTECT_HOME_READ_ONLY] = "read-only", | |
417116f2 LP |
1435 | }; |
1436 | ||
1b8689f9 LP |
1437 | DEFINE_STRING_TABLE_LOOKUP(protect_home, ProtectHome); |
1438 | ||
1439 | static const char *const protect_system_table[_PROTECT_SYSTEM_MAX] = { | |
1440 | [PROTECT_SYSTEM_NO] = "no", | |
1441 | [PROTECT_SYSTEM_YES] = "yes", | |
1442 | [PROTECT_SYSTEM_FULL] = "full", | |
3f815163 | 1443 | [PROTECT_SYSTEM_STRICT] = "strict", |
1b8689f9 LP |
1444 | }; |
1445 | ||
1446 | DEFINE_STRING_TABLE_LOOKUP(protect_system, ProtectSystem); |