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 4001–5000 of 6,878

                                (arg_start_mode == START_BOOT ? DISSECT_IMAGE_VALIDATE_OS : 0));
                if (r < 0)
                        return r;
 
        } else if (arg_mstack) {
                assert(!arg_directory);
                assert(!arg_image);
                assert(arg_mstack);
 
                MStackFlags mstack_flags = arg_read_only ? MSTACK_RDONLY : 0;
 
                /* This creates the needed overlayfs or tmpfs, owned by our target userns. Note that we pass
                 * the target mount dir as temporary mount dir here. We after all just need some dir here
                 * that definitely exists, and the temporary mounts on it are not going to be visible
                 * outside. */
                r = mstack_make_mounts(
                                mstack,
                                /* temp_mount_dir= */ directory, /* !! */
                                mstack_flags);
                if (r < 0)
                        return log_error_errno(r, "Failed to make .mstack/ mounts: %m");
 
                /* And then attaches all mounts to the directory */
                r = mstack_bind_mounts(
                                mstack,
                                directory,
                                /* where_fd= */ -EBADF,
                                mstack_flags,
                                /* ret_root_fd= */ NULL);
                if (r < 0)
                        return log_error_errno(r, "Failed bind mount .mstack/ mounts: %m");
        } else {
                assert(arg_directory);
                assert(!arg_image);
                assert(!arg_mstack);
 
                r = mount_nofollow_verbose(LOG_ERR, arg_directory, directory, /* fstype= */ NULL, MS_BIND|MS_REC, /* options= */ NULL);
                if (r < 0)
                        return r;
        }
 
        r = determine_uid_shift(directory);
        if (r < 0)
                return r;
 
        /* If we do userns on our own, we need to chown() all files ourselves before. Otherwise, if userns is
         * off or we are in managed mode we already have the userns applied, hence don't need to chown
         * anything */
        uid_t chown_uid, chown_range;
        if (in_child_chown()) {
                chown_uid = arg_uid_shift;
                chown_range = arg_uid_range;
        } else {
                chown_uid = 0;
                chown_range = UINT32_C(0x10000);
        }
 
        if (arg_userns_mode != USER_NAMESPACE_NO) {
                _cleanup_close_ int mntns_fd = -EBADF;
 
                mntns_fd = namespace_open_by_type(NAMESPACE_MOUNT);
                if (mntns_fd < 0)
                        return log_error_errno(mntns_fd, "Failed to pin outer mount namespace: %m");
 
                l = send_one_fd(fd_outer_socket, mntns_fd, 0);
                if (l < 0)
                        return log_error_errno(l, "Failed to send outer mount namespace fd: %m");
                mntns_fd = safe_close(mntns_fd);
 
                /* Let the parent know which UID shift we read from the image */
                l = send(fd_outer_socket, &arg_uid_shift, sizeof(arg_uid_shift), MSG_NOSIGNAL);
                if (l < 0)
                        return log_error_errno(errno, "Failed to send UID shift: %m");
                if (l != sizeof(arg_uid_shift))
                        return log_error_errno(SYNTHETIC_ERRNO(EIO),
                                               "Short write while sending UID shift.");
 
                if (arg_userns_mode == USER_NAMESPACE_PICK) {
                        /* When we are supposed to pick the UID shift, the parent will check now whether the
                         * UID shift we just read from the image is available. If yes, it will send the UID
                         * shift back to us, if not it will pick a different one, and send it back to us. */
 
                        l = recv(fd_outer_socket, &arg_uid_shift, sizeof(arg_uid_shift), 0);
                        if (l < 0)
                                return log_error_errno(errno, "Failed to recv UID shift: %m");
                        if (l != sizeof(arg_uid_shift))
                                return log_error_errno(SYNTHETIC_ERRNO(EIO),
                                                       "Short read while receiving UID shift.");
 
                        if (in_child_chown())
                                chown_uid = arg_uid_shift;
                }
 
                log_full(arg_quiet ? LOG_DEBUG : LOG_INFO,
                         "Selected user namespace base " UID_FMT " and range " UID_FMT ".", arg_uid_shift, arg_uid_range);
        }
 
        /* So the whole tree is now MS_SLAVE, i.e. we'll still receive mount/umount events from the host
         * mount namespace. For the directory we are going to run our container let's turn this off, so that
         * we'll live in our own little world from now on, and propagation from the host may only happen via
         * the mount tunnel dir, or not at all. */
        r = mount_follow_verbose(LOG_ERR, NULL, directory, NULL, MS_PRIVATE|MS_REC, NULL);
        if (r < 0)
                return r;
 
        r = setup_pivot_root(
                        directory,
                        arg_pivot_root_new,
                        arg_pivot_root_old);
        if (r < 0)
                return r;
 
        r = setup_volatile_mode(
                        directory,
                        arg_volatile_mode,
                        chown_uid,
                        arg_selinux_apifs_context);
        if (r < 0)
                return r;
 
        _cleanup_(machine_bind_user_context_freep) MachineBindUserContext *bind_user_context = NULL;
        r = machine_bind_user_prepare(
                        directory,
                        arg_bind_user,
                        arg_bind_user_shell,
                        arg_bind_user_shell_copy,
                        "/run/host/home",
                        arg_bind_user_groups,
                        &bind_user_context);
        if (r < 0)
                return r;
 
        if (bind_user_context)
                FOREACH_ARRAY(bind_user, bind_user_context->data, bind_user_context->n_data) {
                        _cleanup_free_ char *sm = strdup(user_record_home_directory(bind_user->host_user));
                        if (!sm)
                                return log_oom();
 
                        _cleanup_free_ char *sd = strdup(user_record_home_directory(bind_user->payload_user));
                        if (!sd)
                                return log_oom();
 
                        if (!GREEDY_REALLOC(arg_custom_mounts, arg_n_custom_mounts + 1))
                                return log_oom();
 
                        char *options = strdup("owneridmap");
                        if (!options)
                                return log_oom();
 
                        arg_custom_mounts[arg_n_custom_mounts++] = (CustomMount) {
                                .type = CUSTOM_MOUNT_BIND,
                                .source = TAKE_PTR(sm),
                                .destination = TAKE_PTR(sd),
                                .options = TAKE_PTR(options),
                                .destination_uid = bind_user->payload_user->uid,
                        };
                }
 
        r = mount_custom(
                        directory,
                        arg_custom_mounts,
                        arg_n_custom_mounts,
                        chown_uid,
                        chown_range,
                        arg_selinux_apifs_context,
                        MOUNT_ROOT_ONLY);
        if (r < 0)
                return r;
 
        if (!IN_SET(arg_userns_mode, USER_NAMESPACE_NO, USER_NAMESPACE_MANAGED) &&
            IN_SET(arg_userns_ownership, USER_NAMESPACE_OWNERSHIP_MAP, USER_NAMESPACE_OWNERSHIP_FOREIGN, USER_NAMESPACE_OWNERSHIP_AUTO) &&
            chown_uid != 0) {
                _cleanup_strv_free_ char **dirs = NULL;
                RemountIdmapping mapping;
 
                switch (arg_userns_ownership) {
                case USER_NAMESPACE_OWNERSHIP_MAP:
                        mapping = REMOUNT_IDMAPPING_HOST_ROOT;
                        break;
 
                case USER_NAMESPACE_OWNERSHIP_FOREIGN:
                        mapping = REMOUNT_IDMAPPING_FOREIGN_WITH_HOST_ROOT;
                        break;
 
                case USER_NAMESPACE_OWNERSHIP_AUTO: {
                        struct stat st;
 
                        if (lstat(directory, &st) < 0)
                                return log_error_errno(errno, "Failed to stat() container root directory '%s': %m", directory);
 
                        r = stat_verify_directory(&st);
                        if (r < 0)
                                return log_error_errno(r, "Container root directory '%s' is not a directory: %m", directory);
 
                        mapping = uid_is_foreign(st.st_uid) ?
                                REMOUNT_IDMAPPING_FOREIGN_WITH_HOST_ROOT :
                                REMOUNT_IDMAPPING_HOST_ROOT;
                        break;
                }
 
                default:
                        assert_not_reached();
                }
 
                if (arg_volatile_mode != VOLATILE_YES) {
                        r = strv_extend(&dirs, directory);
                        if (r < 0)
                                return log_oom();
                }
 
                if ((dissected_image && dissected_image->partitions[PARTITION_USR].found) ||
                    arg_volatile_mode == VOLATILE_YES) {
                        char *s = path_join(directory, "/usr");
                        if (!s)
                                return log_oom();
 
                        r = strv_consume(&dirs, s);
                        if (r < 0)
                                return log_oom();
                }
 
                r = remount_idmap(
                                dirs,
                                chown_uid,
                                chown_range,
                                /* source_owner= */ UID_INVALID,
                                /* dest_owner= */ UID_INVALID,
                                mapping);
                if (r == -EINVAL || ERRNO_IS_NEG_NOT_SUPPORTED(r)) {
                        /* This might fail because the kernel or file system doesn't support idmapping. We
                         * can't really distinguish this nicely, nor do we have any guarantees about the
                         * error codes we see, could be EOPNOTSUPP or EINVAL. */
                        if (arg_userns_ownership != USER_NAMESPACE_OWNERSHIP_AUTO)
                                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
                                                       "ID mapped mounts are apparently not available, sorry.");
 
                        log_debug("ID mapped mounts are apparently not available on this kernel or for the selected file system, reverting to recursive chown()ing.");
                        arg_userns_ownership = USER_NAMESPACE_OWNERSHIP_CHOWN;
                } else if (r < 0)
                        return log_error_errno(r, "Failed to set up ID mapped mounts: %m");
                else {
                        log_debug("ID mapped mounts available, making use of them.");
                        idmap = true;
                }
        }
 
        r = setup_volatile_mode_after_remount_idmap(
                        directory,
                        arg_volatile_mode,
                        chown_uid,
                        arg_selinux_apifs_context);
        if (r < 0)
                return r;
 
        if (dissected_image) {
                /* Now we know the uid shift, let's now mount everything else that might be in the image. */
                r = dissected_image_mount_and_warn(
                                dissected_image,
                                directory,
                                chown_uid,
                                chown_range,
                                /* userns_fd= */ -EBADF,
                                determine_dissect_image_flags()|
                                DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY|
                                (idmap ? DISSECT_IMAGE_MOUNT_IDMAPPED : 0));
                if (r < 0)
                        return r;
        }
 
        r = recursive_chown(directory, chown_uid, chown_range);
        if (r < 0)
                return r;
 
        r = base_filesystem_create(directory, chown_uid, (gid_t) chown_uid);
        if (r < 0)
                return r;
 
        if (arg_read_only && arg_volatile_mode == VOLATILE_NO &&
            !has_custom_root_mount(arg_custom_mounts, arg_n_custom_mounts)) {
                r = bind_remount_recursive(directory, MS_RDONLY, MS_RDONLY, NULL);
                if (r < 0)
                        return log_error_errno(r, "Failed to make tree read-only: %m");
        }
 
        r = mount_all(directory,
                      arg_mount_settings,
                      chown_uid,
                      arg_selinux_apifs_context);
        if (r < 0)
                return r;
 
        r = copy_devnodes(directory);
        if (r < 0)
                return r;
 
        r = make_extra_nodes(directory);
        if (r < 0)
                return r;
 
        (void) dev_setup(directory, chown_uid, chown_uid);
 
        _cleanup_free_ char *p = path_join(directory, "/run/host");
        if (!p)
                return log_oom();
 
        (void) make_inaccessible_nodes(p, chown_uid, chown_uid);
 
        r = setup_boot_id_file(directory);
        if (r < 0)
                return r;
 
        r = setup_kmsg_fifo(directory);
        if (r < 0)
                return r;
 
        r = setup_unix_export_host_inside(directory, unix_export_path);
        if (r < 0)
                return r;
 
        r = setup_pts(directory, chown_uid);
        if (r < 0)
                return r;
 
        r = mount_tunnel_dig(directory);
        if (r < 0)
                return r;
 
        r = setup_keyring();
        if (r < 0)
                return r;
 
        r = setup_credentials(directory);
        if (r < 0)
                return r;
 
        r = bind_user_setup(bind_user_context, directory);
        if (r < 0)
                return r;
 
        r = mount_custom(
                        directory,
                        arg_custom_mounts,
                        arg_n_custom_mounts,
                        chown_uid,
                        chown_range,
                        arg_selinux_apifs_context,
                        MOUNT_NON_ROOT_ONLY);
        if (r < 0)
                return r;
 
        r = setup_timezone(directory);
        if (r < 0)
                return r;
 
        r = setup_resolv_conf(directory);
        if (r < 0)
                return r;
 
        r = setup_machine_id(directory);
        if (r < 0)
                return r;
 
        r = setup_journal(directory, chown_uid, chown_range);
        if (r < 0)
                return r;
 
        r = setup_varlink_socket(directory, "io.systemd.NamespaceResource");
        if (r < 0)
                return r;
 
        r = setup_varlink_socket(directory, "io.systemd.MountFileSystem");
        if (r < 0)
                return r;
 
        /* The same stuff as the $container env var, but nicely readable for the entire payload */
        free(p);
        p = path_join(directory, "/run/host/container-manager");
        if (!p)
                return log_oom();
 
        (void) write_string_file(p, arg_container_service_name, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MODE_0444);
 
        /* The same stuff as the $container_uuid env var */
        free(p);
        p = path_join(directory, "/run/host/container-uuid");
        if (!p)
                return log_oom();
 
        (void) write_string_filef(p, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MODE_0444, SD_ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(arg_uuid));
 
        if (!arg_use_cgns) {
                r = mount_cgroups(directory, /* accept_existing= */ true);
                if (r < 0)
                        return r;
        }
 
        /* We have different codepaths here for privileged and non-privileged mode. In privileged mode we'll
         * now switch into the target directory, and then do the final setup from there. If a user namespace
         * is then allocated for the container, the root mount and everything else will be out of reach for
         * it. For unprivileged containers we cannot do that however, since we couldn't mount a sysfs and
         * procfs then anymore, since that only works if there's an unobstructed instance currently
         * visible. Hence there we do it the other way round: we first allocate a new set of namespaces
         * (and fork for it) for which we then mount sysfs/procfs, and only then switch root. */
 
        _cleanup_close_ int notify_fd = -EBADF;
        if (arg_userns_mode != USER_NAMESPACE_MANAGED) {
                /* Mark everything as shared so our mounts get propagated down. This is required to make new
                 * bind mounts available in systemd services inside the container that create a new mount
                 * namespace.  See https://github.com/systemd/systemd/issues/3860 Further submounts (such as
                 * /dev/) done after this will inherit the shared propagation mode.
                 *
                 * IMPORTANT: Do not overmount the root directory anymore from now on to enable moving the root
                 * directory mount to root later on.
                 * https://github.com/systemd/systemd/issues/3847#issuecomment-562735251
                 */
                r = mount_switch_root(directory, MS_SHARED);
                if (r < 0)
                        return log_error_errno(r, "Failed to move root directory: %m");
 
                /* We finished setting up the rootfs which is a shared mount. The mount tunnel needs to be a
                 * dependent mount otherwise we can't MS_MOVE mounts that were propagated from the host into
                 * the container. */
                r = mount_tunnel_open();
                if (r < 0)
                        return r;
 
                if (arg_userns_mode != USER_NAMESPACE_NO) {
                        /* In order to mount procfs and sysfs in an unprivileged container the kernel
                         * requires that a fully visible instance is already present in the target mount
                         * namespace. Mount one here so the inner child can mount its own instances. Later
                         * we umount the temporary instances created here before we actually exec the
                         * payload. Since the rootfs is shared the umount will propagate into the container.
                         * Note, the inner child wouldn't be able to unmount the instances on its own since
                         * it doesn't own the originating mount namespace. IOW, the outer child needs to do
                         * this. */
                        r = pin_fully_visible_api_fs();
                        if (r < 0)
                                return r;
                }
 
                notify_fd = setup_notify_child(NULL);
        } else
                notify_fd = setup_notify_child(directory);
        if (notify_fd < 0)
                return notify_fd;
 
        /* Join the external network namespace first, while we are still in the parent's
         * user namespace and have CAP_SYS_ADMIN there. Once we clone with CLONE_NEWUSER,
         * the child will be in a new user namespace, lacking the capabilities in the
         * parent user namespace required to join its network namespace. */
        if (arg_network_namespace_path && setns(netns_fd, CLONE_NEWNET) < 0)
                return log_error_errno(errno, "Failed to join network namespace: %m");
 
        pid_t pid = raw_clone(SIGCHLD|CLONE_NEWNS|
                        arg_clone_ns_flags |
                        (IN_SET(arg_userns_mode, USER_NAMESPACE_FIXED, USER_NAMESPACE_PICK) ? CLONE_NEWUSER : 0) |
                        ((arg_private_network && arg_userns_mode == USER_NAMESPACE_MANAGED) ? CLONE_NEWNET : 0));
        if (pid < 0)
                return log_error_errno(errno, "Failed to fork inner child: %m");
        if (pid == 0) {
                fd_outer_socket = safe_close(fd_outer_socket);
 
                /* In the child refuse dlopen(), so that we never mix shared libraries from payload and parent */
                block_dlopen();
 
                /* The inner child has all namespaces that are requested, so that we all are owned by the
                 * user if user namespaces are turned on. */
 
                if (arg_userns_mode == USER_NAMESPACE_MANAGED) {
                        /* In managed usernamespace operation, sysfs + procfs are special, we'll have to
                         * mount them inside the inner namespaces, but before we switch root. Hence do so
                         * here. */
                        _cleanup_free_ char *j = path_join(directory, "/proc");
                        if (!j)
                                return log_oom();
 
                        r = mount_follow_verbose(LOG_ERR, "proc", j, "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
                        if (r < 0)
                                return r;
 
                        r = mount_sysfs(directory, arg_mount_settings);
                        if (r < 0)
                                return r;
 
                        r = mount_switch_root(directory, MS_SHARED);
                        if (r < 0)
                                return log_error_errno(r, "Failed to move root directory: %m");
                }
 
                r = inner_child(barrier, fd_inner_socket, fds, fdnames, os_release_pairs);
                if (r < 0)
                        _exit(EXIT_FAILURE);
 
                _exit(EXIT_SUCCESS);
        }
 
        l = send(fd_outer_socket, &pid, sizeof(pid), MSG_NOSIGNAL);
        if (l < 0)
                return log_error_errno(errno, "Failed to send PID: %m");
        if (l != sizeof(pid))
                return log_error_errno(SYNTHETIC_ERRNO(EIO),
                                       "Short write while sending PID.");
 
        l = send(fd_outer_socket, &arg_uuid, sizeof(arg_uuid), MSG_NOSIGNAL);
        if (l < 0)
                return log_error_errno(errno, "Failed to send machine ID: %m");
        if (l != sizeof(arg_uuid))
                return log_error_errno(SYNTHETIC_ERRNO(EIO),
                                       "Short write while sending machine ID.");
 
        l = send_one_fd(fd_outer_socket, notify_fd, 0);
        if (l < 0)
                return log_error_errno(l, "Failed to send notify fd: %m");
 
        fd_outer_socket = safe_close(fd_outer_socket);
        fd_inner_socket = safe_close(fd_inner_socket);
        netns_fd = safe_close(netns_fd);
 
        return 0;
}
 
