HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/shared/seccomp-util.c

102,895 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: d6d619894a60f4a94f096498dbeec4aa8794dcb5

Lines 1001–2000 of 2,699

                "timerfd_gettime\0"
                "timerfd_gettime64\0"
                "timerfd_settime\0"
                "timerfd_settime64\0"
                "times\0"
        },
        [SYSCALL_FILTER_SET_KNOWN] = {
                .name = "@known",
                .help = "All known syscalls declared in the kernel",
                .value =
                "@obsolete\0"
#include "syscall-list.inc"
        },
};
 
const SyscallFilterSet *syscall_filter_set_find(const char *name) {
        if (isempty(name) || name[0] != '@')
                return NULL;
 
        for (unsigned i = 0; i < _SYSCALL_FILTER_SET_MAX; i++)
                if (streq(syscall_filter_sets[i].name, name))
                        return syscall_filter_sets + i;
 
        return NULL;
}
 
static int add_syscall_filter_set(
                scmp_filter_ctx seccomp,
                const SyscallFilterSet *set,
                uint32_t action,
                char **exclude,
                bool log_missing,
                char ***added);
 
int seccomp_add_syscall_filter_item(
                scmp_filter_ctx *seccomp,
                const char *name,
                uint32_t action,
                char **exclude,
                bool log_missing,
                char ***added) {
 
        assert(seccomp);
        assert(name);
 
        if (strv_contains(exclude, name))
                return 0;
 
        /* Any syscalls that are handled are added to the *added strv. The pointer
         * must be either NULL or point to a valid pre-initialized possibly-empty strv. */
 
        if (name[0] == '@') {
                const SyscallFilterSet *other;
 
                other = syscall_filter_set_find(name);
                if (!other)
                        return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
                                               "Filter set %s is not known!",
                                               name);
 
                return add_syscall_filter_set(seccomp, other, action, exclude, log_missing, added);
 
        } else {
                int id, r;
 
                r = dlopen_libseccomp(LOG_DEBUG);
                if (r < 0)
                        return r;
 
                id = sym_seccomp_syscall_resolve_name(name);
                if (id == __NR_SCMP_ERROR) {
                        if (log_missing)
                                log_debug("System call %s is not known, ignoring.", name);
                        return 0;
                }
 
                r = sym_seccomp_rule_add_exact(seccomp, action, id, 0);
                if (r < 0) {
                        /* If the system call is not known on this architecture, then that's fine, let's ignore it */
                        bool ignore = r == -EDOM;
 
                        if (!ignore || log_missing)
                                log_debug_errno(r, "Failed to add rule for system call %s() / %d%s: %m",
                                                name, id, ignore ? ", ignoring" : "");
                        if (!ignore)
                                return r;
                }
 
                if (added) {
                        r = strv_extend(added, name);
                        if (r < 0)
                                return r;
                }
 
                return 0;
        }
}
 
