2 * Check verbose decoding of seccomp SECCOMP_SET_MODE_FILTER.
4 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2016-2017 The strace developers.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <asm/unistd.h>
40 # include <sys/prctl.h>
42 #ifdef HAVE_LINUX_SECCOMP_H
43 # include <linux/seccomp.h>
45 #include <linux/filter.h>
47 #if defined __NR_seccomp \
48 && defined PR_SET_NO_NEW_PRIVS \
49 && defined SECCOMP_SET_MODE_FILTER \
50 && defined SECCOMP_RET_ERRNO \
54 #define SOCK_FILTER_ALLOW_SYSCALL(nr) \
55 BPF_JUMP(BPF_JMP|BPF_K|BPF_JEQ, __NR_ ## nr, 0, 1), \
56 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW)
58 #define SOCK_FILTER_DENY_SYSCALL(nr, err) \
59 BPF_JUMP(BPF_JMP|BPF_K|BPF_JEQ, __NR_ ## nr, 0, 1), \
60 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO|(SECCOMP_RET_DATA & (err)))
62 #define SOCK_FILTER_KILL_PROCESS \
63 BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL)
65 #define PRINT_ALLOW_SYSCALL(nr) \
66 tprintf("BPF_JUMP(BPF_JMP|BPF_K|BPF_JEQ, %#x, 0, 0x1), " \
67 "BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), ", \
70 #define PRINT_DENY_SYSCALL(nr, err) \
71 tprintf("BPF_JUMP(BPF_JMP|BPF_K|BPF_JEQ, %#x, 0, 0x1), " \
72 "BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO|%#x), ", \
75 static const struct sock_filter filter_c[] = {
76 /* load syscall number */
77 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, offsetof(struct seccomp_data, nr)),
80 SOCK_FILTER_ALLOW_SYSCALL(close),
81 SOCK_FILTER_ALLOW_SYSCALL(exit),
82 SOCK_FILTER_ALLOW_SYSCALL(exit_group),
85 SOCK_FILTER_DENY_SYSCALL(sync, EBUSY),
86 SOCK_FILTER_DENY_SYSCALL(setsid, EPERM),
89 SOCK_FILTER_KILL_PROCESS
97 static const char kill_stmt_txt[] =
98 "BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL)";
99 struct sock_filter *const filter =
100 tail_memdup(filter_c, sizeof(filter_c));
101 struct sock_filter *const big_filter =
102 tail_alloc(sizeof(*big_filter) * (BPF_MAXINSNS + 1));
103 TAIL_ALLOC_OBJECT_CONST_PTR(struct sock_fprog, prog);
107 perror_msg_and_fail("pipe");
108 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
109 perror_msg_and_skip("PR_SET_NO_NEW_PRIVS");
111 prog->filter = filter + ARRAY_SIZE(filter_c);
113 syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, 0, prog);
114 tprintf("seccomp(SECCOMP_SET_MODE_FILTER, 0, {len=1, filter=%p})"
115 " = -1 EFAULT (%m)\n", prog->filter);
117 prog->filter = filter + ARRAY_SIZE(filter_c) - 1;
119 syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, 0, prog);
120 tprintf("seccomp(SECCOMP_SET_MODE_FILTER, 0, {len=%u"
121 ", filter=[%s, %p]}) = -1 EFAULT (%m)\n",
122 prog->len, kill_stmt_txt, filter + ARRAY_SIZE(filter_c));
125 syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, 0, prog);
126 tprintf("seccomp(SECCOMP_SET_MODE_FILTER, 0, {len=0, filter=[]})"
127 " = -1 EINVAL (%m)\n");
130 for (i = 0; i <= BPF_MAXINSNS; ++i) {
131 const struct sock_filter stmt =
132 BPF_STMT(BPF_CLASS(i), i << 16);
133 big_filter[i] = stmt;
136 prog->filter = big_filter;
137 prog->len = BPF_MAXINSNS + 1;
138 tprintf("seccomp(SECCOMP_SET_MODE_FILTER, %s, {len=%u, filter=[",
139 "SECCOMP_FILTER_FLAG_TSYNC|SECCOMP_FILTER_FLAG_LOG|0xfffffffc",
141 for (i = 0; i < BPF_MAXINSNS; ++i) {
144 switch (BPF_CLASS(i)) {
146 tprintf("BPF_STMT(BPF_LD|BPF_W|BPF_IMM, %#x)", i << 16);
149 tprintf("BPF_STMT(BPF_LDX|BPF_W|BPF_IMM, %#x)", i << 16);
152 tprintf("BPF_STMT(BPF_ST, %#x)", i << 16);
155 tprintf("BPF_STMT(BPF_STX, %#x)", i << 16);
158 tprintf("BPF_STMT(BPF_ALU|BPF_K|BPF_ADD, %#x)", i << 16);
161 tprintf("BPF_STMT(BPF_JMP|BPF_K|BPF_JA, %#x)", i << 16);
164 tprintf("BPF_STMT(BPF_RET|BPF_K, %#x"
165 " /* SECCOMP_RET_??? */)", i << 16);
168 tprintf("BPF_STMT(BPF_MISC|BPF_TAX, %#x)", i << 16);
173 syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, -1, prog);
174 tprintf(" = -1 EINVAL (%m)\n");
176 prog->filter = filter;
177 prog->len = ARRAY_SIZE(filter_c);
179 tprintf("seccomp(SECCOMP_SET_MODE_FILTER, 0, {len=%u, filter=[",
182 tprintf("BPF_STMT(BPF_LD|BPF_W|BPF_ABS, %#x), ",
183 (unsigned) offsetof(struct seccomp_data, nr));
185 PRINT_ALLOW_SYSCALL(close);
186 PRINT_ALLOW_SYSCALL(exit);
187 PRINT_ALLOW_SYSCALL(exit_group);
189 PRINT_DENY_SYSCALL(sync, EBUSY),
190 PRINT_DENY_SYSCALL(setsid, EPERM),
192 tprintf("%s]}) = 0\n+++ exited with 0 +++\n", kill_stmt_txt);
194 if (syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, 0, prog))
195 perror_msg_and_skip("SECCOMP_SET_MODE_FILTER");
197 if (close(0) || close(1))
205 SKIP_MAIN_UNDEFINED("__NR_seccomp && PR_SET_NO_NEW_PRIVS"
206 " && SECCOMP_SET_MODE_FILTER && SECCOMP_RET_ERRNO"
207 " && BPF_JUMP && BPF_STMT")