HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/nspawn/nspawn.c

300,508 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 1c687382baea8193c023ec86840c44042195bef9

Lines 3001–4000 of 6,878

                r = path_pick_update_warn(
                                &arg_image,
                                &filter,
                                /* n_filters= */ 1,
                                PICK_ARCHITECTURE|PICK_TRIES,
                                &result);
                if (r < 0)
                        return r;
 
                arg_architecture = result.architecture;
        }
 
        if (arg_mstack) {
                _cleanup_(pick_result_done) PickResult result = PICK_RESULT_NULL;
                PickFilter filter = *pick_filter_image_mstack;
 
                filter.architecture = arg_architecture;
 
                r = path_pick_update_warn(
                                &arg_mstack,
                                &filter,
                                /* n_filters= */ 1,
                                PICK_ARCHITECTURE|PICK_TRIES,
                                &result);
                if (r < 0)
                        return r;
 
                arg_architecture = result.architecture;
        }
 
        if (arg_template) {
                _cleanup_(pick_result_done) PickResult result = PICK_RESULT_NULL;
                PickFilter filter = *pick_filter_image_dir;
 
                filter.architecture = arg_architecture;
 
                r = path_pick_update_warn(
                                &arg_template,
                                &filter,
                                /* n_filters= */ 1,
                                PICK_ARCHITECTURE,
                                &result);
                if (r < 0)
                        return r;
 
                arg_architecture = result.architecture;
        }
 
        return 0;
}
 
static int determine_names(void) {
        int r;
 
        if (arg_template && !arg_directory && arg_machine) {
 
                /* If --template= was specified then we should not search for a machine, but instead create a
                 * new one in /var/lib/machine. */
 
                arg_directory = path_join("/var/lib/machines", arg_machine);
                if (!arg_directory)
                        return log_oom();
        }
 
        if (!arg_image && !arg_directory && !arg_mstack) {
                if (arg_machine) {
                        _cleanup_(image_unrefp) Image *i = NULL;
 
                        r = image_find(arg_runtime_scope,
                                       IMAGE_MACHINE, arg_machine, NULL, &i);
                        if (r == -ENOENT)
                                return log_error_errno(r, "No image for machine '%s'.", arg_machine);
                        if (r < 0)
                                return log_error_errno(r, "Failed to find image for machine '%s': %m", arg_machine);
 
                        switch (i->type) {
                        case IMAGE_RAW:
                        case IMAGE_BLOCK:
                                r = free_and_strdup(&arg_image, i->path);
                                break;
 
                        case IMAGE_DIRECTORY:
                        case IMAGE_SUBVOLUME:
                                r = free_and_strdup(&arg_directory, i->path);
                                break;
 
                        case IMAGE_MSTACK:
                                r = free_and_strdup(&arg_mstack, i->path);
                                break;
 
                        default:
                                assert_not_reached();
                        }
                        if (r < 0)
                                return log_oom();
 
                        if (!arg_ephemeral)
                                arg_read_only = arg_read_only || image_is_read_only(i);
                } else {
                        r = safe_getcwd(&arg_directory);
                        if (r < 0)
                                return log_error_errno(r, "Failed to determine current directory: %m");
                }
 
                if (!arg_directory && !arg_image && !arg_mstack)
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine path, please use --directory=, --image= or --mstack=.");
        }
 
        if (!arg_machine) {
                if (arg_directory) {
                        if (path_equal(arg_directory, "/")) {
                                arg_machine = gethostname_malloc();
                                if (!arg_machine)
                                        return log_oom();
                        } else {
                                r = path_extract_filename(arg_directory, &arg_machine);
                                if (r < 0)
                                        return log_error_errno(r, "Failed to extract file name from '%s': %m", arg_directory);
                        }
                } else if (arg_image) {
                        r = path_extract_filename(arg_image, &arg_machine);
                        if (r < 0)
                                return log_error_errno(r, "Failed to extract file name from '%s': %m", arg_image);
 
                        /* Truncate suffix if there is one */
                        char *e = endswith(arg_machine, ".raw");
                        if (e)
                                *e = 0;
                } else if (arg_mstack)  {
                        r = path_extract_filename(arg_mstack, &arg_machine);
                        if (r < 0)
                                return log_error_errno(r, "Failed to extract file name from '%s': %m", arg_mstack);
 
                        char *e = endswith(arg_machine, ".mstack");
                        if (e)
                                *e = 0;
                } else
                        assert_not_reached();
 
                hostname_cleanup(arg_machine);
                if (!hostname_is_valid(arg_machine, 0))
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine machine name automatically, please use -M.");
 
                /* Copy the machine name before the random suffix is added below, otherwise we won't be able
                 * to match fixed config file names. */
                arg_settings_filename = strjoin(arg_machine, ".nspawn");
                if (!arg_settings_filename)
                        return log_oom();
 
                /* Add a random suffix when this is an ephemeral machine, so that we can run many
                 * instances at once without manually having to specify -M each time. */
                if (arg_ephemeral)
                        if (strextendf(&arg_machine, "-%016" PRIx64, random_u64()) < 0)
                                return log_oom();
        } else {
                arg_settings_filename = strjoin(arg_machine, ".nspawn");
                if (!arg_settings_filename)
                        return log_oom();
        }
 
        return 0;
}
 
