Debian Codegraph @

src/nspawn/nspawn.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/loop.h>
4 #include <net/if.h>
5 #include <stdlib.h>
6 #include <sys/file.h>
7 #include <sys/ioctl.h>
8 #include <sys/keyctl.h>
9 #include <sys/mount.h>
10 #include <sys/personality.h>
11 #include <sys/prctl.h>
12 #include <sys/wait.h>
13 #include <unistd.h>
14
15 #include "sd-bus.h"
16 #include "sd-daemon.h"
17 #include "sd-event.h"
18 #include "sd-id128.h"
19 #include "sd-netlink.h"
20 #include "sd-path.h"
21 #include "sd-varlink.h"
22
23 #include "alloc-util.h"
24 #include "barrier.h"
25 #include "base-filesystem.h"
26 #include "btrfs-util.h"
27 #include "build.h"
28 #include "bus-error.h"
29 #include "bus-locator.h"
30 #include "bus-util.h"
31 #include "capability-list.h"
32 #include "capability-util.h"
33 #include "cgroup-setup.h"
34 #include "chase.h"
35 #include "common-signal.h"
36 #include "constants.h"
37 #include "copy.h"
38 #include "cpu-set-util.h"
39 #include "daemon-util.h"
40 #include "dev-setup.h"
41 #include "devnum-util.h"
42 #include "discover-image.h"
43 #include "dissect-image.h"
44 #include "dlfcn-util.h"
45 #include "env-util.h"
46 #include "escape.h"
47 #include "ether-addr-util.h"
48 #include "extract-word.h"
49 #include "fd-util.h"
50 #include "fdset.h"
51 #include "fileio.h"
52 #include "fork-notify.h"
53 #include "format-table.h"
54 #include "format-util.h"
55 #include "fs-util.h"
56 #include "gpt.h"
57 #include "hexdecoct.h"
58 #include "hostname-setup.h"
59 #include "hostname-util.h"
60 #include "id128-util.h"
61 #include "image-policy.h"
62 #include "in-addr-util.h"
63 #include "io-util.h"
64 #include "libmount-util.h"
65 #include "log.h"
66 #include "loop-util.h"
67 #include "loopback-setup.h"
68 #include "machine-bind-user.h"
69 #include "machine-credential.h"
70 #include "machine-register.h"
71 #include "main-func.h"
72 #include "mkdir.h"
73 #include "mount-util.h"
74 #include "mountpoint-util.h"
75 #include "mstack.h"
76 #include "namespace-util.h"
77 #include "netlink-internal.h"
78 #include "notify-recv.h"
79 #include "nspawn-bind-user.h"
80 #include "nspawn-cgroup.h"
81 #include "nspawn-expose-ports.h"
82 #include "nspawn-mount.h"
83 #include "nspawn-network.h"
84 #include "nspawn-oci.h"
85 #include "nspawn-register.h"
86 #include "nspawn-seccomp.h"
87 #include "nspawn-settings.h"
88 #include "nspawn-setuid.h"
89 #include "nspawn-stub-pid1.h"
90 #include "nspawn.h"
91 #include "nsresource.h"
92 #include "os-util.h"
93 #include "parse-helpers.h"
94 #include "osc-context.h"
95 #include "options.h"
96 #include "pager.h"
97 #include "parse-argument.h"
98 #include "parse-util.h"
99 #include "path-lookup.h"
100 #include "path-util.h"
101 #include "pidref.h"
102 #include "polkit-agent.h"
103 #include "pretty-print.h"
104 #include "process-util.h"
105 #include "ptyfwd.h"
106 #include "random-util.h"
107 #include "raw-clone.h"
108 #include "resolve-util.h"
109 #include "rlimit-util.h"
110 #include "rm-rf.h"
111 #include "runtime-scope.h"
112 #include "seccomp-util.h"
113 #include "selinux-util.h"
114 #include "set.h"
115 #include "shift-uid.h"
116 #include "signal-util.h"
117 #include "siphash24.h"
118 #include "snapshot-util.h"
119 #include "socket-util.h"
120 #include "stat-util.h"
121 #include "stdio-util.h"
122 #include "string-table.h"
123 #include "string-util.h"
124 #include "strv.h"
125 #include "sysctl-util.h"
126 #include "terminal-util.h"
127 #include "tmpfile-util.h"
128 #include "uid-classification.h"
129 #include "umask-util.h"
130 #include "unit-name.h"
131 #include "user-record.h"
132 #include "user-util.h"
133 #include "vpick.h"
134
135 /* The notify socket inside the container it can use to talk to nspawn using the sd_notify(3) protocol */
136 #define NSPAWN_NOTIFY_SOCKET_PATH "/run/host/notify"
137 #define NSPAWN_MOUNT_TUNNEL "/run/host/incoming"
138 #define NSPAWN_JOURNAL_SOCKET_PATH "/run/host/journal/socket"
139
140 #define EXIT_FORCE_RESTART 133
141
142 typedef enum ContainerStatus {
143 CONTAINER_TERMINATED,
144 CONTAINER_REBOOTED,
145 } ContainerStatus;
146
147 static char *arg_directory = NULL;
148 static char *arg_template = NULL;
149 static char *arg_chdir = NULL;
150 static char *arg_pivot_root_new = NULL;
151 static char *arg_pivot_root_old = NULL;
152 static char *arg_user = NULL;
153 static uid_t arg_uid = UID_INVALID;
154 static gid_t arg_gid = GID_INVALID;
155 static gid_t* arg_supplementary_gids = NULL;
156 static size_t arg_n_supplementary_gids = 0;
157 static sd_id128_t arg_uuid = {};
158 static char *arg_machine = NULL; /* The name used by the host to refer to this */
159 static char *arg_hostname = NULL; /* The name the payload sees by default */
160 static const char *arg_selinux_context = NULL;
161 static const char *arg_selinux_apifs_context = NULL;
162 static char *arg_slice = NULL;
163 static bool arg_private_network = false;
164 static bool arg_read_only = false;
165 static StartMode arg_start_mode = START_PID1;
166 static bool arg_ephemeral = false;
167 static LinkJournal arg_link_journal = LINK_AUTO;
168 static bool arg_link_journal_try = false;
169 static uint64_t arg_caps_retain =
170 (1ULL << CAP_AUDIT_CONTROL) |
171 (1ULL << CAP_AUDIT_WRITE) |
172 (1ULL << CAP_CHOWN) |
173 (1ULL << CAP_DAC_OVERRIDE) |
174 (1ULL << CAP_DAC_READ_SEARCH) |
175 (1ULL << CAP_FOWNER) |
176 (1ULL << CAP_FSETID) |
177 (1ULL << CAP_IPC_OWNER) |
178 (1ULL << CAP_KILL) |
179 (1ULL << CAP_LEASE) |
180 (1ULL << CAP_LINUX_IMMUTABLE) |
181 (1ULL << CAP_MKNOD) |
182 (1ULL << CAP_NET_BIND_SERVICE) |
183 (1ULL << CAP_NET_BROADCAST) |
184 (1ULL << CAP_NET_RAW) |
185 (1ULL << CAP_SETFCAP) |
186 (1ULL << CAP_SETGID) |
187 (1ULL << CAP_SETPCAP) |
188 (1ULL << CAP_SETUID) |
189 (1ULL << CAP_SYS_ADMIN) |
190 (1ULL << CAP_SYS_BOOT) |
191 (1ULL << CAP_SYS_CHROOT) |
192 (1ULL << CAP_SYS_NICE) |
193 (1ULL << CAP_SYS_PTRACE) |
194 (1ULL << CAP_SYS_RESOURCE) |
195 (1ULL << CAP_SYS_TTY_CONFIG);
196 static uint64_t arg_caps_ambient = 0;
197 static CapabilityQuintet arg_full_capabilities = CAPABILITY_QUINTET_NULL;
198 static CustomMount *arg_custom_mounts = NULL;
199 static size_t arg_n_custom_mounts = 0;
200 static char **arg_setenv = NULL;
201 static bool arg_quiet = false;
202 static int arg_register = -1;
203 static bool arg_keep_unit = false;
204 static char **arg_network_interfaces = NULL;
205 static char **arg_network_macvlan = NULL;
206 static char **arg_network_ipvlan = NULL;
207 static bool arg_network_veth = false;
208 static char **arg_network_veth_extra = NULL;
209 static char *arg_network_bridge = NULL;
210 static char *arg_network_zone = NULL;
211 static char *arg_network_namespace_path = NULL;
212 static struct ether_addr arg_network_provided_mac = {};
213 static PagerFlags arg_pager_flags = 0;
214 static unsigned long arg_personality = PERSONALITY_INVALID;
215 static char *arg_image = NULL;
216 static char *arg_mstack = NULL;
217 static char *arg_oci_bundle = NULL;
218 static VolatileMode arg_volatile_mode = VOLATILE_NO;
219 static ExposePort *arg_expose_ports = NULL;
220 static char **arg_property = NULL;
221 static sd_bus_message *arg_property_message = NULL;
222 static UserNamespaceMode arg_userns_mode = _USER_NAMESPACE_MODE_INVALID;
223 static uid_t arg_uid_shift = UID_INVALID, arg_uid_range = 0x10000U;
224 static unsigned arg_delegate_container_ranges = 0;
225 static UserNamespaceOwnership arg_userns_ownership = _USER_NAMESPACE_OWNERSHIP_INVALID;
226 static int arg_kill_signal = 0;
227 static SettingsMask arg_settings_mask = 0;
228 static int arg_settings_trusted = -1;
229 static char **arg_parameters = NULL;
230 static const char *arg_container_service_name = "systemd-nspawn";
231 static bool arg_notify_ready = false;
232 static bool arg_use_cgns = true;
233 static unsigned long arg_clone_ns_flags = CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS;
234 static MountSettingsMask arg_mount_settings = MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_TMPFS_TMP;
235 static VeritySettings arg_verity_settings = VERITY_SETTINGS_DEFAULT;
236 static char **arg_syscall_allow_list = NULL;
237 static char **arg_syscall_deny_list = NULL;
238 #if HAVE_SECCOMP
239 static scmp_filter_ctx arg_seccomp = NULL;
240 #endif
241 static struct rlimit *arg_rlimit[_RLIMIT_MAX] = {};
242 static bool arg_no_new_privileges = false;
243 static int arg_oom_score_adjust = 0;
244 static bool arg_oom_score_adjust_set = false;
245 static CPUSet arg_cpu_set = {};
246 static ResolvConfMode arg_resolv_conf = RESOLV_CONF_AUTO;
247 static TimezoneMode arg_timezone = TIMEZONE_AUTO;
248 static unsigned arg_console_width = UINT_MAX, arg_console_height = UINT_MAX;
249 static DeviceNode* arg_extra_nodes = NULL;
250 static size_t arg_n_extra_nodes = 0;
251 static char **arg_sysctl = NULL;
252 static ConsoleMode arg_console_mode = _CONSOLE_MODE_INVALID;
253 static MachineCredentialContext arg_credentials = {};
254 static char **arg_bind_user = NULL;
255 static char *arg_bind_user_shell = NULL;
256 static bool arg_bind_user_shell_copy = false;
257 static char **arg_bind_user_groups = NULL;
258 static bool arg_suppress_sync = false;
259 static Set *arg_restrict_address_families = NULL;
260 static bool arg_restrict_address_families_is_allowlist = false;
261 static char *arg_settings_filename = NULL;
262 static Architecture arg_architecture = _ARCHITECTURE_INVALID;
263 static ImagePolicy *arg_image_policy = NULL;
264 static char *arg_background = NULL;
265 static RuntimeScope arg_runtime_scope = _RUNTIME_SCOPE_INVALID;
266 static bool arg_cleanup = false;
267 static bool arg_ask_password = true;
268 static char *arg_forward_journal = NULL;
269 static uint64_t arg_forward_journal_max_use = UINT64_MAX;
270 static uint64_t arg_forward_journal_keep_free = UINT64_MAX;
271 static uint64_t arg_forward_journal_max_file_size = UINT64_MAX;
272 static uint64_t arg_forward_journal_max_files = UINT64_MAX;
273
274 STATIC_DESTRUCTOR_REGISTER(arg_directory, freep);
275 STATIC_DESTRUCTOR_REGISTER(arg_template, freep);
276 STATIC_DESTRUCTOR_REGISTER(arg_chdir, freep);
277 STATIC_DESTRUCTOR_REGISTER(arg_pivot_root_new, freep);
278 STATIC_DESTRUCTOR_REGISTER(arg_pivot_root_old, freep);
279 STATIC_DESTRUCTOR_REGISTER(arg_user, freep);
280 STATIC_DESTRUCTOR_REGISTER(arg_supplementary_gids, freep);
281 STATIC_DESTRUCTOR_REGISTER(arg_machine, freep);
282 STATIC_DESTRUCTOR_REGISTER(arg_hostname, freep);
283 STATIC_DESTRUCTOR_REGISTER(arg_slice, freep);
284 STATIC_DESTRUCTOR_REGISTER(arg_setenv, strv_freep);
285 STATIC_DESTRUCTOR_REGISTER(arg_network_interfaces, strv_freep);
286 STATIC_DESTRUCTOR_REGISTER(arg_network_macvlan, strv_freep);
287 STATIC_DESTRUCTOR_REGISTER(arg_network_ipvlan, strv_freep);
288 STATIC_DESTRUCTOR_REGISTER(arg_network_veth_extra, strv_freep);
289 STATIC_DESTRUCTOR_REGISTER(arg_network_bridge, freep);
290 STATIC_DESTRUCTOR_REGISTER(arg_network_zone, freep);
291 STATIC_DESTRUCTOR_REGISTER(arg_network_namespace_path, freep);
292 STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
293 STATIC_DESTRUCTOR_REGISTER(arg_mstack, freep);
294 STATIC_DESTRUCTOR_REGISTER(arg_oci_bundle, freep);
295 STATIC_DESTRUCTOR_REGISTER(arg_property, strv_freep);
296 STATIC_DESTRUCTOR_REGISTER(arg_property_message, sd_bus_message_unrefp);
297 STATIC_DESTRUCTOR_REGISTER(arg_parameters, strv_freep);
298 STATIC_DESTRUCTOR_REGISTER(arg_verity_settings, verity_settings_done);
299 STATIC_DESTRUCTOR_REGISTER(arg_syscall_allow_list, strv_freep);
300 STATIC_DESTRUCTOR_REGISTER(arg_syscall_deny_list, strv_freep);
301 #if HAVE_SECCOMP
302 STATIC_DESTRUCTOR_REGISTER(arg_seccomp, seccomp_releasep);
303 #endif
304 STATIC_DESTRUCTOR_REGISTER(arg_credentials, machine_credential_context_done);
305 STATIC_DESTRUCTOR_REGISTER(arg_cpu_set, cpu_set_done);
306 STATIC_DESTRUCTOR_REGISTER(arg_sysctl, strv_freep);
307 STATIC_DESTRUCTOR_REGISTER(arg_bind_user, strv_freep);
308 STATIC_DESTRUCTOR_REGISTER(arg_bind_user_shell, freep);
309 STATIC_DESTRUCTOR_REGISTER(arg_bind_user_groups, strv_freep);
310 STATIC_DESTRUCTOR_REGISTER(arg_restrict_address_families, set_freep);
311 STATIC_DESTRUCTOR_REGISTER(arg_settings_filename, freep);
312 STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
313 STATIC_DESTRUCTOR_REGISTER(arg_background, freep);
314 STATIC_DESTRUCTOR_REGISTER(arg_forward_journal, freep);
315
316 static int parse_private_users(
317 const char *s,
318 UserNamespaceMode *ret_userns_mode,
319 uid_t *ret_uid_shift,
320 uid_t *ret_uid_range) {
321
322 int boolean, r;
323
324 assert(ret_userns_mode);
325 assert(ret_uid_shift);
326 assert(ret_uid_range);
327
328 if (!s)
329 boolean = true;
330 else if (!in_charset(s, DIGITS))
331 /* do *not* parse numbers as booleans */
332 boolean = parse_boolean(s);
333 else
334 boolean = -1;
335
336 if (boolean == 0) {
337 /* no: User namespacing off */
338 *ret_userns_mode = USER_NAMESPACE_NO;
339 *ret_uid_shift = UID_INVALID;
340 *ret_uid_range = UINT32_C(0x10000);
341
342 } else if (boolean > 0) {
343 /* yes: User namespacing on, UID range is read from root dir */
344 *ret_userns_mode = USER_NAMESPACE_FIXED;
345 *ret_uid_shift = UID_INVALID;
346 *ret_uid_range = UINT32_C(0x10000);
347
348 } else if (streq(s, "pick")) {
349 /* pick: User namespacing on, UID range is picked randomly */
350 *ret_userns_mode = USER_NAMESPACE_PICK; /* Note that arg_userns_ownership is
351 * implied by USER_NAMESPACE_PICK
352 * further down. */
353 *ret_uid_shift = UID_INVALID;
354 *ret_uid_range = UINT32_C(0x10000);
355
356 } else if (streq(s, "identity")) {
357 /* identity: User namespaces on, UID range is map of the 0…0xFFFF range to
358 * itself, i.e. we don't actually map anything, but do take benefit of
359 * isolation of capability sets. */
360 *ret_userns_mode = USER_NAMESPACE_FIXED;
361 *ret_uid_shift = 0;
362 *ret_uid_range = UINT32_C(0x10000);
363
364 } else if (streq(s, "managed")) {
365 /* managed: User namespace on, and acquire it from systemd-nsresourced */
366 *ret_userns_mode = USER_NAMESPACE_MANAGED;
367 *ret_uid_shift = UID_INVALID;
368 *ret_uid_range = UINT32_C(0x10000);
369
370 } else {
371 /* anything else: User namespacing on, UID range is explicitly configured */
372 r = parse_userns_uid_range(s, ret_uid_shift, ret_uid_range);
373 if (r < 0)
374 return r;
375 *ret_userns_mode = USER_NAMESPACE_FIXED;
376 }
377
378 return 0;
379 }
380
381 static int help(void) {
382 _cleanup_free_ char *link = NULL;
383 int r;
384
385 pager_open(arg_pager_flags);
386
387 r = terminal_urlify_man("systemd-nspawn", "1", &link);
388 if (r < 0)
389 return log_oom();
390
391 static const char* const groups[] = {
392 NULL,
393 "Image",
394 "Execution",
395 "System Identity",
396 "Properties",
397 "User Namespacing",
398 "Networking",
399 "Security",
400 "Resources",
401 "Integration",
402 "Mounts",
403 "Input/Output",
404 "Credentials",
405 "Other",
406 };
407
408 Table* tables[ELEMENTSOF(groups)] = {};
409 CLEANUP_ELEMENTS(tables, table_unref_array_clear);
410
411 for (size_t i = 0; i < ELEMENTSOF(groups); i++) {
412 r = option_parser_get_help_table_group(groups[i], &tables[i]);
413 if (r < 0)
414 return r;
415 }
416
417 (void) table_sync_column_widths(0, tables[0], tables[1], tables[2], tables[3],
418 tables[4], tables[5], tables[6], tables[7],
419 tables[8], tables[9], tables[10], tables[11],
420 tables[12], tables[13]);
421
422 printf("%s [OPTIONS...] [PATH] [ARGUMENTS...]\n\n"
423 "%sSpawn a command or OS in a lightweight container.%s\n\n",
424 program_invocation_short_name,
425 ansi_highlight(),
426 ansi_normal());
427
428 r = table_print_or_warn(tables[0]);
429 if (r < 0)
430 return r;
431
432 for (size_t i = 1; i < ELEMENTSOF(groups); i++) {
433 printf("\n%s%s:%s\n", ansi_underline(), groups[i], ansi_normal());
434
435 r = table_print_or_warn(tables[i]);
436 if (r < 0)
437 return r;
438 }
439
440 printf("\nSee the %s for details.\n", link);
441 return 0;
442 }
443
444 static int custom_mount_check_all(void) {
445 FOREACH_ARRAY(m, arg_custom_mounts, arg_n_custom_mounts)
446 if (path_equal(m->destination, "/") && arg_userns_mode != USER_NAMESPACE_NO) {
447 if (arg_userns_ownership != USER_NAMESPACE_OWNERSHIP_OFF)
448 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
449 "--private-users-ownership=%s may not be combined with custom root mounts.",
450 user_namespace_ownership_to_string(arg_userns_ownership));
451 if (arg_uid_shift == UID_INVALID)
452 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
453 "--private-users with automatic UID shift may not be combined with custom root mounts.");
454 }
455
456 return 0;
457 }
458
459 static int parse_capability_spec(const char *spec, uint64_t *ret_mask) {
460 uint64_t mask = 0;
461 int r;
462
463 assert(ret_mask);
464
465 for (;;) {
466 _cleanup_free_ char *t = NULL;
467
468 r = extract_first_word(&spec, &t, ",", 0);
469 if (r < 0)
470 return log_error_errno(r, "Failed to parse capability %s.", t);
471 if (r == 0)
472 break;
473
474 if (streq(t, "help")) {
475 for (unsigned i = 0; i < capability_list_length(); i++) {
476 const char *name;
477
478 name = capability_to_name(i);
479 if (name)
480 puts(name);
481 }
482
483 return 0; /* quit */
484 }
485
486 if (streq(t, "all"))
487 mask = UINT64_MAX;
488 else {
489 r = capability_from_name(t);
490 if (r < 0)
491 return log_error_errno(r, "Failed to parse capability %s.", t);
492
493 mask |= 1ULL << r;
494 }
495 }
496
497 *ret_mask = mask;
498 return 1; /* continue */
499 }
500
501 static int parse_share_ns_env(const char *name, unsigned long ns_flag) {
502 int r;
503
504 r = getenv_bool(name);
505 if (r == -ENXIO)
506 return 0;
507 if (r < 0)
508 return log_error_errno(r, "Failed to parse $%s: %m", name);
509
510 arg_clone_ns_flags = (arg_clone_ns_flags & ~ns_flag) | (r > 0 ? 0 : ns_flag);
511 arg_settings_mask |= SETTING_CLONE_NS_FLAGS;
512 return 0;
513 }
514
515 static int parse_mount_settings_env(void) {
516 const char *e;
517 int r;
518
519 r = getenv_bool("SYSTEMD_NSPAWN_TMPFS_TMP");
520 if (r < 0 && r != -ENXIO)
521 return log_error_errno(r, "Failed to parse $SYSTEMD_NSPAWN_TMPFS_TMP: %m");
522 if (r >= 0)
523 SET_FLAG(arg_mount_settings, MOUNT_APPLY_TMPFS_TMP, r > 0);
524
525 e = getenv("SYSTEMD_NSPAWN_API_VFS_WRITABLE");
526 if (streq_ptr(e, "network"))
527 arg_mount_settings |= MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_APIVFS_NETNS;
528 else if (e) {
529 r = parse_boolean(e);
530 if (r < 0)
531 return log_error_errno(r, "Failed to parse $SYSTEMD_NSPAWN_API_VFS_WRITABLE: %m");
532
533 SET_FLAG(arg_mount_settings, MOUNT_APPLY_APIVFS_RO, r == 0);
534 SET_FLAG(arg_mount_settings, MOUNT_APPLY_APIVFS_NETNS, false);
535 }
536
537 return 0;
538 }
539
540 static int parse_environment(void) {
541 const char *e;
542 int r;
543
544 r = parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_IPC", CLONE_NEWIPC);
545 if (r < 0)
546 return r;
547 r = parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_PID", CLONE_NEWPID);
548 if (r < 0)
549 return r;
550 r = parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_UTS", CLONE_NEWUTS);
551 if (r < 0)
552 return r;
553 r = parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_SYSTEM", CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS);
554 if (r < 0)
555 return r;
556
557 r = parse_mount_settings_env();
558 if (r < 0)
559 return r;
560
561 /* SYSTEMD_NSPAWN_USE_CGNS=0 can be used to disable CLONE_NEWCGROUP use,
562 * even if it is supported. If not supported, it has no effect. */
563 if (!namespace_type_supported(NAMESPACE_CGROUP))
564 arg_use_cgns = false;
565 else {
566 r = getenv_bool("SYSTEMD_NSPAWN_USE_CGNS");
567 if (r < 0) {
568 if (r != -ENXIO)
569 return log_error_errno(r, "Failed to parse $SYSTEMD_NSPAWN_USE_CGNS: %m");
570
571 arg_use_cgns = true;
572 } else {
573 arg_use_cgns = r > 0;
574 arg_settings_mask |= SETTING_USE_CGNS;
575 }
576 }
577
578 e = getenv("SYSTEMD_NSPAWN_CONTAINER_SERVICE");
579 if (e)
580 arg_container_service_name = e;
581
582 e = getenv("SYSTEMD_NSPAWN_NETWORK_MAC");
583 if (e) {
584 r = parse_ether_addr(e, &arg_network_provided_mac);
585 if (r < 0)
586 return log_error_errno(r, "Failed to parse provided MAC address via environment variable");
587 }
588
589 r = getenv_bool("SYSTEMD_SUPPRESS_SYNC");
590 if (r >= 0)
591 arg_suppress_sync = r;
592 else if (r != -ENXIO)
593 log_debug_errno(r, "Failed to parse $SYSTEMD_SUPPRESS_SYNC, ignoring: %m");
594
595 return 0;
596 }
597
598 static int parse_argv(int argc, char *argv[]) {
599 uint64_t plus = 0, minus = 0;
600 bool mask_all_settings = false, mask_no_settings = false;
601 int r;
602
603 assert(argc >= 0);
604 assert(argv);
605
606 OptionParser opts = { argc, argv, OPTION_PARSER_STOP_AT_FIRST_NONOPTION };
607
608 FOREACH_OPTION_OR_RETURN(c, &opts) {
609 switch (c) {
610
611 OPTION_COMMON_HELP:
612 return help();
613
614 OPTION_COMMON_VERSION:
615 return version();
616
617 OPTION('q', "quiet", NULL, "Do not show status information"):
618 arg_quiet = true;
619 break;
620
621 OPTION_COMMON_NO_PAGER:
622 arg_pager_flags |= PAGER_DISABLE;
623 break;
624
625 OPTION_LONG("settings", "BOOLEAN", "Load additional settings from .nspawn file"):
626 /* no → do not read files
627 * yes → read files, do not override cmdline, trust only subset
628 * override → read files, override cmdline, trust only subset
629 * trusted → read files, do not override cmdline, trust all
630 */
631
632 r = parse_boolean(opts.arg);
633 if (r < 0) {
634 if (streq(opts.arg, "trusted")) {
635 mask_all_settings = false;
636 mask_no_settings = false;
637 arg_settings_trusted = true;
638
639 } else if (streq(opts.arg, "override")) {
640 mask_all_settings = false;
641 mask_no_settings = true;
642 arg_settings_trusted = -1;
643 } else
644 return log_error_errno(r, "Failed to parse --settings= argument: %s", opts.arg);
645 } else if (r > 0) {
646 /* yes */
647 mask_all_settings = false;
648 mask_no_settings = false;
649 arg_settings_trusted = -1;
650 } else {
651 /* no */
652 mask_all_settings = true;
653 mask_no_settings = false;
654 arg_settings_trusted = false;
655 }
656 break;
657
658 OPTION_LONG("cleanup", NULL,
659 "Clean up left-over mounts and underlying mount points used by the container"):
660 arg_cleanup = true;
661 break;
662
663 OPTION_COMMON_NO_ASK_PASSWORD:
664 arg_ask_password = false;
665 break;
666
667 OPTION_GROUP("Image"): {}
668
669 OPTION('D', "directory", "PATH", "Root directory for the container"):
670 r = parse_path_argument(opts.arg, false, &arg_directory);
671 if (r < 0)
672 return r;
673 arg_settings_mask |= SETTING_DIRECTORY;
674 break;
675
676 OPTION_LONG("template", "PATH",
677 "Initialize root directory from template directory, if missing"):
678 r = parse_path_argument(opts.arg, false, &arg_template);
679 if (r < 0)
680 return r;
681 arg_settings_mask |= SETTING_DIRECTORY;
682 break;
683
684 OPTION('x', "ephemeral", NULL,
685 "Run container with snapshot of root directory, and remove it after exit"):
686 arg_ephemeral = true;
687 arg_settings_mask |= SETTING_EPHEMERAL;
688 break;
689
690 OPTION('i', "image", "PATH",
691 "Root file system disk image (or device node) for the container"):
692 r = parse_path_argument(opts.arg, false, &arg_image);
693 if (r < 0)
694 return r;
695 arg_settings_mask |= SETTING_DIRECTORY;
696 break;
697
698 OPTION_LONG("image-policy", "POLICY", "Specify disk image dissection policy"):
699 r = parse_image_policy_argument(opts.arg, &arg_image_policy);
700 if (r < 0)
701 return r;
702 break;
703
704 OPTION_LONG("mstack", "PATH", /* help= */ NULL):
705 r = parse_path_argument(opts.arg, false, &arg_mstack);
706 if (r < 0)
707 return r;
708 arg_settings_mask |= SETTING_DIRECTORY;
709 break;
710
711 OPTION_LONG("oci-bundle", "PATH", "OCI bundle directory"):
712 r = parse_path_argument(opts.arg, false, &arg_oci_bundle);
713 if (r < 0)
714 return r;
715 break;
716
717 OPTION_LONG("read-only", NULL, "Mount the root directory read-only"):
718 arg_read_only = true;
719 arg_settings_mask |= SETTING_READ_ONLY;
720 break;
721
722 OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "volatile", "MODE", "Run the system in volatile mode"):
723 if (!opts.arg)
724 arg_volatile_mode = VOLATILE_YES;
725 else if (streq(opts.arg, "help"))
726 return DUMP_STRING_TABLE(volatile_mode, VolatileMode, _VOLATILE_MODE_MAX);
727 else {
728 VolatileMode m;
729
730 m = volatile_mode_from_string(opts.arg);
731 if (m < 0)
732 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
733 "Failed to parse --volatile= argument: %s", opts.arg);
734 else
735 arg_volatile_mode = m;
736 }
737 arg_settings_mask |= SETTING_VOLATILE_MODE;
738 break;
739
740 OPTION_LONG("root-hash", "HASH", "Specify verity root hash for root disk image"): {
741 _cleanup_(iovec_done) struct iovec k = {};
742
743 r = unhexmem(opts.arg, &k.iov_base, &k.iov_len);
744 if (r < 0)
745 return log_error_errno(r, "Failed to parse root hash: %s", opts.arg);
746 if (k.iov_len < sizeof(sd_id128_t))
747 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Root hash must be at least 128-bit long: %s", opts.arg);
748
749 iovec_done(&arg_verity_settings.root_hash);
750 arg_verity_settings.root_hash = TAKE_STRUCT(k);
751 break;
752 }
753
754 OPTION_LONG("root-hash-sig", "SIG",
755 "Specify pkcs7 signature of root hash for verity"): {
756 _cleanup_(iovec_done) struct iovec p = {};
757 const char *value;
758
759 if ((value = startswith(opts.arg, "base64:"))) {
760 r = unbase64mem(value, &p.iov_base, &p.iov_len);
761 if (r < 0)
762 return log_error_errno(r, "Failed to parse root hash signature '%s': %m", opts.arg);
763
764 } else {
765 r = read_full_file(opts.arg, (char**) &p.iov_base, &p.iov_len);
766 if (r < 0)
767 return log_error_errno(r, "Failed to parse root hash signature file '%s': %m", opts.arg);
768 }
769
770 iovec_done(&arg_verity_settings.root_hash_sig);
771 arg_verity_settings.root_hash_sig = TAKE_STRUCT(p);
772 break;
773 }
774
775 OPTION_LONG("verity-data", "PATH", "Specify hash device for verity"):
776 r = parse_path_argument(opts.arg, false, &arg_verity_settings.data_path);
777 if (r < 0)
778 return r;
779 break;
780
781 OPTION_LONG("pivot-root", "PATH[:PATH]",
782 "Pivot root to given directory in the container"):
783 r = pivot_root_parse(&arg_pivot_root_new, &arg_pivot_root_old, opts.arg);
784 if (r < 0)
785 return log_error_errno(r, "Failed to parse --pivot-root= argument %s: %m", opts.arg);
786 arg_settings_mask |= SETTING_PIVOT_ROOT;
787 break;
788
789 OPTION_GROUP("Execution"): {}
790
791 OPTION('a', "as-pid2", NULL, "Maintain a stub init as PID1, invoke binary as PID2"):
792 if (arg_start_mode == START_BOOT)
793 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
794 "--boot and --as-pid2 may not be combined.");
795 arg_start_mode = START_PID2;
796 arg_settings_mask |= SETTING_START_MODE;
797 break;
798
799 OPTION('b', "boot", NULL, "Boot up full system (i.e. invoke init)"):
800 if (arg_start_mode == START_PID2)
801 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
802 "--boot and --as-pid2 may not be combined.");
803 arg_start_mode = START_BOOT;
804 arg_settings_mask |= SETTING_START_MODE;
805 break;
806
807 OPTION_LONG("chdir", "PATH", "Set working directory in the container"): {
808 _cleanup_free_ char *wd = NULL;
809
810 if (!path_is_absolute(opts.arg))
811 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
812 "Working directory %s is not an absolute path.", opts.arg);
813
814 r = path_simplify_alloc(opts.arg, &wd);
815 if (r < 0)
816 return log_error_errno(r, "Failed to simplify path %s: %m", opts.arg);
817
818 if (!path_is_normalized(wd))
819 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Working directory path is not normalized: %s", wd);
820
821 if (path_below_api_vfs(wd))
822 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Working directory is below API VFS, refusing: %s", wd);
823
824 free_and_replace(arg_chdir, wd);
825 arg_settings_mask |= SETTING_WORKING_DIRECTORY;
826 break;
827 }
828
829 OPTION('E', "setenv", "NAME[=VALUE]", "Pass an environment variable to PID 1"):
830 r = strv_env_replace_strdup_passthrough(&arg_setenv, opts.arg);
831 if (r < 0)
832 return log_error_errno(r, "Cannot assign environment variable %s: %m", opts.arg);
833 arg_settings_mask |= SETTING_ENVIRONMENT;
834 break;
835
836 OPTION('u', "uid", "USER", "Run the command under specified user or UID"):
837 r = free_and_strdup(&arg_user, opts.arg);
838 if (r < 0)
839 return log_oom();
840 arg_settings_mask |= SETTING_USER;
841 break;
842
843 OPTION_LONG("kill-signal", "SIGNAL", "Select signal to use for shutting down PID 1"):
844 if (streq(opts.arg, "help"))
845 return DUMP_STRING_TABLE(signal, int, _NSIG);
846
847 arg_kill_signal = signal_from_string(opts.arg);
848 if (arg_kill_signal < 0)
849 return log_error_errno(arg_kill_signal, "Cannot parse signal: %s", opts.arg);
850 arg_settings_mask |= SETTING_KILL_SIGNAL;
851 break;
852
853 OPTION_LONG("notify-ready", "BOOLEAN", "Receive notifications from the child init process"):
854 r = parse_boolean_argument("--notify-ready=", opts.arg, &arg_notify_ready);
855 if (r < 0)
856 return r;
857 arg_settings_mask |= SETTING_NOTIFY_READY;
858 break;
859
860 OPTION_LONG("suppress-sync", "BOOLEAN", "Suppress any form of disk data synchronization"):
861 r = parse_boolean_argument("--suppress-sync=", opts.arg, &arg_suppress_sync);
862 if (r < 0)
863 return r;
864 arg_settings_mask |= SETTING_SUPPRESS_SYNC;
865 break;
866
867 OPTION_GROUP("System Identity"): {}
868
869 OPTION('M', "machine", "NAME", "Set the machine name for the container"):
870 if (!isempty(opts.arg) && !hostname_is_valid(opts.arg, /* flags= */ 0))
871 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
872 "Invalid machine name: %s", opts.arg);
873 r = free_and_strdup_warn(&arg_machine, opts.arg);
874 if (r < 0)
875 return r;
876 break;
877
878 OPTION_LONG("hostname", "NAME", "Override the hostname for the container"):
879 if (!isempty(opts.arg) && !hostname_is_valid(opts.arg, /* flags= */ 0))
880 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
881 "Invalid hostname: %s", opts.arg);
882 r = free_and_strdup_warn(&arg_hostname, opts.arg);
883 if (r < 0)
884 return r;
885 arg_settings_mask |= SETTING_HOSTNAME;
886 break;
887
888 OPTION_LONG("uuid", "UUID", "Set a specific machine UUID for the container"):
889 r = id128_from_string_nonzero(opts.arg, &arg_uuid);
890 if (r == -ENXIO)
891 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
892 "Machine UUID may not be all zeroes.");
893 if (r < 0)
894 return log_error_errno(r, "Invalid UUID: %s", opts.arg);
895 arg_settings_mask |= SETTING_MACHINE_ID;
896 break;
897
898 OPTION_GROUP("Properties"): {}
899
900 OPTION('S', "slice", "SLICE", "Place the container in the specified slice"): {
901 _cleanup_free_ char *mangled = NULL;
902
903 r = unit_name_mangle_with_suffix(opts.arg, NULL, UNIT_NAME_MANGLE_WARN, ".slice", &mangled);
904 if (r < 0)
905 return log_oom();
906
907 free_and_replace(arg_slice, mangled);
908 arg_settings_mask |= SETTING_SLICE;
909 break;
910 }
911
912 OPTION_LONG("property", "NAME=VALUE", "Set scope unit property"):
913 if (strv_extend(&arg_property, opts.arg) < 0)
914 return log_oom();
915 break;
916
917 OPTION_LONG("register", "BOOLEAN", "Register container as machine"):
918 r = parse_tristate_argument_with_auto("--register=", opts.arg, &arg_register);
919 if (r < 0)
920 return r;
921 break;
922
923 OPTION_LONG("keep-unit", NULL,
924 "Do not register a scope for the machine, reuse the service unit nspawn is running in"):
925 arg_keep_unit = true;
926 break;
927
928 OPTION_GROUP("User Namespacing"): {}
929
930 OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "private-users", "MODE",
931 "Run within user namespace, configure UID/GID range"):
932 r = parse_private_users(opts.arg, &arg_userns_mode, &arg_uid_shift, &arg_uid_range);
933 if (r < 0)
934 return r;
935 arg_settings_mask |= SETTING_USERNS;
936 break;
937
938 OPTION_LONG("private-users-ownership", "MODE",
939 "Adjust ('chown') or map ('map') OS tree ownership to private UID/GID range"):
940 if (streq(opts.arg, "help"))
941 return DUMP_STRING_TABLE(user_namespace_ownership, UserNamespaceOwnership, _USER_NAMESPACE_OWNERSHIP_MAX);
942
943 arg_userns_ownership = user_namespace_ownership_from_string(opts.arg);
944 if (arg_userns_ownership < 0)
945 return log_error_errno(arg_userns_ownership, "Cannot parse --private-users-ownership= value: %s", opts.arg);
946 arg_settings_mask |= SETTING_USERNS;
947 break;
948
949 OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "private-users-chown", "MODE", /* help= */ NULL): /* obsolete */
950 arg_userns_ownership = USER_NAMESPACE_OWNERSHIP_CHOWN;
951 arg_settings_mask |= SETTING_USERNS;
952 break;
953
954 OPTION_LONG("private-users-delegate", "N",
955 "Delegate N additional 64K UID/GID ranges for use by nested containers"):
956 r = safe_atou(opts.arg, &arg_delegate_container_ranges);
957 if (r < 0)
958 return log_error_errno(r, "Failed to parse --private-users-delegate= parameter: %s", opts.arg);
959 arg_settings_mask |= SETTING_USERNS;
960 break;
961
962 OPTION_SHORT('U', NULL,
963 "Equivalent to --private-users=pick and --private-users-ownership=auto"):
964 if (userns_supported()) {
965 arg_userns_mode = _USER_NAMESPACE_MODE_INVALID;
966 arg_uid_shift = UID_INVALID;
967 arg_uid_range = UINT32_C(0x10000);
968 arg_settings_mask |= SETTING_USERNS;
969 }
970 break;
971
972 OPTION_GROUP("Networking"): {}
973
974 OPTION_LONG("private-network", NULL, "Disable network in container"):
975 arg_private_network = true;
976 arg_settings_mask |= SETTING_NETWORK;
977 break;
978
979 OPTION_LONG("network-interface", "HOSTIF[:CONTAINERIF]",
980 "Assign an existing network interface to the container"):
981 r = interface_pair_parse(&arg_network_interfaces, opts.arg);
982 if (r < 0)
983 return r;
984 arg_private_network = true;
985 arg_settings_mask |= SETTING_NETWORK;
986 break;
987
988 OPTION_LONG("network-macvlan", "HOSTIF[:CONTAINERIF]",
989 "Create a macvlan network interface based on an existing network interface to the container"):
990 r = macvlan_pair_parse(&arg_network_macvlan, opts.arg);
991 if (r < 0)
992 return r;
993 arg_private_network = true;
994 arg_settings_mask |= SETTING_NETWORK;
995 break;
996
997 OPTION_LONG("network-ipvlan", "HOSTIF[:CONTAINERIF]",
998 "Create an ipvlan network interface based on an existing network interface to the container"):
999 r = ipvlan_pair_parse(&arg_network_ipvlan, opts.arg);
1000 if (r < 0)
1001 return r;
1002 arg_private_network = true;
1003 arg_settings_mask |= SETTING_NETWORK;
1004 break;
1005
1006 OPTION('n', "network-veth", NULL,
1007 "Add a virtual Ethernet connection between host and container"):
1008 arg_network_veth = true;
1009 arg_private_network = true;
1010 arg_settings_mask |= SETTING_NETWORK;
1011 break;
1012
1013 OPTION_LONG("network-veth-extra", "HOSTIF[:CONTAINERIF]",
1014 "Add an additional virtual Ethernet link between host and container"):
1015 r = veth_extra_parse(&arg_network_veth_extra, opts.arg);
1016 if (r < 0)
1017 return log_error_errno(r, "Failed to parse --network-veth-extra= parameter: %s", opts.arg);
1018 arg_private_network = true;
1019 arg_settings_mask |= SETTING_NETWORK;
1020 break;
1021
1022 OPTION_LONG("network-bridge", "INTERFACE",
1023 "Add a virtual Ethernet connection to the container and attach it to an existing bridge on the host"):
1024 if (!ifname_valid(opts.arg))
1025 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1026 "Bridge interface name not valid: %s", opts.arg);
1027 r = free_and_strdup(&arg_network_bridge, opts.arg);
1028 if (r < 0)
1029 return log_oom();
1030 arg_network_veth = true;
1031 arg_private_network = true;
1032 arg_settings_mask |= SETTING_NETWORK;
1033 break;
1034
1035 OPTION_LONG("network-zone", "NAME",
1036 "Similar, but attach the new interface to an automatically managed bridge interface"): {
1037 _cleanup_free_ char *j = NULL;
1038
1039 j = strjoin("vz-", opts.arg);
1040 if (!j)
1041 return log_oom();
1042
1043 if (!ifname_valid(j))
1044 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1045 "Network zone name not valid: %s", j);
1046
1047 free_and_replace(arg_network_zone, j);
1048 arg_network_veth = true;
1049 arg_private_network = true;
1050 arg_settings_mask |= SETTING_NETWORK;
1051 break;
1052 }
1053
1054 OPTION_LONG("network-namespace-path", "PATH",
1055 "Set network namespace to the one represented by the specified kernel namespace file node"):
1056 r = parse_path_argument(opts.arg, false, &arg_network_namespace_path);
1057 if (r < 0)
1058 return r;
1059 arg_settings_mask |= SETTING_NETWORK;
1060 break;
1061
1062 OPTION('p', "port", "[PROTOCOL:]HOSTPORT[:CONTAINERPORT]",
1063 "Expose a container IP port on the host"):
1064 r = expose_port_parse(&arg_expose_ports, opts.arg);
1065 if (r == -EEXIST)
1066 return log_error_errno(r, "Duplicate port specification: %s", opts.arg);
1067 if (r < 0)
1068 return log_error_errno(r, "Failed to parse host port %s: %m", opts.arg);
1069 arg_settings_mask |= SETTING_EXPOSE_PORTS;
1070 break;
1071
1072 OPTION_GROUP("Security"): {}
1073
1074 OPTION_LONG("capability", "CAP",
1075 "In addition to the default, retain specified capability"): {}
1076 OPTION_LONG("drop-capability", "CAP",
1077 "Drop the specified capability from the default set"): {
1078 uint64_t m;
1079 r = parse_capability_spec(opts.arg, &m);
1080 if (r <= 0)
1081 return r;
1082
1083 if (streq(opts.opt->long_code, "capability"))
1084 plus |= m;
1085 else
1086 minus |= m;
1087 arg_settings_mask |= SETTING_CAPABILITY;
1088 break;
1089 }
1090
1091 OPTION_LONG("ambient-capability", "CAP",
1092 "Sets the specified capability for the started process"): {
1093 uint64_t m;
1094 r = parse_capability_spec(opts.arg, &m);
1095 if (r <= 0)
1096 return r;
1097 arg_caps_ambient |= m;
1098 arg_settings_mask |= SETTING_CAPABILITY;
1099 break;
1100 }
1101
1102 OPTION_LONG("no-new-privileges", "BOOL",
1103 "Set PR_SET_NO_NEW_PRIVS flag for container payload"):
1104 r = parse_boolean_argument("--no-new-privileges=", opts.arg, &arg_no_new_privileges);
1105 if (r < 0)
1106 return r;
1107 arg_settings_mask |= SETTING_NO_NEW_PRIVILEGES;
1108 break;
1109
1110 OPTION_LONG("system-call-filter", "LIST|~LIST",
1111 "Permit/prohibit specific system calls"): {
1112 bool negative;
1113 const char *items;
1114
1115 negative = opts.arg[0] == '~';
1116 items = negative ? opts.arg + 1 : opts.arg;
1117
1118 for (;;) {
1119 _cleanup_free_ char *word = NULL;
1120
1121 r = extract_first_word(&items, &word, NULL, 0);
1122 if (r == 0)
1123 break;
1124 if (r == -ENOMEM)
1125 return log_oom();
1126 if (r < 0)
1127 return log_error_errno(r, "Failed to parse system call filter: %m");
1128
1129 if (negative)
1130 r = strv_extend(&arg_syscall_deny_list, word);
1131 else
1132 r = strv_extend(&arg_syscall_allow_list, word);
1133 if (r < 0)
1134 return log_oom();
1135 }
1136 arg_settings_mask |= SETTING_SYSCALL_FILTER;
1137 break;
1138 }
1139
1140 OPTION_LONG("restrict-address-families", "LIST", "Restrict socket address families to the given allowlist"):
1141 r = parse_address_families(opts.arg, &arg_restrict_address_families, &arg_restrict_address_families_is_allowlist);
1142 if (r < 0)
1143 return log_error_errno(r, "Failed to parse --restrict-address-families= argument: %s", opts.arg);
1144
1145 arg_settings_mask |= SETTING_RESTRICT_ADDRESS_FAMILIES;
1146 break;
1147
1148 OPTION('Z', "selinux-context", "SECLABEL",
1149 "Set the SELinux security context to be used by processes in the container"):
1150 arg_selinux_context = opts.arg;
1151 break;
1152
1153 OPTION('L', "selinux-apifs-context", "SECLABEL",
1154 "Set the SELinux security context to be used by API/tmpfs file systems in the container"):
1155 arg_selinux_apifs_context = opts.arg;
1156 break;
1157
1158 OPTION_GROUP("Resources"): {}
1159
1160 OPTION_LONG("rlimit", "NAME=LIMIT", "Set a resource limit for the payload"): {
1161 const char *eq;
1162 _cleanup_free_ char *name = NULL;
1163 int rl;
1164
1165 if (streq(opts.arg, "help"))
1166 return DUMP_STRING_TABLE(rlimit, int, _RLIMIT_MAX);
1167
1168 eq = strchr(opts.arg, '=');
1169 if (!eq)
1170 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1171 "--rlimit= expects an '=' assignment.");
1172
1173 name = strndup(opts.arg, eq - opts.arg);
1174 if (!name)
1175 return log_oom();
1176
1177 rl = rlimit_from_string_harder(name);
1178 if (rl < 0)
1179 return log_error_errno(rl, "Unknown resource limit: %s", name);
1180
1181 if (!arg_rlimit[rl]) {
1182 arg_rlimit[rl] = new0(struct rlimit, 1);
1183 if (!arg_rlimit[rl])
1184 return log_oom();
1185 }
1186
1187 r = rlimit_parse(rl, eq + 1, arg_rlimit[rl]);
1188 if (r < 0)
1189 return log_error_errno(r, "Failed to parse resource limit: %s", eq + 1);
1190
1191 arg_settings_mask |= SETTING_RLIMIT_FIRST << rl;
1192 break;
1193 }
1194
1195 OPTION_LONG("oom-score-adjust", "VALUE", "Adjust the OOM score value for the payload"):
1196 r = parse_oom_score_adjust(opts.arg, &arg_oom_score_adjust);
1197 if (r < 0)
1198 return log_error_errno(r, "Failed to parse --oom-score-adjust= parameter: %s", opts.arg);
1199 arg_oom_score_adjust_set = true;
1200 arg_settings_mask |= SETTING_OOM_SCORE_ADJUST;
1201 break;
1202
1203 OPTION_LONG("cpu-affinity", "CPUS", "Adjust the CPU affinity of the container"): {
1204 CPUSet cpuset;
1205
1206 r = parse_cpu_set(opts.arg, &cpuset);
1207 if (r < 0)
1208 return log_error_errno(r, "Failed to parse CPU affinity mask %s: %m", opts.arg);
1209
1210 cpu_set_done_and_replace(arg_cpu_set, cpuset);
1211 arg_settings_mask |= SETTING_CPU_AFFINITY;
1212 break;
1213 }
1214
1215 OPTION_LONG("personality", "ARCH", "Pick personality for this container"):
1216 arg_personality = personality_from_string(opts.arg);
1217 if (arg_personality == PERSONALITY_INVALID)
1218 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1219 "Unknown or unsupported personality '%s'.", opts.arg);
1220 arg_settings_mask |= SETTING_PERSONALITY;
1221 break;
1222
1223 OPTION_GROUP("Integration"): {}
1224
1225 OPTION_LONG("resolv-conf", "MODE", "Select mode of /etc/resolv.conf initialization"):
1226 if (streq(opts.arg, "help"))
1227 return DUMP_STRING_TABLE(resolv_conf_mode, ResolvConfMode, _RESOLV_CONF_MODE_MAX);
1228
1229 arg_resolv_conf = resolv_conf_mode_from_string(opts.arg);
1230 if (arg_resolv_conf < 0)
1231 return log_error_errno(arg_resolv_conf,
1232 "Failed to parse /etc/resolv.conf mode: %s", opts.arg);
1233 arg_settings_mask |= SETTING_RESOLV_CONF;
1234 break;
1235
1236 OPTION_LONG("timezone", "MODE", "Select mode of /etc/localtime initialization"):
1237 if (streq(opts.arg, "help"))
1238 return DUMP_STRING_TABLE(timezone_mode, TimezoneMode, _TIMEZONE_MODE_MAX);
1239
1240 arg_timezone = timezone_mode_from_string(opts.arg);
1241 if (arg_timezone < 0)
1242 return log_error_errno(arg_timezone,
1243 "Failed to parse /etc/localtime mode: %s", opts.arg);
1244 arg_settings_mask |= SETTING_TIMEZONE;
1245 break;
1246
1247 OPTION_LONG("link-journal", "MODE",
1248 "Link up guest journal, one of no, auto, guest, host, try-guest, try-host"):
1249 r = parse_link_journal(opts.arg, &arg_link_journal, &arg_link_journal_try);
1250 if (r < 0)
1251 return log_error_errno(r, "Failed to parse link journal mode %s", opts.arg);
1252 arg_settings_mask |= SETTING_LINK_JOURNAL;
1253 break;
1254
1255 OPTION_SHORT('j', NULL, "Equivalent to --link-journal=try-guest"):
1256 arg_link_journal = LINK_GUEST;
1257 arg_link_journal_try = true;
1258 arg_settings_mask |= SETTING_LINK_JOURNAL;
1259 break;
1260
1261 OPTION_LONG("forward-journal", "FILE|DIR", "Forward the container's journal to the host"):
1262 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_forward_journal);
1263 if (r < 0)
1264 return r;
1265 break;
1266
1267 OPTION_LONG("forward-journal-max-use", "BYTES", "Maximum disk space for forwarded journal"):
1268 r = parse_size(opts.arg, 1024, &arg_forward_journal_max_use);
1269 if (r < 0)
1270 return log_error_errno(r, "Failed to parse --forward-journal-max-use= value: %s", opts.arg);
1271 break;
1272
1273 OPTION_LONG("forward-journal-keep-free", "BYTES", "Minimum disk space to keep free"):
1274 r = parse_size(opts.arg, 1024, &arg_forward_journal_keep_free);
1275 if (r < 0)
1276 return log_error_errno(r, "Failed to parse --forward-journal-keep-free= value: %s", opts.arg);
1277 break;
1278
1279 OPTION_LONG("forward-journal-max-file-size", "BYTES", "Maximum size of individual journal files"):
1280 r = parse_size(opts.arg, 1024, &arg_forward_journal_max_file_size);
1281 if (r < 0)
1282 return log_error_errno(r, "Failed to parse --forward-journal-max-file-size= value: %s", opts.arg);
1283 break;
1284
1285 OPTION_LONG("forward-journal-max-files", "N", "Maximum number of journal files to keep"):
1286 r = safe_atou64(opts.arg, &arg_forward_journal_max_files);
1287 if (r < 0)
1288 return log_error_errno(r, "Failed to parse --forward-journal-max-files= value: %s", opts.arg);
1289 break;
1290
1291 OPTION_GROUP("Mounts"): {}
1292
1293 OPTION_LONG("bind", "PATH[:PATH[:OPTIONS]]",
1294 "Bind mount a file or directory from the host into the container"): {}
1295 OPTION_LONG("bind-ro", "PATH[:PATH[:OPTIONS]]",
1296 "Similar, but creates a read-only bind mount"):
1297 r = bind_mount_parse(&arg_custom_mounts, &arg_n_custom_mounts, opts.arg,
1298 streq(opts.opt->long_code, "bind-ro"));
1299 if (r < 0)
1300 return log_error_errno(r, "Failed to parse --bind(-ro)= argument %s: %m", opts.arg);
1301 arg_settings_mask |= SETTING_CUSTOM_MOUNTS;
1302 break;
1303
1304 OPTION_LONG("inaccessible", "PATH",
1305 "Over-mount file node with inaccessible node to mask it"):
1306 r = inaccessible_mount_parse(&arg_custom_mounts, &arg_n_custom_mounts, opts.arg);
1307 if (r < 0)
1308 return log_error_errno(r, "Failed to parse --inaccessible= argument %s: %m", opts.arg);
1309 arg_settings_mask |= SETTING_CUSTOM_MOUNTS;
1310 break;
1311
1312 OPTION_LONG("tmpfs", "PATH:[OPTIONS]",
1313 "Mount an empty tmpfs to the specified directory"):
1314 r = tmpfs_mount_parse(&arg_custom_mounts, &arg_n_custom_mounts, opts.arg);
1315 if (r < 0)
1316 return log_error_errno(r, "Failed to parse --tmpfs= argument %s: %m", opts.arg);
1317 arg_settings_mask |= SETTING_CUSTOM_MOUNTS;
1318 break;
1319
1320 OPTION_LONG("overlay", "PATH[:PATH...]:PATH",
1321 "Create an overlay mount from the host to the container"): {}
1322 OPTION_LONG("overlay-ro", "PATH[:PATH...]:PATH",
1323 "Similar, but creates a read-only overlay mount"):
1324 r = overlay_mount_parse(&arg_custom_mounts, &arg_n_custom_mounts, opts.arg,
1325 streq(opts.opt->long_code, "overlay-ro"));
1326 if (r == -EADDRNOTAVAIL)
1327 return log_error_errno(r, "--overlay(-ro)= needs at least two colon-separated directories specified.");
1328 if (r < 0)
1329 return log_error_errno(r, "Failed to parse --overlay(-ro)= argument %s: %m", opts.arg);
1330 arg_settings_mask |= SETTING_CUSTOM_MOUNTS;
1331 break;
1332
1333 OPTION_LONG("bind-user", "NAME", "Bind user from host to container"):
1334 if (!valid_user_group_name(opts.arg, 0))
1335 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid user name to bind: %s", opts.arg);
1336 if (strv_extend(&arg_bind_user, opts.arg) < 0)
1337 return log_oom();
1338 arg_settings_mask |= SETTING_BIND_USER;
1339 break;
1340
1341 OPTION_LONG("bind-user-shell", "BOOL|PATH",
1342 "Configure the shell to use for --bind-user= users"): {
1343 bool copy = false;
1344 char *sh = NULL;
1345 r = parse_user_shell(opts.arg, &sh, &copy);
1346 if (r == -ENOMEM)
1347 return log_oom();
1348 if (r < 0)
1349 return log_error_errno(r, "Invalid user shell to bind: %s", opts.arg);
1350
1351 free_and_replace(arg_bind_user_shell, sh);
1352 arg_bind_user_shell_copy = copy;
1353 arg_settings_mask |= SETTING_BIND_USER_SHELL;
1354 break;
1355 }
1356
1357 OPTION_LONG("bind-user-group", "GROUP",
1358 "Add an auxiliary group to --bind-user= users"):
1359 if (!valid_user_group_name(opts.arg, /* flags= */ 0))
1360 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid bind user auxiliary group name: %s", opts.arg);
1361 if (strv_extend(&arg_bind_user_groups, opts.arg) < 0)
1362 return log_oom();
1363 break;
1364
1365 OPTION_GROUP("Input/Output"): {}
1366
1367 OPTION_LONG("console", "MODE",
1368 "Select how stdin/stdout/stderr and /dev/console are set up for the container"):
1369 if (streq(opts.arg, "help"))
1370 return DUMP_STRING_TABLE(console_mode, ConsoleMode, _CONSOLE_MODE_MAX);
1371
1372 arg_console_mode = console_mode_from_string(opts.arg);
1373 if (arg_console_mode < 0)
1374 return log_error_errno(arg_console_mode, "Unknown console mode: %s", opts.arg);
1375 arg_settings_mask |= SETTING_CONSOLE_MODE;
1376 break;
1377
1378 OPTION('P', "pipe", NULL, "Equivalent to --console=pipe"):
1379 arg_console_mode = CONSOLE_PIPE;
1380 arg_settings_mask |= SETTING_CONSOLE_MODE;
1381 break;
1382
1383 OPTION_LONG("background", "COLOR", "Set ANSI color for background"):
1384 r = parse_background_argument(opts.arg, &arg_background);
1385 if (r < 0)
1386 return r;
1387 break;
1388
1389 OPTION_GROUP("Credentials"): {}
1390
1391 OPTION_LONG("set-credential", "ID:VALUE",
1392 "Pass a credential with literal value to container"):
1393 r = machine_credential_set(&arg_credentials, opts.arg);
1394 if (r < 0)
1395 return r;
1396 arg_settings_mask |= SETTING_CREDENTIALS;
1397 break;
1398
1399 OPTION_LONG("load-credential", "ID:PATH",
1400 "Load credential to pass to container from file or AF_UNIX stream socket"):
1401 r = machine_credential_load(&arg_credentials, opts.arg);
1402 if (r < 0)
1403 return r;
1404 arg_settings_mask |= SETTING_CREDENTIALS;
1405 break;
1406
1407 OPTION_GROUP("Other"): {}
1408
1409 OPTION_LONG("share-system", NULL, /* help= */ NULL): /* not documented */
1410 log_warning("Please do not use --share-system anymore, use $SYSTEMD_NSPAWN_SHARE_* instead.");
1411 arg_clone_ns_flags = 0;
1412 break;
1413
1414 OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "user", "NAME", "Run in the user service manager scope"):
1415 if (opts.arg)
1416 /* --user=NAME is a deprecated alias for --uid=NAME */
1417 log_warning("--user=NAME is deprecated, use --uid=NAME instead.");
1418 else {
1419 /* --user= used to require an argument (the container user to run as). It has
1420 * been repurposed to optionally set the runtime scope, with --uid= replacing
1421 * the old container user functionality. To maintain backwards compatibility
1422 * with the space-separated form (--user NAME), if the next opts.arg does not look
1423 * like an option, interpret it as a user name. */
1424 const char *t = option_parser_peek_next_arg(&opts);
1425 if (t && t[0] != '-') {
1426 opts.arg = option_parser_consume_next_arg(&opts);
1427 log_warning("--user NAME is deprecated, use --uid=NAME instead.");
1428 }
1429 }
1430
1431 if (opts.arg) {
1432 r = free_and_strdup(&arg_user, opts.arg);
1433 if (r < 0)
1434 return log_oom();
1435 arg_settings_mask |= SETTING_USER;
1436 } else
1437 arg_runtime_scope = RUNTIME_SCOPE_USER;
1438 break;
1439
1440 OPTION_LONG("system", NULL, "Run in the system service manager scope"):
1441 arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
1442 break;
1443 }
1444 }
1445
1446 char **args = option_parser_get_args(&opts);
1447 if (!strv_isempty(args)) {
1448 strv_free(arg_parameters);
1449 arg_parameters = strv_copy(args);
1450 if (!arg_parameters)
1451 return log_oom();
1452
1453 arg_settings_mask |= SETTING_START_MODE;
1454 }
1455
1456 if (arg_ephemeral && arg_template && !arg_directory)
1457 /* User asked for ephemeral execution but specified --template= instead of --directory=. Semantically
1458 * such an invocation makes some sense, see https://github.com/systemd/systemd/issues/3667. Let's
1459 * accept this here, and silently make "--ephemeral --template=" equivalent to "--ephemeral
1460 * --directory=". */
1461 arg_directory = TAKE_PTR(arg_template);
1462
1463 /* Derive runtime scope from UID if not explicitly set via --user/--system */
1464 if (arg_runtime_scope < 0)
1465 arg_runtime_scope = getuid() == 0 ? RUNTIME_SCOPE_SYSTEM : RUNTIME_SCOPE_USER;
1466
1467 if (arg_userns_mode == _USER_NAMESPACE_MODE_INVALID) {
1468 /* -U sets arg_userns_mode to _USER_NAMESPACE_MODE_INVALID to defer the PICK vs MANAGED
1469 * resolution to here where arg_runtime_scope has its final value. */
1470 if (arg_runtime_scope == RUNTIME_SCOPE_USER)
1471 arg_userns_mode = USER_NAMESPACE_MANAGED;
1472 else if (FLAGS_SET(arg_settings_mask, SETTING_USERNS))
1473 arg_userns_mode = USER_NAMESPACE_PICK;
1474 else
1475 arg_userns_mode = USER_NAMESPACE_NO;
1476 }
1477
1478 if (!FLAGS_SET(arg_settings_mask, SETTING_NETWORK))
1479 /* Imply private networking for unprivileged operation, since kernel otherwise
1480 * refuses mounting sysfs. */
1481 arg_private_network = arg_runtime_scope == RUNTIME_SCOPE_USER;
1482
1483 arg_caps_retain |= plus;
1484 arg_caps_retain |= arg_private_network ? UINT64_C(1) << CAP_NET_ADMIN : 0;
1485 arg_caps_retain &= ~minus;
1486
1487 if ((arg_forward_journal_max_use != UINT64_MAX ||
1488 arg_forward_journal_keep_free != UINT64_MAX ||
1489 arg_forward_journal_max_file_size != UINT64_MAX ||
1490 arg_forward_journal_max_files != UINT64_MAX) && !arg_forward_journal)
1491 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--forward-journal-max-use=/--forward-journal-keep-free=/--forward-journal-max-file-size=/--forward-journal-max-files= require --forward-journal=.");
1492
1493 /* Make sure to parse environment before we reset the settings mask below */
1494 r = parse_environment();
1495 if (r < 0)
1496 return r;
1497
1498 /* Load all settings from .nspawn files */
1499 if (mask_no_settings)
1500 arg_settings_mask = 0;
1501
1502 /* Don't load any settings from .nspawn files */
1503 if (mask_all_settings)
1504 arg_settings_mask = _SETTINGS_MASK_ALL;
1505
1506 return 1;
1507 }
1508
1509 static int verify_arguments(void) {
1510 int r;
1511
1512 SET_FLAG(arg_mount_settings, MOUNT_UNMANAGED, arg_userns_mode != USER_NAMESPACE_MANAGED);
1513
1514 /* We can mount selinuxfs only if we are privileged and can do so before userns. In managed mode we
1515 * have to enter the userns earlier, hence cannot do that. */
1516 /* SET_FLAG(arg_mount_settings, MOUNT_PRIVILEGED, arg_runtime_scope == RUNTIME_SCOPE_SYSTEM); */
1517 SET_FLAG(arg_mount_settings, MOUNT_PRIVILEGED, arg_userns_mode != USER_NAMESPACE_MANAGED);
1518
1519 SET_FLAG(arg_mount_settings, MOUNT_USE_USERNS, arg_userns_mode != USER_NAMESPACE_NO);
1520
1521 if (arg_private_network)
1522 SET_FLAG(arg_mount_settings, MOUNT_APPLY_APIVFS_NETNS, arg_private_network);
1523
1524 if (arg_runtime_scope != RUNTIME_SCOPE_SYSTEM && arg_userns_mode != USER_NAMESPACE_MANAGED)
1525 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "User-scoped operation requires managed user namespaces, as otherwise no UID range can be acquired.");
1526
1527 if (arg_userns_mode == USER_NAMESPACE_MANAGED && !arg_private_network)
1528 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Managed user namespace operation requires private networking, as otherwise /sys/ may not be mounted.");
1529
1530 if (arg_delegate_container_ranges > 0 && arg_userns_mode != USER_NAMESPACE_MANAGED)
1531 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--private-users-delegate= requires --private-users=managed.");
1532
1533 if (!(arg_clone_ns_flags & CLONE_NEWPID) ||
1534 !(arg_clone_ns_flags & CLONE_NEWUTS)) {
1535 arg_register = 0;
1536 if (arg_start_mode != START_PID1)
1537 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--boot cannot be used without namespacing.");
1538 }
1539
1540 if (arg_userns_ownership < 0)
1541 arg_userns_ownership =
1542 IN_SET(arg_userns_mode, USER_NAMESPACE_PICK, USER_NAMESPACE_MANAGED) ? USER_NAMESPACE_OWNERSHIP_AUTO : USER_NAMESPACE_OWNERSHIP_OFF;
1543
1544 if (arg_start_mode == START_BOOT && arg_kill_signal <= 0)
1545 arg_kill_signal = SIGRTMIN+3;
1546
1547 if (arg_volatile_mode != VOLATILE_NO) /* Make sure all file systems contained in the image are mounted read-only if we are in volatile mode */
1548 arg_read_only = true;
1549
1550 if (has_custom_root_mount(arg_custom_mounts, arg_n_custom_mounts))
1551 arg_read_only = true;
1552
1553 if (!!arg_directory + !!arg_image + !!arg_mstack > 1)
1554 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--directory=, --image= --mstack= may not be combined.");
1555
1556 if (arg_template && (arg_image || arg_mstack))
1557 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--template= and --image=/--mstack= may not be combined.");
1558
1559 if (arg_template && !(arg_directory || arg_machine))
1560 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--template= needs --directory= or --machine=.");
1561
1562 if (arg_ephemeral && arg_template)
1563 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--ephemeral and --template= may not be combined.");
1564
1565 if (arg_ephemeral && arg_mstack)
1566 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--ephemeral and --mstack= may not be combined.");
1567
1568 /* Permit --ephemeral with --link-journal=try-* to satisfy principle of the least astonishment
1569 * (by common sense, "try" means "do not fail if not possible") */
1570 if (arg_ephemeral && !IN_SET(arg_link_journal, LINK_NO, LINK_AUTO) && !arg_link_journal_try)
1571 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--ephemeral and --link-journal={host,guest} may not be combined.");
1572
1573 if (arg_userns_mode != USER_NAMESPACE_NO && !userns_supported())
1574 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--private-users= is not supported, kernel compiled without user namespace support.");
1575
1576 if (arg_userns_ownership == USER_NAMESPACE_OWNERSHIP_CHOWN && arg_read_only)
1577 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1578 "--read-only and --private-users-ownership=chown may not be combined.");
1579
1580 /* We don't support --private-users-ownership=chown together with any of the volatile modes since we
1581 * couldn't change the read-only part of the tree (i.e. /usr) anyway, or because it would trigger a
1582 * massive copy-up (in case of overlay) making the entire exercise pointless. */
1583 if (arg_userns_ownership == USER_NAMESPACE_OWNERSHIP_CHOWN && arg_volatile_mode != VOLATILE_NO)
1584 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--volatile= and --private-users-ownership=chown may not be combined.");
1585
1586 /* If --network-namespace-path is given with any other network-related option (except --private-network),
1587 * we need to error out, to avoid conflicts between different network options. */
1588 if (arg_network_namespace_path &&
1589 (arg_network_interfaces || arg_network_macvlan ||
1590 arg_network_ipvlan || arg_network_veth_extra ||
1591 arg_network_bridge || arg_network_zone ||
1592 arg_network_veth))
1593 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--network-namespace-path= cannot be combined with other network options.");
1594
1595 if (arg_network_bridge && arg_network_zone)
1596 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1597 "--network-bridge= and --network-zone= may not be combined.");
1598
1599 if (arg_userns_mode != USER_NAMESPACE_NO && (arg_mount_settings & MOUNT_APPLY_APIVFS_NETNS) && !arg_private_network)
1600 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid namespacing settings. Mounting sysfs with --private-users requires --private-network.");
1601
1602 if (arg_userns_mode != USER_NAMESPACE_NO && !(arg_mount_settings & MOUNT_APPLY_APIVFS_RO))
1603 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot combine --private-users with read-write API VFS mounts.");
1604
1605 if (arg_expose_ports && !arg_private_network)
1606 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot use --port= without private networking.");
1607
1608 if (arg_caps_ambient) {
1609 if (arg_caps_ambient == CAP_MASK_UNSET)
1610 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "AmbientCapability= does not support the value all.");
1611
1612 if ((arg_caps_ambient & arg_caps_retain) != arg_caps_ambient)
1613 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "AmbientCapability= setting is not fully covered by Capability= setting.");
1614
1615 if (arg_start_mode == START_BOOT)
1616 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "AmbientCapability= setting is not useful for boot mode.");
1617 }
1618
1619 /* Drop duplicate --bind-user= and --bind-user-group= entries */
1620 strv_uniq(arg_bind_user);
1621 strv_uniq(arg_bind_user_groups);
1622
1623 if (arg_bind_user_shell && strv_isempty(arg_bind_user))
1624 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot use --bind-user-shell= without --bind-user=");
1625
1626 if (!strv_isempty(arg_bind_user_groups) && strv_isempty(arg_bind_user))
1627 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot use --bind-user-group= without --bind-user=");
1628
1629 r = custom_mount_check_all();
1630 if (r < 0)
1631 return r;
1632
1633 if (arg_console_mode == CONSOLE_PIPE && isatty_safe(STDIN_FILENO) && isatty_safe(STDOUT_FILENO))
1634 log_full(arg_quiet ? LOG_DEBUG : LOG_NOTICE,
1635 "Console mode 'pipe' selected, but standard input/output are connected to an interactive TTY. "
1636 "Most likely you want to use 'interactive' console mode for proper interactivity and shell job control. "
1637 "Proceeding anyway.");
1638
1639 return 0;
1640 }
1641
1642 static int split_boot_parameters(void) {
1643 _cleanup_strv_free_ char **kept = NULL;
1644 int r;
1645
1646 /* When the kernel hands the command line to PID 1, any KEY=VALUE assignment whose KEY does not
1647 * contain a '.' is exported as an environment variable (with '-' replaced by '_'), rather than
1648 * passed as an argument. Mimic the same split here so users can pass kernel-cmdline-style
1649 * arguments after the container path and get the behavior they'd get on a real boot. */
1650
1651 if (arg_start_mode != START_BOOT)
1652 return 0;
1653
1654 STRV_FOREACH(p, arg_parameters) {
1655 _cleanup_free_ char *key = NULL, *value = NULL;
1656
1657 if (split_pair(*p, "=", &key, &value) >= 0 && !strchr(key, '.')) {
1658 string_replace_char(key, '-', '_');
1659
1660 if (env_name_is_valid(key) && env_value_is_valid(value)) {
1661 r = strv_env_assign(&arg_setenv, key, value);
1662 if (r < 0)
1663 return log_error_errno(r, "Cannot assign environment variable: %m");
1664
1665 arg_settings_mask |= SETTING_ENVIRONMENT;
1666 continue;
1667 }
1668 }
1669
1670 r = strv_extend(&kept, *p);
1671 if (r < 0)
1672 return log_oom();
1673 }
1674
1675 strv_free_and_replace(arg_parameters, kept);
1676 return 0;
1677 }
1678
1679 static int verify_network_interfaces_initialized(void) {
1680 int r;
1681 r = test_network_interfaces_initialized(arg_network_interfaces);
1682 if (r < 0)
1683 return r;
1684
1685 r = test_network_interfaces_initialized(arg_network_macvlan);
1686 if (r < 0)
1687 return r;
1688
1689 r = test_network_interfaces_initialized(arg_network_ipvlan);
1690 if (r < 0)
1691 return r;
1692
1693 return 0;
1694 }
1695
1696 static int in_child_chown(void) {
1697 /* Returns true when chown()ing inodes we create inside the outer child is required. Basically, we
1698 * need the chowning when we implement userns ourselves. If userns is off we don#t need to chown(),
1699 * obviously. And if we are in managed mode we already entered the userns, and hence don#t need to
1700 * manually chown either. */
1701 return IN_SET(arg_userns_mode, USER_NAMESPACE_PICK, USER_NAMESPACE_FIXED);
1702 }
1703
1704 int userns_chown_at(int fd, const char *fname, uid_t uid, gid_t gid, int flags) {
1705 assert(fd >= 0 || fd == AT_FDCWD);
1706
1707 if (!in_child_chown())
1708 return 0;
1709
1710 if (uid == UID_INVALID && gid == GID_INVALID)
1711 return 0;
1712
1713 if (uid != UID_INVALID) {
1714 uid += arg_uid_shift;
1715
1716 if (uid < arg_uid_shift || uid >= arg_uid_shift + arg_uid_range)
1717 return -EOVERFLOW;
1718 }
1719
1720 if (gid != GID_INVALID) {
1721 gid += (gid_t) arg_uid_shift;
1722
1723 if (gid < (gid_t) arg_uid_shift || gid >= (gid_t) (arg_uid_shift + arg_uid_range))
1724 return -EOVERFLOW;
1725 }
1726
1727 if (isempty(fname))
1728 flags |= AT_EMPTY_PATH;
1729
1730 return RET_NERRNO(fchownat(fd, strempty(fname), uid, gid, flags));
1731 }
1732
1733 int userns_lchown(const char *path, uid_t uid, gid_t gid) {
1734 return userns_chown_at(AT_FDCWD, path, uid, gid, AT_SYMLINK_NOFOLLOW);
1735 }
1736
1737 int userns_mkdir(const char *root, const char *path, mode_t mode, uid_t uid, gid_t gid) {
1738 int r;
1739
1740 assert(path);
1741
1742 _cleanup_close_ int parent_fd = -EBADF;
1743 _cleanup_free_ char *dname = NULL;
1744 r = chase(path, root, CHASE_PARENT|CHASE_PREFIX_ROOT|CHASE_EXTRACT_FILENAME, &dname, &parent_fd);
1745 if (r < 0)
1746 return r;
1747
1748 _cleanup_close_ int dir_fd = open_mkdir_at(parent_fd, dname, O_EXCL|O_CLOEXEC, mode);
1749 if (dir_fd == -EEXIST)
1750 return 0;
1751 if (dir_fd < 0)
1752 return dir_fd;
1753
1754 return userns_chown_at(dir_fd, /* fname= */ NULL, uid, gid, AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH);
1755 }
1756
1757 static const char *timezone_from_path(const char *path) {
1758 return PATH_STARTSWITH_SET(
1759 path,
1760 "../usr/share/zoneinfo/",
1761 "/usr/share/zoneinfo/");
1762 }
1763
1764 static bool etc_writable(void) {
1765 return !arg_read_only || IN_SET(arg_volatile_mode, VOLATILE_YES, VOLATILE_OVERLAY);
1766 }
1767
1768 static int setup_timezone(const char *dest) {
1769 _cleanup_free_ char *p = NULL, *etc = NULL;
1770 const char *where, *check;
1771 TimezoneMode m;
1772 int r;
1773
1774 assert(dest);
1775
1776 if (IN_SET(arg_timezone, TIMEZONE_AUTO, TIMEZONE_SYMLINK)) {
1777 r = readlink_malloc("/etc/localtime", &p);
1778 if (r == -ENOENT && arg_timezone == TIMEZONE_AUTO)
1779 m = etc_writable() ? TIMEZONE_DELETE : TIMEZONE_OFF;
1780 else if (r == -EINVAL && arg_timezone == TIMEZONE_AUTO) /* regular file? */
1781 m = etc_writable() ? TIMEZONE_COPY : TIMEZONE_BIND;
1782 else if (r < 0) {
1783 log_warning_errno(r, "Failed to read host's /etc/localtime symlink, not updating container timezone: %m");
1784 /* To handle warning, delete /etc/localtime and replace it with a symbolic link to a time zone data
1785 * file.
1786 *
1787 * Example:
1788 * ln -s /usr/share/zoneinfo/UTC /etc/localtime
1789 */
1790 return 0;
1791 } else if (arg_timezone == TIMEZONE_AUTO)
1792 m = etc_writable() ? TIMEZONE_SYMLINK : TIMEZONE_BIND;
1793 else
1794 m = arg_timezone;
1795 } else
1796 m = arg_timezone;
1797
1798 if (m == TIMEZONE_OFF)
1799 return 0;
1800
1801 r = chase("/etc", dest, CHASE_PREFIX_ROOT, &etc, NULL);
1802 if (r < 0) {
1803 log_warning_errno(r, "Failed to resolve /etc path in container, ignoring: %m");
1804 return 0;
1805 }
1806
1807 where = strjoina(etc, "/localtime");
1808
1809 switch (m) {
1810
1811 case TIMEZONE_DELETE:
1812 if (unlink(where) < 0)
1813 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, "Failed to remove '%s', ignoring: %m", where);
1814
1815 return 0;
1816
1817 case TIMEZONE_SYMLINK: {
1818 _cleanup_free_ char *q = NULL;
1819 const char *z, *what;
1820
1821 z = timezone_from_path(p);
1822 if (!z) {
1823 log_warning("/etc/localtime does not point into /usr/share/zoneinfo/, not updating container timezone.");
1824 return 0;
1825 }
1826
1827 r = readlink_malloc(where, &q);
1828 if (r >= 0 && streq_ptr(timezone_from_path(q), z))
1829 return 0; /* Already pointing to the right place? Then do nothing .. */
1830
1831 check = strjoina(dest, "/usr/share/zoneinfo/", z);
1832 r = chase(check, dest, 0, NULL, NULL);
1833 if (r < 0)
1834 log_debug_errno(r, "Timezone %s does not exist (or is not accessible) in container, not creating symlink: %m", z);
1835 else {
1836 if (unlink(where) < 0 && errno != ENOENT) {
1837 log_full_errno(ERRNO_IS_FS_WRITE_REFUSED(errno) ? LOG_DEBUG : LOG_WARNING, /* Don't complain on read-only images */
1838 errno, "Failed to remove existing timezone info %s in container, ignoring: %m", where);
1839 return 0;
1840 }
1841
1842 what = strjoina("../usr/share/zoneinfo/", z);
1843 if (symlink(what, where) < 0) {
1844 log_full_errno(ERRNO_IS_FS_WRITE_REFUSED(errno) ? LOG_DEBUG : LOG_WARNING,
1845 errno, "Failed to correct timezone of container, ignoring: %m");
1846 return 0;
1847 }
1848
1849 break;
1850 }
1851
1852 _fallthrough_;
1853 }
1854
1855 case TIMEZONE_BIND: {
1856 _cleanup_free_ char *resolved = NULL;
1857 int found;
1858
1859 found = chase(where, dest, CHASE_NONEXISTENT, &resolved, NULL);
1860 if (found < 0) {
1861 log_warning_errno(found, "Failed to resolve /etc/localtime path in container, ignoring: %m");
1862 return 0;
1863 }
1864
1865 if (found == 0) /* missing? */
1866 (void) touch(resolved);
1867
1868 r = mount_nofollow_verbose(LOG_WARNING, "/etc/localtime", resolved, NULL, MS_BIND, NULL);
1869 if (r >= 0)
1870 return mount_nofollow_verbose(LOG_ERR, NULL, resolved, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY|MS_NOSUID|MS_NODEV, NULL);
1871
1872 _fallthrough_;
1873 }
1874
1875 case TIMEZONE_COPY:
1876 /* If mounting failed, try to copy */
1877 r = copy_file_atomic("/etc/localtime", where, 0644, COPY_REFLINK|COPY_REPLACE);
1878 if (r < 0) {
1879 log_full_errno(ERRNO_IS_NEG_FS_WRITE_REFUSED(r) ? LOG_DEBUG : LOG_WARNING, r,
1880 "Failed to copy /etc/localtime to %s, ignoring: %m", where);
1881 return 0;
1882 }
1883
1884 break;
1885
1886 default:
1887 assert_not_reached();
1888 }
1889
1890 /* Fix permissions of the symlink or file copy we just created */
1891 r = userns_lchown(where, 0, 0);
1892 if (r < 0)
1893 log_warning_errno(r, "Failed to chown /etc/localtime, ignoring: %m");
1894
1895 return 0;
1896 }
1897
1898 static int have_resolv_conf(const char *path) {
1899 assert(path);
1900
1901 if (access(path, F_OK) < 0) {
1902 if (errno == ENOENT)
1903 return 0;
1904
1905 return log_debug_errno(errno, "Failed to determine whether '%s' is available: %m", path);
1906 }
1907
1908 return 1;
1909 }
1910
1911 static int resolved_listening(void) {
1912 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1913 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1914 _cleanup_free_ char *dns_stub_listener_mode = NULL;
1915 int r;
1916
1917 /* Check if resolved is listening */
1918
1919 r = sd_bus_open_system(&bus);
1920 if (r < 0)
1921 return log_debug_errno(r, "Failed to open system bus: %m");
1922
1923 r = bus_name_has_owner(bus, "org.freedesktop.resolve1", NULL);
1924 if (r < 0)
1925 return log_debug_errno(r, "Failed to check whether the 'org.freedesktop.resolve1' bus name is taken: %m");
1926 if (r == 0)
1927 return 0;
1928
1929 r = bus_get_property_string(bus, bus_resolve_mgr, "DNSStubListener", &error, &dns_stub_listener_mode);
1930 if (r < 0)
1931 return log_debug_errno(r, "Failed to query DNSStubListener property: %s", bus_error_message(&error, r));
1932
1933 return STR_IN_SET(dns_stub_listener_mode, "udp", "yes");
1934 }
1935
1936 static int setup_resolv_conf(const char *dest) {
1937 _cleanup_free_ char *etc = NULL;
1938 const char *where, *what;
1939 ResolvConfMode m;
1940 int r;
1941
1942 assert(dest);
1943
1944 if (arg_resolv_conf == RESOLV_CONF_AUTO) {
1945 if (arg_private_network)
1946 m = RESOLV_CONF_OFF;
1947 else if (have_resolv_conf(PRIVATE_STUB_RESOLV_CONF) > 0 && resolved_listening() > 0)
1948 m = etc_writable() ? RESOLV_CONF_COPY_STUB : RESOLV_CONF_BIND_STUB;
1949 else if (have_resolv_conf("/etc/resolv.conf") > 0)
1950 m = etc_writable() ? RESOLV_CONF_COPY_HOST : RESOLV_CONF_BIND_HOST;
1951 else
1952 m = etc_writable() ? RESOLV_CONF_DELETE : RESOLV_CONF_OFF;
1953
1954 } else
1955 m = arg_resolv_conf;
1956
1957 if (m == RESOLV_CONF_OFF)
1958 return 0;
1959
1960 r = chase("/etc", dest, CHASE_PREFIX_ROOT, &etc, NULL);
1961 if (r < 0) {
1962 log_warning_errno(r, "Failed to resolve /etc path in container, ignoring: %m");
1963 return 0;
1964 }
1965
1966 where = strjoina(etc, "/resolv.conf");
1967
1968 if (m == RESOLV_CONF_DELETE) {
1969 if (unlink(where) < 0)
1970 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, "Failed to remove '%s', ignoring: %m", where);
1971
1972 return 0;
1973 }
1974
1975 if (IN_SET(m, RESOLV_CONF_BIND_STATIC, RESOLV_CONF_REPLACE_STATIC, RESOLV_CONF_COPY_STATIC))
1976 what = PRIVATE_STATIC_RESOLV_CONF;
1977 else if (IN_SET(m, RESOLV_CONF_BIND_UPLINK, RESOLV_CONF_REPLACE_UPLINK, RESOLV_CONF_COPY_UPLINK))
1978 what = PRIVATE_UPLINK_RESOLV_CONF;
1979 else if (IN_SET(m, RESOLV_CONF_BIND_STUB, RESOLV_CONF_REPLACE_STUB, RESOLV_CONF_COPY_STUB))
1980 what = PRIVATE_STUB_RESOLV_CONF;
1981 else
1982 what = "/etc/resolv.conf";
1983
1984 if (IN_SET(m, RESOLV_CONF_BIND_HOST, RESOLV_CONF_BIND_STATIC, RESOLV_CONF_BIND_UPLINK, RESOLV_CONF_BIND_STUB)) {
1985 _cleanup_free_ char *resolved = NULL;
1986 int found;
1987
1988 found = chase(where, dest, CHASE_NONEXISTENT|CHASE_NOFOLLOW, &resolved, NULL);
1989 if (found < 0) {
1990 log_warning_errno(found, "Failed to resolve /etc/resolv.conf path in container, ignoring: %m");
1991 return 0;
1992 }
1993
1994 if (found == 0) /* missing? */
1995 (void) touch(resolved);
1996
1997 r = mount_nofollow_verbose(LOG_WARNING, what, resolved, NULL, MS_BIND, NULL);
1998 if (r >= 0)
1999 return mount_nofollow_verbose(LOG_ERR, NULL, resolved, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY|MS_NOSUID|MS_NODEV, NULL);
2000
2001 /* If that didn't work, let's copy the file */
2002 }
2003
2004 if (IN_SET(m, RESOLV_CONF_REPLACE_HOST, RESOLV_CONF_REPLACE_STATIC, RESOLV_CONF_REPLACE_UPLINK, RESOLV_CONF_REPLACE_STUB))
2005 r = copy_file_atomic(what, where, 0644, COPY_REFLINK|COPY_REPLACE);
2006 else
2007 r = copy_file(what, where, O_TRUNC|O_NOFOLLOW, 0644, COPY_REFLINK);
2008 if (r < 0) {
2009 /* If the file already exists as symlink, let's suppress the warning, under the assumption that
2010 * resolved or something similar runs inside and the symlink points there.
2011 *
2012 * If the disk image is read-only, there's also no point in complaining.
2013 */
2014 log_full_errno(!IN_SET(RESOLV_CONF_COPY_HOST, RESOLV_CONF_COPY_STATIC, RESOLV_CONF_COPY_UPLINK, RESOLV_CONF_COPY_STUB) &&
2015 (r == -ELOOP || ERRNO_IS_NEG_FS_WRITE_REFUSED(r)) ? LOG_DEBUG : LOG_WARNING, r,
2016 "Failed to copy /etc/resolv.conf to %s, ignoring: %m", where);
2017 return 0;
2018 }
2019
2020 r = userns_lchown(where, 0, 0);
2021 if (r < 0)
2022 log_warning_errno(r, "Failed to chown /etc/resolv.conf, ignoring: %m");
2023
2024 return 0;
2025 }
2026
2027 static int setup_boot_id_file(const char *directory) {
2028 _cleanup_free_ char *p = NULL;
2029 sd_id128_t rnd;
2030 int r;
2031
2032 assert(directory);
2033
2034 /* Generate a new randomized boot ID, so that each boot-up of the container gets a new one. We create
2035 * the backing file here in the outer child already, since /run/host/ is mounted read-only by the time
2036 * the inner child runs. We intentionally do not unlink it: bind mounts of unlinked files cannot be
2037 * replicated to other mount namespaces (both the old and new mount APIs fail with ENOENT). Since
2038 * mount_private_apivfs() needs to replicate submounts like boot_id when setting up a fresh /proc
2039 * instance, the backing file must remain on disk. It lives in /run/host/ which is cleaned up on
2040 * shutdown anyway. */
2041
2042 p = path_join(directory, "/run/host/proc-sys-kernel-random-boot-id");
2043 if (!p)
2044 return log_oom();
2045
2046 r = sd_id128_randomize(&rnd);
2047 if (r < 0)
2048 return log_error_errno(r, "Failed to generate random boot id: %m");
2049
2050 r = id128_write(p, ID128_FORMAT_UUID, rnd);
2051 if (r < 0)
2052 return log_error_errno(r, "Failed to write boot id: %m");
2053
2054 return userns_lchown(p, 0, 0);
2055 }
2056
2057 static int setup_boot_id(void) {
2058 int r;
2059
2060 r = mount_nofollow_verbose(
2061 LOG_ERR,
2062 "/run/host/proc-sys-kernel-random-boot-id",
2063 "/proc/sys/kernel/random/boot_id",
2064 /* fstype= */ NULL,
2065 MS_BIND,
2066 /* options= */ NULL);
2067 if (r < 0)
2068 return r;
2069
2070 return mount_nofollow_verbose(
2071 LOG_ERR,
2072 /* what= */ NULL,
2073 "/proc/sys/kernel/random/boot_id",
2074 /* fstype= */ NULL,
2075 MS_BIND|MS_REMOUNT|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV,
2076 /* options= */ NULL);
2077 }
2078
2079 static int bind_mount_devnode(const char *from, const char *to) {
2080 int r;
2081
2082 assert(from);
2083 assert(to);
2084
2085 r = touch(to);
2086 if (r < 0)
2087 return log_debug_errno(r, "Failed to touch %s: %m", to);
2088
2089 r = mount_nofollow_verbose(LOG_DEBUG, from, to, NULL, MS_BIND, NULL);
2090 if (r < 0) {
2091 (void) unlink(to);
2092 return log_error_errno(r, "Failed to bind mount %s to %s: %m", from, to);
2093 }
2094
2095 return 0;
2096 }
2097
2098 static int copy_devnode_one(const char *dest, const char *node, bool check) {
2099 int r;
2100
2101 assert(dest);
2102 assert(!isempty(node));
2103
2104 BLOCK_WITH_UMASK(0000);
2105
2106 _cleanup_free_ char *from = path_join("/dev/", node);
2107 if (!from)
2108 return log_oom();
2109
2110 if (check) {
2111 /* If 'check' is true, create /dev/fuse only when it is accessible. The check is necessary,
2112 * as some custom service units that invoke systemd-nspawn may enable DevicePolicy= without
2113 * DeviceAllow= for the device node. */
2114 _cleanup_close_ int fd = open(from, O_CLOEXEC|O_RDWR);
2115 if (fd < 0) {
2116 log_debug_errno(errno,
2117 "Failed to open %s, skipping creation of the device node in the container, ignoring: %m",
2118 from);
2119 return 0;
2120 }
2121 }
2122
2123 _cleanup_free_ char *to = path_join(dest, from);
2124 if (!to)
2125 return log_oom();
2126
2127 struct stat st;
2128 if (stat(from, &st) < 0) {
2129 if (errno != ENOENT)
2130 return log_error_errno(errno, "Failed to stat %s: %m", from);
2131
2132 log_debug_errno(errno, "Device node %s does not exist, ignoring.", from);
2133 return 0;
2134 }
2135 r = stat_verify_device_node(&st);
2136 if (r < 0)
2137 return log_error_errno(r, "'%s' is not a device node.", from);
2138
2139 /* Create the parent directory of the device node. Here, we assume that the path has at most one
2140 * subdirectory under /dev/, e.g. /dev/net/tun. */
2141 _cleanup_free_ char *parent = NULL;
2142 r = path_extract_directory(from, &parent);
2143 if (r < 0)
2144 return log_error_errno(r, "Failed to extract directory from %s: %m", from);
2145 r = userns_mkdir(dest, parent, 0755, 0, 0);
2146 if (r < 0)
2147 return log_error_errno(r, "Failed to create directory %s: %m", parent);
2148
2149 r = RET_NERRNO(mknod(to, st.st_mode, st.st_rdev));
2150 if (r < 0) {
2151 /* Explicitly warn the user when /dev/ is already populated. */
2152 if (r == -EEXIST)
2153 log_notice("%s/dev/ is pre-mounted and pre-populated. If a pre-mounted /dev/ is provided it needs to be an unpopulated file system.", dest);
2154
2155 /* If arg_uid_shift != 0, then we cannot fall back to use bind mount. */
2156 if (!(arg_userns_mode == USER_NAMESPACE_NO ||
2157 (arg_userns_mode == USER_NAMESPACE_FIXED && arg_uid_shift == 0))) {
2158
2159 if (arg_userns_mode != USER_NAMESPACE_MANAGED || !ERRNO_IS_NEG_PRIVILEGE(r))
2160 return log_error_errno(r, "Failed to mknod(%s): %m", to);
2161
2162 log_debug_errno(r, "Failed to create device node '%s' and running in managed mode, resorting to bind mount: %m", to);
2163 }
2164
2165 /* Some systems abusively restrict mknod but allow bind mounts. */
2166 if (bind_mount_devnode(from, to) < 0)
2167 /* use the original error code. */
2168 return log_error_errno(r, "Both mknod() and bind mount %s failed: %m", to);
2169 } else {
2170 /* mknod() succeeds, chown() it if necessary. */
2171 r = userns_lchown(to, 0, 0);
2172 if (r < 0)
2173 return log_error_errno(r, "chown() of device node %s failed: %m", to);
2174 }
2175
2176 _cleanup_free_ char *dn = path_join("/dev", S_ISCHR(st.st_mode) ? "char" : "block");
2177 if (!dn)
2178 return log_oom();
2179
2180 r = userns_mkdir(dest, dn, 0755, 0, 0);
2181 if (r < 0)
2182 return log_error_errno(r, "Failed to create '%s': %m", dn);
2183
2184 _cleanup_free_ char *sl = NULL;
2185 if (asprintf(&sl, "%s/" DEVNUM_FORMAT_STR, dn, DEVNUM_FORMAT_VAL(st.st_rdev)) < 0)
2186 return log_oom();
2187
2188 _cleanup_free_ char *prefixed = path_join(dest, sl);
2189 if (!prefixed)
2190 return log_oom();
2191
2192 _cleanup_free_ char *t = path_join("..", node);
2193 if (!t)
2194 return log_oom();
2195
2196 if (symlink(t, prefixed) < 0)
2197 log_debug_errno(errno, "Failed to symlink '%s' to '%s', ignoring: %m", t, prefixed);
2198
2199 return 0;
2200 }
2201
2202 static int copy_devnodes(const char *dest) {
2203 int r = 0;
2204
2205 assert(dest);
2206
2207 /* Required basic device nodes. */
2208 FOREACH_STRING(node, "null", "zero", "full", "random", "urandom", "tty") {
2209 r = copy_devnode_one(dest, node, /* check= */ false);
2210 if (r < 0)
2211 return r;
2212 }
2213
2214 /* Optional device nodes. */
2215 FOREACH_STRING(node, "fuse", "net/tun") {
2216 r = copy_devnode_one(dest, node, /* check= */ true);
2217 if (r < 0)
2218 return r;
2219 }
2220
2221 return 0;
2222 }
2223
2224 static int make_extra_nodes(const char *dest) {
2225 int r;
2226
2227 BLOCK_WITH_UMASK(0000);
2228
2229 FOREACH_ARRAY(node, arg_extra_nodes, arg_n_extra_nodes) {
2230 _cleanup_free_ char *path = NULL;
2231
2232 path = path_join(dest, node->path);
2233 if (!path)
2234 return log_oom();
2235
2236 dev_t dev = S_ISCHR(node->mode) || S_ISBLK(node->mode) ? makedev(node->major, node->minor) : 0;
2237 if (mknod(path, node->mode, dev) < 0)
2238 return log_error_errno(errno, "Failed to create device node '%s': %m", path);
2239
2240 r = chmod_and_chown(path, node->mode, node->uid, node->gid);
2241 if (r < 0)
2242 return log_error_errno(r, "Failed to adjust device node ownership of '%s': %m", path);
2243 }
2244
2245 return 0;
2246 }
2247
2248 static int setup_pts(const char *dest, uid_t chown_uid) {
2249 _cleanup_free_ char *options = NULL;
2250 int r;
2251
2252 #if HAVE_SELINUX
2253 if (arg_selinux_apifs_context)
2254 (void) asprintf(&options,
2255 "newinstance,ptmxmode=0666,mode=" STRINGIFY(TTY_MODE) ",gid=" GID_FMT ",context=\"%s\"",
2256 chown_uid + TTY_GID,
2257 arg_selinux_apifs_context);
2258 else
2259 #endif
2260 (void) asprintf(&options,
2261 "newinstance,ptmxmode=0666,mode=" STRINGIFY(TTY_MODE) ",gid=" GID_FMT,
2262 chown_uid + TTY_GID);
2263
2264 if (!options)
2265 return log_oom();
2266
2267 /* Mount /dev/pts itself */
2268 _cleanup_free_ char *p = path_join(dest, "/dev/pts");
2269 if (!p)
2270 return log_oom();
2271
2272 r = RET_NERRNO(mkdir(p, 0755));
2273 if (r < 0)
2274 return log_error_errno(r, "Failed to create /dev/pts: %m");
2275
2276 r = mount_nofollow_verbose(LOG_ERR, "devpts", p, "devpts", MS_NOSUID|MS_NOEXEC, options);
2277 if (r < 0)
2278 return r;
2279 r = userns_lchown(p, 0, 0);
2280 if (r < 0)
2281 return log_error_errno(r, "Failed to chown /dev/pts: %m");
2282
2283 /* Create /dev/ptmx symlink */
2284 free(p);
2285 p = path_join(dest, "/dev/ptmx");
2286 if (!p)
2287 return log_oom();
2288
2289 if (symlink("pts/ptmx", p) < 0)
2290 return log_error_errno(errno, "Failed to create /dev/ptmx symlink: %m");
2291 r = userns_lchown(p, 0, 0);
2292 if (r < 0)
2293 return log_error_errno(r, "Failed to chown /dev/ptmx: %m");
2294
2295 /* And fix /dev/pts/ptmx ownership */
2296 free(p);
2297 p = path_join(dest, "/dev/pts/ptmx");
2298 if (!p)
2299 return log_oom();
2300
2301 r = userns_lchown(p, 0, 0);
2302 if (r < 0)
2303 return log_error_errno(r, "Failed to chown /dev/pts/ptmx: %m");
2304
2305 return 0;
2306 }
2307
2308 static int setup_stdio_as_dev_console(void) {
2309 _cleanup_close_ int terminal = -EBADF;
2310 int r;
2311
2312 /* We open the TTY in O_NOCTTY mode, so that we do not become controller yet. We'll do that later
2313 * explicitly, if we are configured to. */
2314 terminal = open_terminal("/dev/console", O_RDWR|O_NOCTTY);
2315 if (terminal < 0)
2316 return log_error_errno(terminal, "Failed to open console: %m");
2317
2318 /* Make sure we can continue logging to the original stderr, even if
2319 * stderr points elsewhere now */
2320 r = log_dup_console();
2321 if (r < 0)
2322 return log_error_errno(r, "Failed to duplicate stderr: %m");
2323
2324 /* invalidates 'terminal' on success and failure */
2325 r = rearrange_stdio(terminal, terminal, terminal);
2326 TAKE_FD(terminal);
2327 if (r < 0)
2328 return log_error_errno(r, "Failed to move console to stdin/stdout/stderr: %m");
2329
2330 return 0;
2331 }
2332
2333 static int setup_dev_console(const char *console) {
2334 _cleanup_free_ char *p = NULL;
2335 int r;
2336
2337 /* Create /dev/console symlink */
2338 r = path_make_relative("/dev", console, &p);
2339 if (r < 0)
2340 return log_error_errno(r, "Failed to create relative path: %m");
2341
2342 if (symlink(p, "/dev/console") < 0)
2343 return log_error_errno(errno, "Failed to create /dev/console symlink: %m");
2344
2345 return 0;
2346 }
2347
2348 static int setup_keyring(void) {
2349 key_serial_t keyring;
2350
2351 /* Allocate a new session keyring for the container. This makes sure the keyring of the session
2352 * systemd-nspawn was invoked from doesn't leak into the container. Note that by default we block
2353 * keyctl() and request_key() anyway via seccomp so doing this operation isn't strictly necessary,
2354 * but in case people explicitly allow-list these system calls let's make sure we don't leak anything
2355 * into the container. */
2356
2357 keyring = keyctl(KEYCTL_JOIN_SESSION_KEYRING, 0, 0, 0, 0);
2358 if (keyring == -1) {
2359 if (errno == ENOSYS)
2360 log_debug_errno(errno, "Kernel keyring not supported, ignoring.");
2361 else if (ERRNO_IS_PRIVILEGE(errno))
2362 log_debug_errno(errno, "Kernel keyring access prohibited, ignoring.");
2363 else
2364 return log_error_errno(errno, "Setting up kernel keyring failed: %m");
2365 }
2366
2367 return 0;
2368 }
2369
2370 int make_run_host(const char *root) {
2371 int r;
2372
2373 assert(root);
2374
2375 r = userns_mkdir(root, "/run/host", 0755, 0, 0);
2376 if (r < 0)
2377 return log_error_errno(r, "Failed to create /run/host/: %m");
2378
2379 return 0;
2380 }
2381
2382 static int setup_credentials(const char *root) {
2383 bool world_readable = false;
2384 int r;
2385
2386 if (arg_credentials.n_credentials == 0)
2387 return 0;
2388
2389 /* If starting a single-process container as a non-root user, the uid will only be resolved after we
2390 * are inside the inner child, when credential directories and files are already read-only, so they
2391 * are unusable as the single process won't have access to them. We also don't have access to the
2392 * uid that will actually be used from here, as we are setting credentials up from the outer child.
2393 * In order to make them usable as requested by the configuration, make them world readable in that
2394 * case, as by definition there are no other processes in that case besides the one being started,
2395 * which is being configured to be able to access credentials, and any of its children which will
2396 * inherit its privileges anyway. To ensure this, also enforce (and document) that
2397 * --no-new-privileges is necessary for this combination to work. */
2398 if (arg_no_new_privileges && !isempty(arg_user) && !STR_IN_SET(arg_user, "root", "0") &&
2399 arg_start_mode == START_PID1)
2400 world_readable = true;
2401
2402 r = make_run_host(root);
2403 if (r < 0)
2404 return r;
2405
2406 r = userns_mkdir(root, "/run/host/credentials", world_readable ? 0777 : 0700, 0, 0);
2407 if (r < 0)
2408 return log_error_errno(r, "Failed to create /run/host/credentials: %m");
2409
2410 _cleanup_free_ char *q = path_join(root, "/run/host/credentials");
2411 if (!q)
2412 return log_oom();
2413
2414 r = mount_nofollow_verbose(LOG_ERR, NULL, q, "ramfs", MS_NOSUID|MS_NOEXEC|MS_NODEV, "mode=0700");
2415 if (r < 0)
2416 return r;
2417
2418 FOREACH_ARRAY(cred, arg_credentials.credentials, arg_credentials.n_credentials) {
2419 _cleanup_free_ char *j = NULL;
2420 _cleanup_close_ int fd = -EBADF;
2421
2422 j = path_join(q, cred->id);
2423 if (!j)
2424 return log_oom();
2425
2426 fd = open(j, O_CREAT|O_EXCL|O_WRONLY|O_CLOEXEC|O_NOFOLLOW, world_readable ? 0666 : 0600);
2427 if (fd < 0)
2428 return log_error_errno(errno, "Failed to create credential file %s: %m", j);
2429
2430 r = loop_write(fd, cred->data, cred->size);
2431 if (r < 0)
2432 return log_error_errno(r, "Failed to write credential to file %s: %m", j);
2433
2434 if (fchmod(fd, world_readable ? 0444 : 0400) < 0)
2435 return log_error_errno(errno, "Failed to adjust access mode of %s: %m", j);
2436
2437 if (in_child_chown())
2438 if (fchown(fd, arg_uid_shift, arg_uid_shift) < 0)
2439 return log_error_errno(errno, "Failed to adjust ownership of %s: %m", j);
2440 }
2441
2442 if (chmod(q, world_readable ? 0555 : 0500) < 0)
2443 return log_error_errno(errno, "Failed to adjust access mode of %s: %m", q);
2444
2445 r = userns_lchown(q, 0, 0);
2446 if (r < 0)
2447 return r;
2448
2449 /* Make both mount and superblock read-only now */
2450 r = mount_nofollow_verbose(LOG_ERR, NULL, q, NULL, MS_REMOUNT|MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
2451 if (r < 0)
2452 return r;
2453
2454 return mount_nofollow_verbose(LOG_ERR, NULL, q, NULL, MS_REMOUNT|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, "mode=0500");
2455 }
2456
2457 static int setup_kmsg_fifo(const char *directory) {
2458 _cleanup_free_ char *p = NULL;
2459
2460 assert(directory);
2461
2462 p = path_join(directory, "/run/host/proc-kmsg");
2463 if (!p)
2464 return log_oom();
2465
2466 BLOCK_WITH_UMASK(0000);
2467
2468 if (mkfifo(p, 0600) < 0)
2469 return log_error_errno(errno, "mkfifo() for /run/host/proc-kmsg failed: %m");
2470
2471 return userns_lchown(p, 0, 0);
2472 }
2473
2474 static int setup_kmsg(int fd_inner_socket) {
2475 _cleanup_close_ int fd = -EBADF;
2476 int r;
2477
2478 assert(fd_inner_socket >= 0);
2479
2480 /* We bind mount the kmsg FIFO (created in the outer child) to /proc/kmsg. While FIFOs on the reading
2481 * side behave very similar to /proc/kmsg, their writing side behaves differently from /dev/kmsg in
2482 * that writing blocks when nothing is reading. In order to avoid any problems with containers
2483 * deadlocking due to this we simply make /dev/kmsg unavailable to the container. */
2484
2485 r = mount_nofollow_verbose(LOG_ERR, "/run/host/proc-kmsg", "/proc/kmsg", NULL, MS_BIND, NULL);
2486 if (r < 0)
2487 return r;
2488
2489 fd = open("/run/host/proc-kmsg", O_RDWR|O_NONBLOCK|O_CLOEXEC);
2490 if (fd < 0)
2491 return log_error_errno(errno, "Failed to open fifo: %m");
2492
2493 /* NB: We intentionally do not unlink the backing FIFO. See setup_boot_id_file() for details. */
2494
2495 /* Store away the fd in the socket, so that it stays open as long as we run the child */
2496 r = send_one_fd(fd_inner_socket, fd, 0);
2497 if (r < 0)
2498 return log_error_errno(r, "Failed to send FIFO fd: %m");
2499
2500 return 0;
2501 }
2502
2503 struct ExposeArgs {
2504 union in_addr_union address4;
2505 union in_addr_union address6;
2506 sd_netlink *nfnl;
2507 };
2508
2509 static int on_address_change(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
2510 struct ExposeArgs *args = ASSERT_PTR(userdata);
2511
2512 assert(rtnl);
2513 assert(m);
2514
2515 (void) expose_port_execute(rtnl, args->nfnl, arg_expose_ports, AF_INET, &args->address4);
2516 (void) expose_port_execute(rtnl, args->nfnl, arg_expose_ports, AF_INET6, &args->address6);
2517 return 0;
2518 }
2519
2520 static int setup_hostname(void) {
2521 int r;
2522
2523 if ((arg_clone_ns_flags & CLONE_NEWUTS) == 0)
2524 return 0;
2525
2526 r = sethostname_idempotent(arg_hostname ?: arg_machine);
2527 if (r < 0)
2528 return log_error_errno(r, "Failed to set hostname: %m");
2529
2530 return 0;
2531 }
2532
2533 static int setup_journal(const char *directory, uid_t uid_shift, uid_t uid_range) {
2534 _cleanup_free_ char *d = NULL;
2535 sd_id128_t this_id;
2536 bool try;
2537 int r;
2538
2539 /* Don't link journals in ephemeral mode */
2540 if (arg_ephemeral)
2541 return 0;
2542
2543 if (arg_link_journal == LINK_NO)
2544 return 0;
2545
2546 try = arg_link_journal_try || arg_link_journal == LINK_AUTO;
2547
2548 r = sd_id128_get_machine(&this_id);
2549 if (r < 0)
2550 return log_error_errno(r, "Failed to retrieve machine ID: %m");
2551
2552 if (sd_id128_equal(arg_uuid, this_id)) {
2553 log_full(try ? LOG_WARNING : LOG_ERR,
2554 "Host and machine ids are equal (%s): refusing to link journals", SD_ID128_TO_STRING(arg_uuid));
2555 if (try)
2556 return 0;
2557 return -EEXIST;
2558 }
2559
2560 FOREACH_STRING(dirname, "/var", "/var/log", "/var/log/journal") {
2561 r = userns_mkdir(directory, dirname, 0755, 0, 0);
2562 if (r < 0) {
2563 bool ignore = r == -EROFS && try;
2564 log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, r,
2565 "Failed to create %s%s: %m", dirname, ignore ? ", ignoring" : "");
2566 return ignore ? 0 : r;
2567 }
2568 }
2569
2570 _cleanup_free_ char *p = path_join("/var/log/journal/", SD_ID128_TO_STRING(arg_uuid));
2571 if (!p)
2572 return log_oom();
2573
2574 _cleanup_free_ char *q = path_join(directory, p);
2575 if (!q)
2576 return log_oom();
2577
2578 if (path_is_mount_point(p) > 0) {
2579 if (try)
2580 return 0;
2581
2582 return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
2583 "%s: already a mount point, refusing to use for journal", p);
2584 }
2585
2586 if (path_is_mount_point(q) > 0) {
2587 if (try)
2588 return 0;
2589
2590 return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
2591 "%s: already a mount point, refusing to use for journal", q);
2592 }
2593
2594 r = readlink_and_make_absolute(p, &d);
2595 if (r >= 0) {
2596 if (IN_SET(arg_link_journal, LINK_GUEST, LINK_AUTO) &&
2597 path_equal(d, q)) {
2598
2599 r = userns_mkdir(directory, p, 0755, 0, 0);
2600 if (r < 0)
2601 log_warning_errno(r, "Failed to create directory %s: %m", q);
2602 return 0;
2603 }
2604
2605 if (unlink(p) < 0)
2606 return log_error_errno(errno, "Failed to remove symlink %s: %m", p);
2607 } else if (r == -EINVAL) {
2608
2609 if (arg_link_journal == LINK_GUEST &&
2610 rmdir(p) < 0) {
2611
2612 if (errno == ENOTDIR) {
2613 log_error("%s already exists and is neither a symlink nor a directory", p);
2614 return r;
2615 } else
2616 return log_error_errno(errno, "Failed to remove %s: %m", p);
2617 }
2618 } else if (r != -ENOENT)
2619 return log_error_errno(r, "readlink(%s) failed: %m", p);
2620
2621 if (arg_link_journal == LINK_GUEST) {
2622
2623 if (symlink(q, p) < 0) {
2624 if (try) {
2625 log_debug_errno(errno, "Failed to symlink %s to %s, skipping journal setup: %m", q, p);
2626 return 0;
2627 } else
2628 return log_error_errno(errno, "Failed to symlink %s to %s: %m", q, p);
2629 }
2630
2631 r = userns_mkdir(directory, p, 0755, 0, 0);
2632 if (r < 0)
2633 log_warning_errno(r, "Failed to create directory %s: %m", q);
2634 return 0;
2635 }
2636
2637 if (arg_link_journal == LINK_HOST) {
2638 /* don't create parents here — if the host doesn't have
2639 * permanent journal set up, don't force it here */
2640
2641 r = RET_NERRNO(mkdir(p, 0755));
2642 if (r < 0 && r != -EEXIST) {
2643 if (try) {
2644 log_debug_errno(r, "Failed to create %s, skipping journal setup: %m", p);
2645 return 0;
2646 } else
2647 return log_error_errno(r, "Failed to create %s: %m", p);
2648 }
2649
2650 } else if (access(p, F_OK) < 0)
2651 return 0;
2652
2653 if (dir_is_empty(q, /* ignore_hidden_or_backup= */ false) == 0)
2654 log_warning("%s is not empty, proceeding anyway.", q);
2655
2656 r = userns_mkdir(directory, p, 0755, 0, 0);
2657 if (r < 0)
2658 return log_error_errno(r, "Failed to create %s: %m", q);
2659
2660 return mount_custom(
2661 directory,
2662 &(CustomMount) {
2663 .type = CUSTOM_MOUNT_BIND,
2664 .options = (char*) (uid_is_valid(uid_shift) ? "rootidmap" : NULL),
2665 .source = p,
2666 .destination = p,
2667 .destination_uid = UID_INVALID,
2668 },
2669 /* n= */ 1,
2670 uid_shift,
2671 uid_range,
2672 arg_selinux_apifs_context,
2673 MOUNT_NON_ROOT_ONLY);
2674 }
2675
2676 static int drop_capabilities(uid_t uid) {
2677 CapabilityQuintet q;
2678
2679 /* Let's initialize all five capability sets to something valid. If the quintet was configured via
2680 * OCI use that, but fill in missing bits. If it wasn't then derive the quintet in full from
2681 * arg_caps_retain. */
2682
2683 if (capability_quintet_is_set(&arg_full_capabilities)) {
2684 q = arg_full_capabilities;
2685
2686 if (q.bounding == UINT64_MAX)
2687 q.bounding = uid == 0 ? arg_caps_retain : 0;
2688
2689 if (q.effective == UINT64_MAX)
2690 q.effective = uid == 0 ? q.bounding : 0;
2691
2692 if (q.inheritable == UINT64_MAX)
2693 q.inheritable = uid == 0 ? q.bounding : arg_caps_ambient;
2694
2695 if (q.permitted == UINT64_MAX)
2696 q.permitted = uid == 0 ? q.bounding : arg_caps_ambient;
2697
2698 if (q.ambient == UINT64_MAX)
2699 q.ambient = arg_caps_ambient;
2700
2701 if (capability_quintet_mangle(&q))
2702 return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Cannot set capabilities that are not in the current bounding set.");
2703
2704 } else {
2705 q = (CapabilityQuintet) {
2706 .bounding = arg_caps_retain,
2707 .effective = uid == 0 ? arg_caps_retain : 0,
2708 .inheritable = uid == 0 ? arg_caps_retain : arg_caps_ambient,
2709 .permitted = uid == 0 ? arg_caps_retain : arg_caps_ambient,
2710 .ambient = arg_caps_ambient,
2711 };
2712
2713 /* If we're not using OCI, proceed with mangled capabilities (so we don't error out)
2714 * in order to maintain the same behavior as systemd < 242. */
2715 if (capability_quintet_mangle(&q))
2716 log_full(arg_quiet ? LOG_DEBUG : LOG_WARNING,
2717 "Some capabilities will not be set because they are not in the current bounding set.");
2718
2719 }
2720
2721 return capability_quintet_enforce(&q);
2722 }
2723
2724 static int reset_audit_loginuid(void) {
2725 _cleanup_free_ char *p = NULL;
2726 int r;
2727
2728 if ((arg_clone_ns_flags & CLONE_NEWPID) == 0)
2729 return 0;
2730
2731 /* if we are in managed userns mode, then we are already in our userns, hence we cannot reset the
2732 * loginuid anyway, hence don't bother */
2733 if (arg_userns_mode == USER_NAMESPACE_MANAGED)
2734 return 0;
2735
2736 r = read_virtual_file("/proc/self/loginuid", SIZE_MAX, &p, /* ret_size= */ NULL);
2737 if (r == -ENOENT)
2738 return 0;
2739 if (r < 0)
2740 return log_error_errno(r, "Failed to read /proc/self/loginuid: %m");
2741
2742 /* Already reset? */
2743 if (streq(p, "4294967295"))
2744 return 0;
2745
2746 r = write_string_file("/proc/self/loginuid", "4294967295", WRITE_STRING_FILE_DISABLE_BUFFER);
2747 if (r < 0)
2748 return log_error_errno(r, "Failed to reset audit login UID: %m");
2749
2750 return 0;
2751 }
2752
2753 static int mount_tunnel_dig(const char *root) {
2754 int r;
2755
2756 if (arg_userns_mode == USER_NAMESPACE_MANAGED) {
2757 log_debug("Not digging mount tunnel, because running in managed user namespace mode.");
2758 return 0;
2759 }
2760
2761 (void) mkdir_p("/run/systemd/nspawn/", 0755);
2762 (void) mkdir_p("/run/systemd/nspawn/propagate", 0600);
2763 _cleanup_free_ char *p = path_join("/run/systemd/nspawn/propagate/", arg_machine);
2764 if (!p)
2765 return log_oom();
2766
2767 (void) mkdir_p(p, 0600);
2768
2769 r = make_run_host(root);
2770 if (r < 0)
2771 return r;
2772
2773 r = userns_mkdir(root, NSPAWN_MOUNT_TUNNEL, 0600, 0, 0);
2774 if (r < 0)
2775 return log_error_errno(r, "Failed to create "NSPAWN_MOUNT_TUNNEL": %m");
2776
2777 _cleanup_free_ char *q = path_join(root, NSPAWN_MOUNT_TUNNEL);
2778 if (!q)
2779 return log_oom();
2780
2781 r = mount_nofollow_verbose(LOG_ERR, p, q, NULL, MS_BIND, NULL);
2782 if (r < 0)
2783 return r;
2784
2785 r = mount_nofollow_verbose(LOG_ERR, NULL, q, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL);
2786 if (r < 0)
2787 return r;
2788
2789 return 0;
2790 }
2791
2792 static int mount_tunnel_open(void) {
2793 int r;
2794
2795 if (arg_userns_mode == USER_NAMESPACE_MANAGED) {
2796 log_debug("Not opening up mount tunnel, because running in managed user namespace mode.");
2797 return 0;
2798 }
2799
2800 r = mount_follow_verbose(LOG_ERR, NULL, NSPAWN_MOUNT_TUNNEL, NULL, MS_SLAVE, NULL);
2801 if (r < 0)
2802 return r;
2803
2804 return 0;
2805 }
2806
2807 static int setup_machine_id(const char *directory) {
2808 int r;
2809
2810 /* If the UUID in the container is already set, then that's what counts, and we use. If it isn't set, and the
2811 * caller passed --uuid=, then we'll pass it in the $container_uuid env var to PID 1 of the container. The
2812 * assumption is that PID 1 will then write it to /etc/machine-id to make it persistent. If --uuid= is not
2813 * passed we generate a random UUID, and pass it via $container_uuid. In effect this means that /etc/machine-id
2814 * in the container and our idea of the container UUID will always be in sync (at least if PID 1 in the
2815 * container behaves nicely). */
2816
2817 r = id128_get_machine(directory, &arg_uuid);
2818 if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r)) {
2819 /* If the file is missing, empty, or uninitialized, we don't mind */
2820 if (sd_id128_is_null(arg_uuid)) {
2821 r = sd_id128_randomize(&arg_uuid);
2822 if (r < 0)
2823 return log_error_errno(r, "Failed to acquire randomized machine UUID: %m");
2824 }
2825 } else if (r < 0)
2826 return log_error_errno(r, "Failed to read machine ID from container image: %m");
2827
2828 return 0;
2829 }
2830
2831 static int setup_varlink_socket(const char *directory, const char *name) {
2832 int r;
2833
2834 assert(directory);
2835
2836 if (arg_delegate_container_ranges == 0)
2837 return 0;
2838
2839 r = make_run_host(directory);
2840 if (r < 0)
2841 return r;
2842
2843 _cleanup_free_ char *src = path_join("/run/systemd", name);
2844 if (!src)
2845 return log_oom();
2846
2847 _cleanup_free_ char *dest = path_join(directory, "/run/host", name);
2848 if (!dest)
2849 return log_oom();
2850
2851 r = touch(dest);
2852 if (r < 0)
2853 return log_error_errno(r, "Failed to create %s: %m", dest);
2854
2855 r = userns_lchown(dest, 0, 0);
2856 if (r < 0)
2857 return log_error_errno(r, "Failed to chown %s: %m", dest);
2858
2859 return mount_nofollow_verbose(
2860 LOG_ERR,
2861 src,
2862 dest,
2863 /* fstype= */ NULL,
2864 MS_BIND|MS_RDONLY,
2865 /* options= */ NULL);
2866 }
2867
2868 static int recursive_chown(const char *directory, uid_t shift, uid_t range) {
2869 int r;
2870
2871 assert(directory);
2872
2873 if (arg_userns_mode == USER_NAMESPACE_NO || arg_userns_ownership != USER_NAMESPACE_OWNERSHIP_CHOWN)
2874 return 0;
2875
2876 r = path_patch_uid(directory, arg_uid_shift, arg_uid_range);
2877 if (r == -EOPNOTSUPP)
2878 return log_error_errno(r, "Automatic UID/GID adjusting is only supported for UID/GID ranges starting at multiples of 2^16 with a range of 2^16.");
2879 if (r == -EBADE)
2880 return log_error_errno(r, "Upper 16 bits of root directory UID and GID do not match.");
2881 if (r < 0)
2882 return log_error_errno(r, "Failed to adjust UID/GID shift of OS tree: %m");
2883 if (r == 0)
2884 log_debug("Root directory of image is already owned by the right UID/GID range, skipping recursive chown operation.");
2885 else
2886 log_debug("Patched directory tree to match UID/GID range.");
2887
2888 return r;
2889 }
2890
2891 /*
2892 * Return values:
2893 * < 0 : pidref_wait_for_terminate() failed to get the state of the
2894 * container, the container was terminated by a signal, or
2895 * failed for an unknown reason. No change is made to the
2896 * container argument.
2897 * > 0 : The program executed in the container terminated with an
2898 * error. The exit code of the program executed in the
2899 * container is returned. The container argument has been set
2900 * to CONTAINER_TERMINATED.
2901 * 0 : The container is being rebooted, has been shut down or exited
2902 * successfully. The container argument has been set to either
2903 * CONTAINER_TERMINATED or CONTAINER_REBOOTED.
2904 *
2905 * That is, success is indicated by a return value of zero, and an
2906 * error is indicated by a non-zero value.
2907 */
2908 static int wait_for_container(PidRef *pid, ContainerStatus *container) {
2909 siginfo_t status;
2910 int r;
2911
2912 assert(pidref_is_set(pid));
2913 assert(container);
2914
2915 r = pidref_wait_for_terminate(pid, &status);
2916 if (r < 0)
2917 return log_warning_errno(r, "Failed to wait for container: %m");
2918
2919 pidref_done(pid);
2920
2921 switch (status.si_code) {
2922
2923 case CLD_EXITED:
2924 if (status.si_status == 0)
2925 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s exited successfully.", arg_machine);
2926 else
2927 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s failed with error code %i.", arg_machine, status.si_status);
2928
2929 *container = CONTAINER_TERMINATED;
2930 return status.si_status;
2931
2932 case CLD_KILLED:
2933 if (status.si_status == SIGINT) {
2934 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s has been shut down.", arg_machine);
2935 *container = CONTAINER_TERMINATED;
2936 return 0;
2937
2938 } else if (status.si_status == SIGHUP) {
2939 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s is being rebooted.", arg_machine);
2940 *container = CONTAINER_REBOOTED;
2941 return 0;
2942 }
2943
2944 _fallthrough_;
2945 case CLD_DUMPED:
2946 return log_error_errno(SYNTHETIC_ERRNO(EIO),
2947 "Container %s terminated by signal %s.", arg_machine, signal_to_string(status.si_status));
2948
2949 default:
2950 return log_error_errno(SYNTHETIC_ERRNO(EIO),
2951 "Container %s failed due to unknown reason.", arg_machine);
2952 }
2953 }
2954
2955 static int on_orderly_shutdown(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
2956 PidRef *pid = userdata;
2957
2958 assert(si);
2959
2960 if (pidref_is_set(pid))
2961 if (pidref_kill(pid, arg_kill_signal) >= 0) {
2962 log_info("Trying to halt container by sending %s to container PID 1. Send SIGTERM again to trigger immediate termination.",
2963 signal_to_string(si->ssi_signo));
2964 sd_event_source_set_userdata(s, NULL);
2965 sd_notify(/* unset_environment= */ false, NOTIFY_STOPPING_MESSAGE);
2966 return 0;
2967 }
2968
2969 log_debug("Got %s, exiting.", signal_to_string(si->ssi_signo));
2970
2971 sd_event_exit(sd_event_source_get_event(s), 0);
2972 return 0;
2973 }
2974
2975 static int on_sigchld(sd_event_source *s, const struct signalfd_siginfo *ssi, void *userdata) {
2976
2977 assert(s);
2978 assert(ssi);
2979
2980 PidRef *pid = ASSERT_PTR(userdata);
2981 for (;;) {
2982 siginfo_t si = {};
2983
2984 if (waitid(P_ALL, 0, &si, WNOHANG|WNOWAIT|WEXITED) < 0)
2985 return log_error_errno(errno, "Failed to waitid(): %m");
2986 if (si.si_pid == 0) /* No pending children. */
2987 break;
2988 if (si.si_pid == pid->pid) {
2989 /* The main process we care for has exited. Return from
2990 * signal handler but leave the zombie. */
2991 sd_event_exit(sd_event_source_get_event(s), 0);
2992 break;
2993 }
2994
2995 /* Reap all other children. */
2996 (void) waitid(P_PID, si.si_pid, &si, WNOHANG|WEXITED);
2997 }
2998
2999 return 0;
3000 }
3001
3002 static int on_request_stop(sd_bus_message *m, void *userdata, sd_bus_error *error) {
3003 PidRef *pid = ASSERT_PTR(userdata);
3004
3005 assert(m);
3006
3007 if (arg_kill_signal > 0) {
3008 log_info("Container termination requested. Attempting to halt container.");
3009 (void) pidref_kill(pid, arg_kill_signal);
3010 } else {
3011 log_info("Container termination requested. Exiting.");
3012 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), 0);
3013 }
3014
3015 return 0;
3016 }
3017
3018 static int pick_paths(void) {
3019 int r;
3020
3021 if (arg_directory) {
3022 _cleanup_(pick_result_done) PickResult result = PICK_RESULT_NULL;
3023 PickFilter filter = *pick_filter_image_dir;
3024
3025 filter.architecture = arg_architecture;
3026
3027 r = path_pick_update_warn(
3028 &arg_directory,
3029 &filter,
3030 /* n_filters= */ 1,
3031 PICK_ARCHITECTURE|PICK_TRIES,
3032 &result);
3033 if (r < 0) {
3034 /* Accept ENOENT here so that the --template= logic can work */
3035 if (r != -ENOENT)
3036 return r;
3037 } else
3038 arg_architecture = result.architecture;
3039 }
3040
3041 if (arg_image) {
3042 _cleanup_(pick_result_done) PickResult result = PICK_RESULT_NULL;
3043 PickFilter filter = *pick_filter_image_raw;
3044
3045 filter.architecture = arg_architecture;
3046
3047 r = path_pick_update_warn(
3048 &arg_image,
3049 &filter,
3050 /* n_filters= */ 1,
3051 PICK_ARCHITECTURE|PICK_TRIES,
3052 &result);
3053 if (r < 0)
3054 return r;
3055
3056 arg_architecture = result.architecture;
3057 }
3058
3059 if (arg_mstack) {
3060 _cleanup_(pick_result_done) PickResult result = PICK_RESULT_NULL;
3061 PickFilter filter = *pick_filter_image_mstack;
3062
3063 filter.architecture = arg_architecture;
3064
3065 r = path_pick_update_warn(
3066 &arg_mstack,
3067 &filter,
3068 /* n_filters= */ 1,
3069 PICK_ARCHITECTURE|PICK_TRIES,
3070 &result);
3071 if (r < 0)
3072 return r;
3073
3074 arg_architecture = result.architecture;
3075 }
3076
3077 if (arg_template) {
3078 _cleanup_(pick_result_done) PickResult result = PICK_RESULT_NULL;
3079 PickFilter filter = *pick_filter_image_dir;
3080
3081 filter.architecture = arg_architecture;
3082
3083 r = path_pick_update_warn(
3084 &arg_template,
3085 &filter,
3086 /* n_filters= */ 1,
3087 PICK_ARCHITECTURE,
3088 &result);
3089 if (r < 0)
3090 return r;
3091
3092 arg_architecture = result.architecture;
3093 }
3094
3095 return 0;
3096 }
3097
3098 static int determine_names(void) {
3099 int r;
3100
3101 if (arg_template && !arg_directory && arg_machine) {
3102
3103 /* If --template= was specified then we should not search for a machine, but instead create a
3104 * new one in /var/lib/machine. */
3105
3106 arg_directory = path_join("/var/lib/machines", arg_machine);
3107 if (!arg_directory)
3108 return log_oom();
3109 }
3110
3111 if (!arg_image && !arg_directory && !arg_mstack) {
3112 if (arg_machine) {
3113 _cleanup_(image_unrefp) Image *i = NULL;
3114
3115 r = image_find(arg_runtime_scope,
3116 IMAGE_MACHINE, arg_machine, NULL, &i);
3117 if (r == -ENOENT)
3118 return log_error_errno(r, "No image for machine '%s'.", arg_machine);
3119 if (r < 0)
3120 return log_error_errno(r, "Failed to find image for machine '%s': %m", arg_machine);
3121
3122 switch (i->type) {
3123 case IMAGE_RAW:
3124 case IMAGE_BLOCK:
3125 r = free_and_strdup(&arg_image, i->path);
3126 break;
3127
3128 case IMAGE_DIRECTORY:
3129 case IMAGE_SUBVOLUME:
3130 r = free_and_strdup(&arg_directory, i->path);
3131 break;
3132
3133 case IMAGE_MSTACK:
3134 r = free_and_strdup(&arg_mstack, i->path);
3135 break;
3136
3137 default:
3138 assert_not_reached();
3139 }
3140 if (r < 0)
3141 return log_oom();
3142
3143 if (!arg_ephemeral)
3144 arg_read_only = arg_read_only || image_is_read_only(i);
3145 } else {
3146 r = safe_getcwd(&arg_directory);
3147 if (r < 0)
3148 return log_error_errno(r, "Failed to determine current directory: %m");
3149 }
3150
3151 if (!arg_directory && !arg_image && !arg_mstack)
3152 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine path, please use --directory=, --image= or --mstack=.");
3153 }
3154
3155 if (!arg_machine) {
3156 if (arg_directory) {
3157 if (path_equal(arg_directory, "/")) {
3158 arg_machine = gethostname_malloc();
3159 if (!arg_machine)
3160 return log_oom();
3161 } else {
3162 r = path_extract_filename(arg_directory, &arg_machine);
3163 if (r < 0)
3164 return log_error_errno(r, "Failed to extract file name from '%s': %m", arg_directory);
3165 }
3166 } else if (arg_image) {
3167 r = path_extract_filename(arg_image, &arg_machine);
3168 if (r < 0)
3169 return log_error_errno(r, "Failed to extract file name from '%s': %m", arg_image);
3170
3171 /* Truncate suffix if there is one */
3172 char *e = endswith(arg_machine, ".raw");
3173 if (e)
3174 *e = 0;
3175 } else if (arg_mstack) {
3176 r = path_extract_filename(arg_mstack, &arg_machine);
3177 if (r < 0)
3178 return log_error_errno(r, "Failed to extract file name from '%s': %m", arg_mstack);
3179
3180 char *e = endswith(arg_machine, ".mstack");
3181 if (e)
3182 *e = 0;
3183 } else
3184 assert_not_reached();
3185
3186 hostname_cleanup(arg_machine);
3187 if (!hostname_is_valid(arg_machine, 0))
3188 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine machine name automatically, please use -M.");
3189
3190 /* Copy the machine name before the random suffix is added below, otherwise we won't be able
3191 * to match fixed config file names. */
3192 arg_settings_filename = strjoin(arg_machine, ".nspawn");
3193 if (!arg_settings_filename)
3194 return log_oom();
3195
3196 /* Add a random suffix when this is an ephemeral machine, so that we can run many
3197 * instances at once without manually having to specify -M each time. */
3198 if (arg_ephemeral)
3199 if (strextendf(&arg_machine, "-%016" PRIx64, random_u64()) < 0)
3200 return log_oom();
3201 } else {
3202 arg_settings_filename = strjoin(arg_machine, ".nspawn");
3203 if (!arg_settings_filename)
3204 return log_oom();
3205 }
3206
3207 return 0;
3208 }
3209
3210 static int chase_and_update(char **p, ChaseFlags flags) {
3211 char *chased;
3212 int r;
3213
3214 assert(p);
3215
3216 if (!*p)
3217 return 0;
3218
3219 r = chase(*p, /* root= */ NULL, flags, &chased, /* ret_fd= */ NULL);
3220 if (r < 0)
3221 return log_error_errno(r, "Failed to resolve path %s: %m", *p);
3222
3223 return free_and_replace(*p, chased);
3224 }
3225
3226 static int determine_uid_shift(const char *directory) {
3227 assert(directory);
3228
3229 if (arg_userns_mode == USER_NAMESPACE_MANAGED) {
3230 /* In managed mode we should already know the UID shift */
3231 assert(uid_is_valid(arg_uid_shift));
3232 return 0;
3233 }
3234
3235 if (arg_userns_mode == USER_NAMESPACE_NO) {
3236 arg_uid_shift = 0;
3237 return 0;
3238 }
3239
3240 if (arg_uid_shift == UID_INVALID) {
3241 struct stat st;
3242
3243 /* Read the UID shift off the image. Maybe we can reuse this to avoid chowning. */
3244
3245 if (stat(directory, &st) < 0)
3246 return log_error_errno(errno, "Failed to determine UID base of %s: %m", directory);
3247
3248 arg_uid_shift = st.st_uid & UINT32_C(0xffff0000);
3249
3250 if (arg_uid_shift != (st.st_gid & UINT32_C(0xffff0000)))
3251 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3252 "UID and GID base of %s don't match.", directory);
3253
3254 arg_uid_range = UINT32_C(0x10000);
3255
3256 if (arg_uid_shift != 0) {
3257 /* If the image is shifted already, then we'll fall back to classic chowning, for
3258 * compatibility (and simplicity), or refuse if mapping is explicitly requested. */
3259
3260 if (arg_userns_ownership == USER_NAMESPACE_OWNERSHIP_AUTO) {
3261 log_debug("UID base of %s is non-zero, not using UID mapping.", directory);
3262 arg_userns_ownership = USER_NAMESPACE_OWNERSHIP_CHOWN;
3263 } else if (arg_userns_ownership == USER_NAMESPACE_OWNERSHIP_MAP)
3264 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3265 "UID base of %s is not zero, UID mapping not supported.", directory);
3266 }
3267 }
3268
3269 if (!userns_shift_range_valid(arg_uid_shift, arg_uid_range))
3270 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "UID base too high for UID range.");
3271
3272 return 0;
3273 }
3274
3275 static unsigned long effective_clone_ns_flags(void) {
3276 unsigned long flags = arg_clone_ns_flags;
3277
3278 if (arg_private_network)
3279 flags |= CLONE_NEWNET;
3280 if (arg_use_cgns)
3281 flags |= CLONE_NEWCGROUP;
3282 if (arg_userns_mode != USER_NAMESPACE_NO)
3283 flags |= CLONE_NEWUSER;
3284
3285 return flags;
3286 }
3287
3288 static int patch_sysctl(void) {
3289
3290 /* This table is inspired by runc's sysctl() function */
3291 static const struct {
3292 const char *key;
3293 bool prefix;
3294 unsigned long clone_flags;
3295 } safe_sysctl[] = {
3296 { "kernel.hostname", false, CLONE_NEWUTS },
3297 { "kernel.domainname", false, CLONE_NEWUTS },
3298 { "kernel.msgmax", false, CLONE_NEWIPC },
3299 { "kernel.msgmnb", false, CLONE_NEWIPC },
3300 { "kernel.msgmni", false, CLONE_NEWIPC },
3301 { "kernel.sem", false, CLONE_NEWIPC },
3302 { "kernel.shmall", false, CLONE_NEWIPC },
3303 { "kernel.shmmax", false, CLONE_NEWIPC },
3304 { "kernel.shmmni", false, CLONE_NEWIPC },
3305 { "fs.mqueue.", true, CLONE_NEWIPC },
3306 { "net.", true, CLONE_NEWNET },
3307 };
3308
3309 unsigned long flags;
3310 int r;
3311
3312 flags = effective_clone_ns_flags();
3313
3314 STRV_FOREACH_PAIR(k, v, arg_sysctl) {
3315 bool good = false;
3316
3317 FOREACH_ELEMENT(i, safe_sysctl) {
3318 if (!FLAGS_SET(flags, i->clone_flags))
3319 continue;
3320
3321 if (i->prefix)
3322 good = startswith(*k, i->key);
3323 else
3324 good = streq(*k, i->key);
3325
3326 if (good)
3327 break;
3328 }
3329
3330 if (!good)
3331 return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Refusing to write to sysctl '%s', as it is not safe in the selected namespaces.", *k);
3332
3333 r = sysctl_write(*k, *v);
3334 if (r < 0)
3335 return log_error_errno(r, "Failed to write sysctl '%s': %m", *k);
3336 }
3337
3338 return 0;
3339 }
3340
3341 static int inner_child(
3342 Barrier *barrier,
3343 int fd_inner_socket,
3344 FDSet *fds,
3345 char **os_release_pairs) {
3346
3347 _cleanup_free_ char *home = NULL;
3348 size_t n_env = 1;
3349 char *envp[] = {
3350 (char*) "PATH=" DEFAULT_PATH_COMPAT,
3351 NULL, /* container */
3352 NULL, /* TERM */
3353 NULL, /* COLORTERM */
3354 NULL, /* NO_COLOR */
3355 NULL, /* HOME */
3356 NULL, /* USER */
3357 NULL, /* LOGNAME */
3358 NULL, /* container_uuid */
3359 NULL, /* LISTEN_FDS */
3360 NULL, /* LISTEN_PID */
3361 NULL, /* NOTIFY_SOCKET */
3362 NULL, /* CREDENTIALS_DIRECTORY */
3363 NULL, /* LANG */
3364 NULL
3365 };
3366 const char *exec_target;
3367 _cleanup_strv_free_ char **env_use = NULL;
3368 int r, which_failed;
3369
3370 /* This is the "inner" child process, i.e. the one forked off by the "outer" child process, which is the one
3371 * the container manager itself forked off. At the time of clone() it gained its own CLONE_NEWNS, CLONE_NEWPID,
3372 * CLONE_NEWUTS, CLONE_NEWIPC, CLONE_NEWUSER namespaces. Note that it has its own CLONE_NEWNS namespace,
3373 * separate from the CLONE_NEWNS created for the "outer" child, and also separate from the host's CLONE_NEWNS
3374 * namespace. The reason for having two levels of CLONE_NEWNS namespaces is that the "inner" one is owned by
3375 * the CLONE_NEWUSER namespace of the container, while the "outer" one is owned by the host's CLONE_NEWUSER
3376 * namespace.
3377 *
3378 * Note at this point we have no CLONE_NEWNET namespace yet. We'll acquire that one later through
3379 * unshare(). See below. */
3380
3381 assert(barrier);
3382 assert(fd_inner_socket >= 0);
3383
3384 log_debug("Inner child is initializing.");
3385
3386 if (arg_userns_mode != USER_NAMESPACE_NO) {
3387 /* Tell the parent, that it now can write the UID map. */
3388 (void) barrier_place(barrier); /* #1 */
3389
3390 /* Wait until the parent wrote the UID map */
3391 if (!barrier_place_and_sync(barrier)) /* #2 */
3392 return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Parent died too early");
3393
3394 /* Become the new root user inside our namespace */
3395 r = reset_uid_gid();
3396 if (r < 0)
3397 return log_error_errno(r, "Couldn't become new root: %m");
3398
3399 /* Creating a new user namespace means all MS_SHARED mounts become MS_SLAVE. Let's put them
3400 * back to MS_SHARED here, since that's what we want as defaults. (This will not reconnect
3401 * propagation, but simply create new peer groups for all our mounts). */
3402 r = mount_follow_verbose(LOG_ERR, NULL, "/", NULL, MS_SHARED|MS_REC, NULL);
3403 if (r < 0)
3404 return r;
3405 }
3406
3407 r = mount_all(/* dest= */ NULL,
3408 arg_mount_settings | MOUNT_IN_USERNS,
3409 arg_uid_shift,
3410 arg_selinux_apifs_context);
3411 if (r < 0)
3412 return r;
3413
3414 if (!arg_network_namespace_path && arg_private_network) {
3415 _cleanup_close_ int netns_fd = -EBADF;
3416
3417 if (arg_userns_mode != USER_NAMESPACE_MANAGED)
3418 if (unshare(CLONE_NEWNET) < 0)
3419 return log_error_errno(errno, "Failed to unshare network namespace: %m");
3420
3421 netns_fd = namespace_open_by_type(NAMESPACE_NET);
3422 if (netns_fd < 0)
3423 return log_error_errno(netns_fd, "Failed to open newly allocate network namespace: %m");
3424
3425 r = send_one_fd(fd_inner_socket, netns_fd, 0);
3426 if (r < 0)
3427 return log_error_errno(r, "Failed to send network namespace to supervisor: %m");
3428
3429 /* Tell the parent that it can setup network interfaces. */
3430 (void) barrier_place(barrier); /* #3 */
3431 }
3432
3433 if (arg_userns_mode != USER_NAMESPACE_MANAGED) {
3434 r = mount_sysfs(NULL, arg_mount_settings | MOUNT_IN_USERNS);
3435 if (r < 0)
3436 return r;
3437 }
3438
3439 /* Wait until we are cgroup-ified, so that we can mount the right cgroup path writable */
3440 if (!barrier_place_and_sync(barrier)) /* #4 */
3441 return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
3442 "Parent died too early");
3443
3444 if (arg_use_cgns) {
3445 r = unshare(CLONE_NEWCGROUP);
3446 if (r < 0)
3447 return log_error_errno(errno, "Failed to unshare cgroup namespace: %m");
3448
3449 r = mount_cgroups(/* dest= */ NULL, /* accept_existing= */ false);
3450 } else
3451 r = bind_mount_cgroup_hierarchy();
3452 if (r < 0)
3453 return r;
3454
3455 r = setup_boot_id();
3456 if (r < 0)
3457 return r;
3458
3459 r = setup_kmsg(fd_inner_socket);
3460 if (r < 0)
3461 return r;
3462
3463 r = mount_custom(
3464 "/",
3465 arg_custom_mounts,
3466 arg_n_custom_mounts,
3467 0,
3468 0,
3469 arg_selinux_apifs_context,
3470 MOUNT_NON_ROOT_ONLY | MOUNT_IN_USERNS);
3471 if (r < 0)
3472 return r;
3473
3474 if (setsid() < 0)
3475 return log_error_errno(errno, "setsid() failed: %m");
3476
3477 if (arg_private_network)
3478 (void) loopback_setup();
3479
3480 if (arg_expose_ports) {
3481 r = expose_port_send_rtnl(fd_inner_socket);
3482 if (r < 0)
3483 return r;
3484 }
3485
3486 if (arg_console_mode != CONSOLE_PIPE) {
3487 _cleanup_close_ int master = -EBADF;
3488 _cleanup_free_ char *console = NULL;
3489
3490 /* Allocate a pty and make it available as /dev/console. */
3491 master = openpt_allocate(O_RDWR|O_NONBLOCK, &console);
3492 if (master < 0)
3493 return log_error_errno(master, "Failed to allocate a pty: %m");
3494
3495 r = setup_dev_console(console);
3496 if (r < 0)
3497 return log_error_errno(r, "Failed to set up /dev/console: %m");
3498
3499 r = send_one_fd(fd_inner_socket, master, 0);
3500 if (r < 0)
3501 return log_error_errno(r, "Failed to send master fd: %m");
3502
3503 r = setup_stdio_as_dev_console();
3504 if (r < 0)
3505 return r;
3506 }
3507
3508 r = patch_sysctl();
3509 if (r < 0)
3510 return r;
3511
3512 if (arg_oom_score_adjust_set) {
3513 r = set_oom_score_adjust(arg_oom_score_adjust);
3514 if (r < 0)
3515 return log_error_errno(r, "Failed to adjust OOM score: %m");
3516 }
3517
3518 if (arg_cpu_set.set)
3519 if (sched_setaffinity(0, arg_cpu_set.allocated, arg_cpu_set.set) < 0)
3520 return log_error_errno(errno, "Failed to set CPU affinity: %m");
3521
3522 (void) setup_hostname();
3523
3524 if (arg_personality != PERSONALITY_INVALID) {
3525 r = safe_personality(arg_personality);
3526 if (r < 0)
3527 return log_error_errno(r, "personality() failed: %m");
3528 #ifdef ARCHITECTURE_SECONDARY
3529 } else if (arg_architecture == ARCHITECTURE_SECONDARY) {
3530 r = safe_personality(PER_LINUX32);
3531 if (r < 0)
3532 return log_error_errno(r, "personality() failed: %m");
3533 #endif
3534 } else if (!arg_quiet && arg_architecture >= 0 && arg_architecture != native_architecture())
3535 log_notice("Selected architecture '%s' not supported natively on the local CPU, assuming "
3536 "invocation with qemu userspace emulator (or equivalent) in effect.",
3537 architecture_to_string(arg_architecture));
3538
3539 r = setrlimit_closest_all((const struct rlimit *const*) arg_rlimit, &which_failed);
3540 if (r < 0)
3541 return log_error_errno(r, "Failed to apply resource limit RLIMIT_%s: %m", rlimit_to_string(which_failed));
3542
3543 #if HAVE_SECCOMP
3544 if (arg_seccomp) {
3545
3546 if (is_seccomp_available()) {
3547 r = sym_seccomp_load(arg_seccomp);
3548 if (ERRNO_IS_NEG_SECCOMP_FATAL(r))
3549 return log_error_errno(r, "Failed to install seccomp filter: %m");
3550 if (r < 0)
3551 log_debug_errno(r, "Failed to install seccomp filter: %m");
3552 }
3553 } else
3554 #endif
3555 {
3556 r = setup_seccomp(arg_caps_retain, arg_syscall_allow_list, arg_syscall_deny_list, arg_restrict_address_families, arg_restrict_address_families_is_allowlist);
3557 if (r < 0)
3558 return r;
3559 }
3560
3561 if (arg_suppress_sync) {
3562 #if HAVE_SECCOMP
3563 r = seccomp_suppress_sync();
3564 if (r < 0)
3565 log_debug_errno(r, "Failed to install sync() suppression seccomp filter, ignoring: %m");
3566 #else
3567 log_debug("systemd is built without SECCOMP support. Ignoring --suppress-sync= command line option and SuppressSync= setting.");
3568 #endif
3569 }
3570
3571 #if HAVE_SELINUX
3572 if (arg_selinux_context && mac_selinux_use())
3573 if (sym_setexeccon_raw(arg_selinux_context) < 0)
3574 return log_error_errno(errno, "setexeccon(\"%s\") failed: %m", arg_selinux_context);
3575 #endif
3576
3577 /* Make sure we keep the caps across the uid/gid dropping, so that we can retain some selected caps
3578 * if we need to later on. */
3579 if (prctl(PR_SET_KEEPCAPS, 1) < 0)
3580 return log_error_errno(errno, "Failed to set PR_SET_KEEPCAPS: %m");
3581
3582 if (uid_is_valid(arg_uid) || gid_is_valid(arg_gid))
3583 r = change_uid_gid_raw(arg_uid, arg_gid, arg_supplementary_gids, arg_n_supplementary_gids, arg_console_mode != CONSOLE_PIPE);
3584 else
3585 r = change_uid_gid(arg_user, arg_console_mode != CONSOLE_PIPE, &home);
3586 if (r < 0)
3587 return r;
3588
3589 r = drop_capabilities(getuid());
3590 if (r < 0)
3591 return log_error_errno(r, "Dropping capabilities failed: %m");
3592
3593 if (arg_no_new_privileges)
3594 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
3595 return log_error_errno(errno, "Failed to disable new privileges: %m");
3596
3597 /* LXC sets container=lxc, so follow the scheme here */
3598 envp[n_env++] = strjoina("container=", arg_container_service_name);
3599
3600 /* Propagate $TERM & Co. unless we are invoked in pipe mode and stdin/stdout/stderr don't refer to a TTY */
3601 if (arg_console_mode != CONSOLE_PIPE && !terminal_is_dumb())
3602 FOREACH_STRING(v, "TERM=", "COLORTERM=", "NO_COLOR=") {
3603 char *t = strv_find_prefix(environ, v);
3604 if (!t)
3605 continue;
3606
3607 envp[n_env++] = t;
3608 }
3609 else
3610 envp[n_env++] = (char*) "TERM=dumb";
3611
3612 if (home || !uid_is_valid(arg_uid) || arg_uid == 0)
3613 if (asprintf(envp + n_env++, "HOME=%s", home ?: "/root") < 0)
3614 return log_oom();
3615
3616 if (arg_user || !uid_is_valid(arg_uid) || arg_uid == 0)
3617 if (asprintf(envp + n_env++, "USER=%s", arg_user ?: "root") < 0 ||
3618 asprintf(envp + n_env++, "LOGNAME=%s", arg_user ?: "root") < 0)
3619 return log_oom();
3620
3621 assert(!sd_id128_is_null(arg_uuid));
3622
3623 if (asprintf(envp + n_env++, "container_uuid=%s", SD_ID128_TO_UUID_STRING(arg_uuid)) < 0)
3624 return log_oom();
3625
3626 if (!fdset_isempty(fds)) {
3627 r = fdset_cloexec(fds, false);
3628 if (r < 0)
3629 return log_error_errno(r, "Failed to unset O_CLOEXEC for file descriptors.");
3630
3631 if ((asprintf(envp + n_env++, "LISTEN_FDS=%u", fdset_size(fds)) < 0) ||
3632 (asprintf(envp + n_env++, "LISTEN_PID=1") < 0))
3633 return log_oom();
3634 }
3635 if (asprintf(envp + n_env++, "NOTIFY_SOCKET=%s", NSPAWN_NOTIFY_SOCKET_PATH) < 0)
3636 return log_oom();
3637
3638 if (arg_credentials.n_credentials > 0) {
3639 envp[n_env] = strdup("CREDENTIALS_DIRECTORY=/run/host/credentials");
3640 if (!envp[n_env])
3641 return log_oom();
3642 n_env++;
3643 }
3644
3645 if (arg_start_mode != START_BOOT) {
3646 envp[n_env] = strdup("LANG=" SYSTEMD_NSPAWN_LOCALE);
3647 if (!envp[n_env])
3648 return log_oom();
3649 n_env++;
3650 }
3651
3652 env_use = strv_env_merge(envp, os_release_pairs, arg_setenv);
3653 if (!env_use)
3654 return log_oom();
3655
3656 /* Let the parent know that we are ready and wait until the parent is ready with the setup, too... */
3657 if (!barrier_place_and_sync(barrier)) /* #5 */
3658 return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Parent died too early");
3659
3660 /* Note, this should be done this late (💣 and not moved earlier! 💣), so that all namespacing
3661 * changes are already in effect by now, so that any resolved paths here definitely reference
3662 * resources inside the container, and not outside of them. */
3663 if (arg_chdir)
3664 if (chdir(arg_chdir) < 0)
3665 return log_error_errno(errno, "Failed to change to specified working directory %s: %m", arg_chdir);
3666
3667 if (arg_start_mode == START_PID2) {
3668 r = stub_pid1(arg_uuid);
3669 if (r < 0)
3670 return r;
3671 }
3672
3673 if (arg_console_mode != CONSOLE_PIPE) {
3674 /* So far our pty wasn't controlled by any process. Finally, it's time to change that, if we
3675 * are configured for that. Acquire it as controlling tty. */
3676 if (ioctl(STDIN_FILENO, TIOCSCTTY) < 0)
3677 return log_error_errno(errno, "Failed to acquire controlling TTY: %m");
3678 }
3679
3680 log_debug("Inner child finished, invoking payload.");
3681
3682 /* Now, explicitly close the log, so that we then can close all remaining fds. Closing the log explicitly first
3683 * has the benefit that the logging subsystem knows about it, and is thus ready to be reopened should we need
3684 * it again. Note that the other fds closed here are at least the locking and barrier fds. */
3685 log_close();
3686 log_set_open_when_needed(true);
3687 log_settle_target();
3688
3689 (void) fdset_close_others(fds);
3690
3691 if (arg_start_mode == START_BOOT) {
3692 char **a;
3693 size_t m;
3694
3695 /* Automatically search for the init system */
3696
3697 m = strv_length(arg_parameters);
3698 a = newa(char*, m + 2);
3699 memcpy_safe(a + 1, arg_parameters, m * sizeof(char*));
3700 a[1 + m] = NULL;
3701
3702 FOREACH_STRING(init,
3703 "/usr/lib/systemd/systemd",
3704 "/lib/systemd/systemd",
3705 "/sbin/init") {
3706 a[0] = (char*) init;
3707 execve(a[0], a, env_use);
3708 }
3709
3710 exec_target = "/usr/lib/systemd/systemd, /lib/systemd/systemd, /sbin/init";
3711 } else if (!strv_isempty(arg_parameters)) {
3712 const char *dollar_path;
3713
3714 exec_target = arg_parameters[0];
3715
3716 /* Use the user supplied search $PATH if there is one, or DEFAULT_PATH_COMPAT if not to search the
3717 * binary. */
3718 dollar_path = strv_env_get(env_use, "PATH");
3719 if (dollar_path) {
3720 if (setenv("PATH", dollar_path, 1) < 0)
3721 return log_error_errno(errno, "Failed to update $PATH: %m");
3722 }
3723
3724 execvpe(arg_parameters[0], arg_parameters, env_use);
3725 } else {
3726 if (!arg_chdir)
3727 /* If we cannot change the directory, we'll end up in /, that is expected. */
3728 (void) chdir(home ?: "/root");
3729
3730 execle(DEFAULT_USER_SHELL, "-" DEFAULT_USER_SHELL_NAME, NULL, env_use);
3731 if (!streq(DEFAULT_USER_SHELL, "/bin/bash"))
3732 execle("/bin/bash", "-bash", NULL, env_use);
3733 if (!streq(DEFAULT_USER_SHELL, "/bin/sh"))
3734 execle("/bin/sh", "-sh", NULL, env_use);
3735
3736 exec_target = DEFAULT_USER_SHELL ", /bin/bash, /bin/sh";
3737 }
3738
3739 return log_error_errno(errno, "execv(%s) failed: %m", exec_target);
3740 }
3741
3742 static int setup_notify_child(const void *directory) {
3743 _cleanup_close_ int fd = -EBADF;
3744 _cleanup_free_ char *j = NULL;
3745 union sockaddr_union sa = {
3746 .un.sun_family = AF_UNIX,
3747 };
3748 int r;
3749
3750 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
3751 if (fd < 0)
3752 return log_error_errno(errno, "Failed to allocate notification socket: %m");
3753
3754 if (directory) {
3755 j = path_join(directory, NSPAWN_NOTIFY_SOCKET_PATH);
3756 if (!j)
3757 return log_oom();
3758 }
3759
3760 r = sockaddr_un_set_path(&sa.un, j ?: NSPAWN_NOTIFY_SOCKET_PATH);
3761 if (r < 0)
3762 return log_error_errno(r, "Failed to set AF_UNIX path to %s: %m", j ?: NSPAWN_NOTIFY_SOCKET_PATH);
3763
3764 (void) mkdir_parents(sa.un.sun_path, 0755);
3765 (void) sockaddr_un_unlink(&sa.un);
3766
3767 WITH_UMASK(0577) { /* only set "w" bit, which is all that's necessary for connecting from the container */
3768 r = bind(fd, &sa.sa, sockaddr_un_len(&sa.un));
3769 if (r < 0)
3770 return log_error_errno(errno, "bind(" NSPAWN_NOTIFY_SOCKET_PATH ") failed: %m");
3771 }
3772
3773 r = userns_lchown(sa.un.sun_path, 0, 0);
3774 if (r < 0)
3775 return log_error_errno(r, "Failed to chown " NSPAWN_NOTIFY_SOCKET_PATH ": %m");
3776
3777 r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true);
3778 if (r < 0)
3779 return log_error_errno(r, "Failed to enable SO_PASSCRED: %m");
3780
3781 r = setsockopt_int(fd, SOL_SOCKET, SO_PASSPIDFD, true);
3782 if (r < 0)
3783 log_debug_errno(r, "Failed to enable SO_PASSPIDFD, ignoring: %m");
3784
3785 /* Only allow the container payload to pass file descriptors to us if we ourselves are
3786 * supervised by a service manager that enabled the FD store. */
3787 if (!fdstore_detected()) {
3788 r = setsockopt_int(fd, SOL_SOCKET, SO_PASSRIGHTS, false);
3789 if (r < 0)
3790 log_debug_errno(r, "Failed to turn off SO_PASSRIGHTS, ignoring: %m");
3791 }
3792
3793 return TAKE_FD(fd);
3794 }
3795
3796 static int setup_unix_export_dir_outside(const char *runtime_dir, char **ret) {
3797 int r;
3798
3799 assert(runtime_dir);
3800 assert(ret);
3801
3802 if (arg_userns_mode == USER_NAMESPACE_MANAGED) {
3803 log_debug("Not digging socket tunnel, because running in managed user namespace mode.");
3804 return 0;
3805 }
3806
3807 _cleanup_free_ char *p = NULL;
3808 p = path_join(runtime_dir, "unix-export");
3809 if (!p)
3810 return log_oom();
3811
3812 r = path_is_mount_point(p);
3813 if (r > 0)
3814 return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "Mount point '%s' exists already, refusing.", p);
3815 if (r < 0 && r != -ENOENT)
3816 return log_error_errno(r, "Failed to detect if '%s' is a mount point: %m", p);
3817
3818 r = mkdir_p(p, 0755);
3819 if (r < 0)
3820 return log_error_errno(r, "Failed to create '%s': %m", p);
3821
3822 _cleanup_(rmdir_and_freep) char *q = TAKE_PTR(p);
3823
3824 /* Mount the "unix export" directory really tiny, just 64 inodes. We mark the superblock writable
3825 * (since the container shall bind sockets into it). */
3826 r = mount_nofollow_verbose(
3827 LOG_ERR,
3828 "tmpfs",
3829 q,
3830 "tmpfs",
3831 MS_NODEV|MS_NOEXEC|MS_NOSUID|MS_NOSYMFOLLOW,
3832 "size=4M,nr_inodes=64,mode=0755");
3833 if (r < 0)
3834 return r;
3835
3836 _cleanup_(umount_and_rmdir_and_freep) char *w = TAKE_PTR(q);
3837
3838 /* After creating the superblock we change the bind mount to be read-only. This means that the fs
3839 * itself is writable, but not through the mount accessible from the host. */
3840 r = mount_nofollow_verbose(
3841 LOG_ERR,
3842 /* what= */ NULL,
3843 w,
3844 /* fstype= */ NULL,
3845 MS_BIND|MS_REMOUNT|MS_RDONLY|MS_NODEV|MS_NOEXEC|MS_NOSUID|MS_NOSYMFOLLOW,
3846 /* options= */ NULL);
3847 if (r < 0)
3848 return r;
3849
3850 *ret = TAKE_PTR(w);
3851 return 0;
3852 }
3853
3854 static int setup_unix_export_host_inside(const char *directory, const char *unix_export_path) {
3855 int r;
3856
3857 assert(directory);
3858
3859 if (arg_userns_mode == USER_NAMESPACE_MANAGED)
3860 return 0;
3861
3862 assert(unix_export_path);
3863
3864 r = make_run_host(directory);
3865 if (r < 0)
3866 return r;
3867
3868 _cleanup_free_ char *p = path_join(directory, "run/host/unix-export");
3869 if (!p)
3870 return log_oom();
3871
3872 if (mkdir(p, 0755) < 0)
3873 return log_error_errno(errno, "Failed to create '%s': %m", p);
3874
3875 r = mount_nofollow_verbose(
3876 LOG_ERR,
3877 unix_export_path,
3878 p,
3879 /* fstype= */ NULL,
3880 MS_BIND,
3881 /* options= */ NULL);
3882 if (r < 0)
3883 return r;
3884
3885 r = mount_nofollow_verbose(
3886 LOG_ERR,
3887 /* what= */ NULL,
3888 p,
3889 /* fstype= */ NULL,
3890 MS_BIND|MS_REMOUNT|MS_NODEV|MS_NOEXEC|MS_NOSUID|MS_NOSYMFOLLOW,
3891 /* options= */ NULL);
3892 if (r < 0)
3893 return r;
3894
3895 r = userns_lchown(p, 0, 0);
3896 if (r < 0)
3897 return log_error_errno(r, "Failed to chown '%s': %m", p);
3898
3899 return 0;
3900 }
3901
3902 static DissectImageFlags determine_dissect_image_flags(void) {
3903 return
3904 DISSECT_IMAGE_GENERIC_ROOT |
3905 DISSECT_IMAGE_REQUIRE_ROOT |
3906 DISSECT_IMAGE_RELAX_VAR_CHECK |
3907 DISSECT_IMAGE_USR_NO_ROOT |
3908 DISSECT_IMAGE_DISCARD_ON_LOOP |
3909 DISSECT_IMAGE_ADD_PARTITION_DEVICES |
3910 DISSECT_IMAGE_PIN_PARTITION_DEVICES |
3911 (arg_read_only ? DISSECT_IMAGE_READ_ONLY : DISSECT_IMAGE_FSCK|DISSECT_IMAGE_GROWFS) |
3912 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY |
3913 DISSECT_IMAGE_VERITY_SHARE |
3914 (arg_console_mode == CONSOLE_INTERACTIVE && arg_ask_password ? DISSECT_IMAGE_ALLOW_INTERACTIVE_AUTH : 0) |
3915 ((arg_userns_ownership == USER_NAMESPACE_OWNERSHIP_FOREIGN) ? DISSECT_IMAGE_FOREIGN_UID :
3916 (arg_userns_ownership != USER_NAMESPACE_OWNERSHIP_AUTO) ? DISSECT_IMAGE_IDENTITY_UID : 0);
3917 }
3918
3919 static int outer_child(
3920 Barrier *barrier,
3921 const char *directory,
3922 int mount_fd,
3923 DissectedImage *dissected_image,
3924 MStack *mstack,
3925 int fd_outer_socket,
3926 int fd_inner_socket,
3927 FDSet *fds,
3928 int netns_fd,
3929 const char *unix_export_path) {
3930
3931 _cleanup_strv_free_ char **os_release_pairs = NULL;
3932 bool idmap = false;
3933 ssize_t l;
3934 int r;
3935
3936 /* This is the "outer" child process, i.e the one forked off by the container manager itself. Its
3937 * namespace situation is:
3938 *
3939 * - CLONE_NEWUSER : if not in USER_NAMESPACE_MANAGED mode: still in the host's
3940 * if USER_NAMESPACE_MANAGED mode: already has its own (created by nsresource_allocate_userns()->setns(userns_fd))
3941 * - CLONE_NEWNS : already has its own (created by clone() if not USER_NAMESPACE_MANAGED, or unshare() otherwise)
3942 * - CLONE_NEWPID : still in the host's
3943 * - CLONE_NEWUTS : still in the host's
3944 * - CLONE_NEWIPC : still in the host's
3945 * - CLONE_NEWNET : still in the host's
3946 *
3947 * After it completed a number of initializations a second child (the "inner" one) is forked off it,
3948 * and it exits. */
3949
3950 assert(barrier);
3951 assert(directory);
3952 assert(fd_outer_socket >= 0);
3953 assert(fd_inner_socket >= 0);
3954
3955 log_debug("Outer child is initializing.");
3956
3957 r = load_os_release_pairs_with_prefix("/", "container_host_", &os_release_pairs);
3958 if (r < 0)
3959 log_debug_errno(r, "Failed to read os-release from host for container, ignoring: %m");
3960
3961 if (prctl(PR_SET_PDEATHSIG, SIGKILL) < 0)
3962 return log_error_errno(errno, "PR_SET_PDEATHSIG failed: %m");
3963
3964 r = reset_audit_loginuid();
3965 if (r < 0)
3966 return r;
3967
3968 /* Mark everything as slave, so that we still receive mounts from the real root, but don't propagate
3969 * mounts to the real root. */
3970 r = mount_follow_verbose(LOG_ERR, NULL, "/", NULL, MS_SLAVE|MS_REC, NULL);
3971 if (r < 0)
3972 return r;
3973
3974 /* Put the root dir into the target directory now. One of three mechanisms is provided: either we
3975 * have a single mount fd (typically unprivileged --directory= mode) or we have a fully dissected
3976 * image (--image= mode), or we have a regular path. */
3977 if (mount_fd >= 0) {
3978 assert(arg_directory);
3979 assert(!arg_image);
3980 assert(!arg_mstack);
3981
3982 if (move_mount(mount_fd, "", AT_FDCWD, directory, MOVE_MOUNT_F_EMPTY_PATH) < 0)
3983 return log_error_errno(errno, "Failed to attach root directory: %m");
3984
3985 mount_fd = safe_close(mount_fd);
3986 log_debug("Successfully attached root directory to '%s'.", directory);
3987
3988 } else if (dissected_image) {
3989 assert(!arg_directory);
3990 assert(arg_image);
3991 assert(!arg_mstack);
3992
3993 /* If we are operating on a disk image, then mount its root directory now, but leave out the
3994 * rest. We can read the UID shift from it if we need to. Further down we'll mount the rest,
3995 * but then with the uid shift known. That way we can mount VFAT file systems shifted to the
3996 * right place right away. This makes sure ESP partitions and userns are compatible. */
3997
3998 r = dissected_image_mount_and_warn(
3999 dissected_image,
4000 directory,
4001 arg_uid_shift,
4002 arg_uid_range,
4003 /* userns_fd= */ -EBADF,
4004 determine_dissect_image_flags()|
4005 DISSECT_IMAGE_MOUNT_ROOT_ONLY|
4006 (arg_start_mode == START_BOOT ? DISSECT_IMAGE_VALIDATE_OS : 0));
4007 if (r < 0)
4008 return r;
4009
4010 } else if (arg_mstack) {
4011 assert(!arg_directory);
4012 assert(!arg_image);
4013 assert(arg_mstack);
4014
4015 MStackFlags mstack_flags = arg_read_only ? MSTACK_RDONLY : 0;
4016
4017 /* This creates the needed overlayfs or tmpfs, owned by our target userns. Note that we pass
4018 * the target mount dir as temporary mount dir here. We after all just need some dir here
4019 * that definitely exists, and the temporary mounts on it are not going to be visible
4020 * outside. */
4021 r = mstack_make_mounts(
4022 mstack,
4023 /* temp_mount_dir= */ directory, /* !! */
4024 mstack_flags);
4025 if (r < 0)
4026 return log_error_errno(r, "Failed to make .mstack/ mounts: %m");
4027
4028 /* And then attaches all mounts to the directory */
4029 r = mstack_bind_mounts(
4030 mstack,
4031 directory,
4032 /* where_fd= */ -EBADF,
4033 mstack_flags,
4034 /* ret_root_fd= */ NULL);
4035 if (r < 0)
4036 return log_error_errno(r, "Failed bind mount .mstack/ mounts: %m");
4037 } else {
4038 assert(arg_directory);
4039 assert(!arg_image);
4040 assert(!arg_mstack);
4041
4042 r = mount_nofollow_verbose(LOG_ERR, arg_directory, directory, /* fstype= */ NULL, MS_BIND|MS_REC, /* options= */ NULL);
4043 if (r < 0)
4044 return r;
4045 }
4046
4047 r = determine_uid_shift(directory);
4048 if (r < 0)
4049 return r;
4050
4051 /* If we do userns on our own, we need to chown() all files ourselves before. Otherwise, if userns is
4052 * off or we are in managed mode we already have the userns applied, hence don't need to chown
4053 * anything */
4054 uid_t chown_uid, chown_range;
4055 if (in_child_chown()) {
4056 chown_uid = arg_uid_shift;
4057 chown_range = arg_uid_range;
4058 } else {
4059 chown_uid = 0;
4060 chown_range = UINT32_C(0x10000);
4061 }
4062
4063 if (arg_userns_mode != USER_NAMESPACE_NO) {
4064 _cleanup_close_ int mntns_fd = -EBADF;
4065
4066 mntns_fd = namespace_open_by_type(NAMESPACE_MOUNT);
4067 if (mntns_fd < 0)
4068 return log_error_errno(mntns_fd, "Failed to pin outer mount namespace: %m");
4069
4070 l = send_one_fd(fd_outer_socket, mntns_fd, 0);
4071 if (l < 0)
4072 return log_error_errno(l, "Failed to send outer mount namespace fd: %m");
4073 mntns_fd = safe_close(mntns_fd);
4074
4075 /* Let the parent know which UID shift we read from the image */
4076 l = send(fd_outer_socket, &arg_uid_shift, sizeof(arg_uid_shift), MSG_NOSIGNAL);
4077 if (l < 0)
4078 return log_error_errno(errno, "Failed to send UID shift: %m");
4079 if (l != sizeof(arg_uid_shift))
4080 return log_error_errno(SYNTHETIC_ERRNO(EIO),
4081 "Short write while sending UID shift.");
4082
4083 if (arg_userns_mode == USER_NAMESPACE_PICK) {
4084 /* When we are supposed to pick the UID shift, the parent will check now whether the
4085 * UID shift we just read from the image is available. If yes, it will send the UID
4086 * shift back to us, if not it will pick a different one, and send it back to us. */
4087
4088 l = recv(fd_outer_socket, &arg_uid_shift, sizeof(arg_uid_shift), 0);
4089 if (l < 0)
4090 return log_error_errno(errno, "Failed to recv UID shift: %m");
4091 if (l != sizeof(arg_uid_shift))
4092 return log_error_errno(SYNTHETIC_ERRNO(EIO),
4093 "Short read while receiving UID shift.");
4094
4095 if (in_child_chown())
4096 chown_uid = arg_uid_shift;
4097 }
4098
4099 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO,
4100 "Selected user namespace base " UID_FMT " and range " UID_FMT ".", arg_uid_shift, arg_uid_range);
4101 }
4102
4103 /* So the whole tree is now MS_SLAVE, i.e. we'll still receive mount/umount events from the host
4104 * mount namespace. For the directory we are going to run our container let's turn this off, so that
4105 * we'll live in our own little world from now on, and propagation from the host may only happen via
4106 * the mount tunnel dir, or not at all. */
4107 r = mount_follow_verbose(LOG_ERR, NULL, directory, NULL, MS_PRIVATE|MS_REC, NULL);
4108 if (r < 0)
4109 return r;
4110
4111 r = setup_pivot_root(
4112 directory,
4113 arg_pivot_root_new,
4114 arg_pivot_root_old);
4115 if (r < 0)
4116 return r;
4117
4118 r = setup_volatile_mode(
4119 directory,
4120 arg_volatile_mode,
4121 chown_uid,
4122 arg_selinux_apifs_context);
4123 if (r < 0)
4124 return r;
4125
4126 _cleanup_(machine_bind_user_context_freep) MachineBindUserContext *bind_user_context = NULL;
4127 r = machine_bind_user_prepare(
4128 directory,
4129 arg_bind_user,
4130 arg_bind_user_shell,
4131 arg_bind_user_shell_copy,
4132 "/run/host/home",
4133 arg_bind_user_groups,
4134 &bind_user_context);
4135 if (r < 0)
4136 return r;
4137
4138 if (bind_user_context)
4139 FOREACH_ARRAY(bind_user, bind_user_context->data, bind_user_context->n_data) {
4140 _cleanup_free_ char *sm = strdup(user_record_home_directory(bind_user->host_user));
4141 if (!sm)
4142 return log_oom();
4143
4144 _cleanup_free_ char *sd = strdup(user_record_home_directory(bind_user->payload_user));
4145 if (!sd)
4146 return log_oom();
4147
4148 if (!GREEDY_REALLOC(arg_custom_mounts, arg_n_custom_mounts + 1))
4149 return log_oom();
4150
4151 char *options = strdup("owneridmap");
4152 if (!options)
4153 return log_oom();
4154
4155 arg_custom_mounts[arg_n_custom_mounts++] = (CustomMount) {
4156 .type = CUSTOM_MOUNT_BIND,
4157 .source = TAKE_PTR(sm),
4158 .destination = TAKE_PTR(sd),
4159 .options = TAKE_PTR(options),
4160 .destination_uid = bind_user->payload_user->uid,
4161 };
4162 }
4163
4164 r = mount_custom(
4165 directory,
4166 arg_custom_mounts,
4167 arg_n_custom_mounts,
4168 chown_uid,
4169 chown_range,
4170 arg_selinux_apifs_context,
4171 MOUNT_ROOT_ONLY);
4172 if (r < 0)
4173 return r;
4174
4175 if (!IN_SET(arg_userns_mode, USER_NAMESPACE_NO, USER_NAMESPACE_MANAGED) &&
4176 IN_SET(arg_userns_ownership, USER_NAMESPACE_OWNERSHIP_MAP, USER_NAMESPACE_OWNERSHIP_FOREIGN, USER_NAMESPACE_OWNERSHIP_AUTO) &&
4177 chown_uid != 0) {
4178 _cleanup_strv_free_ char **dirs = NULL;
4179 RemountIdmapping mapping;
4180
4181 switch (arg_userns_ownership) {
4182 case USER_NAMESPACE_OWNERSHIP_MAP:
4183 mapping = REMOUNT_IDMAPPING_HOST_ROOT;
4184 break;
4185
4186 case USER_NAMESPACE_OWNERSHIP_FOREIGN:
4187 mapping = REMOUNT_IDMAPPING_FOREIGN_WITH_HOST_ROOT;
4188 break;
4189
4190 case USER_NAMESPACE_OWNERSHIP_AUTO: {
4191 struct stat st;
4192
4193 if (lstat(directory, &st) < 0)
4194 return log_error_errno(errno, "Failed to stat() container root directory '%s': %m", directory);
4195
4196 r = stat_verify_directory(&st);
4197 if (r < 0)
4198 return log_error_errno(r, "Container root directory '%s' is not a directory: %m", directory);
4199
4200 mapping = uid_is_foreign(st.st_uid) ?
4201 REMOUNT_IDMAPPING_FOREIGN_WITH_HOST_ROOT :
4202 REMOUNT_IDMAPPING_HOST_ROOT;
4203 break;
4204 }
4205
4206 default:
4207 assert_not_reached();
4208 }
4209
4210 if (arg_volatile_mode != VOLATILE_YES) {
4211 r = strv_extend(&dirs, directory);
4212 if (r < 0)
4213 return log_oom();
4214 }
4215
4216 if ((dissected_image && dissected_image->partitions[PARTITION_USR].found) ||
4217 arg_volatile_mode == VOLATILE_YES) {
4218 char *s = path_join(directory, "/usr");
4219 if (!s)
4220 return log_oom();
4221
4222 r = strv_consume(&dirs, s);
4223 if (r < 0)
4224 return log_oom();
4225 }
4226
4227 r = remount_idmap(
4228 dirs,
4229 chown_uid,
4230 chown_range,
4231 /* source_owner= */ UID_INVALID,
4232 /* dest_owner= */ UID_INVALID,
4233 mapping);
4234 if (r == -EINVAL || ERRNO_IS_NEG_NOT_SUPPORTED(r)) {
4235 /* This might fail because the kernel or file system doesn't support idmapping. We
4236 * can't really distinguish this nicely, nor do we have any guarantees about the
4237 * error codes we see, could be EOPNOTSUPP or EINVAL. */
4238 if (arg_userns_ownership != USER_NAMESPACE_OWNERSHIP_AUTO)
4239 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
4240 "ID mapped mounts are apparently not available, sorry.");
4241
4242 log_debug("ID mapped mounts are apparently not available on this kernel or for the selected file system, reverting to recursive chown()ing.");
4243 arg_userns_ownership = USER_NAMESPACE_OWNERSHIP_CHOWN;
4244 } else if (r < 0)
4245 return log_error_errno(r, "Failed to set up ID mapped mounts: %m");
4246 else {
4247 log_debug("ID mapped mounts available, making use of them.");
4248 idmap = true;
4249 }
4250 }
4251
4252 r = setup_volatile_mode_after_remount_idmap(
4253 directory,
4254 arg_volatile_mode,
4255 chown_uid,
4256 arg_selinux_apifs_context);
4257 if (r < 0)
4258 return r;
4259
4260 if (dissected_image) {
4261 /* Now we know the uid shift, let's now mount everything else that might be in the image. */
4262 r = dissected_image_mount_and_warn(
4263 dissected_image,
4264 directory,
4265 chown_uid,
4266 chown_range,
4267 /* userns_fd= */ -EBADF,
4268 determine_dissect_image_flags()|
4269 DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY|
4270 (idmap ? DISSECT_IMAGE_MOUNT_IDMAPPED : 0));
4271 if (r < 0)
4272 return r;
4273 }
4274
4275 r = recursive_chown(directory, chown_uid, chown_range);
4276 if (r < 0)
4277 return r;
4278
4279 r = base_filesystem_create(directory, chown_uid, (gid_t) chown_uid);
4280 if (r < 0)
4281 return r;
4282
4283 if (arg_read_only && arg_volatile_mode == VOLATILE_NO &&
4284 !has_custom_root_mount(arg_custom_mounts, arg_n_custom_mounts)) {
4285 r = bind_remount_recursive(directory, MS_RDONLY, MS_RDONLY, NULL);
4286 if (r < 0)
4287 return log_error_errno(r, "Failed to make tree read-only: %m");
4288 }
4289
4290 r = mount_all(directory,
4291 arg_mount_settings,
4292 chown_uid,
4293 arg_selinux_apifs_context);
4294 if (r < 0)
4295 return r;
4296
4297 r = copy_devnodes(directory);
4298 if (r < 0)
4299 return r;
4300
4301 r = make_extra_nodes(directory);
4302 if (r < 0)
4303 return r;
4304
4305 (void) dev_setup(directory, chown_uid, chown_uid);
4306
4307 _cleanup_free_ char *p = path_join(directory, "/run/host");
4308 if (!p)
4309 return log_oom();
4310
4311 (void) make_inaccessible_nodes(p, chown_uid, chown_uid);
4312
4313 r = setup_boot_id_file(directory);
4314 if (r < 0)
4315 return r;
4316
4317 r = setup_kmsg_fifo(directory);
4318 if (r < 0)
4319 return r;
4320
4321 r = setup_unix_export_host_inside(directory, unix_export_path);
4322 if (r < 0)
4323 return r;
4324
4325 r = setup_pts(directory, chown_uid);
4326 if (r < 0)
4327 return r;
4328
4329 r = mount_tunnel_dig(directory);
4330 if (r < 0)
4331 return r;
4332
4333 r = setup_keyring();
4334 if (r < 0)
4335 return r;
4336
4337 r = setup_credentials(directory);
4338 if (r < 0)
4339 return r;
4340
4341 r = bind_user_setup(bind_user_context, directory);
4342 if (r < 0)
4343 return r;
4344
4345 r = mount_custom(
4346 directory,
4347 arg_custom_mounts,
4348 arg_n_custom_mounts,
4349 chown_uid,
4350 chown_range,
4351 arg_selinux_apifs_context,
4352 MOUNT_NON_ROOT_ONLY);
4353 if (r < 0)
4354 return r;
4355
4356 r = setup_timezone(directory);
4357 if (r < 0)
4358 return r;
4359
4360 r = setup_resolv_conf(directory);
4361 if (r < 0)
4362 return r;
4363
4364 r = setup_machine_id(directory);
4365 if (r < 0)
4366 return r;
4367
4368 r = setup_journal(directory, chown_uid, chown_range);
4369 if (r < 0)
4370 return r;
4371
4372 r = setup_varlink_socket(directory, "io.systemd.NamespaceResource");
4373 if (r < 0)
4374 return r;
4375
4376 r = setup_varlink_socket(directory, "io.systemd.MountFileSystem");
4377 if (r < 0)
4378 return r;
4379
4380 /* The same stuff as the $container env var, but nicely readable for the entire payload */
4381 free(p);
4382 p = path_join(directory, "/run/host/container-manager");
4383 if (!p)
4384 return log_oom();
4385
4386 (void) write_string_file(p, arg_container_service_name, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MODE_0444);
4387
4388 /* The same stuff as the $container_uuid env var */
4389 free(p);
4390 p = path_join(directory, "/run/host/container-uuid");
4391 if (!p)
4392 return log_oom();
4393
4394 (void) write_string_filef(p, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MODE_0444, SD_ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(arg_uuid));
4395
4396 if (!arg_use_cgns) {
4397 r = mount_cgroups(directory, /* accept_existing= */ true);
4398 if (r < 0)
4399 return r;
4400 }
4401
4402 /* We have different codepaths here for privileged and non-privileged mode. In privileged mode we'll
4403 * now switch into the target directory, and then do the final setup from there. If a user namespace
4404 * is then allocated for the container, the root mount and everything else will be out of reach for
4405 * it. For unprivileged containers we cannot do that however, since we couldn't mount a sysfs and
4406 * procfs then anymore, since that only works if there's an unobstructed instance currently
4407 * visible. Hence there we do it the other way round: we first allocate a new set of namespaces
4408 * (and fork for it) for which we then mount sysfs/procfs, and only then switch root. */
4409
4410 _cleanup_close_ int notify_fd = -EBADF;
4411 if (arg_userns_mode != USER_NAMESPACE_MANAGED) {
4412 /* Mark everything as shared so our mounts get propagated down. This is required to make new
4413 * bind mounts available in systemd services inside the container that create a new mount
4414 * namespace. See https://github.com/systemd/systemd/issues/3860 Further submounts (such as
4415 * /dev/) done after this will inherit the shared propagation mode.
4416 *
4417 * IMPORTANT: Do not overmount the root directory anymore from now on to enable moving the root
4418 * directory mount to root later on.
4419 * https://github.com/systemd/systemd/issues/3847#issuecomment-562735251
4420 */
4421 r = mount_switch_root(directory, MS_SHARED);
4422 if (r < 0)
4423 return log_error_errno(r, "Failed to move root directory: %m");
4424
4425 /* We finished setting up the rootfs which is a shared mount. The mount tunnel needs to be a
4426 * dependent mount otherwise we can't MS_MOVE mounts that were propagated from the host into
4427 * the container. */
4428 r = mount_tunnel_open();
4429 if (r < 0)
4430 return r;
4431
4432 if (arg_userns_mode != USER_NAMESPACE_NO) {
4433 /* In order to mount procfs and sysfs in an unprivileged container the kernel
4434 * requires that a fully visible instance is already present in the target mount
4435 * namespace. Mount one here so the inner child can mount its own instances. Later
4436 * we umount the temporary instances created here before we actually exec the
4437 * payload. Since the rootfs is shared the umount will propagate into the container.
4438 * Note, the inner child wouldn't be able to unmount the instances on its own since
4439 * it doesn't own the originating mount namespace. IOW, the outer child needs to do
4440 * this. */
4441 r = pin_fully_visible_api_fs();
4442 if (r < 0)
4443 return r;
4444 }
4445
4446 notify_fd = setup_notify_child(NULL);
4447 } else
4448 notify_fd = setup_notify_child(directory);
4449 if (notify_fd < 0)
4450 return notify_fd;
4451
4452 /* Join the external network namespace first, while we are still in the parent's
4453 * user namespace and have CAP_SYS_ADMIN there. Once we clone with CLONE_NEWUSER,
4454 * the child will be in a new user namespace, lacking the capabilities in the
4455 * parent user namespace required to join its network namespace. */
4456 if (arg_network_namespace_path && setns(netns_fd, CLONE_NEWNET) < 0)
4457 return log_error_errno(errno, "Failed to join network namespace: %m");
4458
4459 pid_t pid = raw_clone(SIGCHLD|CLONE_NEWNS|
4460 arg_clone_ns_flags |
4461 (IN_SET(arg_userns_mode, USER_NAMESPACE_FIXED, USER_NAMESPACE_PICK) ? CLONE_NEWUSER : 0) |
4462 ((arg_private_network && arg_userns_mode == USER_NAMESPACE_MANAGED) ? CLONE_NEWNET : 0));
4463 if (pid < 0)
4464 return log_error_errno(errno, "Failed to fork inner child: %m");
4465 if (pid == 0) {
4466 fd_outer_socket = safe_close(fd_outer_socket);
4467
4468 /* In the child refuse dlopen(), so that we never mix shared libraries from payload and parent */
4469 block_dlopen();
4470
4471 /* The inner child has all namespaces that are requested, so that we all are owned by the
4472 * user if user namespaces are turned on. */
4473
4474 if (arg_userns_mode == USER_NAMESPACE_MANAGED) {
4475 /* In managed usernamespace operation, sysfs + procfs are special, we'll have to
4476 * mount them inside the inner namespaces, but before we switch root. Hence do so
4477 * here. */
4478 _cleanup_free_ char *j = path_join(directory, "/proc");
4479 if (!j)
4480 return log_oom();
4481
4482 r = mount_follow_verbose(LOG_ERR, "proc", j, "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
4483 if (r < 0)
4484 return r;
4485
4486 r = mount_sysfs(directory, arg_mount_settings);
4487 if (r < 0)
4488 return r;
4489
4490 r = mount_switch_root(directory, MS_SHARED);
4491 if (r < 0)
4492 return log_error_errno(r, "Failed to move root directory: %m");
4493 }
4494
4495 r = inner_child(barrier, fd_inner_socket, fds, os_release_pairs);
4496 if (r < 0)
4497 _exit(EXIT_FAILURE);
4498
4499 _exit(EXIT_SUCCESS);
4500 }
4501
4502 l = send(fd_outer_socket, &pid, sizeof(pid), MSG_NOSIGNAL);
4503 if (l < 0)
4504 return log_error_errno(errno, "Failed to send PID: %m");
4505 if (l != sizeof(pid))
4506 return log_error_errno(SYNTHETIC_ERRNO(EIO),
4507 "Short write while sending PID.");
4508
4509 l = send(fd_outer_socket, &arg_uuid, sizeof(arg_uuid), MSG_NOSIGNAL);
4510 if (l < 0)
4511 return log_error_errno(errno, "Failed to send machine ID: %m");
4512 if (l != sizeof(arg_uuid))
4513 return log_error_errno(SYNTHETIC_ERRNO(EIO),
4514 "Short write while sending machine ID.");
4515
4516 l = send_one_fd(fd_outer_socket, notify_fd, 0);
4517 if (l < 0)
4518 return log_error_errno(l, "Failed to send notify fd: %m");
4519
4520 fd_outer_socket = safe_close(fd_outer_socket);
4521 fd_inner_socket = safe_close(fd_inner_socket);
4522 netns_fd = safe_close(netns_fd);
4523
4524 return 0;
4525 }
4526
4527 static int uid_shift_pick(uid_t *shift, LockFile *ret_lock_file) {
4528 bool tried_hashed = false;
4529 unsigned n_tries = 100;
4530 uid_t candidate;
4531 int r;
4532
4533 assert(shift);
4534 assert(ret_lock_file);
4535 assert(arg_userns_mode == USER_NAMESPACE_PICK);
4536 assert(arg_uid_range == 0x10000U);
4537
4538 candidate = *shift;
4539
4540 (void) mkdir("/run/systemd/nspawn-uid", 0755);
4541
4542 for (;;) {
4543 char lock_path[STRLEN("/run/systemd/nspawn-uid/") + DECIMAL_STR_MAX(uid_t) + 1];
4544 _cleanup_(release_lock_file) LockFile lf = LOCK_FILE_INIT;
4545
4546 if (--n_tries <= 0)
4547 return -EBUSY;
4548
4549 if (candidate < CONTAINER_UID_BASE_MIN || candidate > CONTAINER_UID_BASE_MAX)
4550 goto next;
4551 if ((candidate & UINT32_C(0xFFFF)) != 0)
4552 goto next;
4553
4554 xsprintf(lock_path, "/run/systemd/nspawn-uid/" UID_FMT, candidate);
4555 r = make_lock_file(lock_path, LOCK_EX|LOCK_NB, &lf);
4556 if (r == -EBUSY) /* Range already taken by another nspawn instance */
4557 goto next;
4558 if (r < 0)
4559 return r;
4560
4561 /* Make some superficial checks whether the range is currently known in the user database */
4562 if (getpwuid_malloc(candidate, /* ret= */ NULL) >= 0)
4563 goto next;
4564 if (getpwuid_malloc(candidate + UINT32_C(0xFFFE), /* ret= */ NULL) >= 0)
4565 goto next;
4566 if (getgrgid_malloc(candidate, /* ret= */ NULL) >= 0)
4567 goto next;
4568 if (getgrgid_malloc(candidate + UINT32_C(0xFFFE), /* ret= */ NULL) >= 0)
4569 goto next;
4570
4571 *ret_lock_file = lf;
4572 lf = (struct LockFile) LOCK_FILE_INIT;
4573 *shift = candidate;
4574 return 0;
4575
4576 next:
4577 if (arg_machine && !tried_hashed) {
4578 /* Try to hash the base from the container name */
4579
4580 static const uint8_t hash_key[] = {
4581 0xe1, 0x56, 0xe0, 0xf0, 0x4a, 0xf0, 0x41, 0xaf,
4582 0x96, 0x41, 0xcf, 0x41, 0x33, 0x94, 0xff, 0x72
4583 };
4584
4585 candidate = (uid_t) siphash24(arg_machine, strlen(arg_machine), hash_key);
4586
4587 tried_hashed = true;
4588 } else
4589 random_bytes(&candidate, sizeof(candidate));
4590
4591 candidate = (candidate % (CONTAINER_UID_BASE_MAX - CONTAINER_UID_BASE_MIN)) + CONTAINER_UID_BASE_MIN;
4592 candidate &= (uid_t) UINT32_C(0xFFFF0000);
4593 }
4594 }
4595
4596 static int setup_uid_map(
4597 const PidRef *pid,
4598 const uid_t bind_user_uid[],
4599 size_t n_bind_user_uid) {
4600
4601 char uid_map[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(uid_t) + 1];
4602 _cleanup_free_ char *s = NULL;
4603 int r;
4604
4605 assert(pidref_is_set(pid));
4606 assert(pid->pid > 1);
4607
4608 if (asprintf(&s, "0 " UID_FMT " " UID_FMT "\n", arg_uid_shift, arg_uid_range) < 0)
4609 return log_oom();
4610
4611 xsprintf(uid_map, "/proc/" PID_FMT "/uid_map", pid->pid);
4612 r = write_string_file(uid_map, s, WRITE_STRING_FILE_DISABLE_BUFFER);
4613 if (r < 0)
4614 return log_error_errno(r, "Failed to write UID map: %m");
4615
4616 xsprintf(uid_map, "/proc/" PID_FMT "/gid_map", pid->pid);
4617 r = write_string_file(uid_map, s, WRITE_STRING_FILE_DISABLE_BUFFER);
4618 if (r < 0)
4619 return log_error_errno(r, "Failed to write GID map: %m");
4620
4621 return 0;
4622 }
4623
4624 static int forward_fd_store(char **tags, FDSet *fds) {
4625 int r;
4626
4627 /* Forward fd-store related messages to our own service manager, so that file descriptors stored
4628 * by the inner payload propagate up the chain and are preserved across restarts. Skip entirely
4629 * if we have no upstream supervisor (no NOTIFY_SOCKET) or no fd store available (no FDSTORE).
4630 *
4631 * Forwarded entries are namespaced with a "payload-" prefix on their FDNAME so that they
4632 * cannot collide with fd-store entries that nspawn itself might want to push to its own
4633 * upstream supervisor (the container payload and nspawn share a single upstream fdstore
4634 * namespace, since there's only one init system per container). */
4635 if (!getenv("NOTIFY_SOCKET") || !fdstore_detected())
4636 return 0;
4637
4638 if (strv_contains(tags, "FDSTOREREMOVE=1")) {
4639 const char *fdname = strv_find_startswith(tags, "FDNAME=");
4640 if (!fdname)
4641 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
4642 "Got FDSTOREREMOVE=1 from container payload without FDNAME=, ignoring.");
4643 if (!fdname_is_valid(fdname))
4644 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
4645 "Got FDSTOREREMOVE=1 from container payload with invalid FDNAME='%s', ignoring.",
4646 fdname);
4647
4648 r = sd_notifyf(/* unset_environment= */ false,
4649 "FDSTOREREMOVE=1\nFDNAME=payload-%s", fdname);
4650 if (r < 0)
4651 return log_warning_errno(r, "Failed to forward FDSTOREREMOVE upstream, ignoring: %m");
4652 } else if (strv_contains(tags, "FDSTORE=1")) {
4653 if (fdset_isempty(fds)) {
4654 log_debug("Got FDSTORE=1 from container payload without any attached file descriptors, ignoring.");
4655 return 0;
4656 }
4657
4658 _cleanup_free_ int *fds_array = NULL;
4659 int n;
4660
4661 n = fdset_to_array(fds, &fds_array);
4662 if (n < 0)
4663 return log_warning_errno(n, "Failed to convert fdset to array, ignoring FDSTORE forward: %m");
4664
4665 const char *fdname = strv_find_startswith(tags, "FDNAME=");
4666 bool fdpoll_off = strv_contains(tags, "FDPOLL=0");
4667 _cleanup_free_ char *msg = NULL;
4668 unsigned n_fds = (unsigned) n;
4669
4670 if (fdname && !fdname_is_valid(fdname)) {
4671 log_warning("Got FDSTORE=1 from container payload with invalid FDNAME='%s', ignoring name.", fdname);
4672 fdname = NULL;
4673 }
4674
4675 if (asprintf(&msg, "FDSTORE=1\nFDNAME=payload-%s%s%s",
4676 fdname ?: "stored",
4677 fdpoll_off ? "\nFDPOLL=" : "",
4678 fdpoll_off ? "0" : "") < 0)
4679 return log_oom();
4680
4681 r = sd_pid_notify_with_fds(
4682 0,
4683 /* unset_environment= */ false,
4684 msg,
4685 fds_array,
4686 n_fds);
4687 if (r < 0)
4688 return log_warning_errno(r, "Failed to forward FDSTORE upstream, ignoring: %m");
4689 }
4690
4691 return 0;
4692 }
4693
4694 static int nspawn_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
4695 PidRef *inner_child_pid = ASSERT_PTR(userdata);
4696 int r;
4697
4698 assert(userdata);
4699
4700 _cleanup_(pidref_done) PidRef sender_pid = PIDREF_NULL;
4701 _cleanup_strv_free_ char **tags = NULL;
4702 _cleanup_(fdset_freep) FDSet *fds = NULL;
4703 r = notify_recv_with_fds_strv(fd, &tags, /* ret_ucred= */ NULL, &sender_pid, &fds);
4704 if (r == -EAGAIN)
4705 return 0;
4706 if (r < 0)
4707 return r;
4708
4709 if (!pidref_equal(&sender_pid, inner_child_pid)) {
4710 log_debug("Received notify message from process that is not the payload's PID 1. Ignoring.");
4711 return 0;
4712 }
4713
4714 if (DEBUG_LOGGING) {
4715 _cleanup_free_ char *joined = strv_join(tags, " ");
4716
4717 if (joined) {
4718 _cleanup_free_ char *j = cescape(joined);
4719 free_and_replace(joined, j);
4720 }
4721
4722 log_debug("Got sd_notify() message: %s", strnull(joined));
4723 }
4724
4725 char *status = strv_find_startswith(tags, "STATUS=");
4726 if (status)
4727 (void) sd_notifyf(/* unset_environment= */ false, "STATUS=Container running: %s", status);
4728
4729 if (strv_contains(tags, "READY=1")) {
4730 r = sd_notify(/* unset_environment= */ false, "READY=1\n");
4731 if (r < 0)
4732 log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
4733
4734 if (!status)
4735 (void) sd_notifyf(/* unset_environment= */ false, "STATUS=Container running.");
4736 }
4737
4738 (void) forward_fd_store(tags, fds);
4739
4740 return 0;
4741 }
4742
4743 static int setup_notify_parent(sd_event *event, int fd, PidRef *inner_child_pid, sd_event_source **notify_event_source) {
4744 int r;
4745
4746 assert(notify_event_source);
4747
4748 if (fd < 0)
4749 return 0;
4750
4751 r = sd_event_add_io(event, notify_event_source, fd, EPOLLIN, nspawn_dispatch_notify_fd, inner_child_pid);
4752 if (r < 0)
4753 return log_error_errno(r, "Failed to allocate notify event source: %m");
4754
4755 (void) sd_event_source_set_description(*notify_event_source, "nspawn-notify");
4756
4757 return 0;
4758 }
4759
4760 static int ptyfwd_hotkey(PTYForward *f, char c, void *userdata) {
4761 PidRef *pid = ASSERT_PTR(userdata);
4762 const char *word;
4763 int sig = 0, r;
4764
4765 assert(f);
4766
4767 switch (c) {
4768 case 'p':
4769 sig = SIGRTMIN+4;
4770 word = "power off";
4771 break;
4772
4773 case 'r':
4774 sig = SIGRTMIN+5;
4775 word = "reboot";
4776 break;
4777
4778 default:
4779 log_info("Unknown hotkey sequence ^]^]%c, ignoring.", c);
4780 return 0;
4781 }
4782
4783 r = pidref_kill(pid, sig);
4784 if (r < 0)
4785 log_error_errno(r, "Failed to send %s (%s request) to PID 1 of container: %m", signal_to_string(sig), word);
4786 else
4787 log_info("Sent %s (%s request) to PID 1 of container.", signal_to_string(sig), word);
4788
4789 return 0;
4790 }
4791
4792 static int merge_settings(Settings *settings, const char *path) {
4793 int rl;
4794
4795 assert(settings);
4796 assert(path);
4797
4798 /* Copy over bits from the settings, unless they have been explicitly masked by command line switches. Note
4799 * that this steals the fields of the Settings* structure, and hence modifies it. */
4800
4801 if ((arg_settings_mask & SETTING_START_MODE) == 0 &&
4802 settings->start_mode >= 0) {
4803 arg_start_mode = settings->start_mode;
4804 strv_free_and_replace(arg_parameters, settings->parameters);
4805 }
4806
4807 if ((arg_settings_mask & SETTING_EPHEMERAL) == 0 &&
4808 settings->ephemeral >= 0) {
4809
4810 if (!arg_settings_trusted)
4811 log_warning("Ignoring ephemeral setting, file %s is not trusted.", path);
4812 else
4813 arg_ephemeral = settings->ephemeral;
4814 }
4815
4816 if ((arg_settings_mask & SETTING_DIRECTORY) == 0 &&
4817 settings->root) {
4818
4819 if (!arg_settings_trusted)
4820 log_warning("Ignoring root directory setting, file %s is not trusted.", path);
4821 else
4822 free_and_replace(arg_directory, settings->root);
4823 }
4824
4825 if ((arg_settings_mask & SETTING_PIVOT_ROOT) == 0 &&
4826 settings->pivot_root_new) {
4827 free_and_replace(arg_pivot_root_new, settings->pivot_root_new);
4828 free_and_replace(arg_pivot_root_old, settings->pivot_root_old);
4829 }
4830
4831 if ((arg_settings_mask & SETTING_WORKING_DIRECTORY) == 0 &&
4832 settings->working_directory)
4833 free_and_replace(arg_chdir, settings->working_directory);
4834
4835 if ((arg_settings_mask & SETTING_ENVIRONMENT) == 0 &&
4836 settings->environment)
4837 strv_free_and_replace(arg_setenv, settings->environment);
4838
4839 if ((arg_settings_mask & SETTING_USER) == 0) {
4840
4841 if (settings->user)
4842 free_and_replace(arg_user, settings->user);
4843
4844 if (uid_is_valid(settings->uid))
4845 arg_uid = settings->uid;
4846 if (gid_is_valid(settings->gid))
4847 arg_gid = settings->gid;
4848 if (settings->n_supplementary_gids > 0) {
4849 free_and_replace(arg_supplementary_gids, settings->supplementary_gids);
4850 arg_n_supplementary_gids = settings->n_supplementary_gids;
4851 }
4852 }
4853
4854 if ((arg_settings_mask & SETTING_CAPABILITY) == 0) {
4855 uint64_t plus, minus;
4856 uint64_t network_minus = 0;
4857 uint64_t ambient;
4858
4859 /* Note that we copy both the simple plus/minus caps here, and the full quintet from the
4860 * Settings structure */
4861
4862 plus = settings->capability;
4863 minus = settings->drop_capability;
4864
4865 if ((arg_settings_mask & SETTING_NETWORK) == 0 &&
4866 settings_network_configured(settings)) {
4867 if (settings_private_network(settings))
4868 plus |= UINT64_C(1) << CAP_NET_ADMIN;
4869 else
4870 network_minus |= UINT64_C(1) << CAP_NET_ADMIN;
4871 }
4872
4873 if (!arg_settings_trusted && plus != 0) {
4874 if (settings->capability != 0)
4875 log_warning("Ignoring Capability= setting, file %s is not trusted.", path);
4876 } else {
4877 arg_caps_retain &= ~network_minus;
4878 arg_caps_retain |= plus;
4879 }
4880
4881 arg_caps_retain &= ~minus;
4882
4883 /* Copy the full capabilities over too */
4884 if (capability_quintet_is_set(&settings->full_capabilities)) {
4885 if (!arg_settings_trusted)
4886 log_warning("Ignoring capability settings, file %s is not trusted.", path);
4887 else
4888 arg_full_capabilities = settings->full_capabilities;
4889 }
4890
4891 ambient = settings->ambient_capability;
4892 if (!arg_settings_trusted && ambient != 0)
4893 log_warning("Ignoring AmbientCapability= setting, file %s is not trusted.", path);
4894 else
4895 arg_caps_ambient |= ambient;
4896 }
4897
4898 if ((arg_settings_mask & SETTING_KILL_SIGNAL) == 0 &&
4899 settings->kill_signal > 0)
4900 arg_kill_signal = settings->kill_signal;
4901
4902 if ((arg_settings_mask & SETTING_PERSONALITY) == 0 &&
4903 settings->personality != PERSONALITY_INVALID)
4904 arg_personality = settings->personality;
4905
4906 if ((arg_settings_mask & SETTING_MACHINE_ID) == 0 &&
4907 !sd_id128_is_null(settings->machine_id)) {
4908
4909 if (!arg_settings_trusted)
4910 log_warning("Ignoring MachineID= setting, file %s is not trusted.", path);
4911 else
4912 arg_uuid = settings->machine_id;
4913 }
4914
4915 if ((arg_settings_mask & SETTING_READ_ONLY) == 0 &&
4916 settings->read_only >= 0)
4917 arg_read_only = settings->read_only;
4918
4919 if ((arg_settings_mask & SETTING_VOLATILE_MODE) == 0 &&
4920 settings->volatile_mode != _VOLATILE_MODE_INVALID)
4921 arg_volatile_mode = settings->volatile_mode;
4922
4923 if ((arg_settings_mask & SETTING_CUSTOM_MOUNTS) == 0 &&
4924 settings->n_custom_mounts > 0) {
4925
4926 if (!arg_settings_trusted)
4927 log_warning("Ignoring TemporaryFileSystem=, Bind= and BindReadOnly= settings, file %s is not trusted.", path);
4928 else {
4929 custom_mount_free_all(arg_custom_mounts, arg_n_custom_mounts);
4930 arg_custom_mounts = TAKE_PTR(settings->custom_mounts);
4931 arg_n_custom_mounts = settings->n_custom_mounts;
4932 settings->n_custom_mounts = 0;
4933 }
4934 }
4935
4936 if ((arg_settings_mask & SETTING_NETWORK) == 0 &&
4937 settings_network_configured(settings)) {
4938
4939 if (!arg_settings_trusted)
4940 log_warning("Ignoring network settings, file %s is not trusted.", path);
4941 else {
4942 arg_network_veth = settings_network_veth(settings);
4943 arg_private_network = settings_private_network(settings);
4944
4945 strv_free_and_replace(arg_network_interfaces, settings->network_interfaces);
4946 strv_free_and_replace(arg_network_macvlan, settings->network_macvlan);
4947 strv_free_and_replace(arg_network_ipvlan, settings->network_ipvlan);
4948 strv_free_and_replace(arg_network_veth_extra, settings->network_veth_extra);
4949
4950 free_and_replace(arg_network_bridge, settings->network_bridge);
4951 free_and_replace(arg_network_zone, settings->network_zone);
4952
4953 free_and_replace(arg_network_namespace_path, settings->network_namespace_path);
4954 }
4955 }
4956
4957 if ((arg_settings_mask & SETTING_EXPOSE_PORTS) == 0 &&
4958 settings->expose_ports) {
4959
4960 if (!arg_settings_trusted)
4961 log_warning("Ignoring Port= setting, file %s is not trusted.", path);
4962 else {
4963 expose_port_free_all(arg_expose_ports);
4964 arg_expose_ports = TAKE_PTR(settings->expose_ports);
4965 }
4966 }
4967
4968 if ((arg_settings_mask & SETTING_USERNS) == 0 &&
4969 settings->userns_mode != _USER_NAMESPACE_MODE_INVALID) {
4970
4971 if (!arg_settings_trusted)
4972 log_warning("Ignoring PrivateUsers= and PrivateUsersChown= settings, file %s is not trusted.", path);
4973 else {
4974 arg_userns_mode = settings->userns_mode;
4975 arg_uid_shift = settings->uid_shift;
4976 arg_uid_range = settings->uid_range;
4977 arg_userns_ownership = settings->userns_ownership;
4978 arg_delegate_container_ranges = settings->delegate_container_ranges;
4979 }
4980 }
4981
4982 if ((arg_settings_mask & SETTING_BIND_USER) == 0 &&
4983 !strv_isempty(settings->bind_user)) {
4984
4985 if (!arg_settings_trusted)
4986 log_warning("Ignoring bind user setting, file %s is not trusted.", path);
4987 else
4988 strv_free_and_replace(arg_bind_user, settings->bind_user);
4989 }
4990
4991 if (!FLAGS_SET(arg_settings_mask, SETTING_BIND_USER_SHELL) &&
4992 settings->bind_user_shell_set) {
4993
4994 if (!arg_settings_trusted)
4995 log_warning("Ignoring bind user shell setting, file %s is not trusted.", path);
4996 else {
4997 free_and_replace(arg_bind_user_shell, settings->bind_user_shell);
4998 arg_bind_user_shell_copy = settings->bind_user_shell_copy;
4999 }
5000 }
5001
5002 if ((arg_settings_mask & SETTING_NOTIFY_READY) == 0 &&
5003 settings->notify_ready >= 0)
5004 arg_notify_ready = settings->notify_ready;
5005
5006 if ((arg_settings_mask & SETTING_SYSCALL_FILTER) == 0) {
5007
5008 if (!strv_isempty(settings->syscall_allow_list) || !strv_isempty(settings->syscall_deny_list)) {
5009 if (!arg_settings_trusted && !strv_isempty(settings->syscall_allow_list))
5010 log_warning("Ignoring SystemCallFilter= settings, file %s is not trusted.", path);
5011 else {
5012 strv_free_and_replace(arg_syscall_allow_list, settings->syscall_allow_list);
5013 strv_free_and_replace(arg_syscall_deny_list, settings->syscall_deny_list);
5014 }
5015 }
5016
5017 #if HAVE_SECCOMP
5018 if (settings->seccomp) {
5019 if (!arg_settings_trusted)
5020 log_warning("Ignoring SECCOMP filter, file %s is not trusted.", path);
5021 else {
5022 sym_seccomp_release(arg_seccomp);
5023 arg_seccomp = TAKE_PTR(settings->seccomp);
5024 }
5025 }
5026 #endif
5027 }
5028
5029 for (rl = 0; rl < _RLIMIT_MAX; rl++) {
5030 if ((arg_settings_mask & (SETTING_RLIMIT_FIRST << rl)))
5031 continue;
5032
5033 if (!settings->rlimit[rl])
5034 continue;
5035
5036 if (!arg_settings_trusted) {
5037 log_warning("Ignoring Limit%s= setting, file '%s' is not trusted.", rlimit_to_string(rl), path);
5038 continue;
5039 }
5040
5041 free_and_replace(arg_rlimit[rl], settings->rlimit[rl]);
5042 }
5043
5044 if ((arg_settings_mask & SETTING_HOSTNAME) == 0 &&
5045 settings->hostname)
5046 free_and_replace(arg_hostname, settings->hostname);
5047
5048 if ((arg_settings_mask & SETTING_NO_NEW_PRIVILEGES) == 0 &&
5049 settings->no_new_privileges >= 0)
5050 arg_no_new_privileges = settings->no_new_privileges;
5051
5052 if ((arg_settings_mask & SETTING_OOM_SCORE_ADJUST) == 0 &&
5053 settings->oom_score_adjust_set) {
5054
5055 if (!arg_settings_trusted)
5056 log_warning("Ignoring OOMScoreAdjust= setting, file '%s' is not trusted.", path);
5057 else {
5058 arg_oom_score_adjust = settings->oom_score_adjust;
5059 arg_oom_score_adjust_set = true;
5060 }
5061 }
5062
5063 if ((arg_settings_mask & SETTING_CPU_AFFINITY) == 0 &&
5064 settings->cpu_set.set) {
5065
5066 if (!arg_settings_trusted)
5067 log_warning("Ignoring CPUAffinity= setting, file '%s' is not trusted.", path);
5068 else
5069 cpu_set_done_and_replace(arg_cpu_set, settings->cpu_set);
5070 }
5071
5072 if ((arg_settings_mask & SETTING_RESOLV_CONF) == 0 &&
5073 settings->resolv_conf != _RESOLV_CONF_MODE_INVALID)
5074 arg_resolv_conf = settings->resolv_conf;
5075
5076 if ((arg_settings_mask & SETTING_LINK_JOURNAL) == 0 &&
5077 settings->link_journal != _LINK_JOURNAL_INVALID) {
5078
5079 if (!arg_settings_trusted)
5080 log_warning("Ignoring journal link setting, file '%s' is not trusted.", path);
5081 else {
5082 arg_link_journal = settings->link_journal;
5083 arg_link_journal_try = settings->link_journal_try;
5084 }
5085 }
5086
5087 if ((arg_settings_mask & SETTING_TIMEZONE) == 0 &&
5088 settings->timezone != _TIMEZONE_MODE_INVALID)
5089 arg_timezone = settings->timezone;
5090
5091 if ((arg_settings_mask & SETTING_SLICE) == 0 &&
5092 settings->slice) {
5093
5094 if (!arg_settings_trusted)
5095 log_warning("Ignoring slice setting, file '%s' is not trusted.", path);
5096 else
5097 free_and_replace(arg_slice, settings->slice);
5098 }
5099
5100 if ((arg_settings_mask & SETTING_USE_CGNS) == 0 &&
5101 settings->use_cgns >= 0) {
5102
5103 if (!arg_settings_trusted)
5104 log_warning("Ignoring cgroup namespace setting, file '%s' is not trusted.", path);
5105 else
5106 arg_use_cgns = settings->use_cgns;
5107 }
5108
5109 if ((arg_settings_mask & SETTING_CLONE_NS_FLAGS) == 0 &&
5110 settings->clone_ns_flags != ULONG_MAX) {
5111
5112 if (!arg_settings_trusted)
5113 log_warning("Ignoring namespace setting, file '%s' is not trusted.", path);
5114 else
5115 arg_clone_ns_flags = settings->clone_ns_flags;
5116 }
5117
5118 if ((arg_settings_mask & SETTING_CONSOLE_MODE) == 0 &&
5119 settings->console_mode >= 0) {
5120
5121 if (!arg_settings_trusted)
5122 log_warning("Ignoring console mode setting, file '%s' is not trusted.", path);
5123 else
5124 arg_console_mode = settings->console_mode;
5125 }
5126
5127 if ((arg_settings_mask & SETTING_SUPPRESS_SYNC) == 0 &&
5128 settings->suppress_sync >= 0)
5129 arg_suppress_sync = settings->suppress_sync;
5130
5131 if (!FLAGS_SET(arg_settings_mask, SETTING_RESTRICT_ADDRESS_FAMILIES) &&
5132 (settings->restrict_address_families || settings->restrict_address_families_is_allowlist)) {
5133 set_free_and_replace(arg_restrict_address_families, settings->restrict_address_families);
5134 arg_restrict_address_families_is_allowlist = settings->restrict_address_families_is_allowlist;
5135 }
5136
5137 /* The following properties can only be set through the OCI settings logic, not from the command line, hence we
5138 * don't consult arg_settings_mask for them. */
5139
5140 sd_bus_message_unref(arg_property_message);
5141 arg_property_message = TAKE_PTR(settings->properties);
5142
5143 arg_console_width = settings->console_width;
5144 arg_console_height = settings->console_height;
5145
5146 device_node_array_free(arg_extra_nodes, arg_n_extra_nodes);
5147 arg_extra_nodes = TAKE_PTR(settings->extra_nodes);
5148 arg_n_extra_nodes = settings->n_extra_nodes;
5149 settings->n_extra_nodes = 0;
5150
5151 return 0;
5152 }
5153
5154 static int load_settings(void) {
5155 _cleanup_(settings_freep) Settings *settings = NULL;
5156 _cleanup_fclose_ FILE *f = NULL;
5157 _cleanup_free_ char *p = NULL;
5158 int r;
5159
5160 if (arg_oci_bundle)
5161 return 0;
5162
5163 /* If all settings are masked, there's no point in looking for
5164 * the settings file */
5165 if (FLAGS_SET(arg_settings_mask, _SETTINGS_MASK_ALL))
5166 return 0;
5167
5168 /* We first look in the admin's directories in /etc/ and /run/ */
5169 static const uint64_t lookup_dir_system[] = {
5170 SD_PATH_SYSTEM_CONFIGURATION,
5171 SD_PATH_SYSTEM_RUNTIME,
5172 _SD_PATH_INVALID,
5173 };
5174 static const uint64_t lookup_dir_user[] = {
5175 SD_PATH_USER_CONFIGURATION,
5176 SD_PATH_USER_RUNTIME,
5177 _SD_PATH_INVALID,
5178 };
5179
5180 const uint64_t *q = arg_runtime_scope == RUNTIME_SCOPE_SYSTEM ? lookup_dir_system : lookup_dir_user;
5181 for (; *q != _SD_PATH_INVALID; q++) {
5182 _cleanup_free_ char *cd = NULL;
5183 r = sd_path_lookup(*q, "systemd/nspawn", &cd);
5184 if (r < 0) {
5185 log_warning_errno(r, "Failed to determine settings directory, ignoring: %m");
5186 continue;
5187 }
5188
5189 _cleanup_free_ char *j = NULL;
5190 j = path_join(cd, arg_settings_filename);
5191 if (!j)
5192 return log_oom();
5193
5194 f = fopen(j, "re");
5195 if (f) {
5196 p = TAKE_PTR(j);
5197
5198 log_debug("Found settings file: %s", p);
5199
5200 /* By default, we trust configuration from /etc and /run */
5201 if (arg_settings_trusted < 0)
5202 arg_settings_trusted = true;
5203
5204 break;
5205 }
5206
5207 if (errno != ENOENT)
5208 return log_error_errno(errno, "Failed to open %s: %m", j);
5209 }
5210
5211 if (!f) {
5212 /* After that, let's look for a file next to the
5213 * actual image we shall boot. */
5214
5215 if (arg_image) {
5216 r = file_in_same_dir(arg_image, arg_settings_filename, &p);
5217 if (r < 0)
5218 return log_error_errno(r, "Failed to generate settings path from image path: %m");
5219 } else if (arg_directory) {
5220 r = file_in_same_dir(arg_directory, arg_settings_filename, &p);
5221 if (r < 0 && r != -EADDRNOTAVAIL) /* if directory is root fs, don't complain */
5222 return log_error_errno(r, "Failed to generate settings path from directory path: %m");
5223 } else if (arg_mstack) {
5224 r = file_in_same_dir(arg_mstack, arg_settings_filename, &p);
5225 if (r < 0)
5226 return log_error_errno(r, "Failed to generate settings path from mstack path: %m");
5227 }
5228
5229 if (p) {
5230 f = fopen(p, "re");
5231 if (!f && errno != ENOENT)
5232 return log_error_errno(errno, "Failed to open %s: %m", p);
5233
5234 if (f)
5235 log_debug("Found settings file: %s", p);
5236
5237 /* By default, we do not trust configuration from /var/lib/machines */
5238 if (arg_settings_trusted < 0)
5239 arg_settings_trusted = false;
5240 }
5241 }
5242
5243 if (!f)
5244 return 0;
5245
5246 log_debug("Settings are trusted: %s", yes_no(arg_settings_trusted));
5247
5248 r = settings_load(f, p, &settings);
5249 if (r < 0)
5250 return r;
5251
5252 return merge_settings(settings, p);
5253 }
5254
5255 static int load_oci_bundle(void) {
5256 _cleanup_(settings_freep) Settings *settings = NULL;
5257 int r;
5258
5259 if (!arg_oci_bundle)
5260 return 0;
5261
5262 /* By default let's trust OCI bundles */
5263 if (arg_settings_trusted < 0)
5264 arg_settings_trusted = true;
5265
5266 r = oci_load(NULL, arg_oci_bundle, &settings);
5267 if (r < 0)
5268 return r;
5269
5270 return merge_settings(settings, arg_oci_bundle);
5271 }
5272
5273 static int run_container(
5274 const char *runtime_dir,
5275 const char *directory,
5276 int mount_fd,
5277 DissectedImage *dissected_image,
5278 MStack *mstack,
5279 int userns_fd,
5280 FDSet *fds,
5281 char veth_name[IFNAMSIZ],
5282 bool *veth_created,
5283 struct ExposeArgs *expose_args,
5284 int *master,
5285 PidRef *pid,
5286 int *ret) {
5287
5288 _cleanup_(release_lock_file) LockFile uid_shift_lock = LOCK_FILE_INIT;
5289 _cleanup_close_ int etc_passwd_lock = -EBADF;
5290 _cleanup_close_pair_ int
5291 fd_inner_socket_pair[2] = EBADF_PAIR,
5292 fd_outer_socket_pair[2] = EBADF_PAIR;
5293
5294 _cleanup_close_ int notify_socket = -EBADF, mntns_fd = -EBADF, fd_kmsg_fifo = -EBADF;
5295 _cleanup_(barrier_destroy) Barrier barrier = BARRIER_NULL;
5296 _cleanup_(sd_event_source_unrefp) sd_event_source *notify_event_source = NULL;
5297 _cleanup_(umount_and_rmdir_and_freep) char *unix_export_host_dir = NULL;
5298 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
5299 _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
5300 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
5301 _cleanup_free_ uid_t *bind_user_uid = NULL;
5302 size_t n_bind_user_uid = 0;
5303 ContainerStatus container_status = 0;
5304 int ifi = 0, r;
5305 ssize_t l;
5306 sigset_t mask_chld;
5307 _cleanup_close_ int child_netns_fd = -EBADF;
5308
5309 assert_se(sigemptyset(&mask_chld) == 0);
5310 assert_se(sigaddset(&mask_chld, SIGCHLD) == 0);
5311
5312 /* Set up the unix export host directory on the host first */
5313 r = setup_unix_export_dir_outside(runtime_dir, &unix_export_host_dir);
5314 if (r < 0)
5315 return r;
5316
5317 if (arg_userns_mode == USER_NAMESPACE_PICK) {
5318 /* When we shall pick the UID/GID range, let's first lock /etc/passwd, so that we can safely
5319 * check with getpwuid() if the specific user already exists. Note that /etc might be
5320 * read-only, in which case this will fail with EROFS. But that's really OK, as in that case we
5321 * can be reasonably sure that no users are going to be added. Note that getpwuid() checks are
5322 * really just an extra safety net. We kinda assume that the UID range we allocate from is
5323 * really ours. */
5324
5325 etc_passwd_lock = take_etc_passwd_lock(NULL);
5326 if (etc_passwd_lock < 0 && etc_passwd_lock != -EROFS)
5327 return log_error_errno(etc_passwd_lock, "Failed to take /etc/passwd lock: %m");
5328 }
5329
5330 r = barrier_create(&barrier);
5331 if (r < 0)
5332 return log_error_errno(r, "Cannot initialize IPC barrier: %m");
5333
5334 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, fd_inner_socket_pair) < 0)
5335 return log_error_errno(errno, "Failed to create inner socket pair: %m");
5336
5337 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, fd_outer_socket_pair) < 0)
5338 return log_error_errno(errno, "Failed to create outer socket pair: %m");
5339
5340 /* Child can be killed before execv(), so handle SIGCHLD in order to interrupt
5341 * parent's blocking calls and give it a chance to call wait() and terminate. */
5342 r = sigprocmask(SIG_UNBLOCK, &mask_chld, NULL);
5343 if (r < 0)
5344 return log_error_errno(errno, "Failed to change the signal mask: %m");
5345
5346 r = sigaction(SIGCHLD, &sigaction_nop_nocldstop, NULL);
5347 if (r < 0)
5348 return log_error_errno(errno, "Failed to install SIGCHLD handler: %m");
5349
5350 if (arg_network_namespace_path) {
5351 child_netns_fd = open(arg_network_namespace_path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
5352 if (child_netns_fd < 0)
5353 return log_error_errno(errno, "Cannot open file %s: %m", arg_network_namespace_path);
5354
5355 r = fd_is_namespace(child_netns_fd, NAMESPACE_NET);
5356 if (r < 0)
5357 return log_error_errno(r, "Failed to check %s fs type: %m", arg_network_namespace_path);
5358 if (r == 0)
5359 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
5360 "Path %s doesn't refer to a network namespace, refusing.", arg_network_namespace_path);
5361 }
5362
5363 bool in_child;
5364 if (arg_userns_mode != USER_NAMESPACE_MANAGED) {
5365 assert(userns_fd < 0);
5366 /* If we have no user namespace then we'll clone and create a new mount namespace right-away. */
5367
5368 pid_t _pid = raw_clone(SIGCHLD|CLONE_NEWNS);
5369 if (_pid < 0)
5370 return log_error_errno(errno, "clone() failed%s: %m",
5371 errno == EINVAL ?
5372 ", do you have namespace support enabled in your kernel? (You need UTS, IPC, PID and NET namespacing built in)" : "");
5373 if (_pid != 0) {
5374 r = pidref_set_pid(pid, _pid);
5375 if (r < 0)
5376 return log_error_errno(r, "Failed to allocate pidfd: %m");
5377 }
5378
5379 in_child = _pid == 0;
5380 } else {
5381 assert(userns_fd >= 0);
5382 /* If we have a user namespace then we'll clone() first, and then join the user namespace,
5383 * and then open the mount namespace, so that it is owned by the user namespace */
5384
5385 pid_t _pid = raw_clone(SIGCHLD);
5386 if (_pid < 0)
5387 return log_error_errno(errno, "clone() failed: %m");
5388
5389 if (_pid != 0) {
5390 r = pidref_set_pid(pid, _pid);
5391 if (r < 0)
5392 return log_error_errno(r, "Failed to allocate pidfd: %m");
5393 }
5394
5395 in_child = _pid == 0;
5396 if (in_child) {
5397 if (setns(userns_fd, CLONE_NEWUSER) < 0) {
5398 log_error_errno(errno, "Failed to join allocate user namespace: %m");
5399 _exit(EXIT_FAILURE);
5400 }
5401
5402 r = reset_uid_gid();
5403 if (r < 0) {
5404 log_error_errno(r, "Failed to reset UID/GID to root: %m");
5405 _exit(EXIT_FAILURE);
5406 }
5407
5408 if (unshare(CLONE_NEWNS) < 0) {
5409 log_error_errno(errno, "Failed to unshare file system namespace: %m");
5410 _exit(EXIT_FAILURE);
5411 }
5412 }
5413 }
5414
5415 if (in_child) {
5416 /* The outer child only has a file system namespace. */
5417 barrier_set_role(&barrier, BARRIER_CHILD);
5418
5419 fd_inner_socket_pair[0] = safe_close(fd_inner_socket_pair[0]);
5420 fd_outer_socket_pair[0] = safe_close(fd_outer_socket_pair[0]);
5421
5422 (void) reset_all_signal_handlers();
5423 (void) reset_signal_mask();
5424
5425 r = outer_child(&barrier,
5426 directory,
5427 mount_fd,
5428 dissected_image,
5429 mstack,
5430 fd_outer_socket_pair[1],
5431 fd_inner_socket_pair[1],
5432 fds,
5433 child_netns_fd,
5434 unix_export_host_dir);
5435 if (r < 0)
5436 _exit(EXIT_FAILURE);
5437
5438 _exit(EXIT_SUCCESS);
5439 }
5440
5441 barrier_set_role(&barrier, BARRIER_PARENT);
5442
5443 fdset_close(fds, /* async= */ false);
5444
5445 fd_inner_socket_pair[1] = safe_close(fd_inner_socket_pair[1]);
5446 fd_outer_socket_pair[1] = safe_close(fd_outer_socket_pair[1]);
5447
5448 if (arg_userns_mode != USER_NAMESPACE_NO) {
5449 mntns_fd = receive_one_fd(fd_outer_socket_pair[0], 0);
5450 if (mntns_fd < 0)
5451 return log_error_errno(mntns_fd, "Failed to receive mount namespace fd from outer child: %m");
5452
5453 /* The child just let us know the UID shift it might have read from the image. */
5454 l = recv(fd_outer_socket_pair[0], &arg_uid_shift, sizeof arg_uid_shift, 0);
5455 if (l < 0)
5456 return log_error_errno(errno, "Failed to read UID shift: %m");
5457 if (l != sizeof arg_uid_shift)
5458 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading UID shift.");
5459
5460 if (arg_userns_mode == USER_NAMESPACE_PICK) {
5461 /* If we are supposed to pick the UID shift, let's try to use the shift read from the
5462 * image, but if that's already in use, pick a new one, and report back to the child,
5463 * which one we now picked. */
5464
5465 r = uid_shift_pick(&arg_uid_shift, &uid_shift_lock);
5466 if (r < 0)
5467 return log_error_errno(r, "Failed to pick suitable UID/GID range: %m");
5468
5469 l = send(fd_outer_socket_pair[0], &arg_uid_shift, sizeof arg_uid_shift, MSG_NOSIGNAL);
5470 if (l < 0)
5471 return log_error_errno(errno, "Failed to send UID shift: %m");
5472 if (l != sizeof arg_uid_shift)
5473 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write while writing UID shift.");
5474 }
5475 }
5476
5477 /* Wait for the outer child. */
5478 r = pidref_wait_for_terminate_and_check("(sd-namespace)", pid, WAIT_LOG_ABNORMAL);
5479 if (r < 0)
5480 return r;
5481 pidref_done(pid);
5482 if (r != EXIT_SUCCESS)
5483 return -EIO;
5484
5485 /* And now retrieve the PID of the inner child. */
5486 pid_t _pid;
5487 l = recv(fd_outer_socket_pair[0], &_pid, sizeof _pid, 0);
5488 if (l < 0)
5489 return log_error_errno(errno, "Failed to read inner child PID: %m");
5490 if (l != sizeof _pid)
5491 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading inner child PID.");
5492
5493 r = pidref_set_pid(pid, _pid);
5494 if (r < 0)
5495 return log_error_errno(r, "Failed to allocate pidfd: %m");
5496
5497 /* We also retrieve container UUID in case it was generated by outer child */
5498 l = recv(fd_outer_socket_pair[0], &arg_uuid, sizeof arg_uuid, 0);
5499 if (l < 0)
5500 return log_error_errno(errno, "Failed to read container machine ID: %m");
5501 if (l != sizeof(arg_uuid))
5502 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading container machined ID.");
5503
5504 /* We also retrieve the socket used for notifications generated by outer child */
5505 notify_socket = receive_one_fd(fd_outer_socket_pair[0], 0);
5506 if (notify_socket < 0)
5507 return log_error_errno(notify_socket,
5508 "Failed to receive notification socket from the outer child: %m");
5509
5510 log_debug("Init process invoked as PID "PID_FMT, pid->pid);
5511
5512 if (arg_userns_mode != USER_NAMESPACE_NO) {
5513 if (!barrier_place_and_sync(&barrier)) /* #1 */
5514 return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Child died too early.");
5515
5516 if (arg_userns_mode != USER_NAMESPACE_MANAGED) {
5517 r = setup_uid_map(pid, bind_user_uid, n_bind_user_uid);
5518 if (r < 0)
5519 return r;
5520 }
5521
5522 (void) barrier_place(&barrier); /* #2 */
5523 }
5524
5525 if (arg_private_network) {
5526 if (!arg_network_namespace_path) {
5527 /* Wait until the child has unshared its network namespace. */
5528 if (!barrier_place_and_sync(&barrier)) /* #3 */
5529 return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Child died too early");
5530
5531 /* Make sure we have an open file descriptor to the child's network namespace so it
5532 * stays alive even if the child exits. */
5533 assert(child_netns_fd < 0);
5534 child_netns_fd = receive_one_fd(fd_inner_socket_pair[0], 0);
5535 if (child_netns_fd < 0)
5536 return log_error_errno(child_netns_fd, "Failed to receive child network namespace: %m");
5537 }
5538
5539 r = move_network_interfaces(child_netns_fd, arg_network_interfaces);
5540 if (r < 0)
5541 return r;
5542
5543 if (arg_network_veth) {
5544 if (arg_userns_mode != USER_NAMESPACE_MANAGED) {
5545 r = setup_veth(arg_machine, pid, veth_name,
5546 arg_network_bridge || arg_network_zone, &arg_network_provided_mac);
5547 if (r < 0)
5548 return r;
5549 else if (r > 0)
5550 ifi = r;
5551 } else {
5552 _cleanup_free_ char *host_ifname = NULL;
5553
5554 r = nsresource_add_netif_veth(
5555 /* vl= */ NULL,
5556 userns_fd,
5557 child_netns_fd,
5558 /* namespace_ifname= */ NULL,
5559 &host_ifname,
5560 /* ret_namespace_ifname= */ NULL);
5561 if (r < 0)
5562 return log_error_errno(r, "Failed to add network interface to container: %m");
5563
5564 ifi = if_nametoindex(host_ifname);
5565 if (ifi == 0)
5566 return log_error_errno(errno, "Failed to resolve interface '%s': %m", host_ifname);
5567
5568 if (strlen(host_ifname) >= IFNAMSIZ)
5569 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Host interface name too long?");
5570
5571 strcpy(veth_name, host_ifname);
5572 }
5573
5574 if (arg_network_bridge) {
5575 /* Add the interface to a bridge */
5576 r = setup_bridge(veth_name, arg_network_bridge, false);
5577 if (r < 0)
5578 return r;
5579 if (r > 0)
5580 ifi = r;
5581 } else if (arg_network_zone) {
5582 /* Add the interface to a bridge, possibly creating it */
5583 r = setup_bridge(veth_name, arg_network_zone, true);
5584 if (r < 0)
5585 return r;
5586 if (r > 0)
5587 ifi = r;
5588 }
5589 }
5590
5591 r = setup_veth_extra(arg_machine, pid, arg_network_veth_extra);
5592 if (r < 0)
5593 return r;
5594
5595 /* We created the primary and extra veth links now; let's remember this, so that we know to
5596 remove them later on. Note that we don't bother with removing veth links that were created
5597 here when their setup failed half-way, because in that case the kernel should be able to
5598 remove them on its own, since they cannot be referenced by anything yet. */
5599 *veth_created = true;
5600
5601 r = setup_macvlan(arg_machine, pid, arg_network_macvlan);
5602 if (r < 0)
5603 return r;
5604
5605 r = setup_ipvlan(arg_machine, pid, arg_network_ipvlan);
5606 if (r < 0)
5607 return r;
5608 }
5609
5610 /* Registration always happens on the system bus */
5611 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *system_bus = NULL;
5612 if (arg_register != 0 || (arg_runtime_scope == RUNTIME_SCOPE_SYSTEM && !arg_keep_unit)) {
5613 r = sd_bus_default_system(&system_bus);
5614 if (r < 0)
5615 return log_error_errno(r, "Failed to open system bus: %m");
5616
5617 r = sd_bus_set_close_on_exit(system_bus, false);
5618 if (r < 0)
5619 return log_error_errno(r, "Failed to disable close-on-exit behaviour: %m");
5620
5621 (void) sd_bus_set_allow_interactive_authorization(system_bus, arg_ask_password);
5622 }
5623
5624 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *user_bus = NULL;
5625 _cleanup_(sd_bus_unrefp) sd_bus *runtime_bus = NULL;
5626
5627 if (arg_register != 0 || !arg_keep_unit) {
5628 if (arg_runtime_scope == RUNTIME_SCOPE_SYSTEM)
5629 runtime_bus = sd_bus_ref(system_bus);
5630 else {
5631 r = sd_bus_default_user(&user_bus);
5632 if (r < 0)
5633 return log_error_errno(r, "Failed to open user bus: %m");
5634
5635 r = sd_bus_set_close_on_exit(user_bus, false);
5636 if (r < 0)
5637 return log_error_errno(r, "Failed to disable close-on-exit behaviour: %m");
5638
5639 runtime_bus = sd_bus_ref(user_bus);
5640 }
5641 }
5642
5643 /* Scope allocation happens on the user bus if we are unpriv, otherwise system bus. */
5644 if (arg_keep_unit) {
5645 /* If we are not supposed to allocate a unit, then let's move the process now, so that we can
5646 * register things while being in the right cgroup location already. Otherwise, let's move
5647 * the process later, once we have unit and hence cgroup. */
5648 r = create_subcgroup(
5649 pid,
5650 arg_keep_unit,
5651 arg_uid_shift,
5652 userns_fd,
5653 arg_userns_mode);
5654 if (r < 0)
5655 return r;
5656 } else {
5657 /* When a new scope is created for this container, then we'll be registered as its controller, in which
5658 * case PID 1 will send us a friendly RequestStop signal, when it is asked to terminate the
5659 * scope. Let's hook into that, and cleanly shut down the container, and print a friendly message. */
5660
5661 r = sd_bus_match_signal_async(
5662 runtime_bus,
5663 /* ret= */ NULL,
5664 "org.freedesktop.systemd1",
5665 /* path= */ NULL,
5666 "org.freedesktop.systemd1.Scope",
5667 "RequestStop",
5668 on_request_stop,
5669 /* install_callback= */ NULL,
5670 pid);
5671 if (r < 0)
5672 return log_error_errno(r, "Failed to request RequestStop match: %m");
5673 }
5674
5675 bool scope_allocated = false;
5676 if (!arg_keep_unit) {
5677 AllocateScopeFlags flags = ALLOCATE_SCOPE_ALLOW_PIDFD;
5678 r = allocate_scope(
5679 runtime_bus,
5680 arg_machine,
5681 pid,
5682 arg_slice,
5683 arg_custom_mounts, arg_n_custom_mounts,
5684 arg_kill_signal,
5685 arg_property,
5686 arg_property_message,
5687 arg_start_mode,
5688 flags);
5689 if (r < 0)
5690 return r;
5691
5692 scope_allocated = true;
5693 }
5694
5695 MachineRegistrationContext machine_ctx = {
5696 .scope = arg_runtime_scope == RUNTIME_SCOPE_SYSTEM ? RUNTIME_SCOPE_SYSTEM : _RUNTIME_SCOPE_INVALID,
5697 .system_bus = system_bus,
5698 .user_bus = runtime_bus,
5699 };
5700 if (arg_register != 0) {
5701 const MachineRegistration reg = {
5702 .name = arg_machine,
5703 .id = arg_uuid,
5704 .service = arg_container_service_name,
5705 .class = "container",
5706 .pidref = pid,
5707 .root_directory = arg_directory,
5708 .local_ifindex = ifi,
5709 };
5710
5711 r = register_machine_with_fallback_and_log(
5712 &machine_ctx,
5713 &reg,
5714 /* graceful= */ arg_register < 0);
5715 if (r < 0)
5716 return r;
5717 }
5718
5719 if (arg_keep_unit && (arg_slice || arg_property))
5720 log_notice("Machine and scope registration turned off, --slice= and --property= settings will have no effect.");
5721
5722 if (!arg_keep_unit) {
5723 r = create_subcgroup(
5724 pid,
5725 arg_keep_unit,
5726 arg_uid_shift,
5727 userns_fd,
5728 arg_userns_mode);
5729 if (r < 0)
5730 return r;
5731 }
5732
5733 /* Notify the child that the parent is ready with all its setup (including cgroup-ification), and
5734 * that the child can now hand over control to the code to run inside the container. */
5735 (void) barrier_place(&barrier); /* #4 */
5736
5737 /* Block SIGCHLD here, before notifying child.
5738 * process_pty() will handle it with the other signals. */
5739 assert_se(sigprocmask(SIG_BLOCK, &mask_chld, NULL) >= 0);
5740
5741 /* Reset signal to default */
5742 r = default_signals(SIGCHLD);
5743 if (r < 0)
5744 return log_error_errno(r, "Failed to reset SIGCHLD: %m");
5745
5746 r = sd_event_new(&event);
5747 if (r < 0)
5748 return log_error_errno(r, "Failed to get default event source: %m");
5749
5750 (void) sd_event_set_watchdog(event, true);
5751
5752 if (system_bus) {
5753 r = sd_bus_attach_event(system_bus, event, 0);
5754 if (r < 0)
5755 return log_error_errno(r, "Failed to attach system bus to event loop: %m");
5756 }
5757
5758 if (user_bus) {
5759 r = sd_bus_attach_event(user_bus, event, 0);
5760 if (r < 0)
5761 return log_error_errno(r, "Failed to attach user bus to event loop: %m");
5762 }
5763
5764 r = setup_notify_parent(event, notify_socket, pid, &notify_event_source);
5765 if (r < 0)
5766 return r;
5767
5768 /* Wait that the child is completely ready now, and has mounted their own copies of procfs and so on,
5769 * before we take the fully visible instances away. */
5770 if (!barrier_sync(&barrier)) /* #5.1 */
5771 return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Child died too early.");
5772
5773 if (!IN_SET(arg_userns_mode, USER_NAMESPACE_NO, USER_NAMESPACE_MANAGED)) {
5774 r = wipe_fully_visible_api_fs(mntns_fd);
5775 if (r < 0)
5776 return r;
5777 mntns_fd = safe_close(mntns_fd);
5778 }
5779
5780 /* And now let the child know that we completed removing the procfs instances, and it can start the
5781 * payload. */
5782 if (!barrier_place(&barrier)) /* #5.2 */
5783 return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Child died too early.");
5784
5785 /* At this point we have made use of the UID we picked, and thus nss-systemd/systemd-machined.service
5786 * will make them appear in getpwuid(), thus we can release the /etc/passwd lock. */
5787 etc_passwd_lock = safe_close(etc_passwd_lock);
5788
5789 (void) sd_notifyf(/* unset_environment= */ false,
5790 "STATUS=Container started.\n"
5791 "X_NSPAWN_LEADER_PID=" PID_FMT, pid->pid);
5792 if (!arg_notify_ready) {
5793 r = sd_notify(/* unset_environment= */ false, "READY=1\n");
5794 if (r < 0)
5795 log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
5796 }
5797
5798 /* All operations that might need Polkit authorizations (i.e. userns registration, mounts, and
5799 * machine registration are complete now, get rid of the agent again, so that we retain exclusive
5800 * control of the TTY from now on. */
5801 polkit_agent_close();
5802
5803 /* Note: we do not use SD_EVENT_SIGNAL_PROCMASK or sd_event_set_signal_exit(), since we want the
5804 * signals to be block continuously, even if we destroy the event loop and allocate a new one on
5805 * container reboot. */
5806
5807 if (arg_kill_signal > 0) {
5808 /* Try to kill the init system on SIGINT or SIGTERM */
5809 (void) sd_event_add_signal(event, NULL, SIGINT, on_orderly_shutdown, pid);
5810 (void) sd_event_add_signal(event, NULL, SIGTERM, on_orderly_shutdown, pid);
5811 } else {
5812 /* Immediately exit */
5813 (void) sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
5814 (void) sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
5815 }
5816
5817 (void) sd_event_add_signal(event, NULL, SIGRTMIN+18, sigrtmin18_handler, NULL);
5818
5819 r = sd_event_add_memory_pressure(event, NULL, NULL, NULL);
5820 if (r < 0)
5821 log_debug_errno(r, "Failed to allocate memory pressure event source, ignoring: %m");
5822
5823 /* Exit when the child exits */
5824 (void) sd_event_add_signal(event, NULL, SIGCHLD, on_sigchld, pid);
5825
5826 /* Retrieve the kmsg fifo allocated by inner child */
5827 fd_kmsg_fifo = receive_one_fd(fd_inner_socket_pair[0], 0);
5828 if (fd_kmsg_fifo < 0)
5829 return log_error_errno(fd_kmsg_fifo, "Failed to receive kmsg fifo from inner child: %m");
5830
5831 if (arg_expose_ports) {
5832 r = expose_port_watch_rtnl(event, fd_inner_socket_pair[0], on_address_change, expose_args, &rtnl);
5833 if (r < 0)
5834 return r;
5835
5836 (void) expose_port_execute(rtnl, expose_args->nfnl, arg_expose_ports, AF_INET, &expose_args->address4);
5837 (void) expose_port_execute(rtnl, expose_args->nfnl, arg_expose_ports, AF_INET6, &expose_args->address6);
5838 }
5839
5840 _cleanup_(osc_context_closep) sd_id128_t osc_context_id = SD_ID128_NULL;
5841 if (IN_SET(arg_console_mode, CONSOLE_INTERACTIVE, CONSOLE_READ_ONLY) && !terminal_is_dumb()) {
5842 r = osc_context_open_container(arg_machine, /* ret_seq= */ NULL, &osc_context_id);
5843 if (r < 0)
5844 return r;
5845 }
5846
5847 if (arg_console_mode != CONSOLE_PIPE) {
5848 _cleanup_close_ int fd = -EBADF;
5849 PTYForwardFlags flags = 0;
5850
5851 /* Retrieve the master pty allocated by inner child */
5852 fd = receive_one_fd(fd_inner_socket_pair[0], 0);
5853 if (fd < 0)
5854 return log_error_errno(fd, "Failed to receive master pty from the inner child: %m");
5855
5856 switch (arg_console_mode) {
5857
5858 case CONSOLE_READ_ONLY:
5859 flags |= PTY_FORWARD_READ_ONLY;
5860
5861 _fallthrough_;
5862
5863 case CONSOLE_INTERACTIVE:
5864 flags |= PTY_FORWARD_IGNORE_VHANGUP;
5865
5866 r = pty_forward_new(event, fd, flags, &forward);
5867 if (r < 0)
5868 return log_error_errno(r, "Failed to create PTY forwarder: %m");
5869
5870 if (arg_console_width != UINT_MAX || arg_console_height != UINT_MAX)
5871 (void) pty_forward_set_width_height(
5872 forward,
5873 arg_console_width,
5874 arg_console_height);
5875
5876 if (!arg_background) {
5877 _cleanup_free_ char *bg = NULL;
5878
5879 r = terminal_tint_color(220 /* blue */, &bg);
5880 if (r < 0)
5881 log_debug_errno(r, "Failed to determine terminal background color, not tinting.");
5882 else
5883 (void) pty_forward_set_background_color(forward, bg);
5884 } else if (!isempty(arg_background))
5885 (void) pty_forward_set_background_color(forward, arg_background);
5886
5887 (void) pty_forward_set_window_title(forward, GLYPH_BLUE_CIRCLE, /* hostname= */ NULL,
5888 STRV_MAKE("Container", arg_machine));
5889
5890 pty_forward_set_hotkey_handler(forward, ptyfwd_hotkey, pid);
5891 break;
5892
5893 default:
5894 assert(arg_console_mode == CONSOLE_PASSIVE);
5895 }
5896
5897 *master = TAKE_FD(fd);
5898 }
5899
5900 fd_inner_socket_pair[0] = safe_close(fd_inner_socket_pair[0]);
5901
5902 r = sd_event_loop(event);
5903 if (r < 0)
5904 return log_error_errno(r, "Failed to run event loop: %m");
5905
5906 /* Kill if it is not dead yet anyway */
5907 if (scope_allocated)
5908 terminate_scope(runtime_bus, arg_machine);
5909
5910 /* Normally redundant, but better safe than sorry */
5911 (void) pidref_kill(pid, SIGKILL);
5912
5913 fd_kmsg_fifo = safe_close(fd_kmsg_fifo);
5914
5915 if (arg_private_network && arg_userns_mode != USER_NAMESPACE_MANAGED) {
5916 r = move_back_network_interfaces(child_netns_fd, arg_network_interfaces);
5917 if (r < 0)
5918 return r;
5919
5920 r = remove_macvlan(child_netns_fd, arg_network_macvlan);
5921 if (r < 0)
5922 return r;
5923 }
5924
5925 r = wait_for_container(pid, &container_status);
5926
5927 /* Tell machined that we are gone. */
5928 unregister_machine_with_fallback_and_log(&machine_ctx, arg_machine);
5929
5930 if (r < 0)
5931 /* We failed to wait for the container, or the container exited abnormally. */
5932 return r;
5933 if (r > 0 || container_status == CONTAINER_TERMINATED) {
5934 /* r > 0 → The container exited with a non-zero status.
5935 * As a special case, we need to replace 133 with a different value,
5936 * because 133 is special-cased in the service file to reboot the container.
5937 * otherwise → The container exited with zero status and a reboot was not requested.
5938 */
5939 if (r == EXIT_FORCE_RESTART)
5940 r = EXIT_FAILURE; /* replace 133 with the general failure code */
5941 *ret = r;
5942 return 0; /* finito */
5943 }
5944
5945 /* CONTAINER_REBOOTED, loop again */
5946
5947 if (arg_keep_unit) {
5948 /* Special handling if we are running as a service: instead of simply
5949 * restarting the machine we want to restart the entire service, so let's
5950 * inform systemd about this with the special exit code 133. The service
5951 * file uses RestartForceExitStatus=133 so that this results in a full
5952 * nspawn restart. This is necessary since we might have cgroup parameters
5953 * set we want to have flushed out. */
5954 *ret = EXIT_FORCE_RESTART;
5955 return 0; /* finito */
5956 }
5957
5958 expose_port_flush(expose_args->nfnl, arg_expose_ports, AF_INET, &expose_args->address4);
5959 expose_port_flush(expose_args->nfnl, arg_expose_ports, AF_INET6, &expose_args->address6);
5960
5961 (void) remove_veth_links(veth_name, arg_network_veth_extra);
5962 *veth_created = false;
5963 return 1; /* loop again */
5964 }
5965
5966 static int initialize_rlimits(void) {
5967 /* The default resource limits the kernel passes to PID 1, as per kernel 5.16. Let's pass our container payload
5968 * the same values as the kernel originally passed to PID 1, in order to minimize differences between host and
5969 * container execution environments. */
5970
5971 static const struct rlimit kernel_defaults[_RLIMIT_MAX] = {
5972 [RLIMIT_AS] = { RLIM_INFINITY, RLIM_INFINITY },
5973 [RLIMIT_CORE] = { 0, RLIM_INFINITY },
5974 [RLIMIT_CPU] = { RLIM_INFINITY, RLIM_INFINITY },
5975 [RLIMIT_DATA] = { RLIM_INFINITY, RLIM_INFINITY },
5976 [RLIMIT_FSIZE] = { RLIM_INFINITY, RLIM_INFINITY },
5977 [RLIMIT_LOCKS] = { RLIM_INFINITY, RLIM_INFINITY },
5978 [RLIMIT_MEMLOCK] = { DEFAULT_RLIMIT_MEMLOCK, DEFAULT_RLIMIT_MEMLOCK },
5979 [RLIMIT_MSGQUEUE] = { 819200, 819200 },
5980 [RLIMIT_NICE] = { 0, 0 },
5981 [RLIMIT_NOFILE] = { 1024, 4096 },
5982 [RLIMIT_RSS] = { RLIM_INFINITY, RLIM_INFINITY },
5983 [RLIMIT_RTPRIO] = { 0, 0 },
5984 [RLIMIT_RTTIME] = { RLIM_INFINITY, RLIM_INFINITY },
5985 [RLIMIT_STACK] = { 8388608, RLIM_INFINITY },
5986
5987 /* The kernel scales the default for RLIMIT_NPROC and RLIMIT_SIGPENDING based on the system's amount of
5988 * RAM. To provide best compatibility we'll read these limits off PID 1 instead of hardcoding them
5989 * here. This is safe as we know that PID 1 doesn't change these two limits and thus the original
5990 * kernel's initialization should still be valid during runtime — at least if PID 1 is systemd. Note
5991 * that PID 1 changes a number of other resource limits during early initialization which is why we
5992 * don't read the other limits from PID 1 but prefer the static table above. */
5993 };
5994
5995 int rl, r;
5996
5997 for (rl = 0; rl < _RLIMIT_MAX; rl++) {
5998 /* Let's only fill in what the user hasn't explicitly configured anyway */
5999 if ((arg_settings_mask & (SETTING_RLIMIT_FIRST << rl)) == 0) {
6000 const struct rlimit *v;
6001 struct rlimit buffer;
6002
6003 if (IN_SET(rl, RLIMIT_NPROC, RLIMIT_SIGPENDING)) {
6004 /* For these two let's read the limits off PID 1. See above for an explanation. */
6005
6006 r = pid_getrlimit(1, rl, &buffer);
6007 if (r < 0)
6008 return log_error_errno(r, "Failed to read resource limit RLIMIT_%s of PID 1: %m", rlimit_to_string(rl));
6009
6010 v = &buffer;
6011 } else if (rl == RLIMIT_NOFILE) {
6012 /* We nowadays bump RLIMIT_NOFILE's hard limit early in PID 1 for all
6013 * userspace. Given that nspawn containers are often run without our PID 1,
6014 * let's grant the containers a raised RLIMIT_NOFILE hard limit by default,
6015 * so that container userspace gets similar resources as host userspace
6016 * gets. */
6017 buffer = kernel_defaults[rl];
6018 buffer.rlim_max = MIN((rlim_t) read_nr_open(), (rlim_t) HIGH_RLIMIT_NOFILE);
6019 v = &buffer;
6020 } else
6021 v = kernel_defaults + rl;
6022
6023 arg_rlimit[rl] = newdup(struct rlimit, v, 1);
6024 if (!arg_rlimit[rl])
6025 return log_oom();
6026 }
6027
6028 if (DEBUG_LOGGING) {
6029 _cleanup_free_ char *k = NULL;
6030
6031 (void) rlimit_format(arg_rlimit[rl], &k);
6032 log_debug("Setting RLIMIT_%s to %s.", rlimit_to_string(rl), k);
6033 }
6034 }
6035
6036 return 0;
6037 }
6038
6039 static int cant_be_in_netns(void) {
6040 _cleanup_close_ int fd = -EBADF;
6041 struct ucred ucred;
6042 int r;
6043
6044 /* Check if we are in the same netns as udev. If we aren't, then device monitoring (and thus waiting
6045 * for loopback block devices) won't work, and we will hang. Detect this case and exit early with a
6046 * nice message. */
6047
6048 if (!arg_image) /* only matters if --image= us used, i.e. we actually need to use loopback devices */
6049 return 0;
6050
6051 fd = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
6052 if (fd < 0)
6053 return log_error_errno(errno, "Failed to allocate udev control socket: %m");
6054
6055 r = connect_unix_path(fd, AT_FDCWD, "/run/udev/control");
6056 if (r == -ENOENT || ERRNO_IS_NEG_DISCONNECT(r))
6057 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
6058 "Sorry, but --image= requires access to the host's /run/ hierarchy, since we need access to udev.");
6059 if (ERRNO_IS_NEG_PRIVILEGE(r)) {
6060 log_debug_errno(r, "Can't connect to udev control socket, assuming we are in same netns.");
6061 return 0;
6062 }
6063 if (r < 0)
6064 return log_error_errno(r, "Failed to connect socket to udev control socket: %m");
6065
6066 r = getpeercred(fd, &ucred);
6067 if (r < 0)
6068 return log_error_errno(r, "Failed to determine peer of udev control socket: %m");
6069
6070 r = in_same_namespace(ucred.pid, 0, NAMESPACE_NET);
6071 if (r < 0)
6072 return log_error_errno(r, "Failed to determine network namespace of udev: %m");
6073 if (r == 0)
6074 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
6075 "Sorry, but --image= is only supported in the main network namespace, since we need access to udev/AF_NETLINK.");
6076 return 0;
6077 }
6078
6079 static void cleanup_propagation_and_export_directories(const char *runtime_dir) {
6080 _cleanup_free_ char *p = NULL;
6081
6082 if (!runtime_dir || arg_userns_mode == USER_NAMESPACE_MANAGED)
6083 return;
6084
6085 p = path_join("/run/systemd/nspawn/propagate", arg_machine);
6086 if (p)
6087 (void) rm_rf(p, REMOVE_ROOT);
6088
6089 free(p);
6090 p = path_join(runtime_dir, "unix-export");
6091 if (p) {
6092 (void) umount2(p, MNT_DETACH|UMOUNT_NOFOLLOW);
6093 (void) rmdir(p);
6094 }
6095 }
6096
6097 static int do_cleanup(void) {
6098 int r;
6099
6100 if (arg_ephemeral)
6101 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot specify --ephemeral with --cleanup.");
6102
6103 r = determine_names();
6104 if (r < 0)
6105 return r;
6106
6107 _cleanup_free_ char *subdir = path_join("systemd/nspawn", arg_machine);
6108 if (!subdir)
6109 return log_oom();
6110
6111 _cleanup_free_ char *runtime_dir = NULL;
6112 r = runtime_directory(arg_runtime_scope, subdir, &runtime_dir);
6113 if (r < 0)
6114 return r;
6115
6116 cleanup_propagation_and_export_directories(runtime_dir);
6117 return 0;
6118 }
6119
6120 static int run(int argc, char *argv[]) {
6121 bool remove_image = false, veth_created = false;
6122 _cleanup_close_ int master = -EBADF, userns_fd = -EBADF, mount_fd = -EBADF;
6123 _cleanup_fdset_free_ FDSet *fds = NULL;
6124 int r, ret = EXIT_SUCCESS;
6125 char veth_name[IFNAMSIZ] = "";
6126 struct ExposeArgs expose_args = {};
6127 _cleanup_(release_lock_file) LockFile tree_global_lock = LOCK_FILE_INIT, tree_local_lock = LOCK_FILE_INIT;
6128 _cleanup_(rmdir_and_freep) char *rootdir = NULL;
6129 _cleanup_(rm_rf_subvolume_and_freep) char *snapshot_dir = NULL;
6130 _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
6131 _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL;
6132 _cleanup_(mstack_freep) MStack *mstack = NULL;
6133 _cleanup_(sd_netlink_unrefp) sd_netlink *nfnl = NULL;
6134 _cleanup_(pidref_done) PidRef pid = PIDREF_NULL;
6135 _cleanup_(sd_varlink_unrefp) sd_varlink *nsresource_link = NULL, *mountfsd_link = NULL;
6136 _cleanup_free_ char *runtime_dir = NULL, *subdir = NULL;
6137 _cleanup_(rm_rf_physical_and_freep) char *runtime_dir_destroy = NULL;
6138 _cleanup_(fork_notify_terminate) PidRef journal_remote_pidref = PIDREF_NULL;
6139
6140 log_setup();
6141
6142 r = parse_argv(argc, argv);
6143 if (r <= 0)
6144 goto finish;
6145
6146 if (arg_cleanup)
6147 return do_cleanup();
6148
6149 (void) DLOPEN_LIBMOUNT(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
6150 (void) DLOPEN_LIBSECCOMP(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
6151 (void) DLOPEN_LIBSELINUX(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
6152
6153 r = cg_has_legacy();
6154 if (r < 0)
6155 goto finish;
6156 if (r > 0) {
6157 r = log_error_errno(SYNTHETIC_ERRNO(EPROTO),
6158 "Detected host uses legacy cgroup v1 hierarchy, refusing.");
6159 goto finish;
6160 }
6161
6162 r = cant_be_in_netns();
6163 if (r < 0)
6164 goto finish;
6165
6166 r = initialize_rlimits();
6167 if (r < 0)
6168 goto finish;
6169
6170 r = load_oci_bundle();
6171 if (r < 0)
6172 goto finish;
6173
6174 r = pick_paths();
6175 if (r < 0)
6176 goto finish;
6177
6178 r = determine_names();
6179 if (r < 0)
6180 goto finish;
6181
6182 r = load_settings();
6183 if (r < 0)
6184 goto finish;
6185
6186 if (!FLAGS_SET(arg_settings_mask, SETTING_RESTRICT_ADDRESS_FAMILIES) && !arg_restrict_address_families)
6187 log_notice("Note: in a future version of systemd-nspawn the default set of permitted socket address"
6188 " families will be restricted to AF_INET, AF_INET6 and AF_UNIX."
6189 " Use --restrict-address-families= to configure the set of permitted socket address"
6190 " families, or set RestrictAddressFamilies= in a .nspawn file.");
6191
6192 /* If we're not unsharing the network namespace and are unsharing the user namespace, we won't have
6193 * permissions to bind ports in the container, so let's drop the CAP_NET_BIND_SERVICE capability to
6194 * indicate that. */
6195 if (!arg_private_network && arg_userns_mode != USER_NAMESPACE_NO)
6196 arg_caps_retain &= ~(UINT64_C(1) << CAP_NET_BIND_SERVICE);
6197
6198 r = verify_arguments();
6199 if (r < 0)
6200 goto finish;
6201
6202 r = split_boot_parameters();
6203 if (r < 0)
6204 goto finish;
6205
6206 r = resolve_network_interface_names(arg_network_interfaces);
6207 if (r < 0)
6208 goto finish;
6209
6210 r = verify_network_interfaces_initialized();
6211 if (r < 0)
6212 goto finish;
6213
6214 /* Ignore SIGPIPE here, because we use splice() on the ptyfwd stuff and that will generate SIGPIPE if
6215 * the result is closed. Note that the container payload child will reset signal mask+handler anyway,
6216 * so just turning this off here means we only turn it off in nspawn itself, not any children. */
6217 (void) ignore_signals(SIGPIPE);
6218
6219 r = fdset_new_listen_fds(&fds, /* unset= */ false);
6220 if (r < 0) {
6221 log_error_errno(r, "Failed to collect file descriptors: %m");
6222 goto finish;
6223 }
6224
6225 /* The "default" umask. This is appropriate for most file and directory
6226 * operations performed by nspawn, and is the umask that will be used for
6227 * the child. Functions like copy_devnodes() change the umask temporarily. */
6228 umask(0022);
6229
6230 if (arg_console_mode < 0)
6231 arg_console_mode = isatty_safe(STDIN_FILENO) && isatty_safe(STDOUT_FILENO) ?
6232 CONSOLE_INTERACTIVE : CONSOLE_READ_ONLY;
6233
6234 if (arg_console_mode == CONSOLE_AUTOPIPE) {
6235 if (isatty_safe(STDIN_FILENO) && isatty_safe(STDOUT_FILENO))
6236 arg_console_mode = CONSOLE_INTERACTIVE;
6237 else
6238 arg_console_mode = CONSOLE_PIPE;
6239 }
6240
6241 if (arg_console_mode == CONSOLE_PIPE) /* if we pass STDERR on to the container, don't add our own logs into it too */
6242 arg_quiet = true;
6243
6244 polkit_agent_open();
6245
6246 if (arg_userns_mode == USER_NAMESPACE_MANAGED) {
6247 /* Let's allocate a 64K userns first, if managed mode is chosen */
6248
6249 r = nsresource_connect(&nsresource_link);
6250 if (r < 0) {
6251 log_error_errno(r, "Failed to connect to nsresourced: %m");
6252 goto finish;
6253 }
6254
6255 r = mountfsd_connect(&mountfsd_link);
6256 if (r < 0) {
6257 log_error_errno(r, "Failed to connect to mountfsd: %m");
6258 goto finish;
6259 }
6260
6261 _cleanup_free_ char *userns_name = NULL;
6262 if (asprintf(&userns_name, "nspawn-" PID_FMT "-%s", getpid_cached(), arg_machine) < 0) {
6263 r = log_oom();
6264 goto finish;
6265 }
6266
6267 userns_fd = nsresource_allocate_userns_full(
6268 nsresource_link,
6269 userns_name,
6270 NSRESOURCE_UIDS_64K,
6271 arg_delegate_container_ranges);
6272 if (userns_fd < 0) {
6273 r = log_error_errno(userns_fd, "Failed to allocate user namespace with 64K users: %m");
6274 goto finish;
6275 }
6276
6277 r = userns_get_base_uid(userns_fd, &arg_uid_shift, /* ret_gid= */ NULL);
6278 if (r < 0) {
6279 log_error_errno(r, "Failed to determine UID shift from userns: %m");
6280 goto finish;
6281 }
6282
6283 arg_uid_range = NSRESOURCE_UIDS_64K;
6284 }
6285
6286 if (arg_directory) {
6287 assert(!arg_image);
6288
6289 /* Safety precaution: let's not allow running images from the live host OS image, as long as
6290 * /var from the host will propagate into container dynamically (because bad things happen if
6291 * two systems write to the same /var). Let's allow it for the special cases where /var is
6292 * either copied (i.e. --ephemeral) or replaced (i.e. --volatile=yes|state). */
6293 if (path_equal(arg_directory, "/") && !(arg_ephemeral || IN_SET(arg_volatile_mode, VOLATILE_YES, VOLATILE_STATE))) {
6294 r = log_error_errno(SYNTHETIC_ERRNO(EINVAL),
6295 "Spawning container on root directory is not supported. Consider using --ephemeral, --volatile=yes or --volatile=state.");
6296 goto finish;
6297 }
6298
6299 if (arg_ephemeral) {
6300 r = chase_and_update(&arg_directory, 0);
6301 if (r < 0)
6302 goto finish;
6303
6304 r = create_ephemeral_snapshot(
6305 arg_directory,
6306 arg_runtime_scope,
6307 arg_read_only,
6308 &tree_global_lock,
6309 &tree_local_lock,
6310 &snapshot_dir);
6311 if (r < 0) {
6312 log_error_errno(r, "Failed to create ephemeral snapshot: %m");
6313 goto finish;
6314 }
6315
6316 r = free_and_strdup(&arg_directory, snapshot_dir);
6317 if (r < 0) {
6318 log_oom();
6319 goto finish;
6320 }
6321 } else {
6322 r = chase_and_update(&arg_directory, arg_template ? CHASE_NONEXISTENT : 0);
6323 if (r < 0)
6324 goto finish;
6325
6326 r = image_path_lock(
6327 arg_runtime_scope,
6328 arg_directory,
6329 (arg_read_only ? LOCK_SH : LOCK_EX) | LOCK_NB,
6330 arg_runtime_scope == RUNTIME_SCOPE_SYSTEM ? &tree_global_lock : NULL,
6331 &tree_local_lock);
6332 if (r == -EBUSY) {
6333 log_error_errno(r, "Directory tree %s is currently busy.", arg_directory);
6334 goto finish;
6335 }
6336 if (r < 0) {
6337 log_error_errno(r, "Failed to lock %s: %m", arg_directory);
6338 goto finish;
6339 }
6340
6341 if (arg_template) {
6342 r = chase_and_update(&arg_template, 0);
6343 if (r < 0)
6344 goto finish;
6345
6346 {
6347 BLOCK_SIGNALS(SIGINT);
6348 r = btrfs_subvol_snapshot_at(AT_FDCWD, arg_template, AT_FDCWD, arg_directory,
6349 (arg_read_only ? BTRFS_SNAPSHOT_READ_ONLY : 0) |
6350 BTRFS_SNAPSHOT_FALLBACK_COPY |
6351 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY |
6352 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE |
6353 BTRFS_SNAPSHOT_RECURSIVE |
6354 BTRFS_SNAPSHOT_QUOTA |
6355 BTRFS_SNAPSHOT_SIGINT);
6356 }
6357 if (r == -EEXIST)
6358 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO,
6359 "Directory %s already exists, not populating from template %s.", arg_directory, arg_template);
6360 else if (r == -EINTR) {
6361 log_error_errno(r, "Interrupted while copying file system tree to %s, removed again.", arg_directory);
6362 goto finish;
6363 } else if (r < 0) {
6364 log_error_errno(r, "Couldn't create snapshot %s from %s: %m", arg_directory, arg_template);
6365 goto finish;
6366 } else
6367 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO,
6368 "Populated %s from template %s.", arg_directory, arg_template);
6369 }
6370 }
6371
6372 if (arg_start_mode == START_BOOT) {
6373 _cleanup_free_ char *b = NULL;
6374 const char *p;
6375 int check_os_release, is_os_tree;
6376
6377 if (arg_pivot_root_new) {
6378 b = path_join(arg_directory, arg_pivot_root_new);
6379 if (!b) {
6380 r = log_oom();
6381 goto finish;
6382 }
6383
6384 p = b;
6385 } else
6386 p = arg_directory;
6387
6388 check_os_release = getenv_bool("SYSTEMD_NSPAWN_CHECK_OS_RELEASE");
6389 if (check_os_release < 0 && check_os_release != -ENXIO) {
6390 r = log_error_errno(check_os_release, "Failed to parse $SYSTEMD_NSPAWN_CHECK_OS_RELEASE: %m");
6391 goto finish;
6392 }
6393
6394 is_os_tree = path_is_os_tree(p);
6395 if (is_os_tree == 0 && check_os_release == 0)
6396 log_debug("Directory %s is missing an os-release file, continuing anyway.", p);
6397 else if (is_os_tree <= 0) {
6398 r = log_error_errno(SYNTHETIC_ERRNO(EINVAL),
6399 "Directory %s doesn't look like an OS root directory (os-release file is missing). Refusing.", p);
6400 goto finish;
6401 }
6402 } else {
6403 _cleanup_free_ char *p = NULL;
6404
6405 if (arg_pivot_root_new)
6406 p = path_join(arg_directory, arg_pivot_root_new, "/usr/");
6407 else
6408 p = path_join(arg_directory, "/usr/");
6409 if (!p) {
6410 r = log_oom();
6411 goto finish;
6412 }
6413
6414 r = access_nofollow(p, F_OK);
6415 if (r == -ENOENT) {
6416 log_error_errno(r, "Directory %s doesn't look like it has an OS tree (/usr/ directory is missing). Refusing.", arg_directory);
6417 goto finish;
6418 } else if (r < 0) {
6419 log_error_errno(r, "Unable to determine if %s looks like it has an OS tree (i.e. whether /usr/ exists): %m", arg_directory);
6420 goto finish;
6421 }
6422 }
6423
6424 if (userns_fd >= 0) {
6425 r = mountfsd_mount_directory(
6426 mountfsd_link,
6427 arg_directory,
6428 userns_fd,
6429 determine_dissect_image_flags(),
6430 &mount_fd);
6431 if (r < 0) {
6432 log_error_errno(r, "Failed to mount directory via mountfsd: %m");
6433 goto finish;
6434 }
6435 }
6436
6437 } else if (arg_image) {
6438 DissectImageFlags dissect_image_flags =
6439 determine_dissect_image_flags();
6440
6441 assert(!arg_template);
6442
6443 r = chase_and_update(&arg_image, 0);
6444 if (r < 0)
6445 goto finish;
6446
6447 if (arg_ephemeral) {
6448 _cleanup_free_ char *np = NULL;
6449
6450 r = tempfn_random(arg_image, "machine.", &np);
6451 if (r < 0) {
6452 log_error_errno(r, "Failed to generate name for image snapshot: %m");
6453 goto finish;
6454 }
6455
6456 /* Always take an exclusive lock on our own ephemeral copy. */
6457 r = image_path_lock(
6458 arg_runtime_scope,
6459 np,
6460 LOCK_EX|LOCK_NB,
6461 arg_runtime_scope == RUNTIME_SCOPE_SYSTEM ? &tree_global_lock : NULL,
6462 &tree_local_lock);
6463 if (r < 0) {
6464 log_error_errno(r, "Failed to create image lock: %m");
6465 goto finish;
6466 }
6467
6468 {
6469 BLOCK_SIGNALS(SIGINT);
6470 r = copy_file(arg_image, np, O_EXCL, arg_read_only ? 0400 : 0600,
6471 COPY_REFLINK|COPY_CRTIME|COPY_SIGINT|COPY_NOCOW_AFTER);
6472 }
6473 if (r == -EINTR) {
6474 log_error_errno(r, "Interrupted while copying image file to %s, removed again.", np);
6475 goto finish;
6476 }
6477 if (r < 0) {
6478 r = log_error_errno(r, "Failed to copy image file: %m");
6479 goto finish;
6480 }
6481
6482 free_and_replace(arg_image, np);
6483 remove_image = true;
6484 } else {
6485 r = image_path_lock(
6486 arg_runtime_scope,
6487 arg_image,
6488 (arg_read_only ? LOCK_SH : LOCK_EX) | LOCK_NB,
6489 arg_runtime_scope == RUNTIME_SCOPE_SYSTEM ? &tree_global_lock : NULL,
6490 &tree_local_lock);
6491 if (r == -EBUSY) {
6492 log_error_errno(r, "Disk image %s is currently busy.", arg_image);
6493 goto finish;
6494 }
6495 if (r < 0) {
6496 log_error_errno(r, "Failed to create image lock: %m");
6497 goto finish;
6498 }
6499
6500 r = verity_settings_load(
6501 &arg_verity_settings,
6502 arg_image, NULL, NULL);
6503 if (r < 0) {
6504 log_error_errno(r, "Failed to read verity artefacts for %s: %m", arg_image);
6505 goto finish;
6506 }
6507
6508 if (arg_verity_settings.data_path)
6509 dissect_image_flags |= DISSECT_IMAGE_NO_PARTITION_TABLE;
6510 }
6511
6512 if (arg_userns_mode != USER_NAMESPACE_MANAGED) {
6513 r = loop_device_make_by_path(
6514 arg_image,
6515 arg_read_only ? O_RDONLY : -1,
6516 /* sector_size= */ UINT32_MAX,
6517 FLAGS_SET(dissect_image_flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN,
6518 LOCK_SH,
6519 &loop);
6520 if (r < 0) {
6521 log_error_errno(r, "Failed to set up loopback block device: %m");
6522 goto finish;
6523 }
6524
6525 r = dissect_loop_device_and_warn(
6526 loop,
6527 &arg_verity_settings,
6528 /* mount_options= */ NULL,
6529 arg_image_policy ?: &image_policy_container,
6530 /* image_filter= */ NULL,
6531 dissect_image_flags,
6532 &dissected_image);
6533 if (r == -ENOPKG) {
6534 /* dissected_image_and_warn() already printed a brief error message. Extend on that with more details */
6535 log_notice("Note that the disk image needs to\n"
6536 " a) either contain only a single MBR partition of type 0x83 that is marked bootable\n"
6537 " b) or contain a single GPT partition of type 0FC63DAF-8483-4772-8E79-3D69D8477DE4\n"
6538 " c) or follow https://uapi-group.org/specifications/specs/discoverable_partitions_specification\n"
6539 " d) or contain a file system without a partition table\n"
6540 "in order to be bootable with systemd-nspawn.");
6541 goto finish;
6542 }
6543 if (r < 0)
6544 goto finish;
6545
6546 r = dissected_image_load_verity_sig_partition(
6547 dissected_image,
6548 loop->fd,
6549 &arg_verity_settings);
6550 if (r < 0) {
6551 log_error_errno(r, "Failed to load Verity signature partition: %m");
6552 goto finish;
6553 }
6554
6555 r = dissected_image_guess_verity_roothash(
6556 dissected_image,
6557 &arg_verity_settings);
6558 if (r < 0) {
6559 log_error_errno(r, "Failed to guess Verity root hash: %m");
6560 goto finish;
6561 }
6562
6563 if (dissected_image->has_verity && !iovec_is_set(&arg_verity_settings.root_hash))
6564 log_notice("Note: image %s contains verity information, but no root hash specified and no embedded "
6565 "root hash signature found! Proceeding without integrity checking.", arg_image);
6566
6567 r = dissected_image_decrypt_interactively(
6568 dissected_image,
6569 NULL,
6570 &arg_verity_settings,
6571 arg_image_policy ?: &image_policy_container,
6572 dissect_image_flags);
6573 if (r < 0)
6574 goto finish;
6575 } else {
6576 r = mountfsd_mount_image(
6577 mountfsd_link,
6578 arg_image,
6579 userns_fd,
6580 /* options= */ NULL,
6581 arg_image_policy,
6582 &arg_verity_settings,
6583 dissect_image_flags,
6584 &dissected_image);
6585 if (r < 0) {
6586 log_error_errno(r, "Failed to mount image via mountfsd: %m");
6587 goto finish;
6588 }
6589 }
6590
6591 /* Now that we mounted the image, let's try to remove it again, if it is ephemeral */
6592 if (remove_image && unlink(arg_image) >= 0)
6593 remove_image = false;
6594
6595 if (arg_architecture < 0)
6596 arg_architecture = dissected_image_architecture(dissected_image);
6597
6598 } else if (arg_mstack) {
6599 assert(!arg_template);
6600 assert(!arg_ephemeral);
6601
6602 r = chase_and_update(&arg_mstack, CHASE_MUST_BE_DIRECTORY);
6603 if (r < 0)
6604 goto finish;
6605
6606 if (!IN_SET(arg_userns_mode, USER_NAMESPACE_NO, USER_NAMESPACE_MANAGED))
6607 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--mstack= requires managed user namespacing, or user namespace turned off.");
6608
6609 MStackFlags mstack_flags = arg_read_only ? MSTACK_RDONLY : 0;
6610 r = mstack_load(arg_mstack,
6611 /* dir_fd= */ -EBADF,
6612 &mstack);
6613 if (r < 0) {
6614 log_error_errno(r, "Failed to load .mstack/ directory '%s': %m", arg_mstack);
6615 goto finish;
6616 }
6617
6618 r = mstack_open_images(
6619 mstack,
6620 mountfsd_link,
6621 userns_fd,
6622 arg_image_policy,
6623 /* image_filter= */ NULL,
6624 mstack_flags);
6625 if (r < 0) {
6626 log_error_errno(r, "Failed to open .mstack/ layer images '%s': %m", arg_mstack);
6627 goto finish;
6628 }
6629 } else
6630 assert_not_reached();
6631
6632 subdir = path_join("systemd/nspawn", arg_machine);
6633 if (!subdir) {
6634 r = log_oom();
6635 goto finish;
6636 }
6637
6638 r = runtime_directory_make(
6639 arg_runtime_scope,
6640 subdir,
6641 &runtime_dir,
6642 &runtime_dir_destroy);
6643 if (r < 0) {
6644 log_error_errno(r, "Failed to create runtime directory: %m");
6645 goto finish;
6646 }
6647
6648 /* Create a temporary place to mount stuff. */
6649 r = mkdtemp_malloc("/tmp/nspawn-root-XXXXXX", &rootdir);
6650 if (r < 0) {
6651 log_error_errno(r, "Failed to create temporary directory: %m");
6652 goto finish;
6653 }
6654
6655 r = custom_mount_prepare_all(rootdir, arg_custom_mounts, arg_n_custom_mounts);
6656 if (r < 0)
6657 goto finish;
6658
6659 mountfsd_link = sd_varlink_unref(mountfsd_link);
6660 nsresource_link = sd_varlink_unref(nsresource_link);
6661
6662 if (!arg_quiet) {
6663 const char *t = arg_mstack ?: arg_image ?: arg_directory;
6664 _cleanup_free_ char *u = NULL;
6665 (void) terminal_urlify_path(t, t, &u);
6666
6667 log_info("%s %sSpawning container %s on %s.%s",
6668 glyph(GLYPH_LIGHT_SHADE), ansi_grey(), arg_machine, u ?: t, ansi_normal());
6669
6670 if (arg_console_mode == CONSOLE_INTERACTIVE)
6671 log_info("%s %sPress %sCtrl-]%s three times within 1s to kill container; two times followed by %sr%s\n"
6672 "%s %sto reboot container; two times followed by %sp%s to poweroff container.%s",
6673 glyph(GLYPH_LIGHT_SHADE), ansi_grey(), ansi_highlight(), ansi_grey(), ansi_highlight(), ansi_normal(),
6674 glyph(GLYPH_LIGHT_SHADE), ansi_grey(), ansi_highlight(), ansi_grey(), ansi_normal());
6675 }
6676
6677 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, SIGWINCH, SIGTERM, SIGINT, SIGRTMIN+18) >= 0);
6678
6679 r = make_reaper_process(true);
6680 if (r < 0) {
6681 log_error_errno(r, "Failed to become subreaper: %m");
6682 goto finish;
6683 }
6684
6685 if (arg_expose_ports) {
6686 r = sd_nfnl_socket_open(&nfnl);
6687 if (r < 0) {
6688 log_error_errno(r, "Cannot expose configured ports, failed to initialize nftables: %m");
6689 goto finish;
6690 }
6691 expose_args.nfnl = nfnl;
6692 }
6693
6694 if (arg_forward_journal) {
6695 _cleanup_free_ char *socket_path = path_join(runtime_dir, "journal-remote-socket");
6696 if (!socket_path) {
6697 r = log_oom();
6698 goto finish;
6699 }
6700
6701 union sockaddr_union sa;
6702 r = sockaddr_un_set_path(&sa.un, socket_path);
6703 if (r < 0) {
6704 log_error_errno(r, "Failed to set AF_UNIX path to '%s': %m", socket_path);
6705 goto finish;
6706 }
6707
6708 (void) sockaddr_un_unlink(&sa.un);
6709
6710 r = fork_journal_remote(
6711 socket_path,
6712 arg_forward_journal,
6713 arg_forward_journal_max_use,
6714 arg_forward_journal_keep_free,
6715 arg_forward_journal_max_file_size,
6716 arg_forward_journal_max_files,
6717 &journal_remote_pidref);
6718 if (r < 0)
6719 goto finish;
6720
6721 CustomMount *cm = custom_mount_add(&arg_custom_mounts, &arg_n_custom_mounts, CUSTOM_MOUNT_BIND);
6722 if (!cm) {
6723 r = log_oom();
6724 goto finish;
6725 }
6726
6727 cm->source = TAKE_PTR(socket_path);
6728 cm->read_only = true;
6729 cm->destination = strdup(NSPAWN_JOURNAL_SOCKET_PATH);
6730 if (!cm->destination) {
6731 r = log_oom();
6732 goto finish;
6733 }
6734
6735 r = machine_credential_add(&arg_credentials, "journal.forward_to_socket", NSPAWN_JOURNAL_SOCKET_PATH, SIZE_MAX);
6736 if (r == -EEXIST) {
6737 log_error_errno(r, "Credential 'journal.forward_to_socket' already set via --set-credential=, refusing --forward-journal=.");
6738 goto finish;
6739 }
6740 if (r < 0) {
6741 log_error_errno(r, "Failed to add 'journal.forward_to_socket' credential: %m");
6742 goto finish;
6743 }
6744 }
6745
6746 for (;;) {
6747 r = run_container(
6748 runtime_dir,
6749 rootdir,
6750 mount_fd,
6751 dissected_image,
6752 mstack,
6753 userns_fd,
6754 fds,
6755 veth_name,
6756 &veth_created,
6757 &expose_args, &master,
6758 &pid, &ret);
6759 if (r <= 0)
6760 break;
6761 }
6762
6763 finish:
6764 (void) sd_notify(false,
6765 r == 0 && ret == EXIT_FORCE_RESTART ? "STOPPING=1\nSTATUS=Restarting..." :
6766 "STOPPING=1\nSTATUS=Terminating...");
6767
6768 if (pidref_is_set(&pid))
6769 (void) pidref_kill(&pid, SIGKILL);
6770
6771 /* Try to flush whatever is still queued in the pty */
6772 if (master >= 0) {
6773 (void) copy_bytes(master, STDOUT_FILENO, UINT64_MAX, 0);
6774 master = safe_close(master);
6775 }
6776
6777 if (pidref_is_set(&pid)) {
6778 (void) pidref_wait_for_terminate(&pid, NULL);
6779 pidref_done(&pid);
6780 }
6781
6782 pager_close();
6783
6784 if (remove_image && arg_image) {
6785 if (unlink(arg_image) < 0)
6786 log_warning_errno(errno, "Can't remove image file '%s', ignoring: %m", arg_image);
6787 }
6788
6789 cleanup_propagation_and_export_directories(runtime_dir);
6790
6791 expose_port_flush(nfnl, arg_expose_ports, AF_INET, &expose_args.address4);
6792 expose_port_flush(nfnl, arg_expose_ports, AF_INET6, &expose_args.address6);
6793
6794 if (arg_userns_mode != USER_NAMESPACE_MANAGED) {
6795 if (veth_created)
6796 (void) remove_veth_links(veth_name, arg_network_veth_extra);
6797 (void) remove_bridge(arg_network_zone);
6798 }
6799
6800 custom_mount_free_all(arg_custom_mounts, arg_n_custom_mounts);
6801 expose_port_free_all(arg_expose_ports);
6802 rlimit_free_all(arg_rlimit);
6803 device_node_array_free(arg_extra_nodes, arg_n_extra_nodes);
6804
6805 if (r < 0)
6806 return r;
6807
6808 return ret;
6809 }
6810
6811 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);