static int add_syscall_filter_set(
                scmp_filter_ctx seccomp,
                const SyscallFilterSet *set,
                uint32_t action,
                char **exclude,
                bool log_missing,
                char ***added) {
 
        int r;
 
        /* Any syscalls that are handled are added to the *added strv. It needs to be initialized. */
 
        assert(seccomp);
        assert(set);
 
        NULSTR_FOREACH(sys, set->value) {
                r = seccomp_add_syscall_filter_item(seccomp, sys, action, exclude, log_missing, added);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static uint32_t override_default_action(uint32_t default_action) {
        /* When the requested filter is an allow-list, and the default action is something critical, we
         * install ENOSYS as the default action, but it will only apply to syscalls which are not in the
         * @known set. */
 
        if (default_action == SCMP_ACT_ALLOW)
                return default_action;
 
        if (default_action == SCMP_ACT_LOG)
                return default_action;
 
        return SCMP_ACT_ERRNO(ENOSYS);
}
 
int seccomp_load_syscall_filter_set(uint32_t default_action, const SyscallFilterSet *set, uint32_t action, bool log_missing) {
        uint32_t arch, default_action_override;
        int r;
 
        assert(set);
 
        /* The one-stop solution: allocate a seccomp object, add the specified filter to it, and apply it. Once for
         * each local arch. */
 
        r = dlopen_libseccomp(LOG_DEBUG);
        if (r < 0)
                return r;
 
        default_action_override = override_default_action(default_action);
 
        SECCOMP_FOREACH_LOCAL_ARCH(arch) {
                _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
                _cleanup_strv_free_ char **added = NULL;
 
                log_trace("Operating on architecture: %s", seccomp_arch_to_string(arch));
 
                r = seccomp_init_for_arch(&seccomp, arch, default_action_override);
                if (r < 0)
                        return r;
 
                r = add_syscall_filter_set(seccomp, set, action, NULL, log_missing, &added);
                if (r < 0)
                        return log_debug_errno(r, "Failed to add filter set: %m");
 
                if (default_action != default_action_override)
                        NULSTR_FOREACH(name, syscall_filter_sets[SYSCALL_FILTER_SET_KNOWN].value) {
                                int id;
 
                                id = sym_seccomp_syscall_resolve_name(name);
                                if (id < 0)
                                        continue;
 
                                /* Ignore the syscall if it was already handled above */
                                if (strv_contains(added, name))
                                        continue;
 
                                r = sym_seccomp_rule_add_exact(seccomp, default_action, id, 0);
                                if (r < 0 && r != -EDOM)  /* EDOM means that the syscall is not available for arch */
                                        return log_debug_errno(r, "Failed to add rule for system call %s() / %d: %m",
                                                               name, id);
                        }
 
#if (SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 5) || SCMP_VER_MAJOR > 2
                /* We have a large filter here, so let's turn on the binary tree mode if possible. */
                r = sym_seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_OPTIMIZE, 2);
                if (r < 0)
                        log_warning_errno(r, "Failed to set SCMP_FLTATR_CTL_OPTIMIZE, ignoring: %m");
#endif
 
                r = sym_seccomp_load(seccomp);
                if (ERRNO_IS_NEG_SECCOMP_FATAL(r))
                        return r;
                if (r < 0)
                        log_debug_errno(r, "Failed to install filter set for architecture %s, skipping: %m",
                                        seccomp_arch_to_string(arch));
        }
 
        return 0;
}
 
int seccomp_load_syscall_filter_set_raw(uint32_t default_action, Hashmap* filter, uint32_t action, bool log_missing) {
        uint32_t arch, default_action_override;
        int r;
 
        /* Similar to seccomp_load_syscall_filter_set(), but takes a raw Hashmap* of syscalls, instead
         * of a SyscallFilterSet* table. */
 
        if (hashmap_isempty(filter) && default_action == SCMP_ACT_ALLOW)
                return 0;
 
        r = dlopen_libseccomp(LOG_DEBUG);
        if (r < 0)
                return r;
 
        default_action_override = override_default_action(default_action);
 
        SECCOMP_FOREACH_LOCAL_ARCH(arch) {
                _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
                void *syscall_id, *val;
 
                log_trace("Operating on architecture: %s", seccomp_arch_to_string(arch));
 
                r = seccomp_init_for_arch(&seccomp, arch, default_action_override);
                if (r < 0)
                        return r;
 
                HASHMAP_FOREACH_KEY(val, syscall_id, filter) {
                        uint32_t a = action;
                        int id = PTR_TO_INT(syscall_id) - 1;
                        int error = PTR_TO_INT(val);
 
                        if (error == SECCOMP_ERROR_NUMBER_KILL)
                                a = SCMP_ACT_KILL_PROCESS;
                        else if (action == SCMP_ACT_LOG)
                                a = SCMP_ACT_LOG;
                        else if (error >= 0)
                                a = SCMP_ACT_ERRNO(error);
 
                        r = sym_seccomp_rule_add_exact(seccomp, a, id, 0);
                        if (r < 0) {
                                /* If the system call is not known on this architecture, then that's
                                 * fine, let's ignore it */
                                _cleanup_free_ char *n = NULL;
                                bool ignore;
 
                                n = sym_seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE, id);
                                ignore = r == -EDOM;
                                if (!ignore || log_missing)
                                        log_debug_errno(r, "Failed to add rule for system call %s() / %d%s: %m",
                                                        strna(n), id, ignore ? ", ignoring" : "");
                                if (!ignore)
                                        return r;
                        }
                }
 
                if (default_action != default_action_override)
                        NULSTR_FOREACH(name, syscall_filter_sets[SYSCALL_FILTER_SET_KNOWN].value) {
                                int id;
 
                                id = sym_seccomp_syscall_resolve_name(name);
                                if (id < 0)
                                        continue;
 
                                /* Ignore the syscall if it was already handled above */
                                if (hashmap_contains(filter, INT_TO_PTR(id + 1)))
                                        continue;
 
                                r = sym_seccomp_rule_add_exact(seccomp, default_action, id, 0);
                                if (r < 0 && r != -EDOM)  /* EDOM means that the syscall is not available for arch */
                                        return log_debug_errno(r, "Failed to add rule for system call %s() / %d: %m",
                                                               name, id);
                        }
 
#if (SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 5) || SCMP_VER_MAJOR > 2
                /* We have a large filter here, so let's turn on the binary tree mode if possible. */
                r = sym_seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_OPTIMIZE, 2);
                if (r < 0)
                        log_warning_errno(r, "Failed to set SCMP_FLTATR_CTL_OPTIMIZE, ignoring: %m");
#endif
 
                r = sym_seccomp_load(seccomp);
                if (ERRNO_IS_NEG_SECCOMP_FATAL(r))
                        return r;
                if (r < 0)
                        log_debug_errno(r, "Failed to install system call filter for architecture %s, skipping: %m",
                                        seccomp_arch_to_string(arch));
        }
 
        return 0;
}
 
