You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qemu-seccomp.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 "qemu/osdep.h"
  16. #include "qapi/error.h"
  17. #include "qemu/config-file.h"
  18. #include "qemu/option.h"
  19. #include "qemu/module.h"
  20. #include <sys/prctl.h>
  21. #include <seccomp.h>
  22. #include "sysemu/seccomp.h"
  23. #include <linux/seccomp.h>
  24. /* For some architectures (notably ARM) cacheflush is not supported until
  25. * libseccomp 2.2.3, but configure enforces that we are using a more recent
  26. * version on those hosts, so it is OK for this check to be less strict.
  27. */
  28. #if SCMP_VER_MAJOR >= 3
  29. #define HAVE_CACHEFLUSH
  30. #elif SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 2
  31. #define HAVE_CACHEFLUSH
  32. #endif
  33. struct QemuSeccompSyscall {
  34. int32_t num;
  35. uint8_t set;
  36. uint8_t narg;
  37. const struct scmp_arg_cmp *arg_cmp;
  38. };
  39. const struct scmp_arg_cmp sched_setscheduler_arg[] = {
  40. /* was SCMP_A1(SCMP_CMP_NE, SCHED_IDLE), but expanded due to GCC 4.x bug */
  41. { .arg = 1, .op = SCMP_CMP_NE, .datum_a = SCHED_IDLE }
  42. };
  43. static const struct QemuSeccompSyscall blacklist[] = {
  44. /* default set of syscalls to blacklist */
  45. { SCMP_SYS(reboot), QEMU_SECCOMP_SET_DEFAULT },
  46. { SCMP_SYS(swapon), QEMU_SECCOMP_SET_DEFAULT },
  47. { SCMP_SYS(swapoff), QEMU_SECCOMP_SET_DEFAULT },
  48. { SCMP_SYS(syslog), QEMU_SECCOMP_SET_DEFAULT },
  49. { SCMP_SYS(mount), QEMU_SECCOMP_SET_DEFAULT },
  50. { SCMP_SYS(umount), QEMU_SECCOMP_SET_DEFAULT },
  51. { SCMP_SYS(kexec_load), QEMU_SECCOMP_SET_DEFAULT },
  52. { SCMP_SYS(afs_syscall), QEMU_SECCOMP_SET_DEFAULT },
  53. { SCMP_SYS(break), QEMU_SECCOMP_SET_DEFAULT },
  54. { SCMP_SYS(ftime), QEMU_SECCOMP_SET_DEFAULT },
  55. { SCMP_SYS(getpmsg), QEMU_SECCOMP_SET_DEFAULT },
  56. { SCMP_SYS(gtty), QEMU_SECCOMP_SET_DEFAULT },
  57. { SCMP_SYS(lock), QEMU_SECCOMP_SET_DEFAULT },
  58. { SCMP_SYS(mpx), QEMU_SECCOMP_SET_DEFAULT },
  59. { SCMP_SYS(prof), QEMU_SECCOMP_SET_DEFAULT },
  60. { SCMP_SYS(profil), QEMU_SECCOMP_SET_DEFAULT },
  61. { SCMP_SYS(putpmsg), QEMU_SECCOMP_SET_DEFAULT },
  62. { SCMP_SYS(security), QEMU_SECCOMP_SET_DEFAULT },
  63. { SCMP_SYS(stty), QEMU_SECCOMP_SET_DEFAULT },
  64. { SCMP_SYS(tuxcall), QEMU_SECCOMP_SET_DEFAULT },
  65. { SCMP_SYS(ulimit), QEMU_SECCOMP_SET_DEFAULT },
  66. { SCMP_SYS(vserver), QEMU_SECCOMP_SET_DEFAULT },
  67. /* obsolete */
  68. { SCMP_SYS(readdir), QEMU_SECCOMP_SET_OBSOLETE },
  69. { SCMP_SYS(_sysctl), QEMU_SECCOMP_SET_OBSOLETE },
  70. { SCMP_SYS(bdflush), QEMU_SECCOMP_SET_OBSOLETE },
  71. { SCMP_SYS(create_module), QEMU_SECCOMP_SET_OBSOLETE },
  72. { SCMP_SYS(get_kernel_syms), QEMU_SECCOMP_SET_OBSOLETE },
  73. { SCMP_SYS(query_module), QEMU_SECCOMP_SET_OBSOLETE },
  74. { SCMP_SYS(sgetmask), QEMU_SECCOMP_SET_OBSOLETE },
  75. { SCMP_SYS(ssetmask), QEMU_SECCOMP_SET_OBSOLETE },
  76. { SCMP_SYS(sysfs), QEMU_SECCOMP_SET_OBSOLETE },
  77. { SCMP_SYS(uselib), QEMU_SECCOMP_SET_OBSOLETE },
  78. { SCMP_SYS(ustat), QEMU_SECCOMP_SET_OBSOLETE },
  79. /* privileged */
  80. { SCMP_SYS(setuid), QEMU_SECCOMP_SET_PRIVILEGED },
  81. { SCMP_SYS(setgid), QEMU_SECCOMP_SET_PRIVILEGED },
  82. { SCMP_SYS(setpgid), QEMU_SECCOMP_SET_PRIVILEGED },
  83. { SCMP_SYS(setsid), QEMU_SECCOMP_SET_PRIVILEGED },
  84. { SCMP_SYS(setreuid), QEMU_SECCOMP_SET_PRIVILEGED },
  85. { SCMP_SYS(setregid), QEMU_SECCOMP_SET_PRIVILEGED },
  86. { SCMP_SYS(setresuid), QEMU_SECCOMP_SET_PRIVILEGED },
  87. { SCMP_SYS(setresgid), QEMU_SECCOMP_SET_PRIVILEGED },
  88. { SCMP_SYS(setfsuid), QEMU_SECCOMP_SET_PRIVILEGED },
  89. { SCMP_SYS(setfsgid), QEMU_SECCOMP_SET_PRIVILEGED },
  90. /* spawn */
  91. { SCMP_SYS(fork), QEMU_SECCOMP_SET_SPAWN },
  92. { SCMP_SYS(vfork), QEMU_SECCOMP_SET_SPAWN },
  93. { SCMP_SYS(execve), QEMU_SECCOMP_SET_SPAWN },
  94. /* resource control */
  95. { SCMP_SYS(getpriority), QEMU_SECCOMP_SET_RESOURCECTL },
  96. { SCMP_SYS(setpriority), QEMU_SECCOMP_SET_RESOURCECTL },
  97. { SCMP_SYS(sched_setparam), QEMU_SECCOMP_SET_RESOURCECTL },
  98. { SCMP_SYS(sched_getparam), QEMU_SECCOMP_SET_RESOURCECTL },
  99. { SCMP_SYS(sched_setscheduler), QEMU_SECCOMP_SET_RESOURCECTL,
  100. ARRAY_SIZE(sched_setscheduler_arg), sched_setscheduler_arg },
  101. { SCMP_SYS(sched_getscheduler), QEMU_SECCOMP_SET_RESOURCECTL },
  102. { SCMP_SYS(sched_setaffinity), QEMU_SECCOMP_SET_RESOURCECTL },
  103. { SCMP_SYS(sched_getaffinity), QEMU_SECCOMP_SET_RESOURCECTL },
  104. { SCMP_SYS(sched_get_priority_max), QEMU_SECCOMP_SET_RESOURCECTL },
  105. { SCMP_SYS(sched_get_priority_min), QEMU_SECCOMP_SET_RESOURCECTL },
  106. };
  107. static inline __attribute__((unused)) int
  108. qemu_seccomp(unsigned int operation, unsigned int flags, void *args)
  109. {
  110. #ifdef __NR_seccomp
  111. return syscall(__NR_seccomp, operation, flags, args);
  112. #else
  113. errno = ENOSYS;
  114. return -1;
  115. #endif
  116. }
  117. static uint32_t qemu_seccomp_get_action(int set)
  118. {
  119. switch (set) {
  120. case QEMU_SECCOMP_SET_DEFAULT:
  121. case QEMU_SECCOMP_SET_OBSOLETE:
  122. case QEMU_SECCOMP_SET_PRIVILEGED:
  123. case QEMU_SECCOMP_SET_SPAWN: {
  124. #if defined(SECCOMP_GET_ACTION_AVAIL) && defined(SCMP_ACT_KILL_PROCESS) && \
  125. defined(SECCOMP_RET_KILL_PROCESS)
  126. static int kill_process = -1;
  127. if (kill_process == -1) {
  128. uint32_t action = SECCOMP_RET_KILL_PROCESS;
  129. if (qemu_seccomp(SECCOMP_GET_ACTION_AVAIL, 0, &action) == 0) {
  130. kill_process = 1;
  131. }
  132. kill_process = 0;
  133. }
  134. if (kill_process == 1) {
  135. return SCMP_ACT_KILL_PROCESS;
  136. }
  137. #endif
  138. return SCMP_ACT_TRAP;
  139. }
  140. case QEMU_SECCOMP_SET_RESOURCECTL:
  141. return SCMP_ACT_ERRNO(EPERM);
  142. default:
  143. g_assert_not_reached();
  144. }
  145. }
  146. static int seccomp_start(uint32_t seccomp_opts, Error **errp)
  147. {
  148. int rc = -1;
  149. unsigned int i = 0;
  150. scmp_filter_ctx ctx;
  151. ctx = seccomp_init(SCMP_ACT_ALLOW);
  152. if (ctx == NULL) {
  153. error_setg(errp, "failed to initialize seccomp context");
  154. goto seccomp_return;
  155. }
  156. rc = seccomp_attr_set(ctx, SCMP_FLTATR_CTL_TSYNC, 1);
  157. if (rc != 0) {
  158. error_setg_errno(errp, -rc,
  159. "failed to set seccomp thread synchronization");
  160. goto seccomp_return;
  161. }
  162. for (i = 0; i < ARRAY_SIZE(blacklist); i++) {
  163. uint32_t action;
  164. if (!(seccomp_opts & blacklist[i].set)) {
  165. continue;
  166. }
  167. action = qemu_seccomp_get_action(blacklist[i].set);
  168. rc = seccomp_rule_add_array(ctx, action, blacklist[i].num,
  169. blacklist[i].narg, blacklist[i].arg_cmp);
  170. if (rc < 0) {
  171. error_setg_errno(errp, -rc,
  172. "failed to add seccomp blacklist rules");
  173. goto seccomp_return;
  174. }
  175. }
  176. rc = seccomp_load(ctx);
  177. if (rc < 0) {
  178. error_setg_errno(errp, -rc,
  179. "failed to load seccomp syscall filter in kernel");
  180. }
  181. seccomp_return:
  182. seccomp_release(ctx);
  183. return rc < 0 ? -1 : 0;
  184. }
  185. #ifdef CONFIG_SECCOMP
  186. int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
  187. {
  188. if (qemu_opt_get_bool(opts, "enable", false)) {
  189. uint32_t seccomp_opts = QEMU_SECCOMP_SET_DEFAULT
  190. | QEMU_SECCOMP_SET_OBSOLETE;
  191. const char *value = NULL;
  192. value = qemu_opt_get(opts, "obsolete");
  193. if (value) {
  194. if (g_str_equal(value, "allow")) {
  195. seccomp_opts &= ~QEMU_SECCOMP_SET_OBSOLETE;
  196. } else if (g_str_equal(value, "deny")) {
  197. /* this is the default option, this if is here
  198. * to provide a little bit of consistency for
  199. * the command line */
  200. } else {
  201. error_setg(errp, "invalid argument for obsolete");
  202. return -1;
  203. }
  204. }
  205. value = qemu_opt_get(opts, "elevateprivileges");
  206. if (value) {
  207. if (g_str_equal(value, "deny")) {
  208. seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
  209. } else if (g_str_equal(value, "children")) {
  210. seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
  211. /* calling prctl directly because we're
  212. * not sure if host has CAP_SYS_ADMIN set*/
  213. if (prctl(PR_SET_NO_NEW_PRIVS, 1)) {
  214. error_setg(errp, "failed to set no_new_privs aborting");
  215. return -1;
  216. }
  217. } else if (g_str_equal(value, "allow")) {
  218. /* default value */
  219. } else {
  220. error_setg(errp, "invalid argument for elevateprivileges");
  221. return -1;
  222. }
  223. }
  224. value = qemu_opt_get(opts, "spawn");
  225. if (value) {
  226. if (g_str_equal(value, "deny")) {
  227. seccomp_opts |= QEMU_SECCOMP_SET_SPAWN;
  228. } else if (g_str_equal(value, "allow")) {
  229. /* default value */
  230. } else {
  231. error_setg(errp, "invalid argument for spawn");
  232. return -1;
  233. }
  234. }
  235. value = qemu_opt_get(opts, "resourcecontrol");
  236. if (value) {
  237. if (g_str_equal(value, "deny")) {
  238. seccomp_opts |= QEMU_SECCOMP_SET_RESOURCECTL;
  239. } else if (g_str_equal(value, "allow")) {
  240. /* default value */
  241. } else {
  242. error_setg(errp, "invalid argument for resourcecontrol");
  243. return -1;
  244. }
  245. }
  246. if (seccomp_start(seccomp_opts, errp) < 0) {
  247. return -1;
  248. }
  249. }
  250. return 0;
  251. }
  252. static QemuOptsList qemu_sandbox_opts = {
  253. .name = "sandbox",
  254. .implied_opt_name = "enable",
  255. .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
  256. .desc = {
  257. {
  258. .name = "enable",
  259. .type = QEMU_OPT_BOOL,
  260. },
  261. {
  262. .name = "obsolete",
  263. .type = QEMU_OPT_STRING,
  264. },
  265. {
  266. .name = "elevateprivileges",
  267. .type = QEMU_OPT_STRING,
  268. },
  269. {
  270. .name = "spawn",
  271. .type = QEMU_OPT_STRING,
  272. },
  273. {
  274. .name = "resourcecontrol",
  275. .type = QEMU_OPT_STRING,
  276. },
  277. { /* end of list */ }
  278. },
  279. };
  280. static void seccomp_register(void)
  281. {
  282. bool add = false;
  283. /* FIXME: use seccomp_api_get() >= 2 check when released */
  284. #if defined(SECCOMP_FILTER_FLAG_TSYNC)
  285. int check;
  286. /* check host TSYNC capability, it returns errno == ENOSYS if unavailable */
  287. check = qemu_seccomp(SECCOMP_SET_MODE_FILTER,
  288. SECCOMP_FILTER_FLAG_TSYNC, NULL);
  289. if (check < 0 && errno == EFAULT) {
  290. add = true;
  291. }
  292. #endif
  293. if (add) {
  294. qemu_add_opts(&qemu_sandbox_opts);
  295. }
  296. }
  297. opts_init(seccomp_register);
  298. #endif