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