int seccomp_parse_syscall_filter(
                const char *name,
                int errno_num,
                Hashmap *filter,
                SeccompParseFlags flags,
                const char *unit,
                const char *filename,
                unsigned line) {
 
        int r;
 
        assert(name);
        assert(filter);
 
        if (!FLAGS_SET(flags, SECCOMP_PARSE_INVERT) && errno_num >= 0)
                return -EINVAL;
 
        if (name[0] == '@') {
                const SyscallFilterSet *set;
 
                set = syscall_filter_set_find(name);
                if (!set) {
                        if (!FLAGS_SET(flags, SECCOMP_PARSE_PERMISSIVE))
                                return -EINVAL;
 
                        log_syntax(unit, FLAGS_SET(flags, SECCOMP_PARSE_LOG) ? LOG_WARNING : LOG_DEBUG, filename, line, 0,
                                   "Unknown system call group, ignoring: %s", name);
                        return 0;
                }
 
                NULSTR_FOREACH(i, set->value) {
                        /* Call ourselves again, for the group to parse. Note that we downgrade logging here
                         * (i.e. take away the SECCOMP_PARSE_LOG flag) since any issues in the group table
                         * are our own problem, not a problem in user configuration data and we shouldn't
                         * pretend otherwise by complaining about them. */
                        r = seccomp_parse_syscall_filter(i, errno_num, filter, flags &~ SECCOMP_PARSE_LOG, unit, filename, line);
                        if (r < 0)
                                return r;
                }
        } else {
                int id;
 
                r = dlopen_libseccomp(LOG_DEBUG);
                if (r < 0) {
                        if (!FLAGS_SET(flags, SECCOMP_PARSE_PERMISSIVE))
                                return r;
 
                        log_syntax(unit, FLAGS_SET(flags, SECCOMP_PARSE_LOG) ? LOG_INFO : LOG_DEBUG, filename, line, r,
                                   "System call %s cannot be resolved as libseccomp is not available, ignoring: %m", name);
                        return 0;
                }
 
                id = sym_seccomp_syscall_resolve_name(name);
                if (id == __NR_SCMP_ERROR) {
                        if (!FLAGS_SET(flags, SECCOMP_PARSE_PERMISSIVE))
                                return -EINVAL;
 
                        log_syntax(unit, FLAGS_SET(flags, SECCOMP_PARSE_LOG) ? LOG_WARNING : LOG_DEBUG, filename, line, 0,
                                   "System call %s is not known, ignoring.", name);
                        return 0;
                }
 
                /* If we previously wanted to forbid a syscall and now we want to allow it, then remove it
                 * from the list. The entries in allow-list with non-negative error value will be handled
                 * with SCMP_ACT_ERRNO() instead of the default action. */
                if (!FLAGS_SET(flags, SECCOMP_PARSE_INVERT) == FLAGS_SET(flags, SECCOMP_PARSE_ALLOW_LIST) ||
                    (FLAGS_SET(flags, SECCOMP_PARSE_INVERT | SECCOMP_PARSE_ALLOW_LIST) && errno_num >= 0)) {
                        r = hashmap_put(filter, INT_TO_PTR(id + 1), INT_TO_PTR(errno_num));
                        if (r < 0)
                                switch (r) {
                                case -ENOMEM:
                                        return FLAGS_SET(flags, SECCOMP_PARSE_LOG) ? log_oom() : -ENOMEM;
                                case -EEXIST:
                                        assert_se(hashmap_update(filter, INT_TO_PTR(id + 1), INT_TO_PTR(errno_num)) == 0);
                                        break;
                                default:
                                        return r;
                                }
                } else
                        (void) hashmap_remove(filter, INT_TO_PTR(id + 1));
        }
 
        return 0;
}
 