static int chase_and_update(char **p, ChaseFlags flags) {
        char *chased;
        int r;
 
        assert(p);
 
        if (!*p)
                return 0;
 
        r = chase(*p, /* root= */ NULL, flags, &chased, /* ret_fd= */ NULL);
        if (r < 0)
                return log_error_errno(r, "Failed to resolve path %s: %m", *p);
 
        return free_and_replace(*p, chased);
}
 
static int determine_uid_shift(const char *directory) {
        assert(directory);
 
        if (arg_userns_mode == USER_NAMESPACE_MANAGED) {
                /* In managed mode we should already know the UID shift */
                assert(uid_is_valid(arg_uid_shift));
                return 0;
        }
 
        if (arg_userns_mode == USER_NAMESPACE_NO) {
                arg_uid_shift = 0;
                return 0;
        }
 
        if (arg_uid_shift == UID_INVALID) {
                struct stat st;
 
                /* Read the UID shift off the image. Maybe we can reuse this to avoid chowning. */
 
                if (stat(directory, &st) < 0)
                        return log_error_errno(errno, "Failed to determine UID base of %s: %m", directory);
 
                arg_uid_shift = st.st_uid & UINT32_C(0xffff0000);
 
                if (arg_uid_shift != (st.st_gid & UINT32_C(0xffff0000)))
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                               "UID and GID base of %s don't match.", directory);
 
                arg_uid_range = UINT32_C(0x10000);
 
                if (arg_uid_shift != 0) {
                        /* If the image is shifted already, then we'll fall back to classic chowning, for
                         * compatibility (and simplicity), or refuse if mapping is explicitly requested.  */
 
                        if (arg_userns_ownership == USER_NAMESPACE_OWNERSHIP_AUTO) {
                                log_debug("UID base of %s is non-zero, not using UID mapping.", directory);
                                arg_userns_ownership = USER_NAMESPACE_OWNERSHIP_CHOWN;
                        } else if (arg_userns_ownership == USER_NAMESPACE_OWNERSHIP_MAP)
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                       "UID base of %s is not zero, UID mapping not supported.", directory);
                }
        }
 
        if (!userns_shift_range_valid(arg_uid_shift, arg_uid_range))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "UID base too high for UID range.");
 
        return 0;
}
 
static unsigned long effective_clone_ns_flags(void) {
        unsigned long flags = arg_clone_ns_flags;
 
        if (arg_private_network)
                flags |= CLONE_NEWNET;
        if (arg_use_cgns)
                flags |= CLONE_NEWCGROUP;
        if (arg_userns_mode != USER_NAMESPACE_NO)
                flags |= CLONE_NEWUSER;
 
        return flags;
}
 
static int patch_sysctl(void) {
 
        /* This table is inspired by runc's sysctl() function */
        static const struct {
                const char *key;
                bool prefix;
                unsigned long clone_flags;
        } safe_sysctl[] = {
                { "kernel.hostname",   false, CLONE_NEWUTS },
                { "kernel.domainname", false, CLONE_NEWUTS },
                { "kernel.msgmax",     false, CLONE_NEWIPC },
                { "kernel.msgmnb",     false, CLONE_NEWIPC },
                { "kernel.msgmni",     false, CLONE_NEWIPC },
                { "kernel.sem",        false, CLONE_NEWIPC },
                { "kernel.shmall",     false, CLONE_NEWIPC },
                { "kernel.shmmax",     false, CLONE_NEWIPC },
                { "kernel.shmmni",     false, CLONE_NEWIPC },
                { "fs.mqueue.",        true,  CLONE_NEWIPC },
                { "net.",              true,  CLONE_NEWNET },
        };
 
        unsigned long flags;
        int r;
 
        flags = effective_clone_ns_flags();
 
        if (FLAGS_SET(flags, CLONE_NEWUSER|CLONE_NEWNET) && !arg_network_namespace_path) {
                /* Follow the system-wide default from sysctl.d/50-default.conf, but clamp it to the
                 * groups mapped into the container's user namespace. */
                r = sysctl_writef("net/ipv4/ping_group_range", "0 " GID_FMT,
                                 MIN(arg_uid_range - 1, (gid_t) INT32_MAX));
                if (r < 0)
                        log_warning_errno(r,
                                          "Failed to set sysctl 'net.ipv4.ping_group_range', ignoring: %m");
        }
 
        STRV_FOREACH_PAIR(k, v, arg_sysctl) {
                bool good = false;
 
                FOREACH_ELEMENT(i, safe_sysctl) {
                        if (!FLAGS_SET(flags, i->clone_flags))
                                continue;
 
                        if (i->prefix)
                                good = startswith(*k, i->key);
                        else
                                good = streq(*k, i->key);
 
                        if (good)
                                break;
                }
 
                if (!good)
                        return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Refusing to write to sysctl '%s', as it is not safe in the selected namespaces.", *k);
 
                r = sysctl_write(*k, *v);
                if (r < 0)
                        return log_error_errno(r, "Failed to write sysctl '%s': %m", *k);
        }
 
        return 0;
}
 
