1 /* $NetBSD: seccomp.c,v 1.1.1.5 2020/06/15 00:18:48 christos Exp $ */ 2 3 /* 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice immediately at the beginning of the file, without modification, 9 * this list of conditions, and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 /* 27 * libseccomp hooks. 28 */ 29 #include "file.h" 30 31 #ifndef lint 32 #if 0 33 FILE_RCSID("@(#)$File: seccomp.c,v 1.15 2020/05/30 23:56:26 christos Exp $") 34 #else 35 __RCSID("$NetBSD: seccomp.c,v 1.1.1.5 2020/06/15 00:18:48 christos Exp $"); 36 #endif 37 #endif /* lint */ 38 39 #if HAVE_LIBSECCOMP 40 #include <seccomp.h> /* libseccomp */ 41 #include <sys/prctl.h> /* prctl */ 42 #include <sys/ioctl.h> 43 #include <sys/socket.h> 44 #include <fcntl.h> 45 #include <stdlib.h> 46 #include <errno.h> 47 48 #define DENY_RULE(call) \ 49 do \ 50 if (seccomp_rule_add (ctx, SCMP_ACT_KILL, SCMP_SYS(call), 0) == -1) \ 51 goto out; \ 52 while (/*CONSTCOND*/0) 53 #define ALLOW_RULE(call) \ 54 do \ 55 if (seccomp_rule_add (ctx, SCMP_ACT_ALLOW, SCMP_SYS(call), 0) == -1) \ 56 goto out; \ 57 while (/*CONSTCOND*/0) 58 59 #define ALLOW_IOCTL_RULE(param) \ 60 do \ 61 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(ioctl), 1, \ 62 SCMP_CMP(1, SCMP_CMP_EQ, param)) == -1) \ 63 goto out; \ 64 while (/*CONSTCOND*/0) 65 66 static scmp_filter_ctx ctx; 67 68 int 69 enable_sandbox_basic(void) 70 { 71 72 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) 73 return -1; 74 75 if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1) 76 return -1; 77 78 // initialize the filter 79 ctx = seccomp_init(SCMP_ACT_ALLOW); 80 if (ctx == NULL) 81 return 1; 82 83 DENY_RULE(_sysctl); 84 DENY_RULE(acct); 85 DENY_RULE(add_key); 86 DENY_RULE(adjtimex); 87 DENY_RULE(chroot); 88 DENY_RULE(clock_adjtime); 89 DENY_RULE(create_module); 90 DENY_RULE(delete_module); 91 DENY_RULE(fanotify_init); 92 DENY_RULE(finit_module); 93 DENY_RULE(get_kernel_syms); 94 DENY_RULE(get_mempolicy); 95 DENY_RULE(init_module); 96 DENY_RULE(io_cancel); 97 DENY_RULE(io_destroy); 98 DENY_RULE(io_getevents); 99 DENY_RULE(io_setup); 100 DENY_RULE(io_submit); 101 DENY_RULE(ioperm); 102 DENY_RULE(iopl); 103 DENY_RULE(ioprio_set); 104 DENY_RULE(kcmp); 105 #ifdef __NR_kexec_file_load 106 DENY_RULE(kexec_file_load); 107 #endif 108 DENY_RULE(kexec_load); 109 DENY_RULE(keyctl); 110 DENY_RULE(lookup_dcookie); 111 DENY_RULE(mbind); 112 DENY_RULE(nfsservctl); 113 DENY_RULE(migrate_pages); 114 DENY_RULE(modify_ldt); 115 DENY_RULE(mount); 116 DENY_RULE(move_pages); 117 DENY_RULE(name_to_handle_at); 118 DENY_RULE(open_by_handle_at); 119 DENY_RULE(perf_event_open); 120 DENY_RULE(pivot_root); 121 DENY_RULE(process_vm_readv); 122 DENY_RULE(process_vm_writev); 123 DENY_RULE(ptrace); 124 DENY_RULE(reboot); 125 DENY_RULE(remap_file_pages); 126 DENY_RULE(request_key); 127 DENY_RULE(set_mempolicy); 128 DENY_RULE(swapoff); 129 DENY_RULE(swapon); 130 DENY_RULE(sysfs); 131 DENY_RULE(syslog); 132 DENY_RULE(tuxcall); 133 DENY_RULE(umount2); 134 DENY_RULE(uselib); 135 DENY_RULE(vmsplice); 136 137 // blocking dangerous syscalls that file should not need 138 DENY_RULE (execve); 139 DENY_RULE (socket); 140 // ... 141 142 143 // applying filter... 144 if (seccomp_load (ctx) == -1) 145 goto out; 146 // free ctx after the filter has been loaded into the kernel 147 seccomp_release(ctx); 148 return 0; 149 150 out: 151 seccomp_release(ctx); 152 return -1; 153 } 154 155 156 int 157 enable_sandbox_full(void) 158 { 159 160 // prevent child processes from getting more priv e.g. via setuid, 161 // capabilities, ... 162 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) 163 return -1; 164 165 if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1) 166 return -1; 167 168 // initialize the filter 169 ctx = seccomp_init(SCMP_ACT_KILL); 170 if (ctx == NULL) 171 return -1; 172 173 ALLOW_RULE(access); 174 ALLOW_RULE(brk); 175 ALLOW_RULE(close); 176 ALLOW_RULE(dup2); 177 ALLOW_RULE(exit); 178 ALLOW_RULE(exit_group); 179 ALLOW_RULE(fcntl); 180 ALLOW_RULE(fcntl64); 181 ALLOW_RULE(fstat); 182 ALLOW_RULE(fstat64); 183 #ifdef XZLIBSUPPORT 184 ALLOW_RULE(futex); 185 #endif 186 ALLOW_RULE(getdents); 187 #ifdef __NR_getdents64 188 ALLOW_RULE(getdents64); 189 #endif 190 #ifdef FIONREAD 191 // called in src/compress.c under sread 192 ALLOW_IOCTL_RULE(FIONREAD); 193 #endif 194 #ifdef TIOCGWINSZ 195 // musl libc may call ioctl TIOCGWINSZ on stdout 196 ALLOW_IOCTL_RULE(TIOCGWINSZ); 197 #endif 198 #ifdef TCGETS 199 // glibc may call ioctl TCGETS on stdout on physical terminal 200 ALLOW_IOCTL_RULE(TCGETS); 201 #endif 202 ALLOW_RULE(lseek); 203 ALLOW_RULE(_llseek); 204 ALLOW_RULE(lstat); 205 ALLOW_RULE(lstat64); 206 ALLOW_RULE(madvise); 207 ALLOW_RULE(mmap); 208 ALLOW_RULE(mmap2); 209 ALLOW_RULE(mprotect); 210 ALLOW_RULE(mremap); 211 ALLOW_RULE(munmap); 212 #ifdef __NR_newfstatat 213 ALLOW_RULE(newfstatat); 214 #endif 215 ALLOW_RULE(open); 216 ALLOW_RULE(openat); 217 ALLOW_RULE(pread64); 218 ALLOW_RULE(read); 219 ALLOW_RULE(readlink); 220 #ifdef __NR_readlinkat 221 ALLOW_RULE(readlinkat); 222 #endif 223 ALLOW_RULE(rt_sigaction); 224 ALLOW_RULE(rt_sigprocmask); 225 ALLOW_RULE(rt_sigreturn); 226 ALLOW_RULE(select); 227 ALLOW_RULE(stat); 228 ALLOW_RULE(stat64); 229 ALLOW_RULE(sysinfo); 230 ALLOW_RULE(umask); // Used in file_pipe2file() 231 ALLOW_RULE(getpid); // Used by glibc in file_pipe2file() 232 ALLOW_RULE(unlink); 233 ALLOW_RULE(write); 234 235 236 #if 0 237 // needed by valgrind 238 ALLOW_RULE(gettid); 239 ALLOW_RULE(rt_sigtimedwait); 240 #endif 241 242 #if 0 243 /* special restrictions for socket, only allow AF_UNIX/AF_LOCAL */ 244 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1, 245 SCMP_CMP(0, SCMP_CMP_EQ, AF_UNIX)) == -1) 246 goto out; 247 248 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1, 249 SCMP_CMP(0, SCMP_CMP_EQ, AF_LOCAL)) == -1) 250 goto out; 251 252 253 /* special restrictions for open, prevent opening files for writing */ 254 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1, 255 SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY | O_RDWR, 0)) == -1) 256 goto out; 257 258 if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1, 259 SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY, O_WRONLY)) == -1) 260 goto out; 261 262 if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1, 263 SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_RDWR, O_RDWR)) == -1) 264 goto out; 265 266 267 /* allow stderr */ 268 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1, 269 SCMP_CMP(0, SCMP_CMP_EQ, 2)) == -1) 270 goto out; 271 #endif 272 273 // applying filter... 274 if (seccomp_load(ctx) == -1) 275 goto out; 276 // free ctx after the filter has been loaded into the kernel 277 seccomp_release(ctx); 278 return 0; 279 280 out: 281 // something went wrong 282 seccomp_release(ctx); 283 return -1; 284 } 285 #endif 286