int seccomp_restrict_namespaces(unsigned long retain) {
        uint32_t arch;
        int r;
 
        if (DEBUG_LOGGING) {
                _cleanup_free_ char *s = NULL;
 
                (void) namespace_flags_to_string(retain, &s);
                log_debug("Restricting namespace to: %s.", strna(s));
        }
 
        /* NOOP? */
        if (FLAGS_SET(retain, NAMESPACE_FLAGS_ALL))
                return 0;
 
        r = dlopen_libseccomp(LOG_DEBUG);
        if (r < 0)
                return r;
 
        SECCOMP_FOREACH_LOCAL_ARCH(arch) {
                _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
 
                log_trace("Operating on architecture: %s", seccomp_arch_to_string(arch));
 
                r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW);
                if (r < 0)
                        return r;
 
                /* We cannot filter on individual flags to clone3(), and we need to disable the
                 * syscall altogether. ENOSYS is used instead of EPERM, so that glibc and other
                 * users shall fall back to clone(), as if on an older kernel.
                 *
                 * C.f. https://github.com/flatpak/flatpak/commit/a10f52a7565c549612c92b8e736a6698a53db330,
                 * https://github.com/moby/moby/issues/42680. */
 
                r = sym_seccomp_rule_add_exact(
                                seccomp,
                                SCMP_ACT_ERRNO(ENOSYS),
                                SCMP_SYS(clone3),
                                0);
                if (r < 0)
                        log_debug_errno(r, "Failed to add clone3() rule for architecture %s, ignoring: %m",
                                        seccomp_arch_to_string(arch));
 
                if ((retain & NAMESPACE_FLAGS_ALL) == 0)
                        /* If every single kind of namespace shall be prohibited, then let's block the whole
                         * setns() syscall altogether. */
                        r = sym_seccomp_rule_add_exact(
                                        seccomp,
                                        SCMP_ACT_ERRNO(EPERM),
                                        SCMP_SYS(setns),
                                        0);
                else
                        /* Otherwise, block only the invocations with the appropriate flags in the loop
                         * below, but also the special invocation with a zero flags argument, right here. The
                         * kernel takes nstype as an int and truncates it to 32 bits, hence only compare those. */
                        r = sym_seccomp_rule_add_exact(
                                        seccomp,
                                        SCMP_ACT_ERRNO(EPERM),
                                        SCMP_SYS(setns),
                                        1,
                                        SCMP_A1(SCMP_CMP_MASKED_EQ, UINT32_MAX, 0));
                if (r < 0) {
                        log_debug_errno(r, "Failed to add setns() rule for architecture %s, skipping: %m",
                                        seccomp_arch_to_string(arch));
                        continue;
                }
 
                for (unsigned i = 0; namespace_info[i].proc_name; i++) {
                        unsigned long f;
 
                        f = namespace_info[i].clone_flag;
                        if (FLAGS_SET(retain, f)) {
                                log_debug("Permitting %s.", namespace_info[i].proc_name);
                                continue;
                        }
 
                        log_trace("Blocking %s.", namespace_info[i].proc_name);
 
                        r = sym_seccomp_rule_add_exact(
                                        seccomp,
                                        SCMP_ACT_ERRNO(EPERM),
                                        SCMP_SYS(unshare),
                                        1,
                                        SCMP_A0(SCMP_CMP_MASKED_EQ, f, f));
                        if (r < 0) {
                                log_debug_errno(r, "Failed to add unshare() rule for architecture %s, skipping: %m",
                                                seccomp_arch_to_string(arch));
                                break;
                        }
 
                        /* On s390/s390x the first two parameters to clone are switched */
                        if (!IN_SET(arch, SCMP_ARCH_S390, SCMP_ARCH_S390X))
                                r = sym_seccomp_rule_add_exact(
                                                seccomp,
                                                SCMP_ACT_ERRNO(EPERM),
                                                SCMP_SYS(clone),
                                                1,
                                                SCMP_A0(SCMP_CMP_MASKED_EQ, f, f));
                        else
                                r = sym_seccomp_rule_add_exact(
                                                seccomp,
                                                SCMP_ACT_ERRNO(EPERM),
                                                SCMP_SYS(clone),
                                                1,
                                                SCMP_A1(SCMP_CMP_MASKED_EQ, f, f));
                        if (r < 0) {
                                log_debug_errno(r, "Failed to add clone() rule for architecture %s, skipping: %m",
                                                seccomp_arch_to_string(arch));
                                break;
                        }
 
                        if ((retain & NAMESPACE_FLAGS_ALL) != 0) {
                                r = sym_seccomp_rule_add_exact(
                                                seccomp,
                                                SCMP_ACT_ERRNO(EPERM),
                                                SCMP_SYS(setns),
                                                1,
                                                SCMP_A1(SCMP_CMP_MASKED_EQ, f, f));
                                if (r < 0) {
                                        log_debug_errno(r, "Failed to add setns() rule for architecture %s, skipping: %m",
                                                        seccomp_arch_to_string(arch));
                                        break;
                                }
                        }
                }
                if (r < 0)
                        continue;
 
                r = sym_seccomp_load(seccomp);
                if (ERRNO_IS_NEG_SECCOMP_FATAL(r))
                        return r;
                if (r < 0)
                        log_debug_errno(r, "Failed to install namespace restriction rules for architecture %s, skipping: %m",
                                        seccomp_arch_to_string(arch));
        }
 
        return 0;
}
 
