]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
15ae422b LP |
2 | |
3 | #include <errno.h> | |
e08f94ac | 4 | #include <linux/loop.h> |
07630cea | 5 | #include <sched.h> |
15ae422b | 6 | #include <stdio.h> |
19df770f | 7 | #include <sys/file.h> |
07630cea | 8 | #include <sys/mount.h> |
07630cea | 9 | #include <unistd.h> |
3657d3a0 | 10 | #if WANT_LINUX_FS_H |
25e870b5 | 11 | #include <linux/fs.h> |
3657d3a0 | 12 | #endif |
15ae422b | 13 | |
b5efdb8a | 14 | #include "alloc-util.h" |
10404d52 | 15 | #include "base-filesystem.h" |
f461a28d | 16 | #include "chase.h" |
7f112f50 | 17 | #include "dev-setup.h" |
ec61371f | 18 | #include "devnum-util.h" |
93f59701 LB |
19 | #include "env-util.h" |
20 | #include "escape.h" | |
d51f8eb3 | 21 | #include "extension-util.h" |
3ffd4af2 | 22 | #include "fd-util.h" |
e5f10caf | 23 | #include "format-util.h" |
e2341b6b | 24 | #include "glyph-util.h" |
e908468b | 25 | #include "label.h" |
b3d13314 | 26 | #include "list.h" |
13339577 | 27 | #include "lock-util.h" |
915e6d16 | 28 | #include "loop-util.h" |
07630cea | 29 | #include "loopback-setup.h" |
1c265fcd | 30 | #include "missing_syscall.h" |
35cd0ba5 | 31 | #include "mkdir-label.h" |
4349cd7c | 32 | #include "mount-util.h" |
049af8ad | 33 | #include "mountpoint-util.h" |
0cb8e3d1 | 34 | #include "namespace-util.h" |
3ffd4af2 | 35 | #include "namespace.h" |
54c2459d | 36 | #include "nsflags.h" |
d8b4d14d | 37 | #include "nulstr-util.h" |
93f59701 | 38 | #include "os-util.h" |
07630cea | 39 | #include "path-util.h" |
d7b8eec7 | 40 | #include "selinux-util.h" |
2583fbea | 41 | #include "socket-util.h" |
760877e9 | 42 | #include "sort-util.h" |
36ce7110 | 43 | #include "stat-util.h" |
8b43440b | 44 | #include "string-table.h" |
07630cea LP |
45 | #include "string-util.h" |
46 | #include "strv.h" | |
a652f050 | 47 | #include "tmpfile-util.h" |
affb60b1 | 48 | #include "umask-util.h" |
ee104e11 | 49 | #include "user-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, | |
93f59701 | 56 | OVERLAY_MOUNT, |
b3d13314 | 57 | MOUNT_IMAGES, |
d2d6c096 LP |
58 | BIND_MOUNT, |
59 | BIND_MOUNT_RECURSIVE, | |
ac0930c8 | 60 | PRIVATE_TMP, |
56a13a49 | 61 | PRIVATE_TMP_READONLY, |
7f112f50 | 62 | PRIVATE_DEV, |
5d997827 | 63 | BIND_DEV, |
6c47cd7d | 64 | EMPTY_DIR, |
277b269e | 65 | PRIVATE_SYSFS, |
10028263 | 66 | BIND_SYSFS, |
5d997827 LP |
67 | PROCFS, |
68 | READONLY, | |
59eeb84b | 69 | READWRITE, |
ddc155b2 TM |
70 | NOEXEC, |
71 | EXEC, | |
2abd4e38 | 72 | TMPFS, |
12413985 | 73 | RUN, |
a07b9926 | 74 | EXTENSION_DIRECTORIES, /* Bind-mounted outside the root directory, and used by subsequent mounts */ |
93f59701 | 75 | EXTENSION_IMAGES, /* Mounted outside the root directory, and used by subsequent mounts */ |
80271a44 | 76 | MQUEUEFS, |
1e05071d | 77 | READWRITE_IMPLICIT, /* Should have the lowest priority. */ |
5beb8688 | 78 | _MOUNT_MODE_MAX, |
c17ec25e | 79 | } MountMode; |
15ae422b | 80 | |
34de407a | 81 | typedef struct MountEntry { |
5327c910 | 82 | const char *path_const; /* Memory allocated on stack or static */ |
cfbeb4ef | 83 | MountMode mode:5; |
5327c910 LP |
84 | bool ignore:1; /* Ignore if path does not exist? */ |
85 | bool has_prefix:1; /* Already is prefixed by the root dir? */ | |
cfbeb4ef | 86 | bool read_only:1; /* Shall this mount point be read-only? */ |
9ce4e4b0 | 87 | bool nosuid:1; /* Shall set MS_NOSUID on the mount itself */ |
ddc155b2 TM |
88 | bool noexec:1; /* Shall set MS_NOEXEC on the mount itself */ |
89 | bool exec:1; /* Shall clear MS_NOEXEC on the mount itself */ | |
088696fe | 90 | bool applied:1; /* Already applied */ |
55fe7432 | 91 | char *path_malloc; /* Use this instead of 'path_const' if we had to allocate memory */ |
809ceb82 LB |
92 | const char *unprefixed_path_const; /* If the path was amended with a prefix, these will save the original */ |
93 | char *unprefixed_path_malloc; | |
b3d13314 | 94 | const char *source_const; /* The source path, for bind mounts or images */ |
d2d6c096 | 95 | char *source_malloc; |
2abd4e38 YW |
96 | const char *options_const;/* Mount options for tmpfs */ |
97 | char *options_malloc; | |
98 | unsigned long flags; /* Mount flags used by EMPTY_DIR and TMPFS. Do not include MS_RDONLY here, but please use read_only. */ | |
088696fe | 99 | unsigned n_followed; |
427353f6 | 100 | LIST_HEAD(MountOptions, image_options); |
34de407a | 101 | } MountEntry; |
15ae422b | 102 | |
94293d65 | 103 | /* If MountAPIVFS= is used, let's mount /sys, /proc, /dev and /run into the it, but only as a fallback if the user hasn't mounted |
3fe91079 | 104 | * something there already. These mounts are hence overridden by any other explicitly configured mounts. */ |
5d997827 LP |
105 | static const MountEntry apivfs_table[] = { |
106 | { "/proc", PROCFS, false }, | |
107 | { "/dev", BIND_DEV, false }, | |
10028263 | 108 | { "/sys", BIND_SYSFS, false }, |
9f563f27 | 109 | { "/run", RUN, false, .options_const = "mode=0755" TMPFS_LIMITS_RUN, .flags = MS_NOSUID|MS_NODEV|MS_STRICTATIME }, |
5d997827 | 110 | }; |
f471b2af | 111 | |
11a30cec | 112 | /* ProtectKernelTunables= option and the related filesystem APIs */ |
788e7201 | 113 | static const MountEntry protect_kernel_tunables_proc_table[] = { |
1e05071d YW |
114 | { "/proc/acpi", READONLY, true }, |
115 | { "/proc/apm", READONLY, true }, /* Obsolete API, there's no point in permitting access to this, ever */ | |
116 | { "/proc/asound", READONLY, true }, | |
117 | { "/proc/bus", READONLY, true }, | |
118 | { "/proc/fs", READONLY, true }, | |
119 | { "/proc/irq", READONLY, true }, | |
120 | { "/proc/kallsyms", INACCESSIBLE, true }, | |
121 | { "/proc/kcore", INACCESSIBLE, true }, | |
122 | { "/proc/latency_stats", READONLY, true }, | |
123 | { "/proc/mtrr", READONLY, true }, | |
124 | { "/proc/scsi", READONLY, true }, | |
4e399953 | 125 | { "/proc/sys", READONLY, true }, |
1e05071d YW |
126 | { "/proc/sysrq-trigger", READONLY, true }, |
127 | { "/proc/timer_stats", READONLY, true }, | |
788e7201 TM |
128 | }; |
129 | ||
130 | static const MountEntry protect_kernel_tunables_sys_table[] = { | |
1e05071d YW |
131 | { "/sys", READONLY, false }, |
132 | { "/sys/fs/bpf", READONLY, true }, | |
133 | { "/sys/fs/cgroup", READWRITE_IMPLICIT, false }, /* READONLY is set by ProtectControlGroups= option */ | |
134 | { "/sys/fs/selinux", READWRITE_IMPLICIT, true }, | |
135 | { "/sys/kernel/debug", READONLY, true }, | |
136 | { "/sys/kernel/tracing", READONLY, true }, | |
11a30cec DH |
137 | }; |
138 | ||
c575770b | 139 | /* ProtectKernelModules= option */ |
34de407a | 140 | static const MountEntry protect_kernel_modules_table[] = { |
349cc4a5 | 141 | #if HAVE_SPLIT_USR |
c6232fb0 | 142 | { "/lib/modules", INACCESSIBLE, true }, |
c575770b | 143 | #endif |
c6232fb0 | 144 | { "/usr/lib/modules", INACCESSIBLE, true }, |
c575770b DH |
145 | }; |
146 | ||
94a7b275 | 147 | /* ProtectKernelLogs= option */ |
788e7201 | 148 | static const MountEntry protect_kernel_logs_proc_table[] = { |
94a7b275 | 149 | { "/proc/kmsg", INACCESSIBLE, true }, |
788e7201 TM |
150 | }; |
151 | ||
152 | static const MountEntry protect_kernel_logs_dev_table[] = { | |
94a7b275 KK |
153 | { "/dev/kmsg", INACCESSIBLE, true }, |
154 | }; | |
155 | ||
b6c432ca DH |
156 | /* |
157 | * ProtectHome=read-only table, protect $HOME and $XDG_RUNTIME_DIR and rest of | |
158 | * system should be protected by ProtectSystem= | |
159 | */ | |
34de407a | 160 | static const MountEntry protect_home_read_only_table[] = { |
c6232fb0 LP |
161 | { "/home", READONLY, true }, |
162 | { "/run/user", READONLY, true }, | |
163 | { "/root", READONLY, true }, | |
b6c432ca DH |
164 | }; |
165 | ||
e4da7d8c YW |
166 | /* ProtectHome=tmpfs table */ |
167 | static const MountEntry protect_home_tmpfs_table[] = { | |
7d85383e TM |
168 | { "/home", TMPFS, true, .read_only = true, .options_const = "mode=0755" TMPFS_LIMITS_EMPTY_OR_ALMOST, .flags = MS_NODEV|MS_STRICTATIME }, |
169 | { "/run/user", TMPFS, true, .read_only = true, .options_const = "mode=0755" TMPFS_LIMITS_EMPTY_OR_ALMOST, .flags = MS_NODEV|MS_STRICTATIME }, | |
170 | { "/root", TMPFS, true, .read_only = true, .options_const = "mode=0700" TMPFS_LIMITS_EMPTY_OR_ALMOST, .flags = MS_NODEV|MS_STRICTATIME }, | |
e4da7d8c YW |
171 | }; |
172 | ||
b6c432ca | 173 | /* ProtectHome=yes table */ |
34de407a | 174 | static const MountEntry protect_home_yes_table[] = { |
c6232fb0 LP |
175 | { "/home", INACCESSIBLE, true }, |
176 | { "/run/user", INACCESSIBLE, true }, | |
177 | { "/root", INACCESSIBLE, true }, | |
b6c432ca DH |
178 | }; |
179 | ||
f471b2af | 180 | /* ProtectSystem=yes table */ |
34de407a | 181 | static const MountEntry protect_system_yes_table[] = { |
c6232fb0 LP |
182 | { "/usr", READONLY, false }, |
183 | { "/boot", READONLY, true }, | |
184 | { "/efi", READONLY, true }, | |
7486f305 AB |
185 | #if HAVE_SPLIT_USR |
186 | { "/lib", READONLY, true }, | |
187 | { "/lib64", READONLY, true }, | |
188 | { "/bin", READONLY, true }, | |
671f0f8d | 189 | # if HAVE_SPLIT_BIN |
7486f305 | 190 | { "/sbin", READONLY, true }, |
671f0f8d | 191 | # endif |
7486f305 | 192 | #endif |
f471b2af DH |
193 | }; |
194 | ||
195 | /* ProtectSystem=full includes ProtectSystem=yes */ | |
34de407a | 196 | static const MountEntry protect_system_full_table[] = { |
c6232fb0 LP |
197 | { "/usr", READONLY, false }, |
198 | { "/boot", READONLY, true }, | |
199 | { "/efi", READONLY, true }, | |
200 | { "/etc", READONLY, false }, | |
7486f305 AB |
201 | #if HAVE_SPLIT_USR |
202 | { "/lib", READONLY, true }, | |
203 | { "/lib64", READONLY, true }, | |
204 | { "/bin", READONLY, true }, | |
671f0f8d | 205 | # if HAVE_SPLIT_BIN |
7486f305 | 206 | { "/sbin", READONLY, true }, |
671f0f8d | 207 | # endif |
7486f305 | 208 | #endif |
f471b2af DH |
209 | }; |
210 | ||
211 | /* | |
212 | * ProtectSystem=strict table. In this strict mode, we mount everything | |
213 | * read-only, except for /proc, /dev, /sys which are the kernel API VFS, | |
214 | * which are left writable, but PrivateDevices= + ProtectKernelTunables= | |
215 | * protect those, and these options should be fully orthogonal. | |
216 | * (And of course /home and friends are also left writable, as ProtectHome= | |
217 | * shall manage those, orthogonally). | |
218 | */ | |
34de407a | 219 | static const MountEntry protect_system_strict_table[] = { |
1e05071d YW |
220 | { "/", READONLY, false }, |
221 | { "/proc", READWRITE_IMPLICIT, false }, /* ProtectKernelTunables= */ | |
222 | { "/sys", READWRITE_IMPLICIT, false }, /* ProtectKernelTunables= */ | |
223 | { "/dev", READWRITE_IMPLICIT, false }, /* PrivateDevices= */ | |
224 | { "/home", READWRITE_IMPLICIT, true }, /* ProtectHome= */ | |
225 | { "/run/user", READWRITE_IMPLICIT, true }, /* ProtectHome= */ | |
226 | { "/root", READWRITE_IMPLICIT, true }, /* ProtectHome= */ | |
f471b2af DH |
227 | }; |
228 | ||
5beb8688 YW |
229 | static const char * const mount_mode_table[_MOUNT_MODE_MAX] = { |
230 | [INACCESSIBLE] = "inaccessible", | |
93f59701 | 231 | [OVERLAY_MOUNT] = "overlay", |
5beb8688 YW |
232 | [BIND_MOUNT] = "bind", |
233 | [BIND_MOUNT_RECURSIVE] = "rbind", | |
234 | [PRIVATE_TMP] = "private-tmp", | |
235 | [PRIVATE_DEV] = "private-dev", | |
236 | [BIND_DEV] = "bind-dev", | |
237 | [EMPTY_DIR] = "empty", | |
277b269e | 238 | [PRIVATE_SYSFS] = "private-sysfs", |
10028263 | 239 | [BIND_SYSFS] = "bind-sysfs", |
5beb8688 YW |
240 | [PROCFS] = "procfs", |
241 | [READONLY] = "read-only", | |
242 | [READWRITE] = "read-write", | |
243 | [TMPFS] = "tmpfs", | |
b3d13314 | 244 | [MOUNT_IMAGES] = "mount-images", |
5beb8688 | 245 | [READWRITE_IMPLICIT] = "rw-implicit", |
ddc155b2 TM |
246 | [EXEC] = "exec", |
247 | [NOEXEC] = "noexec", | |
80271a44 | 248 | [MQUEUEFS] = "mqueuefs", |
5beb8688 YW |
249 | }; |
250 | ||
251 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(mount_mode, MountMode); | |
252 | ||
34de407a | 253 | static const char *mount_entry_path(const MountEntry *p) { |
f0a4feb0 DH |
254 | assert(p); |
255 | ||
5327c910 LP |
256 | /* Returns the path of this bind mount. If the malloc()-allocated ->path_buffer field is set we return that, |
257 | * otherwise the stack/static ->path field is returned. */ | |
f0a4feb0 | 258 | |
5327c910 | 259 | return p->path_malloc ?: p->path_const; |
f0a4feb0 DH |
260 | } |
261 | ||
809ceb82 LB |
262 | static const char *mount_entry_unprefixed_path(const MountEntry *p) { |
263 | assert(p); | |
264 | ||
265 | /* Returns the unprefixed path (ie: before prefix_where_needed() ran), if any */ | |
266 | ||
267 | return p->unprefixed_path_malloc ?: p->unprefixed_path_const ?: mount_entry_path(p); | |
268 | } | |
269 | ||
270 | static void mount_entry_consume_prefix(MountEntry *p, char *new_path) { | |
271 | assert(p); | |
272 | assert(p->path_malloc || p->path_const); | |
273 | assert(new_path); | |
274 | ||
275 | /* Saves current path in unprefixed_ variable, and takes over new_path */ | |
276 | ||
277 | free_and_replace(p->unprefixed_path_malloc, p->path_malloc); | |
278 | /* If we didn't have a path on the heap, then it's a static one */ | |
279 | if (!p->unprefixed_path_malloc) | |
280 | p->unprefixed_path_const = p->path_const; | |
281 | p->path_malloc = new_path; | |
282 | p->has_prefix = true; | |
283 | } | |
284 | ||
34de407a | 285 | static bool mount_entry_read_only(const MountEntry *p) { |
cfbeb4ef LP |
286 | assert(p); |
287 | ||
56a13a49 | 288 | return p->read_only || IN_SET(p->mode, READONLY, INACCESSIBLE, PRIVATE_TMP_READONLY); |
cfbeb4ef LP |
289 | } |
290 | ||
ddc155b2 TM |
291 | static bool mount_entry_noexec(const MountEntry *p) { |
292 | assert(p); | |
293 | ||
277b269e | 294 | return p->noexec || IN_SET(p->mode, NOEXEC, INACCESSIBLE, PRIVATE_SYSFS, BIND_SYSFS, PROCFS); |
ddc155b2 TM |
295 | } |
296 | ||
297 | static bool mount_entry_exec(const MountEntry *p) { | |
298 | assert(p); | |
299 | ||
300 | return p->exec || p->mode == EXEC; | |
301 | } | |
302 | ||
d2d6c096 LP |
303 | static const char *mount_entry_source(const MountEntry *p) { |
304 | assert(p); | |
305 | ||
306 | return p->source_malloc ?: p->source_const; | |
307 | } | |
308 | ||
2abd4e38 YW |
309 | static const char *mount_entry_options(const MountEntry *p) { |
310 | assert(p); | |
311 | ||
312 | return p->options_malloc ?: p->options_const; | |
313 | } | |
314 | ||
1eb7e08e LP |
315 | static void mount_entry_done(MountEntry *p) { |
316 | assert(p); | |
317 | ||
318 | p->path_malloc = mfree(p->path_malloc); | |
809ceb82 | 319 | p->unprefixed_path_malloc = mfree(p->unprefixed_path_malloc); |
1eb7e08e | 320 | p->source_malloc = mfree(p->source_malloc); |
2abd4e38 | 321 | p->options_malloc = mfree(p->options_malloc); |
427353f6 | 322 | p->image_options = mount_options_free_all(p->image_options); |
1eb7e08e LP |
323 | } |
324 | ||
d18aff04 | 325 | static int append_access_mounts(MountEntry **p, char **strv, MountMode mode, bool forcibly_require_prefix) { |
613b411c LP |
326 | assert(p); |
327 | ||
1e05071d | 328 | /* Adds a list of user-supplied READWRITE/READWRITE_IMPLICIT/READONLY/INACCESSIBLE entries */ |
5327c910 | 329 | |
15ae422b | 330 | STRV_FOREACH(i, strv) { |
5327c910 LP |
331 | bool ignore = false, needs_prefix = false; |
332 | const char *e = *i; | |
15ae422b | 333 | |
5327c910 LP |
334 | /* Look for any prefixes */ |
335 | if (startswith(e, "-")) { | |
336 | e++; | |
9c94d52e | 337 | ignore = true; |
ea92ae33 | 338 | } |
5327c910 LP |
339 | if (startswith(e, "+")) { |
340 | e++; | |
341 | needs_prefix = true; | |
342 | } | |
ea92ae33 | 343 | |
baaa35ad ZJS |
344 | if (!path_is_absolute(e)) |
345 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
346 | "Path is not absolute: %s", e); | |
15ae422b | 347 | |
34de407a | 348 | *((*p)++) = (MountEntry) { |
5327c910 LP |
349 | .path_const = e, |
350 | .mode = mode, | |
351 | .ignore = ignore, | |
d18aff04 | 352 | .has_prefix = !needs_prefix && !forcibly_require_prefix, |
5327c910 | 353 | }; |
15ae422b LP |
354 | } |
355 | ||
356 | return 0; | |
357 | } | |
358 | ||
6c47cd7d | 359 | static int append_empty_dir_mounts(MountEntry **p, char **strv) { |
6c47cd7d LP |
360 | assert(p); |
361 | ||
362 | /* Adds tmpfs mounts to provide readable but empty directories. This is primarily used to implement the | |
363 | * "/private/" boundary directories for DynamicUser=1. */ | |
364 | ||
365 | STRV_FOREACH(i, strv) { | |
366 | ||
367 | *((*p)++) = (MountEntry) { | |
368 | .path_const = *i, | |
369 | .mode = EMPTY_DIR, | |
370 | .ignore = false, | |
6c47cd7d | 371 | .read_only = true, |
9f563f27 | 372 | .options_const = "mode=0755" TMPFS_LIMITS_EMPTY_OR_ALMOST, |
2abd4e38 | 373 | .flags = MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, |
6c47cd7d LP |
374 | }; |
375 | } | |
376 | ||
377 | return 0; | |
378 | } | |
379 | ||
da6053d0 | 380 | static int append_bind_mounts(MountEntry **p, const BindMount *binds, size_t n) { |
d2d6c096 LP |
381 | assert(p); |
382 | ||
fe96c0f8 | 383 | for (size_t i = 0; i < n; i++) { |
d2d6c096 LP |
384 | const BindMount *b = binds + i; |
385 | ||
386 | *((*p)++) = (MountEntry) { | |
387 | .path_const = b->destination, | |
388 | .mode = b->recursive ? BIND_MOUNT_RECURSIVE : BIND_MOUNT, | |
389 | .read_only = b->read_only, | |
9ce4e4b0 | 390 | .nosuid = b->nosuid, |
d2d6c096 | 391 | .source_const = b->source, |
4ca763a9 | 392 | .ignore = b->ignore_enoent, |
d2d6c096 LP |
393 | }; |
394 | } | |
395 | ||
396 | return 0; | |
397 | } | |
398 | ||
b3d13314 LB |
399 | static int append_mount_images(MountEntry **p, const MountImage *mount_images, size_t n) { |
400 | assert(p); | |
401 | ||
402 | for (size_t i = 0; i < n; i++) { | |
403 | const MountImage *m = mount_images + i; | |
404 | ||
405 | *((*p)++) = (MountEntry) { | |
406 | .path_const = m->destination, | |
407 | .mode = MOUNT_IMAGES, | |
408 | .source_const = m->source, | |
427353f6 | 409 | .image_options = m->mount_options, |
b3d13314 LB |
410 | .ignore = m->ignore_enoent, |
411 | }; | |
412 | } | |
413 | ||
414 | return 0; | |
415 | } | |
416 | ||
a07b9926 | 417 | static int append_extensions( |
93f59701 LB |
418 | MountEntry **p, |
419 | const char *root, | |
420 | const char *extension_dir, | |
421 | char **hierarchies, | |
422 | const MountImage *mount_images, | |
a07b9926 LB |
423 | size_t n, |
424 | char **extension_directories) { | |
93f59701 LB |
425 | |
426 | _cleanup_strv_free_ char **overlays = NULL; | |
93f59701 LB |
427 | int r; |
428 | ||
a07b9926 | 429 | if (n == 0 && strv_isempty(extension_directories)) |
93f59701 LB |
430 | return 0; |
431 | ||
24759d8f LB |
432 | assert(p); |
433 | assert(extension_dir); | |
434 | ||
93f59701 | 435 | /* Prepare a list of overlays, that will have as each element a string suitable for being |
7a7d2f16 | 436 | * passed as a lowerdir= parameter, so start with the hierarchy on the root. |
93f59701 LB |
437 | * The overlays vector will have the same number of elements and will correspond to the |
438 | * hierarchies vector, so they can be iterated upon together. */ | |
439 | STRV_FOREACH(hierarchy, hierarchies) { | |
440 | _cleanup_free_ char *prefixed_hierarchy = NULL; | |
441 | ||
442 | prefixed_hierarchy = path_join(root, *hierarchy); | |
443 | if (!prefixed_hierarchy) | |
444 | return -ENOMEM; | |
445 | ||
446 | r = strv_consume(&overlays, TAKE_PTR(prefixed_hierarchy)); | |
447 | if (r < 0) | |
448 | return r; | |
449 | } | |
450 | ||
451 | /* First, prepare a mount for each image, but these won't be visible to the unit, instead | |
452 | * they will be mounted in our propagate directory, and used as a source for the overlay. */ | |
453 | for (size_t i = 0; i < n; i++) { | |
454 | _cleanup_free_ char *mount_point = NULL; | |
455 | const MountImage *m = mount_images + i; | |
456 | ||
457 | r = asprintf(&mount_point, "%s/%zu", extension_dir, i); | |
458 | if (r < 0) | |
459 | return -ENOMEM; | |
460 | ||
461 | for (size_t j = 0; hierarchies && hierarchies[j]; ++j) { | |
462 | _cleanup_free_ char *prefixed_hierarchy = NULL, *escaped = NULL, *lowerdir = NULL; | |
463 | ||
464 | prefixed_hierarchy = path_join(mount_point, hierarchies[j]); | |
465 | if (!prefixed_hierarchy) | |
466 | return -ENOMEM; | |
467 | ||
468 | escaped = shell_escape(prefixed_hierarchy, ",:"); | |
469 | if (!escaped) | |
470 | return -ENOMEM; | |
471 | ||
472 | /* Note that lowerdir= parameters are in 'reverse' order, so the | |
473 | * top-most directory in the overlay comes first in the list. */ | |
474 | lowerdir = strjoin(escaped, ":", overlays[j]); | |
475 | if (!lowerdir) | |
476 | return -ENOMEM; | |
477 | ||
478 | free_and_replace(overlays[j], lowerdir); | |
479 | } | |
480 | ||
481 | *((*p)++) = (MountEntry) { | |
482 | .path_malloc = TAKE_PTR(mount_point), | |
483 | .image_options = m->mount_options, | |
484 | .ignore = m->ignore_enoent, | |
485 | .source_const = m->source, | |
486 | .mode = EXTENSION_IMAGES, | |
487 | .has_prefix = true, | |
488 | }; | |
489 | } | |
490 | ||
a07b9926 LB |
491 | /* Secondly, extend the lowerdir= parameters with each ExtensionDirectory. |
492 | * Bind mount them in the same location as the ExtensionImages, so that we | |
493 | * can check that they are valid trees (extension-release.d). */ | |
494 | STRV_FOREACH(extension_directory, extension_directories) { | |
495 | _cleanup_free_ char *mount_point = NULL, *source = NULL; | |
496 | const char *e = *extension_directory; | |
497 | bool ignore_enoent = false; | |
498 | ||
499 | /* Pick up the counter where the ExtensionImages left it. */ | |
500 | r = asprintf(&mount_point, "%s/%zu", extension_dir, n++); | |
501 | if (r < 0) | |
502 | return -ENOMEM; | |
503 | ||
504 | /* Look for any prefixes */ | |
505 | if (startswith(e, "-")) { | |
506 | e++; | |
507 | ignore_enoent = true; | |
508 | } | |
509 | /* Ignore this for now */ | |
510 | if (startswith(e, "+")) | |
511 | e++; | |
512 | ||
513 | source = strdup(e); | |
514 | if (!source) | |
515 | return -ENOMEM; | |
516 | ||
517 | for (size_t j = 0; hierarchies && hierarchies[j]; ++j) { | |
518 | _cleanup_free_ char *prefixed_hierarchy = NULL, *escaped = NULL, *lowerdir = NULL; | |
519 | ||
520 | prefixed_hierarchy = path_join(mount_point, hierarchies[j]); | |
521 | if (!prefixed_hierarchy) | |
522 | return -ENOMEM; | |
523 | ||
524 | escaped = shell_escape(prefixed_hierarchy, ",:"); | |
525 | if (!escaped) | |
526 | return -ENOMEM; | |
527 | ||
528 | /* Note that lowerdir= parameters are in 'reverse' order, so the | |
529 | * top-most directory in the overlay comes first in the list. */ | |
530 | lowerdir = strjoin(escaped, ":", overlays[j]); | |
531 | if (!lowerdir) | |
532 | return -ENOMEM; | |
533 | ||
534 | free_and_replace(overlays[j], lowerdir); | |
535 | } | |
536 | ||
537 | *((*p)++) = (MountEntry) { | |
538 | .path_malloc = TAKE_PTR(mount_point), | |
539 | .source_const = TAKE_PTR(source), | |
540 | .mode = EXTENSION_DIRECTORIES, | |
541 | .ignore = ignore_enoent, | |
542 | .has_prefix = true, | |
543 | .read_only = true, | |
544 | }; | |
545 | } | |
546 | ||
93f59701 LB |
547 | /* Then, for each hierarchy, prepare an overlay with the list of lowerdir= strings |
548 | * set up earlier. */ | |
549 | for (size_t i = 0; hierarchies && hierarchies[i]; ++i) { | |
550 | _cleanup_free_ char *prefixed_hierarchy = NULL; | |
551 | ||
552 | prefixed_hierarchy = path_join(root, hierarchies[i]); | |
553 | if (!prefixed_hierarchy) | |
554 | return -ENOMEM; | |
555 | ||
556 | *((*p)++) = (MountEntry) { | |
557 | .path_malloc = TAKE_PTR(prefixed_hierarchy), | |
558 | .options_malloc = TAKE_PTR(overlays[i]), | |
559 | .mode = OVERLAY_MOUNT, | |
560 | .has_prefix = true, | |
561 | .ignore = true, /* If the source image doesn't set the ignore bit it will fail earlier. */ | |
562 | }; | |
563 | } | |
564 | ||
565 | return 0; | |
566 | } | |
567 | ||
da6053d0 | 568 | static int append_tmpfs_mounts(MountEntry **p, const TemporaryFileSystem *tmpfs, size_t n) { |
2abd4e38 YW |
569 | assert(p); |
570 | ||
b67ec8e5 | 571 | for (size_t i = 0; i < n; i++) { |
2abd4e38 YW |
572 | const TemporaryFileSystem *t = tmpfs + i; |
573 | _cleanup_free_ char *o = NULL, *str = NULL; | |
ad8e66dc | 574 | unsigned long flags; |
2abd4e38 | 575 | bool ro = false; |
b67ec8e5 | 576 | int r; |
2abd4e38 | 577 | |
baaa35ad ZJS |
578 | if (!path_is_absolute(t->path)) |
579 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
580 | "Path is not absolute: %s", | |
581 | t->path); | |
2abd4e38 | 582 | |
b67ec8e5 | 583 | str = strjoin("mode=0755" NESTED_TMPFS_LIMITS ",", t->options); |
ad8e66dc AJ |
584 | if (!str) |
585 | return -ENOMEM; | |
2abd4e38 | 586 | |
ad8e66dc AJ |
587 | r = mount_option_mangle(str, MS_NODEV|MS_STRICTATIME, &flags, &o); |
588 | if (r < 0) | |
589 | return log_debug_errno(r, "Failed to parse mount option '%s': %m", str); | |
2abd4e38 | 590 | |
ad8e66dc AJ |
591 | ro = flags & MS_RDONLY; |
592 | if (ro) | |
593 | flags ^= MS_RDONLY; | |
2abd4e38 YW |
594 | |
595 | *((*p)++) = (MountEntry) { | |
596 | .path_const = t->path, | |
597 | .mode = TMPFS, | |
598 | .read_only = ro, | |
ad8e66dc | 599 | .options_malloc = TAKE_PTR(o), |
2abd4e38 YW |
600 | .flags = flags, |
601 | }; | |
2abd4e38 YW |
602 | } |
603 | ||
604 | return 0; | |
605 | } | |
606 | ||
da6053d0 | 607 | static int append_static_mounts(MountEntry **p, const MountEntry *mounts, size_t n, bool ignore_protect) { |
11a30cec | 608 | assert(p); |
f471b2af | 609 | assert(mounts); |
11a30cec | 610 | |
5327c910 | 611 | /* Adds a list of static pre-defined entries */ |
f471b2af | 612 | |
fe96c0f8 | 613 | for (size_t i = 0; i < n; i++) |
34de407a LP |
614 | *((*p)++) = (MountEntry) { |
615 | .path_const = mount_entry_path(mounts+i), | |
5327c910 LP |
616 | .mode = mounts[i].mode, |
617 | .ignore = mounts[i].ignore || ignore_protect, | |
618 | }; | |
f471b2af DH |
619 | |
620 | return 0; | |
621 | } | |
622 | ||
34de407a | 623 | static int append_protect_home(MountEntry **p, ProtectHome protect_home, bool ignore_protect) { |
c575770b DH |
624 | assert(p); |
625 | ||
5327c910 | 626 | switch (protect_home) { |
b6c432ca | 627 | |
5327c910 | 628 | case PROTECT_HOME_NO: |
b6c432ca DH |
629 | return 0; |
630 | ||
b6c432ca | 631 | case PROTECT_HOME_READ_ONLY: |
5327c910 LP |
632 | return append_static_mounts(p, protect_home_read_only_table, ELEMENTSOF(protect_home_read_only_table), ignore_protect); |
633 | ||
e4da7d8c YW |
634 | case PROTECT_HOME_TMPFS: |
635 | return append_static_mounts(p, protect_home_tmpfs_table, ELEMENTSOF(protect_home_tmpfs_table), ignore_protect); | |
636 | ||
b6c432ca | 637 | case PROTECT_HOME_YES: |
5327c910 LP |
638 | return append_static_mounts(p, protect_home_yes_table, ELEMENTSOF(protect_home_yes_table), ignore_protect); |
639 | ||
b6c432ca | 640 | default: |
04499a70 | 641 | assert_not_reached(); |
b6c432ca | 642 | } |
b6c432ca DH |
643 | } |
644 | ||
34de407a | 645 | static int append_protect_system(MountEntry **p, ProtectSystem protect_system, bool ignore_protect) { |
f471b2af DH |
646 | assert(p); |
647 | ||
5327c910 LP |
648 | switch (protect_system) { |
649 | ||
650 | case PROTECT_SYSTEM_NO: | |
f471b2af DH |
651 | return 0; |
652 | ||
f471b2af | 653 | case PROTECT_SYSTEM_STRICT: |
5327c910 LP |
654 | return append_static_mounts(p, protect_system_strict_table, ELEMENTSOF(protect_system_strict_table), ignore_protect); |
655 | ||
f471b2af | 656 | case PROTECT_SYSTEM_YES: |
5327c910 LP |
657 | return append_static_mounts(p, protect_system_yes_table, ELEMENTSOF(protect_system_yes_table), ignore_protect); |
658 | ||
f471b2af | 659 | case PROTECT_SYSTEM_FULL: |
5327c910 LP |
660 | return append_static_mounts(p, protect_system_full_table, ELEMENTSOF(protect_system_full_table), ignore_protect); |
661 | ||
f471b2af | 662 | default: |
04499a70 | 663 | assert_not_reached(); |
f471b2af | 664 | } |
11a30cec DH |
665 | } |
666 | ||
93bab288 | 667 | static int mount_path_compare(const MountEntry *a, const MountEntry *b) { |
a0827e2b | 668 | int d; |
15ae422b | 669 | |
a07b9926 | 670 | /* ExtensionImages/Directories will be used by other mounts as a base, so sort them first |
93f59701 LB |
671 | * regardless of the prefix - they are set up in the propagate directory anyway */ |
672 | d = -CMP(a->mode == EXTENSION_IMAGES, b->mode == EXTENSION_IMAGES); | |
a07b9926 LB |
673 | if (d != 0) |
674 | return d; | |
675 | d = -CMP(a->mode == EXTENSION_DIRECTORIES, b->mode == EXTENSION_DIRECTORIES); | |
93f59701 LB |
676 | if (d != 0) |
677 | return d; | |
678 | ||
6ee1a919 | 679 | /* If the paths are not equal, then order prefixes first */ |
93bab288 | 680 | d = path_compare(mount_entry_path(a), mount_entry_path(b)); |
6ee1a919 LP |
681 | if (d != 0) |
682 | return d; | |
15ae422b | 683 | |
6ee1a919 | 684 | /* If the paths are equal, check the mode */ |
93bab288 | 685 | return CMP((int) a->mode, (int) b->mode); |
15ae422b LP |
686 | } |
687 | ||
da6053d0 | 688 | static int prefix_where_needed(MountEntry *m, size_t n, const char *root_directory) { |
4a756839 | 689 | /* Prefixes all paths in the bind mount table with the root directory if the entry needs that. */ |
5327c910 | 690 | |
fe96c0f8 SS |
691 | assert(m || n == 0); |
692 | ||
693 | for (size_t i = 0; i < n; i++) { | |
5327c910 LP |
694 | char *s; |
695 | ||
696 | if (m[i].has_prefix) | |
697 | continue; | |
698 | ||
c6134d3e | 699 | s = path_join(root_directory, mount_entry_path(m+i)); |
5327c910 LP |
700 | if (!s) |
701 | return -ENOMEM; | |
702 | ||
809ceb82 | 703 | mount_entry_consume_prefix(&m[i], s); |
5327c910 LP |
704 | } |
705 | ||
706 | return 0; | |
707 | } | |
708 | ||
da6053d0 | 709 | static void drop_duplicates(MountEntry *m, size_t *n) { |
34de407a | 710 | MountEntry *f, *t, *previous; |
15ae422b | 711 | |
c17ec25e | 712 | assert(m); |
15ae422b | 713 | assert(n); |
15ae422b | 714 | |
fe3c2583 LP |
715 | /* Drops duplicate entries. Expects that the array is properly ordered already. */ |
716 | ||
1d54cd5d | 717 | for (f = m, t = m, previous = NULL; f < m + *n; f++) { |
15ae422b | 718 | |
fe3c2583 | 719 | /* The first one wins (which is the one with the more restrictive mode), see mount_path_compare() |
088696fe LP |
720 | * above. Note that we only drop duplicates that haven't been mounted yet. */ |
721 | if (previous && | |
722 | path_equal(mount_entry_path(f), mount_entry_path(previous)) && | |
723 | !f->applied && !previous->applied) { | |
5beb8688 | 724 | log_debug("%s (%s) is duplicate.", mount_entry_path(f), mount_mode_to_string(f->mode)); |
ddc155b2 TM |
725 | /* Propagate the flags to the remaining entry */ |
726 | previous->read_only = previous->read_only || mount_entry_read_only(f); | |
727 | previous->noexec = previous->noexec || mount_entry_noexec(f); | |
728 | previous->exec = previous->exec || mount_entry_exec(f); | |
1eb7e08e | 729 | mount_entry_done(f); |
15ae422b | 730 | continue; |
fe3c2583 | 731 | } |
15ae422b | 732 | |
e2d7c1a0 | 733 | *t = *f; |
15ae422b | 734 | previous = t; |
fe3c2583 LP |
735 | t++; |
736 | } | |
737 | ||
738 | *n = t - m; | |
739 | } | |
740 | ||
da6053d0 | 741 | static void drop_inaccessible(MountEntry *m, size_t *n) { |
34de407a | 742 | MountEntry *f, *t; |
fe3c2583 LP |
743 | const char *clear = NULL; |
744 | ||
745 | assert(m); | |
746 | assert(n); | |
747 | ||
748 | /* Drops all entries obstructed by another entry further up the tree. Expects that the array is properly | |
749 | * ordered already. */ | |
750 | ||
1d54cd5d | 751 | for (f = m, t = m; f < m + *n; f++) { |
fe3c2583 LP |
752 | |
753 | /* If we found a path set for INACCESSIBLE earlier, and this entry has it as prefix we should drop | |
754 | * it, as inaccessible paths really should drop the entire subtree. */ | |
34de407a LP |
755 | if (clear && path_startswith(mount_entry_path(f), clear)) { |
756 | log_debug("%s is masked by %s.", mount_entry_path(f), clear); | |
1eb7e08e | 757 | mount_entry_done(f); |
fe3c2583 LP |
758 | continue; |
759 | } | |
15ae422b | 760 | |
34de407a | 761 | clear = f->mode == INACCESSIBLE ? mount_entry_path(f) : NULL; |
fe3c2583 LP |
762 | |
763 | *t = *f; | |
15ae422b LP |
764 | t++; |
765 | } | |
766 | ||
c17ec25e | 767 | *n = t - m; |
15ae422b LP |
768 | } |
769 | ||
da6053d0 | 770 | static void drop_nop(MountEntry *m, size_t *n) { |
34de407a | 771 | MountEntry *f, *t; |
7648a565 LP |
772 | |
773 | assert(m); | |
774 | assert(n); | |
775 | ||
776 | /* Drops all entries which have an immediate parent that has the same type, as they are redundant. Assumes the | |
777 | * list is ordered by prefixes. */ | |
778 | ||
1d54cd5d | 779 | for (f = m, t = m; f < m + *n; f++) { |
7648a565 | 780 | |
1e05071d YW |
781 | /* Only suppress such subtrees for READONLY, READWRITE and READWRITE_IMPLICIT entries */ |
782 | if (IN_SET(f->mode, READONLY, READWRITE, READWRITE_IMPLICIT)) { | |
e7bf2fca | 783 | MountEntry *found = NULL; |
7648a565 LP |
784 | |
785 | /* Now let's find the first parent of the entry we are looking at. */ | |
e7bf2fca | 786 | for (MountEntry *p = PTR_SUB1(t, m); p; p = PTR_SUB1(p, m)) |
34de407a | 787 | if (path_startswith(mount_entry_path(f), mount_entry_path(p))) { |
e7bf2fca | 788 | found = p; |
7648a565 LP |
789 | break; |
790 | } | |
7648a565 LP |
791 | |
792 | /* We found it, let's see if it's the same mode, if so, we can drop this entry */ | |
e7bf2fca | 793 | if (found && found->mode == f->mode) { |
5beb8688 YW |
794 | log_debug("%s (%s) is made redundant by %s (%s)", |
795 | mount_entry_path(f), mount_mode_to_string(f->mode), | |
e7bf2fca | 796 | mount_entry_path(found), mount_mode_to_string(found->mode)); |
1eb7e08e | 797 | mount_entry_done(f); |
7648a565 LP |
798 | continue; |
799 | } | |
800 | } | |
801 | ||
802 | *t = *f; | |
803 | t++; | |
804 | } | |
805 | ||
806 | *n = t - m; | |
807 | } | |
808 | ||
da6053d0 | 809 | static void drop_outside_root(const char *root_directory, MountEntry *m, size_t *n) { |
34de407a | 810 | MountEntry *f, *t; |
cd2902c9 LP |
811 | |
812 | assert(m); | |
813 | assert(n); | |
814 | ||
1d54cd5d | 815 | /* Nothing to do */ |
cd2902c9 LP |
816 | if (!root_directory) |
817 | return; | |
818 | ||
819 | /* Drops all mounts that are outside of the root directory. */ | |
820 | ||
1d54cd5d | 821 | for (f = m, t = m; f < m + *n; f++) { |
cd2902c9 | 822 | |
a07b9926 LB |
823 | /* ExtensionImages/Directories bases are opened in /run/systemd/unit-extensions on the host */ |
824 | if (!IN_SET(f->mode, EXTENSION_IMAGES, EXTENSION_DIRECTORIES) && !path_startswith(mount_entry_path(f), root_directory)) { | |
34de407a | 825 | log_debug("%s is outside of root directory.", mount_entry_path(f)); |
1eb7e08e | 826 | mount_entry_done(f); |
cd2902c9 LP |
827 | continue; |
828 | } | |
829 | ||
830 | *t = *f; | |
831 | t++; | |
832 | } | |
833 | ||
834 | *n = t - m; | |
835 | } | |
836 | ||
b2a60844 LP |
837 | static int clone_device_node( |
838 | const char *d, | |
839 | const char *temporary_mount, | |
840 | bool *make_devnode) { | |
841 | ||
842 | _cleanup_free_ char *sl = NULL; | |
843 | const char *dn, *bn, *t; | |
b5e99f23 ДГ |
844 | struct stat st; |
845 | int r; | |
846 | ||
414b304b | 847 | if (stat(d, &st) < 0) { |
b2a60844 LP |
848 | if (errno == ENOENT) { |
849 | log_debug_errno(errno, "Device node '%s' to clone does not exist, ignoring.", d); | |
af984e13 | 850 | return -ENXIO; |
b2a60844 LP |
851 | } |
852 | ||
853 | return log_debug_errno(errno, "Failed to stat() device node '%s' to clone, ignoring: %m", d); | |
b5e99f23 ДГ |
854 | } |
855 | ||
856 | if (!S_ISBLK(st.st_mode) && | |
baaa35ad ZJS |
857 | !S_ISCHR(st.st_mode)) |
858 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
859 | "Device node '%s' to clone is not a device node, ignoring.", | |
860 | d); | |
b5e99f23 | 861 | |
6f7f3a33 | 862 | dn = strjoina(temporary_mount, d); |
b5e99f23 | 863 | |
b2a60844 | 864 | /* First, try to create device node properly */ |
16498617 CB |
865 | if (*make_devnode) { |
866 | mac_selinux_create_file_prepare(d, st.st_mode); | |
867 | r = mknod(dn, st.st_mode, st.st_rdev); | |
868 | mac_selinux_create_file_clear(); | |
b2a60844 LP |
869 | if (r >= 0) |
870 | goto add_symlink; | |
16498617 CB |
871 | if (errno != EPERM) |
872 | return log_debug_errno(errno, "mknod failed for %s: %m", d); | |
873 | ||
b2a60844 | 874 | /* This didn't work, let's not try this again for the next iterations. */ |
16498617 CB |
875 | *make_devnode = false; |
876 | } | |
877 | ||
d73020f2 | 878 | /* We're about to fall back to bind-mounting the device node. So create a dummy bind-mount target. |
1acf344d | 879 | * Do not prepare device-node SELinux label (see issue 13762) */ |
16498617 | 880 | r = mknod(dn, S_IFREG, 0); |
16498617 | 881 | if (r < 0 && errno != EEXIST) |
b2a60844 | 882 | return log_debug_errno(errno, "mknod() fallback failed for '%s': %m", d); |
16498617 | 883 | |
21935150 LP |
884 | /* Fallback to bind-mounting: The assumption here is that all used device nodes carry standard |
885 | * properties. Specifically, the devices nodes we bind-mount should either be owned by root:root or | |
886 | * root:tty (e.g. /dev/tty, /dev/ptmx) and should not carry ACLs. */ | |
887 | r = mount_nofollow_verbose(LOG_DEBUG, d, dn, NULL, MS_BIND, NULL); | |
888 | if (r < 0) | |
889 | return r; | |
b2a60844 LP |
890 | |
891 | add_symlink: | |
892 | bn = path_startswith(d, "/dev/"); | |
893 | if (!bn) | |
894 | return 0; | |
895 | ||
896 | /* Create symlinks like /dev/char/1:9 → ../urandom */ | |
ec61371f | 897 | if (asprintf(&sl, "%s/dev/%s/" DEVNUM_FORMAT_STR, |
cbc056c8 ZJS |
898 | temporary_mount, |
899 | S_ISCHR(st.st_mode) ? "char" : "block", | |
ec61371f | 900 | DEVNUM_FORMAT_VAL(st.st_rdev)) < 0) |
b2a60844 LP |
901 | return log_oom(); |
902 | ||
903 | (void) mkdir_parents(sl, 0755); | |
904 | ||
905 | t = strjoina("../", bn); | |
b2a60844 | 906 | if (symlink(t, sl) < 0) |
2e4a4fae | 907 | log_debug_errno(errno, "Failed to symlink '%s' to '%s', ignoring: %m", t, sl); |
b5e99f23 | 908 | |
af984e13 | 909 | return 0; |
b5e99f23 ДГ |
910 | } |
911 | ||
5d997827 | 912 | static int mount_private_dev(MountEntry *m) { |
7f112f50 LP |
913 | static const char devnodes[] = |
914 | "/dev/null\0" | |
915 | "/dev/zero\0" | |
916 | "/dev/full\0" | |
917 | "/dev/random\0" | |
918 | "/dev/urandom\0" | |
919 | "/dev/tty\0"; | |
920 | ||
2b85f4e1 | 921 | char temporary_mount[] = "/tmp/namespace-dev-XXXXXX"; |
12e2b70f | 922 | const char *dev = NULL, *devpts = NULL, *devshm = NULL, *devhugepages = NULL, *devmqueue = NULL, *devlog = NULL, *devptmx = NULL; |
16498617 | 923 | bool can_mknod = true; |
7f112f50 LP |
924 | int r; |
925 | ||
926 | assert(m); | |
927 | ||
2b85f4e1 | 928 | if (!mkdtemp(temporary_mount)) |
2e4a4fae | 929 | return log_debug_errno(errno, "Failed to create temporary directory '%s': %m", temporary_mount); |
2b85f4e1 | 930 | |
63c372cb | 931 | dev = strjoina(temporary_mount, "/dev"); |
dc751688 | 932 | (void) mkdir(dev, 0755); |
9f563f27 | 933 | r = mount_nofollow_verbose(LOG_DEBUG, "tmpfs", dev, "tmpfs", DEV_MOUNT_OPTIONS, "mode=0755" TMPFS_LIMITS_PRIVATE_DEV); |
21935150 | 934 | if (r < 0) |
2b85f4e1 | 935 | goto fail; |
21935150 | 936 | |
03bc11d1 | 937 | r = label_fix_full(AT_FDCWD, dev, "/dev", 0); |
c3151977 | 938 | if (r < 0) { |
1ce268c7 | 939 | log_debug_errno(r, "Failed to fix label of '%s' as /dev: %m", dev); |
c3151977 TM |
940 | goto fail; |
941 | } | |
2b85f4e1 | 942 | |
63c372cb | 943 | devpts = strjoina(temporary_mount, "/dev/pts"); |
dc751688 | 944 | (void) mkdir(devpts, 0755); |
21935150 LP |
945 | r = mount_nofollow_verbose(LOG_DEBUG, "/dev/pts", devpts, NULL, MS_BIND, NULL); |
946 | if (r < 0) | |
2b85f4e1 | 947 | goto fail; |
2b85f4e1 | 948 | |
2e4a4fae YW |
949 | /* /dev/ptmx can either be a device node or a symlink to /dev/pts/ptmx. |
950 | * When /dev/ptmx a device node, /dev/pts/ptmx has 000 permissions making it inaccessible. | |
951 | * Thus, in that case make a clone. | |
952 | * In nspawn and other containers it will be a symlink, in that case make it a symlink. */ | |
36ce7110 | 953 | r = is_symlink("/dev/ptmx"); |
2e4a4fae YW |
954 | if (r < 0) { |
955 | log_debug_errno(r, "Failed to detect whether /dev/ptmx is a symlink or not: %m"); | |
3164e3cb | 956 | goto fail; |
2e4a4fae | 957 | } else if (r > 0) { |
414b304b ДГ |
958 | devptmx = strjoina(temporary_mount, "/dev/ptmx"); |
959 | if (symlink("pts/ptmx", devptmx) < 0) { | |
2e4a4fae | 960 | r = log_debug_errno(errno, "Failed to create a symlink '%s' to pts/ptmx: %m", devptmx); |
414b304b ДГ |
961 | goto fail; |
962 | } | |
963 | } else { | |
16498617 | 964 | r = clone_device_node("/dev/ptmx", temporary_mount, &can_mknod); |
152c475f LP |
965 | if (r < 0) |
966 | goto fail; | |
414b304b | 967 | } |
e06b6479 | 968 | |
63c372cb | 969 | devshm = strjoina(temporary_mount, "/dev/shm"); |
8d953682 | 970 | (void) mkdir(devshm, 0755); |
21935150 LP |
971 | r = mount_nofollow_verbose(LOG_DEBUG, "/dev/shm", devshm, NULL, MS_BIND, NULL); |
972 | if (r < 0) | |
2b85f4e1 | 973 | goto fail; |
2b85f4e1 | 974 | |
63c372cb | 975 | devmqueue = strjoina(temporary_mount, "/dev/mqueue"); |
dc751688 | 976 | (void) mkdir(devmqueue, 0755); |
21935150 | 977 | (void) mount_nofollow_verbose(LOG_DEBUG, "/dev/mqueue", devmqueue, NULL, MS_BIND, NULL); |
2b85f4e1 | 978 | |
63c372cb | 979 | devhugepages = strjoina(temporary_mount, "/dev/hugepages"); |
dc751688 | 980 | (void) mkdir(devhugepages, 0755); |
21935150 | 981 | (void) mount_nofollow_verbose(LOG_DEBUG, "/dev/hugepages", devhugepages, NULL, MS_BIND, NULL); |
2b85f4e1 | 982 | |
63c372cb | 983 | devlog = strjoina(temporary_mount, "/dev/log"); |
2e4a4fae YW |
984 | if (symlink("/run/systemd/journal/dev-log", devlog) < 0) |
985 | log_debug_errno(errno, "Failed to create a symlink '%s' to /run/systemd/journal/dev-log, ignoring: %m", devlog); | |
82d25240 | 986 | |
7f112f50 | 987 | NULSTR_FOREACH(d, devnodes) { |
16498617 | 988 | r = clone_device_node(d, temporary_mount, &can_mknod); |
37b22b3b | 989 | /* ENXIO means the *source* is not a device file, skip creation in that case */ |
af984e13 | 990 | if (r < 0 && r != -ENXIO) |
2b85f4e1 | 991 | goto fail; |
7f112f50 LP |
992 | } |
993 | ||
2e4a4fae YW |
994 | r = dev_setup(temporary_mount, UID_INVALID, GID_INVALID); |
995 | if (r < 0) | |
105a1a36 | 996 | log_debug_errno(r, "Failed to set up basic device tree at '%s', ignoring: %m", temporary_mount); |
7f112f50 | 997 | |
d73020f2 LP |
998 | /* Create the /dev directory if missing. It is more likely to be missing when the service is started |
999 | * with RootDirectory. This is consistent with mount units creating the mount points when missing. */ | |
34de407a | 1000 | (void) mkdir_p_label(mount_entry_path(m), 0755); |
ee818b89 | 1001 | |
9e5f8252 | 1002 | /* Unmount everything in old /dev */ |
2e4a4fae YW |
1003 | r = umount_recursive(mount_entry_path(m), 0); |
1004 | if (r < 0) | |
1005 | log_debug_errno(r, "Failed to unmount directories below '%s', ignoring: %m", mount_entry_path(m)); | |
1006 | ||
21935150 LP |
1007 | r = mount_nofollow_verbose(LOG_DEBUG, dev, mount_entry_path(m), NULL, MS_MOVE, NULL); |
1008 | if (r < 0) | |
2b85f4e1 | 1009 | goto fail; |
7f112f50 | 1010 | |
1019a48f LP |
1011 | (void) rmdir(dev); |
1012 | (void) rmdir(temporary_mount); | |
7f112f50 | 1013 | |
2b85f4e1 | 1014 | return 0; |
7f112f50 | 1015 | |
2b85f4e1 LP |
1016 | fail: |
1017 | if (devpts) | |
21935150 | 1018 | (void) umount_verbose(LOG_DEBUG, devpts, UMOUNT_NOFOLLOW); |
7f112f50 | 1019 | |
2b85f4e1 | 1020 | if (devshm) |
21935150 | 1021 | (void) umount_verbose(LOG_DEBUG, devshm, UMOUNT_NOFOLLOW); |
7f112f50 | 1022 | |
2b85f4e1 | 1023 | if (devhugepages) |
21935150 | 1024 | (void) umount_verbose(LOG_DEBUG, devhugepages, UMOUNT_NOFOLLOW); |
7f112f50 | 1025 | |
2b85f4e1 | 1026 | if (devmqueue) |
21935150 | 1027 | (void) umount_verbose(LOG_DEBUG, devmqueue, UMOUNT_NOFOLLOW); |
7f112f50 | 1028 | |
21935150 | 1029 | (void) umount_verbose(LOG_DEBUG, dev, UMOUNT_NOFOLLOW); |
1019a48f LP |
1030 | (void) rmdir(dev); |
1031 | (void) rmdir(temporary_mount); | |
7f112f50 | 1032 | |
2b85f4e1 | 1033 | return r; |
7f112f50 LP |
1034 | } |
1035 | ||
2a2969fd | 1036 | static int mount_bind_dev(const MountEntry *m) { |
5d997827 LP |
1037 | int r; |
1038 | ||
1039 | assert(m); | |
1040 | ||
d73020f2 LP |
1041 | /* Implements the little brother of mount_private_dev(): simply bind mounts the host's /dev into the |
1042 | * service's /dev. This is only used when RootDirectory= is set. */ | |
5d997827 | 1043 | |
645767d6 LP |
1044 | (void) mkdir_p_label(mount_entry_path(m), 0755); |
1045 | ||
5d997827 LP |
1046 | r = path_is_mount_point(mount_entry_path(m), NULL, 0); |
1047 | if (r < 0) | |
1048 | return log_debug_errno(r, "Unable to determine whether /dev is already mounted: %m"); | |
1049 | if (r > 0) /* make this a NOP if /dev is already a mount point */ | |
1050 | return 0; | |
1051 | ||
21935150 LP |
1052 | r = mount_nofollow_verbose(LOG_DEBUG, "/dev", mount_entry_path(m), NULL, MS_BIND|MS_REC, NULL); |
1053 | if (r < 0) | |
1054 | return r; | |
5d997827 LP |
1055 | |
1056 | return 1; | |
1057 | } | |
1058 | ||
277b269e YW |
1059 | static int mount_private_sysfs(const MountEntry *m) { |
1060 | const char *p = mount_entry_path(ASSERT_PTR(m)); | |
1061 | int r; | |
1062 | ||
1063 | (void) mkdir_p_label(p, 0755); | |
1064 | ||
1065 | r = remount_sysfs(p); | |
1066 | if (r < 0 && (ERRNO_IS_PRIVILEGE(r) || ERRNO_IS_NOT_SUPPORTED(r))) { | |
1067 | /* Running with an unprivileged user (PrivateUsers=yes), or the kernel seems old. Falling | |
1068 | * back to bind mount the host's version so that we get all child mounts of it, too. */ | |
1069 | ||
1070 | log_debug_errno(r, "Failed to remount sysfs on %s, falling back to bind mount: %m", p); | |
1071 | ||
1072 | (void) umount_recursive(p, 0); | |
1073 | ||
1074 | r = mount_nofollow_verbose(LOG_DEBUG, "/sys", p, NULL, MS_BIND|MS_REC, NULL); | |
1075 | } | |
1076 | if (r < 0) | |
1077 | return log_debug_errno(r, "Failed to remount sysfs on %s: %m", p); | |
1078 | ||
1079 | return 1; | |
1080 | } | |
1081 | ||
10028263 | 1082 | static int mount_bind_sysfs(const MountEntry *m) { |
5d997827 LP |
1083 | int r; |
1084 | ||
1085 | assert(m); | |
1086 | ||
645767d6 LP |
1087 | (void) mkdir_p_label(mount_entry_path(m), 0755); |
1088 | ||
5d997827 LP |
1089 | r = path_is_mount_point(mount_entry_path(m), NULL, 0); |
1090 | if (r < 0) | |
1091 | return log_debug_errno(r, "Unable to determine whether /sys is already mounted: %m"); | |
1092 | if (r > 0) /* make this a NOP if /sys is already a mount point */ | |
1093 | return 0; | |
1094 | ||
1095 | /* Bind mount the host's version so that we get all child mounts of it, too. */ | |
21935150 LP |
1096 | r = mount_nofollow_verbose(LOG_DEBUG, "/sys", mount_entry_path(m), NULL, MS_BIND|MS_REC, NULL); |
1097 | if (r < 0) | |
1098 | return r; | |
5d997827 LP |
1099 | |
1100 | return 1; | |
1101 | } | |
1102 | ||
4e399953 | 1103 | static int mount_procfs(const MountEntry *m, const NamespaceInfo *ns_info) { |
61f8a7bd | 1104 | _cleanup_free_ char *opts = NULL; |
4e399953 | 1105 | const char *entry_path; |
61f8a7bd | 1106 | int r, n; |
5d997827 LP |
1107 | |
1108 | assert(m); | |
4e399953 | 1109 | assert(ns_info); |
5d997827 | 1110 | |
4e399953 LP |
1111 | if (ns_info->protect_proc != PROTECT_PROC_DEFAULT || |
1112 | ns_info->proc_subset != PROC_SUBSET_ALL) { | |
4e399953 LP |
1113 | |
1114 | /* Starting with kernel 5.8 procfs' hidepid= logic is truly per-instance (previously it | |
1115 | * pretended to be per-instance but actually was per-namespace), hence let's make use of it | |
1116 | * if requested. To make sure this logic succeeds only on kernels where hidepid= is | |
1117 | * per-instance, we'll exclusively use the textual value for hidepid=, since support was | |
1118 | * added in the same commit: if it's supported it is thus also per-instance. */ | |
1119 | ||
1c265fcd | 1120 | const char *hpv = ns_info->protect_proc == PROTECT_PROC_DEFAULT ? |
7c76e181 ZJS |
1121 | "off" : |
1122 | protect_proc_to_string(ns_info->protect_proc); | |
1c265fcd DDM |
1123 | |
1124 | /* hidepid= support was added in 5.8, so we can use fsconfig()/fsopen() (which were added in | |
1125 | * 5.2) to check if hidepid= is supported. This avoids a noisy dmesg log by the kernel when | |
1126 | * trying to use hidepid= on systems where it isn't supported. The same applies for subset=. | |
1127 | * fsopen()/fsconfig() was also backported on some distros which allows us to detect | |
1128 | * hidepid=/subset= support in even more scenarios. */ | |
1129 | ||
117e7034 | 1130 | if (mount_option_supported("proc", "hidepid", hpv) != 0) { |
1c265fcd DDM |
1131 | opts = strjoin("hidepid=", hpv); |
1132 | if (!opts) | |
1133 | return -ENOMEM; | |
1134 | } | |
1135 | ||
117e7034 LP |
1136 | if (ns_info->proc_subset == PROC_SUBSET_PID && |
1137 | mount_option_supported("proc", "subset", "pid") != 0) | |
1c265fcd DDM |
1138 | if (!strextend_with_separator(&opts, ",", "subset=pid")) |
1139 | return -ENOMEM; | |
4e399953 LP |
1140 | } |
1141 | ||
61f8a7bd YW |
1142 | entry_path = mount_entry_path(m); |
1143 | (void) mkdir_p_label(entry_path, 0755); | |
1144 | ||
1145 | /* Mount a new instance, so that we get the one that matches our user namespace, if we are running in | |
1146 | * one. i.e we don't reuse existing mounts here under any condition, we want a new instance owned by | |
1147 | * our user namespace and with our hidepid= settings applied. Hence, let's get rid of everything | |
1148 | * mounted on /proc/ first. */ | |
1149 | ||
1150 | n = umount_recursive(entry_path, 0); | |
1151 | ||
1152 | r = mount_nofollow_verbose(LOG_DEBUG, "proc", entry_path, "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, opts); | |
1153 | if (r == -EINVAL && opts) | |
1154 | /* If this failed with EINVAL then this likely means the textual hidepid= stuff is | |
1155 | * not supported by the kernel, and thus the per-instance hidepid= neither, which | |
1156 | * means we really don't want to use it, since it would affect our host's /proc | |
1157 | * mount. Hence let's gracefully fallback to a classic, unrestricted version. */ | |
1158 | r = mount_nofollow_verbose(LOG_DEBUG, "proc", entry_path, "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL); | |
1159 | if (r == -EPERM) { | |
24ee0f9d | 1160 | /* When we do not have enough privileges to mount /proc, fallback to use existing /proc. */ |
61f8a7bd YW |
1161 | |
1162 | if (n > 0) | |
1163 | /* /proc or some of sub-mounts are umounted in the above. Refuse incomplete tree. | |
1164 | * Propagate the original error code returned by mount() in the above. */ | |
1165 | return -EPERM; | |
1166 | ||
1167 | r = path_is_mount_point(entry_path, NULL, 0); | |
1168 | if (r < 0) | |
1169 | return log_debug_errno(r, "Unable to determine whether /proc is already mounted: %m"); | |
ea63a260 LB |
1170 | if (r == 0) { |
1171 | /* We lack permissions to mount a new instance of /proc, and it is not already | |
1172 | * mounted. But we can access the host's, so as a final fallback bind-mount it to | |
1173 | * the destination, as most likely we are inside a user manager in an unprivileged | |
1174 | * user namespace. */ | |
1175 | r = mount_nofollow_verbose(LOG_DEBUG, "/proc", entry_path, NULL, MS_BIND|MS_REC, NULL); | |
1176 | if (r < 0) | |
1177 | return -EPERM; | |
1178 | } | |
ad74f28a YW |
1179 | } else if (r < 0) |
1180 | return r; | |
5d997827 LP |
1181 | |
1182 | return 1; | |
1183 | } | |
1184 | ||
2abd4e38 | 1185 | static int mount_tmpfs(const MountEntry *m) { |
df6b900a | 1186 | const char *entry_path, *inner_path; |
abad72be | 1187 | int r; |
abad72be | 1188 | |
6c47cd7d LP |
1189 | assert(m); |
1190 | ||
df6b900a | 1191 | entry_path = mount_entry_path(m); |
809ceb82 | 1192 | inner_path = mount_entry_unprefixed_path(m); |
df6b900a | 1193 | |
d73020f2 LP |
1194 | /* First, get rid of everything that is below if there is anything. Then, overmount with our new |
1195 | * tmpfs */ | |
6c47cd7d | 1196 | |
abad72be CG |
1197 | (void) mkdir_p_label(entry_path, 0755); |
1198 | (void) umount_recursive(entry_path, 0); | |
6c47cd7d | 1199 | |
21935150 LP |
1200 | r = mount_nofollow_verbose(LOG_DEBUG, "tmpfs", entry_path, "tmpfs", m->flags, mount_entry_options(m)); |
1201 | if (r < 0) | |
1202 | return r; | |
abad72be | 1203 | |
03bc11d1 | 1204 | r = label_fix_full(AT_FDCWD, entry_path, inner_path, 0); |
abad72be | 1205 | if (r < 0) |
df6b900a | 1206 | return log_debug_errno(r, "Failed to fix label of '%s' as '%s': %m", entry_path, inner_path); |
6c47cd7d LP |
1207 | |
1208 | return 1; | |
1209 | } | |
1210 | ||
94293d65 LB |
1211 | static int mount_run(const MountEntry *m) { |
1212 | int r; | |
1213 | ||
1214 | assert(m); | |
1215 | ||
1216 | r = path_is_mount_point(mount_entry_path(m), NULL, 0); | |
1217 | if (r < 0 && r != -ENOENT) | |
1218 | return log_debug_errno(r, "Unable to determine whether /run is already mounted: %m"); | |
1219 | if (r > 0) /* make this a NOP if /run is already a mount point */ | |
1220 | return 0; | |
1221 | ||
1222 | return mount_tmpfs(m); | |
1223 | } | |
1224 | ||
80271a44 XR |
1225 | static int mount_mqueuefs(const MountEntry *m) { |
1226 | int r; | |
1227 | const char *entry_path; | |
1228 | ||
1229 | assert(m); | |
1230 | ||
1231 | entry_path = mount_entry_path(m); | |
1232 | ||
1233 | (void) mkdir_p_label(entry_path, 0755); | |
1234 | (void) umount_recursive(entry_path, 0); | |
1235 | ||
1236 | r = mount_nofollow_verbose(LOG_DEBUG, "mqueue", entry_path, "mqueue", m->flags, mount_entry_options(m)); | |
1237 | if (r < 0) | |
1238 | return r; | |
1239 | ||
1240 | return 0; | |
1241 | } | |
1242 | ||
93f59701 LB |
1243 | static int mount_image(const MountEntry *m, const char *root_directory) { |
1244 | ||
1245 | _cleanup_free_ char *host_os_release_id = NULL, *host_os_release_version_id = NULL, | |
1246 | *host_os_release_sysext_level = NULL; | |
b3d13314 LB |
1247 | int r; |
1248 | ||
89e62e0b LP |
1249 | assert(m); |
1250 | ||
93f59701 LB |
1251 | if (m->mode == EXTENSION_IMAGES) { |
1252 | r = parse_os_release( | |
1253 | empty_to_root(root_directory), | |
1254 | "ID", &host_os_release_id, | |
1255 | "VERSION_ID", &host_os_release_version_id, | |
1256 | "SYSEXT_LEVEL", &host_os_release_sysext_level, | |
1257 | NULL); | |
1258 | if (r < 0) | |
1259 | return log_debug_errno(r, "Failed to acquire 'os-release' data of OS tree '%s': %m", empty_to_root(root_directory)); | |
78ab2b50 LB |
1260 | if (isempty(host_os_release_id)) |
1261 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "'ID' field not found or empty in 'os-release' data of OS tree '%s': %m", empty_to_root(root_directory)); | |
93f59701 LB |
1262 | } |
1263 | ||
1264 | r = verity_dissect_and_mount( | |
7c76e181 ZJS |
1265 | /* src_fd= */ -1, mount_entry_source(m), mount_entry_path(m), m->image_options, |
1266 | host_os_release_id, host_os_release_version_id, host_os_release_sysext_level, NULL); | |
b850a9b2 LB |
1267 | if (r == -ENOENT && m->ignore) |
1268 | return 0; | |
db4c8a25 LB |
1269 | if (r == -ESTALE && host_os_release_id) |
1270 | return log_error_errno(r, | |
1271 | "Failed to mount image %s, extension-release metadata does not match the lower layer's: ID=%s%s%s%s%s", | |
1272 | mount_entry_source(m), | |
1273 | host_os_release_id, | |
1274 | host_os_release_version_id ? " VERSION_ID=" : "", | |
1275 | strempty(host_os_release_version_id), | |
1276 | host_os_release_sysext_level ? " SYSEXT_LEVEL=" : "", | |
1277 | strempty(host_os_release_sysext_level)); | |
b3d13314 | 1278 | if (r < 0) |
4beda316 | 1279 | return log_debug_errno(r, "Failed to mount image %s on %s: %m", mount_entry_source(m), mount_entry_path(m)); |
b3d13314 LB |
1280 | |
1281 | return 1; | |
1282 | } | |
1283 | ||
93f59701 LB |
1284 | static int mount_overlay(const MountEntry *m) { |
1285 | const char *options; | |
1286 | int r; | |
1287 | ||
1288 | assert(m); | |
1289 | ||
1290 | options = strjoina("lowerdir=", mount_entry_options(m)); | |
1291 | ||
1292 | (void) mkdir_p_label(mount_entry_path(m), 0755); | |
1293 | ||
1294 | r = mount_nofollow_verbose(LOG_DEBUG, "overlay", mount_entry_path(m), "overlay", MS_RDONLY, options); | |
1295 | if (r == -ENOENT && m->ignore) | |
1296 | return 0; | |
1297 | if (r < 0) | |
1298 | return r; | |
1299 | ||
1300 | return 1; | |
1301 | } | |
1302 | ||
088696fe | 1303 | static int follow_symlink( |
d2d6c096 | 1304 | const char *root_directory, |
088696fe | 1305 | MountEntry *m) { |
d2d6c096 | 1306 | |
088696fe | 1307 | _cleanup_free_ char *target = NULL; |
8fceda93 LP |
1308 | int r; |
1309 | ||
088696fe LP |
1310 | /* Let's chase symlinks, but only one step at a time. That's because depending where the symlink points we |
1311 | * might need to change the order in which we mount stuff. Hence: let's normalize piecemeal, and do one step at | |
1312 | * a time by specifying CHASE_STEP. This function returns 0 if we resolved one step, and > 0 if we reached the | |
1313 | * end and already have a fully normalized name. */ | |
8fceda93 | 1314 | |
f461a28d | 1315 | r = chase(mount_entry_path(m), root_directory, CHASE_STEP|CHASE_NONEXISTENT, &target, NULL); |
088696fe LP |
1316 | if (r < 0) |
1317 | return log_debug_errno(r, "Failed to chase symlinks '%s': %m", mount_entry_path(m)); | |
1318 | if (r > 0) /* Reached the end, nothing more to resolve */ | |
1319 | return 1; | |
8fceda93 | 1320 | |
f461a28d | 1321 | if (m->n_followed >= CHASE_MAX) /* put a boundary on things */ |
baaa35ad ZJS |
1322 | return log_debug_errno(SYNTHETIC_ERRNO(ELOOP), |
1323 | "Symlink loop on '%s'.", | |
1324 | mount_entry_path(m)); | |
8fceda93 | 1325 | |
e2341b6b DT |
1326 | log_debug("Followed mount entry path symlink %s %s %s.", |
1327 | mount_entry_path(m), special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), target); | |
8fceda93 | 1328 | |
809ceb82 | 1329 | mount_entry_consume_prefix(m, TAKE_PTR(target)); |
8fceda93 | 1330 | |
088696fe LP |
1331 | m->n_followed ++; |
1332 | ||
1333 | return 0; | |
8fceda93 LP |
1334 | } |
1335 | ||
82fb2da2 | 1336 | static int apply_one_mount( |
8fceda93 | 1337 | const char *root_directory, |
4e399953 LP |
1338 | MountEntry *m, |
1339 | const NamespaceInfo *ns_info) { | |
ac0930c8 | 1340 | |
e5f10caf | 1341 | _cleanup_free_ char *inaccessible = NULL; |
a227a4be | 1342 | bool rbind = true, make = false; |
15ae422b | 1343 | const char *what; |
15ae422b | 1344 | int r; |
15ae422b | 1345 | |
c17ec25e | 1346 | assert(m); |
4e399953 | 1347 | assert(ns_info); |
15ae422b | 1348 | |
34de407a | 1349 | log_debug("Applying namespace mount on %s", mount_entry_path(m)); |
fe3c2583 | 1350 | |
c17ec25e | 1351 | switch (m->mode) { |
15ae422b | 1352 | |
160cfdbe | 1353 | case INACCESSIBLE: { |
e5f10caf AZ |
1354 | _cleanup_free_ char *tmp = NULL; |
1355 | const char *runtime_dir; | |
160cfdbe | 1356 | struct stat target; |
6d313367 LP |
1357 | |
1358 | /* First, get rid of everything that is below if there | |
1359 | * is anything... Then, overmount it with an | |
c4b41707 | 1360 | * inaccessible path. */ |
34de407a | 1361 | (void) umount_recursive(mount_entry_path(m), 0); |
6d313367 | 1362 | |
088696fe LP |
1363 | if (lstat(mount_entry_path(m), &target) < 0) { |
1364 | if (errno == ENOENT && m->ignore) | |
1365 | return 0; | |
1366 | ||
cbc056c8 ZJS |
1367 | return log_debug_errno(errno, "Failed to lstat() %s to determine what to mount over it: %m", |
1368 | mount_entry_path(m)); | |
088696fe | 1369 | } |
15ae422b | 1370 | |
e5f10caf | 1371 | if (geteuid() == 0) |
48b747fa | 1372 | runtime_dir = "/run"; |
e5f10caf | 1373 | else { |
48b747fa LP |
1374 | if (asprintf(&tmp, "/run/user/" UID_FMT, geteuid()) < 0) |
1375 | return -ENOMEM; | |
e5f10caf AZ |
1376 | |
1377 | runtime_dir = tmp; | |
1378 | } | |
1379 | ||
1380 | r = mode_to_inaccessible_node(runtime_dir, target.st_mode, &inaccessible); | |
1381 | if (r < 0) | |
baaa35ad ZJS |
1382 | return log_debug_errno(SYNTHETIC_ERRNO(ELOOP), |
1383 | "File type not supported for inaccessible mounts. Note that symlinks are not allowed"); | |
e5f10caf | 1384 | what = inaccessible; |
c4b41707 | 1385 | break; |
160cfdbe | 1386 | } |
fe3c2583 | 1387 | |
15ae422b | 1388 | case READONLY: |
15ae422b | 1389 | case READWRITE: |
1e05071d | 1390 | case READWRITE_IMPLICIT: |
ddc155b2 TM |
1391 | case EXEC: |
1392 | case NOEXEC: | |
8fceda93 | 1393 | r = path_is_mount_point(mount_entry_path(m), root_directory, 0); |
088696fe LP |
1394 | if (r == -ENOENT && m->ignore) |
1395 | return 0; | |
d944dc95 | 1396 | if (r < 0) |
cbc056c8 ZJS |
1397 | return log_debug_errno(r, "Failed to determine whether %s is already a mount point: %m", |
1398 | mount_entry_path(m)); | |
1399 | if (r > 0) /* Nothing to do here, it is already a mount. We just later toggle the MS_RDONLY | |
ddc155b2 | 1400 | * and MS_NOEXEC bits for the mount point if needed. */ |
6b7c9f8b | 1401 | return 0; |
6b7c9f8b | 1402 | /* This isn't a mount point yet, let's make it one. */ |
34de407a | 1403 | what = mount_entry_path(m); |
6b7c9f8b | 1404 | break; |
15ae422b | 1405 | |
a07b9926 LB |
1406 | case EXTENSION_DIRECTORIES: { |
1407 | _cleanup_free_ char *host_os_release_id = NULL, *host_os_release_version_id = NULL, | |
1408 | *host_os_release_sysext_level = NULL, *extension_name = NULL; | |
1409 | _cleanup_strv_free_ char **extension_release = NULL; | |
1410 | ||
1411 | r = path_extract_filename(mount_entry_source(m), &extension_name); | |
1412 | if (r < 0) | |
1413 | return log_debug_errno(r, "Failed to extract extension name from %s: %m", mount_entry_source(m)); | |
1414 | ||
1415 | r = parse_os_release( | |
1416 | empty_to_root(root_directory), | |
1417 | "ID", &host_os_release_id, | |
1418 | "VERSION_ID", &host_os_release_version_id, | |
1419 | "SYSEXT_LEVEL", &host_os_release_sysext_level, | |
1420 | NULL); | |
1421 | if (r < 0) | |
1422 | return log_debug_errno(r, "Failed to acquire 'os-release' data of OS tree '%s': %m", empty_to_root(root_directory)); | |
1423 | if (isempty(host_os_release_id)) | |
1424 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "'ID' field not found or empty in 'os-release' data of OS tree '%s': %m", empty_to_root(root_directory)); | |
1425 | ||
b60e0f57 | 1426 | r = load_extension_release_pairs(mount_entry_source(m), IMAGE_SYSEXT, extension_name, /* relax_extension_release_check= */ false, &extension_release); |
a07b9926 LB |
1427 | if (r == -ENOENT && m->ignore) |
1428 | return 0; | |
1429 | if (r < 0) | |
1430 | return log_debug_errno(r, "Failed to parse directory %s extension-release metadata: %m", extension_name); | |
1431 | ||
1432 | r = extension_release_validate( | |
1433 | extension_name, | |
1434 | host_os_release_id, | |
1435 | host_os_release_version_id, | |
1436 | host_os_release_sysext_level, | |
1437 | /* host_sysext_scope */ NULL, /* Leave empty, we need to accept both system and portable */ | |
1438 | extension_release); | |
1439 | if (r == 0) | |
1440 | return log_debug_errno(SYNTHETIC_ERRNO(ESTALE), "Directory %s extension-release metadata does not match the root's", extension_name); | |
1441 | if (r < 0) | |
1442 | return log_debug_errno(r, "Failed to compare directory %s extension-release metadata with the root's os-release: %m", extension_name); | |
1443 | ||
1444 | _fallthrough_; | |
1445 | } | |
1446 | ||
d2d6c096 LP |
1447 | case BIND_MOUNT: |
1448 | rbind = false; | |
d2d6c096 | 1449 | |
4831981d | 1450 | _fallthrough_; |
088696fe LP |
1451 | case BIND_MOUNT_RECURSIVE: { |
1452 | _cleanup_free_ char *chased = NULL; | |
5d997827 | 1453 | |
cbc056c8 ZJS |
1454 | /* Since mount() will always follow symlinks we chase the symlinks on our own first. Note |
1455 | * that bind mount source paths are always relative to the host root, hence we pass NULL as | |
f461a28d | 1456 | * root directory to chase() here. */ |
088696fe | 1457 | |
f461a28d | 1458 | r = chase(mount_entry_source(m), NULL, CHASE_TRAIL_SLASH, &chased, NULL); |
088696fe LP |
1459 | if (r == -ENOENT && m->ignore) { |
1460 | log_debug_errno(r, "Path %s does not exist, ignoring.", mount_entry_source(m)); | |
1461 | return 0; | |
1462 | } | |
1463 | if (r < 0) | |
1464 | return log_debug_errno(r, "Failed to follow symlinks on %s: %m", mount_entry_source(m)); | |
1465 | ||
e2341b6b DT |
1466 | log_debug("Followed source symlinks %s %s %s.", |
1467 | mount_entry_source(m), special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), chased); | |
088696fe LP |
1468 | |
1469 | free_and_replace(m->source_malloc, chased); | |
d2d6c096 LP |
1470 | |
1471 | what = mount_entry_source(m); | |
a227a4be | 1472 | make = true; |
d2d6c096 | 1473 | break; |
088696fe | 1474 | } |
d2d6c096 | 1475 | |
6c47cd7d | 1476 | case EMPTY_DIR: |
2abd4e38 YW |
1477 | case TMPFS: |
1478 | return mount_tmpfs(m); | |
6c47cd7d | 1479 | |
ac0930c8 | 1480 | case PRIVATE_TMP: |
56a13a49 | 1481 | case PRIVATE_TMP_READONLY: |
89bd586c | 1482 | what = mount_entry_source(m); |
a227a4be | 1483 | make = true; |
15ae422b | 1484 | break; |
e364ad06 | 1485 | |
d6797c92 | 1486 | case PRIVATE_DEV: |
5d997827 LP |
1487 | return mount_private_dev(m); |
1488 | ||
1489 | case BIND_DEV: | |
1490 | return mount_bind_dev(m); | |
1491 | ||
277b269e YW |
1492 | case PRIVATE_SYSFS: |
1493 | return mount_private_sysfs(m); | |
1494 | ||
10028263 YW |
1495 | case BIND_SYSFS: |
1496 | return mount_bind_sysfs(m); | |
5d997827 LP |
1497 | |
1498 | case PROCFS: | |
4e399953 | 1499 | return mount_procfs(m, ns_info); |
d6797c92 | 1500 | |
94293d65 LB |
1501 | case RUN: |
1502 | return mount_run(m); | |
1503 | ||
80271a44 XR |
1504 | case MQUEUEFS: |
1505 | return mount_mqueuefs(m); | |
1506 | ||
b3d13314 | 1507 | case MOUNT_IMAGES: |
93f59701 LB |
1508 | return mount_image(m, NULL); |
1509 | ||
1510 | case EXTENSION_IMAGES: | |
1511 | return mount_image(m, root_directory); | |
1512 | ||
1513 | case OVERLAY_MOUNT: | |
1514 | return mount_overlay(m); | |
b3d13314 | 1515 | |
e364ad06 | 1516 | default: |
04499a70 | 1517 | assert_not_reached(); |
15ae422b LP |
1518 | } |
1519 | ||
ac0930c8 | 1520 | assert(what); |
15ae422b | 1521 | |
21935150 LP |
1522 | r = mount_nofollow_verbose(LOG_DEBUG, what, mount_entry_path(m), NULL, MS_BIND|(rbind ? MS_REC : 0), NULL); |
1523 | if (r < 0) { | |
a227a4be | 1524 | bool try_again = false; |
a227a4be LP |
1525 | |
1526 | if (r == -ENOENT && make) { | |
8bab8029 | 1527 | int q; |
a227a4be | 1528 | |
cbc056c8 ZJS |
1529 | /* Hmm, either the source or the destination are missing. Let's see if we can create |
1530 | the destination, then try again. */ | |
a227a4be | 1531 | |
8bab8029 | 1532 | (void) mkdir_parents(mount_entry_path(m), 0755); |
a227a4be | 1533 | |
8bab8029 | 1534 | q = make_mount_point_inode_from_path(what, mount_entry_path(m), 0755); |
9d6d4c30 | 1535 | if (q < 0 && q != -EEXIST) |
8bab8029 LB |
1536 | log_error_errno(q, "Failed to create destination mount point node '%s': %m", |
1537 | mount_entry_path(m)); | |
1538 | else | |
1539 | try_again = true; | |
a227a4be LP |
1540 | } |
1541 | ||
21935150 LP |
1542 | if (try_again) |
1543 | r = mount_nofollow_verbose(LOG_DEBUG, what, mount_entry_path(m), NULL, MS_BIND|(rbind ? MS_REC : 0), NULL); | |
a227a4be | 1544 | if (r < 0) |
5dc60faa | 1545 | return log_error_errno(r, "Failed to mount %s to %s: %m", what, mount_entry_path(m)); |
a227a4be | 1546 | } |
6b7c9f8b | 1547 | |
34de407a | 1548 | log_debug("Successfully mounted %s to %s", what, mount_entry_path(m)); |
6b7c9f8b | 1549 | return 0; |
ac0930c8 | 1550 | } |
15ae422b | 1551 | |
6b000af4 | 1552 | static int make_read_only(const MountEntry *m, char **deny_list, FILE *proc_self_mountinfo) { |
9ce4e4b0 | 1553 | unsigned long new_flags = 0, flags_mask = 0; |
57ccd9f6 YW |
1554 | bool submounts; |
1555 | int r; | |
15ae422b | 1556 | |
c17ec25e | 1557 | assert(m); |
ac9de0b3 | 1558 | assert(proc_self_mountinfo); |
ac0930c8 | 1559 | |
9ce4e4b0 LP |
1560 | if (mount_entry_read_only(m) || m->mode == PRIVATE_DEV) { |
1561 | new_flags |= MS_RDONLY; | |
1562 | flags_mask |= MS_RDONLY; | |
1563 | } | |
1564 | ||
1565 | if (m->nosuid) { | |
1566 | new_flags |= MS_NOSUID; | |
1567 | flags_mask |= MS_NOSUID; | |
1568 | } | |
1569 | ||
1570 | if (flags_mask == 0) /* No Change? */ | |
6b7c9f8b LP |
1571 | return 0; |
1572 | ||
9ce4e4b0 LP |
1573 | /* We generally apply these changes recursively, except for /dev, and the cases we know there's |
1574 | * nothing further down. Set /dev readonly, but not submounts like /dev/shm. Also, we only set the | |
1575 | * per-mount read-only flag. We can't set it on the superblock, if we are inside a user namespace | |
1576 | * and running Linux <= 4.17. */ | |
1577 | submounts = | |
1578 | mount_entry_read_only(m) && | |
1579 | !IN_SET(m->mode, EMPTY_DIR, TMPFS); | |
1580 | if (submounts) | |
6b000af4 | 1581 | r = bind_remount_recursive_with_mountinfo(mount_entry_path(m), new_flags, flags_mask, deny_list, proc_self_mountinfo); |
9ce4e4b0 | 1582 | else |
7cce68e1 | 1583 | r = bind_remount_one_with_mountinfo(mount_entry_path(m), new_flags, flags_mask, proc_self_mountinfo); |
9ce4e4b0 | 1584 | |
ddc155b2 | 1585 | /* Note that we only turn on the MS_RDONLY flag here, we never turn it off. Something that was marked |
867189b5 LP |
1586 | * read-only already stays this way. This improves compatibility with container managers, where we |
1587 | * won't attempt to undo read-only mounts already applied. */ | |
ac0930c8 | 1588 | |
8fceda93 | 1589 | if (r == -ENOENT && m->ignore) |
867189b5 | 1590 | return 0; |
763a260a | 1591 | if (r < 0) |
9ce4e4b0 | 1592 | return log_debug_errno(r, "Failed to re-mount '%s'%s: %m", mount_entry_path(m), |
763a260a | 1593 | submounts ? " and its submounts" : ""); |
763a260a | 1594 | return 0; |
d944dc95 LP |
1595 | } |
1596 | ||
ddc155b2 TM |
1597 | static int make_noexec(const MountEntry *m, char **deny_list, FILE *proc_self_mountinfo) { |
1598 | unsigned long new_flags = 0, flags_mask = 0; | |
57ccd9f6 YW |
1599 | bool submounts; |
1600 | int r; | |
ddc155b2 TM |
1601 | |
1602 | assert(m); | |
1603 | assert(proc_self_mountinfo); | |
1604 | ||
1605 | if (mount_entry_noexec(m)) { | |
1606 | new_flags |= MS_NOEXEC; | |
1607 | flags_mask |= MS_NOEXEC; | |
1608 | } else if (mount_entry_exec(m)) { | |
1609 | new_flags &= ~MS_NOEXEC; | |
1610 | flags_mask |= MS_NOEXEC; | |
1611 | } | |
1612 | ||
1613 | if (flags_mask == 0) /* No Change? */ | |
1614 | return 0; | |
1615 | ||
1616 | submounts = !IN_SET(m->mode, EMPTY_DIR, TMPFS); | |
1617 | ||
1618 | if (submounts) | |
1619 | r = bind_remount_recursive_with_mountinfo(mount_entry_path(m), new_flags, flags_mask, deny_list, proc_self_mountinfo); | |
1620 | else | |
1621 | r = bind_remount_one_with_mountinfo(mount_entry_path(m), new_flags, flags_mask, proc_self_mountinfo); | |
1622 | ||
1623 | if (r == -ENOENT && m->ignore) | |
1624 | return 0; | |
1625 | if (r < 0) | |
1626 | return log_debug_errno(r, "Failed to re-mount '%s'%s: %m", mount_entry_path(m), | |
1627 | submounts ? " and its submounts" : ""); | |
1628 | return 0; | |
1629 | } | |
1630 | ||
6720e356 | 1631 | static int make_nosuid(const MountEntry *m, FILE *proc_self_mountinfo) { |
57ccd9f6 YW |
1632 | bool submounts; |
1633 | int r; | |
6720e356 YW |
1634 | |
1635 | assert(m); | |
1636 | assert(proc_self_mountinfo); | |
1637 | ||
1638 | submounts = !IN_SET(m->mode, EMPTY_DIR, TMPFS); | |
1639 | ||
1640 | if (submounts) | |
1641 | r = bind_remount_recursive_with_mountinfo(mount_entry_path(m), MS_NOSUID, MS_NOSUID, NULL, proc_self_mountinfo); | |
1642 | else | |
1643 | r = bind_remount_one_with_mountinfo(mount_entry_path(m), MS_NOSUID, MS_NOSUID, proc_self_mountinfo); | |
1644 | if (r == -ENOENT && m->ignore) | |
1645 | return 0; | |
1646 | if (r < 0) | |
1647 | return log_debug_errno(r, "Failed to re-mount '%s'%s: %m", mount_entry_path(m), | |
1648 | submounts ? " and its submounts" : ""); | |
1649 | return 0; | |
1650 | } | |
1651 | ||
9b68367b | 1652 | static bool namespace_info_mount_apivfs(const NamespaceInfo *ns_info) { |
5d997827 LP |
1653 | assert(ns_info); |
1654 | ||
9c988f93 DH |
1655 | /* |
1656 | * ProtectControlGroups= and ProtectKernelTunables= imply MountAPIVFS=, | |
1657 | * since to protect the API VFS mounts, they need to be around in the | |
9b68367b | 1658 | * first place... |
9c988f93 | 1659 | */ |
5d997827 | 1660 | |
9b68367b YW |
1661 | return ns_info->mount_apivfs || |
1662 | ns_info->protect_control_groups || | |
4e399953 LP |
1663 | ns_info->protect_kernel_tunables || |
1664 | ns_info->protect_proc != PROTECT_PROC_DEFAULT || | |
1665 | ns_info->proc_subset != PROC_SUBSET_ALL; | |
5d997827 LP |
1666 | } |
1667 | ||
da6053d0 | 1668 | static size_t namespace_calculate_mounts( |
bb0ff3fb | 1669 | const NamespaceInfo *ns_info, |
2652c6c1 DH |
1670 | char** read_write_paths, |
1671 | char** read_only_paths, | |
1672 | char** inaccessible_paths, | |
ddc155b2 TM |
1673 | char** exec_paths, |
1674 | char** no_exec_paths, | |
6c47cd7d | 1675 | char** empty_directories, |
da6053d0 LP |
1676 | size_t n_bind_mounts, |
1677 | size_t n_temporary_filesystems, | |
b3d13314 | 1678 | size_t n_mount_images, |
93f59701 | 1679 | size_t n_extension_images, |
a07b9926 | 1680 | size_t n_extension_directories, |
93f59701 | 1681 | size_t n_hierarchies, |
2652c6c1 DH |
1682 | const char* tmp_dir, |
1683 | const char* var_tmp_dir, | |
bbb4e7f3 | 1684 | const char *creds_path, |
5e8deb94 | 1685 | const char* log_namespace, |
3bdc25a4 LP |
1686 | bool setup_propagate, |
1687 | const char* notify_socket) { | |
2652c6c1 | 1688 | |
da6053d0 LP |
1689 | size_t protect_home_cnt; |
1690 | size_t protect_system_cnt = | |
52b3d652 | 1691 | (ns_info->protect_system == PROTECT_SYSTEM_STRICT ? |
f471b2af | 1692 | ELEMENTSOF(protect_system_strict_table) : |
52b3d652 | 1693 | ((ns_info->protect_system == PROTECT_SYSTEM_FULL) ? |
f471b2af | 1694 | ELEMENTSOF(protect_system_full_table) : |
52b3d652 | 1695 | ((ns_info->protect_system == PROTECT_SYSTEM_YES) ? |
f471b2af DH |
1696 | ELEMENTSOF(protect_system_yes_table) : 0))); |
1697 | ||
b6c432ca | 1698 | protect_home_cnt = |
52b3d652 | 1699 | (ns_info->protect_home == PROTECT_HOME_YES ? |
b6c432ca | 1700 | ELEMENTSOF(protect_home_yes_table) : |
52b3d652 | 1701 | ((ns_info->protect_home == PROTECT_HOME_READ_ONLY) ? |
e4da7d8c | 1702 | ELEMENTSOF(protect_home_read_only_table) : |
52b3d652 | 1703 | ((ns_info->protect_home == PROTECT_HOME_TMPFS) ? |
e4da7d8c | 1704 | ELEMENTSOF(protect_home_tmpfs_table) : 0))); |
b6c432ca | 1705 | |
2652c6c1 DH |
1706 | return !!tmp_dir + !!var_tmp_dir + |
1707 | strv_length(read_write_paths) + | |
1708 | strv_length(read_only_paths) + | |
1709 | strv_length(inaccessible_paths) + | |
ddc155b2 TM |
1710 | strv_length(exec_paths) + |
1711 | strv_length(no_exec_paths) + | |
6c47cd7d | 1712 | strv_length(empty_directories) + |
d2d6c096 | 1713 | n_bind_mounts + |
b3d13314 | 1714 | n_mount_images + |
a07b9926 | 1715 | (n_extension_images > 0 || n_extension_directories > 0 ? /* Mount each image and directory plus an overlay per hierarchy */ |
7c76e181 | 1716 | n_hierarchies + n_extension_images + n_extension_directories: 0) + |
2abd4e38 | 1717 | n_temporary_filesystems + |
c575770b | 1718 | ns_info->private_dev + |
788e7201 TM |
1719 | (ns_info->protect_kernel_tunables ? |
1720 | ELEMENTSOF(protect_kernel_tunables_proc_table) + ELEMENTSOF(protect_kernel_tunables_sys_table) : 0) + | |
c575770b | 1721 | (ns_info->protect_kernel_modules ? ELEMENTSOF(protect_kernel_modules_table) : 0) + |
788e7201 TM |
1722 | (ns_info->protect_kernel_logs ? |
1723 | ELEMENTSOF(protect_kernel_logs_proc_table) + ELEMENTSOF(protect_kernel_logs_dev_table) : 0) + | |
94a7b275 | 1724 | (ns_info->protect_control_groups ? 1 : 0) + |
5d997827 | 1725 | protect_home_cnt + protect_system_cnt + |
aecd5ac6 | 1726 | (ns_info->protect_hostname ? 2 : 0) + |
91dd5f7c | 1727 | (namespace_info_mount_apivfs(ns_info) ? ELEMENTSOF(apivfs_table) : 0) + |
bbb4e7f3 | 1728 | (creds_path ? 2 : 1) + |
3bdc25a4 LP |
1729 | !!log_namespace + |
1730 | setup_propagate + /* /run/systemd/incoming */ | |
80271a44 | 1731 | !!notify_socket + |
c2da3bf2 | 1732 | ns_info->private_network + /* /sys */ |
80271a44 | 1733 | ns_info->private_ipc; /* /dev/mqueue */ |
2652c6c1 DH |
1734 | } |
1735 | ||
fbf90c0d CB |
1736 | /* Walk all mount entries and dropping any unused mounts. This affects all |
1737 | * mounts: | |
1738 | * - that are implicitly protected by a path that has been rendered inaccessible | |
1739 | * - whose immediate parent requests the same protection mode as the mount itself | |
1740 | * - that are outside of the relevant root directory | |
1741 | * - which are duplicates | |
1742 | */ | |
1743 | static void drop_unused_mounts(const char *root_directory, MountEntry *mounts, size_t *n_mounts) { | |
9b68367b | 1744 | assert(root_directory); |
f8b64b57 LP |
1745 | assert(n_mounts); |
1746 | assert(mounts || *n_mounts == 0); | |
1747 | ||
93bab288 | 1748 | typesafe_qsort(mounts, *n_mounts, mount_path_compare); |
f8b64b57 LP |
1749 | |
1750 | drop_duplicates(mounts, n_mounts); | |
1751 | drop_outside_root(root_directory, mounts, n_mounts); | |
1752 | drop_inaccessible(mounts, n_mounts); | |
1753 | drop_nop(mounts, n_mounts); | |
1754 | } | |
1755 | ||
df61e79a | 1756 | static int create_symlinks_from_tuples(const char *root, char **strv_symlinks) { |
df61e79a LB |
1757 | int r; |
1758 | ||
1759 | STRV_FOREACH_PAIR(src, dst, strv_symlinks) { | |
1760 | _cleanup_free_ char *src_abs = NULL, *dst_abs = NULL; | |
1761 | ||
1762 | src_abs = path_join(root, *src); | |
1763 | dst_abs = path_join(root, *dst); | |
1764 | if (!src_abs || !dst_abs) | |
1765 | return -ENOMEM; | |
1766 | ||
1767 | r = mkdir_parents_label(dst_abs, 0755); | |
1768 | if (r < 0) | |
1769 | return r; | |
1770 | ||
1771 | r = symlink_idempotent(src_abs, dst_abs, true); | |
1772 | if (r < 0) | |
1773 | return r; | |
1774 | } | |
1775 | ||
1776 | return 0; | |
1777 | } | |
1778 | ||
82fb2da2 LB |
1779 | static int apply_mounts( |
1780 | const char *root, | |
1781 | const NamespaceInfo *ns_info, | |
1782 | MountEntry *mounts, | |
1783 | size_t *n_mounts, | |
df61e79a | 1784 | char **exec_dir_symlinks, |
82fb2da2 LB |
1785 | char **error_path) { |
1786 | ||
1787 | _cleanup_fclose_ FILE *proc_self_mountinfo = NULL; | |
1788 | _cleanup_free_ char **deny_list = NULL; | |
82fb2da2 LB |
1789 | int r; |
1790 | ||
1791 | if (n_mounts == 0) /* Shortcut: nothing to do */ | |
1792 | return 0; | |
1793 | ||
1794 | assert(root); | |
1795 | assert(mounts); | |
1796 | assert(n_mounts); | |
1797 | ||
1798 | /* Open /proc/self/mountinfo now as it may become unavailable if we mount anything on top of | |
1799 | * /proc. For example, this is the case with the option: 'InaccessiblePaths=/proc'. */ | |
1800 | proc_self_mountinfo = fopen("/proc/self/mountinfo", "re"); | |
1801 | if (!proc_self_mountinfo) { | |
d60e3b40 LP |
1802 | r = -errno; |
1803 | ||
82fb2da2 LB |
1804 | if (error_path) |
1805 | *error_path = strdup("/proc/self/mountinfo"); | |
d60e3b40 LP |
1806 | |
1807 | return log_debug_errno(r, "Failed to open /proc/self/mountinfo: %m"); | |
82fb2da2 LB |
1808 | } |
1809 | ||
1810 | /* First round, establish all mounts we need */ | |
1811 | for (;;) { | |
1812 | bool again = false; | |
1813 | ||
1814 | for (MountEntry *m = mounts; m < mounts + *n_mounts; ++m) { | |
1815 | ||
1816 | if (m->applied) | |
1817 | continue; | |
1818 | ||
a07b9926 LB |
1819 | /* ExtensionImages/Directories are first opened in the propagate directory, not in the root_directory */ |
1820 | r = follow_symlink(!IN_SET(m->mode, EXTENSION_IMAGES, EXTENSION_DIRECTORIES) ? root : NULL, m); | |
82fb2da2 LB |
1821 | if (r < 0) { |
1822 | if (error_path && mount_entry_path(m)) | |
1823 | *error_path = strdup(mount_entry_path(m)); | |
1824 | return r; | |
1825 | } | |
1826 | if (r == 0) { | |
1827 | /* We hit a symlinked mount point. The entry got rewritten and might | |
1828 | * point to a very different place now. Let's normalize the changed | |
1829 | * list, and start from the beginning. After all to mount the entry | |
1830 | * at the new location we might need some other mounts first */ | |
1831 | again = true; | |
1832 | break; | |
1833 | } | |
1834 | ||
1835 | r = apply_one_mount(root, m, ns_info); | |
1836 | if (r < 0) { | |
1837 | if (error_path && mount_entry_path(m)) | |
1838 | *error_path = strdup(mount_entry_path(m)); | |
1839 | return r; | |
1840 | } | |
1841 | ||
1842 | m->applied = true; | |
1843 | } | |
1844 | ||
1845 | if (!again) | |
1846 | break; | |
1847 | ||
fbf90c0d | 1848 | drop_unused_mounts(root, mounts, n_mounts); |
82fb2da2 LB |
1849 | } |
1850 | ||
df61e79a LB |
1851 | /* Now that all filesystems have been set up, but before the |
1852 | * read-only switches are flipped, create the exec dirs symlinks. | |
1853 | * Note that when /var/lib is not empty/tmpfs, these symlinks will already | |
1854 | * exist, which means this will be a no-op. */ | |
1855 | r = create_symlinks_from_tuples(root, exec_dir_symlinks); | |
1856 | if (r < 0) | |
6d7c999a | 1857 | return log_debug_errno(r, "Failed to set up ExecDirectories symlinks inside mount namespace: %m"); |
df61e79a | 1858 | |
82fb2da2 LB |
1859 | /* Create a deny list we can pass to bind_mount_recursive() */ |
1860 | deny_list = new(char*, (*n_mounts)+1); | |
1861 | if (!deny_list) | |
1862 | return -ENOMEM; | |
8794e6db | 1863 | for (size_t j = 0; j < *n_mounts; j++) |
82fb2da2 | 1864 | deny_list[j] = (char*) mount_entry_path(mounts+j); |
8794e6db | 1865 | deny_list[*n_mounts] = NULL; |
82fb2da2 LB |
1866 | |
1867 | /* Second round, flip the ro bits if necessary. */ | |
1868 | for (MountEntry *m = mounts; m < mounts + *n_mounts; ++m) { | |
1869 | r = make_read_only(m, deny_list, proc_self_mountinfo); | |
1870 | if (r < 0) { | |
1871 | if (error_path && mount_entry_path(m)) | |
1872 | *error_path = strdup(mount_entry_path(m)); | |
1873 | return r; | |
1874 | } | |
1875 | } | |
1876 | ||
1877 | /* Third round, flip the noexec bits with a simplified deny list. */ | |
8794e6db | 1878 | for (size_t j = 0; j < *n_mounts; j++) |
82fb2da2 LB |
1879 | if (IN_SET((mounts+j)->mode, EXEC, NOEXEC)) |
1880 | deny_list[j] = (char*) mount_entry_path(mounts+j); | |
8794e6db | 1881 | deny_list[*n_mounts] = NULL; |
82fb2da2 LB |
1882 | |
1883 | for (MountEntry *m = mounts; m < mounts + *n_mounts; ++m) { | |
1884 | r = make_noexec(m, deny_list, proc_self_mountinfo); | |
1885 | if (r < 0) { | |
1886 | if (error_path && mount_entry_path(m)) | |
1887 | *error_path = strdup(mount_entry_path(m)); | |
1888 | return r; | |
1889 | } | |
1890 | } | |
1891 | ||
6720e356 YW |
1892 | /* Fourth round, flip the nosuid bits without a deny list. */ |
1893 | if (ns_info->mount_nosuid) | |
1894 | for (MountEntry *m = mounts; m < mounts + *n_mounts; ++m) { | |
1895 | r = make_nosuid(m, proc_self_mountinfo); | |
1896 | if (r < 0) { | |
1897 | if (error_path && mount_entry_path(m)) | |
1898 | *error_path = strdup(mount_entry_path(m)); | |
1899 | return r; | |
1900 | } | |
1901 | } | |
1902 | ||
82fb2da2 LB |
1903 | return 1; |
1904 | } | |
1905 | ||
c8c535d5 LP |
1906 | static bool root_read_only( |
1907 | char **read_only_paths, | |
1908 | ProtectSystem protect_system) { | |
1909 | ||
1910 | /* Determine whether the root directory is going to be read-only given the configured settings. */ | |
1911 | ||
1912 | if (protect_system == PROTECT_SYSTEM_STRICT) | |
1913 | return true; | |
1914 | ||
de46b2be | 1915 | if (prefixed_path_strv_contains(read_only_paths, "/")) |
c8c535d5 LP |
1916 | return true; |
1917 | ||
1918 | return false; | |
1919 | } | |
1920 | ||
1921 | static bool home_read_only( | |
1922 | char** read_only_paths, | |
1923 | char** inaccessible_paths, | |
1924 | char** empty_directories, | |
1925 | const BindMount *bind_mounts, | |
1926 | size_t n_bind_mounts, | |
1927 | const TemporaryFileSystem *temporary_filesystems, | |
1928 | size_t n_temporary_filesystems, | |
1929 | ProtectHome protect_home) { | |
1930 | ||
c8c535d5 LP |
1931 | /* Determine whether the /home directory is going to be read-only given the configured settings. Yes, |
1932 | * this is a bit sloppy, since we don't bother checking for cases where / is affected by multiple | |
1933 | * settings. */ | |
1934 | ||
1935 | if (protect_home != PROTECT_HOME_NO) | |
1936 | return true; | |
1937 | ||
de46b2be TM |
1938 | if (prefixed_path_strv_contains(read_only_paths, "/home") || |
1939 | prefixed_path_strv_contains(inaccessible_paths, "/home") || | |
1940 | prefixed_path_strv_contains(empty_directories, "/home")) | |
c8c535d5 LP |
1941 | return true; |
1942 | ||
8794e6db | 1943 | for (size_t i = 0; i < n_temporary_filesystems; i++) |
c8c535d5 LP |
1944 | if (path_equal(temporary_filesystems[i].path, "/home")) |
1945 | return true; | |
1946 | ||
1947 | /* If /home is overmounted with some dir from the host it's not writable. */ | |
8794e6db | 1948 | for (size_t i = 0; i < n_bind_mounts; i++) |
c8c535d5 LP |
1949 | if (path_equal(bind_mounts[i].destination, "/home")) |
1950 | return true; | |
1951 | ||
1952 | return false; | |
1953 | } | |
1954 | ||
89e62e0b LP |
1955 | static int verity_settings_prepare( |
1956 | VeritySettings *verity, | |
1957 | const char *root_image, | |
1958 | const void *root_hash, | |
1959 | size_t root_hash_size, | |
1960 | const char *root_hash_path, | |
1961 | const void *root_hash_sig, | |
1962 | size_t root_hash_sig_size, | |
1963 | const char *root_hash_sig_path, | |
1964 | const char *verity_data_path) { | |
1965 | ||
1966 | int r; | |
1967 | ||
1968 | assert(verity); | |
1969 | ||
1970 | if (root_hash) { | |
1971 | void *d; | |
1972 | ||
1973 | d = memdup(root_hash, root_hash_size); | |
1974 | if (!d) | |
1975 | return -ENOMEM; | |
1976 | ||
1977 | free_and_replace(verity->root_hash, d); | |
1978 | verity->root_hash_size = root_hash_size; | |
aee36b4e | 1979 | verity->designator = PARTITION_ROOT; |
89e62e0b LP |
1980 | } |
1981 | ||
1982 | if (root_hash_sig) { | |
1983 | void *d; | |
1984 | ||
1985 | d = memdup(root_hash_sig, root_hash_sig_size); | |
1986 | if (!d) | |
1987 | return -ENOMEM; | |
1988 | ||
1989 | free_and_replace(verity->root_hash_sig, d); | |
1990 | verity->root_hash_sig_size = root_hash_sig_size; | |
aee36b4e | 1991 | verity->designator = PARTITION_ROOT; |
89e62e0b LP |
1992 | } |
1993 | ||
1994 | if (verity_data_path) { | |
1995 | r = free_and_strdup(&verity->data_path, verity_data_path); | |
1996 | if (r < 0) | |
1997 | return r; | |
1998 | } | |
1999 | ||
2000 | r = verity_settings_load( | |
2001 | verity, | |
2002 | root_image, | |
2003 | root_hash_path, | |
2004 | root_hash_sig_path); | |
2005 | if (r < 0) | |
2006 | return log_debug_errno(r, "Failed to load root hash: %m"); | |
2007 | ||
2008 | return 0; | |
2009 | } | |
2010 | ||
613b411c | 2011 | int setup_namespace( |
ee818b89 | 2012 | const char* root_directory, |
915e6d16 | 2013 | const char* root_image, |
18d73705 | 2014 | const MountOptions *root_image_options, |
bb0ff3fb | 2015 | const NamespaceInfo *ns_info, |
2a624c36 AP |
2016 | char** read_write_paths, |
2017 | char** read_only_paths, | |
2018 | char** inaccessible_paths, | |
ddc155b2 TM |
2019 | char** exec_paths, |
2020 | char** no_exec_paths, | |
6c47cd7d | 2021 | char** empty_directories, |
df61e79a | 2022 | char** exec_dir_symlinks, |
d2d6c096 | 2023 | const BindMount *bind_mounts, |
da6053d0 | 2024 | size_t n_bind_mounts, |
2abd4e38 | 2025 | const TemporaryFileSystem *temporary_filesystems, |
da6053d0 | 2026 | size_t n_temporary_filesystems, |
b3d13314 LB |
2027 | const MountImage *mount_images, |
2028 | size_t n_mount_images, | |
a004cb4c LP |
2029 | const char* tmp_dir, |
2030 | const char* var_tmp_dir, | |
bbb4e7f3 | 2031 | const char *creds_path, |
91dd5f7c | 2032 | const char *log_namespace, |
874cdcbc | 2033 | unsigned long mount_propagation_flag, |
0389f4fa LB |
2034 | const void *root_hash, |
2035 | size_t root_hash_size, | |
2036 | const char *root_hash_path, | |
d4d55b0d LB |
2037 | const void *root_hash_sig, |
2038 | size_t root_hash_sig_size, | |
2039 | const char *root_hash_sig_path, | |
89e62e0b | 2040 | const char *verity_data_path, |
93f59701 LB |
2041 | const MountImage *extension_images, |
2042 | size_t n_extension_images, | |
a07b9926 | 2043 | char **extension_directories, |
5e8deb94 LB |
2044 | const char *propagate_dir, |
2045 | const char *incoming_dir, | |
24759d8f | 2046 | const char *extension_dir, |
3bdc25a4 | 2047 | const char *notify_socket, |
7cc5ef5f | 2048 | char **error_path) { |
15ae422b | 2049 | |
915e6d16 LP |
2050 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
2051 | _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL; | |
aee36b4e | 2052 | _cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT; |
93f59701 | 2053 | _cleanup_strv_free_ char **hierarchies = NULL; |
5f7a690a | 2054 | MountEntry *m = NULL, *mounts = NULL; |
5e8deb94 | 2055 | bool require_prefix = false, setup_propagate = false; |
24759d8f | 2056 | const char *root; |
9cb1709b LP |
2057 | DissectImageFlags dissect_image_flags = |
2058 | DISSECT_IMAGE_GENERIC_ROOT | | |
2059 | DISSECT_IMAGE_REQUIRE_ROOT | | |
2060 | DISSECT_IMAGE_DISCARD_ON_LOOP | | |
2061 | DISSECT_IMAGE_RELAX_VAR_CHECK | | |
2062 | DISSECT_IMAGE_FSCK | | |
c65f854a | 2063 | DISSECT_IMAGE_USR_NO_ROOT | |
73d88b80 LP |
2064 | DISSECT_IMAGE_GROWFS | |
2065 | DISSECT_IMAGE_ADD_PARTITION_DEVICES | | |
2066 | DISSECT_IMAGE_PIN_PARTITION_DEVICES; | |
89e62e0b LP |
2067 | size_t n_mounts; |
2068 | int r; | |
15ae422b | 2069 | |
915e6d16 LP |
2070 | assert(ns_info); |
2071 | ||
cdf42f9b LP |
2072 | /* Make sure that all mknod(), mkdir() calls we do are unaffected by the umask, and the access modes |
2073 | * we configure take effect */ | |
2074 | BLOCK_WITH_UMASK(0000); | |
2075 | ||
5e8deb94 LB |
2076 | if (!isempty(propagate_dir) && !isempty(incoming_dir)) |
2077 | setup_propagate = true; | |
2078 | ||
874cdcbc LP |
2079 | if (mount_propagation_flag == 0) |
2080 | mount_propagation_flag = MS_SHARED; | |
ac0930c8 | 2081 | |
915e6d16 | 2082 | if (root_image) { |
c8c535d5 LP |
2083 | /* Make the whole image read-only if we can determine that we only access it in a read-only fashion. */ |
2084 | if (root_read_only(read_only_paths, | |
52b3d652 | 2085 | ns_info->protect_system) && |
c8c535d5 LP |
2086 | home_read_only(read_only_paths, inaccessible_paths, empty_directories, |
2087 | bind_mounts, n_bind_mounts, temporary_filesystems, n_temporary_filesystems, | |
52b3d652 | 2088 | ns_info->protect_home) && |
c9ef8573 | 2089 | strv_isempty(read_write_paths)) |
915e6d16 LP |
2090 | dissect_image_flags |= DISSECT_IMAGE_READ_ONLY; |
2091 | ||
89e62e0b LP |
2092 | r = verity_settings_prepare( |
2093 | &verity, | |
2094 | root_image, | |
2095 | root_hash, root_hash_size, root_hash_path, | |
2096 | root_hash_sig, root_hash_sig_size, root_hash_sig_path, | |
2097 | verity_data_path); | |
915e6d16 | 2098 | if (r < 0) |
89e62e0b LP |
2099 | return r; |
2100 | ||
2101 | SET_FLAG(dissect_image_flags, DISSECT_IMAGE_NO_PARTITION_TABLE, verity.data_path); | |
915e6d16 | 2102 | |
89e62e0b LP |
2103 | r = loop_device_make_by_path( |
2104 | root_image, | |
ef9c184d | 2105 | FLAGS_SET(dissect_image_flags, DISSECT_IMAGE_DEVICE_READ_ONLY) ? O_RDONLY : -1 /* < 0 means writable if possible, read-only as fallback */, |
22ee78a8 | 2106 | /* sector_size= */ UINT32_MAX, |
89e62e0b | 2107 | FLAGS_SET(dissect_image_flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN, |
7f52206a | 2108 | LOCK_SH, |
89e62e0b | 2109 | &loop_device); |
78ebe980 | 2110 | if (r < 0) |
89e62e0b LP |
2111 | return log_debug_errno(r, "Failed to create loop device for root image: %m"); |
2112 | ||
bad31660 YW |
2113 | r = dissect_loop_device( |
2114 | loop_device, | |
89e62e0b LP |
2115 | &verity, |
2116 | root_image_options, | |
2117 | dissect_image_flags, | |
2118 | &dissected_image); | |
78ebe980 | 2119 | if (r < 0) |
763a260a | 2120 | return log_debug_errno(r, "Failed to dissect image: %m"); |
78ebe980 | 2121 | |
88b3300f LP |
2122 | r = dissected_image_load_verity_sig_partition( |
2123 | dissected_image, | |
2124 | loop_device->fd, | |
2125 | &verity); | |
2126 | if (r < 0) | |
2127 | return r; | |
2128 | ||
89e62e0b LP |
2129 | r = dissected_image_decrypt( |
2130 | dissected_image, | |
2131 | NULL, | |
2132 | &verity, | |
e330f97a | 2133 | dissect_image_flags); |
915e6d16 | 2134 | if (r < 0) |
763a260a | 2135 | return log_debug_errno(r, "Failed to decrypt dissected image: %m"); |
915e6d16 LP |
2136 | } |
2137 | ||
e908468b LP |
2138 | if (root_directory) |
2139 | root = root_directory; | |
0722b359 | 2140 | else { |
77f16dbd DDM |
2141 | /* /run/systemd should have been created by PID 1 early on already, but in some cases, like |
2142 | * when running tests (test-execute), it might not have been created yet so let's make sure | |
2143 | * we create it if it doesn't already exist. */ | |
2144 | (void) mkdir_p_label("/run/systemd", 0755); | |
2145 | ||
d73020f2 LP |
2146 | /* Always create the mount namespace in a temporary directory, instead of operating directly |
2147 | * in the root. The temporary directory prevents any mounts from being potentially obscured | |
2148 | * my other mounts we already applied. We use the same mount point for all images, which is | |
2149 | * safe, since they all live in their own namespaces after all, and hence won't see each | |
2150 | * other. */ | |
e908468b LP |
2151 | |
2152 | root = "/run/systemd/unit-root"; | |
2153 | (void) mkdir_label(root, 0700); | |
d18aff04 | 2154 | require_prefix = true; |
0722b359 | 2155 | } |
e908468b | 2156 | |
a07b9926 | 2157 | if (n_extension_images > 0 || !strv_isempty(extension_directories)) { |
93f59701 LB |
2158 | r = parse_env_extension_hierarchies(&hierarchies); |
2159 | if (r < 0) | |
2160 | return r; | |
2161 | } | |
2162 | ||
cfbeb4ef LP |
2163 | n_mounts = namespace_calculate_mounts( |
2164 | ns_info, | |
2165 | read_write_paths, | |
2166 | read_only_paths, | |
2167 | inaccessible_paths, | |
ddc155b2 TM |
2168 | exec_paths, |
2169 | no_exec_paths, | |
6c47cd7d | 2170 | empty_directories, |
f5c52a77 | 2171 | n_bind_mounts, |
2abd4e38 | 2172 | n_temporary_filesystems, |
b3d13314 | 2173 | n_mount_images, |
93f59701 | 2174 | n_extension_images, |
a07b9926 | 2175 | strv_length(extension_directories), |
93f59701 | 2176 | strv_length(hierarchies), |
cfbeb4ef | 2177 | tmp_dir, var_tmp_dir, |
bbb4e7f3 | 2178 | creds_path, |
5e8deb94 | 2179 | log_namespace, |
3bdc25a4 LP |
2180 | setup_propagate, |
2181 | notify_socket); | |
613b411c | 2182 | |
f0a4feb0 | 2183 | if (n_mounts > 0) { |
5f7a690a LP |
2184 | m = mounts = new0(MountEntry, n_mounts); |
2185 | if (!mounts) | |
2186 | return -ENOMEM; | |
2187 | ||
d18aff04 | 2188 | r = append_access_mounts(&m, read_write_paths, READWRITE, require_prefix); |
613b411c | 2189 | if (r < 0) |
f0a4feb0 | 2190 | goto finish; |
613b411c | 2191 | |
d18aff04 | 2192 | r = append_access_mounts(&m, read_only_paths, READONLY, require_prefix); |
613b411c | 2193 | if (r < 0) |
f0a4feb0 | 2194 | goto finish; |
613b411c | 2195 | |
d18aff04 | 2196 | r = append_access_mounts(&m, inaccessible_paths, INACCESSIBLE, require_prefix); |
613b411c | 2197 | if (r < 0) |
f0a4feb0 | 2198 | goto finish; |
7ff7394d | 2199 | |
ddc155b2 TM |
2200 | r = append_access_mounts(&m, exec_paths, EXEC, require_prefix); |
2201 | if (r < 0) | |
2202 | goto finish; | |
2203 | ||
2204 | r = append_access_mounts(&m, no_exec_paths, NOEXEC, require_prefix); | |
2205 | if (r < 0) | |
2206 | goto finish; | |
2207 | ||
6c47cd7d LP |
2208 | r = append_empty_dir_mounts(&m, empty_directories); |
2209 | if (r < 0) | |
2210 | goto finish; | |
2211 | ||
d2d6c096 LP |
2212 | r = append_bind_mounts(&m, bind_mounts, n_bind_mounts); |
2213 | if (r < 0) | |
2214 | goto finish; | |
2215 | ||
2abd4e38 YW |
2216 | r = append_tmpfs_mounts(&m, temporary_filesystems, n_temporary_filesystems); |
2217 | if (r < 0) | |
2218 | goto finish; | |
2219 | ||
613b411c | 2220 | if (tmp_dir) { |
56a13a49 ZJS |
2221 | bool ro = streq(tmp_dir, RUN_SYSTEMD_EMPTY); |
2222 | ||
34de407a | 2223 | *(m++) = (MountEntry) { |
5327c910 | 2224 | .path_const = "/tmp", |
56a13a49 | 2225 | .mode = ro ? PRIVATE_TMP_READONLY : PRIVATE_TMP, |
89bd586c | 2226 | .source_const = tmp_dir, |
5327c910 | 2227 | }; |
613b411c | 2228 | } |
7ff7394d | 2229 | |
613b411c | 2230 | if (var_tmp_dir) { |
56a13a49 ZJS |
2231 | bool ro = streq(var_tmp_dir, RUN_SYSTEMD_EMPTY); |
2232 | ||
34de407a | 2233 | *(m++) = (MountEntry) { |
5327c910 | 2234 | .path_const = "/var/tmp", |
56a13a49 | 2235 | .mode = ro ? PRIVATE_TMP_READONLY : PRIVATE_TMP, |
89bd586c | 2236 | .source_const = var_tmp_dir, |
5327c910 | 2237 | }; |
7ff7394d | 2238 | } |
ac0930c8 | 2239 | |
b3d13314 LB |
2240 | r = append_mount_images(&m, mount_images, n_mount_images); |
2241 | if (r < 0) | |
2242 | goto finish; | |
2243 | ||
a07b9926 | 2244 | r = append_extensions(&m, root, extension_dir, hierarchies, extension_images, n_extension_images, extension_directories); |
93f59701 LB |
2245 | if (r < 0) |
2246 | goto finish; | |
2247 | ||
d46b79bb | 2248 | if (ns_info->private_dev) |
34de407a | 2249 | *(m++) = (MountEntry) { |
5327c910 LP |
2250 | .path_const = "/dev", |
2251 | .mode = PRIVATE_DEV, | |
9ce4e4b0 | 2252 | .flags = DEV_MOUNT_OPTIONS, |
5327c910 | 2253 | }; |
7f112f50 | 2254 | |
788e7201 TM |
2255 | /* In case /proc is successfully mounted with pid tree subset only (ProcSubset=pid), the |
2256 | protective mounts to non-pid /proc paths would fail. But the pid only option may have | |
2257 | failed gracefully, so let's try the mounts but it's not fatal if they don't succeed. */ | |
2258 | bool ignore_protect_proc = ns_info->ignore_protect_paths || ns_info->proc_subset == PROC_SUBSET_PID; | |
c575770b | 2259 | if (ns_info->protect_kernel_tunables) { |
cbc056c8 | 2260 | r = append_static_mounts(&m, |
788e7201 TM |
2261 | protect_kernel_tunables_proc_table, |
2262 | ELEMENTSOF(protect_kernel_tunables_proc_table), | |
2263 | ignore_protect_proc); | |
2264 | if (r < 0) | |
2265 | goto finish; | |
2266 | ||
2267 | r = append_static_mounts(&m, | |
2268 | protect_kernel_tunables_sys_table, | |
2269 | ELEMENTSOF(protect_kernel_tunables_sys_table), | |
cbc056c8 | 2270 | ns_info->ignore_protect_paths); |
c575770b | 2271 | if (r < 0) |
f0a4feb0 | 2272 | goto finish; |
c575770b DH |
2273 | } |
2274 | ||
2275 | if (ns_info->protect_kernel_modules) { | |
cbc056c8 ZJS |
2276 | r = append_static_mounts(&m, |
2277 | protect_kernel_modules_table, | |
2278 | ELEMENTSOF(protect_kernel_modules_table), | |
2279 | ns_info->ignore_protect_paths); | |
c575770b | 2280 | if (r < 0) |
f0a4feb0 | 2281 | goto finish; |
c575770b | 2282 | } |
59eeb84b | 2283 | |
94a7b275 | 2284 | if (ns_info->protect_kernel_logs) { |
cbc056c8 | 2285 | r = append_static_mounts(&m, |
788e7201 TM |
2286 | protect_kernel_logs_proc_table, |
2287 | ELEMENTSOF(protect_kernel_logs_proc_table), | |
2288 | ignore_protect_proc); | |
2289 | if (r < 0) | |
2290 | goto finish; | |
2291 | ||
2292 | r = append_static_mounts(&m, | |
2293 | protect_kernel_logs_dev_table, | |
2294 | ELEMENTSOF(protect_kernel_logs_dev_table), | |
cbc056c8 | 2295 | ns_info->ignore_protect_paths); |
94a7b275 KK |
2296 | if (r < 0) |
2297 | goto finish; | |
2298 | } | |
2299 | ||
d46b79bb | 2300 | if (ns_info->protect_control_groups) |
34de407a | 2301 | *(m++) = (MountEntry) { |
5327c910 LP |
2302 | .path_const = "/sys/fs/cgroup", |
2303 | .mode = READONLY, | |
2304 | }; | |
59eeb84b | 2305 | |
52b3d652 | 2306 | r = append_protect_home(&m, ns_info->protect_home, ns_info->ignore_protect_paths); |
b6c432ca | 2307 | if (r < 0) |
f0a4feb0 | 2308 | goto finish; |
417116f2 | 2309 | |
52b3d652 | 2310 | r = append_protect_system(&m, ns_info->protect_system, false); |
f471b2af | 2311 | if (r < 0) |
f0a4feb0 | 2312 | goto finish; |
417116f2 | 2313 | |
9b68367b | 2314 | if (namespace_info_mount_apivfs(ns_info)) { |
cbc056c8 ZJS |
2315 | r = append_static_mounts(&m, |
2316 | apivfs_table, | |
2317 | ELEMENTSOF(apivfs_table), | |
2318 | ns_info->ignore_protect_paths); | |
5d997827 LP |
2319 | if (r < 0) |
2320 | goto finish; | |
2321 | } | |
2322 | ||
1361f015 CB |
2323 | /* Note, if proc is mounted with subset=pid then neither of the |
2324 | * two paths will exist, i.e. they are implicitly protected by | |
2325 | * the mount option. */ | |
aecd5ac6 TM |
2326 | if (ns_info->protect_hostname) { |
2327 | *(m++) = (MountEntry) { | |
2328 | .path_const = "/proc/sys/kernel/hostname", | |
2329 | .mode = READONLY, | |
1361f015 | 2330 | .ignore = ignore_protect_proc, |
aecd5ac6 TM |
2331 | }; |
2332 | *(m++) = (MountEntry) { | |
2333 | .path_const = "/proc/sys/kernel/domainname", | |
2334 | .mode = READONLY, | |
1361f015 | 2335 | .ignore = ignore_protect_proc, |
aecd5ac6 TM |
2336 | }; |
2337 | } | |
2338 | ||
c2da3bf2 YW |
2339 | if (ns_info->private_network) |
2340 | *(m++) = (MountEntry) { | |
2341 | .path_const = "/sys", | |
2342 | .mode = PRIVATE_SYSFS, | |
2343 | }; | |
2344 | ||
57ac6959 | 2345 | if (ns_info->private_ipc) |
80271a44 XR |
2346 | *(m++) = (MountEntry) { |
2347 | .path_const = "/dev/mqueue", | |
2348 | .mode = MQUEUEFS, | |
2349 | .flags = MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, | |
2350 | }; | |
80271a44 | 2351 | |
bbb4e7f3 LP |
2352 | if (creds_path) { |
2353 | /* If our service has a credentials store configured, then bind that one in, but hide | |
2354 | * everything else. */ | |
2355 | ||
2356 | *(m++) = (MountEntry) { | |
2357 | .path_const = "/run/credentials", | |
2358 | .mode = TMPFS, | |
2359 | .read_only = true, | |
2360 | .options_const = "mode=0755" TMPFS_LIMITS_EMPTY_OR_ALMOST, | |
2361 | .flags = MS_NODEV|MS_STRICTATIME|MS_NOSUID|MS_NOEXEC, | |
2362 | }; | |
2363 | ||
2364 | *(m++) = (MountEntry) { | |
2365 | .path_const = creds_path, | |
2366 | .mode = BIND_MOUNT, | |
2367 | .read_only = true, | |
2368 | .source_const = creds_path, | |
2369 | }; | |
2370 | } else { | |
2371 | /* If our service has no credentials store configured, then make the whole | |
2372 | * credentials tree inaccessible wholesale. */ | |
2373 | ||
2374 | *(m++) = (MountEntry) { | |
2375 | .path_const = "/run/credentials", | |
2376 | .mode = INACCESSIBLE, | |
2377 | .ignore = true, | |
2378 | }; | |
2379 | } | |
2380 | ||
91dd5f7c | 2381 | if (log_namespace) { |
c2b2df60 | 2382 | _cleanup_free_ char *q = NULL; |
91dd5f7c LP |
2383 | |
2384 | q = strjoin("/run/systemd/journal.", log_namespace); | |
2385 | if (!q) { | |
2386 | r = -ENOMEM; | |
2387 | goto finish; | |
2388 | } | |
2389 | ||
2390 | *(m++) = (MountEntry) { | |
2391 | .path_const = "/run/systemd/journal", | |
2392 | .mode = BIND_MOUNT_RECURSIVE, | |
2393 | .read_only = true, | |
2394 | .source_malloc = TAKE_PTR(q), | |
2395 | }; | |
2396 | } | |
2397 | ||
5e8deb94 LB |
2398 | /* Will be used to add bind mounts at runtime */ |
2399 | if (setup_propagate) | |
2400 | *(m++) = (MountEntry) { | |
2401 | .source_const = propagate_dir, | |
2402 | .path_const = incoming_dir, | |
2403 | .mode = BIND_MOUNT, | |
2404 | .read_only = true, | |
2405 | }; | |
2406 | ||
3bdc25a4 LP |
2407 | if (notify_socket) |
2408 | *(m++) = (MountEntry) { | |
2409 | .path_const = notify_socket, | |
2410 | .source_const = notify_socket, | |
2411 | .mode = BIND_MOUNT, | |
2412 | .read_only = true, | |
2413 | }; | |
2414 | ||
f0a4feb0 | 2415 | assert(mounts + n_mounts == m); |
ac0930c8 | 2416 | |
5327c910 | 2417 | /* Prepend the root directory where that's necessary */ |
e908468b | 2418 | r = prefix_where_needed(mounts, n_mounts, root); |
5327c910 LP |
2419 | if (r < 0) |
2420 | goto finish; | |
2421 | ||
fbf90c0d | 2422 | drop_unused_mounts(root, mounts, &n_mounts); |
15ae422b LP |
2423 | } |
2424 | ||
1beab8b0 LP |
2425 | /* All above is just preparation, figuring out what to do. Let's now actually start doing something. */ |
2426 | ||
d944dc95 | 2427 | if (unshare(CLONE_NEWNS) < 0) { |
763a260a | 2428 | r = log_debug_errno(errno, "Failed to unshare the mount namespace: %m"); |
eacfbd89 LP |
2429 | if (ERRNO_IS_PRIVILEGE(r) || |
2430 | ERRNO_IS_NOT_SUPPORTED(r)) | |
cbc056c8 ZJS |
2431 | /* If the kernel doesn't support namespaces, or when there's a MAC or seccomp filter |
2432 | * in place that doesn't allow us to create namespaces (or a missing cap), then | |
2433 | * propagate a recognizable error back, which the caller can use to detect this case | |
2434 | * (and only this) and optionally continue without namespacing applied. */ | |
1beab8b0 LP |
2435 | r = -ENOANO; |
2436 | ||
d944dc95 LP |
2437 | goto finish; |
2438 | } | |
1e4e94c8 | 2439 | |
5e8deb94 LB |
2440 | /* Create the source directory to allow runtime propagation of mounts */ |
2441 | if (setup_propagate) | |
2442 | (void) mkdir_p(propagate_dir, 0600); | |
2443 | ||
3ebf0b0b LB |
2444 | if (n_extension_images > 0 || !strv_isempty(extension_directories)) |
2445 | /* ExtensionImages/Directories mountpoint directories will be created while parsing the | |
2446 | * mounts to create, so have the parent ready */ | |
93f59701 | 2447 | (void) mkdir_p(extension_dir, 0600); |
93f59701 | 2448 | |
9b68367b YW |
2449 | /* Remount / as SLAVE so that nothing now mounted in the namespace |
2450 | * shows up in the parent */ | |
2451 | if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) { | |
763a260a | 2452 | r = log_debug_errno(errno, "Failed to remount '/' as SLAVE: %m"); |
9b68367b | 2453 | goto finish; |
ee818b89 AC |
2454 | } |
2455 | ||
915e6d16 | 2456 | if (root_image) { |
e908468b | 2457 | /* A root image is specified, mount it to the right place */ |
21b61b1d | 2458 | r = dissected_image_mount(dissected_image, root, UID_INVALID, UID_INVALID, dissect_image_flags); |
763a260a YW |
2459 | if (r < 0) { |
2460 | log_debug_errno(r, "Failed to mount root image: %m"); | |
41bc4849 LP |
2461 | goto finish; |
2462 | } | |
2463 | ||
2464 | /* Now release the block device lock, so that udevd is free to call BLKRRPART on the device | |
2465 | * if it likes. */ | |
2466 | r = loop_device_flock(loop_device, LOCK_UN); | |
2467 | if (r < 0) { | |
2468 | log_debug_errno(r, "Failed to release lock on loopback block device: %m"); | |
915e6d16 | 2469 | goto finish; |
763a260a | 2470 | } |
915e6d16 | 2471 | |
3044d343 YW |
2472 | r = dissected_image_relinquish(dissected_image); |
2473 | if (r < 0) { | |
2474 | log_debug_errno(r, "Failed to relinquish dissected image: %m"); | |
2475 | goto finish; | |
07ce7407 | 2476 | } |
78ebe980 | 2477 | |
915e6d16 LP |
2478 | } else if (root_directory) { |
2479 | ||
e908468b LP |
2480 | /* A root directory is specified. Turn its directory into bind mount, if it isn't one yet. */ |
2481 | r = path_is_mount_point(root, NULL, AT_SYMLINK_FOLLOW); | |
763a260a YW |
2482 | if (r < 0) { |
2483 | log_debug_errno(r, "Failed to detect that %s is a mount point or not: %m", root); | |
d944dc95 | 2484 | goto finish; |
763a260a | 2485 | } |
8f1ad200 | 2486 | if (r == 0) { |
21935150 LP |
2487 | r = mount_nofollow_verbose(LOG_DEBUG, root, root, NULL, MS_BIND|MS_REC, NULL); |
2488 | if (r < 0) | |
8f1ad200 | 2489 | goto finish; |
d944dc95 | 2490 | } |
e908468b | 2491 | |
9b68367b | 2492 | } else { |
e908468b | 2493 | /* Let's mount the main root directory to the root directory to use */ |
21935150 LP |
2494 | r = mount_nofollow_verbose(LOG_DEBUG, "/", root, NULL, MS_BIND|MS_REC, NULL); |
2495 | if (r < 0) | |
e908468b | 2496 | goto finish; |
ee818b89 | 2497 | } |
c2c13f2d | 2498 | |
4e0c20de LP |
2499 | /* Try to set up the new root directory before mounting anything else there. */ |
2500 | if (root_image || root_directory) | |
2501 | (void) base_filesystem_create(root, UID_INVALID, GID_INVALID); | |
2502 | ||
82fb2da2 | 2503 | /* Now make the magic happen */ |
df61e79a | 2504 | r = apply_mounts(root, ns_info, mounts, &n_mounts, exec_dir_symlinks, error_path); |
82fb2da2 LB |
2505 | if (r < 0) |
2506 | goto finish; | |
15ae422b | 2507 | |
9b68367b | 2508 | /* MS_MOVE does not work on MS_SHARED so the remount MS_SHARED will be done later */ |
9d50f850 | 2509 | r = mount_switch_root(root, /* mount_propagation_flag = */ 0); |
ea63a260 LB |
2510 | if (r == -EINVAL && root_directory) { |
2511 | /* If we are using root_directory and we don't have privileges (ie: user manager in a user | |
2512 | * namespace) and the root_directory is already a mount point in the parent namespace, | |
2513 | * MS_MOVE will fail as we don't have permission to change it (with EINVAL rather than | |
2514 | * EPERM). Attempt to bind-mount it over itself (like we do above if it's not already a | |
2515 | * mount point) and try again. */ | |
2516 | r = mount_nofollow_verbose(LOG_DEBUG, root, root, NULL, MS_BIND|MS_REC, NULL); | |
2517 | if (r < 0) | |
2518 | goto finish; | |
9d50f850 | 2519 | r = mount_switch_root(root, /* mount_propagation_flag = */ 0); |
ea63a260 | 2520 | } |
763a260a YW |
2521 | if (r < 0) { |
2522 | log_debug_errno(r, "Failed to mount root with MS_MOVE: %m"); | |
9b68367b | 2523 | goto finish; |
763a260a | 2524 | } |
ee818b89 | 2525 | |
874cdcbc LP |
2526 | /* Remount / as the desired mode. Note that this will not reestablish propagation from our side to |
2527 | * the host, since what's disconnected is disconnected. */ | |
2528 | if (mount(NULL, "/", NULL, mount_propagation_flag | MS_REC, NULL) < 0) { | |
763a260a | 2529 | r = log_debug_errno(errno, "Failed to remount '/' with desired mount flags: %m"); |
d944dc95 LP |
2530 | goto finish; |
2531 | } | |
15ae422b | 2532 | |
5e8deb94 LB |
2533 | /* bind_mount_in_namespace() will MS_MOVE into that directory, and that's only |
2534 | * supported for non-shared mounts. This needs to happen after remounting / or it will fail. */ | |
2535 | if (setup_propagate) { | |
2536 | r = mount(NULL, incoming_dir, NULL, MS_SLAVE, NULL); | |
2537 | if (r < 0) { | |
2538 | log_error_errno(r, "Failed to remount %s with MS_SLAVE: %m", incoming_dir); | |
2539 | goto finish; | |
2540 | } | |
2541 | } | |
2542 | ||
d944dc95 | 2543 | r = 0; |
15ae422b | 2544 | |
d944dc95 | 2545 | finish: |
0cd41757 LP |
2546 | if (n_mounts > 0) |
2547 | for (m = mounts; m < mounts + n_mounts; m++) | |
2548 | mount_entry_done(m); | |
613b411c | 2549 | |
5f7a690a LP |
2550 | free(mounts); |
2551 | ||
613b411c LP |
2552 | return r; |
2553 | } | |
2554 | ||
da6053d0 | 2555 | void bind_mount_free_many(BindMount *b, size_t n) { |
d2d6c096 LP |
2556 | assert(b || n == 0); |
2557 | ||
fe96c0f8 | 2558 | for (size_t i = 0; i < n; i++) { |
d2d6c096 LP |
2559 | free(b[i].source); |
2560 | free(b[i].destination); | |
2561 | } | |
2562 | ||
2563 | free(b); | |
2564 | } | |
2565 | ||
da6053d0 | 2566 | int bind_mount_add(BindMount **b, size_t *n, const BindMount *item) { |
d2d6c096 LP |
2567 | _cleanup_free_ char *s = NULL, *d = NULL; |
2568 | BindMount *c; | |
2569 | ||
2570 | assert(b); | |
2571 | assert(n); | |
2572 | assert(item); | |
2573 | ||
2574 | s = strdup(item->source); | |
2575 | if (!s) | |
2576 | return -ENOMEM; | |
2577 | ||
2578 | d = strdup(item->destination); | |
2579 | if (!d) | |
2580 | return -ENOMEM; | |
2581 | ||
aa484f35 | 2582 | c = reallocarray(*b, *n + 1, sizeof(BindMount)); |
d2d6c096 LP |
2583 | if (!c) |
2584 | return -ENOMEM; | |
2585 | ||
2586 | *b = c; | |
2587 | ||
2588 | c[(*n) ++] = (BindMount) { | |
1cc6c93a YW |
2589 | .source = TAKE_PTR(s), |
2590 | .destination = TAKE_PTR(d), | |
d2d6c096 | 2591 | .read_only = item->read_only, |
9ce4e4b0 | 2592 | .nosuid = item->nosuid, |
d2d6c096 LP |
2593 | .recursive = item->recursive, |
2594 | .ignore_enoent = item->ignore_enoent, | |
2595 | }; | |
2596 | ||
d2d6c096 LP |
2597 | return 0; |
2598 | } | |
2599 | ||
b3d13314 | 2600 | MountImage* mount_image_free_many(MountImage *m, size_t *n) { |
b3d13314 LB |
2601 | assert(n); |
2602 | assert(m || *n == 0); | |
2603 | ||
fe96c0f8 | 2604 | for (size_t i = 0; i < *n; i++) { |
b3d13314 LB |
2605 | free(m[i].source); |
2606 | free(m[i].destination); | |
427353f6 | 2607 | mount_options_free_all(m[i].mount_options); |
b3d13314 LB |
2608 | } |
2609 | ||
2610 | free(m); | |
2611 | *n = 0; | |
2612 | return NULL; | |
2613 | } | |
2614 | ||
2615 | int mount_image_add(MountImage **m, size_t *n, const MountImage *item) { | |
2616 | _cleanup_free_ char *s = NULL, *d = NULL; | |
427353f6 | 2617 | _cleanup_(mount_options_free_allp) MountOptions *options = NULL; |
b3d13314 LB |
2618 | MountImage *c; |
2619 | ||
2620 | assert(m); | |
2621 | assert(n); | |
2622 | assert(item); | |
2623 | ||
2624 | s = strdup(item->source); | |
2625 | if (!s) | |
2626 | return -ENOMEM; | |
2627 | ||
93f59701 LB |
2628 | if (item->destination) { |
2629 | d = strdup(item->destination); | |
2630 | if (!d) | |
2631 | return -ENOMEM; | |
2632 | } | |
b3d13314 | 2633 | |
427353f6 | 2634 | LIST_FOREACH(mount_options, i, item->mount_options) { |
c2b2df60 | 2635 | _cleanup_(mount_options_free_allp) MountOptions *o = NULL; |
427353f6 LB |
2636 | |
2637 | o = new(MountOptions, 1); | |
2638 | if (!o) | |
2639 | return -ENOMEM; | |
2640 | ||
2641 | *o = (MountOptions) { | |
2642 | .partition_designator = i->partition_designator, | |
2643 | .options = strdup(i->options), | |
2644 | }; | |
2645 | if (!o->options) | |
2646 | return -ENOMEM; | |
2647 | ||
2648 | LIST_APPEND(mount_options, options, TAKE_PTR(o)); | |
2649 | } | |
2650 | ||
b3d13314 LB |
2651 | c = reallocarray(*m, *n + 1, sizeof(MountImage)); |
2652 | if (!c) | |
2653 | return -ENOMEM; | |
2654 | ||
2655 | *m = c; | |
2656 | ||
2657 | c[(*n) ++] = (MountImage) { | |
2658 | .source = TAKE_PTR(s), | |
2659 | .destination = TAKE_PTR(d), | |
427353f6 | 2660 | .mount_options = TAKE_PTR(options), |
b3d13314 | 2661 | .ignore_enoent = item->ignore_enoent, |
93f59701 | 2662 | .type = item->type, |
b3d13314 LB |
2663 | }; |
2664 | ||
2665 | return 0; | |
2666 | } | |
2667 | ||
da6053d0 | 2668 | void temporary_filesystem_free_many(TemporaryFileSystem *t, size_t n) { |
2abd4e38 YW |
2669 | assert(t || n == 0); |
2670 | ||
fe96c0f8 | 2671 | for (size_t i = 0; i < n; i++) { |
2abd4e38 YW |
2672 | free(t[i].path); |
2673 | free(t[i].options); | |
2674 | } | |
2675 | ||
2676 | free(t); | |
2677 | } | |
2678 | ||
2679 | int temporary_filesystem_add( | |
2680 | TemporaryFileSystem **t, | |
da6053d0 | 2681 | size_t *n, |
2abd4e38 YW |
2682 | const char *path, |
2683 | const char *options) { | |
2684 | ||
2685 | _cleanup_free_ char *p = NULL, *o = NULL; | |
2686 | TemporaryFileSystem *c; | |
2687 | ||
2688 | assert(t); | |
2689 | assert(n); | |
2690 | assert(path); | |
2691 | ||
2692 | p = strdup(path); | |
2693 | if (!p) | |
2694 | return -ENOMEM; | |
2695 | ||
2696 | if (!isempty(options)) { | |
2697 | o = strdup(options); | |
2698 | if (!o) | |
2699 | return -ENOMEM; | |
2700 | } | |
2701 | ||
aa484f35 | 2702 | c = reallocarray(*t, *n + 1, sizeof(TemporaryFileSystem)); |
2abd4e38 YW |
2703 | if (!c) |
2704 | return -ENOMEM; | |
2705 | ||
2706 | *t = c; | |
2707 | ||
2708 | c[(*n) ++] = (TemporaryFileSystem) { | |
1cc6c93a YW |
2709 | .path = TAKE_PTR(p), |
2710 | .options = TAKE_PTR(o), | |
2abd4e38 YW |
2711 | }; |
2712 | ||
2abd4e38 YW |
2713 | return 0; |
2714 | } | |
2715 | ||
a652f050 JR |
2716 | static int make_tmp_prefix(const char *prefix) { |
2717 | _cleanup_free_ char *t = NULL; | |
254d1313 | 2718 | _cleanup_close_ int fd = -EBADF; |
a652f050 JR |
2719 | int r; |
2720 | ||
2721 | /* Don't do anything unless we know the dir is actually missing */ | |
2722 | r = access(prefix, F_OK); | |
2723 | if (r >= 0) | |
2724 | return 0; | |
2725 | if (errno != ENOENT) | |
2726 | return -errno; | |
2727 | ||
2053593f | 2728 | WITH_UMASK(000) |
30443439 | 2729 | r = mkdir_parents(prefix, 0755); |
a652f050 JR |
2730 | if (r < 0) |
2731 | return r; | |
2732 | ||
2733 | r = tempfn_random(prefix, NULL, &t); | |
2734 | if (r < 0) | |
2735 | return r; | |
2736 | ||
96603ea0 LP |
2737 | /* umask will corrupt this access mode, but that doesn't matter, we need to call chmod() anyway for |
2738 | * the suid bit, below. */ | |
2739 | fd = open_mkdir_at(AT_FDCWD, t, O_EXCL|O_CLOEXEC, 0777); | |
2740 | if (fd < 0) | |
2741 | return fd; | |
a652f050 | 2742 | |
96603ea0 LP |
2743 | r = RET_NERRNO(fchmod(fd, 01777)); |
2744 | if (r < 0) { | |
a652f050 JR |
2745 | (void) rmdir(t); |
2746 | return r; | |
2747 | } | |
2748 | ||
96603ea0 LP |
2749 | r = RET_NERRNO(rename(t, prefix)); |
2750 | if (r < 0) { | |
a652f050 JR |
2751 | (void) rmdir(t); |
2752 | return r == -EEXIST ? 0 : r; /* it's fine if someone else created the dir by now */ | |
2753 | } | |
2754 | ||
2755 | return 0; | |
2756 | ||
2757 | } | |
2758 | ||
56a13a49 | 2759 | static int setup_one_tmp_dir(const char *id, const char *prefix, char **path, char **tmp_path) { |
613b411c | 2760 | _cleanup_free_ char *x = NULL; |
19cd4e19 | 2761 | _cleanup_free_ char *y = NULL; |
6b46ea73 | 2762 | sd_id128_t boot_id; |
56a13a49 | 2763 | bool rw = true; |
6b46ea73 | 2764 | int r; |
613b411c LP |
2765 | |
2766 | assert(id); | |
2767 | assert(prefix); | |
2768 | assert(path); | |
2769 | ||
6b46ea73 LP |
2770 | /* We include the boot id in the directory so that after a |
2771 | * reboot we can easily identify obsolete directories. */ | |
2772 | ||
2773 | r = sd_id128_get_boot(&boot_id); | |
2774 | if (r < 0) | |
2775 | return r; | |
2776 | ||
85b55869 | 2777 | x = strjoin(prefix, "/systemd-private-", SD_ID128_TO_STRING(boot_id), "-", id, "-XXXXXX"); |
613b411c LP |
2778 | if (!x) |
2779 | return -ENOMEM; | |
2780 | ||
a652f050 JR |
2781 | r = make_tmp_prefix(prefix); |
2782 | if (r < 0) | |
2783 | return r; | |
2784 | ||
2053593f | 2785 | WITH_UMASK(0077) |
56a13a49 ZJS |
2786 | if (!mkdtemp(x)) { |
2787 | if (errno == EROFS || ERRNO_IS_DISK_SPACE(errno)) | |
2788 | rw = false; | |
2789 | else | |
2790 | return -errno; | |
2791 | } | |
613b411c | 2792 | |
56a13a49 | 2793 | if (rw) { |
19cd4e19 | 2794 | y = strjoin(x, "/tmp"); |
2795 | if (!y) | |
2796 | return -ENOMEM; | |
2797 | ||
2053593f | 2798 | WITH_UMASK(0000) |
19cd4e19 | 2799 | if (mkdir(y, 0777 | S_ISVTX) < 0) |
7c76e181 | 2800 | return -errno; |
19cd4e19 | 2801 | |
03bc11d1 | 2802 | r = label_fix_full(AT_FDCWD, y, prefix, 0); |
56a13a49 ZJS |
2803 | if (r < 0) |
2804 | return r; | |
19cd4e19 | 2805 | |
2806 | if (tmp_path) | |
2807 | *tmp_path = TAKE_PTR(y); | |
56a13a49 ZJS |
2808 | } else { |
2809 | /* Trouble: we failed to create the directory. Instead of failing, let's simulate /tmp being | |
2810 | * read-only. This way the service will get the EROFS result as if it was writing to the real | |
2811 | * file system. */ | |
2053593f | 2812 | WITH_UMASK(0000) |
30443439 | 2813 | r = mkdir_p(RUN_SYSTEMD_EMPTY, 0500); |
56a13a49 ZJS |
2814 | if (r < 0) |
2815 | return r; | |
613b411c | 2816 | |
3f181262 LP |
2817 | r = free_and_strdup(&x, RUN_SYSTEMD_EMPTY); |
2818 | if (r < 0) | |
2819 | return r; | |
c17ec25e | 2820 | } |
15ae422b | 2821 | |
1cc6c93a | 2822 | *path = TAKE_PTR(x); |
613b411c LP |
2823 | return 0; |
2824 | } | |
2825 | ||
2826 | int setup_tmp_dirs(const char *id, char **tmp_dir, char **var_tmp_dir) { | |
56a13a49 ZJS |
2827 | _cleanup_(namespace_cleanup_tmpdirp) char *a = NULL; |
2828 | _cleanup_(rmdir_and_freep) char *a_tmp = NULL; | |
2829 | char *b; | |
613b411c LP |
2830 | int r; |
2831 | ||
2832 | assert(id); | |
2833 | assert(tmp_dir); | |
2834 | assert(var_tmp_dir); | |
2835 | ||
56a13a49 | 2836 | r = setup_one_tmp_dir(id, "/tmp", &a, &a_tmp); |
613b411c LP |
2837 | if (r < 0) |
2838 | return r; | |
2839 | ||
56a13a49 ZJS |
2840 | r = setup_one_tmp_dir(id, "/var/tmp", &b, NULL); |
2841 | if (r < 0) | |
613b411c | 2842 | return r; |
613b411c | 2843 | |
56a13a49 ZJS |
2844 | a_tmp = mfree(a_tmp); /* avoid rmdir */ |
2845 | *tmp_dir = TAKE_PTR(a); | |
2846 | *var_tmp_dir = TAKE_PTR(b); | |
613b411c LP |
2847 | |
2848 | return 0; | |
2849 | } | |
2850 | ||
13339577 | 2851 | int setup_shareable_ns(int ns_storage_socket[static 2], unsigned long nsflag) { |
5bb1d7fb | 2852 | _cleanup_close_ int ns = -EBADF; |
13339577 | 2853 | int r; |
54c2459d | 2854 | const char *ns_name, *ns_path; |
613b411c | 2855 | |
54c2459d XR |
2856 | assert(ns_storage_socket); |
2857 | assert(ns_storage_socket[0] >= 0); | |
2858 | assert(ns_storage_socket[1] >= 0); | |
2859 | ||
2860 | ns_name = namespace_single_flag_to_string(nsflag); | |
2861 | assert(ns_name); | |
613b411c LP |
2862 | |
2863 | /* We use the passed socketpair as a storage buffer for our | |
76cd584b LP |
2864 | * namespace reference fd. Whatever process runs this first |
2865 | * shall create a new namespace, all others should just join | |
2866 | * it. To serialize that we use a file lock on the socket | |
2867 | * pair. | |
613b411c LP |
2868 | * |
2869 | * It's a bit crazy, but hey, works great! */ | |
2870 | ||
13339577 DDM |
2871 | r = posix_lock(ns_storage_socket[0], LOCK_EX); |
2872 | if (r < 0) | |
2873 | return r; | |
613b411c | 2874 | |
13339577 | 2875 | CLEANUP_POSIX_UNLOCK(ns_storage_socket[0]); |
613b411c | 2876 | |
13339577 DDM |
2877 | ns = receive_one_fd(ns_storage_socket[0], MSG_PEEK|MSG_DONTWAIT); |
2878 | if (ns >= 0) { | |
2879 | /* Yay, found something, so let's join the namespace */ | |
2880 | r = RET_NERRNO(setns(ns, nsflag)); | |
2881 | if (r < 0) | |
2882 | return r; | |
613b411c | 2883 | |
13339577 DDM |
2884 | return 0; |
2885 | } | |
613b411c | 2886 | |
13339577 DDM |
2887 | if (ns != -EAGAIN) |
2888 | return ns; | |
613b411c | 2889 | |
13339577 | 2890 | /* Nothing stored yet, so let's create a new namespace. */ |
613b411c | 2891 | |
13339577 DDM |
2892 | if (unshare(nsflag) < 0) |
2893 | return -errno; | |
613b411c | 2894 | |
13339577 | 2895 | (void) loopback_setup(); |
613b411c | 2896 | |
13339577 DDM |
2897 | ns_path = strjoina("/proc/self/ns/", ns_name); |
2898 | ns = open(ns_path, O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
2899 | if (ns < 0) | |
2900 | return -errno; | |
613b411c | 2901 | |
13339577 DDM |
2902 | r = send_one_fd(ns_storage_socket[1], ns, MSG_DONTWAIT); |
2903 | if (r < 0) | |
2904 | return r; | |
613b411c | 2905 | |
13339577 | 2906 | return 1; |
15ae422b | 2907 | } |
417116f2 | 2908 | |
13339577 | 2909 | int open_shareable_ns_path(int ns_storage_socket[static 2], const char *path, unsigned long nsflag) { |
5bb1d7fb | 2910 | _cleanup_close_ int ns = -EBADF; |
13339577 | 2911 | int r; |
51af7fb2 | 2912 | |
54c2459d XR |
2913 | assert(ns_storage_socket); |
2914 | assert(ns_storage_socket[0] >= 0); | |
2915 | assert(ns_storage_socket[1] >= 0); | |
51af7fb2 LP |
2916 | assert(path); |
2917 | ||
54c2459d XR |
2918 | /* If the storage socket doesn't contain a ns fd yet, open one via the file system and store it in |
2919 | * it. This is supposed to be called ahead of time, i.e. before setup_shareable_ns() which will | |
2920 | * allocate a new anonymous ns if needed. */ | |
51af7fb2 | 2921 | |
13339577 DDM |
2922 | r = posix_lock(ns_storage_socket[0], LOCK_EX); |
2923 | if (r < 0) | |
2924 | return r; | |
51af7fb2 | 2925 | |
13339577 | 2926 | CLEANUP_POSIX_UNLOCK(ns_storage_socket[0]); |
51af7fb2 | 2927 | |
13339577 DDM |
2928 | ns = receive_one_fd(ns_storage_socket[0], MSG_PEEK|MSG_DONTWAIT); |
2929 | if (ns >= 0) | |
2930 | return 0; | |
2931 | if (ns != -EAGAIN) | |
2932 | return ns; | |
51af7fb2 | 2933 | |
13339577 | 2934 | /* Nothing stored yet. Open the file from the file system. */ |
51af7fb2 | 2935 | |
13339577 DDM |
2936 | ns = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC); |
2937 | if (ns < 0) | |
2938 | return -errno; | |
51af7fb2 | 2939 | |
13339577 DDM |
2940 | r = fd_is_ns(ns, nsflag); |
2941 | if (r == 0) | |
2942 | return -EINVAL; | |
2943 | if (r < 0 && r != -EUCLEAN) /* EUCLEAN: we don't know */ | |
2944 | return r; | |
51af7fb2 | 2945 | |
13339577 DDM |
2946 | r = send_one_fd(ns_storage_socket[1], ns, MSG_DONTWAIT); |
2947 | if (r < 0) | |
2948 | return r; | |
51af7fb2 | 2949 | |
13339577 | 2950 | return 1; |
51af7fb2 LP |
2951 | } |
2952 | ||
6e2d7c4f MS |
2953 | bool ns_type_supported(NamespaceType type) { |
2954 | const char *t, *ns_proc; | |
2955 | ||
0fa5b831 LP |
2956 | t = namespace_type_to_string(type); |
2957 | if (!t) /* Don't know how to translate this? Then it's not supported */ | |
6e2d7c4f MS |
2958 | return false; |
2959 | ||
6e2d7c4f | 2960 | ns_proc = strjoina("/proc/self/ns/", t); |
6e2d7c4f MS |
2961 | return access(ns_proc, F_OK) == 0; |
2962 | } | |
2963 | ||
1b8689f9 | 2964 | static const char *const protect_home_table[_PROTECT_HOME_MAX] = { |
cbc056c8 ZJS |
2965 | [PROTECT_HOME_NO] = "no", |
2966 | [PROTECT_HOME_YES] = "yes", | |
1b8689f9 | 2967 | [PROTECT_HOME_READ_ONLY] = "read-only", |
cbc056c8 | 2968 | [PROTECT_HOME_TMPFS] = "tmpfs", |
417116f2 LP |
2969 | }; |
2970 | ||
1e8c7bd5 | 2971 | DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(protect_home, ProtectHome, PROTECT_HOME_YES); |
5e1c6154 | 2972 | |
1b8689f9 | 2973 | static const char *const protect_system_table[_PROTECT_SYSTEM_MAX] = { |
cbc056c8 ZJS |
2974 | [PROTECT_SYSTEM_NO] = "no", |
2975 | [PROTECT_SYSTEM_YES] = "yes", | |
2976 | [PROTECT_SYSTEM_FULL] = "full", | |
3f815163 | 2977 | [PROTECT_SYSTEM_STRICT] = "strict", |
1b8689f9 LP |
2978 | }; |
2979 | ||
1e8c7bd5 | 2980 | DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(protect_system, ProtectSystem, PROTECT_SYSTEM_YES); |
03c791aa | 2981 | |
6e2d7c4f | 2982 | static const char* const namespace_type_table[] = { |
cbc056c8 | 2983 | [NAMESPACE_MOUNT] = "mnt", |
6e2d7c4f | 2984 | [NAMESPACE_CGROUP] = "cgroup", |
cbc056c8 ZJS |
2985 | [NAMESPACE_UTS] = "uts", |
2986 | [NAMESPACE_IPC] = "ipc", | |
2987 | [NAMESPACE_USER] = "user", | |
2988 | [NAMESPACE_PID] = "pid", | |
2989 | [NAMESPACE_NET] = "net", | |
c3b9c418 | 2990 | [NAMESPACE_TIME] = "time", |
6e2d7c4f MS |
2991 | }; |
2992 | ||
2993 | DEFINE_STRING_TABLE_LOOKUP(namespace_type, NamespaceType); | |
4e399953 LP |
2994 | |
2995 | static const char* const protect_proc_table[_PROTECT_PROC_MAX] = { | |
2996 | [PROTECT_PROC_DEFAULT] = "default", | |
2997 | [PROTECT_PROC_NOACCESS] = "noaccess", | |
2998 | [PROTECT_PROC_INVISIBLE] = "invisible", | |
2999 | [PROTECT_PROC_PTRACEABLE] = "ptraceable", | |
3000 | }; | |
3001 | ||
3002 | DEFINE_STRING_TABLE_LOOKUP(protect_proc, ProtectProc); | |
3003 | ||
3004 | static const char* const proc_subset_table[_PROC_SUBSET_MAX] = { | |
3005 | [PROC_SUBSET_ALL] = "all", | |
3006 | [PROC_SUBSET_PID] = "pid", | |
3007 | }; | |
3008 | ||
3009 | DEFINE_STRING_TABLE_LOOKUP(proc_subset, ProcSubset); |