| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
/* This needs to be included before the seccomp headers, otherwise missing syscalls will be defined in this |
| 4 |
* file and pass the ifdef, but they won't be defined in the seccomp headers so things like |
| 5 |
* SCMP_SYS(fchmodat2) will resolve as empty and fail the build with older glibc/libseccomp. */ |
| 6 |
#include <sys/syscall.h> |
| 7 |
#include <fcntl.h> |
| 8 |
#include <linux/seccomp.h> |
| 9 |
#include <sched.h> |
| 10 |
#include <sys/mman.h> |
| 11 |
#include <sys/prctl.h> |
| 12 |
#include <sys/shm.h> |
| 13 |
#include <sys/stat.h> |
| 14 |
|
| 15 |
#ifdef __mips__ |
| 16 |
#include <asm/sgidefs.h> |
| 17 |
#endif |
| 18 |
|
| 19 |
#include "sd-dlopen.h" |
| 20 |
|
| 21 |
#include "af-list.h" |
| 22 |
#include "alloc-util.h" |
| 23 |
#include "env-util.h" |
| 24 |
#include "errno-list.h" |
| 25 |
#include "log.h" |
| 26 |
#include "namespace-util.h" |
| 27 |
#include "nsflags.h" |
| 28 |
#include "nulstr-util.h" |
| 29 |
#include "parse-util.h" |
| 30 |
#include "process-util.h" |
| 31 |
#include "seccomp-util.h" |
| 32 |
#include "set.h" |
| 33 |
#include "string-util.h" |
| 34 |
#include "strv.h" |
| 35 |
|
| 36 |
#if HAVE_SECCOMP |
| 37 |
DLSYM_PROTOTYPE(seccomp_api_get) = NULL; |
| 38 |
DLSYM_PROTOTYPE(seccomp_arch_add) = NULL; |
| 39 |
DLSYM_PROTOTYPE(seccomp_arch_exist) = NULL; |
| 40 |
DLSYM_PROTOTYPE(seccomp_arch_native) = NULL; |
| 41 |
DLSYM_PROTOTYPE(seccomp_arch_remove) = NULL; |
| 42 |
DLSYM_PROTOTYPE(seccomp_attr_set) = NULL; |
| 43 |
DLSYM_PROTOTYPE(seccomp_init) = NULL; |
| 44 |
DLSYM_PROTOTYPE(seccomp_load) = NULL; |
| 45 |
DLSYM_PROTOTYPE(seccomp_release) = NULL; |
| 46 |
DLSYM_PROTOTYPE(seccomp_rule_add_array) = NULL; |
| 47 |
DLSYM_PROTOTYPE(seccomp_rule_add_exact) = NULL; |
| 48 |
DLSYM_PROTOTYPE(seccomp_syscall_resolve_name) = NULL; |
| 49 |
DLSYM_PROTOTYPE(seccomp_syscall_resolve_num_arch) = NULL; |
| 50 |
|
| 51 |
/* This array will be modified at runtime as seccomp_restrict_archs is called. */ |
| 52 |
uint32_t seccomp_local_archs[] = { |
| 53 |
|
| 54 |
/* Note: always list the native arch we are compiled as last, so that users can deny-list seccomp(), but our own calls to it still succeed */ |
| 55 |
|
| 56 |
#if defined(__x86_64__) && defined(__ILP32__) |
| 57 |
SCMP_ARCH_X86, |
| 58 |
SCMP_ARCH_X86_64, |
| 59 |
SCMP_ARCH_X32, /* native */ |
| 60 |
#elif defined(__x86_64__) && !defined(__ILP32__) |
| 61 |
SCMP_ARCH_X86, |
| 62 |
SCMP_ARCH_X32, |
| 63 |
SCMP_ARCH_X86_64, /* native */ |
| 64 |
#elif defined(__i386__) |
| 65 |
SCMP_ARCH_X86, |
| 66 |
#elif defined(__aarch64__) |
| 67 |
SCMP_ARCH_ARM, |
| 68 |
SCMP_ARCH_AARCH64, /* native */ |
| 69 |
#elif defined(__arm__) |
| 70 |
SCMP_ARCH_ARM, |
| 71 |
#elif defined(__loongarch_lp64) |
| 72 |
SCMP_ARCH_LOONGARCH64, |
| 73 |
#elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI32 |
| 74 |
SCMP_ARCH_MIPSEL, |
| 75 |
SCMP_ARCH_MIPS, /* native */ |
| 76 |
#elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI32 |
| 77 |
SCMP_ARCH_MIPS, |
| 78 |
SCMP_ARCH_MIPSEL, /* native */ |
| 79 |
#elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI64 |
| 80 |
SCMP_ARCH_MIPSEL, |
| 81 |
SCMP_ARCH_MIPS, |
| 82 |
SCMP_ARCH_MIPSEL64N32, |
| 83 |
SCMP_ARCH_MIPS64N32, |
| 84 |
SCMP_ARCH_MIPSEL64, |
| 85 |
SCMP_ARCH_MIPS64, /* native */ |
| 86 |
#elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_ABI64 |
| 87 |
SCMP_ARCH_MIPS, |
| 88 |
SCMP_ARCH_MIPSEL, |
| 89 |
SCMP_ARCH_MIPS64N32, |
| 90 |
SCMP_ARCH_MIPSEL64N32, |
| 91 |
SCMP_ARCH_MIPS64, |
| 92 |
SCMP_ARCH_MIPSEL64, /* native */ |
| 93 |
#elif defined(__mips__) && __BYTE_ORDER == __BIG_ENDIAN && _MIPS_SIM == _MIPS_SIM_NABI32 |
| 94 |
SCMP_ARCH_MIPSEL, |
| 95 |
SCMP_ARCH_MIPS, |
| 96 |
SCMP_ARCH_MIPSEL64, |
| 97 |
SCMP_ARCH_MIPS64, |
| 98 |
SCMP_ARCH_MIPSEL64N32, |
| 99 |
SCMP_ARCH_MIPS64N32, /* native */ |
| 100 |
#elif defined(__mips__) && __BYTE_ORDER == __LITTLE_ENDIAN && _MIPS_SIM == _MIPS_SIM_NABI32 |
| 101 |
SCMP_ARCH_MIPS, |
| 102 |
SCMP_ARCH_MIPSEL, |
| 103 |
SCMP_ARCH_MIPS64, |
| 104 |
SCMP_ARCH_MIPSEL64, |
| 105 |
SCMP_ARCH_MIPS64N32, |
| 106 |
SCMP_ARCH_MIPSEL64N32, /* native */ |
| 107 |
#elif defined(__hppa64__) |
| 108 |
SCMP_ARCH_PARISC, |
| 109 |
SCMP_ARCH_PARISC64, /* native */ |
| 110 |
#elif defined(__hppa__) |
| 111 |
SCMP_ARCH_PARISC, |
| 112 |
#elif defined(__powerpc64__) && __BYTE_ORDER == __BIG_ENDIAN |
| 113 |
SCMP_ARCH_PPC, |
| 114 |
SCMP_ARCH_PPC64LE, |
| 115 |
SCMP_ARCH_PPC64, /* native */ |
| 116 |
#elif defined(__powerpc64__) && __BYTE_ORDER == __LITTLE_ENDIAN |
| 117 |
SCMP_ARCH_PPC, |
| 118 |
SCMP_ARCH_PPC64, |
| 119 |
SCMP_ARCH_PPC64LE, /* native */ |
| 120 |
#elif defined(__powerpc__) |
| 121 |
SCMP_ARCH_PPC, |
| 122 |
#elif defined(__riscv) && __riscv_xlen == 64 && defined(SCMP_ARCH_RISCV64) |
| 123 |
SCMP_ARCH_RISCV64, |
| 124 |
#elif defined(__s390x__) |
| 125 |
SCMP_ARCH_S390, |
| 126 |
SCMP_ARCH_S390X, /* native */ |
| 127 |
#elif defined(__s390__) |
| 128 |
SCMP_ARCH_S390, |
| 129 |
#endif |
| 130 |
SECCOMP_LOCAL_ARCH_END |
| 131 |
}; |
| 132 |
|
| 133 |
const char* seccomp_arch_to_string(uint32_t c) { |
| 134 |
/* Maintain order used in <seccomp.h>. |
| 135 |
* |
| 136 |
* Names used here should be the same as those used for ConditionArchitecture=, |
| 137 |
* except for "subarchitectures" like x32. */ |
| 138 |
|
| 139 |
switch (c) { |
| 140 |
case SCMP_ARCH_NATIVE: |
| 141 |
return "native"; |
| 142 |
case SCMP_ARCH_X86: |
| 143 |
return "x86"; |
| 144 |
case SCMP_ARCH_X86_64: |
| 145 |
return "x86-64"; |
| 146 |
case SCMP_ARCH_X32: |
| 147 |
return "x32"; |
| 148 |
case SCMP_ARCH_ARM: |
| 149 |
return "arm"; |
| 150 |
case SCMP_ARCH_AARCH64: |
| 151 |
return "arm64"; |
| 152 |
#ifdef SCMP_ARCH_LOONGARCH64 |
| 153 |
case SCMP_ARCH_LOONGARCH64: |
| 154 |
return "loongarch64"; |
| 155 |
#endif |
| 156 |
case SCMP_ARCH_MIPS: |
| 157 |
return "mips"; |
| 158 |
case SCMP_ARCH_MIPS64: |
| 159 |
return "mips64"; |
| 160 |
case SCMP_ARCH_MIPS64N32: |
| 161 |
return "mips64-n32"; |
| 162 |
case SCMP_ARCH_MIPSEL: |
| 163 |
return "mips-le"; |
| 164 |
case SCMP_ARCH_MIPSEL64: |
| 165 |
return "mips64-le"; |
| 166 |
case SCMP_ARCH_MIPSEL64N32: |
| 167 |
return "mips64-le-n32"; |
| 168 |
case SCMP_ARCH_PARISC: |
| 169 |
return "parisc"; |
| 170 |
case SCMP_ARCH_PARISC64: |
| 171 |
return "parisc64"; |
| 172 |
case SCMP_ARCH_PPC: |
| 173 |
return "ppc"; |
| 174 |
case SCMP_ARCH_PPC64: |
| 175 |
return "ppc64"; |
| 176 |
case SCMP_ARCH_PPC64LE: |
| 177 |
return "ppc64-le"; |
| 178 |
#ifdef SCMP_ARCH_RISCV64 |
| 179 |
case SCMP_ARCH_RISCV64: |
| 180 |
return "riscv64"; |
| 181 |
#endif |
| 182 |
case SCMP_ARCH_S390: |
| 183 |
return "s390"; |
| 184 |
case SCMP_ARCH_S390X: |
| 185 |
return "s390x"; |
| 186 |
default: |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
int seccomp_arch_from_string(const char *n, uint32_t *ret) { |
| 192 |
if (!n) |
| 193 |
return -EINVAL; |
| 194 |
|
| 195 |
assert(ret); |
| 196 |
|
| 197 |
if (streq(n, "native")) |
| 198 |
*ret = SCMP_ARCH_NATIVE; |
| 199 |
else if (streq(n, "x86")) |
| 200 |
*ret = SCMP_ARCH_X86; |
| 201 |
else if (streq(n, "x86-64")) |
| 202 |
*ret = SCMP_ARCH_X86_64; |
| 203 |
else if (streq(n, "x32")) |
| 204 |
*ret = SCMP_ARCH_X32; |
| 205 |
else if (streq(n, "arm")) |
| 206 |
*ret = SCMP_ARCH_ARM; |
| 207 |
else if (streq(n, "arm64")) |
| 208 |
*ret = SCMP_ARCH_AARCH64; |
| 209 |
#ifdef SCMP_ARCH_LOONGARCH64 |
| 210 |
else if (streq(n, "loongarch64")) |
| 211 |
*ret = SCMP_ARCH_LOONGARCH64; |
| 212 |
#endif |
| 213 |
else if (streq(n, "mips")) |
| 214 |
*ret = SCMP_ARCH_MIPS; |
| 215 |
else if (streq(n, "mips64")) |
| 216 |
*ret = SCMP_ARCH_MIPS64; |
| 217 |
else if (streq(n, "mips64-n32")) |
| 218 |
*ret = SCMP_ARCH_MIPS64N32; |
| 219 |
else if (streq(n, "mips-le")) |
| 220 |
*ret = SCMP_ARCH_MIPSEL; |
| 221 |
else if (streq(n, "mips64-le")) |
| 222 |
*ret = SCMP_ARCH_MIPSEL64; |
| 223 |
else if (streq(n, "mips64-le-n32")) |
| 224 |
*ret = SCMP_ARCH_MIPSEL64N32; |
| 225 |
else if (streq(n, "parisc")) |
| 226 |
*ret = SCMP_ARCH_PARISC; |
| 227 |
else if (streq(n, "parisc64")) |
| 228 |
*ret = SCMP_ARCH_PARISC64; |
| 229 |
else if (streq(n, "ppc")) |
| 230 |
*ret = SCMP_ARCH_PPC; |
| 231 |
else if (streq(n, "ppc64")) |
| 232 |
*ret = SCMP_ARCH_PPC64; |
| 233 |
else if (streq(n, "ppc64-le")) |
| 234 |
*ret = SCMP_ARCH_PPC64LE; |
| 235 |
#ifdef SCMP_ARCH_RISCV64 |
| 236 |
else if (streq(n, "riscv64")) |
| 237 |
*ret = SCMP_ARCH_RISCV64; |
| 238 |
#endif |
| 239 |
else if (streq(n, "s390")) |
| 240 |
*ret = SCMP_ARCH_S390; |
| 241 |
else if (streq(n, "s390x")) |
| 242 |
*ret = SCMP_ARCH_S390X; |
| 243 |
else |
| 244 |
return -EINVAL; |
| 245 |
|
| 246 |
return 0; |
| 247 |
} |
| 248 |
|
| 249 |
int seccomp_init_for_arch(scmp_filter_ctx *ret, uint32_t arch, uint32_t default_action) { |
| 250 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 251 |
int r; |
| 252 |
|
| 253 |
assert(ret); |
| 254 |
|
| 255 |
/* Much like seccomp_init(), but initializes the filter for one specific architecture only, without affecting |
| 256 |
* any others. Also, turns off the NNP fiddling. */ |
| 257 |
|
| 258 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 259 |
if (r < 0) |
| 260 |
return r; |
| 261 |
|
| 262 |
seccomp = sym_seccomp_init(default_action); |
| 263 |
if (!seccomp) |
| 264 |
return -ENOMEM; |
| 265 |
|
| 266 |
if (arch != SCMP_ARCH_NATIVE && |
| 267 |
arch != sym_seccomp_arch_native()) { |
| 268 |
|
| 269 |
r = sym_seccomp_arch_remove(seccomp, sym_seccomp_arch_native()); |
| 270 |
if (r < 0) |
| 271 |
return r; |
| 272 |
|
| 273 |
r = sym_seccomp_arch_add(seccomp, arch); |
| 274 |
if (r < 0) |
| 275 |
return r; |
| 276 |
|
| 277 |
assert(sym_seccomp_arch_exist(seccomp, arch) >= 0); |
| 278 |
assert(sym_seccomp_arch_exist(seccomp, SCMP_ARCH_NATIVE) == -EEXIST); |
| 279 |
assert(sym_seccomp_arch_exist(seccomp, sym_seccomp_arch_native()) == -EEXIST); |
| 280 |
} else { |
| 281 |
assert(sym_seccomp_arch_exist(seccomp, SCMP_ARCH_NATIVE) >= 0); |
| 282 |
assert(sym_seccomp_arch_exist(seccomp, sym_seccomp_arch_native()) >= 0); |
| 283 |
} |
| 284 |
|
| 285 |
r = sym_seccomp_attr_set(seccomp, SCMP_FLTATR_ACT_BADARCH, SCMP_ACT_ALLOW); |
| 286 |
if (r < 0) |
| 287 |
return r; |
| 288 |
|
| 289 |
r = sym_seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_NNP, 0); |
| 290 |
if (r < 0) |
| 291 |
return r; |
| 292 |
|
| 293 |
#if SCMP_VER_MAJOR >= 3 || (SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 4) |
| 294 |
if (getenv_bool("SYSTEMD_LOG_SECCOMP") > 0) { |
| 295 |
r = sym_seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_LOG, 1); |
| 296 |
if (r < 0) |
| 297 |
log_debug_errno(r, "Failed to enable seccomp event logging: %m"); |
| 298 |
} |
| 299 |
#endif |
| 300 |
|
| 301 |
*ret = TAKE_PTR(seccomp); |
| 302 |
return 0; |
| 303 |
} |
| 304 |
|
| 305 |
static bool is_basic_seccomp_available(void) { |
| 306 |
return prctl(PR_GET_SECCOMP, 0, 0, 0, 0) >= 0; |
| 307 |
} |
| 308 |
|
| 309 |
static bool is_seccomp_filter_available(void) { |
| 310 |
return prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL, 0, 0) < 0 && |
| 311 |
errno == EFAULT; |
| 312 |
} |
| 313 |
|
| 314 |
bool is_seccomp_available(void) { |
| 315 |
static int cached_enabled = -1; |
| 316 |
|
| 317 |
if (cached_enabled < 0) { |
| 318 |
if (dlopen_libseccomp(LOG_DEBUG) < 0) |
| 319 |
return (cached_enabled = false); |
| 320 |
|
| 321 |
int b = secure_getenv_bool("SYSTEMD_SECCOMP"); |
| 322 |
if (b != 0) { |
| 323 |
if (b < 0 && b != -ENXIO) /* ENXIO: env var unset */ |
| 324 |
log_debug_errno(b, "Failed to parse $SYSTEMD_SECCOMP value, ignoring."); |
| 325 |
|
| 326 |
cached_enabled = |
| 327 |
is_basic_seccomp_available() && |
| 328 |
is_seccomp_filter_available(); |
| 329 |
} else |
| 330 |
cached_enabled = false; |
| 331 |
} |
| 332 |
|
| 333 |
return cached_enabled; |
| 334 |
} |
| 335 |
|
| 336 |
const SyscallFilterSet syscall_filter_sets[_SYSCALL_FILTER_SET_MAX] = { |
| 337 |
[SYSCALL_FILTER_SET_DEFAULT] = { |
| 338 |
.name = "@default", |
| 339 |
.help = "System calls that are always permitted", |
| 340 |
.value = |
| 341 |
"@sandbox\0" |
| 342 |
"arch_prctl\0" /* Used during platform-specific initialization by ld-linux.so. */ |
| 343 |
"brk\0" |
| 344 |
"cacheflush\0" |
| 345 |
"clock_getres\0" |
| 346 |
"clock_getres_time64\0" |
| 347 |
"clock_gettime\0" |
| 348 |
"clock_gettime64\0" |
| 349 |
"clock_nanosleep\0" |
| 350 |
"clock_nanosleep_time64\0" |
| 351 |
"execve\0" |
| 352 |
"exit\0" |
| 353 |
"exit_group\0" |
| 354 |
"futex\0" |
| 355 |
"futex_time64\0" |
| 356 |
"futex_waitv\0" |
| 357 |
"get_robust_list\0" |
| 358 |
"get_thread_area\0" |
| 359 |
"getegid\0" |
| 360 |
"getegid32\0" |
| 361 |
"geteuid\0" |
| 362 |
"geteuid32\0" |
| 363 |
"getgid\0" |
| 364 |
"getgid32\0" |
| 365 |
"getgroups\0" |
| 366 |
"getgroups32\0" |
| 367 |
"getpgid\0" |
| 368 |
"getpgrp\0" |
| 369 |
"getpid\0" |
| 370 |
"getppid\0" |
| 371 |
"getrandom\0" |
| 372 |
"getresgid\0" |
| 373 |
"getresgid32\0" |
| 374 |
"getresuid\0" |
| 375 |
"getresuid32\0" |
| 376 |
"getrlimit\0" /* make sure processes can query stack size and such */ |
| 377 |
"getsid\0" |
| 378 |
"gettid\0" |
| 379 |
"gettimeofday\0" |
| 380 |
"getuid\0" |
| 381 |
"getuid32\0" |
| 382 |
"lsm_get_self_attr\0" |
| 383 |
"lsm_list_modules\0" |
| 384 |
"membarrier\0" |
| 385 |
"mmap\0" |
| 386 |
"mmap2\0" |
| 387 |
"mprotect\0" |
| 388 |
"mseal\0" |
| 389 |
"munmap\0" |
| 390 |
"nanosleep\0" |
| 391 |
"pause\0" |
| 392 |
"prlimit64\0" |
| 393 |
"restart_syscall\0" |
| 394 |
"riscv_flush_icache\0" |
| 395 |
"riscv_hwprobe\0" |
| 396 |
"rseq\0" |
| 397 |
"rt_sigreturn\0" |
| 398 |
"sched_getaffinity\0" |
| 399 |
"sched_yield\0" |
| 400 |
"set_robust_list\0" |
| 401 |
"set_thread_area\0" |
| 402 |
"set_tid_address\0" |
| 403 |
"set_tls\0" |
| 404 |
"sigreturn\0" |
| 405 |
"time\0" |
| 406 |
"ugetrlimit\0" |
| 407 |
"uretprobe\0" |
| 408 |
}, |
| 409 |
[SYSCALL_FILTER_SET_AIO] = { |
| 410 |
.name = "@aio", |
| 411 |
.help = "Asynchronous IO", |
| 412 |
.value = |
| 413 |
"io_cancel\0" |
| 414 |
"io_destroy\0" |
| 415 |
"io_getevents\0" |
| 416 |
"io_pgetevents\0" |
| 417 |
"io_pgetevents_time64\0" |
| 418 |
"io_setup\0" |
| 419 |
"io_submit\0" |
| 420 |
"io_uring_enter\0" |
| 421 |
"io_uring_register\0" |
| 422 |
"io_uring_setup\0" |
| 423 |
}, |
| 424 |
[SYSCALL_FILTER_SET_BASIC_IO] = { |
| 425 |
.name = "@basic-io", |
| 426 |
.help = "Basic IO", |
| 427 |
.value = |
| 428 |
"_llseek\0" |
| 429 |
"close\0" |
| 430 |
"close_range\0" |
| 431 |
"dup\0" |
| 432 |
"dup2\0" |
| 433 |
"dup3\0" |
| 434 |
"llseek\0" |
| 435 |
"lseek\0" |
| 436 |
"pread64\0" |
| 437 |
"preadv\0" |
| 438 |
"preadv2\0" |
| 439 |
"pwrite64\0" |
| 440 |
"pwritev\0" |
| 441 |
"pwritev2\0" |
| 442 |
"read\0" |
| 443 |
"readv\0" |
| 444 |
"write\0" |
| 445 |
"writev\0" |
| 446 |
}, |
| 447 |
[SYSCALL_FILTER_SET_CHOWN] = { |
| 448 |
.name = "@chown", |
| 449 |
.help = "Change ownership of files and directories", |
| 450 |
.value = |
| 451 |
"chown\0" |
| 452 |
"chown32\0" |
| 453 |
"fchown\0" |
| 454 |
"fchown32\0" |
| 455 |
"fchownat\0" |
| 456 |
"lchown\0" |
| 457 |
"lchown32\0" |
| 458 |
}, |
| 459 |
[SYSCALL_FILTER_SET_CLOCK] = { |
| 460 |
.name = "@clock", |
| 461 |
.help = "Change the system time", |
| 462 |
.value = |
| 463 |
"adjtimex\0" |
| 464 |
"clock_adjtime\0" |
| 465 |
"clock_adjtime64\0" |
| 466 |
"clock_settime\0" |
| 467 |
"clock_settime64\0" |
| 468 |
"settimeofday\0" |
| 469 |
}, |
| 470 |
[SYSCALL_FILTER_SET_CPU_EMULATION] = { |
| 471 |
.name = "@cpu-emulation", |
| 472 |
.help = "System calls for CPU emulation functionality", |
| 473 |
.value = |
| 474 |
"modify_ldt\0" |
| 475 |
"subpage_prot\0" |
| 476 |
"switch_endian\0" |
| 477 |
"vm86\0" |
| 478 |
"vm86old\0" |
| 479 |
}, |
| 480 |
[SYSCALL_FILTER_SET_DEBUG] = { |
| 481 |
.name = "@debug", |
| 482 |
.help = "Debugging, performance monitoring and tracing functionality", |
| 483 |
.value = |
| 484 |
"lookup_dcookie\0" |
| 485 |
"perf_event_open\0" |
| 486 |
"pidfd_getfd\0" |
| 487 |
"ptrace\0" |
| 488 |
"rtas\0" |
| 489 |
"s390_runtime_instr\0" |
| 490 |
"sys_debug_setcontext\0" |
| 491 |
}, |
| 492 |
[SYSCALL_FILTER_SET_FILE_SYSTEM] = { |
| 493 |
.name = "@file-system", |
| 494 |
.help = "File system operations", |
| 495 |
.value = |
| 496 |
"access\0" |
| 497 |
"chdir\0" |
| 498 |
"chmod\0" |
| 499 |
"close\0" |
| 500 |
"creat\0" |
| 501 |
"faccessat\0" |
| 502 |
"faccessat2\0" |
| 503 |
"fallocate\0" |
| 504 |
"fchdir\0" |
| 505 |
"fchmod\0" |
| 506 |
"fchmodat\0" |
| 507 |
"fchmodat2\0" |
| 508 |
"fcntl\0" |
| 509 |
"fcntl64\0" |
| 510 |
"fgetxattr\0" |
| 511 |
"file_getattr\0" |
| 512 |
"file_setattr\0" |
| 513 |
"flistxattr\0" |
| 514 |
"fremovexattr\0" |
| 515 |
"fsetxattr\0" |
| 516 |
"fstat\0" |
| 517 |
"fstat64\0" |
| 518 |
"fstatat\0" |
| 519 |
"fstatat64\0" |
| 520 |
"fstatfs\0" |
| 521 |
"fstatfs64\0" |
| 522 |
"ftruncate\0" |
| 523 |
"ftruncate64\0" |
| 524 |
"futimesat\0" |
| 525 |
"getcwd\0" |
| 526 |
"getdents\0" |
| 527 |
"getdents64\0" |
| 528 |
"getxattr\0" |
| 529 |
"getxattrat\0" |
| 530 |
"inotify_add_watch\0" |
| 531 |
"inotify_init\0" |
| 532 |
"inotify_init1\0" |
| 533 |
"inotify_rm_watch\0" |
| 534 |
"lgetxattr\0" |
| 535 |
"link\0" |
| 536 |
"linkat\0" |
| 537 |
"listmount\0" |
| 538 |
"listxattr\0" |
| 539 |
"listxattrat\0" |
| 540 |
"llistxattr\0" |
| 541 |
"lremovexattr\0" |
| 542 |
"lsetxattr\0" |
| 543 |
"lstat\0" |
| 544 |
"lstat64\0" |
| 545 |
"mkdir\0" |
| 546 |
"mkdirat\0" |
| 547 |
"mknod\0" |
| 548 |
"mknodat\0" |
| 549 |
"newfstat\0" |
| 550 |
"newfstatat\0" |
| 551 |
"oldfstat\0" |
| 552 |
"oldlstat\0" |
| 553 |
"oldstat\0" |
| 554 |
"open\0" |
| 555 |
"open_tree\0" |
| 556 |
"openat\0" |
| 557 |
"openat2\0" |
| 558 |
"readlink\0" |
| 559 |
"readlinkat\0" |
| 560 |
"removexattr\0" |
| 561 |
"removexattrat\0" |
| 562 |
"rename\0" |
| 563 |
"renameat\0" |
| 564 |
"renameat2\0" |
| 565 |
"rmdir\0" |
| 566 |
"setxattr\0" |
| 567 |
"setxattrat\0" |
| 568 |
"stat\0" |
| 569 |
"stat64\0" |
| 570 |
"statfs\0" |
| 571 |
"statfs64\0" |
| 572 |
"statmount\0" |
| 573 |
"statx\0" |
| 574 |
"symlink\0" |
| 575 |
"symlinkat\0" |
| 576 |
"truncate\0" |
| 577 |
"truncate64\0" |
| 578 |
"unlink\0" |
| 579 |
"unlinkat\0" |
| 580 |
"utime\0" |
| 581 |
"utimensat\0" |
| 582 |
"utimensat_time64\0" |
| 583 |
"utimes\0" |
| 584 |
}, |
| 585 |
[SYSCALL_FILTER_SET_IO_EVENT] = { |
| 586 |
.name = "@io-event", |
| 587 |
.help = "Event loop system calls", |
| 588 |
.value = |
| 589 |
"_newselect\0" |
| 590 |
"epoll_create\0" |
| 591 |
"epoll_create1\0" |
| 592 |
"epoll_ctl\0" |
| 593 |
"epoll_ctl_old\0" |
| 594 |
"epoll_pwait\0" |
| 595 |
"epoll_pwait2\0" |
| 596 |
"epoll_wait\0" |
| 597 |
"epoll_wait_old\0" |
| 598 |
"eventfd\0" |
| 599 |
"eventfd2\0" |
| 600 |
"poll\0" |
| 601 |
"ppoll\0" |
| 602 |
"ppoll_time64\0" |
| 603 |
"pselect6\0" |
| 604 |
"pselect6_time64\0" |
| 605 |
"select\0" |
| 606 |
}, |
| 607 |
[SYSCALL_FILTER_SET_IPC] = { |
| 608 |
.name = "@ipc", |
| 609 |
.help = "SysV IPC, POSIX Message Queues or other IPC", |
| 610 |
.value = |
| 611 |
"ipc\0" |
| 612 |
"memfd_create\0" |
| 613 |
"mq_getsetattr\0" |
| 614 |
"mq_notify\0" |
| 615 |
"mq_open\0" |
| 616 |
"mq_timedreceive\0" |
| 617 |
"mq_timedreceive_time64\0" |
| 618 |
"mq_timedsend\0" |
| 619 |
"mq_timedsend_time64\0" |
| 620 |
"mq_unlink\0" |
| 621 |
"msgctl\0" |
| 622 |
"msgget\0" |
| 623 |
"msgrcv\0" |
| 624 |
"msgsnd\0" |
| 625 |
"pipe\0" |
| 626 |
"pipe2\0" |
| 627 |
"process_madvise\0" |
| 628 |
"process_vm_readv\0" |
| 629 |
"process_vm_writev\0" |
| 630 |
"semctl\0" |
| 631 |
"semget\0" |
| 632 |
"semop\0" |
| 633 |
"semtimedop\0" |
| 634 |
"semtimedop_time64\0" |
| 635 |
"shmat\0" |
| 636 |
"shmctl\0" |
| 637 |
"shmdt\0" |
| 638 |
"shmget\0" |
| 639 |
}, |
| 640 |
[SYSCALL_FILTER_SET_KEYRING] = { |
| 641 |
.name = "@keyring", |
| 642 |
.help = "Kernel keyring access", |
| 643 |
.value = |
| 644 |
"add_key\0" |
| 645 |
"keyctl\0" |
| 646 |
"request_key\0" |
| 647 |
}, |
| 648 |
[SYSCALL_FILTER_SET_MEMLOCK] = { |
| 649 |
.name = "@memlock", |
| 650 |
.help = "Memory locking control", |
| 651 |
.value = |
| 652 |
"mlock\0" |
| 653 |
"mlock2\0" |
| 654 |
"mlockall\0" |
| 655 |
"munlock\0" |
| 656 |
"munlockall\0" |
| 657 |
}, |
| 658 |
[SYSCALL_FILTER_SET_MODULE] = { |
| 659 |
.name = "@module", |
| 660 |
.help = "Loading and unloading of kernel modules", |
| 661 |
.value = |
| 662 |
"delete_module\0" |
| 663 |
"finit_module\0" |
| 664 |
"init_module\0" |
| 665 |
}, |
| 666 |
[SYSCALL_FILTER_SET_MOUNT] = { |
| 667 |
.name = "@mount", |
| 668 |
.help = "Mounting and unmounting of file systems", |
| 669 |
.value = |
| 670 |
"chroot\0" |
| 671 |
"fsconfig\0" |
| 672 |
"fsmount\0" |
| 673 |
"fsopen\0" |
| 674 |
"fspick\0" |
| 675 |
"mount\0" |
| 676 |
"mount_setattr\0" |
| 677 |
"move_mount\0" |
| 678 |
"open_tree_attr\0" |
| 679 |
"pivot_root\0" |
| 680 |
"umount\0" |
| 681 |
"umount2\0" |
| 682 |
}, |
| 683 |
[SYSCALL_FILTER_SET_NETWORK_IO] = { |
| 684 |
.name = "@network-io", |
| 685 |
.help = "Network or Unix socket IO, should not be needed if not network facing", |
| 686 |
.value = |
| 687 |
"accept\0" |
| 688 |
"accept4\0" |
| 689 |
"bind\0" |
| 690 |
"connect\0" |
| 691 |
"getpeername\0" |
| 692 |
"getsockname\0" |
| 693 |
"getsockopt\0" |
| 694 |
"listen\0" |
| 695 |
"recv\0" |
| 696 |
"recvfrom\0" |
| 697 |
"recvmmsg\0" |
| 698 |
"recvmmsg_time64\0" |
| 699 |
"recvmsg\0" |
| 700 |
"send\0" |
| 701 |
"sendmmsg\0" |
| 702 |
"sendmsg\0" |
| 703 |
"sendto\0" |
| 704 |
"setsockopt\0" |
| 705 |
"shutdown\0" |
| 706 |
"socket\0" |
| 707 |
"socketcall\0" |
| 708 |
"socketpair\0" |
| 709 |
}, |
| 710 |
[SYSCALL_FILTER_SET_OBSOLETE] = { |
| 711 |
/* some unknown even to libseccomp */ |
| 712 |
.name = "@obsolete", |
| 713 |
.help = "Unusual, obsolete or unimplemented system calls", |
| 714 |
.value = |
| 715 |
"_sysctl\0" |
| 716 |
"afs_syscall\0" |
| 717 |
"bdflush\0" |
| 718 |
"break\0" |
| 719 |
"create_module\0" |
| 720 |
"ftime\0" |
| 721 |
"get_kernel_syms\0" |
| 722 |
"getpmsg\0" |
| 723 |
"gtty\0" |
| 724 |
"idle\0" |
| 725 |
"lock\0" |
| 726 |
"mpx\0" |
| 727 |
"prof\0" |
| 728 |
"profil\0" |
| 729 |
"putpmsg\0" |
| 730 |
"query_module\0" |
| 731 |
"security\0" |
| 732 |
"sgetmask\0" |
| 733 |
"ssetmask\0" |
| 734 |
"stime\0" |
| 735 |
"stty\0" |
| 736 |
"sysfs\0" |
| 737 |
"tuxcall\0" |
| 738 |
"ulimit\0" |
| 739 |
"uselib\0" |
| 740 |
"ustat\0" |
| 741 |
"vserver\0" |
| 742 |
}, |
| 743 |
[SYSCALL_FILTER_SET_PKEY] = { |
| 744 |
.name = "@pkey", |
| 745 |
.help = "System calls used for memory protection keys", |
| 746 |
.value = |
| 747 |
"pkey_alloc\0" |
| 748 |
"pkey_free\0" |
| 749 |
"pkey_mprotect\0" |
| 750 |
}, |
| 751 |
[SYSCALL_FILTER_SET_PRIVILEGED] = { |
| 752 |
.name = "@privileged", |
| 753 |
.help = "All system calls which need super-user capabilities", |
| 754 |
.value = |
| 755 |
"@chown\0" |
| 756 |
"@clock\0" |
| 757 |
"@module\0" |
| 758 |
"@raw-io\0" |
| 759 |
"@reboot\0" |
| 760 |
"@swap\0" |
| 761 |
"_sysctl\0" |
| 762 |
"acct\0" |
| 763 |
"bpf\0" |
| 764 |
"capset\0" |
| 765 |
"chroot\0" |
| 766 |
"fanotify_init\0" |
| 767 |
"fanotify_mark\0" |
| 768 |
"nfsservctl\0" |
| 769 |
"open_by_handle_at\0" |
| 770 |
"pivot_root\0" |
| 771 |
"quotactl\0" |
| 772 |
"quotactl_fd\0" |
| 773 |
"setdomainname\0" |
| 774 |
"setfsuid\0" |
| 775 |
"setfsuid32\0" |
| 776 |
"setgroups\0" |
| 777 |
"setgroups32\0" |
| 778 |
"sethostname\0" |
| 779 |
"setresuid\0" |
| 780 |
"setresuid32\0" |
| 781 |
"setreuid\0" |
| 782 |
"setreuid32\0" |
| 783 |
"setuid\0" /* We list the explicit system calls here, as @setuid also includes setgid() which is not necessarily privileged */ |
| 784 |
"setuid32\0" |
| 785 |
"vhangup\0" |
| 786 |
}, |
| 787 |
[SYSCALL_FILTER_SET_PROCESS] = { |
| 788 |
.name = "@process", |
| 789 |
.help = "Process control, execution, namespacing operations", |
| 790 |
.value = |
| 791 |
"capget\0" /* Able to query arbitrary processes */ |
| 792 |
"clone\0" |
| 793 |
/* ia64 as the only architecture has clone2, a replacement for clone, but ia64 doesn't |
| 794 |
* implement seccomp, so we don't need to list it at all. C.f. |
| 795 |
* acce2f71779c54086962fefce3833d886c655f62 in the kernel. */ |
| 796 |
"clone3\0" |
| 797 |
"execveat\0" |
| 798 |
"fork\0" |
| 799 |
"getrusage\0" |
| 800 |
"kill\0" |
| 801 |
"pidfd_open\0" |
| 802 |
"pidfd_send_signal\0" |
| 803 |
"prctl\0" |
| 804 |
"rt_sigqueueinfo\0" |
| 805 |
"rt_tgsigqueueinfo\0" |
| 806 |
"setns\0" |
| 807 |
"swapcontext\0" /* Some archs e.g. powerpc32 are using it to do userspace context switches */ |
| 808 |
"tgkill\0" |
| 809 |
"times\0" |
| 810 |
"tkill\0" |
| 811 |
"unshare\0" |
| 812 |
"vfork\0" |
| 813 |
"wait4\0" |
| 814 |
"waitid\0" |
| 815 |
"waitpid\0" |
| 816 |
}, |
| 817 |
[SYSCALL_FILTER_SET_RAW_IO] = { |
| 818 |
.name = "@raw-io", |
| 819 |
.help = "Raw I/O port access", |
| 820 |
.value = |
| 821 |
"ioperm\0" |
| 822 |
"iopl\0" |
| 823 |
"pciconfig_iobase\0" |
| 824 |
"pciconfig_read\0" |
| 825 |
"pciconfig_write\0" |
| 826 |
"s390_pci_mmio_read\0" |
| 827 |
"s390_pci_mmio_write\0" |
| 828 |
}, |
| 829 |
[SYSCALL_FILTER_SET_REBOOT] = { |
| 830 |
.name = "@reboot", |
| 831 |
.help = "Reboot and reboot preparation/kexec", |
| 832 |
.value = |
| 833 |
"kexec_file_load\0" |
| 834 |
"kexec_load\0" |
| 835 |
"reboot\0" |
| 836 |
}, |
| 837 |
[SYSCALL_FILTER_SET_RESOURCES] = { |
| 838 |
.name = "@resources", |
| 839 |
.help = "Alter resource settings", |
| 840 |
.value = |
| 841 |
"ioprio_set\0" |
| 842 |
"mbind\0" |
| 843 |
"migrate_pages\0" |
| 844 |
"move_pages\0" |
| 845 |
"nice\0" |
| 846 |
"sched_setaffinity\0" |
| 847 |
"sched_setattr\0" |
| 848 |
"sched_setparam\0" |
| 849 |
"sched_setscheduler\0" |
| 850 |
"set_mempolicy\0" |
| 851 |
"set_mempolicy_home_node\0" |
| 852 |
"setpriority\0" |
| 853 |
"setrlimit\0" |
| 854 |
}, |
| 855 |
[SYSCALL_FILTER_SET_SANDBOX] = { |
| 856 |
.name = "@sandbox", |
| 857 |
.help = "Sandbox functionality", |
| 858 |
.value = |
| 859 |
"landlock_add_rule\0" |
| 860 |
"landlock_create_ruleset\0" |
| 861 |
"landlock_restrict_self\0" |
| 862 |
"seccomp\0" |
| 863 |
}, |
| 864 |
[SYSCALL_FILTER_SET_SETUID] = { |
| 865 |
.name = "@setuid", |
| 866 |
.help = "Operations for changing user/group credentials", |
| 867 |
.value = |
| 868 |
"setgid\0" |
| 869 |
"setgid32\0" |
| 870 |
"setgroups\0" |
| 871 |
"setgroups32\0" |
| 872 |
"setregid\0" |
| 873 |
"setregid32\0" |
| 874 |
"setresgid\0" |
| 875 |
"setresgid32\0" |
| 876 |
"setresuid\0" |
| 877 |
"setresuid32\0" |
| 878 |
"setreuid\0" |
| 879 |
"setreuid32\0" |
| 880 |
"setuid\0" |
| 881 |
"setuid32\0" |
| 882 |
}, |
| 883 |
[SYSCALL_FILTER_SET_SIGNAL] = { |
| 884 |
.name = "@signal", |
| 885 |
.help = "Process signal handling", |
| 886 |
.value = |
| 887 |
"rt_sigaction\0" |
| 888 |
"rt_sigpending\0" |
| 889 |
"rt_sigprocmask\0" |
| 890 |
"rt_sigsuspend\0" |
| 891 |
"rt_sigtimedwait\0" |
| 892 |
"rt_sigtimedwait_time64\0" |
| 893 |
"sigaction\0" |
| 894 |
"sigaltstack\0" |
| 895 |
"signal\0" |
| 896 |
"signalfd\0" |
| 897 |
"signalfd4\0" |
| 898 |
"sigpending\0" |
| 899 |
"sigprocmask\0" |
| 900 |
"sigsuspend\0" |
| 901 |
}, |
| 902 |
[SYSCALL_FILTER_SET_SWAP] = { |
| 903 |
.name = "@swap", |
| 904 |
.help = "Enable/disable swap devices", |
| 905 |
.value = |
| 906 |
"swapoff\0" |
| 907 |
"swapon\0" |
| 908 |
}, |
| 909 |
[SYSCALL_FILTER_SET_SYNC] = { |
| 910 |
.name = "@sync", |
| 911 |
.help = "Synchronize files and memory to storage", |
| 912 |
.value = |
| 913 |
/* Please also update the list in seccomp_suppress_sync(). */ |
| 914 |
"fdatasync\0" |
| 915 |
"fsync\0" |
| 916 |
"msync\0" |
| 917 |
"sync\0" |
| 918 |
"sync_file_range\0" |
| 919 |
"sync_file_range2\0" |
| 920 |
"syncfs\0" |
| 921 |
}, |
| 922 |
[SYSCALL_FILTER_SET_SYSTEM_SERVICE] = { |
| 923 |
.name = "@system-service", |
| 924 |
.help = "General system service operations", |
| 925 |
.value = |
| 926 |
"@aio\0" |
| 927 |
"@basic-io\0" |
| 928 |
"@chown\0" |
| 929 |
"@default\0" |
| 930 |
"@file-system\0" |
| 931 |
"@io-event\0" |
| 932 |
"@ipc\0" |
| 933 |
"@keyring\0" |
| 934 |
"@memlock\0" |
| 935 |
"@network-io\0" |
| 936 |
"@process\0" |
| 937 |
"@resources\0" |
| 938 |
"@setuid\0" |
| 939 |
"@signal\0" |
| 940 |
"@sync\0" |
| 941 |
"@timer\0" |
| 942 |
"arm_fadvise64_64\0" |
| 943 |
"capget\0" |
| 944 |
"capset\0" |
| 945 |
"copy_file_range\0" |
| 946 |
"fadvise64\0" |
| 947 |
"fadvise64_64\0" |
| 948 |
"flock\0" |
| 949 |
"get_mempolicy\0" |
| 950 |
"getcpu\0" |
| 951 |
"getpriority\0" |
| 952 |
"ioctl\0" |
| 953 |
"ioprio_get\0" |
| 954 |
"kcmp\0" |
| 955 |
"madvise\0" |
| 956 |
"mremap\0" |
| 957 |
"name_to_handle_at\0" |
| 958 |
"oldolduname\0" |
| 959 |
"olduname\0" |
| 960 |
"personality\0" |
| 961 |
"readahead\0" |
| 962 |
"readdir\0" |
| 963 |
"remap_file_pages\0" |
| 964 |
"sched_get_priority_max\0" |
| 965 |
"sched_get_priority_min\0" |
| 966 |
"sched_getattr\0" |
| 967 |
"sched_getparam\0" |
| 968 |
"sched_getscheduler\0" |
| 969 |
"sched_rr_get_interval\0" |
| 970 |
"sched_rr_get_interval_time64\0" |
| 971 |
"sched_yield\0" |
| 972 |
"sendfile\0" |
| 973 |
"sendfile64\0" |
| 974 |
"setfsgid\0" |
| 975 |
"setfsgid32\0" |
| 976 |
"setfsuid\0" |
| 977 |
"setfsuid32\0" |
| 978 |
"setpgid\0" |
| 979 |
"setsid\0" |
| 980 |
"splice\0" |
| 981 |
"sysinfo\0" |
| 982 |
"tee\0" |
| 983 |
"umask\0" |
| 984 |
"uname\0" |
| 985 |
"userfaultfd\0" |
| 986 |
"vmsplice\0" |
| 987 |
}, |
| 988 |
[SYSCALL_FILTER_SET_TIMER] = { |
| 989 |
.name = "@timer", |
| 990 |
.help = "Schedule operations by time", |
| 991 |
.value = |
| 992 |
"alarm\0" |
| 993 |
"getitimer\0" |
| 994 |
"setitimer\0" |
| 995 |
"timer_create\0" |
| 996 |
"timer_delete\0" |
| 997 |
"timer_getoverrun\0" |
| 998 |
"timer_gettime\0" |
| 999 |
"timer_gettime64\0" |
| 1000 |
"timer_settime\0" |
| 1001 |
"timer_settime64\0" |
| 1002 |
"timerfd_create\0" |
| 1003 |
"timerfd_gettime\0" |
| 1004 |
"timerfd_gettime64\0" |
| 1005 |
"timerfd_settime\0" |
| 1006 |
"timerfd_settime64\0" |
| 1007 |
"times\0" |
| 1008 |
}, |
| 1009 |
[SYSCALL_FILTER_SET_KNOWN] = { |
| 1010 |
.name = "@known", |
| 1011 |
.help = "All known syscalls declared in the kernel", |
| 1012 |
.value = |
| 1013 |
"@obsolete\0" |
| 1014 |
#include "syscall-list.inc" |
| 1015 |
}, |
| 1016 |
}; |
| 1017 |
|
| 1018 |
const SyscallFilterSet *syscall_filter_set_find(const char *name) { |
| 1019 |
if (isempty(name) || name[0] != '@') |
| 1020 |
return NULL; |
| 1021 |
|
| 1022 |
for (unsigned i = 0; i < _SYSCALL_FILTER_SET_MAX; i++) |
| 1023 |
if (streq(syscall_filter_sets[i].name, name)) |
| 1024 |
return syscall_filter_sets + i; |
| 1025 |
|
| 1026 |
return NULL; |
| 1027 |
} |
| 1028 |
|
| 1029 |
static int add_syscall_filter_set( |
| 1030 |
scmp_filter_ctx seccomp, |
| 1031 |
const SyscallFilterSet *set, |
| 1032 |
uint32_t action, |
| 1033 |
char **exclude, |
| 1034 |
bool log_missing, |
| 1035 |
char ***added); |
| 1036 |
|
| 1037 |
int seccomp_add_syscall_filter_item( |
| 1038 |
scmp_filter_ctx *seccomp, |
| 1039 |
const char *name, |
| 1040 |
uint32_t action, |
| 1041 |
char **exclude, |
| 1042 |
bool log_missing, |
| 1043 |
char ***added) { |
| 1044 |
|
| 1045 |
assert(seccomp); |
| 1046 |
assert(name); |
| 1047 |
|
| 1048 |
if (strv_contains(exclude, name)) |
| 1049 |
return 0; |
| 1050 |
|
| 1051 |
/* Any syscalls that are handled are added to the *added strv. The pointer |
| 1052 |
* must be either NULL or point to a valid pre-initialized possibly-empty strv. */ |
| 1053 |
|
| 1054 |
if (name[0] == '@') { |
| 1055 |
const SyscallFilterSet *other; |
| 1056 |
|
| 1057 |
other = syscall_filter_set_find(name); |
| 1058 |
if (!other) |
| 1059 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1060 |
"Filter set %s is not known!", |
| 1061 |
name); |
| 1062 |
|
| 1063 |
return add_syscall_filter_set(seccomp, other, action, exclude, log_missing, added); |
| 1064 |
|
| 1065 |
} else { |
| 1066 |
int id, r; |
| 1067 |
|
| 1068 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 1069 |
if (r < 0) |
| 1070 |
return r; |
| 1071 |
|
| 1072 |
id = sym_seccomp_syscall_resolve_name(name); |
| 1073 |
if (id == __NR_SCMP_ERROR) { |
| 1074 |
if (log_missing) |
| 1075 |
log_debug("System call %s is not known, ignoring.", name); |
| 1076 |
return 0; |
| 1077 |
} |
| 1078 |
|
| 1079 |
r = sym_seccomp_rule_add_exact(seccomp, action, id, 0); |
| 1080 |
if (r < 0) { |
| 1081 |
/* If the system call is not known on this architecture, then that's fine, let's ignore it */ |
| 1082 |
bool ignore = r == -EDOM; |
| 1083 |
|
| 1084 |
if (!ignore || log_missing) |
| 1085 |
log_debug_errno(r, "Failed to add rule for system call %s() / %d%s: %m", |
| 1086 |
name, id, ignore ? ", ignoring" : ""); |
| 1087 |
if (!ignore) |
| 1088 |
return r; |
| 1089 |
} |
| 1090 |
|
| 1091 |
if (added) { |
| 1092 |
r = strv_extend(added, name); |
| 1093 |
if (r < 0) |
| 1094 |
return r; |
| 1095 |
} |
| 1096 |
|
| 1097 |
return 0; |
| 1098 |
} |
| 1099 |
} |
| 1100 |
|
| 1101 |
static int add_syscall_filter_set( |
| 1102 |
scmp_filter_ctx seccomp, |
| 1103 |
const SyscallFilterSet *set, |
| 1104 |
uint32_t action, |
| 1105 |
char **exclude, |
| 1106 |
bool log_missing, |
| 1107 |
char ***added) { |
| 1108 |
|
| 1109 |
int r; |
| 1110 |
|
| 1111 |
/* Any syscalls that are handled are added to the *added strv. It needs to be initialized. */ |
| 1112 |
|
| 1113 |
assert(seccomp); |
| 1114 |
assert(set); |
| 1115 |
|
| 1116 |
NULSTR_FOREACH(sys, set->value) { |
| 1117 |
r = seccomp_add_syscall_filter_item(seccomp, sys, action, exclude, log_missing, added); |
| 1118 |
if (r < 0) |
| 1119 |
return r; |
| 1120 |
} |
| 1121 |
|
| 1122 |
return 0; |
| 1123 |
} |
| 1124 |
|
| 1125 |
static uint32_t override_default_action(uint32_t default_action) { |
| 1126 |
/* When the requested filter is an allow-list, and the default action is something critical, we |
| 1127 |
* install ENOSYS as the default action, but it will only apply to syscalls which are not in the |
| 1128 |
* @known set. */ |
| 1129 |
|
| 1130 |
if (default_action == SCMP_ACT_ALLOW) |
| 1131 |
return default_action; |
| 1132 |
|
| 1133 |
if (default_action == SCMP_ACT_LOG) |
| 1134 |
return default_action; |
| 1135 |
|
| 1136 |
return SCMP_ACT_ERRNO(ENOSYS); |
| 1137 |
} |
| 1138 |
|
| 1139 |
int seccomp_load_syscall_filter_set(uint32_t default_action, const SyscallFilterSet *set, uint32_t action, bool log_missing) { |
| 1140 |
uint32_t arch, default_action_override; |
| 1141 |
int r; |
| 1142 |
|
| 1143 |
assert(set); |
| 1144 |
|
| 1145 |
/* The one-stop solution: allocate a seccomp object, add the specified filter to it, and apply it. Once for |
| 1146 |
* each local arch. */ |
| 1147 |
|
| 1148 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 1149 |
if (r < 0) |
| 1150 |
return r; |
| 1151 |
|
| 1152 |
default_action_override = override_default_action(default_action); |
| 1153 |
|
| 1154 |
SECCOMP_FOREACH_LOCAL_ARCH(arch) { |
| 1155 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 1156 |
_cleanup_strv_free_ char **added = NULL; |
| 1157 |
|
| 1158 |
log_trace("Operating on architecture: %s", seccomp_arch_to_string(arch)); |
| 1159 |
|
| 1160 |
r = seccomp_init_for_arch(&seccomp, arch, default_action_override); |
| 1161 |
if (r < 0) |
| 1162 |
return r; |
| 1163 |
|
| 1164 |
r = add_syscall_filter_set(seccomp, set, action, NULL, log_missing, &added); |
| 1165 |
if (r < 0) |
| 1166 |
return log_debug_errno(r, "Failed to add filter set: %m"); |
| 1167 |
|
| 1168 |
if (default_action != default_action_override) |
| 1169 |
NULSTR_FOREACH(name, syscall_filter_sets[SYSCALL_FILTER_SET_KNOWN].value) { |
| 1170 |
int id; |
| 1171 |
|
| 1172 |
id = sym_seccomp_syscall_resolve_name(name); |
| 1173 |
if (id < 0) |
| 1174 |
continue; |
| 1175 |
|
| 1176 |
/* Ignore the syscall if it was already handled above */ |
| 1177 |
if (strv_contains(added, name)) |
| 1178 |
continue; |
| 1179 |
|
| 1180 |
r = sym_seccomp_rule_add_exact(seccomp, default_action, id, 0); |
| 1181 |
if (r < 0 && r != -EDOM) /* EDOM means that the syscall is not available for arch */ |
| 1182 |
return log_debug_errno(r, "Failed to add rule for system call %s() / %d: %m", |
| 1183 |
name, id); |
| 1184 |
} |
| 1185 |
|
| 1186 |
#if (SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 5) || SCMP_VER_MAJOR > 2 |
| 1187 |
/* We have a large filter here, so let's turn on the binary tree mode if possible. */ |
| 1188 |
r = sym_seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_OPTIMIZE, 2); |
| 1189 |
if (r < 0) |
| 1190 |
log_warning_errno(r, "Failed to set SCMP_FLTATR_CTL_OPTIMIZE, ignoring: %m"); |
| 1191 |
#endif |
| 1192 |
|
| 1193 |
r = sym_seccomp_load(seccomp); |
| 1194 |
if (ERRNO_IS_NEG_SECCOMP_FATAL(r)) |
| 1195 |
return r; |
| 1196 |
if (r < 0) |
| 1197 |
log_debug_errno(r, "Failed to install filter set for architecture %s, skipping: %m", |
| 1198 |
seccomp_arch_to_string(arch)); |
| 1199 |
} |
| 1200 |
|
| 1201 |
return 0; |
| 1202 |
} |
| 1203 |
|
| 1204 |
int seccomp_load_syscall_filter_set_raw(uint32_t default_action, Hashmap* filter, uint32_t action, bool log_missing) { |
| 1205 |
uint32_t arch, default_action_override; |
| 1206 |
int r; |
| 1207 |
|
| 1208 |
/* Similar to seccomp_load_syscall_filter_set(), but takes a raw Hashmap* of syscalls, instead |
| 1209 |
* of a SyscallFilterSet* table. */ |
| 1210 |
|
| 1211 |
if (hashmap_isempty(filter) && default_action == SCMP_ACT_ALLOW) |
| 1212 |
return 0; |
| 1213 |
|
| 1214 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 1215 |
if (r < 0) |
| 1216 |
return r; |
| 1217 |
|
| 1218 |
default_action_override = override_default_action(default_action); |
| 1219 |
|
| 1220 |
SECCOMP_FOREACH_LOCAL_ARCH(arch) { |
| 1221 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 1222 |
void *syscall_id, *val; |
| 1223 |
|
| 1224 |
log_trace("Operating on architecture: %s", seccomp_arch_to_string(arch)); |
| 1225 |
|
| 1226 |
r = seccomp_init_for_arch(&seccomp, arch, default_action_override); |
| 1227 |
if (r < 0) |
| 1228 |
return r; |
| 1229 |
|
| 1230 |
HASHMAP_FOREACH_KEY(val, syscall_id, filter) { |
| 1231 |
uint32_t a = action; |
| 1232 |
int id = PTR_TO_INT(syscall_id) - 1; |
| 1233 |
int error = PTR_TO_INT(val); |
| 1234 |
|
| 1235 |
if (error == SECCOMP_ERROR_NUMBER_KILL) |
| 1236 |
a = SCMP_ACT_KILL_PROCESS; |
| 1237 |
else if (action == SCMP_ACT_LOG) |
| 1238 |
a = SCMP_ACT_LOG; |
| 1239 |
else if (error >= 0) |
| 1240 |
a = SCMP_ACT_ERRNO(error); |
| 1241 |
|
| 1242 |
r = sym_seccomp_rule_add_exact(seccomp, a, id, 0); |
| 1243 |
if (r < 0) { |
| 1244 |
/* If the system call is not known on this architecture, then that's |
| 1245 |
* fine, let's ignore it */ |
| 1246 |
_cleanup_free_ char *n = NULL; |
| 1247 |
bool ignore; |
| 1248 |
|
| 1249 |
n = sym_seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE, id); |
| 1250 |
ignore = r == -EDOM; |
| 1251 |
if (!ignore || log_missing) |
| 1252 |
log_debug_errno(r, "Failed to add rule for system call %s() / %d%s: %m", |
| 1253 |
strna(n), id, ignore ? ", ignoring" : ""); |
| 1254 |
if (!ignore) |
| 1255 |
return r; |
| 1256 |
} |
| 1257 |
} |
| 1258 |
|
| 1259 |
if (default_action != default_action_override) |
| 1260 |
NULSTR_FOREACH(name, syscall_filter_sets[SYSCALL_FILTER_SET_KNOWN].value) { |
| 1261 |
int id; |
| 1262 |
|
| 1263 |
id = sym_seccomp_syscall_resolve_name(name); |
| 1264 |
if (id < 0) |
| 1265 |
continue; |
| 1266 |
|
| 1267 |
/* Ignore the syscall if it was already handled above */ |
| 1268 |
if (hashmap_contains(filter, INT_TO_PTR(id + 1))) |
| 1269 |
continue; |
| 1270 |
|
| 1271 |
r = sym_seccomp_rule_add_exact(seccomp, default_action, id, 0); |
| 1272 |
if (r < 0 && r != -EDOM) /* EDOM means that the syscall is not available for arch */ |
| 1273 |
return log_debug_errno(r, "Failed to add rule for system call %s() / %d: %m", |
| 1274 |
name, id); |
| 1275 |
} |
| 1276 |
|
| 1277 |
#if (SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 5) || SCMP_VER_MAJOR > 2 |
| 1278 |
/* We have a large filter here, so let's turn on the binary tree mode if possible. */ |
| 1279 |
r = sym_seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_OPTIMIZE, 2); |
| 1280 |
if (r < 0) |
| 1281 |
log_warning_errno(r, "Failed to set SCMP_FLTATR_CTL_OPTIMIZE, ignoring: %m"); |
| 1282 |
#endif |
| 1283 |
|
| 1284 |
r = sym_seccomp_load(seccomp); |
| 1285 |
if (ERRNO_IS_NEG_SECCOMP_FATAL(r)) |
| 1286 |
return r; |
| 1287 |
if (r < 0) |
| 1288 |
log_debug_errno(r, "Failed to install system call filter for architecture %s, skipping: %m", |
| 1289 |
seccomp_arch_to_string(arch)); |
| 1290 |
} |
| 1291 |
|
| 1292 |
return 0; |
| 1293 |
} |
| 1294 |
|
| 1295 |
int seccomp_parse_syscall_filter( |
| 1296 |
const char *name, |
| 1297 |
int errno_num, |
| 1298 |
Hashmap *filter, |
| 1299 |
SeccompParseFlags flags, |
| 1300 |
const char *unit, |
| 1301 |
const char *filename, |
| 1302 |
unsigned line) { |
| 1303 |
|
| 1304 |
int r; |
| 1305 |
|
| 1306 |
assert(name); |
| 1307 |
assert(filter); |
| 1308 |
|
| 1309 |
if (!FLAGS_SET(flags, SECCOMP_PARSE_INVERT) && errno_num >= 0) |
| 1310 |
return -EINVAL; |
| 1311 |
|
| 1312 |
if (name[0] == '@') { |
| 1313 |
const SyscallFilterSet *set; |
| 1314 |
|
| 1315 |
set = syscall_filter_set_find(name); |
| 1316 |
if (!set) { |
| 1317 |
if (!FLAGS_SET(flags, SECCOMP_PARSE_PERMISSIVE)) |
| 1318 |
return -EINVAL; |
| 1319 |
|
| 1320 |
log_syntax(unit, FLAGS_SET(flags, SECCOMP_PARSE_LOG) ? LOG_WARNING : LOG_DEBUG, filename, line, 0, |
| 1321 |
"Unknown system call group, ignoring: %s", name); |
| 1322 |
return 0; |
| 1323 |
} |
| 1324 |
|
| 1325 |
NULSTR_FOREACH(i, set->value) { |
| 1326 |
/* Call ourselves again, for the group to parse. Note that we downgrade logging here |
| 1327 |
* (i.e. take away the SECCOMP_PARSE_LOG flag) since any issues in the group table |
| 1328 |
* are our own problem, not a problem in user configuration data and we shouldn't |
| 1329 |
* pretend otherwise by complaining about them. */ |
| 1330 |
r = seccomp_parse_syscall_filter(i, errno_num, filter, flags &~ SECCOMP_PARSE_LOG, unit, filename, line); |
| 1331 |
if (r < 0) |
| 1332 |
return r; |
| 1333 |
} |
| 1334 |
} else { |
| 1335 |
int id; |
| 1336 |
|
| 1337 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 1338 |
if (r < 0) { |
| 1339 |
if (!FLAGS_SET(flags, SECCOMP_PARSE_PERMISSIVE)) |
| 1340 |
return r; |
| 1341 |
|
| 1342 |
log_syntax(unit, FLAGS_SET(flags, SECCOMP_PARSE_LOG) ? LOG_INFO : LOG_DEBUG, filename, line, r, |
| 1343 |
"System call %s cannot be resolved as libseccomp is not available, ignoring: %m", name); |
| 1344 |
return 0; |
| 1345 |
} |
| 1346 |
|
| 1347 |
id = sym_seccomp_syscall_resolve_name(name); |
| 1348 |
if (id == __NR_SCMP_ERROR) { |
| 1349 |
if (!FLAGS_SET(flags, SECCOMP_PARSE_PERMISSIVE)) |
| 1350 |
return -EINVAL; |
| 1351 |
|
| 1352 |
log_syntax(unit, FLAGS_SET(flags, SECCOMP_PARSE_LOG) ? LOG_WARNING : LOG_DEBUG, filename, line, 0, |
| 1353 |
"System call %s is not known, ignoring.", name); |
| 1354 |
return 0; |
| 1355 |
} |
| 1356 |
|
| 1357 |
/* If we previously wanted to forbid a syscall and now we want to allow it, then remove it |
| 1358 |
* from the list. The entries in allow-list with non-negative error value will be handled |
| 1359 |
* with SCMP_ACT_ERRNO() instead of the default action. */ |
| 1360 |
if (!FLAGS_SET(flags, SECCOMP_PARSE_INVERT) == FLAGS_SET(flags, SECCOMP_PARSE_ALLOW_LIST) || |
| 1361 |
(FLAGS_SET(flags, SECCOMP_PARSE_INVERT | SECCOMP_PARSE_ALLOW_LIST) && errno_num >= 0)) { |
| 1362 |
r = hashmap_put(filter, INT_TO_PTR(id + 1), INT_TO_PTR(errno_num)); |
| 1363 |
if (r < 0) |
| 1364 |
switch (r) { |
| 1365 |
case -ENOMEM: |
| 1366 |
return FLAGS_SET(flags, SECCOMP_PARSE_LOG) ? log_oom() : -ENOMEM; |
| 1367 |
case -EEXIST: |
| 1368 |
assert_se(hashmap_update(filter, INT_TO_PTR(id + 1), INT_TO_PTR(errno_num)) == 0); |
| 1369 |
break; |
| 1370 |
default: |
| 1371 |
return r; |
| 1372 |
} |
| 1373 |
} else |
| 1374 |
(void) hashmap_remove(filter, INT_TO_PTR(id + 1)); |
| 1375 |
} |
| 1376 |
|
| 1377 |
return 0; |
| 1378 |
} |
| 1379 |
|
| 1380 |
int seccomp_restrict_namespaces(unsigned long retain) { |
| 1381 |
uint32_t arch; |
| 1382 |
int r; |
| 1383 |
|
| 1384 |
if (DEBUG_LOGGING) { |
| 1385 |
_cleanup_free_ char *s = NULL; |
| 1386 |
|
| 1387 |
(void) namespace_flags_to_string(retain, &s); |
| 1388 |
log_debug("Restricting namespace to: %s.", strna(s)); |
| 1389 |
} |
| 1390 |
|
| 1391 |
/* NOOP? */ |
| 1392 |
if (FLAGS_SET(retain, NAMESPACE_FLAGS_ALL)) |
| 1393 |
return 0; |
| 1394 |
|
| 1395 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 1396 |
if (r < 0) |
| 1397 |
return r; |
| 1398 |
|
| 1399 |
SECCOMP_FOREACH_LOCAL_ARCH(arch) { |
| 1400 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 1401 |
|
| 1402 |
log_trace("Operating on architecture: %s", seccomp_arch_to_string(arch)); |
| 1403 |
|
| 1404 |
r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW); |
| 1405 |
if (r < 0) |
| 1406 |
return r; |
| 1407 |
|
| 1408 |
/* We cannot filter on individual flags to clone3(), and we need to disable the |
| 1409 |
* syscall altogether. ENOSYS is used instead of EPERM, so that glibc and other |
| 1410 |
* users shall fall back to clone(), as if on an older kernel. |
| 1411 |
* |
| 1412 |
* C.f. https://github.com/flatpak/flatpak/commit/a10f52a7565c549612c92b8e736a6698a53db330, |
| 1413 |
* https://github.com/moby/moby/issues/42680. */ |
| 1414 |
|
| 1415 |
r = sym_seccomp_rule_add_exact( |
| 1416 |
seccomp, |
| 1417 |
SCMP_ACT_ERRNO(ENOSYS), |
| 1418 |
SCMP_SYS(clone3), |
| 1419 |
0); |
| 1420 |
if (r < 0) |
| 1421 |
log_debug_errno(r, "Failed to add clone3() rule for architecture %s, ignoring: %m", |
| 1422 |
seccomp_arch_to_string(arch)); |
| 1423 |
|
| 1424 |
if ((retain & NAMESPACE_FLAGS_ALL) == 0) |
| 1425 |
/* If every single kind of namespace shall be prohibited, then let's block the whole |
| 1426 |
* setns() syscall altogether. */ |
| 1427 |
r = sym_seccomp_rule_add_exact( |
| 1428 |
seccomp, |
| 1429 |
SCMP_ACT_ERRNO(EPERM), |
| 1430 |
SCMP_SYS(setns), |
| 1431 |
0); |
| 1432 |
else |
| 1433 |
/* Otherwise, block only the invocations with the appropriate flags in the loop |
| 1434 |
* below, but also the special invocation with a zero flags argument, right here. */ |
| 1435 |
r = sym_seccomp_rule_add_exact( |
| 1436 |
seccomp, |
| 1437 |
SCMP_ACT_ERRNO(EPERM), |
| 1438 |
SCMP_SYS(setns), |
| 1439 |
1, |
| 1440 |
SCMP_A1(SCMP_CMP_EQ, 0)); |
| 1441 |
if (r < 0) { |
| 1442 |
log_debug_errno(r, "Failed to add setns() rule for architecture %s, skipping: %m", |
| 1443 |
seccomp_arch_to_string(arch)); |
| 1444 |
continue; |
| 1445 |
} |
| 1446 |
|
| 1447 |
for (unsigned i = 0; namespace_info[i].proc_name; i++) { |
| 1448 |
unsigned long f; |
| 1449 |
|
| 1450 |
f = namespace_info[i].clone_flag; |
| 1451 |
if (FLAGS_SET(retain, f)) { |
| 1452 |
log_debug("Permitting %s.", namespace_info[i].proc_name); |
| 1453 |
continue; |
| 1454 |
} |
| 1455 |
|
| 1456 |
log_trace("Blocking %s.", namespace_info[i].proc_name); |
| 1457 |
|
| 1458 |
r = sym_seccomp_rule_add_exact( |
| 1459 |
seccomp, |
| 1460 |
SCMP_ACT_ERRNO(EPERM), |
| 1461 |
SCMP_SYS(unshare), |
| 1462 |
1, |
| 1463 |
SCMP_A0(SCMP_CMP_MASKED_EQ, f, f)); |
| 1464 |
if (r < 0) { |
| 1465 |
log_debug_errno(r, "Failed to add unshare() rule for architecture %s, skipping: %m", |
| 1466 |
seccomp_arch_to_string(arch)); |
| 1467 |
break; |
| 1468 |
} |
| 1469 |
|
| 1470 |
/* On s390/s390x the first two parameters to clone are switched */ |
| 1471 |
if (!IN_SET(arch, SCMP_ARCH_S390, SCMP_ARCH_S390X)) |
| 1472 |
r = sym_seccomp_rule_add_exact( |
| 1473 |
seccomp, |
| 1474 |
SCMP_ACT_ERRNO(EPERM), |
| 1475 |
SCMP_SYS(clone), |
| 1476 |
1, |
| 1477 |
SCMP_A0(SCMP_CMP_MASKED_EQ, f, f)); |
| 1478 |
else |
| 1479 |
r = sym_seccomp_rule_add_exact( |
| 1480 |
seccomp, |
| 1481 |
SCMP_ACT_ERRNO(EPERM), |
| 1482 |
SCMP_SYS(clone), |
| 1483 |
1, |
| 1484 |
SCMP_A1(SCMP_CMP_MASKED_EQ, f, f)); |
| 1485 |
if (r < 0) { |
| 1486 |
log_debug_errno(r, "Failed to add clone() rule for architecture %s, skipping: %m", |
| 1487 |
seccomp_arch_to_string(arch)); |
| 1488 |
break; |
| 1489 |
} |
| 1490 |
|
| 1491 |
if ((retain & NAMESPACE_FLAGS_ALL) != 0) { |
| 1492 |
r = sym_seccomp_rule_add_exact( |
| 1493 |
seccomp, |
| 1494 |
SCMP_ACT_ERRNO(EPERM), |
| 1495 |
SCMP_SYS(setns), |
| 1496 |
1, |
| 1497 |
SCMP_A1(SCMP_CMP_MASKED_EQ, f, f)); |
| 1498 |
if (r < 0) { |
| 1499 |
log_debug_errno(r, "Failed to add setns() rule for architecture %s, skipping: %m", |
| 1500 |
seccomp_arch_to_string(arch)); |
| 1501 |
break; |
| 1502 |
} |
| 1503 |
} |
| 1504 |
} |
| 1505 |
if (r < 0) |
| 1506 |
continue; |
| 1507 |
|
| 1508 |
r = sym_seccomp_load(seccomp); |
| 1509 |
if (ERRNO_IS_NEG_SECCOMP_FATAL(r)) |
| 1510 |
return r; |
| 1511 |
if (r < 0) |
| 1512 |
log_debug_errno(r, "Failed to install namespace restriction rules for architecture %s, skipping: %m", |
| 1513 |
seccomp_arch_to_string(arch)); |
| 1514 |
} |
| 1515 |
|
| 1516 |
return 0; |
| 1517 |
} |
| 1518 |
|
| 1519 |
int seccomp_protect_sysctl(void) { |
| 1520 |
uint32_t arch; |
| 1521 |
int r; |
| 1522 |
|
| 1523 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 1524 |
if (r < 0) |
| 1525 |
return r; |
| 1526 |
|
| 1527 |
SECCOMP_FOREACH_LOCAL_ARCH(arch) { |
| 1528 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 1529 |
|
| 1530 |
log_trace("Operating on architecture: %s", seccomp_arch_to_string(arch)); |
| 1531 |
|
| 1532 |
if (IN_SET(arch, |
| 1533 |
SCMP_ARCH_AARCH64, |
| 1534 |
#ifdef SCMP_ARCH_LOONGARCH64 |
| 1535 |
SCMP_ARCH_LOONGARCH64, |
| 1536 |
#endif |
| 1537 |
#ifdef SCMP_ARCH_RISCV64 |
| 1538 |
SCMP_ARCH_RISCV64, |
| 1539 |
#endif |
| 1540 |
SCMP_ARCH_X32 |
| 1541 |
)) |
| 1542 |
/* No _sysctl syscall */ |
| 1543 |
continue; |
| 1544 |
|
| 1545 |
r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW); |
| 1546 |
if (r < 0) |
| 1547 |
return r; |
| 1548 |
|
| 1549 |
r = sym_seccomp_rule_add_exact( |
| 1550 |
seccomp, |
| 1551 |
SCMP_ACT_ERRNO(EPERM), |
| 1552 |
SCMP_SYS(_sysctl), |
| 1553 |
0); |
| 1554 |
if (r < 0) { |
| 1555 |
log_debug_errno(r, "Failed to add _sysctl() rule for architecture %s, skipping: %m", |
| 1556 |
seccomp_arch_to_string(arch)); |
| 1557 |
continue; |
| 1558 |
} |
| 1559 |
|
| 1560 |
r = sym_seccomp_load(seccomp); |
| 1561 |
if (ERRNO_IS_NEG_SECCOMP_FATAL(r)) |
| 1562 |
return r; |
| 1563 |
if (r < 0) |
| 1564 |
log_debug_errno(r, "Failed to install sysctl protection rules for architecture %s, skipping: %m", |
| 1565 |
seccomp_arch_to_string(arch)); |
| 1566 |
} |
| 1567 |
|
| 1568 |
return 0; |
| 1569 |
} |
| 1570 |
|
| 1571 |
int seccomp_protect_syslog(void) { |
| 1572 |
uint32_t arch; |
| 1573 |
int r; |
| 1574 |
|
| 1575 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 1576 |
if (r < 0) |
| 1577 |
return r; |
| 1578 |
|
| 1579 |
SECCOMP_FOREACH_LOCAL_ARCH(arch) { |
| 1580 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 1581 |
|
| 1582 |
r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW); |
| 1583 |
if (r < 0) |
| 1584 |
return r; |
| 1585 |
|
| 1586 |
r = sym_seccomp_rule_add_exact( |
| 1587 |
seccomp, |
| 1588 |
SCMP_ACT_ERRNO(EPERM), |
| 1589 |
SCMP_SYS(syslog), |
| 1590 |
0); |
| 1591 |
|
| 1592 |
if (r < 0) { |
| 1593 |
log_debug_errno(r, "Failed to add syslog() rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch)); |
| 1594 |
continue; |
| 1595 |
} |
| 1596 |
|
| 1597 |
r = sym_seccomp_load(seccomp); |
| 1598 |
if (ERRNO_IS_NEG_SECCOMP_FATAL(r)) |
| 1599 |
return r; |
| 1600 |
if (r < 0) |
| 1601 |
log_debug_errno(r, "Failed to install syslog protection rules for architecture %s, skipping: %m", |
| 1602 |
seccomp_arch_to_string(arch)); |
| 1603 |
} |
| 1604 |
|
| 1605 |
return 0; |
| 1606 |
} |
| 1607 |
|
| 1608 |
int seccomp_restrict_address_families(Set *address_families, bool allow_list) { |
| 1609 |
uint32_t arch; |
| 1610 |
int r; |
| 1611 |
|
| 1612 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 1613 |
if (r < 0) |
| 1614 |
return r; |
| 1615 |
|
| 1616 |
SECCOMP_FOREACH_LOCAL_ARCH(arch) { |
| 1617 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 1618 |
bool supported; |
| 1619 |
|
| 1620 |
log_trace("Operating on architecture: %s", seccomp_arch_to_string(arch)); |
| 1621 |
|
| 1622 |
switch (arch) { |
| 1623 |
|
| 1624 |
case SCMP_ARCH_X86_64: |
| 1625 |
case SCMP_ARCH_X32: |
| 1626 |
case SCMP_ARCH_ARM: |
| 1627 |
case SCMP_ARCH_AARCH64: |
| 1628 |
#ifdef SCMP_ARCH_LOONGARCH64 |
| 1629 |
case SCMP_ARCH_LOONGARCH64: |
| 1630 |
#endif |
| 1631 |
case SCMP_ARCH_MIPSEL64N32: |
| 1632 |
case SCMP_ARCH_MIPS64N32: |
| 1633 |
case SCMP_ARCH_MIPSEL64: |
| 1634 |
case SCMP_ARCH_MIPS64: |
| 1635 |
#ifdef SCMP_ARCH_RISCV64 |
| 1636 |
case SCMP_ARCH_RISCV64: |
| 1637 |
#endif |
| 1638 |
/* These we know we support (i.e. are the ones that do not use socketcall()) */ |
| 1639 |
supported = true; |
| 1640 |
break; |
| 1641 |
|
| 1642 |
case SCMP_ARCH_S390: |
| 1643 |
case SCMP_ARCH_S390X: |
| 1644 |
case SCMP_ARCH_X86: |
| 1645 |
case SCMP_ARCH_MIPSEL: |
| 1646 |
case SCMP_ARCH_MIPS: |
| 1647 |
case SCMP_ARCH_PARISC: |
| 1648 |
case SCMP_ARCH_PARISC64: |
| 1649 |
case SCMP_ARCH_PPC: |
| 1650 |
case SCMP_ARCH_PPC64: |
| 1651 |
case SCMP_ARCH_PPC64LE: |
| 1652 |
default: |
| 1653 |
/* These we either know we don't support (i.e. are the ones that do use socketcall()), or we |
| 1654 |
* don't know */ |
| 1655 |
supported = false; |
| 1656 |
} |
| 1657 |
|
| 1658 |
if (!supported) |
| 1659 |
continue; |
| 1660 |
|
| 1661 |
r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW); |
| 1662 |
if (r < 0) |
| 1663 |
return r; |
| 1664 |
|
| 1665 |
if (allow_list) { |
| 1666 |
int first = 0, last = 0; |
| 1667 |
void *afp; |
| 1668 |
|
| 1669 |
/* If this is an allow list, we first block the address families that are out of |
| 1670 |
* range and then everything that is not in the set. First, we find the lowest and |
| 1671 |
* highest address family in the set. */ |
| 1672 |
|
| 1673 |
SET_FOREACH(afp, address_families) { |
| 1674 |
int af = PTR_TO_INT(afp); |
| 1675 |
|
| 1676 |
if (af <= 0 || af >= af_max()) |
| 1677 |
continue; |
| 1678 |
|
| 1679 |
if (first == 0 || af < first) |
| 1680 |
first = af; |
| 1681 |
|
| 1682 |
if (last == 0 || af > last) |
| 1683 |
last = af; |
| 1684 |
} |
| 1685 |
|
| 1686 |
assert((first == 0) == (last == 0)); |
| 1687 |
|
| 1688 |
if (first == 0) { |
| 1689 |
|
| 1690 |
/* No entries in the valid range, block everything */ |
| 1691 |
r = sym_seccomp_rule_add_exact( |
| 1692 |
seccomp, |
| 1693 |
SCMP_ACT_ERRNO(EAFNOSUPPORT), |
| 1694 |
SCMP_SYS(socket), |
| 1695 |
0); |
| 1696 |
if (r < 0) { |
| 1697 |
log_debug_errno(r, "Failed to add socket() rule for architecture %s, skipping: %m", |
| 1698 |
seccomp_arch_to_string(arch)); |
| 1699 |
continue; |
| 1700 |
} |
| 1701 |
|
| 1702 |
} else { |
| 1703 |
|
| 1704 |
/* Block everything below the first entry */ |
| 1705 |
r = sym_seccomp_rule_add_exact( |
| 1706 |
seccomp, |
| 1707 |
SCMP_ACT_ERRNO(EAFNOSUPPORT), |
| 1708 |
SCMP_SYS(socket), |
| 1709 |
1, |
| 1710 |
SCMP_A0(SCMP_CMP_LT, first)); |
| 1711 |
if (r < 0) { |
| 1712 |
log_debug_errno(r, "Failed to add socket() rule for architecture %s, skipping: %m", |
| 1713 |
seccomp_arch_to_string(arch)); |
| 1714 |
continue; |
| 1715 |
} |
| 1716 |
|
| 1717 |
/* Block everything above the last entry */ |
| 1718 |
r = sym_seccomp_rule_add_exact( |
| 1719 |
seccomp, |
| 1720 |
SCMP_ACT_ERRNO(EAFNOSUPPORT), |
| 1721 |
SCMP_SYS(socket), |
| 1722 |
1, |
| 1723 |
SCMP_A0(SCMP_CMP_GT, last)); |
| 1724 |
if (r < 0) { |
| 1725 |
log_debug_errno(r, "Failed to add socket() rule for architecture %s, skipping: %m", |
| 1726 |
seccomp_arch_to_string(arch)); |
| 1727 |
continue; |
| 1728 |
} |
| 1729 |
|
| 1730 |
/* Block everything between the first and last entry */ |
| 1731 |
for (int af = 1; af < af_max(); af++) { |
| 1732 |
|
| 1733 |
if (set_contains(address_families, INT_TO_PTR(af))) |
| 1734 |
continue; |
| 1735 |
|
| 1736 |
r = sym_seccomp_rule_add_exact( |
| 1737 |
seccomp, |
| 1738 |
SCMP_ACT_ERRNO(EAFNOSUPPORT), |
| 1739 |
SCMP_SYS(socket), |
| 1740 |
1, |
| 1741 |
SCMP_A0(SCMP_CMP_EQ, af)); |
| 1742 |
if (r < 0) |
| 1743 |
break; |
| 1744 |
} |
| 1745 |
if (r < 0) { |
| 1746 |
log_debug_errno(r, "Failed to add socket() rule for architecture %s, skipping: %m", |
| 1747 |
seccomp_arch_to_string(arch)); |
| 1748 |
continue; |
| 1749 |
} |
| 1750 |
} |
| 1751 |
|
| 1752 |
} else { |
| 1753 |
void *af; |
| 1754 |
|
| 1755 |
/* If this is a deny list, then generate one rule for each address family that are |
| 1756 |
* then combined in OR checks. */ |
| 1757 |
|
| 1758 |
SET_FOREACH(af, address_families) { |
| 1759 |
r = sym_seccomp_rule_add_exact( |
| 1760 |
seccomp, |
| 1761 |
SCMP_ACT_ERRNO(EAFNOSUPPORT), |
| 1762 |
SCMP_SYS(socket), |
| 1763 |
1, |
| 1764 |
SCMP_A0(SCMP_CMP_EQ, PTR_TO_INT(af))); |
| 1765 |
if (r < 0) |
| 1766 |
break; |
| 1767 |
} |
| 1768 |
if (r < 0) { |
| 1769 |
log_debug_errno(r, "Failed to add socket() rule for architecture %s, skipping: %m", |
| 1770 |
seccomp_arch_to_string(arch)); |
| 1771 |
continue; |
| 1772 |
} |
| 1773 |
} |
| 1774 |
|
| 1775 |
r = sym_seccomp_load(seccomp); |
| 1776 |
if (ERRNO_IS_NEG_SECCOMP_FATAL(r)) |
| 1777 |
return r; |
| 1778 |
if (r < 0) |
| 1779 |
log_debug_errno(r, "Failed to install socket family rules for architecture %s, skipping: %m", |
| 1780 |
seccomp_arch_to_string(arch)); |
| 1781 |
} |
| 1782 |
|
| 1783 |
return 0; |
| 1784 |
} |
| 1785 |
|
| 1786 |
int seccomp_restrict_realtime_full(int error_code) { |
| 1787 |
static const int permitted_policies[] = { |
| 1788 |
SCHED_OTHER, |
| 1789 |
SCHED_BATCH, |
| 1790 |
SCHED_IDLE, |
| 1791 |
}; |
| 1792 |
|
| 1793 |
int r, max_policy = 0; |
| 1794 |
uint32_t arch; |
| 1795 |
|
| 1796 |
assert(error_code > 0); |
| 1797 |
|
| 1798 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 1799 |
if (r < 0) |
| 1800 |
return r; |
| 1801 |
|
| 1802 |
/* Determine the highest policy constant we want to allow */ |
| 1803 |
FOREACH_ELEMENT(policy, permitted_policies) |
| 1804 |
if (*policy > max_policy) |
| 1805 |
max_policy = *policy; |
| 1806 |
|
| 1807 |
SECCOMP_FOREACH_LOCAL_ARCH(arch) { |
| 1808 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 1809 |
int p; |
| 1810 |
|
| 1811 |
log_trace("Operating on architecture: %s", seccomp_arch_to_string(arch)); |
| 1812 |
|
| 1813 |
r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW); |
| 1814 |
if (r < 0) |
| 1815 |
return r; |
| 1816 |
|
| 1817 |
/* Go through all policies with lower values than that, and block them -- unless they appear in the |
| 1818 |
* allow list. */ |
| 1819 |
for (p = 0; p < max_policy; p++) { |
| 1820 |
bool good = false; |
| 1821 |
|
| 1822 |
/* Check if this is in the allow list. */ |
| 1823 |
FOREACH_ELEMENT(policy, permitted_policies) |
| 1824 |
if (*policy == p) { |
| 1825 |
good = true; |
| 1826 |
break; |
| 1827 |
} |
| 1828 |
|
| 1829 |
if (good) |
| 1830 |
continue; |
| 1831 |
|
| 1832 |
/* Deny this policy */ |
| 1833 |
r = sym_seccomp_rule_add_exact( |
| 1834 |
seccomp, |
| 1835 |
SCMP_ACT_ERRNO(error_code), |
| 1836 |
SCMP_SYS(sched_setscheduler), |
| 1837 |
1, |
| 1838 |
SCMP_A1(SCMP_CMP_EQ, p)); |
| 1839 |
if (r < 0) { |
| 1840 |
log_debug_errno(r, "Failed to add scheduler rule for architecture %s, skipping: %m", |
| 1841 |
seccomp_arch_to_string(arch)); |
| 1842 |
continue; |
| 1843 |
} |
| 1844 |
} |
| 1845 |
|
| 1846 |
/* Deny-list all other policies, i.e. the ones with higher values. Note that all comparisons |
| 1847 |
* are unsigned here, hence no need no check for < 0 values. */ |
| 1848 |
r = sym_seccomp_rule_add_exact( |
| 1849 |
seccomp, |
| 1850 |
SCMP_ACT_ERRNO(error_code), |
| 1851 |
SCMP_SYS(sched_setscheduler), |
| 1852 |
1, |
| 1853 |
SCMP_A1(SCMP_CMP_GT, max_policy)); |
| 1854 |
if (r < 0) { |
| 1855 |
log_debug_errno(r, "Failed to add scheduler rule for architecture %s, skipping: %m", |
| 1856 |
seccomp_arch_to_string(arch)); |
| 1857 |
continue; |
| 1858 |
} |
| 1859 |
|
| 1860 |
r = sym_seccomp_load(seccomp); |
| 1861 |
if (ERRNO_IS_NEG_SECCOMP_FATAL(r)) |
| 1862 |
return r; |
| 1863 |
if (r < 0) |
| 1864 |
log_debug_errno(r, "Failed to install realtime protection rules for architecture %s, skipping: %m", |
| 1865 |
seccomp_arch_to_string(arch)); |
| 1866 |
} |
| 1867 |
|
| 1868 |
return 0; |
| 1869 |
} |
| 1870 |
|
| 1871 |
static int add_seccomp_syscall_filter(scmp_filter_ctx seccomp, |
| 1872 |
uint32_t arch, |
| 1873 |
int nr, |
| 1874 |
unsigned arg_cnt, |
| 1875 |
const struct scmp_arg_cmp arg) { |
| 1876 |
int r; |
| 1877 |
|
| 1878 |
r = sym_seccomp_rule_add_exact(seccomp, SCMP_ACT_ERRNO(EPERM), nr, arg_cnt, arg); |
| 1879 |
if (r < 0) { |
| 1880 |
_cleanup_free_ char *n = NULL; |
| 1881 |
|
| 1882 |
n = sym_seccomp_syscall_resolve_num_arch(arch, nr); |
| 1883 |
log_debug_errno(r, "Failed to add %s() rule for architecture %s, skipping: %m", |
| 1884 |
strna(n), |
| 1885 |
seccomp_arch_to_string(arch)); |
| 1886 |
} |
| 1887 |
|
| 1888 |
return r; |
| 1889 |
} |
| 1890 |
|
| 1891 |
/* For known architectures, check that syscalls are indeed defined or not. */ |
| 1892 |
#if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || defined(__loongarch_lp64) || (defined(__riscv) && __riscv_xlen == 64) |
| 1893 |
assert_cc(SCMP_SYS(shmget) > 0); |
| 1894 |
assert_cc(SCMP_SYS(shmat) > 0); |
| 1895 |
assert_cc(SCMP_SYS(shmdt) > 0); |
| 1896 |
#endif |
| 1897 |
|
| 1898 |
int seccomp_memory_deny_write_execute(void) { |
| 1899 |
uint32_t arch; |
| 1900 |
unsigned loaded = 0; |
| 1901 |
int r; |
| 1902 |
|
| 1903 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 1904 |
if (r < 0) |
| 1905 |
return r; |
| 1906 |
|
| 1907 |
SECCOMP_FOREACH_LOCAL_ARCH(arch) { |
| 1908 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 1909 |
int filter_syscall = 0, block_syscall = 0, shmat_syscall = 0; |
| 1910 |
|
| 1911 |
log_trace("Operating on architecture: %s", seccomp_arch_to_string(arch)); |
| 1912 |
|
| 1913 |
switch (arch) { |
| 1914 |
|
| 1915 |
/* Note that on some architectures shmat() isn't available, and the call is multiplexed through ipc(). |
| 1916 |
* We ignore that here, which means there's still a way to get writable/executable |
| 1917 |
* memory, if an IPC key is mapped like this. That's a pity, but no total loss. |
| 1918 |
* |
| 1919 |
* Also, PARISC isn't here right now because it still needs executable memory, but work is in progress |
| 1920 |
* on that front (kernel work done in 5.18). |
| 1921 |
*/ |
| 1922 |
|
| 1923 |
case SCMP_ARCH_X86: |
| 1924 |
case SCMP_ARCH_S390: |
| 1925 |
filter_syscall = SCMP_SYS(mmap2); |
| 1926 |
block_syscall = SCMP_SYS(mmap); |
| 1927 |
/* shmat multiplexed, see above */ |
| 1928 |
break; |
| 1929 |
|
| 1930 |
case SCMP_ARCH_PPC: |
| 1931 |
case SCMP_ARCH_PPC64: |
| 1932 |
case SCMP_ARCH_PPC64LE: |
| 1933 |
case SCMP_ARCH_S390X: |
| 1934 |
filter_syscall = SCMP_SYS(mmap); |
| 1935 |
/* shmat multiplexed, see above */ |
| 1936 |
break; |
| 1937 |
|
| 1938 |
case SCMP_ARCH_ARM: |
| 1939 |
filter_syscall = SCMP_SYS(mmap2); /* arm has only mmap2 */ |
| 1940 |
shmat_syscall = SCMP_SYS(shmat); |
| 1941 |
break; |
| 1942 |
|
| 1943 |
case SCMP_ARCH_X86_64: |
| 1944 |
case SCMP_ARCH_X32: |
| 1945 |
case SCMP_ARCH_AARCH64: |
| 1946 |
#ifdef SCMP_ARCH_LOONGARCH64 |
| 1947 |
case SCMP_ARCH_LOONGARCH64: |
| 1948 |
#endif |
| 1949 |
#ifdef SCMP_ARCH_RISCV64 |
| 1950 |
case SCMP_ARCH_RISCV64: |
| 1951 |
#endif |
| 1952 |
filter_syscall = SCMP_SYS(mmap); /* amd64, x32, arm64, loongarch64 and riscv64 have only mmap */ |
| 1953 |
shmat_syscall = SCMP_SYS(shmat); |
| 1954 |
break; |
| 1955 |
|
| 1956 |
/* Please add more definitions here, if you port systemd to other architectures! */ |
| 1957 |
|
| 1958 |
#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) |
| 1959 |
#warning "Consider adding the right mmap() syscall definitions here!" |
| 1960 |
#endif |
| 1961 |
} |
| 1962 |
|
| 1963 |
/* Can't filter mmap() on this arch, then skip it */ |
| 1964 |
if (filter_syscall == 0) |
| 1965 |
continue; |
| 1966 |
|
| 1967 |
r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW); |
| 1968 |
if (r < 0) |
| 1969 |
return r; |
| 1970 |
|
| 1971 |
r = add_seccomp_syscall_filter(seccomp, arch, filter_syscall, |
| 1972 |
1, |
| 1973 |
SCMP_A2(SCMP_CMP_MASKED_EQ, PROT_EXEC|PROT_WRITE, PROT_EXEC|PROT_WRITE)); |
| 1974 |
if (r < 0) |
| 1975 |
continue; |
| 1976 |
|
| 1977 |
if (block_syscall != 0) { |
| 1978 |
r = add_seccomp_syscall_filter(seccomp, arch, block_syscall, 0, (const struct scmp_arg_cmp){} ); |
| 1979 |
if (r < 0) |
| 1980 |
continue; |
| 1981 |
} |
| 1982 |
|
| 1983 |
r = add_seccomp_syscall_filter(seccomp, arch, SCMP_SYS(mprotect), |
| 1984 |
1, |
| 1985 |
SCMP_A2(SCMP_CMP_MASKED_EQ, PROT_EXEC, PROT_EXEC)); |
| 1986 |
if (r < 0) |
| 1987 |
continue; |
| 1988 |
|
| 1989 |
r = add_seccomp_syscall_filter(seccomp, arch, SCMP_SYS(pkey_mprotect), |
| 1990 |
1, |
| 1991 |
SCMP_A2(SCMP_CMP_MASKED_EQ, PROT_EXEC, PROT_EXEC)); |
| 1992 |
if (r < 0) |
| 1993 |
continue; |
| 1994 |
|
| 1995 |
if (shmat_syscall > 0) { |
| 1996 |
r = add_seccomp_syscall_filter(seccomp, arch, shmat_syscall, |
| 1997 |
1, |
| 1998 |
SCMP_A2(SCMP_CMP_MASKED_EQ, SHM_EXEC, SHM_EXEC)); |
| 1999 |
if (r < 0) |
| 2000 |
continue; |
| 2001 |
} |
| 2002 |
|
| 2003 |
r = sym_seccomp_load(seccomp); |
| 2004 |
if (ERRNO_IS_NEG_SECCOMP_FATAL(r)) |
| 2005 |
return r; |
| 2006 |
if (r < 0) |
| 2007 |
log_debug_errno(r, "Failed to install MemoryDenyWriteExecute= rule for architecture %s, skipping: %m", |
| 2008 |
seccomp_arch_to_string(arch)); |
| 2009 |
loaded++; |
| 2010 |
} |
| 2011 |
|
| 2012 |
if (loaded == 0) |
| 2013 |
log_debug("Failed to install any seccomp rules for MemoryDenyWriteExecute=."); |
| 2014 |
|
| 2015 |
return loaded; |
| 2016 |
} |
| 2017 |
|
| 2018 |
int seccomp_restrict_archs(Set *archs) { |
| 2019 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 2020 |
int r; |
| 2021 |
bool blocked_new = false; |
| 2022 |
|
| 2023 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 2024 |
if (r < 0) |
| 2025 |
return r; |
| 2026 |
|
| 2027 |
/* This installs a filter with no rules, but that restricts the system call architectures to the specified |
| 2028 |
* list. |
| 2029 |
* |
| 2030 |
* There are some qualifications. However the most important use is to stop processes from bypassing |
| 2031 |
* system call restrictions, in case they used a broader (multiplexing) syscall which is only available |
| 2032 |
* in a non-native architecture. There are no holes in this use case, at least so far. */ |
| 2033 |
|
| 2034 |
/* Note libseccomp includes our "native" (current) architecture in the filter by default. |
| 2035 |
* We do not remove it. For example, our callers expect to be able to call execve() afterwards |
| 2036 |
* to run a program with the restrictions applied. */ |
| 2037 |
seccomp = sym_seccomp_init(SCMP_ACT_ALLOW); |
| 2038 |
if (!seccomp) |
| 2039 |
return -ENOMEM; |
| 2040 |
|
| 2041 |
for (unsigned i = 0; seccomp_local_archs[i] != SECCOMP_LOCAL_ARCH_END; ++i) { |
| 2042 |
uint32_t arch = seccomp_local_archs[i]; |
| 2043 |
|
| 2044 |
/* See above comment, our "native" architecture is never blocked. */ |
| 2045 |
if (arch == sym_seccomp_arch_native()) |
| 2046 |
continue; |
| 2047 |
|
| 2048 |
/* That architecture might have already been blocked by a previous call to seccomp_restrict_archs. */ |
| 2049 |
if (arch == SECCOMP_LOCAL_ARCH_BLOCKED) |
| 2050 |
continue; |
| 2051 |
|
| 2052 |
bool block = !set_contains(archs, UINT32_TO_PTR(arch + 1)); |
| 2053 |
|
| 2054 |
/* The vdso for x32 assumes that x86-64 syscalls are available. Let's allow them, since x32 |
| 2055 |
* x32 syscalls should basically match x86-64 for everything except the pointer type. |
| 2056 |
* The important thing is that you can block the old 32-bit x86 syscalls. |
| 2057 |
* https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=850047 */ |
| 2058 |
if (block && arch == SCMP_ARCH_X86_64 && sym_seccomp_arch_native() == SCMP_ARCH_X32) |
| 2059 |
block = !set_contains(archs, UINT32_TO_PTR(SCMP_ARCH_X32 + 1)); |
| 2060 |
|
| 2061 |
if (block) { |
| 2062 |
seccomp_local_archs[i] = SECCOMP_LOCAL_ARCH_BLOCKED; |
| 2063 |
blocked_new = true; |
| 2064 |
} else { |
| 2065 |
r = sym_seccomp_arch_add(seccomp, arch); |
| 2066 |
if (r < 0 && r != -EEXIST) |
| 2067 |
return r; |
| 2068 |
} |
| 2069 |
} |
| 2070 |
|
| 2071 |
/* All architectures that will be blocked by the seccomp program were |
| 2072 |
* already blocked. */ |
| 2073 |
if (!blocked_new) |
| 2074 |
return 0; |
| 2075 |
|
| 2076 |
r = sym_seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_NNP, 0); |
| 2077 |
if (r < 0) |
| 2078 |
return r; |
| 2079 |
|
| 2080 |
r = sym_seccomp_load(seccomp); |
| 2081 |
if (ERRNO_IS_NEG_SECCOMP_FATAL(r)) |
| 2082 |
return r; |
| 2083 |
if (r < 0) |
| 2084 |
log_debug_errno(r, "Failed to restrict system call architectures, skipping: %m"); |
| 2085 |
|
| 2086 |
return 0; |
| 2087 |
} |
| 2088 |
|
| 2089 |
int parse_syscall_archs(char **l, Set **archs) { |
| 2090 |
int r; |
| 2091 |
|
| 2092 |
assert(l); |
| 2093 |
assert(archs); |
| 2094 |
|
| 2095 |
STRV_FOREACH(s, l) { |
| 2096 |
uint32_t a; |
| 2097 |
|
| 2098 |
r = seccomp_arch_from_string(*s, &a); |
| 2099 |
if (r < 0) |
| 2100 |
return -EINVAL; |
| 2101 |
|
| 2102 |
r = set_ensure_put(archs, NULL, UINT32_TO_PTR(a + 1)); |
| 2103 |
if (r < 0) |
| 2104 |
return -ENOMEM; |
| 2105 |
} |
| 2106 |
|
| 2107 |
return 0; |
| 2108 |
} |
| 2109 |
|
| 2110 |
int seccomp_filter_set_add_by_name(Hashmap *filter, bool add, const char *name) { |
| 2111 |
int r; |
| 2112 |
|
| 2113 |
assert(filter); |
| 2114 |
assert(name); |
| 2115 |
|
| 2116 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 2117 |
if (r < 0) |
| 2118 |
return r; |
| 2119 |
|
| 2120 |
if (name[0] == '@') { |
| 2121 |
const SyscallFilterSet *more; |
| 2122 |
|
| 2123 |
more = syscall_filter_set_find(name); |
| 2124 |
if (!more) |
| 2125 |
return -ENXIO; |
| 2126 |
|
| 2127 |
return seccomp_filter_set_add(filter, add, more); |
| 2128 |
} |
| 2129 |
|
| 2130 |
int id = sym_seccomp_syscall_resolve_name(name); |
| 2131 |
if (id == __NR_SCMP_ERROR) { |
| 2132 |
log_debug("System call %s is not known, ignoring.", name); |
| 2133 |
return 0; |
| 2134 |
} |
| 2135 |
|
| 2136 |
if (add) |
| 2137 |
return hashmap_put(filter, INT_TO_PTR(id + 1), INT_TO_PTR(-1)); |
| 2138 |
|
| 2139 |
(void) hashmap_remove(filter, INT_TO_PTR(id + 1)); |
| 2140 |
return 0; |
| 2141 |
} |
| 2142 |
|
| 2143 |
int seccomp_filter_set_add(Hashmap *filter, bool add, const SyscallFilterSet *set) { |
| 2144 |
int r; |
| 2145 |
|
| 2146 |
assert(filter); |
| 2147 |
assert(set); |
| 2148 |
|
| 2149 |
NULSTR_FOREACH(i, set->value) { |
| 2150 |
r = seccomp_filter_set_add_by_name(filter, add, i); |
| 2151 |
if (r < 0) |
| 2152 |
return r; |
| 2153 |
} |
| 2154 |
|
| 2155 |
return 0; |
| 2156 |
} |
| 2157 |
|
| 2158 |
int seccomp_lock_personality(unsigned long personality) { |
| 2159 |
uint32_t arch; |
| 2160 |
int r; |
| 2161 |
|
| 2162 |
if (personality >= PERSONALITY_INVALID) |
| 2163 |
return -EINVAL; |
| 2164 |
|
| 2165 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 2166 |
if (r < 0) |
| 2167 |
return r; |
| 2168 |
|
| 2169 |
SECCOMP_FOREACH_LOCAL_ARCH(arch) { |
| 2170 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 2171 |
|
| 2172 |
r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW); |
| 2173 |
if (r < 0) |
| 2174 |
return r; |
| 2175 |
|
| 2176 |
r = sym_seccomp_rule_add_exact( |
| 2177 |
seccomp, |
| 2178 |
SCMP_ACT_ERRNO(EPERM), |
| 2179 |
SCMP_SYS(personality), |
| 2180 |
1, |
| 2181 |
SCMP_A0(SCMP_CMP_NE, personality)); |
| 2182 |
if (r < 0) { |
| 2183 |
log_debug_errno(r, "Failed to add scheduler rule for architecture %s, skipping: %m", |
| 2184 |
seccomp_arch_to_string(arch)); |
| 2185 |
continue; |
| 2186 |
} |
| 2187 |
|
| 2188 |
r = sym_seccomp_load(seccomp); |
| 2189 |
if (ERRNO_IS_NEG_SECCOMP_FATAL(r)) |
| 2190 |
return r; |
| 2191 |
if (r < 0) |
| 2192 |
log_debug_errno(r, "Failed to enable personality lock for architecture %s, skipping: %m", |
| 2193 |
seccomp_arch_to_string(arch)); |
| 2194 |
} |
| 2195 |
|
| 2196 |
return 0; |
| 2197 |
} |
| 2198 |
|
| 2199 |
int seccomp_protect_hostname(void) { |
| 2200 |
uint32_t arch; |
| 2201 |
int r; |
| 2202 |
|
| 2203 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 2204 |
if (r < 0) |
| 2205 |
return r; |
| 2206 |
|
| 2207 |
SECCOMP_FOREACH_LOCAL_ARCH(arch) { |
| 2208 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 2209 |
|
| 2210 |
r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW); |
| 2211 |
if (r < 0) |
| 2212 |
return r; |
| 2213 |
|
| 2214 |
r = sym_seccomp_rule_add_exact( |
| 2215 |
seccomp, |
| 2216 |
SCMP_ACT_ERRNO(EPERM), |
| 2217 |
SCMP_SYS(sethostname), |
| 2218 |
0); |
| 2219 |
if (r < 0) { |
| 2220 |
log_debug_errno(r, "Failed to add sethostname() rule for architecture %s, skipping: %m", |
| 2221 |
seccomp_arch_to_string(arch)); |
| 2222 |
continue; |
| 2223 |
} |
| 2224 |
|
| 2225 |
r = sym_seccomp_rule_add_exact( |
| 2226 |
seccomp, |
| 2227 |
SCMP_ACT_ERRNO(EPERM), |
| 2228 |
SCMP_SYS(setdomainname), |
| 2229 |
0); |
| 2230 |
if (r < 0) { |
| 2231 |
log_debug_errno(r, "Failed to add setdomainname() rule for architecture %s, skipping: %m", |
| 2232 |
seccomp_arch_to_string(arch)); |
| 2233 |
continue; |
| 2234 |
} |
| 2235 |
|
| 2236 |
r = sym_seccomp_load(seccomp); |
| 2237 |
if (ERRNO_IS_NEG_SECCOMP_FATAL(r)) |
| 2238 |
return r; |
| 2239 |
if (r < 0) |
| 2240 |
log_debug_errno(r, "Failed to apply hostname restrictions for architecture %s, skipping: %m", |
| 2241 |
seccomp_arch_to_string(arch)); |
| 2242 |
} |
| 2243 |
|
| 2244 |
return 0; |
| 2245 |
} |
| 2246 |
|
| 2247 |
static int seccomp_restrict_sxid(scmp_filter_ctx seccomp, mode_t m) { |
| 2248 |
/* Checks the mode_t parameter of the following system calls: |
| 2249 |
* |
| 2250 |
* → chmod() + fchmod() + fchmodat() + fchmodat2() |
| 2251 |
* → open() + creat() + openat() |
| 2252 |
* → mkdir() + mkdirat() |
| 2253 |
* → mknod() + mknodat() |
| 2254 |
* |
| 2255 |
* Returns error if *everything* failed, and 0 otherwise. |
| 2256 |
*/ |
| 2257 |
int r; |
| 2258 |
bool any = false; |
| 2259 |
|
| 2260 |
r = sym_seccomp_rule_add_exact( |
| 2261 |
seccomp, |
| 2262 |
SCMP_ACT_ERRNO(EPERM), |
| 2263 |
SCMP_SYS(chmod), |
| 2264 |
1, |
| 2265 |
SCMP_A1(SCMP_CMP_MASKED_EQ, m, m)); |
| 2266 |
if (r < 0) |
| 2267 |
log_debug_errno(r, "Failed to add filter for chmod: %m"); |
| 2268 |
else |
| 2269 |
any = true; |
| 2270 |
|
| 2271 |
r = sym_seccomp_rule_add_exact( |
| 2272 |
seccomp, |
| 2273 |
SCMP_ACT_ERRNO(EPERM), |
| 2274 |
SCMP_SYS(fchmod), |
| 2275 |
1, |
| 2276 |
SCMP_A1(SCMP_CMP_MASKED_EQ, m, m)); |
| 2277 |
if (r < 0) |
| 2278 |
log_debug_errno(r, "Failed to add filter for fchmod: %m"); |
| 2279 |
else |
| 2280 |
any = true; |
| 2281 |
|
| 2282 |
r = sym_seccomp_rule_add_exact( |
| 2283 |
seccomp, |
| 2284 |
SCMP_ACT_ERRNO(EPERM), |
| 2285 |
SCMP_SYS(fchmodat), |
| 2286 |
1, |
| 2287 |
SCMP_A2(SCMP_CMP_MASKED_EQ, m, m)); |
| 2288 |
if (r < 0) |
| 2289 |
log_debug_errno(r, "Failed to add filter for fchmodat: %m"); |
| 2290 |
else |
| 2291 |
any = true; |
| 2292 |
|
| 2293 |
#if defined(__SNR_fchmodat2) |
| 2294 |
r = sym_seccomp_rule_add_exact( |
| 2295 |
seccomp, |
| 2296 |
SCMP_ACT_ERRNO(EPERM), |
| 2297 |
SCMP_SYS(fchmodat2), |
| 2298 |
1, |
| 2299 |
SCMP_A2(SCMP_CMP_MASKED_EQ, m, m)); |
| 2300 |
#else |
| 2301 |
/* It looks like this libseccomp does not know about fchmodat2(). |
| 2302 |
* Pretend the fchmodat2() system call is not supported at all, |
| 2303 |
* regardless of the kernel version. */ |
| 2304 |
r = sym_seccomp_rule_add_exact( |
| 2305 |
seccomp, |
| 2306 |
SCMP_ACT_ERRNO(ENOSYS), |
| 2307 |
__NR_fchmodat2, |
| 2308 |
0); |
| 2309 |
#endif |
| 2310 |
if (r < 0) |
| 2311 |
log_debug_errno(r, "Failed to add filter for fchmodat2: %m"); |
| 2312 |
else |
| 2313 |
any = true; |
| 2314 |
|
| 2315 |
r = sym_seccomp_rule_add_exact( |
| 2316 |
seccomp, |
| 2317 |
SCMP_ACT_ERRNO(EPERM), |
| 2318 |
SCMP_SYS(mkdir), |
| 2319 |
1, |
| 2320 |
SCMP_A1(SCMP_CMP_MASKED_EQ, m, m)); |
| 2321 |
if (r < 0) |
| 2322 |
log_debug_errno(r, "Failed to add filter for mkdir: %m"); |
| 2323 |
else |
| 2324 |
any = true; |
| 2325 |
|
| 2326 |
r = sym_seccomp_rule_add_exact( |
| 2327 |
seccomp, |
| 2328 |
SCMP_ACT_ERRNO(EPERM), |
| 2329 |
SCMP_SYS(mkdirat), |
| 2330 |
1, |
| 2331 |
SCMP_A2(SCMP_CMP_MASKED_EQ, m, m)); |
| 2332 |
if (r < 0) |
| 2333 |
log_debug_errno(r, "Failed to add filter for mkdirat: %m"); |
| 2334 |
else |
| 2335 |
any = true; |
| 2336 |
|
| 2337 |
r = sym_seccomp_rule_add_exact( |
| 2338 |
seccomp, |
| 2339 |
SCMP_ACT_ERRNO(EPERM), |
| 2340 |
SCMP_SYS(mknod), |
| 2341 |
1, |
| 2342 |
SCMP_A1(SCMP_CMP_MASKED_EQ, m, m)); |
| 2343 |
if (r < 0) |
| 2344 |
log_debug_errno(r, "Failed to add filter for mknod: %m"); |
| 2345 |
else |
| 2346 |
any = true; |
| 2347 |
|
| 2348 |
r = sym_seccomp_rule_add_exact( |
| 2349 |
seccomp, |
| 2350 |
SCMP_ACT_ERRNO(EPERM), |
| 2351 |
SCMP_SYS(mknodat), |
| 2352 |
1, |
| 2353 |
SCMP_A2(SCMP_CMP_MASKED_EQ, m, m)); |
| 2354 |
if (r < 0) |
| 2355 |
log_debug_errno(r, "Failed to add filter for mknodat: %m"); |
| 2356 |
else |
| 2357 |
any = true; |
| 2358 |
|
| 2359 |
r = sym_seccomp_rule_add_exact( |
| 2360 |
seccomp, |
| 2361 |
SCMP_ACT_ERRNO(EPERM), |
| 2362 |
SCMP_SYS(open), |
| 2363 |
2, |
| 2364 |
SCMP_A1(SCMP_CMP_MASKED_EQ, O_CREAT, O_CREAT), |
| 2365 |
SCMP_A2(SCMP_CMP_MASKED_EQ, m, m)); |
| 2366 |
if (r < 0) |
| 2367 |
log_debug_errno(r, "Failed to add filter for open: %m"); |
| 2368 |
else |
| 2369 |
any = true; |
| 2370 |
|
| 2371 |
r = sym_seccomp_rule_add_exact( |
| 2372 |
seccomp, |
| 2373 |
SCMP_ACT_ERRNO(EPERM), |
| 2374 |
SCMP_SYS(openat), |
| 2375 |
2, |
| 2376 |
SCMP_A2(SCMP_CMP_MASKED_EQ, O_CREAT, O_CREAT), |
| 2377 |
SCMP_A3(SCMP_CMP_MASKED_EQ, m, m)); |
| 2378 |
if (r < 0) |
| 2379 |
log_debug_errno(r, "Failed to add filter for openat: %m"); |
| 2380 |
else |
| 2381 |
any = true; |
| 2382 |
|
| 2383 |
#if defined(__SNR_openat2) |
| 2384 |
/* The new openat2() system call can't be filtered sensibly, since it moves the flags parameter into |
| 2385 |
* an indirect structure. Let's block it entirely for now. That should be a reasonably OK thing to do |
| 2386 |
* for now, since openat2() is very new and code generally needs fallback logic anyway to be |
| 2387 |
* compatible with kernels that are not absolutely recent. We would normally return EPERM for a |
| 2388 |
* policy check, but this isn't strictly a policy check. Instead, we return ENOSYS to force programs |
| 2389 |
* to call open() or openat() instead. We can properly enforce policy for those functions. */ |
| 2390 |
r = sym_seccomp_rule_add_exact( |
| 2391 |
seccomp, |
| 2392 |
SCMP_ACT_ERRNO(ENOSYS), |
| 2393 |
SCMP_SYS(openat2), |
| 2394 |
0); |
| 2395 |
if (r < 0) |
| 2396 |
log_debug_errno(r, "Failed to add filter for openat2: %m"); |
| 2397 |
else |
| 2398 |
any = true; |
| 2399 |
#endif |
| 2400 |
|
| 2401 |
r = sym_seccomp_rule_add_exact( |
| 2402 |
seccomp, |
| 2403 |
SCMP_ACT_ERRNO(EPERM), |
| 2404 |
SCMP_SYS(creat), |
| 2405 |
1, |
| 2406 |
SCMP_A1(SCMP_CMP_MASKED_EQ, m, m)); |
| 2407 |
if (r < 0) |
| 2408 |
log_debug_errno(r, "Failed to add filter for creat: %m"); |
| 2409 |
else |
| 2410 |
any = true; |
| 2411 |
|
| 2412 |
return any ? 0 : r; |
| 2413 |
} |
| 2414 |
|
| 2415 |
int seccomp_restrict_suid_sgid(void) { |
| 2416 |
uint32_t arch; |
| 2417 |
int r, k; |
| 2418 |
|
| 2419 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 2420 |
if (r < 0) |
| 2421 |
return r; |
| 2422 |
|
| 2423 |
SECCOMP_FOREACH_LOCAL_ARCH(arch) { |
| 2424 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 2425 |
|
| 2426 |
r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW); |
| 2427 |
if (r < 0) |
| 2428 |
return r; |
| 2429 |
|
| 2430 |
r = seccomp_restrict_sxid(seccomp, S_ISUID); |
| 2431 |
if (r < 0) |
| 2432 |
log_debug_errno(r, "Failed to add suid rule for architecture %s, ignoring: %m", |
| 2433 |
seccomp_arch_to_string(arch)); |
| 2434 |
|
| 2435 |
k = seccomp_restrict_sxid(seccomp, S_ISGID); |
| 2436 |
if (k < 0) |
| 2437 |
log_debug_errno(k, "Failed to add sgid rule for architecture %s, ignoring: %m", |
| 2438 |
seccomp_arch_to_string(arch)); |
| 2439 |
|
| 2440 |
if (r < 0 && k < 0) |
| 2441 |
continue; |
| 2442 |
|
| 2443 |
r = sym_seccomp_load(seccomp); |
| 2444 |
if (ERRNO_IS_NEG_SECCOMP_FATAL(r)) |
| 2445 |
return r; |
| 2446 |
if (r < 0) |
| 2447 |
log_debug_errno(r, "Failed to apply suid/sgid restrictions for architecture %s, skipping: %m", |
| 2448 |
seccomp_arch_to_string(arch)); |
| 2449 |
} |
| 2450 |
|
| 2451 |
return 0; |
| 2452 |
} |
| 2453 |
|
| 2454 |
int parse_syscall_and_errno(const char *in, char **name, int *error) { |
| 2455 |
_cleanup_free_ char *n = NULL; |
| 2456 |
const char *p; |
| 2457 |
int e = -1; |
| 2458 |
|
| 2459 |
assert(in); |
| 2460 |
assert(name); |
| 2461 |
assert(error); |
| 2462 |
|
| 2463 |
/* |
| 2464 |
* This parse "syscall:errno" like "uname:EILSEQ", "@sync:255". |
| 2465 |
* If errno is omitted, then error is set to -1. |
| 2466 |
* Empty syscall name is not allowed. |
| 2467 |
* Here, we do not check that the syscall name is valid or not. |
| 2468 |
*/ |
| 2469 |
|
| 2470 |
p = strchr(in, ':'); |
| 2471 |
if (p) { |
| 2472 |
e = seccomp_parse_errno_or_action(p + 1); |
| 2473 |
if (e < 0) |
| 2474 |
return e; |
| 2475 |
|
| 2476 |
n = strndup(in, p - in); |
| 2477 |
} else |
| 2478 |
n = strdup(in); |
| 2479 |
|
| 2480 |
if (!n) |
| 2481 |
return -ENOMEM; |
| 2482 |
|
| 2483 |
if (isempty(n)) |
| 2484 |
return -EINVAL; |
| 2485 |
|
| 2486 |
*error = e; |
| 2487 |
*name = TAKE_PTR(n); |
| 2488 |
|
| 2489 |
return 0; |
| 2490 |
} |
| 2491 |
|
| 2492 |
static int block_open_flag(scmp_filter_ctx seccomp, int flag) { |
| 2493 |
bool any = false; |
| 2494 |
int r; |
| 2495 |
|
| 2496 |
/* Blocks open() with the specified flag, where flag is O_SYNC or so. This makes these calls return |
| 2497 |
* EINVAL, in the hope the client code will retry without O_SYNC then. */ |
| 2498 |
|
| 2499 |
r = sym_seccomp_rule_add_exact( |
| 2500 |
seccomp, |
| 2501 |
SCMP_ACT_ERRNO(EINVAL), |
| 2502 |
SCMP_SYS(open), |
| 2503 |
1, |
| 2504 |
SCMP_A1(SCMP_CMP_MASKED_EQ, flag, flag)); |
| 2505 |
if (r < 0) |
| 2506 |
log_debug_errno(r, "Failed to add filter for open: %m"); |
| 2507 |
else |
| 2508 |
any = true; |
| 2509 |
|
| 2510 |
r = sym_seccomp_rule_add_exact( |
| 2511 |
seccomp, |
| 2512 |
SCMP_ACT_ERRNO(EINVAL), |
| 2513 |
SCMP_SYS(openat), |
| 2514 |
1, |
| 2515 |
SCMP_A2(SCMP_CMP_MASKED_EQ, flag, flag)); |
| 2516 |
if (r < 0) |
| 2517 |
log_debug_errno(r, "Failed to add filter for openat: %m"); |
| 2518 |
else |
| 2519 |
any = true; |
| 2520 |
|
| 2521 |
#if defined(__SNR_openat2) |
| 2522 |
/* The new openat2() system call can't be filtered sensibly, see above. */ |
| 2523 |
r = sym_seccomp_rule_add_exact( |
| 2524 |
seccomp, |
| 2525 |
SCMP_ACT_ERRNO(ENOSYS), |
| 2526 |
SCMP_SYS(openat2), |
| 2527 |
0); |
| 2528 |
if (r < 0) |
| 2529 |
log_debug_errno(r, "Failed to add filter for openat2: %m"); |
| 2530 |
else |
| 2531 |
any = true; |
| 2532 |
#endif |
| 2533 |
|
| 2534 |
return any ? 0 : r; |
| 2535 |
} |
| 2536 |
|
| 2537 |
int seccomp_suppress_sync(void) { |
| 2538 |
uint32_t arch; |
| 2539 |
int r; |
| 2540 |
|
| 2541 |
/* This behaves slightly differently from SystemCallFilter=~@sync:0, in that negative fds (which |
| 2542 |
* we can determine to be invalid) are still refused with EBADF. See #34478. |
| 2543 |
* |
| 2544 |
* Additionally, O_SYNC/O_DSYNC are masked. */ |
| 2545 |
|
| 2546 |
r = dlopen_libseccomp(LOG_DEBUG); |
| 2547 |
if (r < 0) |
| 2548 |
return r; |
| 2549 |
|
| 2550 |
SECCOMP_FOREACH_LOCAL_ARCH(arch) { |
| 2551 |
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL; |
| 2552 |
|
| 2553 |
r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW); |
| 2554 |
if (r < 0) |
| 2555 |
return r; |
| 2556 |
|
| 2557 |
NULSTR_FOREACH(c, syscall_filter_sets[SYSCALL_FILTER_SET_SYNC].value) { |
| 2558 |
int id; |
| 2559 |
|
| 2560 |
id = sym_seccomp_syscall_resolve_name(c); |
| 2561 |
if (id == __NR_SCMP_ERROR) { |
| 2562 |
log_debug("System call %s is not known, ignoring.", c); |
| 2563 |
continue; |
| 2564 |
} |
| 2565 |
|
| 2566 |
if (STR_IN_SET(c, "fdatasync", "fsync", "sync_file_range", "sync_file_range2", "syncfs")) |
| 2567 |
r = sym_seccomp_rule_add_exact( |
| 2568 |
seccomp, |
| 2569 |
SCMP_ACT_ERRNO(0), /* success → we want this to be a NOP after all */ |
| 2570 |
id, |
| 2571 |
1, |
| 2572 |
SCMP_A0(SCMP_CMP_LE, INT_MAX)); /* The rule handles arguments in unsigned. Hence, this |
| 2573 |
* means non-negative fd matches the rule, and the negative |
| 2574 |
* fd passed to the syscall (then it fails with EBADF). */ |
| 2575 |
else |
| 2576 |
r = sym_seccomp_rule_add_exact( |
| 2577 |
seccomp, |
| 2578 |
SCMP_ACT_ERRNO(0), /* success → we want this to be a NOP after all */ |
| 2579 |
id, |
| 2580 |
0); |
| 2581 |
if (r < 0) |
| 2582 |
log_debug_errno(r, "Failed to add filter for system call %s, ignoring: %m", c); |
| 2583 |
} |
| 2584 |
|
| 2585 |
(void) block_open_flag(seccomp, O_SYNC); |
| 2586 |
#if O_DSYNC != O_SYNC |
| 2587 |
(void) block_open_flag(seccomp, O_DSYNC); |
| 2588 |
#endif |
| 2589 |
|
| 2590 |
r = sym_seccomp_load(seccomp); |
| 2591 |
if (ERRNO_IS_NEG_SECCOMP_FATAL(r)) |
| 2592 |
return r; |
| 2593 |
if (r < 0) |
| 2594 |
log_debug_errno(r, "Failed to apply sync() suppression for architecture %s, skipping: %m", |
| 2595 |
seccomp_arch_to_string(arch)); |
| 2596 |
} |
| 2597 |
|
| 2598 |
return 0; |
| 2599 |
} |
| 2600 |
|
| 2601 |
#endif |
| 2602 |
|
| 2603 |
int dlopen_libseccomp(int log_level) { |
| 2604 |
#if HAVE_SECCOMP |
| 2605 |
static void *libseccomp_dl = NULL; |
| 2606 |
|
| 2607 |
LIBSECCOMP_NOTE(SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED); |
| 2608 |
|
| 2609 |
return dlopen_many_sym_or_warn( |
| 2610 |
&libseccomp_dl, |
| 2611 |
"libseccomp.so.2", |
| 2612 |
log_level, |
| 2613 |
DLSYM_ARG(seccomp_api_get), |
| 2614 |
DLSYM_ARG(seccomp_arch_add), |
| 2615 |
DLSYM_ARG(seccomp_arch_exist), |
| 2616 |
DLSYM_ARG(seccomp_arch_native), |
| 2617 |
DLSYM_ARG(seccomp_arch_remove), |
| 2618 |
DLSYM_ARG(seccomp_attr_set), |
| 2619 |
DLSYM_ARG(seccomp_init), |
| 2620 |
DLSYM_ARG(seccomp_load), |
| 2621 |
DLSYM_ARG(seccomp_release), |
| 2622 |
DLSYM_ARG(seccomp_rule_add_array), |
| 2623 |
DLSYM_ARG(seccomp_rule_add_exact), |
| 2624 |
DLSYM_ARG(seccomp_syscall_resolve_name), |
| 2625 |
DLSYM_ARG(seccomp_syscall_resolve_num_arch)); |
| 2626 |
#else |
| 2627 |
return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 2628 |
"libseccomp support is not compiled in."); |
| 2629 |
#endif |
| 2630 |
} |
| 2631 |
|
| 2632 |
bool seccomp_errno_or_action_is_valid(int n) { |
| 2633 |
return n == SECCOMP_ERROR_NUMBER_KILL || errno_is_valid(n); |
| 2634 |
} |
| 2635 |
|
| 2636 |
int seccomp_parse_errno_or_action(const char *p) { |
| 2637 |
if (streq_ptr(p, "kill")) |
| 2638 |
return SECCOMP_ERROR_NUMBER_KILL; |
| 2639 |
return parse_errno(p); |
| 2640 |
} |
| 2641 |
|
| 2642 |
const char* seccomp_errno_or_action_to_string(int num) { |
| 2643 |
if (num == SECCOMP_ERROR_NUMBER_KILL) |
| 2644 |
return "kill"; |
| 2645 |
return errno_name_no_fallback(num); |
| 2646 |
} |