]>
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" |
07630cea LP |
32 | #include "loopback-setup.h" |
33 | #include "missing.h" | |
34 | #include "mkdir.h" | |
4349cd7c | 35 | #include "mount-util.h" |
3ffd4af2 | 36 | #include "namespace.h" |
07630cea | 37 | #include "path-util.h" |
d7b8eec7 | 38 | #include "selinux-util.h" |
2583fbea | 39 | #include "socket-util.h" |
8b43440b | 40 | #include "string-table.h" |
07630cea LP |
41 | #include "string-util.h" |
42 | #include "strv.h" | |
affb60b1 | 43 | #include "umask-util.h" |
ee104e11 | 44 | #include "user-util.h" |
07630cea | 45 | #include "util.h" |
15ae422b | 46 | |
c17ec25e | 47 | typedef enum MountMode { |
15ae422b LP |
48 | /* This is ordered by priority! */ |
49 | INACCESSIBLE, | |
50 | READONLY, | |
ac0930c8 LP |
51 | PRIVATE_TMP, |
52 | PRIVATE_VAR_TMP, | |
7f112f50 | 53 | PRIVATE_DEV, |
a610cc4f | 54 | PRIVATE_BUS_ENDPOINT, |
15ae422b | 55 | READWRITE |
c17ec25e | 56 | } MountMode; |
15ae422b | 57 | |
c17ec25e | 58 | typedef struct BindMount { |
15ae422b | 59 | const char *path; |
c17ec25e | 60 | MountMode mode; |
ac0930c8 | 61 | bool done; |
ea92ae33 | 62 | bool ignore; |
c17ec25e | 63 | } BindMount; |
15ae422b | 64 | |
c17ec25e | 65 | static int append_mounts(BindMount **p, char **strv, MountMode mode) { |
15ae422b LP |
66 | char **i; |
67 | ||
613b411c LP |
68 | assert(p); |
69 | ||
15ae422b LP |
70 | STRV_FOREACH(i, strv) { |
71 | ||
ea92ae33 | 72 | (*p)->ignore = false; |
002b2268 | 73 | (*p)->done = false; |
ea92ae33 | 74 | |
94828d2d | 75 | if ((mode == INACCESSIBLE || mode == READONLY || mode == READWRITE) && (*i)[0] == '-') { |
ea92ae33 MW |
76 | (*p)->ignore = true; |
77 | (*i)++; | |
78 | } | |
79 | ||
15ae422b LP |
80 | if (!path_is_absolute(*i)) |
81 | return -EINVAL; | |
82 | ||
83 | (*p)->path = *i; | |
84 | (*p)->mode = mode; | |
85 | (*p)++; | |
86 | } | |
87 | ||
88 | return 0; | |
89 | } | |
90 | ||
c17ec25e MS |
91 | static int mount_path_compare(const void *a, const void *b) { |
92 | const BindMount *p = a, *q = b; | |
a0827e2b | 93 | int d; |
15ae422b | 94 | |
a0827e2b | 95 | d = path_compare(p->path, q->path); |
15ae422b | 96 | |
5a8af538 | 97 | if (d == 0) { |
15ae422b LP |
98 | /* If the paths are equal, check the mode */ |
99 | if (p->mode < q->mode) | |
100 | return -1; | |
101 | ||
102 | if (p->mode > q->mode) | |
103 | return 1; | |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
108 | /* If the paths are not equal, then order prefixes first */ | |
a0827e2b | 109 | return d; |
15ae422b LP |
110 | } |
111 | ||
c17ec25e MS |
112 | static void drop_duplicates(BindMount *m, unsigned *n) { |
113 | BindMount *f, *t, *previous; | |
15ae422b | 114 | |
c17ec25e | 115 | assert(m); |
15ae422b | 116 | assert(n); |
15ae422b | 117 | |
c17ec25e | 118 | for (f = m, t = m, previous = NULL; f < m+*n; f++) { |
15ae422b | 119 | |
ac0930c8 | 120 | /* The first one wins */ |
15ae422b LP |
121 | if (previous && path_equal(f->path, previous->path)) |
122 | continue; | |
123 | ||
e2d7c1a0 | 124 | *t = *f; |
15ae422b | 125 | |
15ae422b LP |
126 | previous = t; |
127 | ||
128 | t++; | |
129 | } | |
130 | ||
c17ec25e | 131 | *n = t - m; |
15ae422b LP |
132 | } |
133 | ||
7f112f50 LP |
134 | static int mount_dev(BindMount *m) { |
135 | static const char devnodes[] = | |
136 | "/dev/null\0" | |
137 | "/dev/zero\0" | |
138 | "/dev/full\0" | |
139 | "/dev/random\0" | |
140 | "/dev/urandom\0" | |
141 | "/dev/tty\0"; | |
142 | ||
2b85f4e1 | 143 | char temporary_mount[] = "/tmp/namespace-dev-XXXXXX"; |
63cc4c31 | 144 | const char *d, *dev = NULL, *devpts = NULL, *devshm = NULL, *devhugepages = NULL, *devmqueue = NULL, *devlog = NULL, *devptmx = NULL; |
7f112f50 LP |
145 | _cleanup_umask_ mode_t u; |
146 | int r; | |
147 | ||
148 | assert(m); | |
149 | ||
150 | u = umask(0000); | |
151 | ||
2b85f4e1 LP |
152 | if (!mkdtemp(temporary_mount)) |
153 | return -errno; | |
154 | ||
63c372cb | 155 | dev = strjoina(temporary_mount, "/dev"); |
dc751688 | 156 | (void) mkdir(dev, 0755); |
2b85f4e1 LP |
157 | if (mount("tmpfs", dev, "tmpfs", MS_NOSUID|MS_STRICTATIME, "mode=755") < 0) { |
158 | r = -errno; | |
159 | goto fail; | |
160 | } | |
161 | ||
63c372cb | 162 | devpts = strjoina(temporary_mount, "/dev/pts"); |
dc751688 | 163 | (void) mkdir(devpts, 0755); |
2b85f4e1 LP |
164 | if (mount("/dev/pts", devpts, NULL, MS_BIND, NULL) < 0) { |
165 | r = -errno; | |
166 | goto fail; | |
167 | } | |
168 | ||
63c372cb | 169 | devptmx = strjoina(temporary_mount, "/dev/ptmx"); |
3164e3cb ZJS |
170 | if (symlink("pts/ptmx", devptmx) < 0) { |
171 | r = -errno; | |
172 | goto fail; | |
173 | } | |
e06b6479 | 174 | |
63c372cb | 175 | devshm = strjoina(temporary_mount, "/dev/shm"); |
dc751688 | 176 | (void) mkdir(devshm, 01777); |
2b85f4e1 LP |
177 | r = mount("/dev/shm", devshm, NULL, MS_BIND, NULL); |
178 | if (r < 0) { | |
179 | r = -errno; | |
180 | goto fail; | |
181 | } | |
182 | ||
63c372cb | 183 | devmqueue = strjoina(temporary_mount, "/dev/mqueue"); |
dc751688 | 184 | (void) mkdir(devmqueue, 0755); |
3164e3cb | 185 | (void) mount("/dev/mqueue", devmqueue, NULL, MS_BIND, NULL); |
2b85f4e1 | 186 | |
63c372cb | 187 | devhugepages = strjoina(temporary_mount, "/dev/hugepages"); |
dc751688 | 188 | (void) mkdir(devhugepages, 0755); |
3164e3cb | 189 | (void) mount("/dev/hugepages", devhugepages, NULL, MS_BIND, NULL); |
2b85f4e1 | 190 | |
63c372cb | 191 | devlog = strjoina(temporary_mount, "/dev/log"); |
3164e3cb | 192 | (void) symlink("/run/systemd/journal/dev-log", devlog); |
82d25240 | 193 | |
7f112f50 | 194 | NULSTR_FOREACH(d, devnodes) { |
2b85f4e1 LP |
195 | _cleanup_free_ char *dn = NULL; |
196 | struct stat st; | |
197 | ||
198 | r = stat(d, &st); | |
7f112f50 | 199 | if (r < 0) { |
2b85f4e1 LP |
200 | |
201 | if (errno == ENOENT) | |
202 | continue; | |
203 | ||
204 | r = -errno; | |
205 | goto fail; | |
7f112f50 LP |
206 | } |
207 | ||
2b85f4e1 LP |
208 | if (!S_ISBLK(st.st_mode) && |
209 | !S_ISCHR(st.st_mode)) { | |
210 | r = -EINVAL; | |
211 | goto fail; | |
212 | } | |
213 | ||
214 | if (st.st_rdev == 0) | |
215 | continue; | |
216 | ||
217 | dn = strappend(temporary_mount, d); | |
218 | if (!dn) { | |
219 | r = -ENOMEM; | |
220 | goto fail; | |
221 | } | |
222 | ||
ecabcf8b | 223 | mac_selinux_create_file_prepare(d, st.st_mode); |
2b85f4e1 | 224 | r = mknod(dn, st.st_mode, st.st_rdev); |
ecabcf8b | 225 | mac_selinux_create_file_clear(); |
dd078a1e | 226 | |
2b85f4e1 LP |
227 | if (r < 0) { |
228 | r = -errno; | |
229 | goto fail; | |
230 | } | |
7f112f50 LP |
231 | } |
232 | ||
03cfe0d5 | 233 | dev_setup(temporary_mount, UID_INVALID, GID_INVALID); |
7f112f50 | 234 | |
ee818b89 AC |
235 | /* Create the /dev directory if missing. It is more likely to be |
236 | * missing when the service is started with RootDirectory. This is | |
237 | * consistent with mount units creating the mount points when missing. | |
238 | */ | |
239 | (void) mkdir_p_label(m->path, 0755); | |
240 | ||
241 | if (mount(dev, m->path, NULL, MS_MOVE, NULL) < 0) { | |
2b85f4e1 LP |
242 | r = -errno; |
243 | goto fail; | |
244 | } | |
7f112f50 | 245 | |
2b85f4e1 LP |
246 | rmdir(dev); |
247 | rmdir(temporary_mount); | |
7f112f50 | 248 | |
2b85f4e1 | 249 | return 0; |
7f112f50 | 250 | |
2b85f4e1 LP |
251 | fail: |
252 | if (devpts) | |
253 | umount(devpts); | |
7f112f50 | 254 | |
2b85f4e1 LP |
255 | if (devshm) |
256 | umount(devshm); | |
7f112f50 | 257 | |
2b85f4e1 LP |
258 | if (devhugepages) |
259 | umount(devhugepages); | |
7f112f50 | 260 | |
2b85f4e1 LP |
261 | if (devmqueue) |
262 | umount(devmqueue); | |
7f112f50 | 263 | |
d267c5aa ZJS |
264 | umount(dev); |
265 | rmdir(dev); | |
2b85f4e1 | 266 | rmdir(temporary_mount); |
7f112f50 | 267 | |
2b85f4e1 | 268 | return r; |
7f112f50 LP |
269 | } |
270 | ||
a610cc4f DM |
271 | static int mount_kdbus(BindMount *m) { |
272 | ||
273 | char temporary_mount[] = "/tmp/kdbus-dev-XXXXXX"; | |
274 | _cleanup_free_ char *basepath = NULL; | |
275 | _cleanup_umask_ mode_t u; | |
120d578e | 276 | char *busnode = NULL, *root; |
a610cc4f DM |
277 | struct stat st; |
278 | int r; | |
279 | ||
280 | assert(m); | |
281 | ||
282 | u = umask(0000); | |
283 | ||
4a62c710 MS |
284 | if (!mkdtemp(temporary_mount)) |
285 | return log_error_errno(errno, "Failed create temp dir: %m"); | |
a610cc4f | 286 | |
63c372cb | 287 | root = strjoina(temporary_mount, "/kdbus"); |
dc751688 | 288 | (void) mkdir(root, 0755); |
a610cc4f DM |
289 | if (mount("tmpfs", root, "tmpfs", MS_NOSUID|MS_STRICTATIME, "mode=777") < 0) { |
290 | r = -errno; | |
291 | goto fail; | |
292 | } | |
293 | ||
294 | /* create a new /dev/null dev node copy so we have some fodder to | |
295 | * bind-mount the custom endpoint over. */ | |
296 | if (stat("/dev/null", &st) < 0) { | |
76ef789d | 297 | r = log_error_errno(errno, "Failed to stat /dev/null: %m"); |
a610cc4f DM |
298 | goto fail; |
299 | } | |
300 | ||
63c372cb | 301 | busnode = strjoina(root, "/bus"); |
a610cc4f | 302 | if (mknod(busnode, (st.st_mode & ~07777) | 0600, st.st_rdev) < 0) { |
94c156cd LP |
303 | r = log_error_errno(errno, "mknod() for %s failed: %m", |
304 | busnode); | |
a610cc4f DM |
305 | goto fail; |
306 | } | |
307 | ||
4543768d | 308 | r = mount(m->path, busnode, NULL, MS_BIND, NULL); |
a610cc4f | 309 | if (r < 0) { |
94c156cd LP |
310 | r = log_error_errno(errno, "bind mount of %s failed: %m", |
311 | m->path); | |
a610cc4f DM |
312 | goto fail; |
313 | } | |
314 | ||
315 | basepath = dirname_malloc(m->path); | |
316 | if (!basepath) { | |
317 | r = -ENOMEM; | |
318 | goto fail; | |
319 | } | |
320 | ||
321 | if (mount(root, basepath, NULL, MS_MOVE, NULL) < 0) { | |
94c156cd LP |
322 | r = log_error_errno(errno, "bind mount of %s failed: %m", |
323 | basepath); | |
a610cc4f DM |
324 | goto fail; |
325 | } | |
326 | ||
327 | rmdir(temporary_mount); | |
328 | return 0; | |
329 | ||
330 | fail: | |
331 | if (busnode) { | |
332 | umount(busnode); | |
333 | unlink(busnode); | |
334 | } | |
335 | ||
1775f1eb ZJS |
336 | umount(root); |
337 | rmdir(root); | |
a610cc4f DM |
338 | rmdir(temporary_mount); |
339 | ||
340 | return r; | |
341 | } | |
342 | ||
ac0930c8 | 343 | static int apply_mount( |
c17ec25e | 344 | BindMount *m, |
ac0930c8 | 345 | const char *tmp_dir, |
c17ec25e | 346 | const char *var_tmp_dir) { |
ac0930c8 | 347 | |
15ae422b | 348 | const char *what; |
15ae422b | 349 | int r; |
15ae422b | 350 | |
c17ec25e | 351 | assert(m); |
15ae422b | 352 | |
c17ec25e | 353 | switch (m->mode) { |
15ae422b LP |
354 | |
355 | case INACCESSIBLE: | |
6d313367 LP |
356 | |
357 | /* First, get rid of everything that is below if there | |
358 | * is anything... Then, overmount it with an | |
359 | * inaccessible directory. */ | |
360 | umount_recursive(m->path, 0); | |
361 | ||
c17ec25e | 362 | what = "/run/systemd/inaccessible"; |
15ae422b LP |
363 | break; |
364 | ||
365 | case READONLY: | |
15ae422b | 366 | case READWRITE: |
d6797c92 LP |
367 | /* Nothing to mount here, we just later toggle the |
368 | * MS_RDONLY bit for the mount point */ | |
369 | return 0; | |
15ae422b | 370 | |
ac0930c8 LP |
371 | case PRIVATE_TMP: |
372 | what = tmp_dir; | |
373 | break; | |
374 | ||
375 | case PRIVATE_VAR_TMP: | |
376 | what = var_tmp_dir; | |
15ae422b | 377 | break; |
e364ad06 | 378 | |
d6797c92 LP |
379 | case PRIVATE_DEV: |
380 | return mount_dev(m); | |
381 | ||
a610cc4f DM |
382 | case PRIVATE_BUS_ENDPOINT: |
383 | return mount_kdbus(m); | |
384 | ||
e364ad06 LP |
385 | default: |
386 | assert_not_reached("Unknown mode"); | |
15ae422b LP |
387 | } |
388 | ||
ac0930c8 | 389 | assert(what); |
15ae422b | 390 | |
c17ec25e | 391 | r = mount(what, m->path, NULL, MS_BIND|MS_REC, NULL); |
ac0930c8 | 392 | if (r >= 0) |
c17ec25e | 393 | log_debug("Successfully mounted %s to %s", what, m->path); |
ea92ae33 | 394 | else if (m->ignore && errno == ENOENT) |
d6797c92 | 395 | return 0; |
15ae422b | 396 | |
ac0930c8 LP |
397 | return r; |
398 | } | |
15ae422b | 399 | |
c17ec25e | 400 | static int make_read_only(BindMount *m) { |
ac0930c8 | 401 | int r; |
15ae422b | 402 | |
c17ec25e | 403 | assert(m); |
ac0930c8 | 404 | |
d6797c92 LP |
405 | if (IN_SET(m->mode, INACCESSIBLE, READONLY)) |
406 | r = bind_remount_recursive(m->path, true); | |
664064d6 | 407 | else if (IN_SET(m->mode, READWRITE, PRIVATE_TMP, PRIVATE_VAR_TMP, PRIVATE_DEV)) |
d6797c92 LP |
408 | r = bind_remount_recursive(m->path, false); |
409 | else | |
410 | r = 0; | |
ac0930c8 | 411 | |
d6797c92 LP |
412 | if (m->ignore && r == -ENOENT) |
413 | return 0; | |
ac0930c8 | 414 | |
d6797c92 | 415 | return r; |
15ae422b LP |
416 | } |
417 | ||
613b411c | 418 | int setup_namespace( |
ee818b89 | 419 | const char* root_directory, |
613b411c LP |
420 | char** read_write_dirs, |
421 | char** read_only_dirs, | |
422 | char** inaccessible_dirs, | |
a004cb4c LP |
423 | const char* tmp_dir, |
424 | const char* var_tmp_dir, | |
425 | const char* bus_endpoint_path, | |
7f112f50 | 426 | bool private_dev, |
1b8689f9 LP |
427 | ProtectHome protect_home, |
428 | ProtectSystem protect_system, | |
e6547662 | 429 | unsigned long mount_flags) { |
15ae422b | 430 | |
7ff7394d | 431 | BindMount *m, *mounts = NULL; |
613b411c | 432 | unsigned n; |
c17ec25e | 433 | int r = 0; |
15ae422b | 434 | |
613b411c | 435 | if (mount_flags == 0) |
c17ec25e | 436 | mount_flags = MS_SHARED; |
ac0930c8 | 437 | |
d5a3f0ea ZJS |
438 | if (unshare(CLONE_NEWNS) < 0) |
439 | return -errno; | |
15ae422b | 440 | |
a610cc4f | 441 | n = !!tmp_dir + !!var_tmp_dir + !!bus_endpoint_path + |
613b411c LP |
442 | strv_length(read_write_dirs) + |
443 | strv_length(read_only_dirs) + | |
7f112f50 | 444 | strv_length(inaccessible_dirs) + |
417116f2 | 445 | private_dev + |
c8835999 | 446 | (protect_home != PROTECT_HOME_NO ? 3 : 0) + |
051be1f7 | 447 | (protect_system != PROTECT_SYSTEM_NO ? 2 : 0) + |
1b8689f9 | 448 | (protect_system == PROTECT_SYSTEM_FULL ? 1 : 0); |
613b411c LP |
449 | |
450 | if (n > 0) { | |
002b2268 | 451 | m = mounts = (BindMount *) alloca0(n * sizeof(BindMount)); |
613b411c LP |
452 | r = append_mounts(&m, read_write_dirs, READWRITE); |
453 | if (r < 0) | |
454 | return r; | |
455 | ||
456 | r = append_mounts(&m, read_only_dirs, READONLY); | |
457 | if (r < 0) | |
458 | return r; | |
459 | ||
460 | r = append_mounts(&m, inaccessible_dirs, INACCESSIBLE); | |
461 | if (r < 0) | |
7ff7394d ZJS |
462 | return r; |
463 | ||
613b411c | 464 | if (tmp_dir) { |
ee818b89 | 465 | m->path = prefix_roota(root_directory, "/tmp"); |
7ff7394d ZJS |
466 | m->mode = PRIVATE_TMP; |
467 | m++; | |
613b411c | 468 | } |
7ff7394d | 469 | |
613b411c | 470 | if (var_tmp_dir) { |
ee818b89 | 471 | m->path = prefix_roota(root_directory, "/var/tmp"); |
7ff7394d ZJS |
472 | m->mode = PRIVATE_VAR_TMP; |
473 | m++; | |
474 | } | |
ac0930c8 | 475 | |
7f112f50 | 476 | if (private_dev) { |
ee818b89 | 477 | m->path = prefix_roota(root_directory, "/dev"); |
7f112f50 LP |
478 | m->mode = PRIVATE_DEV; |
479 | m++; | |
480 | } | |
481 | ||
a610cc4f | 482 | if (bus_endpoint_path) { |
ee818b89 | 483 | m->path = prefix_roota(root_directory, bus_endpoint_path); |
a610cc4f DM |
484 | m->mode = PRIVATE_BUS_ENDPOINT; |
485 | m++; | |
486 | } | |
487 | ||
1b8689f9 | 488 | if (protect_home != PROTECT_HOME_NO) { |
ee818b89 AC |
489 | const char *home_dir, *run_user_dir, *root_dir; |
490 | ||
491 | home_dir = prefix_roota(root_directory, "/home"); | |
492 | home_dir = strjoina("-", home_dir); | |
493 | run_user_dir = prefix_roota(root_directory, "/run/user"); | |
494 | run_user_dir = strjoina("-", run_user_dir); | |
495 | root_dir = prefix_roota(root_directory, "/root"); | |
496 | root_dir = strjoina("-", root_dir); | |
497 | ||
498 | r = append_mounts(&m, STRV_MAKE(home_dir, run_user_dir, root_dir), | |
499 | protect_home == PROTECT_HOME_READ_ONLY ? READONLY : INACCESSIBLE); | |
417116f2 LP |
500 | if (r < 0) |
501 | return r; | |
502 | } | |
503 | ||
1b8689f9 | 504 | if (protect_system != PROTECT_SYSTEM_NO) { |
ee818b89 AC |
505 | const char *usr_dir, *boot_dir, *etc_dir; |
506 | ||
d38e01dc | 507 | usr_dir = prefix_roota(root_directory, "/usr"); |
ee818b89 AC |
508 | boot_dir = prefix_roota(root_directory, "/boot"); |
509 | boot_dir = strjoina("-", boot_dir); | |
510 | etc_dir = prefix_roota(root_directory, "/etc"); | |
511 | ||
512 | r = append_mounts(&m, protect_system == PROTECT_SYSTEM_FULL | |
513 | ? STRV_MAKE(usr_dir, boot_dir, etc_dir) | |
514 | : STRV_MAKE(usr_dir, boot_dir), READONLY); | |
417116f2 LP |
515 | if (r < 0) |
516 | return r; | |
517 | } | |
518 | ||
7ff7394d | 519 | assert(mounts + n == m); |
ac0930c8 | 520 | |
7ff7394d ZJS |
521 | qsort(mounts, n, sizeof(BindMount), mount_path_compare); |
522 | drop_duplicates(mounts, &n); | |
15ae422b LP |
523 | } |
524 | ||
ee818b89 | 525 | if (n > 0 || root_directory) { |
c2c13f2d LP |
526 | /* Remount / as SLAVE so that nothing now mounted in the namespace |
527 | shows up in the parent */ | |
528 | if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) | |
529 | return -errno; | |
ee818b89 AC |
530 | } |
531 | ||
532 | if (root_directory) { | |
533 | /* Turn directory into bind mount */ | |
534 | if (mount(root_directory, root_directory, NULL, MS_BIND|MS_REC, NULL) < 0) | |
535 | return -errno; | |
536 | } | |
c2c13f2d | 537 | |
ee818b89 | 538 | if (n > 0) { |
c2c13f2d LP |
539 | for (m = mounts; m < mounts + n; ++m) { |
540 | r = apply_mount(m, tmp_dir, var_tmp_dir); | |
541 | if (r < 0) | |
542 | goto fail; | |
543 | } | |
15ae422b | 544 | |
c2c13f2d LP |
545 | for (m = mounts; m < mounts + n; ++m) { |
546 | r = make_read_only(m); | |
547 | if (r < 0) | |
548 | goto fail; | |
549 | } | |
15ae422b LP |
550 | } |
551 | ||
ee818b89 AC |
552 | if (root_directory) { |
553 | /* MS_MOVE does not work on MS_SHARED so the remount MS_SHARED will be done later */ | |
554 | r = mount_move_root(root_directory); | |
555 | ||
556 | /* at this point, we cannot rollback */ | |
557 | if (r < 0) | |
558 | return r; | |
559 | } | |
560 | ||
c2c13f2d LP |
561 | /* Remount / as the desired mode. Not that this will not |
562 | * reestablish propagation from our side to the host, since | |
563 | * what's disconnected is disconnected. */ | |
1f6b4113 | 564 | if (mount(NULL, "/", NULL, mount_flags | MS_REC, NULL) < 0) |
ee818b89 AC |
565 | /* at this point, we cannot rollback */ |
566 | return -errno; | |
15ae422b | 567 | |
15ae422b LP |
568 | return 0; |
569 | ||
613b411c | 570 | fail: |
c2c13f2d LP |
571 | if (n > 0) { |
572 | for (m = mounts; m < mounts + n; ++m) | |
573 | if (m->done) | |
42b1b990 | 574 | (void) umount2(m->path, MNT_DETACH); |
c2c13f2d | 575 | } |
613b411c LP |
576 | |
577 | return r; | |
578 | } | |
579 | ||
580 | static int setup_one_tmp_dir(const char *id, const char *prefix, char **path) { | |
581 | _cleanup_free_ char *x = NULL; | |
6b46ea73 LP |
582 | char bid[SD_ID128_STRING_MAX]; |
583 | sd_id128_t boot_id; | |
584 | int r; | |
613b411c LP |
585 | |
586 | assert(id); | |
587 | assert(prefix); | |
588 | assert(path); | |
589 | ||
6b46ea73 LP |
590 | /* We include the boot id in the directory so that after a |
591 | * reboot we can easily identify obsolete directories. */ | |
592 | ||
593 | r = sd_id128_get_boot(&boot_id); | |
594 | if (r < 0) | |
595 | return r; | |
596 | ||
597 | x = strjoin(prefix, "/systemd-private-", sd_id128_to_string(boot_id, bid), "-", id, "-XXXXXX", NULL); | |
613b411c LP |
598 | if (!x) |
599 | return -ENOMEM; | |
600 | ||
601 | RUN_WITH_UMASK(0077) | |
602 | if (!mkdtemp(x)) | |
603 | return -errno; | |
604 | ||
605 | RUN_WITH_UMASK(0000) { | |
606 | char *y; | |
607 | ||
63c372cb | 608 | y = strjoina(x, "/tmp"); |
613b411c LP |
609 | |
610 | if (mkdir(y, 0777 | S_ISVTX) < 0) | |
611 | return -errno; | |
c17ec25e | 612 | } |
15ae422b | 613 | |
613b411c LP |
614 | *path = x; |
615 | x = NULL; | |
616 | ||
617 | return 0; | |
618 | } | |
619 | ||
620 | int setup_tmp_dirs(const char *id, char **tmp_dir, char **var_tmp_dir) { | |
621 | char *a, *b; | |
622 | int r; | |
623 | ||
624 | assert(id); | |
625 | assert(tmp_dir); | |
626 | assert(var_tmp_dir); | |
627 | ||
628 | r = setup_one_tmp_dir(id, "/tmp", &a); | |
629 | if (r < 0) | |
630 | return r; | |
631 | ||
632 | r = setup_one_tmp_dir(id, "/var/tmp", &b); | |
633 | if (r < 0) { | |
634 | char *t; | |
635 | ||
63c372cb | 636 | t = strjoina(a, "/tmp"); |
613b411c LP |
637 | rmdir(t); |
638 | rmdir(a); | |
639 | ||
640 | free(a); | |
641 | return r; | |
642 | } | |
643 | ||
644 | *tmp_dir = a; | |
645 | *var_tmp_dir = b; | |
646 | ||
647 | return 0; | |
648 | } | |
649 | ||
650 | int setup_netns(int netns_storage_socket[2]) { | |
651 | _cleanup_close_ int netns = -1; | |
3ee897d6 | 652 | int r, q; |
613b411c LP |
653 | |
654 | assert(netns_storage_socket); | |
655 | assert(netns_storage_socket[0] >= 0); | |
656 | assert(netns_storage_socket[1] >= 0); | |
657 | ||
658 | /* We use the passed socketpair as a storage buffer for our | |
76cd584b LP |
659 | * namespace reference fd. Whatever process runs this first |
660 | * shall create a new namespace, all others should just join | |
661 | * it. To serialize that we use a file lock on the socket | |
662 | * pair. | |
613b411c LP |
663 | * |
664 | * It's a bit crazy, but hey, works great! */ | |
665 | ||
666 | if (lockf(netns_storage_socket[0], F_LOCK, 0) < 0) | |
667 | return -errno; | |
668 | ||
3ee897d6 LP |
669 | netns = receive_one_fd(netns_storage_socket[0], MSG_DONTWAIT); |
670 | if (netns == -EAGAIN) { | |
613b411c LP |
671 | /* Nothing stored yet, so let's create a new namespace */ |
672 | ||
673 | if (unshare(CLONE_NEWNET) < 0) { | |
674 | r = -errno; | |
675 | goto fail; | |
676 | } | |
677 | ||
678 | loopback_setup(); | |
679 | ||
680 | netns = open("/proc/self/ns/net", O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
681 | if (netns < 0) { | |
682 | r = -errno; | |
683 | goto fail; | |
684 | } | |
685 | ||
686 | r = 1; | |
613b411c | 687 | |
3ee897d6 LP |
688 | } else if (netns < 0) { |
689 | r = netns; | |
690 | goto fail; | |
613b411c | 691 | |
3ee897d6 LP |
692 | } else { |
693 | /* Yay, found something, so let's join the namespace */ | |
613b411c LP |
694 | if (setns(netns, CLONE_NEWNET) < 0) { |
695 | r = -errno; | |
696 | goto fail; | |
697 | } | |
698 | ||
699 | r = 0; | |
700 | } | |
701 | ||
3ee897d6 LP |
702 | q = send_one_fd(netns_storage_socket[1], netns, MSG_DONTWAIT); |
703 | if (q < 0) { | |
704 | r = q; | |
613b411c LP |
705 | goto fail; |
706 | } | |
707 | ||
708 | fail: | |
709 | lockf(netns_storage_socket[0], F_ULOCK, 0); | |
15ae422b LP |
710 | return r; |
711 | } | |
417116f2 | 712 | |
1b8689f9 LP |
713 | static const char *const protect_home_table[_PROTECT_HOME_MAX] = { |
714 | [PROTECT_HOME_NO] = "no", | |
715 | [PROTECT_HOME_YES] = "yes", | |
716 | [PROTECT_HOME_READ_ONLY] = "read-only", | |
417116f2 LP |
717 | }; |
718 | ||
1b8689f9 LP |
719 | DEFINE_STRING_TABLE_LOOKUP(protect_home, ProtectHome); |
720 | ||
721 | static const char *const protect_system_table[_PROTECT_SYSTEM_MAX] = { | |
722 | [PROTECT_SYSTEM_NO] = "no", | |
723 | [PROTECT_SYSTEM_YES] = "yes", | |
724 | [PROTECT_SYSTEM_FULL] = "full", | |
725 | }; | |
726 | ||
727 | DEFINE_STRING_TABLE_LOOKUP(protect_system, ProtectSystem); |