static int inner_child(
                Barrier *barrier,
                int fd_inner_socket,
                FDSet *fds,
                char **fdnames,
                char **os_release_pairs) {
 
        _cleanup_free_ char *home = NULL;
        size_t n_env = 1;
        char *envp[] = {
                (char*) "PATH=" DEFAULT_PATH_COMPAT,
                NULL, /* container */
                NULL, /* TERM */
                NULL, /* COLORTERM */
                NULL, /* NO_COLOR */
                NULL, /* HOME */
                NULL, /* USER */
                NULL, /* LOGNAME */
                NULL, /* container_uuid */
                NULL, /* LISTEN_FDS */
                NULL, /* LISTEN_FDNAMES */
                NULL, /* NOTIFY_SOCKET */
                NULL, /* CREDENTIALS_DIRECTORY */
                NULL, /* LANG */
                NULL
        };
        const char *exec_target;
        _cleanup_strv_free_ char **env_use = NULL;
        int r, which_failed;
 
        /* This is the "inner" child process, i.e. the one forked off by the "outer" child process, which is the one
         * the container manager itself forked off. At the time of clone() it gained its own CLONE_NEWNS, CLONE_NEWPID,
         * CLONE_NEWUTS, CLONE_NEWIPC, CLONE_NEWUSER namespaces. Note that it has its own CLONE_NEWNS namespace,
         * separate from the CLONE_NEWNS created for the "outer" child, and also separate from the host's CLONE_NEWNS
         * namespace. The reason for having two levels of CLONE_NEWNS namespaces is that the "inner" one is owned by
         * the CLONE_NEWUSER namespace of the container, while the "outer" one is owned by the host's CLONE_NEWUSER
         * namespace.
         *
         * Note at this point we have no CLONE_NEWNET namespace yet. We'll acquire that one later through
         * unshare(). See below. */
 
        assert(barrier);
        assert(fd_inner_socket >= 0);
 
        log_debug("Inner child is initializing.");
 
        if (arg_userns_mode != USER_NAMESPACE_NO) {
                /* Tell the parent, that it now can write the UID map. */
                (void) barrier_place(barrier); /* #1 */
 
                /* Wait until the parent wrote the UID map */
                if (!barrier_place_and_sync(barrier)) /* #2 */
                        return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Parent died too early");
 
                /* Become the new root user inside our namespace */
                r = reset_uid_gid();
                if (r < 0)
                        return log_error_errno(r, "Couldn't become new root: %m");
 
                /* Creating a new user namespace means all MS_SHARED mounts become MS_SLAVE. Let's put them
                 * back to MS_SHARED here, since that's what we want as defaults. (This will not reconnect
                 * propagation, but simply create new peer groups for all our mounts). */
                r = mount_follow_verbose(LOG_ERR, NULL, "/", NULL, MS_SHARED|MS_REC, NULL);
                if (r < 0)
                        return r;
        }
 
        r = mount_all(/* dest= */ NULL,
                      arg_mount_settings | MOUNT_IN_USERNS,
                      arg_uid_shift,
                      arg_selinux_apifs_context);
        if (r < 0)
                return r;
 
        if (!arg_network_namespace_path && arg_private_network) {
                _cleanup_close_ int netns_fd = -EBADF;
 
                if (arg_userns_mode != USER_NAMESPACE_MANAGED)
                        if (unshare(CLONE_NEWNET) < 0)
                                return log_error_errno(errno, "Failed to unshare network namespace: %m");
 
                netns_fd = namespace_open_by_type(NAMESPACE_NET);
                if (netns_fd < 0)
                        return log_error_errno(netns_fd, "Failed to open newly allocate network namespace: %m");
 
                r = send_one_fd(fd_inner_socket, netns_fd, 0);
                if (r < 0)
                        return log_error_errno(r, "Failed to send network namespace to supervisor: %m");
 
                /* Tell the parent that it can setup network interfaces. */
                (void) barrier_place(barrier); /* #3 */
        }
 
        if (arg_userns_mode != USER_NAMESPACE_MANAGED) {
                r = mount_sysfs(NULL, arg_mount_settings | MOUNT_IN_USERNS);
                if (r < 0)
                        return r;
        }
 
        /* Wait until we are cgroup-ified, so that we can mount the right cgroup path writable */
        if (!barrier_place_and_sync(barrier)) /* #4 */
                return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
                                       "Parent died too early");
 
        if (arg_use_cgns) {
                r = unshare(CLONE_NEWCGROUP);
                if (r < 0)
                        return log_error_errno(errno, "Failed to unshare cgroup namespace: %m");
 
                r = mount_cgroups(/* dest= */ NULL, /* accept_existing= */ false);
        } else
                r = bind_mount_cgroup_hierarchy();
        if (r < 0)
                return r;
 
        r = setup_boot_id();
        if (r < 0)
                return r;
 
        r = setup_kmsg(fd_inner_socket);
        if (r < 0)
                return r;
 
        r = mount_custom(
                        "/",
                        arg_custom_mounts,
                        arg_n_custom_mounts,
                        0,
                        0,
                        arg_selinux_apifs_context,
                        MOUNT_NON_ROOT_ONLY | MOUNT_IN_USERNS);
        if (r < 0)
                return r;
 
        if (setsid() < 0)
                return log_error_errno(errno, "setsid() failed: %m");
 
        if (arg_private_network)
                (void) loopback_setup();
 
        if (arg_expose_ports) {
                r = expose_port_send_rtnl(fd_inner_socket);
                if (r < 0)
                        return r;
        }
 
        if (arg_console_mode != CONSOLE_PIPE) {
                _cleanup_close_ int master = -EBADF;
                _cleanup_free_ char *console = NULL;
 
                /* Allocate a pty and make it available as /dev/console. */
                master = openpt_allocate(O_RDWR|O_NONBLOCK, &console);
                if (master < 0)
                        return log_error_errno(master, "Failed to allocate a pty: %m");
 
                r = setup_dev_console(console);
                if (r < 0)
                        return log_error_errno(r, "Failed to set up /dev/console: %m");
 
                r = send_one_fd(fd_inner_socket, master, 0);
                if (r < 0)
                        return log_error_errno(r, "Failed to send master fd: %m");
 
                r = setup_stdio_as_dev_console();
                if (r < 0)
                        return r;
        }
 
        r = patch_sysctl();
        if (r < 0)
                return r;
 
        if (arg_oom_score_adjust_set) {
                r = set_oom_score_adjust(arg_oom_score_adjust);
                if (r < 0)
                        return log_error_errno(r, "Failed to adjust OOM score: %m");
        }
 
        if (arg_cpu_set.set)
                if (sched_setaffinity(0, arg_cpu_set.allocated, arg_cpu_set.set) < 0)
                        return log_error_errno(errno, "Failed to set CPU affinity: %m");
 
        (void) setup_hostname();
 
        if (arg_personality != PERSONALITY_INVALID) {
                r = safe_personality(arg_personality);
                if (r < 0)
                        return log_error_errno(r, "personality() failed: %m");
#ifdef ARCHITECTURE_SECONDARY
        } else if (arg_architecture == ARCHITECTURE_SECONDARY) {
                r = safe_personality(PER_LINUX32);
                if (r < 0)
                        return log_error_errno(r, "personality() failed: %m");
#endif
        } else if (!arg_quiet && arg_architecture >= 0 && arg_architecture != native_architecture())
                log_notice("Selected architecture '%s' not supported natively on the local CPU, assuming "
                           "invocation with qemu userspace emulator (or equivalent) in effect.",
                           architecture_to_string(arg_architecture));
 
        r = setrlimit_closest_all((const struct rlimit *const*) arg_rlimit, &which_failed);
        if (r < 0)
                return log_error_errno(r, "Failed to apply resource limit RLIMIT_%s: %m", rlimit_to_string(which_failed));
 
