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