static int uid_shift_pick(uid_t *shift, LockFile *ret_lock_file) {
        bool tried_hashed = false;
        unsigned n_tries = 100;
        uid_t candidate;
        int r;
 
        assert(shift);
        assert(ret_lock_file);
        assert(arg_userns_mode == USER_NAMESPACE_PICK);
        assert(arg_uid_range == 0x10000U);
 
        candidate = *shift;
 
        (void) mkdir("/run/systemd/nspawn-uid", 0755);
 
        for (;;) {
                char lock_path[STRLEN("/run/systemd/nspawn-uid/") + DECIMAL_STR_MAX(uid_t) + 1];
                _cleanup_(release_lock_file) LockFile lf = LOCK_FILE_INIT;
 
                if (--n_tries <= 0)
                        return -EBUSY;
 
                if (candidate < CONTAINER_UID_BASE_MIN || candidate > CONTAINER_UID_BASE_MAX)
                        goto next;
                if ((candidate & UINT32_C(0xFFFF)) != 0)
                        goto next;
 
                xsprintf(lock_path, "/run/systemd/nspawn-uid/" UID_FMT, candidate);
                r = make_lock_file(lock_path, LOCK_EX|LOCK_NB, &lf);
                if (r == -EBUSY) /* Range already taken by another nspawn instance */
                        goto next;
                if (r < 0)
                        return r;
 
                /* Make some superficial checks whether the range is currently known in the user database */
                if (getpwuid_malloc(candidate, /* ret= */ NULL) >= 0)
                        goto next;
                if (getpwuid_malloc(candidate + UINT32_C(0xFFFE), /* ret= */ NULL) >= 0)
                        goto next;
                if (getgrgid_malloc(candidate, /* ret= */ NULL) >= 0)
                        goto next;
                if (getgrgid_malloc(candidate + UINT32_C(0xFFFE), /* ret= */ NULL) >= 0)
                        goto next;
 
                *ret_lock_file = lf;
                lf = (struct LockFile) LOCK_FILE_INIT;
                *shift = candidate;
                return 0;
 
        next:
                if (arg_machine && !tried_hashed) {
                        /* Try to hash the base from the container name */
 
                        static const uint8_t hash_key[] = {
                                0xe1, 0x56, 0xe0, 0xf0, 0x4a, 0xf0, 0x41, 0xaf,
                                0x96, 0x41, 0xcf, 0x41, 0x33, 0x94, 0xff, 0x72
                        };
 
                        candidate = (uid_t) siphash24(arg_machine, strlen(arg_machine), hash_key);
 
                        tried_hashed = true;
                } else
                        random_bytes(&candidate, sizeof(candidate));
 
                candidate = (candidate % (CONTAINER_UID_BASE_MAX - CONTAINER_UID_BASE_MIN)) + CONTAINER_UID_BASE_MIN;
                candidate &= (uid_t) UINT32_C(0xFFFF0000);
        }
}
 
