HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/core/namespace.c

173,584 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: dd15bda3ac6915b01bea339ebbd5f9b2ab083c7e

Lines 4001–4291 of 4,291

        r = pidref_namespace_open(target, &pidns_fd, &mntns_fd, /* ret_netns_fd= */ NULL, /* ret_userns_fd= */ NULL, &root_fd);
        if (r < 0)
                return log_debug_errno(r, "Failed to open namespace: %m");
 
        r = is_our_namespace(mntns_fd, NAMESPACE_MOUNT);
        if (r < 0)
                return log_debug_errno(r, "Failed to check if target namespace is separate: %m");
        if (r > 0)
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Target namespace is not separate, cannot reload extensions");
 
        (void) dlopen_cryptsetup(LOG_DEBUG);
 
        extension_dir = path_join(p->private_namespace_dir, "unit-extensions");
        if (!extension_dir)
                return log_oom_debug();
 
        r = parse_env_extension_hierarchies(&hierarchies, hierarchy_env);
        if (r < 0)
                return r;
 
        r = append_extensions(
                        &ml,
                        overlay_prefix,
                        p->private_namespace_dir,
                        hierarchies,
                        p->extension_images,
                        p->n_extension_images,
                        p->extension_directories);
        if (r < 0)
                return r;
 
        sort_and_drop_unused_mounts(&ml, overlay_prefix);
        if (ml.n_mounts == 0)
                return 0;
 
        /**
         * There are three main steps:
         * 1. In child, set up the extension images and directories in a slave mountns, so that we have
         *    access to their FDs
         * 2. Fork into a grandchild, which will enter the target namespace and attempt to "unpeel" the
         *    overlays to obtain FDs the underlying directories, over which we will reapply the overlays
         * 3. In the child again, receive the FDs and reapply the overlays
         */
        r = pidref_safe_fork(
                        "(sd-ns-refresh-exts)",
                        FORK_DEATHSIG_SIGTERM|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE,
                        /* ret= */ NULL);
        if (r < 0)
                return r;
        if (r == 0) {
                /* Child (host namespace) */
                _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
                _cleanup_(pidref_done_sigkill_wait) PidRef grandchild = PIDREF_NULL;
 
                 (void) mkdir_p_label(overlay_prefix, 0555);
 
                r = refresh_apply_and_prune(p, &ml);
                if (r < 0) {
                        log_debug_errno(r, "Failed to apply extensions for refreshing: %m");
                        _exit(EXIT_FAILURE);
                }
 
                /* Create a grandchild process to handle the unmounting and reopening of hierarchy */
                r = socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, pair);
                if (r < 0) {
                        log_debug_errno(errno, "Failed to create socket pair: %m");
                        _exit(EXIT_FAILURE);
                }
 
                r = pidref_safe_fork("(sd-ns-refresh-exts-grandchild)", FORK_LOG|FORK_DEATHSIG_SIGKILL, &grandchild);
                if (r < 0)
                        _exit(EXIT_FAILURE);
                if (r == 0) {
                        /* Grandchild (target service namespace) */
                        pair[0] = safe_close(pair[0]);
 
                        r = refresh_grandchild_proc(target, &ml, overlay_prefix, pidns_fd, mntns_fd, root_fd, pair[1]);
                        if (r < 0) {
                                pair[1] = safe_close(pair[1]);
                                _exit(EXIT_FAILURE);
                        }
 
                        _exit(EXIT_SUCCESS);
                }
 
                pair[1] = safe_close(pair[1]);
 
                /* Until kernel 6.15, the FDs to the individual layers used to set up the OverlayFS via
                 * lowerdir=/proc/self/fd/X need to remain open until the OverlayFS mount is _attached_
                 * (as opposed to merely created) to its mount point, hence we need to ensure these FDs
                 * stay open until the grandchild has attached the mount and exited. */
                // TODO: once the kernel baseline is >= 6.15, move the FD array into the helper function
                // and close them immediately
                int *fd_layers = NULL;
                size_t n_fd_layers = 0;
                CLEANUP_ARRAY(fd_layers, n_fd_layers, close_many_and_free);
 
                FOREACH_ARRAY(m, ml.mounts, ml.n_mounts) {
                        r = handle_mount_from_grandchild(m, overlay_prefix, &fd_layers, &n_fd_layers, pair[0]);
                        if (r < 0)
                                _exit(EXIT_FAILURE);
                }
 
                r = pidref_wait_for_terminate_and_check("(sd-ns-refresh-exts-grandchild)", &grandchild, 0);
                if (r < 0) {
                        log_debug_errno(r, "Failed to wait for target namespace process to finish: %m");
                        _exit(EXIT_FAILURE);
                }
 
                pidref_done(&grandchild);
 
                if (r != EXIT_SUCCESS) {
                        log_debug("Target namespace fork did not succeed");
                        _exit(EXIT_FAILURE);
                }
 
                _exit(EXIT_SUCCESS);
        }
 
        return 0;
}
 
static const char *const protect_home_table[_PROTECT_HOME_MAX] = {
        [PROTECT_HOME_NO]        = "no",
        [PROTECT_HOME_YES]       = "yes",
        [PROTECT_HOME_READ_ONLY] = "read-only",
        [PROTECT_HOME_TMPFS]     = "tmpfs",
};
 
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(protect_home, ProtectHome, PROTECT_HOME_YES);
 
static const char *const protect_hostname_table[_PROTECT_HOSTNAME_MAX] = {
        [PROTECT_HOSTNAME_NO]      = "no",
        [PROTECT_HOSTNAME_YES]     = "yes",
        [PROTECT_HOSTNAME_PRIVATE] = "private",
};
 
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(protect_hostname, ProtectHostname, PROTECT_HOSTNAME_YES);
 