#if HAVE_SECCOMP
        if (arg_seccomp) {
 
                if (is_seccomp_available()) {
                        r = sym_seccomp_load(arg_seccomp);
                        if (ERRNO_IS_NEG_SECCOMP_FATAL(r))
                                return log_error_errno(r, "Failed to install seccomp filter: %m");
                        if (r < 0)
                                log_debug_errno(r, "Failed to install seccomp filter: %m");
                }
        } else
#endif
        {
                r = setup_seccomp(arg_caps_retain, arg_syscall_allow_list, arg_syscall_deny_list, arg_restrict_address_families, arg_restrict_address_families_is_allowlist);
                if (r < 0)
                        return r;
        }
 
        if (arg_suppress_sync) {
#if HAVE_SECCOMP
                r = seccomp_suppress_sync();
                if (r < 0)
                        log_debug_errno(r, "Failed to install sync() suppression seccomp filter, ignoring: %m");
#else
                log_debug("systemd is built without SECCOMP support. Ignoring --suppress-sync= command line option and SuppressSync= setting.");
#endif
        }
 
#if HAVE_SELINUX
        if (arg_selinux_context && mac_selinux_use())
                if (sym_setexeccon_raw(arg_selinux_context) < 0)
                        return log_error_errno(errno, "setexeccon(\"%s\") failed: %m", arg_selinux_context);
