qemu-seccomp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. */
  15. #include <stdio.h>
  16. #include <seccomp.h>
  17. #include "sysemu/seccomp.h"
  18. struct QemuSeccompSyscall {
  19. int32_t num;
  20. uint8_t priority;
  21. };
  22. static const struct QemuSeccompSyscall seccomp_whitelist[] = {
  23. { SCMP_SYS(timer_settime), 255 },
  24. { SCMP_SYS(timer_gettime), 254 },
  25. { SCMP_SYS(futex), 253 },
  26. { SCMP_SYS(select), 252 },
  27. { SCMP_SYS(recvfrom), 251 },
  28. { SCMP_SYS(sendto), 250 },
  29. { SCMP_SYS(socketcall), 250 },
  30. { SCMP_SYS(read), 249 },
  31. { SCMP_SYS(io_submit), 249 },
  32. { SCMP_SYS(brk), 248 },
  33. { SCMP_SYS(clone), 247 },
  34. { SCMP_SYS(mmap), 247 },
  35. { SCMP_SYS(mprotect), 246 },
  36. { SCMP_SYS(execve), 245 },
  37. { SCMP_SYS(open), 245 },
  38. { SCMP_SYS(ioctl), 245 },
  39. { SCMP_SYS(socket), 245 },
  40. { SCMP_SYS(setsockopt), 245 },
  41. { SCMP_SYS(recvmsg), 245 },
  42. { SCMP_SYS(sendmsg), 245 },
  43. { SCMP_SYS(accept), 245 },
  44. { SCMP_SYS(connect), 245 },
  45. { SCMP_SYS(socketpair), 245 },
  46. { SCMP_SYS(bind), 245 },
  47. { SCMP_SYS(listen), 245 },
  48. { SCMP_SYS(semget), 245 },
  49. { SCMP_SYS(ipc), 245 },
  50. { SCMP_SYS(gettimeofday), 245 },
  51. { SCMP_SYS(readlink), 245 },
  52. { SCMP_SYS(access), 245 },
  53. { SCMP_SYS(prctl), 245 },
  54. { SCMP_SYS(signalfd), 245 },
  55. { SCMP_SYS(getrlimit), 245 },
  56. { SCMP_SYS(set_tid_address), 245 },
  57. { SCMP_SYS(statfs), 245 },
  58. { SCMP_SYS(unlink), 245 },
  59. { SCMP_SYS(wait4), 245 },
  60. { SCMP_SYS(fcntl64), 245 },
  61. { SCMP_SYS(fstat64), 245 },
  62. { SCMP_SYS(stat64), 245 },
  63. { SCMP_SYS(getgid32), 245 },
  64. { SCMP_SYS(getegid32), 245 },
  65. { SCMP_SYS(getuid32), 245 },
  66. { SCMP_SYS(geteuid32), 245 },
  67. { SCMP_SYS(sigreturn), 245 },
  68. { SCMP_SYS(_newselect), 245 },
  69. { SCMP_SYS(_llseek), 245 },
  70. { SCMP_SYS(mmap2), 245 },
  71. { SCMP_SYS(sigprocmask), 245 },
  72. { SCMP_SYS(sched_getparam), 245 },
  73. { SCMP_SYS(sched_getscheduler), 245 },
  74. { SCMP_SYS(fstat), 245 },
  75. { SCMP_SYS(clock_getres), 245 },
  76. { SCMP_SYS(sched_get_priority_min), 245 },
  77. { SCMP_SYS(sched_get_priority_max), 245 },
  78. { SCMP_SYS(stat), 245 },
  79. { SCMP_SYS(uname), 245 },
  80. { SCMP_SYS(eventfd2), 245 },
  81. { SCMP_SYS(io_getevents), 245 },
  82. { SCMP_SYS(dup), 245 },
  83. { SCMP_SYS(dup2), 245 },
  84. { SCMP_SYS(dup3), 245 },
  85. { SCMP_SYS(gettid), 245 },
  86. { SCMP_SYS(getgid), 245 },
  87. { SCMP_SYS(getegid), 245 },
  88. { SCMP_SYS(getuid), 245 },
  89. { SCMP_SYS(geteuid), 245 },
  90. { SCMP_SYS(timer_create), 245 },
  91. { SCMP_SYS(times), 245 },
  92. { SCMP_SYS(exit), 245 },
  93. { SCMP_SYS(clock_gettime), 245 },
  94. { SCMP_SYS(time), 245 },
  95. { SCMP_SYS(restart_syscall), 245 },
  96. { SCMP_SYS(pwrite64), 245 },
  97. { SCMP_SYS(nanosleep), 245 },
  98. { SCMP_SYS(chown), 245 },
  99. { SCMP_SYS(openat), 245 },
  100. { SCMP_SYS(getdents), 245 },
  101. { SCMP_SYS(timer_delete), 245 },
  102. { SCMP_SYS(exit_group), 245 },
  103. { SCMP_SYS(rt_sigreturn), 245 },
  104. { SCMP_SYS(sync), 245 },
  105. { SCMP_SYS(pread64), 245 },
  106. { SCMP_SYS(madvise), 245 },
  107. { SCMP_SYS(set_robust_list), 245 },
  108. { SCMP_SYS(lseek), 245 },
  109. { SCMP_SYS(pselect6), 245 },
  110. { SCMP_SYS(fork), 245 },
  111. { SCMP_SYS(rt_sigprocmask), 245 },
  112. { SCMP_SYS(write), 244 },
  113. { SCMP_SYS(fcntl), 243 },
  114. { SCMP_SYS(tgkill), 242 },
  115. { SCMP_SYS(kill), 242 },
  116. { SCMP_SYS(rt_sigaction), 242 },
  117. { SCMP_SYS(pipe2), 242 },
  118. { SCMP_SYS(munmap), 242 },
  119. { SCMP_SYS(mremap), 242 },
  120. { SCMP_SYS(fdatasync), 242 },
  121. { SCMP_SYS(close), 242 },
  122. { SCMP_SYS(rt_sigpending), 242 },
  123. { SCMP_SYS(rt_sigtimedwait), 242 },
  124. { SCMP_SYS(readv), 242 },
  125. { SCMP_SYS(writev), 242 },
  126. { SCMP_SYS(preadv), 242 },
  127. { SCMP_SYS(pwritev), 242 },
  128. { SCMP_SYS(setrlimit), 242 },
  129. { SCMP_SYS(ftruncate), 242 },
  130. { SCMP_SYS(lstat), 242 },
  131. { SCMP_SYS(pipe), 242 },
  132. { SCMP_SYS(umask), 242 },
  133. { SCMP_SYS(chdir), 242 },
  134. { SCMP_SYS(setitimer), 242 },
  135. { SCMP_SYS(setsid), 242 },
  136. { SCMP_SYS(poll), 242 },
  137. { SCMP_SYS(epoll_create), 242 },
  138. { SCMP_SYS(epoll_ctl), 242 },
  139. { SCMP_SYS(epoll_wait), 242 },
  140. { SCMP_SYS(waitpid), 242 },
  141. { SCMP_SYS(getsockname), 242 },
  142. { SCMP_SYS(getpeername), 242 },
  143. { SCMP_SYS(accept4), 242 },
  144. { SCMP_SYS(newfstatat), 241 },
  145. { SCMP_SYS(shutdown), 241 },
  146. { SCMP_SYS(getsockopt), 241 },
  147. { SCMP_SYS(semop), 241 },
  148. { SCMP_SYS(semtimedop), 241 },
  149. { SCMP_SYS(epoll_ctl_old), 241 },
  150. { SCMP_SYS(epoll_wait_old), 241 },
  151. { SCMP_SYS(epoll_pwait), 241 },
  152. { SCMP_SYS(epoll_create1), 241 },
  153. { SCMP_SYS(ppoll), 241 },
  154. { SCMP_SYS(creat), 241 },
  155. { SCMP_SYS(link), 241 },
  156. { SCMP_SYS(getpid), 241 },
  157. { SCMP_SYS(getppid), 241 },
  158. { SCMP_SYS(getpgrp), 241 },
  159. { SCMP_SYS(getpgid), 241 },
  160. { SCMP_SYS(getsid), 241 },
  161. { SCMP_SYS(getdents64), 241 },
  162. { SCMP_SYS(getresuid), 241 },
  163. { SCMP_SYS(getresgid), 241 },
  164. { SCMP_SYS(getgroups), 241 },
  165. { SCMP_SYS(getresuid32), 241 },
  166. { SCMP_SYS(getresgid32), 241 },
  167. { SCMP_SYS(getgroups32), 241 },
  168. { SCMP_SYS(signal), 241 },
  169. { SCMP_SYS(sigaction), 241 },
  170. { SCMP_SYS(sigsuspend), 241 },
  171. { SCMP_SYS(sigpending), 241 },
  172. { SCMP_SYS(truncate64), 241 },
  173. { SCMP_SYS(ftruncate64), 241 },
  174. { SCMP_SYS(fchown32), 241 },
  175. { SCMP_SYS(chown32), 241 },
  176. { SCMP_SYS(lchown32), 241 },
  177. { SCMP_SYS(statfs64), 241 },
  178. { SCMP_SYS(fstatfs64), 241 },
  179. { SCMP_SYS(fstatat64), 241 },
  180. { SCMP_SYS(lstat64), 241 },
  181. { SCMP_SYS(sendfile64), 241 },
  182. { SCMP_SYS(ugetrlimit), 241 },
  183. { SCMP_SYS(alarm), 241 },
  184. { SCMP_SYS(rt_sigsuspend), 241 },
  185. { SCMP_SYS(rt_sigqueueinfo), 241 },
  186. { SCMP_SYS(rt_tgsigqueueinfo), 241 },
  187. { SCMP_SYS(sigaltstack), 241 },
  188. { SCMP_SYS(signalfd4), 241 },
  189. { SCMP_SYS(truncate), 241 },
  190. { SCMP_SYS(fchown), 241 },
  191. { SCMP_SYS(lchown), 241 },
  192. { SCMP_SYS(fchownat), 241 },
  193. { SCMP_SYS(fstatfs), 241 },
  194. { SCMP_SYS(getitimer), 241 },
  195. { SCMP_SYS(syncfs), 241 },
  196. { SCMP_SYS(fsync), 241 },
  197. { SCMP_SYS(fchdir), 241 },
  198. { SCMP_SYS(msync), 241 },
  199. { SCMP_SYS(sched_setparam), 241 },
  200. { SCMP_SYS(sched_setscheduler), 241 },
  201. { SCMP_SYS(sched_yield), 241 },
  202. { SCMP_SYS(sched_rr_get_interval), 241 },
  203. { SCMP_SYS(sched_setaffinity), 241 },
  204. { SCMP_SYS(sched_getaffinity), 241 },
  205. { SCMP_SYS(readahead), 241 },
  206. { SCMP_SYS(timer_getoverrun), 241 },
  207. { SCMP_SYS(unlinkat), 241 },
  208. { SCMP_SYS(readlinkat), 241 },
  209. { SCMP_SYS(faccessat), 241 },
  210. { SCMP_SYS(get_robust_list), 241 },
  211. { SCMP_SYS(splice), 241 },
  212. { SCMP_SYS(vmsplice), 241 },
  213. { SCMP_SYS(getcpu), 241 },
  214. { SCMP_SYS(sendmmsg), 241 },
  215. { SCMP_SYS(recvmmsg), 241 },
  216. { SCMP_SYS(prlimit64), 241 },
  217. { SCMP_SYS(waitid), 241 },
  218. { SCMP_SYS(io_cancel), 241 },
  219. { SCMP_SYS(io_setup), 241 },
  220. { SCMP_SYS(io_destroy), 241 },
  221. { SCMP_SYS(arch_prctl), 240 },
  222. { SCMP_SYS(mkdir), 240 },
  223. { SCMP_SYS(fchmod), 240 },
  224. { SCMP_SYS(shmget), 240 },
  225. { SCMP_SYS(shmat), 240 },
  226. { SCMP_SYS(shmdt), 240 }
  227. };
  228. int seccomp_start(void)
  229. {
  230. int rc = 0;
  231. unsigned int i = 0;
  232. scmp_filter_ctx ctx;
  233. ctx = seccomp_init(SCMP_ACT_KILL);
  234. if (ctx == NULL) {
  235. rc = -1;
  236. goto seccomp_return;
  237. }
  238. for (i = 0; i < ARRAY_SIZE(seccomp_whitelist); i++) {
  239. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, seccomp_whitelist[i].num, 0);
  240. if (rc < 0) {
  241. goto seccomp_return;
  242. }
  243. rc = seccomp_syscall_priority(ctx, seccomp_whitelist[i].num,
  244. seccomp_whitelist[i].priority);
  245. if (rc < 0) {
  246. goto seccomp_return;
  247. }
  248. }
  249. rc = seccomp_load(ctx);
  250. seccomp_return:
  251. seccomp_release(ctx);
  252. return rc;
  253. }