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