int seccomp_protect_sysctl(void) {
        uint32_t arch;
        int r;
 
        r = dlopen_libseccomp(LOG_DEBUG);
        if (r < 0)
                return r;
 
        SECCOMP_FOREACH_LOCAL_ARCH(arch) {
                _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
 
                log_trace("Operating on architecture: %s", seccomp_arch_to_string(arch));
 
                if (IN_SET(arch,
                           SCMP_ARCH_AARCH64,
#ifdef SCMP_ARCH_LOONGARCH64
                           SCMP_ARCH_LOONGARCH64,
#endif
#ifdef SCMP_ARCH_RISCV64
                           SCMP_ARCH_RISCV64,
#endif
                           SCMP_ARCH_X32
                          ))
                        /* No _sysctl syscall */
                        continue;
 
                r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW);
                if (r < 0)
                        return r;
 
                r = sym_seccomp_rule_add_exact(
                                seccomp,
                                SCMP_ACT_ERRNO(EPERM),
                                SCMP_SYS(_sysctl),
                                0);
                if (r < 0) {
                        log_debug_errno(r, "Failed to add _sysctl() rule for architecture %s, skipping: %m",
                                        seccomp_arch_to_string(arch));
                        continue;
                }
 
                r = sym_seccomp_load(seccomp);
                if (ERRNO_IS_NEG_SECCOMP_FATAL(r))
                        return r;
                if (r < 0)
                        log_debug_errno(r, "Failed to install sysctl protection rules for architecture %s, skipping: %m",
                                        seccomp_arch_to_string(arch));
        }
 
        return 0;
}
 
int seccomp_protect_syslog(void) {
        uint32_t arch;
        int r;
 
        r = dlopen_libseccomp(LOG_DEBUG);
        if (r < 0)
                return r;
 
        SECCOMP_FOREACH_LOCAL_ARCH(arch) {
                _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
 
                r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW);
                if (r < 0)
                        return r;
 
                r = sym_seccomp_rule_add_exact(
                                seccomp,
                                SCMP_ACT_ERRNO(EPERM),
                                SCMP_SYS(syslog),
                                0);
 
                if (r < 0) {
                        log_debug_errno(r, "Failed to add syslog() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch));
                        continue;
                }
 
                r = sym_seccomp_load(seccomp);
                if (ERRNO_IS_NEG_SECCOMP_FATAL(r))
                        return r;
                if (r < 0)
                        log_debug_errno(r, "Failed to install syslog protection rules for architecture %s, skipping: %m",
                                        seccomp_arch_to_string(arch));
        }
 
        return 0;
}
 