#endif
 
        /* Make sure we keep the caps across the uid/gid dropping, so that we can retain some selected caps
         * if we need to later on. */
        r = prctl_safe(PR_SET_KEEPCAPS, 1, 0, 0, 0);
        if (r < 0)
                return log_error_errno(r, "Failed to set PR_SET_KEEPCAPS: %m");
 
        if (uid_is_valid(arg_uid) || gid_is_valid(arg_gid))
                r = change_uid_gid_raw(arg_uid, arg_gid, arg_supplementary_gids, arg_n_supplementary_gids, arg_console_mode != CONSOLE_PIPE);
        else
                r = change_uid_gid(arg_user, arg_console_mode != CONSOLE_PIPE, &home);
        if (r < 0)
                return r;
 
        r = drop_capabilities(getuid());
        if (r < 0)
                return log_error_errno(r, "Dropping capabilities failed: %m");
 
        if (arg_no_new_privileges) {
                r = proc_set_nnp();
                if (r < 0)
                        return log_error_errno(r, "Failed to disable new privileges: %m");
        }
 
        /* LXC sets container=lxc, so follow the scheme here */
        envp[n_env++] = strjoina("container=", arg_container_service_name);
 
        /* Propagate $TERM & Co. unless we are invoked in pipe mode and stdin/stdout/stderr don't refer to a TTY */
        if (arg_console_mode != CONSOLE_PIPE && !terminal_is_dumb())
                FOREACH_STRING(v, "TERM=", "COLORTERM=", "NO_COLOR=") {
                        char *t = strv_find_prefix(environ, v);
                        if (!t)
                                continue;
 
                        envp[n_env++] = t;
                }
        else
                envp[n_env++] = (char*) "TERM=dumb";
 
        if (home || !uid_is_valid(arg_uid) || arg_uid == 0)
                if (asprintf(envp + n_env++, "HOME=%s", home ?: "/root") < 0)
                        return log_oom();
 
        if (arg_user || !uid_is_valid(arg_uid) || arg_uid == 0)
                if (asprintf(envp + n_env++, "USER=%s", arg_user ?: "root") < 0 ||
                    asprintf(envp + n_env++, "LOGNAME=%s", arg_user ?: "root") < 0)
                        return log_oom();
 
        assert(!sd_id128_is_null(arg_uuid));
 
        if (asprintf(envp + n_env++, "container_uuid=%s", SD_ID128_TO_UUID_STRING(arg_uuid)) < 0)
                return log_oom();
 
        if (!fdset_isempty(fds)) {
                r = fdset_cloexec(fds, false);
                if (r < 0)
                        return log_error_errno(r, "Failed to unset O_CLOEXEC for file descriptors.");
 
                if (asprintf(envp + n_env++, "LISTEN_FDS=%u", fdset_size(fds)) < 0)
                        return log_oom();
 
                if (fdnames) {
                        _cleanup_free_ char *joined = strv_join(fdnames, ":");
                        if (!joined)
                                return log_oom();
 
                        if (asprintf(envp + n_env++, "LISTEN_FDNAMES=%s", joined) < 0)
                                return log_oom();
                }
        }
        if (asprintf(envp + n_env++, "NOTIFY_SOCKET=%s", NSPAWN_NOTIFY_SOCKET_PATH) < 0)
                return log_oom();
 
        if (arg_credentials.n_credentials > 0) {
                envp[n_env] = strdup("CREDENTIALS_DIRECTORY=/run/host/credentials");
                if (!envp[n_env])
                        return log_oom();
                n_env++;
        }
 
        if (arg_start_mode != START_BOOT) {
                envp[n_env] = strdup("LANG=" SYSTEMD_NSPAWN_LOCALE);
                if (!envp[n_env])
                        return log_oom();
                n_env++;
        }
 
        env_use = strv_env_merge(envp, os_release_pairs, arg_setenv);
        if (!env_use)
                return log_oom();
 
        /* Let the parent know that we are ready and wait until the parent is ready with the setup, too... */
        if (!barrier_place_and_sync(barrier)) /* #5 */
                return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Parent died too early");
 
        /* Note, this should be done this late (💣 and not moved earlier! 💣), so that all namespacing
         * changes are already in effect by now, so that any resolved paths here definitely reference
         * resources inside the container, and not outside of them. */
        if (arg_chdir)
                if (chdir(arg_chdir) < 0)
                        return log_error_errno(errno, "Failed to change to specified working directory %s: %m", arg_chdir);
 
        if (arg_start_mode == START_PID2) {
                r = stub_pid1(arg_uuid);
                if (r < 0)
                        return r;
        }
 
        /* Now that we know which process is going to execute the payload, set $LISTEN_PID and
         * $LISTEN_PIDFDID. If $LISTEN_PID is overridden via --setenv=, leave $LISTEN_PIDFDID alone too, as
         * it would refer to a different process than $LISTEN_PID then. */
        if (!fdset_isempty(fds) && !strv_env_get(arg_setenv, "LISTEN_PID")) {
                r = strv_env_assignf(&env_use, "LISTEN_PID", PID_FMT, getpid_cached());
                if (r < 0)
                        return log_error_errno(r, "Failed to set $LISTEN_PID: %m");
 
                uint64_t pidfdid;
                if (!strv_env_get(arg_setenv, "LISTEN_PIDFDID") &&
                    pidfd_get_inode_id_self_cached(&pidfdid) >= 0) {
                        r = strv_env_assignf(&env_use, "LISTEN_PIDFDID", "%" PRIu64, pidfdid);
                        if (r < 0)
                                return log_error_errno(r, "Failed to set $LISTEN_PIDFDID: %m");
                }
        }
 
        if (arg_console_mode != CONSOLE_PIPE) {
                /* So far our pty wasn't controlled by any process. Finally, it's time to change that, if we
                 * are configured for that. Acquire it as controlling tty. */
                if (ioctl(STDIN_FILENO, TIOCSCTTY) < 0)
                        return log_error_errno(errno, "Failed to acquire controlling TTY: %m");
        }
 
        log_debug("Inner child finished, invoking payload.");
 
        /* Now, explicitly close the log, so that we then can close all remaining fds. Closing the log explicitly first
         * has the benefit that the logging subsystem knows about it, and is thus ready to be reopened should we need
         * it again. Note that the other fds closed here are at least the locking and barrier fds. */
        log_close();
        log_set_open_when_needed(true);
        log_settle_target();
 
        (void) fdset_close_others(fds);
 
        if (arg_start_mode == START_BOOT) {
                char **a;
                size_t m;
 
                /* Automatically search for the init system */
 
                m = strv_length(arg_parameters);
                a = newa(char*, m + 2);
                memcpy_safe(a + 1, arg_parameters, m * sizeof(char*));
                a[1 + m] = NULL;
 
                FOREACH_STRING(init,
                               "/usr/lib/systemd/systemd",
                               "/lib/systemd/systemd",
                               "/sbin/init") {
                        a[0] = (char*) init;
                        execve(a[0], a, env_use);
                }
 
                exec_target = "/usr/lib/systemd/systemd, /lib/systemd/systemd, /sbin/init";
        } else if (!strv_isempty(arg_parameters)) {
                const char *dollar_path;
 
                exec_target = arg_parameters[0];
 
                /* Use the user supplied search $PATH if there is one, or DEFAULT_PATH_COMPAT if not to search the
                 * binary. */
                dollar_path = strv_env_get(env_use, "PATH");
                if (dollar_path) {
                        if (setenv("PATH", dollar_path, 1) < 0)
                                return log_error_errno(errno, "Failed to update $PATH: %m");
                }
 
                execvpe(arg_parameters[0], arg_parameters, env_use);
        } else {
                if (!arg_chdir)
                        /* If we cannot change the directory, we'll end up in /, that is expected. */
                        (void) chdir(home ?: "/root");
 
                execle(DEFAULT_USER_SHELL, "-" DEFAULT_USER_SHELL_NAME, NULL, env_use);
                if (!streq(DEFAULT_USER_SHELL, "/bin/bash"))
                        execle("/bin/bash", "-bash", NULL, env_use);
                if (!streq(DEFAULT_USER_SHELL, "/bin/sh"))
                        execle("/bin/sh", "-sh", NULL, env_use);
 
                exec_target = DEFAULT_USER_SHELL ", /bin/bash, /bin/sh";
        }
 
        return log_error_errno(errno, "execv(%s) failed: %m", exec_target);
}
 
