| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "sd-dlopen.h" |
| 5 |
|
| 6 |
#include "errno-util.h" |
| 7 |
#include "shared-forward.h" |
| 8 |
|
| 9 |
#if HAVE_SECCOMP |
| 10 |
#include <seccomp.h> /* IWYU pragma: export */ |
| 11 |
|
| 12 |
#include "dlfcn-util.h" |
| 13 |
|
| 14 |
extern DLSYM_PROTOTYPE(seccomp_api_get); |
| 15 |
extern DLSYM_PROTOTYPE(seccomp_arch_add); |
| 16 |
extern DLSYM_PROTOTYPE(seccomp_arch_exist); |
| 17 |
extern DLSYM_PROTOTYPE(seccomp_arch_native); |
| 18 |
extern DLSYM_PROTOTYPE(seccomp_arch_remove); |
| 19 |
extern DLSYM_PROTOTYPE(seccomp_attr_set); |
| 20 |
extern DLSYM_PROTOTYPE(seccomp_init); |
| 21 |
extern DLSYM_PROTOTYPE(seccomp_load); |
| 22 |
extern DLSYM_PROTOTYPE(seccomp_release); |
| 23 |
extern DLSYM_PROTOTYPE(seccomp_rule_add_array); |
| 24 |
extern DLSYM_PROTOTYPE(seccomp_rule_add_exact); |
| 25 |
extern DLSYM_PROTOTYPE(seccomp_syscall_resolve_name); |
| 26 |
extern DLSYM_PROTOTYPE(seccomp_syscall_resolve_num_arch); |
| 27 |
|
| 28 |
DECLARE_STRING_TABLE_LOOKUP_TO_STRING(seccomp_arch, uint32_t); |
| 29 |
int seccomp_arch_from_string(const char *n, uint32_t *ret); |
| 30 |
|
| 31 |
int seccomp_init_for_arch(scmp_filter_ctx *ret, uint32_t arch, uint32_t default_action); |
| 32 |
|
| 33 |
bool is_seccomp_available(void); |
| 34 |
|
| 35 |
typedef struct SyscallFilterSet { |
| 36 |
const char *name; |
| 37 |
const char *help; |
| 38 |
const char *value; |
| 39 |
} SyscallFilterSet; |
| 40 |
|
| 41 |
enum { |
| 42 |
/* Please leave DEFAULT first and KNOWN last, but sort the rest alphabetically */ |
| 43 |
SYSCALL_FILTER_SET_DEFAULT, |
| 44 |
SYSCALL_FILTER_SET_AIO, |
| 45 |
SYSCALL_FILTER_SET_BASIC_IO, |
| 46 |
SYSCALL_FILTER_SET_CHOWN, |
| 47 |
SYSCALL_FILTER_SET_CLOCK, |
| 48 |
SYSCALL_FILTER_SET_CPU_EMULATION, |
| 49 |
SYSCALL_FILTER_SET_DEBUG, |
| 50 |
SYSCALL_FILTER_SET_FILE_SYSTEM, |
| 51 |
SYSCALL_FILTER_SET_IO_EVENT, |
| 52 |
SYSCALL_FILTER_SET_IPC, |
| 53 |
SYSCALL_FILTER_SET_KEYRING, |
| 54 |
SYSCALL_FILTER_SET_MEMLOCK, |
| 55 |
SYSCALL_FILTER_SET_MODULE, |
| 56 |
SYSCALL_FILTER_SET_MOUNT, |
| 57 |
SYSCALL_FILTER_SET_NETWORK_IO, |
| 58 |
SYSCALL_FILTER_SET_OBSOLETE, |
| 59 |
SYSCALL_FILTER_SET_PKEY, |
| 60 |
SYSCALL_FILTER_SET_PRIVILEGED, |
| 61 |
SYSCALL_FILTER_SET_PROCESS, |
| 62 |
SYSCALL_FILTER_SET_RAW_IO, |
| 63 |
SYSCALL_FILTER_SET_REBOOT, |
| 64 |
SYSCALL_FILTER_SET_RESOURCES, |
| 65 |
SYSCALL_FILTER_SET_SANDBOX, |
| 66 |
SYSCALL_FILTER_SET_SETUID, |
| 67 |
SYSCALL_FILTER_SET_SIGNAL, |
| 68 |
SYSCALL_FILTER_SET_SWAP, |
| 69 |
SYSCALL_FILTER_SET_SYNC, |
| 70 |
SYSCALL_FILTER_SET_SYSTEM_SERVICE, |
| 71 |
SYSCALL_FILTER_SET_TIMER, |
| 72 |
SYSCALL_FILTER_SET_KNOWN, |
| 73 |
_SYSCALL_FILTER_SET_MAX, |
| 74 |
}; |
| 75 |
|
| 76 |
assert_cc(SYSCALL_FILTER_SET_DEFAULT == 0); |
| 77 |
assert_cc(SYSCALL_FILTER_SET_KNOWN == _SYSCALL_FILTER_SET_MAX-1); |
| 78 |
|
| 79 |
extern const SyscallFilterSet syscall_filter_sets[]; |
| 80 |
|
| 81 |
const SyscallFilterSet *syscall_filter_set_find(const char *name); |
| 82 |
|
| 83 |
int seccomp_filter_set_add_by_name(Hashmap *filter, bool add, const char *name); |
| 84 |
int seccomp_filter_set_add(Hashmap *filter, bool add, const SyscallFilterSet *set); |
| 85 |
|
| 86 |
int seccomp_add_syscall_filter_item( |
| 87 |
scmp_filter_ctx *seccomp, |
| 88 |
const char *name, |
| 89 |
uint32_t action, |
| 90 |
char **exclude, |
| 91 |
bool log_missing, |
| 92 |
char ***added); |
| 93 |
|
| 94 |
int seccomp_load_syscall_filter_set(uint32_t default_action, const SyscallFilterSet *set, uint32_t action, bool log_missing); |
| 95 |
int seccomp_load_syscall_filter_set_raw(uint32_t default_action, Hashmap *filter, uint32_t action, bool log_missing); |
| 96 |
|
| 97 |
typedef enum SeccompParseFlags { |
| 98 |
SECCOMP_PARSE_INVERT = 1 << 0, |
| 99 |
SECCOMP_PARSE_ALLOW_LIST = 1 << 1, |
| 100 |
SECCOMP_PARSE_LOG = 1 << 2, |
| 101 |
SECCOMP_PARSE_PERMISSIVE = 1 << 3, |
| 102 |
} SeccompParseFlags; |
| 103 |
|
| 104 |
int seccomp_parse_syscall_filter( |
| 105 |
const char *name, |
| 106 |
int errno_num, |
| 107 |
Hashmap *filter, |
| 108 |
SeccompParseFlags flags, |
| 109 |
const char *unit, |
| 110 |
const char *filename, unsigned line); |
| 111 |
|
| 112 |
int seccomp_restrict_archs(Set *archs); |
| 113 |
int seccomp_restrict_namespaces(unsigned long retain); |
| 114 |
int seccomp_protect_sysctl(void); |
| 115 |
int seccomp_protect_syslog(void); |
| 116 |
int seccomp_restrict_address_families(Set *address_families, bool allow_list); |
| 117 |
int seccomp_restrict_realtime_full(int error_code); /* This is mostly for testing code. */ |
| 118 |
static inline int seccomp_restrict_realtime(void) { |
| 119 |
return seccomp_restrict_realtime_full(EPERM); |
| 120 |
} |
| 121 |
int seccomp_memory_deny_write_execute(void); |
| 122 |
int seccomp_lock_personality(unsigned long personality); |
| 123 |
int seccomp_protect_hostname(void); |
| 124 |
int seccomp_restrict_suid_sgid(void); |
| 125 |
|
| 126 |
extern uint32_t seccomp_local_archs[]; |
| 127 |
|
| 128 |
#define SECCOMP_LOCAL_ARCH_END UINT32_MAX |
| 129 |
|
| 130 |
/* Note: 0 is safe to use here because although SCMP_ARCH_NATIVE is 0, it would |
| 131 |
* never be in the seccomp_local_archs array anyway so we can use it as a |
| 132 |
* marker. */ |
| 133 |
#define SECCOMP_LOCAL_ARCH_BLOCKED 0 |
| 134 |
|
| 135 |
#define SECCOMP_FOREACH_LOCAL_ARCH(arch) \ |
| 136 |
for (unsigned _i = ({ (arch) = seccomp_local_archs[0]; 0; }); \ |
| 137 |
(arch) != SECCOMP_LOCAL_ARCH_END; \ |
| 138 |
(arch) = seccomp_local_archs[++_i]) \ |
| 139 |
if ((arch) != SECCOMP_LOCAL_ARCH_BLOCKED) |
| 140 |
|
| 141 |
/* EACCES: does not have the CAP_SYS_ADMIN or no_new_privs == 1 |
| 142 |
* ENOMEM: out of memory, failed to allocate space for a libseccomp structure, or would exceed a defined constant |
| 143 |
* EFAULT: addresses passed as args (by libseccomp) are invalid */ |
| 144 |
static inline bool ERRNO_IS_NEG_SECCOMP_FATAL(intmax_t r) { |
| 145 |
return IN_SET(r, |
| 146 |
-EPERM, |
| 147 |
-EACCES, |
| 148 |
-ENOMEM, |
| 149 |
-EFAULT); |
| 150 |
} |
| 151 |
_DEFINE_ABS_WRAPPER(SECCOMP_FATAL); |
| 152 |
|
| 153 |
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_RENAME(scmp_filter_ctx, sym_seccomp_release, seccomp_releasep, NULL); |
| 154 |
|
| 155 |
int parse_syscall_archs(char **l, Set **ret_archs); |
| 156 |
int parse_syscall_and_errno(const char *in, char **name, int *error); |
| 157 |
|
| 158 |
int seccomp_suppress_sync(void); |
| 159 |
|
| 160 |
#define LIBSECCOMP_NOTE(priority) \ |
| 161 |
SD_ELF_NOTE_DLOPEN("seccomp", \ |
| 162 |
"Support for Seccomp Sandboxes", \ |
| 163 |
priority, \ |
| 164 |
"libseccomp.so.2") |
| 165 |
|
| 166 |
#define DLOPEN_LIBSECCOMP(log_level, priority) \ |
| 167 |
({ \ |
| 168 |
LIBSECCOMP_NOTE(priority); \ |
| 169 |
dlopen_libseccomp(log_level); \ |
| 170 |
}) |
| 171 |
#else |
| 172 |
|
| 173 |
static inline bool is_seccomp_available(void) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
#define DLOPEN_LIBSECCOMP(log_level, priority) dlopen_libseccomp(log_level) |
| 179 |
#endif |
| 180 |
|
| 181 |
int dlopen_libseccomp(int log_level); |
| 182 |
|
| 183 |
/* This is a special value to be used where syscall filters otherwise expect errno numbers, will be |
| 184 |
replaced with real seccomp action. */ |
| 185 |
enum { |
| 186 |
SECCOMP_ERROR_NUMBER_KILL = INT_MAX - 1, |
| 187 |
}; |
| 188 |
|
| 189 |
bool seccomp_errno_or_action_is_valid(int n) _const_; |
| 190 |
int seccomp_parse_errno_or_action(const char *p) _pure_; |
| 191 |
DECLARE_STRING_TABLE_LOOKUP_TO_STRING(seccomp_errno_or_action, int); |