| 1 | // The widget engine process's own seccomp-bpf filter (WidgetEngineService#onCreate), on top of SELinux isolated_app and |
| 2 | // the zygote's app filter: code that escapes QuickJS should find no way to start programs or processes, and as little |
| 3 | // of the kernel's attack surface (perf events, eBPF, userfaultfd, io_uring, keyrings, ptrace) as possible. |
| 4 | // |
| 5 |
| // Two modes. Audit: the listed calls trap (SIGSYS); the handler records them, logged by a thread as "TwidgetSeccomp" |
| 6 | // lines, and makes the call itself through an escape hatch the filter lets through (instruction_pointer at |
| 7 | // twidget_sc_gadget_ip), so nothing changes but the log. Process creation and exec are refused (EPERM) even then: they |
| 8 | // cannot be redone from a signal handler, and the engine never needs them. ioctl is not filtered: ART's GC makes |
| 9 | // userfaultfd ioctls from threads with SIGSYS blocked, where a trap kills the process (the kernel resets the handler), |
| 10 | // so even auditing it is unsafe; narrowing ioctl needs another way. Enforce: the dangerous calls kill the process |
| 11 | // (the client restarts the engine), other sockets than AF_UNIX get EPERM, clone3 ENOSYS (bionic falls back to clone, |
| 12 | // whose flags the filter can read). |
| 13 | // |
| 14 | // Needs SECCOMP_FILTER_FLAG_TSYNC (Linux 3.17; every device from Android 8): ART's threads exist before the service |
| 15 | // starts. Without it the engine runs unfiltered, which is logged. |
| 16 |
| 17 | #include <jni.h> |
| 18 | #include <android/log.h> |
| 19 | #include <asm/unistd.h> |
| 20 | #include <errno.h> |
| 21 | #include <linux/audit.h> |
| 22 | #include <linux/filter.h> |
| 23 | #include <linux/seccomp.h> |
| 24 | #include <pthread.h> |
| 25 | #include <sched.h> |
| 26 | #include <signal.h> |
| 27 | #include <stddef.h> |
| 28 | #include <stdint.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/prctl.h> |
| 31 | #include <sys/socket.h> |
| 32 | #include <sys/syscall.h> |
| 33 | #include <ucontext.h> |
| 34 | #include <unistd.h> |
| 35 |
| 36 | #include <atomic> |
| 37 | #include <vector> |
| 38 |
| 39 | #define TAG "TwidgetSeccomp" |
| 40 |
| 41 | #ifndef SECCOMP_RET_KILL_PROCESS |
| 42 | #define SECCOMP_RET_KILL_PROCESS 0x80000000U |
| 43 | #endif |
| 44 | #ifndef SECCOMP_FILTER_FLAG_TSYNC |
| 45 | #define SECCOMP_FILTER_FLAG_TSYNC 1 |
| 46 | #endif |
| 47 | #ifndef SECCOMP_SET_MODE_FILTER |
| 48 | #define SECCOMP_SET_MODE_FILTER 1 |
| 49 | #endif |
| 50 | #ifndef SYS_SECCOMP |
| 51 | #define SYS_SECCOMP 1 |
| 52 | #endif |
| 53 |
| 54 | extern "C" long twidget_sc_gadget(long nr, long a0, long a1, long a2, long a3, long a4, long a5); |
| 55 | extern "C" char twidget_sc_gadget_ip[]; |
| 56 |
| 57 | // The escape hatch: one syscall instruction, at a known address right before twidget_sc_gadget_ip (where the kernel |
| 58 | // reports the instruction pointer of a syscall, after its instruction). |
| 59 | #if defined(__aarch64__) |
| 60 | #define TW_ARCH AUDIT_ARCH_AARCH64 |
| 61 | asm(R"( |
| 62 | .text |
| 63 | .p2align 2 |
| 64 | .globl twidget_sc_gadget |
| 65 | .hidden twidget_sc_gadget |
| 66 | .type twidget_sc_gadget, %function |
| 67 | twidget_sc_gadget: |
| 68 | mov x8, x0 |
| 69 | mov x0, x1 |
| 70 | mov x1, x2 |
| 71 | mov x2, x3 |
| 72 | mov x3, x4 |
| 73 | mov x4, x5 |
| 74 | mov x5, x6 |
| 75 | svc #0 |
| 76 | .globl twidget_sc_gadget_ip |
| 77 | .hidden twidget_sc_gadget_ip |
| 78 | twidget_sc_gadget_ip: |
| 79 | ret |
| 80 | .size twidget_sc_gadget, . - twidget_sc_gadget |
| 81 | )"); |
| 82 | #elif defined(__arm__) |
| 83 | #define TW_ARCH AUDIT_ARCH_ARM |
| 84 | asm(R"( |
| 85 | .text |
| 86 | .p2align 2 |
| 87 | .arm |
| 88 | .globl twidget_sc_gadget |
| 89 | .hidden twidget_sc_gadget |
| 90 | .type twidget_sc_gadget, %function |
| 91 | twidget_sc_gadget: |
| 92 | push {r4, r5, r7, lr} |
| 93 | mov r7, r0 |
| 94 | mov r0, r1 |
| 95 | mov r1, r2 |
| 96 | mov r2, r3 |
| 97 | ldr r3, [sp, #16] |
| 98 | ldr r4, [sp, #20] |
| 99 | ldr r5, [sp, #24] |
| 100 | svc #0 |
| 101 | .globl twidget_sc_gadget_ip |
| 102 | .hidden twidget_sc_gadget_ip |
| 103 | twidget_sc_gadget_ip: |
| 104 | pop {r4, r5, r7, pc} |
| 105 | .size twidget_sc_gadget, . - twidget_sc_gadget |
| 106 | )"); |
| 107 | #elif defined(__x86_64__) |
| 108 | #define TW_ARCH AUDIT_ARCH_X86_64 |
| 109 | asm(R"( |
| 110 | .text |
| 111 | .globl twidget_sc_gadget |
| 112 | .hidden twidget_sc_gadget |
| 113 | .type twidget_sc_gadget, @function |
| 114 | twidget_sc_gadget: |
| 115 | mov %rdi, %rax |
| 116 | mov %rsi, %rdi |
| 117 | mov %rdx, %rsi |
| 118 | mov %rcx, %rdx |
| 119 | mov %r8, %r10 |
| 120 | mov %r9, %r8 |
| 121 | mov 8(%rsp), %r9 |
| 122 | syscall |
| 123 | .globl twidget_sc_gadget_ip |
| 124 | .hidden twidget_sc_gadget_ip |
| 125 | twidget_sc_gadget_ip: |
| 126 | ret |
| 127 | .size twidget_sc_gadget, . - twidget_sc_gadget |
| 128 | )"); |
| 129 | #elif defined(__i386__) |
| 130 | #define TW_ARCH AUDIT_ARCH_I386 |
| 131 | asm(R"( |
| 132 | .text |
| 133 | .globl twidget_sc_gadget |
| 134 | .hidden twidget_sc_gadget |
| 135 | .type twidget_sc_gadget, @function |
| 136 | twidget_sc_gadget: |
| 137 | push %ebp |
| 138 | push %edi |
| 139 | push %esi |
| 140 | push %ebx |
| 141 | mov 20(%esp), %eax |
| 142 | mov 24(%esp), %ebx |
| 143 | mov 28(%esp), %ecx |
| 144 | mov 32(%esp), %edx |
| 145 | mov 36(%esp), %esi |
| 146 | mov 40(%esp), %edi |
| 147 | mov 44(%esp), %ebp |
| 148 | int $0x80 |
| 149 | .globl twidget_sc_gadget_ip |
| 150 | .hidden twidget_sc_gadget_ip |
| 151 | twidget_sc_gadget_ip: |
| 152 | pop %ebx |
| 153 | pop %esi |
| 154 | pop %edi |
| 155 | pop %ebp |
| 156 | ret |
| 157 | .size twidget_sc_gadget, . - twidget_sc_gadget |
| 158 | )"); |
| 159 | #else |
| 160 | #error "twidget seccomp: unknown architecture" |
| 161 | #endif |
| 162 |
| 163 | namespace { |
| 164 |
| 165 | enum Kind { |
| 166 | KILL, // never needed; enforce kills the process |
| 167 | NO_PROC, // process creation, exec: EPERM in both modes |
| 168 | }; |
| 169 |
| 170 | struct Rule { |
| 171 | int nr; |
| 172 | const char *name; |
| 173 | Kind kind; |
| 174 | }; |
| 175 |
| 176 | #define R(name, kind) {__NR_##name, #name, kind} |
| 177 | const Rule RULES[] = { |
| 178 | R(execve, NO_PROC), |
| 179 | #ifdef __NR_execveat |
| 180 | R(execveat, NO_PROC), |
| 181 | #endif |
| 182 | #ifdef __NR_fork |
| 183 | R(fork, NO_PROC), |
| 184 | #endif |
| 185 | #ifdef __NR_vfork |
| 186 | R(vfork, NO_PROC), |
| 187 | #endif |
| 188 | R(ptrace, KILL), |
| 189 | R(process_vm_readv, KILL), |
| 190 | R(process_vm_writev, KILL), |
| 191 | R(perf_event_open, KILL), |
| 192 | #ifdef __NR_bpf |
| 193 | R(bpf, KILL), |
| 194 | #endif |
| 195 | #ifdef __NR_io_uring_setup |
| 196 | R(io_uring_setup, KILL), |
| 197 | R(io_uring_enter, KILL), |
| 198 | R(io_uring_register, KILL), |
| 199 | #endif |
| 200 | R(add_key, KILL), |
| 201 | R(request_key, KILL), |
| 202 | R(keyctl, KILL), |
| 203 | R(kexec_load, KILL), |
| 204 | #ifdef __NR_kexec_file_load |
| 205 | R(kexec_file_load, KILL), |
| 206 | #endif |
| 207 | R(init_module, KILL), |
| 208 | R(finit_module, KILL), |
| 209 | R(delete_module, KILL), |
| 210 | R(unshare, KILL), |
| 211 | R(setns, KILL), |
| 212 | R(mount, KILL), |
| 213 | R(umount2, KILL), |
| 214 | R(pivot_root, KILL), |
| 215 | R(chroot, KILL), |
| 216 | R(swapon, KILL), |
| 217 | R(swapoff, KILL), |
| 218 | R(reboot, KILL), |
| 219 | R(acct, KILL), |
| 220 | R(name_to_handle_at, KILL), |
| 221 | R(open_by_handle_at, KILL), |
| 222 | #ifdef __NR_iopl |
| 223 | R(iopl, KILL), |
| 224 | #endif |
| 225 | #ifdef __NR_ioperm |
| 226 | R(ioperm, KILL), |
| 227 | #endif |
| 228 | #ifdef __NR_modify_ldt |
| 229 | R(modify_ldt, KILL), |
| 230 | #endif |
| 231 | }; |
| 232 | #undef R |
| 233 |
| 234 | bool enforcing; |
| 235 | // Our traps carry this in SECCOMP_RET_DATA (si_errno): SIGSYS also comes from the zygote's own app filter, whose calls |
| 236 | // go to the handler that was there before (debuggerd's), as without ours. |
| 237 | constexpr uint32_t TRAP_DATA = 0x5eed; |
| 238 | struct sigaction previous; |
| 239 |
| 240 | // What the handler saw, async-signal-safe: counts by syscall number. |
| 241 | constexpr int MAX_NR = 1024; |
| 242 | std::atomic<uint32_t> counts[MAX_NR]; |
| 243 |
| 244 | const char *nameOf(int nr) { |
| 245 | for (const Rule &rule : RULES) |
| 246 | if (rule.nr == nr) return rule.name; |
| 247 | #ifdef __NR_socket |
| 248 | if (nr == __NR_socket) return "socket"; |
| 249 | #endif |
| 250 | #ifdef __NR_socketpair |
| 251 | if (nr == __NR_socketpair) return "socketpair"; |
| 252 | #endif |
| 253 | if (nr == __NR_clone) return "clone"; |
| 254 | #ifdef __NR_userfaultfd |
| 255 | if (nr == __NR_userfaultfd) return "userfaultfd"; |
| 256 | #endif |
| 257 | return "?"; |
| 258 | } |
| 259 |
| 260 | bool noProcess(int nr) { |
| 261 | for (const Rule &rule : RULES) |
| 262 | if (rule.nr == nr) return rule.kind == NO_PROC; |
| 263 | return nr == __NR_clone; // only a clone without CLONE_THREAD traps |
| 264 | } |
| 265 |
| 266 | void setResult(void *context, long result) { |
| 267 | auto *uc = static_cast<ucontext_t *>(context); |
| 268 | #if defined(__aarch64__) |
| 269 | uc->uc_mcontext.regs[0] = result; |
| 270 | #elif defined(__arm__) |
| 271 | uc->uc_mcontext.arm_r0 = result; |
| 272 | #elif defined(__x86_64__) |
| 273 | uc->uc_mcontext.gregs[REG_RAX] = result; |
| 274 | #elif defined(__i386__) |
| 275 | uc->uc_mcontext.gregs[REG_EAX] = result; |
| 276 | #endif |
| 277 | } |
| 278 |
| 279 | std::atomic<uint32_t> systemBlocked[MAX_NR]; |
| 280 |
| 281 | void onSigsys(int signal, siginfo_t *info, void *context) { |
| 282 | // Our own call, redone in the audit handler (SA_NODEFER brings it here): the system's filter refuses it too, and |
| 283 | // stacked filters report the newest one's data, ours. Without our filter the original call would have hit the |
| 284 | // system's trap; the engine gets ENOSYS instead, and the log says so. |
| 285 | if (info->si_call_addr == static_cast<void *>(twidget_sc_gadget_ip)) { |
| 286 | int nr = info->si_syscall; |
| 287 | if (nr >= 0 && nr < MAX_NR) systemBlocked[nr].fetch_add(1, std::memory_order_relaxed); |
| 288 | setResult(context, -ENOSYS); |
| 289 | return; |
| 290 | } |
| 291 | if (info->si_errno != static_cast<int>(TRAP_DATA)) { |
| 292 | __android_log_print(ANDROID_LOG_ERROR, TAG, "foreign SIGSYS: nr %d arch 0x%x data 0x%x", info->si_syscall, |
| 293 | info->si_arch, info->si_errno); |
| 294 | if ((previous.sa_flags & SA_SIGINFO) && previous.sa_sigaction) { |
| 295 | previous.sa_sigaction(signal, info, context); |
| 296 | } else if (previous.sa_handler != SIG_IGN && previous.sa_handler != SIG_DFL && previous.sa_handler) { |
| 297 | previous.sa_handler(signal); |
| 298 | } else { |
| 299 | sigaction(SIGSYS, &previous, nullptr); |
| 300 | raise(SIGSYS); |
| 301 | } |
| 302 | return; |
| 303 | } |
| 304 | int saved = errno; |
| 305 | auto *uc = static_cast<ucontext_t *>(context); |
| 306 | long nr = info->si_syscall, a[6]; |
| 307 | #if defined(__aarch64__) |
| 308 | for (int i = 0; i < 6; i++) a[i] = uc->uc_mcontext.regs[i]; |
| 309 | #elif defined(__arm__) |
| 310 | a[0] = uc->uc_mcontext.arm_r0; a[1] = uc->uc_mcontext.arm_r1; a[2] = uc->uc_mcontext.arm_r2; |
| 311 | a[3] = uc->uc_mcontext.arm_r3; a[4] = uc->uc_mcontext.arm_r4; a[5] = uc->uc_mcontext.arm_r5; |
| 312 | #elif defined(__x86_64__) |
| 313 | const greg_t *g = uc->uc_mcontext.gregs; |
| 314 | a[0] = g[REG_RDI]; a[1] = g[REG_RSI]; a[2] = g[REG_RDX]; a[3] = g[REG_R10]; a[4] = g[REG_R8]; a[5] = g[REG_R9]; |
| 315 | #elif defined(__i386__) |
| 316 | const greg_t *g = uc->uc_mcontext.gregs; |
| 317 | a[0] = g[REG_EBX]; a[1] = g[REG_ECX]; a[2] = g[REG_EDX]; a[3] = g[REG_ESI]; a[4] = g[REG_EDI]; a[5] = g[REG_EBP]; |
| 318 | #endif |
| 319 | if (nr >= 0 && nr < MAX_NR) counts[nr].fetch_add(1, std::memory_order_relaxed); |
| 320 | long result = noProcess(static_cast<int>(nr)) ? -EPERM : twidget_sc_gadget(nr, a[0], a[1], a[2], a[3], a[4], a[5]); |
| 321 | setResult(context, result); |
| 322 | errno = saved; |
| 323 | } |
| 324 |
| 325 | // Logs what the handler recorded since the last pass, every two seconds. |
| 326 | void *reporter(void *) { |
| 327 | static uint32_t logged[MAX_NR], loggedBlocked[MAX_NR]; |
| 328 | for (;;) { |
| 329 | sleep(2); |
| 330 | for (int nr = 0; nr < MAX_NR; nr++) { |
| 331 | uint32_t count = counts[nr].load(std::memory_order_relaxed); |
| 332 | if (count != logged[nr]) { |
| 333 | __android_log_print(ANDROID_LOG_WARN, TAG, "audit: %s (%d) x%u", nameOf(nr), nr, count); |
| 334 | logged[nr] = count; |
| 335 | } |
| 336 | uint32_t blocked = systemBlocked[nr].load(std::memory_order_relaxed); |
| 337 | if (blocked != loggedBlocked[nr]) { |
| 338 | __android_log_print(ANDROID_LOG_WARN, TAG, "audit: %s (%d) refused by the system filter too, x%u", |
| 339 | nameOf(nr), nr, blocked); |
| 340 | loggedBlocked[nr] = blocked; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | return nullptr; |
| 345 | } |
| 346 |
| 347 | // The filter as BPF, built at run time for the gadget's address. |
| 348 | struct Program { |
| 349 | std::vector<sock_filter> code; |
| 350 | void op(uint16_t code_, uint32_t k, uint8_t jt = 0, uint8_t jf = 0) { code.push_back({code_, jt, jf, k}); } |
| 351 | void load(uint32_t offset) { op(BPF_LD | BPF_W | BPF_ABS, offset); } |
| 352 | void ret(uint32_t value) { op(BPF_RET | BPF_K, value); } |
| 353 | // if (A == value) return result |
| 354 | void ifEqual(uint32_t value, uint32_t result) { |
| 355 | op(BPF_JMP | BPF_JEQ | BPF_K, value, 0, 1); |
| 356 | ret(result); |
| 357 | } |
| 358 | }; |
| 359 |
| 360 | constexpr uint32_t argLow(int i) { |
| 361 | return offsetof(seccomp_data, args) + 8 * i; // little-endian: the low word first |
| 362 | } |
| 363 |
| 364 | Program build(bool enforce) { |
| 365 | const uint32_t trap = SECCOMP_RET_TRAP | TRAP_DATA; |
| 366 | const uint32_t kill = enforce ? SECCOMP_RET_KILL_PROCESS : trap; |
| 367 | const uint32_t noProc = enforce ? (SECCOMP_RET_ERRNO | EPERM) : trap; |
| 368 | const uint32_t denied = enforce ? (SECCOMP_RET_ERRNO | EPERM) : trap; |
| 369 | Program p; |
| 370 |
| 371 | // Another architecture's calls (int 0x80 from x86_64) would get around every number below. |
| 372 | p.load(offsetof(seccomp_data, arch)); |
| 373 | p.op(BPF_JMP | BPF_JEQ | BPF_K, TW_ARCH, 1, 0); |
| 374 | p.ret(enforce ? SECCOMP_RET_KILL_PROCESS : (SECCOMP_RET_ERRNO | ENOSYS)); |
| 375 |
| 376 | if (!enforce) { |
| 377 | // The audit handler's own calls. |
| 378 | uint64_t ip = reinterpret_cast<uintptr_t>(twidget_sc_gadget_ip); |
| 379 | p.load(offsetof(seccomp_data, instruction_pointer)); |
| 380 | p.op(BPF_JMP | BPF_JEQ | BPF_K, static_cast<uint32_t>(ip), 0, 3); |
| 381 | p.load(offsetof(seccomp_data, instruction_pointer) + 4); |
| 382 | p.op(BPF_JMP | BPF_JEQ | BPF_K, static_cast<uint32_t>(ip >> 32), 0, 1); |
| 383 | p.ret(SECCOMP_RET_ALLOW); |
| 384 | } |
| 385 |
| 386 | p.load(offsetof(seccomp_data, nr)); |
| 387 | for (const Rule &rule : RULES) p.ifEqual(rule.nr, rule.kind == NO_PROC ? noProc : kill); |
| 388 |
| 389 | #ifdef __NR_clone3 |
| 390 | // Its flags sit in memory the filter cannot read: bionic falls back to clone. |
| 391 | p.ifEqual(__NR_clone3, SECCOMP_RET_ERRNO | ENOSYS); |
| 392 | #endif |
| 393 | // clone: threads only. |
| 394 | p.op(BPF_JMP | BPF_JEQ | BPF_K, __NR_clone, 0, 4); |
| 395 | p.load(argLow(0)); |
| 396 | p.op(BPF_JMP | BPF_JSET | BPF_K, CLONE_THREAD, 0, 1); |
| 397 | p.ret(SECCOMP_RET_ALLOW); |
| 398 | p.ret(noProc); |
| 399 |
| 400 | #if defined(__NR_socket) && !defined(__i386__) |
| 401 | // Sockets: AF_UNIX only (logd). SELinux and the missing inet group refuse the rest already. (32-bit x86 goes |
| 402 | // through socketcall, whose arguments are in memory.) |
| 403 | for (int nr : {__NR_socket, __NR_socketpair}) { |
| 404 | p.load(offsetof(seccomp_data, nr)); |
| 405 | p.op(BPF_JMP | BPF_JEQ | BPF_K, static_cast<uint32_t>(nr), 0, 4); |
| 406 | p.load(argLow(0)); |
| 407 | p.op(BPF_JMP | BPF_JEQ | BPF_K, AF_UNIX, 0, 1); |
| 408 | p.ret(SECCOMP_RET_ALLOW); |
| 409 | p.ret(denied); |
| 410 | } |
| 411 | #endif |
| 412 |
| 413 | #ifdef __NR_userfaultfd |
| 414 | // userfaultfd: ART's concurrent mark-compact GC (Android 14+) uses it, user-mode faults only. Kernel-mode faults |
| 415 | // are what makes it an exploit primitive (stalling the kernel mid-copy), and UFFD_USER_MODE_ONLY takes them away. |
| 416 | p.load(offsetof(seccomp_data, nr)); |
| 417 | p.op(BPF_JMP | BPF_JEQ | BPF_K, __NR_userfaultfd, 0, 4); |
| 418 | p.load(argLow(0)); |
| 419 | p.op(BPF_JMP | BPF_JSET | BPF_K, 1 /* UFFD_USER_MODE_ONLY */, 0, 1); |
| 420 | p.ret(SECCOMP_RET_ALLOW); |
| 421 | p.ret(kill); |
| 422 | #endif |
| 423 |
| 424 | p.ret(SECCOMP_RET_ALLOW); |
| 425 | return p; |
| 426 | } |
| 427 |
| 428 | } // namespace |
| 429 |
| 430 | // 0 when the filter is on; else the errno of the step that failed (the engine then runs without it). |
| 431 | extern "C" JNIEXPORT jint JNICALL Java_org_telegram_utils_twidget_WidgetEngineService_installSeccomp(JNIEnv *, jclass, |
| 432 | jboolean enforce) { |
| 433 | static std::atomic<bool> installed{false}; |
| 434 | if (installed.exchange(true)) return 0; |
| 435 | enforcing = enforce; |
| 436 | if (!enforce) { |
| 437 | struct sigaction action = {}; |
| 438 | action.sa_sigaction = onSigsys; |
| 439 | action.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_NODEFER; |
| 440 | sigemptyset(&action.sa_mask); |
| 441 | if (sigaction(SIGSYS, &action, &previous) != 0) { |
| 442 | int error = errno; |
| 443 | __android_log_print(ANDROID_LOG_ERROR, TAG, "no SIGSYS handler (%s): unfiltered", strerror(error)); |
| 444 | return error; |
| 445 | } |
| 446 | } |
| 447 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) { |
| 448 | int error = errno; |
| 449 | __android_log_print(ANDROID_LOG_ERROR, TAG, "PR_SET_NO_NEW_PRIVS failed (%s): unfiltered", strerror(error)); |
| 450 | return error; |
| 451 | } |
| 452 | Program program = build(enforce); |
| 453 | sock_fprog prog = {static_cast<unsigned short>(program.code.size()), program.code.data()}; |
| 454 | long result = syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC, &prog); |
| 455 | if (result != 0) { |
| 456 | // -1 with errno (ENOSYS/EINVAL: no seccomp or no TSYNC), or the id of a thread that could not be synced. |
| 457 | int error = result < 0 ? errno : ESRCH; |
| 458 | __android_log_print(ANDROID_LOG_ERROR, TAG, "seccomp filter not installed (%s, %ld): unfiltered", |
| 459 | strerror(error), result); |
| 460 | return error; |
| 461 | } |
| 462 | __android_log_print(ANDROID_LOG_INFO, TAG, "filter on: %s, %zu instructions", enforce ? "enforce" : "audit", |
| 463 | program.code.size()); |
| 464 | if (!enforce) { |
| 465 | pthread_t thread; |
| 466 | if (pthread_create(&thread, nullptr, reporter, nullptr) == 0) pthread_detach(thread); |
| 467 | } |
| 468 | return 0; |
| 469 | } |
| 470 |
| 471 | // Debug builds: a few calls the filter refuses, with what came back, as a log line each. Audit: each also shows up as |
| 472 | // an "audit:" line, then gets the kernel's own answer (exec still EPERM). |
| 473 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetEngineService_seccompSelfTest(JNIEnv *, jclass) { |
| 474 | auto run = [](const char *what, long result) { |
| 475 | int error = result < 0 ? errno : 0; |
| 476 | __android_log_print(ANDROID_LOG_INFO, TAG, "self-test: %s -> %ld (%s)", what, result, error ? strerror(error) : "ok"); |
| 477 | }; |
| 478 | #if defined(__NR_socket) && !defined(__i386__) |
| 479 | { |
| 480 | long fd = syscall(__NR_socket, AF_INET, SOCK_STREAM, 0); |
| 481 | run("socket(AF_INET)", fd); |
| 482 | if (fd >= 0) close(static_cast<int>(fd)); |
| 483 | } |
| 484 | #endif |
| 485 | run("execve(NULL)", syscall(__NR_execve, nullptr, nullptr, nullptr)); |
| 486 | if (enforcing) return; // the rest kills the process when enforcing |
| 487 | run("perf_event_open(NULL)", syscall(__NR_perf_event_open, nullptr, 0, -1, -1, 0)); |
| 488 | #ifdef __NR_userfaultfd |
| 489 | { |
| 490 | long fd = syscall(__NR_userfaultfd, 0); |
| 491 | run("userfaultfd(0)", fd); |
| 492 | if (fd >= 0) close(static_cast<int>(fd)); |
| 493 | } |
| 494 | #endif |
| 495 | } |
| 496 |