blob: 978d66bd28b6953e0fc0b47d9cff131bea0c7bfb [file] [log] [blame]
Eduardo Otubo2f668be2012-08-14 18:44:06 -03001/*
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 Maydelld38ea872016-01-29 17:50:05 +000015#include "qemu/osdep.h"
Eduardo Otubo2f668be2012-08-14 18:44:06 -030016#include <seccomp.h>
Paolo Bonzini9c17d612012-12-17 18:20:04 +010017#include "sysemu/seccomp.h"
Eduardo Otubo2f668be2012-08-14 18:44:06 -030018
James Hogan81bed732016-04-08 14:16:33 +010019/* 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 Jones47d20672015-11-02 23:53:26 +010023#if SCMP_VER_MAJOR >= 3
24 #define HAVE_CACHEFLUSH
James Hogan81bed732016-04-08 14:16:33 +010025#elif SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR >= 2
Andrew Jones47d20672015-11-02 23:53:26 +010026 #define HAVE_CACHEFLUSH
27#endif
28
Eduardo Otubo2f668be2012-08-14 18:44:06 -030029struct QemuSeccompSyscall {
30 int32_t num;
Eduardo Otubo1bd61522017-02-28 21:13:12 +010031 uint8_t set;
Eduardo Otubo2f668be2012-08-14 18:44:06 -030032};
33
Eduardo Otubo1bd61522017-02-28 21:13:12 +010034static const struct QemuSeccompSyscall blacklist[] = {
35 /* default set of syscalls to blacklist */
36 { SCMP_SYS(reboot), QEMU_SECCOMP_SET_DEFAULT },
37 { SCMP_SYS(swapon), QEMU_SECCOMP_SET_DEFAULT },
38 { SCMP_SYS(swapoff), QEMU_SECCOMP_SET_DEFAULT },
39 { SCMP_SYS(syslog), QEMU_SECCOMP_SET_DEFAULT },
40 { SCMP_SYS(mount), QEMU_SECCOMP_SET_DEFAULT },
41 { SCMP_SYS(umount), QEMU_SECCOMP_SET_DEFAULT },
42 { SCMP_SYS(kexec_load), QEMU_SECCOMP_SET_DEFAULT },
43 { SCMP_SYS(afs_syscall), QEMU_SECCOMP_SET_DEFAULT },
44 { SCMP_SYS(break), QEMU_SECCOMP_SET_DEFAULT },
45 { SCMP_SYS(ftime), QEMU_SECCOMP_SET_DEFAULT },
46 { SCMP_SYS(getpmsg), QEMU_SECCOMP_SET_DEFAULT },
47 { SCMP_SYS(gtty), QEMU_SECCOMP_SET_DEFAULT },
48 { SCMP_SYS(lock), QEMU_SECCOMP_SET_DEFAULT },
49 { SCMP_SYS(mpx), QEMU_SECCOMP_SET_DEFAULT },
50 { SCMP_SYS(prof), QEMU_SECCOMP_SET_DEFAULT },
51 { SCMP_SYS(profil), QEMU_SECCOMP_SET_DEFAULT },
52 { SCMP_SYS(putpmsg), QEMU_SECCOMP_SET_DEFAULT },
53 { SCMP_SYS(security), QEMU_SECCOMP_SET_DEFAULT },
54 { SCMP_SYS(stty), QEMU_SECCOMP_SET_DEFAULT },
55 { SCMP_SYS(tuxcall), QEMU_SECCOMP_SET_DEFAULT },
56 { SCMP_SYS(ulimit), QEMU_SECCOMP_SET_DEFAULT },
57 { SCMP_SYS(vserver), QEMU_SECCOMP_SET_DEFAULT },
Eduardo Otubo2b716fa2017-03-01 23:17:29 +010058 /* obsolete */
59 { SCMP_SYS(readdir), QEMU_SECCOMP_SET_OBSOLETE },
60 { SCMP_SYS(_sysctl), QEMU_SECCOMP_SET_OBSOLETE },
61 { SCMP_SYS(bdflush), QEMU_SECCOMP_SET_OBSOLETE },
62 { SCMP_SYS(create_module), QEMU_SECCOMP_SET_OBSOLETE },
63 { SCMP_SYS(get_kernel_syms), QEMU_SECCOMP_SET_OBSOLETE },
64 { SCMP_SYS(query_module), QEMU_SECCOMP_SET_OBSOLETE },
65 { SCMP_SYS(sgetmask), QEMU_SECCOMP_SET_OBSOLETE },
66 { SCMP_SYS(ssetmask), QEMU_SECCOMP_SET_OBSOLETE },
67 { SCMP_SYS(sysfs), QEMU_SECCOMP_SET_OBSOLETE },
68 { SCMP_SYS(uselib), QEMU_SECCOMP_SET_OBSOLETE },
69 { SCMP_SYS(ustat), QEMU_SECCOMP_SET_OBSOLETE },
Eduardo Otubo73a1e642017-03-13 22:13:27 +010070 /* privileged */
71 { SCMP_SYS(setuid), QEMU_SECCOMP_SET_PRIVILEGED },
72 { SCMP_SYS(setgid), QEMU_SECCOMP_SET_PRIVILEGED },
73 { SCMP_SYS(setpgid), QEMU_SECCOMP_SET_PRIVILEGED },
74 { SCMP_SYS(setsid), QEMU_SECCOMP_SET_PRIVILEGED },
75 { SCMP_SYS(setreuid), QEMU_SECCOMP_SET_PRIVILEGED },
76 { SCMP_SYS(setregid), QEMU_SECCOMP_SET_PRIVILEGED },
77 { SCMP_SYS(setresuid), QEMU_SECCOMP_SET_PRIVILEGED },
78 { SCMP_SYS(setresgid), QEMU_SECCOMP_SET_PRIVILEGED },
79 { SCMP_SYS(setfsuid), QEMU_SECCOMP_SET_PRIVILEGED },
80 { SCMP_SYS(setfsgid), QEMU_SECCOMP_SET_PRIVILEGED },
Eduardo Otubo2f668be2012-08-14 18:44:06 -030081};
82
Eduardo Otubo2b716fa2017-03-01 23:17:29 +010083
84int seccomp_start(uint32_t seccomp_opts)
Eduardo Otubo2f668be2012-08-14 18:44:06 -030085{
86 int rc = 0;
87 unsigned int i = 0;
88 scmp_filter_ctx ctx;
89
Eduardo Otubo1bd61522017-02-28 21:13:12 +010090 ctx = seccomp_init(SCMP_ACT_ALLOW);
Eduardo Otubo2f668be2012-08-14 18:44:06 -030091 if (ctx == NULL) {
Corey Bryant2a13f992013-12-18 11:48:11 -050092 rc = -1;
Eduardo Otubo2f668be2012-08-14 18:44:06 -030093 goto seccomp_return;
94 }
95
Eduardo Otubo1bd61522017-02-28 21:13:12 +010096 for (i = 0; i < ARRAY_SIZE(blacklist); i++) {
Eduardo Otubo2b716fa2017-03-01 23:17:29 +010097 if (!(seccomp_opts & blacklist[i].set)) {
98 continue;
99 }
100
Eduardo Otubo1bd61522017-02-28 21:13:12 +0100101 rc = seccomp_rule_add(ctx, SCMP_ACT_KILL, blacklist[i].num, 0);
Eduardo Otubo2f668be2012-08-14 18:44:06 -0300102 if (rc < 0) {
103 goto seccomp_return;
104 }
105 }
106
107 rc = seccomp_load(ctx);
108
109 seccomp_return:
110 seccomp_release(ctx);
111 return rc;
112}