int seccomp_restrict_address_families(Set *address_families, bool allow_list) {
        uint32_t arch;
        int r;
 
        r = dlopen_libseccomp(LOG_DEBUG);
        if (r < 0)
                return r;
 
        SECCOMP_FOREACH_LOCAL_ARCH(arch) {
                _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
                bool supported;
 
                log_trace("Operating on architecture: %s", seccomp_arch_to_string(arch));
 
                switch (arch) {
 
                case SCMP_ARCH_X86_64:
                case SCMP_ARCH_X32:
                case SCMP_ARCH_ARM:
                case SCMP_ARCH_AARCH64:
#ifdef SCMP_ARCH_LOONGARCH64
                case SCMP_ARCH_LOONGARCH64:
#endif
                case SCMP_ARCH_MIPSEL64N32:
                case SCMP_ARCH_MIPS64N32:
                case SCMP_ARCH_MIPSEL64:
                case SCMP_ARCH_MIPS64:
#ifdef SCMP_ARCH_RISCV64
                case SCMP_ARCH_RISCV64:
#endif
                        /* These we know we support (i.e. are the ones that do not use socketcall()) */
                        supported = true;
                        break;
 
                case SCMP_ARCH_S390:
                case SCMP_ARCH_S390X:
                case SCMP_ARCH_X86:
                case SCMP_ARCH_MIPSEL:
                case SCMP_ARCH_MIPS:
                case SCMP_ARCH_PARISC:
                case SCMP_ARCH_PARISC64:
                case SCMP_ARCH_PPC:
                case SCMP_ARCH_PPC64:
                case SCMP_ARCH_PPC64LE:
                default:
                        /* These we either know we don't support (i.e. are the ones that do use socketcall()), or we
                         * don't know */
                        supported = false;
                }
 
                if (!supported)
                        continue;
 
                r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW);
                if (r < 0)
                        return r;
 
                if (allow_list) {
                        int first = 0, last = 0;
                        void *afp;
 
                        /* If this is an allow list, we first block the address families that are out of
                         * range and then everything that is not in the set. First, we find the lowest and
                         * highest address family in the set. */
 
                        SET_FOREACH(afp, address_families) {
                                int af = PTR_TO_INT(afp);
 
                                if (af <= 0 || af >= af_max())
                                        continue;
 
                                if (first == 0 || af < first)
                                        first = af;
 
                                if (last == 0 || af > last)
                                        last = af;
                        }
 
                        assert((first == 0) == (last == 0));
 
                        if (first == 0) {
 
                                /* No entries in the valid range, block everything */
                                r = sym_seccomp_rule_add_exact(
                                                seccomp,
                                                SCMP_ACT_ERRNO(EAFNOSUPPORT),
                                                SCMP_SYS(socket),
                                                0);
                                if (r < 0) {
                                        log_debug_errno(r, "Failed to add socket() rule for architecture %s, skipping: %m",
                                                        seccomp_arch_to_string(arch));
                                        continue;
                                }
 
                        } else {
 
                                /* Block everything below the first entry */
                                r = sym_seccomp_rule_add_exact(
                                                seccomp,
                                                SCMP_ACT_ERRNO(EAFNOSUPPORT),
                                                SCMP_SYS(socket),
                                                1,
                                                SCMP_A0(SCMP_CMP_LT, first));
                                if (r < 0) {
                                        log_debug_errno(r, "Failed to add socket() rule for architecture %s, skipping: %m",
                                                        seccomp_arch_to_string(arch));
                                        continue;
                                }
 
                                /* Block everything above the last entry */
                                r = sym_seccomp_rule_add_exact(
                                                seccomp,
                                                SCMP_ACT_ERRNO(EAFNOSUPPORT),
                                                SCMP_SYS(socket),
                                                1,
                                                SCMP_A0(SCMP_CMP_GT, last));
                                if (r < 0) {
                                        log_debug_errno(r, "Failed to add socket() rule for architecture %s, skipping: %m",
                                                        seccomp_arch_to_string(arch));
                                        continue;
                                }
 
                                /* Block everything between the first and last entry */
                                for (int af = 1; af < af_max(); af++) {
 
                                        if (set_contains(address_families, INT_TO_PTR(af)))
                                                continue;
 
                                        r = sym_seccomp_rule_add_exact(
                                                        seccomp,
                                                        SCMP_ACT_ERRNO(EAFNOSUPPORT),
                                                        SCMP_SYS(socket),
                                                        1,
                                                        SCMP_A0(SCMP_CMP_EQ, af));
                                        if (r < 0)
                                                break;
                                }
                                if (r < 0) {
                                        log_debug_errno(r, "Failed to add socket() rule for architecture %s, skipping: %m",
                                                        seccomp_arch_to_string(arch));
                                        continue;
                                }
                        }
 
                } else {
                        void *af;
 
                        /* If this is a deny list, then generate one rule for each address family that are
                         * then combined in OR checks. The kernel takes the domain as an int, i.e. it
                         * truncates the 64-bit register to its low 32 bits, hence only compare those. A plain
                         * equality check could be bypassed by setting any of the upper bits. */
 
                        SET_FOREACH(af, address_families) {
                                r = sym_seccomp_rule_add_exact(
                                                seccomp,
                                                SCMP_ACT_ERRNO(EAFNOSUPPORT),
                                                SCMP_SYS(socket),
                                                1,
                                                SCMP_A0(SCMP_CMP_MASKED_EQ, UINT32_MAX, (uint32_t) PTR_TO_INT(af)));
                                if (r < 0)
                                        break;
                        }
                        if (r < 0) {
                                log_debug_errno(r, "Failed to add socket() rule for architecture %s, skipping: %m",
                                                seccomp_arch_to_string(arch));
                                continue;
                        }
                }
 
                r = sym_seccomp_load(seccomp);
                if (ERRNO_IS_NEG_SECCOMP_FATAL(r))
                        return r;
                if (r < 0)
                        log_debug_errno(r, "Failed to install socket family rules for architecture %s, skipping: %m",
                                        seccomp_arch_to_string(arch));
        }
 
        return 0;
}
 
