| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <poll.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <sys/eventfd.h> |
| 7 |
#include <sys/mman.h> |
| 8 |
#include <sys/personality.h> |
| 9 |
#include <sys/shm.h> |
| 10 |
#include <sys/socket.h> |
| 11 |
#include <sys/stat.h> |
| 12 |
#include <unistd.h> |
| 13 |
#if HAVE_VALGRIND_VALGRIND_H |
| 14 |
#include <valgrind/valgrind.h> |
| 15 |
#endif |
| 16 |
|
| 17 |
#include "alloc-util.h" |
| 18 |
#include "capability-util.h" |
| 19 |
#include "errno-list.h" |
| 20 |
#include "fd-util.h" |
| 21 |
#include "fileio.h" |
| 22 |
#include "fs-util.h" |
| 23 |
#include "memory-util.h" |
| 24 |
#include "nsflags.h" |
| 25 |
#include "nulstr-util.h" |
| 26 |
#include "process-util.h" |
| 27 |
#include "raw-clone.h" |
| 28 |
#include "rm-rf.h" |
| 29 |
#include "seccomp-util.h" |
| 30 |
#include "set.h" |
| 31 |
#include "string-util.h" |
| 32 |
#include "tests.h" |
| 33 |
#include "tmpfile-util.h" |
| 34 |
#include "virt.h" |
| 35 |
|
| 36 |
/* __NR_socket may be invalid due to libseccomp */ |
| 37 |
#if !defined(__NR_socket) || __NR_socket < 0 || defined(__i386__) || defined(__s390x__) || defined(__s390__) || defined(__powerpc64__) || defined(__powerpc__) |
| 38 |
/* On these archs, socket() is implemented via the socketcall() syscall multiplexer, |
| 39 |
* and we can't restrict it hence via seccomp. */ |
| 40 |
# define SECCOMP_RESTRICT_ADDRESS_FAMILIES_BROKEN 1 |
| 41 |
#else |
| 42 |
# define SECCOMP_RESTRICT_ADDRESS_FAMILIES_BROKEN 0 |
| 43 |
#endif |
| 44 |
|
| 45 |
#define CHECK_SECCOMP(refuse_container) \ |
| 46 |
if (!is_seccomp_available()) \ |
| 47 |
return (void) log_tests_skipped("Seccomp not available"); \ |
| 48 |
if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0) \ |
| 49 |
return (void) log_tests_skipped("Not privileged"); \ |
| 50 |
if (refuse_container && detect_container() > 0) \ |
| 51 |
return (void) log_tests_skipped("Running in container"); |
| 52 |
|
| 53 |
TEST(parse_syscall_and_errno) { |
| 54 |
_cleanup_free_ char *n = NULL; |
| 55 |
int e; |
| 56 |
|
| 57 |
CHECK_SECCOMP(/* refuse_container= */ false); |
| 58 |
|
| 59 |
assert_se(parse_syscall_and_errno("uname:EILSEQ", &n, &e) >= 0); |
| 60 |
ASSERT_STREQ(n, "uname"); |
| 61 |
assert_se(e == errno_from_name("EILSEQ") && e >= 0); |
| 62 |
n = mfree(n); |
| 63 |
|
| 64 |
assert_se(parse_syscall_and_errno("uname:EINVAL", &n, &e) >= 0); |
| 65 |
ASSERT_STREQ(n, "uname"); |
| 66 |
assert_se(e == errno_from_name("EINVAL") && e >= 0); |
| 67 |
n = mfree(n); |
| 68 |
|
| 69 |
assert_se(parse_syscall_and_errno("@sync:4095", &n, &e) >= 0); |
| 70 |
ASSERT_STREQ(n, "@sync"); |
| 71 |
assert_se(e == 4095); |
| 72 |
n = mfree(n); |
| 73 |
|
| 74 |
/* If errno is omitted, then e is set to -1 */ |
| 75 |
assert_se(parse_syscall_and_errno("mount", &n, &e) >= 0); |
| 76 |
ASSERT_STREQ(n, "mount"); |
| 77 |
assert_se(e == -1); |
| 78 |
n = mfree(n); |
| 79 |
|
| 80 |
/* parse_syscall_and_errno() does not check the syscall name is valid or not. */ |
| 81 |
assert_se(parse_syscall_and_errno("hoge:255", &n, &e) >= 0); |
| 82 |
ASSERT_STREQ(n, "hoge"); |
| 83 |
assert_se(e == 255); |
| 84 |
n = mfree(n); |
| 85 |
|
| 86 |
/* 0 is also a valid errno. */ |
| 87 |
assert_se(parse_syscall_and_errno("hoge:0", &n, &e) >= 0); |
| 88 |
ASSERT_STREQ(n, "hoge"); |
| 89 |
assert_se(e == 0); |
| 90 |
n = mfree(n); |
| 91 |
|
| 92 |
assert_se(parse_syscall_and_errno("hoge:kill", &n, &e) >= 0); |
| 93 |
ASSERT_STREQ(n, "hoge"); |
| 94 |
assert_se(e == SECCOMP_ERROR_NUMBER_KILL); |
| 95 |
n = mfree(n); |
| 96 |
|
| 97 |
/* The function checks the syscall name is empty or not. */ |
| 98 |
assert_se(parse_syscall_and_errno("", &n, &e) == -EINVAL); |
| 99 |
assert_se(parse_syscall_and_errno(":255", &n, &e) == -EINVAL); |
| 100 |
|
| 101 |
/* errno must be a valid errno name or number between 0 and ERRNO_MAX == 4095, or "kill" */ |
| 102 |
assert_se(parse_syscall_and_errno("hoge:4096", &n, &e) == -ERANGE); |
| 103 |
assert_se(parse_syscall_and_errno("hoge:-3", &n, &e) == -ERANGE); |
| 104 |
assert_se(parse_syscall_and_errno("hoge:12.3", &n, &e) == -EINVAL); |
| 105 |
assert_se(parse_syscall_and_errno("hoge:123junk", &n, &e) == -EINVAL); |
| 106 |
assert_se(parse_syscall_and_errno("hoge:junk123", &n, &e) == -EINVAL); |
| 107 |
assert_se(parse_syscall_and_errno("hoge:255:EILSEQ", &n, &e) == -EINVAL); |
| 108 |
assert_se(parse_syscall_and_errno("hoge:-EINVAL", &n, &e) == -EINVAL); |
| 109 |
assert_se(parse_syscall_and_errno("hoge:EINVALaaa", &n, &e) == -EINVAL); |
| 110 |
assert_se(parse_syscall_and_errno("hoge:", &n, &e) == -EINVAL); |
| 111 |
} |
| 112 |
|
| 113 |
TEST(seccomp_arch_to_string) { |
| 114 |
uint32_t a, b; |
| 115 |
const char *name; |
| 116 |
|
| 117 |
CHECK_SECCOMP(/* refuse_container= */ false); |
| 118 |
|
| 119 |
a = sym_seccomp_arch_native(); |
| 120 |
assert_se(a > 0); |
| 121 |
name = seccomp_arch_to_string(a); |
| 122 |
assert_se(name); |
| 123 |
assert_se(seccomp_arch_from_string(name, &b) >= 0); |
| 124 |
assert_se(a == b); |
| 125 |
} |
| 126 |
|
| 127 |
TEST(architecture_table) { |
| 128 |
const char *n2; |
| 129 |
|
| 130 |
NULSTR_FOREACH(n, |
| 131 |
"native\0" |
| 132 |
"x86\0" |
| 133 |
"x86-64\0" |
| 134 |
"x32\0" |
| 135 |
"arm\0" |
| 136 |
"arm64\0" |
| 137 |
#ifdef SCMP_ARCH_LOONGARCH64 |
| 138 |
"loongarch64\0" |
| 139 |
#endif |
| 140 |
"mips\0" |
| 141 |
"mips64\0" |
| 142 |
"mips64-n32\0" |
| 143 |
"mips-le\0" |
| 144 |
"mips64-le\0" |
| 145 |
"mips64-le-n32\0" |
| 146 |
"parisc\0" |
| 147 |
"parisc64\0" |
| 148 |
"ppc\0" |
| 149 |
"ppc64\0" |
| 150 |
"ppc64-le\0" |
| 151 |
#ifdef SCMP_ARCH_RISCV64 |
| 152 |
"riscv64\0" |
| 153 |
#endif |
| 154 |
"s390\0" |
| 155 |
"s390x\0") { |
| 156 |
uint32_t c; |
| 157 |
|
| 158 |
assert_se(seccomp_arch_from_string(n, &c) >= 0); |
| 159 |
n2 = seccomp_arch_to_string(c); |
| 160 |
log_info("seccomp-arch: %s → 0x%"PRIx32" → %s", n, c, n2); |
| 161 |
ASSERT_STREQ(n, n2); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
TEST(syscall_filter_set_find) { |
| 166 |
assert_se(!syscall_filter_set_find(NULL)); |
| 167 |
assert_se(!syscall_filter_set_find("")); |
| 168 |
assert_se(!syscall_filter_set_find("quux")); |
| 169 |
assert_se(!syscall_filter_set_find("@quux")); |
| 170 |
|
| 171 |
assert_se(syscall_filter_set_find("@clock") == syscall_filter_sets + SYSCALL_FILTER_SET_CLOCK); |
| 172 |
assert_se(syscall_filter_set_find("@default") == syscall_filter_sets + SYSCALL_FILTER_SET_DEFAULT); |
| 173 |
assert_se(syscall_filter_set_find("@raw-io") == syscall_filter_sets + SYSCALL_FILTER_SET_RAW_IO); |
| 174 |
} |
| 175 |
|
| 176 |
TEST(filter_sets) { |
| 177 |
int r; |
| 178 |
|
| 179 |
CHECK_SECCOMP(/* skip_container= */ false); |
| 180 |
|
| 181 |
for (unsigned i = 0; i < _SYSCALL_FILTER_SET_MAX; i++) { |
| 182 |
|
| 183 |
#if HAVE_VALGRIND_VALGRIND_H |
| 184 |
if (RUNNING_ON_VALGRIND && IN_SET(i, SYSCALL_FILTER_SET_DEFAULT, SYSCALL_FILTER_SET_BASIC_IO, SYSCALL_FILTER_SET_SIGNAL)) { |
| 185 |
/* valgrind at least requires rt_sigprocmask(), read(), write(). */ |
| 186 |
log_info("Running on valgrind, skipping %s", syscall_filter_sets[i].name); |
| 187 |
continue; |
| 188 |
} |
| 189 |
#endif |
| 190 |
#if HAS_FEATURE_ADDRESS_SANITIZER |
| 191 |
if (IN_SET(i, SYSCALL_FILTER_SET_DEFAULT, SYSCALL_FILTER_SET_BASIC_IO, SYSCALL_FILTER_SET_SIGNAL)) { |
| 192 |
/* ASAN at least requires sigaltstack(), read(), write(). */ |
| 193 |
log_info("Running on address sanitizer, skipping %s", syscall_filter_sets[i].name); |
| 194 |
continue; |
| 195 |
} |
| 196 |
#endif |
| 197 |
|
| 198 |
log_info("Testing %s", syscall_filter_sets[i].name); |
| 199 |
|
| 200 |
r = ASSERT_OK(pidref_safe_fork("(filter_sets)", FORK_LOG|FORK_WAIT, NULL)); |
| 201 |
if (r == 0) { |
| 202 |
int fd; |
| 203 |
|
| 204 |
/* If we look at the default set (or one that includes it), allow-list instead of deny-list */ |
| 205 |
if (IN_SET(i, SYSCALL_FILTER_SET_DEFAULT, |
| 206 |
SYSCALL_FILTER_SET_SYSTEM_SERVICE, |
| 207 |
SYSCALL_FILTER_SET_KNOWN)) |
| 208 |
r = seccomp_load_syscall_filter_set(SCMP_ACT_ERRNO(EUCLEAN), syscall_filter_sets + i, SCMP_ACT_ALLOW, true); |
| 209 |
else |
| 210 |
r = seccomp_load_syscall_filter_set(SCMP_ACT_ALLOW, syscall_filter_sets + i, SCMP_ACT_ERRNO(EUCLEAN), true); |
| 211 |
if (r < 0) |
| 212 |
_exit(EXIT_FAILURE); |
| 213 |
|
| 214 |
/* Test the sycall filter with one random system call */ |
| 215 |
fd = eventfd(0, EFD_NONBLOCK|EFD_CLOEXEC); |
| 216 |
if (IN_SET(i, SYSCALL_FILTER_SET_IO_EVENT, SYSCALL_FILTER_SET_DEFAULT)) |
| 217 |
assert_se(fd < 0 && errno == EUCLEAN); |
| 218 |
else { |
| 219 |
assert_se(fd >= 0); |
| 220 |
safe_close(fd); |
| 221 |
} |
| 222 |
|
| 223 |
_exit(EXIT_SUCCESS); |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
TEST(filter_sets_ordered) { |
| 229 |
/* Ensure "@default" always remains at the beginning of the list */ |
| 230 |
assert_se(SYSCALL_FILTER_SET_DEFAULT == 0); |
| 231 |
ASSERT_STREQ(syscall_filter_sets[0].name, "@default"); |
| 232 |
|
| 233 |
/* Ensure "@known" always remains at the end of the list */ |
| 234 |
assert_se(SYSCALL_FILTER_SET_KNOWN == _SYSCALL_FILTER_SET_MAX - 1); |
| 235 |
ASSERT_STREQ(syscall_filter_sets[SYSCALL_FILTER_SET_KNOWN].name, "@known"); |
| 236 |
|
| 237 |
for (size_t i = 0; i < _SYSCALL_FILTER_SET_MAX; i++) { |
| 238 |
const char *p = NULL; |
| 239 |
|
| 240 |
/* Make sure each group has a description */ |
| 241 |
assert_se(!isempty(syscall_filter_sets[0].help)); |
| 242 |
|
| 243 |
/* Make sure the groups are ordered alphabetically, except for the first and last entries */ |
| 244 |
assert_se(i < 2 || i == _SYSCALL_FILTER_SET_MAX - 1 || |
| 245 |
strcmp(syscall_filter_sets[i-1].name, syscall_filter_sets[i].name) < 0); |
| 246 |
|
| 247 |
NULSTR_FOREACH(k, syscall_filter_sets[i].value) { |
| 248 |
|
| 249 |
/* Ensure each syscall list is in itself ordered, but groups before names */ |
| 250 |
assert_se(!p || |
| 251 |
(*p == '@' && *k != '@') || |
| 252 |
(((*p == '@' && *k == '@') || |
| 253 |
(*p != '@' && *k != '@')) && |
| 254 |
strcmp(p, k) < 0)); |
| 255 |
|
| 256 |
p = k; |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
TEST(restrict_namespace) { |
| 262 |
char *s = NULL; |
| 263 |
unsigned long ul; |
| 264 |
int r; |
| 265 |
|
| 266 |
if (!have_namespaces()) |
| 267 |
return (void) log_tests_skipped("Testing without namespaces"); |
| 268 |
|
| 269 |
assert_se(namespace_flags_to_string(0, &s) == 0 && isempty(s)); |
| 270 |
s = mfree(s); |
| 271 |
assert_se(namespace_flags_to_string(CLONE_NEWNS, &s) == 0 && streq(s, "mnt")); |
| 272 |
s = mfree(s); |
| 273 |
assert_se(namespace_flags_to_string(CLONE_NEWNS|CLONE_NEWIPC, &s) == 0 && streq(s, "ipc mnt")); |
| 274 |
s = mfree(s); |
| 275 |
assert_se(namespace_flags_to_string(CLONE_NEWCGROUP, &s) == 0 && streq(s, "cgroup")); |
| 276 |
s = mfree(s); |
| 277 |
|
| 278 |
assert_se(namespace_flags_from_string("mnt", &ul) == 0 && ul == CLONE_NEWNS); |
| 279 |
assert_se(namespace_flags_from_string(NULL, &ul) == 0 && ul == 0); |
| 280 |
assert_se(namespace_flags_from_string("", &ul) == 0 && ul == 0); |
| 281 |
assert_se(namespace_flags_from_string("uts", &ul) == 0 && ul == CLONE_NEWUTS); |
| 282 |
assert_se(namespace_flags_from_string("mnt uts ipc", &ul) == 0 && ul == (CLONE_NEWNS|CLONE_NEWUTS|CLONE_NEWIPC)); |
| 283 |
|
| 284 |
assert_se(namespace_flags_to_string(CLONE_NEWUTS, &s) == 0 && streq(s, "uts")); |
| 285 |
assert_se(namespace_flags_from_string(s, &ul) == 0 && ul == CLONE_NEWUTS); |
| 286 |
s = mfree(s); |
| 287 |
assert_se(namespace_flags_from_string("ipc", &ul) == 0 && ul == CLONE_NEWIPC); |
| 288 |
assert_se(namespace_flags_to_string(ul, &s) == 0 && streq(s, "ipc")); |
| 289 |
s = mfree(s); |
| 290 |
|
| 291 |
assert_se(namespace_flags_to_string(NAMESPACE_FLAGS_ALL, &s) == 0); |
| 292 |
ASSERT_STREQ(s, "cgroup ipc net mnt pid user uts time"); |
| 293 |
assert_se(namespace_flags_from_string(s, &ul) == 0 && ul == NAMESPACE_FLAGS_ALL); |
| 294 |
s = mfree(s); |
| 295 |
|
| 296 |
CHECK_SECCOMP(/* skip_container= */ false); |
| 297 |
|
| 298 |
r = ASSERT_OK(pidref_safe_fork("(restrict-namespace)", FORK_LOG|FORK_WAIT, NULL)); |
| 299 |
if (r == 0) { |
| 300 |
|
| 301 |
assert_se(seccomp_restrict_namespaces(CLONE_NEWNS|CLONE_NEWNET) >= 0); |
| 302 |
|
| 303 |
assert_se(unshare(CLONE_NEWNS) == 0); |
| 304 |
assert_se(unshare(CLONE_NEWNET) == 0); |
| 305 |
assert_se(unshare(CLONE_NEWUTS) == -1); |
| 306 |
assert_se(errno == EPERM); |
| 307 |
assert_se(unshare(CLONE_NEWIPC) == -1); |
| 308 |
assert_se(errno == EPERM); |
| 309 |
assert_se(unshare(CLONE_NEWNET|CLONE_NEWUTS) == -1); |
| 310 |
assert_se(errno == EPERM); |
| 311 |
|
| 312 |
/* We use fd 0 (stdin) here, which of course will fail with EINVAL on setns(). Except of course our |
| 313 |
* seccomp filter worked, and hits first and makes it return EPERM */ |
| 314 |
assert_se(setns(0, CLONE_NEWNS) == -1); |
| 315 |
assert_se(errno == EINVAL); |
| 316 |
assert_se(setns(0, CLONE_NEWNET) == -1); |
| 317 |
assert_se(errno == EINVAL); |
| 318 |
assert_se(setns(0, CLONE_NEWUTS) == -1); |
| 319 |
assert_se(errno == EPERM); |
| 320 |
assert_se(setns(0, CLONE_NEWIPC) == -1); |
| 321 |
assert_se(errno == EPERM); |
| 322 |
assert_se(setns(0, CLONE_NEWNET|CLONE_NEWUTS) == -1); |
| 323 |
assert_se(errno == EPERM); |
| 324 |
assert_se(setns(0, 0) == -1); |
| 325 |
assert_se(errno == EPERM); |
| 326 |
|
| 327 |
pid_t pid = raw_clone(CLONE_NEWNS); |
| 328 |
assert_se(pid >= 0); |
| 329 |
if (pid == 0) |
| 330 |
_exit(EXIT_SUCCESS); |
| 331 |
pid = raw_clone(CLONE_NEWNET); |
| 332 |
assert_se(pid >= 0); |
| 333 |
if (pid == 0) |
| 334 |
_exit(EXIT_SUCCESS); |
| 335 |
pid = raw_clone(CLONE_NEWUTS); |
| 336 |
assert_se(pid < 0); |
| 337 |
assert_se(errno == EPERM); |
| 338 |
pid = raw_clone(CLONE_NEWIPC); |
| 339 |
assert_se(pid < 0); |
| 340 |
assert_se(errno == EPERM); |
| 341 |
pid = raw_clone(CLONE_NEWNET|CLONE_NEWUTS); |
| 342 |
assert_se(pid < 0); |
| 343 |
assert_se(errno == EPERM); |
| 344 |
|
| 345 |
_exit(EXIT_SUCCESS); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
TEST(protect_sysctl) { |
| 350 |
int r; |
| 351 |
|
| 352 |
/* in containers _sysctl() is likely missing anyway */ |
| 353 |
CHECK_SECCOMP(/* skip_container= */ true); |
| 354 |
|
| 355 |
_cleanup_free_ char *seccomp = NULL; |
| 356 |
assert_se(get_proc_field("/proc/self/status", "Seccomp", &seccomp) == 0); |
| 357 |
if (!streq(seccomp, "0")) |
| 358 |
log_warning("Warning: seccomp filter detected, results may be unreliable for %s", __func__); |
| 359 |
|
| 360 |
r = ASSERT_OK(pidref_safe_fork("(protect-sysctl)", FORK_LOG|FORK_WAIT, NULL)); |
| 361 |
if (r == 0) { |
| 362 |
#if defined __NR__sysctl && __NR__sysctl >= 0 |
| 363 |
assert_se(syscall(__NR__sysctl, NULL) < 0); |
| 364 |
assert_se(IN_SET(errno, EFAULT, ENOSYS)); |
| 365 |
#endif |
| 366 |
|
| 367 |
assert_se(seccomp_protect_sysctl() >= 0); |
| 368 |
|
| 369 |
#if HAVE_VALGRIND_VALGRIND_H |
| 370 |
if (RUNNING_ON_VALGRIND) { |
| 371 |
log_info("Running on valgrind, skipping syscall/EPERM test"); |
| 372 |
_exit(EXIT_SUCCESS); |
| 373 |
} |
| 374 |
#endif |
| 375 |
|
| 376 |
#if defined __NR__sysctl && __NR__sysctl >= 0 |
| 377 |
assert_se(syscall(__NR__sysctl, 0, 0, 0) < 0); |
| 378 |
assert_se(errno == EPERM); |
| 379 |
#endif |
| 380 |
|
| 381 |
_exit(EXIT_SUCCESS); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
TEST(protect_syslog) { |
| 386 |
int r; |
| 387 |
|
| 388 |
/* in containers syslog() is likely missing anyway */ |
| 389 |
CHECK_SECCOMP(/* skip_container= */ true); |
| 390 |
|
| 391 |
r = ASSERT_OK(pidref_safe_fork("(protect-syslog)", FORK_LOG|FORK_WAIT, NULL)); |
| 392 |
if (r == 0) { |
| 393 |
#if defined __NR_syslog && __NR_syslog >= 0 |
| 394 |
assert_se(syscall(__NR_syslog, -1, NULL, 0) < 0); |
| 395 |
assert_se(errno == EINVAL); |
| 396 |
#endif |
| 397 |
|
| 398 |
assert_se(seccomp_protect_syslog() >= 0); |
| 399 |
|
| 400 |
#if defined __NR_syslog && __NR_syslog >= 0 |
| 401 |
assert_se(syscall(__NR_syslog, 0, 0, 0) < 0); |
| 402 |
assert_se(errno == EPERM); |
| 403 |
#endif |
| 404 |
|
| 405 |
_exit(EXIT_SUCCESS); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
TEST(restrict_address_families) { |
| 410 |
int r; |
| 411 |
|
| 412 |
CHECK_SECCOMP(/* skip_container= */ false); |
| 413 |
|
| 414 |
r = ASSERT_OK(pidref_safe_fork("(restrict-address-families)", FORK_LOG|FORK_WAIT, NULL)); |
| 415 |
if (r == 0) { |
| 416 |
int fd; |
| 417 |
Set *s; |
| 418 |
|
| 419 |
fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 420 |
assert_se(fd >= 0); |
| 421 |
safe_close(fd); |
| 422 |
|
| 423 |
fd = socket(AF_UNIX, SOCK_DGRAM, 0); |
| 424 |
assert_se(fd >= 0); |
| 425 |
safe_close(fd); |
| 426 |
|
| 427 |
fd = socket(AF_NETLINK, SOCK_DGRAM, 0); |
| 428 |
assert_se(fd >= 0); |
| 429 |
safe_close(fd); |
| 430 |
|
| 431 |
assert_se(s = set_new(NULL)); |
| 432 |
ASSERT_OK(set_put(s, INT_TO_PTR(AF_UNIX))); |
| 433 |
|
| 434 |
assert_se(seccomp_restrict_address_families(s, false) >= 0); |
| 435 |
|
| 436 |
fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 437 |
assert_se(fd >= 0); |
| 438 |
safe_close(fd); |
| 439 |
|
| 440 |
fd = socket(AF_UNIX, SOCK_DGRAM, 0); |
| 441 |
#if SECCOMP_RESTRICT_ADDRESS_FAMILIES_BROKEN |
| 442 |
assert_se(fd >= 0); |
| 443 |
safe_close(fd); |
| 444 |
#else |
| 445 |
assert_se(fd < 0); |
| 446 |
assert_se(errno == EAFNOSUPPORT); |
| 447 |
#endif |
| 448 |
|
| 449 |
fd = socket(AF_NETLINK, SOCK_DGRAM, 0); |
| 450 |
assert_se(fd >= 0); |
| 451 |
safe_close(fd); |
| 452 |
|
| 453 |
set_clear(s); |
| 454 |
|
| 455 |
ASSERT_OK(set_put(s, INT_TO_PTR(AF_INET))); |
| 456 |
|
| 457 |
assert_se(seccomp_restrict_address_families(s, true) >= 0); |
| 458 |
|
| 459 |
fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 460 |
assert_se(fd >= 0); |
| 461 |
safe_close(fd); |
| 462 |
|
| 463 |
fd = socket(AF_UNIX, SOCK_DGRAM, 0); |
| 464 |
#if SECCOMP_RESTRICT_ADDRESS_FAMILIES_BROKEN |
| 465 |
assert_se(fd >= 0); |
| 466 |
safe_close(fd); |
| 467 |
#else |
| 468 |
assert_se(fd < 0); |
| 469 |
assert_se(errno == EAFNOSUPPORT); |
| 470 |
#endif |
| 471 |
|
| 472 |
fd = socket(AF_NETLINK, SOCK_DGRAM, 0); |
| 473 |
#if SECCOMP_RESTRICT_ADDRESS_FAMILIES_BROKEN |
| 474 |
assert_se(fd >= 0); |
| 475 |
safe_close(fd); |
| 476 |
#else |
| 477 |
assert_se(fd < 0); |
| 478 |
assert_se(errno == EAFNOSUPPORT); |
| 479 |
#endif |
| 480 |
|
| 481 |
_exit(EXIT_SUCCESS); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
TEST(restrict_realtime) { |
| 486 |
int r; |
| 487 |
|
| 488 |
/* in containers RT privs are likely missing anyway */ |
| 489 |
CHECK_SECCOMP(/* skip_container= */ true); |
| 490 |
|
| 491 |
r = ASSERT_OK(pidref_safe_fork("(restrict-realtime)", FORK_LOG|FORK_WAIT, NULL)); |
| 492 |
if (r == 0) { |
| 493 |
/* On some CI environments, the restriction may be already enabled. */ |
| 494 |
if (sched_setscheduler(0, SCHED_FIFO, &(struct sched_param) { .sched_priority = 1 }) < 0) { |
| 495 |
if (errno == ENOSYS) { |
| 496 |
log_tests_skipped("sched_setscheduler() is not available or already filtered"); |
| 497 |
_exit(EXIT_SUCCESS); |
| 498 |
} |
| 499 |
|
| 500 |
log_full_errno(errno == EPERM ? LOG_DEBUG : LOG_WARNING, errno, |
| 501 |
"Failed to set scheduler parameter for FIFO: %m"); |
| 502 |
assert(errno == EPERM); |
| 503 |
} |
| 504 |
if (sched_setscheduler(0, SCHED_RR, &(struct sched_param) { .sched_priority = 1 }) < 0) { |
| 505 |
log_full_errno(errno == EPERM ? LOG_DEBUG : LOG_WARNING, errno, |
| 506 |
"Failed to set scheduler parameter for RR: %m"); |
| 507 |
assert(errno == EPERM); |
| 508 |
} |
| 509 |
|
| 510 |
assert_se(sched_setscheduler(0, SCHED_IDLE, &(struct sched_param) { .sched_priority = 0 }) >= 0); |
| 511 |
assert_se(sched_setscheduler(0, SCHED_BATCH, &(struct sched_param) { .sched_priority = 0 }) >= 0); |
| 512 |
assert_se(sched_setscheduler(0, SCHED_OTHER, &(struct sched_param) {}) >= 0); |
| 513 |
|
| 514 |
assert_se(seccomp_restrict_realtime_full(ENOANO) >= 0); |
| 515 |
|
| 516 |
assert_se(sched_setscheduler(0, SCHED_IDLE, &(struct sched_param) { .sched_priority = 0 }) >= 0); |
| 517 |
assert_se(sched_setscheduler(0, SCHED_BATCH, &(struct sched_param) { .sched_priority = 0 }) >= 0); |
| 518 |
assert_se(sched_setscheduler(0, SCHED_OTHER, &(struct sched_param) {}) >= 0); |
| 519 |
|
| 520 |
assert_se(sched_setscheduler(0, SCHED_FIFO, &(struct sched_param) { .sched_priority = 1 }) < 0); |
| 521 |
assert_se(errno == ENOANO); |
| 522 |
assert_se(sched_setscheduler(0, SCHED_RR, &(struct sched_param) { .sched_priority = 1 }) < 0); |
| 523 |
assert_se(errno == ENOANO); |
| 524 |
|
| 525 |
_exit(EXIT_SUCCESS); |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
TEST(memory_deny_write_execute_mmap) { |
| 530 |
int r; |
| 531 |
|
| 532 |
CHECK_SECCOMP(/* skip_container= */ false); |
| 533 |
|
| 534 |
#if HAVE_VALGRIND_VALGRIND_H |
| 535 |
if (RUNNING_ON_VALGRIND) { |
| 536 |
log_notice("Running on valgrind, skipping %s", __func__); |
| 537 |
return; |
| 538 |
} |
| 539 |
#endif |
| 540 |
#if HAS_FEATURE_ADDRESS_SANITIZER |
| 541 |
log_notice("Running on address sanitizer, skipping %s", __func__); |
| 542 |
return; |
| 543 |
#endif |
| 544 |
|
| 545 |
r = ASSERT_OK(pidref_safe_fork("(memory_deny_write_execute_mmap)", FORK_LOG|FORK_WAIT, NULL)); |
| 546 |
if (r == 0) { |
| 547 |
void *p; |
| 548 |
|
| 549 |
p = mmap(NULL, page_size(), PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 550 |
assert_se(p != MAP_FAILED); |
| 551 |
assert_se(munmap(p, page_size()) >= 0); |
| 552 |
|
| 553 |
p = mmap(NULL, page_size(), PROT_WRITE|PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 554 |
assert_se(p != MAP_FAILED); |
| 555 |
assert_se(munmap(p, page_size()) >= 0); |
| 556 |
|
| 557 |
assert_se(seccomp_memory_deny_write_execute() >= 0); |
| 558 |
|
| 559 |
p = mmap(NULL, page_size(), PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 560 |
#if defined(__x86_64__) || defined(__i386__) || defined(__powerpc64__) || defined(__arm__) || defined(__aarch64__) || defined(__loongarch_lp64) |
| 561 |
assert_se(p == MAP_FAILED); |
| 562 |
# ifdef __GLIBC__ |
| 563 |
ASSERT_EQ(errno, EPERM); |
| 564 |
# else |
| 565 |
ASSERT_EQ(errno, ENOMEM); /* musl maps EPERM to ENOMEM. See src/mman/mmap.c in musl. */ |
| 566 |
# endif |
| 567 |
#endif |
| 568 |
/* Depending on kernel, libseccomp, and glibc versions, other architectures |
| 569 |
* might fail or not. Let's not assert success. */ |
| 570 |
if (p != MAP_FAILED) |
| 571 |
assert_se(munmap(p, page_size()) == 0); |
| 572 |
|
| 573 |
p = mmap(NULL, page_size(), PROT_WRITE|PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 574 |
assert_se(p != MAP_FAILED); |
| 575 |
assert_se(munmap(p, page_size()) >= 0); |
| 576 |
|
| 577 |
_exit(EXIT_SUCCESS); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
TEST(memory_deny_write_execute_shmat) { |
| 582 |
uint32_t arch; |
| 583 |
int r, shmid; |
| 584 |
|
| 585 |
SECCOMP_FOREACH_LOCAL_ARCH(arch) { |
| 586 |
log_debug("arch %s: SCMP_SYS(mmap) = %d", seccomp_arch_to_string(arch), SCMP_SYS(mmap)); |
| 587 |
log_debug("arch %s: SCMP_SYS(mmap2) = %d", seccomp_arch_to_string(arch), SCMP_SYS(mmap2)); |
| 588 |
log_debug("arch %s: SCMP_SYS(shmget) = %d", seccomp_arch_to_string(arch), SCMP_SYS(shmget)); |
| 589 |
log_debug("arch %s: SCMP_SYS(shmat) = %d", seccomp_arch_to_string(arch), SCMP_SYS(shmat)); |
| 590 |
log_debug("arch %s: SCMP_SYS(shmdt) = %d", seccomp_arch_to_string(arch), SCMP_SYS(shmdt)); |
| 591 |
} |
| 592 |
|
| 593 |
CHECK_SECCOMP(/* skip_container= */ false); |
| 594 |
|
| 595 |
#if HAVE_VALGRIND_VALGRIND_H |
| 596 |
if (RUNNING_ON_VALGRIND) { |
| 597 |
log_notice("Running on valgrind, skipping %s", __func__); |
| 598 |
return; |
| 599 |
} |
| 600 |
#endif |
| 601 |
#if HAS_FEATURE_ADDRESS_SANITIZER |
| 602 |
log_notice("Running on address sanitizer, skipping %s", __func__); |
| 603 |
return; |
| 604 |
#endif |
| 605 |
|
| 606 |
shmid = shmget(IPC_PRIVATE, page_size(), 0); |
| 607 |
assert_se(shmid >= 0); |
| 608 |
|
| 609 |
r = ASSERT_OK(pidref_safe_fork("(memory-deny-write-execute)", FORK_LOG|FORK_WAIT, NULL)); |
| 610 |
if (r == 0) { |
| 611 |
void *p; |
| 612 |
|
| 613 |
p = shmat(shmid, NULL, 0); |
| 614 |
if (p == MAP_FAILED) { |
| 615 |
log_tests_skipped_errno(errno, "shmat() is already disabled"); |
| 616 |
_exit(EXIT_SUCCESS); |
| 617 |
} |
| 618 |
assert_se(shmdt(p) == 0); |
| 619 |
|
| 620 |
p = shmat(shmid, NULL, SHM_EXEC); |
| 621 |
assert_se(p != MAP_FAILED); |
| 622 |
assert_se(shmdt(p) == 0); |
| 623 |
|
| 624 |
assert_se(seccomp_memory_deny_write_execute() >= 0); |
| 625 |
|
| 626 |
p = shmat(shmid, NULL, SHM_EXEC); |
| 627 |
log_debug_errno(p == MAP_FAILED ? errno : 0, "shmat(SHM_EXEC): %m"); |
| 628 |
#if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || defined(__loongarch_lp64) |
| 629 |
assert_se(p == MAP_FAILED); |
| 630 |
assert_se(errno == EPERM); |
| 631 |
#endif |
| 632 |
/* Depending on kernel, libseccomp, and glibc versions, other architectures |
| 633 |
* might fail or not. Let's not assert success. */ |
| 634 |
if (p != MAP_FAILED) |
| 635 |
assert_se(shmdt(p) == 0); |
| 636 |
|
| 637 |
p = shmat(shmid, NULL, 0); |
| 638 |
log_debug_errno(p == MAP_FAILED ? errno : 0, "shmat(0): %m"); |
| 639 |
assert_se(p != MAP_FAILED); |
| 640 |
assert_se(shmdt(p) == 0); |
| 641 |
|
| 642 |
_exit(EXIT_SUCCESS); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
TEST(restrict_archs) { |
| 647 |
int r; |
| 648 |
|
| 649 |
CHECK_SECCOMP(/* skip_container= */ false); |
| 650 |
|
| 651 |
r = ASSERT_OK(pidref_safe_fork("(restrict-archs)", FORK_LOG|FORK_WAIT, NULL)); |
| 652 |
if (r == 0) { |
| 653 |
_cleanup_set_free_ Set *s = NULL; |
| 654 |
|
| 655 |
assert_se(access("/", F_OK) >= 0); |
| 656 |
|
| 657 |
assert_se(s = set_new(NULL)); |
| 658 |
|
| 659 |
#ifdef __x86_64__ |
| 660 |
ASSERT_OK(set_put(s, UINT32_TO_PTR(SCMP_ARCH_X86+1))); |
| 661 |
#endif |
| 662 |
assert_se(seccomp_restrict_archs(s) >= 0); |
| 663 |
|
| 664 |
assert_se(access("/", F_OK) >= 0); |
| 665 |
assert_se(seccomp_restrict_archs(NULL) >= 0); |
| 666 |
|
| 667 |
assert_se(access("/", F_OK) >= 0); |
| 668 |
|
| 669 |
_exit(EXIT_SUCCESS); |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
TEST(load_syscall_filter_set_raw) { |
| 674 |
int r; |
| 675 |
|
| 676 |
CHECK_SECCOMP(/* skip_container= */ false); |
| 677 |
|
| 678 |
r = ASSERT_OK(pidref_safe_fork("(load-filter)", FORK_LOG|FORK_WAIT, NULL)); |
| 679 |
if (r == 0) { |
| 680 |
_cleanup_hashmap_free_ Hashmap *s = NULL; |
| 681 |
|
| 682 |
assert_se(access("/", F_OK) >= 0); |
| 683 |
assert_se(poll(NULL, 0, 0) == 0); |
| 684 |
|
| 685 |
assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, NULL, SCMP_ACT_KILL_PROCESS, true) >= 0); |
| 686 |
assert_se(access("/", F_OK) >= 0); |
| 687 |
assert_se(poll(NULL, 0, 0) == 0); |
| 688 |
|
| 689 |
assert_se(s = hashmap_new(NULL)); |
| 690 |
#if defined __NR_access && __NR_access >= 0 |
| 691 |
assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_access + 1), INT_TO_PTR(-1)) >= 0); |
| 692 |
log_debug("has access()"); |
| 693 |
#endif |
| 694 |
#if defined __NR_faccessat && __NR_faccessat >= 0 |
| 695 |
assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_faccessat + 1), INT_TO_PTR(-1)) >= 0); |
| 696 |
log_debug("has faccessat()"); |
| 697 |
#endif |
| 698 |
#if defined __NR_faccessat2 && __NR_faccessat2 >= 0 |
| 699 |
assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_faccessat2 + 1), INT_TO_PTR(-1)) >= 0); |
| 700 |
log_debug("has faccessat2()"); |
| 701 |
#endif |
| 702 |
|
| 703 |
assert_se(!hashmap_isempty(s)); |
| 704 |
assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EUCLEAN), true) >= 0); |
| 705 |
|
| 706 |
assert_se(access("/", F_OK) < 0); |
| 707 |
assert_se(errno == EUCLEAN); |
| 708 |
|
| 709 |
assert_se(poll(NULL, 0, 0) == 0); |
| 710 |
|
| 711 |
hashmap_clear(s); |
| 712 |
#if defined __NR_access && __NR_access >= 0 |
| 713 |
assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_access + 1), INT_TO_PTR(EILSEQ)) >= 0); |
| 714 |
#endif |
| 715 |
#if defined __NR_faccessat && __NR_faccessat >= 0 |
| 716 |
assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_faccessat + 1), INT_TO_PTR(EILSEQ)) >= 0); |
| 717 |
#endif |
| 718 |
#if defined __NR_faccessat2 && __NR_faccessat2 >= 0 |
| 719 |
assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_faccessat2 + 1), INT_TO_PTR(EILSEQ)) >= 0); |
| 720 |
#endif |
| 721 |
|
| 722 |
assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EUCLEAN), true) >= 0); |
| 723 |
|
| 724 |
assert_se(access("/", F_OK) < 0); |
| 725 |
assert_se(errno == EILSEQ); |
| 726 |
|
| 727 |
assert_se(poll(NULL, 0, 0) == 0); |
| 728 |
|
| 729 |
hashmap_clear(s); |
| 730 |
#if defined __NR_poll && __NR_poll >= 0 |
| 731 |
assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_poll + 1), INT_TO_PTR(-1)) >= 0); |
| 732 |
log_debug("has poll()"); |
| 733 |
#endif |
| 734 |
#if defined __NR_ppoll && __NR_ppoll >= 0 |
| 735 |
assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_ppoll + 1), INT_TO_PTR(-1)) >= 0); |
| 736 |
log_debug("has ppoll()"); |
| 737 |
#endif |
| 738 |
#if defined __NR_ppoll_time64 && __NR_ppoll_time64 >= 0 |
| 739 |
assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_ppoll_time64 + 1), INT_TO_PTR(-1)) >= 0); |
| 740 |
log_debug("has ppoll_time64()"); |
| 741 |
#endif |
| 742 |
|
| 743 |
assert_se(!hashmap_isempty(s)); |
| 744 |
assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EUNATCH), true) >= 0); |
| 745 |
|
| 746 |
assert_se(access("/", F_OK) < 0); |
| 747 |
assert_se(errno == EILSEQ); |
| 748 |
|
| 749 |
assert_se(poll(NULL, 0, 0) < 0); |
| 750 |
assert_se(errno == EUNATCH); |
| 751 |
|
| 752 |
hashmap_clear(s); |
| 753 |
#if defined __NR_poll && __NR_poll >= 0 |
| 754 |
assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_poll + 1), INT_TO_PTR(EILSEQ)) >= 0); |
| 755 |
#endif |
| 756 |
#if defined __NR_ppoll && __NR_ppoll >= 0 |
| 757 |
assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_ppoll + 1), INT_TO_PTR(EILSEQ)) >= 0); |
| 758 |
#endif |
| 759 |
#if defined __NR_ppoll_time64 && __NR_ppoll_time64 >= 0 |
| 760 |
assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_ppoll_time64 + 1), INT_TO_PTR(EILSEQ)) >= 0); |
| 761 |
#endif |
| 762 |
|
| 763 |
assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EUNATCH), true) >= 0); |
| 764 |
|
| 765 |
assert_se(access("/", F_OK) < 0); |
| 766 |
assert_se(errno == EILSEQ); |
| 767 |
|
| 768 |
assert_se(poll(NULL, 0, 0) < 0); |
| 769 |
assert_se(errno == EILSEQ); |
| 770 |
|
| 771 |
_exit(EXIT_SUCCESS); |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
TEST(native_syscalls_filtered) { |
| 776 |
int r; |
| 777 |
|
| 778 |
CHECK_SECCOMP(/* skip_container= */ false); |
| 779 |
|
| 780 |
r = ASSERT_OK(pidref_safe_fork("(native-syscalls)", FORK_LOG|FORK_WAIT, NULL)); |
| 781 |
if (r == 0) { |
| 782 |
_cleanup_set_free_ Set *arch_s = NULL; |
| 783 |
_cleanup_hashmap_free_ Hashmap *s = NULL; |
| 784 |
|
| 785 |
/* Passing "native" or an empty set is equivalent, just do both here. */ |
| 786 |
assert_se(arch_s = set_new(NULL)); |
| 787 |
assert_se(seccomp_restrict_archs(arch_s) >= 0); |
| 788 |
ASSERT_OK(set_put(arch_s, (void*) SCMP_ARCH_NATIVE)); |
| 789 |
assert_se(seccomp_restrict_archs(arch_s) >= 0); |
| 790 |
|
| 791 |
assert_se(access("/", F_OK) >= 0); |
| 792 |
assert_se(poll(NULL, 0, 0) == 0); |
| 793 |
|
| 794 |
assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, NULL, SCMP_ACT_KILL_PROCESS, true) >= 0); |
| 795 |
assert_se(access("/", F_OK) >= 0); |
| 796 |
assert_se(poll(NULL, 0, 0) == 0); |
| 797 |
|
| 798 |
assert_se(s = hashmap_new(NULL)); |
| 799 |
#if defined __NR_access && __NR_access >= 0 |
| 800 |
assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_access + 1), INT_TO_PTR(-1)) >= 0); |
| 801 |
log_debug("has access()"); |
| 802 |
#endif |
| 803 |
#if defined __NR_faccessat && __NR_faccessat >= 0 |
| 804 |
assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_faccessat + 1), INT_TO_PTR(-1)) >= 0); |
| 805 |
log_debug("has faccessat()"); |
| 806 |
#endif |
| 807 |
#if defined __NR_faccessat2 && __NR_faccessat2 >= 0 |
| 808 |
assert_se(hashmap_put(s, UINT32_TO_PTR(__NR_faccessat2 + 1), INT_TO_PTR(-1)) >= 0); |
| 809 |
log_debug("has faccessat2()"); |
| 810 |
#endif |
| 811 |
|
| 812 |
assert_se(!hashmap_isempty(s)); |
| 813 |
assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EUCLEAN), true) >= 0); |
| 814 |
|
| 815 |
assert_se(access("/", F_OK) < 0); |
| 816 |
assert_se(errno == EUCLEAN); |
| 817 |
|
| 818 |
_exit(EXIT_SUCCESS); |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
TEST(lock_personality) { |
| 823 |
unsigned long current_opinionated; |
| 824 |
int r; |
| 825 |
|
| 826 |
CHECK_SECCOMP(/* skip_container= */ false); |
| 827 |
|
| 828 |
assert_se(opinionated_personality(¤t_opinionated) >= 0); |
| 829 |
|
| 830 |
log_info("current personality=0x%lX", (unsigned long) safe_personality(PERSONALITY_INVALID)); |
| 831 |
log_info("current opinionated personality=0x%lX", current_opinionated); |
| 832 |
|
| 833 |
r = ASSERT_OK(pidref_safe_fork("(lock-personality)", FORK_LOG|FORK_WAIT, NULL)); |
| 834 |
if (r == 0) { |
| 835 |
unsigned long current; |
| 836 |
|
| 837 |
assert_se(seccomp_lock_personality(current_opinionated) >= 0); |
| 838 |
|
| 839 |
current = safe_personality(current_opinionated); |
| 840 |
assert_se((current & OPINIONATED_PERSONALITY_MASK) == current_opinionated); |
| 841 |
|
| 842 |
/* Note, we also test that safe_personality() works correctly, by checking whether errno is properly |
| 843 |
* set, in addition to the return value */ |
| 844 |
errno = 0; |
| 845 |
assert_se(safe_personality(PER_LINUX | MMAP_PAGE_ZERO) == -EPERM); |
| 846 |
assert_se(errno == EPERM); |
| 847 |
|
| 848 |
if (!FLAGS_SET(current, ADDR_NO_RANDOMIZE)) |
| 849 |
assert_se(safe_personality(PER_LINUX | ADDR_NO_RANDOMIZE) == -EPERM); |
| 850 |
assert_se(safe_personality(PER_LINUX | ADDR_COMPAT_LAYOUT) == -EPERM); |
| 851 |
assert_se(safe_personality(PER_LINUX | READ_IMPLIES_EXEC) == -EPERM); |
| 852 |
assert_se(safe_personality(PER_LINUX_32BIT) == -EPERM); |
| 853 |
assert_se(safe_personality(PER_SVR4) == -EPERM); |
| 854 |
assert_se(safe_personality(PER_BSD) == -EPERM); |
| 855 |
assert_se(safe_personality(current_opinionated == PER_LINUX ? PER_LINUX32 : PER_LINUX) == -EPERM); |
| 856 |
assert_se(safe_personality(PER_LINUX32_3GB) == -EPERM); |
| 857 |
assert_se(safe_personality(PER_UW7) == -EPERM); |
| 858 |
assert_se(safe_personality(0x42) == -EPERM); |
| 859 |
|
| 860 |
assert_se(safe_personality(PERSONALITY_INVALID) == -EPERM); /* maybe remove this later */ |
| 861 |
|
| 862 |
current = safe_personality(current_opinionated); |
| 863 |
assert_se((current & OPINIONATED_PERSONALITY_MASK) == current_opinionated); |
| 864 |
_exit(EXIT_SUCCESS); |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
static int real_open(const char *path, int flags, mode_t mode) { |
| 869 |
/* glibc internally calls openat() when open() is requested. Let's hence define our own wrapper for |
| 870 |
* testing purposes that calls the real syscall, on architectures where SYS_open is defined. On |
| 871 |
* other architectures, let's just fall back to the glibc call. */ |
| 872 |
|
| 873 |
#if defined __NR_open && __NR_open >= 0 |
| 874 |
return (int) syscall(__NR_open, path, flags, mode); |
| 875 |
#else |
| 876 |
return open(path, flags, mode); |
| 877 |
#endif |
| 878 |
} |
| 879 |
|
| 880 |
static int try_fchmodat2(int dirfd, const char *path, mode_t mode, int flags) { |
| 881 |
int r; |
| 882 |
|
| 883 |
/* glibc does not provide a direct wrapper for fchmodat2(). Let's hence define our own wrapper for |
| 884 |
* testing purposes that calls the real syscall, on architectures and in environments where |
| 885 |
* SYS_fchmodat2 is defined. Otherwise, let's just fall back to the glibc fchmodat() call. */ |
| 886 |
|
| 887 |
/* Not supported by fchmodat() */ |
| 888 |
assert_se(!FLAGS_SET(flags, AT_EMPTY_PATH)); |
| 889 |
|
| 890 |
r = RET_NERRNO(fchmodat2(dirfd, path, mode, flags)); |
| 891 |
if (r != -ENOSYS) |
| 892 |
return r; |
| 893 |
|
| 894 |
/* The syscall might still be unsupported by kernel or libseccomp. */ |
| 895 |
return RET_NERRNO(fchmodat(dirfd, path, mode, flags)); |
| 896 |
} |
| 897 |
|
| 898 |
TEST(restrict_suid_sgid) { |
| 899 |
int r; |
| 900 |
|
| 901 |
CHECK_SECCOMP(/* skip_container= */ false); |
| 902 |
|
| 903 |
r = ASSERT_OK(pidref_safe_fork("(suid-sgid)", FORK_LOG|FORK_WAIT, NULL)); |
| 904 |
if (r == 0) { |
| 905 |
char path[] = "/tmp/suidsgidXXXXXX", dir[] = "/tmp/suidsgiddirXXXXXX"; |
| 906 |
int fd = -EBADF, k = -EBADF; |
| 907 |
const char *z; |
| 908 |
|
| 909 |
fd = mkostemp_safe(path); |
| 910 |
assert_se(fd >= 0); |
| 911 |
|
| 912 |
assert_se(mkdtemp(dir)); |
| 913 |
z = strjoina(dir, "/test"); |
| 914 |
|
| 915 |
assert_se(chmod(path, 0755 | S_ISUID) >= 0); |
| 916 |
assert_se(chmod(path, 0755 | S_ISGID) >= 0); |
| 917 |
assert_se(chmod(path, 0755 | S_ISGID | S_ISUID) >= 0); |
| 918 |
assert_se(chmod(path, 0755) >= 0); |
| 919 |
|
| 920 |
assert_se(fchmod(fd, 0755 | S_ISUID) >= 0); |
| 921 |
assert_se(fchmod(fd, 0755 | S_ISGID) >= 0); |
| 922 |
assert_se(fchmod(fd, 0755 | S_ISGID | S_ISUID) >= 0); |
| 923 |
assert_se(fchmod(fd, 0755) >= 0); |
| 924 |
|
| 925 |
assert_se(fchmodat(AT_FDCWD, path, 0755 | S_ISUID, 0) >= 0); |
| 926 |
assert_se(fchmodat(AT_FDCWD, path, 0755 | S_ISGID, 0) >= 0); |
| 927 |
assert_se(fchmodat(AT_FDCWD, path, 0755 | S_ISGID | S_ISUID, 0) >= 0); |
| 928 |
assert_se(fchmodat(AT_FDCWD, path, 0755, 0) >= 0); |
| 929 |
|
| 930 |
assert_se(try_fchmodat2(AT_FDCWD, path, 0755 | S_ISUID, 0) >= 0); |
| 931 |
assert_se(try_fchmodat2(AT_FDCWD, path, 0755 | S_ISGID, 0) >= 0); |
| 932 |
assert_se(try_fchmodat2(AT_FDCWD, path, 0755 | S_ISGID | S_ISUID, 0) >= 0); |
| 933 |
assert_se(try_fchmodat2(AT_FDCWD, path, 0755, 0) >= 0); |
| 934 |
|
| 935 |
k = real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID); |
| 936 |
k = safe_close(k); |
| 937 |
assert_se(unlink(z) >= 0); |
| 938 |
|
| 939 |
k = real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISGID); |
| 940 |
k = safe_close(k); |
| 941 |
assert_se(unlink(z) >= 0); |
| 942 |
|
| 943 |
k = real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID | S_ISGID); |
| 944 |
k = safe_close(k); |
| 945 |
assert_se(unlink(z) >= 0); |
| 946 |
|
| 947 |
k = real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644); |
| 948 |
k = safe_close(k); |
| 949 |
assert_se(unlink(z) >= 0); |
| 950 |
|
| 951 |
k = creat(z, 0644 | S_ISUID); |
| 952 |
k = safe_close(k); |
| 953 |
assert_se(unlink(z) >= 0); |
| 954 |
|
| 955 |
k = creat(z, 0644 | S_ISGID); |
| 956 |
k = safe_close(k); |
| 957 |
assert_se(unlink(z) >= 0); |
| 958 |
|
| 959 |
k = creat(z, 0644 | S_ISUID | S_ISGID); |
| 960 |
k = safe_close(k); |
| 961 |
assert_se(unlink(z) >= 0); |
| 962 |
|
| 963 |
k = creat(z, 0644); |
| 964 |
k = safe_close(k); |
| 965 |
assert_se(unlink(z) >= 0); |
| 966 |
|
| 967 |
k = openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID); |
| 968 |
k = safe_close(k); |
| 969 |
assert_se(unlink(z) >= 0); |
| 970 |
|
| 971 |
k = openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISGID); |
| 972 |
k = safe_close(k); |
| 973 |
assert_se(unlink(z) >= 0); |
| 974 |
|
| 975 |
k = openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID | S_ISGID); |
| 976 |
k = safe_close(k); |
| 977 |
assert_se(unlink(z) >= 0); |
| 978 |
|
| 979 |
k = openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644); |
| 980 |
k = safe_close(k); |
| 981 |
assert_se(unlink(z) >= 0); |
| 982 |
|
| 983 |
assert_se(mkdir(z, 0755 | S_ISUID) >= 0); |
| 984 |
assert_se(rmdir(z) >= 0); |
| 985 |
assert_se(mkdir(z, 0755 | S_ISGID) >= 0); |
| 986 |
assert_se(rmdir(z) >= 0); |
| 987 |
assert_se(mkdir(z, 0755 | S_ISUID | S_ISGID) >= 0); |
| 988 |
assert_se(rmdir(z) >= 0); |
| 989 |
assert_se(mkdir(z, 0755) >= 0); |
| 990 |
assert_se(rmdir(z) >= 0); |
| 991 |
|
| 992 |
assert_se(mkdirat(AT_FDCWD, z, 0755 | S_ISUID) >= 0); |
| 993 |
assert_se(rmdir(z) >= 0); |
| 994 |
assert_se(mkdirat(AT_FDCWD, z, 0755 | S_ISGID) >= 0); |
| 995 |
assert_se(rmdir(z) >= 0); |
| 996 |
assert_se(mkdirat(AT_FDCWD, z, 0755 | S_ISUID | S_ISGID) >= 0); |
| 997 |
assert_se(rmdir(z) >= 0); |
| 998 |
assert_se(mkdirat(AT_FDCWD, z, 0755) >= 0); |
| 999 |
assert_se(rmdir(z) >= 0); |
| 1000 |
|
| 1001 |
assert_se(mknod(z, S_IFREG | 0755 | S_ISUID, 0) >= 0); |
| 1002 |
assert_se(unlink(z) >= 0); |
| 1003 |
assert_se(mknod(z, S_IFREG | 0755 | S_ISGID, 0) >= 0); |
| 1004 |
assert_se(unlink(z) >= 0); |
| 1005 |
assert_se(mknod(z, S_IFREG | 0755 | S_ISUID | S_ISGID, 0) >= 0); |
| 1006 |
assert_se(unlink(z) >= 0); |
| 1007 |
assert_se(mknod(z, S_IFREG | 0755, 0) >= 0); |
| 1008 |
assert_se(unlink(z) >= 0); |
| 1009 |
|
| 1010 |
assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755 | S_ISUID, 0) >= 0); |
| 1011 |
assert_se(unlink(z) >= 0); |
| 1012 |
assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755 | S_ISGID, 0) >= 0); |
| 1013 |
assert_se(unlink(z) >= 0); |
| 1014 |
assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755 | S_ISUID | S_ISGID, 0) >= 0); |
| 1015 |
assert_se(unlink(z) >= 0); |
| 1016 |
assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755, 0) >= 0); |
| 1017 |
assert_se(unlink(z) >= 0); |
| 1018 |
|
| 1019 |
assert_se(seccomp_restrict_suid_sgid() >= 0); |
| 1020 |
|
| 1021 |
assert_se(chmod(path, 0775 | S_ISUID) < 0 && errno == EPERM); |
| 1022 |
assert_se(chmod(path, 0775 | S_ISGID) < 0 && errno == EPERM); |
| 1023 |
assert_se(chmod(path, 0775 | S_ISGID | S_ISUID) < 0 && errno == EPERM); |
| 1024 |
assert_se(chmod(path, 0775) >= 0); |
| 1025 |
|
| 1026 |
assert_se(fchmod(fd, 0775 | S_ISUID) < 0 && errno == EPERM); |
| 1027 |
assert_se(fchmod(fd, 0775 | S_ISGID) < 0 && errno == EPERM); |
| 1028 |
assert_se(fchmod(fd, 0775 | S_ISGID | S_ISUID) < 0 && errno == EPERM); |
| 1029 |
assert_se(fchmod(fd, 0775) >= 0); |
| 1030 |
|
| 1031 |
assert_se(fchmodat(AT_FDCWD, path, 0755 | S_ISUID, 0) < 0 && errno == EPERM); |
| 1032 |
assert_se(fchmodat(AT_FDCWD, path, 0755 | S_ISGID, 0) < 0 && errno == EPERM); |
| 1033 |
assert_se(fchmodat(AT_FDCWD, path, 0755 | S_ISGID | S_ISUID, 0) < 0 && errno == EPERM); |
| 1034 |
assert_se(fchmodat(AT_FDCWD, path, 0755, 0) >= 0); |
| 1035 |
|
| 1036 |
assert_se(try_fchmodat2(AT_FDCWD, path, 0755 | S_ISUID, 0) < 0 && errno == EPERM); |
| 1037 |
assert_se(try_fchmodat2(AT_FDCWD, path, 0755 | S_ISGID, 0) < 0 && errno == EPERM); |
| 1038 |
assert_se(try_fchmodat2(AT_FDCWD, path, 0755 | S_ISGID | S_ISUID, 0) < 0 && errno == EPERM); |
| 1039 |
assert_se(try_fchmodat2(AT_FDCWD, path, 0755, 0) >= 0); |
| 1040 |
|
| 1041 |
assert_se(real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID) < 0 && errno == EPERM); |
| 1042 |
assert_se(real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISGID) < 0 && errno == EPERM); |
| 1043 |
assert_se(real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID | S_ISGID) < 0 && errno == EPERM); |
| 1044 |
k = real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644); |
| 1045 |
k = safe_close(k); |
| 1046 |
assert_se(unlink(z) >= 0); |
| 1047 |
|
| 1048 |
assert_se(creat(z, 0644 | S_ISUID) < 0 && errno == EPERM); |
| 1049 |
assert_se(creat(z, 0644 | S_ISGID) < 0 && errno == EPERM); |
| 1050 |
assert_se(creat(z, 0644 | S_ISUID | S_ISGID) < 0 && errno == EPERM); |
| 1051 |
k = creat(z, 0644); |
| 1052 |
k = safe_close(k); |
| 1053 |
assert_se(unlink(z) >= 0); |
| 1054 |
|
| 1055 |
assert_se(openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID) < 0 && errno == EPERM); |
| 1056 |
assert_se(openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISGID) < 0 && errno == EPERM); |
| 1057 |
assert_se(openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID | S_ISGID) < 0 && errno == EPERM); |
| 1058 |
k = openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644); |
| 1059 |
k = safe_close(k); |
| 1060 |
assert_se(unlink(z) >= 0); |
| 1061 |
|
| 1062 |
assert_se(mkdir(z, 0755 | S_ISUID) < 0 && errno == EPERM); |
| 1063 |
assert_se(mkdir(z, 0755 | S_ISGID) < 0 && errno == EPERM); |
| 1064 |
assert_se(mkdir(z, 0755 | S_ISUID | S_ISGID) < 0 && errno == EPERM); |
| 1065 |
assert_se(mkdir(z, 0755) >= 0); |
| 1066 |
assert_se(rmdir(z) >= 0); |
| 1067 |
|
| 1068 |
assert_se(mkdirat(AT_FDCWD, z, 0755 | S_ISUID) < 0 && errno == EPERM); |
| 1069 |
assert_se(mkdirat(AT_FDCWD, z, 0755 | S_ISGID) < 0 && errno == EPERM); |
| 1070 |
assert_se(mkdirat(AT_FDCWD, z, 0755 | S_ISUID | S_ISGID) < 0 && errno == EPERM); |
| 1071 |
assert_se(mkdirat(AT_FDCWD, z, 0755) >= 0); |
| 1072 |
assert_se(rmdir(z) >= 0); |
| 1073 |
|
| 1074 |
assert_se(mknod(z, S_IFREG | 0755 | S_ISUID, 0) < 0 && errno == EPERM); |
| 1075 |
assert_se(mknod(z, S_IFREG | 0755 | S_ISGID, 0) < 0 && errno == EPERM); |
| 1076 |
assert_se(mknod(z, S_IFREG | 0755 | S_ISUID | S_ISGID, 0) < 0 && errno == EPERM); |
| 1077 |
assert_se(mknod(z, S_IFREG | 0755, 0) >= 0); |
| 1078 |
assert_se(unlink(z) >= 0); |
| 1079 |
|
| 1080 |
assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755 | S_ISUID, 0) < 0 && errno == EPERM); |
| 1081 |
assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755 | S_ISGID, 0) < 0 && errno == EPERM); |
| 1082 |
assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755 | S_ISUID | S_ISGID, 0) < 0 && errno == EPERM); |
| 1083 |
assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755, 0) >= 0); |
| 1084 |
assert_se(unlink(z) >= 0); |
| 1085 |
|
| 1086 |
assert_se(unlink(path) >= 0); |
| 1087 |
assert_se(rm_rf(dir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); |
| 1088 |
|
| 1089 |
_exit(EXIT_SUCCESS); |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
static void test_seccomp_suppress_sync_child(void) { |
| 1094 |
_cleanup_(unlink_and_freep) char *path = NULL; |
| 1095 |
_cleanup_close_ int fd = -EBADF; |
| 1096 |
|
| 1097 |
ASSERT_OK(tempfn_random("/tmp/seccomp_suppress_sync", NULL, &path)); |
| 1098 |
fd = open(path, O_RDWR | O_CREAT | O_SYNC | O_CLOEXEC, 0666); |
| 1099 |
/* We might be running in an environment where sync() is already suppressed. */ |
| 1100 |
if (fd >= 0) { |
| 1101 |
ASSERT_ERROR_ERRNO(fdatasync(-1), EBADF); |
| 1102 |
ASSERT_ERROR_ERRNO(fsync(-1), EBADF); |
| 1103 |
ASSERT_ERROR_ERRNO(syncfs(-1), EBADF); |
| 1104 |
|
| 1105 |
ASSERT_ERROR_ERRNO(fdatasync(INT_MAX), EBADF); |
| 1106 |
ASSERT_ERROR_ERRNO(fsync(INT_MAX), EBADF); |
| 1107 |
ASSERT_ERROR_ERRNO(syncfs(INT_MAX), EBADF); |
| 1108 |
} else if (errno != EINVAL) |
| 1109 |
ASSERT_OK_ERRNO(fd); |
| 1110 |
|
| 1111 |
ASSERT_OK(seccomp_suppress_sync()); |
| 1112 |
|
| 1113 |
fd = safe_close(fd); |
| 1114 |
fd = ASSERT_ERROR_ERRNO(open(path, O_RDWR | O_CREAT | O_SYNC | O_CLOEXEC, 0666), EINVAL); |
| 1115 |
|
| 1116 |
ASSERT_OK_ERRNO(fdatasync(INT_MAX)); |
| 1117 |
ASSERT_OK_ERRNO(fsync(INT_MAX)); |
| 1118 |
ASSERT_OK_ERRNO(syncfs(INT_MAX)); |
| 1119 |
|
| 1120 |
ASSERT_ERROR_ERRNO(fdatasync(-1), EBADF); |
| 1121 |
ASSERT_ERROR_ERRNO(fsync(-1), EBADF); |
| 1122 |
ASSERT_ERROR_ERRNO(syncfs(-1), EBADF); |
| 1123 |
} |
| 1124 |
|
| 1125 |
TEST(seccomp_suppress_sync) { |
| 1126 |
int r; |
| 1127 |
|
| 1128 |
CHECK_SECCOMP(/* skip_container= */ false); |
| 1129 |
|
| 1130 |
r = ASSERT_OK(pidref_safe_fork("(suppress-sync)", FORK_LOG|FORK_WAIT, NULL)); |
| 1131 |
if (r == 0) { |
| 1132 |
test_seccomp_suppress_sync_child(); |
| 1133 |
_exit(EXIT_SUCCESS); |
| 1134 |
} |
| 1135 |
} |
| 1136 |
|
| 1137 |
DEFINE_TEST_MAIN(LOG_DEBUG); |