HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/shared/seccomp-util.c
Git blob: d6d619894a60f4a94f096498dbeec4aa8794dcb5
Lines 2001–2699 of 2,699
r = add_seccomp_syscall_filter(seccomp, arch, SCMP_SYS(pkey_mprotect),
1,
SCMP_A2(SCMP_CMP_MASKED_EQ, PROT_EXEC, PROT_EXEC));
if (r < 0)
continue;
if (shmat_syscall > 0) {
r = add_seccomp_syscall_filter(seccomp, arch, shmat_syscall,
1,
SCMP_A2(SCMP_CMP_MASKED_EQ, SHM_EXEC, SHM_EXEC));
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 MemoryDenyWriteExecute= rule for architecture %s, skipping: %m",
seccomp_arch_to_string(arch));
loaded++;
}
if (loaded == 0)
log_debug("Failed to install any seccomp rules for MemoryDenyWriteExecute=.");
return loaded;
}
int seccomp_restrict_archs(Set *archs) {
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
int r;
bool blocked_new = false;
r = dlopen_libseccomp(LOG_DEBUG);
if (r < 0)
return r;
/* This installs a filter with no rules, but that restricts the system call architectures to the specified
* list.
*
* There are some qualifications. However the most important use is to stop processes from bypassing
* system call restrictions, in case they used a broader (multiplexing) syscall which is only available
* in a non-native architecture. There are no holes in this use case, at least so far. */
/* Note libseccomp includes our "native" (current) architecture in the filter by default.
* We do not remove it. For example, our callers expect to be able to call execve() afterwards
* to run a program with the restrictions applied. */
seccomp = sym_seccomp_init(SCMP_ACT_ALLOW);
if (!seccomp)
return -ENOMEM;
for (unsigned i = 0; seccomp_local_archs[i] != SECCOMP_LOCAL_ARCH_END; ++i) {
uint32_t arch = seccomp_local_archs[i];
/* See above comment, our "native" architecture is never blocked. */
if (arch == sym_seccomp_arch_native())
continue;
/* That architecture might have already been blocked by a previous call to seccomp_restrict_archs. */
if (arch == SECCOMP_LOCAL_ARCH_BLOCKED)
continue;
bool block = !set_contains(archs, UINT32_TO_PTR(arch + 1));
/* The vdso for x32 assumes that x86-64 syscalls are available. Let's allow them, since x32
* x32 syscalls should basically match x86-64 for everything except the pointer type.
* The important thing is that you can block the old 32-bit x86 syscalls.
* https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=850047 */
if (block && arch == SCMP_ARCH_X86_64 && sym_seccomp_arch_native() == SCMP_ARCH_X32)
block = !set_contains(archs, UINT32_TO_PTR(SCMP_ARCH_X32 + 1));
if (block) {
seccomp_local_archs[i] = SECCOMP_LOCAL_ARCH_BLOCKED;
blocked_new = true;
} else {
r = sym_seccomp_arch_add(seccomp, arch);
if (r < 0 && r != -EEXIST)
return r;
}
}
/* All architectures that will be blocked by the seccomp program were
* already blocked. */
if (!blocked_new)
return 0;
r = sym_seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_NNP, 0);
if (r < 0)
return r;
r = sym_seccomp_load(seccomp);
if (ERRNO_IS_NEG_SECCOMP_FATAL(r))
return r;
if (r < 0)
log_debug_errno(r, "Failed to restrict system call architectures, skipping: %m");
return 0;
}
int parse_syscall_archs(char **l, Set **archs) {
int r;
assert(l);
assert(archs);
STRV_FOREACH(s, l) {
uint32_t a;
r = seccomp_arch_from_string(*s, &a);
if (r < 0)
return -EINVAL;
r = set_ensure_put(archs, NULL, UINT32_TO_PTR(a + 1));
if (r < 0)
return -ENOMEM;
}
return 0;
}
int seccomp_filter_set_add_by_name(Hashmap *filter, bool add, const char *name) {
int r;
assert(filter);
assert(name);
r = dlopen_libseccomp(LOG_DEBUG);
if (r < 0)
return r;
if (name[0] == '@') {
const SyscallFilterSet *more;
more = syscall_filter_set_find(name);
if (!more)
return -ENXIO;
return seccomp_filter_set_add(filter, add, more);
}
int id = sym_seccomp_syscall_resolve_name(name);
if (id == __NR_SCMP_ERROR) {
log_debug("System call %s is not known, ignoring.", name);
return 0;
}
if (add)
return hashmap_put(filter, INT_TO_PTR(id + 1), INT_TO_PTR(-1));
(void) hashmap_remove(filter, INT_TO_PTR(id + 1));
return 0;
}
int seccomp_filter_set_add(Hashmap *filter, bool add, const SyscallFilterSet *set) {
int r;
assert(filter);
assert(set);
NULSTR_FOREACH(i, set->value) {
r = seccomp_filter_set_add_by_name(filter, add, i);
if (r < 0)
return r;
}
return 0;
}
/* Block PTRACE_POKE{TEXT,DATA} FOLL_FORCE writes. A missing filter is a bypass, so unlike the other helpers
* this fails when the native architecture cannot be filtered. A secondary architecture the kernel refuses a
* filter for is skipped as everywhere else: it cannot run binaries either. */
int seccomp_restrict_ptrace(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 = add_seccomp_syscall_filter(seccomp, arch, SCMP_SYS(ptrace),
1, SCMP_A0(SCMP_CMP_EQ, PTRACE_POKETEXT));
if (r < 0)
return r;
r = add_seccomp_syscall_filter(seccomp, arch, SCMP_SYS(ptrace),
1, SCMP_A0(SCMP_CMP_EQ, PTRACE_POKEDATA));
if (r < 0)
return r;
r = sym_seccomp_load(seccomp);
if (r < 0) {
if (ERRNO_IS_NEG_SECCOMP_FATAL(r) || arch == sym_seccomp_arch_native())
return log_debug_errno(r, "Failed to apply ptrace restrictions for architecture %s: %m",
seccomp_arch_to_string(arch));
log_debug_errno(r, "Failed to apply ptrace restrictions for architecture %s, skipping: %m",
seccomp_arch_to_string(arch));
}
}
return 0;
}
int seccomp_lock_personality(unsigned long personality) {
uint32_t arch;
int r;
if (personality >= PERSONALITY_INVALID)
return -EINVAL;
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(personality),
1,
SCMP_A0(SCMP_CMP_NE, personality));
if (r < 0) {
log_debug_errno(r, "Failed to add scheduler 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 enable personality lock for architecture %s, skipping: %m",
seccomp_arch_to_string(arch));
}
return 0;
}
int seccomp_protect_hostname(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(sethostname),
0);
if (r < 0) {
log_debug_errno(r, "Failed to add sethostname() rule for architecture %s, skipping: %m",
seccomp_arch_to_string(arch));
continue;
}
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(EPERM),
SCMP_SYS(setdomainname),
0);
if (r < 0) {
log_debug_errno(r, "Failed to add setdomainname() 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 apply hostname restrictions for architecture %s, skipping: %m",
seccomp_arch_to_string(arch));
}
return 0;
}
static int seccomp_restrict_sxid(scmp_filter_ctx seccomp, mode_t m) {
/* Checks the mode_t parameter of the following system calls:
*
* → chmod() + fchmod() + fchmodat() + fchmodat2()
* → open() + creat() + openat()
* → mkdir() + mkdirat()
* → mknod() + mknodat()
*
* Returns error if *everything* failed, and 0 otherwise.
*/
int r;
bool any = false;
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(EPERM),
SCMP_SYS(chmod),
1,
SCMP_A1(SCMP_CMP_MASKED_EQ, m, m));
if (r < 0)
log_debug_errno(r, "Failed to add filter for chmod: %m");
else
any = true;
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(EPERM),
SCMP_SYS(fchmod),
1,
SCMP_A1(SCMP_CMP_MASKED_EQ, m, m));
if (r < 0)
log_debug_errno(r, "Failed to add filter for fchmod: %m");
else
any = true;
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(EPERM),
SCMP_SYS(fchmodat),
1,
SCMP_A2(SCMP_CMP_MASKED_EQ, m, m));
if (r < 0)
log_debug_errno(r, "Failed to add filter for fchmodat: %m");
else
any = true;
#if defined(__SNR_fchmodat2)
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(EPERM),
SCMP_SYS(fchmodat2),
1,
SCMP_A2(SCMP_CMP_MASKED_EQ, m, m));
#else
/* It looks like this libseccomp does not know about fchmodat2().
* Pretend the fchmodat2() system call is not supported at all,
* regardless of the kernel version. */
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(ENOSYS),
__NR_fchmodat2,
0);
#endif
if (r < 0)
log_debug_errno(r, "Failed to add filter for fchmodat2: %m");
else
any = true;
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(EPERM),
SCMP_SYS(mkdir),
1,
SCMP_A1(SCMP_CMP_MASKED_EQ, m, m));
if (r < 0)
log_debug_errno(r, "Failed to add filter for mkdir: %m");
else
any = true;
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(EPERM),
SCMP_SYS(mkdirat),
1,
SCMP_A2(SCMP_CMP_MASKED_EQ, m, m));
if (r < 0)
log_debug_errno(r, "Failed to add filter for mkdirat: %m");
else
any = true;
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(EPERM),
SCMP_SYS(mknod),
1,
SCMP_A1(SCMP_CMP_MASKED_EQ, m, m));
if (r < 0)
log_debug_errno(r, "Failed to add filter for mknod: %m");
else
any = true;
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(EPERM),
SCMP_SYS(mknodat),
1,
SCMP_A2(SCMP_CMP_MASKED_EQ, m, m));
if (r < 0)
log_debug_errno(r, "Failed to add filter for mknodat: %m");
else
any = true;
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(EPERM),
SCMP_SYS(open),
2,
SCMP_A1(SCMP_CMP_MASKED_EQ, O_CREAT, O_CREAT),
SCMP_A2(SCMP_CMP_MASKED_EQ, m, m));
if (r < 0)
log_debug_errno(r, "Failed to add filter for open: %m");
else
any = true;
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(EPERM),
SCMP_SYS(openat),
2,
SCMP_A2(SCMP_CMP_MASKED_EQ, O_CREAT, O_CREAT),
SCMP_A3(SCMP_CMP_MASKED_EQ, m, m));
if (r < 0)
log_debug_errno(r, "Failed to add filter for openat: %m");
else
any = true;
#if defined(__SNR_openat2)
/* The new openat2() system call can't be filtered sensibly, since it moves the flags parameter into
* an indirect structure. Let's block it entirely for now. That should be a reasonably OK thing to do
* for now, since openat2() is very new and code generally needs fallback logic anyway to be
* compatible with kernels that are not absolutely recent. We would normally return EPERM for a
* policy check, but this isn't strictly a policy check. Instead, we return ENOSYS to force programs
* to call open() or openat() instead. We can properly enforce policy for those functions. */
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(ENOSYS),
SCMP_SYS(openat2),
0);
if (r < 0)
log_debug_errno(r, "Failed to add filter for openat2: %m");
else
any = true;
#endif
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(EPERM),
SCMP_SYS(creat),
1,
SCMP_A1(SCMP_CMP_MASKED_EQ, m, m));
if (r < 0)
log_debug_errno(r, "Failed to add filter for creat: %m");
else
any = true;
return any ? 0 : r;
}
int seccomp_restrict_suid_sgid(void) {
uint32_t arch;
int r, k;
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 = seccomp_restrict_sxid(seccomp, S_ISUID);
if (r < 0)
log_debug_errno(r, "Failed to add suid rule for architecture %s, ignoring: %m",
seccomp_arch_to_string(arch));
k = seccomp_restrict_sxid(seccomp, S_ISGID);
if (k < 0)
log_debug_errno(k, "Failed to add sgid rule for architecture %s, ignoring: %m",
seccomp_arch_to_string(arch));
if (r < 0 && k < 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 apply suid/sgid restrictions for architecture %s, skipping: %m",
seccomp_arch_to_string(arch));
}
return 0;
}
int parse_syscall_and_errno(const char *in, char **name, int *error) {
_cleanup_free_ char *n = NULL;
const char *p;
int e = -1;
assert(in);
assert(name);
assert(error);
/*
* This parse "syscall:errno" like "uname:EILSEQ", "@sync:255".
* If errno is omitted, then error is set to -1.
* Empty syscall name is not allowed.
* Here, we do not check that the syscall name is valid or not.
*/
p = strchr(in, ':');
if (p) {
e = seccomp_parse_errno_or_action(p + 1);
if (e < 0)
return e;
n = strndup(in, p - in);
} else
n = strdup(in);
if (!n)
return -ENOMEM;
if (isempty(n))
return -EINVAL;
*error = e;
*name = TAKE_PTR(n);
return 0;
}
static int block_open_flag(scmp_filter_ctx seccomp, int flag) {
bool any = false;
int r;
/* Blocks open() with the specified flag, where flag is O_SYNC or so. This makes these calls return
* EINVAL, in the hope the client code will retry without O_SYNC then. */
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(EINVAL),
SCMP_SYS(open),
1,
SCMP_A1(SCMP_CMP_MASKED_EQ, flag, flag));
if (r < 0)
log_debug_errno(r, "Failed to add filter for open: %m");
else
any = true;
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(EINVAL),
SCMP_SYS(openat),
1,
SCMP_A2(SCMP_CMP_MASKED_EQ, flag, flag));
if (r < 0)
log_debug_errno(r, "Failed to add filter for openat: %m");
else
any = true;
/* We can't reasonably filter openat2() here, because the flags are in an indirect struct instead of
* a regular scalar argument, see also the comment in seccomp_restrict_sxid() above. However,
* blocking it here causes an increasing number of issues as more software moves to openat2() without
* any fallback to open()/openat().
*
* Given that calling openat2() with O_SYNC is rather rare, and most of the heavy-lifting is done by
* filtering out the sync-family syscalls in seccomp_suppress_sync() below, let's just blanket-allow
* openat2() to avoid unnecessarily breaking stuff left and right when nspawn is running with
* --suppress-sync=yes. This means that we might issue a synchronous write when something calls
* openat2() with O_SYNC, but it is the best we can do at least until a better solution pops up. */
return any ? 0 : r;
}
int seccomp_suppress_sync(void) {
uint32_t arch;
int r;
/* This behaves slightly differently from SystemCallFilter=~@sync:0, in that negative fds (which
* we can determine to be invalid) are still refused with EBADF. See #34478.
*
* Additionally, O_SYNC/O_DSYNC are masked (except for openat2(), see above). */
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;
NULSTR_FOREACH(c, syscall_filter_sets[SYSCALL_FILTER_SET_SYNC].value) {
int id;
id = sym_seccomp_syscall_resolve_name(c);
if (id == __NR_SCMP_ERROR) {
log_debug("System call %s is not known, ignoring.", c);
continue;
}
if (STR_IN_SET(c, "fdatasync", "fsync", "sync_file_range", "sync_file_range2", "syncfs"))
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(0), /* success → we want this to be a NOP after all */
id,
1,
SCMP_A0(SCMP_CMP_LE, INT_MAX)); /* The rule handles arguments in unsigned. Hence, this
* means non-negative fd matches the rule, and the negative
* fd passed to the syscall (then it fails with EBADF). */
else
r = sym_seccomp_rule_add_exact(
seccomp,
SCMP_ACT_ERRNO(0), /* success → we want this to be a NOP after all */
id,
0);
if (r < 0)
log_debug_errno(r, "Failed to add filter for system call %s, ignoring: %m", c);
}
(void) block_open_flag(seccomp, O_SYNC);
#if O_DSYNC != O_SYNC
(void) block_open_flag(seccomp, O_DSYNC);
#endif
r = sym_seccomp_load(seccomp);
if (ERRNO_IS_NEG_SECCOMP_FATAL(r))
return r;
if (r < 0)
log_debug_errno(r, "Failed to apply sync() suppression for architecture %s, skipping: %m",
seccomp_arch_to_string(arch));
}
return 0;
}
#endif
int dlopen_libseccomp(int log_level) {
#if HAVE_SECCOMP
static void *libseccomp_dl = NULL;
LIBSECCOMP_NOTE(suggested);
return dlopen_many_sym_or_warn(
&libseccomp_dl,
"libseccomp.so.2",
log_level,
DLSYM_ARG(seccomp_api_get),
DLSYM_ARG(seccomp_arch_add),
DLSYM_ARG(seccomp_arch_exist),
DLSYM_ARG(seccomp_arch_native),
DLSYM_ARG(seccomp_arch_remove),
DLSYM_ARG(seccomp_attr_set),
DLSYM_ARG(seccomp_init),
DLSYM_ARG(seccomp_load),
DLSYM_ARG(seccomp_release),
DLSYM_ARG(seccomp_rule_add_array),
DLSYM_ARG(seccomp_rule_add_exact),
DLSYM_ARG(seccomp_syscall_resolve_name),
DLSYM_ARG(seccomp_syscall_resolve_num_arch));
#else
return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
"libseccomp support is not compiled in.");
#endif
}
bool seccomp_errno_or_action_is_valid(int n) {
return n == SECCOMP_ERROR_NUMBER_KILL || errno_is_valid(n);
}
int seccomp_parse_errno_or_action(const char *p) {
if (streq_ptr(p, "kill"))
return SECCOMP_ERROR_NUMBER_KILL;
return parse_errno(p);
}
const char* seccomp_errno_or_action_to_string(int num) {
if (num == SECCOMP_ERROR_NUMBER_KILL)
return "kill";
return errno_name_no_fallback(num);
}