Thomas Gleixner | e500db3 | 2019-06-04 10:11:14 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 4 | * |
| 5 | * Test code for seccomp bpf. |
| 6 | */ |
| 7 | |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 8 | #define _GNU_SOURCE |
Matt Redfearn | 0ce105b | 2016-03-29 09:35:29 +0100 | [diff] [blame] | 9 | #include <sys/types.h> |
Kees Cook | 10859f3 | 2017-09-07 16:32:46 -0700 | [diff] [blame] | 10 | |
| 11 | /* |
| 12 | * glibc 2.26 and later have SIGSYS in siginfo_t. Before that, |
| 13 | * we need to use the kernel's siginfo.h file and trick glibc |
| 14 | * into accepting it. |
| 15 | */ |
| 16 | #if !__GLIBC_PREREQ(2, 26) |
| 17 | # include <asm/siginfo.h> |
| 18 | # define __have_siginfo_t 1 |
| 19 | # define __have_sigval_t 1 |
| 20 | # define __have_sigevent_t 1 |
| 21 | #endif |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 22 | |
| 23 | #include <errno.h> |
| 24 | #include <linux/filter.h> |
| 25 | #include <sys/prctl.h> |
| 26 | #include <sys/ptrace.h> |
| 27 | #include <sys/user.h> |
| 28 | #include <linux/prctl.h> |
| 29 | #include <linux/ptrace.h> |
| 30 | #include <linux/seccomp.h> |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 31 | #include <pthread.h> |
| 32 | #include <semaphore.h> |
| 33 | #include <signal.h> |
| 34 | #include <stddef.h> |
| 35 | #include <stdbool.h> |
| 36 | #include <string.h> |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 37 | #include <time.h> |
Christian Brauner | 223e660 | 2019-09-20 10:30:06 +0200 | [diff] [blame] | 38 | #include <limits.h> |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 39 | #include <linux/elf.h> |
| 40 | #include <sys/uio.h> |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 41 | #include <sys/utsname.h> |
Robert Sesek | fd88d16 | 2015-11-02 15:28:49 -0500 | [diff] [blame] | 42 | #include <sys/fcntl.h> |
| 43 | #include <sys/mman.h> |
| 44 | #include <sys/times.h> |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 45 | #include <sys/socket.h> |
| 46 | #include <sys/ioctl.h> |
Christian Brauner | 0eebfed | 2019-09-20 10:30:07 +0200 | [diff] [blame] | 47 | #include <linux/kcmp.h> |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 48 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 49 | #include <unistd.h> |
| 50 | #include <sys/syscall.h> |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 51 | #include <poll.h> |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 52 | |
Mickaël Salaün | 0b40808 | 2017-05-26 20:43:56 +0200 | [diff] [blame] | 53 | #include "../kselftest_harness.h" |
Christian Brauner | ad56821 | 2020-05-31 13:50:31 +0200 | [diff] [blame^] | 54 | #include "../clone3/clone3_selftests.h" |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 55 | |
Kees Cook | 8b1bc88 | 2020-07-10 09:24:36 -0700 | [diff] [blame] | 56 | /* Attempt to de-conflict with the selftests tree. */ |
| 57 | #ifndef SKIP |
| 58 | #define SKIP(s, ...) XFAIL(s, ##__VA_ARGS__) |
| 59 | #endif |
| 60 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 61 | #ifndef PR_SET_PTRACER |
| 62 | # define PR_SET_PTRACER 0x59616d61 |
| 63 | #endif |
| 64 | |
| 65 | #ifndef PR_SET_NO_NEW_PRIVS |
| 66 | #define PR_SET_NO_NEW_PRIVS 38 |
| 67 | #define PR_GET_NO_NEW_PRIVS 39 |
| 68 | #endif |
| 69 | |
| 70 | #ifndef PR_SECCOMP_EXT |
| 71 | #define PR_SECCOMP_EXT 43 |
| 72 | #endif |
| 73 | |
| 74 | #ifndef SECCOMP_EXT_ACT |
| 75 | #define SECCOMP_EXT_ACT 1 |
| 76 | #endif |
| 77 | |
| 78 | #ifndef SECCOMP_EXT_ACT_TSYNC |
| 79 | #define SECCOMP_EXT_ACT_TSYNC 1 |
| 80 | #endif |
| 81 | |
| 82 | #ifndef SECCOMP_MODE_STRICT |
| 83 | #define SECCOMP_MODE_STRICT 1 |
| 84 | #endif |
| 85 | |
| 86 | #ifndef SECCOMP_MODE_FILTER |
| 87 | #define SECCOMP_MODE_FILTER 2 |
| 88 | #endif |
| 89 | |
Kees Cook | f3e1821 | 2017-08-11 13:20:33 -0700 | [diff] [blame] | 90 | #ifndef SECCOMP_RET_ALLOW |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 91 | struct seccomp_data { |
| 92 | int nr; |
| 93 | __u32 arch; |
| 94 | __u64 instruction_pointer; |
| 95 | __u64 args[6]; |
| 96 | }; |
| 97 | #endif |
| 98 | |
Kees Cook | f3e1821 | 2017-08-11 13:20:33 -0700 | [diff] [blame] | 99 | #ifndef SECCOMP_RET_KILL_PROCESS |
| 100 | #define SECCOMP_RET_KILL_PROCESS 0x80000000U /* kill the process */ |
Kees Cook | fd76875 | 2017-08-11 12:53:18 -0700 | [diff] [blame] | 101 | #define SECCOMP_RET_KILL_THREAD 0x00000000U /* kill the thread */ |
| 102 | #endif |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 103 | #ifndef SECCOMP_RET_KILL |
Kees Cook | fd76875 | 2017-08-11 12:53:18 -0700 | [diff] [blame] | 104 | #define SECCOMP_RET_KILL SECCOMP_RET_KILL_THREAD |
| 105 | #define SECCOMP_RET_TRAP 0x00030000U /* disallow and force a SIGSYS */ |
| 106 | #define SECCOMP_RET_ERRNO 0x00050000U /* returns an errno */ |
| 107 | #define SECCOMP_RET_TRACE 0x7ff00000U /* pass to a tracer or disallow */ |
| 108 | #define SECCOMP_RET_ALLOW 0x7fff0000U /* allow */ |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 109 | #endif |
| 110 | #ifndef SECCOMP_RET_LOG |
Kees Cook | fd76875 | 2017-08-11 12:53:18 -0700 | [diff] [blame] | 111 | #define SECCOMP_RET_LOG 0x7ffc0000U /* allow after logging */ |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 112 | #endif |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 113 | |
Kees Cook | f3e1821 | 2017-08-11 13:20:33 -0700 | [diff] [blame] | 114 | #ifndef __NR_seccomp |
| 115 | # if defined(__i386__) |
| 116 | # define __NR_seccomp 354 |
| 117 | # elif defined(__x86_64__) |
| 118 | # define __NR_seccomp 317 |
| 119 | # elif defined(__arm__) |
| 120 | # define __NR_seccomp 383 |
| 121 | # elif defined(__aarch64__) |
| 122 | # define __NR_seccomp 277 |
David Abdurachmanov | 5340627 | 2019-10-04 17:12:22 -0700 | [diff] [blame] | 123 | # elif defined(__riscv) |
| 124 | # define __NR_seccomp 277 |
Kees Cook | f3e1821 | 2017-08-11 13:20:33 -0700 | [diff] [blame] | 125 | # elif defined(__hppa__) |
| 126 | # define __NR_seccomp 338 |
| 127 | # elif defined(__powerpc__) |
| 128 | # define __NR_seccomp 358 |
| 129 | # elif defined(__s390__) |
| 130 | # define __NR_seccomp 348 |
| 131 | # else |
| 132 | # warning "seccomp syscall number unknown for this architecture" |
| 133 | # define __NR_seccomp 0xffff |
| 134 | # endif |
| 135 | #endif |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 136 | |
Kees Cook | f3e1821 | 2017-08-11 13:20:33 -0700 | [diff] [blame] | 137 | #ifndef SECCOMP_SET_MODE_STRICT |
| 138 | #define SECCOMP_SET_MODE_STRICT 0 |
| 139 | #endif |
| 140 | |
| 141 | #ifndef SECCOMP_SET_MODE_FILTER |
| 142 | #define SECCOMP_SET_MODE_FILTER 1 |
| 143 | #endif |
| 144 | |
| 145 | #ifndef SECCOMP_GET_ACTION_AVAIL |
| 146 | #define SECCOMP_GET_ACTION_AVAIL 2 |
| 147 | #endif |
| 148 | |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 149 | #ifndef SECCOMP_GET_NOTIF_SIZES |
| 150 | #define SECCOMP_GET_NOTIF_SIZES 3 |
| 151 | #endif |
| 152 | |
Kees Cook | f3e1821 | 2017-08-11 13:20:33 -0700 | [diff] [blame] | 153 | #ifndef SECCOMP_FILTER_FLAG_TSYNC |
Kees Cook | 00a02d0 | 2018-05-03 14:56:12 -0700 | [diff] [blame] | 154 | #define SECCOMP_FILTER_FLAG_TSYNC (1UL << 0) |
Kees Cook | f3e1821 | 2017-08-11 13:20:33 -0700 | [diff] [blame] | 155 | #endif |
| 156 | |
| 157 | #ifndef SECCOMP_FILTER_FLAG_LOG |
Kees Cook | 00a02d0 | 2018-05-03 14:56:12 -0700 | [diff] [blame] | 158 | #define SECCOMP_FILTER_FLAG_LOG (1UL << 1) |
| 159 | #endif |
| 160 | |
| 161 | #ifndef SECCOMP_FILTER_FLAG_SPEC_ALLOW |
| 162 | #define SECCOMP_FILTER_FLAG_SPEC_ALLOW (1UL << 2) |
Kees Cook | f3e1821 | 2017-08-11 13:20:33 -0700 | [diff] [blame] | 163 | #endif |
| 164 | |
Tycho Andersen | d057dc4 | 2018-02-20 19:47:47 -0700 | [diff] [blame] | 165 | #ifndef PTRACE_SECCOMP_GET_METADATA |
| 166 | #define PTRACE_SECCOMP_GET_METADATA 0x420d |
| 167 | |
| 168 | struct seccomp_metadata { |
| 169 | __u64 filter_off; /* Input: which filter */ |
| 170 | __u64 flags; /* Output: filter's flags */ |
| 171 | }; |
| 172 | #endif |
| 173 | |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 174 | #ifndef SECCOMP_FILTER_FLAG_NEW_LISTENER |
| 175 | #define SECCOMP_FILTER_FLAG_NEW_LISTENER (1UL << 3) |
| 176 | |
| 177 | #define SECCOMP_RET_USER_NOTIF 0x7fc00000U |
| 178 | |
| 179 | #define SECCOMP_IOC_MAGIC '!' |
| 180 | #define SECCOMP_IO(nr) _IO(SECCOMP_IOC_MAGIC, nr) |
| 181 | #define SECCOMP_IOR(nr, type) _IOR(SECCOMP_IOC_MAGIC, nr, type) |
| 182 | #define SECCOMP_IOW(nr, type) _IOW(SECCOMP_IOC_MAGIC, nr, type) |
| 183 | #define SECCOMP_IOWR(nr, type) _IOWR(SECCOMP_IOC_MAGIC, nr, type) |
| 184 | |
| 185 | /* Flags for seccomp notification fd ioctl. */ |
| 186 | #define SECCOMP_IOCTL_NOTIF_RECV SECCOMP_IOWR(0, struct seccomp_notif) |
| 187 | #define SECCOMP_IOCTL_NOTIF_SEND SECCOMP_IOWR(1, \ |
| 188 | struct seccomp_notif_resp) |
| 189 | #define SECCOMP_IOCTL_NOTIF_ID_VALID SECCOMP_IOR(2, __u64) |
| 190 | |
| 191 | struct seccomp_notif { |
| 192 | __u64 id; |
| 193 | __u32 pid; |
| 194 | __u32 flags; |
| 195 | struct seccomp_data data; |
| 196 | }; |
| 197 | |
| 198 | struct seccomp_notif_resp { |
| 199 | __u64 id; |
| 200 | __s64 val; |
| 201 | __s32 error; |
| 202 | __u32 flags; |
| 203 | }; |
| 204 | |
| 205 | struct seccomp_notif_sizes { |
| 206 | __u16 seccomp_notif; |
| 207 | __u16 seccomp_notif_resp; |
| 208 | __u16 seccomp_data; |
| 209 | }; |
| 210 | #endif |
| 211 | |
Tycho Andersen | 8828229 | 2019-08-26 08:43:02 -0600 | [diff] [blame] | 212 | #ifndef PTRACE_EVENTMSG_SYSCALL_ENTRY |
| 213 | #define PTRACE_EVENTMSG_SYSCALL_ENTRY 1 |
| 214 | #define PTRACE_EVENTMSG_SYSCALL_EXIT 2 |
| 215 | #endif |
| 216 | |
Christian Brauner | 2aa8d8d | 2019-10-21 11:10:55 +0200 | [diff] [blame] | 217 | #ifndef SECCOMP_USER_NOTIF_FLAG_CONTINUE |
| 218 | #define SECCOMP_USER_NOTIF_FLAG_CONTINUE 0x00000001 |
| 219 | #endif |
| 220 | |
Tycho Andersen | 5189149 | 2020-03-04 11:05:17 -0700 | [diff] [blame] | 221 | #ifndef SECCOMP_FILTER_FLAG_TSYNC_ESRCH |
| 222 | #define SECCOMP_FILTER_FLAG_TSYNC_ESRCH (1UL << 4) |
| 223 | #endif |
| 224 | |
Kees Cook | f3e1821 | 2017-08-11 13:20:33 -0700 | [diff] [blame] | 225 | #ifndef seccomp |
| 226 | int seccomp(unsigned int op, unsigned int flags, void *args) |
| 227 | { |
| 228 | errno = 0; |
| 229 | return syscall(__NR_seccomp, op, flags, args); |
| 230 | } |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 231 | #endif |
| 232 | |
Michael Ellerman | c385d0d | 2015-07-23 20:21:10 +1000 | [diff] [blame] | 233 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 234 | #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n])) |
Michael Ellerman | c385d0d | 2015-07-23 20:21:10 +1000 | [diff] [blame] | 235 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 236 | #define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]) + sizeof(__u32)) |
| 237 | #else |
| 238 | #error "wut? Unknown __BYTE_ORDER?!" |
| 239 | #endif |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 240 | |
| 241 | #define SIBLING_EXIT_UNKILLED 0xbadbeef |
| 242 | #define SIBLING_EXIT_FAILURE 0xbadface |
| 243 | #define SIBLING_EXIT_NEWPRIVS 0xbadfeed |
| 244 | |
| 245 | TEST(mode_strict_support) |
| 246 | { |
| 247 | long ret; |
| 248 | |
| 249 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, NULL, NULL, NULL); |
| 250 | ASSERT_EQ(0, ret) { |
| 251 | TH_LOG("Kernel does not support CONFIG_SECCOMP"); |
| 252 | } |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 253 | syscall(__NR_exit, 0); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | TEST_SIGNAL(mode_strict_cannot_call_prctl, SIGKILL) |
| 257 | { |
| 258 | long ret; |
| 259 | |
| 260 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, NULL, NULL, NULL); |
| 261 | ASSERT_EQ(0, ret) { |
| 262 | TH_LOG("Kernel does not support CONFIG_SECCOMP"); |
| 263 | } |
| 264 | syscall(__NR_prctl, PR_SET_SECCOMP, SECCOMP_MODE_FILTER, |
| 265 | NULL, NULL, NULL); |
| 266 | EXPECT_FALSE(true) { |
| 267 | TH_LOG("Unreachable!"); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /* Note! This doesn't test no new privs behavior */ |
| 272 | TEST(no_new_privs_support) |
| 273 | { |
| 274 | long ret; |
| 275 | |
| 276 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 277 | EXPECT_EQ(0, ret) { |
| 278 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 279 | } |
| 280 | } |
| 281 | |
Kees Cook | f3f6e30 | 2017-08-02 14:08:39 -0700 | [diff] [blame] | 282 | /* Tests kernel support by checking for a copy_from_user() fault on NULL. */ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 283 | TEST(mode_filter_support) |
| 284 | { |
| 285 | long ret; |
| 286 | |
| 287 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, NULL, 0, 0); |
| 288 | ASSERT_EQ(0, ret) { |
| 289 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 290 | } |
| 291 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL, NULL, NULL); |
| 292 | EXPECT_EQ(-1, ret); |
| 293 | EXPECT_EQ(EFAULT, errno) { |
| 294 | TH_LOG("Kernel does not support CONFIG_SECCOMP_FILTER!"); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | TEST(mode_filter_without_nnp) |
| 299 | { |
| 300 | struct sock_filter filter[] = { |
| 301 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 302 | }; |
| 303 | struct sock_fprog prog = { |
| 304 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 305 | .filter = filter, |
| 306 | }; |
| 307 | long ret; |
| 308 | |
| 309 | ret = prctl(PR_GET_NO_NEW_PRIVS, 0, NULL, 0, 0); |
| 310 | ASSERT_LE(0, ret) { |
| 311 | TH_LOG("Expected 0 or unsupported for NO_NEW_PRIVS"); |
| 312 | } |
| 313 | errno = 0; |
| 314 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); |
| 315 | /* Succeeds with CAP_SYS_ADMIN, fails without */ |
| 316 | /* TODO(wad) check caps not euid */ |
| 317 | if (geteuid()) { |
| 318 | EXPECT_EQ(-1, ret); |
| 319 | EXPECT_EQ(EACCES, errno); |
| 320 | } else { |
| 321 | EXPECT_EQ(0, ret); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | #define MAX_INSNS_PER_PATH 32768 |
| 326 | |
| 327 | TEST(filter_size_limits) |
| 328 | { |
| 329 | int i; |
| 330 | int count = BPF_MAXINSNS + 1; |
| 331 | struct sock_filter allow[] = { |
| 332 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 333 | }; |
| 334 | struct sock_filter *filter; |
| 335 | struct sock_fprog prog = { }; |
| 336 | long ret; |
| 337 | |
| 338 | filter = calloc(count, sizeof(*filter)); |
| 339 | ASSERT_NE(NULL, filter); |
| 340 | |
| 341 | for (i = 0; i < count; i++) |
| 342 | filter[i] = allow[0]; |
| 343 | |
| 344 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 345 | ASSERT_EQ(0, ret); |
| 346 | |
| 347 | prog.filter = filter; |
| 348 | prog.len = count; |
| 349 | |
| 350 | /* Too many filter instructions in a single filter. */ |
| 351 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); |
| 352 | ASSERT_NE(0, ret) { |
| 353 | TH_LOG("Installing %d insn filter was allowed", prog.len); |
| 354 | } |
| 355 | |
| 356 | /* One less is okay, though. */ |
| 357 | prog.len -= 1; |
| 358 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); |
| 359 | ASSERT_EQ(0, ret) { |
| 360 | TH_LOG("Installing %d insn filter wasn't allowed", prog.len); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | TEST(filter_chain_limits) |
| 365 | { |
| 366 | int i; |
| 367 | int count = BPF_MAXINSNS; |
| 368 | struct sock_filter allow[] = { |
| 369 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 370 | }; |
| 371 | struct sock_filter *filter; |
| 372 | struct sock_fprog prog = { }; |
| 373 | long ret; |
| 374 | |
| 375 | filter = calloc(count, sizeof(*filter)); |
| 376 | ASSERT_NE(NULL, filter); |
| 377 | |
| 378 | for (i = 0; i < count; i++) |
| 379 | filter[i] = allow[0]; |
| 380 | |
| 381 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 382 | ASSERT_EQ(0, ret); |
| 383 | |
| 384 | prog.filter = filter; |
| 385 | prog.len = 1; |
| 386 | |
| 387 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); |
| 388 | ASSERT_EQ(0, ret); |
| 389 | |
| 390 | prog.len = count; |
| 391 | |
| 392 | /* Too many total filter instructions. */ |
| 393 | for (i = 0; i < MAX_INSNS_PER_PATH; i++) { |
| 394 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); |
| 395 | if (ret != 0) |
| 396 | break; |
| 397 | } |
| 398 | ASSERT_NE(0, ret) { |
| 399 | TH_LOG("Allowed %d %d-insn filters (total with penalties:%d)", |
| 400 | i, count, i * (count + 4)); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | TEST(mode_filter_cannot_move_to_strict) |
| 405 | { |
| 406 | struct sock_filter filter[] = { |
| 407 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 408 | }; |
| 409 | struct sock_fprog prog = { |
| 410 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 411 | .filter = filter, |
| 412 | }; |
| 413 | long ret; |
| 414 | |
| 415 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 416 | ASSERT_EQ(0, ret); |
| 417 | |
| 418 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); |
| 419 | ASSERT_EQ(0, ret); |
| 420 | |
| 421 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, NULL, 0, 0); |
| 422 | EXPECT_EQ(-1, ret); |
| 423 | EXPECT_EQ(EINVAL, errno); |
| 424 | } |
| 425 | |
| 426 | |
| 427 | TEST(mode_filter_get_seccomp) |
| 428 | { |
| 429 | struct sock_filter filter[] = { |
| 430 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 431 | }; |
| 432 | struct sock_fprog prog = { |
| 433 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 434 | .filter = filter, |
| 435 | }; |
| 436 | long ret; |
| 437 | |
| 438 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 439 | ASSERT_EQ(0, ret); |
| 440 | |
| 441 | ret = prctl(PR_GET_SECCOMP, 0, 0, 0, 0); |
| 442 | EXPECT_EQ(0, ret); |
| 443 | |
| 444 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); |
| 445 | ASSERT_EQ(0, ret); |
| 446 | |
| 447 | ret = prctl(PR_GET_SECCOMP, 0, 0, 0, 0); |
| 448 | EXPECT_EQ(2, ret); |
| 449 | } |
| 450 | |
| 451 | |
| 452 | TEST(ALLOW_all) |
| 453 | { |
| 454 | struct sock_filter filter[] = { |
| 455 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 456 | }; |
| 457 | struct sock_fprog prog = { |
| 458 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 459 | .filter = filter, |
| 460 | }; |
| 461 | long ret; |
| 462 | |
| 463 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 464 | ASSERT_EQ(0, ret); |
| 465 | |
| 466 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); |
| 467 | ASSERT_EQ(0, ret); |
| 468 | } |
| 469 | |
| 470 | TEST(empty_prog) |
| 471 | { |
| 472 | struct sock_filter filter[] = { |
| 473 | }; |
| 474 | struct sock_fprog prog = { |
| 475 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 476 | .filter = filter, |
| 477 | }; |
| 478 | long ret; |
| 479 | |
| 480 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 481 | ASSERT_EQ(0, ret); |
| 482 | |
| 483 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); |
| 484 | EXPECT_EQ(-1, ret); |
| 485 | EXPECT_EQ(EINVAL, errno); |
| 486 | } |
| 487 | |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 488 | TEST(log_all) |
| 489 | { |
| 490 | struct sock_filter filter[] = { |
| 491 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_LOG), |
| 492 | }; |
| 493 | struct sock_fprog prog = { |
| 494 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 495 | .filter = filter, |
| 496 | }; |
| 497 | long ret; |
| 498 | pid_t parent = getppid(); |
| 499 | |
| 500 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 501 | ASSERT_EQ(0, ret); |
| 502 | |
| 503 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); |
| 504 | ASSERT_EQ(0, ret); |
| 505 | |
| 506 | /* getppid() should succeed and be logged (no check for logging) */ |
| 507 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 508 | } |
| 509 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 510 | TEST_SIGNAL(unknown_ret_is_kill_inside, SIGSYS) |
| 511 | { |
| 512 | struct sock_filter filter[] = { |
| 513 | BPF_STMT(BPF_RET|BPF_K, 0x10000000U), |
| 514 | }; |
| 515 | struct sock_fprog prog = { |
| 516 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 517 | .filter = filter, |
| 518 | }; |
| 519 | long ret; |
| 520 | |
| 521 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 522 | ASSERT_EQ(0, ret); |
| 523 | |
| 524 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); |
| 525 | ASSERT_EQ(0, ret); |
| 526 | EXPECT_EQ(0, syscall(__NR_getpid)) { |
| 527 | TH_LOG("getpid() shouldn't ever return"); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /* return code >= 0x80000000 is unused. */ |
| 532 | TEST_SIGNAL(unknown_ret_is_kill_above_allow, SIGSYS) |
| 533 | { |
| 534 | struct sock_filter filter[] = { |
| 535 | BPF_STMT(BPF_RET|BPF_K, 0x90000000U), |
| 536 | }; |
| 537 | struct sock_fprog prog = { |
| 538 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 539 | .filter = filter, |
| 540 | }; |
| 541 | long ret; |
| 542 | |
| 543 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 544 | ASSERT_EQ(0, ret); |
| 545 | |
| 546 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); |
| 547 | ASSERT_EQ(0, ret); |
| 548 | EXPECT_EQ(0, syscall(__NR_getpid)) { |
| 549 | TH_LOG("getpid() shouldn't ever return"); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | TEST_SIGNAL(KILL_all, SIGSYS) |
| 554 | { |
| 555 | struct sock_filter filter[] = { |
| 556 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), |
| 557 | }; |
| 558 | struct sock_fprog prog = { |
| 559 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 560 | .filter = filter, |
| 561 | }; |
| 562 | long ret; |
| 563 | |
| 564 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 565 | ASSERT_EQ(0, ret); |
| 566 | |
| 567 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); |
| 568 | ASSERT_EQ(0, ret); |
| 569 | } |
| 570 | |
| 571 | TEST_SIGNAL(KILL_one, SIGSYS) |
| 572 | { |
| 573 | struct sock_filter filter[] = { |
| 574 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 575 | offsetof(struct seccomp_data, nr)), |
| 576 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 0, 1), |
| 577 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), |
| 578 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 579 | }; |
| 580 | struct sock_fprog prog = { |
| 581 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 582 | .filter = filter, |
| 583 | }; |
| 584 | long ret; |
| 585 | pid_t parent = getppid(); |
| 586 | |
| 587 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 588 | ASSERT_EQ(0, ret); |
| 589 | |
| 590 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); |
| 591 | ASSERT_EQ(0, ret); |
| 592 | |
| 593 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 594 | /* getpid() should never return. */ |
| 595 | EXPECT_EQ(0, syscall(__NR_getpid)); |
| 596 | } |
| 597 | |
| 598 | TEST_SIGNAL(KILL_one_arg_one, SIGSYS) |
| 599 | { |
Robert Sesek | fd88d16 | 2015-11-02 15:28:49 -0500 | [diff] [blame] | 600 | void *fatal_address; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 601 | struct sock_filter filter[] = { |
| 602 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 603 | offsetof(struct seccomp_data, nr)), |
Robert Sesek | fd88d16 | 2015-11-02 15:28:49 -0500 | [diff] [blame] | 604 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_times, 1, 0), |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 605 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 606 | /* Only both with lower 32-bit for now. */ |
| 607 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg(0)), |
Robert Sesek | fd88d16 | 2015-11-02 15:28:49 -0500 | [diff] [blame] | 608 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, |
| 609 | (unsigned long)&fatal_address, 0, 1), |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 610 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), |
| 611 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 612 | }; |
| 613 | struct sock_fprog prog = { |
| 614 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 615 | .filter = filter, |
| 616 | }; |
| 617 | long ret; |
| 618 | pid_t parent = getppid(); |
Robert Sesek | fd88d16 | 2015-11-02 15:28:49 -0500 | [diff] [blame] | 619 | struct tms timebuf; |
| 620 | clock_t clock = times(&timebuf); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 621 | |
| 622 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 623 | ASSERT_EQ(0, ret); |
| 624 | |
| 625 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); |
| 626 | ASSERT_EQ(0, ret); |
| 627 | |
| 628 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
Robert Sesek | fd88d16 | 2015-11-02 15:28:49 -0500 | [diff] [blame] | 629 | EXPECT_LE(clock, syscall(__NR_times, &timebuf)); |
| 630 | /* times() should never return. */ |
| 631 | EXPECT_EQ(0, syscall(__NR_times, &fatal_address)); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | TEST_SIGNAL(KILL_one_arg_six, SIGSYS) |
| 635 | { |
Robert Sesek | fd88d16 | 2015-11-02 15:28:49 -0500 | [diff] [blame] | 636 | #ifndef __NR_mmap2 |
| 637 | int sysno = __NR_mmap; |
| 638 | #else |
| 639 | int sysno = __NR_mmap2; |
| 640 | #endif |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 641 | struct sock_filter filter[] = { |
| 642 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 643 | offsetof(struct seccomp_data, nr)), |
Robert Sesek | fd88d16 | 2015-11-02 15:28:49 -0500 | [diff] [blame] | 644 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, sysno, 1, 0), |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 645 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 646 | /* Only both with lower 32-bit for now. */ |
| 647 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg(5)), |
| 648 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x0C0FFEE, 0, 1), |
| 649 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), |
| 650 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 651 | }; |
| 652 | struct sock_fprog prog = { |
| 653 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 654 | .filter = filter, |
| 655 | }; |
| 656 | long ret; |
| 657 | pid_t parent = getppid(); |
Robert Sesek | fd88d16 | 2015-11-02 15:28:49 -0500 | [diff] [blame] | 658 | int fd; |
| 659 | void *map1, *map2; |
Bamvor Jian Zhang | 2ce47b4 | 2015-11-13 11:17:51 +0800 | [diff] [blame] | 660 | int page_size = sysconf(_SC_PAGESIZE); |
| 661 | |
| 662 | ASSERT_LT(0, page_size); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 663 | |
| 664 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 665 | ASSERT_EQ(0, ret); |
| 666 | |
| 667 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); |
| 668 | ASSERT_EQ(0, ret); |
| 669 | |
Robert Sesek | fd88d16 | 2015-11-02 15:28:49 -0500 | [diff] [blame] | 670 | fd = open("/dev/zero", O_RDONLY); |
| 671 | ASSERT_NE(-1, fd); |
| 672 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 673 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
Robert Sesek | fd88d16 | 2015-11-02 15:28:49 -0500 | [diff] [blame] | 674 | map1 = (void *)syscall(sysno, |
Bamvor Jian Zhang | 2ce47b4 | 2015-11-13 11:17:51 +0800 | [diff] [blame] | 675 | NULL, page_size, PROT_READ, MAP_PRIVATE, fd, page_size); |
Robert Sesek | fd88d16 | 2015-11-02 15:28:49 -0500 | [diff] [blame] | 676 | EXPECT_NE(MAP_FAILED, map1); |
| 677 | /* mmap2() should never return. */ |
| 678 | map2 = (void *)syscall(sysno, |
Bamvor Jian Zhang | 2ce47b4 | 2015-11-13 11:17:51 +0800 | [diff] [blame] | 679 | NULL, page_size, PROT_READ, MAP_PRIVATE, fd, 0x0C0FFEE); |
Robert Sesek | fd88d16 | 2015-11-02 15:28:49 -0500 | [diff] [blame] | 680 | EXPECT_EQ(MAP_FAILED, map2); |
| 681 | |
| 682 | /* The test failed, so clean up the resources. */ |
Bamvor Jian Zhang | 2ce47b4 | 2015-11-13 11:17:51 +0800 | [diff] [blame] | 683 | munmap(map1, page_size); |
| 684 | munmap(map2, page_size); |
Robert Sesek | fd88d16 | 2015-11-02 15:28:49 -0500 | [diff] [blame] | 685 | close(fd); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Kees Cook | f3e1821 | 2017-08-11 13:20:33 -0700 | [diff] [blame] | 688 | /* This is a thread task to die via seccomp filter violation. */ |
| 689 | void *kill_thread(void *data) |
| 690 | { |
| 691 | bool die = (bool)data; |
| 692 | |
| 693 | if (die) { |
| 694 | prctl(PR_GET_SECCOMP, 0, 0, 0, 0); |
| 695 | return (void *)SIBLING_EXIT_FAILURE; |
| 696 | } |
| 697 | |
| 698 | return (void *)SIBLING_EXIT_UNKILLED; |
| 699 | } |
| 700 | |
| 701 | /* Prepare a thread that will kill itself or both of us. */ |
| 702 | void kill_thread_or_group(struct __test_metadata *_metadata, bool kill_process) |
| 703 | { |
| 704 | pthread_t thread; |
| 705 | void *status; |
| 706 | /* Kill only when calling __NR_prctl. */ |
| 707 | struct sock_filter filter_thread[] = { |
| 708 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 709 | offsetof(struct seccomp_data, nr)), |
| 710 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_prctl, 0, 1), |
| 711 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL_THREAD), |
| 712 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 713 | }; |
| 714 | struct sock_fprog prog_thread = { |
| 715 | .len = (unsigned short)ARRAY_SIZE(filter_thread), |
| 716 | .filter = filter_thread, |
| 717 | }; |
| 718 | struct sock_filter filter_process[] = { |
| 719 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 720 | offsetof(struct seccomp_data, nr)), |
| 721 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_prctl, 0, 1), |
| 722 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL_PROCESS), |
| 723 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 724 | }; |
| 725 | struct sock_fprog prog_process = { |
| 726 | .len = (unsigned short)ARRAY_SIZE(filter_process), |
| 727 | .filter = filter_process, |
| 728 | }; |
| 729 | |
| 730 | ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { |
| 731 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 732 | } |
| 733 | |
| 734 | ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER, 0, |
| 735 | kill_process ? &prog_process : &prog_thread)); |
| 736 | |
| 737 | /* |
| 738 | * Add the KILL_THREAD rule again to make sure that the KILL_PROCESS |
| 739 | * flag cannot be downgraded by a new filter. |
| 740 | */ |
| 741 | ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog_thread)); |
| 742 | |
| 743 | /* Start a thread that will exit immediately. */ |
| 744 | ASSERT_EQ(0, pthread_create(&thread, NULL, kill_thread, (void *)false)); |
| 745 | ASSERT_EQ(0, pthread_join(thread, &status)); |
| 746 | ASSERT_EQ(SIBLING_EXIT_UNKILLED, (unsigned long)status); |
| 747 | |
| 748 | /* Start a thread that will die immediately. */ |
| 749 | ASSERT_EQ(0, pthread_create(&thread, NULL, kill_thread, (void *)true)); |
| 750 | ASSERT_EQ(0, pthread_join(thread, &status)); |
| 751 | ASSERT_NE(SIBLING_EXIT_FAILURE, (unsigned long)status); |
| 752 | |
| 753 | /* |
| 754 | * If we get here, only the spawned thread died. Let the parent know |
| 755 | * the whole process didn't die (i.e. this thread, the spawner, |
| 756 | * stayed running). |
| 757 | */ |
| 758 | exit(42); |
| 759 | } |
| 760 | |
| 761 | TEST(KILL_thread) |
| 762 | { |
| 763 | int status; |
| 764 | pid_t child_pid; |
| 765 | |
| 766 | child_pid = fork(); |
| 767 | ASSERT_LE(0, child_pid); |
| 768 | if (child_pid == 0) { |
| 769 | kill_thread_or_group(_metadata, false); |
| 770 | _exit(38); |
| 771 | } |
| 772 | |
| 773 | ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); |
| 774 | |
| 775 | /* If only the thread was killed, we'll see exit 42. */ |
| 776 | ASSERT_TRUE(WIFEXITED(status)); |
| 777 | ASSERT_EQ(42, WEXITSTATUS(status)); |
| 778 | } |
| 779 | |
| 780 | TEST(KILL_process) |
| 781 | { |
| 782 | int status; |
| 783 | pid_t child_pid; |
| 784 | |
| 785 | child_pid = fork(); |
| 786 | ASSERT_LE(0, child_pid); |
| 787 | if (child_pid == 0) { |
| 788 | kill_thread_or_group(_metadata, true); |
| 789 | _exit(38); |
| 790 | } |
| 791 | |
| 792 | ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); |
| 793 | |
| 794 | /* If the entire process was killed, we'll see SIGSYS. */ |
| 795 | ASSERT_TRUE(WIFSIGNALED(status)); |
| 796 | ASSERT_EQ(SIGSYS, WTERMSIG(status)); |
| 797 | } |
| 798 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 799 | /* TODO(wad) add 64-bit versus 32-bit arg tests. */ |
| 800 | TEST(arg_out_of_range) |
| 801 | { |
| 802 | struct sock_filter filter[] = { |
| 803 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg(6)), |
| 804 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 805 | }; |
| 806 | struct sock_fprog prog = { |
| 807 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 808 | .filter = filter, |
| 809 | }; |
| 810 | long ret; |
| 811 | |
| 812 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 813 | ASSERT_EQ(0, ret); |
| 814 | |
| 815 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); |
| 816 | EXPECT_EQ(-1, ret); |
| 817 | EXPECT_EQ(EINVAL, errno); |
| 818 | } |
| 819 | |
Kees Cook | f3f6e30 | 2017-08-02 14:08:39 -0700 | [diff] [blame] | 820 | #define ERRNO_FILTER(name, errno) \ |
| 821 | struct sock_filter _read_filter_##name[] = { \ |
| 822 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, \ |
| 823 | offsetof(struct seccomp_data, nr)), \ |
| 824 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1), \ |
| 825 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | errno), \ |
| 826 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), \ |
| 827 | }; \ |
| 828 | struct sock_fprog prog_##name = { \ |
| 829 | .len = (unsigned short)ARRAY_SIZE(_read_filter_##name), \ |
| 830 | .filter = _read_filter_##name, \ |
| 831 | } |
| 832 | |
| 833 | /* Make sure basic errno values are correctly passed through a filter. */ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 834 | TEST(ERRNO_valid) |
| 835 | { |
Kees Cook | f3f6e30 | 2017-08-02 14:08:39 -0700 | [diff] [blame] | 836 | ERRNO_FILTER(valid, E2BIG); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 837 | long ret; |
| 838 | pid_t parent = getppid(); |
| 839 | |
| 840 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 841 | ASSERT_EQ(0, ret); |
| 842 | |
Kees Cook | f3f6e30 | 2017-08-02 14:08:39 -0700 | [diff] [blame] | 843 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_valid); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 844 | ASSERT_EQ(0, ret); |
| 845 | |
| 846 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 847 | EXPECT_EQ(-1, read(0, NULL, 0)); |
| 848 | EXPECT_EQ(E2BIG, errno); |
| 849 | } |
| 850 | |
Kees Cook | f3f6e30 | 2017-08-02 14:08:39 -0700 | [diff] [blame] | 851 | /* Make sure an errno of zero is correctly handled by the arch code. */ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 852 | TEST(ERRNO_zero) |
| 853 | { |
Kees Cook | f3f6e30 | 2017-08-02 14:08:39 -0700 | [diff] [blame] | 854 | ERRNO_FILTER(zero, 0); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 855 | long ret; |
| 856 | pid_t parent = getppid(); |
| 857 | |
| 858 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 859 | ASSERT_EQ(0, ret); |
| 860 | |
Kees Cook | f3f6e30 | 2017-08-02 14:08:39 -0700 | [diff] [blame] | 861 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_zero); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 862 | ASSERT_EQ(0, ret); |
| 863 | |
| 864 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 865 | /* "errno" of 0 is ok. */ |
| 866 | EXPECT_EQ(0, read(0, NULL, 0)); |
| 867 | } |
| 868 | |
Kees Cook | f3f6e30 | 2017-08-02 14:08:39 -0700 | [diff] [blame] | 869 | /* |
| 870 | * The SECCOMP_RET_DATA mask is 16 bits wide, but errno is smaller. |
| 871 | * This tests that the errno value gets capped correctly, fixed by |
| 872 | * 580c57f10768 ("seccomp: cap SECCOMP_RET_ERRNO data to MAX_ERRNO"). |
| 873 | */ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 874 | TEST(ERRNO_capped) |
| 875 | { |
Kees Cook | f3f6e30 | 2017-08-02 14:08:39 -0700 | [diff] [blame] | 876 | ERRNO_FILTER(capped, 4096); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 877 | long ret; |
| 878 | pid_t parent = getppid(); |
| 879 | |
| 880 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 881 | ASSERT_EQ(0, ret); |
| 882 | |
Kees Cook | f3f6e30 | 2017-08-02 14:08:39 -0700 | [diff] [blame] | 883 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_capped); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 884 | ASSERT_EQ(0, ret); |
| 885 | |
| 886 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 887 | EXPECT_EQ(-1, read(0, NULL, 0)); |
| 888 | EXPECT_EQ(4095, errno); |
| 889 | } |
| 890 | |
Kees Cook | f3f6e30 | 2017-08-02 14:08:39 -0700 | [diff] [blame] | 891 | /* |
| 892 | * Filters are processed in reverse order: last applied is executed first. |
| 893 | * Since only the SECCOMP_RET_ACTION mask is tested for return values, the |
| 894 | * SECCOMP_RET_DATA mask results will follow the most recently applied |
| 895 | * matching filter return (and not the lowest or highest value). |
| 896 | */ |
| 897 | TEST(ERRNO_order) |
| 898 | { |
| 899 | ERRNO_FILTER(first, 11); |
| 900 | ERRNO_FILTER(second, 13); |
| 901 | ERRNO_FILTER(third, 12); |
| 902 | long ret; |
| 903 | pid_t parent = getppid(); |
| 904 | |
| 905 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 906 | ASSERT_EQ(0, ret); |
| 907 | |
| 908 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_first); |
| 909 | ASSERT_EQ(0, ret); |
| 910 | |
| 911 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_second); |
| 912 | ASSERT_EQ(0, ret); |
| 913 | |
| 914 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog_third); |
| 915 | ASSERT_EQ(0, ret); |
| 916 | |
| 917 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 918 | EXPECT_EQ(-1, read(0, NULL, 0)); |
| 919 | EXPECT_EQ(12, errno); |
| 920 | } |
| 921 | |
Kees Cook | 1ae81d7 | 2020-03-11 12:21:28 -0700 | [diff] [blame] | 922 | FIXTURE(TRAP) { |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 923 | struct sock_fprog prog; |
| 924 | }; |
| 925 | |
| 926 | FIXTURE_SETUP(TRAP) |
| 927 | { |
| 928 | struct sock_filter filter[] = { |
| 929 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 930 | offsetof(struct seccomp_data, nr)), |
| 931 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 0, 1), |
| 932 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP), |
| 933 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 934 | }; |
| 935 | |
| 936 | memset(&self->prog, 0, sizeof(self->prog)); |
| 937 | self->prog.filter = malloc(sizeof(filter)); |
| 938 | ASSERT_NE(NULL, self->prog.filter); |
| 939 | memcpy(self->prog.filter, filter, sizeof(filter)); |
| 940 | self->prog.len = (unsigned short)ARRAY_SIZE(filter); |
| 941 | } |
| 942 | |
| 943 | FIXTURE_TEARDOWN(TRAP) |
| 944 | { |
| 945 | if (self->prog.filter) |
| 946 | free(self->prog.filter); |
| 947 | } |
| 948 | |
| 949 | TEST_F_SIGNAL(TRAP, dfl, SIGSYS) |
| 950 | { |
| 951 | long ret; |
| 952 | |
| 953 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 954 | ASSERT_EQ(0, ret); |
| 955 | |
| 956 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog); |
| 957 | ASSERT_EQ(0, ret); |
| 958 | syscall(__NR_getpid); |
| 959 | } |
| 960 | |
| 961 | /* Ensure that SIGSYS overrides SIG_IGN */ |
| 962 | TEST_F_SIGNAL(TRAP, ign, SIGSYS) |
| 963 | { |
| 964 | long ret; |
| 965 | |
| 966 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 967 | ASSERT_EQ(0, ret); |
| 968 | |
| 969 | signal(SIGSYS, SIG_IGN); |
| 970 | |
| 971 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog); |
| 972 | ASSERT_EQ(0, ret); |
| 973 | syscall(__NR_getpid); |
| 974 | } |
| 975 | |
Kees Cook | 10859f3 | 2017-09-07 16:32:46 -0700 | [diff] [blame] | 976 | static siginfo_t TRAP_info; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 977 | static volatile int TRAP_nr; |
| 978 | static void TRAP_action(int nr, siginfo_t *info, void *void_context) |
| 979 | { |
| 980 | memcpy(&TRAP_info, info, sizeof(TRAP_info)); |
| 981 | TRAP_nr = nr; |
| 982 | } |
| 983 | |
| 984 | TEST_F(TRAP, handler) |
| 985 | { |
| 986 | int ret, test; |
| 987 | struct sigaction act; |
| 988 | sigset_t mask; |
| 989 | |
| 990 | memset(&act, 0, sizeof(act)); |
| 991 | sigemptyset(&mask); |
| 992 | sigaddset(&mask, SIGSYS); |
| 993 | |
| 994 | act.sa_sigaction = &TRAP_action; |
| 995 | act.sa_flags = SA_SIGINFO; |
| 996 | ret = sigaction(SIGSYS, &act, NULL); |
| 997 | ASSERT_EQ(0, ret) { |
| 998 | TH_LOG("sigaction failed"); |
| 999 | } |
| 1000 | ret = sigprocmask(SIG_UNBLOCK, &mask, NULL); |
| 1001 | ASSERT_EQ(0, ret) { |
| 1002 | TH_LOG("sigprocmask failed"); |
| 1003 | } |
| 1004 | |
| 1005 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1006 | ASSERT_EQ(0, ret); |
| 1007 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog); |
| 1008 | ASSERT_EQ(0, ret); |
| 1009 | TRAP_nr = 0; |
| 1010 | memset(&TRAP_info, 0, sizeof(TRAP_info)); |
| 1011 | /* Expect the registers to be rolled back. (nr = error) may vary |
| 1012 | * based on arch. */ |
| 1013 | ret = syscall(__NR_getpid); |
| 1014 | /* Silence gcc warning about volatile. */ |
| 1015 | test = TRAP_nr; |
| 1016 | EXPECT_EQ(SIGSYS, test); |
| 1017 | struct local_sigsys { |
| 1018 | void *_call_addr; /* calling user insn */ |
| 1019 | int _syscall; /* triggering system call number */ |
| 1020 | unsigned int _arch; /* AUDIT_ARCH_* of syscall */ |
| 1021 | } *sigsys = (struct local_sigsys *) |
| 1022 | #ifdef si_syscall |
| 1023 | &(TRAP_info.si_call_addr); |
| 1024 | #else |
| 1025 | &TRAP_info.si_pid; |
| 1026 | #endif |
| 1027 | EXPECT_EQ(__NR_getpid, sigsys->_syscall); |
| 1028 | /* Make sure arch is non-zero. */ |
| 1029 | EXPECT_NE(0, sigsys->_arch); |
| 1030 | EXPECT_NE(0, (unsigned long)sigsys->_call_addr); |
| 1031 | } |
| 1032 | |
Kees Cook | 1ae81d7 | 2020-03-11 12:21:28 -0700 | [diff] [blame] | 1033 | FIXTURE(precedence) { |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1034 | struct sock_fprog allow; |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 1035 | struct sock_fprog log; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1036 | struct sock_fprog trace; |
| 1037 | struct sock_fprog error; |
| 1038 | struct sock_fprog trap; |
| 1039 | struct sock_fprog kill; |
| 1040 | }; |
| 1041 | |
| 1042 | FIXTURE_SETUP(precedence) |
| 1043 | { |
| 1044 | struct sock_filter allow_insns[] = { |
| 1045 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 1046 | }; |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 1047 | struct sock_filter log_insns[] = { |
| 1048 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 1049 | offsetof(struct seccomp_data, nr)), |
| 1050 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0), |
| 1051 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 1052 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_LOG), |
| 1053 | }; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1054 | struct sock_filter trace_insns[] = { |
| 1055 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 1056 | offsetof(struct seccomp_data, nr)), |
| 1057 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0), |
| 1058 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 1059 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE), |
| 1060 | }; |
| 1061 | struct sock_filter error_insns[] = { |
| 1062 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 1063 | offsetof(struct seccomp_data, nr)), |
| 1064 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0), |
| 1065 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 1066 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO), |
| 1067 | }; |
| 1068 | struct sock_filter trap_insns[] = { |
| 1069 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 1070 | offsetof(struct seccomp_data, nr)), |
| 1071 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0), |
| 1072 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 1073 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP), |
| 1074 | }; |
| 1075 | struct sock_filter kill_insns[] = { |
| 1076 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 1077 | offsetof(struct seccomp_data, nr)), |
| 1078 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 1, 0), |
| 1079 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 1080 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), |
| 1081 | }; |
| 1082 | |
| 1083 | memset(self, 0, sizeof(*self)); |
| 1084 | #define FILTER_ALLOC(_x) \ |
| 1085 | self->_x.filter = malloc(sizeof(_x##_insns)); \ |
| 1086 | ASSERT_NE(NULL, self->_x.filter); \ |
| 1087 | memcpy(self->_x.filter, &_x##_insns, sizeof(_x##_insns)); \ |
| 1088 | self->_x.len = (unsigned short)ARRAY_SIZE(_x##_insns) |
| 1089 | FILTER_ALLOC(allow); |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 1090 | FILTER_ALLOC(log); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1091 | FILTER_ALLOC(trace); |
| 1092 | FILTER_ALLOC(error); |
| 1093 | FILTER_ALLOC(trap); |
| 1094 | FILTER_ALLOC(kill); |
| 1095 | } |
| 1096 | |
| 1097 | FIXTURE_TEARDOWN(precedence) |
| 1098 | { |
| 1099 | #define FILTER_FREE(_x) if (self->_x.filter) free(self->_x.filter) |
| 1100 | FILTER_FREE(allow); |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 1101 | FILTER_FREE(log); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1102 | FILTER_FREE(trace); |
| 1103 | FILTER_FREE(error); |
| 1104 | FILTER_FREE(trap); |
| 1105 | FILTER_FREE(kill); |
| 1106 | } |
| 1107 | |
| 1108 | TEST_F(precedence, allow_ok) |
| 1109 | { |
| 1110 | pid_t parent, res = 0; |
| 1111 | long ret; |
| 1112 | |
| 1113 | parent = getppid(); |
| 1114 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1115 | ASSERT_EQ(0, ret); |
| 1116 | |
| 1117 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); |
| 1118 | ASSERT_EQ(0, ret); |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 1119 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); |
| 1120 | ASSERT_EQ(0, ret); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1121 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); |
| 1122 | ASSERT_EQ(0, ret); |
| 1123 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error); |
| 1124 | ASSERT_EQ(0, ret); |
| 1125 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap); |
| 1126 | ASSERT_EQ(0, ret); |
| 1127 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->kill); |
| 1128 | ASSERT_EQ(0, ret); |
| 1129 | /* Should work just fine. */ |
| 1130 | res = syscall(__NR_getppid); |
| 1131 | EXPECT_EQ(parent, res); |
| 1132 | } |
| 1133 | |
| 1134 | TEST_F_SIGNAL(precedence, kill_is_highest, SIGSYS) |
| 1135 | { |
| 1136 | pid_t parent, res = 0; |
| 1137 | long ret; |
| 1138 | |
| 1139 | parent = getppid(); |
| 1140 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1141 | ASSERT_EQ(0, ret); |
| 1142 | |
| 1143 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); |
| 1144 | ASSERT_EQ(0, ret); |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 1145 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); |
| 1146 | ASSERT_EQ(0, ret); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1147 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); |
| 1148 | ASSERT_EQ(0, ret); |
| 1149 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error); |
| 1150 | ASSERT_EQ(0, ret); |
| 1151 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap); |
| 1152 | ASSERT_EQ(0, ret); |
| 1153 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->kill); |
| 1154 | ASSERT_EQ(0, ret); |
| 1155 | /* Should work just fine. */ |
| 1156 | res = syscall(__NR_getppid); |
| 1157 | EXPECT_EQ(parent, res); |
| 1158 | /* getpid() should never return. */ |
| 1159 | res = syscall(__NR_getpid); |
| 1160 | EXPECT_EQ(0, res); |
| 1161 | } |
| 1162 | |
| 1163 | TEST_F_SIGNAL(precedence, kill_is_highest_in_any_order, SIGSYS) |
| 1164 | { |
| 1165 | pid_t parent; |
| 1166 | long ret; |
| 1167 | |
| 1168 | parent = getppid(); |
| 1169 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1170 | ASSERT_EQ(0, ret); |
| 1171 | |
| 1172 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); |
| 1173 | ASSERT_EQ(0, ret); |
| 1174 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->kill); |
| 1175 | ASSERT_EQ(0, ret); |
| 1176 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error); |
| 1177 | ASSERT_EQ(0, ret); |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 1178 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); |
| 1179 | ASSERT_EQ(0, ret); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1180 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); |
| 1181 | ASSERT_EQ(0, ret); |
| 1182 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap); |
| 1183 | ASSERT_EQ(0, ret); |
| 1184 | /* Should work just fine. */ |
| 1185 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 1186 | /* getpid() should never return. */ |
| 1187 | EXPECT_EQ(0, syscall(__NR_getpid)); |
| 1188 | } |
| 1189 | |
| 1190 | TEST_F_SIGNAL(precedence, trap_is_second, SIGSYS) |
| 1191 | { |
| 1192 | pid_t parent; |
| 1193 | long ret; |
| 1194 | |
| 1195 | parent = getppid(); |
| 1196 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1197 | ASSERT_EQ(0, ret); |
| 1198 | |
| 1199 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); |
| 1200 | ASSERT_EQ(0, ret); |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 1201 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); |
| 1202 | ASSERT_EQ(0, ret); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1203 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); |
| 1204 | ASSERT_EQ(0, ret); |
| 1205 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error); |
| 1206 | ASSERT_EQ(0, ret); |
| 1207 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap); |
| 1208 | ASSERT_EQ(0, ret); |
| 1209 | /* Should work just fine. */ |
| 1210 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 1211 | /* getpid() should never return. */ |
| 1212 | EXPECT_EQ(0, syscall(__NR_getpid)); |
| 1213 | } |
| 1214 | |
| 1215 | TEST_F_SIGNAL(precedence, trap_is_second_in_any_order, SIGSYS) |
| 1216 | { |
| 1217 | pid_t parent; |
| 1218 | long ret; |
| 1219 | |
| 1220 | parent = getppid(); |
| 1221 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1222 | ASSERT_EQ(0, ret); |
| 1223 | |
| 1224 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); |
| 1225 | ASSERT_EQ(0, ret); |
| 1226 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trap); |
| 1227 | ASSERT_EQ(0, ret); |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 1228 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); |
| 1229 | ASSERT_EQ(0, ret); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1230 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); |
| 1231 | ASSERT_EQ(0, ret); |
| 1232 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error); |
| 1233 | ASSERT_EQ(0, ret); |
| 1234 | /* Should work just fine. */ |
| 1235 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 1236 | /* getpid() should never return. */ |
| 1237 | EXPECT_EQ(0, syscall(__NR_getpid)); |
| 1238 | } |
| 1239 | |
| 1240 | TEST_F(precedence, errno_is_third) |
| 1241 | { |
| 1242 | pid_t parent; |
| 1243 | long ret; |
| 1244 | |
| 1245 | parent = getppid(); |
| 1246 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1247 | ASSERT_EQ(0, ret); |
| 1248 | |
| 1249 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); |
| 1250 | ASSERT_EQ(0, ret); |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 1251 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); |
| 1252 | ASSERT_EQ(0, ret); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1253 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); |
| 1254 | ASSERT_EQ(0, ret); |
| 1255 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error); |
| 1256 | ASSERT_EQ(0, ret); |
| 1257 | /* Should work just fine. */ |
| 1258 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 1259 | EXPECT_EQ(0, syscall(__NR_getpid)); |
| 1260 | } |
| 1261 | |
| 1262 | TEST_F(precedence, errno_is_third_in_any_order) |
| 1263 | { |
| 1264 | pid_t parent; |
| 1265 | long ret; |
| 1266 | |
| 1267 | parent = getppid(); |
| 1268 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1269 | ASSERT_EQ(0, ret); |
| 1270 | |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 1271 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); |
| 1272 | ASSERT_EQ(0, ret); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1273 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->error); |
| 1274 | ASSERT_EQ(0, ret); |
| 1275 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); |
| 1276 | ASSERT_EQ(0, ret); |
| 1277 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); |
| 1278 | ASSERT_EQ(0, ret); |
| 1279 | /* Should work just fine. */ |
| 1280 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 1281 | EXPECT_EQ(0, syscall(__NR_getpid)); |
| 1282 | } |
| 1283 | |
| 1284 | TEST_F(precedence, trace_is_fourth) |
| 1285 | { |
| 1286 | pid_t parent; |
| 1287 | long ret; |
| 1288 | |
| 1289 | parent = getppid(); |
| 1290 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1291 | ASSERT_EQ(0, ret); |
| 1292 | |
| 1293 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); |
| 1294 | ASSERT_EQ(0, ret); |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 1295 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); |
| 1296 | ASSERT_EQ(0, ret); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1297 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); |
| 1298 | ASSERT_EQ(0, ret); |
| 1299 | /* Should work just fine. */ |
| 1300 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 1301 | /* No ptracer */ |
| 1302 | EXPECT_EQ(-1, syscall(__NR_getpid)); |
| 1303 | } |
| 1304 | |
| 1305 | TEST_F(precedence, trace_is_fourth_in_any_order) |
| 1306 | { |
| 1307 | pid_t parent; |
| 1308 | long ret; |
| 1309 | |
| 1310 | parent = getppid(); |
| 1311 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1312 | ASSERT_EQ(0, ret); |
| 1313 | |
| 1314 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->trace); |
| 1315 | ASSERT_EQ(0, ret); |
| 1316 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); |
| 1317 | ASSERT_EQ(0, ret); |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 1318 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); |
| 1319 | ASSERT_EQ(0, ret); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1320 | /* Should work just fine. */ |
| 1321 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 1322 | /* No ptracer */ |
| 1323 | EXPECT_EQ(-1, syscall(__NR_getpid)); |
| 1324 | } |
| 1325 | |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 1326 | TEST_F(precedence, log_is_fifth) |
| 1327 | { |
| 1328 | pid_t mypid, parent; |
| 1329 | long ret; |
| 1330 | |
| 1331 | mypid = getpid(); |
| 1332 | parent = getppid(); |
| 1333 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1334 | ASSERT_EQ(0, ret); |
| 1335 | |
| 1336 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); |
| 1337 | ASSERT_EQ(0, ret); |
| 1338 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); |
| 1339 | ASSERT_EQ(0, ret); |
| 1340 | /* Should work just fine. */ |
| 1341 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 1342 | /* Should also work just fine */ |
| 1343 | EXPECT_EQ(mypid, syscall(__NR_getpid)); |
| 1344 | } |
| 1345 | |
| 1346 | TEST_F(precedence, log_is_fifth_in_any_order) |
| 1347 | { |
| 1348 | pid_t mypid, parent; |
| 1349 | long ret; |
| 1350 | |
| 1351 | mypid = getpid(); |
| 1352 | parent = getppid(); |
| 1353 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1354 | ASSERT_EQ(0, ret); |
| 1355 | |
| 1356 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->log); |
| 1357 | ASSERT_EQ(0, ret); |
| 1358 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->allow); |
| 1359 | ASSERT_EQ(0, ret); |
| 1360 | /* Should work just fine. */ |
| 1361 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 1362 | /* Should also work just fine */ |
| 1363 | EXPECT_EQ(mypid, syscall(__NR_getpid)); |
| 1364 | } |
| 1365 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1366 | #ifndef PTRACE_O_TRACESECCOMP |
| 1367 | #define PTRACE_O_TRACESECCOMP 0x00000080 |
| 1368 | #endif |
| 1369 | |
| 1370 | /* Catch the Ubuntu 12.04 value error. */ |
| 1371 | #if PTRACE_EVENT_SECCOMP != 7 |
| 1372 | #undef PTRACE_EVENT_SECCOMP |
| 1373 | #endif |
| 1374 | |
| 1375 | #ifndef PTRACE_EVENT_SECCOMP |
| 1376 | #define PTRACE_EVENT_SECCOMP 7 |
| 1377 | #endif |
| 1378 | |
| 1379 | #define IS_SECCOMP_EVENT(status) ((status >> 16) == PTRACE_EVENT_SECCOMP) |
| 1380 | bool tracer_running; |
| 1381 | void tracer_stop(int sig) |
| 1382 | { |
| 1383 | tracer_running = false; |
| 1384 | } |
| 1385 | |
| 1386 | typedef void tracer_func_t(struct __test_metadata *_metadata, |
| 1387 | pid_t tracee, int status, void *args); |
| 1388 | |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 1389 | void start_tracer(struct __test_metadata *_metadata, int fd, pid_t tracee, |
| 1390 | tracer_func_t tracer_func, void *args, bool ptrace_syscall) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1391 | { |
| 1392 | int ret = -1; |
| 1393 | struct sigaction action = { |
| 1394 | .sa_handler = tracer_stop, |
| 1395 | }; |
| 1396 | |
| 1397 | /* Allow external shutdown. */ |
| 1398 | tracer_running = true; |
| 1399 | ASSERT_EQ(0, sigaction(SIGUSR1, &action, NULL)); |
| 1400 | |
| 1401 | errno = 0; |
| 1402 | while (ret == -1 && errno != EINVAL) |
| 1403 | ret = ptrace(PTRACE_ATTACH, tracee, NULL, 0); |
| 1404 | ASSERT_EQ(0, ret) { |
| 1405 | kill(tracee, SIGKILL); |
| 1406 | } |
| 1407 | /* Wait for attach stop */ |
| 1408 | wait(NULL); |
| 1409 | |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 1410 | ret = ptrace(PTRACE_SETOPTIONS, tracee, NULL, ptrace_syscall ? |
| 1411 | PTRACE_O_TRACESYSGOOD : |
| 1412 | PTRACE_O_TRACESECCOMP); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1413 | ASSERT_EQ(0, ret) { |
| 1414 | TH_LOG("Failed to set PTRACE_O_TRACESECCOMP"); |
| 1415 | kill(tracee, SIGKILL); |
| 1416 | } |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 1417 | ret = ptrace(ptrace_syscall ? PTRACE_SYSCALL : PTRACE_CONT, |
| 1418 | tracee, NULL, 0); |
| 1419 | ASSERT_EQ(0, ret); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1420 | |
| 1421 | /* Unblock the tracee */ |
| 1422 | ASSERT_EQ(1, write(fd, "A", 1)); |
| 1423 | ASSERT_EQ(0, close(fd)); |
| 1424 | |
| 1425 | /* Run until we're shut down. Must assert to stop execution. */ |
| 1426 | while (tracer_running) { |
| 1427 | int status; |
| 1428 | |
| 1429 | if (wait(&status) != tracee) |
| 1430 | continue; |
| 1431 | if (WIFSIGNALED(status) || WIFEXITED(status)) |
| 1432 | /* Child is dead. Time to go. */ |
| 1433 | return; |
| 1434 | |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 1435 | /* Check if this is a seccomp event. */ |
| 1436 | ASSERT_EQ(!ptrace_syscall, IS_SECCOMP_EVENT(status)); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1437 | |
| 1438 | tracer_func(_metadata, tracee, status, args); |
| 1439 | |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 1440 | ret = ptrace(ptrace_syscall ? PTRACE_SYSCALL : PTRACE_CONT, |
| 1441 | tracee, NULL, 0); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1442 | ASSERT_EQ(0, ret); |
| 1443 | } |
| 1444 | /* Directly report the status of our test harness results. */ |
| 1445 | syscall(__NR_exit, _metadata->passed ? EXIT_SUCCESS : EXIT_FAILURE); |
| 1446 | } |
| 1447 | |
| 1448 | /* Common tracer setup/teardown functions. */ |
| 1449 | void cont_handler(int num) |
| 1450 | { } |
| 1451 | pid_t setup_trace_fixture(struct __test_metadata *_metadata, |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 1452 | tracer_func_t func, void *args, bool ptrace_syscall) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1453 | { |
| 1454 | char sync; |
| 1455 | int pipefd[2]; |
| 1456 | pid_t tracer_pid; |
| 1457 | pid_t tracee = getpid(); |
| 1458 | |
| 1459 | /* Setup a pipe for clean synchronization. */ |
| 1460 | ASSERT_EQ(0, pipe(pipefd)); |
| 1461 | |
| 1462 | /* Fork a child which we'll promote to tracer */ |
| 1463 | tracer_pid = fork(); |
| 1464 | ASSERT_LE(0, tracer_pid); |
| 1465 | signal(SIGALRM, cont_handler); |
| 1466 | if (tracer_pid == 0) { |
| 1467 | close(pipefd[0]); |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 1468 | start_tracer(_metadata, pipefd[1], tracee, func, args, |
| 1469 | ptrace_syscall); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1470 | syscall(__NR_exit, 0); |
| 1471 | } |
| 1472 | close(pipefd[1]); |
| 1473 | prctl(PR_SET_PTRACER, tracer_pid, 0, 0, 0); |
| 1474 | read(pipefd[0], &sync, 1); |
| 1475 | close(pipefd[0]); |
| 1476 | |
| 1477 | return tracer_pid; |
| 1478 | } |
| 1479 | void teardown_trace_fixture(struct __test_metadata *_metadata, |
| 1480 | pid_t tracer) |
| 1481 | { |
| 1482 | if (tracer) { |
| 1483 | int status; |
| 1484 | /* |
| 1485 | * Extract the exit code from the other process and |
| 1486 | * adopt it for ourselves in case its asserts failed. |
| 1487 | */ |
| 1488 | ASSERT_EQ(0, kill(tracer, SIGUSR1)); |
| 1489 | ASSERT_EQ(tracer, waitpid(tracer, &status, 0)); |
| 1490 | if (WEXITSTATUS(status)) |
| 1491 | _metadata->passed = 0; |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | /* "poke" tracer arguments and function. */ |
| 1496 | struct tracer_args_poke_t { |
| 1497 | unsigned long poke_addr; |
| 1498 | }; |
| 1499 | |
| 1500 | void tracer_poke(struct __test_metadata *_metadata, pid_t tracee, int status, |
| 1501 | void *args) |
| 1502 | { |
| 1503 | int ret; |
| 1504 | unsigned long msg; |
| 1505 | struct tracer_args_poke_t *info = (struct tracer_args_poke_t *)args; |
| 1506 | |
| 1507 | ret = ptrace(PTRACE_GETEVENTMSG, tracee, NULL, &msg); |
| 1508 | EXPECT_EQ(0, ret); |
| 1509 | /* If this fails, don't try to recover. */ |
| 1510 | ASSERT_EQ(0x1001, msg) { |
| 1511 | kill(tracee, SIGKILL); |
| 1512 | } |
| 1513 | /* |
| 1514 | * Poke in the message. |
| 1515 | * Registers are not touched to try to keep this relatively arch |
| 1516 | * agnostic. |
| 1517 | */ |
| 1518 | ret = ptrace(PTRACE_POKEDATA, tracee, info->poke_addr, 0x1001); |
| 1519 | EXPECT_EQ(0, ret); |
| 1520 | } |
| 1521 | |
Kees Cook | 1ae81d7 | 2020-03-11 12:21:28 -0700 | [diff] [blame] | 1522 | FIXTURE(TRACE_poke) { |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1523 | struct sock_fprog prog; |
| 1524 | pid_t tracer; |
| 1525 | long poked; |
| 1526 | struct tracer_args_poke_t tracer_args; |
| 1527 | }; |
| 1528 | |
| 1529 | FIXTURE_SETUP(TRACE_poke) |
| 1530 | { |
| 1531 | struct sock_filter filter[] = { |
| 1532 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 1533 | offsetof(struct seccomp_data, nr)), |
| 1534 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1), |
| 1535 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1001), |
| 1536 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 1537 | }; |
| 1538 | |
| 1539 | self->poked = 0; |
| 1540 | memset(&self->prog, 0, sizeof(self->prog)); |
| 1541 | self->prog.filter = malloc(sizeof(filter)); |
| 1542 | ASSERT_NE(NULL, self->prog.filter); |
| 1543 | memcpy(self->prog.filter, filter, sizeof(filter)); |
| 1544 | self->prog.len = (unsigned short)ARRAY_SIZE(filter); |
| 1545 | |
| 1546 | /* Set up tracer args. */ |
| 1547 | self->tracer_args.poke_addr = (unsigned long)&self->poked; |
| 1548 | |
| 1549 | /* Launch tracer. */ |
| 1550 | self->tracer = setup_trace_fixture(_metadata, tracer_poke, |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 1551 | &self->tracer_args, false); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | FIXTURE_TEARDOWN(TRACE_poke) |
| 1555 | { |
| 1556 | teardown_trace_fixture(_metadata, self->tracer); |
| 1557 | if (self->prog.filter) |
| 1558 | free(self->prog.filter); |
| 1559 | } |
| 1560 | |
| 1561 | TEST_F(TRACE_poke, read_has_side_effects) |
| 1562 | { |
| 1563 | ssize_t ret; |
| 1564 | |
| 1565 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1566 | ASSERT_EQ(0, ret); |
| 1567 | |
| 1568 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0); |
| 1569 | ASSERT_EQ(0, ret); |
| 1570 | |
| 1571 | EXPECT_EQ(0, self->poked); |
| 1572 | ret = read(-1, NULL, 0); |
| 1573 | EXPECT_EQ(-1, ret); |
| 1574 | EXPECT_EQ(0x1001, self->poked); |
| 1575 | } |
| 1576 | |
| 1577 | TEST_F(TRACE_poke, getpid_runs_normally) |
| 1578 | { |
| 1579 | long ret; |
| 1580 | |
| 1581 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1582 | ASSERT_EQ(0, ret); |
| 1583 | |
| 1584 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0); |
| 1585 | ASSERT_EQ(0, ret); |
| 1586 | |
| 1587 | EXPECT_EQ(0, self->poked); |
| 1588 | EXPECT_NE(0, syscall(__NR_getpid)); |
| 1589 | EXPECT_EQ(0, self->poked); |
| 1590 | } |
| 1591 | |
| 1592 | #if defined(__x86_64__) |
| 1593 | # define ARCH_REGS struct user_regs_struct |
| 1594 | # define SYSCALL_NUM orig_rax |
| 1595 | # define SYSCALL_RET rax |
| 1596 | #elif defined(__i386__) |
| 1597 | # define ARCH_REGS struct user_regs_struct |
| 1598 | # define SYSCALL_NUM orig_eax |
| 1599 | # define SYSCALL_RET eax |
| 1600 | #elif defined(__arm__) |
| 1601 | # define ARCH_REGS struct pt_regs |
| 1602 | # define SYSCALL_NUM ARM_r7 |
| 1603 | # define SYSCALL_RET ARM_r0 |
| 1604 | #elif defined(__aarch64__) |
| 1605 | # define ARCH_REGS struct user_pt_regs |
| 1606 | # define SYSCALL_NUM regs[8] |
| 1607 | # define SYSCALL_RET regs[0] |
David Abdurachmanov | 5340627 | 2019-10-04 17:12:22 -0700 | [diff] [blame] | 1608 | #elif defined(__riscv) && __riscv_xlen == 64 |
| 1609 | # define ARCH_REGS struct user_regs_struct |
| 1610 | # define SYSCALL_NUM a7 |
| 1611 | # define SYSCALL_RET a0 |
Helge Deller | 64e2a42 | 2016-04-01 22:40:53 +0200 | [diff] [blame] | 1612 | #elif defined(__hppa__) |
| 1613 | # define ARCH_REGS struct user_regs_struct |
| 1614 | # define SYSCALL_NUM gr[20] |
| 1615 | # define SYSCALL_RET gr[28] |
Michael Ellerman | 5d83c2b | 2015-07-23 20:21:11 +1000 | [diff] [blame] | 1616 | #elif defined(__powerpc__) |
| 1617 | # define ARCH_REGS struct pt_regs |
| 1618 | # define SYSCALL_NUM gpr[0] |
| 1619 | # define SYSCALL_RET gpr[3] |
Kees Cook | b623c4d | 2015-08-21 11:22:35 -0700 | [diff] [blame] | 1620 | #elif defined(__s390__) |
| 1621 | # define ARCH_REGS s390_regs |
| 1622 | # define SYSCALL_NUM gprs[2] |
| 1623 | # define SYSCALL_RET gprs[2] |
Matt Redfearn | 0ce105b | 2016-03-29 09:35:29 +0100 | [diff] [blame] | 1624 | #elif defined(__mips__) |
| 1625 | # define ARCH_REGS struct pt_regs |
| 1626 | # define SYSCALL_NUM regs[2] |
| 1627 | # define SYSCALL_SYSCALL_NUM regs[4] |
| 1628 | # define SYSCALL_RET regs[2] |
| 1629 | # define SYSCALL_NUM_RET_SHARE_REG |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1630 | #else |
| 1631 | # error "Do not know how to find your architecture's registers and syscalls" |
| 1632 | #endif |
| 1633 | |
Kees Cook | a33b2d0 | 2016-08-08 17:46:23 -0700 | [diff] [blame] | 1634 | /* When the syscall return can't be changed, stub out the tests for it. */ |
| 1635 | #ifdef SYSCALL_NUM_RET_SHARE_REG |
| 1636 | # define EXPECT_SYSCALL_RETURN(val, action) EXPECT_EQ(-1, action) |
| 1637 | #else |
Kees Cook | ed5f132 | 2019-01-25 10:33:59 -0800 | [diff] [blame] | 1638 | # define EXPECT_SYSCALL_RETURN(val, action) \ |
| 1639 | do { \ |
| 1640 | errno = 0; \ |
| 1641 | if (val < 0) { \ |
| 1642 | EXPECT_EQ(-1, action); \ |
| 1643 | EXPECT_EQ(-(val), errno); \ |
| 1644 | } else { \ |
| 1645 | EXPECT_EQ(val, action); \ |
| 1646 | } \ |
| 1647 | } while (0) |
Kees Cook | a33b2d0 | 2016-08-08 17:46:23 -0700 | [diff] [blame] | 1648 | #endif |
| 1649 | |
Mickaël Salaün | 4a0b880 | 2015-12-29 21:35:45 +0100 | [diff] [blame] | 1650 | /* Use PTRACE_GETREGS and PTRACE_SETREGS when available. This is useful for |
| 1651 | * architectures without HAVE_ARCH_TRACEHOOK (e.g. User-mode Linux). |
| 1652 | */ |
Matt Redfearn | 0ce105b | 2016-03-29 09:35:29 +0100 | [diff] [blame] | 1653 | #if defined(__x86_64__) || defined(__i386__) || defined(__mips__) |
Mickaël Salaün | 4a0b880 | 2015-12-29 21:35:45 +0100 | [diff] [blame] | 1654 | #define HAVE_GETREGS |
| 1655 | #endif |
| 1656 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1657 | /* Architecture-specific syscall fetching routine. */ |
| 1658 | int get_syscall(struct __test_metadata *_metadata, pid_t tracee) |
| 1659 | { |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1660 | ARCH_REGS regs; |
Mickaël Salaün | 4a0b880 | 2015-12-29 21:35:45 +0100 | [diff] [blame] | 1661 | #ifdef HAVE_GETREGS |
| 1662 | EXPECT_EQ(0, ptrace(PTRACE_GETREGS, tracee, 0, ®s)) { |
| 1663 | TH_LOG("PTRACE_GETREGS failed"); |
| 1664 | return -1; |
| 1665 | } |
| 1666 | #else |
| 1667 | struct iovec iov; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1668 | |
| 1669 | iov.iov_base = ®s; |
| 1670 | iov.iov_len = sizeof(regs); |
| 1671 | EXPECT_EQ(0, ptrace(PTRACE_GETREGSET, tracee, NT_PRSTATUS, &iov)) { |
| 1672 | TH_LOG("PTRACE_GETREGSET failed"); |
| 1673 | return -1; |
| 1674 | } |
Mickaël Salaün | 4a0b880 | 2015-12-29 21:35:45 +0100 | [diff] [blame] | 1675 | #endif |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1676 | |
Matt Redfearn | 0ce105b | 2016-03-29 09:35:29 +0100 | [diff] [blame] | 1677 | #if defined(__mips__) |
| 1678 | if (regs.SYSCALL_NUM == __NR_O32_Linux) |
| 1679 | return regs.SYSCALL_SYSCALL_NUM; |
| 1680 | #endif |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1681 | return regs.SYSCALL_NUM; |
| 1682 | } |
| 1683 | |
| 1684 | /* Architecture-specific syscall changing routine. */ |
| 1685 | void change_syscall(struct __test_metadata *_metadata, |
Kees Cook | ed5f132 | 2019-01-25 10:33:59 -0800 | [diff] [blame] | 1686 | pid_t tracee, int syscall, int result) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1687 | { |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1688 | int ret; |
| 1689 | ARCH_REGS regs; |
Mickaël Salaün | 4a0b880 | 2015-12-29 21:35:45 +0100 | [diff] [blame] | 1690 | #ifdef HAVE_GETREGS |
| 1691 | ret = ptrace(PTRACE_GETREGS, tracee, 0, ®s); |
| 1692 | #else |
| 1693 | struct iovec iov; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1694 | iov.iov_base = ®s; |
| 1695 | iov.iov_len = sizeof(regs); |
| 1696 | ret = ptrace(PTRACE_GETREGSET, tracee, NT_PRSTATUS, &iov); |
Mickaël Salaün | 4a0b880 | 2015-12-29 21:35:45 +0100 | [diff] [blame] | 1697 | #endif |
Mickaël Salaün | 34a048c | 2017-06-11 14:32:58 +0200 | [diff] [blame] | 1698 | EXPECT_EQ(0, ret) {} |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1699 | |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 1700 | #if defined(__x86_64__) || defined(__i386__) || defined(__powerpc__) || \ |
David Abdurachmanov | 5340627 | 2019-10-04 17:12:22 -0700 | [diff] [blame] | 1701 | defined(__s390__) || defined(__hppa__) || defined(__riscv) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1702 | { |
| 1703 | regs.SYSCALL_NUM = syscall; |
| 1704 | } |
Matt Redfearn | 0ce105b | 2016-03-29 09:35:29 +0100 | [diff] [blame] | 1705 | #elif defined(__mips__) |
| 1706 | { |
| 1707 | if (regs.SYSCALL_NUM == __NR_O32_Linux) |
| 1708 | regs.SYSCALL_SYSCALL_NUM = syscall; |
| 1709 | else |
| 1710 | regs.SYSCALL_NUM = syscall; |
| 1711 | } |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1712 | |
| 1713 | #elif defined(__arm__) |
| 1714 | # ifndef PTRACE_SET_SYSCALL |
| 1715 | # define PTRACE_SET_SYSCALL 23 |
| 1716 | # endif |
| 1717 | { |
| 1718 | ret = ptrace(PTRACE_SET_SYSCALL, tracee, NULL, syscall); |
| 1719 | EXPECT_EQ(0, ret); |
| 1720 | } |
| 1721 | |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 1722 | #elif defined(__aarch64__) |
| 1723 | # ifndef NT_ARM_SYSTEM_CALL |
| 1724 | # define NT_ARM_SYSTEM_CALL 0x404 |
| 1725 | # endif |
| 1726 | { |
| 1727 | iov.iov_base = &syscall; |
| 1728 | iov.iov_len = sizeof(syscall); |
| 1729 | ret = ptrace(PTRACE_SETREGSET, tracee, NT_ARM_SYSTEM_CALL, |
| 1730 | &iov); |
| 1731 | EXPECT_EQ(0, ret); |
| 1732 | } |
| 1733 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1734 | #else |
| 1735 | ASSERT_EQ(1, 0) { |
| 1736 | TH_LOG("How is the syscall changed on this architecture?"); |
| 1737 | } |
| 1738 | #endif |
| 1739 | |
| 1740 | /* If syscall is skipped, change return value. */ |
| 1741 | if (syscall == -1) |
Matt Redfearn | 0ce105b | 2016-03-29 09:35:29 +0100 | [diff] [blame] | 1742 | #ifdef SYSCALL_NUM_RET_SHARE_REG |
| 1743 | TH_LOG("Can't modify syscall return on this architecture"); |
| 1744 | #else |
Kees Cook | ed5f132 | 2019-01-25 10:33:59 -0800 | [diff] [blame] | 1745 | regs.SYSCALL_RET = result; |
Matt Redfearn | 0ce105b | 2016-03-29 09:35:29 +0100 | [diff] [blame] | 1746 | #endif |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1747 | |
Mickaël Salaün | 4a0b880 | 2015-12-29 21:35:45 +0100 | [diff] [blame] | 1748 | #ifdef HAVE_GETREGS |
| 1749 | ret = ptrace(PTRACE_SETREGS, tracee, 0, ®s); |
| 1750 | #else |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 1751 | iov.iov_base = ®s; |
| 1752 | iov.iov_len = sizeof(regs); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1753 | ret = ptrace(PTRACE_SETREGSET, tracee, NT_PRSTATUS, &iov); |
Mickaël Salaün | 4a0b880 | 2015-12-29 21:35:45 +0100 | [diff] [blame] | 1754 | #endif |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1755 | EXPECT_EQ(0, ret); |
| 1756 | } |
| 1757 | |
| 1758 | void tracer_syscall(struct __test_metadata *_metadata, pid_t tracee, |
| 1759 | int status, void *args) |
| 1760 | { |
| 1761 | int ret; |
| 1762 | unsigned long msg; |
| 1763 | |
| 1764 | /* Make sure we got the right message. */ |
| 1765 | ret = ptrace(PTRACE_GETEVENTMSG, tracee, NULL, &msg); |
| 1766 | EXPECT_EQ(0, ret); |
| 1767 | |
Kees Cook | b623c4d | 2015-08-21 11:22:35 -0700 | [diff] [blame] | 1768 | /* Validate and take action on expected syscalls. */ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1769 | switch (msg) { |
| 1770 | case 0x1002: |
| 1771 | /* change getpid to getppid. */ |
Kees Cook | b623c4d | 2015-08-21 11:22:35 -0700 | [diff] [blame] | 1772 | EXPECT_EQ(__NR_getpid, get_syscall(_metadata, tracee)); |
Kees Cook | ed5f132 | 2019-01-25 10:33:59 -0800 | [diff] [blame] | 1773 | change_syscall(_metadata, tracee, __NR_getppid, 0); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1774 | break; |
| 1775 | case 0x1003: |
Kees Cook | ed5f132 | 2019-01-25 10:33:59 -0800 | [diff] [blame] | 1776 | /* skip gettid with valid return code. */ |
Kees Cook | b623c4d | 2015-08-21 11:22:35 -0700 | [diff] [blame] | 1777 | EXPECT_EQ(__NR_gettid, get_syscall(_metadata, tracee)); |
Kees Cook | ed5f132 | 2019-01-25 10:33:59 -0800 | [diff] [blame] | 1778 | change_syscall(_metadata, tracee, -1, 45000); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1779 | break; |
| 1780 | case 0x1004: |
Kees Cook | ed5f132 | 2019-01-25 10:33:59 -0800 | [diff] [blame] | 1781 | /* skip openat with error. */ |
| 1782 | EXPECT_EQ(__NR_openat, get_syscall(_metadata, tracee)); |
| 1783 | change_syscall(_metadata, tracee, -1, -ESRCH); |
| 1784 | break; |
| 1785 | case 0x1005: |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1786 | /* do nothing (allow getppid) */ |
Kees Cook | b623c4d | 2015-08-21 11:22:35 -0700 | [diff] [blame] | 1787 | EXPECT_EQ(__NR_getppid, get_syscall(_metadata, tracee)); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1788 | break; |
| 1789 | default: |
| 1790 | EXPECT_EQ(0, msg) { |
| 1791 | TH_LOG("Unknown PTRACE_GETEVENTMSG: 0x%lx", msg); |
| 1792 | kill(tracee, SIGKILL); |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | } |
| 1797 | |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 1798 | void tracer_ptrace(struct __test_metadata *_metadata, pid_t tracee, |
| 1799 | int status, void *args) |
| 1800 | { |
| 1801 | int ret, nr; |
| 1802 | unsigned long msg; |
| 1803 | static bool entry; |
| 1804 | |
Elvira Khabirova | 201766a | 2019-07-16 16:29:42 -0700 | [diff] [blame] | 1805 | /* |
| 1806 | * The traditional way to tell PTRACE_SYSCALL entry/exit |
| 1807 | * is by counting. |
| 1808 | */ |
| 1809 | entry = !entry; |
| 1810 | |
| 1811 | /* Make sure we got an appropriate message. */ |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 1812 | ret = ptrace(PTRACE_GETEVENTMSG, tracee, NULL, &msg); |
| 1813 | EXPECT_EQ(0, ret); |
Elvira Khabirova | 201766a | 2019-07-16 16:29:42 -0700 | [diff] [blame] | 1814 | EXPECT_EQ(entry ? PTRACE_EVENTMSG_SYSCALL_ENTRY |
| 1815 | : PTRACE_EVENTMSG_SYSCALL_EXIT, msg); |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 1816 | |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 1817 | if (!entry) |
| 1818 | return; |
| 1819 | |
| 1820 | nr = get_syscall(_metadata, tracee); |
| 1821 | |
| 1822 | if (nr == __NR_getpid) |
Kees Cook | ed5f132 | 2019-01-25 10:33:59 -0800 | [diff] [blame] | 1823 | change_syscall(_metadata, tracee, __NR_getppid, 0); |
| 1824 | if (nr == __NR_gettid) |
| 1825 | change_syscall(_metadata, tracee, -1, 45000); |
Anders Roxell | 912ec31 | 2018-01-05 17:31:18 +0100 | [diff] [blame] | 1826 | if (nr == __NR_openat) |
Kees Cook | ed5f132 | 2019-01-25 10:33:59 -0800 | [diff] [blame] | 1827 | change_syscall(_metadata, tracee, -1, -ESRCH); |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 1828 | } |
| 1829 | |
Kees Cook | 1ae81d7 | 2020-03-11 12:21:28 -0700 | [diff] [blame] | 1830 | FIXTURE(TRACE_syscall) { |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1831 | struct sock_fprog prog; |
| 1832 | pid_t tracer, mytid, mypid, parent; |
| 1833 | }; |
| 1834 | |
| 1835 | FIXTURE_SETUP(TRACE_syscall) |
| 1836 | { |
| 1837 | struct sock_filter filter[] = { |
| 1838 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 1839 | offsetof(struct seccomp_data, nr)), |
| 1840 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 0, 1), |
| 1841 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1002), |
| 1842 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_gettid, 0, 1), |
| 1843 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1003), |
Kees Cook | ed5f132 | 2019-01-25 10:33:59 -0800 | [diff] [blame] | 1844 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_openat, 0, 1), |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1845 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1004), |
Kees Cook | ed5f132 | 2019-01-25 10:33:59 -0800 | [diff] [blame] | 1846 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1), |
| 1847 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE | 0x1005), |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1848 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 1849 | }; |
| 1850 | |
| 1851 | memset(&self->prog, 0, sizeof(self->prog)); |
| 1852 | self->prog.filter = malloc(sizeof(filter)); |
| 1853 | ASSERT_NE(NULL, self->prog.filter); |
| 1854 | memcpy(self->prog.filter, filter, sizeof(filter)); |
| 1855 | self->prog.len = (unsigned short)ARRAY_SIZE(filter); |
| 1856 | |
| 1857 | /* Prepare some testable syscall results. */ |
| 1858 | self->mytid = syscall(__NR_gettid); |
| 1859 | ASSERT_GT(self->mytid, 0); |
| 1860 | ASSERT_NE(self->mytid, 1) { |
| 1861 | TH_LOG("Running this test as init is not supported. :)"); |
| 1862 | } |
| 1863 | |
| 1864 | self->mypid = getpid(); |
| 1865 | ASSERT_GT(self->mypid, 0); |
| 1866 | ASSERT_EQ(self->mytid, self->mypid); |
| 1867 | |
| 1868 | self->parent = getppid(); |
| 1869 | ASSERT_GT(self->parent, 0); |
| 1870 | ASSERT_NE(self->parent, self->mypid); |
| 1871 | |
| 1872 | /* Launch tracer. */ |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 1873 | self->tracer = setup_trace_fixture(_metadata, tracer_syscall, NULL, |
| 1874 | false); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | FIXTURE_TEARDOWN(TRACE_syscall) |
| 1878 | { |
| 1879 | teardown_trace_fixture(_metadata, self->tracer); |
| 1880 | if (self->prog.filter) |
| 1881 | free(self->prog.filter); |
| 1882 | } |
| 1883 | |
Kees Cook | a33b2d0 | 2016-08-08 17:46:23 -0700 | [diff] [blame] | 1884 | TEST_F(TRACE_syscall, ptrace_syscall_redirected) |
| 1885 | { |
| 1886 | /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */ |
| 1887 | teardown_trace_fixture(_metadata, self->tracer); |
| 1888 | self->tracer = setup_trace_fixture(_metadata, tracer_ptrace, NULL, |
| 1889 | true); |
| 1890 | |
| 1891 | /* Tracer will redirect getpid to getppid. */ |
| 1892 | EXPECT_NE(self->mypid, syscall(__NR_getpid)); |
| 1893 | } |
| 1894 | |
Kees Cook | ed5f132 | 2019-01-25 10:33:59 -0800 | [diff] [blame] | 1895 | TEST_F(TRACE_syscall, ptrace_syscall_errno) |
Kees Cook | a33b2d0 | 2016-08-08 17:46:23 -0700 | [diff] [blame] | 1896 | { |
| 1897 | /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */ |
| 1898 | teardown_trace_fixture(_metadata, self->tracer); |
| 1899 | self->tracer = setup_trace_fixture(_metadata, tracer_ptrace, NULL, |
| 1900 | true); |
| 1901 | |
Kees Cook | ed5f132 | 2019-01-25 10:33:59 -0800 | [diff] [blame] | 1902 | /* Tracer should skip the open syscall, resulting in ESRCH. */ |
| 1903 | EXPECT_SYSCALL_RETURN(-ESRCH, syscall(__NR_openat)); |
| 1904 | } |
| 1905 | |
| 1906 | TEST_F(TRACE_syscall, ptrace_syscall_faked) |
| 1907 | { |
| 1908 | /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */ |
| 1909 | teardown_trace_fixture(_metadata, self->tracer); |
| 1910 | self->tracer = setup_trace_fixture(_metadata, tracer_ptrace, NULL, |
| 1911 | true); |
| 1912 | |
| 1913 | /* Tracer should skip the gettid syscall, resulting fake pid. */ |
| 1914 | EXPECT_SYSCALL_RETURN(45000, syscall(__NR_gettid)); |
Kees Cook | a33b2d0 | 2016-08-08 17:46:23 -0700 | [diff] [blame] | 1915 | } |
| 1916 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1917 | TEST_F(TRACE_syscall, syscall_allowed) |
| 1918 | { |
| 1919 | long ret; |
| 1920 | |
| 1921 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1922 | ASSERT_EQ(0, ret); |
| 1923 | |
| 1924 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0); |
| 1925 | ASSERT_EQ(0, ret); |
| 1926 | |
| 1927 | /* getppid works as expected (no changes). */ |
| 1928 | EXPECT_EQ(self->parent, syscall(__NR_getppid)); |
| 1929 | EXPECT_NE(self->mypid, syscall(__NR_getppid)); |
| 1930 | } |
| 1931 | |
| 1932 | TEST_F(TRACE_syscall, syscall_redirected) |
| 1933 | { |
| 1934 | long ret; |
| 1935 | |
| 1936 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1937 | ASSERT_EQ(0, ret); |
| 1938 | |
| 1939 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0); |
| 1940 | ASSERT_EQ(0, ret); |
| 1941 | |
| 1942 | /* getpid has been redirected to getppid as expected. */ |
| 1943 | EXPECT_EQ(self->parent, syscall(__NR_getpid)); |
| 1944 | EXPECT_NE(self->mypid, syscall(__NR_getpid)); |
| 1945 | } |
| 1946 | |
Kees Cook | ed5f132 | 2019-01-25 10:33:59 -0800 | [diff] [blame] | 1947 | TEST_F(TRACE_syscall, syscall_errno) |
| 1948 | { |
| 1949 | long ret; |
| 1950 | |
| 1951 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1952 | ASSERT_EQ(0, ret); |
| 1953 | |
| 1954 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0); |
| 1955 | ASSERT_EQ(0, ret); |
| 1956 | |
| 1957 | /* openat has been skipped and an errno return. */ |
| 1958 | EXPECT_SYSCALL_RETURN(-ESRCH, syscall(__NR_openat)); |
| 1959 | } |
| 1960 | |
| 1961 | TEST_F(TRACE_syscall, syscall_faked) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1962 | { |
| 1963 | long ret; |
| 1964 | |
| 1965 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1966 | ASSERT_EQ(0, ret); |
| 1967 | |
| 1968 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0); |
| 1969 | ASSERT_EQ(0, ret); |
| 1970 | |
| 1971 | /* gettid has been skipped and an altered return value stored. */ |
Kees Cook | ed5f132 | 2019-01-25 10:33:59 -0800 | [diff] [blame] | 1972 | EXPECT_SYSCALL_RETURN(45000, syscall(__NR_gettid)); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 1973 | } |
| 1974 | |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 1975 | TEST_F(TRACE_syscall, skip_after_RET_TRACE) |
| 1976 | { |
| 1977 | struct sock_filter filter[] = { |
| 1978 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 1979 | offsetof(struct seccomp_data, nr)), |
| 1980 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1), |
| 1981 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | EPERM), |
| 1982 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 1983 | }; |
| 1984 | struct sock_fprog prog = { |
| 1985 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 1986 | .filter = filter, |
| 1987 | }; |
| 1988 | long ret; |
| 1989 | |
| 1990 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 1991 | ASSERT_EQ(0, ret); |
| 1992 | |
| 1993 | /* Install fixture filter. */ |
| 1994 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0); |
| 1995 | ASSERT_EQ(0, ret); |
| 1996 | |
| 1997 | /* Install "errno on getppid" filter. */ |
| 1998 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); |
| 1999 | ASSERT_EQ(0, ret); |
| 2000 | |
| 2001 | /* Tracer will redirect getpid to getppid, and we should see EPERM. */ |
Kees Cook | a33b2d0 | 2016-08-08 17:46:23 -0700 | [diff] [blame] | 2002 | errno = 0; |
Kees Cook | 58d0a86 | 2016-05-26 11:47:01 -0700 | [diff] [blame] | 2003 | EXPECT_EQ(-1, syscall(__NR_getpid)); |
| 2004 | EXPECT_EQ(EPERM, errno); |
| 2005 | } |
| 2006 | |
| 2007 | TEST_F_SIGNAL(TRACE_syscall, kill_after_RET_TRACE, SIGSYS) |
| 2008 | { |
| 2009 | struct sock_filter filter[] = { |
| 2010 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 2011 | offsetof(struct seccomp_data, nr)), |
| 2012 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1), |
| 2013 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), |
| 2014 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 2015 | }; |
| 2016 | struct sock_fprog prog = { |
| 2017 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 2018 | .filter = filter, |
| 2019 | }; |
| 2020 | long ret; |
| 2021 | |
| 2022 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 2023 | ASSERT_EQ(0, ret); |
| 2024 | |
| 2025 | /* Install fixture filter. */ |
| 2026 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &self->prog, 0, 0); |
| 2027 | ASSERT_EQ(0, ret); |
| 2028 | |
| 2029 | /* Install "death on getppid" filter. */ |
| 2030 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); |
| 2031 | ASSERT_EQ(0, ret); |
| 2032 | |
| 2033 | /* Tracer will redirect getpid to getppid, and we should die. */ |
| 2034 | EXPECT_NE(self->mypid, syscall(__NR_getpid)); |
| 2035 | } |
| 2036 | |
| 2037 | TEST_F(TRACE_syscall, skip_after_ptrace) |
| 2038 | { |
| 2039 | struct sock_filter filter[] = { |
| 2040 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 2041 | offsetof(struct seccomp_data, nr)), |
| 2042 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1), |
| 2043 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | EPERM), |
| 2044 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 2045 | }; |
| 2046 | struct sock_fprog prog = { |
| 2047 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 2048 | .filter = filter, |
| 2049 | }; |
| 2050 | long ret; |
| 2051 | |
| 2052 | /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */ |
| 2053 | teardown_trace_fixture(_metadata, self->tracer); |
| 2054 | self->tracer = setup_trace_fixture(_metadata, tracer_ptrace, NULL, |
| 2055 | true); |
| 2056 | |
| 2057 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 2058 | ASSERT_EQ(0, ret); |
| 2059 | |
| 2060 | /* Install "errno on getppid" filter. */ |
| 2061 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); |
| 2062 | ASSERT_EQ(0, ret); |
| 2063 | |
| 2064 | /* Tracer will redirect getpid to getppid, and we should see EPERM. */ |
| 2065 | EXPECT_EQ(-1, syscall(__NR_getpid)); |
| 2066 | EXPECT_EQ(EPERM, errno); |
| 2067 | } |
| 2068 | |
| 2069 | TEST_F_SIGNAL(TRACE_syscall, kill_after_ptrace, SIGSYS) |
| 2070 | { |
| 2071 | struct sock_filter filter[] = { |
| 2072 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 2073 | offsetof(struct seccomp_data, nr)), |
| 2074 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getppid, 0, 1), |
| 2075 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), |
| 2076 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 2077 | }; |
| 2078 | struct sock_fprog prog = { |
| 2079 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 2080 | .filter = filter, |
| 2081 | }; |
| 2082 | long ret; |
| 2083 | |
| 2084 | /* Swap SECCOMP_RET_TRACE tracer for PTRACE_SYSCALL tracer. */ |
| 2085 | teardown_trace_fixture(_metadata, self->tracer); |
| 2086 | self->tracer = setup_trace_fixture(_metadata, tracer_ptrace, NULL, |
| 2087 | true); |
| 2088 | |
| 2089 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 2090 | ASSERT_EQ(0, ret); |
| 2091 | |
| 2092 | /* Install "death on getppid" filter. */ |
| 2093 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); |
| 2094 | ASSERT_EQ(0, ret); |
| 2095 | |
| 2096 | /* Tracer will redirect getpid to getppid, and we should die. */ |
| 2097 | EXPECT_NE(self->mypid, syscall(__NR_getpid)); |
| 2098 | } |
| 2099 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2100 | TEST(seccomp_syscall) |
| 2101 | { |
| 2102 | struct sock_filter filter[] = { |
| 2103 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 2104 | }; |
| 2105 | struct sock_fprog prog = { |
| 2106 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 2107 | .filter = filter, |
| 2108 | }; |
| 2109 | long ret; |
| 2110 | |
| 2111 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 2112 | ASSERT_EQ(0, ret) { |
| 2113 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 2114 | } |
| 2115 | |
| 2116 | /* Reject insane operation. */ |
| 2117 | ret = seccomp(-1, 0, &prog); |
Kees Cook | b623c4d | 2015-08-21 11:22:35 -0700 | [diff] [blame] | 2118 | ASSERT_NE(ENOSYS, errno) { |
| 2119 | TH_LOG("Kernel does not support seccomp syscall!"); |
| 2120 | } |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2121 | EXPECT_EQ(EINVAL, errno) { |
| 2122 | TH_LOG("Did not reject crazy op value!"); |
| 2123 | } |
| 2124 | |
| 2125 | /* Reject strict with flags or pointer. */ |
| 2126 | ret = seccomp(SECCOMP_SET_MODE_STRICT, -1, NULL); |
| 2127 | EXPECT_EQ(EINVAL, errno) { |
| 2128 | TH_LOG("Did not reject mode strict with flags!"); |
| 2129 | } |
| 2130 | ret = seccomp(SECCOMP_SET_MODE_STRICT, 0, &prog); |
| 2131 | EXPECT_EQ(EINVAL, errno) { |
| 2132 | TH_LOG("Did not reject mode strict with uargs!"); |
| 2133 | } |
| 2134 | |
| 2135 | /* Reject insane args for filter. */ |
| 2136 | ret = seccomp(SECCOMP_SET_MODE_FILTER, -1, &prog); |
| 2137 | EXPECT_EQ(EINVAL, errno) { |
| 2138 | TH_LOG("Did not reject crazy filter flags!"); |
| 2139 | } |
| 2140 | ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, NULL); |
| 2141 | EXPECT_EQ(EFAULT, errno) { |
| 2142 | TH_LOG("Did not reject NULL filter!"); |
| 2143 | } |
| 2144 | |
| 2145 | ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog); |
| 2146 | EXPECT_EQ(0, errno) { |
| 2147 | TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER: %s", |
| 2148 | strerror(errno)); |
| 2149 | } |
| 2150 | } |
| 2151 | |
| 2152 | TEST(seccomp_syscall_mode_lock) |
| 2153 | { |
| 2154 | struct sock_filter filter[] = { |
| 2155 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 2156 | }; |
| 2157 | struct sock_fprog prog = { |
| 2158 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 2159 | .filter = filter, |
| 2160 | }; |
| 2161 | long ret; |
| 2162 | |
| 2163 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, NULL, 0, 0); |
| 2164 | ASSERT_EQ(0, ret) { |
| 2165 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 2166 | } |
| 2167 | |
| 2168 | ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog); |
Kees Cook | b623c4d | 2015-08-21 11:22:35 -0700 | [diff] [blame] | 2169 | ASSERT_NE(ENOSYS, errno) { |
| 2170 | TH_LOG("Kernel does not support seccomp syscall!"); |
| 2171 | } |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2172 | EXPECT_EQ(0, ret) { |
| 2173 | TH_LOG("Could not install filter!"); |
| 2174 | } |
| 2175 | |
| 2176 | /* Make sure neither entry point will switch to strict. */ |
| 2177 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT, 0, 0, 0); |
| 2178 | EXPECT_EQ(EINVAL, errno) { |
| 2179 | TH_LOG("Switched to mode strict!"); |
| 2180 | } |
| 2181 | |
| 2182 | ret = seccomp(SECCOMP_SET_MODE_STRICT, 0, NULL); |
| 2183 | EXPECT_EQ(EINVAL, errno) { |
| 2184 | TH_LOG("Switched to mode strict!"); |
| 2185 | } |
| 2186 | } |
| 2187 | |
Tyler Hicks | 2b7ea5b | 2017-08-11 04:33:55 +0000 | [diff] [blame] | 2188 | /* |
| 2189 | * Test detection of known and unknown filter flags. Userspace needs to be able |
| 2190 | * to check if a filter flag is supported by the current kernel and a good way |
| 2191 | * of doing that is by attempting to enter filter mode, with the flag bit in |
| 2192 | * question set, and a NULL pointer for the _args_ parameter. EFAULT indicates |
| 2193 | * that the flag is valid and EINVAL indicates that the flag is invalid. |
| 2194 | */ |
| 2195 | TEST(detect_seccomp_filter_flags) |
| 2196 | { |
Tyler Hicks | e66a399 | 2017-08-11 04:33:56 +0000 | [diff] [blame] | 2197 | unsigned int flags[] = { SECCOMP_FILTER_FLAG_TSYNC, |
Kees Cook | 00a02d0 | 2018-05-03 14:56:12 -0700 | [diff] [blame] | 2198 | SECCOMP_FILTER_FLAG_LOG, |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 2199 | SECCOMP_FILTER_FLAG_SPEC_ALLOW, |
Tycho Andersen | 5189149 | 2020-03-04 11:05:17 -0700 | [diff] [blame] | 2200 | SECCOMP_FILTER_FLAG_NEW_LISTENER, |
| 2201 | SECCOMP_FILTER_FLAG_TSYNC_ESRCH }; |
Kees Cook | 4ee0776 | 2019-04-24 09:32:55 -0700 | [diff] [blame] | 2202 | unsigned int exclusive[] = { |
| 2203 | SECCOMP_FILTER_FLAG_TSYNC, |
| 2204 | SECCOMP_FILTER_FLAG_NEW_LISTENER }; |
| 2205 | unsigned int flag, all_flags, exclusive_mask; |
Tyler Hicks | 2b7ea5b | 2017-08-11 04:33:55 +0000 | [diff] [blame] | 2206 | int i; |
| 2207 | long ret; |
| 2208 | |
Kees Cook | 4ee0776 | 2019-04-24 09:32:55 -0700 | [diff] [blame] | 2209 | /* Test detection of individual known-good filter flags */ |
Tyler Hicks | 2b7ea5b | 2017-08-11 04:33:55 +0000 | [diff] [blame] | 2210 | for (i = 0, all_flags = 0; i < ARRAY_SIZE(flags); i++) { |
Kees Cook | 00a02d0 | 2018-05-03 14:56:12 -0700 | [diff] [blame] | 2211 | int bits = 0; |
| 2212 | |
Tyler Hicks | 2b7ea5b | 2017-08-11 04:33:55 +0000 | [diff] [blame] | 2213 | flag = flags[i]; |
Kees Cook | 00a02d0 | 2018-05-03 14:56:12 -0700 | [diff] [blame] | 2214 | /* Make sure the flag is a single bit! */ |
| 2215 | while (flag) { |
| 2216 | if (flag & 0x1) |
| 2217 | bits ++; |
| 2218 | flag >>= 1; |
| 2219 | } |
| 2220 | ASSERT_EQ(1, bits); |
| 2221 | flag = flags[i]; |
| 2222 | |
Tyler Hicks | 2b7ea5b | 2017-08-11 04:33:55 +0000 | [diff] [blame] | 2223 | ret = seccomp(SECCOMP_SET_MODE_FILTER, flag, NULL); |
| 2224 | ASSERT_NE(ENOSYS, errno) { |
| 2225 | TH_LOG("Kernel does not support seccomp syscall!"); |
| 2226 | } |
| 2227 | EXPECT_EQ(-1, ret); |
| 2228 | EXPECT_EQ(EFAULT, errno) { |
| 2229 | TH_LOG("Failed to detect that a known-good filter flag (0x%X) is supported!", |
| 2230 | flag); |
| 2231 | } |
| 2232 | |
| 2233 | all_flags |= flag; |
| 2234 | } |
| 2235 | |
Kees Cook | 4ee0776 | 2019-04-24 09:32:55 -0700 | [diff] [blame] | 2236 | /* |
| 2237 | * Test detection of all known-good filter flags combined. But |
| 2238 | * for the exclusive flags we need to mask them out and try them |
| 2239 | * individually for the "all flags" testing. |
| 2240 | */ |
| 2241 | exclusive_mask = 0; |
| 2242 | for (i = 0; i < ARRAY_SIZE(exclusive); i++) |
| 2243 | exclusive_mask |= exclusive[i]; |
| 2244 | for (i = 0; i < ARRAY_SIZE(exclusive); i++) { |
| 2245 | flag = all_flags & ~exclusive_mask; |
| 2246 | flag |= exclusive[i]; |
| 2247 | |
| 2248 | ret = seccomp(SECCOMP_SET_MODE_FILTER, flag, NULL); |
| 2249 | EXPECT_EQ(-1, ret); |
| 2250 | EXPECT_EQ(EFAULT, errno) { |
| 2251 | TH_LOG("Failed to detect that all known-good filter flags (0x%X) are supported!", |
| 2252 | flag); |
| 2253 | } |
Tyler Hicks | 2b7ea5b | 2017-08-11 04:33:55 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
Kees Cook | 4ee0776 | 2019-04-24 09:32:55 -0700 | [diff] [blame] | 2256 | /* Test detection of an unknown filter flags, without exclusives. */ |
Tyler Hicks | 2b7ea5b | 2017-08-11 04:33:55 +0000 | [diff] [blame] | 2257 | flag = -1; |
Kees Cook | 4ee0776 | 2019-04-24 09:32:55 -0700 | [diff] [blame] | 2258 | flag &= ~exclusive_mask; |
Tyler Hicks | 2b7ea5b | 2017-08-11 04:33:55 +0000 | [diff] [blame] | 2259 | ret = seccomp(SECCOMP_SET_MODE_FILTER, flag, NULL); |
| 2260 | EXPECT_EQ(-1, ret); |
| 2261 | EXPECT_EQ(EINVAL, errno) { |
| 2262 | TH_LOG("Failed to detect that an unknown filter flag (0x%X) is unsupported!", |
| 2263 | flag); |
| 2264 | } |
| 2265 | |
| 2266 | /* |
| 2267 | * Test detection of an unknown filter flag that may simply need to be |
| 2268 | * added to this test |
| 2269 | */ |
| 2270 | flag = flags[ARRAY_SIZE(flags) - 1] << 1; |
| 2271 | ret = seccomp(SECCOMP_SET_MODE_FILTER, flag, NULL); |
| 2272 | EXPECT_EQ(-1, ret); |
| 2273 | EXPECT_EQ(EINVAL, errno) { |
| 2274 | TH_LOG("Failed to detect that an unknown filter flag (0x%X) is unsupported! Does a new flag need to be added to this test?", |
| 2275 | flag); |
| 2276 | } |
| 2277 | } |
| 2278 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2279 | TEST(TSYNC_first) |
| 2280 | { |
| 2281 | struct sock_filter filter[] = { |
| 2282 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 2283 | }; |
| 2284 | struct sock_fprog prog = { |
| 2285 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 2286 | .filter = filter, |
| 2287 | }; |
| 2288 | long ret; |
| 2289 | |
| 2290 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, NULL, 0, 0); |
| 2291 | ASSERT_EQ(0, ret) { |
| 2292 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 2293 | } |
| 2294 | |
Mickaël Salaün | 6c045d0 | 2016-03-29 20:46:07 +0200 | [diff] [blame] | 2295 | ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2296 | &prog); |
Kees Cook | b623c4d | 2015-08-21 11:22:35 -0700 | [diff] [blame] | 2297 | ASSERT_NE(ENOSYS, errno) { |
| 2298 | TH_LOG("Kernel does not support seccomp syscall!"); |
| 2299 | } |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2300 | EXPECT_EQ(0, ret) { |
| 2301 | TH_LOG("Could not install initial filter with TSYNC!"); |
| 2302 | } |
| 2303 | } |
| 2304 | |
| 2305 | #define TSYNC_SIBLINGS 2 |
| 2306 | struct tsync_sibling { |
| 2307 | pthread_t tid; |
| 2308 | pid_t system_tid; |
| 2309 | sem_t *started; |
| 2310 | pthread_cond_t *cond; |
| 2311 | pthread_mutex_t *mutex; |
| 2312 | int diverge; |
| 2313 | int num_waits; |
| 2314 | struct sock_fprog *prog; |
| 2315 | struct __test_metadata *metadata; |
| 2316 | }; |
| 2317 | |
Kees Cook | 93bd70e | 2017-03-20 16:41:35 -0700 | [diff] [blame] | 2318 | /* |
| 2319 | * To avoid joining joined threads (which is not allowed by Bionic), |
| 2320 | * make sure we both successfully join and clear the tid to skip a |
| 2321 | * later join attempt during fixture teardown. Any remaining threads |
| 2322 | * will be directly killed during teardown. |
| 2323 | */ |
| 2324 | #define PTHREAD_JOIN(tid, status) \ |
| 2325 | do { \ |
| 2326 | int _rc = pthread_join(tid, status); \ |
| 2327 | if (_rc) { \ |
| 2328 | TH_LOG("pthread_join of tid %u failed: %d\n", \ |
| 2329 | (unsigned int)tid, _rc); \ |
| 2330 | } else { \ |
| 2331 | tid = 0; \ |
| 2332 | } \ |
| 2333 | } while (0) |
| 2334 | |
Kees Cook | 1ae81d7 | 2020-03-11 12:21:28 -0700 | [diff] [blame] | 2335 | FIXTURE(TSYNC) { |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2336 | struct sock_fprog root_prog, apply_prog; |
| 2337 | struct tsync_sibling sibling[TSYNC_SIBLINGS]; |
| 2338 | sem_t started; |
| 2339 | pthread_cond_t cond; |
| 2340 | pthread_mutex_t mutex; |
| 2341 | int sibling_count; |
| 2342 | }; |
| 2343 | |
| 2344 | FIXTURE_SETUP(TSYNC) |
| 2345 | { |
| 2346 | struct sock_filter root_filter[] = { |
| 2347 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 2348 | }; |
| 2349 | struct sock_filter apply_filter[] = { |
| 2350 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 2351 | offsetof(struct seccomp_data, nr)), |
| 2352 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 0, 1), |
| 2353 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), |
| 2354 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 2355 | }; |
| 2356 | |
| 2357 | memset(&self->root_prog, 0, sizeof(self->root_prog)); |
| 2358 | memset(&self->apply_prog, 0, sizeof(self->apply_prog)); |
| 2359 | memset(&self->sibling, 0, sizeof(self->sibling)); |
| 2360 | self->root_prog.filter = malloc(sizeof(root_filter)); |
| 2361 | ASSERT_NE(NULL, self->root_prog.filter); |
| 2362 | memcpy(self->root_prog.filter, &root_filter, sizeof(root_filter)); |
| 2363 | self->root_prog.len = (unsigned short)ARRAY_SIZE(root_filter); |
| 2364 | |
| 2365 | self->apply_prog.filter = malloc(sizeof(apply_filter)); |
| 2366 | ASSERT_NE(NULL, self->apply_prog.filter); |
| 2367 | memcpy(self->apply_prog.filter, &apply_filter, sizeof(apply_filter)); |
| 2368 | self->apply_prog.len = (unsigned short)ARRAY_SIZE(apply_filter); |
| 2369 | |
| 2370 | self->sibling_count = 0; |
| 2371 | pthread_mutex_init(&self->mutex, NULL); |
| 2372 | pthread_cond_init(&self->cond, NULL); |
| 2373 | sem_init(&self->started, 0, 0); |
| 2374 | self->sibling[0].tid = 0; |
| 2375 | self->sibling[0].cond = &self->cond; |
| 2376 | self->sibling[0].started = &self->started; |
| 2377 | self->sibling[0].mutex = &self->mutex; |
| 2378 | self->sibling[0].diverge = 0; |
| 2379 | self->sibling[0].num_waits = 1; |
| 2380 | self->sibling[0].prog = &self->root_prog; |
| 2381 | self->sibling[0].metadata = _metadata; |
| 2382 | self->sibling[1].tid = 0; |
| 2383 | self->sibling[1].cond = &self->cond; |
| 2384 | self->sibling[1].started = &self->started; |
| 2385 | self->sibling[1].mutex = &self->mutex; |
| 2386 | self->sibling[1].diverge = 0; |
| 2387 | self->sibling[1].prog = &self->root_prog; |
| 2388 | self->sibling[1].num_waits = 1; |
| 2389 | self->sibling[1].metadata = _metadata; |
| 2390 | } |
| 2391 | |
| 2392 | FIXTURE_TEARDOWN(TSYNC) |
| 2393 | { |
| 2394 | int sib = 0; |
| 2395 | |
| 2396 | if (self->root_prog.filter) |
| 2397 | free(self->root_prog.filter); |
| 2398 | if (self->apply_prog.filter) |
| 2399 | free(self->apply_prog.filter); |
| 2400 | |
| 2401 | for ( ; sib < self->sibling_count; ++sib) { |
| 2402 | struct tsync_sibling *s = &self->sibling[sib]; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2403 | |
| 2404 | if (!s->tid) |
| 2405 | continue; |
Kees Cook | 93bd70e | 2017-03-20 16:41:35 -0700 | [diff] [blame] | 2406 | /* |
| 2407 | * If a thread is still running, it may be stuck, so hit |
| 2408 | * it over the head really hard. |
| 2409 | */ |
| 2410 | pthread_kill(s->tid, 9); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2411 | } |
| 2412 | pthread_mutex_destroy(&self->mutex); |
| 2413 | pthread_cond_destroy(&self->cond); |
| 2414 | sem_destroy(&self->started); |
| 2415 | } |
| 2416 | |
| 2417 | void *tsync_sibling(void *data) |
| 2418 | { |
| 2419 | long ret = 0; |
| 2420 | struct tsync_sibling *me = data; |
| 2421 | |
| 2422 | me->system_tid = syscall(__NR_gettid); |
| 2423 | |
| 2424 | pthread_mutex_lock(me->mutex); |
| 2425 | if (me->diverge) { |
| 2426 | /* Just re-apply the root prog to fork the tree */ |
| 2427 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, |
| 2428 | me->prog, 0, 0); |
| 2429 | } |
| 2430 | sem_post(me->started); |
| 2431 | /* Return outside of started so parent notices failures. */ |
| 2432 | if (ret) { |
| 2433 | pthread_mutex_unlock(me->mutex); |
| 2434 | return (void *)SIBLING_EXIT_FAILURE; |
| 2435 | } |
| 2436 | do { |
| 2437 | pthread_cond_wait(me->cond, me->mutex); |
| 2438 | me->num_waits = me->num_waits - 1; |
| 2439 | } while (me->num_waits); |
| 2440 | pthread_mutex_unlock(me->mutex); |
| 2441 | |
| 2442 | ret = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0); |
| 2443 | if (!ret) |
| 2444 | return (void *)SIBLING_EXIT_NEWPRIVS; |
| 2445 | read(0, NULL, 0); |
| 2446 | return (void *)SIBLING_EXIT_UNKILLED; |
| 2447 | } |
| 2448 | |
| 2449 | void tsync_start_sibling(struct tsync_sibling *sibling) |
| 2450 | { |
| 2451 | pthread_create(&sibling->tid, NULL, tsync_sibling, (void *)sibling); |
| 2452 | } |
| 2453 | |
| 2454 | TEST_F(TSYNC, siblings_fail_prctl) |
| 2455 | { |
| 2456 | long ret; |
| 2457 | void *status; |
| 2458 | struct sock_filter filter[] = { |
| 2459 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 2460 | offsetof(struct seccomp_data, nr)), |
| 2461 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_prctl, 0, 1), |
| 2462 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO | EINVAL), |
| 2463 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 2464 | }; |
| 2465 | struct sock_fprog prog = { |
| 2466 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 2467 | .filter = filter, |
| 2468 | }; |
| 2469 | |
| 2470 | ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { |
| 2471 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 2472 | } |
| 2473 | |
| 2474 | /* Check prctl failure detection by requesting sib 0 diverge. */ |
| 2475 | ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog); |
Kees Cook | b623c4d | 2015-08-21 11:22:35 -0700 | [diff] [blame] | 2476 | ASSERT_NE(ENOSYS, errno) { |
| 2477 | TH_LOG("Kernel does not support seccomp syscall!"); |
| 2478 | } |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2479 | ASSERT_EQ(0, ret) { |
| 2480 | TH_LOG("setting filter failed"); |
| 2481 | } |
| 2482 | |
| 2483 | self->sibling[0].diverge = 1; |
| 2484 | tsync_start_sibling(&self->sibling[0]); |
| 2485 | tsync_start_sibling(&self->sibling[1]); |
| 2486 | |
| 2487 | while (self->sibling_count < TSYNC_SIBLINGS) { |
| 2488 | sem_wait(&self->started); |
| 2489 | self->sibling_count++; |
| 2490 | } |
| 2491 | |
| 2492 | /* Signal the threads to clean up*/ |
| 2493 | pthread_mutex_lock(&self->mutex); |
| 2494 | ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { |
| 2495 | TH_LOG("cond broadcast non-zero"); |
| 2496 | } |
| 2497 | pthread_mutex_unlock(&self->mutex); |
| 2498 | |
| 2499 | /* Ensure diverging sibling failed to call prctl. */ |
Kees Cook | 93bd70e | 2017-03-20 16:41:35 -0700 | [diff] [blame] | 2500 | PTHREAD_JOIN(self->sibling[0].tid, &status); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2501 | EXPECT_EQ(SIBLING_EXIT_FAILURE, (long)status); |
Kees Cook | 93bd70e | 2017-03-20 16:41:35 -0700 | [diff] [blame] | 2502 | PTHREAD_JOIN(self->sibling[1].tid, &status); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2503 | EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status); |
| 2504 | } |
| 2505 | |
| 2506 | TEST_F(TSYNC, two_siblings_with_ancestor) |
| 2507 | { |
| 2508 | long ret; |
| 2509 | void *status; |
| 2510 | |
| 2511 | ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { |
| 2512 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 2513 | } |
| 2514 | |
| 2515 | ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &self->root_prog); |
Kees Cook | b623c4d | 2015-08-21 11:22:35 -0700 | [diff] [blame] | 2516 | ASSERT_NE(ENOSYS, errno) { |
| 2517 | TH_LOG("Kernel does not support seccomp syscall!"); |
| 2518 | } |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2519 | ASSERT_EQ(0, ret) { |
| 2520 | TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!"); |
| 2521 | } |
| 2522 | tsync_start_sibling(&self->sibling[0]); |
| 2523 | tsync_start_sibling(&self->sibling[1]); |
| 2524 | |
| 2525 | while (self->sibling_count < TSYNC_SIBLINGS) { |
| 2526 | sem_wait(&self->started); |
| 2527 | self->sibling_count++; |
| 2528 | } |
| 2529 | |
Mickaël Salaün | 6c045d0 | 2016-03-29 20:46:07 +0200 | [diff] [blame] | 2530 | ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2531 | &self->apply_prog); |
| 2532 | ASSERT_EQ(0, ret) { |
| 2533 | TH_LOG("Could install filter on all threads!"); |
| 2534 | } |
| 2535 | /* Tell the siblings to test the policy */ |
| 2536 | pthread_mutex_lock(&self->mutex); |
| 2537 | ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { |
| 2538 | TH_LOG("cond broadcast non-zero"); |
| 2539 | } |
| 2540 | pthread_mutex_unlock(&self->mutex); |
| 2541 | /* Ensure they are both killed and don't exit cleanly. */ |
Kees Cook | 93bd70e | 2017-03-20 16:41:35 -0700 | [diff] [blame] | 2542 | PTHREAD_JOIN(self->sibling[0].tid, &status); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2543 | EXPECT_EQ(0x0, (long)status); |
Kees Cook | 93bd70e | 2017-03-20 16:41:35 -0700 | [diff] [blame] | 2544 | PTHREAD_JOIN(self->sibling[1].tid, &status); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2545 | EXPECT_EQ(0x0, (long)status); |
| 2546 | } |
| 2547 | |
| 2548 | TEST_F(TSYNC, two_sibling_want_nnp) |
| 2549 | { |
| 2550 | void *status; |
| 2551 | |
| 2552 | /* start siblings before any prctl() operations */ |
| 2553 | tsync_start_sibling(&self->sibling[0]); |
| 2554 | tsync_start_sibling(&self->sibling[1]); |
| 2555 | while (self->sibling_count < TSYNC_SIBLINGS) { |
| 2556 | sem_wait(&self->started); |
| 2557 | self->sibling_count++; |
| 2558 | } |
| 2559 | |
| 2560 | /* Tell the siblings to test no policy */ |
| 2561 | pthread_mutex_lock(&self->mutex); |
| 2562 | ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { |
| 2563 | TH_LOG("cond broadcast non-zero"); |
| 2564 | } |
| 2565 | pthread_mutex_unlock(&self->mutex); |
| 2566 | |
| 2567 | /* Ensure they are both upset about lacking nnp. */ |
Kees Cook | 93bd70e | 2017-03-20 16:41:35 -0700 | [diff] [blame] | 2568 | PTHREAD_JOIN(self->sibling[0].tid, &status); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2569 | EXPECT_EQ(SIBLING_EXIT_NEWPRIVS, (long)status); |
Kees Cook | 93bd70e | 2017-03-20 16:41:35 -0700 | [diff] [blame] | 2570 | PTHREAD_JOIN(self->sibling[1].tid, &status); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2571 | EXPECT_EQ(SIBLING_EXIT_NEWPRIVS, (long)status); |
| 2572 | } |
| 2573 | |
| 2574 | TEST_F(TSYNC, two_siblings_with_no_filter) |
| 2575 | { |
| 2576 | long ret; |
| 2577 | void *status; |
| 2578 | |
| 2579 | /* start siblings before any prctl() operations */ |
| 2580 | tsync_start_sibling(&self->sibling[0]); |
| 2581 | tsync_start_sibling(&self->sibling[1]); |
| 2582 | while (self->sibling_count < TSYNC_SIBLINGS) { |
| 2583 | sem_wait(&self->started); |
| 2584 | self->sibling_count++; |
| 2585 | } |
| 2586 | |
| 2587 | ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { |
| 2588 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 2589 | } |
| 2590 | |
Mickaël Salaün | 6c045d0 | 2016-03-29 20:46:07 +0200 | [diff] [blame] | 2591 | ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2592 | &self->apply_prog); |
Kees Cook | b623c4d | 2015-08-21 11:22:35 -0700 | [diff] [blame] | 2593 | ASSERT_NE(ENOSYS, errno) { |
| 2594 | TH_LOG("Kernel does not support seccomp syscall!"); |
| 2595 | } |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2596 | ASSERT_EQ(0, ret) { |
| 2597 | TH_LOG("Could install filter on all threads!"); |
| 2598 | } |
| 2599 | |
| 2600 | /* Tell the siblings to test the policy */ |
| 2601 | pthread_mutex_lock(&self->mutex); |
| 2602 | ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { |
| 2603 | TH_LOG("cond broadcast non-zero"); |
| 2604 | } |
| 2605 | pthread_mutex_unlock(&self->mutex); |
| 2606 | |
| 2607 | /* Ensure they are both killed and don't exit cleanly. */ |
Kees Cook | 93bd70e | 2017-03-20 16:41:35 -0700 | [diff] [blame] | 2608 | PTHREAD_JOIN(self->sibling[0].tid, &status); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2609 | EXPECT_EQ(0x0, (long)status); |
Kees Cook | 93bd70e | 2017-03-20 16:41:35 -0700 | [diff] [blame] | 2610 | PTHREAD_JOIN(self->sibling[1].tid, &status); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2611 | EXPECT_EQ(0x0, (long)status); |
| 2612 | } |
| 2613 | |
| 2614 | TEST_F(TSYNC, two_siblings_with_one_divergence) |
| 2615 | { |
| 2616 | long ret; |
| 2617 | void *status; |
| 2618 | |
| 2619 | ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { |
| 2620 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 2621 | } |
| 2622 | |
| 2623 | ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &self->root_prog); |
Kees Cook | b623c4d | 2015-08-21 11:22:35 -0700 | [diff] [blame] | 2624 | ASSERT_NE(ENOSYS, errno) { |
| 2625 | TH_LOG("Kernel does not support seccomp syscall!"); |
| 2626 | } |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2627 | ASSERT_EQ(0, ret) { |
| 2628 | TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!"); |
| 2629 | } |
| 2630 | self->sibling[0].diverge = 1; |
| 2631 | tsync_start_sibling(&self->sibling[0]); |
| 2632 | tsync_start_sibling(&self->sibling[1]); |
| 2633 | |
| 2634 | while (self->sibling_count < TSYNC_SIBLINGS) { |
| 2635 | sem_wait(&self->started); |
| 2636 | self->sibling_count++; |
| 2637 | } |
| 2638 | |
Mickaël Salaün | 6c045d0 | 2016-03-29 20:46:07 +0200 | [diff] [blame] | 2639 | ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2640 | &self->apply_prog); |
| 2641 | ASSERT_EQ(self->sibling[0].system_tid, ret) { |
| 2642 | TH_LOG("Did not fail on diverged sibling."); |
| 2643 | } |
| 2644 | |
| 2645 | /* Wake the threads */ |
| 2646 | pthread_mutex_lock(&self->mutex); |
| 2647 | ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { |
| 2648 | TH_LOG("cond broadcast non-zero"); |
| 2649 | } |
| 2650 | pthread_mutex_unlock(&self->mutex); |
| 2651 | |
| 2652 | /* Ensure they are both unkilled. */ |
Kees Cook | 93bd70e | 2017-03-20 16:41:35 -0700 | [diff] [blame] | 2653 | PTHREAD_JOIN(self->sibling[0].tid, &status); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2654 | EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status); |
Kees Cook | 93bd70e | 2017-03-20 16:41:35 -0700 | [diff] [blame] | 2655 | PTHREAD_JOIN(self->sibling[1].tid, &status); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2656 | EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status); |
| 2657 | } |
| 2658 | |
Tycho Andersen | 5189149 | 2020-03-04 11:05:17 -0700 | [diff] [blame] | 2659 | TEST_F(TSYNC, two_siblings_with_one_divergence_no_tid_in_err) |
| 2660 | { |
| 2661 | long ret, flags; |
| 2662 | void *status; |
| 2663 | |
| 2664 | ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { |
| 2665 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 2666 | } |
| 2667 | |
| 2668 | ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &self->root_prog); |
| 2669 | ASSERT_NE(ENOSYS, errno) { |
| 2670 | TH_LOG("Kernel does not support seccomp syscall!"); |
| 2671 | } |
| 2672 | ASSERT_EQ(0, ret) { |
| 2673 | TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!"); |
| 2674 | } |
| 2675 | self->sibling[0].diverge = 1; |
| 2676 | tsync_start_sibling(&self->sibling[0]); |
| 2677 | tsync_start_sibling(&self->sibling[1]); |
| 2678 | |
| 2679 | while (self->sibling_count < TSYNC_SIBLINGS) { |
| 2680 | sem_wait(&self->started); |
| 2681 | self->sibling_count++; |
| 2682 | } |
| 2683 | |
| 2684 | flags = SECCOMP_FILTER_FLAG_TSYNC | \ |
| 2685 | SECCOMP_FILTER_FLAG_TSYNC_ESRCH; |
| 2686 | ret = seccomp(SECCOMP_SET_MODE_FILTER, flags, &self->apply_prog); |
| 2687 | ASSERT_EQ(ESRCH, errno) { |
| 2688 | TH_LOG("Did not return ESRCH for diverged sibling."); |
| 2689 | } |
| 2690 | ASSERT_EQ(-1, ret) { |
| 2691 | TH_LOG("Did not fail on diverged sibling."); |
| 2692 | } |
| 2693 | |
| 2694 | /* Wake the threads */ |
| 2695 | pthread_mutex_lock(&self->mutex); |
| 2696 | ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { |
| 2697 | TH_LOG("cond broadcast non-zero"); |
| 2698 | } |
| 2699 | pthread_mutex_unlock(&self->mutex); |
| 2700 | |
| 2701 | /* Ensure they are both unkilled. */ |
| 2702 | PTHREAD_JOIN(self->sibling[0].tid, &status); |
| 2703 | EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status); |
| 2704 | PTHREAD_JOIN(self->sibling[1].tid, &status); |
| 2705 | EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status); |
| 2706 | } |
| 2707 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2708 | TEST_F(TSYNC, two_siblings_not_under_filter) |
| 2709 | { |
| 2710 | long ret, sib; |
| 2711 | void *status; |
Kees Cook | ed492c2 | 2019-01-27 01:43:57 -0800 | [diff] [blame] | 2712 | struct timespec delay = { .tv_nsec = 100000000 }; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2713 | |
| 2714 | ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { |
| 2715 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 2716 | } |
| 2717 | |
| 2718 | /* |
| 2719 | * Sibling 0 will have its own seccomp policy |
| 2720 | * and Sibling 1 will not be under seccomp at |
| 2721 | * all. Sibling 1 will enter seccomp and 0 |
| 2722 | * will cause failure. |
| 2723 | */ |
| 2724 | self->sibling[0].diverge = 1; |
| 2725 | tsync_start_sibling(&self->sibling[0]); |
| 2726 | tsync_start_sibling(&self->sibling[1]); |
| 2727 | |
| 2728 | while (self->sibling_count < TSYNC_SIBLINGS) { |
| 2729 | sem_wait(&self->started); |
| 2730 | self->sibling_count++; |
| 2731 | } |
| 2732 | |
| 2733 | ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &self->root_prog); |
Kees Cook | b623c4d | 2015-08-21 11:22:35 -0700 | [diff] [blame] | 2734 | ASSERT_NE(ENOSYS, errno) { |
| 2735 | TH_LOG("Kernel does not support seccomp syscall!"); |
| 2736 | } |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2737 | ASSERT_EQ(0, ret) { |
| 2738 | TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!"); |
| 2739 | } |
| 2740 | |
Mickaël Salaün | 6c045d0 | 2016-03-29 20:46:07 +0200 | [diff] [blame] | 2741 | ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2742 | &self->apply_prog); |
| 2743 | ASSERT_EQ(ret, self->sibling[0].system_tid) { |
| 2744 | TH_LOG("Did not fail on diverged sibling."); |
| 2745 | } |
| 2746 | sib = 1; |
| 2747 | if (ret == self->sibling[0].system_tid) |
| 2748 | sib = 0; |
| 2749 | |
| 2750 | pthread_mutex_lock(&self->mutex); |
| 2751 | |
| 2752 | /* Increment the other siblings num_waits so we can clean up |
| 2753 | * the one we just saw. |
| 2754 | */ |
| 2755 | self->sibling[!sib].num_waits += 1; |
| 2756 | |
| 2757 | /* Signal the thread to clean up*/ |
| 2758 | ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { |
| 2759 | TH_LOG("cond broadcast non-zero"); |
| 2760 | } |
| 2761 | pthread_mutex_unlock(&self->mutex); |
Kees Cook | 93bd70e | 2017-03-20 16:41:35 -0700 | [diff] [blame] | 2762 | PTHREAD_JOIN(self->sibling[sib].tid, &status); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2763 | EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status); |
| 2764 | /* Poll for actual task death. pthread_join doesn't guarantee it. */ |
| 2765 | while (!kill(self->sibling[sib].system_tid, 0)) |
Kees Cook | ed492c2 | 2019-01-27 01:43:57 -0800 | [diff] [blame] | 2766 | nanosleep(&delay, NULL); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2767 | /* Switch to the remaining sibling */ |
| 2768 | sib = !sib; |
| 2769 | |
Mickaël Salaün | 6c045d0 | 2016-03-29 20:46:07 +0200 | [diff] [blame] | 2770 | ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2771 | &self->apply_prog); |
| 2772 | ASSERT_EQ(0, ret) { |
| 2773 | TH_LOG("Expected the remaining sibling to sync"); |
| 2774 | }; |
| 2775 | |
| 2776 | pthread_mutex_lock(&self->mutex); |
| 2777 | |
| 2778 | /* If remaining sibling didn't have a chance to wake up during |
| 2779 | * the first broadcast, manually reduce the num_waits now. |
| 2780 | */ |
| 2781 | if (self->sibling[sib].num_waits > 1) |
| 2782 | self->sibling[sib].num_waits = 1; |
| 2783 | ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) { |
| 2784 | TH_LOG("cond broadcast non-zero"); |
| 2785 | } |
| 2786 | pthread_mutex_unlock(&self->mutex); |
Kees Cook | 93bd70e | 2017-03-20 16:41:35 -0700 | [diff] [blame] | 2787 | PTHREAD_JOIN(self->sibling[sib].tid, &status); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2788 | EXPECT_EQ(0, (long)status); |
| 2789 | /* Poll for actual task death. pthread_join doesn't guarantee it. */ |
| 2790 | while (!kill(self->sibling[sib].system_tid, 0)) |
Kees Cook | ed492c2 | 2019-01-27 01:43:57 -0800 | [diff] [blame] | 2791 | nanosleep(&delay, NULL); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2792 | |
Mickaël Salaün | 6c045d0 | 2016-03-29 20:46:07 +0200 | [diff] [blame] | 2793 | ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2794 | &self->apply_prog); |
| 2795 | ASSERT_EQ(0, ret); /* just us chickens */ |
| 2796 | } |
| 2797 | |
| 2798 | /* Make sure restarted syscalls are seen directly as "restart_syscall". */ |
| 2799 | TEST(syscall_restart) |
| 2800 | { |
| 2801 | long ret; |
| 2802 | unsigned long msg; |
| 2803 | pid_t child_pid; |
| 2804 | int pipefd[2]; |
| 2805 | int status; |
| 2806 | siginfo_t info = { }; |
| 2807 | struct sock_filter filter[] = { |
| 2808 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 2809 | offsetof(struct seccomp_data, nr)), |
| 2810 | |
| 2811 | #ifdef __NR_sigreturn |
Thadeu Lima de Souza Cascardo | d42b8db | 2020-04-08 20:57:53 -0300 | [diff] [blame] | 2812 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_sigreturn, 7, 0), |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2813 | #endif |
Thadeu Lima de Souza Cascardo | d42b8db | 2020-04-08 20:57:53 -0300 | [diff] [blame] | 2814 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_read, 6, 0), |
| 2815 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_exit, 5, 0), |
| 2816 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_rt_sigreturn, 4, 0), |
| 2817 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_nanosleep, 5, 0), |
| 2818 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_clock_nanosleep, 4, 0), |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2819 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_restart_syscall, 4, 0), |
| 2820 | |
| 2821 | /* Allow __NR_write for easy logging. */ |
| 2822 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_write, 0, 1), |
| 2823 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 2824 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 2825 | /* The nanosleep jump target. */ |
| 2826 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE|0x100), |
| 2827 | /* The restart_syscall jump target. */ |
| 2828 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE|0x200), |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2829 | }; |
| 2830 | struct sock_fprog prog = { |
| 2831 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 2832 | .filter = filter, |
| 2833 | }; |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 2834 | #if defined(__arm__) |
| 2835 | struct utsname utsbuf; |
| 2836 | #endif |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2837 | |
| 2838 | ASSERT_EQ(0, pipe(pipefd)); |
| 2839 | |
| 2840 | child_pid = fork(); |
| 2841 | ASSERT_LE(0, child_pid); |
| 2842 | if (child_pid == 0) { |
| 2843 | /* Child uses EXPECT not ASSERT to deliver status correctly. */ |
| 2844 | char buf = ' '; |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 2845 | struct timespec timeout = { }; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2846 | |
| 2847 | /* Attach parent as tracer and stop. */ |
| 2848 | EXPECT_EQ(0, ptrace(PTRACE_TRACEME)); |
| 2849 | EXPECT_EQ(0, raise(SIGSTOP)); |
| 2850 | |
| 2851 | EXPECT_EQ(0, close(pipefd[1])); |
| 2852 | |
| 2853 | EXPECT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { |
| 2854 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 2855 | } |
| 2856 | |
| 2857 | ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog, 0, 0); |
| 2858 | EXPECT_EQ(0, ret) { |
| 2859 | TH_LOG("Failed to install filter!"); |
| 2860 | } |
| 2861 | |
| 2862 | EXPECT_EQ(1, read(pipefd[0], &buf, 1)) { |
| 2863 | TH_LOG("Failed to read() sync from parent"); |
| 2864 | } |
| 2865 | EXPECT_EQ('.', buf) { |
| 2866 | TH_LOG("Failed to get sync data from read()"); |
| 2867 | } |
| 2868 | |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 2869 | /* Start nanosleep to be interrupted. */ |
| 2870 | timeout.tv_sec = 1; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2871 | errno = 0; |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 2872 | EXPECT_EQ(0, nanosleep(&timeout, NULL)) { |
| 2873 | TH_LOG("Call to nanosleep() failed (errno %d)", errno); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2874 | } |
| 2875 | |
| 2876 | /* Read final sync from parent. */ |
| 2877 | EXPECT_EQ(1, read(pipefd[0], &buf, 1)) { |
| 2878 | TH_LOG("Failed final read() from parent"); |
| 2879 | } |
| 2880 | EXPECT_EQ('!', buf) { |
| 2881 | TH_LOG("Failed to get final data from read()"); |
| 2882 | } |
| 2883 | |
| 2884 | /* Directly report the status of our test harness results. */ |
| 2885 | syscall(__NR_exit, _metadata->passed ? EXIT_SUCCESS |
| 2886 | : EXIT_FAILURE); |
| 2887 | } |
| 2888 | EXPECT_EQ(0, close(pipefd[0])); |
| 2889 | |
| 2890 | /* Attach to child, setup options, and release. */ |
| 2891 | ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); |
| 2892 | ASSERT_EQ(true, WIFSTOPPED(status)); |
| 2893 | ASSERT_EQ(0, ptrace(PTRACE_SETOPTIONS, child_pid, NULL, |
| 2894 | PTRACE_O_TRACESECCOMP)); |
| 2895 | ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0)); |
| 2896 | ASSERT_EQ(1, write(pipefd[1], ".", 1)); |
| 2897 | |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 2898 | /* Wait for nanosleep() to start. */ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2899 | ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); |
| 2900 | ASSERT_EQ(true, WIFSTOPPED(status)); |
| 2901 | ASSERT_EQ(SIGTRAP, WSTOPSIG(status)); |
| 2902 | ASSERT_EQ(PTRACE_EVENT_SECCOMP, (status >> 16)); |
| 2903 | ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG, child_pid, NULL, &msg)); |
| 2904 | ASSERT_EQ(0x100, msg); |
Thadeu Lima de Souza Cascardo | d42b8db | 2020-04-08 20:57:53 -0300 | [diff] [blame] | 2905 | ret = get_syscall(_metadata, child_pid); |
| 2906 | EXPECT_TRUE(ret == __NR_nanosleep || ret == __NR_clock_nanosleep); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2907 | |
| 2908 | /* Might as well check siginfo for sanity while we're here. */ |
| 2909 | ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child_pid, NULL, &info)); |
| 2910 | ASSERT_EQ(SIGTRAP, info.si_signo); |
| 2911 | ASSERT_EQ(SIGTRAP | (PTRACE_EVENT_SECCOMP << 8), info.si_code); |
| 2912 | EXPECT_EQ(0, info.si_errno); |
| 2913 | EXPECT_EQ(getuid(), info.si_uid); |
| 2914 | /* Verify signal delivery came from child (seccomp-triggered). */ |
| 2915 | EXPECT_EQ(child_pid, info.si_pid); |
| 2916 | |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 2917 | /* Interrupt nanosleep with SIGSTOP (which we'll need to handle). */ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2918 | ASSERT_EQ(0, kill(child_pid, SIGSTOP)); |
| 2919 | ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0)); |
| 2920 | ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); |
| 2921 | ASSERT_EQ(true, WIFSTOPPED(status)); |
| 2922 | ASSERT_EQ(SIGSTOP, WSTOPSIG(status)); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2923 | ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child_pid, NULL, &info)); |
Kees Cook | 2bd61ab | 2018-12-06 15:50:38 -0800 | [diff] [blame] | 2924 | /* |
| 2925 | * There is no siginfo on SIGSTOP any more, so we can't verify |
| 2926 | * signal delivery came from parent now (getpid() == info.si_pid). |
| 2927 | * https://lkml.kernel.org/r/CAGXu5jJaZAOzP1qFz66tYrtbuywqb+UN2SOA1VLHpCCOiYvYeg@mail.gmail.com |
| 2928 | * At least verify the SIGSTOP via PTRACE_GETSIGINFO. |
| 2929 | */ |
| 2930 | EXPECT_EQ(SIGSTOP, info.si_signo); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2931 | |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 2932 | /* Restart nanosleep with SIGCONT, which triggers restart_syscall. */ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2933 | ASSERT_EQ(0, kill(child_pid, SIGCONT)); |
| 2934 | ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0)); |
| 2935 | ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); |
| 2936 | ASSERT_EQ(true, WIFSTOPPED(status)); |
| 2937 | ASSERT_EQ(SIGCONT, WSTOPSIG(status)); |
| 2938 | ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0)); |
| 2939 | |
| 2940 | /* Wait for restart_syscall() to start. */ |
| 2941 | ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); |
| 2942 | ASSERT_EQ(true, WIFSTOPPED(status)); |
| 2943 | ASSERT_EQ(SIGTRAP, WSTOPSIG(status)); |
| 2944 | ASSERT_EQ(PTRACE_EVENT_SECCOMP, (status >> 16)); |
| 2945 | ASSERT_EQ(0, ptrace(PTRACE_GETEVENTMSG, child_pid, NULL, &msg)); |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 2946 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2947 | ASSERT_EQ(0x200, msg); |
| 2948 | ret = get_syscall(_metadata, child_pid); |
| 2949 | #if defined(__arm__) |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 2950 | /* |
| 2951 | * FIXME: |
| 2952 | * - native ARM registers do NOT expose true syscall. |
| 2953 | * - compat ARM registers on ARM64 DO expose true syscall. |
| 2954 | */ |
| 2955 | ASSERT_EQ(0, uname(&utsbuf)); |
| 2956 | if (strncmp(utsbuf.machine, "arm", 3) == 0) { |
| 2957 | EXPECT_EQ(__NR_nanosleep, ret); |
| 2958 | } else |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2959 | #endif |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 2960 | { |
| 2961 | EXPECT_EQ(__NR_restart_syscall, ret); |
| 2962 | } |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2963 | |
Kees Cook | 256d0af | 2015-10-06 12:30:25 -0700 | [diff] [blame] | 2964 | /* Write again to end test. */ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2965 | ASSERT_EQ(0, ptrace(PTRACE_CONT, child_pid, NULL, 0)); |
| 2966 | ASSERT_EQ(1, write(pipefd[1], "!", 1)); |
| 2967 | EXPECT_EQ(0, close(pipefd[1])); |
| 2968 | |
| 2969 | ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); |
| 2970 | if (WIFSIGNALED(status) || WEXITSTATUS(status)) |
| 2971 | _metadata->passed = 0; |
| 2972 | } |
| 2973 | |
Tyler Hicks | e66a399 | 2017-08-11 04:33:56 +0000 | [diff] [blame] | 2974 | TEST_SIGNAL(filter_flag_log, SIGSYS) |
| 2975 | { |
| 2976 | struct sock_filter allow_filter[] = { |
| 2977 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 2978 | }; |
| 2979 | struct sock_filter kill_filter[] = { |
| 2980 | BPF_STMT(BPF_LD|BPF_W|BPF_ABS, |
| 2981 | offsetof(struct seccomp_data, nr)), |
| 2982 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, __NR_getpid, 0, 1), |
| 2983 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL), |
| 2984 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 2985 | }; |
| 2986 | struct sock_fprog allow_prog = { |
| 2987 | .len = (unsigned short)ARRAY_SIZE(allow_filter), |
| 2988 | .filter = allow_filter, |
| 2989 | }; |
| 2990 | struct sock_fprog kill_prog = { |
| 2991 | .len = (unsigned short)ARRAY_SIZE(kill_filter), |
| 2992 | .filter = kill_filter, |
| 2993 | }; |
| 2994 | long ret; |
| 2995 | pid_t parent = getppid(); |
| 2996 | |
| 2997 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 2998 | ASSERT_EQ(0, ret); |
| 2999 | |
| 3000 | /* Verify that the FILTER_FLAG_LOG flag isn't accepted in strict mode */ |
| 3001 | ret = seccomp(SECCOMP_SET_MODE_STRICT, SECCOMP_FILTER_FLAG_LOG, |
| 3002 | &allow_prog); |
| 3003 | ASSERT_NE(ENOSYS, errno) { |
| 3004 | TH_LOG("Kernel does not support seccomp syscall!"); |
| 3005 | } |
| 3006 | EXPECT_NE(0, ret) { |
| 3007 | TH_LOG("Kernel accepted FILTER_FLAG_LOG flag in strict mode!"); |
| 3008 | } |
| 3009 | EXPECT_EQ(EINVAL, errno) { |
| 3010 | TH_LOG("Kernel returned unexpected errno for FILTER_FLAG_LOG flag in strict mode!"); |
| 3011 | } |
| 3012 | |
| 3013 | /* Verify that a simple, permissive filter can be added with no flags */ |
| 3014 | ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &allow_prog); |
| 3015 | EXPECT_EQ(0, ret); |
| 3016 | |
| 3017 | /* See if the same filter can be added with the FILTER_FLAG_LOG flag */ |
| 3018 | ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_LOG, |
| 3019 | &allow_prog); |
| 3020 | ASSERT_NE(EINVAL, errno) { |
| 3021 | TH_LOG("Kernel does not support the FILTER_FLAG_LOG flag!"); |
| 3022 | } |
| 3023 | EXPECT_EQ(0, ret); |
| 3024 | |
| 3025 | /* Ensure that the kill filter works with the FILTER_FLAG_LOG flag */ |
| 3026 | ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_LOG, |
| 3027 | &kill_prog); |
| 3028 | EXPECT_EQ(0, ret); |
| 3029 | |
| 3030 | EXPECT_EQ(parent, syscall(__NR_getppid)); |
| 3031 | /* getpid() should never return. */ |
| 3032 | EXPECT_EQ(0, syscall(__NR_getpid)); |
| 3033 | } |
| 3034 | |
Tyler Hicks | d612b1f | 2017-08-11 04:33:53 +0000 | [diff] [blame] | 3035 | TEST(get_action_avail) |
| 3036 | { |
Kees Cook | fd76875 | 2017-08-11 12:53:18 -0700 | [diff] [blame] | 3037 | __u32 actions[] = { SECCOMP_RET_KILL_THREAD, SECCOMP_RET_TRAP, |
Tyler Hicks | d612b1f | 2017-08-11 04:33:53 +0000 | [diff] [blame] | 3038 | SECCOMP_RET_ERRNO, SECCOMP_RET_TRACE, |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 3039 | SECCOMP_RET_LOG, SECCOMP_RET_ALLOW }; |
Tyler Hicks | d612b1f | 2017-08-11 04:33:53 +0000 | [diff] [blame] | 3040 | __u32 unknown_action = 0x10000000U; |
| 3041 | int i; |
| 3042 | long ret; |
| 3043 | |
| 3044 | ret = seccomp(SECCOMP_GET_ACTION_AVAIL, 0, &actions[0]); |
| 3045 | ASSERT_NE(ENOSYS, errno) { |
| 3046 | TH_LOG("Kernel does not support seccomp syscall!"); |
| 3047 | } |
| 3048 | ASSERT_NE(EINVAL, errno) { |
| 3049 | TH_LOG("Kernel does not support SECCOMP_GET_ACTION_AVAIL operation!"); |
| 3050 | } |
| 3051 | EXPECT_EQ(ret, 0); |
| 3052 | |
| 3053 | for (i = 0; i < ARRAY_SIZE(actions); i++) { |
| 3054 | ret = seccomp(SECCOMP_GET_ACTION_AVAIL, 0, &actions[i]); |
| 3055 | EXPECT_EQ(ret, 0) { |
| 3056 | TH_LOG("Expected action (0x%X) not available!", |
| 3057 | actions[i]); |
| 3058 | } |
| 3059 | } |
| 3060 | |
| 3061 | /* Check that an unknown action is handled properly (EOPNOTSUPP) */ |
| 3062 | ret = seccomp(SECCOMP_GET_ACTION_AVAIL, 0, &unknown_action); |
| 3063 | EXPECT_EQ(ret, -1); |
| 3064 | EXPECT_EQ(errno, EOPNOTSUPP); |
| 3065 | } |
| 3066 | |
Tycho Andersen | d057dc4 | 2018-02-20 19:47:47 -0700 | [diff] [blame] | 3067 | TEST(get_metadata) |
| 3068 | { |
| 3069 | pid_t pid; |
| 3070 | int pipefd[2]; |
| 3071 | char buf; |
| 3072 | struct seccomp_metadata md; |
Kees Cook | 6c3b6d5 | 2018-03-15 09:59:16 -0700 | [diff] [blame] | 3073 | long ret; |
Tycho Andersen | d057dc4 | 2018-02-20 19:47:47 -0700 | [diff] [blame] | 3074 | |
Tycho Andersen | 3aa415dd | 2019-01-18 17:12:15 -0700 | [diff] [blame] | 3075 | /* Only real root can get metadata. */ |
| 3076 | if (geteuid()) { |
Kees Cook | 8b1bc88 | 2020-07-10 09:24:36 -0700 | [diff] [blame] | 3077 | SKIP(return, "get_metadata requires real root"); |
Tycho Andersen | 3aa415dd | 2019-01-18 17:12:15 -0700 | [diff] [blame] | 3078 | return; |
| 3079 | } |
| 3080 | |
Tycho Andersen | d057dc4 | 2018-02-20 19:47:47 -0700 | [diff] [blame] | 3081 | ASSERT_EQ(0, pipe(pipefd)); |
| 3082 | |
| 3083 | pid = fork(); |
| 3084 | ASSERT_GE(pid, 0); |
| 3085 | if (pid == 0) { |
| 3086 | struct sock_filter filter[] = { |
| 3087 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 3088 | }; |
| 3089 | struct sock_fprog prog = { |
| 3090 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 3091 | .filter = filter, |
| 3092 | }; |
| 3093 | |
| 3094 | /* one with log, one without */ |
Tycho Andersen | fb024a0 | 2019-01-18 17:12:12 -0700 | [diff] [blame] | 3095 | EXPECT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER, |
Tycho Andersen | d057dc4 | 2018-02-20 19:47:47 -0700 | [diff] [blame] | 3096 | SECCOMP_FILTER_FLAG_LOG, &prog)); |
Tycho Andersen | fb024a0 | 2019-01-18 17:12:12 -0700 | [diff] [blame] | 3097 | EXPECT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog)); |
Tycho Andersen | d057dc4 | 2018-02-20 19:47:47 -0700 | [diff] [blame] | 3098 | |
Tycho Andersen | fb024a0 | 2019-01-18 17:12:12 -0700 | [diff] [blame] | 3099 | EXPECT_EQ(0, close(pipefd[0])); |
Tycho Andersen | d057dc4 | 2018-02-20 19:47:47 -0700 | [diff] [blame] | 3100 | ASSERT_EQ(1, write(pipefd[1], "1", 1)); |
| 3101 | ASSERT_EQ(0, close(pipefd[1])); |
| 3102 | |
| 3103 | while (1) |
| 3104 | sleep(100); |
| 3105 | } |
| 3106 | |
| 3107 | ASSERT_EQ(0, close(pipefd[1])); |
| 3108 | ASSERT_EQ(1, read(pipefd[0], &buf, 1)); |
| 3109 | |
| 3110 | ASSERT_EQ(0, ptrace(PTRACE_ATTACH, pid)); |
| 3111 | ASSERT_EQ(pid, waitpid(pid, NULL, 0)); |
| 3112 | |
Kees Cook | 6c3b6d5 | 2018-03-15 09:59:16 -0700 | [diff] [blame] | 3113 | /* Past here must not use ASSERT or child process is never killed. */ |
| 3114 | |
Tycho Andersen | d057dc4 | 2018-02-20 19:47:47 -0700 | [diff] [blame] | 3115 | md.filter_off = 0; |
Kees Cook | 6c3b6d5 | 2018-03-15 09:59:16 -0700 | [diff] [blame] | 3116 | errno = 0; |
| 3117 | ret = ptrace(PTRACE_SECCOMP_GET_METADATA, pid, sizeof(md), &md); |
| 3118 | EXPECT_EQ(sizeof(md), ret) { |
| 3119 | if (errno == EINVAL) |
Kees Cook | 8b1bc88 | 2020-07-10 09:24:36 -0700 | [diff] [blame] | 3120 | SKIP(goto skip, "Kernel does not support PTRACE_SECCOMP_GET_METADATA (missing CONFIG_CHECKPOINT_RESTORE?)"); |
Kees Cook | 6c3b6d5 | 2018-03-15 09:59:16 -0700 | [diff] [blame] | 3121 | } |
| 3122 | |
Tycho Andersen | d057dc4 | 2018-02-20 19:47:47 -0700 | [diff] [blame] | 3123 | EXPECT_EQ(md.flags, SECCOMP_FILTER_FLAG_LOG); |
| 3124 | EXPECT_EQ(md.filter_off, 0); |
| 3125 | |
| 3126 | md.filter_off = 1; |
Kees Cook | 6c3b6d5 | 2018-03-15 09:59:16 -0700 | [diff] [blame] | 3127 | ret = ptrace(PTRACE_SECCOMP_GET_METADATA, pid, sizeof(md), &md); |
| 3128 | EXPECT_EQ(sizeof(md), ret); |
Tycho Andersen | d057dc4 | 2018-02-20 19:47:47 -0700 | [diff] [blame] | 3129 | EXPECT_EQ(md.flags, 0); |
| 3130 | EXPECT_EQ(md.filter_off, 1); |
| 3131 | |
Kees Cook | 6c3b6d5 | 2018-03-15 09:59:16 -0700 | [diff] [blame] | 3132 | skip: |
Tycho Andersen | d057dc4 | 2018-02-20 19:47:47 -0700 | [diff] [blame] | 3133 | ASSERT_EQ(0, kill(pid, SIGKILL)); |
| 3134 | } |
| 3135 | |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3136 | static int user_trap_syscall(int nr, unsigned int flags) |
| 3137 | { |
| 3138 | struct sock_filter filter[] = { |
| 3139 | BPF_STMT(BPF_LD+BPF_W+BPF_ABS, |
| 3140 | offsetof(struct seccomp_data, nr)), |
| 3141 | BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, nr, 0, 1), |
| 3142 | BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_USER_NOTIF), |
| 3143 | BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW), |
| 3144 | }; |
| 3145 | |
| 3146 | struct sock_fprog prog = { |
| 3147 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 3148 | .filter = filter, |
| 3149 | }; |
| 3150 | |
| 3151 | return seccomp(SECCOMP_SET_MODE_FILTER, flags, &prog); |
| 3152 | } |
| 3153 | |
Christian Brauner | 223e660 | 2019-09-20 10:30:06 +0200 | [diff] [blame] | 3154 | #define USER_NOTIF_MAGIC INT_MAX |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3155 | TEST(user_notification_basic) |
| 3156 | { |
| 3157 | pid_t pid; |
| 3158 | long ret; |
| 3159 | int status, listener; |
| 3160 | struct seccomp_notif req = {}; |
| 3161 | struct seccomp_notif_resp resp = {}; |
| 3162 | struct pollfd pollfd; |
| 3163 | |
| 3164 | struct sock_filter filter[] = { |
| 3165 | BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), |
| 3166 | }; |
| 3167 | struct sock_fprog prog = { |
| 3168 | .len = (unsigned short)ARRAY_SIZE(filter), |
| 3169 | .filter = filter, |
| 3170 | }; |
| 3171 | |
Tycho Andersen | c714070 | 2019-01-18 17:12:16 -0700 | [diff] [blame] | 3172 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 3173 | ASSERT_EQ(0, ret) { |
| 3174 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 3175 | } |
| 3176 | |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3177 | pid = fork(); |
| 3178 | ASSERT_GE(pid, 0); |
| 3179 | |
| 3180 | /* Check that we get -ENOSYS with no listener attached */ |
| 3181 | if (pid == 0) { |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3182 | if (user_trap_syscall(__NR_getppid, 0) < 0) |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3183 | exit(1); |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3184 | ret = syscall(__NR_getppid); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3185 | exit(ret >= 0 || errno != ENOSYS); |
| 3186 | } |
| 3187 | |
| 3188 | EXPECT_EQ(waitpid(pid, &status, 0), pid); |
| 3189 | EXPECT_EQ(true, WIFEXITED(status)); |
| 3190 | EXPECT_EQ(0, WEXITSTATUS(status)); |
| 3191 | |
Tycho Andersen | 0b54b44 | 2019-01-18 17:12:13 -0700 | [diff] [blame] | 3192 | /* Add some no-op filters for grins. */ |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3193 | EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog), 0); |
| 3194 | EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog), 0); |
| 3195 | EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog), 0); |
| 3196 | EXPECT_EQ(seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog), 0); |
| 3197 | |
| 3198 | /* Check that the basic notification machinery works */ |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3199 | listener = user_trap_syscall(__NR_getppid, |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3200 | SECCOMP_FILTER_FLAG_NEW_LISTENER); |
Kees Cook | 3d244c1 | 2019-01-16 16:35:25 -0800 | [diff] [blame] | 3201 | ASSERT_GE(listener, 0); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3202 | |
| 3203 | /* Installing a second listener in the chain should EBUSY */ |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3204 | EXPECT_EQ(user_trap_syscall(__NR_getppid, |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3205 | SECCOMP_FILTER_FLAG_NEW_LISTENER), |
| 3206 | -1); |
| 3207 | EXPECT_EQ(errno, EBUSY); |
| 3208 | |
| 3209 | pid = fork(); |
| 3210 | ASSERT_GE(pid, 0); |
| 3211 | |
| 3212 | if (pid == 0) { |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3213 | ret = syscall(__NR_getppid); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3214 | exit(ret != USER_NOTIF_MAGIC); |
| 3215 | } |
| 3216 | |
| 3217 | pollfd.fd = listener; |
| 3218 | pollfd.events = POLLIN | POLLOUT; |
| 3219 | |
| 3220 | EXPECT_GT(poll(&pollfd, 1, -1), 0); |
| 3221 | EXPECT_EQ(pollfd.revents, POLLIN); |
| 3222 | |
Sargun Dhillon | e4ab5cc | 2019-12-30 12:38:11 -0800 | [diff] [blame] | 3223 | /* Test that we can't pass garbage to the kernel. */ |
| 3224 | memset(&req, 0, sizeof(req)); |
| 3225 | req.pid = -1; |
| 3226 | errno = 0; |
| 3227 | ret = ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req); |
| 3228 | EXPECT_EQ(-1, ret); |
| 3229 | EXPECT_EQ(EINVAL, errno); |
| 3230 | |
| 3231 | if (ret) { |
| 3232 | req.pid = 0; |
| 3233 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); |
| 3234 | } |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3235 | |
| 3236 | pollfd.fd = listener; |
| 3237 | pollfd.events = POLLIN | POLLOUT; |
| 3238 | |
| 3239 | EXPECT_GT(poll(&pollfd, 1, -1), 0); |
| 3240 | EXPECT_EQ(pollfd.revents, POLLOUT); |
| 3241 | |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3242 | EXPECT_EQ(req.data.nr, __NR_getppid); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3243 | |
| 3244 | resp.id = req.id; |
| 3245 | resp.error = 0; |
| 3246 | resp.val = USER_NOTIF_MAGIC; |
| 3247 | |
| 3248 | /* check that we make sure flags == 0 */ |
| 3249 | resp.flags = 1; |
| 3250 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), -1); |
| 3251 | EXPECT_EQ(errno, EINVAL); |
| 3252 | |
| 3253 | resp.flags = 0; |
| 3254 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0); |
| 3255 | |
| 3256 | EXPECT_EQ(waitpid(pid, &status, 0), pid); |
| 3257 | EXPECT_EQ(true, WIFEXITED(status)); |
| 3258 | EXPECT_EQ(0, WEXITSTATUS(status)); |
| 3259 | } |
| 3260 | |
Tycho Andersen | 5189149 | 2020-03-04 11:05:17 -0700 | [diff] [blame] | 3261 | TEST(user_notification_with_tsync) |
| 3262 | { |
| 3263 | int ret; |
| 3264 | unsigned int flags; |
| 3265 | |
Kees Cook | e4d0502 | 2020-07-10 10:29:41 -0700 | [diff] [blame] | 3266 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 3267 | ASSERT_EQ(0, ret) { |
| 3268 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 3269 | } |
| 3270 | |
Tycho Andersen | 5189149 | 2020-03-04 11:05:17 -0700 | [diff] [blame] | 3271 | /* these were exclusive */ |
| 3272 | flags = SECCOMP_FILTER_FLAG_NEW_LISTENER | |
| 3273 | SECCOMP_FILTER_FLAG_TSYNC; |
| 3274 | ASSERT_EQ(-1, user_trap_syscall(__NR_getppid, flags)); |
| 3275 | ASSERT_EQ(EINVAL, errno); |
| 3276 | |
| 3277 | /* but now they're not */ |
| 3278 | flags |= SECCOMP_FILTER_FLAG_TSYNC_ESRCH; |
| 3279 | ret = user_trap_syscall(__NR_getppid, flags); |
| 3280 | close(ret); |
| 3281 | ASSERT_LE(0, ret); |
| 3282 | } |
| 3283 | |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3284 | TEST(user_notification_kill_in_middle) |
| 3285 | { |
| 3286 | pid_t pid; |
| 3287 | long ret; |
| 3288 | int listener; |
| 3289 | struct seccomp_notif req = {}; |
| 3290 | struct seccomp_notif_resp resp = {}; |
| 3291 | |
Tycho Andersen | c714070 | 2019-01-18 17:12:16 -0700 | [diff] [blame] | 3292 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 3293 | ASSERT_EQ(0, ret) { |
| 3294 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 3295 | } |
| 3296 | |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3297 | listener = user_trap_syscall(__NR_getppid, |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3298 | SECCOMP_FILTER_FLAG_NEW_LISTENER); |
Kees Cook | 3d244c1 | 2019-01-16 16:35:25 -0800 | [diff] [blame] | 3299 | ASSERT_GE(listener, 0); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3300 | |
| 3301 | /* |
| 3302 | * Check that nothing bad happens when we kill the task in the middle |
| 3303 | * of a syscall. |
| 3304 | */ |
| 3305 | pid = fork(); |
| 3306 | ASSERT_GE(pid, 0); |
| 3307 | |
| 3308 | if (pid == 0) { |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3309 | ret = syscall(__NR_getppid); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3310 | exit(ret != USER_NOTIF_MAGIC); |
| 3311 | } |
| 3312 | |
| 3313 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); |
| 3314 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ID_VALID, &req.id), 0); |
| 3315 | |
| 3316 | EXPECT_EQ(kill(pid, SIGKILL), 0); |
| 3317 | EXPECT_EQ(waitpid(pid, NULL, 0), pid); |
| 3318 | |
| 3319 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ID_VALID, &req.id), -1); |
| 3320 | |
| 3321 | resp.id = req.id; |
| 3322 | ret = ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp); |
| 3323 | EXPECT_EQ(ret, -1); |
| 3324 | EXPECT_EQ(errno, ENOENT); |
| 3325 | } |
| 3326 | |
| 3327 | static int handled = -1; |
| 3328 | |
| 3329 | static void signal_handler(int signal) |
| 3330 | { |
| 3331 | if (write(handled, "c", 1) != 1) |
| 3332 | perror("write from signal"); |
| 3333 | } |
| 3334 | |
| 3335 | TEST(user_notification_signal) |
| 3336 | { |
| 3337 | pid_t pid; |
| 3338 | long ret; |
| 3339 | int status, listener, sk_pair[2]; |
| 3340 | struct seccomp_notif req = {}; |
| 3341 | struct seccomp_notif_resp resp = {}; |
| 3342 | char c; |
| 3343 | |
Tycho Andersen | c714070 | 2019-01-18 17:12:16 -0700 | [diff] [blame] | 3344 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 3345 | ASSERT_EQ(0, ret) { |
| 3346 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 3347 | } |
| 3348 | |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3349 | ASSERT_EQ(socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair), 0); |
| 3350 | |
| 3351 | listener = user_trap_syscall(__NR_gettid, |
| 3352 | SECCOMP_FILTER_FLAG_NEW_LISTENER); |
Kees Cook | 3d244c1 | 2019-01-16 16:35:25 -0800 | [diff] [blame] | 3353 | ASSERT_GE(listener, 0); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3354 | |
| 3355 | pid = fork(); |
| 3356 | ASSERT_GE(pid, 0); |
| 3357 | |
| 3358 | if (pid == 0) { |
| 3359 | close(sk_pair[0]); |
| 3360 | handled = sk_pair[1]; |
| 3361 | if (signal(SIGUSR1, signal_handler) == SIG_ERR) { |
| 3362 | perror("signal"); |
| 3363 | exit(1); |
| 3364 | } |
| 3365 | /* |
| 3366 | * ERESTARTSYS behavior is a bit hard to test, because we need |
| 3367 | * to rely on a signal that has not yet been handled. Let's at |
| 3368 | * least check that the error code gets propagated through, and |
| 3369 | * hope that it doesn't break when there is actually a signal :) |
| 3370 | */ |
| 3371 | ret = syscall(__NR_gettid); |
| 3372 | exit(!(ret == -1 && errno == 512)); |
| 3373 | } |
| 3374 | |
| 3375 | close(sk_pair[1]); |
| 3376 | |
Sargun Dhillon | 88c13f8 | 2019-12-28 22:24:49 -0800 | [diff] [blame] | 3377 | memset(&req, 0, sizeof(req)); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3378 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); |
| 3379 | |
| 3380 | EXPECT_EQ(kill(pid, SIGUSR1), 0); |
| 3381 | |
| 3382 | /* |
| 3383 | * Make sure the signal really is delivered, which means we're not |
| 3384 | * stuck in the user notification code any more and the notification |
| 3385 | * should be dead. |
| 3386 | */ |
| 3387 | EXPECT_EQ(read(sk_pair[0], &c, 1), 1); |
| 3388 | |
| 3389 | resp.id = req.id; |
| 3390 | resp.error = -EPERM; |
| 3391 | resp.val = 0; |
| 3392 | |
| 3393 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), -1); |
| 3394 | EXPECT_EQ(errno, ENOENT); |
| 3395 | |
Sargun Dhillon | 88c13f8 | 2019-12-28 22:24:49 -0800 | [diff] [blame] | 3396 | memset(&req, 0, sizeof(req)); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3397 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); |
| 3398 | |
| 3399 | resp.id = req.id; |
| 3400 | resp.error = -512; /* -ERESTARTSYS */ |
| 3401 | resp.val = 0; |
| 3402 | |
| 3403 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0); |
| 3404 | |
| 3405 | EXPECT_EQ(waitpid(pid, &status, 0), pid); |
| 3406 | EXPECT_EQ(true, WIFEXITED(status)); |
| 3407 | EXPECT_EQ(0, WEXITSTATUS(status)); |
| 3408 | } |
| 3409 | |
| 3410 | TEST(user_notification_closed_listener) |
| 3411 | { |
| 3412 | pid_t pid; |
| 3413 | long ret; |
| 3414 | int status, listener; |
| 3415 | |
Tycho Andersen | c714070 | 2019-01-18 17:12:16 -0700 | [diff] [blame] | 3416 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 3417 | ASSERT_EQ(0, ret) { |
| 3418 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 3419 | } |
| 3420 | |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3421 | listener = user_trap_syscall(__NR_getppid, |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3422 | SECCOMP_FILTER_FLAG_NEW_LISTENER); |
Kees Cook | 3d244c1 | 2019-01-16 16:35:25 -0800 | [diff] [blame] | 3423 | ASSERT_GE(listener, 0); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3424 | |
| 3425 | /* |
| 3426 | * Check that we get an ENOSYS when the listener is closed. |
| 3427 | */ |
| 3428 | pid = fork(); |
| 3429 | ASSERT_GE(pid, 0); |
| 3430 | if (pid == 0) { |
| 3431 | close(listener); |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3432 | ret = syscall(__NR_getppid); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3433 | exit(ret != -1 && errno != ENOSYS); |
| 3434 | } |
| 3435 | |
| 3436 | close(listener); |
| 3437 | |
| 3438 | EXPECT_EQ(waitpid(pid, &status, 0), pid); |
| 3439 | EXPECT_EQ(true, WIFEXITED(status)); |
| 3440 | EXPECT_EQ(0, WEXITSTATUS(status)); |
| 3441 | } |
| 3442 | |
| 3443 | /* |
| 3444 | * Check that a pid in a child namespace still shows up as valid in ours. |
| 3445 | */ |
| 3446 | TEST(user_notification_child_pid_ns) |
| 3447 | { |
| 3448 | pid_t pid; |
| 3449 | int status, listener; |
| 3450 | struct seccomp_notif req = {}; |
| 3451 | struct seccomp_notif_resp resp = {}; |
| 3452 | |
Kees Cook | d7d2e5b | 2020-07-10 10:26:11 -0700 | [diff] [blame] | 3453 | ASSERT_EQ(unshare(CLONE_NEWUSER | CLONE_NEWPID), 0) { |
| 3454 | if (errno == EINVAL) |
| 3455 | SKIP(return, "kernel missing CLONE_NEWUSER support"); |
| 3456 | }; |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3457 | |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3458 | listener = user_trap_syscall(__NR_getppid, |
| 3459 | SECCOMP_FILTER_FLAG_NEW_LISTENER); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3460 | ASSERT_GE(listener, 0); |
| 3461 | |
| 3462 | pid = fork(); |
| 3463 | ASSERT_GE(pid, 0); |
| 3464 | |
| 3465 | if (pid == 0) |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3466 | exit(syscall(__NR_getppid) != USER_NOTIF_MAGIC); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3467 | |
| 3468 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); |
| 3469 | EXPECT_EQ(req.pid, pid); |
| 3470 | |
| 3471 | resp.id = req.id; |
| 3472 | resp.error = 0; |
| 3473 | resp.val = USER_NOTIF_MAGIC; |
| 3474 | |
| 3475 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0); |
| 3476 | |
| 3477 | EXPECT_EQ(waitpid(pid, &status, 0), pid); |
| 3478 | EXPECT_EQ(true, WIFEXITED(status)); |
| 3479 | EXPECT_EQ(0, WEXITSTATUS(status)); |
| 3480 | close(listener); |
| 3481 | } |
| 3482 | |
| 3483 | /* |
| 3484 | * Check that a pid in a sibling (i.e. unrelated) namespace shows up as 0, i.e. |
| 3485 | * invalid. |
| 3486 | */ |
| 3487 | TEST(user_notification_sibling_pid_ns) |
| 3488 | { |
| 3489 | pid_t pid, pid2; |
| 3490 | int status, listener; |
| 3491 | struct seccomp_notif req = {}; |
| 3492 | struct seccomp_notif_resp resp = {}; |
| 3493 | |
Tycho Andersen | c714070 | 2019-01-18 17:12:16 -0700 | [diff] [blame] | 3494 | ASSERT_EQ(prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0), 0) { |
| 3495 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 3496 | } |
| 3497 | |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3498 | listener = user_trap_syscall(__NR_getppid, |
| 3499 | SECCOMP_FILTER_FLAG_NEW_LISTENER); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3500 | ASSERT_GE(listener, 0); |
| 3501 | |
| 3502 | pid = fork(); |
| 3503 | ASSERT_GE(pid, 0); |
| 3504 | |
| 3505 | if (pid == 0) { |
| 3506 | ASSERT_EQ(unshare(CLONE_NEWPID), 0); |
| 3507 | |
| 3508 | pid2 = fork(); |
| 3509 | ASSERT_GE(pid2, 0); |
| 3510 | |
| 3511 | if (pid2 == 0) |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3512 | exit(syscall(__NR_getppid) != USER_NOTIF_MAGIC); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3513 | |
| 3514 | EXPECT_EQ(waitpid(pid2, &status, 0), pid2); |
| 3515 | EXPECT_EQ(true, WIFEXITED(status)); |
| 3516 | EXPECT_EQ(0, WEXITSTATUS(status)); |
| 3517 | exit(WEXITSTATUS(status)); |
| 3518 | } |
| 3519 | |
| 3520 | /* Create the sibling ns, and sibling in it. */ |
Kees Cook | d7d2e5b | 2020-07-10 10:26:11 -0700 | [diff] [blame] | 3521 | ASSERT_EQ(unshare(CLONE_NEWPID), 0) { |
| 3522 | if (errno == EPERM) |
| 3523 | SKIP(return, "CLONE_NEWPID requires CAP_SYS_ADMIN"); |
| 3524 | } |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3525 | ASSERT_EQ(errno, 0); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3526 | |
| 3527 | pid2 = fork(); |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3528 | ASSERT_GE(pid2, 0); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3529 | |
| 3530 | if (pid2 == 0) { |
| 3531 | ASSERT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); |
| 3532 | /* |
| 3533 | * The pid should be 0, i.e. the task is in some namespace that |
| 3534 | * we can't "see". |
| 3535 | */ |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3536 | EXPECT_EQ(req.pid, 0); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3537 | |
| 3538 | resp.id = req.id; |
| 3539 | resp.error = 0; |
| 3540 | resp.val = USER_NOTIF_MAGIC; |
| 3541 | |
| 3542 | ASSERT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0); |
| 3543 | exit(0); |
| 3544 | } |
| 3545 | |
| 3546 | close(listener); |
| 3547 | |
| 3548 | EXPECT_EQ(waitpid(pid, &status, 0), pid); |
| 3549 | EXPECT_EQ(true, WIFEXITED(status)); |
| 3550 | EXPECT_EQ(0, WEXITSTATUS(status)); |
| 3551 | |
| 3552 | EXPECT_EQ(waitpid(pid2, &status, 0), pid2); |
| 3553 | EXPECT_EQ(true, WIFEXITED(status)); |
| 3554 | EXPECT_EQ(0, WEXITSTATUS(status)); |
| 3555 | } |
| 3556 | |
| 3557 | TEST(user_notification_fault_recv) |
| 3558 | { |
| 3559 | pid_t pid; |
| 3560 | int status, listener; |
| 3561 | struct seccomp_notif req = {}; |
| 3562 | struct seccomp_notif_resp resp = {}; |
| 3563 | |
Tycho Andersen | 30d53a5 | 2019-01-18 17:12:17 -0700 | [diff] [blame] | 3564 | ASSERT_EQ(unshare(CLONE_NEWUSER), 0); |
| 3565 | |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3566 | listener = user_trap_syscall(__NR_getppid, |
| 3567 | SECCOMP_FILTER_FLAG_NEW_LISTENER); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3568 | ASSERT_GE(listener, 0); |
| 3569 | |
| 3570 | pid = fork(); |
| 3571 | ASSERT_GE(pid, 0); |
| 3572 | |
| 3573 | if (pid == 0) |
Kees Cook | 9dd3fcb | 2019-04-11 16:56:31 -0700 | [diff] [blame] | 3574 | exit(syscall(__NR_getppid) != USER_NOTIF_MAGIC); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3575 | |
| 3576 | /* Do a bad recv() */ |
| 3577 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, NULL), -1); |
| 3578 | EXPECT_EQ(errno, EFAULT); |
| 3579 | |
| 3580 | /* We should still be able to receive this notification, though. */ |
| 3581 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); |
| 3582 | EXPECT_EQ(req.pid, pid); |
| 3583 | |
| 3584 | resp.id = req.id; |
| 3585 | resp.error = 0; |
| 3586 | resp.val = USER_NOTIF_MAGIC; |
| 3587 | |
| 3588 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0); |
| 3589 | |
| 3590 | EXPECT_EQ(waitpid(pid, &status, 0), pid); |
| 3591 | EXPECT_EQ(true, WIFEXITED(status)); |
| 3592 | EXPECT_EQ(0, WEXITSTATUS(status)); |
| 3593 | } |
| 3594 | |
| 3595 | TEST(seccomp_get_notif_sizes) |
| 3596 | { |
| 3597 | struct seccomp_notif_sizes sizes; |
| 3598 | |
Kees Cook | 3d244c1 | 2019-01-16 16:35:25 -0800 | [diff] [blame] | 3599 | ASSERT_EQ(seccomp(SECCOMP_GET_NOTIF_SIZES, 0, &sizes), 0); |
Tycho Andersen | 6a21cc5 | 2018-12-09 11:24:13 -0700 | [diff] [blame] | 3600 | EXPECT_EQ(sizes.seccomp_notif, sizeof(struct seccomp_notif)); |
| 3601 | EXPECT_EQ(sizes.seccomp_notif_resp, sizeof(struct seccomp_notif_resp)); |
| 3602 | } |
| 3603 | |
Christian Brauner | 0eebfed | 2019-09-20 10:30:07 +0200 | [diff] [blame] | 3604 | static int filecmp(pid_t pid1, pid_t pid2, int fd1, int fd2) |
| 3605 | { |
| 3606 | #ifdef __NR_kcmp |
| 3607 | return syscall(__NR_kcmp, pid1, pid2, KCMP_FILE, fd1, fd2); |
| 3608 | #else |
| 3609 | errno = ENOSYS; |
| 3610 | return -1; |
| 3611 | #endif |
| 3612 | } |
| 3613 | |
| 3614 | TEST(user_notification_continue) |
| 3615 | { |
| 3616 | pid_t pid; |
| 3617 | long ret; |
| 3618 | int status, listener; |
| 3619 | struct seccomp_notif req = {}; |
| 3620 | struct seccomp_notif_resp resp = {}; |
| 3621 | struct pollfd pollfd; |
| 3622 | |
| 3623 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 3624 | ASSERT_EQ(0, ret) { |
| 3625 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 3626 | } |
| 3627 | |
| 3628 | listener = user_trap_syscall(__NR_dup, SECCOMP_FILTER_FLAG_NEW_LISTENER); |
| 3629 | ASSERT_GE(listener, 0); |
| 3630 | |
| 3631 | pid = fork(); |
| 3632 | ASSERT_GE(pid, 0); |
| 3633 | |
| 3634 | if (pid == 0) { |
| 3635 | int dup_fd, pipe_fds[2]; |
| 3636 | pid_t self; |
| 3637 | |
| 3638 | ret = pipe(pipe_fds); |
| 3639 | if (ret < 0) |
| 3640 | exit(1); |
| 3641 | |
| 3642 | dup_fd = dup(pipe_fds[0]); |
| 3643 | if (dup_fd < 0) |
| 3644 | exit(1); |
| 3645 | |
| 3646 | self = getpid(); |
| 3647 | |
| 3648 | ret = filecmp(self, self, pipe_fds[0], dup_fd); |
| 3649 | if (ret) |
| 3650 | exit(2); |
| 3651 | |
| 3652 | exit(0); |
| 3653 | } |
| 3654 | |
| 3655 | pollfd.fd = listener; |
| 3656 | pollfd.events = POLLIN | POLLOUT; |
| 3657 | |
| 3658 | EXPECT_GT(poll(&pollfd, 1, -1), 0); |
| 3659 | EXPECT_EQ(pollfd.revents, POLLIN); |
| 3660 | |
| 3661 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0); |
| 3662 | |
| 3663 | pollfd.fd = listener; |
| 3664 | pollfd.events = POLLIN | POLLOUT; |
| 3665 | |
| 3666 | EXPECT_GT(poll(&pollfd, 1, -1), 0); |
| 3667 | EXPECT_EQ(pollfd.revents, POLLOUT); |
| 3668 | |
| 3669 | EXPECT_EQ(req.data.nr, __NR_dup); |
| 3670 | |
| 3671 | resp.id = req.id; |
| 3672 | resp.flags = SECCOMP_USER_NOTIF_FLAG_CONTINUE; |
| 3673 | |
| 3674 | /* |
| 3675 | * Verify that setting SECCOMP_USER_NOTIF_FLAG_CONTINUE enforces other |
| 3676 | * args be set to 0. |
| 3677 | */ |
| 3678 | resp.error = 0; |
| 3679 | resp.val = USER_NOTIF_MAGIC; |
| 3680 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), -1); |
| 3681 | EXPECT_EQ(errno, EINVAL); |
| 3682 | |
| 3683 | resp.error = USER_NOTIF_MAGIC; |
| 3684 | resp.val = 0; |
| 3685 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), -1); |
| 3686 | EXPECT_EQ(errno, EINVAL); |
| 3687 | |
| 3688 | resp.error = 0; |
| 3689 | resp.val = 0; |
| 3690 | EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0) { |
| 3691 | if (errno == EINVAL) |
Kees Cook | 8b1bc88 | 2020-07-10 09:24:36 -0700 | [diff] [blame] | 3692 | SKIP(goto skip, "Kernel does not support SECCOMP_USER_NOTIF_FLAG_CONTINUE"); |
Christian Brauner | 0eebfed | 2019-09-20 10:30:07 +0200 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | skip: |
| 3696 | EXPECT_EQ(waitpid(pid, &status, 0), pid); |
| 3697 | EXPECT_EQ(true, WIFEXITED(status)); |
| 3698 | EXPECT_EQ(0, WEXITSTATUS(status)) { |
| 3699 | if (WEXITSTATUS(status) == 2) { |
Kees Cook | 8b1bc88 | 2020-07-10 09:24:36 -0700 | [diff] [blame] | 3700 | SKIP(return, "Kernel does not support kcmp() syscall"); |
Christian Brauner | 0eebfed | 2019-09-20 10:30:07 +0200 | [diff] [blame] | 3701 | return; |
| 3702 | } |
| 3703 | } |
| 3704 | } |
| 3705 | |
Christian Brauner | ad56821 | 2020-05-31 13:50:31 +0200 | [diff] [blame^] | 3706 | TEST(user_notification_filter_empty) |
| 3707 | { |
| 3708 | pid_t pid; |
| 3709 | long ret; |
| 3710 | int status; |
| 3711 | struct pollfd pollfd; |
| 3712 | struct clone_args args = { |
| 3713 | .flags = CLONE_FILES, |
| 3714 | .exit_signal = SIGCHLD, |
| 3715 | }; |
| 3716 | |
| 3717 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 3718 | ASSERT_EQ(0, ret) { |
| 3719 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 3720 | } |
| 3721 | |
| 3722 | pid = sys_clone3(&args, sizeof(args)); |
| 3723 | ASSERT_GE(pid, 0); |
| 3724 | |
| 3725 | if (pid == 0) { |
| 3726 | int listener; |
| 3727 | |
| 3728 | listener = user_trap_syscall(__NR_mknod, SECCOMP_FILTER_FLAG_NEW_LISTENER); |
| 3729 | if (listener < 0) |
| 3730 | _exit(EXIT_FAILURE); |
| 3731 | |
| 3732 | if (dup2(listener, 200) != 200) |
| 3733 | _exit(EXIT_FAILURE); |
| 3734 | |
| 3735 | close(listener); |
| 3736 | |
| 3737 | _exit(EXIT_SUCCESS); |
| 3738 | } |
| 3739 | |
| 3740 | EXPECT_EQ(waitpid(pid, &status, 0), pid); |
| 3741 | EXPECT_EQ(true, WIFEXITED(status)); |
| 3742 | EXPECT_EQ(0, WEXITSTATUS(status)); |
| 3743 | |
| 3744 | /* |
| 3745 | * The seccomp filter has become unused so we should be notified once |
| 3746 | * the kernel gets around to cleaning up task struct. |
| 3747 | */ |
| 3748 | pollfd.fd = 200; |
| 3749 | pollfd.events = POLLHUP; |
| 3750 | |
| 3751 | EXPECT_GT(poll(&pollfd, 1, 2000), 0); |
| 3752 | EXPECT_GT((pollfd.revents & POLLHUP) ?: 0, 0); |
| 3753 | } |
| 3754 | |
| 3755 | static void *do_thread(void *data) |
| 3756 | { |
| 3757 | return NULL; |
| 3758 | } |
| 3759 | |
| 3760 | TEST(user_notification_filter_empty_threaded) |
| 3761 | { |
| 3762 | pid_t pid; |
| 3763 | long ret; |
| 3764 | int status; |
| 3765 | struct pollfd pollfd; |
| 3766 | struct clone_args args = { |
| 3767 | .flags = CLONE_FILES, |
| 3768 | .exit_signal = SIGCHLD, |
| 3769 | }; |
| 3770 | |
| 3771 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 3772 | ASSERT_EQ(0, ret) { |
| 3773 | TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); |
| 3774 | } |
| 3775 | |
| 3776 | pid = sys_clone3(&args, sizeof(args)); |
| 3777 | ASSERT_GE(pid, 0); |
| 3778 | |
| 3779 | if (pid == 0) { |
| 3780 | pid_t pid1, pid2; |
| 3781 | int listener, status; |
| 3782 | pthread_t thread; |
| 3783 | |
| 3784 | listener = user_trap_syscall(__NR_dup, SECCOMP_FILTER_FLAG_NEW_LISTENER); |
| 3785 | if (listener < 0) |
| 3786 | _exit(EXIT_FAILURE); |
| 3787 | |
| 3788 | if (dup2(listener, 200) != 200) |
| 3789 | _exit(EXIT_FAILURE); |
| 3790 | |
| 3791 | close(listener); |
| 3792 | |
| 3793 | pid1 = fork(); |
| 3794 | if (pid1 < 0) |
| 3795 | _exit(EXIT_FAILURE); |
| 3796 | |
| 3797 | if (pid1 == 0) |
| 3798 | _exit(EXIT_SUCCESS); |
| 3799 | |
| 3800 | pid2 = fork(); |
| 3801 | if (pid2 < 0) |
| 3802 | _exit(EXIT_FAILURE); |
| 3803 | |
| 3804 | if (pid2 == 0) |
| 3805 | _exit(EXIT_SUCCESS); |
| 3806 | |
| 3807 | if (pthread_create(&thread, NULL, do_thread, NULL) || |
| 3808 | pthread_join(thread, NULL)) |
| 3809 | _exit(EXIT_FAILURE); |
| 3810 | |
| 3811 | if (pthread_create(&thread, NULL, do_thread, NULL) || |
| 3812 | pthread_join(thread, NULL)) |
| 3813 | _exit(EXIT_FAILURE); |
| 3814 | |
| 3815 | if (waitpid(pid1, &status, 0) != pid1 || !WIFEXITED(status) || |
| 3816 | WEXITSTATUS(status)) |
| 3817 | _exit(EXIT_FAILURE); |
| 3818 | |
| 3819 | if (waitpid(pid2, &status, 0) != pid2 || !WIFEXITED(status) || |
| 3820 | WEXITSTATUS(status)) |
| 3821 | _exit(EXIT_FAILURE); |
| 3822 | |
| 3823 | exit(EXIT_SUCCESS); |
| 3824 | } |
| 3825 | |
| 3826 | EXPECT_EQ(waitpid(pid, &status, 0), pid); |
| 3827 | EXPECT_EQ(true, WIFEXITED(status)); |
| 3828 | EXPECT_EQ(0, WEXITSTATUS(status)); |
| 3829 | |
| 3830 | /* |
| 3831 | * The seccomp filter has become unused so we should be notified once |
| 3832 | * the kernel gets around to cleaning up task struct. |
| 3833 | */ |
| 3834 | pollfd.fd = 200; |
| 3835 | pollfd.events = POLLHUP; |
| 3836 | |
| 3837 | EXPECT_GT(poll(&pollfd, 1, 2000), 0); |
| 3838 | EXPECT_GT((pollfd.revents & POLLHUP) ?: 0, 0); |
| 3839 | } |
| 3840 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 3841 | /* |
| 3842 | * TODO: |
| 3843 | * - add microbenchmarks |
| 3844 | * - expand NNP testing |
| 3845 | * - better arch-specific TRACE and TRAP handlers. |
| 3846 | * - endianness checking when appropriate |
| 3847 | * - 64-bit arg prodding |
| 3848 | * - arch value testing (x86 modes especially) |
Tyler Hicks | e66a399 | 2017-08-11 04:33:56 +0000 | [diff] [blame] | 3849 | * - verify that FILTER_FLAG_LOG filters generate log messages |
Tyler Hicks | 59f5cf4 | 2017-08-11 04:33:57 +0000 | [diff] [blame] | 3850 | * - verify that RET_LOG generates log messages |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 3851 | * - ... |
| 3852 | */ |
| 3853 | |
| 3854 | TEST_HARNESS_MAIN |