| 1 | /* |
| 2 | * Copyright (c) 2019-2022 Joris Vink <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #include <sys/param.h> |
| 18 | #include <sys/mman.h> |
| 19 | #include <sys/epoll.h> |
| 20 | #include <sys/ptrace.h> |
| 21 | #include <sys/prctl.h> |
| 22 | #include <sys/user.h> |
| 23 | #include <sys/syscall.h> |
| 24 | |
| 25 | #include <linux/ptrace.h> |
| 26 | #include <linux/seccomp.h> |
| 27 | #include <linux/filter.h> |
| 28 | #include <linux/audit.h> |
| 29 | |
| 30 | #include <stddef.h> |
| 31 | #include <sched.h> |
| 32 | |
| 33 | #include "kore.h"cxx . . $ `<file>/include/kore/kore.h`/ |
| 34 | #include "seccomp.h"cxx . . $ `<file>/include/kore/seccomp.h`/ |
| 35 | #include "platform.h"cxx . . $ `<file>/obj/platform.h`/ |
| 36 | |
| 37 | #if defined(KORE_USE_PYTHON) |
| 38 | #include "python_api.h" |
| 39 | #endif |
| 40 | |
| 41 | #if !defined(SECCOMP_KILL_POLICY) |
| 42 | #define SECCOMP_KILL_POLICYcxx . . $ `src/seccomp.c:42:9`! SECCOMP_RET_KILLcxx . . $ `<external>/2712060761133573813/seccomp.h:40:9`! |
| 43 | #endif |
| 44 | |
| 45 | /* |
| 46 | * The bare minimum to be able to run kore. These are added last and can |
| 47 | * be overwritten by a filter program that is added before hand. |
| 48 | */ |
| 49 | static struct sock_filtercxx . . $ sock_filter# filter_korecxx . . $ filter_kore.[] = { |
| 50 | /* Deny these, but with EACCESS instead of dying. */ |
| 51 | KORE_SYSCALL_DENYcxx . . $ `include/kore/seccomp.h:92:9`!(ioctl, EACCEScxx . . $ `<external>/8987895939074593727/errno-base.h:17:9`!), |
| 52 | |
| 53 | /* File related. */ |
| 54 | #if defined(SYS_opencxx . . $ `<external>/16530160117609833761/syscall.h:1167:10`!) |
| 55 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(open), |
| 56 | #endif |
| 57 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(read), |
| 58 | #if defined(SYS_statcxx . . $ `<external>/16530160117609833761/syscall.h:2311:10`!) |
| 59 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(stat), |
| 60 | #endif |
| 61 | #if defined(SYS_stat64) |
| 62 | KORE_SYSCALL_ALLOW(stat64), |
| 63 | #endif |
| 64 | #if defined(SYS_lstatcxx . . $ `<external>/16530160117609833761/syscall.h:927:10`!) |
| 65 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(lstat), |
| 66 | #endif |
| 67 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(fstat), |
| 68 | #if defined(SYS_fstat64) |
| 69 | KORE_SYSCALL_ALLOW(fstat64), |
| 70 | #endif |
| 71 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(write), |
| 72 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(fcntl), |
| 73 | #if defined(SYS_fcntl64) |
| 74 | KORE_SYSCALL_ALLOW(fcntl64), |
| 75 | #endif |
| 76 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(lseek), |
| 77 | #if defined(SYS__llseek) |
| 78 | KORE_SYSCALL_ALLOW(_llseek), |
| 79 | #endif |
| 80 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(close), |
| 81 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(openat), |
| 82 | #if defined(SYS_accesscxx . . $ `<external>/16530160117609833761/syscall.h:43:10`!) |
| 83 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(access), |
| 84 | #endif |
| 85 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(writev), |
| 86 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(getcwd), |
| 87 | #if defined(SYS_unlinkcxx . . $ `<external>/16530160117609833761/syscall.h:2535:10`!) |
| 88 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(unlink), |
| 89 | #endif |
| 90 | #if defined(SYS_readlinkcxx . . $ `<external>/16530160117609833761/syscall.h:1811:10`!) |
| 91 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(readlink), |
| 92 | #endif |
| 93 | #if defined(SYS_readlinkatcxx . . $ `<external>/16530160117609833761/syscall.h:1815:10`!) |
| 94 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(readlinkat), |
| 95 | #endif |
| 96 | |
| 97 | /* Process related. */ |
| 98 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(exit), |
| 99 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(kill), |
| 100 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(getpid), |
| 101 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(getuid), |
| 102 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(geteuid), |
| 103 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(exit_group), |
| 104 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(nanosleep), |
| 105 | #if defined(SYS_clock_nanosleepcxx . . $ `<external>/16530160117609833761/syscall.h:203:10`!) |
| 106 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(clock_nanosleep), |
| 107 | #endif |
| 108 | #if defined(SYS_sigreturn) |
| 109 | KORE_SYSCALL_ALLOW(sigreturn), |
| 110 | #endif |
| 111 | |
| 112 | /* Memory related. */ |
| 113 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(brk), |
| 114 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(munmap), |
| 115 | |
| 116 | /* Deny mmap/mprotect calls with PROT_EXEC/PROT_WRITE protection. */ |
| 117 | #if defined(SYS_mmapcxx . . $ `<external>/16530160117609833761/syscall.h:999:10`!) |
| 118 | KORE_SYSCALL_DENY_WITH_FLAGcxx . . $ `include/kore/seccomp.h:101:9`!(mmap, 2, PROT_EXECcxx . . $ `<external>/17226767460805440860/mman-linux.h:34:9`! | PROT_WRITEcxx . . $ `<external>/17226767460805440860/mman-linux.h:33:9`!, EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!), |
| 119 | #endif |
| 120 | #if defined(SYS_mmap2) |
| 121 | KORE_SYSCALL_DENY_WITH_FLAG(mmap2, 2, PROT_EXEC | PROT_WRITE, EINVAL), |
| 122 | #endif |
| 123 | KORE_SYSCALL_DENY_WITH_FLAGcxx . . $ `include/kore/seccomp.h:101:9`!(mprotect, 2, PROT_EXECcxx . . $ `<external>/17226767460805440860/mman-linux.h:34:9`!, EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!), |
| 124 | |
| 125 | #if defined(SYS_mmapcxx . . $ `<external>/16530160117609833761/syscall.h:999:10`!) |
| 126 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(mmap), |
| 127 | #endif |
| 128 | #if defined(SYS_mmap2) |
| 129 | KORE_SYSCALL_ALLOW(mmap2), |
| 130 | #endif |
| 131 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(madvise), |
| 132 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(mprotect), |
| 133 | |
| 134 | /* Net related. */ |
| 135 | #if defined(SYS_pollcxx . . $ `<external>/16530160117609833761/syscall.h:1703:10`!) |
| 136 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(poll), |
| 137 | #endif |
| 138 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(ppoll), |
| 139 | #if defined(SYS_send) |
| 140 | KORE_SYSCALL_ALLOW(send), |
| 141 | #endif |
| 142 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(sendto), |
| 143 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(accept), |
| 144 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(sendfile), |
| 145 | #if defined(SYS_recv) |
| 146 | KORE_SYSCALL_ALLOW(recv), |
| 147 | #endif |
| 148 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(recvfrom), |
| 149 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(epoll_ctl), |
| 150 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(setsockopt), |
| 151 | #if defined(SYS_epoll_waitcxx . . $ `<external>/16530160117609833761/syscall.h:303:10`!) |
| 152 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(epoll_wait), |
| 153 | #endif |
| 154 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(epoll_pwait), |
| 155 | |
| 156 | /* Signal related. */ |
| 157 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(sigaltstack), |
| 158 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(rt_sigreturn), |
| 159 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(rt_sigaction), |
| 160 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(rt_sigprocmask), |
| 161 | |
| 162 | /* "Other" without clear category. */ |
| 163 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(futex), |
| 164 | #if defined(SYS_clock_gettimecxx . . $ `<external>/16530160117609833761/syscall.h:195:10`!) |
| 165 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(clock_gettime), |
| 166 | #endif |
| 167 | |
| 168 | #if defined(__NR_getrandomcxx . . $ `<external>/1941951872834623984/unistd_64.h:322:9`!) |
| 169 | KORE_SYSCALL_ALLOWcxx . . $ `include/kore/seccomp.h:105:9`!(getrandom), |
| 170 | #endif |
| 171 | }; |
| 172 | |
| 173 | /* bpf program prologue. */ |
| 174 | static struct sock_filtercxx . . $ sock_filter# filter_prologuecxx . . $ filter_prologue.[] = { |
| 175 | /* Load arch member into accumulator (A) (arch is __u32). */ |
| 176 | KORE_BPF_LOADcxx . . $ `include/kore/seccomp.h:121:9`!(arch, 0), |
| 177 | |
| 178 | /* Compare accumulator against constant, if false jump over kill. */ |
| 179 | KORE_BPF_CMPcxx . . $ `include/kore/seccomp.h:129:9`!(SECCOMP_AUDIT_ARCHcxx . . $ `obj/platform.h:5:9`!, 1, 0), |
| 180 | KORE_BPF_RETcxx . . $ `include/kore/seccomp.h:125:9`!(SECCOMP_RET_KILLcxx . . $ `<external>/2712060761133573813/seccomp.h:40:9`!), |
| 181 | |
| 182 | /* Load the system call number into the accumulator. */ |
| 183 | KORE_BPF_LOADcxx . . $ `include/kore/seccomp.h:121:9`!(nr, 0), |
| 184 | }; |
| 185 | |
| 186 | /* bpf program epilogue. */ |
| 187 | static struct sock_filtercxx . . $ sock_filter# filter_epiloguecxx . . $ filter_epilogue.[] = { |
| 188 | /* Return hit if no system calls matched our list. */ |
| 189 | BPF_STMTcxx . . $ `<external>/14429533954890206851/filter.h:49:9`!(BPF_RETcxx . . $ `<external>/2534867171150301890/bpf_common.h:13:10`!+BPF_Kcxx . . $ `<external>/2534867171150301890/bpf_common.h:50:10`!, SECCOMP_KILL_POLICYcxx . . $ `src/seccomp.c:42:9`!) |
| 190 | }; |
| 191 | |
| 192 | static struct sock_filtercxx . . $ sock_filter# *seccomp_filter_updatecxx . . $ seccomp_filter_update(6fad38d1c0779d5d).(struct sock_filtercxx . . $ sock_filter# *, |
| 193 | const char *, size_t); |
| 194 | |
| 195 | #define filter_prologue_lencxx . . $ `src/seccomp.c:195:9`! KORE_FILTER_LENcxx . . $ `include/kore/seccomp.h:137:9`!(filter_prologue) |
| 196 | #define filter_epilogue_lencxx . . $ `src/seccomp.c:196:9`! KORE_FILTER_LENcxx . . $ `include/kore/seccomp.h:137:9`!(filter_epilogue) |
| 197 | |
| 198 | static void seccomp_register_violationcxx . . $ seccomp_register_violation(d4f767463ce0a6b3).(pid_tcxx . . $ pid_t#); |
| 199 | |
| 200 | struct filtercxx . . $ filter# { |
| 201 | char *namecxx . . $ filter#name.; |
| 202 | struct sock_filtercxx . . $ sock_filter# *progcxx . . $ filter#prog.; |
| 203 | size_t instructionscxx . . $ filter#instructions.; |
| 204 | TAILQ_ENTRYcxx . . $ `<external>/2546723208511035544/queue.h:382:9`!(filter) listcxx . . $ filter#list.; |
| 205 | }; |
| 206 | |
| 207 | static TAILQ_HEADcxx . . $ `<external>/2546723208511035544/queue.h:372:9`!(, filter) filterscxx . . $ filters.; |
| 208 | static struct filtercxx . . $ filter# *ufiltercxx . . $ ufilter. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 209 | |
| 210 | /* |
| 211 | * If enabled will instruct the parent process to ptrace its children and |
| 212 | * log any seccomp SECCOMP_RET_TRACE rule. |
| 213 | */ |
| 214 | int kore_seccomp_tracingcxx . . $ kore_seccomp_tracing. = 0; |
| 215 | |
| 216 | void |
| 217 | kore_seccomp_initcxx . . $ kore_seccomp_init(49f6e7a06ebc5aa8).(void) |
| 218 | { |
| 219 | TAILQ_INITcxx . . $ `<external>/2546723208511035544/queue.h:387:9`!(&filters); |
| 220 | } |
| 221 | |
| 222 | void |
| 223 | kore_seccomp_dropcxx . . $ kore_seccomp_drop(49f6e7a06ebc5aa8).(void) |
| 224 | { |
| 225 | struct filtercxx . . $ filter# *filter; |
| 226 | |
| 227 | while ((filter = TAILQ_FIRSTcxx . . $ `<external>/2546723208511035544/queue.h:458:9`!(&filters)) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 228 | if (!kore_quietcxx . . $ kore_quiet.) { |
| 229 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, |
| 230 | "seccomp filter '%s' dropped", filter->namecxx . . $ filter#name.); |
| 231 | } |
| 232 | TAILQ_REMOVEcxx . . $ `<external>/2546723208511035544/queue.h:426:9`!(&filters, filter, list); |
| 233 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(filter->namecxx . . $ filter#name.); |
| 234 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(filter); |
| 235 | } |
| 236 | |
| 237 | TAILQ_INITcxx . . $ `<external>/2546723208511035544/queue.h:387:9`!(&filters); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | kore_seccomp_enablecxx . . $ kore_seccomp_enable(49f6e7a06ebc5aa8).(void) |
| 242 | { |
| 243 | struct sock_filtercxx . . $ sock_filter# *sf; |
| 244 | struct sock_fprogcxx . . $ sock_fprog# prog; |
| 245 | struct kore_runtime_callcxx . . $ kore_runtime_call# *rcall; |
| 246 | struct filtercxx . . $ filter# *filter; |
| 247 | size_t prog_len, off, i; |
| 248 | |
| 249 | /* |
| 250 | * If kore_seccomp_tracing is turned on, set the default policy to |
| 251 | * SECCOMP_RET_TRACE so we can log the system calls. |
| 252 | */ |
| 253 | if (kore_seccomp_tracingcxx . . $ kore_seccomp_tracing.) { |
| 254 | filter_epiloguecxx . . $ filter_epilogue.[0].kcxx . . $ sock_filter#k. = SECCOMP_RET_TRACEcxx . . $ `<external>/2712060761133573813/seccomp.h:44:9`!; |
| 255 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!, "seccomp tracing enabled"); |
| 256 | } |
| 257 | |
| 258 | #if defined(KORE_USE_PYTHON) |
| 259 | ufilter = TAILQ_FIRST(&filters); |
| 260 | kore_python_seccomp_hook("koreapp.seccomp"); |
| 261 | ufilter = NULL; |
| 262 | #endif |
| 263 | |
| 264 | /* Allow application to add its own filters. */ |
| 265 | if ((rcall = kore_runtime_getcallcxx . . $ kore_runtime_getcall(b514b5951ac4d62f).("kore_seccomp_hook")) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 266 | ufiltercxx . . $ ufilter. = TAILQ_FIRSTcxx . . $ `<external>/2546723208511035544/queue.h:458:9`!(&filters); |
| 267 | kore_runtime_executecxx . . $ kore_runtime_execute(5bf3e738821b3e09).(rcall); |
| 268 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(rcall); |
| 269 | ufiltercxx . . $ ufilter. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 270 | } |
| 271 | |
| 272 | if (workercxx . . $ worker.->idcxx . . $ kore_worker#id. != KORE_WORKER_KEYMGRcxx . . $ `include/kore/kore.h:652:9`!) { |
| 273 | /* Add worker required syscalls. */ |
| 274 | kore_seccomp_filtercxx . . $ kore_seccomp_filter(60a8714db3c5c2a).("worker", filter_korecxx . . $ filter_kore., |
| 275 | KORE_FILTER_LENcxx . . $ `include/kore/seccomp.h:137:9`!(filter_kore)); |
| 276 | } |
| 277 | |
| 278 | /* Start with the prologue. */ |
| 279 | prog_len = filter_prologue_lencxx . . $ `src/seccomp.c:195:9`!; |
| 280 | |
| 281 | /* Now account for all enabled filters. */ |
| 282 | TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(filter, &filters, list) |
| 283 | prog_len += filter->instructionscxx . . $ filter#instructions.; |
| 284 | |
| 285 | /* Finally add the epilogue. */ |
| 286 | prog_len += filter_epilogue_lencxx . . $ `src/seccomp.c:196:9`!; |
| 287 | |
| 288 | /* Build the entire bpf program now. */ |
| 289 | if ((sf = calloccxx . . $ calloc(63a7bf2941bfcd7a).(prog_len, sizeof(*sf))) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 290 | fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("calloc"); |
| 291 | |
| 292 | off = 0; |
| 293 | for (i = 0; i < filter_prologue_lencxx . . $ `src/seccomp.c:195:9`!; i++) |
| 294 | sf[off++] = filter_prologuecxx . . $ filter_prologue.[i]; |
| 295 | |
| 296 | TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(filter, &filters, list) { |
| 297 | for (i = 0; i < filter->instructionscxx . . $ filter#instructions.; i++) |
| 298 | sf[off++] = filter->progcxx . . $ filter#prog.[i]; |
| 299 | } |
| 300 | |
| 301 | for (i = 0; i < filter_epilogue_lencxx . . $ `src/seccomp.c:196:9`!; i++) |
| 302 | sf[off++] = filter_epiloguecxx . . $ filter_epilogue.[i]; |
| 303 | |
| 304 | /* Lock and load it. */ |
| 305 | if (prctlcxx . . $ prctl(e5daca538bd362ba).(PR_SET_NO_NEW_PRIVScxx . . $ `<external>/9872755456582759113/prctl.h:175:9`!, 1, 0, 0, 0) == -1) |
| 306 | fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("prctl: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 307 | |
| 308 | prog.filtercxx . . $ sock_fprog#filter. = sf; |
| 309 | prog.lencxx . . $ sock_fprog#len. = prog_len; |
| 310 | |
| 311 | if (prctlcxx . . $ prctl(e5daca538bd362ba).(PR_SET_SECCOMPcxx . . $ `<external>/9872755456582759113/prctl.h:68:9`!, SECCOMP_MODE_FILTERcxx . . $ `<external>/2712060761133573813/seccomp.h:12:9`!, &prog) == -1) |
| 312 | fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("prctl: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 313 | |
| 314 | #if defined(KORE_USE_PYTHON) |
| 315 | kore_python_seccomp_cleanup(); |
| 316 | #endif |
| 317 | } |
| 318 | |
| 319 | int |
| 320 | kore_seccomp_filtercxx . . $ kore_seccomp_filter(60a8714db3c5c2a).(const char *name, void *prog, size_t len) |
| 321 | { |
| 322 | struct filtercxx . . $ filter# *filter; |
| 323 | |
| 324 | TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(filter, &filters, list) { |
| 325 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(filter->namecxx . . $ filter#name., name)) |
| 326 | return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!); |
| 327 | } |
| 328 | |
| 329 | filter = kore_calloccxx . . $ kore_calloc(63a7bf2941bfcd7a).(1, sizeof(*filter)); |
| 330 | |
| 331 | filter->progcxx . . $ filter#prog. = prog; |
| 332 | filter->instructionscxx . . $ filter#instructions. = len; |
| 333 | filter->namecxx . . $ filter#name. = kore_strdupcxx . . $ kore_strdup(a8b8e32347182212).(name); |
| 334 | |
| 335 | if (ufiltercxx . . $ ufilter.) { |
| 336 | TAILQ_INSERT_BEFOREcxx . . $ `<external>/2546723208511035544/queue.h:419:9`!(ufilter, filter, list); |
| 337 | } else { |
| 338 | TAILQ_INSERT_TAILcxx . . $ `<external>/2546723208511035544/queue.h:402:9`!(&filters, filter, list); |
| 339 | } |
| 340 | |
| 341 | return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!); |
| 342 | } |
| 343 | |
| 344 | void |
| 345 | kore_seccomp_tracemecxx . . $ kore_seccomp_traceme(49f6e7a06ebc5aa8).(void) |
| 346 | { |
| 347 | if (kore_seccomp_tracingcxx . . $ kore_seccomp_tracing. == 0) |
| 348 | return; |
| 349 | |
| 350 | if (ptracecxx . . $ ptrace(aece84b69c03923e).(PTRACE_TRACEMEcxx . . $ `<external>/946871669964031177/ptrace.h:11:9`!, 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) == -1) |
| 351 | fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("ptrace: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 352 | if (killcxx . . $ kill(9b79fb6aee4c0440).(workercxx . . $ worker.->pidcxx . . $ kore_worker#pid., SIGSTOPcxx . . $ `<external>/1457673850468744829/signum-arch.h:38:9`!) == -1) |
| 353 | fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("kill: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 354 | } |
| 355 | |
| 356 | int |
| 357 | kore_seccomp_tracecxx . . $ kore_seccomp_trace(9b79fb6aee4c0440).(pid_tcxx . . $ pid_t# pid, int status) |
| 358 | { |
| 359 | int evt; |
| 360 | |
| 361 | if (kore_seccomp_tracingcxx . . $ kore_seccomp_tracing. == 0) |
| 362 | return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!); |
| 363 | |
| 364 | if (WIFSTOPPEDcxx . . $ `<external>/10696550936717951914/stdlib.h:49:10`!(status) && WSTOPSIGcxx . . $ `<external>/10696550936717951914/stdlib.h:46:10`!(status) == SIGSTOPcxx . . $ `<external>/1457673850468744829/signum-arch.h:38:9`!) { |
| 365 | if (ptracecxx . . $ ptrace(aece84b69c03923e).(PTRACE_SETOPTIONScxx . . $ `<external>/946871669964031177/ptrace.h:28:9`!, pid, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, |
| 366 | PTRACE_O_TRACESECCOMPcxx . . $ `<external>/946871669964031177/ptrace.h:179:9`! | PTRACE_O_TRACECLONEcxx . . $ `<external>/946871669964031177/ptrace.h:175:9`! | |
| 367 | PTRACE_O_TRACEFORKcxx . . $ `<external>/946871669964031177/ptrace.h:173:9`!) == -1) |
| 368 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("ptrace: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 369 | if (ptracecxx . . $ ptrace(aece84b69c03923e).(PTRACE_CONTcxx . . $ `<external>/946871669964031177/ptrace.h:18:9`!, pid, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) == -1) |
| 370 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("ptrace: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 371 | return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!); |
| 372 | } |
| 373 | |
| 374 | if (WIFSTOPPEDcxx . . $ `<external>/10696550936717951914/stdlib.h:49:10`!(status) && WSTOPSIGcxx . . $ `<external>/10696550936717951914/stdlib.h:46:10`!(status) == SIGTRAPcxx . . $ `<external>/11773118241360591791/signum-generic.h:58:9`!) { |
| 375 | evt = status >> 8; |
| 376 | if (evt == (SIGTRAPcxx . . $ `<external>/11773118241360591791/signum-generic.h:58:9`! | (PTRACE_EVENT_SECCOMPcxx . . $ `<external>/946871669964031177/ptrace.h:167:9`! << 8))) |
| 377 | seccomp_register_violationcxx . . $ seccomp_register_violation(d4f767463ce0a6b3).(pid); |
| 378 | if (ptracecxx . . $ ptrace(aece84b69c03923e).(PTRACE_CONTcxx . . $ `<external>/946871669964031177/ptrace.h:18:9`!, pid, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) == -1) |
| 379 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("ptrace: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 380 | return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!); |
| 381 | } |
| 382 | |
| 383 | if (WIFSTOPPEDcxx . . $ `<external>/10696550936717951914/stdlib.h:49:10`!(status)) { |
| 384 | if (ptracecxx . . $ ptrace(aece84b69c03923e).(PTRACE_CONTcxx . . $ `<external>/946871669964031177/ptrace.h:18:9`!, pid, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, WSTOPSIGcxx . . $ `<external>/10696550936717951914/stdlib.h:46:10`!(status)) == -1) |
| 385 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("ptrace: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 386 | return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!); |
| 387 | } |
| 388 | |
| 389 | return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!); |
| 390 | } |
| 391 | |
| 392 | int |
| 393 | kore_seccomp_syscall_resolvecxx . . $ kore_seccomp_syscall_resolve(5c4306d8553b3696).(const char *name) |
| 394 | { |
| 395 | int i; |
| 396 | |
| 397 | for (i = 0; kore_syscall_mapcxx . . $ kore_syscall_map.[i].namecxx . . $ $anonymous_type_14aa40a37ca5b4b_0#name. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; i++) { |
| 398 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(name, kore_syscall_mapcxx . . $ kore_syscall_map.[i].namecxx . . $ $anonymous_type_14aa40a37ca5b4b_0#name.)) |
| 399 | return (kore_syscall_mapcxx . . $ kore_syscall_map.[i].nrcxx . . $ $anonymous_type_14aa40a37ca5b4b_0#nr.); |
| 400 | } |
| 401 | |
| 402 | return (-1); |
| 403 | } |
| 404 | |
| 405 | const char * |
| 406 | kore_seccomp_syscall_namecxx . . $ kore_seccomp_syscall_name(595caefabb192cea).(long sysnr) |
| 407 | { |
| 408 | int i; |
| 409 | |
| 410 | for (i = 0; kore_syscall_mapcxx . . $ kore_syscall_map.[i].namecxx . . $ $anonymous_type_14aa40a37ca5b4b_0#name. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; i++) { |
| 411 | if (kore_syscall_mapcxx . . $ kore_syscall_map.[i].nrcxx . . $ $anonymous_type_14aa40a37ca5b4b_0#nr. == sysnr) |
| 412 | return (kore_syscall_mapcxx . . $ kore_syscall_map.[i].namecxx . . $ $anonymous_type_14aa40a37ca5b4b_0#name.); |
| 413 | } |
| 414 | |
| 415 | return ("unknown"); |
| 416 | } |
| 417 | |
| 418 | struct sock_filtercxx . . $ sock_filter# * |
| 419 | kore_seccomp_syscall_filtercxx . . $ kore_seccomp_syscall_filter(9b5d45889321fd5).(const char *name, int action) |
| 420 | { |
| 421 | struct sock_filtercxx . . $ sock_filter# filter[] = { |
| 422 | KORE_SYSCALL_FILTERcxx . . $ `include/kore/seccomp.h:39:9`!(exit, action), |
| 423 | KORE_BPF_GUARDcxx . . $ `include/kore/seccomp.h:140:9`! |
| 424 | }; |
| 425 | |
| 426 | return (seccomp_filter_updatecxx . . $ seccomp_filter_update(6fad38d1c0779d5d).(filter, name, KORE_FILTER_LENcxx . . $ `include/kore/seccomp.h:137:9`!(filter))); |
| 427 | } |
| 428 | |
| 429 | struct sock_filtercxx . . $ sock_filter# * |
| 430 | kore_seccomp_syscall_argcxx . . $ kore_seccomp_syscall_arg(6dde0091c3e53423).(const char *name, int action, int arg, int value) |
| 431 | { |
| 432 | struct sock_filtercxx . . $ sock_filter# filter[] = { |
| 433 | KORE_SYSCALL_ARGcxx . . $ `include/kore/seccomp.h:51:9`!(exit, arg, value, action), |
| 434 | KORE_BPF_GUARDcxx . . $ `include/kore/seccomp.h:140:9`! |
| 435 | }; |
| 436 | |
| 437 | return (seccomp_filter_updatecxx . . $ seccomp_filter_update(6fad38d1c0779d5d).(filter, name, KORE_FILTER_LENcxx . . $ `include/kore/seccomp.h:137:9`!(filter))); |
| 438 | } |
| 439 | |
| 440 | struct sock_filtercxx . . $ sock_filter# * |
| 441 | kore_seccomp_syscall_maskcxx . . $ kore_seccomp_syscall_mask(6dde0091c3e53423).(const char *name, int action, int arg, int value) |
| 442 | { |
| 443 | struct sock_filtercxx . . $ sock_filter# filter[] = { |
| 444 | KORE_SYSCALL_MASKcxx . . $ `include/kore/seccomp.h:65:9`!(exit, arg, value, action), |
| 445 | KORE_BPF_GUARDcxx . . $ `include/kore/seccomp.h:140:9`! |
| 446 | }; |
| 447 | |
| 448 | return (seccomp_filter_updatecxx . . $ seccomp_filter_update(6fad38d1c0779d5d).(filter, name, KORE_FILTER_LENcxx . . $ `include/kore/seccomp.h:137:9`!(filter))); |
| 449 | } |
| 450 | |
| 451 | struct sock_filtercxx . . $ sock_filter# * |
| 452 | kore_seccomp_syscall_flagcxx . . $ kore_seccomp_syscall_flag(6dde0091c3e53423).(const char *name, int action, int arg, int value) |
| 453 | { |
| 454 | struct sock_filtercxx . . $ sock_filter# filter[] = { |
| 455 | KORE_SYSCALL_WITH_FLAGcxx . . $ `include/kore/seccomp.h:80:9`!(exit, arg, value, action), |
| 456 | KORE_BPF_GUARDcxx . . $ `include/kore/seccomp.h:140:9`! |
| 457 | }; |
| 458 | |
| 459 | return (seccomp_filter_updatecxx . . $ seccomp_filter_update(6fad38d1c0779d5d).(filter, name, KORE_FILTER_LENcxx . . $ `include/kore/seccomp.h:137:9`!(filter))); |
| 460 | } |
| 461 | |
| 462 | static void |
| 463 | seccomp_register_violationcxx . . $ seccomp_register_violation(d4f767463ce0a6b3).(pid_tcxx . . $ pid_t# pid) |
| 464 | { |
| 465 | int idx; |
| 466 | struct kore_workercxx . . $ kore_worker# *kw; |
| 467 | struct ioveccxx . . $ iovec# iov; |
| 468 | #if defined(__arm__) |
| 469 | struct pt_regs regs; |
| 470 | #else |
| 471 | struct user_regs_structcxx . . $ user_regs_struct# regs; |
| 472 | #endif |
| 473 | long sysnr; |
| 474 | const char *name; |
| 475 | |
| 476 | iov.iov_basecxx . . $ iovec#iov_base. = ®s; |
| 477 | iov.iov_lencxx . . $ iovec#iov_len. = sizeof(regs); |
| 478 | |
| 479 | if (ptracecxx . . $ ptrace(aece84b69c03923e).(PTRACE_GETREGSETcxx . . $ `<external>/946871669964031177/ptrace.h:50:9`!, pid, 1, &iov) == -1) |
| 480 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("ptrace: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 481 | |
| 482 | #if SECCOMP_AUDIT_ARCHcxx . . $ `obj/platform.h:5:9`! == AUDIT_ARCH_X86_64cxx . . $ `<external>/15835500652152965316/audit.h:448:9`! |
| 483 | sysnr = regs.orig_raxcxx . . $ user_regs_struct#orig_rax.; |
| 484 | #elif SECCOMP_AUDIT_ARCH == AUDIT_ARCH_AARCH64 |
| 485 | sysnr = regs.regs[8]; |
| 486 | #elif SECCOMP_AUDIT_ARCH == AUDIT_ARCH_ARM |
| 487 | sysnr = regs.uregs[7]; |
| 488 | #else |
| 489 | #error "platform not supported" |
| 490 | #endif |
| 491 | |
| 492 | name = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 493 | for (idx = 0; idx < worker_countcxx . . $ worker_count.; idx++) { |
| 494 | kw = kore_worker_datacxx . . $ kore_worker_data(a59abc2c9e0c50f4).(idx); |
| 495 | if (kw->pidcxx . . $ kore_worker#pid. == pid) { |
| 496 | name = kore_worker_namecxx . . $ kore_worker_name(f7ae7e56280f18ae).(kw->idcxx . . $ kore_worker#id.); |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | if (name == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 502 | name = "<child>"; |
| 503 | |
| 504 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "seccomp violation, %s pid=%d, syscall=%ld:%s", |
| 505 | name, pid, sysnr, kore_seccomp_syscall_namecxx . . $ kore_seccomp_syscall_name(595caefabb192cea).(sysnr)); |
| 506 | } |
| 507 | |
| 508 | static struct sock_filtercxx . . $ sock_filter# * |
| 509 | seccomp_filter_updatecxx . . $ seccomp_filter_update(6fad38d1c0779d5d).(struct sock_filtercxx . . $ sock_filter# *filter, const char *name, size_t elm) |
| 510 | { |
| 511 | int nr; |
| 512 | struct sock_filtercxx . . $ sock_filter# *result; |
| 513 | |
| 514 | if ((nr = kore_seccomp_syscall_resolvecxx . . $ kore_seccomp_syscall_resolve(5c4306d8553b3696).(name)) == -1) |
| 515 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 516 | |
| 517 | result = kore_calloccxx . . $ kore_calloc(63a7bf2941bfcd7a).(elm, sizeof(struct sock_filtercxx . . $ sock_filter#)); |
| 518 | memcpycxx . . $ memcpy(cc817cb175f282d).(result, filter, elm * sizeof(struct sock_filtercxx . . $ sock_filter#)); |
| 519 | |
| 520 | /* Update the syscall number to the one specified. */ |
| 521 | result[0].kcxx . . $ sock_filter#k. = nr; |
| 522 | |
| 523 | return (result); |
| 524 | } |