static int setup_uid_map(
                const PidRef *pid,
                const uid_t bind_user_uid[],
                size_t n_bind_user_uid) {
 
        char uid_map[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(uid_t) + 1];
        _cleanup_free_ char *s = NULL;
        int r;
 
        assert(pidref_is_set(pid));
        assert(pid->pid > 1);
 
        if (asprintf(&s, "0 " UID_FMT " " UID_FMT "\n", arg_uid_shift, arg_uid_range) < 0)
                return log_oom();
 
        xsprintf(uid_map, "/proc/" PID_FMT "/uid_map", pid->pid);
        r = write_string_file(uid_map, s, WRITE_STRING_FILE_DISABLE_BUFFER);
        if (r < 0)
                return log_error_errno(r, "Failed to write UID map: %m");
 
        xsprintf(uid_map, "/proc/" PID_FMT "/gid_map", pid->pid);
        r = write_string_file(uid_map, s, WRITE_STRING_FILE_DISABLE_BUFFER);
        if (r < 0)
                return log_error_errno(r, "Failed to write GID map: %m");
 
        return 0;
}
 
static bool fd_store_forwarding_enabled(void) {
        /* We can only forward fd-store related messages if we have an upstream supervisor (NOTIFY_SOCKET)
         * with an fd store available (FDSTORE). */
        return getenv("NOTIFY_SOCKET") && fdstore_detected();
}
 