int seccomp_restrict_realtime_full(int error_code) {
        static const int permitted_policies[] = {
                SCHED_OTHER,
                SCHED_BATCH,
                SCHED_IDLE,
        };
 
        int r, max_policy = 0;
        uint32_t arch;
 
        assert(error_code > 0);
 
        r = dlopen_libseccomp(LOG_DEBUG);
        if (r < 0)
                return r;
 
        /* Determine the highest policy constant we want to allow */
        FOREACH_ELEMENT(policy, permitted_policies)
                if (*policy > max_policy)
                        max_policy = *policy;
 
        SECCOMP_FOREACH_LOCAL_ARCH(arch) {
                _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
                int p;
 
                log_trace("Operating on architecture: %s", seccomp_arch_to_string(arch));
 
                r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW);
                if (r < 0)
                        return r;
 
                /* Go through all policies with lower values than that, and block them -- unless they appear in the
                 * allow list. */
                for (p = 0; p < max_policy; p++) {
                        bool good = false;
 
                        /* Check if this is in the allow list. */
                        FOREACH_ELEMENT(policy, permitted_policies)
                                if (*policy == p) {
                                        good = true;
                                        break;
                                }
 
                        if (good)
                                continue;
 
                        /* Deny this policy */
                        r = sym_seccomp_rule_add_exact(
                                        seccomp,
                                        SCMP_ACT_ERRNO(error_code),
                                        SCMP_SYS(sched_setscheduler),
                                        1,
                                        SCMP_A1(SCMP_CMP_EQ, p));
                        if (r < 0) {
                                log_debug_errno(r, "Failed to add scheduler rule for architecture %s, skipping: %m",
                                                seccomp_arch_to_string(arch));
                                continue;
                        }
                }
 
                /* Deny-list all other policies, i.e. the ones with higher values. Note that all comparisons
                 * are unsigned here, hence no need no check for < 0 values. */
                r = sym_seccomp_rule_add_exact(
                                seccomp,
                                SCMP_ACT_ERRNO(error_code),
                                SCMP_SYS(sched_setscheduler),
                                1,
                                SCMP_A1(SCMP_CMP_GT, max_policy));
                if (r < 0) {
                        log_debug_errno(r, "Failed to add scheduler rule for architecture %s, skipping: %m",
                                        seccomp_arch_to_string(arch));
                        continue;
                }
 
                /* sched_setattr() can switch to SCHED_FIFO/SCHED_RR and is the only way to get
                 * SCHED_DEADLINE, but the policy is in a struct passed by pointer, which seccomp cannot
                 * inspect. Hence refuse the call altogether. */
                r = sym_seccomp_rule_add_exact(
                                seccomp,
                                SCMP_ACT_ERRNO(error_code),
                                SCMP_SYS(sched_setattr),
                                0);
                if (r < 0)
                        log_debug_errno(r, "Failed to add sched_setattr() rule for %s, ignoring: %m",
                                        seccomp_arch_to_string(arch));
 
                r = sym_seccomp_load(seccomp);
                if (ERRNO_IS_NEG_SECCOMP_FATAL(r))
                        return r;
                if (r < 0)
                        log_debug_errno(r, "Failed to install realtime protection rules for architecture %s, skipping: %m",
                                        seccomp_arch_to_string(arch));
        }
 
        return 0;
}
 
static int add_seccomp_syscall_filter(scmp_filter_ctx seccomp,
                                      uint32_t arch,
                                      int nr,
                                      unsigned arg_cnt,
                                      const struct scmp_arg_cmp arg) {
        int r;
 
        r = sym_seccomp_rule_add_exact(seccomp, SCMP_ACT_ERRNO(EPERM), nr, arg_cnt, arg);
        if (r < 0) {
                _cleanup_free_ char *n = NULL;
 
                n = sym_seccomp_syscall_resolve_num_arch(arch, nr);
                log_debug_errno(r, "Failed to add %s() rule for architecture %s, skipping: %m",
                                strna(n),
                                seccomp_arch_to_string(arch));
        }
 
        return r;
}
 
