| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/capability.h> |
| 4 |
#include <linux/netlink.h> |
| 5 |
#include <sys/socket.h> |
| 6 |
|
| 7 |
#include "log.h" |
| 8 |
#include "nspawn-seccomp.h" |
| 9 |
#include "seccomp-util.h" |
| 10 |
#include "set.h" |
| 11 |
#include "strv.h" |
| 12 |
|
| 13 |
#if HAVE_SECCOMP |
| 14 |
|
| 15 |
static int add_syscall_filters( |
| 16 |
scmp_filter_ctx ctx, |
| 17 |
uint32_t arch, |
| 18 |
uint64_t cap_list_retain, |
| 19 |
char **syscall_allow_list, |
| 20 |
char **syscall_deny_list) { |
| 21 |
|
| 22 |
static const struct { |
| 23 |
uint64_t capability; |
| 24 |
const char* name; |
| 25 |
} allow_list[] = { |
| 26 |
/* Let's use set names where we can */ |
| 27 |
{ 0, "@aio" }, |
| 28 |
{ 0, "@basic-io" }, |
| 29 |
{ 0, "@chown" }, |
| 30 |
{ 0, "@default" }, |
| 31 |
{ 0, "@file-system" }, |
| 32 |
{ 0, "@io-event" }, |
| 33 |
{ 0, "@ipc" }, |
| 34 |
{ 0, "@keyring" }, |
| 35 |
{ 0, "@mount" }, |
| 36 |
{ 0, "@network-io" }, |
| 37 |
{ 0, "@process" }, |
| 38 |
{ 0, "@resources" }, |
| 39 |
{ 0, "@setuid" }, |
| 40 |
{ 0, "@signal" }, |
| 41 |
{ 0, "@sync" }, |
| 42 |
{ 0, "@timer" }, |
| 43 |
|
| 44 |
/* The following four are sets we optionally enable, n case the caps have been configured for it */ |
| 45 |
{ CAP_SYS_TIME, "@clock" }, |
| 46 |
{ CAP_SYS_MODULE, "@module" }, |
| 47 |
{ CAP_SYS_RAWIO, "@raw-io" }, |
| 48 |
{ CAP_IPC_LOCK, "@memlock" }, |
| 49 |
|
| 50 |
/* Plus a good set of additional syscalls which are not part of any of the groups above */ |
| 51 |
{ 0, "arm_fadvise64_64" }, |
| 52 |
{ 0, "brk" }, |
| 53 |
{ 0, "cachestat" }, |
| 54 |
{ 0, "capget" }, |
| 55 |
{ 0, "capset" }, |
| 56 |
{ 0, "copy_file_range" }, |
| 57 |
{ 0, "fadvise64" }, |
| 58 |
{ 0, "fadvise64_64" }, |
| 59 |
{ 0, "flock" }, |
| 60 |
{ 0, "get_mempolicy" }, |
| 61 |
{ 0, "getcpu" }, |
| 62 |
{ 0, "getpriority" }, |
| 63 |
{ 0, "getrandom" }, |
| 64 |
{ 0, "ioctl" }, |
| 65 |
{ 0, "ioprio_get" }, |
| 66 |
{ 0, "kcmp" }, |
| 67 |
{ 0, "madvise" }, |
| 68 |
{ 0, "mincore" }, |
| 69 |
{ 0, "mprotect" }, |
| 70 |
{ 0, "mremap" }, |
| 71 |
{ 0, "name_to_handle_at" }, |
| 72 |
{ 0, "oldolduname" }, |
| 73 |
{ 0, "olduname" }, |
| 74 |
{ 0, "personality" }, |
| 75 |
{ 0, "readahead" }, |
| 76 |
{ 0, "readdir" }, |
| 77 |
{ 0, "remap_file_pages" }, |
| 78 |
{ 0, "sched_get_priority_max" }, |
| 79 |
{ 0, "sched_get_priority_min" }, |
| 80 |
{ 0, "sched_getaffinity" }, |
| 81 |
{ 0, "sched_getattr" }, |
| 82 |
{ 0, "sched_getparam" }, |
| 83 |
{ 0, "sched_getscheduler" }, |
| 84 |
{ 0, "sched_rr_get_interval" }, |
| 85 |
{ 0, "sched_rr_get_interval_time64" }, |
| 86 |
{ 0, "sched_yield" }, |
| 87 |
{ 0, "sendfile" }, |
| 88 |
{ 0, "sendfile64" }, |
| 89 |
{ 0, "setdomainname" }, |
| 90 |
{ 0, "setfsgid" }, |
| 91 |
{ 0, "setfsgid32" }, |
| 92 |
{ 0, "setfsuid" }, |
| 93 |
{ 0, "setfsuid32" }, |
| 94 |
{ 0, "sethostname" }, |
| 95 |
{ 0, "setpgid" }, |
| 96 |
{ 0, "setsid" }, |
| 97 |
{ 0, "splice" }, |
| 98 |
{ 0, "sysinfo" }, |
| 99 |
{ 0, "tee" }, |
| 100 |
{ 0, "umask" }, |
| 101 |
{ 0, "uname" }, |
| 102 |
{ 0, "userfaultfd" }, |
| 103 |
{ 0, "vmsplice" }, |
| 104 |
|
| 105 |
/* The following individual syscalls are added depending on specified caps */ |
| 106 |
{ CAP_SYS_PACCT, "acct" }, |
| 107 |
{ CAP_SYS_PTRACE, "process_vm_readv" }, |
| 108 |
{ CAP_SYS_PTRACE, "process_vm_writev" }, |
| 109 |
{ CAP_SYS_PTRACE, "ptrace" }, |
| 110 |
{ CAP_SYS_BOOT, "reboot" }, |
| 111 |
{ CAP_SYSLOG, "syslog" }, |
| 112 |
{ CAP_SYS_TTY_CONFIG, "vhangup" }, |
| 113 |
{ CAP_BPF, "bpf", }, |
| 114 |
|
| 115 |
/* |
| 116 |
* The following syscalls and groups are knowingly excluded: |
| 117 |
* |
| 118 |
* @cpu-emulation |
| 119 |
* @obsolete |
| 120 |
* @pkey |
| 121 |
* @swap |
| 122 |
* |
| 123 |
* fanotify_init |
| 124 |
* fanotify_mark |
| 125 |
* kexec_file_load |
| 126 |
* kexec_load |
| 127 |
* lookup_dcookie |
| 128 |
* nfsservctl |
| 129 |
* open_by_handle_at |
| 130 |
* perf_event_open |
| 131 |
* quotactl |
| 132 |
*/ |
| 133 |
}; |
| 134 |
|
| 135 |
_cleanup_strv_free_ char **added = NULL; |
| 136 |
int r; |
| 137 |
|
| 138 |
FOREACH_ELEMENT(i, allow_list) { |
| 139 |
if (i->capability != 0 && (cap_list_retain & (1ULL << i->capability)) == 0) |
| 140 |
continue; |
| 141 |
|
| 142 |
r = seccomp_add_syscall_filter_item(ctx, |
| 143 |
i->name, |
| 144 |
SCMP_ACT_ALLOW, |
| 145 |
syscall_deny_list, |
| 146 |
false, |
| 147 |
&added); |
| 148 |
if (r < 0) |
| 149 |
return log_error_errno(r, "Failed to add syscall filter item %s: %m", i->name); |
| 150 |
} |
| 151 |
|
| 152 |
STRV_FOREACH(p, syscall_allow_list) { |
| 153 |
r = seccomp_add_syscall_filter_item(ctx, *p, SCMP_ACT_ALLOW, syscall_deny_list, true, &added); |
| 154 |
if (r < 0) |
| 155 |
log_warning_errno(r, "Failed to add rule for system call %s on %s, ignoring: %m", |
| 156 |
*p, seccomp_arch_to_string(arch)); |
| 157 |
} |
| 158 |
|
| 159 |
/* The default action is ENOSYS. Respond with EPERM to all other "known" but not allow-listed |
| 160 |
* syscalls. */ |
| 161 |
r = seccomp_add_syscall_filter_item(ctx, "@known", SCMP_ACT_ERRNO(EPERM), added, true, NULL); |
| 162 |
if (r < 0) |
| 163 |
log_warning_errno(r, "Failed to add rule for @known set on %s, ignoring: %m", |
| 164 |
seccomp_arch_to_string(arch)); |
| 165 |
|
| 166 |
#if (SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 5) || SCMP_VER_MAJOR > 2 |
| 167 |
/* We have a large filter here, so let's turn on the binary tree mode if possible. */ |
| 168 |
r = sym_seccomp_attr_set(ctx, SCMP_FLTATR_CTL_OPTIMIZE, 2); |
| 169 |
if (r < 0) |
| 170 |
log_warning_errno(r, "Failed to set SCMP_FLTATR_CTL_OPTIMIZE, ignoring: %m"); |
| 171 |
#endif |
| 172 |
|
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
int setup_seccomp( |
| 177 |
uint64_t cap_list_retain, |
| 178 |
char **syscall_allow_list, |
| 179 |
char **syscall_deny_list, |
| 180 |
Set *restrict_address_families, |
| 181 |
bool restrict_address_families_is_allowlist) { |
| 182 |
|
| 183 |
uint32_t arch; |
| 184 |
int r; |
| 185 |
|
| 186 |
if (!is_seccomp_available()) { |
| 187 |
log_debug("SECCOMP features not detected in the kernel or disabled at runtime, disabling SECCOMP filtering"); |
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
SECCOMP_FOREACH_LOCAL_ARCH(arch) { |
| 192 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 193 |
|
| 194 |
log_debug("Applying allow list on architecture: %s", seccomp_arch_to_string(arch)); |
| 195 |
|
| 196 |
/* We install ENOSYS as the default action, but it will only apply to syscalls which are not |
| 197 |
* in the @known set, see above. */ |
| 198 |
r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ERRNO(ENOSYS)); |
| 199 |
if (r < 0) |
| 200 |
return log_error_errno(r, "Failed to allocate seccomp object: %m"); |
| 201 |
|
| 202 |
r = add_syscall_filters(seccomp, arch, cap_list_retain, syscall_allow_list, syscall_deny_list); |
| 203 |
if (r < 0) |
| 204 |
return r; |
| 205 |
|
| 206 |
r = sym_seccomp_load(seccomp); |
| 207 |
if (ERRNO_IS_NEG_SECCOMP_FATAL(r)) |
| 208 |
return log_error_errno(r, "Failed to install seccomp filter: %m"); |
| 209 |
if (r < 0) |
| 210 |
log_debug_errno(r, "Failed to install filter set for architecture %s, skipping: %m", |
| 211 |
seccomp_arch_to_string(arch)); |
| 212 |
} |
| 213 |
|
| 214 |
SECCOMP_FOREACH_LOCAL_ARCH(arch) { |
| 215 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 216 |
|
| 217 |
log_debug("Applying NETLINK_AUDIT mask on architecture: %s", seccomp_arch_to_string(arch)); |
| 218 |
|
| 219 |
r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW); |
| 220 |
if (r < 0) |
| 221 |
return log_error_errno(r, "Failed to allocate seccomp object: %m"); |
| 222 |
|
| 223 |
/* |
| 224 |
Audit is broken in containers, much of the userspace audit hookup will fail if running inside a |
| 225 |
container. We don't care and just turn off creation of audit sockets. |
| 226 |
|
| 227 |
This will make socket(AF_NETLINK, *, NETLINK_AUDIT) fail with EAFNOSUPPORT which audit userspace uses |
| 228 |
as indication that audit is disabled in the kernel. |
| 229 |
*/ |
| 230 |
|
| 231 |
r = sym_seccomp_rule_add_exact( |
| 232 |
seccomp, |
| 233 |
SCMP_ACT_ERRNO(EAFNOSUPPORT), |
| 234 |
SCMP_SYS(socket), |
| 235 |
2, |
| 236 |
SCMP_A0(SCMP_CMP_EQ, AF_NETLINK), |
| 237 |
SCMP_A2(SCMP_CMP_EQ, NETLINK_AUDIT)); |
| 238 |
if (r < 0) { |
| 239 |
log_debug_errno(r, "Failed to add audit seccomp rule, ignoring: %m"); |
| 240 |
continue; |
| 241 |
} |
| 242 |
|
| 243 |
r = sym_seccomp_load(seccomp); |
| 244 |
if (ERRNO_IS_NEG_SECCOMP_FATAL(r)) |
| 245 |
return log_error_errno(r, "Failed to install seccomp audit filter: %m"); |
| 246 |
if (r < 0) |
| 247 |
log_debug_errno(r, "Failed to install filter set for architecture %s, skipping: %m", |
| 248 |
seccomp_arch_to_string(arch)); |
| 249 |
} |
| 250 |
|
| 251 |
if (restrict_address_families_is_allowlist || !set_isempty(restrict_address_families)) { |
| 252 |
r = seccomp_restrict_address_families(restrict_address_families, restrict_address_families_is_allowlist); |
| 253 |
if (r < 0) |
| 254 |
return log_error_errno(r, "Failed to install address family filter: %m"); |
| 255 |
} |
| 256 |
|
| 257 |
return 0; |
| 258 |
} |
| 259 |
|
| 260 |
#else |
| 261 |
|
| 262 |
int setup_seccomp(uint64_t cap_list_retain, char **syscall_allow_list, char **syscall_deny_list, Set *restrict_address_families, bool restrict_address_families_is_allowlist) { |
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
#endif |