| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/magic.h> |
| 4 |
#include <sched.h> |
| 5 |
#include <sys/mount.h> |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "chase.h" |
| 10 |
#include "errno-util.h" |
| 11 |
#include "escape.h" |
| 12 |
#include "extract-word.h" |
| 13 |
#include "fd-util.h" |
| 14 |
#include "format-util.h" |
| 15 |
#include "fs-util.h" |
| 16 |
#include "log.h" |
| 17 |
#include "mkdir.h" |
| 18 |
#include "mount-util.h" |
| 19 |
#include "mountpoint-util.h" |
| 20 |
#include "namespace-util.h" |
| 21 |
#include "nspawn-mount.h" |
| 22 |
#include "path-util.h" |
| 23 |
#include "rm-rf.h" |
| 24 |
#include "sort-util.h" |
| 25 |
#include "stat-util.h" |
| 26 |
#include "string-util.h" |
| 27 |
#include "strv.h" |
| 28 |
#include "tmpfile-util.h" |
| 29 |
#include "user-util.h" |
| 30 |
|
| 31 |
CustomMount* custom_mount_add(CustomMount **l, size_t *n, CustomMountType t) { |
| 32 |
CustomMount *ret; |
| 33 |
|
| 34 |
assert(l); |
| 35 |
assert(n); |
| 36 |
assert(t >= 0); |
| 37 |
assert(t < _CUSTOM_MOUNT_TYPE_MAX); |
| 38 |
|
| 39 |
if (!GREEDY_REALLOC(*l, *n + 1)) |
| 40 |
return NULL; |
| 41 |
|
| 42 |
ret = *l + *n; |
| 43 |
(*n)++; |
| 44 |
|
| 45 |
*ret = (CustomMount) { |
| 46 |
.type = t, |
| 47 |
.destination_uid = UID_INVALID, |
| 48 |
}; |
| 49 |
|
| 50 |
return ret; |
| 51 |
} |
| 52 |
|
| 53 |
void custom_mount_free_all(CustomMount *l, size_t n) { |
| 54 |
FOREACH_ARRAY(m, l, n) { |
| 55 |
free(m->source); |
| 56 |
free(m->destination); |
| 57 |
free(m->options); |
| 58 |
|
| 59 |
if (m->work_dir) { |
| 60 |
(void) rm_rf(m->work_dir, REMOVE_ROOT|REMOVE_PHYSICAL); |
| 61 |
free(m->work_dir); |
| 62 |
} |
| 63 |
|
| 64 |
if (m->rm_rf_tmpdir) { |
| 65 |
(void) rm_rf(m->rm_rf_tmpdir, REMOVE_ROOT|REMOVE_PHYSICAL); |
| 66 |
free(m->rm_rf_tmpdir); |
| 67 |
} |
| 68 |
|
| 69 |
strv_free(m->lower); |
| 70 |
free(m->type_argument); |
| 71 |
} |
| 72 |
|
| 73 |
free(l); |
| 74 |
} |
| 75 |
|
| 76 |
static int custom_mount_compare(const CustomMount *a, const CustomMount *b) { |
| 77 |
int r; |
| 78 |
|
| 79 |
r = path_compare(a->destination, b->destination); |
| 80 |
if (r != 0) |
| 81 |
return r; |
| 82 |
|
| 83 |
return CMP(a->type, b->type); |
| 84 |
} |
| 85 |
|
| 86 |
static int source_path_parse(const char *p, char **ret) { |
| 87 |
assert(p); |
| 88 |
assert(ret); |
| 89 |
|
| 90 |
if (isempty(p)) |
| 91 |
return -EINVAL; |
| 92 |
|
| 93 |
if (*p == '+') { |
| 94 |
if (!path_is_absolute(p + 1)) |
| 95 |
return -EINVAL; |
| 96 |
|
| 97 |
char *s = strdup(p); |
| 98 |
if (!s) |
| 99 |
return -ENOMEM; |
| 100 |
|
| 101 |
*ret = TAKE_PTR(s); |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
return path_make_absolute_cwd(p, ret); |
| 106 |
} |
| 107 |
|
| 108 |
static int source_path_parse_nullable(const char *p, char **ret) { |
| 109 |
assert(p); |
| 110 |
assert(ret); |
| 111 |
|
| 112 |
if (isempty(p)) { |
| 113 |
*ret = NULL; |
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
return source_path_parse(p, ret); |
| 118 |
} |
| 119 |
|
| 120 |
static char *resolve_source_path(const char *dest, const char *source) { |
| 121 |
if (!source) |
| 122 |
return NULL; |
| 123 |
|
| 124 |
if (source[0] == '+') |
| 125 |
return path_join(dest, source + 1); |
| 126 |
|
| 127 |
return strdup(source); |
| 128 |
} |
| 129 |
|
| 130 |
static int allocate_temporary_source(CustomMount *m) { |
| 131 |
int r; |
| 132 |
|
| 133 |
assert(m); |
| 134 |
assert(!m->source); |
| 135 |
assert(!m->rm_rf_tmpdir); |
| 136 |
|
| 137 |
r = mkdtemp_malloc("/var/tmp/nspawn-temp-XXXXXX", &m->rm_rf_tmpdir); |
| 138 |
if (r < 0) |
| 139 |
return log_error_errno(r, "Failed to acquire temporary directory: %m"); |
| 140 |
|
| 141 |
m->source = path_join(m->rm_rf_tmpdir, "src"); |
| 142 |
if (!m->source) |
| 143 |
return log_oom(); |
| 144 |
|
| 145 |
if (mkdir(m->source, 0755) < 0) |
| 146 |
return log_error_errno(errno, "Failed to create %s: %m", m->source); |
| 147 |
|
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
int custom_mount_prepare_all(const char *dest, CustomMount *l, size_t n) { |
| 152 |
int r; |
| 153 |
|
| 154 |
/* Prepare all custom mounts. This will make sure we know all temporary directories. This is called in the |
| 155 |
* parent process, so that we know the temporary directories to remove on exit before we fork off the |
| 156 |
* children. */ |
| 157 |
|
| 158 |
assert(l || n == 0); |
| 159 |
|
| 160 |
/* Order the custom mounts, and make sure we have a working directory */ |
| 161 |
typesafe_qsort(l, n, custom_mount_compare); |
| 162 |
|
| 163 |
FOREACH_ARRAY(m, l, n) { |
| 164 |
/* /proc we mount in the inner child, i.e. when we acquired CLONE_NEWPID. All other mounts we mount |
| 165 |
* already in the outer child, so that the mounts are already established before CLONE_NEWPID and in |
| 166 |
* particular CLONE_NEWUSER. This also means any custom mounts below /proc also need to be mounted in |
| 167 |
* the inner child, not the outer one. Determine this here. */ |
| 168 |
m->in_userns = path_startswith(m->destination, "/proc"); |
| 169 |
|
| 170 |
if (m->type == CUSTOM_MOUNT_BIND) { |
| 171 |
if (m->source) { |
| 172 |
char *s; |
| 173 |
|
| 174 |
s = resolve_source_path(dest, m->source); |
| 175 |
if (!s) |
| 176 |
return log_oom(); |
| 177 |
|
| 178 |
free_and_replace(m->source, s); |
| 179 |
} else { |
| 180 |
/* No source specified? In that case, use a throw-away temporary directory in /var/tmp */ |
| 181 |
|
| 182 |
r = allocate_temporary_source(m); |
| 183 |
if (r < 0) |
| 184 |
return r; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if (m->type == CUSTOM_MOUNT_OVERLAY) { |
| 189 |
STRV_FOREACH(j, m->lower) { |
| 190 |
char *s; |
| 191 |
|
| 192 |
s = resolve_source_path(dest, *j); |
| 193 |
if (!s) |
| 194 |
return log_oom(); |
| 195 |
|
| 196 |
free_and_replace(*j, s); |
| 197 |
} |
| 198 |
|
| 199 |
if (m->source) { |
| 200 |
char *s; |
| 201 |
|
| 202 |
s = resolve_source_path(dest, m->source); |
| 203 |
if (!s) |
| 204 |
return log_oom(); |
| 205 |
|
| 206 |
free_and_replace(m->source, s); |
| 207 |
} else { |
| 208 |
r = allocate_temporary_source(m); |
| 209 |
if (r < 0) |
| 210 |
return r; |
| 211 |
} |
| 212 |
|
| 213 |
if (m->work_dir) { |
| 214 |
char *s; |
| 215 |
|
| 216 |
s = resolve_source_path(dest, m->work_dir); |
| 217 |
if (!s) |
| 218 |
return log_oom(); |
| 219 |
|
| 220 |
free_and_replace(m->work_dir, s); |
| 221 |
} else { |
| 222 |
r = tempfn_random(m->source, NULL, &m->work_dir); |
| 223 |
if (r < 0) |
| 224 |
return log_error_errno(r, "Failed to acquire working directory: %m"); |
| 225 |
} |
| 226 |
|
| 227 |
(void) mkdir_label(m->work_dir, 0700); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
int bind_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) { |
| 235 |
_cleanup_free_ char *source = NULL, *destination = NULL, *opts = NULL, *p = NULL; |
| 236 |
CustomMount *m; |
| 237 |
int r; |
| 238 |
|
| 239 |
assert(l); |
| 240 |
assert(n); |
| 241 |
|
| 242 |
r = extract_many_words(&s, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &source, &destination); |
| 243 |
if (r < 0) |
| 244 |
return r; |
| 245 |
if (r == 0) |
| 246 |
return -EINVAL; |
| 247 |
if (r == 1) { |
| 248 |
destination = strdup(source[0] == '+' ? source+1 : source); |
| 249 |
if (!destination) |
| 250 |
return -ENOMEM; |
| 251 |
} |
| 252 |
if (r == 2 && !isempty(s)) { |
| 253 |
opts = strdup(s); |
| 254 |
if (!opts) |
| 255 |
return -ENOMEM; |
| 256 |
} |
| 257 |
|
| 258 |
r = source_path_parse_nullable(source, &p); |
| 259 |
if (r < 0) |
| 260 |
return r; |
| 261 |
|
| 262 |
if (!path_is_absolute(destination)) |
| 263 |
return -EINVAL; |
| 264 |
|
| 265 |
m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND); |
| 266 |
if (!m) |
| 267 |
return -ENOMEM; |
| 268 |
|
| 269 |
m->source = TAKE_PTR(p); |
| 270 |
m->destination = TAKE_PTR(destination); |
| 271 |
m->read_only = read_only; |
| 272 |
m->options = TAKE_PTR(opts); |
| 273 |
|
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
int tmpfs_mount_parse(CustomMount **l, size_t *n, const char *s) { |
| 278 |
_cleanup_free_ char *path = NULL, *opts = NULL; |
| 279 |
const char *p = ASSERT_PTR(s); |
| 280 |
CustomMount *m; |
| 281 |
int r; |
| 282 |
|
| 283 |
assert(l); |
| 284 |
assert(n); |
| 285 |
|
| 286 |
r = extract_first_word(&p, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 287 |
if (r < 0) |
| 288 |
return r; |
| 289 |
if (r == 0) |
| 290 |
return -EINVAL; |
| 291 |
|
| 292 |
if (isempty(p)) |
| 293 |
opts = strdup("mode=0755"); |
| 294 |
else |
| 295 |
opts = strdup(p); |
| 296 |
if (!opts) |
| 297 |
return -ENOMEM; |
| 298 |
|
| 299 |
if (!path_is_absolute(path)) |
| 300 |
return -EINVAL; |
| 301 |
|
| 302 |
m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS); |
| 303 |
if (!m) |
| 304 |
return -ENOMEM; |
| 305 |
|
| 306 |
m->destination = TAKE_PTR(path); |
| 307 |
m->options = TAKE_PTR(opts); |
| 308 |
|
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
int overlay_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) { |
| 313 |
_cleanup_free_ char *upper = NULL, *destination = NULL; |
| 314 |
_cleanup_strv_free_ char **lower = NULL; |
| 315 |
CustomMount *m; |
| 316 |
int r, k; |
| 317 |
|
| 318 |
k = strv_split_full(&lower, s, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 319 |
if (k < 0) |
| 320 |
return k; |
| 321 |
if (k < 2) |
| 322 |
return -EADDRNOTAVAIL; |
| 323 |
if (k == 2) { |
| 324 |
_cleanup_free_ char *p = NULL; |
| 325 |
|
| 326 |
/* If two parameters are specified, the first one is the lower, the second one the upper directory. And |
| 327 |
* we'll also define the destination mount point the same as the upper. */ |
| 328 |
|
| 329 |
r = source_path_parse(lower[0], &p); |
| 330 |
if (r < 0) |
| 331 |
return r; |
| 332 |
|
| 333 |
free_and_replace(lower[0], p); |
| 334 |
|
| 335 |
r = source_path_parse(lower[1], &p); |
| 336 |
if (r < 0) |
| 337 |
return r; |
| 338 |
|
| 339 |
free_and_replace(lower[1], p); |
| 340 |
|
| 341 |
upper = TAKE_PTR(lower[1]); |
| 342 |
|
| 343 |
destination = strdup(upper[0] == '+' ? upper+1 : upper); /* take the destination without "+" prefix */ |
| 344 |
if (!destination) |
| 345 |
return -ENOMEM; |
| 346 |
} else { |
| 347 |
_cleanup_free_ char *p = NULL; |
| 348 |
|
| 349 |
/* If more than two parameters are specified, the last one is the destination, the second to last one |
| 350 |
* the "upper", and all before that the "lower" directories. */ |
| 351 |
|
| 352 |
destination = lower[k - 1]; |
| 353 |
upper = TAKE_PTR(lower[k - 2]); |
| 354 |
|
| 355 |
STRV_FOREACH(i, lower) { |
| 356 |
r = source_path_parse(*i, &p); |
| 357 |
if (r < 0) |
| 358 |
return r; |
| 359 |
|
| 360 |
free_and_replace(*i, p); |
| 361 |
} |
| 362 |
|
| 363 |
/* If the upper directory is unspecified, then let's create it automatically as a throw-away directory |
| 364 |
* in /var/tmp */ |
| 365 |
r = source_path_parse_nullable(upper, &p); |
| 366 |
if (r < 0) |
| 367 |
return r; |
| 368 |
|
| 369 |
free_and_replace(upper, p); |
| 370 |
|
| 371 |
if (!path_is_absolute(destination)) |
| 372 |
return -EINVAL; |
| 373 |
} |
| 374 |
|
| 375 |
m = custom_mount_add(l, n, CUSTOM_MOUNT_OVERLAY); |
| 376 |
if (!m) |
| 377 |
return -ENOMEM; |
| 378 |
|
| 379 |
m->destination = TAKE_PTR(destination); |
| 380 |
m->source = TAKE_PTR(upper); |
| 381 |
m->lower = TAKE_PTR(lower); |
| 382 |
m->read_only = read_only; |
| 383 |
|
| 384 |
return 0; |
| 385 |
} |
| 386 |
|
| 387 |
int inaccessible_mount_parse(CustomMount **l, size_t *n, const char *s) { |
| 388 |
_cleanup_free_ char *path = NULL; |
| 389 |
CustomMount *m; |
| 390 |
|
| 391 |
assert(l); |
| 392 |
assert(n); |
| 393 |
assert(s); |
| 394 |
|
| 395 |
if (!path_is_absolute(s)) |
| 396 |
return -EINVAL; |
| 397 |
|
| 398 |
path = strdup(s); |
| 399 |
if (!path) |
| 400 |
return -ENOMEM; |
| 401 |
|
| 402 |
m = custom_mount_add(l, n, CUSTOM_MOUNT_INACCESSIBLE); |
| 403 |
if (!m) |
| 404 |
return -ENOMEM; |
| 405 |
|
| 406 |
m->destination = TAKE_PTR(path); |
| 407 |
return 0; |
| 408 |
} |
| 409 |
|
| 410 |
int tmpfs_patch_options( |
| 411 |
const char *options, |
| 412 |
uid_t uid_shift, |
| 413 |
const char *selinux_apifs_context, |
| 414 |
char **ret) { |
| 415 |
|
| 416 |
_cleanup_free_ char *buf = NULL; |
| 417 |
|
| 418 |
assert(ret); |
| 419 |
|
| 420 |
if (options) { |
| 421 |
buf = strdup(options); |
| 422 |
if (!buf) |
| 423 |
return -ENOMEM; |
| 424 |
} |
| 425 |
|
| 426 |
if (uid_shift != UID_INVALID) |
| 427 |
if (strextendf_with_separator(&buf, ",", "uid=" UID_FMT ",gid=" UID_FMT, uid_shift, uid_shift) < 0) |
| 428 |
return -ENOMEM; |
| 429 |
|
| 430 |
#if HAVE_SELINUX |
| 431 |
if (selinux_apifs_context) |
| 432 |
if (strextendf_with_separator(&buf, ",", "context=\"%s\"", selinux_apifs_context) < 0) |
| 433 |
return -ENOMEM; |
| 434 |
#endif |
| 435 |
|
| 436 |
*ret = TAKE_PTR(buf); |
| 437 |
return !!*ret; |
| 438 |
} |
| 439 |
|
| 440 |
int mount_sysfs(const char *dest, MountSettingsMask mount_settings) { |
| 441 |
_cleanup_free_ char *top = NULL, *full = NULL;; |
| 442 |
unsigned long extra_flags = 0; |
| 443 |
int r; |
| 444 |
|
| 445 |
top = path_join(dest, "/sys"); |
| 446 |
if (!top) |
| 447 |
return log_oom(); |
| 448 |
|
| 449 |
r = path_is_mount_point(top); |
| 450 |
if (r < 0) |
| 451 |
return log_error_errno(r, "Failed to determine if '%s' is a mountpoint: %m", top); |
| 452 |
if (r == 0) { |
| 453 |
/* If this is not a mount point yet, then mount a tmpfs there */ |
| 454 |
r = mount_nofollow_verbose(LOG_ERR, "tmpfs", top, "tmpfs", MS_NOSUID|MS_NOEXEC|MS_NODEV, "mode=0555" TMPFS_LIMITS_SYS); |
| 455 |
if (r < 0) |
| 456 |
return r; |
| 457 |
} else { |
| 458 |
r = path_is_fs_type(top, SYSFS_MAGIC); |
| 459 |
if (r < 0) |
| 460 |
return log_error_errno(r, "Failed to determine filesystem type of %s: %m", top); |
| 461 |
|
| 462 |
/* /sys/ might already be mounted as sysfs by the outer child in the !netns case. In this case, it's |
| 463 |
* all good. Don't touch it because we don't have the right to do so, see |
| 464 |
* https://github.com/systemd/systemd/issues/1555. |
| 465 |
*/ |
| 466 |
if (r > 0) |
| 467 |
return 0; |
| 468 |
} |
| 469 |
|
| 470 |
full = path_join(top, "/full"); |
| 471 |
if (!full) |
| 472 |
return log_oom(); |
| 473 |
|
| 474 |
if (mkdir(full, 0755) < 0 && errno != EEXIST) |
| 475 |
return log_error_errno(errno, "Failed to create directory '%s': %m", full); |
| 476 |
|
| 477 |
if (FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_RO)) |
| 478 |
extra_flags |= MS_RDONLY; |
| 479 |
|
| 480 |
r = mount_nofollow_verbose(LOG_ERR, "sysfs", full, "sysfs", |
| 481 |
MS_NOSUID|MS_NOEXEC|MS_NODEV|extra_flags, NULL); |
| 482 |
if (r < 0) |
| 483 |
return r; |
| 484 |
|
| 485 |
FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") { |
| 486 |
_cleanup_free_ char *from = NULL, *to = NULL; |
| 487 |
|
| 488 |
from = path_join(full, x); |
| 489 |
if (!from) |
| 490 |
return log_oom(); |
| 491 |
|
| 492 |
to = path_join(top, x); |
| 493 |
if (!to) |
| 494 |
return log_oom(); |
| 495 |
|
| 496 |
(void) mkdir(to, 0755); |
| 497 |
|
| 498 |
r = mount_nofollow_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL); |
| 499 |
if (r < 0) |
| 500 |
return r; |
| 501 |
|
| 502 |
r = mount_nofollow_verbose(LOG_ERR, NULL, to, NULL, |
| 503 |
MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL); |
| 504 |
if (r < 0) |
| 505 |
return r; |
| 506 |
} |
| 507 |
|
| 508 |
r = umount_verbose(LOG_ERR, full, UMOUNT_NOFOLLOW); |
| 509 |
if (r < 0) |
| 510 |
return r; |
| 511 |
|
| 512 |
if (rmdir(full) < 0) |
| 513 |
return log_error_errno(errno, "Failed to remove %s: %m", full); |
| 514 |
|
| 515 |
/* Create mountpoints. Otherwise we are not allowed since we remount /sys/ read-only. */ |
| 516 |
FOREACH_STRING(p, "/fs/cgroup", "/fs/bpf") { |
| 517 |
_cleanup_free_ char *x = path_join(top, p); |
| 518 |
if (!x) |
| 519 |
return log_oom(); |
| 520 |
|
| 521 |
(void) mkdir_p(x, 0755); |
| 522 |
} |
| 523 |
|
| 524 |
return mount_nofollow_verbose(LOG_ERR, NULL, top, NULL, |
| 525 |
MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL); |
| 526 |
} |
| 527 |
|
| 528 |
#define PROC_DEFAULT_MOUNT_FLAGS (MS_NOSUID|MS_NOEXEC|MS_NODEV) |
| 529 |
#define SYS_DEFAULT_MOUNT_FLAGS (MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV) |
| 530 |
|
| 531 |
int mount_all(const char *dest, |
| 532 |
MountSettingsMask mount_settings, |
| 533 |
uid_t uid_shift, |
| 534 |
const char *selinux_apifs_context) { |
| 535 |
|
| 536 |
#define PROC_INACCESSIBLE_REG(path) \ |
| 537 |
{ "/run/host/inaccessible/reg", (path), NULL, NULL, MS_BIND, \ |
| 538 |
MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ \ |
| 539 |
{ NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \ |
| 540 |
MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */ |
| 541 |
|
| 542 |
#define PROC_READ_ONLY(path) \ |
| 543 |
{ (path), (path), NULL, NULL, MS_BIND, \ |
| 544 |
MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ \ |
| 545 |
{ NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \ |
| 546 |
MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */ |
| 547 |
|
| 548 |
typedef struct MountPoint { |
| 549 |
const char *what; |
| 550 |
const char *where; |
| 551 |
const char *type; |
| 552 |
const char *options; |
| 553 |
unsigned long flags; |
| 554 |
MountSettingsMask mount_settings; |
| 555 |
} MountPoint; |
| 556 |
|
| 557 |
static const MountPoint mount_table[] = { |
| 558 |
/* First we list inner child mounts (i.e. mounts applied *after* entering user namespacing when we are privileged) */ |
| 559 |
{ "proc", "/proc", "proc", NULL, PROC_DEFAULT_MOUNT_FLAGS, |
| 560 |
MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_MKDIR|MOUNT_FOLLOW_SYMLINKS }, /* we follow symlinks here since not following them requires /proc/ already being mounted, which we don't have here. */ |
| 561 |
|
| 562 |
{ "/proc/sys", "/proc/sys", NULL, NULL, MS_BIND, |
| 563 |
MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ |
| 564 |
|
| 565 |
{ "/proc/sys/net", "/proc/sys/net", NULL, NULL, MS_BIND, |
| 566 |
MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_APIVFS_NETNS }, /* (except for this) */ |
| 567 |
|
| 568 |
{ NULL, "/proc/sys", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, |
| 569 |
MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* ... then, make it r/o */ |
| 570 |
|
| 571 |
/* Make these files inaccessible to container payloads: they potentially leak information about kernel |
| 572 |
* internals or the host's execution environment to the container */ |
| 573 |
PROC_INACCESSIBLE_REG("/proc/kallsyms"), |
| 574 |
PROC_INACCESSIBLE_REG("/proc/kcore"), |
| 575 |
PROC_INACCESSIBLE_REG("/proc/keys"), |
| 576 |
PROC_INACCESSIBLE_REG("/proc/sysrq-trigger"), |
| 577 |
PROC_INACCESSIBLE_REG("/proc/timer_list"), |
| 578 |
|
| 579 |
/* Make these directories read-only to container payloads: they show hardware information, and in some |
| 580 |
* cases contain tunables the container really shouldn't have access to. */ |
| 581 |
PROC_READ_ONLY("/proc/acpi"), |
| 582 |
PROC_READ_ONLY("/proc/apm"), |
| 583 |
PROC_READ_ONLY("/proc/asound"), |
| 584 |
PROC_READ_ONLY("/proc/bus"), |
| 585 |
PROC_READ_ONLY("/proc/fs"), |
| 586 |
PROC_READ_ONLY("/proc/irq"), |
| 587 |
PROC_READ_ONLY("/proc/scsi"), |
| 588 |
|
| 589 |
{ "mqueue", "/dev/mqueue", "mqueue", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, |
| 590 |
MOUNT_IN_USERNS|MOUNT_MKDIR }, |
| 591 |
|
| 592 |
/* Then we list outer child mounts (i.e. mounts applied *before* entering user namespacing when we are privileged) */ |
| 593 |
{ "tmpfs", "/tmp", "tmpfs", "mode=01777" NESTED_TMPFS_LIMITS, MS_NOSUID|MS_NODEV|MS_STRICTATIME, |
| 594 |
MOUNT_FATAL|MOUNT_APPLY_TMPFS_TMP|MOUNT_MKDIR|MOUNT_USRQUOTA_GRACEFUL }, |
| 595 |
{ "tmpfs", "/sys", "tmpfs", "mode=0555" TMPFS_LIMITS_SYS, MS_NOSUID|MS_NOEXEC|MS_NODEV, |
| 596 |
MOUNT_FATAL|MOUNT_APPLY_APIVFS_NETNS|MOUNT_MKDIR|MOUNT_UNMANAGED }, |
| 597 |
{ "sysfs", "/sys", "sysfs", NULL, SYS_DEFAULT_MOUNT_FLAGS, |
| 598 |
MOUNT_FATAL|MOUNT_APPLY_APIVFS_RO|MOUNT_MKDIR|MOUNT_UNMANAGED }, /* skipped if above was mounted */ |
| 599 |
{ "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, |
| 600 |
MOUNT_FATAL|MOUNT_MKDIR|MOUNT_UNMANAGED }, /* skipped if above was mounted */ |
| 601 |
{ "tmpfs", "/dev", "tmpfs", "mode=0755" TMPFS_LIMITS_PRIVATE_DEV, MS_NOSUID|MS_STRICTATIME, |
| 602 |
MOUNT_FATAL|MOUNT_MKDIR }, |
| 603 |
{ "tmpfs", "/dev/shm", "tmpfs", "mode=01777" NESTED_TMPFS_LIMITS, MS_NOSUID|MS_NODEV|MS_STRICTATIME, |
| 604 |
MOUNT_FATAL|MOUNT_MKDIR|MOUNT_USRQUOTA_GRACEFUL }, |
| 605 |
{ "tmpfs", "/run", "tmpfs", "mode=0755" TMPFS_LIMITS_RUN, MS_NOSUID|MS_NODEV|MS_STRICTATIME, |
| 606 |
MOUNT_FATAL|MOUNT_MKDIR }, |
| 607 |
{ "/run/host", "/run/host", NULL, NULL, MS_BIND, |
| 608 |
MOUNT_FATAL|MOUNT_MKDIR|MOUNT_PREFIX_ROOT }, /* Prepare this so that we can make it read-only when we are done */ |
| 609 |
{ "/etc/os-release", "/run/host/os-release", NULL, NULL, MS_BIND, |
| 610 |
MOUNT_TOUCH }, /* As per kernel interface requirements, bind mount first (creating mount points) and make read-only later */ |
| 611 |
{ "/usr/lib/os-release", "/run/host/os-release", NULL, NULL, MS_BIND, |
| 612 |
MOUNT_FATAL }, /* If /etc/os-release doesn't exist use the version in /usr/lib as fallback */ |
| 613 |
{ NULL, "/run/host/os-release", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, |
| 614 |
MOUNT_FATAL }, |
| 615 |
{ NULL, "/run/host/os-release", NULL, NULL, MS_PRIVATE, |
| 616 |
MOUNT_FATAL }, /* Turn off propagation (we only want that for the mount propagation tunnel dir) */ |
| 617 |
{ NULL, "/run/host", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, |
| 618 |
MOUNT_FATAL|MOUNT_IN_USERNS }, |
| 619 |
#if HAVE_SELINUX |
| 620 |
{ "/sys/fs/selinux", "/sys/fs/selinux", NULL, NULL, MS_BIND, |
| 621 |
MOUNT_MKDIR|MOUNT_PRIVILEGED }, /* Bind mount first (mkdir/chown the mount point in case /sys/ is mounted as minimal skeleton tmpfs) */ |
| 622 |
{ NULL, "/sys/fs/selinux", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, |
| 623 |
MOUNT_UNMANAGED|MOUNT_PRIVILEGED }, /* Then, make it r/o (don't mkdir/chown the mount point here, the previous entry already did that) */ |
| 624 |
{ NULL, "/sys/fs/selinux", NULL, NULL, MS_PRIVATE, |
| 625 |
MOUNT_UNMANAGED|MOUNT_PRIVILEGED }, /* Turn off propagation (we only want that for the mount propagation tunnel dir) */ |
| 626 |
#endif |
| 627 |
}; |
| 628 |
|
| 629 |
bool use_userns = FLAGS_SET(mount_settings, MOUNT_USE_USERNS); |
| 630 |
bool netns = FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_NETNS); |
| 631 |
bool ro = FLAGS_SET(mount_settings, MOUNT_APPLY_APIVFS_RO); |
| 632 |
bool in_userns = FLAGS_SET(mount_settings, MOUNT_IN_USERNS); |
| 633 |
bool tmpfs_tmp = FLAGS_SET(mount_settings, MOUNT_APPLY_TMPFS_TMP); |
| 634 |
bool unmanaged = FLAGS_SET(mount_settings, MOUNT_UNMANAGED); |
| 635 |
bool privileged = FLAGS_SET(mount_settings, MOUNT_PRIVILEGED); |
| 636 |
int r; |
| 637 |
|
| 638 |
FOREACH_ELEMENT(m, mount_table) { |
| 639 |
_cleanup_free_ char *where = NULL, *options = NULL, *prefixed = NULL; |
| 640 |
bool fatal = FLAGS_SET(m->mount_settings, MOUNT_FATAL); |
| 641 |
const char *o; |
| 642 |
|
| 643 |
/* If we are in managed user namespace mode but the entry is marked for mount outside of |
| 644 |
* managed user namespace mode, and to be mounted outside the user namespace, then skip it */ |
| 645 |
if (!unmanaged && FLAGS_SET(m->mount_settings, MOUNT_UNMANAGED) && !FLAGS_SET(m->mount_settings, MOUNT_IN_USERNS)) |
| 646 |
continue; |
| 647 |
|
| 648 |
if (in_userns != FLAGS_SET(m->mount_settings, MOUNT_IN_USERNS)) |
| 649 |
continue; |
| 650 |
|
| 651 |
if (!netns && FLAGS_SET(m->mount_settings, MOUNT_APPLY_APIVFS_NETNS)) |
| 652 |
continue; |
| 653 |
|
| 654 |
if (!ro && FLAGS_SET(m->mount_settings, MOUNT_APPLY_APIVFS_RO)) |
| 655 |
continue; |
| 656 |
|
| 657 |
if (!tmpfs_tmp && FLAGS_SET(m->mount_settings, MOUNT_APPLY_TMPFS_TMP)) |
| 658 |
continue; |
| 659 |
|
| 660 |
if (!privileged && FLAGS_SET(m->mount_settings, MOUNT_PRIVILEGED)) |
| 661 |
continue; |
| 662 |
|
| 663 |
r = chase(m->where, dest, CHASE_NONEXISTENT|CHASE_PREFIX_ROOT, &where, NULL); |
| 664 |
if (r < 0) |
| 665 |
return log_error_errno(r, "Failed to resolve %s%s: %m", strempty(dest), m->where); |
| 666 |
|
| 667 |
/* Skip this entry if it is not a remount. */ |
| 668 |
if (m->what) { |
| 669 |
r = path_is_mount_point(where); |
| 670 |
if (r < 0 && r != -ENOENT) |
| 671 |
return log_error_errno(r, "Failed to detect whether %s is a mount point: %m", where); |
| 672 |
if (r > 0) |
| 673 |
continue; |
| 674 |
} |
| 675 |
|
| 676 |
if ((m->mount_settings & (MOUNT_MKDIR|MOUNT_TOUCH)) != 0) { |
| 677 |
uid_t u = (use_userns && !in_userns) ? uid_shift : UID_INVALID; |
| 678 |
|
| 679 |
if (FLAGS_SET(m->mount_settings, MOUNT_TOUCH)) |
| 680 |
r = mkdir_parents_safe(dest, where, 0755, u, u, 0); |
| 681 |
else |
| 682 |
r = mkdir_p_safe(dest, where, 0755, u, u, 0); |
| 683 |
if (r < 0 && r != -EEXIST) { |
| 684 |
if (fatal && r != -EROFS) |
| 685 |
return log_error_errno(r, "Failed to create directory %s: %m", where); |
| 686 |
|
| 687 |
log_debug_errno(r, "Failed to create directory %s: %m", where); |
| 688 |
|
| 689 |
/* If mkdir() or chown() failed due to the root directory being read only, |
| 690 |
* attempt to mount this fs anyway and let mount_verbose log any errors */ |
| 691 |
if (r != -EROFS) |
| 692 |
continue; |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
if (FLAGS_SET(m->mount_settings, MOUNT_TOUCH)) { |
| 697 |
r = touch(where); |
| 698 |
if (r < 0 && r != -EEXIST) { |
| 699 |
if (fatal && r != -EROFS) |
| 700 |
return log_error_errno(r, "Failed to create file %s: %m", where); |
| 701 |
|
| 702 |
log_debug_errno(r, "Failed to create file %s: %m", where); |
| 703 |
if (r != -EROFS) |
| 704 |
continue; |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
o = m->options; |
| 709 |
if (streq_ptr(m->type, "tmpfs")) { |
| 710 |
r = tmpfs_patch_options(o, in_userns ? 0 : uid_shift, selinux_apifs_context, &options); |
| 711 |
if (r < 0) |
| 712 |
return log_oom(); |
| 713 |
if (r > 0) |
| 714 |
o = options; |
| 715 |
} |
| 716 |
|
| 717 |
if (FLAGS_SET(m->mount_settings, MOUNT_USRQUOTA_GRACEFUL)) { |
| 718 |
r = mount_option_supported(m->type, /* key= */ "usrquota", /* value= */ NULL); |
| 719 |
if (r < 0) |
| 720 |
log_warning_errno(r, "Failed to determine if '%s' supports 'usrquota', assuming it doesn't: %m", m->type); |
| 721 |
else if (r == 0) |
| 722 |
log_debug("Kernel doesn't support 'usrquota' on '%s', not including in mount options for '%s'.", m->type, m->where); |
| 723 |
else { |
| 724 |
_cleanup_free_ char *joined = NULL; |
| 725 |
|
| 726 |
if (!strextend_with_separator(&joined, ",", o ?: POINTER_MAX, "usrquota")) |
| 727 |
return log_oom(); |
| 728 |
|
| 729 |
free_and_replace(options, joined); |
| 730 |
o = options; |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
if (FLAGS_SET(m->mount_settings, MOUNT_PREFIX_ROOT)) { |
| 735 |
/* Optionally prefix the mount source with the root dir. This is useful in bind |
| 736 |
* mounts to be created within the container image before we transition into it. Note |
| 737 |
* that MOUNT_IN_USERNS is run after we transitioned hence prefixing is not necessary |
| 738 |
* for those. */ |
| 739 |
r = chase(m->what, dest, CHASE_PREFIX_ROOT, &prefixed, NULL); |
| 740 |
if (r < 0) |
| 741 |
return log_error_errno(r, "Failed to resolve %s%s: %m", strempty(dest), m->what); |
| 742 |
} |
| 743 |
|
| 744 |
r = mount_verbose_full( |
| 745 |
fatal ? LOG_ERR : LOG_DEBUG, |
| 746 |
prefixed ?: m->what, |
| 747 |
where, |
| 748 |
m->type, |
| 749 |
m->flags, |
| 750 |
o, |
| 751 |
FLAGS_SET(m->mount_settings, MOUNT_FOLLOW_SYMLINKS)); |
| 752 |
if (r < 0 && fatal) |
| 753 |
return r; |
| 754 |
} |
| 755 |
|
| 756 |
return 0; |
| 757 |
} |
| 758 |
|
| 759 |
static int parse_mount_bind_options(const char *options, unsigned long *open_tree_flags, char **mount_opts, RemountIdmapping *idmapping) { |
| 760 |
unsigned long flags = *ASSERT_PTR(open_tree_flags); |
| 761 |
char *opts = NULL; |
| 762 |
RemountIdmapping new_idmapping = *ASSERT_PTR(idmapping); |
| 763 |
int r; |
| 764 |
|
| 765 |
assert(options); |
| 766 |
assert(mount_opts); |
| 767 |
|
| 768 |
for (;;) { |
| 769 |
_cleanup_free_ char *word = NULL; |
| 770 |
|
| 771 |
r = extract_first_word(&options, &word, ",", 0); |
| 772 |
if (r < 0) |
| 773 |
return log_error_errno(r, "Failed to extract mount option: %m"); |
| 774 |
if (r == 0) |
| 775 |
break; |
| 776 |
|
| 777 |
if (streq(word, "rbind")) |
| 778 |
flags |= AT_RECURSIVE; |
| 779 |
else if (streq(word, "norbind")) |
| 780 |
flags &= ~AT_RECURSIVE; |
| 781 |
else if (streq(word, "idmap")) |
| 782 |
new_idmapping = REMOUNT_IDMAPPING_HOST_ROOT; |
| 783 |
else if (streq(word, "noidmap")) |
| 784 |
new_idmapping = REMOUNT_IDMAPPING_NONE; |
| 785 |
else if (streq(word, "rootidmap")) |
| 786 |
new_idmapping = REMOUNT_IDMAPPING_HOST_OWNER; |
| 787 |
else if (streq(word, "owneridmap")) |
| 788 |
new_idmapping = REMOUNT_IDMAPPING_HOST_OWNER_TO_TARGET_OWNER; |
| 789 |
else |
| 790 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 791 |
"Invalid bind mount option: %s", word); |
| 792 |
} |
| 793 |
|
| 794 |
*open_tree_flags = flags; |
| 795 |
*idmapping = new_idmapping; |
| 796 |
/* in the future mount_opts will hold string options for mount(2) */ |
| 797 |
*mount_opts = opts; |
| 798 |
|
| 799 |
return 0; |
| 800 |
} |
| 801 |
|
| 802 |
static int mount_bind(const char *dest, CustomMount *m, uid_t uid_shift, uid_t uid_range) { |
| 803 |
_cleanup_free_ char *mount_opts = NULL, *where = NULL; |
| 804 |
unsigned long open_tree_flags = OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC | AT_RECURSIVE; |
| 805 |
struct stat source_st, dest_st; |
| 806 |
uid_t dest_uid = UID_INVALID; |
| 807 |
int r; |
| 808 |
RemountIdmapping idmapping = REMOUNT_IDMAPPING_NONE; |
| 809 |
|
| 810 |
assert(dest); |
| 811 |
assert(m); |
| 812 |
|
| 813 |
if (m->options) { |
| 814 |
r = parse_mount_bind_options(m->options, &open_tree_flags, &mount_opts, &idmapping); |
| 815 |
if (r < 0) |
| 816 |
return r; |
| 817 |
} |
| 818 |
|
| 819 |
/* ID remapping cannot be done if user namespaces are not in use (uid_shift is UID_INVALID). |
| 820 |
* Fail if idmapping was explicitly requested in this state. Otherwise, treat UID_INVALID |
| 821 |
* as 0 for ownership operations. */ |
| 822 |
if (idmapping != REMOUNT_IDMAPPING_NONE && !uid_is_valid(uid_shift)) |
| 823 |
return log_error_errno( |
| 824 |
SYNTHETIC_ERRNO(EINVAL), |
| 825 |
"ID remapping requested for %s, but user namespacing is not enabled.", |
| 826 |
m->source); |
| 827 |
|
| 828 |
uid_t chown_uid = uid_is_valid(uid_shift) ? uid_shift : 0; |
| 829 |
|
| 830 |
/* If this is a bind mount from a temporary sources change ownership of the source to the container's |
| 831 |
* root UID. Otherwise it would always show up as "nobody" if user namespacing is used. */ |
| 832 |
if (m->rm_rf_tmpdir && chown(m->source, chown_uid, chown_uid) < 0) |
| 833 |
return log_error_errno(errno, "Failed to chown %s: %m", m->source); |
| 834 |
|
| 835 |
/* UID/GIDs of idmapped mounts are always resolved in the caller's user namespace. In other |
| 836 |
* words, they're not nested. If we're doing an idmapped mount from a bind mount that's |
| 837 |
* already idmapped itself, the old idmap is replaced with the new one. This means that the |
| 838 |
* source uid which we put in the idmap userns has to be the uid of mount source in the |
| 839 |
* caller's userns *without* any mount idmapping in place. To get that uid, we clone the |
| 840 |
* mount source tree and clear any existing idmapping and temporarily mount that tree over |
| 841 |
* the mount source before we stat the mount source to figure out the source uid. */ |
| 842 |
_cleanup_close_ int fd_clone = |
| 843 |
idmapping == REMOUNT_IDMAPPING_NONE ? |
| 844 |
RET_NERRNO(open_tree( |
| 845 |
AT_FDCWD, |
| 846 |
m->source, |
| 847 |
open_tree_flags)) : |
| 848 |
open_tree_try_drop_idmap( |
| 849 |
AT_FDCWD, |
| 850 |
m->source, |
| 851 |
open_tree_flags); |
| 852 |
if (fd_clone < 0) |
| 853 |
return log_error_errno(errno, "Failed to clone %s: %m", m->source); |
| 854 |
|
| 855 |
if (fstat(fd_clone, &source_st) < 0) |
| 856 |
return log_error_errno(errno, "Failed to stat %s: %m", m->source); |
| 857 |
|
| 858 |
r = chase(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL); |
| 859 |
if (r < 0) |
| 860 |
return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination); |
| 861 |
if (r > 0) { /* Path exists already? */ |
| 862 |
|
| 863 |
if (stat(where, &dest_st) < 0) |
| 864 |
return log_error_errno(errno, "Failed to stat %s: %m", where); |
| 865 |
|
| 866 |
dest_uid = uid_is_valid(m->destination_uid) ? chown_uid + m->destination_uid : dest_st.st_uid; |
| 867 |
|
| 868 |
if (S_ISDIR(source_st.st_mode) && !S_ISDIR(dest_st.st_mode)) |
| 869 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 870 |
"Cannot bind mount directory %s on file %s.", |
| 871 |
m->source, where); |
| 872 |
|
| 873 |
if (!S_ISDIR(source_st.st_mode) && S_ISDIR(dest_st.st_mode)) |
| 874 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 875 |
"Cannot bind mount file %s on directory %s.", |
| 876 |
m->source, where); |
| 877 |
|
| 878 |
} else { /* Path doesn't exist yet? */ |
| 879 |
r = mkdir_parents_safe_label(dest, where, 0755, chown_uid, chown_uid, MKDIR_IGNORE_EXISTING); |
| 880 |
if (r < 0) |
| 881 |
return log_error_errno(r, "Failed to make parents of %s: %m", where); |
| 882 |
|
| 883 |
/* Create the mount point. Any non-directory file can be |
| 884 |
* mounted on any non-directory file (regular, fifo, socket, |
| 885 |
* char, block). |
| 886 |
*/ |
| 887 |
if (S_ISDIR(source_st.st_mode)) |
| 888 |
r = mkdir_label(where, 0755); |
| 889 |
else |
| 890 |
r = touch(where); |
| 891 |
if (r < 0) |
| 892 |
return log_error_errno(r, "Failed to create mount point %s: %m", where); |
| 893 |
|
| 894 |
if (chown(where, chown_uid, chown_uid) < 0) |
| 895 |
return log_error_errno(errno, "Failed to chown %s: %m", where); |
| 896 |
|
| 897 |
dest_uid = chown_uid + (uid_is_valid(m->destination_uid) ? m->destination_uid : 0); |
| 898 |
} |
| 899 |
|
| 900 |
if (move_mount(fd_clone, "", AT_FDCWD, where, MOVE_MOUNT_F_EMPTY_PATH) < 0) |
| 901 |
return log_error_errno(errno, "Failed to mount %s to %s: %m", m->source, where); |
| 902 |
|
| 903 |
fd_clone = safe_close(fd_clone); |
| 904 |
|
| 905 |
if (m->read_only) { |
| 906 |
r = bind_remount_recursive(where, MS_RDONLY, MS_RDONLY, NULL); |
| 907 |
if (r < 0) |
| 908 |
return log_error_errno(r, "Read-only bind mount failed: %m"); |
| 909 |
} |
| 910 |
|
| 911 |
if (idmapping != REMOUNT_IDMAPPING_NONE) { |
| 912 |
r = remount_idmap(STRV_MAKE(where), uid_shift, uid_range, source_st.st_uid, dest_uid, idmapping); |
| 913 |
if (r < 0) |
| 914 |
return log_error_errno(r, "Failed to map ids for bind mount %s: %m", where); |
| 915 |
} |
| 916 |
|
| 917 |
return 0; |
| 918 |
} |
| 919 |
|
| 920 |
static int mount_tmpfs(const char *dest, CustomMount *m, uid_t uid_shift, const char *selinux_apifs_context) { |
| 921 |
const char *options; |
| 922 |
_cleanup_free_ char *buf = NULL, *where = NULL; |
| 923 |
int r; |
| 924 |
|
| 925 |
assert(dest); |
| 926 |
assert(m); |
| 927 |
|
| 928 |
r = chase(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL); |
| 929 |
if (r < 0) |
| 930 |
return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination); |
| 931 |
if (r == 0) { /* Doesn't exist yet? */ |
| 932 |
r = mkdir_p_label(where, 0755); |
| 933 |
if (r < 0) |
| 934 |
return log_error_errno(r, "Creating mount point for tmpfs %s failed: %m", where); |
| 935 |
} |
| 936 |
|
| 937 |
r = tmpfs_patch_options(m->options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf); |
| 938 |
if (r < 0) |
| 939 |
return log_oom(); |
| 940 |
options = r > 0 ? buf : m->options; |
| 941 |
|
| 942 |
return mount_nofollow_verbose(LOG_ERR, "tmpfs", where, "tmpfs", MS_NODEV|MS_STRICTATIME, options); |
| 943 |
} |
| 944 |
|
| 945 |
static char *joined_and_escaped_lower_dirs(char **lower) { |
| 946 |
_cleanup_strv_free_ char **sv = NULL; |
| 947 |
|
| 948 |
sv = strv_copy(lower); |
| 949 |
if (!sv) |
| 950 |
return NULL; |
| 951 |
|
| 952 |
strv_reverse(sv); |
| 953 |
|
| 954 |
if (!strv_shell_escape(sv, ",:")) |
| 955 |
return NULL; |
| 956 |
|
| 957 |
return strv_join(sv, ":"); |
| 958 |
} |
| 959 |
|
| 960 |
static int mount_overlay(const char *dest, CustomMount *m) { |
| 961 |
_cleanup_free_ char *lower = NULL, *where = NULL, *escaped_source = NULL; |
| 962 |
const char *options; |
| 963 |
int r; |
| 964 |
|
| 965 |
assert(dest); |
| 966 |
assert(m); |
| 967 |
|
| 968 |
r = chase(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL); |
| 969 |
if (r < 0) |
| 970 |
return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination); |
| 971 |
if (r == 0) { /* Doesn't exist yet? */ |
| 972 |
r = mkdir_label(where, 0755); |
| 973 |
if (r < 0) |
| 974 |
return log_error_errno(r, "Creating mount point for overlay %s failed: %m", where); |
| 975 |
} |
| 976 |
|
| 977 |
(void) mkdir_p_label(m->source, 0755); |
| 978 |
|
| 979 |
lower = joined_and_escaped_lower_dirs(m->lower); |
| 980 |
if (!lower) |
| 981 |
return log_oom(); |
| 982 |
|
| 983 |
escaped_source = shell_escape(m->source, ",:"); |
| 984 |
if (!escaped_source) |
| 985 |
return log_oom(); |
| 986 |
|
| 987 |
if (m->read_only) |
| 988 |
options = strjoina("lowerdir=", escaped_source, ":", lower); |
| 989 |
else { |
| 990 |
_cleanup_free_ char *escaped_work_dir = NULL; |
| 991 |
|
| 992 |
escaped_work_dir = shell_escape(m->work_dir, ",:"); |
| 993 |
if (!escaped_work_dir) |
| 994 |
return log_oom(); |
| 995 |
|
| 996 |
options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir); |
| 997 |
} |
| 998 |
|
| 999 |
return mount_nofollow_verbose(LOG_ERR, "overlay", where, "overlay", m->read_only ? MS_RDONLY : 0, options); |
| 1000 |
} |
| 1001 |
|
| 1002 |
static int mount_inaccessible(const char *dest, CustomMount *m) { |
| 1003 |
_cleanup_free_ char *where = NULL, *source = NULL; |
| 1004 |
struct stat st; |
| 1005 |
int r; |
| 1006 |
|
| 1007 |
assert(dest); |
| 1008 |
assert(m); |
| 1009 |
|
| 1010 |
r = chase_and_stat(m->destination, dest, CHASE_PREFIX_ROOT, &where, &st); |
| 1011 |
if (r < 0) { |
| 1012 |
log_full_errno(m->graceful ? LOG_DEBUG : LOG_ERR, r, "Failed to resolve %s/%s: %m", dest, m->destination); |
| 1013 |
return m->graceful ? 0 : r; |
| 1014 |
} |
| 1015 |
|
| 1016 |
r = mode_to_inaccessible_node(NULL, st.st_mode, &source); |
| 1017 |
if (r < 0) |
| 1018 |
return m->graceful ? 0 : r; |
| 1019 |
|
| 1020 |
r = mount_nofollow_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, source, where, NULL, MS_BIND, NULL); |
| 1021 |
if (r < 0) |
| 1022 |
return m->graceful ? 0 : r; |
| 1023 |
|
| 1024 |
r = mount_nofollow_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, NULL, where, NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, NULL); |
| 1025 |
if (r < 0) { |
| 1026 |
(void) umount_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, where, UMOUNT_NOFOLLOW); |
| 1027 |
return m->graceful ? 0 : r; |
| 1028 |
} |
| 1029 |
|
| 1030 |
return 0; |
| 1031 |
} |
| 1032 |
|
| 1033 |
static int mount_arbitrary(const char *dest, CustomMount *m) { |
| 1034 |
_cleanup_free_ char *where = NULL; |
| 1035 |
int r; |
| 1036 |
|
| 1037 |
assert(dest); |
| 1038 |
assert(m); |
| 1039 |
|
| 1040 |
r = chase(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL); |
| 1041 |
if (r < 0) |
| 1042 |
return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination); |
| 1043 |
if (r == 0) { /* Doesn't exist yet? */ |
| 1044 |
r = mkdir_p_label(where, 0755); |
| 1045 |
if (r < 0) |
| 1046 |
return log_error_errno(r, "Creating mount point for mount %s failed: %m", where); |
| 1047 |
} |
| 1048 |
|
| 1049 |
return mount_nofollow_verbose(LOG_ERR, m->source, where, m->type_argument, 0, m->options); |
| 1050 |
} |
| 1051 |
|
| 1052 |
int mount_custom( |
| 1053 |
const char *dest, |
| 1054 |
CustomMount *mounts, size_t n, |
| 1055 |
uid_t uid_shift, |
| 1056 |
uid_t uid_range, |
| 1057 |
const char *selinux_apifs_context, |
| 1058 |
MountSettingsMask mount_settings) { |
| 1059 |
int r; |
| 1060 |
|
| 1061 |
assert(dest); |
| 1062 |
|
| 1063 |
FOREACH_ARRAY(m, mounts, n) { |
| 1064 |
if (FLAGS_SET(mount_settings, MOUNT_IN_USERNS) != m->in_userns) |
| 1065 |
continue; |
| 1066 |
|
| 1067 |
if (FLAGS_SET(mount_settings, MOUNT_ROOT_ONLY) && !path_equal(m->destination, "/")) |
| 1068 |
continue; |
| 1069 |
|
| 1070 |
if (FLAGS_SET(mount_settings, MOUNT_NON_ROOT_ONLY) && path_equal(m->destination, "/")) |
| 1071 |
continue; |
| 1072 |
|
| 1073 |
switch (m->type) { |
| 1074 |
|
| 1075 |
case CUSTOM_MOUNT_BIND: |
| 1076 |
r = mount_bind(dest, m, uid_shift, uid_range); |
| 1077 |
break; |
| 1078 |
|
| 1079 |
case CUSTOM_MOUNT_TMPFS: |
| 1080 |
r = mount_tmpfs(dest, m, uid_shift, selinux_apifs_context); |
| 1081 |
break; |
| 1082 |
|
| 1083 |
case CUSTOM_MOUNT_OVERLAY: |
| 1084 |
r = mount_overlay(dest, m); |
| 1085 |
break; |
| 1086 |
|
| 1087 |
case CUSTOM_MOUNT_INACCESSIBLE: |
| 1088 |
r = mount_inaccessible(dest, m); |
| 1089 |
break; |
| 1090 |
|
| 1091 |
case CUSTOM_MOUNT_ARBITRARY: |
| 1092 |
r = mount_arbitrary(dest, m); |
| 1093 |
break; |
| 1094 |
|
| 1095 |
default: |
| 1096 |
assert_not_reached(); |
| 1097 |
} |
| 1098 |
|
| 1099 |
if (r < 0) |
| 1100 |
return r; |
| 1101 |
} |
| 1102 |
|
| 1103 |
return 0; |
| 1104 |
} |
| 1105 |
|
| 1106 |
bool has_custom_root_mount(const CustomMount *mounts, size_t n) { |
| 1107 |
FOREACH_ARRAY(m, mounts, n) |
| 1108 |
if (path_equal(m->destination, "/")) |
| 1109 |
return true; |
| 1110 |
|
| 1111 |
return false; |
| 1112 |
} |
| 1113 |
|
| 1114 |
static int setup_volatile_state(const char *directory) { |
| 1115 |
int r; |
| 1116 |
|
| 1117 |
assert(directory); |
| 1118 |
|
| 1119 |
/* --volatile=state means we simply overmount /var with a tmpfs, and the rest read-only. */ |
| 1120 |
|
| 1121 |
/* First, remount the root directory. */ |
| 1122 |
r = bind_remount_recursive(directory, MS_RDONLY, MS_RDONLY, NULL); |
| 1123 |
if (r < 0) |
| 1124 |
return log_error_errno(r, "Failed to remount %s read-only: %m", directory); |
| 1125 |
|
| 1126 |
return 0; |
| 1127 |
} |
| 1128 |
|
| 1129 |
static int setup_volatile_state_after_remount_idmap(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) { |
| 1130 |
_cleanup_free_ char *buf = NULL; |
| 1131 |
int r; |
| 1132 |
|
| 1133 |
assert(directory); |
| 1134 |
|
| 1135 |
/* Then, after remount_idmap(), overmount /var/ with a tmpfs. */ |
| 1136 |
|
| 1137 |
_cleanup_free_ char *p = path_join(directory, "/var"); |
| 1138 |
if (!p) |
| 1139 |
return log_oom(); |
| 1140 |
|
| 1141 |
r = mkdir(p, 0755); |
| 1142 |
if (r < 0 && errno != EEXIST) |
| 1143 |
return log_error_errno(errno, "Failed to create %s: %m", directory); |
| 1144 |
|
| 1145 |
const char *options = "mode=0755" TMPFS_LIMITS_VOLATILE_STATE; |
| 1146 |
r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf); |
| 1147 |
if (r < 0) |
| 1148 |
return log_oom(); |
| 1149 |
if (r > 0) |
| 1150 |
options = buf; |
| 1151 |
|
| 1152 |
return mount_nofollow_verbose(LOG_ERR, "tmpfs", p, "tmpfs", MS_STRICTATIME, options); |
| 1153 |
} |
| 1154 |
|
| 1155 |
static int setup_volatile_yes(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) { |
| 1156 |
bool tmpfs_mounted = false, bind_mounted = false; |
| 1157 |
_cleanup_(rmdir_and_freep) char *template = NULL; |
| 1158 |
_cleanup_free_ char *buf = NULL, *bindir = NULL, *f = NULL, *t = NULL; |
| 1159 |
struct stat st; |
| 1160 |
int r; |
| 1161 |
|
| 1162 |
assert(directory); |
| 1163 |
|
| 1164 |
/* --volatile=yes means we mount a tmpfs to the root dir, and the original /usr to use inside it, and |
| 1165 |
* that read-only. Before we start setting this up let's validate if the image has the /usr merge |
| 1166 |
* implemented, and let's output a friendly log message if it hasn't. */ |
| 1167 |
|
| 1168 |
bindir = path_join(directory, "/bin"); |
| 1169 |
if (!bindir) |
| 1170 |
return log_oom(); |
| 1171 |
if (lstat(bindir, &st) < 0) { |
| 1172 |
if (errno != ENOENT) |
| 1173 |
return log_error_errno(errno, "Failed to stat /bin directory below image: %m"); |
| 1174 |
|
| 1175 |
/* ENOENT is fine, just means the image is probably just a naked /usr and we can create the |
| 1176 |
* rest. */ |
| 1177 |
} else if (S_ISDIR(st.st_mode)) |
| 1178 |
return log_error_errno(SYNTHETIC_ERRNO(EISDIR), |
| 1179 |
"Sorry, --volatile=yes mode is not supported with OS images that have not merged /bin/, /sbin/, /lib/, /lib64/ into /usr/. " |
| 1180 |
"Please work with your distribution and help them adopt the merged /usr scheme."); |
| 1181 |
else if (!S_ISLNK(st.st_mode)) |
| 1182 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1183 |
"Error starting image: if --volatile=yes is used /bin must be a symlink (for merged /usr support) or non-existent (in which case a symlink is created automatically)."); |
| 1184 |
|
| 1185 |
r = mkdtemp_malloc("/tmp/nspawn-volatile-XXXXXX", &template); |
| 1186 |
if (r < 0) |
| 1187 |
return log_error_errno(r, "Failed to create temporary directory: %m"); |
| 1188 |
|
| 1189 |
const char *options = "mode=0755" TMPFS_LIMITS_ROOTFS; |
| 1190 |
r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf); |
| 1191 |
if (r < 0) |
| 1192 |
goto fail; |
| 1193 |
if (r > 0) |
| 1194 |
options = buf; |
| 1195 |
|
| 1196 |
r = mount_nofollow_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options); |
| 1197 |
if (r < 0) |
| 1198 |
goto fail; |
| 1199 |
|
| 1200 |
tmpfs_mounted = true; |
| 1201 |
|
| 1202 |
f = path_join(directory, "/usr"); |
| 1203 |
if (!f) { |
| 1204 |
r = log_oom(); |
| 1205 |
goto fail; |
| 1206 |
} |
| 1207 |
|
| 1208 |
t = path_join(template, "/usr"); |
| 1209 |
if (!t) { |
| 1210 |
r = log_oom(); |
| 1211 |
goto fail; |
| 1212 |
} |
| 1213 |
|
| 1214 |
r = mkdir(t, 0755); |
| 1215 |
if (r < 0 && errno != EEXIST) { |
| 1216 |
r = log_error_errno(errno, "Failed to create %s: %m", t); |
| 1217 |
goto fail; |
| 1218 |
} |
| 1219 |
|
| 1220 |
r = mount_nofollow_verbose(LOG_ERR, f, t, NULL, MS_BIND|MS_REC, NULL); |
| 1221 |
if (r < 0) |
| 1222 |
goto fail; |
| 1223 |
|
| 1224 |
bind_mounted = true; |
| 1225 |
|
| 1226 |
r = bind_remount_recursive(t, MS_RDONLY, MS_RDONLY, NULL); |
| 1227 |
if (r < 0) { |
| 1228 |
log_error_errno(r, "Failed to remount %s read-only: %m", t); |
| 1229 |
goto fail; |
| 1230 |
} |
| 1231 |
|
| 1232 |
r = mount_nofollow_verbose(LOG_ERR, template, directory, NULL, MS_MOVE, NULL); |
| 1233 |
if (r < 0) |
| 1234 |
goto fail; |
| 1235 |
|
| 1236 |
(void) rmdir(template); |
| 1237 |
|
| 1238 |
return 0; |
| 1239 |
|
| 1240 |
fail: |
| 1241 |
if (bind_mounted) |
| 1242 |
(void) umount_verbose(LOG_ERR, t, UMOUNT_NOFOLLOW); |
| 1243 |
|
| 1244 |
if (tmpfs_mounted) |
| 1245 |
(void) umount_verbose(LOG_ERR, template, UMOUNT_NOFOLLOW); |
| 1246 |
|
| 1247 |
return r; |
| 1248 |
} |
| 1249 |
|
| 1250 |
static int setup_volatile_overlay(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) { |
| 1251 |
_cleanup_free_ char *buf = NULL, *escaped_directory = NULL, *escaped_upper = NULL, *escaped_work = NULL; |
| 1252 |
_cleanup_(rmdir_and_freep) char *template = NULL; |
| 1253 |
const char *upper, *work, *options; |
| 1254 |
bool tmpfs_mounted = false; |
| 1255 |
int r; |
| 1256 |
|
| 1257 |
assert(directory); |
| 1258 |
|
| 1259 |
/* --volatile=overlay means we mount an overlayfs to the root dir. */ |
| 1260 |
|
| 1261 |
r = mkdtemp_malloc("/tmp/nspawn-volatile-XXXXXX", &template); |
| 1262 |
if (r < 0) |
| 1263 |
return log_error_errno(r, "Failed to create temporary directory: %m"); |
| 1264 |
|
| 1265 |
options = "mode=0755" TMPFS_LIMITS_ROOTFS; |
| 1266 |
r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf); |
| 1267 |
if (r < 0) |
| 1268 |
goto finish; |
| 1269 |
if (r > 0) |
| 1270 |
options = buf; |
| 1271 |
|
| 1272 |
r = mount_nofollow_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options); |
| 1273 |
if (r < 0) |
| 1274 |
goto finish; |
| 1275 |
|
| 1276 |
tmpfs_mounted = true; |
| 1277 |
|
| 1278 |
upper = strjoina(template, "/upper"); |
| 1279 |
work = strjoina(template, "/work"); |
| 1280 |
|
| 1281 |
if (mkdir(upper, 0755) < 0) { |
| 1282 |
r = log_error_errno(errno, "Failed to create %s: %m", upper); |
| 1283 |
goto finish; |
| 1284 |
} |
| 1285 |
if (mkdir(work, 0755) < 0) { |
| 1286 |
r = log_error_errno(errno, "Failed to create %s: %m", work); |
| 1287 |
goto finish; |
| 1288 |
} |
| 1289 |
|
| 1290 |
/* And now, let's overmount the root dir with an overlayfs that uses the root dir as lower dir. It's kinda nice |
| 1291 |
* that the kernel allows us to do that without going through some mount point rearrangements. */ |
| 1292 |
|
| 1293 |
escaped_directory = shell_escape(directory, ",:"); |
| 1294 |
escaped_upper = shell_escape(upper, ",:"); |
| 1295 |
escaped_work = shell_escape(work, ",:"); |
| 1296 |
if (!escaped_directory || !escaped_upper || !escaped_work) { |
| 1297 |
r = -ENOMEM; |
| 1298 |
goto finish; |
| 1299 |
} |
| 1300 |
|
| 1301 |
options = strjoina("lowerdir=", escaped_directory, ",upperdir=", escaped_upper, ",workdir=", escaped_work); |
| 1302 |
r = mount_nofollow_verbose(LOG_ERR, "overlay", directory, "overlay", 0, options); |
| 1303 |
|
| 1304 |
finish: |
| 1305 |
if (tmpfs_mounted) |
| 1306 |
(void) umount_verbose(LOG_ERR, template, UMOUNT_NOFOLLOW); |
| 1307 |
|
| 1308 |
return r; |
| 1309 |
} |
| 1310 |
|
| 1311 |
int setup_volatile_mode( |
| 1312 |
const char *directory, |
| 1313 |
VolatileMode mode, |
| 1314 |
uid_t uid_shift, |
| 1315 |
const char *selinux_apifs_context) { |
| 1316 |
|
| 1317 |
switch (mode) { |
| 1318 |
|
| 1319 |
case VOLATILE_YES: |
| 1320 |
return setup_volatile_yes(directory, uid_shift, selinux_apifs_context); |
| 1321 |
|
| 1322 |
case VOLATILE_STATE: |
| 1323 |
return setup_volatile_state(directory); |
| 1324 |
|
| 1325 |
case VOLATILE_OVERLAY: |
| 1326 |
return setup_volatile_overlay(directory, uid_shift, selinux_apifs_context); |
| 1327 |
|
| 1328 |
default: |
| 1329 |
return 0; |
| 1330 |
} |
| 1331 |
} |
| 1332 |
|
| 1333 |
int setup_volatile_mode_after_remount_idmap( |
| 1334 |
const char *directory, |
| 1335 |
VolatileMode mode, |
| 1336 |
uid_t uid_shift, |
| 1337 |
const char *selinux_apifs_context) { |
| 1338 |
|
| 1339 |
switch (mode) { |
| 1340 |
|
| 1341 |
case VOLATILE_STATE: |
| 1342 |
return setup_volatile_state_after_remount_idmap(directory, uid_shift, selinux_apifs_context); |
| 1343 |
|
| 1344 |
default: |
| 1345 |
return 0; |
| 1346 |
} |
| 1347 |
} |
| 1348 |
|
| 1349 |
/* Expects *pivot_root_new and *pivot_root_old to be initialised to allocated memory or NULL. */ |
| 1350 |
int pivot_root_parse(char **pivot_root_new, char **pivot_root_old, const char *s) { |
| 1351 |
_cleanup_free_ char *root_new = NULL, *root_old = NULL; |
| 1352 |
const char *p = s; |
| 1353 |
int r; |
| 1354 |
|
| 1355 |
assert(pivot_root_new); |
| 1356 |
assert(pivot_root_old); |
| 1357 |
|
| 1358 |
r = extract_first_word(&p, &root_new, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 1359 |
if (r < 0) |
| 1360 |
return r; |
| 1361 |
if (r == 0) |
| 1362 |
return -EINVAL; |
| 1363 |
|
| 1364 |
if (isempty(p)) |
| 1365 |
root_old = NULL; |
| 1366 |
else { |
| 1367 |
root_old = strdup(p); |
| 1368 |
if (!root_old) |
| 1369 |
return -ENOMEM; |
| 1370 |
} |
| 1371 |
|
| 1372 |
if (!path_is_absolute(root_new)) |
| 1373 |
return -EINVAL; |
| 1374 |
if (!path_is_normalized(root_new)) |
| 1375 |
return -EINVAL; |
| 1376 |
if (root_old && (!path_is_absolute(root_old) || !path_is_normalized(root_old))) |
| 1377 |
return -EINVAL; |
| 1378 |
|
| 1379 |
free_and_replace(*pivot_root_new, root_new); |
| 1380 |
free_and_replace(*pivot_root_old, root_old); |
| 1381 |
|
| 1382 |
return 0; |
| 1383 |
} |
| 1384 |
|
| 1385 |
int setup_pivot_root(const char *directory, const char *pivot_root_new, const char *pivot_root_old) { |
| 1386 |
_cleanup_free_ char *directory_pivot_root_new = NULL; |
| 1387 |
_cleanup_free_ char *pivot_tmp_pivot_root_old = NULL; |
| 1388 |
_cleanup_(rmdir_and_freep) char *pivot_tmp = NULL; |
| 1389 |
int r; |
| 1390 |
|
| 1391 |
assert(directory); |
| 1392 |
|
| 1393 |
if (!pivot_root_new) |
| 1394 |
return 0; |
| 1395 |
|
| 1396 |
/* Pivot pivot_root_new to / and the existing / to pivot_root_old. |
| 1397 |
* If pivot_root_old is NULL, the existing / disappears. |
| 1398 |
* This requires a temporary directory, pivot_tmp, which is |
| 1399 |
* not a child of either. |
| 1400 |
* |
| 1401 |
* This is typically used for OSTree-style containers, where the root partition contains several |
| 1402 |
* sysroots which could be run. Normally, one would be chosen by the bootloader and pivoted to / by |
| 1403 |
* initrd. |
| 1404 |
* |
| 1405 |
* For example, for an OSTree deployment, pivot_root_new |
| 1406 |
* would be: /ostree/deploy/$os/deploy/$checksum. Note that this |
| 1407 |
* code doesn’t do the /var mount which OSTree expects: use |
| 1408 |
* --bind +/sysroot/ostree/deploy/$os/var:/var for that. |
| 1409 |
* |
| 1410 |
* So in the OSTree case, we’ll end up with something like: |
| 1411 |
* - directory = /tmp/nspawn-root-123456 |
| 1412 |
* - pivot_root_new = /ostree/deploy/os/deploy/123abc |
| 1413 |
* - pivot_root_old = /sysroot |
| 1414 |
* - directory_pivot_root_new = |
| 1415 |
* /tmp/nspawn-root-123456/ostree/deploy/os/deploy/123abc |
| 1416 |
* - pivot_tmp = /tmp/nspawn-pivot-123456 |
| 1417 |
* - pivot_tmp_pivot_root_old = /tmp/nspawn-pivot-123456/sysroot |
| 1418 |
* |
| 1419 |
* Requires all file systems at directory and below to be mounted |
| 1420 |
* MS_PRIVATE or MS_SLAVE so they can be moved. |
| 1421 |
*/ |
| 1422 |
directory_pivot_root_new = path_join(directory, pivot_root_new); |
| 1423 |
if (!directory_pivot_root_new) |
| 1424 |
return log_oom(); |
| 1425 |
|
| 1426 |
/* Remount directory_pivot_root_new to make it movable. */ |
| 1427 |
r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, directory_pivot_root_new, NULL, MS_BIND, NULL); |
| 1428 |
if (r < 0) |
| 1429 |
return r; |
| 1430 |
|
| 1431 |
if (pivot_root_old) { |
| 1432 |
r = mkdtemp_malloc("/tmp/nspawn-pivot-XXXXXX", &pivot_tmp); |
| 1433 |
if (r < 0) |
| 1434 |
return log_error_errno(r, "Failed to create temporary directory: %m"); |
| 1435 |
|
| 1436 |
pivot_tmp_pivot_root_old = path_join(pivot_tmp, pivot_root_old); |
| 1437 |
if (!pivot_tmp_pivot_root_old) |
| 1438 |
return log_oom(); |
| 1439 |
|
| 1440 |
r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, pivot_tmp, NULL, MS_MOVE, NULL); |
| 1441 |
if (r < 0) |
| 1442 |
return r; |
| 1443 |
|
| 1444 |
r = mount_nofollow_verbose(LOG_ERR, directory, pivot_tmp_pivot_root_old, NULL, MS_MOVE, NULL); |
| 1445 |
if (r < 0) |
| 1446 |
return r; |
| 1447 |
|
| 1448 |
r = mount_nofollow_verbose(LOG_ERR, pivot_tmp, directory, NULL, MS_MOVE, NULL); |
| 1449 |
} else |
| 1450 |
r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, directory, NULL, MS_MOVE, NULL); |
| 1451 |
|
| 1452 |
if (r < 0) |
| 1453 |
return r; |
| 1454 |
|
| 1455 |
return 0; |
| 1456 |
} |
| 1457 |
|
| 1458 |
#define NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS "/run/host/proc" |
| 1459 |
#define NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS "/run/host/sys" |
| 1460 |
|
| 1461 |
int pin_fully_visible_api_fs(void) { |
| 1462 |
int r; |
| 1463 |
|
| 1464 |
log_debug("Pinning fully visible API FS"); |
| 1465 |
|
| 1466 |
(void) mkdir_p(NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS, 0755); |
| 1467 |
(void) mkdir_p(NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS, 0755); |
| 1468 |
|
| 1469 |
r = mount_follow_verbose(LOG_ERR, "proc", NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS, "proc", PROC_DEFAULT_MOUNT_FLAGS, NULL); |
| 1470 |
if (r < 0) |
| 1471 |
return r; |
| 1472 |
|
| 1473 |
r = mount_follow_verbose(LOG_ERR, "sysfs", NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS, "sysfs", SYS_DEFAULT_MOUNT_FLAGS, NULL); |
| 1474 |
if (r < 0) |
| 1475 |
return r; |
| 1476 |
|
| 1477 |
return 0; |
| 1478 |
} |
| 1479 |
|
| 1480 |
static int do_wipe_fully_visible_api_fs(void) { |
| 1481 |
if (umount2(NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS, MNT_DETACH) < 0) |
| 1482 |
return log_error_errno(errno, "Failed to unmount temporary proc: %m"); |
| 1483 |
|
| 1484 |
if (rmdir(NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS) < 0) |
| 1485 |
return log_error_errno(errno, "Failed to remove temporary proc mountpoint: %m"); |
| 1486 |
|
| 1487 |
if (umount2(NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS, MNT_DETACH) < 0) |
| 1488 |
return log_error_errno(errno, "Failed to unmount temporary sys: %m"); |
| 1489 |
|
| 1490 |
if (rmdir(NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS) < 0) |
| 1491 |
return log_error_errno(errno, "Failed to remove temporary sys mountpoint: %m"); |
| 1492 |
|
| 1493 |
return 0; |
| 1494 |
} |
| 1495 |
|
| 1496 |
int wipe_fully_visible_api_fs(int mntns_fd) { |
| 1497 |
_cleanup_close_ int orig_mntns_fd = -EBADF; |
| 1498 |
int r; |
| 1499 |
|
| 1500 |
log_debug("Wiping fully visible API FS"); |
| 1501 |
|
| 1502 |
orig_mntns_fd = namespace_open_by_type(NAMESPACE_MOUNT); |
| 1503 |
if (orig_mntns_fd < 0) |
| 1504 |
return log_error_errno(orig_mntns_fd, "Failed to pin originating mount namespace: %m"); |
| 1505 |
|
| 1506 |
if (setns(mntns_fd, CLONE_NEWNS) < 0) |
| 1507 |
return log_error_errno(errno, "Failed to enter mount namespace: %m"); |
| 1508 |
|
| 1509 |
r = do_wipe_fully_visible_api_fs(); |
| 1510 |
|
| 1511 |
if (setns(orig_mntns_fd, CLONE_NEWNS) < 0) |
| 1512 |
return log_error_errno(errno, "Failed to enter original mount namespace: %m"); |
| 1513 |
|
| 1514 |
return r; |
| 1515 |
} |