2 * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
3 * Copyright (c) 2015-2017 The strace developers.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifdef HAVE_LINUX_SECCOMP_H
32 # include <linux/seccomp.h>
34 #include "xlat/seccomp_ops.h"
35 #include "xlat/seccomp_filter_flags.h"
37 #ifdef HAVE_LINUX_FILTER_H
38 # include <linux/filter.h>
39 # include "xlat/bpf_class.h"
40 # include "xlat/bpf_miscop.h"
41 # include "xlat/bpf_mode.h"
42 # include "xlat/bpf_op_alu.h"
43 # include "xlat/bpf_op_jmp.h"
44 # include "xlat/bpf_rval.h"
45 # include "xlat/bpf_size.h"
46 # include "xlat/bpf_src.h"
48 # ifndef SECCOMP_RET_ACTION
49 # define SECCOMP_RET_ACTION 0x7fff0000U
51 # include "xlat/seccomp_ret_action.h"
61 #ifdef HAVE_LINUX_FILTER_H
64 decode_bpf_code(uint16_t code)
66 uint16_t i = code & ~BPF_CLASS(code);
68 printxval(bpf_class, BPF_CLASS(code), "BPF_???");
69 switch (BPF_CLASS(code)) {
73 printxval(bpf_size, BPF_SIZE(code), "BPF_???");
75 printxval(bpf_mode, BPF_MODE(code), "BPF_???");
81 tprints_comment("BPF_???");
86 printxval(bpf_src, BPF_SRC(code), "BPF_???");
88 printxval(bpf_op_alu, BPF_OP(code), "BPF_???");
92 printxval(bpf_src, BPF_SRC(code), "BPF_???");
94 printxval(bpf_op_jmp, BPF_OP(code), "BPF_???");
98 printxval(bpf_rval, BPF_RVAL(code), "BPF_???");
102 tprints_comment("BPF_???");
107 printxval(bpf_miscop, BPF_MISCOP(code), "BPF_???");
108 i &= ~BPF_MISCOP(code);
111 tprints_comment("BPF_???");
118 #endif /* HAVE_LINUX_FILTER_H */
121 decode_bpf_stmt(const struct bpf_filter *filter)
123 #ifdef HAVE_LINUX_FILTER_H
124 tprints("BPF_STMT(");
125 decode_bpf_code(filter->code);
127 if (BPF_CLASS(filter->code) == BPF_RET) {
128 unsigned int action = SECCOMP_RET_ACTION & filter->k;
129 unsigned int data = filter->k & ~action;
131 printxval(seccomp_ret_action, action, "SECCOMP_RET_???");
133 tprintf("|%#x)", data);
137 tprintf("%#x)", filter->k);
140 tprintf("BPF_STMT(%#x, %#x)", filter->code, filter->k);
141 #endif /* HAVE_LINUX_FILTER_H */
145 decode_bpf_jump(const struct bpf_filter *filter)
147 #ifdef HAVE_LINUX_FILTER_H
148 tprints("BPF_JUMP(");
149 decode_bpf_code(filter->code);
150 tprintf(", %#x, %#x, %#x)",
151 filter->k, filter->jt, filter->jf);
153 tprintf("BPF_JUMP(%#x, %#x, %#x, %#x)",
154 filter->code, filter->k, filter->jt, filter->jf);
155 #endif /* HAVE_LINUX_FILTER_H */
159 # define BPF_MAXINSNS 4096
163 print_bpf_filter(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
165 const struct bpf_filter *filter = elem_buf;
166 unsigned int *pn = data;
168 if ((*pn)++ >= BPF_MAXINSNS) {
173 if (filter->jt || filter->jf)
174 decode_bpf_jump(filter);
176 decode_bpf_stmt(filter);
182 print_seccomp_fprog(struct tcb *const tcp, const kernel_ulong_t addr,
183 const unsigned short len)
188 unsigned int insns = 0;
189 struct bpf_filter filter;
191 print_array(tcp, addr, len, &filter, sizeof(filter),
192 umoven_or_printaddr, print_bpf_filter, &insns);
196 #include "seccomp_fprog.h"
199 print_seccomp_filter(struct tcb *const tcp, const kernel_ulong_t addr)
201 struct seccomp_fprog fprog;
203 if (fetch_seccomp_fprog(tcp, addr, &fprog)) {
204 tprintf("{len=%hu, filter=", fprog.len);
205 print_seccomp_fprog(tcp, fprog.filter, fprog.len);
211 decode_seccomp_set_mode_strict(const unsigned int flags,
212 const kernel_ulong_t addr)
214 tprintf("%u, ", flags);
220 unsigned int op = tcp->u_arg[0];
222 printxval(seccomp_ops, op, "SECCOMP_SET_MODE_???");
225 if (op == SECCOMP_SET_MODE_FILTER) {
226 printflags(seccomp_filter_flags, tcp->u_arg[1],
227 "SECCOMP_FILTER_FLAG_???");
229 print_seccomp_filter(tcp, tcp->u_arg[2]);
231 decode_seccomp_set_mode_strict(tcp->u_arg[1],