static int forward_fd_store(char **tags, FDSet *fds) {
        int r;
 
        /* Forward fd-store related messages to our own service manager, so that file descriptors stored
         * by the inner payload propagate up the chain and are preserved across restarts.
         *
         * Forwarded entries are namespaced with a "payload-" prefix on their FDNAME so that they
         * cannot collide with fd-store entries that nspawn itself might want to push to its own
         * upstream supervisor (the container payload and nspawn share a single upstream fdstore
         * namespace, since there's only one init system per container). */
        if (!fd_store_forwarding_enabled())
                return 0;
 
        if (strv_contains(tags, "FDSTOREREMOVE=1")) {
                const char *fdname = strv_find_startswith(tags, "FDNAME=");
                if (!fdname)
                        return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
                                                 "Got FDSTOREREMOVE=1 from container payload without FDNAME=, ignoring.");
                if (!fdname_is_valid(fdname))
                        return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
                                                 "Got FDSTOREREMOVE=1 from container payload with invalid FDNAME='%s', ignoring.",
                                                 fdname);
 
                r = sd_notifyf(/* unset_environment= */ false,
                               "FDSTOREREMOVE=1\nFDNAME=payload-%s", fdname);
                if (r < 0)
                        return log_warning_errno(r, "Failed to forward FDSTOREREMOVE upstream, ignoring: %m");
        } else if (strv_contains(tags, "FDSTORE=1")) {
                if (fdset_isempty(fds)) {
                        log_debug("Got FDSTORE=1 from container payload without any attached file descriptors, ignoring.");
                        return 0;
                }
 
                _cleanup_free_ int *fds_array = NULL;
                int n;
 
                n = fdset_to_array(fds, &fds_array);
                if (n < 0)
                        return log_warning_errno(n, "Failed to convert fdset to array, ignoring FDSTORE forward: %m");
 
                const char *fdname = strv_find_startswith(tags, "FDNAME=");
                bool fdpoll_off = strv_contains(tags, "FDPOLL=0");
                _cleanup_free_ char *msg = NULL;
                unsigned n_fds = (unsigned) n;
 
                if (fdname && !fdname_is_valid(fdname)) {
                        log_warning("Got FDSTORE=1 from container payload with invalid FDNAME='%s', ignoring name.", fdname);
                        fdname = NULL;
                }
 
                if (asprintf(&msg, "FDSTORE=1\nFDNAME=payload-%s%s%s",
                             fdname ?: "stored",
                             fdpoll_off ? "\nFDPOLL=" : "",
                             fdpoll_off ? "0" : "") < 0)
                        return log_oom();
 
                r = sd_pid_notify_with_fds(
                                0,
                                /* unset_environment= */ false,
                                msg,
                                fds_array,
                                n_fds);
                if (r < 0)
                        return log_warning_errno(r, "Failed to forward FDSTORE upstream, ignoring: %m");
        }
 
        return 0;
}
 