static int setup_notify_child(const void *directory) {
        _cleanup_close_ int fd = -EBADF;
        _cleanup_free_ char *j = NULL;
        union sockaddr_union sa = {
                .un.sun_family = AF_UNIX,
        };
        int r;
 
        fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
        if (fd < 0)
                return log_error_errno(errno, "Failed to allocate notification socket: %m");
 
        if (directory) {
                j = path_join(directory, NSPAWN_NOTIFY_SOCKET_PATH);
                if (!j)
                        return log_oom();
        }
 
        r = sockaddr_un_set_path(&sa.un, j ?: NSPAWN_NOTIFY_SOCKET_PATH);
        if (r < 0)
                return log_error_errno(r, "Failed to set AF_UNIX path to %s: %m", j ?: NSPAWN_NOTIFY_SOCKET_PATH);
 
        (void) mkdir_parents(sa.un.sun_path, 0755);
        (void) sockaddr_un_unlink(&sa.un);
 
        WITH_UMASK(0577) { /* only set "w" bit, which is all that's necessary for connecting from the container */
                r = bind(fd, &sa.sa, sockaddr_un_len(&sa.un));
                if (r < 0)
                        return log_error_errno(errno, "bind(" NSPAWN_NOTIFY_SOCKET_PATH ") failed: %m");
        }
 
        r = userns_lchown(sa.un.sun_path, 0, 0);
        if (r < 0)
                return log_error_errno(r, "Failed to chown " NSPAWN_NOTIFY_SOCKET_PATH ": %m");
 
        r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true);
        if (r < 0)
                return log_error_errno(r, "Failed to enable SO_PASSCRED: %m");
 
        r = setsockopt_int(fd, SOL_SOCKET, SO_PASSPIDFD, true);
        if (r < 0)
                log_debug_errno(r, "Failed to enable SO_PASSPIDFD, ignoring: %m");
 
        /* Only allow the container payload to pass file descriptors to us if we ourselves are
         * supervised by a service manager that enabled the FD store. */
        if (!fdstore_detected()) {
                r = setsockopt_int(fd, SOL_SOCKET, SO_PASSRIGHTS, false);
                if (r < 0)
                        log_debug_errno(r, "Failed to turn off SO_PASSRIGHTS, ignoring: %m");
        }
 
        return TAKE_FD(fd);
}
 
