]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn.c
nspawn: move payload to sub-cgroup first, then sync cgroup trees
[thirdparty/systemd.git] / src / nspawn / nspawn.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #if HAVE_BLKID
4 #include <blkid.h>
5 #endif
6 #include <errno.h>
7 #include <getopt.h>
8 #include <grp.h>
9 #include <linux/fs.h>
10 #include <linux/loop.h>
11 #include <pwd.h>
12 #include <sched.h>
13 #if HAVE_SELINUX
14 #include <selinux/selinux.h>
15 #endif
16 #include <signal.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/file.h>
21 #include <sys/personality.h>
22 #include <sys/prctl.h>
23 #include <sys/types.h>
24 #include <sys/wait.h>
25 #include <unistd.h>
26
27 #include "sd-bus.h"
28 #include "sd-daemon.h"
29 #include "sd-id128.h"
30
31 #include "alloc-util.h"
32 #include "barrier.h"
33 #include "base-filesystem.h"
34 #include "blkid-util.h"
35 #include "btrfs-util.h"
36 #include "bus-error.h"
37 #include "bus-util.h"
38 #include "cap-list.h"
39 #include "capability-util.h"
40 #include "cgroup-util.h"
41 #include "copy.h"
42 #include "cpu-set-util.h"
43 #include "dev-setup.h"
44 #include "dissect-image.h"
45 #include "env-util.h"
46 #include "fd-util.h"
47 #include "fdset.h"
48 #include "fileio.h"
49 #include "format-util.h"
50 #include "fs-util.h"
51 #include "gpt.h"
52 #include "hexdecoct.h"
53 #include "hostname-util.h"
54 #include "id128-util.h"
55 #include "log.h"
56 #include "loop-util.h"
57 #include "loopback-setup.h"
58 #include "machine-image.h"
59 #include "macro.h"
60 #include "missing.h"
61 #include "mkdir.h"
62 #include "mount-util.h"
63 #include "mountpoint-util.h"
64 #include "netlink-util.h"
65 #include "nspawn-cgroup.h"
66 #include "nspawn-def.h"
67 #include "nspawn-expose-ports.h"
68 #include "nspawn-mount.h"
69 #include "nspawn-network.h"
70 #include "nspawn-patch-uid.h"
71 #include "nspawn-register.h"
72 #include "nspawn-seccomp.h"
73 #include "nspawn-settings.h"
74 #include "nspawn-setuid.h"
75 #include "nspawn-stub-pid1.h"
76 #include "os-util.h"
77 #include "pager.h"
78 #include "parse-util.h"
79 #include "path-util.h"
80 #include "pretty-print.h"
81 #include "process-util.h"
82 #include "ptyfwd.h"
83 #include "random-util.h"
84 #include "raw-clone.h"
85 #include "rlimit-util.h"
86 #include "rm-rf.h"
87 #include "selinux-util.h"
88 #include "signal-util.h"
89 #include "socket-util.h"
90 #include "stat-util.h"
91 #include "stdio-util.h"
92 #include "string-table.h"
93 #include "string-util.h"
94 #include "strv.h"
95 #include "terminal-util.h"
96 #include "tmpfile-util.h"
97 #include "umask-util.h"
98 #include "user-util.h"
99 #include "util.h"
100
101 #if HAVE_SPLIT_USR
102 #define STATIC_RESOLV_CONF "/lib/systemd/resolv.conf"
103 #else
104 #define STATIC_RESOLV_CONF "/usr/lib/systemd/resolv.conf"
105 #endif
106
107 /* nspawn is listening on the socket at the path in the constant nspawn_notify_socket_path
108 * nspawn_notify_socket_path is relative to the container
109 * the init process in the container pid can send messages to nspawn following the sd_notify(3) protocol */
110 #define NSPAWN_NOTIFY_SOCKET_PATH "/run/systemd/nspawn/notify"
111
112 #define EXIT_FORCE_RESTART 133
113
114 typedef enum ContainerStatus {
115 CONTAINER_TERMINATED,
116 CONTAINER_REBOOTED
117 } ContainerStatus;
118
119 static char *arg_directory = NULL;
120 static char *arg_template = NULL;
121 static char *arg_chdir = NULL;
122 static char *arg_pivot_root_new = NULL;
123 static char *arg_pivot_root_old = NULL;
124 static char *arg_user = NULL;
125 static sd_id128_t arg_uuid = {};
126 static char *arg_machine = NULL; /* The name used by the host to refer to this */
127 static char *arg_hostname = NULL; /* The name the payload sees by default */
128 static const char *arg_selinux_context = NULL;
129 static const char *arg_selinux_apifs_context = NULL;
130 static const char *arg_slice = NULL;
131 static bool arg_private_network = false;
132 static bool arg_read_only = false;
133 static StartMode arg_start_mode = START_PID1;
134 static bool arg_ephemeral = false;
135 static LinkJournal arg_link_journal = LINK_AUTO;
136 static bool arg_link_journal_try = false;
137 static uint64_t arg_caps_retain =
138 (1ULL << CAP_AUDIT_CONTROL) |
139 (1ULL << CAP_AUDIT_WRITE) |
140 (1ULL << CAP_CHOWN) |
141 (1ULL << CAP_DAC_OVERRIDE) |
142 (1ULL << CAP_DAC_READ_SEARCH) |
143 (1ULL << CAP_FOWNER) |
144 (1ULL << CAP_FSETID) |
145 (1ULL << CAP_IPC_OWNER) |
146 (1ULL << CAP_KILL) |
147 (1ULL << CAP_LEASE) |
148 (1ULL << CAP_LINUX_IMMUTABLE) |
149 (1ULL << CAP_MKNOD) |
150 (1ULL << CAP_NET_BIND_SERVICE) |
151 (1ULL << CAP_NET_BROADCAST) |
152 (1ULL << CAP_NET_RAW) |
153 (1ULL << CAP_SETFCAP) |
154 (1ULL << CAP_SETGID) |
155 (1ULL << CAP_SETPCAP) |
156 (1ULL << CAP_SETUID) |
157 (1ULL << CAP_SYS_ADMIN) |
158 (1ULL << CAP_SYS_BOOT) |
159 (1ULL << CAP_SYS_CHROOT) |
160 (1ULL << CAP_SYS_NICE) |
161 (1ULL << CAP_SYS_PTRACE) |
162 (1ULL << CAP_SYS_RESOURCE) |
163 (1ULL << CAP_SYS_TTY_CONFIG);
164 static CustomMount *arg_custom_mounts = NULL;
165 static size_t arg_n_custom_mounts = 0;
166 static char **arg_setenv = NULL;
167 static bool arg_quiet = false;
168 static bool arg_register = true;
169 static bool arg_keep_unit = false;
170 static char **arg_network_interfaces = NULL;
171 static char **arg_network_macvlan = NULL;
172 static char **arg_network_ipvlan = NULL;
173 static bool arg_network_veth = false;
174 static char **arg_network_veth_extra = NULL;
175 static char *arg_network_bridge = NULL;
176 static char *arg_network_zone = NULL;
177 static char *arg_network_namespace_path = NULL;
178 static unsigned long arg_personality = PERSONALITY_INVALID;
179 static char *arg_image = NULL;
180 static VolatileMode arg_volatile_mode = VOLATILE_NO;
181 static ExposePort *arg_expose_ports = NULL;
182 static char **arg_property = NULL;
183 static UserNamespaceMode arg_userns_mode = USER_NAMESPACE_NO;
184 static uid_t arg_uid_shift = UID_INVALID, arg_uid_range = 0x10000U;
185 static bool arg_userns_chown = false;
186 static int arg_kill_signal = 0;
187 static CGroupUnified arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_UNKNOWN;
188 static SettingsMask arg_settings_mask = 0;
189 static int arg_settings_trusted = -1;
190 static char **arg_parameters = NULL;
191 static const char *arg_container_service_name = "systemd-nspawn";
192 static bool arg_notify_ready = false;
193 static bool arg_use_cgns = true;
194 static unsigned long arg_clone_ns_flags = CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS;
195 static MountSettingsMask arg_mount_settings = MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_TMPFS_TMP;
196 static void *arg_root_hash = NULL;
197 static size_t arg_root_hash_size = 0;
198 static char **arg_syscall_whitelist = NULL;
199 static char **arg_syscall_blacklist = NULL;
200 static struct rlimit *arg_rlimit[_RLIMIT_MAX] = {};
201 static bool arg_no_new_privileges = false;
202 static int arg_oom_score_adjust = 0;
203 static bool arg_oom_score_adjust_set = false;
204 static cpu_set_t *arg_cpuset = NULL;
205 static unsigned arg_cpuset_ncpus = 0;
206 static ResolvConfMode arg_resolv_conf = RESOLV_CONF_AUTO;
207 static TimezoneMode arg_timezone = TIMEZONE_AUTO;
208
209 static int help(void) {
210 _cleanup_free_ char *link = NULL;
211 int r;
212
213 (void) pager_open(false);
214
215 r = terminal_urlify_man("systemd-nspawn", "1", &link);
216 if (r < 0)
217 return log_oom();
218
219 printf("%s [OPTIONS...] [PATH] [ARGUMENTS...]\n\n"
220 "Spawn a command or OS in a light-weight container.\n\n"
221 " -h --help Show this help\n"
222 " --version Print version string\n"
223 " -q --quiet Do not show status information\n"
224 " -D --directory=PATH Root directory for the container\n"
225 " --template=PATH Initialize root directory from template directory,\n"
226 " if missing\n"
227 " -x --ephemeral Run container with snapshot of root directory, and\n"
228 " remove it after exit\n"
229 " -i --image=PATH File system device or disk image for the container\n"
230 " --root-hash=HASH Specify verity root hash\n"
231 " -a --as-pid2 Maintain a stub init as PID1, invoke binary as PID2\n"
232 " -b --boot Boot up full system (i.e. invoke init)\n"
233 " --chdir=PATH Set working directory in the container\n"
234 " --pivot-root=PATH[:PATH]\n"
235 " Pivot root to given directory in the container\n"
236 " -u --user=USER Run the command under specified user or uid\n"
237 " -M --machine=NAME Set the machine name for the container\n"
238 " --hostname=NAME Override the hostname for the container\n"
239 " --uuid=UUID Set a specific machine UUID for the container\n"
240 " -S --slice=SLICE Place the container in the specified slice\n"
241 " --property=NAME=VALUE Set scope unit property\n"
242 " -U --private-users=pick Run within user namespace, autoselect UID/GID range\n"
243 " --private-users[=UIDBASE[:NUIDS]]\n"
244 " Similar, but with user configured UID/GID range\n"
245 " --private-users-chown Adjust OS tree ownership to private UID/GID range\n"
246 " --private-network Disable network in container\n"
247 " --network-interface=INTERFACE\n"
248 " Assign an existing network interface to the\n"
249 " container\n"
250 " --network-macvlan=INTERFACE\n"
251 " Create a macvlan network interface based on an\n"
252 " existing network interface to the container\n"
253 " --network-ipvlan=INTERFACE\n"
254 " Create a ipvlan network interface based on an\n"
255 " existing network interface to the container\n"
256 " -n --network-veth Add a virtual Ethernet connection between host\n"
257 " and container\n"
258 " --network-veth-extra=HOSTIF[:CONTAINERIF]\n"
259 " Add an additional virtual Ethernet link between\n"
260 " host and container\n"
261 " --network-bridge=INTERFACE\n"
262 " Add a virtual Ethernet connection to the container\n"
263 " and attach it to an existing bridge on the host\n"
264 " --network-zone=NAME Similar, but attach the new interface to an\n"
265 " an automatically managed bridge interface\n"
266 " --network-namespace-path=PATH\n"
267 " Set network namespace to the one represented by\n"
268 " the specified kernel namespace file node\n"
269 " -p --port=[PROTOCOL:]HOSTPORT[:CONTAINERPORT]\n"
270 " Expose a container IP port on the host\n"
271 " -Z --selinux-context=SECLABEL\n"
272 " Set the SELinux security context to be used by\n"
273 " processes in the container\n"
274 " -L --selinux-apifs-context=SECLABEL\n"
275 " Set the SELinux security context to be used by\n"
276 " API/tmpfs file systems in the container\n"
277 " --capability=CAP In addition to the default, retain specified\n"
278 " capability\n"
279 " --drop-capability=CAP Drop the specified capability from the default set\n"
280 " --system-call-filter=LIST|~LIST\n"
281 " Permit/prohibit specific system calls\n"
282 " --rlimit=NAME=LIMIT Set a resource limit for the payload\n"
283 " --oom-score-adjust=VALUE\n"
284 " Adjust the OOM score value for the payload\n"
285 " --cpu-affinity=CPUS Adjust the CPU affinity of the container\n"
286 " --kill-signal=SIGNAL Select signal to use for shutting down PID 1\n"
287 " --link-journal=MODE Link up guest journal, one of no, auto, guest, \n"
288 " host, try-guest, try-host\n"
289 " -j Equivalent to --link-journal=try-guest\n"
290 " --resolv-conf=MODE Select mode of /etc/resolv.conf initialization\n"
291 " --timezone=MODE Select mode of /etc/localtime initialization\n"
292 " --read-only Mount the root directory read-only\n"
293 " --bind=PATH[:PATH[:OPTIONS]]\n"
294 " Bind mount a file or directory from the host into\n"
295 " the container\n"
296 " --bind-ro=PATH[:PATH[:OPTIONS]\n"
297 " Similar, but creates a read-only bind mount\n"
298 " --tmpfs=PATH:[OPTIONS] Mount an empty tmpfs to the specified directory\n"
299 " --overlay=PATH[:PATH...]:PATH\n"
300 " Create an overlay mount from the host to \n"
301 " the container\n"
302 " --overlay-ro=PATH[:PATH...]:PATH\n"
303 " Similar, but creates a read-only overlay mount\n"
304 " -E --setenv=NAME=VALUE Pass an environment variable to PID 1\n"
305 " --register=BOOLEAN Register container as machine\n"
306 " --keep-unit Do not register a scope for the machine, reuse\n"
307 " the service unit nspawn is running in\n"
308 " --volatile[=MODE] Run the system in volatile mode\n"
309 " --settings=BOOLEAN Load additional settings from .nspawn file\n"
310 " --notify-ready=BOOLEAN Receive notifications from the child init process\n"
311 "\nSee the %s for details.\n"
312 , program_invocation_short_name
313 , link
314 );
315
316 return 0;
317 }
318
319 static int custom_mount_check_all(void) {
320 size_t i;
321
322 for (i = 0; i < arg_n_custom_mounts; i++) {
323 CustomMount *m = &arg_custom_mounts[i];
324
325 if (path_equal(m->destination, "/") && arg_userns_mode != USER_NAMESPACE_NO) {
326 if (arg_userns_chown)
327 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
328 "--private-users-chown may not be combined with custom root mounts.");
329 else if (arg_uid_shift == UID_INVALID)
330 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
331 "--private-users with automatic UID shift may not be combined with custom root mounts.");
332 }
333 }
334
335 return 0;
336 }
337
338 static int detect_unified_cgroup_hierarchy_from_environment(void) {
339 const char *e;
340 int r;
341
342 /* Allow the user to control whether the unified hierarchy is used */
343 e = getenv("UNIFIED_CGROUP_HIERARCHY");
344 if (e) {
345 r = parse_boolean(e);
346 if (r < 0)
347 return log_error_errno(r, "Failed to parse $UNIFIED_CGROUP_HIERARCHY.");
348 if (r > 0)
349 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_ALL;
350 else
351 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
352 }
353
354 return 0;
355 }
356
357 static int detect_unified_cgroup_hierarchy_from_image(const char *directory) {
358 int r;
359
360 /* Let's inherit the mode to use from the host system, but let's take into consideration what systemd in the
361 * image actually supports. */
362 r = cg_all_unified();
363 if (r < 0)
364 return log_error_errno(r, "Failed to determine whether we are in all unified mode.");
365 if (r > 0) {
366 /* Unified cgroup hierarchy support was added in 230. Unfortunately the detection
367 * routine only detects 231, so we'll have a false negative here for 230. */
368 r = systemd_installation_has_version(directory, 230);
369 if (r < 0)
370 return log_error_errno(r, "Failed to determine systemd version in container: %m");
371 if (r > 0)
372 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_ALL;
373 else
374 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
375 } else if (cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) > 0) {
376 /* Mixed cgroup hierarchy support was added in 233 */
377 r = systemd_installation_has_version(directory, 233);
378 if (r < 0)
379 return log_error_errno(r, "Failed to determine systemd version in container: %m");
380 if (r > 0)
381 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_SYSTEMD;
382 else
383 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
384 } else
385 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
386
387 log_debug("Using %s hierarchy for container.",
388 arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_NONE ? "legacy" :
389 arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_SYSTEMD ? "hybrid" : "unified");
390
391 return 0;
392 }
393
394 static void parse_share_ns_env(const char *name, unsigned long ns_flag) {
395 int r;
396
397 r = getenv_bool(name);
398 if (r == -ENXIO)
399 return;
400 if (r < 0)
401 log_warning_errno(r, "Failed to parse %s from environment, defaulting to false.", name);
402 arg_clone_ns_flags = (arg_clone_ns_flags & ~ns_flag) | (r > 0 ? 0 : ns_flag);
403 }
404
405 static void parse_mount_settings_env(void) {
406 const char *e;
407 int r;
408
409 r = getenv_bool("SYSTEMD_NSPAWN_TMPFS_TMP");
410 if (r >= 0)
411 SET_FLAG(arg_mount_settings, MOUNT_APPLY_TMPFS_TMP, r > 0);
412 else if (r != -ENXIO)
413 log_warning_errno(r, "Failed to parse $SYSTEMD_NSPAWN_TMPFS_TMP, ignoring: %m");
414
415 e = getenv("SYSTEMD_NSPAWN_API_VFS_WRITABLE");
416 if (!e)
417 return;
418
419 if (streq(e, "network")) {
420 arg_mount_settings |= MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_APIVFS_NETNS;
421 return;
422 }
423
424 r = parse_boolean(e);
425 if (r < 0) {
426 log_warning_errno(r, "Failed to parse SYSTEMD_NSPAWN_API_VFS_WRITABLE from environment, ignoring.");
427 return;
428 }
429
430 SET_FLAG(arg_mount_settings, MOUNT_APPLY_APIVFS_RO, r == 0);
431 SET_FLAG(arg_mount_settings, MOUNT_APPLY_APIVFS_NETNS, false);
432 }
433
434 static void parse_environment(void) {
435 const char *e;
436 int r;
437
438 parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_IPC", CLONE_NEWIPC);
439 parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_PID", CLONE_NEWPID);
440 parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_UTS", CLONE_NEWUTS);
441 parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_SYSTEM", CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS);
442
443 parse_mount_settings_env();
444
445 /* SYSTEMD_NSPAWN_USE_CGNS=0 can be used to disable CLONE_NEWCGROUP use,
446 * even if it is supported. If not supported, it has no effect. */
447 r = getenv_bool("SYSTEMD_NSPAWN_USE_CGNS");
448 if (r == 0 || !cg_ns_supported())
449 arg_use_cgns = false;
450
451 e = getenv("SYSTEMD_NSPAWN_CONTAINER_SERVICE");
452 if (e)
453 arg_container_service_name = e;
454
455 detect_unified_cgroup_hierarchy_from_environment();
456 }
457
458 static int parse_argv(int argc, char *argv[]) {
459 enum {
460 ARG_VERSION = 0x100,
461 ARG_PRIVATE_NETWORK,
462 ARG_UUID,
463 ARG_READ_ONLY,
464 ARG_CAPABILITY,
465 ARG_DROP_CAPABILITY,
466 ARG_LINK_JOURNAL,
467 ARG_BIND,
468 ARG_BIND_RO,
469 ARG_TMPFS,
470 ARG_OVERLAY,
471 ARG_OVERLAY_RO,
472 ARG_SHARE_SYSTEM,
473 ARG_REGISTER,
474 ARG_KEEP_UNIT,
475 ARG_NETWORK_INTERFACE,
476 ARG_NETWORK_MACVLAN,
477 ARG_NETWORK_IPVLAN,
478 ARG_NETWORK_BRIDGE,
479 ARG_NETWORK_ZONE,
480 ARG_NETWORK_VETH_EXTRA,
481 ARG_NETWORK_NAMESPACE_PATH,
482 ARG_PERSONALITY,
483 ARG_VOLATILE,
484 ARG_TEMPLATE,
485 ARG_PROPERTY,
486 ARG_PRIVATE_USERS,
487 ARG_KILL_SIGNAL,
488 ARG_SETTINGS,
489 ARG_CHDIR,
490 ARG_PIVOT_ROOT,
491 ARG_PRIVATE_USERS_CHOWN,
492 ARG_NOTIFY_READY,
493 ARG_ROOT_HASH,
494 ARG_SYSTEM_CALL_FILTER,
495 ARG_RLIMIT,
496 ARG_HOSTNAME,
497 ARG_NO_NEW_PRIVILEGES,
498 ARG_OOM_SCORE_ADJUST,
499 ARG_CPU_AFFINITY,
500 ARG_RESOLV_CONF,
501 ARG_TIMEZONE,
502 };
503
504 static const struct option options[] = {
505 { "help", no_argument, NULL, 'h' },
506 { "version", no_argument, NULL, ARG_VERSION },
507 { "directory", required_argument, NULL, 'D' },
508 { "template", required_argument, NULL, ARG_TEMPLATE },
509 { "ephemeral", no_argument, NULL, 'x' },
510 { "user", required_argument, NULL, 'u' },
511 { "private-network", no_argument, NULL, ARG_PRIVATE_NETWORK },
512 { "as-pid2", no_argument, NULL, 'a' },
513 { "boot", no_argument, NULL, 'b' },
514 { "uuid", required_argument, NULL, ARG_UUID },
515 { "read-only", no_argument, NULL, ARG_READ_ONLY },
516 { "capability", required_argument, NULL, ARG_CAPABILITY },
517 { "drop-capability", required_argument, NULL, ARG_DROP_CAPABILITY },
518 { "no-new-privileges", required_argument, NULL, ARG_NO_NEW_PRIVILEGES },
519 { "link-journal", required_argument, NULL, ARG_LINK_JOURNAL },
520 { "bind", required_argument, NULL, ARG_BIND },
521 { "bind-ro", required_argument, NULL, ARG_BIND_RO },
522 { "tmpfs", required_argument, NULL, ARG_TMPFS },
523 { "overlay", required_argument, NULL, ARG_OVERLAY },
524 { "overlay-ro", required_argument, NULL, ARG_OVERLAY_RO },
525 { "machine", required_argument, NULL, 'M' },
526 { "hostname", required_argument, NULL, ARG_HOSTNAME },
527 { "slice", required_argument, NULL, 'S' },
528 { "setenv", required_argument, NULL, 'E' },
529 { "selinux-context", required_argument, NULL, 'Z' },
530 { "selinux-apifs-context", required_argument, NULL, 'L' },
531 { "quiet", no_argument, NULL, 'q' },
532 { "share-system", no_argument, NULL, ARG_SHARE_SYSTEM }, /* not documented */
533 { "register", required_argument, NULL, ARG_REGISTER },
534 { "keep-unit", no_argument, NULL, ARG_KEEP_UNIT },
535 { "network-interface", required_argument, NULL, ARG_NETWORK_INTERFACE },
536 { "network-macvlan", required_argument, NULL, ARG_NETWORK_MACVLAN },
537 { "network-ipvlan", required_argument, NULL, ARG_NETWORK_IPVLAN },
538 { "network-veth", no_argument, NULL, 'n' },
539 { "network-veth-extra", required_argument, NULL, ARG_NETWORK_VETH_EXTRA },
540 { "network-bridge", required_argument, NULL, ARG_NETWORK_BRIDGE },
541 { "network-zone", required_argument, NULL, ARG_NETWORK_ZONE },
542 { "network-namespace-path", required_argument, NULL, ARG_NETWORK_NAMESPACE_PATH },
543 { "personality", required_argument, NULL, ARG_PERSONALITY },
544 { "image", required_argument, NULL, 'i' },
545 { "volatile", optional_argument, NULL, ARG_VOLATILE },
546 { "port", required_argument, NULL, 'p' },
547 { "property", required_argument, NULL, ARG_PROPERTY },
548 { "private-users", optional_argument, NULL, ARG_PRIVATE_USERS },
549 { "private-users-chown", optional_argument, NULL, ARG_PRIVATE_USERS_CHOWN },
550 { "kill-signal", required_argument, NULL, ARG_KILL_SIGNAL },
551 { "settings", required_argument, NULL, ARG_SETTINGS },
552 { "chdir", required_argument, NULL, ARG_CHDIR },
553 { "pivot-root", required_argument, NULL, ARG_PIVOT_ROOT },
554 { "notify-ready", required_argument, NULL, ARG_NOTIFY_READY },
555 { "root-hash", required_argument, NULL, ARG_ROOT_HASH },
556 { "system-call-filter", required_argument, NULL, ARG_SYSTEM_CALL_FILTER },
557 { "rlimit", required_argument, NULL, ARG_RLIMIT },
558 { "oom-score-adjust", required_argument, NULL, ARG_OOM_SCORE_ADJUST },
559 { "cpu-affinity", required_argument, NULL, ARG_CPU_AFFINITY },
560 { "resolv-conf", required_argument, NULL, ARG_RESOLV_CONF },
561 { "timezone", required_argument, NULL, ARG_TIMEZONE },
562 {}
563 };
564
565 int c, r;
566 const char *p;
567 uint64_t plus = 0, minus = 0;
568 bool mask_all_settings = false, mask_no_settings = false;
569
570 assert(argc >= 0);
571 assert(argv);
572
573 while ((c = getopt_long(argc, argv, "+hD:u:abL:M:jS:Z:qi:xp:nUE:", options, NULL)) >= 0)
574 switch (c) {
575
576 case 'h':
577 return help();
578
579 case ARG_VERSION:
580 return version();
581
582 case 'D':
583 r = parse_path_argument_and_warn(optarg, false, &arg_directory);
584 if (r < 0)
585 return r;
586 break;
587
588 case ARG_TEMPLATE:
589 r = parse_path_argument_and_warn(optarg, false, &arg_template);
590 if (r < 0)
591 return r;
592 break;
593
594 case 'i':
595 r = parse_path_argument_and_warn(optarg, false, &arg_image);
596 if (r < 0)
597 return r;
598 break;
599
600 case 'x':
601 arg_ephemeral = true;
602 arg_settings_mask |= SETTING_EPHEMERAL;
603 break;
604
605 case 'u':
606 r = free_and_strdup(&arg_user, optarg);
607 if (r < 0)
608 return log_oom();
609
610 arg_settings_mask |= SETTING_USER;
611 break;
612
613 case ARG_NETWORK_ZONE: {
614 char *j;
615
616 j = strappend("vz-", optarg);
617 if (!j)
618 return log_oom();
619
620 if (!ifname_valid(j)) {
621 log_error("Network zone name not valid: %s", j);
622 free(j);
623 return -EINVAL;
624 }
625
626 free_and_replace(arg_network_zone, j);
627
628 arg_network_veth = true;
629 arg_private_network = true;
630 arg_settings_mask |= SETTING_NETWORK;
631 break;
632 }
633
634 case ARG_NETWORK_BRIDGE:
635
636 if (!ifname_valid(optarg))
637 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
638 "Bridge interface name not valid: %s", optarg);
639
640 r = free_and_strdup(&arg_network_bridge, optarg);
641 if (r < 0)
642 return log_oom();
643
644 _fallthrough_;
645 case 'n':
646 arg_network_veth = true;
647 arg_private_network = true;
648 arg_settings_mask |= SETTING_NETWORK;
649 break;
650
651 case ARG_NETWORK_VETH_EXTRA:
652 r = veth_extra_parse(&arg_network_veth_extra, optarg);
653 if (r < 0)
654 return log_error_errno(r, "Failed to parse --network-veth-extra= parameter: %s", optarg);
655
656 arg_private_network = true;
657 arg_settings_mask |= SETTING_NETWORK;
658 break;
659
660 case ARG_NETWORK_INTERFACE:
661 if (!ifname_valid(optarg))
662 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
663 "Network interface name not valid: %s", optarg);
664
665 if (strv_extend(&arg_network_interfaces, optarg) < 0)
666 return log_oom();
667
668 arg_private_network = true;
669 arg_settings_mask |= SETTING_NETWORK;
670 break;
671
672 case ARG_NETWORK_MACVLAN:
673
674 if (!ifname_valid(optarg))
675 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
676 "MACVLAN network interface name not valid: %s", optarg);
677
678 if (strv_extend(&arg_network_macvlan, optarg) < 0)
679 return log_oom();
680
681 arg_private_network = true;
682 arg_settings_mask |= SETTING_NETWORK;
683 break;
684
685 case ARG_NETWORK_IPVLAN:
686
687 if (!ifname_valid(optarg))
688 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
689 "IPVLAN network interface name not valid: %s", optarg);
690
691 if (strv_extend(&arg_network_ipvlan, optarg) < 0)
692 return log_oom();
693
694 _fallthrough_;
695 case ARG_PRIVATE_NETWORK:
696 arg_private_network = true;
697 arg_settings_mask |= SETTING_NETWORK;
698 break;
699
700 case ARG_NETWORK_NAMESPACE_PATH:
701 r = parse_path_argument_and_warn(optarg, false, &arg_network_namespace_path);
702 if (r < 0)
703 return r;
704
705 break;
706
707 case 'b':
708 if (arg_start_mode == START_PID2)
709 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
710 "--boot and --as-pid2 may not be combined.");
711
712 arg_start_mode = START_BOOT;
713 arg_settings_mask |= SETTING_START_MODE;
714 break;
715
716 case 'a':
717 if (arg_start_mode == START_BOOT)
718 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
719 "--boot and --as-pid2 may not be combined.");
720
721 arg_start_mode = START_PID2;
722 arg_settings_mask |= SETTING_START_MODE;
723 break;
724
725 case ARG_UUID:
726 r = sd_id128_from_string(optarg, &arg_uuid);
727 if (r < 0)
728 return log_error_errno(r, "Invalid UUID: %s", optarg);
729
730 if (sd_id128_is_null(arg_uuid))
731 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
732 "Machine UUID may not be all zeroes.");
733
734 arg_settings_mask |= SETTING_MACHINE_ID;
735 break;
736
737 case 'S':
738 arg_slice = optarg;
739 break;
740
741 case 'M':
742 if (isempty(optarg))
743 arg_machine = mfree(arg_machine);
744 else {
745 if (!machine_name_is_valid(optarg))
746 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
747 "Invalid machine name: %s", optarg);
748
749 r = free_and_strdup(&arg_machine, optarg);
750 if (r < 0)
751 return log_oom();
752 }
753 break;
754
755 case ARG_HOSTNAME:
756 if (isempty(optarg))
757 arg_hostname = mfree(arg_hostname);
758 else {
759 if (!hostname_is_valid(optarg, false))
760 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
761 "Invalid hostname: %s", optarg);
762
763 r = free_and_strdup(&arg_hostname, optarg);
764 if (r < 0)
765 return log_oom();
766 }
767
768 arg_settings_mask |= SETTING_HOSTNAME;
769 break;
770
771 case 'Z':
772 arg_selinux_context = optarg;
773 break;
774
775 case 'L':
776 arg_selinux_apifs_context = optarg;
777 break;
778
779 case ARG_READ_ONLY:
780 arg_read_only = true;
781 arg_settings_mask |= SETTING_READ_ONLY;
782 break;
783
784 case ARG_CAPABILITY:
785 case ARG_DROP_CAPABILITY: {
786 p = optarg;
787 for (;;) {
788 _cleanup_free_ char *t = NULL;
789
790 r = extract_first_word(&p, &t, ",", 0);
791 if (r < 0)
792 return log_error_errno(r, "Failed to parse capability %s.", t);
793
794 if (r == 0)
795 break;
796
797 if (streq(t, "all")) {
798 if (c == ARG_CAPABILITY)
799 plus = (uint64_t) -1;
800 else
801 minus = (uint64_t) -1;
802 } else {
803 r = capability_from_name(t);
804 if (r < 0)
805 return log_error_errno(r, "Failed to parse capability %s.", t);
806
807 if (c == ARG_CAPABILITY)
808 plus |= 1ULL << r;
809 else
810 minus |= 1ULL << r;
811 }
812 }
813
814 arg_settings_mask |= SETTING_CAPABILITY;
815 break;
816 }
817
818 case ARG_NO_NEW_PRIVILEGES:
819 r = parse_boolean(optarg);
820 if (r < 0)
821 return log_error_errno(r, "Failed to parse --no-new-privileges= argument: %s", optarg);
822
823 arg_no_new_privileges = r;
824 arg_settings_mask |= SETTING_NO_NEW_PRIVILEGES;
825 break;
826
827 case 'j':
828 arg_link_journal = LINK_GUEST;
829 arg_link_journal_try = true;
830 arg_settings_mask |= SETTING_LINK_JOURNAL;
831 break;
832
833 case ARG_LINK_JOURNAL:
834 r = parse_link_journal(optarg, &arg_link_journal, &arg_link_journal_try);
835 if (r < 0) {
836 log_error_errno(r, "Failed to parse link journal mode %s", optarg);
837 return -EINVAL;
838 }
839
840 arg_settings_mask |= SETTING_LINK_JOURNAL;
841 break;
842
843 case ARG_BIND:
844 case ARG_BIND_RO:
845 r = bind_mount_parse(&arg_custom_mounts, &arg_n_custom_mounts, optarg, c == ARG_BIND_RO);
846 if (r < 0)
847 return log_error_errno(r, "Failed to parse --bind(-ro)= argument %s: %m", optarg);
848
849 arg_settings_mask |= SETTING_CUSTOM_MOUNTS;
850 break;
851
852 case ARG_TMPFS:
853 r = tmpfs_mount_parse(&arg_custom_mounts, &arg_n_custom_mounts, optarg);
854 if (r < 0)
855 return log_error_errno(r, "Failed to parse --tmpfs= argument %s: %m", optarg);
856
857 arg_settings_mask |= SETTING_CUSTOM_MOUNTS;
858 break;
859
860 case ARG_OVERLAY:
861 case ARG_OVERLAY_RO:
862 r = overlay_mount_parse(&arg_custom_mounts, &arg_n_custom_mounts, optarg, c == ARG_OVERLAY_RO);
863 if (r == -EADDRNOTAVAIL)
864 return log_error_errno(r, "--overlay(-ro)= needs at least two colon-separated directories specified.");
865 if (r < 0)
866 return log_error_errno(r, "Failed to parse --overlay(-ro)= argument %s: %m", optarg);
867
868 arg_settings_mask |= SETTING_CUSTOM_MOUNTS;
869 break;
870
871 case 'E': {
872 char **n;
873
874 if (!env_assignment_is_valid(optarg))
875 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
876 "Environment variable assignment '%s' is not valid.", optarg);
877
878 n = strv_env_set(arg_setenv, optarg);
879 if (!n)
880 return log_oom();
881
882 strv_free_and_replace(arg_setenv, n);
883 arg_settings_mask |= SETTING_ENVIRONMENT;
884 break;
885 }
886
887 case 'q':
888 arg_quiet = true;
889 break;
890
891 case ARG_SHARE_SYSTEM:
892 /* We don't officially support this anymore, except for compat reasons. People should use the
893 * $SYSTEMD_NSPAWN_SHARE_* environment variables instead. */
894 log_warning("Please do not use --share-system anymore, use $SYSTEMD_NSPAWN_SHARE_* instead.");
895 arg_clone_ns_flags = 0;
896 break;
897
898 case ARG_REGISTER:
899 r = parse_boolean(optarg);
900 if (r < 0) {
901 log_error("Failed to parse --register= argument: %s", optarg);
902 return r;
903 }
904
905 arg_register = r;
906 break;
907
908 case ARG_KEEP_UNIT:
909 arg_keep_unit = true;
910 break;
911
912 case ARG_PERSONALITY:
913
914 arg_personality = personality_from_string(optarg);
915 if (arg_personality == PERSONALITY_INVALID)
916 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
917 "Unknown or unsupported personality '%s'.", optarg);
918
919 arg_settings_mask |= SETTING_PERSONALITY;
920 break;
921
922 case ARG_VOLATILE:
923
924 if (!optarg)
925 arg_volatile_mode = VOLATILE_YES;
926 else if (streq(optarg, "help")) {
927 DUMP_STRING_TABLE(volatile_mode, VolatileMode, _VOLATILE_MODE_MAX);
928 return 0;
929 } else {
930 VolatileMode m;
931
932 m = volatile_mode_from_string(optarg);
933 if (m < 0)
934 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
935 "Failed to parse --volatile= argument: %s", optarg);
936 else
937 arg_volatile_mode = m;
938 }
939
940 arg_settings_mask |= SETTING_VOLATILE_MODE;
941 break;
942
943 case 'p':
944 r = expose_port_parse(&arg_expose_ports, optarg);
945 if (r == -EEXIST)
946 return log_error_errno(r, "Duplicate port specification: %s", optarg);
947 if (r < 0)
948 return log_error_errno(r, "Failed to parse host port %s: %m", optarg);
949
950 arg_settings_mask |= SETTING_EXPOSE_PORTS;
951 break;
952
953 case ARG_PROPERTY:
954 if (strv_extend(&arg_property, optarg) < 0)
955 return log_oom();
956
957 break;
958
959 case ARG_PRIVATE_USERS: {
960 int boolean = -1;
961
962 if (!optarg)
963 boolean = true;
964 else if (!in_charset(optarg, DIGITS))
965 /* do *not* parse numbers as booleans */
966 boolean = parse_boolean(optarg);
967
968 if (boolean == false) {
969 /* no: User namespacing off */
970 arg_userns_mode = USER_NAMESPACE_NO;
971 arg_uid_shift = UID_INVALID;
972 arg_uid_range = UINT32_C(0x10000);
973 } else if (boolean == true) {
974 /* yes: User namespacing on, UID range is read from root dir */
975 arg_userns_mode = USER_NAMESPACE_FIXED;
976 arg_uid_shift = UID_INVALID;
977 arg_uid_range = UINT32_C(0x10000);
978 } else if (streq(optarg, "pick")) {
979 /* pick: User namespacing on, UID range is picked randomly */
980 arg_userns_mode = USER_NAMESPACE_PICK;
981 arg_uid_shift = UID_INVALID;
982 arg_uid_range = UINT32_C(0x10000);
983 } else {
984 _cleanup_free_ char *buffer = NULL;
985 const char *range, *shift;
986
987 /* anything else: User namespacing on, UID range is explicitly configured */
988
989 range = strchr(optarg, ':');
990 if (range) {
991 buffer = strndup(optarg, range - optarg);
992 if (!buffer)
993 return log_oom();
994 shift = buffer;
995
996 range++;
997 r = safe_atou32(range, &arg_uid_range);
998 if (r < 0)
999 return log_error_errno(r, "Failed to parse UID range \"%s\": %m", range);
1000 } else
1001 shift = optarg;
1002
1003 r = parse_uid(shift, &arg_uid_shift);
1004 if (r < 0)
1005 return log_error_errno(r, "Failed to parse UID \"%s\": %m", optarg);
1006
1007 arg_userns_mode = USER_NAMESPACE_FIXED;
1008 }
1009
1010 if (arg_uid_range <= 0)
1011 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1012 "UID range cannot be 0.");
1013
1014 arg_settings_mask |= SETTING_USERNS;
1015 break;
1016 }
1017
1018 case 'U':
1019 if (userns_supported()) {
1020 arg_userns_mode = USER_NAMESPACE_PICK;
1021 arg_uid_shift = UID_INVALID;
1022 arg_uid_range = UINT32_C(0x10000);
1023
1024 arg_settings_mask |= SETTING_USERNS;
1025 }
1026
1027 break;
1028
1029 case ARG_PRIVATE_USERS_CHOWN:
1030 arg_userns_chown = true;
1031
1032 arg_settings_mask |= SETTING_USERNS;
1033 break;
1034
1035 case ARG_KILL_SIGNAL:
1036 if (streq(optarg, "help")) {
1037 DUMP_STRING_TABLE(signal, int, _NSIG);
1038 return 0;
1039 }
1040
1041 arg_kill_signal = signal_from_string(optarg);
1042 if (arg_kill_signal < 0)
1043 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1044 "Cannot parse signal: %s", optarg);
1045
1046 arg_settings_mask |= SETTING_KILL_SIGNAL;
1047 break;
1048
1049 case ARG_SETTINGS:
1050
1051 /* no → do not read files
1052 * yes → read files, do not override cmdline, trust only subset
1053 * override → read files, override cmdline, trust only subset
1054 * trusted → read files, do not override cmdline, trust all
1055 */
1056
1057 r = parse_boolean(optarg);
1058 if (r < 0) {
1059 if (streq(optarg, "trusted")) {
1060 mask_all_settings = false;
1061 mask_no_settings = false;
1062 arg_settings_trusted = true;
1063
1064 } else if (streq(optarg, "override")) {
1065 mask_all_settings = false;
1066 mask_no_settings = true;
1067 arg_settings_trusted = -1;
1068 } else
1069 return log_error_errno(r, "Failed to parse --settings= argument: %s", optarg);
1070 } else if (r > 0) {
1071 /* yes */
1072 mask_all_settings = false;
1073 mask_no_settings = false;
1074 arg_settings_trusted = -1;
1075 } else {
1076 /* no */
1077 mask_all_settings = true;
1078 mask_no_settings = false;
1079 arg_settings_trusted = false;
1080 }
1081
1082 break;
1083
1084 case ARG_CHDIR:
1085 if (!path_is_absolute(optarg))
1086 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1087 "Working directory %s is not an absolute path.", optarg);
1088
1089 r = free_and_strdup(&arg_chdir, optarg);
1090 if (r < 0)
1091 return log_oom();
1092
1093 arg_settings_mask |= SETTING_WORKING_DIRECTORY;
1094 break;
1095
1096 case ARG_PIVOT_ROOT:
1097 r = pivot_root_parse(&arg_pivot_root_new, &arg_pivot_root_old, optarg);
1098 if (r < 0)
1099 return log_error_errno(r, "Failed to parse --pivot-root= argument %s: %m", optarg);
1100
1101 arg_settings_mask |= SETTING_PIVOT_ROOT;
1102 break;
1103
1104 case ARG_NOTIFY_READY:
1105 r = parse_boolean(optarg);
1106 if (r < 0)
1107 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1108 "%s is not a valid notify mode. Valid modes are: yes, no, and ready.", optarg);
1109 arg_notify_ready = r;
1110 arg_settings_mask |= SETTING_NOTIFY_READY;
1111 break;
1112
1113 case ARG_ROOT_HASH: {
1114 void *k;
1115 size_t l;
1116
1117 r = unhexmem(optarg, strlen(optarg), &k, &l);
1118 if (r < 0)
1119 return log_error_errno(r, "Failed to parse root hash: %s", optarg);
1120 if (l < sizeof(sd_id128_t)) {
1121 log_error("Root hash must be at least 128bit long: %s", optarg);
1122 free(k);
1123 return -EINVAL;
1124 }
1125
1126 free(arg_root_hash);
1127 arg_root_hash = k;
1128 arg_root_hash_size = l;
1129 break;
1130 }
1131
1132 case ARG_SYSTEM_CALL_FILTER: {
1133 bool negative;
1134 const char *items;
1135
1136 negative = optarg[0] == '~';
1137 items = negative ? optarg + 1 : optarg;
1138
1139 for (;;) {
1140 _cleanup_free_ char *word = NULL;
1141
1142 r = extract_first_word(&items, &word, NULL, 0);
1143 if (r == 0)
1144 break;
1145 if (r == -ENOMEM)
1146 return log_oom();
1147 if (r < 0)
1148 return log_error_errno(r, "Failed to parse system call filter: %m");
1149
1150 if (negative)
1151 r = strv_extend(&arg_syscall_blacklist, word);
1152 else
1153 r = strv_extend(&arg_syscall_whitelist, word);
1154 if (r < 0)
1155 return log_oom();
1156 }
1157
1158 arg_settings_mask |= SETTING_SYSCALL_FILTER;
1159 break;
1160 }
1161
1162 case ARG_RLIMIT: {
1163 const char *eq;
1164 char *name;
1165 int rl;
1166
1167 if (streq(optarg, "help")) {
1168 DUMP_STRING_TABLE(rlimit, int, _RLIMIT_MAX);
1169 return 0;
1170 }
1171
1172 eq = strchr(optarg, '=');
1173 if (!eq)
1174 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1175 "--rlimit= expects an '=' assignment.");
1176
1177 name = strndup(optarg, eq - optarg);
1178 if (!name)
1179 return log_oom();
1180
1181 rl = rlimit_from_string_harder(name);
1182 if (rl < 0)
1183 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1184 "Unknown resource limit: %s", name);
1185
1186 if (!arg_rlimit[rl]) {
1187 arg_rlimit[rl] = new0(struct rlimit, 1);
1188 if (!arg_rlimit[rl])
1189 return log_oom();
1190 }
1191
1192 r = rlimit_parse(rl, eq + 1, arg_rlimit[rl]);
1193 if (r < 0)
1194 return log_error_errno(r, "Failed to parse resource limit: %s", eq + 1);
1195
1196 arg_settings_mask |= SETTING_RLIMIT_FIRST << rl;
1197 break;
1198 }
1199
1200 case ARG_OOM_SCORE_ADJUST:
1201 r = parse_oom_score_adjust(optarg, &arg_oom_score_adjust);
1202 if (r < 0)
1203 return log_error_errno(r, "Failed to parse --oom-score-adjust= parameter: %s", optarg);
1204
1205 arg_oom_score_adjust_set = true;
1206 arg_settings_mask |= SETTING_OOM_SCORE_ADJUST;
1207 break;
1208
1209 case ARG_CPU_AFFINITY: {
1210 _cleanup_cpu_free_ cpu_set_t *cpuset = NULL;
1211
1212 r = parse_cpu_set(optarg, &cpuset);
1213 if (r < 0)
1214 return log_error_errno(r, "Failed to parse CPU affinity mask: %s", optarg);
1215
1216 if (arg_cpuset)
1217 CPU_FREE(arg_cpuset);
1218
1219 arg_cpuset = TAKE_PTR(cpuset);
1220 arg_cpuset_ncpus = r;
1221 arg_settings_mask |= SETTING_CPU_AFFINITY;
1222 break;
1223 }
1224
1225 case ARG_RESOLV_CONF:
1226 if (streq(optarg, "help")) {
1227 DUMP_STRING_TABLE(resolv_conf_mode, ResolvConfMode, _RESOLV_CONF_MODE_MAX);
1228 return 0;
1229 }
1230
1231 arg_resolv_conf = resolv_conf_mode_from_string(optarg);
1232 if (arg_resolv_conf < 0)
1233 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1234 "Failed to parse /etc/resolv.conf mode: %s", optarg);
1235
1236 arg_settings_mask |= SETTING_RESOLV_CONF;
1237 break;
1238
1239 case ARG_TIMEZONE:
1240 if (streq(optarg, "help")) {
1241 DUMP_STRING_TABLE(timezone_mode, TimezoneMode, _TIMEZONE_MODE_MAX);
1242 return 0;
1243 }
1244
1245 arg_timezone = timezone_mode_from_string(optarg);
1246 if (arg_timezone < 0)
1247 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1248 "Failed to parse /etc/localtime mode: %s", optarg);
1249
1250 arg_settings_mask |= SETTING_TIMEZONE;
1251 break;
1252
1253 case '?':
1254 return -EINVAL;
1255
1256 default:
1257 assert_not_reached("Unhandled option");
1258 }
1259
1260 if (argc > optind) {
1261 strv_free(arg_parameters);
1262 arg_parameters = strv_copy(argv + optind);
1263 if (!arg_parameters)
1264 return log_oom();
1265
1266 arg_settings_mask |= SETTING_START_MODE;
1267 }
1268
1269 if (arg_ephemeral && arg_template && !arg_directory)
1270 /* User asked for ephemeral execution but specified --template= instead of --directory=. Semantically
1271 * such an invocation makes some sense, see https://github.com/systemd/systemd/issues/3667. Let's
1272 * accept this here, and silently make "--ephemeral --template=" equivalent to "--ephemeral
1273 * --directory=". */
1274 arg_directory = TAKE_PTR(arg_template);
1275
1276 arg_caps_retain = (arg_caps_retain | plus | (arg_private_network ? 1ULL << CAP_NET_ADMIN : 0)) & ~minus;
1277
1278 /* Load all settings from .nspawn files */
1279 if (mask_no_settings)
1280 arg_settings_mask = 0;
1281
1282 /* Don't load any settings from .nspawn files */
1283 if (mask_all_settings)
1284 arg_settings_mask = _SETTINGS_MASK_ALL;
1285
1286 return 1;
1287 }
1288
1289 static int verify_arguments(void) {
1290 int r;
1291
1292 if (arg_userns_mode != USER_NAMESPACE_NO)
1293 arg_mount_settings |= MOUNT_USE_USERNS;
1294
1295 if (arg_private_network)
1296 arg_mount_settings |= MOUNT_APPLY_APIVFS_NETNS;
1297
1298 if (!(arg_clone_ns_flags & CLONE_NEWPID) ||
1299 !(arg_clone_ns_flags & CLONE_NEWUTS)) {
1300 arg_register = false;
1301 if (arg_start_mode != START_PID1)
1302 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--boot cannot be used without namespacing.");
1303 }
1304
1305 if (arg_userns_mode == USER_NAMESPACE_PICK)
1306 arg_userns_chown = true;
1307
1308 if (arg_start_mode == START_BOOT && arg_kill_signal <= 0)
1309 arg_kill_signal = SIGRTMIN+3;
1310
1311 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 */
1312 arg_read_only = true;
1313
1314 if (arg_keep_unit && arg_register && cg_pid_get_owner_uid(0, NULL) >= 0)
1315 /* Save the user from accidentally registering either user-$SESSION.scope or user@.service.
1316 * The latter is not technically a user session, but we don't need to labour the point. */
1317 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--keep-unit --register=yes may not be used when invoked from a user session.");
1318
1319 if (arg_directory && arg_image)
1320 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--directory= and --image= may not be combined.");
1321
1322 if (arg_template && arg_image)
1323 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--template= and --image= may not be combined.");
1324
1325 if (arg_template && !(arg_directory || arg_machine))
1326 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--template= needs --directory= or --machine=.");
1327
1328 if (arg_ephemeral && arg_template)
1329 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--ephemeral and --template= may not be combined.");
1330
1331 if (arg_ephemeral && !IN_SET(arg_link_journal, LINK_NO, LINK_AUTO))
1332 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--ephemeral and --link-journal= may not be combined.");
1333
1334 if (arg_userns_mode != USER_NAMESPACE_NO && !userns_supported())
1335 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--private-users= is not supported, kernel compiled without user namespace support.");
1336
1337 if (arg_userns_chown && arg_read_only)
1338 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--read-only and --private-users-chown may not be combined.");
1339
1340 /* We don't support --private-users-chown together with any of the volatile modes since we couldn't
1341 * change the read-only part of the tree (i.e. /usr) anyway, or because it would trigger a massive
1342 * copy-up (in case of overlay) making the entire excercise pointless. */
1343 if (arg_userns_chown && arg_volatile_mode != VOLATILE_NO)
1344 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--volatile= and --private-users-chown may not be combined.");
1345
1346 /* If --network-namespace-path is given with any other network-related option,
1347 * we need to error out, to avoid conflicts between different network options. */
1348 if (arg_network_namespace_path &&
1349 (arg_network_interfaces || arg_network_macvlan ||
1350 arg_network_ipvlan || arg_network_veth_extra ||
1351 arg_network_bridge || arg_network_zone ||
1352 arg_network_veth || arg_private_network))
1353 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--network-namespace-path cannot be combined with other network options.");
1354
1355 if (arg_network_bridge && arg_network_zone)
1356 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--network-bridge= and --network-zone= may not be combined.");
1357
1358 if (arg_userns_mode != USER_NAMESPACE_NO && (arg_mount_settings & MOUNT_APPLY_APIVFS_NETNS) && !arg_private_network)
1359 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid namespacing settings. Mounting sysfs with --private-users requires --private-network.");
1360
1361 if (arg_userns_mode != USER_NAMESPACE_NO && !(arg_mount_settings & MOUNT_APPLY_APIVFS_RO))
1362 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot combine --private-users with read-write mounts.");
1363
1364 if (arg_expose_ports && !arg_private_network)
1365 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot use --port= without private networking.");
1366
1367 #if ! HAVE_LIBIPTC
1368 if (arg_expose_ports)
1369 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--port= is not supported, compiled without libiptc support.");
1370 #endif
1371
1372 r = custom_mount_check_all();
1373 if (r < 0)
1374 return r;
1375
1376 return 0;
1377 }
1378
1379 static int userns_lchown(const char *p, uid_t uid, gid_t gid) {
1380 assert(p);
1381
1382 if (arg_userns_mode == USER_NAMESPACE_NO)
1383 return 0;
1384
1385 if (uid == UID_INVALID && gid == GID_INVALID)
1386 return 0;
1387
1388 if (uid != UID_INVALID) {
1389 uid += arg_uid_shift;
1390
1391 if (uid < arg_uid_shift || uid >= arg_uid_shift + arg_uid_range)
1392 return -EOVERFLOW;
1393 }
1394
1395 if (gid != GID_INVALID) {
1396 gid += (gid_t) arg_uid_shift;
1397
1398 if (gid < (gid_t) arg_uid_shift || gid >= (gid_t) (arg_uid_shift + arg_uid_range))
1399 return -EOVERFLOW;
1400 }
1401
1402 if (lchown(p, uid, gid) < 0)
1403 return -errno;
1404
1405 return 0;
1406 }
1407
1408 static int userns_mkdir(const char *root, const char *path, mode_t mode, uid_t uid, gid_t gid) {
1409 const char *q;
1410 int r;
1411
1412 q = prefix_roota(root, path);
1413 r = mkdir_errno_wrapper(q, mode);
1414 if (r == -EEXIST)
1415 return 0;
1416 if (r < 0)
1417 return r;
1418
1419 return userns_lchown(q, uid, gid);
1420 }
1421
1422 static const char *timezone_from_path(const char *path) {
1423 return PATH_STARTSWITH_SET(
1424 path,
1425 "../usr/share/zoneinfo/",
1426 "/usr/share/zoneinfo/");
1427 }
1428
1429 static bool etc_writable(void) {
1430 return !arg_read_only || IN_SET(arg_volatile_mode, VOLATILE_YES, VOLATILE_OVERLAY);
1431 }
1432
1433 static int setup_timezone(const char *dest) {
1434 _cleanup_free_ char *p = NULL, *etc = NULL;
1435 const char *where, *check;
1436 TimezoneMode m;
1437 int r;
1438
1439 assert(dest);
1440
1441 if (IN_SET(arg_timezone, TIMEZONE_AUTO, TIMEZONE_SYMLINK)) {
1442 r = readlink_malloc("/etc/localtime", &p);
1443 if (r == -ENOENT && arg_timezone == TIMEZONE_AUTO)
1444 m = etc_writable() ? TIMEZONE_DELETE : TIMEZONE_OFF;
1445 else if (r == -EINVAL && arg_timezone == TIMEZONE_AUTO) /* regular file? */
1446 m = etc_writable() ? TIMEZONE_COPY : TIMEZONE_BIND;
1447 else if (r < 0) {
1448 log_warning_errno(r, "Failed to read host's /etc/localtime symlink, not updating container timezone: %m");
1449 /* To handle warning, delete /etc/localtime and replace it with a symbolic link to a time zone data
1450 * file.
1451 *
1452 * Example:
1453 * ln -s /usr/share/zoneinfo/UTC /etc/localtime
1454 */
1455 return 0;
1456 } else if (arg_timezone == TIMEZONE_AUTO)
1457 m = etc_writable() ? TIMEZONE_SYMLINK : TIMEZONE_BIND;
1458 else
1459 m = arg_timezone;
1460 } else
1461 m = arg_timezone;
1462
1463 if (m == TIMEZONE_OFF)
1464 return 0;
1465
1466 r = chase_symlinks("/etc", dest, CHASE_PREFIX_ROOT, &etc);
1467 if (r < 0) {
1468 log_warning_errno(r, "Failed to resolve /etc path in container, ignoring: %m");
1469 return 0;
1470 }
1471
1472 where = strjoina(etc, "/localtime");
1473
1474 switch (m) {
1475
1476 case TIMEZONE_DELETE:
1477 if (unlink(where) < 0)
1478 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, "Failed to remove '%s', ignoring: %m", where);
1479
1480 return 0;
1481
1482 case TIMEZONE_SYMLINK: {
1483 _cleanup_free_ char *q = NULL;
1484 const char *z, *what;
1485
1486 z = timezone_from_path(p);
1487 if (!z) {
1488 log_warning("/etc/localtime does not point into /usr/share/zoneinfo/, not updating container timezone.");
1489 return 0;
1490 }
1491
1492 r = readlink_malloc(where, &q);
1493 if (r >= 0 && streq_ptr(timezone_from_path(q), z))
1494 return 0; /* Already pointing to the right place? Then do nothing .. */
1495
1496 check = strjoina(dest, "/usr/share/zoneinfo/", z);
1497 r = chase_symlinks(check, dest, 0, NULL);
1498 if (r < 0)
1499 log_debug_errno(r, "Timezone %s does not exist (or is not accessible) in container, not creating symlink: %m", z);
1500 else {
1501 if (unlink(where) < 0 && errno != ENOENT) {
1502 log_full_errno(IN_SET(errno, EROFS, EACCES, EPERM) ? LOG_DEBUG : LOG_WARNING, /* Don't complain on read-only images */
1503 errno, "Failed to remove existing timezone info %s in container, ignoring: %m", where);
1504 return 0;
1505 }
1506
1507 what = strjoina("../usr/share/zoneinfo/", z);
1508 if (symlink(what, where) < 0) {
1509 log_full_errno(IN_SET(errno, EROFS, EACCES, EPERM) ? LOG_DEBUG : LOG_WARNING,
1510 errno, "Failed to correct timezone of container, ignoring: %m");
1511 return 0;
1512 }
1513
1514 break;
1515 }
1516
1517 _fallthrough_;
1518 }
1519
1520 case TIMEZONE_BIND: {
1521 _cleanup_free_ char *resolved = NULL;
1522 int found;
1523
1524 found = chase_symlinks(where, dest, CHASE_NONEXISTENT, &resolved);
1525 if (found < 0) {
1526 log_warning_errno(found, "Failed to resolve /etc/localtime path in container, ignoring: %m");
1527 return 0;
1528 }
1529
1530 if (found == 0) /* missing? */
1531 (void) touch(resolved);
1532
1533 r = mount_verbose(LOG_WARNING, "/etc/localtime", resolved, NULL, MS_BIND, NULL);
1534 if (r >= 0)
1535 return mount_verbose(LOG_ERR, NULL, resolved, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY|MS_NOSUID|MS_NODEV, NULL);
1536
1537 _fallthrough_;
1538 }
1539
1540 case TIMEZONE_COPY:
1541 /* If mounting failed, try to copy */
1542 r = copy_file_atomic("/etc/localtime", where, 0644, 0, COPY_REFLINK|COPY_REPLACE);
1543 if (r < 0) {
1544 log_full_errno(IN_SET(r, -EROFS, -EACCES, -EPERM) ? LOG_DEBUG : LOG_WARNING, r,
1545 "Failed to copy /etc/localtime to %s, ignoring: %m", where);
1546 return 0;
1547 }
1548
1549 break;
1550
1551 default:
1552 assert_not_reached("unexpected mode");
1553 }
1554
1555 /* Fix permissions of the symlink or file copy we just created */
1556 r = userns_lchown(where, 0, 0);
1557 if (r < 0)
1558 log_warning_errno(r, "Failed to chown /etc/localtime, ignoring: %m");
1559
1560 return 0;
1561 }
1562
1563 static int have_resolv_conf(const char *path) {
1564 assert(path);
1565
1566 if (access(path, F_OK) < 0) {
1567 if (errno == ENOENT)
1568 return 0;
1569
1570 return log_debug_errno(errno, "Failed to determine whether '%s' is available: %m", path);
1571 }
1572
1573 return 1;
1574 }
1575
1576 static int resolved_listening(void) {
1577 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1578 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1579 _cleanup_free_ char *dns_stub_listener_mode = NULL;
1580 int r;
1581
1582 /* Check if resolved is listening */
1583
1584 r = sd_bus_open_system(&bus);
1585 if (r < 0)
1586 return log_debug_errno(r, "Failed to open system bus: %m");
1587
1588 r = bus_name_has_owner(bus, "org.freedesktop.resolve1", NULL);
1589 if (r < 0)
1590 return log_debug_errno(r, "Failed to check whether the 'org.freedesktop.resolve1' bus name is taken: %m");
1591 if (r == 0)
1592 return 0;
1593
1594 r = sd_bus_get_property_string(bus,
1595 "org.freedesktop.resolve1",
1596 "/org/freedesktop/resolve1",
1597 "org.freedesktop.resolve1.Manager",
1598 "DNSStubListener",
1599 &error,
1600 &dns_stub_listener_mode);
1601 if (r < 0)
1602 return log_debug_errno(r, "Failed to query DNSStubListener property: %s", bus_error_message(&error, r));
1603
1604 return STR_IN_SET(dns_stub_listener_mode, "udp", "yes");
1605 }
1606
1607 static int setup_resolv_conf(const char *dest) {
1608 _cleanup_free_ char *etc = NULL;
1609 const char *where, *what;
1610 ResolvConfMode m;
1611 int r;
1612
1613 assert(dest);
1614
1615 if (arg_resolv_conf == RESOLV_CONF_AUTO) {
1616 if (arg_private_network)
1617 m = RESOLV_CONF_OFF;
1618 else if (have_resolv_conf(STATIC_RESOLV_CONF) > 0 && resolved_listening() > 0)
1619 m = etc_writable() ? RESOLV_CONF_COPY_STATIC : RESOLV_CONF_BIND_STATIC;
1620 else if (have_resolv_conf("/etc/resolv.conf") > 0)
1621 m = etc_writable() ? RESOLV_CONF_COPY_HOST : RESOLV_CONF_BIND_HOST;
1622 else
1623 m = etc_writable() ? RESOLV_CONF_DELETE : RESOLV_CONF_OFF;
1624 } else
1625 m = arg_resolv_conf;
1626
1627 if (m == RESOLV_CONF_OFF)
1628 return 0;
1629
1630 r = chase_symlinks("/etc", dest, CHASE_PREFIX_ROOT, &etc);
1631 if (r < 0) {
1632 log_warning_errno(r, "Failed to resolve /etc path in container, ignoring: %m");
1633 return 0;
1634 }
1635
1636 where = strjoina(etc, "/resolv.conf");
1637
1638 if (m == RESOLV_CONF_DELETE) {
1639 if (unlink(where) < 0)
1640 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, "Failed to remove '%s', ignoring: %m", where);
1641
1642 return 0;
1643 }
1644
1645 if (IN_SET(m, RESOLV_CONF_BIND_STATIC, RESOLV_CONF_COPY_STATIC))
1646 what = STATIC_RESOLV_CONF;
1647 else
1648 what = "/etc/resolv.conf";
1649
1650 if (IN_SET(m, RESOLV_CONF_BIND_HOST, RESOLV_CONF_BIND_STATIC)) {
1651 _cleanup_free_ char *resolved = NULL;
1652 int found;
1653
1654 found = chase_symlinks(where, dest, CHASE_NONEXISTENT, &resolved);
1655 if (found < 0) {
1656 log_warning_errno(found, "Failed to resolve /etc/resolv.conf path in container, ignoring: %m");
1657 return 0;
1658 }
1659
1660 if (found == 0) /* missing? */
1661 (void) touch(resolved);
1662
1663 r = mount_verbose(LOG_WARNING, what, resolved, NULL, MS_BIND, NULL);
1664 if (r >= 0)
1665 return mount_verbose(LOG_ERR, NULL, resolved, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY|MS_NOSUID|MS_NODEV, NULL);
1666 }
1667
1668 /* If that didn't work, let's copy the file */
1669 r = copy_file(what, where, O_TRUNC|O_NOFOLLOW, 0644, 0, COPY_REFLINK);
1670 if (r < 0) {
1671 /* If the file already exists as symlink, let's suppress the warning, under the assumption that
1672 * resolved or something similar runs inside and the symlink points there.
1673 *
1674 * If the disk image is read-only, there's also no point in complaining.
1675 */
1676 log_full_errno(!IN_SET(RESOLV_CONF_COPY_HOST, RESOLV_CONF_COPY_STATIC) && IN_SET(r, -ELOOP, -EROFS, -EACCES, -EPERM) ? LOG_DEBUG : LOG_WARNING, r,
1677 "Failed to copy /etc/resolv.conf to %s, ignoring: %m", where);
1678 return 0;
1679 }
1680
1681 r = userns_lchown(where, 0, 0);
1682 if (r < 0)
1683 log_warning_errno(r, "Failed to chown /etc/resolv.conf, ignoring: %m");
1684
1685 return 0;
1686 }
1687
1688 static int setup_boot_id(void) {
1689 _cleanup_(unlink_and_freep) char *from = NULL;
1690 _cleanup_free_ char *path = NULL;
1691 sd_id128_t rnd = SD_ID128_NULL;
1692 const char *to;
1693 int r;
1694
1695 /* Generate a new randomized boot ID, so that each boot-up of
1696 * the container gets a new one */
1697
1698 r = tempfn_random_child(NULL, "proc-sys-kernel-random-boot-id", &path);
1699 if (r < 0)
1700 return log_error_errno(r, "Failed to generate random boot ID path: %m");
1701
1702 r = sd_id128_randomize(&rnd);
1703 if (r < 0)
1704 return log_error_errno(r, "Failed to generate random boot id: %m");
1705
1706 r = id128_write(path, ID128_UUID, rnd, false);
1707 if (r < 0)
1708 return log_error_errno(r, "Failed to write boot id: %m");
1709
1710 from = TAKE_PTR(path);
1711 to = "/proc/sys/kernel/random/boot_id";
1712
1713 r = mount_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
1714 if (r < 0)
1715 return r;
1716
1717 return mount_verbose(LOG_ERR, NULL, to, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
1718 }
1719
1720 static int copy_devnodes(const char *dest) {
1721 static const char devnodes[] =
1722 "null\0"
1723 "zero\0"
1724 "full\0"
1725 "random\0"
1726 "urandom\0"
1727 "tty\0"
1728 "net/tun\0";
1729
1730 const char *d;
1731 int r = 0;
1732 _cleanup_umask_ mode_t u;
1733
1734 assert(dest);
1735
1736 u = umask(0000);
1737
1738 /* Create /dev/net, so that we can create /dev/net/tun in it */
1739 if (userns_mkdir(dest, "/dev/net", 0755, 0, 0) < 0)
1740 return log_error_errno(r, "Failed to create /dev/net directory: %m");
1741
1742 NULSTR_FOREACH(d, devnodes) {
1743 _cleanup_free_ char *from = NULL, *to = NULL;
1744 struct stat st;
1745
1746 from = strappend("/dev/", d);
1747 if (!from)
1748 return log_oom();
1749
1750 to = prefix_root(dest, from);
1751 if (!to)
1752 return log_oom();
1753
1754 if (stat(from, &st) < 0) {
1755
1756 if (errno != ENOENT)
1757 return log_error_errno(errno, "Failed to stat %s: %m", from);
1758
1759 } else if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
1760 return log_error_errno(SYNTHETIC_ERRNO(EIO),
1761 "%s is not a char or block device, cannot copy.", from);
1762 else {
1763 _cleanup_free_ char *sl = NULL, *prefixed = NULL, *dn = NULL, *t = NULL;
1764
1765 if (mknod(to, st.st_mode, st.st_rdev) < 0) {
1766 /* Explicitly warn the user when /dev is already populated. */
1767 if (errno == EEXIST)
1768 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);
1769 if (errno != EPERM)
1770 return log_error_errno(errno, "mknod(%s) failed: %m", to);
1771
1772 /* Some systems abusively restrict mknod but allow bind mounts. */
1773 r = touch(to);
1774 if (r < 0)
1775 return log_error_errno(r, "touch (%s) failed: %m", to);
1776 r = mount_verbose(LOG_DEBUG, from, to, NULL, MS_BIND, NULL);
1777 if (r < 0)
1778 return log_error_errno(r, "Both mknod and bind mount (%s) failed: %m", to);
1779 }
1780
1781 r = userns_lchown(to, 0, 0);
1782 if (r < 0)
1783 return log_error_errno(r, "chown() of device node %s failed: %m", to);
1784
1785 dn = strjoin("/dev/", S_ISCHR(st.st_mode) ? "char" : "block");
1786 if (!dn)
1787 return log_oom();
1788
1789 r = userns_mkdir(dest, dn, 0755, 0, 0);
1790 if (r < 0)
1791 return log_error_errno(r, "Failed to create '%s': %m", dn);
1792
1793 if (asprintf(&sl, "%s/%u:%u", dn, major(st.st_rdev), minor(st.st_rdev)) < 0)
1794 return log_oom();
1795
1796 prefixed = prefix_root(dest, sl);
1797 if (!prefixed)
1798 return log_oom();
1799
1800 t = strjoin("../", d);
1801 if (!t)
1802 return log_oom();
1803
1804 if (symlink(t, prefixed) < 0)
1805 log_debug_errno(errno, "Failed to symlink '%s' to '%s': %m", t, prefixed);
1806 }
1807 }
1808
1809 return r;
1810 }
1811
1812 static int setup_pts(const char *dest) {
1813 _cleanup_free_ char *options = NULL;
1814 const char *p;
1815 int r;
1816
1817 #if HAVE_SELINUX
1818 if (arg_selinux_apifs_context)
1819 (void) asprintf(&options,
1820 "newinstance,ptmxmode=0666,mode=620,gid=" GID_FMT ",context=\"%s\"",
1821 arg_uid_shift + TTY_GID,
1822 arg_selinux_apifs_context);
1823 else
1824 #endif
1825 (void) asprintf(&options,
1826 "newinstance,ptmxmode=0666,mode=620,gid=" GID_FMT,
1827 arg_uid_shift + TTY_GID);
1828
1829 if (!options)
1830 return log_oom();
1831
1832 /* Mount /dev/pts itself */
1833 p = prefix_roota(dest, "/dev/pts");
1834 r = mkdir_errno_wrapper(p, 0755);
1835 if (r < 0)
1836 return log_error_errno(r, "Failed to create /dev/pts: %m");
1837
1838 r = mount_verbose(LOG_ERR, "devpts", p, "devpts", MS_NOSUID|MS_NOEXEC, options);
1839 if (r < 0)
1840 return r;
1841 r = userns_lchown(p, 0, 0);
1842 if (r < 0)
1843 return log_error_errno(r, "Failed to chown /dev/pts: %m");
1844
1845 /* Create /dev/ptmx symlink */
1846 p = prefix_roota(dest, "/dev/ptmx");
1847 if (symlink("pts/ptmx", p) < 0)
1848 return log_error_errno(errno, "Failed to create /dev/ptmx symlink: %m");
1849 r = userns_lchown(p, 0, 0);
1850 if (r < 0)
1851 return log_error_errno(r, "Failed to chown /dev/ptmx: %m");
1852
1853 /* And fix /dev/pts/ptmx ownership */
1854 p = prefix_roota(dest, "/dev/pts/ptmx");
1855 r = userns_lchown(p, 0, 0);
1856 if (r < 0)
1857 return log_error_errno(r, "Failed to chown /dev/pts/ptmx: %m");
1858
1859 return 0;
1860 }
1861
1862 static int setup_dev_console(const char *dest, const char *console) {
1863 _cleanup_umask_ mode_t u;
1864 const char *to;
1865 int r;
1866
1867 assert(dest);
1868 assert(console);
1869
1870 u = umask(0000);
1871
1872 r = chmod_and_chown(console, 0600, arg_uid_shift, arg_uid_shift);
1873 if (r < 0)
1874 return log_error_errno(r, "Failed to correct access mode for TTY: %m");
1875
1876 /* We need to bind mount the right tty to /dev/console since
1877 * ptys can only exist on pts file systems. To have something
1878 * to bind mount things on we create a empty regular file. */
1879
1880 to = prefix_roota(dest, "/dev/console");
1881 r = touch(to);
1882 if (r < 0)
1883 return log_error_errno(r, "touch() for /dev/console failed: %m");
1884
1885 return mount_verbose(LOG_ERR, console, to, NULL, MS_BIND, NULL);
1886 }
1887
1888 static int setup_keyring(void) {
1889 key_serial_t keyring;
1890
1891 /* Allocate a new session keyring for the container. This makes sure the keyring of the session systemd-nspawn
1892 * was invoked from doesn't leak into the container. Note that by default we block keyctl() and request_key()
1893 * anyway via seccomp so doing this operation isn't strictly necessary, but in case people explicitly whitelist
1894 * these system calls let's make sure we don't leak anything into the container. */
1895
1896 keyring = keyctl(KEYCTL_JOIN_SESSION_KEYRING, 0, 0, 0, 0);
1897 if (keyring == -1) {
1898 if (errno == ENOSYS)
1899 log_debug_errno(errno, "Kernel keyring not supported, ignoring.");
1900 else if (IN_SET(errno, EACCES, EPERM))
1901 log_debug_errno(errno, "Kernel keyring access prohibited, ignoring.");
1902 else
1903 return log_error_errno(errno, "Setting up kernel keyring failed: %m");
1904 }
1905
1906 return 0;
1907 }
1908
1909 static int setup_kmsg(int kmsg_socket) {
1910 _cleanup_(unlink_and_freep) char *from = NULL;
1911 _cleanup_free_ char *fifo = NULL;
1912 _cleanup_close_ int fd = -1;
1913 _cleanup_umask_ mode_t u;
1914 const char *to;
1915 int r;
1916
1917 assert(kmsg_socket >= 0);
1918
1919 u = umask(0000);
1920
1921 /* We create the kmsg FIFO as as temporary file in /tmp, but immediately delete it after bind mounting it to
1922 * /proc/kmsg. While FIFOs on the reading side behave very similar to /proc/kmsg, their writing side behaves
1923 * differently from /dev/kmsg in that writing blocks when nothing is reading. In order to avoid any problems
1924 * with containers deadlocking due to this we simply make /dev/kmsg unavailable to the container. */
1925
1926 r = tempfn_random_child(NULL, "proc-kmsg", &fifo);
1927 if (r < 0)
1928 return log_error_errno(r, "Failed to generate kmsg path: %m");
1929
1930 if (mkfifo(fifo, 0600) < 0)
1931 return log_error_errno(errno, "mkfifo() for /run/kmsg failed: %m");
1932
1933 from = TAKE_PTR(fifo);
1934 to = "/proc/kmsg";
1935
1936 r = mount_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
1937 if (r < 0)
1938 return r;
1939
1940 fd = open(from, O_RDWR|O_NONBLOCK|O_CLOEXEC);
1941 if (fd < 0)
1942 return log_error_errno(errno, "Failed to open fifo: %m");
1943
1944 /* Store away the fd in the socket, so that it stays open as long as we run the child */
1945 r = send_one_fd(kmsg_socket, fd, 0);
1946 if (r < 0)
1947 return log_error_errno(r, "Failed to send FIFO fd: %m");
1948
1949 return 0;
1950 }
1951
1952 static int on_address_change(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
1953 union in_addr_union *exposed = userdata;
1954
1955 assert(rtnl);
1956 assert(m);
1957 assert(exposed);
1958
1959 expose_port_execute(rtnl, arg_expose_ports, exposed);
1960 return 0;
1961 }
1962
1963 static int setup_hostname(void) {
1964 int r;
1965
1966 if ((arg_clone_ns_flags & CLONE_NEWUTS) == 0)
1967 return 0;
1968
1969 r = sethostname_idempotent(arg_hostname ?: arg_machine);
1970 if (r < 0)
1971 return log_error_errno(r, "Failed to set hostname: %m");
1972
1973 return 0;
1974 }
1975
1976 static int setup_journal(const char *directory) {
1977 _cleanup_free_ char *d = NULL;
1978 const char *dirname, *p, *q;
1979 sd_id128_t this_id;
1980 char id[33];
1981 bool try;
1982 int r;
1983
1984 /* Don't link journals in ephemeral mode */
1985 if (arg_ephemeral)
1986 return 0;
1987
1988 if (arg_link_journal == LINK_NO)
1989 return 0;
1990
1991 try = arg_link_journal_try || arg_link_journal == LINK_AUTO;
1992
1993 r = sd_id128_get_machine(&this_id);
1994 if (r < 0)
1995 return log_error_errno(r, "Failed to retrieve machine ID: %m");
1996
1997 if (sd_id128_equal(arg_uuid, this_id)) {
1998 log_full(try ? LOG_WARNING : LOG_ERR,
1999 "Host and machine ids are equal (%s): refusing to link journals", sd_id128_to_string(arg_uuid, id));
2000 if (try)
2001 return 0;
2002 return -EEXIST;
2003 }
2004
2005 FOREACH_STRING(dirname, "/var", "/var/log", "/var/log/journal") {
2006 r = userns_mkdir(directory, dirname, 0755, 0, 0);
2007 if (r < 0) {
2008 bool ignore = r == -EROFS && try;
2009 log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, r,
2010 "Failed to create %s%s: %m", dirname, ignore ? ", ignoring" : "");
2011 return ignore ? 0 : r;
2012 }
2013 }
2014
2015 (void) sd_id128_to_string(arg_uuid, id);
2016
2017 p = strjoina("/var/log/journal/", id);
2018 q = prefix_roota(directory, p);
2019
2020 if (path_is_mount_point(p, NULL, 0) > 0) {
2021 if (try)
2022 return 0;
2023
2024 return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
2025 "%s: already a mount point, refusing to use for journal", p);
2026 }
2027
2028 if (path_is_mount_point(q, NULL, 0) > 0) {
2029 if (try)
2030 return 0;
2031
2032 return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
2033 "%s: already a mount point, refusing to use for journal", q);
2034 }
2035
2036 r = readlink_and_make_absolute(p, &d);
2037 if (r >= 0) {
2038 if (IN_SET(arg_link_journal, LINK_GUEST, LINK_AUTO) &&
2039 path_equal(d, q)) {
2040
2041 r = userns_mkdir(directory, p, 0755, 0, 0);
2042 if (r < 0)
2043 log_warning_errno(r, "Failed to create directory %s: %m", q);
2044 return 0;
2045 }
2046
2047 if (unlink(p) < 0)
2048 return log_error_errno(errno, "Failed to remove symlink %s: %m", p);
2049 } else if (r == -EINVAL) {
2050
2051 if (arg_link_journal == LINK_GUEST &&
2052 rmdir(p) < 0) {
2053
2054 if (errno == ENOTDIR) {
2055 log_error("%s already exists and is neither a symlink nor a directory", p);
2056 return r;
2057 } else
2058 return log_error_errno(errno, "Failed to remove %s: %m", p);
2059 }
2060 } else if (r != -ENOENT)
2061 return log_error_errno(r, "readlink(%s) failed: %m", p);
2062
2063 if (arg_link_journal == LINK_GUEST) {
2064
2065 if (symlink(q, p) < 0) {
2066 if (try) {
2067 log_debug_errno(errno, "Failed to symlink %s to %s, skipping journal setup: %m", q, p);
2068 return 0;
2069 } else
2070 return log_error_errno(errno, "Failed to symlink %s to %s: %m", q, p);
2071 }
2072
2073 r = userns_mkdir(directory, p, 0755, 0, 0);
2074 if (r < 0)
2075 log_warning_errno(r, "Failed to create directory %s: %m", q);
2076 return 0;
2077 }
2078
2079 if (arg_link_journal == LINK_HOST) {
2080 /* don't create parents here — if the host doesn't have
2081 * permanent journal set up, don't force it here */
2082
2083 r = mkdir_errno_wrapper(p, 0755);
2084 if (r < 0 && r != -EEXIST) {
2085 if (try) {
2086 log_debug_errno(r, "Failed to create %s, skipping journal setup: %m", p);
2087 return 0;
2088 } else
2089 return log_error_errno(r, "Failed to create %s: %m", p);
2090 }
2091
2092 } else if (access(p, F_OK) < 0)
2093 return 0;
2094
2095 if (dir_is_empty(q) == 0)
2096 log_warning("%s is not empty, proceeding anyway.", q);
2097
2098 r = userns_mkdir(directory, p, 0755, 0, 0);
2099 if (r < 0)
2100 return log_error_errno(r, "Failed to create %s: %m", q);
2101
2102 r = mount_verbose(LOG_DEBUG, p, q, NULL, MS_BIND, NULL);
2103 if (r < 0)
2104 return log_error_errno(errno, "Failed to bind mount journal from host into guest: %m");
2105
2106 return 0;
2107 }
2108
2109 static int drop_capabilities(void) {
2110 return capability_bounding_set_drop(arg_caps_retain, false);
2111 }
2112
2113 static int reset_audit_loginuid(void) {
2114 _cleanup_free_ char *p = NULL;
2115 int r;
2116
2117 if ((arg_clone_ns_flags & CLONE_NEWPID) == 0)
2118 return 0;
2119
2120 r = read_one_line_file("/proc/self/loginuid", &p);
2121 if (r == -ENOENT)
2122 return 0;
2123 if (r < 0)
2124 return log_error_errno(r, "Failed to read /proc/self/loginuid: %m");
2125
2126 /* Already reset? */
2127 if (streq(p, "4294967295"))
2128 return 0;
2129
2130 r = write_string_file("/proc/self/loginuid", "4294967295", WRITE_STRING_FILE_DISABLE_BUFFER);
2131 if (r < 0) {
2132 log_error_errno(r,
2133 "Failed to reset audit login UID. This probably means that your kernel is too\n"
2134 "old and you have audit enabled. Note that the auditing subsystem is known to\n"
2135 "be incompatible with containers on old kernels. Please make sure to upgrade\n"
2136 "your kernel or to off auditing with 'audit=0' on the kernel command line before\n"
2137 "using systemd-nspawn. Sleeping for 5s... (%m)");
2138
2139 sleep(5);
2140 }
2141
2142 return 0;
2143 }
2144
2145 static int setup_propagate(const char *root) {
2146 const char *p, *q;
2147 int r;
2148
2149 (void) mkdir_p("/run/systemd/nspawn/", 0755);
2150 (void) mkdir_p("/run/systemd/nspawn/propagate", 0600);
2151 p = strjoina("/run/systemd/nspawn/propagate/", arg_machine);
2152 (void) mkdir_p(p, 0600);
2153
2154 r = userns_mkdir(root, "/run/systemd", 0755, 0, 0);
2155 if (r < 0)
2156 return log_error_errno(r, "Failed to create /run/systemd: %m");
2157
2158 r = userns_mkdir(root, "/run/systemd/nspawn", 0755, 0, 0);
2159 if (r < 0)
2160 return log_error_errno(r, "Failed to create /run/systemd/nspawn: %m");
2161
2162 r = userns_mkdir(root, "/run/systemd/nspawn/incoming", 0600, 0, 0);
2163 if (r < 0)
2164 return log_error_errno(r, "Failed to create /run/systemd/nspawn/incoming: %m");
2165
2166 q = prefix_roota(root, "/run/systemd/nspawn/incoming");
2167 r = mount_verbose(LOG_ERR, p, q, NULL, MS_BIND, NULL);
2168 if (r < 0)
2169 return r;
2170
2171 r = mount_verbose(LOG_ERR, NULL, q, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL);
2172 if (r < 0)
2173 return r;
2174
2175 /* machined will MS_MOVE into that directory, and that's only
2176 * supported for non-shared mounts. */
2177 return mount_verbose(LOG_ERR, NULL, q, NULL, MS_SLAVE, NULL);
2178 }
2179
2180 static int setup_machine_id(const char *directory) {
2181 const char *etc_machine_id;
2182 sd_id128_t id;
2183 int r;
2184
2185 /* If the UUID in the container is already set, then that's what counts, and we use. If it isn't set, and the
2186 * caller passed --uuid=, then we'll pass it in the $container_uuid env var to PID 1 of the container. The
2187 * assumption is that PID 1 will then write it to /etc/machine-id to make it persistent. If --uuid= is not
2188 * passed we generate a random UUID, and pass it via $container_uuid. In effect this means that /etc/machine-id
2189 * in the container and our idea of the container UUID will always be in sync (at least if PID 1 in the
2190 * container behaves nicely). */
2191
2192 etc_machine_id = prefix_roota(directory, "/etc/machine-id");
2193
2194 r = id128_read(etc_machine_id, ID128_PLAIN, &id);
2195 if (r < 0) {
2196 if (!IN_SET(r, -ENOENT, -ENOMEDIUM)) /* If the file is missing or empty, we don't mind */
2197 return log_error_errno(r, "Failed to read machine ID from container image: %m");
2198
2199 if (sd_id128_is_null(arg_uuid)) {
2200 r = sd_id128_randomize(&arg_uuid);
2201 if (r < 0)
2202 return log_error_errno(r, "Failed to acquire randomized machine UUID: %m");
2203 }
2204 } else {
2205 if (sd_id128_is_null(id))
2206 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2207 "Machine ID in container image is zero, refusing.");
2208
2209 arg_uuid = id;
2210 }
2211
2212 return 0;
2213 }
2214
2215 static int recursive_chown(const char *directory, uid_t shift, uid_t range) {
2216 int r;
2217
2218 assert(directory);
2219
2220 if (arg_userns_mode == USER_NAMESPACE_NO || !arg_userns_chown)
2221 return 0;
2222
2223 r = path_patch_uid(directory, arg_uid_shift, arg_uid_range);
2224 if (r == -EOPNOTSUPP)
2225 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.");
2226 if (r == -EBADE)
2227 return log_error_errno(r, "Upper 16 bits of root directory UID and GID do not match.");
2228 if (r < 0)
2229 return log_error_errno(r, "Failed to adjust UID/GID shift of OS tree: %m");
2230 if (r == 0)
2231 log_debug("Root directory of image is already owned by the right UID/GID range, skipping recursive chown operation.");
2232 else
2233 log_debug("Patched directory tree to match UID/GID range.");
2234
2235 return r;
2236 }
2237
2238 /*
2239 * Return values:
2240 * < 0 : wait_for_terminate() failed to get the state of the
2241 * container, the container was terminated by a signal, or
2242 * failed for an unknown reason. No change is made to the
2243 * container argument.
2244 * > 0 : The program executed in the container terminated with an
2245 * error. The exit code of the program executed in the
2246 * container is returned. The container argument has been set
2247 * to CONTAINER_TERMINATED.
2248 * 0 : The container is being rebooted, has been shut down or exited
2249 * successfully. The container argument has been set to either
2250 * CONTAINER_TERMINATED or CONTAINER_REBOOTED.
2251 *
2252 * That is, success is indicated by a return value of zero, and an
2253 * error is indicated by a non-zero value.
2254 */
2255 static int wait_for_container(pid_t pid, ContainerStatus *container) {
2256 siginfo_t status;
2257 int r;
2258
2259 r = wait_for_terminate(pid, &status);
2260 if (r < 0)
2261 return log_warning_errno(r, "Failed to wait for container: %m");
2262
2263 switch (status.si_code) {
2264
2265 case CLD_EXITED:
2266 if (status.si_status == 0)
2267 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s exited successfully.", arg_machine);
2268 else
2269 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s failed with error code %i.", arg_machine, status.si_status);
2270
2271 *container = CONTAINER_TERMINATED;
2272 return status.si_status;
2273
2274 case CLD_KILLED:
2275 if (status.si_status == SIGINT) {
2276 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s has been shut down.", arg_machine);
2277 *container = CONTAINER_TERMINATED;
2278 return 0;
2279
2280 } else if (status.si_status == SIGHUP) {
2281 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s is being rebooted.", arg_machine);
2282 *container = CONTAINER_REBOOTED;
2283 return 0;
2284 }
2285
2286 _fallthrough_;
2287 case CLD_DUMPED:
2288 return log_error_errno(SYNTHETIC_ERRNO(EIO),
2289 "Container %s terminated by signal %s.", arg_machine, signal_to_string(status.si_status));
2290
2291 default:
2292 return log_error_errno(SYNTHETIC_ERRNO(EIO),
2293 "Container %s failed due to unknown reason.", arg_machine);
2294 }
2295 }
2296
2297 static int on_orderly_shutdown(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
2298 pid_t pid;
2299
2300 pid = PTR_TO_PID(userdata);
2301 if (pid > 0) {
2302 if (kill(pid, arg_kill_signal) >= 0) {
2303 log_info("Trying to halt container. Send SIGTERM again to trigger immediate termination.");
2304 sd_event_source_set_userdata(s, NULL);
2305 return 0;
2306 }
2307 }
2308
2309 sd_event_exit(sd_event_source_get_event(s), 0);
2310 return 0;
2311 }
2312
2313 static int on_sigchld(sd_event_source *s, const struct signalfd_siginfo *ssi, void *userdata) {
2314 pid_t pid;
2315
2316 assert(s);
2317 assert(ssi);
2318
2319 pid = PTR_TO_PID(userdata);
2320
2321 for (;;) {
2322 siginfo_t si = {};
2323
2324 if (waitid(P_ALL, 0, &si, WNOHANG|WNOWAIT|WEXITED) < 0)
2325 return log_error_errno(errno, "Failed to waitid(): %m");
2326 if (si.si_pid == 0) /* No pending children. */
2327 break;
2328 if (si.si_pid == pid) {
2329 /* The main process we care for has exited. Return from
2330 * signal handler but leave the zombie. */
2331 sd_event_exit(sd_event_source_get_event(s), 0);
2332 break;
2333 }
2334
2335 /* Reap all other children. */
2336 (void) waitid(P_PID, si.si_pid, &si, WNOHANG|WEXITED);
2337 }
2338
2339 return 0;
2340 }
2341
2342 static int on_request_stop(sd_bus_message *m, void *userdata, sd_bus_error *error) {
2343 pid_t pid;
2344
2345 assert(m);
2346
2347 pid = PTR_TO_PID(userdata);
2348
2349 if (arg_kill_signal > 0) {
2350 log_info("Container termination requested. Attempting to halt container.");
2351 (void) kill(pid, arg_kill_signal);
2352 } else {
2353 log_info("Container termination requested. Exiting.");
2354 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), 0);
2355 }
2356
2357 return 0;
2358 }
2359
2360 static int determine_names(void) {
2361 int r;
2362
2363 if (arg_template && !arg_directory && arg_machine) {
2364
2365 /* If --template= was specified then we should not
2366 * search for a machine, but instead create a new one
2367 * in /var/lib/machine. */
2368
2369 arg_directory = strjoin("/var/lib/machines/", arg_machine);
2370 if (!arg_directory)
2371 return log_oom();
2372 }
2373
2374 if (!arg_image && !arg_directory) {
2375 if (arg_machine) {
2376 _cleanup_(image_unrefp) Image *i = NULL;
2377
2378 r = image_find(IMAGE_MACHINE, arg_machine, &i);
2379 if (r == -ENOENT)
2380 return log_error_errno(r, "No image for machine '%s'.", arg_machine);
2381 if (r < 0)
2382 return log_error_errno(r, "Failed to find image for machine '%s': %m", arg_machine);
2383
2384 if (IN_SET(i->type, IMAGE_RAW, IMAGE_BLOCK))
2385 r = free_and_strdup(&arg_image, i->path);
2386 else
2387 r = free_and_strdup(&arg_directory, i->path);
2388 if (r < 0)
2389 return log_oom();
2390
2391 if (!arg_ephemeral)
2392 arg_read_only = arg_read_only || i->read_only;
2393 } else {
2394 r = safe_getcwd(&arg_directory);
2395 if (r < 0)
2396 return log_error_errno(r, "Failed to determine current directory: %m");
2397 }
2398
2399 if (!arg_directory && !arg_image) {
2400 log_error("Failed to determine path, please use -D or -i.");
2401 return -EINVAL;
2402 }
2403 }
2404
2405 if (!arg_machine) {
2406 if (arg_directory && path_equal(arg_directory, "/"))
2407 arg_machine = gethostname_malloc();
2408 else {
2409 if (arg_image) {
2410 char *e;
2411
2412 arg_machine = strdup(basename(arg_image));
2413
2414 /* Truncate suffix if there is one */
2415 e = endswith(arg_machine, ".raw");
2416 if (e)
2417 *e = 0;
2418 } else
2419 arg_machine = strdup(basename(arg_directory));
2420 }
2421 if (!arg_machine)
2422 return log_oom();
2423
2424 hostname_cleanup(arg_machine);
2425 if (!machine_name_is_valid(arg_machine)) {
2426 log_error("Failed to determine machine name automatically, please use -M.");
2427 return -EINVAL;
2428 }
2429
2430 if (arg_ephemeral) {
2431 char *b;
2432
2433 /* Add a random suffix when this is an
2434 * ephemeral machine, so that we can run many
2435 * instances at once without manually having
2436 * to specify -M each time. */
2437
2438 if (asprintf(&b, "%s-%016" PRIx64, arg_machine, random_u64()) < 0)
2439 return log_oom();
2440
2441 free(arg_machine);
2442 arg_machine = b;
2443 }
2444 }
2445
2446 return 0;
2447 }
2448
2449 static int chase_symlinks_and_update(char **p, unsigned flags) {
2450 char *chased;
2451 int r;
2452
2453 assert(p);
2454
2455 if (!*p)
2456 return 0;
2457
2458 r = chase_symlinks(*p, NULL, flags, &chased);
2459 if (r < 0)
2460 return log_error_errno(r, "Failed to resolve path %s: %m", *p);
2461
2462 free_and_replace(*p, chased);
2463 return r; /* r might be an fd here in case we ever use CHASE_OPEN in flags */
2464 }
2465
2466 static int determine_uid_shift(const char *directory) {
2467 int r;
2468
2469 if (arg_userns_mode == USER_NAMESPACE_NO) {
2470 arg_uid_shift = 0;
2471 return 0;
2472 }
2473
2474 if (arg_uid_shift == UID_INVALID) {
2475 struct stat st;
2476
2477 r = stat(directory, &st);
2478 if (r < 0)
2479 return log_error_errno(errno, "Failed to determine UID base of %s: %m", directory);
2480
2481 arg_uid_shift = st.st_uid & UINT32_C(0xffff0000);
2482
2483 if (arg_uid_shift != (st.st_gid & UINT32_C(0xffff0000)))
2484 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2485 "UID and GID base of %s don't match.", directory);
2486
2487 arg_uid_range = UINT32_C(0x10000);
2488 }
2489
2490 if (arg_uid_shift > (uid_t) -1 - arg_uid_range)
2491 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2492 "UID base too high for UID range.");
2493
2494 return 0;
2495 }
2496
2497 static int inner_child(
2498 Barrier *barrier,
2499 const char *directory,
2500 bool secondary,
2501 int kmsg_socket,
2502 int rtnl_socket,
2503 FDSet *fds) {
2504
2505 _cleanup_free_ char *home = NULL;
2506 char as_uuid[37];
2507 size_t n_env = 1;
2508 const char *envp[] = {
2509 "PATH=" DEFAULT_PATH_COMPAT,
2510 NULL, /* container */
2511 NULL, /* TERM */
2512 NULL, /* HOME */
2513 NULL, /* USER */
2514 NULL, /* LOGNAME */
2515 NULL, /* container_uuid */
2516 NULL, /* LISTEN_FDS */
2517 NULL, /* LISTEN_PID */
2518 NULL, /* NOTIFY_SOCKET */
2519 NULL
2520 };
2521 const char *exec_target;
2522 _cleanup_strv_free_ char **env_use = NULL;
2523 int r;
2524
2525 /* This is the "inner" child process, i.e. the one forked off by the "outer" child process, which is the one
2526 * the container manager itself forked off. At the time of clone() it gained its own CLONE_NEWNS, CLONE_NEWPID,
2527 * CLONE_NEWUTS, CLONE_NEWIPC, CLONE_NEWUSER namespaces. Note that it has its own CLONE_NEWNS namespace,
2528 * separate from the CLONE_NEWNS created for the "outer" child, and also separate from the host's CLONE_NEWNS
2529 * namespace. The reason for having two levels of CLONE_NEWNS namespaces is that the "inner" one is owned by
2530 * the CLONE_NEWUSER namespace of the container, while the "outer" one is owned by the host's CLONE_NEWUSER
2531 * namespace.
2532 *
2533 * Note at this point we have no CLONE_NEWNET namespace yet. We'll acquire that one later through
2534 * unshare(). See below. */
2535
2536 assert(barrier);
2537 assert(directory);
2538 assert(kmsg_socket >= 0);
2539
2540 if (arg_userns_mode != USER_NAMESPACE_NO) {
2541 /* Tell the parent, that it now can write the UID map. */
2542 (void) barrier_place(barrier); /* #1 */
2543
2544 /* Wait until the parent wrote the UID map */
2545 if (!barrier_place_and_sync(barrier)) /* #2 */
2546 return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
2547 "Parent died too early");
2548 }
2549
2550 r = reset_uid_gid();
2551 if (r < 0)
2552 return log_error_errno(r, "Couldn't become new root: %m");
2553
2554 r = mount_all(NULL,
2555 arg_mount_settings | MOUNT_IN_USERNS,
2556 arg_uid_shift,
2557 arg_selinux_apifs_context);
2558 if (r < 0)
2559 return r;
2560
2561 if (!arg_network_namespace_path && arg_private_network) {
2562 r = unshare(CLONE_NEWNET);
2563 if (r < 0)
2564 return log_error_errno(errno, "Failed to unshare network namespace: %m");
2565
2566 /* Tell the parent that it can setup network interfaces. */
2567 (void) barrier_place(barrier); /* #3 */
2568 }
2569
2570 r = mount_sysfs(NULL, arg_mount_settings);
2571 if (r < 0)
2572 return r;
2573
2574 /* Wait until we are cgroup-ified, so that we
2575 * can mount the right cgroup path writable */
2576 if (!barrier_place_and_sync(barrier)) /* #4 */
2577 return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
2578 "Parent died too early");
2579
2580 if (arg_use_cgns) {
2581 r = unshare(CLONE_NEWCGROUP);
2582 if (r < 0)
2583 return log_error_errno(errno, "Failed to unshare cgroup namespace: %m");
2584 r = mount_cgroups(
2585 "",
2586 arg_unified_cgroup_hierarchy,
2587 arg_userns_mode != USER_NAMESPACE_NO,
2588 arg_uid_shift,
2589 arg_uid_range,
2590 arg_selinux_apifs_context,
2591 true);
2592 if (r < 0)
2593 return r;
2594 } else {
2595 r = mount_systemd_cgroup_writable("", arg_unified_cgroup_hierarchy);
2596 if (r < 0)
2597 return r;
2598 }
2599
2600 r = setup_boot_id();
2601 if (r < 0)
2602 return r;
2603
2604 r = setup_kmsg(kmsg_socket);
2605 if (r < 0)
2606 return r;
2607 kmsg_socket = safe_close(kmsg_socket);
2608
2609 if (setsid() < 0)
2610 return log_error_errno(errno, "setsid() failed: %m");
2611
2612 if (arg_private_network)
2613 loopback_setup();
2614
2615 if (arg_expose_ports) {
2616 r = expose_port_send_rtnl(rtnl_socket);
2617 if (r < 0)
2618 return r;
2619 rtnl_socket = safe_close(rtnl_socket);
2620 }
2621
2622 if (arg_oom_score_adjust_set) {
2623 r = set_oom_score_adjust(arg_oom_score_adjust);
2624 if (r < 0)
2625 return log_error_errno(r, "Failed to adjust OOM score: %m");
2626 }
2627
2628 if (arg_cpuset)
2629 if (sched_setaffinity(0, CPU_ALLOC_SIZE(arg_cpuset_ncpus), arg_cpuset) < 0)
2630 return log_error_errno(errno, "Failed to set CPU affinity: %m");
2631
2632 r = drop_capabilities();
2633 if (r < 0)
2634 return log_error_errno(r, "drop_capabilities() failed: %m");
2635
2636 (void) setup_hostname();
2637
2638 if (arg_personality != PERSONALITY_INVALID) {
2639 r = safe_personality(arg_personality);
2640 if (r < 0)
2641 return log_error_errno(r, "personality() failed: %m");
2642 } else if (secondary) {
2643 r = safe_personality(PER_LINUX32);
2644 if (r < 0)
2645 return log_error_errno(r, "personality() failed: %m");
2646 }
2647
2648 #if HAVE_SELINUX
2649 if (arg_selinux_context)
2650 if (setexeccon(arg_selinux_context) < 0)
2651 return log_error_errno(errno, "setexeccon(\"%s\") failed: %m", arg_selinux_context);
2652 #endif
2653
2654 r = change_uid_gid(arg_user, &home);
2655 if (r < 0)
2656 return r;
2657
2658 if (arg_no_new_privileges)
2659 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
2660 return log_error_errno(errno, "Failed to disable new privileges: %m");
2661
2662 /* LXC sets container=lxc, so follow the scheme here */
2663 envp[n_env++] = strjoina("container=", arg_container_service_name);
2664
2665 envp[n_env] = strv_find_prefix(environ, "TERM=");
2666 if (envp[n_env])
2667 n_env++;
2668
2669 if ((asprintf((char**)(envp + n_env++), "HOME=%s", home ? home: "/root") < 0) ||
2670 (asprintf((char**)(envp + n_env++), "USER=%s", arg_user ? arg_user : "root") < 0) ||
2671 (asprintf((char**)(envp + n_env++), "LOGNAME=%s", arg_user ? arg_user : "root") < 0))
2672 return log_oom();
2673
2674 assert(!sd_id128_is_null(arg_uuid));
2675
2676 if (asprintf((char**)(envp + n_env++), "container_uuid=%s", id128_to_uuid_string(arg_uuid, as_uuid)) < 0)
2677 return log_oom();
2678
2679 if (fdset_size(fds) > 0) {
2680 r = fdset_cloexec(fds, false);
2681 if (r < 0)
2682 return log_error_errno(r, "Failed to unset O_CLOEXEC for file descriptors.");
2683
2684 if ((asprintf((char **)(envp + n_env++), "LISTEN_FDS=%u", fdset_size(fds)) < 0) ||
2685 (asprintf((char **)(envp + n_env++), "LISTEN_PID=1") < 0))
2686 return log_oom();
2687 }
2688 if (asprintf((char **)(envp + n_env++), "NOTIFY_SOCKET=%s", NSPAWN_NOTIFY_SOCKET_PATH) < 0)
2689 return log_oom();
2690
2691 env_use = strv_env_merge(2, envp, arg_setenv);
2692 if (!env_use)
2693 return log_oom();
2694
2695 /* Let the parent know that we are ready and
2696 * wait until the parent is ready with the
2697 * setup, too... */
2698 if (!barrier_place_and_sync(barrier)) /* #5 */
2699 return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
2700 "Parent died too early");
2701
2702 if (arg_chdir)
2703 if (chdir(arg_chdir) < 0)
2704 return log_error_errno(errno, "Failed to change to specified working directory %s: %m", arg_chdir);
2705
2706 if (arg_start_mode == START_PID2) {
2707 r = stub_pid1(arg_uuid);
2708 if (r < 0)
2709 return r;
2710 }
2711
2712 /* Now, explicitly close the log, so that we then can close all remaining fds. Closing the log explicitly first
2713 * has the benefit that the logging subsystem knows about it, and is thus ready to be reopened should we need
2714 * it again. Note that the other fds closed here are at least the locking and barrier fds. */
2715 log_close();
2716 log_set_open_when_needed(true);
2717
2718 (void) fdset_close_others(fds);
2719
2720 if (arg_start_mode == START_BOOT) {
2721 char **a;
2722 size_t m;
2723
2724 /* Automatically search for the init system */
2725
2726 m = strv_length(arg_parameters);
2727 a = newa(char*, m + 2);
2728 memcpy_safe(a + 1, arg_parameters, m * sizeof(char*));
2729 a[1 + m] = NULL;
2730
2731 a[0] = (char*) "/usr/lib/systemd/systemd";
2732 execve(a[0], a, env_use);
2733
2734 a[0] = (char*) "/lib/systemd/systemd";
2735 execve(a[0], a, env_use);
2736
2737 a[0] = (char*) "/sbin/init";
2738 execve(a[0], a, env_use);
2739
2740 exec_target = "/usr/lib/systemd/systemd, /lib/systemd/systemd, /sbin/init";
2741 } else if (!strv_isempty(arg_parameters)) {
2742 const char *dollar_path;
2743
2744 exec_target = arg_parameters[0];
2745
2746 /* Use the user supplied search $PATH if there is one, or DEFAULT_PATH_COMPAT if not to search the
2747 * binary. */
2748 dollar_path = strv_env_get(env_use, "PATH");
2749 if (dollar_path) {
2750 if (putenv((char*) dollar_path) != 0)
2751 return log_error_errno(errno, "Failed to update $PATH: %m");
2752 }
2753
2754 execvpe(arg_parameters[0], arg_parameters, env_use);
2755 } else {
2756 if (!arg_chdir)
2757 /* If we cannot change the directory, we'll end up in /, that is expected. */
2758 (void) chdir(home ?: "/root");
2759
2760 execle("/bin/bash", "-bash", NULL, env_use);
2761 execle("/bin/sh", "-sh", NULL, env_use);
2762
2763 exec_target = "/bin/bash, /bin/sh";
2764 }
2765
2766 return log_error_errno(errno, "execv(%s) failed: %m", exec_target);
2767 }
2768
2769 static int setup_sd_notify_child(void) {
2770 _cleanup_close_ int fd = -1;
2771 union sockaddr_union sa = {
2772 .un.sun_family = AF_UNIX,
2773 .un.sun_path = NSPAWN_NOTIFY_SOCKET_PATH,
2774 };
2775 int r;
2776
2777 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
2778 if (fd < 0)
2779 return log_error_errno(errno, "Failed to allocate notification socket: %m");
2780
2781 (void) mkdir_parents(NSPAWN_NOTIFY_SOCKET_PATH, 0755);
2782 (void) sockaddr_un_unlink(&sa.un);
2783
2784 r = bind(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un));
2785 if (r < 0)
2786 return log_error_errno(errno, "bind(" NSPAWN_NOTIFY_SOCKET_PATH ") failed: %m");
2787
2788 r = userns_lchown(NSPAWN_NOTIFY_SOCKET_PATH, 0, 0);
2789 if (r < 0)
2790 return log_error_errno(r, "Failed to chown " NSPAWN_NOTIFY_SOCKET_PATH ": %m");
2791
2792 r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true);
2793 if (r < 0)
2794 return log_error_errno(r, "SO_PASSCRED failed: %m");
2795
2796 return TAKE_FD(fd);
2797 }
2798
2799 static int outer_child(
2800 Barrier *barrier,
2801 const char *directory,
2802 const char *console,
2803 DissectedImage *dissected_image,
2804 bool interactive,
2805 bool secondary,
2806 int pid_socket,
2807 int uuid_socket,
2808 int notify_socket,
2809 int kmsg_socket,
2810 int rtnl_socket,
2811 int uid_shift_socket,
2812 int unified_cgroup_hierarchy_socket,
2813 FDSet *fds,
2814 int netns_fd) {
2815
2816 _cleanup_close_ int fd = -1;
2817 int r, which_failed;
2818 pid_t pid;
2819 ssize_t l;
2820
2821 /* This is the "outer" child process, i.e the one forked off by the container manager itself. It already has
2822 * its own CLONE_NEWNS namespace (which was created by the clone()). It still lives in the host's CLONE_NEWPID,
2823 * CLONE_NEWUTS, CLONE_NEWIPC, CLONE_NEWUSER and CLONE_NEWNET namespaces. After it completed a number of
2824 * initializations a second child (the "inner" one) is forked off it, and it exits. */
2825
2826 assert(barrier);
2827 assert(directory);
2828 assert(console);
2829 assert(pid_socket >= 0);
2830 assert(uuid_socket >= 0);
2831 assert(notify_socket >= 0);
2832 assert(kmsg_socket >= 0);
2833
2834 if (prctl(PR_SET_PDEATHSIG, SIGKILL) < 0)
2835 return log_error_errno(errno, "PR_SET_PDEATHSIG failed: %m");
2836
2837 if (interactive) {
2838 int terminal;
2839
2840 terminal = open_terminal(console, O_RDWR);
2841 if (terminal < 0)
2842 return log_error_errno(terminal, "Failed to open console: %m");
2843
2844 /* Make sure we can continue logging to the original stderr, even if stderr points elsewhere now */
2845 r = log_dup_console();
2846 if (r < 0)
2847 return log_error_errno(r, "Failed to duplicate stderr: %m");
2848
2849 r = rearrange_stdio(terminal, terminal, terminal); /* invalidates 'terminal' on success and failure */
2850 if (r < 0)
2851 return log_error_errno(r, "Failed to move console to stdin/stdout/stderr: %m");
2852 }
2853
2854 r = reset_audit_loginuid();
2855 if (r < 0)
2856 return r;
2857
2858 /* Mark everything as slave, so that we still
2859 * receive mounts from the real root, but don't
2860 * propagate mounts to the real root. */
2861 r = mount_verbose(LOG_ERR, NULL, "/", NULL, MS_SLAVE|MS_REC, NULL);
2862 if (r < 0)
2863 return r;
2864
2865 if (dissected_image) {
2866 /* If we are operating on a disk image, then mount its root directory now, but leave out the rest. We
2867 * can read the UID shift from it if we need to. Further down we'll mount the rest, but then with the
2868 * uid shift known. That way we can mount VFAT file systems shifted to the right place right away. This
2869 * makes sure ESP partitions and userns are compatible. */
2870
2871 r = dissected_image_mount(dissected_image, directory, arg_uid_shift,
2872 DISSECT_IMAGE_MOUNT_ROOT_ONLY|DISSECT_IMAGE_DISCARD_ON_LOOP|
2873 (arg_read_only ? DISSECT_IMAGE_READ_ONLY : 0)|
2874 (arg_start_mode == START_BOOT ? DISSECT_IMAGE_VALIDATE_OS : 0));
2875 if (r < 0)
2876 return r;
2877 }
2878
2879 r = determine_uid_shift(directory);
2880 if (r < 0)
2881 return r;
2882
2883 if (arg_userns_mode != USER_NAMESPACE_NO) {
2884 /* Let the parent know which UID shift we read from the image */
2885 l = send(uid_shift_socket, &arg_uid_shift, sizeof(arg_uid_shift), MSG_NOSIGNAL);
2886 if (l < 0)
2887 return log_error_errno(errno, "Failed to send UID shift: %m");
2888 if (l != sizeof(arg_uid_shift))
2889 return log_error_errno(SYNTHETIC_ERRNO(EIO),
2890 "Short write while sending UID shift.");
2891
2892 if (arg_userns_mode == USER_NAMESPACE_PICK) {
2893 /* When we are supposed to pick the UID shift, the parent will check now whether the UID shift
2894 * we just read from the image is available. If yes, it will send the UID shift back to us, if
2895 * not it will pick a different one, and send it back to us. */
2896
2897 l = recv(uid_shift_socket, &arg_uid_shift, sizeof(arg_uid_shift), 0);
2898 if (l < 0)
2899 return log_error_errno(errno, "Failed to recv UID shift: %m");
2900 if (l != sizeof(arg_uid_shift))
2901 return log_error_errno(SYNTHETIC_ERRNO(EIO),
2902 "Short read while receiving UID shift.");
2903 }
2904
2905 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO,
2906 "Selected user namespace base " UID_FMT " and range " UID_FMT ".", arg_uid_shift, arg_uid_range);
2907 }
2908
2909 if (!dissected_image) {
2910 /* Turn directory into bind mount */
2911 r = mount_verbose(LOG_ERR, directory, directory, NULL, MS_BIND|MS_REC, NULL);
2912 if (r < 0)
2913 return r;
2914 }
2915
2916 r = setup_pivot_root(
2917 directory,
2918 arg_pivot_root_new,
2919 arg_pivot_root_old);
2920 if (r < 0)
2921 return r;
2922
2923 r = setup_volatile_mode(
2924 directory,
2925 arg_volatile_mode,
2926 arg_userns_mode != USER_NAMESPACE_NO,
2927 arg_uid_shift,
2928 arg_uid_range,
2929 arg_selinux_context);
2930 if (r < 0)
2931 return r;
2932
2933 if (dissected_image) {
2934 /* Now we know the uid shift, let's now mount everything else that might be in the image. */
2935 r = dissected_image_mount(dissected_image, directory, arg_uid_shift,
2936 DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY|DISSECT_IMAGE_DISCARD_ON_LOOP|(arg_read_only ? DISSECT_IMAGE_READ_ONLY : 0));
2937 if (r < 0)
2938 return r;
2939 }
2940
2941 if (arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_UNKNOWN) {
2942 /* OK, we don't know yet which cgroup mode to use yet. Let's figure it out, and tell the parent. */
2943
2944 r = detect_unified_cgroup_hierarchy_from_image(directory);
2945 if (r < 0)
2946 return r;
2947
2948 l = send(unified_cgroup_hierarchy_socket, &arg_unified_cgroup_hierarchy, sizeof(arg_unified_cgroup_hierarchy), MSG_NOSIGNAL);
2949 if (l < 0)
2950 return log_error_errno(errno, "Failed to send cgroup mode: %m");
2951 if (l != sizeof(arg_unified_cgroup_hierarchy))
2952 return log_error_errno(SYNTHETIC_ERRNO(EIO),
2953 "Short write while sending cgroup mode.");
2954
2955 unified_cgroup_hierarchy_socket = safe_close(unified_cgroup_hierarchy_socket);
2956 }
2957
2958 /* Mark everything as shared so our mounts get propagated down. This is
2959 * required to make new bind mounts available in systemd services
2960 * inside the containter that create a new mount namespace.
2961 * See https://github.com/systemd/systemd/issues/3860
2962 * Further submounts (such as /dev) done after this will inherit the
2963 * shared propagation mode. */
2964 r = mount_verbose(LOG_ERR, NULL, directory, NULL, MS_SHARED|MS_REC, NULL);
2965 if (r < 0)
2966 return r;
2967
2968 r = recursive_chown(directory, arg_uid_shift, arg_uid_range);
2969 if (r < 0)
2970 return r;
2971
2972 r = base_filesystem_create(directory, arg_uid_shift, (gid_t) arg_uid_shift);
2973 if (r < 0)
2974 return r;
2975
2976 if (arg_read_only && arg_volatile_mode == VOLATILE_NO) {
2977 r = bind_remount_recursive(directory, true, NULL);
2978 if (r < 0)
2979 return log_error_errno(r, "Failed to make tree read-only: %m");
2980 }
2981
2982 r = mount_all(directory,
2983 arg_mount_settings,
2984 arg_uid_shift,
2985 arg_selinux_apifs_context);
2986 if (r < 0)
2987 return r;
2988
2989 r = copy_devnodes(directory);
2990 if (r < 0)
2991 return r;
2992
2993 dev_setup(directory, arg_uid_shift, arg_uid_shift);
2994
2995 r = setup_pts(directory);
2996 if (r < 0)
2997 return r;
2998
2999 r = setup_propagate(directory);
3000 if (r < 0)
3001 return r;
3002
3003 r = setup_dev_console(directory, console);
3004 if (r < 0)
3005 return r;
3006
3007 r = setup_keyring();
3008 if (r < 0)
3009 return r;
3010
3011 r = setup_seccomp(arg_caps_retain, arg_syscall_whitelist, arg_syscall_blacklist);
3012 if (r < 0)
3013 return r;
3014
3015 r = setup_timezone(directory);
3016 if (r < 0)
3017 return r;
3018
3019 r = setup_resolv_conf(directory);
3020 if (r < 0)
3021 return r;
3022
3023 r = setup_machine_id(directory);
3024 if (r < 0)
3025 return r;
3026
3027 r = setup_journal(directory);
3028 if (r < 0)
3029 return r;
3030
3031 r = mount_custom(
3032 directory,
3033 arg_custom_mounts,
3034 arg_n_custom_mounts,
3035 arg_userns_mode != USER_NAMESPACE_NO,
3036 arg_uid_shift,
3037 arg_uid_range,
3038 arg_selinux_apifs_context);
3039 if (r < 0)
3040 return r;
3041
3042 if (!arg_use_cgns) {
3043 r = mount_cgroups(
3044 directory,
3045 arg_unified_cgroup_hierarchy,
3046 arg_userns_mode != USER_NAMESPACE_NO,
3047 arg_uid_shift,
3048 arg_uid_range,
3049 arg_selinux_apifs_context,
3050 false);
3051 if (r < 0)
3052 return r;
3053 }
3054
3055 r = mount_move_root(directory);
3056 if (r < 0)
3057 return log_error_errno(r, "Failed to move root directory: %m");
3058
3059 fd = setup_sd_notify_child();
3060 if (fd < 0)
3061 return fd;
3062
3063 r = setrlimit_closest_all((const struct rlimit *const*) arg_rlimit, &which_failed);
3064 if (r < 0)
3065 return log_error_errno(r, "Failed to apply resource limit RLIMIT_%s: %m", rlimit_to_string(which_failed));
3066
3067 pid = raw_clone(SIGCHLD|CLONE_NEWNS|
3068 arg_clone_ns_flags |
3069 (arg_userns_mode != USER_NAMESPACE_NO ? CLONE_NEWUSER : 0));
3070 if (pid < 0)
3071 return log_error_errno(errno, "Failed to fork inner child: %m");
3072 if (pid == 0) {
3073 pid_socket = safe_close(pid_socket);
3074 uuid_socket = safe_close(uuid_socket);
3075 notify_socket = safe_close(notify_socket);
3076 uid_shift_socket = safe_close(uid_shift_socket);
3077
3078 /* The inner child has all namespaces that are
3079 * requested, so that we all are owned by the user if
3080 * user namespaces are turned on. */
3081
3082 if (arg_network_namespace_path) {
3083 r = namespace_enter(-1, -1, netns_fd, -1, -1);
3084 if (r < 0)
3085 return log_error_errno(r, "Failed to join network namespace: %m");
3086 }
3087
3088 r = inner_child(barrier, directory, secondary, kmsg_socket, rtnl_socket, fds);
3089 if (r < 0)
3090 _exit(EXIT_FAILURE);
3091
3092 _exit(EXIT_SUCCESS);
3093 }
3094
3095 l = send(pid_socket, &pid, sizeof(pid), MSG_NOSIGNAL);
3096 if (l < 0)
3097 return log_error_errno(errno, "Failed to send PID: %m");
3098 if (l != sizeof(pid))
3099 return log_error_errno(SYNTHETIC_ERRNO(EIO),
3100 "Short write while sending PID.");
3101
3102 l = send(uuid_socket, &arg_uuid, sizeof(arg_uuid), MSG_NOSIGNAL);
3103 if (l < 0)
3104 return log_error_errno(errno, "Failed to send machine ID: %m");
3105 if (l != sizeof(arg_uuid))
3106 return log_error_errno(SYNTHETIC_ERRNO(EIO),
3107 "Short write while sending machine ID.");
3108
3109 l = send_one_fd(notify_socket, fd, 0);
3110 if (l < 0)
3111 return log_error_errno(errno, "Failed to send notify fd: %m");
3112
3113 pid_socket = safe_close(pid_socket);
3114 uuid_socket = safe_close(uuid_socket);
3115 notify_socket = safe_close(notify_socket);
3116 kmsg_socket = safe_close(kmsg_socket);
3117 rtnl_socket = safe_close(rtnl_socket);
3118 netns_fd = safe_close(netns_fd);
3119
3120 return 0;
3121 }
3122
3123 static int uid_shift_pick(uid_t *shift, LockFile *ret_lock_file) {
3124 bool tried_hashed = false;
3125 unsigned n_tries = 100;
3126 uid_t candidate;
3127 int r;
3128
3129 assert(shift);
3130 assert(ret_lock_file);
3131 assert(arg_userns_mode == USER_NAMESPACE_PICK);
3132 assert(arg_uid_range == 0x10000U);
3133
3134 candidate = *shift;
3135
3136 (void) mkdir("/run/systemd/nspawn-uid", 0755);
3137
3138 for (;;) {
3139 char lock_path[STRLEN("/run/systemd/nspawn-uid/") + DECIMAL_STR_MAX(uid_t) + 1];
3140 _cleanup_(release_lock_file) LockFile lf = LOCK_FILE_INIT;
3141
3142 if (--n_tries <= 0)
3143 return -EBUSY;
3144
3145 if (candidate < CONTAINER_UID_BASE_MIN || candidate > CONTAINER_UID_BASE_MAX)
3146 goto next;
3147 if ((candidate & UINT32_C(0xFFFF)) != 0)
3148 goto next;
3149
3150 xsprintf(lock_path, "/run/systemd/nspawn-uid/" UID_FMT, candidate);
3151 r = make_lock_file(lock_path, LOCK_EX|LOCK_NB, &lf);
3152 if (r == -EBUSY) /* Range already taken by another nspawn instance */
3153 goto next;
3154 if (r < 0)
3155 return r;
3156
3157 /* Make some superficial checks whether the range is currently known in the user database */
3158 if (getpwuid(candidate))
3159 goto next;
3160 if (getpwuid(candidate + UINT32_C(0xFFFE)))
3161 goto next;
3162 if (getgrgid(candidate))
3163 goto next;
3164 if (getgrgid(candidate + UINT32_C(0xFFFE)))
3165 goto next;
3166
3167 *ret_lock_file = lf;
3168 lf = (struct LockFile) LOCK_FILE_INIT;
3169 *shift = candidate;
3170 return 0;
3171
3172 next:
3173 if (arg_machine && !tried_hashed) {
3174 /* Try to hash the base from the container name */
3175
3176 static const uint8_t hash_key[] = {
3177 0xe1, 0x56, 0xe0, 0xf0, 0x4a, 0xf0, 0x41, 0xaf,
3178 0x96, 0x41, 0xcf, 0x41, 0x33, 0x94, 0xff, 0x72
3179 };
3180
3181 candidate = (uid_t) siphash24(arg_machine, strlen(arg_machine), hash_key);
3182
3183 tried_hashed = true;
3184 } else
3185 random_bytes(&candidate, sizeof(candidate));
3186
3187 candidate = (candidate % (CONTAINER_UID_BASE_MAX - CONTAINER_UID_BASE_MIN)) + CONTAINER_UID_BASE_MIN;
3188 candidate &= (uid_t) UINT32_C(0xFFFF0000);
3189 }
3190 }
3191
3192 static int setup_uid_map(pid_t pid) {
3193 char uid_map[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(uid_t) + 1], line[DECIMAL_STR_MAX(uid_t)*3+3+1];
3194 int r;
3195
3196 assert(pid > 1);
3197
3198 xsprintf(uid_map, "/proc/" PID_FMT "/uid_map", pid);
3199 xsprintf(line, UID_FMT " " UID_FMT " " UID_FMT "\n", 0, arg_uid_shift, arg_uid_range);
3200 r = write_string_file(uid_map, line, WRITE_STRING_FILE_DISABLE_BUFFER);
3201 if (r < 0)
3202 return log_error_errno(r, "Failed to write UID map: %m");
3203
3204 /* We always assign the same UID and GID ranges */
3205 xsprintf(uid_map, "/proc/" PID_FMT "/gid_map", pid);
3206 r = write_string_file(uid_map, line, WRITE_STRING_FILE_DISABLE_BUFFER);
3207 if (r < 0)
3208 return log_error_errno(r, "Failed to write GID map: %m");
3209
3210 return 0;
3211 }
3212
3213 static int nspawn_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
3214 char buf[NOTIFY_BUFFER_MAX+1];
3215 char *p = NULL;
3216 struct iovec iovec = {
3217 .iov_base = buf,
3218 .iov_len = sizeof(buf)-1,
3219 };
3220 union {
3221 struct cmsghdr cmsghdr;
3222 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
3223 CMSG_SPACE(sizeof(int) * NOTIFY_FD_MAX)];
3224 } control = {};
3225 struct msghdr msghdr = {
3226 .msg_iov = &iovec,
3227 .msg_iovlen = 1,
3228 .msg_control = &control,
3229 .msg_controllen = sizeof(control),
3230 };
3231 struct cmsghdr *cmsg;
3232 struct ucred *ucred = NULL;
3233 ssize_t n;
3234 pid_t inner_child_pid;
3235 _cleanup_strv_free_ char **tags = NULL;
3236
3237 assert(userdata);
3238
3239 inner_child_pid = PTR_TO_PID(userdata);
3240
3241 if (revents != EPOLLIN) {
3242 log_warning("Got unexpected poll event for notify fd.");
3243 return 0;
3244 }
3245
3246 n = recvmsg(fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
3247 if (n < 0) {
3248 if (IN_SET(errno, EAGAIN, EINTR))
3249 return 0;
3250
3251 return log_warning_errno(errno, "Couldn't read notification socket: %m");
3252 }
3253 cmsg_close_all(&msghdr);
3254
3255 CMSG_FOREACH(cmsg, &msghdr) {
3256 if (cmsg->cmsg_level == SOL_SOCKET &&
3257 cmsg->cmsg_type == SCM_CREDENTIALS &&
3258 cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
3259
3260 ucred = (struct ucred*) CMSG_DATA(cmsg);
3261 }
3262 }
3263
3264 if (!ucred || ucred->pid != inner_child_pid) {
3265 log_debug("Received notify message without valid credentials. Ignoring.");
3266 return 0;
3267 }
3268
3269 if ((size_t) n >= sizeof(buf)) {
3270 log_warning("Received notify message exceeded maximum size. Ignoring.");
3271 return 0;
3272 }
3273
3274 buf[n] = 0;
3275 tags = strv_split(buf, "\n\r");
3276 if (!tags)
3277 return log_oom();
3278
3279 if (strv_find(tags, "READY=1"))
3280 sd_notifyf(false, "READY=1\n");
3281
3282 p = strv_find_startswith(tags, "STATUS=");
3283 if (p)
3284 sd_notifyf(false, "STATUS=Container running: %s", p);
3285
3286 return 0;
3287 }
3288
3289 static int setup_sd_notify_parent(sd_event *event, int fd, pid_t *inner_child_pid, sd_event_source **notify_event_source) {
3290 int r;
3291
3292 r = sd_event_add_io(event, notify_event_source, fd, EPOLLIN, nspawn_dispatch_notify_fd, inner_child_pid);
3293 if (r < 0)
3294 return log_error_errno(r, "Failed to allocate notify event source: %m");
3295
3296 (void) sd_event_source_set_description(*notify_event_source, "nspawn-notify");
3297
3298 return 0;
3299 }
3300
3301 static int merge_settings(Settings *settings, const char *path) {
3302 int rl;
3303
3304 assert(settings);
3305 assert(path);
3306
3307 /* Copy over bits from the settings, unless they have been explicitly masked by command line switches. Note
3308 * that this steals the fields of the Settings* structure, and hence modifies it. */
3309
3310 if ((arg_settings_mask & SETTING_START_MODE) == 0 &&
3311 settings->start_mode >= 0) {
3312 arg_start_mode = settings->start_mode;
3313 strv_free_and_replace(arg_parameters, settings->parameters);
3314 }
3315
3316 if ((arg_settings_mask & SETTING_EPHEMERAL) == 0)
3317 arg_ephemeral = settings->ephemeral;
3318
3319 if ((arg_settings_mask & SETTING_PIVOT_ROOT) == 0 &&
3320 settings->pivot_root_new) {
3321 free_and_replace(arg_pivot_root_new, settings->pivot_root_new);
3322 free_and_replace(arg_pivot_root_old, settings->pivot_root_old);
3323 }
3324
3325 if ((arg_settings_mask & SETTING_WORKING_DIRECTORY) == 0 &&
3326 settings->working_directory)
3327 free_and_replace(arg_chdir, settings->working_directory);
3328
3329 if ((arg_settings_mask & SETTING_ENVIRONMENT) == 0 &&
3330 settings->environment)
3331 strv_free_and_replace(arg_setenv, settings->environment);
3332
3333 if ((arg_settings_mask & SETTING_USER) == 0 &&
3334 settings->user)
3335 free_and_replace(arg_user, settings->user);
3336
3337 if ((arg_settings_mask & SETTING_CAPABILITY) == 0) {
3338 uint64_t plus;
3339
3340 plus = settings->capability;
3341 if (settings_private_network(settings))
3342 plus |= (1ULL << CAP_NET_ADMIN);
3343
3344 if (!arg_settings_trusted && plus != 0) {
3345 if (settings->capability != 0)
3346 log_warning("Ignoring Capability= setting, file %s is not trusted.", path);
3347 } else
3348 arg_caps_retain |= plus;
3349
3350 arg_caps_retain &= ~settings->drop_capability;
3351 }
3352
3353 if ((arg_settings_mask & SETTING_KILL_SIGNAL) == 0 &&
3354 settings->kill_signal > 0)
3355 arg_kill_signal = settings->kill_signal;
3356
3357 if ((arg_settings_mask & SETTING_PERSONALITY) == 0 &&
3358 settings->personality != PERSONALITY_INVALID)
3359 arg_personality = settings->personality;
3360
3361 if ((arg_settings_mask & SETTING_MACHINE_ID) == 0 &&
3362 !sd_id128_is_null(settings->machine_id)) {
3363
3364 if (!arg_settings_trusted)
3365 log_warning("Ignoring MachineID= setting, file %s is not trusted.", path);
3366 else
3367 arg_uuid = settings->machine_id;
3368 }
3369
3370 if ((arg_settings_mask & SETTING_READ_ONLY) == 0 &&
3371 settings->read_only >= 0)
3372 arg_read_only = settings->read_only;
3373
3374 if ((arg_settings_mask & SETTING_VOLATILE_MODE) == 0 &&
3375 settings->volatile_mode != _VOLATILE_MODE_INVALID)
3376 arg_volatile_mode = settings->volatile_mode;
3377
3378 if ((arg_settings_mask & SETTING_CUSTOM_MOUNTS) == 0 &&
3379 settings->n_custom_mounts > 0) {
3380
3381 if (!arg_settings_trusted)
3382 log_warning("Ignoring TemporaryFileSystem=, Bind= and BindReadOnly= settings, file %s is not trusted.", path);
3383 else {
3384 custom_mount_free_all(arg_custom_mounts, arg_n_custom_mounts);
3385 arg_custom_mounts = TAKE_PTR(settings->custom_mounts);
3386 arg_n_custom_mounts = settings->n_custom_mounts;
3387 settings->n_custom_mounts = 0;
3388 }
3389 }
3390
3391 if ((arg_settings_mask & SETTING_NETWORK) == 0 &&
3392 (settings->private_network >= 0 ||
3393 settings->network_veth >= 0 ||
3394 settings->network_bridge ||
3395 settings->network_zone ||
3396 settings->network_interfaces ||
3397 settings->network_macvlan ||
3398 settings->network_ipvlan ||
3399 settings->network_veth_extra)) {
3400
3401 if (!arg_settings_trusted)
3402 log_warning("Ignoring network settings, file %s is not trusted.", path);
3403 else {
3404 arg_network_veth = settings_network_veth(settings);
3405 arg_private_network = settings_private_network(settings);
3406
3407 strv_free_and_replace(arg_network_interfaces, settings->network_interfaces);
3408 strv_free_and_replace(arg_network_macvlan, settings->network_macvlan);
3409 strv_free_and_replace(arg_network_ipvlan, settings->network_ipvlan);
3410 strv_free_and_replace(arg_network_veth_extra, settings->network_veth_extra);
3411
3412 free_and_replace(arg_network_bridge, settings->network_bridge);
3413 free_and_replace(arg_network_zone, settings->network_zone);
3414 }
3415 }
3416
3417 if ((arg_settings_mask & SETTING_EXPOSE_PORTS) == 0 &&
3418 settings->expose_ports) {
3419
3420 if (!arg_settings_trusted)
3421 log_warning("Ignoring Port= setting, file %s is not trusted.", path);
3422 else {
3423 expose_port_free_all(arg_expose_ports);
3424 arg_expose_ports = TAKE_PTR(settings->expose_ports);
3425 }
3426 }
3427
3428 if ((arg_settings_mask & SETTING_USERNS) == 0 &&
3429 settings->userns_mode != _USER_NAMESPACE_MODE_INVALID) {
3430
3431 if (!arg_settings_trusted)
3432 log_warning("Ignoring PrivateUsers= and PrivateUsersChown= settings, file %s is not trusted.", path);
3433 else {
3434 arg_userns_mode = settings->userns_mode;
3435 arg_uid_shift = settings->uid_shift;
3436 arg_uid_range = settings->uid_range;
3437 arg_userns_chown = settings->userns_chown;
3438 }
3439 }
3440
3441 if ((arg_settings_mask & SETTING_NOTIFY_READY) == 0)
3442 arg_notify_ready = settings->notify_ready;
3443
3444 if ((arg_settings_mask & SETTING_SYSCALL_FILTER) == 0) {
3445
3446 if (!arg_settings_trusted && !strv_isempty(arg_syscall_whitelist))
3447 log_warning("Ignoring SystemCallFilter= settings, file %s is not trusted.", path);
3448 else {
3449 strv_free_and_replace(arg_syscall_whitelist, settings->syscall_whitelist);
3450 strv_free_and_replace(arg_syscall_blacklist, settings->syscall_blacklist);
3451 }
3452 }
3453
3454 for (rl = 0; rl < _RLIMIT_MAX; rl ++) {
3455 if ((arg_settings_mask & (SETTING_RLIMIT_FIRST << rl)))
3456 continue;
3457
3458 if (!settings->rlimit[rl])
3459 continue;
3460
3461 if (!arg_settings_trusted) {
3462 log_warning("Ignoring Limit%s= setting, file '%s' is not trusted.", rlimit_to_string(rl), path);
3463 continue;
3464 }
3465
3466 free_and_replace(arg_rlimit[rl], settings->rlimit[rl]);
3467 }
3468
3469 if ((arg_settings_mask & SETTING_HOSTNAME) == 0 &&
3470 settings->hostname)
3471 free_and_replace(arg_hostname, settings->hostname);
3472
3473 if ((arg_settings_mask & SETTING_NO_NEW_PRIVILEGES) == 0 &&
3474 settings->no_new_privileges >= 0)
3475 arg_no_new_privileges = settings->no_new_privileges;
3476
3477 if ((arg_settings_mask & SETTING_OOM_SCORE_ADJUST) == 0 &&
3478 settings->oom_score_adjust_set) {
3479
3480 if (!arg_settings_trusted)
3481 log_warning("Ignoring OOMScoreAdjust= setting, file '%s' is not trusted.", path);
3482 else {
3483 arg_oom_score_adjust = settings->oom_score_adjust;
3484 arg_oom_score_adjust_set = true;
3485 }
3486 }
3487
3488 if ((arg_settings_mask & SETTING_CPU_AFFINITY) == 0 &&
3489 settings->cpuset) {
3490
3491 if (!arg_settings_trusted)
3492 log_warning("Ignoring CPUAffinity= setting, file '%s' is not trusted.", path);
3493 else {
3494 if (arg_cpuset)
3495 CPU_FREE(arg_cpuset);
3496 arg_cpuset = TAKE_PTR(settings->cpuset);
3497 arg_cpuset_ncpus = settings->cpuset_ncpus;
3498 }
3499 }
3500
3501 if ((arg_settings_mask & SETTING_RESOLV_CONF) == 0 &&
3502 settings->resolv_conf != _RESOLV_CONF_MODE_INVALID)
3503 arg_resolv_conf = settings->resolv_conf;
3504
3505 if ((arg_settings_mask & SETTING_LINK_JOURNAL) == 0 &&
3506 settings->link_journal != _LINK_JOURNAL_INVALID) {
3507
3508 if (!arg_settings_trusted)
3509 log_warning("Ignoring journal link setting, file '%s' is not trusted.", path);
3510 else {
3511 arg_link_journal = settings->link_journal;
3512 arg_link_journal_try = settings->link_journal_try;
3513 }
3514 }
3515
3516 if ((arg_settings_mask & SETTING_TIMEZONE) == 0 &&
3517 settings->timezone != _TIMEZONE_MODE_INVALID)
3518 arg_timezone = settings->timezone;
3519
3520 return 0;
3521 }
3522
3523 static int load_settings(void) {
3524 _cleanup_(settings_freep) Settings *settings = NULL;
3525 _cleanup_fclose_ FILE *f = NULL;
3526 _cleanup_free_ char *p = NULL;
3527 const char *fn, *i;
3528 int r;
3529
3530 /* If all settings are masked, there's no point in looking for
3531 * the settings file */
3532 if ((arg_settings_mask & _SETTINGS_MASK_ALL) == _SETTINGS_MASK_ALL)
3533 return 0;
3534
3535 fn = strjoina(arg_machine, ".nspawn");
3536
3537 /* We first look in the admin's directories in /etc and /run */
3538 FOREACH_STRING(i, "/etc/systemd/nspawn", "/run/systemd/nspawn") {
3539 _cleanup_free_ char *j = NULL;
3540
3541 j = strjoin(i, "/", fn);
3542 if (!j)
3543 return log_oom();
3544
3545 f = fopen(j, "re");
3546 if (f) {
3547 p = TAKE_PTR(j);
3548
3549 /* By default, we trust configuration from /etc and /run */
3550 if (arg_settings_trusted < 0)
3551 arg_settings_trusted = true;
3552
3553 break;
3554 }
3555
3556 if (errno != ENOENT)
3557 return log_error_errno(errno, "Failed to open %s: %m", j);
3558 }
3559
3560 if (!f) {
3561 /* After that, let's look for a file next to the
3562 * actual image we shall boot. */
3563
3564 if (arg_image) {
3565 p = file_in_same_dir(arg_image, fn);
3566 if (!p)
3567 return log_oom();
3568 } else if (arg_directory) {
3569 p = file_in_same_dir(arg_directory, fn);
3570 if (!p)
3571 return log_oom();
3572 }
3573
3574 if (p) {
3575 f = fopen(p, "re");
3576 if (!f && errno != ENOENT)
3577 return log_error_errno(errno, "Failed to open %s: %m", p);
3578
3579 /* By default, we do not trust configuration from /var/lib/machines */
3580 if (arg_settings_trusted < 0)
3581 arg_settings_trusted = false;
3582 }
3583 }
3584
3585 if (!f)
3586 return 0;
3587
3588 log_debug("Settings are trusted: %s", yes_no(arg_settings_trusted));
3589
3590 r = settings_load(f, p, &settings);
3591 if (r < 0)
3592 return r;
3593
3594 return merge_settings(settings, p);
3595 }
3596
3597 static int run(int master,
3598 const char* console,
3599 DissectedImage *dissected_image,
3600 bool interactive,
3601 bool secondary,
3602 FDSet *fds,
3603 char veth_name[IFNAMSIZ], bool *veth_created,
3604 union in_addr_union *exposed,
3605 pid_t *pid, int *ret) {
3606
3607 static const struct sigaction sa = {
3608 .sa_handler = nop_signal_handler,
3609 .sa_flags = SA_NOCLDSTOP|SA_RESTART,
3610 };
3611
3612 _cleanup_(release_lock_file) LockFile uid_shift_lock = LOCK_FILE_INIT;
3613 _cleanup_close_ int etc_passwd_lock = -1;
3614 _cleanup_close_pair_ int
3615 kmsg_socket_pair[2] = { -1, -1 },
3616 rtnl_socket_pair[2] = { -1, -1 },
3617 pid_socket_pair[2] = { -1, -1 },
3618 uuid_socket_pair[2] = { -1, -1 },
3619 notify_socket_pair[2] = { -1, -1 },
3620 uid_shift_socket_pair[2] = { -1, -1 },
3621 unified_cgroup_hierarchy_socket_pair[2] = { -1, -1};
3622
3623 _cleanup_close_ int notify_socket= -1;
3624 _cleanup_(barrier_destroy) Barrier barrier = BARRIER_NULL;
3625 _cleanup_(sd_event_source_unrefp) sd_event_source *notify_event_source = NULL;
3626 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
3627 _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
3628 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
3629 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
3630 ContainerStatus container_status = 0;
3631 char last_char = 0;
3632 int ifi = 0, r;
3633 ssize_t l;
3634 sigset_t mask_chld;
3635 _cleanup_close_ int netns_fd = -1;
3636
3637 assert_se(sigemptyset(&mask_chld) == 0);
3638 assert_se(sigaddset(&mask_chld, SIGCHLD) == 0);
3639
3640 if (arg_userns_mode == USER_NAMESPACE_PICK) {
3641 /* When we shall pick the UID/GID range, let's first lock /etc/passwd, so that we can safely
3642 * check with getpwuid() if the specific user already exists. Note that /etc might be
3643 * read-only, in which case this will fail with EROFS. But that's really OK, as in that case we
3644 * can be reasonably sure that no users are going to be added. Note that getpwuid() checks are
3645 * really just an extra safety net. We kinda assume that the UID range we allocate from is
3646 * really ours. */
3647
3648 etc_passwd_lock = take_etc_passwd_lock(NULL);
3649 if (etc_passwd_lock < 0 && etc_passwd_lock != -EROFS)
3650 return log_error_errno(etc_passwd_lock, "Failed to take /etc/passwd lock: %m");
3651 }
3652
3653 r = barrier_create(&barrier);
3654 if (r < 0)
3655 return log_error_errno(r, "Cannot initialize IPC barrier: %m");
3656
3657 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, kmsg_socket_pair) < 0)
3658 return log_error_errno(errno, "Failed to create kmsg socket pair: %m");
3659
3660 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, rtnl_socket_pair) < 0)
3661 return log_error_errno(errno, "Failed to create rtnl socket pair: %m");
3662
3663 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pid_socket_pair) < 0)
3664 return log_error_errno(errno, "Failed to create pid socket pair: %m");
3665
3666 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, uuid_socket_pair) < 0)
3667 return log_error_errno(errno, "Failed to create id socket pair: %m");
3668
3669 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, notify_socket_pair) < 0)
3670 return log_error_errno(errno, "Failed to create notify socket pair: %m");
3671
3672 if (arg_userns_mode != USER_NAMESPACE_NO)
3673 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, uid_shift_socket_pair) < 0)
3674 return log_error_errno(errno, "Failed to create uid shift socket pair: %m");
3675
3676 if (arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_UNKNOWN)
3677 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, unified_cgroup_hierarchy_socket_pair) < 0)
3678 return log_error_errno(errno, "Failed to create unified cgroup socket pair: %m");
3679
3680 /* Child can be killed before execv(), so handle SIGCHLD in order to interrupt
3681 * parent's blocking calls and give it a chance to call wait() and terminate. */
3682 r = sigprocmask(SIG_UNBLOCK, &mask_chld, NULL);
3683 if (r < 0)
3684 return log_error_errno(errno, "Failed to change the signal mask: %m");
3685
3686 r = sigaction(SIGCHLD, &sa, NULL);
3687 if (r < 0)
3688 return log_error_errno(errno, "Failed to install SIGCHLD handler: %m");
3689
3690 if (arg_network_namespace_path) {
3691 netns_fd = open(arg_network_namespace_path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
3692 if (netns_fd < 0)
3693 return log_error_errno(errno, "Cannot open file %s: %m", arg_network_namespace_path);
3694
3695 r = fd_is_network_ns(netns_fd);
3696 if (r == -EUCLEAN)
3697 log_debug_errno(r, "Cannot determine if passed network namespace path '%s' really refers to a network namespace, assuming it does.", arg_network_namespace_path);
3698 else if (r < 0)
3699 return log_error_errno(r, "Failed to check %s fs type: %m", arg_network_namespace_path);
3700 else if (r == 0) {
3701 log_error("Path %s doesn't refer to a network namespace, refusing.", arg_network_namespace_path);
3702 return -EINVAL;
3703 }
3704 }
3705
3706 *pid = raw_clone(SIGCHLD|CLONE_NEWNS);
3707 if (*pid < 0)
3708 return log_error_errno(errno, "clone() failed%s: %m",
3709 errno == EINVAL ?
3710 ", do you have namespace support enabled in your kernel? (You need UTS, IPC, PID and NET namespacing built in)" : "");
3711
3712 if (*pid == 0) {
3713 /* The outer child only has a file system namespace. */
3714 barrier_set_role(&barrier, BARRIER_CHILD);
3715
3716 master = safe_close(master);
3717
3718 kmsg_socket_pair[0] = safe_close(kmsg_socket_pair[0]);
3719 rtnl_socket_pair[0] = safe_close(rtnl_socket_pair[0]);
3720 pid_socket_pair[0] = safe_close(pid_socket_pair[0]);
3721 uuid_socket_pair[0] = safe_close(uuid_socket_pair[0]);
3722 notify_socket_pair[0] = safe_close(notify_socket_pair[0]);
3723 uid_shift_socket_pair[0] = safe_close(uid_shift_socket_pair[0]);
3724 unified_cgroup_hierarchy_socket_pair[0] = safe_close(unified_cgroup_hierarchy_socket_pair[0]);
3725
3726 (void) reset_all_signal_handlers();
3727 (void) reset_signal_mask();
3728
3729 r = outer_child(&barrier,
3730 arg_directory,
3731 console,
3732 dissected_image,
3733 interactive,
3734 secondary,
3735 pid_socket_pair[1],
3736 uuid_socket_pair[1],
3737 notify_socket_pair[1],
3738 kmsg_socket_pair[1],
3739 rtnl_socket_pair[1],
3740 uid_shift_socket_pair[1],
3741 unified_cgroup_hierarchy_socket_pair[1],
3742 fds,
3743 netns_fd);
3744 if (r < 0)
3745 _exit(EXIT_FAILURE);
3746
3747 _exit(EXIT_SUCCESS);
3748 }
3749
3750 barrier_set_role(&barrier, BARRIER_PARENT);
3751
3752 fds = fdset_free(fds);
3753
3754 kmsg_socket_pair[1] = safe_close(kmsg_socket_pair[1]);
3755 rtnl_socket_pair[1] = safe_close(rtnl_socket_pair[1]);
3756 pid_socket_pair[1] = safe_close(pid_socket_pair[1]);
3757 uuid_socket_pair[1] = safe_close(uuid_socket_pair[1]);
3758 notify_socket_pair[1] = safe_close(notify_socket_pair[1]);
3759 uid_shift_socket_pair[1] = safe_close(uid_shift_socket_pair[1]);
3760 unified_cgroup_hierarchy_socket_pair[1] = safe_close(unified_cgroup_hierarchy_socket_pair[1]);
3761
3762 if (arg_userns_mode != USER_NAMESPACE_NO) {
3763 /* The child just let us know the UID shift it might have read from the image. */
3764 l = recv(uid_shift_socket_pair[0], &arg_uid_shift, sizeof arg_uid_shift, 0);
3765 if (l < 0)
3766 return log_error_errno(errno, "Failed to read UID shift: %m");
3767 if (l != sizeof arg_uid_shift) {
3768 log_error("Short read while reading UID shift.");
3769 return -EIO;
3770 }
3771
3772 if (arg_userns_mode == USER_NAMESPACE_PICK) {
3773 /* If we are supposed to pick the UID shift, let's try to use the shift read from the
3774 * image, but if that's already in use, pick a new one, and report back to the child,
3775 * which one we now picked. */
3776
3777 r = uid_shift_pick(&arg_uid_shift, &uid_shift_lock);
3778 if (r < 0)
3779 return log_error_errno(r, "Failed to pick suitable UID/GID range: %m");
3780
3781 l = send(uid_shift_socket_pair[0], &arg_uid_shift, sizeof arg_uid_shift, MSG_NOSIGNAL);
3782 if (l < 0)
3783 return log_error_errno(errno, "Failed to send UID shift: %m");
3784 if (l != sizeof arg_uid_shift) {
3785 log_error("Short write while writing UID shift.");
3786 return -EIO;
3787 }
3788 }
3789 }
3790
3791 if (arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_UNKNOWN) {
3792 /* The child let us know the support cgroup mode it might have read from the image. */
3793 l = recv(unified_cgroup_hierarchy_socket_pair[0], &arg_unified_cgroup_hierarchy, sizeof(arg_unified_cgroup_hierarchy), 0);
3794 if (l < 0)
3795 return log_error_errno(errno, "Failed to read cgroup mode: %m");
3796 if (l != sizeof(arg_unified_cgroup_hierarchy)) {
3797 log_error("Short read while reading cgroup mode (%zu bytes).%s",
3798 l, l == 0 ? " The child is most likely dead." : "");
3799 return -EIO;
3800 }
3801 }
3802
3803 /* Wait for the outer child. */
3804 r = wait_for_terminate_and_check("(sd-namespace)", *pid, WAIT_LOG_ABNORMAL);
3805 if (r < 0)
3806 return r;
3807 if (r != EXIT_SUCCESS)
3808 return -EIO;
3809
3810 /* And now retrieve the PID of the inner child. */
3811 l = recv(pid_socket_pair[0], pid, sizeof *pid, 0);
3812 if (l < 0)
3813 return log_error_errno(errno, "Failed to read inner child PID: %m");
3814 if (l != sizeof *pid) {
3815 log_error("Short read while reading inner child PID.");
3816 return -EIO;
3817 }
3818
3819 /* We also retrieve container UUID in case it was generated by outer child */
3820 l = recv(uuid_socket_pair[0], &arg_uuid, sizeof arg_uuid, 0);
3821 if (l < 0)
3822 return log_error_errno(errno, "Failed to read container machine ID: %m");
3823 if (l != sizeof(arg_uuid)) {
3824 log_error("Short read while reading container machined ID.");
3825 return -EIO;
3826 }
3827
3828 /* We also retrieve the socket used for notifications generated by outer child */
3829 notify_socket = receive_one_fd(notify_socket_pair[0], 0);
3830 if (notify_socket < 0)
3831 return log_error_errno(notify_socket,
3832 "Failed to receive notification socket from the outer child: %m");
3833
3834 log_debug("Init process invoked as PID "PID_FMT, *pid);
3835
3836 if (arg_userns_mode != USER_NAMESPACE_NO) {
3837 if (!barrier_place_and_sync(&barrier)) { /* #1 */
3838 log_error("Child died too early.");
3839 return -ESRCH;
3840 }
3841
3842 r = setup_uid_map(*pid);
3843 if (r < 0)
3844 return r;
3845
3846 (void) barrier_place(&barrier); /* #2 */
3847 }
3848
3849 if (arg_private_network) {
3850 if (!arg_network_namespace_path) {
3851 /* Wait until the child has unshared its network namespace. */
3852 if (!barrier_place_and_sync(&barrier)) { /* #3 */
3853 log_error("Child died too early");
3854 return -ESRCH;
3855 }
3856 }
3857
3858 r = move_network_interfaces(*pid, arg_network_interfaces);
3859 if (r < 0)
3860 return r;
3861
3862 if (arg_network_veth) {
3863 r = setup_veth(arg_machine, *pid, veth_name,
3864 arg_network_bridge || arg_network_zone);
3865 if (r < 0)
3866 return r;
3867 else if (r > 0)
3868 ifi = r;
3869
3870 if (arg_network_bridge) {
3871 /* Add the interface to a bridge */
3872 r = setup_bridge(veth_name, arg_network_bridge, false);
3873 if (r < 0)
3874 return r;
3875 if (r > 0)
3876 ifi = r;
3877 } else if (arg_network_zone) {
3878 /* Add the interface to a bridge, possibly creating it */
3879 r = setup_bridge(veth_name, arg_network_zone, true);
3880 if (r < 0)
3881 return r;
3882 if (r > 0)
3883 ifi = r;
3884 }
3885 }
3886
3887 r = setup_veth_extra(arg_machine, *pid, arg_network_veth_extra);
3888 if (r < 0)
3889 return r;
3890
3891 /* We created the primary and extra veth links now; let's remember this, so that we know to
3892 remove them later on. Note that we don't bother with removing veth links that were created
3893 here when their setup failed half-way, because in that case the kernel should be able to
3894 remove them on its own, since they cannot be referenced by anything yet. */
3895 *veth_created = true;
3896
3897 r = setup_macvlan(arg_machine, *pid, arg_network_macvlan);
3898 if (r < 0)
3899 return r;
3900
3901 r = setup_ipvlan(arg_machine, *pid, arg_network_ipvlan);
3902 if (r < 0)
3903 return r;
3904 }
3905
3906 if (arg_register || !arg_keep_unit) {
3907 r = sd_bus_default_system(&bus);
3908 if (r < 0)
3909 return log_error_errno(r, "Failed to open system bus: %m");
3910
3911 r = sd_bus_set_close_on_exit(bus, false);
3912 if (r < 0)
3913 return log_error_errno(r, "Failed to disable close-on-exit behaviour: %m");
3914 }
3915
3916 if (!arg_keep_unit) {
3917 /* When a new scope is created for this container, then we'll be registered as its controller, in which
3918 * case PID 1 will send us a friendly RequestStop signal, when it is asked to terminate the
3919 * scope. Let's hook into that, and cleanly shut down the container, and print a friendly message. */
3920
3921 r = sd_bus_match_signal_async(
3922 bus,
3923 NULL,
3924 "org.freedesktop.systemd1",
3925 NULL,
3926 "org.freedesktop.systemd1.Scope",
3927 "RequestStop",
3928 on_request_stop, NULL, PID_TO_PTR(*pid));
3929 if (r < 0)
3930 return log_error_errno(r, "Failed to request RequestStop match: %m");
3931 }
3932
3933 if (arg_register) {
3934 r = register_machine(
3935 bus,
3936 arg_machine,
3937 *pid,
3938 arg_directory,
3939 arg_uuid,
3940 ifi,
3941 arg_slice,
3942 arg_custom_mounts, arg_n_custom_mounts,
3943 arg_kill_signal,
3944 arg_property,
3945 arg_keep_unit,
3946 arg_container_service_name);
3947 if (r < 0)
3948 return r;
3949
3950 } else if (!arg_keep_unit) {
3951 r = allocate_scope(
3952 bus,
3953 arg_machine,
3954 *pid,
3955 arg_slice,
3956 arg_custom_mounts, arg_n_custom_mounts,
3957 arg_kill_signal,
3958 arg_property);
3959 if (r < 0)
3960 return r;
3961
3962 } else if (arg_slice || arg_property)
3963 log_notice("Machine and scope registration turned off, --slice= and --property= settings will have no effect.");
3964
3965 r = create_subcgroup(*pid, arg_keep_unit, arg_unified_cgroup_hierarchy);
3966 if (r < 0)
3967 return r;
3968
3969 r = sync_cgroup(*pid, arg_unified_cgroup_hierarchy, arg_uid_shift);
3970 if (r < 0)
3971 return r;
3972
3973 r = chown_cgroup(*pid, arg_unified_cgroup_hierarchy, arg_uid_shift);
3974 if (r < 0)
3975 return r;
3976
3977 /* Notify the child that the parent is ready with all
3978 * its setup (including cgroup-ification), and that
3979 * the child can now hand over control to the code to
3980 * run inside the container. */
3981 (void) barrier_place(&barrier); /* #4 */
3982
3983 /* Block SIGCHLD here, before notifying child.
3984 * process_pty() will handle it with the other signals. */
3985 assert_se(sigprocmask(SIG_BLOCK, &mask_chld, NULL) >= 0);
3986
3987 /* Reset signal to default */
3988 r = default_signals(SIGCHLD, -1);
3989 if (r < 0)
3990 return log_error_errno(r, "Failed to reset SIGCHLD: %m");
3991
3992 r = sd_event_new(&event);
3993 if (r < 0)
3994 return log_error_errno(r, "Failed to get default event source: %m");
3995
3996 (void) sd_event_set_watchdog(event, true);
3997
3998 if (bus) {
3999 r = sd_bus_attach_event(bus, event, 0);
4000 if (r < 0)
4001 return log_error_errno(r, "Failed to attach bus to event loop: %m");
4002 }
4003
4004 r = setup_sd_notify_parent(event, notify_socket, PID_TO_PTR(*pid), &notify_event_source);
4005 if (r < 0)
4006 return r;
4007
4008 /* Let the child know that we are ready and wait that the child is completely ready now. */
4009 if (!barrier_place_and_sync(&barrier)) { /* #5 */
4010 log_error("Child died too early.");
4011 return -ESRCH;
4012 }
4013
4014 /* At this point we have made use of the UID we picked, and thus nss-mymachines
4015 * will make them appear in getpwuid(), thus we can release the /etc/passwd lock. */
4016 etc_passwd_lock = safe_close(etc_passwd_lock);
4017
4018 sd_notifyf(false,
4019 "STATUS=Container running.\n"
4020 "X_NSPAWN_LEADER_PID=" PID_FMT, *pid);
4021 if (!arg_notify_ready)
4022 (void) sd_notify(false, "READY=1\n");
4023
4024 if (arg_kill_signal > 0) {
4025 /* Try to kill the init system on SIGINT or SIGTERM */
4026 (void) sd_event_add_signal(event, NULL, SIGINT, on_orderly_shutdown, PID_TO_PTR(*pid));
4027 (void) sd_event_add_signal(event, NULL, SIGTERM, on_orderly_shutdown, PID_TO_PTR(*pid));
4028 } else {
4029 /* Immediately exit */
4030 (void) sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
4031 (void) sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
4032 }
4033
4034 /* Exit when the child exits */
4035 (void) sd_event_add_signal(event, NULL, SIGCHLD, on_sigchld, PID_TO_PTR(*pid));
4036
4037 if (arg_expose_ports) {
4038 r = expose_port_watch_rtnl(event, rtnl_socket_pair[0], on_address_change, exposed, &rtnl);
4039 if (r < 0)
4040 return r;
4041
4042 (void) expose_port_execute(rtnl, arg_expose_ports, exposed);
4043 }
4044
4045 rtnl_socket_pair[0] = safe_close(rtnl_socket_pair[0]);
4046
4047 r = pty_forward_new(event, master,
4048 PTY_FORWARD_IGNORE_VHANGUP | (interactive ? 0 : PTY_FORWARD_READ_ONLY),
4049 &forward);
4050 if (r < 0)
4051 return log_error_errno(r, "Failed to create PTY forwarder: %m");
4052
4053 r = sd_event_loop(event);
4054 if (r < 0)
4055 return log_error_errno(r, "Failed to run event loop: %m");
4056
4057 pty_forward_get_last_char(forward, &last_char);
4058
4059 forward = pty_forward_free(forward);
4060
4061 if (!arg_quiet && last_char != '\n')
4062 putc('\n', stdout);
4063
4064 /* Kill if it is not dead yet anyway */
4065 if (bus) {
4066 if (arg_register)
4067 terminate_machine(bus, arg_machine);
4068 else if (!arg_keep_unit)
4069 terminate_scope(bus, arg_machine);
4070 }
4071
4072 /* Normally redundant, but better safe than sorry */
4073 (void) kill(*pid, SIGKILL);
4074
4075 r = wait_for_container(*pid, &container_status);
4076 *pid = 0;
4077
4078 if (r < 0)
4079 /* We failed to wait for the container, or the container exited abnormally. */
4080 return r;
4081 if (r > 0 || container_status == CONTAINER_TERMINATED) {
4082 /* r > 0 → The container exited with a non-zero status.
4083 * As a special case, we need to replace 133 with a different value,
4084 * because 133 is special-cased in the service file to reboot the container.
4085 * otherwise → The container exited with zero status and a reboot was not requested.
4086 */
4087 if (r == EXIT_FORCE_RESTART)
4088 r = EXIT_FAILURE; /* replace 133 with the general failure code */
4089 *ret = r;
4090 return 0; /* finito */
4091 }
4092
4093 /* CONTAINER_REBOOTED, loop again */
4094
4095 if (arg_keep_unit) {
4096 /* Special handling if we are running as a service: instead of simply
4097 * restarting the machine we want to restart the entire service, so let's
4098 * inform systemd about this with the special exit code 133. The service
4099 * file uses RestartForceExitStatus=133 so that this results in a full
4100 * nspawn restart. This is necessary since we might have cgroup parameters
4101 * set we want to have flushed out. */
4102 *ret = EXIT_FORCE_RESTART;
4103 return 0; /* finito */
4104 }
4105
4106 expose_port_flush(arg_expose_ports, exposed);
4107
4108 (void) remove_veth_links(veth_name, arg_network_veth_extra);
4109 *veth_created = false;
4110 return 1; /* loop again */
4111 }
4112
4113 static int initialize_rlimits(void) {
4114 /* The default resource limits the kernel passes to PID 1, as per kernel 4.16. Let's pass our container payload
4115 * the same values as the kernel originally passed to PID 1, in order to minimize differences between host and
4116 * container execution environments. */
4117
4118 static const struct rlimit kernel_defaults[_RLIMIT_MAX] = {
4119 [RLIMIT_AS] = { RLIM_INFINITY, RLIM_INFINITY },
4120 [RLIMIT_CORE] = { 0, RLIM_INFINITY },
4121 [RLIMIT_CPU] = { RLIM_INFINITY, RLIM_INFINITY },
4122 [RLIMIT_DATA] = { RLIM_INFINITY, RLIM_INFINITY },
4123 [RLIMIT_FSIZE] = { RLIM_INFINITY, RLIM_INFINITY },
4124 [RLIMIT_LOCKS] = { RLIM_INFINITY, RLIM_INFINITY },
4125 [RLIMIT_MEMLOCK] = { 65536, 65536 },
4126 [RLIMIT_MSGQUEUE] = { 819200, 819200 },
4127 [RLIMIT_NICE] = { 0, 0 },
4128 [RLIMIT_NOFILE] = { 1024, 4096 },
4129 [RLIMIT_RSS] = { RLIM_INFINITY, RLIM_INFINITY },
4130 [RLIMIT_RTPRIO] = { 0, 0 },
4131 [RLIMIT_RTTIME] = { RLIM_INFINITY, RLIM_INFINITY },
4132 [RLIMIT_STACK] = { 8388608, RLIM_INFINITY },
4133
4134 /* The kernel scales the default for RLIMIT_NPROC and RLIMIT_SIGPENDING based on the system's amount of
4135 * RAM. To provide best compatibility we'll read these limits off PID 1 instead of hardcoding them
4136 * here. This is safe as we know that PID 1 doesn't change these two limits and thus the original
4137 * kernel's initialization should still be valid during runtime — at least if PID 1 is systemd. Note
4138 * that PID 1 changes a number of other resource limits during early initialization which is why we
4139 * don't read the other limits from PID 1 but prefer the static table above. */
4140 };
4141
4142 int rl;
4143
4144 for (rl = 0; rl < _RLIMIT_MAX; rl++) {
4145 /* Let's only fill in what the user hasn't explicitly configured anyway */
4146 if ((arg_settings_mask & (SETTING_RLIMIT_FIRST << rl)) == 0) {
4147 const struct rlimit *v;
4148 struct rlimit buffer;
4149
4150 if (IN_SET(rl, RLIMIT_NPROC, RLIMIT_SIGPENDING)) {
4151 /* For these two let's read the limits off PID 1. See above for an explanation. */
4152
4153 if (prlimit(1, rl, NULL, &buffer) < 0)
4154 return log_error_errno(errno, "Failed to read resource limit RLIMIT_%s of PID 1: %m", rlimit_to_string(rl));
4155
4156 v = &buffer;
4157 } else
4158 v = kernel_defaults + rl;
4159
4160 arg_rlimit[rl] = newdup(struct rlimit, v, 1);
4161 if (!arg_rlimit[rl])
4162 return log_oom();
4163 }
4164
4165 if (DEBUG_LOGGING) {
4166 _cleanup_free_ char *k = NULL;
4167
4168 (void) rlimit_format(arg_rlimit[rl], &k);
4169 log_debug("Setting RLIMIT_%s to %s.", rlimit_to_string(rl), k);
4170 }
4171 }
4172
4173 return 0;
4174 }
4175
4176 int main(int argc, char *argv[]) {
4177 _cleanup_free_ char *console = NULL;
4178 _cleanup_close_ int master = -1;
4179 _cleanup_fdset_free_ FDSet *fds = NULL;
4180 int r, n_fd_passed, ret = EXIT_SUCCESS;
4181 char veth_name[IFNAMSIZ] = "";
4182 bool secondary = false, remove_directory = false, remove_image = false;
4183 pid_t pid = 0;
4184 union in_addr_union exposed = {};
4185 _cleanup_(release_lock_file) LockFile tree_global_lock = LOCK_FILE_INIT, tree_local_lock = LOCK_FILE_INIT;
4186 bool interactive, veth_created = false, remove_tmprootdir = false;
4187 char tmprootdir[] = "/tmp/nspawn-root-XXXXXX";
4188 _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
4189 _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL;
4190 _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL;
4191
4192 log_parse_environment();
4193 log_open();
4194
4195 /* Make sure rename_process() in the stub init process can work */
4196 saved_argv = argv;
4197 saved_argc = argc;
4198
4199 r = parse_argv(argc, argv);
4200 if (r <= 0)
4201 goto finish;
4202
4203 r = must_be_root();
4204 if (r < 0)
4205 goto finish;
4206
4207 r = initialize_rlimits();
4208 if (r < 0)
4209 goto finish;
4210
4211 r = determine_names();
4212 if (r < 0)
4213 goto finish;
4214
4215 r = load_settings();
4216 if (r < 0)
4217 goto finish;
4218
4219 parse_environment();
4220
4221 r = cg_unified_flush();
4222 if (r < 0) {
4223 log_error_errno(r, "Failed to determine whether the unified cgroups hierarchy is used: %m");
4224 goto finish;
4225 }
4226
4227 r = verify_arguments();
4228 if (r < 0)
4229 goto finish;
4230
4231 r = detect_unified_cgroup_hierarchy_from_environment();
4232 if (r < 0)
4233 goto finish;
4234
4235 /* Ignore SIGPIPE here, because we use splice() on the ptyfwd stuff and that will generate SIGPIPE if
4236 * the result is closed. Note that the container payload child will reset signal mask+handler anyway,
4237 * so just turning this off here means we only turn it off in nspawn itself, not any children. */
4238 (void) ignore_signals(SIGPIPE, -1);
4239
4240 n_fd_passed = sd_listen_fds(false);
4241 if (n_fd_passed > 0) {
4242 r = fdset_new_listen_fds(&fds, false);
4243 if (r < 0) {
4244 log_error_errno(r, "Failed to collect file descriptors: %m");
4245 goto finish;
4246 }
4247 }
4248
4249 /* The "default" umask. This is appropriate for most file and directory
4250 * operations performed by nspawn, and is the umask that will be used for
4251 * the child. Functions like copy_devnodes() change the umask temporarily. */
4252 umask(0022);
4253
4254 if (arg_directory) {
4255 assert(!arg_image);
4256
4257 if (path_equal(arg_directory, "/") && !arg_ephemeral) {
4258 log_error("Spawning container on root directory is not supported. Consider using --ephemeral.");
4259 r = -EINVAL;
4260 goto finish;
4261 }
4262
4263 if (arg_ephemeral) {
4264 _cleanup_free_ char *np = NULL;
4265
4266 r = chase_symlinks_and_update(&arg_directory, 0);
4267 if (r < 0)
4268 goto finish;
4269
4270 /* If the specified path is a mount point we
4271 * generate the new snapshot immediately
4272 * inside it under a random name. However if
4273 * the specified is not a mount point we
4274 * create the new snapshot in the parent
4275 * directory, just next to it. */
4276 r = path_is_mount_point(arg_directory, NULL, 0);
4277 if (r < 0) {
4278 log_error_errno(r, "Failed to determine whether directory %s is mount point: %m", arg_directory);
4279 goto finish;
4280 }
4281 if (r > 0)
4282 r = tempfn_random_child(arg_directory, "machine.", &np);
4283 else
4284 r = tempfn_random(arg_directory, "machine.", &np);
4285 if (r < 0) {
4286 log_error_errno(r, "Failed to generate name for directory snapshot: %m");
4287 goto finish;
4288 }
4289
4290 r = image_path_lock(np, (arg_read_only ? LOCK_SH : LOCK_EX) | LOCK_NB, &tree_global_lock, &tree_local_lock);
4291 if (r < 0) {
4292 log_error_errno(r, "Failed to lock %s: %m", np);
4293 goto finish;
4294 }
4295
4296 r = btrfs_subvol_snapshot(arg_directory, np,
4297 (arg_read_only ? BTRFS_SNAPSHOT_READ_ONLY : 0) |
4298 BTRFS_SNAPSHOT_FALLBACK_COPY |
4299 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY |
4300 BTRFS_SNAPSHOT_RECURSIVE |
4301 BTRFS_SNAPSHOT_QUOTA);
4302 if (r < 0) {
4303 log_error_errno(r, "Failed to create snapshot %s from %s: %m", np, arg_directory);
4304 goto finish;
4305 }
4306
4307 free_and_replace(arg_directory, np);
4308
4309 remove_directory = true;
4310
4311 } else {
4312 r = chase_symlinks_and_update(&arg_directory, arg_template ? CHASE_NONEXISTENT : 0);
4313 if (r < 0)
4314 goto finish;
4315
4316 r = image_path_lock(arg_directory, (arg_read_only ? LOCK_SH : LOCK_EX) | LOCK_NB, &tree_global_lock, &tree_local_lock);
4317 if (r == -EBUSY) {
4318 log_error_errno(r, "Directory tree %s is currently busy.", arg_directory);
4319 goto finish;
4320 }
4321 if (r < 0) {
4322 log_error_errno(r, "Failed to lock %s: %m", arg_directory);
4323 goto finish;
4324 }
4325
4326 if (arg_template) {
4327 r = chase_symlinks_and_update(&arg_template, 0);
4328 if (r < 0)
4329 goto finish;
4330
4331 r = btrfs_subvol_snapshot(arg_template, arg_directory,
4332 (arg_read_only ? BTRFS_SNAPSHOT_READ_ONLY : 0) |
4333 BTRFS_SNAPSHOT_FALLBACK_COPY |
4334 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY |
4335 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE |
4336 BTRFS_SNAPSHOT_RECURSIVE |
4337 BTRFS_SNAPSHOT_QUOTA);
4338 if (r == -EEXIST)
4339 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO,
4340 "Directory %s already exists, not populating from template %s.", arg_directory, arg_template);
4341 else if (r < 0) {
4342 log_error_errno(r, "Couldn't create snapshot %s from %s: %m", arg_directory, arg_template);
4343 goto finish;
4344 } else
4345 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO,
4346 "Populated %s from template %s.", arg_directory, arg_template);
4347 }
4348 }
4349
4350 if (arg_start_mode == START_BOOT) {
4351 const char *p;
4352
4353 if (arg_pivot_root_new)
4354 p = prefix_roota(arg_directory, arg_pivot_root_new);
4355 else
4356 p = arg_directory;
4357
4358 if (path_is_os_tree(p) <= 0) {
4359 log_error("Directory %s doesn't look like an OS root directory (os-release file is missing). Refusing.", p);
4360 r = -EINVAL;
4361 goto finish;
4362 }
4363 } else {
4364 const char *p, *q;
4365
4366 if (arg_pivot_root_new)
4367 p = prefix_roota(arg_directory, arg_pivot_root_new);
4368 else
4369 p = arg_directory;
4370
4371 q = strjoina(p, "/usr/");
4372
4373 if (laccess(q, F_OK) < 0) {
4374 log_error("Directory %s doesn't look like it has an OS tree. Refusing.", p);
4375 r = -EINVAL;
4376 goto finish;
4377 }
4378 }
4379
4380 } else {
4381 assert(arg_image);
4382 assert(!arg_template);
4383
4384 r = chase_symlinks_and_update(&arg_image, 0);
4385 if (r < 0)
4386 goto finish;
4387
4388 if (arg_ephemeral) {
4389 _cleanup_free_ char *np = NULL;
4390
4391 r = tempfn_random(arg_image, "machine.", &np);
4392 if (r < 0) {
4393 log_error_errno(r, "Failed to generate name for image snapshot: %m");
4394 goto finish;
4395 }
4396
4397 r = image_path_lock(np, (arg_read_only ? LOCK_SH : LOCK_EX) | LOCK_NB, &tree_global_lock, &tree_local_lock);
4398 if (r < 0) {
4399 r = log_error_errno(r, "Failed to create image lock: %m");
4400 goto finish;
4401 }
4402
4403 r = copy_file(arg_image, np, O_EXCL, arg_read_only ? 0400 : 0600, FS_NOCOW_FL, COPY_REFLINK|COPY_CRTIME);
4404 if (r < 0) {
4405 r = log_error_errno(r, "Failed to copy image file: %m");
4406 goto finish;
4407 }
4408
4409 free_and_replace(arg_image, np);
4410
4411 remove_image = true;
4412 } else {
4413 r = image_path_lock(arg_image, (arg_read_only ? LOCK_SH : LOCK_EX) | LOCK_NB, &tree_global_lock, &tree_local_lock);
4414 if (r == -EBUSY) {
4415 r = log_error_errno(r, "Disk image %s is currently busy.", arg_image);
4416 goto finish;
4417 }
4418 if (r < 0) {
4419 r = log_error_errno(r, "Failed to create image lock: %m");
4420 goto finish;
4421 }
4422
4423 if (!arg_root_hash) {
4424 r = root_hash_load(arg_image, &arg_root_hash, &arg_root_hash_size);
4425 if (r < 0) {
4426 log_error_errno(r, "Failed to load root hash file for %s: %m", arg_image);
4427 goto finish;
4428 }
4429 }
4430 }
4431
4432 if (!mkdtemp(tmprootdir)) {
4433 r = log_error_errno(errno, "Failed to create temporary directory: %m");
4434 goto finish;
4435 }
4436
4437 remove_tmprootdir = true;
4438
4439 arg_directory = strdup(tmprootdir);
4440 if (!arg_directory) {
4441 r = log_oom();
4442 goto finish;
4443 }
4444
4445 r = loop_device_make_by_path(arg_image, arg_read_only ? O_RDONLY : O_RDWR, &loop);
4446 if (r < 0) {
4447 log_error_errno(r, "Failed to set up loopback block device: %m");
4448 goto finish;
4449 }
4450
4451 r = dissect_image_and_warn(
4452 loop->fd,
4453 arg_image,
4454 arg_root_hash, arg_root_hash_size,
4455 DISSECT_IMAGE_REQUIRE_ROOT,
4456 &dissected_image);
4457 if (r == -ENOPKG) {
4458 /* dissected_image_and_warn() already printed a brief error message. Extend on that with more details */
4459 log_notice("Note that the disk image needs to\n"
4460 " a) either contain only a single MBR partition of type 0x83 that is marked bootable\n"
4461 " b) or contain a single GPT partition of type 0FC63DAF-8483-4772-8E79-3D69D8477DE4\n"
4462 " c) or follow http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/\n"
4463 " d) or contain a file system without a partition table\n"
4464 "in order to be bootable with systemd-nspawn.");
4465 goto finish;
4466 }
4467 if (r < 0)
4468 goto finish;
4469
4470 if (!arg_root_hash && dissected_image->can_verity)
4471 log_notice("Note: image %s contains verity information, but no root hash specified! Proceeding without integrity checking.", arg_image);
4472
4473 r = dissected_image_decrypt_interactively(dissected_image, NULL, arg_root_hash, arg_root_hash_size, 0, &decrypted_image);
4474 if (r < 0)
4475 goto finish;
4476
4477 /* Now that we mounted the image, let's try to remove it again, if it is ephemeral */
4478 if (remove_image && unlink(arg_image) >= 0)
4479 remove_image = false;
4480 }
4481
4482 r = custom_mount_prepare_all(arg_directory, arg_custom_mounts, arg_n_custom_mounts);
4483 if (r < 0)
4484 goto finish;
4485
4486 interactive =
4487 isatty(STDIN_FILENO) > 0 &&
4488 isatty(STDOUT_FILENO) > 0;
4489
4490 master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
4491 if (master < 0) {
4492 r = log_error_errno(errno, "Failed to acquire pseudo tty: %m");
4493 goto finish;
4494 }
4495
4496 r = ptsname_malloc(master, &console);
4497 if (r < 0) {
4498 r = log_error_errno(r, "Failed to determine tty name: %m");
4499 goto finish;
4500 }
4501
4502 if (arg_selinux_apifs_context) {
4503 r = mac_selinux_apply(console, arg_selinux_apifs_context);
4504 if (r < 0)
4505 goto finish;
4506 }
4507
4508 if (unlockpt(master) < 0) {
4509 r = log_error_errno(errno, "Failed to unlock tty: %m");
4510 goto finish;
4511 }
4512
4513 if (!arg_quiet)
4514 log_info("Spawning container %s on %s.\nPress ^] three times within 1s to kill container.",
4515 arg_machine, arg_image ?: arg_directory);
4516
4517 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, SIGWINCH, SIGTERM, SIGINT, -1) >= 0);
4518
4519 if (prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0) < 0) {
4520 r = log_error_errno(errno, "Failed to become subreaper: %m");
4521 goto finish;
4522 }
4523
4524 for (;;) {
4525 r = run(master,
4526 console,
4527 dissected_image,
4528 interactive, secondary,
4529 fds,
4530 veth_name, &veth_created,
4531 &exposed,
4532 &pid, &ret);
4533 if (r <= 0)
4534 break;
4535 }
4536
4537 finish:
4538 sd_notify(false,
4539 r == 0 && ret == EXIT_FORCE_RESTART ? "STOPPING=1\nSTATUS=Restarting..." :
4540 "STOPPING=1\nSTATUS=Terminating...");
4541
4542 if (pid > 0)
4543 (void) kill(pid, SIGKILL);
4544
4545 /* Try to flush whatever is still queued in the pty */
4546 if (master >= 0) {
4547 (void) copy_bytes(master, STDOUT_FILENO, (uint64_t) -1, 0);
4548 master = safe_close(master);
4549 }
4550
4551 if (pid > 0)
4552 (void) wait_for_terminate(pid, NULL);
4553
4554 pager_close();
4555
4556 if (remove_directory && arg_directory) {
4557 int k;
4558
4559 k = rm_rf(arg_directory, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
4560 if (k < 0)
4561 log_warning_errno(k, "Cannot remove '%s', ignoring: %m", arg_directory);
4562 }
4563
4564 if (remove_image && arg_image) {
4565 if (unlink(arg_image) < 0)
4566 log_warning_errno(errno, "Can't remove image file '%s', ignoring: %m", arg_image);
4567 }
4568
4569 if (remove_tmprootdir) {
4570 if (rmdir(tmprootdir) < 0)
4571 log_debug_errno(errno, "Can't remove temporary root directory '%s', ignoring: %m", tmprootdir);
4572 }
4573
4574 if (arg_machine) {
4575 const char *p;
4576
4577 p = strjoina("/run/systemd/nspawn/propagate/", arg_machine);
4578 (void) rm_rf(p, REMOVE_ROOT);
4579 }
4580
4581 expose_port_flush(arg_expose_ports, &exposed);
4582
4583 if (veth_created)
4584 (void) remove_veth_links(veth_name, arg_network_veth_extra);
4585 (void) remove_bridge(arg_network_zone);
4586
4587 free(arg_directory);
4588 free(arg_template);
4589 free(arg_image);
4590 free(arg_machine);
4591 free(arg_hostname);
4592 free(arg_user);
4593 free(arg_pivot_root_new);
4594 free(arg_pivot_root_old);
4595 free(arg_chdir);
4596 strv_free(arg_setenv);
4597 free(arg_network_bridge);
4598 strv_free(arg_network_interfaces);
4599 strv_free(arg_network_macvlan);
4600 strv_free(arg_network_ipvlan);
4601 strv_free(arg_network_veth_extra);
4602 strv_free(arg_parameters);
4603 free(arg_network_zone);
4604 free(arg_network_namespace_path);
4605 strv_free(arg_property);
4606 custom_mount_free_all(arg_custom_mounts, arg_n_custom_mounts);
4607 expose_port_free_all(arg_expose_ports);
4608 free(arg_root_hash);
4609 rlimit_free_all(arg_rlimit);
4610 strv_free(arg_syscall_whitelist);
4611 strv_free(arg_syscall_blacklist);
4612 arg_cpuset = cpu_set_mfree(arg_cpuset);
4613
4614 return r < 0 ? EXIT_FAILURE : ret;
4615 }