Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 1 | /* |
| 2 | * QEMU seccomp mode 2 support with libseccomp |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2012 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Eduardo Otubo <eotubo@br.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 10 | * the COPYING file in the top-level directory. |
| 11 | * |
| 12 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 13 | * GNU GPL, version 2 or (at your option) any later version. |
| 14 | */ |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 15 | #include "qemu/osdep.h" |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 16 | #include <seccomp.h> |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 17 | #include "sysemu/seccomp.h" |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 18 | |
James Hogan | 81bed73 | 2016-04-08 14:16:33 +0100 | [diff] [blame] | 19 | /* For some architectures (notably ARM) cacheflush is not supported until |
| 20 | * libseccomp 2.2.3, but configure enforces that we are using a more recent |
| 21 | * version on those hosts, so it is OK for this check to be less strict. |
| 22 | */ |
Andrew Jones | 47d2067 | 2015-11-02 23:53:26 +0100 | [diff] [blame] | 23 | #if SCMP_VER_MAJOR >= 3 |
| 24 | #define HAVE_CACHEFLUSH |
James Hogan | 81bed73 | 2016-04-08 14:16:33 +0100 | [diff] [blame] | 25 | #elif SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 2 |
Andrew Jones | 47d2067 | 2015-11-02 23:53:26 +0100 | [diff] [blame] | 26 | #define HAVE_CACHEFLUSH |
| 27 | #endif |
| 28 | |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 29 | struct QemuSeccompSyscall { |
| 30 | int32_t num; |
| 31 | uint8_t priority; |
| 32 | }; |
| 33 | |
| 34 | static const struct QemuSeccompSyscall seccomp_whitelist[] = { |
| 35 | { SCMP_SYS(timer_settime), 255 }, |
| 36 | { SCMP_SYS(timer_gettime), 254 }, |
| 37 | { SCMP_SYS(futex), 253 }, |
| 38 | { SCMP_SYS(select), 252 }, |
| 39 | { SCMP_SYS(recvfrom), 251 }, |
| 40 | { SCMP_SYS(sendto), 250 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 41 | { SCMP_SYS(socketcall), 250 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 42 | { SCMP_SYS(read), 249 }, |
Paul Moore | 94113bd | 2013-07-15 15:32:01 -0400 | [diff] [blame] | 43 | { SCMP_SYS(io_submit), 249 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 44 | { SCMP_SYS(brk), 248 }, |
| 45 | { SCMP_SYS(clone), 247 }, |
| 46 | { SCMP_SYS(mmap), 247 }, |
| 47 | { SCMP_SYS(mprotect), 246 }, |
| 48 | { SCMP_SYS(execve), 245 }, |
| 49 | { SCMP_SYS(open), 245 }, |
| 50 | { SCMP_SYS(ioctl), 245 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 51 | { SCMP_SYS(socket), 245 }, |
| 52 | { SCMP_SYS(setsockopt), 245 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 53 | { SCMP_SYS(recvmsg), 245 }, |
| 54 | { SCMP_SYS(sendmsg), 245 }, |
| 55 | { SCMP_SYS(accept), 245 }, |
| 56 | { SCMP_SYS(connect), 245 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 57 | { SCMP_SYS(socketpair), 245 }, |
| 58 | { SCMP_SYS(bind), 245 }, |
| 59 | { SCMP_SYS(listen), 245 }, |
| 60 | { SCMP_SYS(semget), 245 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 61 | { SCMP_SYS(ipc), 245 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 62 | { SCMP_SYS(gettimeofday), 245 }, |
| 63 | { SCMP_SYS(readlink), 245 }, |
| 64 | { SCMP_SYS(access), 245 }, |
| 65 | { SCMP_SYS(prctl), 245 }, |
| 66 | { SCMP_SYS(signalfd), 245 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 67 | { SCMP_SYS(getrlimit), 245 }, |
| 68 | { SCMP_SYS(set_tid_address), 245 }, |
| 69 | { SCMP_SYS(statfs), 245 }, |
| 70 | { SCMP_SYS(unlink), 245 }, |
| 71 | { SCMP_SYS(wait4), 245 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 72 | { SCMP_SYS(fcntl64), 245 }, |
| 73 | { SCMP_SYS(fstat64), 245 }, |
| 74 | { SCMP_SYS(stat64), 245 }, |
| 75 | { SCMP_SYS(getgid32), 245 }, |
| 76 | { SCMP_SYS(getegid32), 245 }, |
| 77 | { SCMP_SYS(getuid32), 245 }, |
| 78 | { SCMP_SYS(geteuid32), 245 }, |
| 79 | { SCMP_SYS(sigreturn), 245 }, |
| 80 | { SCMP_SYS(_newselect), 245 }, |
| 81 | { SCMP_SYS(_llseek), 245 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 82 | { SCMP_SYS(mmap2), 245 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 83 | { SCMP_SYS(sigprocmask), 245 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 84 | { SCMP_SYS(sched_getparam), 245 }, |
| 85 | { SCMP_SYS(sched_getscheduler), 245 }, |
| 86 | { SCMP_SYS(fstat), 245 }, |
| 87 | { SCMP_SYS(clock_getres), 245 }, |
| 88 | { SCMP_SYS(sched_get_priority_min), 245 }, |
| 89 | { SCMP_SYS(sched_get_priority_max), 245 }, |
| 90 | { SCMP_SYS(stat), 245 }, |
| 91 | { SCMP_SYS(uname), 245 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 92 | { SCMP_SYS(eventfd2), 245 }, |
Paul Moore | fd21faa | 2013-05-29 16:30:01 -0400 | [diff] [blame] | 93 | { SCMP_SYS(io_getevents), 245 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 94 | { SCMP_SYS(dup), 245 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 95 | { SCMP_SYS(dup2), 245 }, |
| 96 | { SCMP_SYS(dup3), 245 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 97 | { SCMP_SYS(gettid), 245 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 98 | { SCMP_SYS(getgid), 245 }, |
| 99 | { SCMP_SYS(getegid), 245 }, |
| 100 | { SCMP_SYS(getuid), 245 }, |
| 101 | { SCMP_SYS(geteuid), 245 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 102 | { SCMP_SYS(timer_create), 245 }, |
Eduardo Otubo | c236f45 | 2013-09-24 14:50:44 -0300 | [diff] [blame] | 103 | { SCMP_SYS(times), 245 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 104 | { SCMP_SYS(exit), 245 }, |
| 105 | { SCMP_SYS(clock_gettime), 245 }, |
| 106 | { SCMP_SYS(time), 245 }, |
| 107 | { SCMP_SYS(restart_syscall), 245 }, |
| 108 | { SCMP_SYS(pwrite64), 245 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 109 | { SCMP_SYS(nanosleep), 245 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 110 | { SCMP_SYS(chown), 245 }, |
| 111 | { SCMP_SYS(openat), 245 }, |
| 112 | { SCMP_SYS(getdents), 245 }, |
| 113 | { SCMP_SYS(timer_delete), 245 }, |
| 114 | { SCMP_SYS(exit_group), 245 }, |
| 115 | { SCMP_SYS(rt_sigreturn), 245 }, |
| 116 | { SCMP_SYS(sync), 245 }, |
| 117 | { SCMP_SYS(pread64), 245 }, |
| 118 | { SCMP_SYS(madvise), 245 }, |
| 119 | { SCMP_SYS(set_robust_list), 245 }, |
| 120 | { SCMP_SYS(lseek), 245 }, |
| 121 | { SCMP_SYS(pselect6), 245 }, |
| 122 | { SCMP_SYS(fork), 245 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 123 | { SCMP_SYS(rt_sigprocmask), 245 }, |
| 124 | { SCMP_SYS(write), 244 }, |
| 125 | { SCMP_SYS(fcntl), 243 }, |
| 126 | { SCMP_SYS(tgkill), 242 }, |
Paul Moore | e9eecb5 | 2013-11-21 10:40:15 -0500 | [diff] [blame] | 127 | { SCMP_SYS(kill), 242 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 128 | { SCMP_SYS(rt_sigaction), 242 }, |
| 129 | { SCMP_SYS(pipe2), 242 }, |
| 130 | { SCMP_SYS(munmap), 242 }, |
| 131 | { SCMP_SYS(mremap), 242 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 132 | { SCMP_SYS(fdatasync), 242 }, |
| 133 | { SCMP_SYS(close), 242 }, |
| 134 | { SCMP_SYS(rt_sigpending), 242 }, |
| 135 | { SCMP_SYS(rt_sigtimedwait), 242 }, |
| 136 | { SCMP_SYS(readv), 242 }, |
| 137 | { SCMP_SYS(writev), 242 }, |
| 138 | { SCMP_SYS(preadv), 242 }, |
| 139 | { SCMP_SYS(pwritev), 242 }, |
| 140 | { SCMP_SYS(setrlimit), 242 }, |
| 141 | { SCMP_SYS(ftruncate), 242 }, |
| 142 | { SCMP_SYS(lstat), 242 }, |
| 143 | { SCMP_SYS(pipe), 242 }, |
| 144 | { SCMP_SYS(umask), 242 }, |
| 145 | { SCMP_SYS(chdir), 242 }, |
| 146 | { SCMP_SYS(setitimer), 242 }, |
| 147 | { SCMP_SYS(setsid), 242 }, |
| 148 | { SCMP_SYS(poll), 242 }, |
| 149 | { SCMP_SYS(epoll_create), 242 }, |
| 150 | { SCMP_SYS(epoll_ctl), 242 }, |
| 151 | { SCMP_SYS(epoll_wait), 242 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 152 | { SCMP_SYS(waitpid), 242 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 153 | { SCMP_SYS(getsockname), 242 }, |
| 154 | { SCMP_SYS(getpeername), 242 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 155 | { SCMP_SYS(accept4), 242 }, |
Felix Geyer | 8439761 | 2014-01-30 21:28:17 +0100 | [diff] [blame] | 156 | { SCMP_SYS(timerfd_settime), 242 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 157 | { SCMP_SYS(newfstatat), 241 }, |
| 158 | { SCMP_SYS(shutdown), 241 }, |
| 159 | { SCMP_SYS(getsockopt), 241 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 160 | { SCMP_SYS(semop), 241 }, |
| 161 | { SCMP_SYS(semtimedop), 241 }, |
| 162 | { SCMP_SYS(epoll_ctl_old), 241 }, |
| 163 | { SCMP_SYS(epoll_wait_old), 241 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 164 | { SCMP_SYS(epoll_pwait), 241 }, |
| 165 | { SCMP_SYS(epoll_create1), 241 }, |
| 166 | { SCMP_SYS(ppoll), 241 }, |
| 167 | { SCMP_SYS(creat), 241 }, |
| 168 | { SCMP_SYS(link), 241 }, |
| 169 | { SCMP_SYS(getpid), 241 }, |
| 170 | { SCMP_SYS(getppid), 241 }, |
| 171 | { SCMP_SYS(getpgrp), 241 }, |
| 172 | { SCMP_SYS(getpgid), 241 }, |
| 173 | { SCMP_SYS(getsid), 241 }, |
| 174 | { SCMP_SYS(getdents64), 241 }, |
| 175 | { SCMP_SYS(getresuid), 241 }, |
| 176 | { SCMP_SYS(getresgid), 241 }, |
| 177 | { SCMP_SYS(getgroups), 241 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 178 | { SCMP_SYS(getresuid32), 241 }, |
| 179 | { SCMP_SYS(getresgid32), 241 }, |
| 180 | { SCMP_SYS(getgroups32), 241 }, |
| 181 | { SCMP_SYS(signal), 241 }, |
| 182 | { SCMP_SYS(sigaction), 241 }, |
| 183 | { SCMP_SYS(sigsuspend), 241 }, |
| 184 | { SCMP_SYS(sigpending), 241 }, |
| 185 | { SCMP_SYS(truncate64), 241 }, |
| 186 | { SCMP_SYS(ftruncate64), 241 }, |
| 187 | { SCMP_SYS(fchown32), 241 }, |
| 188 | { SCMP_SYS(chown32), 241 }, |
| 189 | { SCMP_SYS(lchown32), 241 }, |
| 190 | { SCMP_SYS(statfs64), 241 }, |
| 191 | { SCMP_SYS(fstatfs64), 241 }, |
| 192 | { SCMP_SYS(fstatat64), 241 }, |
| 193 | { SCMP_SYS(lstat64), 241 }, |
| 194 | { SCMP_SYS(sendfile64), 241 }, |
| 195 | { SCMP_SYS(ugetrlimit), 241 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 196 | { SCMP_SYS(alarm), 241 }, |
| 197 | { SCMP_SYS(rt_sigsuspend), 241 }, |
| 198 | { SCMP_SYS(rt_sigqueueinfo), 241 }, |
| 199 | { SCMP_SYS(rt_tgsigqueueinfo), 241 }, |
| 200 | { SCMP_SYS(sigaltstack), 241 }, |
| 201 | { SCMP_SYS(signalfd4), 241 }, |
| 202 | { SCMP_SYS(truncate), 241 }, |
| 203 | { SCMP_SYS(fchown), 241 }, |
| 204 | { SCMP_SYS(lchown), 241 }, |
| 205 | { SCMP_SYS(fchownat), 241 }, |
| 206 | { SCMP_SYS(fstatfs), 241 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 207 | { SCMP_SYS(getitimer), 241 }, |
| 208 | { SCMP_SYS(syncfs), 241 }, |
| 209 | { SCMP_SYS(fsync), 241 }, |
| 210 | { SCMP_SYS(fchdir), 241 }, |
Eduardo Otubo | fe512d6 | 2012-11-29 13:56:41 -0200 | [diff] [blame] | 211 | { SCMP_SYS(msync), 241 }, |
| 212 | { SCMP_SYS(sched_setparam), 241 }, |
| 213 | { SCMP_SYS(sched_setscheduler), 241 }, |
| 214 | { SCMP_SYS(sched_yield), 241 }, |
| 215 | { SCMP_SYS(sched_rr_get_interval), 241 }, |
| 216 | { SCMP_SYS(sched_setaffinity), 241 }, |
| 217 | { SCMP_SYS(sched_getaffinity), 241 }, |
| 218 | { SCMP_SYS(readahead), 241 }, |
| 219 | { SCMP_SYS(timer_getoverrun), 241 }, |
| 220 | { SCMP_SYS(unlinkat), 241 }, |
| 221 | { SCMP_SYS(readlinkat), 241 }, |
| 222 | { SCMP_SYS(faccessat), 241 }, |
| 223 | { SCMP_SYS(get_robust_list), 241 }, |
| 224 | { SCMP_SYS(splice), 241 }, |
| 225 | { SCMP_SYS(vmsplice), 241 }, |
| 226 | { SCMP_SYS(getcpu), 241 }, |
| 227 | { SCMP_SYS(sendmmsg), 241 }, |
| 228 | { SCMP_SYS(recvmmsg), 241 }, |
| 229 | { SCMP_SYS(prlimit64), 241 }, |
Paul Moore | fd21faa | 2013-05-29 16:30:01 -0400 | [diff] [blame] | 230 | { SCMP_SYS(waitid), 241 }, |
Paul Moore | 94113bd | 2013-07-15 15:32:01 -0400 | [diff] [blame] | 231 | { SCMP_SYS(io_cancel), 241 }, |
Paul Moore | fd21faa | 2013-05-29 16:30:01 -0400 | [diff] [blame] | 232 | { SCMP_SYS(io_setup), 241 }, |
Paul Moore | d2509b6 | 2013-07-18 09:57:03 -0400 | [diff] [blame] | 233 | { SCMP_SYS(io_destroy), 241 }, |
Paul Moore | 0c2acb1 | 2014-01-15 14:38:51 -0500 | [diff] [blame] | 234 | { SCMP_SYS(arch_prctl), 240 }, |
| 235 | { SCMP_SYS(mkdir), 240 }, |
Paul Moore | 918b94e | 2014-01-15 14:38:58 -0500 | [diff] [blame] | 236 | { SCMP_SYS(fchmod), 240 }, |
| 237 | { SCMP_SYS(shmget), 240 }, |
| 238 | { SCMP_SYS(shmat), 240 }, |
Felix Geyer | 8439761 | 2014-01-30 21:28:17 +0100 | [diff] [blame] | 239 | { SCMP_SYS(shmdt), 240 }, |
Paul Moore | e3f9bb0 | 2014-02-26 10:25:01 -0500 | [diff] [blame] | 240 | { SCMP_SYS(timerfd_create), 240 }, |
| 241 | { SCMP_SYS(shmctl), 240 }, |
Paolo Bonzini | 4b45b05 | 2015-01-20 14:32:33 +0100 | [diff] [blame] | 242 | { SCMP_SYS(mlockall), 240 }, |
Paul Moore | e3f9bb0 | 2014-02-26 10:25:01 -0500 | [diff] [blame] | 243 | { SCMP_SYS(mlock), 240 }, |
Paul Moore | b22876c | 2014-08-14 13:19:32 -0400 | [diff] [blame] | 244 | { SCMP_SYS(munlock), 240 }, |
Philipp Gesang | f73adec | 2014-09-05 18:29:31 +0200 | [diff] [blame] | 245 | { SCMP_SYS(semctl), 240 }, |
| 246 | { SCMP_SYS(fallocate), 240 }, |
| 247 | { SCMP_SYS(fadvise64), 240 }, |
| 248 | { SCMP_SYS(inotify_init1), 240 }, |
Paul Moore | ea259ac | 2014-12-17 15:50:09 -0500 | [diff] [blame] | 249 | { SCMP_SYS(inotify_add_watch), 240 }, |
Eduardo Otubo | f8d82b8 | 2015-10-09 17:17:41 +0200 | [diff] [blame] | 250 | { SCMP_SYS(mbind), 240 }, |
Andrew Jones | 47d2067 | 2015-11-02 23:53:26 +0100 | [diff] [blame] | 251 | { SCMP_SYS(memfd_create), 240 }, |
| 252 | #ifdef HAVE_CACHEFLUSH |
| 253 | { SCMP_SYS(cacheflush), 240 }, |
| 254 | #endif |
Miroslav Rezanina | 8e08f8a | 2016-03-07 10:34:46 +0100 | [diff] [blame] | 255 | { SCMP_SYS(sysinfo), 240 }, |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | int seccomp_start(void) |
| 259 | { |
| 260 | int rc = 0; |
| 261 | unsigned int i = 0; |
| 262 | scmp_filter_ctx ctx; |
| 263 | |
| 264 | ctx = seccomp_init(SCMP_ACT_KILL); |
| 265 | if (ctx == NULL) { |
Corey Bryant | 2a13f99 | 2013-12-18 11:48:11 -0500 | [diff] [blame] | 266 | rc = -1; |
Eduardo Otubo | 2f668be | 2012-08-14 18:44:06 -0300 | [diff] [blame] | 267 | goto seccomp_return; |
| 268 | } |
| 269 | |
| 270 | for (i = 0; i < ARRAY_SIZE(seccomp_whitelist); i++) { |
| 271 | rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, seccomp_whitelist[i].num, 0); |
| 272 | if (rc < 0) { |
| 273 | goto seccomp_return; |
| 274 | } |
| 275 | rc = seccomp_syscall_priority(ctx, seccomp_whitelist[i].num, |
| 276 | seccomp_whitelist[i].priority); |
| 277 | if (rc < 0) { |
| 278 | goto seccomp_return; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | rc = seccomp_load(ctx); |
| 283 | |
| 284 | seccomp_return: |
| 285 | seccomp_release(ctx); |
| 286 | return rc; |
| 287 | } |