static int nspawn_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
        PidRef *inner_child_pid = ASSERT_PTR(userdata);
        int r;
 
        assert(userdata);
 
        _cleanup_(pidref_done) PidRef sender_pid = PIDREF_NULL;
        _cleanup_strv_free_ char **tags = NULL;
        _cleanup_(fdset_freep) FDSet *fds = NULL;
        r = notify_recv_with_fds_strv(fd, &tags, /* ret_ucred= */ NULL, &sender_pid, &fds);
        if (r == -EAGAIN)
                return 0;
        if (r < 0)
                return r;
 
        if (!pidref_equal(&sender_pid, inner_child_pid)) {
                log_debug("Received notify message from process that is not the payload's PID 1. Ignoring.");
                return 0;
        }
 
        if (DEBUG_LOGGING) {
                _cleanup_free_ char *joined = strv_join(tags, " ");
 
                if (joined) {
                        _cleanup_free_ char *j = cescape(joined);
                        free_and_replace(joined, j);
                }
 
                log_debug("Got sd_notify() message: %s", strnull(joined));
        }
 
        char *status = strv_find_startswith(tags, "STATUS=");
        if (status)
                (void) sd_notifyf(/* unset_environment= */ false, "STATUS=Container running: %s", status);
 
        if (strv_contains(tags, "READY=1")) {
                r = sd_notify(/* unset_environment= */ false, "READY=1\n");
                if (r < 0)
                        log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
 
                if (!status)
                        (void) sd_notifyf(/* unset_environment= */ false, "STATUS=Container running.");
        }
 
        (void) forward_fd_store(tags, fds);
 
        return 0;
}
 
