2
0

qemu-seccomp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "qemu-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(read), 249 },
  30. { SCMP_SYS(brk), 248 },
  31. { SCMP_SYS(clone), 247 },
  32. { SCMP_SYS(mmap), 247 },
  33. { SCMP_SYS(mprotect), 246 },
  34. { SCMP_SYS(execve), 245 },
  35. { SCMP_SYS(open), 245 },
  36. { SCMP_SYS(ioctl), 245 },
  37. { SCMP_SYS(recvmsg), 245 },
  38. { SCMP_SYS(sendmsg), 245 },
  39. { SCMP_SYS(accept), 245 },
  40. { SCMP_SYS(connect), 245 },
  41. { SCMP_SYS(gettimeofday), 245 },
  42. { SCMP_SYS(readlink), 245 },
  43. { SCMP_SYS(access), 245 },
  44. { SCMP_SYS(prctl), 245 },
  45. { SCMP_SYS(signalfd), 245 },
  46. #if defined(__i386__)
  47. { SCMP_SYS(fcntl64), 245 },
  48. { SCMP_SYS(fstat64), 245 },
  49. { SCMP_SYS(stat64), 245 },
  50. { SCMP_SYS(getgid32), 245 },
  51. { SCMP_SYS(getegid32), 245 },
  52. { SCMP_SYS(getuid32), 245 },
  53. { SCMP_SYS(geteuid32), 245 },
  54. { SCMP_SYS(sigreturn), 245 },
  55. { SCMP_SYS(_newselect), 245 },
  56. { SCMP_SYS(_llseek), 245 },
  57. { SCMP_SYS(mmap2), 245},
  58. { SCMP_SYS(sigprocmask), 245 },
  59. #elif defined(__x86_64__)
  60. { SCMP_SYS(sched_getparam), 245},
  61. { SCMP_SYS(sched_getscheduler), 245},
  62. { SCMP_SYS(fstat), 245},
  63. { SCMP_SYS(clock_getres), 245},
  64. { SCMP_SYS(sched_get_priority_min), 245},
  65. { SCMP_SYS(sched_get_priority_max), 245},
  66. { SCMP_SYS(stat), 245},
  67. { SCMP_SYS(socket), 245},
  68. { SCMP_SYS(setsockopt), 245},
  69. { SCMP_SYS(uname), 245},
  70. { SCMP_SYS(semget), 245},
  71. #endif
  72. { SCMP_SYS(eventfd2), 245 },
  73. { SCMP_SYS(dup), 245 },
  74. { SCMP_SYS(gettid), 245 },
  75. { SCMP_SYS(timer_create), 245 },
  76. { SCMP_SYS(exit), 245 },
  77. { SCMP_SYS(clock_gettime), 245 },
  78. { SCMP_SYS(time), 245 },
  79. { SCMP_SYS(restart_syscall), 245 },
  80. { SCMP_SYS(pwrite64), 245 },
  81. { SCMP_SYS(chown), 245 },
  82. { SCMP_SYS(openat), 245 },
  83. { SCMP_SYS(getdents), 245 },
  84. { SCMP_SYS(timer_delete), 245 },
  85. { SCMP_SYS(exit_group), 245 },
  86. { SCMP_SYS(rt_sigreturn), 245 },
  87. { SCMP_SYS(sync), 245 },
  88. { SCMP_SYS(pread64), 245 },
  89. { SCMP_SYS(madvise), 245 },
  90. { SCMP_SYS(set_robust_list), 245 },
  91. { SCMP_SYS(lseek), 245 },
  92. { SCMP_SYS(pselect6), 245 },
  93. { SCMP_SYS(fork), 245 },
  94. { SCMP_SYS(bind), 245 },
  95. { SCMP_SYS(listen), 245 },
  96. { SCMP_SYS(eventfd), 245 },
  97. { SCMP_SYS(rt_sigprocmask), 245 },
  98. { SCMP_SYS(write), 244 },
  99. { SCMP_SYS(fcntl), 243 },
  100. { SCMP_SYS(tgkill), 242 },
  101. { SCMP_SYS(rt_sigaction), 242 },
  102. { SCMP_SYS(pipe2), 242 },
  103. { SCMP_SYS(munmap), 242 },
  104. { SCMP_SYS(mremap), 242 },
  105. { SCMP_SYS(getsockname), 242 },
  106. { SCMP_SYS(getpeername), 242 },
  107. { SCMP_SYS(fdatasync), 242 },
  108. { SCMP_SYS(close), 242 }
  109. };
  110. int seccomp_start(void)
  111. {
  112. int rc = 0;
  113. unsigned int i = 0;
  114. scmp_filter_ctx ctx;
  115. ctx = seccomp_init(SCMP_ACT_KILL);
  116. if (ctx == NULL) {
  117. goto seccomp_return;
  118. }
  119. for (i = 0; i < ARRAY_SIZE(seccomp_whitelist); i++) {
  120. rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, seccomp_whitelist[i].num, 0);
  121. if (rc < 0) {
  122. goto seccomp_return;
  123. }
  124. rc = seccomp_syscall_priority(ctx, seccomp_whitelist[i].num,
  125. seccomp_whitelist[i].priority);
  126. if (rc < 0) {
  127. goto seccomp_return;
  128. }
  129. }
  130. rc = seccomp_load(ctx);
  131. seccomp_return:
  132. seccomp_release(ctx);
  133. return rc;
  134. }