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