static int setup_notify_parent(sd_event *event, int fd, PidRef *inner_child_pid, sd_event_source **notify_event_source) {
        int r;
 
        assert(notify_event_source);
 
        if (fd < 0)
                return 0;
 
        r = sd_event_add_io(event, notify_event_source, fd, EPOLLIN, nspawn_dispatch_notify_fd, inner_child_pid);
        if (r < 0)
                return log_error_errno(r, "Failed to allocate notify event source: %m");
 
        (void) sd_event_source_set_description(*notify_event_source, "nspawn-notify");
 
        return 0;
}
 
static int ptyfwd_hotkey(PTYForward *f, char c, void *userdata) {
        PidRef *pid = ASSERT_PTR(userdata);
        const char *word;
        int sig = 0, r;
 
        assert(f);
 
        switch (c) {
        case 'p':
                sig = SIGRTMIN+4;
                word = "power off";
                break;
 
        case 'r':
                sig = SIGRTMIN+5;
                word = "reboot";
                break;
 
        default:
                log_info("Unknown hotkey sequence ^]^]%c, ignoring.", c);
                return 0;
        }
 
        r = pidref_kill(pid, sig);
        if (r < 0)
                log_error_errno(r, "Failed to send %s (%s request) to PID 1 of container: %m", signal_to_string(sig), word);
        else
                log_info("Sent %s (%s request) to PID 1 of container.", signal_to_string(sig), word);
 
        return 0;
}
 