static const char *const protect_system_table[_PROTECT_SYSTEM_MAX] = {
        [PROTECT_SYSTEM_NO]     = "no",
        [PROTECT_SYSTEM_YES]    = "yes",
        [PROTECT_SYSTEM_FULL]   = "full",
        [PROTECT_SYSTEM_STRICT] = "strict",
};
 
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(protect_system, ProtectSystem, PROTECT_SYSTEM_YES);
 
static const char *const protect_control_groups_table[_PROTECT_CONTROL_GROUPS_MAX] = {
        [PROTECT_CONTROL_GROUPS_NO]      = "no",
        [PROTECT_CONTROL_GROUPS_YES]     = "yes",
        [PROTECT_CONTROL_GROUPS_PRIVATE] = "private",
        [PROTECT_CONTROL_GROUPS_STRICT]  = "strict",
};
 
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(protect_control_groups, ProtectControlGroups, PROTECT_CONTROL_GROUPS_YES);
 
static const char* const protect_proc_table[_PROTECT_PROC_MAX] = {
        [PROTECT_PROC_DEFAULT]    = "default",
        [PROTECT_PROC_NOACCESS]   = "noaccess",
        [PROTECT_PROC_INVISIBLE]  = "invisible",
        [PROTECT_PROC_PTRACEABLE] = "ptraceable",
};
 
DEFINE_STRING_TABLE_LOOKUP(protect_proc, ProtectProc);
 
static const char* const proc_subset_table[_PROC_SUBSET_MAX] = {
        [PROC_SUBSET_ALL] = "all",
        [PROC_SUBSET_PID] = "pid",
};
 
DEFINE_STRING_TABLE_LOOKUP(proc_subset, ProcSubset);
 
static const char* const private_bpf_table[_PRIVATE_BPF_MAX] = {
        [PRIVATE_BPF_NO]  = "no",
        [PRIVATE_BPF_YES] = "yes",
};
 
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(private_bpf, PrivateBPF, PRIVATE_BPF_YES);
 
#include "bpf-delegate-configs.inc"
 
DEFINE_STRING_TABLE_LOOKUP(bpf_delegate_cmd, uint64_t);
DEFINE_STRING_TABLE_LOOKUP(bpf_delegate_map_type, uint64_t);
DEFINE_STRING_TABLE_LOOKUP(bpf_delegate_prog_type, uint64_t);
DEFINE_STRING_TABLE_LOOKUP(bpf_delegate_attach_type, uint64_t);
 
char* bpf_delegate_to_string(uint64_t u, const char* (*parser)(uint64_t)) {
        assert(parser);
 
        if (u == UINT64_MAX)
                return strdup("any");
 
        _cleanup_free_ char *buf = NULL;
 
        BIT_FOREACH(i, u) {
                const char *s = parser(i);
                if (s) {
                        if (!strextend_with_separator(&buf, ",", s))
                                return NULL;
                } else {
                        if (strextendf_with_separator(&buf, ",", "%d", i) < 0)
                                return NULL;
                }
        }
 
        return TAKE_PTR(buf) ?: strdup("");
}
 
int bpf_delegate_from_string(const char *s, uint64_t *ret, uint64_t (*parser)(const char *)) {
        int r;
 
        assert(s);
        assert(ret);
        assert(parser);
 
        if (streq(s, "any")) {
                *ret = UINT64_MAX;
                return 0;
        }
 
        uint64_t mask = 0;
        for (;;) {
                _cleanup_free_ char *word = NULL;
 
                r = extract_first_word(&s, &word, ",", /* flags= */ 0);
                if (r < 0)
                        return log_warning_errno(r, "Failed to parse delegate options \"%s\": %m", s);
                if (r == 0)
                        break;
 
                r = parser(word);
                if (r < 0)
                        log_warning_errno(r, "Unknown BPF delegate option, ignoring: %s", word);
                else
                        mask |= UINT64_C(1) << r;
        }
 
        *ret = mask;
 
        return 0;
}
 
static const char* const private_tmp_table[_PRIVATE_TMP_MAX] = {
        [PRIVATE_TMP_NO]           = "no",
        [PRIVATE_TMP_CONNECTED]    = "connected",
        [PRIVATE_TMP_DISCONNECTED] = "disconnected",
};
 
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(private_tmp, PrivateTmp, PRIVATE_TMP_CONNECTED);
 
static const char* const private_users_table[_PRIVATE_USERS_MAX] = {
        [PRIVATE_USERS_NO]       = "no",
        [PRIVATE_USERS_SELF]     = "self",
        [PRIVATE_USERS_IDENTITY] = "identity",
        [PRIVATE_USERS_FULL]     = "full",
        [PRIVATE_USERS_MANAGED]  = "managed",
};
 
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(private_users, PrivateUsers, PRIVATE_USERS_SELF);
 
static const char* const private_pids_table[_PRIVATE_PIDS_MAX] = {
        [PRIVATE_PIDS_NO]  = "no",
        [PRIVATE_PIDS_YES] = "yes",
};
 
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(private_pids, PrivatePIDs, PRIVATE_PIDS_YES);
 
void pinned_resource_done(PinnedResource *p) {
        assert(p);
 
        p->directory_fd = safe_close(p->directory_fd);
        p->directory = mfree(p->directory);
        p->image_fd = safe_close(p->image_fd);
        p->image = mfree(p->image);
        p->mstack_loaded = mstack_free(p->mstack_loaded);
        p->mstack = mfree(p->mstack);
}
 
bool pinned_resource_is_set(const PinnedResource *p) {
        if (!p)
                return false;
 
        return p->directory_fd >= 0 ||
                p->directory ||
                p->image_fd >= 0 ||
                p->image ||
                p->mstack_loaded ||
                p->mstack;
}