static int setup_unix_export_dir_outside(const char *runtime_dir, char **ret) {
        int r;
 
        assert(runtime_dir);
        assert(ret);
 
        if (arg_userns_mode == USER_NAMESPACE_MANAGED) {
                log_debug("Not digging socket tunnel, because running in managed user namespace mode.");
                return 0;
        }
 
        _cleanup_free_ char *p = NULL;
        p = path_join(runtime_dir, "unix-export");
        if (!p)
                return log_oom();
 
        r = path_is_mount_point(p);
        if (r > 0)
                return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "Mount point '%s' exists already, refusing.", p);
        if (r < 0 && r != -ENOENT)
                return log_error_errno(r, "Failed to detect if '%s' is a mount point: %m", p);
 
        r = mkdir_p(p, 0755);
        if (r < 0)
                return log_error_errno(r, "Failed to create '%s': %m", p);
 
        _cleanup_(rmdir_and_freep) char *q = TAKE_PTR(p);
 
        /* Mount the "unix export" directory really tiny, just 64 inodes. We mark the superblock writable
         * (since the container shall bind sockets into it). */
        r = mount_nofollow_verbose(
                        LOG_ERR,
                        "tmpfs",
                        q,
                        "tmpfs",
                        MS_NODEV|MS_NOEXEC|MS_NOSUID|MS_NOSYMFOLLOW,
                        "size=4M,nr_inodes=64,mode=0755");
        if (r < 0)
                return r;
 
        _cleanup_(umount_and_rmdir_and_freep) char *w = TAKE_PTR(q);
 
        /* After creating the superblock we change the bind mount to be read-only. This means that the fs
         * itself is writable, but not through the mount accessible from the host. */
        r = mount_nofollow_verbose(
                        LOG_ERR,
                        /* what= */ NULL,
                        w,
                        /* fstype= */ NULL,
                        MS_BIND|MS_REMOUNT|MS_RDONLY|MS_NODEV|MS_NOEXEC|MS_NOSUID|MS_NOSYMFOLLOW,
                        /* options= */ NULL);
        if (r < 0)
                return r;
 
        *ret = TAKE_PTR(w);
        return 0;
}
 
static int setup_unix_export_host_inside(const char *directory, const char *unix_export_path) {
        int r;
 
        assert(directory);
 
        if (arg_userns_mode == USER_NAMESPACE_MANAGED)
                return 0;
 
        assert(unix_export_path);
 
        r = make_run_host(directory);
        if (r < 0)
                return r;
 
        _cleanup_free_ char *p = path_join(directory, "run/host/unix-export");
        if (!p)
                return log_oom();
 
        if (mkdir(p, 0755) < 0)
                return log_error_errno(errno, "Failed to create '%s': %m", p);
 
        r = mount_nofollow_verbose(
                        LOG_ERR,
                        unix_export_path,
                        p,
                        /* fstype= */ NULL,
                        MS_BIND,
                        /* options= */ NULL);
        if (r < 0)
                return r;
 
        r = mount_nofollow_verbose(
                        LOG_ERR,
                        /* what= */ NULL,
                        p,
                        /* fstype= */ NULL,
                        MS_BIND|MS_REMOUNT|MS_NODEV|MS_NOEXEC|MS_NOSUID|MS_NOSYMFOLLOW,
                        /* options= */ NULL);
        if (r < 0)
                return r;
 
        r = userns_lchown(p, 0, 0);
        if (r < 0)
                return log_error_errno(r, "Failed to chown '%s': %m", p);
 
        return 0;
}
 