static int merge_settings(Settings *settings, const char *path) {
        int rl;
 
        assert(settings);
        assert(path);
 
        /* Copy over bits from the settings, unless they have been explicitly masked by command line switches. Note
         * that this steals the fields of the Settings* structure, and hence modifies it. */
 
        if ((arg_settings_mask & SETTING_START_MODE) == 0 &&
            settings->start_mode >= 0) {
                arg_start_mode = settings->start_mode;
                strv_free_and_replace(arg_parameters, settings->parameters);
        }
 
        if ((arg_settings_mask & SETTING_EPHEMERAL) == 0 &&
            settings->ephemeral >= 0) {
 
                if (!arg_settings_trusted)
                        log_warning("Ignoring ephemeral setting, file %s is not trusted.", path);
                else
                        arg_ephemeral = settings->ephemeral;
        }
 
        if ((arg_settings_mask & SETTING_DIRECTORY) == 0 &&
            settings->root) {
 
                if (!arg_settings_trusted)
                        log_warning("Ignoring root directory setting, file %s is not trusted.", path);
                else
                        free_and_replace(arg_directory, settings->root);
        }
 
        if ((arg_settings_mask & SETTING_PIVOT_ROOT) == 0 &&
            settings->pivot_root_new) {
                free_and_replace(arg_pivot_root_new, settings->pivot_root_new);
                free_and_replace(arg_pivot_root_old, settings->pivot_root_old);
        }
 
        if ((arg_settings_mask & SETTING_WORKING_DIRECTORY) == 0 &&
            settings->working_directory)
                free_and_replace(arg_chdir, settings->working_directory);
 
        if ((arg_settings_mask & SETTING_ENVIRONMENT) == 0 &&
            settings->environment)
                strv_free_and_replace(arg_setenv, settings->environment);
 
        if ((arg_settings_mask & SETTING_USER) == 0) {
 
                if (settings->user)
                        free_and_replace(arg_user, settings->user);
 
                if (uid_is_valid(settings->uid))
                        arg_uid = settings->uid;
                if (gid_is_valid(settings->gid))
                        arg_gid = settings->gid;
                if (settings->n_supplementary_gids > 0) {
                        free_and_replace(arg_supplementary_gids, settings->supplementary_gids);
                        arg_n_supplementary_gids = settings->n_supplementary_gids;
                }
        }
 
        if ((arg_settings_mask & SETTING_CAPABILITY) == 0) {
                uint64_t plus, minus;
                uint64_t network_minus = 0;
                uint64_t ambient;
 
                /* Note that we copy both the simple plus/minus caps here, and the full quintet from the
                 * Settings structure */
 
                plus = settings->capability;
                minus = settings->drop_capability;
 
                if ((arg_settings_mask & SETTING_NETWORK) == 0 &&
                    settings_network_configured(settings)) {
                        if (settings_private_network(settings))
                                plus |= UINT64_C(1) << CAP_NET_ADMIN;
                        else
                                network_minus |= UINT64_C(1) << CAP_NET_ADMIN;
                }
 
                if (!arg_settings_trusted && plus != 0) {
                        if (settings->capability != 0)
                                log_warning("Ignoring Capability= setting, file %s is not trusted.", path);
                } else {
                        arg_caps_retain &= ~network_minus;
                        arg_caps_retain |= plus;
                }
 
                arg_caps_retain &= ~minus;
 
                /* Copy the full capabilities over too */
                if (capability_quintet_is_set(&settings->full_capabilities)) {
                        if (!arg_settings_trusted)
                                log_warning("Ignoring capability settings, file %s is not trusted.", path);
                        else
                                arg_full_capabilities = settings->full_capabilities;
                }
 
                ambient = settings->ambient_capability;
                if (!arg_settings_trusted && ambient != 0)
                        log_warning("Ignoring AmbientCapability= setting, file %s is not trusted.", path);
                else
                        arg_caps_ambient |= ambient;
        }
 
        if ((arg_settings_mask & SETTING_KILL_SIGNAL) == 0 &&
            settings->kill_signal > 0)
                arg_kill_signal = settings->kill_signal;
 
        if ((arg_settings_mask & SETTING_PERSONALITY) == 0 &&
            settings->personality != PERSONALITY_INVALID)
                arg_personality = settings->personality;
 
        if ((arg_settings_mask & SETTING_MACHINE_ID) == 0 &&
            !sd_id128_is_null(settings->machine_id)) {
 
                if (!arg_settings_trusted)
                        log_warning("Ignoring MachineID= setting, file %s is not trusted.", path);
                else
                        arg_uuid = settings->machine_id;
        }
 
        if ((arg_settings_mask & SETTING_READ_ONLY) == 0 &&
            settings->read_only >= 0)
                arg_read_only = settings->read_only;
 
        if ((arg_settings_mask & SETTING_VOLATILE_MODE) == 0 &&
            settings->volatile_mode != _VOLATILE_MODE_INVALID)
                arg_volatile_mode = settings->volatile_mode;
 
        if ((arg_settings_mask & SETTING_CUSTOM_MOUNTS) == 0 &&
            settings->n_custom_mounts > 0) {
 
                if (!arg_settings_trusted)
                        log_warning("Ignoring TemporaryFileSystem=, Bind= and BindReadOnly= settings, file %s is not trusted.", path);
                else {
                        custom_mount_free_all(arg_custom_mounts, arg_n_custom_mounts);
                        arg_custom_mounts = TAKE_PTR(settings->custom_mounts);
                        arg_n_custom_mounts = settings->n_custom_mounts;
                        settings->n_custom_mounts = 0;
                }
        }
 
        if ((arg_settings_mask & SETTING_NETWORK) == 0 &&
            settings_network_configured(settings)) {
 
                if (!arg_settings_trusted)
                        log_warning("Ignoring network settings, file %s is not trusted.", path);
                else {
                        arg_network_veth = settings_network_veth(settings);
                        arg_private_network = settings_private_network(settings);
 
                        strv_free_and_replace(arg_network_interfaces, settings->network_interfaces);
                        strv_free_and_replace(arg_network_macvlan, settings->network_macvlan);
                        strv_free_and_replace(arg_network_ipvlan, settings->network_ipvlan);
                        strv_free_and_replace(arg_network_veth_extra, settings->network_veth_extra);
 
                        free_and_replace(arg_network_bridge, settings->network_bridge);
                        free_and_replace(arg_network_zone, settings->network_zone);
 
                        free_and_replace(arg_network_namespace_path, settings->network_namespace_path);
                }
        }
 
        if ((arg_settings_mask & SETTING_EXPOSE_PORTS) == 0 &&
            settings->expose_ports) {
 
                if (!arg_settings_trusted)
                        log_warning("Ignoring Port= setting, file %s is not trusted.", path);
                else {
                        expose_port_free_all(arg_expose_ports);
                        arg_expose_ports = TAKE_PTR(settings->expose_ports);
                }
        }
 
        if ((arg_settings_mask & SETTING_USERNS) == 0 &&
            settings->userns_mode != _USER_NAMESPACE_MODE_INVALID) {
 
                if (!arg_settings_trusted)
                        log_warning("Ignoring PrivateUsers= and PrivateUsersChown= settings, file %s is not trusted.", path);
                else {
                        arg_userns_mode = settings->userns_mode;
                        arg_uid_shift = settings->uid_shift;
                        arg_uid_range = settings->uid_range;
                        arg_userns_ownership = settings->userns_ownership;
                        arg_delegate_container_ranges = settings->delegate_container_ranges;
                }
        }
 
        if ((arg_settings_mask & SETTING_BIND_USER) == 0 &&
            !strv_isempty(settings->bind_user)) {
 
                if (!arg_settings_trusted)
                        log_warning("Ignoring bind user setting, file %s is not trusted.", path);
                else
                        strv_free_and_replace(arg_bind_user, settings->bind_user);
        }
 
        if (!FLAGS_SET(arg_settings_mask, SETTING_BIND_USER_SHELL) &&
            settings->bind_user_shell_set) {
 
                if (!arg_settings_trusted)
                        log_warning("Ignoring bind user shell setting, file %s is not trusted.", path);
                else {
                        free_and_replace(arg_bind_user_shell, settings->bind_user_shell);
                        arg_bind_user_shell_copy = settings->bind_user_shell_copy;
                }
        }