/* For known architectures, check that syscalls are indeed defined or not. */
#if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || defined(__loongarch_lp64) || (defined(__riscv) && __riscv_xlen == 64)
assert_cc(SCMP_SYS(shmget) > 0);
assert_cc(SCMP_SYS(shmat) > 0);
assert_cc(SCMP_SYS(shmdt) > 0);
#endif
 
int seccomp_memory_deny_write_execute(void) {
        uint32_t arch;
        unsigned loaded = 0;
        int r;
 
        r = dlopen_libseccomp(LOG_DEBUG);
        if (r < 0)
                return r;
 
        SECCOMP_FOREACH_LOCAL_ARCH(arch) {
                _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
                int filter_syscall = 0, block_syscall = 0, shmat_syscall = 0;
 
                log_trace("Operating on architecture: %s", seccomp_arch_to_string(arch));
 
                switch (arch) {
 
                /* Note that on some architectures shmat() isn't available, and the call is multiplexed through ipc().
                 * We ignore that here, which means there's still a way to get writable/executable
                 * memory, if an IPC key is mapped like this. That's a pity, but no total loss.
                 *
                 * Also, PARISC isn't here right now because it still needs executable memory, but work is in progress
                 * on that front (kernel work done in 5.18).
                 */
 
                case SCMP_ARCH_X86:
                case SCMP_ARCH_S390:
                        filter_syscall = SCMP_SYS(mmap2);
                        block_syscall = SCMP_SYS(mmap);
                        /* shmat multiplexed, see above */
                        break;
 
                case SCMP_ARCH_PPC:
                case SCMP_ARCH_PPC64:
                case SCMP_ARCH_PPC64LE:
                case SCMP_ARCH_S390X:
                        filter_syscall = SCMP_SYS(mmap);
                        /* shmat multiplexed, see above */
                        break;
 
                case SCMP_ARCH_ARM:
                        filter_syscall = SCMP_SYS(mmap2); /* arm has only mmap2 */
                        shmat_syscall = SCMP_SYS(shmat);
                        break;
 
                case SCMP_ARCH_X86_64:
                case SCMP_ARCH_X32:
                case SCMP_ARCH_AARCH64:
#ifdef SCMP_ARCH_LOONGARCH64
                case SCMP_ARCH_LOONGARCH64:
#endif
#ifdef SCMP_ARCH_RISCV64
                case SCMP_ARCH_RISCV64:
#endif
                        filter_syscall = SCMP_SYS(mmap); /* amd64, x32, arm64, loongarch64 and riscv64 have only mmap */
                        shmat_syscall = SCMP_SYS(shmat);
                        break;
 
                /* Please add more definitions here, if you port systemd to other architectures! */
 
#if !defined(__i386__) && !defined(__x86_64__) && !defined(__hppa__) && !defined(__hppa64__) && !defined(__powerpc__) && !defined(__powerpc64__) && !defined(__arm__) && !defined(__aarch64__) && !defined(__s390__) && !defined(__s390x__) && !(defined(__riscv) && __riscv_xlen == 64) && !defined(__loongarch_lp64)
#warning "Consider adding the right mmap() syscall definitions here!"
#endif
                }
 
                /* Can't filter mmap() on this arch, then skip it */
                if (filter_syscall == 0)
                        continue;
 
                r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW);
                if (r < 0)
                        return r;
 
                r = add_seccomp_syscall_filter(seccomp, arch, filter_syscall,
                                               1,
                                               SCMP_A2(SCMP_CMP_MASKED_EQ, PROT_EXEC|PROT_WRITE, PROT_EXEC|PROT_WRITE));
                if (r < 0)
                        continue;
 
                if (block_syscall != 0) {
                        r = add_seccomp_syscall_filter(seccomp, arch, block_syscall, 0, (const struct scmp_arg_cmp){} );
                        if (r < 0)
                                continue;
                }
 
                r = add_seccomp_syscall_filter(seccomp, arch, SCMP_SYS(mprotect),
                                               1,
                                               SCMP_A2(SCMP_CMP_MASKED_EQ, PROT_EXEC, PROT_EXEC));
                if (r < 0)
                        continue;