]>
Commit | Line | Data |
---|---|---|
15ae422b LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
7 | under the terms of the GNU Lesser General Public License as published by |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
15ae422b LP |
9 | (at your option) any later version. |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 14 | Lesser General Public License for more details. |
15ae422b | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
15ae422b LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
20 | #include <errno.h> | |
07630cea | 21 | #include <sched.h> |
15ae422b | 22 | #include <stdio.h> |
07630cea LP |
23 | #include <string.h> |
24 | #include <sys/mount.h> | |
15ae422b | 25 | #include <sys/stat.h> |
07630cea | 26 | #include <unistd.h> |
25e870b5 | 27 | #include <linux/fs.h> |
15ae422b | 28 | |
b5efdb8a | 29 | #include "alloc-util.h" |
7f112f50 | 30 | #include "dev-setup.h" |
3ffd4af2 | 31 | #include "fd-util.h" |
d944dc95 | 32 | #include "fs-util.h" |
07630cea LP |
33 | #include "loopback-setup.h" |
34 | #include "missing.h" | |
35 | #include "mkdir.h" | |
4349cd7c | 36 | #include "mount-util.h" |
3ffd4af2 | 37 | #include "namespace.h" |
07630cea | 38 | #include "path-util.h" |
d7b8eec7 | 39 | #include "selinux-util.h" |
2583fbea | 40 | #include "socket-util.h" |
8b43440b | 41 | #include "string-table.h" |
07630cea LP |
42 | #include "string-util.h" |
43 | #include "strv.h" | |
affb60b1 | 44 | #include "umask-util.h" |
ee104e11 | 45 | #include "user-util.h" |
07630cea | 46 | #include "util.h" |
15ae422b | 47 | |
737ba3c8 | 48 | #define DEV_MOUNT_OPTIONS (MS_NOSUID|MS_STRICTATIME|MS_NOEXEC) |
49 | ||
c17ec25e | 50 | typedef enum MountMode { |
15ae422b LP |
51 | /* This is ordered by priority! */ |
52 | INACCESSIBLE, | |
53 | READONLY, | |
ac0930c8 LP |
54 | PRIVATE_TMP, |
55 | PRIVATE_VAR_TMP, | |
7f112f50 | 56 | PRIVATE_DEV, |
59eeb84b | 57 | READWRITE, |
c17ec25e | 58 | } MountMode; |
15ae422b | 59 | |
c17ec25e | 60 | typedef struct BindMount { |
f0a4feb0 | 61 | char *path; |
c17ec25e | 62 | MountMode mode; |
11a30cec | 63 | bool ignore; /* Ignore if path does not exist */ |
c17ec25e | 64 | } BindMount; |
15ae422b | 65 | |
11a30cec DH |
66 | typedef struct TargetMount { |
67 | const char *path; | |
68 | MountMode mode; | |
69 | bool ignore; /* Ignore if path does not exist */ | |
70 | } TargetMount; | |
71 | ||
f471b2af DH |
72 | /* |
73 | * The following Protect tables are to protect paths and mark some of them | |
74 | * READONLY, in case a path is covered by an option from another table, then | |
75 | * it is marked READWRITE in the current one, and the more restrictive mode is | |
76 | * applied from that other table. This way all options can be combined in a | |
77 | * safe and comprehensible way for users. | |
78 | */ | |
79 | ||
11a30cec DH |
80 | /* ProtectKernelTunables= option and the related filesystem APIs */ |
81 | static const TargetMount protect_kernel_tunables_table[] = { | |
82 | { "/proc/sys", READONLY, false }, | |
83 | { "/proc/sysrq-trigger", READONLY, true }, | |
49accde7 DH |
84 | { "/proc/latency_stats", READONLY, true }, |
85 | { "/proc/mtrr", READONLY, true }, | |
86 | { "/proc/apm", READONLY, true }, | |
87 | { "/proc/acpi", READONLY, true }, | |
88 | { "/proc/timer_stats", READONLY, true }, | |
89 | { "/proc/asound", READONLY, true }, | |
90 | { "/proc/bus", READONLY, true }, | |
91 | { "/proc/fs", READONLY, true }, | |
92 | { "/proc/irq", READONLY, true }, | |
11a30cec | 93 | { "/sys", READONLY, false }, |
49accde7 DH |
94 | { "/sys/kernel/debug", READONLY, true }, |
95 | { "/sys/kernel/tracing", READONLY, true }, | |
11a30cec DH |
96 | { "/sys/fs/cgroup", READWRITE, false }, /* READONLY is set by ProtectControlGroups= option */ |
97 | }; | |
98 | ||
c575770b DH |
99 | /* ProtectKernelModules= option */ |
100 | static const TargetMount protect_kernel_modules_table[] = { | |
101 | #ifdef HAVE_SPLIT_USR | |
102 | { "/lib/modules", INACCESSIBLE, true }, | |
103 | #endif | |
104 | { "/usr/lib/modules", INACCESSIBLE, true }, | |
105 | }; | |
106 | ||
b6c432ca DH |
107 | /* |
108 | * ProtectHome=read-only table, protect $HOME and $XDG_RUNTIME_DIR and rest of | |
109 | * system should be protected by ProtectSystem= | |
110 | */ | |
111 | static const TargetMount protect_home_read_only_table[] = { | |
112 | { "/home", READONLY, true }, | |
113 | { "/run/user", READONLY, true }, | |
114 | { "/root", READONLY, true }, | |
115 | }; | |
116 | ||
117 | /* ProtectHome=yes table */ | |
118 | static const TargetMount protect_home_yes_table[] = { | |
119 | { "/home", INACCESSIBLE, true }, | |
120 | { "/run/user", INACCESSIBLE, true }, | |
121 | { "/root", INACCESSIBLE, true }, | |
122 | }; | |
123 | ||
f471b2af DH |
124 | /* ProtectSystem=yes table */ |
125 | static const TargetMount protect_system_yes_table[] = { | |
126 | { "/usr", READONLY, false }, | |
127 | { "/boot", READONLY, true }, | |
128 | { "/efi", READONLY, true }, | |
129 | }; | |
130 | ||
131 | /* ProtectSystem=full includes ProtectSystem=yes */ | |
132 | static const TargetMount protect_system_full_table[] = { | |
133 | { "/usr", READONLY, false }, | |
134 | { "/boot", READONLY, true }, | |
135 | { "/efi", READONLY, true }, | |
136 | { "/etc", READONLY, false }, | |
137 | }; | |
138 | ||
139 | /* | |
140 | * ProtectSystem=strict table. In this strict mode, we mount everything | |
141 | * read-only, except for /proc, /dev, /sys which are the kernel API VFS, | |
142 | * which are left writable, but PrivateDevices= + ProtectKernelTunables= | |
143 | * protect those, and these options should be fully orthogonal. | |
144 | * (And of course /home and friends are also left writable, as ProtectHome= | |
145 | * shall manage those, orthogonally). | |
146 | */ | |
147 | static const TargetMount protect_system_strict_table[] = { | |
148 | { "/", READONLY, false }, | |
149 | { "/proc", READWRITE, false }, /* ProtectKernelTunables= */ | |
150 | { "/sys", READWRITE, false }, /* ProtectKernelTunables= */ | |
151 | { "/dev", READWRITE, false }, /* PrivateDevices= */ | |
152 | { "/home", READWRITE, true }, /* ProtectHome= */ | |
153 | { "/run/user", READWRITE, true }, /* ProtectHome= */ | |
154 | { "/root", READWRITE, true }, /* ProtectHome= */ | |
155 | }; | |
156 | ||
46c3230d ZJS |
157 | static void set_bind_mount(BindMount *p, char *path, MountMode mode, bool ignore) { |
158 | p->path = path; | |
159 | p->mode = mode; | |
160 | p->ignore = ignore; | |
f471b2af DH |
161 | } |
162 | ||
f0a4feb0 DH |
163 | static int append_one_mount(BindMount **p, const char *root_directory, |
164 | const char *path, MountMode mode, bool ignore) { | |
165 | char *lpath; | |
166 | assert(p); | |
167 | ||
168 | lpath = prefix_root(root_directory, path); | |
169 | if (!lpath) | |
170 | return -ENOMEM; | |
171 | ||
46c3230d | 172 | set_bind_mount((*p)++, lpath, mode, ignore); |
f0a4feb0 DH |
173 | return 0; |
174 | } | |
175 | ||
c17ec25e | 176 | static int append_mounts(BindMount **p, char **strv, MountMode mode) { |
15ae422b LP |
177 | char **i; |
178 | ||
613b411c LP |
179 | assert(p); |
180 | ||
15ae422b | 181 | STRV_FOREACH(i, strv) { |
9c94d52e | 182 | bool ignore = false; |
f0a4feb0 | 183 | char *path; |
15ae422b | 184 | |
9c94d52e | 185 | if (IN_SET(mode, INACCESSIBLE, READONLY, READWRITE) && startswith(*i, "-")) { |
ea92ae33 | 186 | (*i)++; |
9c94d52e | 187 | ignore = true; |
ea92ae33 MW |
188 | } |
189 | ||
15ae422b LP |
190 | if (!path_is_absolute(*i)) |
191 | return -EINVAL; | |
192 | ||
f0a4feb0 DH |
193 | path = strdup(*i); |
194 | if (!path) | |
195 | return -ENOMEM; | |
196 | ||
46c3230d | 197 | set_bind_mount((*p)++, path, mode, ignore); |
15ae422b LP |
198 | } |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
af964954 DH |
203 | static int append_target_mounts(BindMount **p, const char *root_directory, |
204 | const TargetMount *mounts, const size_t size, bool ignore_protect) { | |
f471b2af | 205 | unsigned i; |
11a30cec DH |
206 | |
207 | assert(p); | |
f471b2af | 208 | assert(mounts); |
11a30cec | 209 | |
f471b2af | 210 | for (i = 0; i < size; i++) { |
af964954 | 211 | bool ignore; |
f471b2af DH |
212 | /* |
213 | * Here we assume that the ignore field is set during | |
214 | * declaration we do not support "-" at the beginning. | |
215 | */ | |
216 | const TargetMount *m = &mounts[i]; | |
f0a4feb0 DH |
217 | char *path; |
218 | ||
219 | path = prefix_root(root_directory, m->path); | |
220 | if (!path) | |
221 | return -ENOMEM; | |
f471b2af DH |
222 | |
223 | if (!path_is_absolute(path)) | |
224 | return -EINVAL; | |
225 | ||
af964954 DH |
226 | /* |
227 | * Ignore paths if they are not present. First we use our | |
228 | * static tables otherwise fallback to Unit context. | |
229 | */ | |
230 | ignore = m->ignore || ignore_protect; | |
231 | ||
232 | set_bind_mount((*p)++, path, m->mode, ignore); | |
11a30cec | 233 | } |
f471b2af DH |
234 | |
235 | return 0; | |
236 | } | |
237 | ||
af964954 | 238 | static int append_protect_kernel_tunables(BindMount **p, const char *root_directory, bool ignore_protect) { |
f471b2af DH |
239 | assert(p); |
240 | ||
241 | return append_target_mounts(p, root_directory, protect_kernel_tunables_table, | |
af964954 | 242 | ELEMENTSOF(protect_kernel_tunables_table), ignore_protect); |
f471b2af DH |
243 | } |
244 | ||
af964954 | 245 | static int append_protect_kernel_modules(BindMount **p, const char *root_directory, bool ignore_protect) { |
c575770b DH |
246 | assert(p); |
247 | ||
248 | return append_target_mounts(p, root_directory, protect_kernel_modules_table, | |
af964954 | 249 | ELEMENTSOF(protect_kernel_modules_table), ignore_protect); |
c575770b DH |
250 | } |
251 | ||
af964954 | 252 | static int append_protect_home(BindMount **p, const char *root_directory, ProtectHome protect_home, bool ignore_protect) { |
b6c432ca DH |
253 | int r = 0; |
254 | ||
255 | assert(p); | |
256 | ||
257 | if (protect_home == PROTECT_HOME_NO) | |
258 | return 0; | |
259 | ||
260 | switch (protect_home) { | |
261 | case PROTECT_HOME_READ_ONLY: | |
262 | r = append_target_mounts(p, root_directory, protect_home_read_only_table, | |
af964954 DH |
263 | ELEMENTSOF(protect_home_read_only_table), |
264 | ignore_protect); | |
b6c432ca DH |
265 | break; |
266 | case PROTECT_HOME_YES: | |
267 | r = append_target_mounts(p, root_directory, protect_home_yes_table, | |
af964954 | 268 | ELEMENTSOF(protect_home_yes_table), ignore_protect); |
b6c432ca DH |
269 | break; |
270 | default: | |
271 | r = -EINVAL; | |
272 | break; | |
273 | } | |
274 | ||
275 | return r; | |
276 | } | |
277 | ||
af964954 | 278 | static int append_protect_system(BindMount **p, const char *root_directory, ProtectSystem protect_system, bool ignore_protect) { |
f471b2af DH |
279 | int r = 0; |
280 | ||
281 | assert(p); | |
282 | ||
283 | if (protect_system == PROTECT_SYSTEM_NO) | |
284 | return 0; | |
285 | ||
286 | switch (protect_system) { | |
287 | case PROTECT_SYSTEM_STRICT: | |
288 | r = append_target_mounts(p, root_directory, protect_system_strict_table, | |
af964954 | 289 | ELEMENTSOF(protect_system_strict_table), ignore_protect); |
f471b2af DH |
290 | break; |
291 | case PROTECT_SYSTEM_YES: | |
292 | r = append_target_mounts(p, root_directory, protect_system_yes_table, | |
af964954 | 293 | ELEMENTSOF(protect_system_yes_table), ignore_protect); |
f471b2af DH |
294 | break; |
295 | case PROTECT_SYSTEM_FULL: | |
296 | r = append_target_mounts(p, root_directory, protect_system_full_table, | |
af964954 | 297 | ELEMENTSOF(protect_system_full_table), ignore_protect); |
f471b2af DH |
298 | break; |
299 | default: | |
300 | r = -EINVAL; | |
301 | break; | |
302 | } | |
303 | ||
304 | return r; | |
11a30cec DH |
305 | } |
306 | ||
c17ec25e MS |
307 | static int mount_path_compare(const void *a, const void *b) { |
308 | const BindMount *p = a, *q = b; | |
a0827e2b | 309 | int d; |
15ae422b | 310 | |
6ee1a919 | 311 | /* If the paths are not equal, then order prefixes first */ |
a0827e2b | 312 | d = path_compare(p->path, q->path); |
6ee1a919 LP |
313 | if (d != 0) |
314 | return d; | |
15ae422b | 315 | |
6ee1a919 LP |
316 | /* If the paths are equal, check the mode */ |
317 | if (p->mode < q->mode) | |
318 | return -1; | |
15ae422b | 319 | |
6ee1a919 LP |
320 | if (p->mode > q->mode) |
321 | return 1; | |
15ae422b | 322 | |
6ee1a919 | 323 | return 0; |
15ae422b LP |
324 | } |
325 | ||
c17ec25e MS |
326 | static void drop_duplicates(BindMount *m, unsigned *n) { |
327 | BindMount *f, *t, *previous; | |
15ae422b | 328 | |
c17ec25e | 329 | assert(m); |
15ae422b | 330 | assert(n); |
15ae422b | 331 | |
fe3c2583 LP |
332 | /* Drops duplicate entries. Expects that the array is properly ordered already. */ |
333 | ||
1d54cd5d | 334 | for (f = m, t = m, previous = NULL; f < m + *n; f++) { |
15ae422b | 335 | |
fe3c2583 LP |
336 | /* The first one wins (which is the one with the more restrictive mode), see mount_path_compare() |
337 | * above. */ | |
338 | if (previous && path_equal(f->path, previous->path)) { | |
339 | log_debug("%s is duplicate.", f->path); | |
f0a4feb0 | 340 | f->path = mfree(f->path); |
15ae422b | 341 | continue; |
fe3c2583 | 342 | } |
15ae422b | 343 | |
e2d7c1a0 | 344 | *t = *f; |
15ae422b | 345 | previous = t; |
fe3c2583 LP |
346 | t++; |
347 | } | |
348 | ||
349 | *n = t - m; | |
350 | } | |
351 | ||
352 | static void drop_inaccessible(BindMount *m, unsigned *n) { | |
353 | BindMount *f, *t; | |
354 | const char *clear = NULL; | |
355 | ||
356 | assert(m); | |
357 | assert(n); | |
358 | ||
359 | /* Drops all entries obstructed by another entry further up the tree. Expects that the array is properly | |
360 | * ordered already. */ | |
361 | ||
1d54cd5d | 362 | for (f = m, t = m; f < m + *n; f++) { |
fe3c2583 LP |
363 | |
364 | /* If we found a path set for INACCESSIBLE earlier, and this entry has it as prefix we should drop | |
365 | * it, as inaccessible paths really should drop the entire subtree. */ | |
366 | if (clear && path_startswith(f->path, clear)) { | |
367 | log_debug("%s is masked by %s.", f->path, clear); | |
f0a4feb0 | 368 | f->path = mfree(f->path); |
fe3c2583 LP |
369 | continue; |
370 | } | |
15ae422b | 371 | |
fe3c2583 LP |
372 | clear = f->mode == INACCESSIBLE ? f->path : NULL; |
373 | ||
374 | *t = *f; | |
15ae422b LP |
375 | t++; |
376 | } | |
377 | ||
c17ec25e | 378 | *n = t - m; |
15ae422b LP |
379 | } |
380 | ||
7648a565 LP |
381 | static void drop_nop(BindMount *m, unsigned *n) { |
382 | BindMount *f, *t; | |
383 | ||
384 | assert(m); | |
385 | assert(n); | |
386 | ||
387 | /* Drops all entries which have an immediate parent that has the same type, as they are redundant. Assumes the | |
388 | * list is ordered by prefixes. */ | |
389 | ||
1d54cd5d | 390 | for (f = m, t = m; f < m + *n; f++) { |
7648a565 LP |
391 | |
392 | /* Only suppress such subtrees for READONLY and READWRITE entries */ | |
393 | if (IN_SET(f->mode, READONLY, READWRITE)) { | |
394 | BindMount *p; | |
395 | bool found = false; | |
396 | ||
397 | /* Now let's find the first parent of the entry we are looking at. */ | |
398 | for (p = t-1; p >= m; p--) { | |
399 | if (path_startswith(f->path, p->path)) { | |
400 | found = true; | |
401 | break; | |
402 | } | |
403 | } | |
404 | ||
405 | /* We found it, let's see if it's the same mode, if so, we can drop this entry */ | |
406 | if (found && p->mode == f->mode) { | |
407 | log_debug("%s is redundant by %s", f->path, p->path); | |
f0a4feb0 | 408 | f->path = mfree(f->path); |
7648a565 LP |
409 | continue; |
410 | } | |
411 | } | |
412 | ||
413 | *t = *f; | |
414 | t++; | |
415 | } | |
416 | ||
417 | *n = t - m; | |
418 | } | |
419 | ||
cd2902c9 LP |
420 | static void drop_outside_root(const char *root_directory, BindMount *m, unsigned *n) { |
421 | BindMount *f, *t; | |
422 | ||
423 | assert(m); | |
424 | assert(n); | |
425 | ||
1d54cd5d | 426 | /* Nothing to do */ |
cd2902c9 LP |
427 | if (!root_directory) |
428 | return; | |
429 | ||
430 | /* Drops all mounts that are outside of the root directory. */ | |
431 | ||
1d54cd5d | 432 | for (f = m, t = m; f < m + *n; f++) { |
cd2902c9 LP |
433 | |
434 | if (!path_startswith(f->path, root_directory)) { | |
435 | log_debug("%s is outside of root directory.", f->path); | |
f0a4feb0 | 436 | f->path = mfree(f->path); |
cd2902c9 LP |
437 | continue; |
438 | } | |
439 | ||
440 | *t = *f; | |
441 | t++; | |
442 | } | |
443 | ||
444 | *n = t - m; | |
445 | } | |
446 | ||
7f112f50 LP |
447 | static int mount_dev(BindMount *m) { |
448 | static const char devnodes[] = | |
449 | "/dev/null\0" | |
450 | "/dev/zero\0" | |
451 | "/dev/full\0" | |
452 | "/dev/random\0" | |
453 | "/dev/urandom\0" | |
454 | "/dev/tty\0"; | |
455 | ||
2b85f4e1 | 456 | char temporary_mount[] = "/tmp/namespace-dev-XXXXXX"; |
63cc4c31 | 457 | const char *d, *dev = NULL, *devpts = NULL, *devshm = NULL, *devhugepages = NULL, *devmqueue = NULL, *devlog = NULL, *devptmx = NULL; |
7f112f50 LP |
458 | _cleanup_umask_ mode_t u; |
459 | int r; | |
460 | ||
461 | assert(m); | |
462 | ||
463 | u = umask(0000); | |
464 | ||
2b85f4e1 LP |
465 | if (!mkdtemp(temporary_mount)) |
466 | return -errno; | |
467 | ||
63c372cb | 468 | dev = strjoina(temporary_mount, "/dev"); |
dc751688 | 469 | (void) mkdir(dev, 0755); |
737ba3c8 | 470 | if (mount("tmpfs", dev, "tmpfs", DEV_MOUNT_OPTIONS, "mode=755") < 0) { |
2b85f4e1 LP |
471 | r = -errno; |
472 | goto fail; | |
473 | } | |
474 | ||
63c372cb | 475 | devpts = strjoina(temporary_mount, "/dev/pts"); |
dc751688 | 476 | (void) mkdir(devpts, 0755); |
2b85f4e1 LP |
477 | if (mount("/dev/pts", devpts, NULL, MS_BIND, NULL) < 0) { |
478 | r = -errno; | |
479 | goto fail; | |
480 | } | |
481 | ||
63c372cb | 482 | devptmx = strjoina(temporary_mount, "/dev/ptmx"); |
3164e3cb ZJS |
483 | if (symlink("pts/ptmx", devptmx) < 0) { |
484 | r = -errno; | |
485 | goto fail; | |
486 | } | |
e06b6479 | 487 | |
63c372cb | 488 | devshm = strjoina(temporary_mount, "/dev/shm"); |
dc751688 | 489 | (void) mkdir(devshm, 01777); |
2b85f4e1 LP |
490 | r = mount("/dev/shm", devshm, NULL, MS_BIND, NULL); |
491 | if (r < 0) { | |
492 | r = -errno; | |
493 | goto fail; | |
494 | } | |
495 | ||
63c372cb | 496 | devmqueue = strjoina(temporary_mount, "/dev/mqueue"); |
dc751688 | 497 | (void) mkdir(devmqueue, 0755); |
3164e3cb | 498 | (void) mount("/dev/mqueue", devmqueue, NULL, MS_BIND, NULL); |
2b85f4e1 | 499 | |
63c372cb | 500 | devhugepages = strjoina(temporary_mount, "/dev/hugepages"); |
dc751688 | 501 | (void) mkdir(devhugepages, 0755); |
3164e3cb | 502 | (void) mount("/dev/hugepages", devhugepages, NULL, MS_BIND, NULL); |
2b85f4e1 | 503 | |
63c372cb | 504 | devlog = strjoina(temporary_mount, "/dev/log"); |
3164e3cb | 505 | (void) symlink("/run/systemd/journal/dev-log", devlog); |
82d25240 | 506 | |
7f112f50 | 507 | NULSTR_FOREACH(d, devnodes) { |
2b85f4e1 LP |
508 | _cleanup_free_ char *dn = NULL; |
509 | struct stat st; | |
510 | ||
511 | r = stat(d, &st); | |
7f112f50 | 512 | if (r < 0) { |
2b85f4e1 LP |
513 | |
514 | if (errno == ENOENT) | |
515 | continue; | |
516 | ||
517 | r = -errno; | |
518 | goto fail; | |
7f112f50 LP |
519 | } |
520 | ||
2b85f4e1 LP |
521 | if (!S_ISBLK(st.st_mode) && |
522 | !S_ISCHR(st.st_mode)) { | |
523 | r = -EINVAL; | |
524 | goto fail; | |
525 | } | |
526 | ||
527 | if (st.st_rdev == 0) | |
528 | continue; | |
529 | ||
530 | dn = strappend(temporary_mount, d); | |
531 | if (!dn) { | |
532 | r = -ENOMEM; | |
533 | goto fail; | |
534 | } | |
535 | ||
ecabcf8b | 536 | mac_selinux_create_file_prepare(d, st.st_mode); |
2b85f4e1 | 537 | r = mknod(dn, st.st_mode, st.st_rdev); |
ecabcf8b | 538 | mac_selinux_create_file_clear(); |
dd078a1e | 539 | |
2b85f4e1 LP |
540 | if (r < 0) { |
541 | r = -errno; | |
542 | goto fail; | |
543 | } | |
7f112f50 LP |
544 | } |
545 | ||
03cfe0d5 | 546 | dev_setup(temporary_mount, UID_INVALID, GID_INVALID); |
7f112f50 | 547 | |
ee818b89 AC |
548 | /* Create the /dev directory if missing. It is more likely to be |
549 | * missing when the service is started with RootDirectory. This is | |
550 | * consistent with mount units creating the mount points when missing. | |
551 | */ | |
552 | (void) mkdir_p_label(m->path, 0755); | |
553 | ||
9e5f8252 | 554 | /* Unmount everything in old /dev */ |
555 | umount_recursive(m->path, 0); | |
ee818b89 | 556 | if (mount(dev, m->path, NULL, MS_MOVE, NULL) < 0) { |
2b85f4e1 LP |
557 | r = -errno; |
558 | goto fail; | |
559 | } | |
7f112f50 | 560 | |
2b85f4e1 LP |
561 | rmdir(dev); |
562 | rmdir(temporary_mount); | |
7f112f50 | 563 | |
2b85f4e1 | 564 | return 0; |
7f112f50 | 565 | |
2b85f4e1 LP |
566 | fail: |
567 | if (devpts) | |
568 | umount(devpts); | |
7f112f50 | 569 | |
2b85f4e1 LP |
570 | if (devshm) |
571 | umount(devshm); | |
7f112f50 | 572 | |
2b85f4e1 LP |
573 | if (devhugepages) |
574 | umount(devhugepages); | |
7f112f50 | 575 | |
2b85f4e1 LP |
576 | if (devmqueue) |
577 | umount(devmqueue); | |
7f112f50 | 578 | |
d267c5aa ZJS |
579 | umount(dev); |
580 | rmdir(dev); | |
2b85f4e1 | 581 | rmdir(temporary_mount); |
7f112f50 | 582 | |
2b85f4e1 | 583 | return r; |
7f112f50 LP |
584 | } |
585 | ||
ac0930c8 | 586 | static int apply_mount( |
c17ec25e | 587 | BindMount *m, |
ac0930c8 | 588 | const char *tmp_dir, |
c17ec25e | 589 | const char *var_tmp_dir) { |
ac0930c8 | 590 | |
15ae422b | 591 | const char *what; |
15ae422b | 592 | int r; |
15ae422b | 593 | |
c17ec25e | 594 | assert(m); |
15ae422b | 595 | |
fe3c2583 LP |
596 | log_debug("Applying namespace mount on %s", m->path); |
597 | ||
c17ec25e | 598 | switch (m->mode) { |
15ae422b | 599 | |
160cfdbe LP |
600 | case INACCESSIBLE: { |
601 | struct stat target; | |
6d313367 LP |
602 | |
603 | /* First, get rid of everything that is below if there | |
604 | * is anything... Then, overmount it with an | |
c4b41707 | 605 | * inaccessible path. */ |
fe3c2583 | 606 | (void) umount_recursive(m->path, 0); |
6d313367 | 607 | |
d944dc95 | 608 | if (lstat(m->path, &target) < 0) |
160cfdbe | 609 | return log_debug_errno(errno, "Failed to lstat() %s to determine what to mount over it: %m", m->path); |
15ae422b | 610 | |
c4b41707 | 611 | what = mode_to_inaccessible_node(target.st_mode); |
5fd7cf6f LP |
612 | if (!what) { |
613 | log_debug("File type not supported for inaccessible mounts. Note that symlinks are not allowed"); | |
c4b41707 AP |
614 | return -ELOOP; |
615 | } | |
616 | break; | |
160cfdbe | 617 | } |
fe3c2583 | 618 | |
15ae422b | 619 | case READONLY: |
15ae422b | 620 | case READWRITE: |
6b7c9f8b LP |
621 | |
622 | r = path_is_mount_point(m->path, 0); | |
d944dc95 | 623 | if (r < 0) |
6b7c9f8b | 624 | return log_debug_errno(r, "Failed to determine whether %s is already a mount point: %m", m->path); |
6b7c9f8b LP |
625 | if (r > 0) /* Nothing to do here, it is already a mount. We just later toggle the MS_RDONLY bit for the mount point if needed. */ |
626 | return 0; | |
6b7c9f8b LP |
627 | /* This isn't a mount point yet, let's make it one. */ |
628 | what = m->path; | |
629 | break; | |
15ae422b | 630 | |
ac0930c8 LP |
631 | case PRIVATE_TMP: |
632 | what = tmp_dir; | |
633 | break; | |
634 | ||
635 | case PRIVATE_VAR_TMP: | |
636 | what = var_tmp_dir; | |
15ae422b | 637 | break; |
e364ad06 | 638 | |
d6797c92 LP |
639 | case PRIVATE_DEV: |
640 | return mount_dev(m); | |
641 | ||
e364ad06 LP |
642 | default: |
643 | assert_not_reached("Unknown mode"); | |
15ae422b LP |
644 | } |
645 | ||
ac0930c8 | 646 | assert(what); |
15ae422b | 647 | |
d944dc95 | 648 | if (mount(what, m->path, NULL, MS_BIND|MS_REC, NULL) < 0) |
5fd7cf6f | 649 | return log_debug_errno(errno, "Failed to mount %s to %s: %m", what, m->path); |
6b7c9f8b LP |
650 | |
651 | log_debug("Successfully mounted %s to %s", what, m->path); | |
652 | return 0; | |
ac0930c8 | 653 | } |
15ae422b | 654 | |
6b7c9f8b LP |
655 | static int make_read_only(BindMount *m, char **blacklist) { |
656 | int r = 0; | |
15ae422b | 657 | |
c17ec25e | 658 | assert(m); |
ac0930c8 | 659 | |
d6797c92 | 660 | if (IN_SET(m->mode, INACCESSIBLE, READONLY)) |
6b7c9f8b LP |
661 | r = bind_remount_recursive(m->path, true, blacklist); |
662 | else if (m->mode == PRIVATE_DEV) { /* Can be readonly but the submounts can't*/ | |
663 | if (mount(NULL, m->path, NULL, MS_REMOUNT|DEV_MOUNT_OPTIONS|MS_RDONLY, NULL) < 0) | |
664 | r = -errno; | |
737ba3c8 | 665 | } else |
6b7c9f8b LP |
666 | return 0; |
667 | ||
668 | /* Not that we only turn on the MS_RDONLY flag here, we never turn it off. Something that was marked read-only | |
669 | * already stays this way. This improves compatibility with container managers, where we won't attempt to undo | |
670 | * read-only mounts already applied. */ | |
ac0930c8 | 671 | |
d6797c92 | 672 | return r; |
15ae422b LP |
673 | } |
674 | ||
1d54cd5d | 675 | /* Chase symlinks and remove failed paths from mounts */ |
d944dc95 LP |
676 | static int chase_all_symlinks(const char *root_directory, BindMount *m, unsigned *n) { |
677 | BindMount *f, *t; | |
1d54cd5d | 678 | int r = 0; |
d944dc95 LP |
679 | |
680 | assert(m); | |
681 | assert(n); | |
682 | ||
683 | /* Since mount() will always follow symlinks and we need to take the different root directory into account we | |
684 | * chase the symlinks on our own first. This call wil do so for all entries and remove all entries where we | |
685 | * can't resolve the path, and which have been marked for such removal. */ | |
686 | ||
49fedb40 | 687 | for (f = m, t = m; f < m + *n; f++) { |
1d54cd5d | 688 | int k; |
f0a4feb0 | 689 | _cleanup_free_ char *chased = NULL; |
49fedb40 | 690 | |
1d54cd5d DH |
691 | k = chase_symlinks(f->path, root_directory, &chased); |
692 | if (k < 0) { | |
693 | /* Get only real errors */ | |
694 | if (r >= 0 && (k != -ENOENT || !f->ignore)) | |
695 | r = k; | |
696 | ||
697 | log_debug_errno(r, "Failed to chase symlinks for %s: %m", f->path); | |
698 | /* Doesn't exist or failed? Then remove it and continue! */ | |
f0a4feb0 | 699 | f->path = mfree(f->path); |
d944dc95 | 700 | continue; |
f0a4feb0 | 701 | } |
d944dc95 | 702 | |
f0a4feb0 DH |
703 | if (!path_equal(f->path, chased)) { |
704 | log_debug("Chased %s → %s", f->path, chased); | |
1d54cd5d | 705 | free_and_replace(f->path, chased); |
d944dc95 LP |
706 | } |
707 | ||
708 | *t = *f; | |
709 | t++; | |
710 | } | |
711 | ||
712 | *n = t - m; | |
1d54cd5d | 713 | return r; |
d944dc95 LP |
714 | } |
715 | ||
2652c6c1 | 716 | static unsigned namespace_calculate_mounts( |
c575770b | 717 | const NameSpaceInfo *ns_info, |
2652c6c1 DH |
718 | char** read_write_paths, |
719 | char** read_only_paths, | |
720 | char** inaccessible_paths, | |
721 | const char* tmp_dir, | |
722 | const char* var_tmp_dir, | |
2652c6c1 DH |
723 | ProtectHome protect_home, |
724 | ProtectSystem protect_system) { | |
725 | ||
b6c432ca | 726 | unsigned protect_home_cnt; |
f471b2af DH |
727 | unsigned protect_system_cnt = |
728 | (protect_system == PROTECT_SYSTEM_STRICT ? | |
729 | ELEMENTSOF(protect_system_strict_table) : | |
730 | ((protect_system == PROTECT_SYSTEM_FULL) ? | |
731 | ELEMENTSOF(protect_system_full_table) : | |
732 | ((protect_system == PROTECT_SYSTEM_YES) ? | |
733 | ELEMENTSOF(protect_system_yes_table) : 0))); | |
734 | ||
b6c432ca DH |
735 | protect_home_cnt = |
736 | (protect_home == PROTECT_HOME_YES ? | |
737 | ELEMENTSOF(protect_home_yes_table) : | |
738 | ((protect_home == PROTECT_HOME_READ_ONLY) ? | |
739 | ELEMENTSOF(protect_home_read_only_table) : 0)); | |
740 | ||
2652c6c1 DH |
741 | return !!tmp_dir + !!var_tmp_dir + |
742 | strv_length(read_write_paths) + | |
743 | strv_length(read_only_paths) + | |
744 | strv_length(inaccessible_paths) + | |
c575770b DH |
745 | ns_info->private_dev + |
746 | (ns_info->protect_kernel_tunables ? ELEMENTSOF(protect_kernel_tunables_table) : 0) + | |
747 | (ns_info->protect_control_groups ? 1 : 0) + | |
748 | (ns_info->protect_kernel_modules ? ELEMENTSOF(protect_kernel_modules_table) : 0) + | |
b6c432ca | 749 | protect_home_cnt + protect_system_cnt; |
2652c6c1 DH |
750 | } |
751 | ||
613b411c | 752 | int setup_namespace( |
ee818b89 | 753 | const char* root_directory, |
c575770b | 754 | const NameSpaceInfo *ns_info, |
2a624c36 AP |
755 | char** read_write_paths, |
756 | char** read_only_paths, | |
757 | char** inaccessible_paths, | |
a004cb4c LP |
758 | const char* tmp_dir, |
759 | const char* var_tmp_dir, | |
1b8689f9 LP |
760 | ProtectHome protect_home, |
761 | ProtectSystem protect_system, | |
e6547662 | 762 | unsigned long mount_flags) { |
15ae422b | 763 | |
7ff7394d | 764 | BindMount *m, *mounts = NULL; |
d944dc95 | 765 | bool make_slave = false; |
f0a4feb0 | 766 | unsigned n_mounts; |
c17ec25e | 767 | int r = 0; |
15ae422b | 768 | |
613b411c | 769 | if (mount_flags == 0) |
c17ec25e | 770 | mount_flags = MS_SHARED; |
ac0930c8 | 771 | |
f0a4feb0 DH |
772 | n_mounts = namespace_calculate_mounts(ns_info, |
773 | read_write_paths, | |
774 | read_only_paths, | |
775 | inaccessible_paths, | |
776 | tmp_dir, var_tmp_dir, | |
777 | protect_home, protect_system); | |
613b411c | 778 | |
2652c6c1 | 779 | /* Set mount slave mode */ |
f0a4feb0 | 780 | if (root_directory || n_mounts > 0) |
d944dc95 LP |
781 | make_slave = true; |
782 | ||
f0a4feb0 DH |
783 | if (n_mounts > 0) { |
784 | m = mounts = (BindMount *) alloca0(n_mounts * sizeof(BindMount)); | |
2a624c36 | 785 | r = append_mounts(&m, read_write_paths, READWRITE); |
613b411c | 786 | if (r < 0) |
f0a4feb0 | 787 | goto finish; |
613b411c | 788 | |
2a624c36 | 789 | r = append_mounts(&m, read_only_paths, READONLY); |
613b411c | 790 | if (r < 0) |
f0a4feb0 | 791 | goto finish; |
613b411c | 792 | |
2a624c36 | 793 | r = append_mounts(&m, inaccessible_paths, INACCESSIBLE); |
613b411c | 794 | if (r < 0) |
f0a4feb0 | 795 | goto finish; |
7ff7394d | 796 | |
613b411c | 797 | if (tmp_dir) { |
f0a4feb0 DH |
798 | r = append_one_mount(&m, root_directory, "/tmp", PRIVATE_TMP, false); |
799 | if (r < 0) | |
800 | goto finish; | |
613b411c | 801 | } |
7ff7394d | 802 | |
613b411c | 803 | if (var_tmp_dir) { |
f0a4feb0 DH |
804 | r = append_one_mount(&m, root_directory, "/var/tmp", PRIVATE_VAR_TMP, false); |
805 | if (r < 0) | |
806 | goto finish; | |
7ff7394d | 807 | } |
ac0930c8 | 808 | |
c575770b | 809 | if (ns_info->private_dev) { |
f0a4feb0 DH |
810 | r = append_one_mount(&m, root_directory, "/dev", PRIVATE_DEV, false); |
811 | if (r < 0) | |
812 | goto finish; | |
7f112f50 LP |
813 | } |
814 | ||
c575770b | 815 | if (ns_info->protect_kernel_tunables) { |
af964954 DH |
816 | r = append_protect_kernel_tunables(&m, root_directory, |
817 | ns_info->ignore_protect_paths); | |
c575770b | 818 | if (r < 0) |
f0a4feb0 | 819 | goto finish; |
c575770b DH |
820 | } |
821 | ||
822 | if (ns_info->protect_kernel_modules) { | |
af964954 DH |
823 | r = append_protect_kernel_modules(&m, root_directory, |
824 | ns_info->ignore_protect_paths); | |
c575770b | 825 | if (r < 0) |
f0a4feb0 | 826 | goto finish; |
c575770b | 827 | } |
59eeb84b | 828 | |
c575770b | 829 | if (ns_info->protect_control_groups) { |
f0a4feb0 DH |
830 | r = append_one_mount(&m, root_directory, "/sys/fs/cgroup", READONLY, false); |
831 | if (r < 0) | |
832 | goto finish; | |
59eeb84b LP |
833 | } |
834 | ||
af964954 DH |
835 | r = append_protect_home(&m, root_directory, protect_home, |
836 | ns_info->ignore_protect_paths); | |
b6c432ca | 837 | if (r < 0) |
f0a4feb0 | 838 | goto finish; |
417116f2 | 839 | |
af964954 | 840 | r = append_protect_system(&m, root_directory, protect_system, false); |
f471b2af | 841 | if (r < 0) |
f0a4feb0 | 842 | goto finish; |
417116f2 | 843 | |
f0a4feb0 | 844 | assert(mounts + n_mounts == m); |
ac0930c8 | 845 | |
d944dc95 LP |
846 | /* Resolve symlinks manually first, as mount() will always follow them relative to the host's |
847 | * root. Moreover we want to suppress duplicates based on the resolved paths. This of course is a bit | |
848 | * racy. */ | |
f0a4feb0 | 849 | r = chase_all_symlinks(root_directory, mounts, &n_mounts); |
d944dc95 LP |
850 | if (r < 0) |
851 | goto finish; | |
852 | ||
f0a4feb0 | 853 | qsort(mounts, n_mounts, sizeof(BindMount), mount_path_compare); |
fe3c2583 | 854 | |
f0a4feb0 DH |
855 | drop_duplicates(mounts, &n_mounts); |
856 | drop_outside_root(root_directory, mounts, &n_mounts); | |
857 | drop_inaccessible(mounts, &n_mounts); | |
858 | drop_nop(mounts, &n_mounts); | |
15ae422b LP |
859 | } |
860 | ||
d944dc95 LP |
861 | if (unshare(CLONE_NEWNS) < 0) { |
862 | r = -errno; | |
863 | goto finish; | |
864 | } | |
1e4e94c8 | 865 | |
d944dc95 | 866 | if (make_slave) { |
c2c13f2d LP |
867 | /* Remount / as SLAVE so that nothing now mounted in the namespace |
868 | shows up in the parent */ | |
d944dc95 LP |
869 | if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) { |
870 | r = -errno; | |
871 | goto finish; | |
872 | } | |
ee818b89 AC |
873 | } |
874 | ||
875 | if (root_directory) { | |
8f1ad200 LP |
876 | /* Turn directory into bind mount, if it isn't one yet */ |
877 | r = path_is_mount_point(root_directory, AT_SYMLINK_FOLLOW); | |
878 | if (r < 0) | |
d944dc95 | 879 | goto finish; |
8f1ad200 LP |
880 | if (r == 0) { |
881 | if (mount(root_directory, root_directory, NULL, MS_BIND|MS_REC, NULL) < 0) { | |
882 | r = -errno; | |
883 | goto finish; | |
884 | } | |
d944dc95 | 885 | } |
ee818b89 | 886 | } |
c2c13f2d | 887 | |
f0a4feb0 | 888 | if (n_mounts > 0) { |
6b7c9f8b LP |
889 | char **blacklist; |
890 | unsigned j; | |
891 | ||
892 | /* First round, add in all special mounts we need */ | |
f0a4feb0 | 893 | for (m = mounts; m < mounts + n_mounts; ++m) { |
c2c13f2d LP |
894 | r = apply_mount(m, tmp_dir, var_tmp_dir); |
895 | if (r < 0) | |
d944dc95 | 896 | goto finish; |
c2c13f2d | 897 | } |
15ae422b | 898 | |
6b7c9f8b | 899 | /* Create a blacklist we can pass to bind_mount_recursive() */ |
f0a4feb0 DH |
900 | blacklist = newa(char*, n_mounts+1); |
901 | for (j = 0; j < n_mounts; j++) | |
6b7c9f8b LP |
902 | blacklist[j] = (char*) mounts[j].path; |
903 | blacklist[j] = NULL; | |
904 | ||
905 | /* Second round, flip the ro bits if necessary. */ | |
f0a4feb0 | 906 | for (m = mounts; m < mounts + n_mounts; ++m) { |
6b7c9f8b | 907 | r = make_read_only(m, blacklist); |
c2c13f2d | 908 | if (r < 0) |
d944dc95 | 909 | goto finish; |
c2c13f2d | 910 | } |
15ae422b LP |
911 | } |
912 | ||
ee818b89 AC |
913 | if (root_directory) { |
914 | /* MS_MOVE does not work on MS_SHARED so the remount MS_SHARED will be done later */ | |
915 | r = mount_move_root(root_directory); | |
d944dc95 LP |
916 | if (r < 0) |
917 | goto finish; | |
ee818b89 AC |
918 | } |
919 | ||
c2c13f2d LP |
920 | /* Remount / as the desired mode. Not that this will not |
921 | * reestablish propagation from our side to the host, since | |
922 | * what's disconnected is disconnected. */ | |
d944dc95 LP |
923 | if (mount(NULL, "/", NULL, mount_flags | MS_REC, NULL) < 0) { |
924 | r = -errno; | |
925 | goto finish; | |
926 | } | |
15ae422b | 927 | |
d944dc95 | 928 | r = 0; |
15ae422b | 929 | |
d944dc95 | 930 | finish: |
f0a4feb0 DH |
931 | for (m = mounts; m < mounts + n_mounts; m++) |
932 | free(m->path); | |
613b411c LP |
933 | |
934 | return r; | |
935 | } | |
936 | ||
937 | static int setup_one_tmp_dir(const char *id, const char *prefix, char **path) { | |
938 | _cleanup_free_ char *x = NULL; | |
6b46ea73 LP |
939 | char bid[SD_ID128_STRING_MAX]; |
940 | sd_id128_t boot_id; | |
941 | int r; | |
613b411c LP |
942 | |
943 | assert(id); | |
944 | assert(prefix); | |
945 | assert(path); | |
946 | ||
6b46ea73 LP |
947 | /* We include the boot id in the directory so that after a |
948 | * reboot we can easily identify obsolete directories. */ | |
949 | ||
950 | r = sd_id128_get_boot(&boot_id); | |
951 | if (r < 0) | |
952 | return r; | |
953 | ||
605405c6 | 954 | x = strjoin(prefix, "/systemd-private-", sd_id128_to_string(boot_id, bid), "-", id, "-XXXXXX"); |
613b411c LP |
955 | if (!x) |
956 | return -ENOMEM; | |
957 | ||
958 | RUN_WITH_UMASK(0077) | |
959 | if (!mkdtemp(x)) | |
960 | return -errno; | |
961 | ||
962 | RUN_WITH_UMASK(0000) { | |
963 | char *y; | |
964 | ||
63c372cb | 965 | y = strjoina(x, "/tmp"); |
613b411c LP |
966 | |
967 | if (mkdir(y, 0777 | S_ISVTX) < 0) | |
968 | return -errno; | |
c17ec25e | 969 | } |
15ae422b | 970 | |
613b411c LP |
971 | *path = x; |
972 | x = NULL; | |
973 | ||
974 | return 0; | |
975 | } | |
976 | ||
977 | int setup_tmp_dirs(const char *id, char **tmp_dir, char **var_tmp_dir) { | |
978 | char *a, *b; | |
979 | int r; | |
980 | ||
981 | assert(id); | |
982 | assert(tmp_dir); | |
983 | assert(var_tmp_dir); | |
984 | ||
985 | r = setup_one_tmp_dir(id, "/tmp", &a); | |
986 | if (r < 0) | |
987 | return r; | |
988 | ||
989 | r = setup_one_tmp_dir(id, "/var/tmp", &b); | |
990 | if (r < 0) { | |
991 | char *t; | |
992 | ||
63c372cb | 993 | t = strjoina(a, "/tmp"); |
613b411c LP |
994 | rmdir(t); |
995 | rmdir(a); | |
996 | ||
997 | free(a); | |
998 | return r; | |
999 | } | |
1000 | ||
1001 | *tmp_dir = a; | |
1002 | *var_tmp_dir = b; | |
1003 | ||
1004 | return 0; | |
1005 | } | |
1006 | ||
1007 | int setup_netns(int netns_storage_socket[2]) { | |
1008 | _cleanup_close_ int netns = -1; | |
3ee897d6 | 1009 | int r, q; |
613b411c LP |
1010 | |
1011 | assert(netns_storage_socket); | |
1012 | assert(netns_storage_socket[0] >= 0); | |
1013 | assert(netns_storage_socket[1] >= 0); | |
1014 | ||
1015 | /* We use the passed socketpair as a storage buffer for our | |
76cd584b LP |
1016 | * namespace reference fd. Whatever process runs this first |
1017 | * shall create a new namespace, all others should just join | |
1018 | * it. To serialize that we use a file lock on the socket | |
1019 | * pair. | |
613b411c LP |
1020 | * |
1021 | * It's a bit crazy, but hey, works great! */ | |
1022 | ||
1023 | if (lockf(netns_storage_socket[0], F_LOCK, 0) < 0) | |
1024 | return -errno; | |
1025 | ||
3ee897d6 LP |
1026 | netns = receive_one_fd(netns_storage_socket[0], MSG_DONTWAIT); |
1027 | if (netns == -EAGAIN) { | |
613b411c LP |
1028 | /* Nothing stored yet, so let's create a new namespace */ |
1029 | ||
1030 | if (unshare(CLONE_NEWNET) < 0) { | |
1031 | r = -errno; | |
1032 | goto fail; | |
1033 | } | |
1034 | ||
1035 | loopback_setup(); | |
1036 | ||
1037 | netns = open("/proc/self/ns/net", O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
1038 | if (netns < 0) { | |
1039 | r = -errno; | |
1040 | goto fail; | |
1041 | } | |
1042 | ||
1043 | r = 1; | |
613b411c | 1044 | |
3ee897d6 LP |
1045 | } else if (netns < 0) { |
1046 | r = netns; | |
1047 | goto fail; | |
613b411c | 1048 | |
3ee897d6 LP |
1049 | } else { |
1050 | /* Yay, found something, so let's join the namespace */ | |
613b411c LP |
1051 | if (setns(netns, CLONE_NEWNET) < 0) { |
1052 | r = -errno; | |
1053 | goto fail; | |
1054 | } | |
1055 | ||
1056 | r = 0; | |
1057 | } | |
1058 | ||
3ee897d6 LP |
1059 | q = send_one_fd(netns_storage_socket[1], netns, MSG_DONTWAIT); |
1060 | if (q < 0) { | |
1061 | r = q; | |
613b411c LP |
1062 | goto fail; |
1063 | } | |
1064 | ||
1065 | fail: | |
fe048ce5 | 1066 | (void) lockf(netns_storage_socket[0], F_ULOCK, 0); |
15ae422b LP |
1067 | return r; |
1068 | } | |
417116f2 | 1069 | |
1b8689f9 LP |
1070 | static const char *const protect_home_table[_PROTECT_HOME_MAX] = { |
1071 | [PROTECT_HOME_NO] = "no", | |
1072 | [PROTECT_HOME_YES] = "yes", | |
1073 | [PROTECT_HOME_READ_ONLY] = "read-only", | |
417116f2 LP |
1074 | }; |
1075 | ||
1b8689f9 LP |
1076 | DEFINE_STRING_TABLE_LOOKUP(protect_home, ProtectHome); |
1077 | ||
1078 | static const char *const protect_system_table[_PROTECT_SYSTEM_MAX] = { | |
1079 | [PROTECT_SYSTEM_NO] = "no", | |
1080 | [PROTECT_SYSTEM_YES] = "yes", | |
1081 | [PROTECT_SYSTEM_FULL] = "full", | |
3f815163 | 1082 | [PROTECT_SYSTEM_STRICT] = "strict", |
1b8689f9 LP |
1083 | }; |
1084 | ||
1085 | DEFINE_STRING_TABLE_LOOKUP(protect_system, ProtectSystem); |