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