static DissectImageFlags determine_dissect_image_flags(void) {
        return
                DISSECT_IMAGE_GENERIC_ROOT |
                DISSECT_IMAGE_REQUIRE_ROOT |
                DISSECT_IMAGE_RELAX_VAR_CHECK |
                DISSECT_IMAGE_USR_NO_ROOT |
                DISSECT_IMAGE_DISCARD_ON_LOOP |
                DISSECT_IMAGE_ADD_PARTITION_DEVICES |
                DISSECT_IMAGE_PIN_PARTITION_DEVICES |
                (arg_read_only ? DISSECT_IMAGE_READ_ONLY : DISSECT_IMAGE_FSCK|DISSECT_IMAGE_GROWFS) |
                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY |
                DISSECT_IMAGE_VERITY_SHARE |
                (arg_console_mode == CONSOLE_INTERACTIVE && arg_ask_password ? DISSECT_IMAGE_ALLOW_INTERACTIVE_AUTH : 0) |
                ((arg_userns_ownership == USER_NAMESPACE_OWNERSHIP_FOREIGN) ? DISSECT_IMAGE_FOREIGN_UID :
                 (arg_userns_ownership != USER_NAMESPACE_OWNERSHIP_AUTO) ? DISSECT_IMAGE_IDENTITY_UID : 0);
}
 
static int outer_child(
                Barrier *barrier,
                const char *directory,
                int mount_fd,
                DissectedImage *dissected_image,
                MStack *mstack,
                int fd_outer_socket,
                int fd_inner_socket,
                FDSet *fds,
                char **fdnames,
                int netns_fd,
                const char *unix_export_path) {
 
        _cleanup_strv_free_ char **os_release_pairs = NULL;
        bool idmap = false;
        ssize_t l;
        int r;
 
        /* This is the "outer" child process, i.e the one forked off by the container manager itself.  Its
         * namespace situation is:
         *
         *  - CLONE_NEWUSER : if not in USER_NAMESPACE_MANAGED mode: still in the host's
         *                    if USER_NAMESPACE_MANAGED mode: already has its own (created by nsresource_allocate_userns()->setns(userns_fd))
         *  - CLONE_NEWNS   : already has its own (created by clone() if not USER_NAMESPACE_MANAGED, or unshare() otherwise)
         *  - CLONE_NEWPID  : still in the host's
         *  - CLONE_NEWUTS  : still in the host's
         *  - CLONE_NEWIPC  : still in the host's
         *  - CLONE_NEWNET  : still in the host's
         *
         * After it completed a number of initializations a second child (the "inner" one) is forked off it,
         * and it exits. */
 
        assert(barrier);
        assert(directory);
        assert(fd_outer_socket >= 0);
        assert(fd_inner_socket >= 0);
 
        log_debug("Outer child is initializing.");
 
        r = load_os_release_pairs_with_prefix("/", "container_host_", &os_release_pairs);
        if (r < 0)
                log_debug_errno(r, "Failed to read os-release from host for container, ignoring: %m");
 
        r = prctl_safe(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
        if (r < 0)
                return log_error_errno(r, "PR_SET_PDEATHSIG failed: %m");
 
        r = reset_audit_loginuid();
        if (r < 0)
                return r;
 
        /* Mark everything as slave, so that we still receive mounts from the real root, but don't propagate
         * mounts to the real root. */
        r = mount_follow_verbose(LOG_ERR, NULL, "/", NULL, MS_SLAVE|MS_REC, NULL);
        if (r < 0)
                return r;
 
        /* Put the root dir into the target directory now. One of three mechanisms is provided: either we
         * have a single mount fd (typically unprivileged --directory= mode) or we have a fully dissected
         * image (--image= mode), or we have a regular path. */
        if (mount_fd >= 0) {
                assert(arg_directory);
                assert(!arg_image);
                assert(!arg_mstack);
 
                if (move_mount(mount_fd, "", AT_FDCWD, directory, MOVE_MOUNT_F_EMPTY_PATH) < 0)
                        return log_error_errno(errno, "Failed to attach root directory: %m");
 
                mount_fd = safe_close(mount_fd);
                log_debug("Successfully attached root directory to '%s'.", directory);
 
        } else if (dissected_image) {
                assert(!arg_directory);
                assert(arg_image);
                assert(!arg_mstack);
 
                /* If we are operating on a disk image, then mount its root directory now, but leave out the
                 * rest. We can read the UID shift from it if we need to. Further down we'll mount the rest,
                 * but then with the uid shift known. That way we can mount VFAT file systems shifted to the
                 * right place right away. This makes sure ESP partitions and userns are compatible. */
 
                r = dissected_image_mount_and_warn(
                                dissected_image,
                                directory,
                                arg_uid_shift,
                                arg_uid_range,
                                /* userns_fd= */ -EBADF,
                                determine_dissect_image_flags()|
                                DISSECT_IMAGE_MOUNT_ROOT_ONLY|