2 * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifdef HAVE_LINUX_SECCOMP_H
31 # include <linux/seccomp.h>
33 #include "xlat/seccomp_ops.h"
34 #include "xlat/seccomp_filter_flags.h"
36 #ifdef HAVE_LINUX_FILTER_H
37 # include <linux/filter.h>
38 # include "xlat/bpf_class.h"
39 # include "xlat/bpf_miscop.h"
40 # include "xlat/bpf_mode.h"
41 # include "xlat/bpf_op_alu.h"
42 # include "xlat/bpf_op_jmp.h"
43 # include "xlat/bpf_rval.h"
44 # include "xlat/bpf_size.h"
45 # include "xlat/bpf_src.h"
47 # ifndef SECCOMP_RET_ACTION
48 # define SECCOMP_RET_ACTION 0x7fff0000U
50 # include "xlat/seccomp_ret_action.h"
60 #ifdef HAVE_LINUX_FILTER_H
63 decode_bpf_code(uint16_t code)
65 uint16_t i = code & ~BPF_CLASS(code);
67 printxval(bpf_class, BPF_CLASS(code), "BPF_???");
68 switch (BPF_CLASS(code)) {
72 printxval(bpf_size, BPF_SIZE(code), "BPF_???");
74 printxval(bpf_mode, BPF_MODE(code), "BPF_???");
79 tprintf("|%#x /* %s */", i, "BPF_???");
83 printxval(bpf_src, BPF_SRC(code), "BPF_???");
85 printxval(bpf_op_alu, BPF_OP(code), "BPF_???");
89 printxval(bpf_src, BPF_SRC(code), "BPF_???");
91 printxval(bpf_op_jmp, BPF_OP(code), "BPF_???");
95 printxval(bpf_rval, BPF_RVAL(code), "BPF_???");
98 tprintf("|%#x /* %s */", i, "BPF_???");
102 printxval(bpf_miscop, BPF_MISCOP(code), "BPF_???");
103 i &= ~BPF_MISCOP(code);
105 tprintf("|%#x /* %s */", i, "BPF_???");
111 #endif /* HAVE_LINUX_FILTER_H */
114 decode_bpf_stmt(const struct bpf_filter *filter)
116 #ifdef HAVE_LINUX_FILTER_H
117 tprints("BPF_STMT(");
118 decode_bpf_code(filter->code);
120 if (BPF_CLASS(filter->code) == BPF_RET) {
121 unsigned int action = SECCOMP_RET_ACTION & filter->k;
122 unsigned int data = filter->k & ~action;
124 printxval(seccomp_ret_action, action, "SECCOMP_RET_???");
126 tprintf("|%#x)", data);
130 tprintf("%#x)", filter->k);
133 tprintf("BPF_STMT(%#x, %#x)", filter->code, filter->k);
134 #endif /* HAVE_LINUX_FILTER_H */
138 decode_bpf_jump(const struct bpf_filter *filter)
140 #ifdef HAVE_LINUX_FILTER_H
141 tprints("BPF_JUMP(");
142 decode_bpf_code(filter->code);
143 tprintf(", %#x, %#x, %#x)",
144 filter->k, filter->jt, filter->jf);
146 tprintf("BPF_JUMP(%#x, %#x, %#x, %#x)",
147 filter->code, filter->k, filter->jt, filter->jf);
148 #endif /* HAVE_LINUX_FILTER_H */
152 # define BPF_MAXINSNS 4096
156 print_bpf_filter(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
158 const struct bpf_filter *filter = elem_buf;
159 unsigned int *pn = data;
161 if ((*pn)++ >= BPF_MAXINSNS) {
166 if (filter->jt || filter->jf)
167 decode_bpf_jump(filter);
169 decode_bpf_stmt(filter);
175 print_seccomp_fprog(struct tcb *tcp, unsigned long addr, unsigned short len)
180 unsigned int insns = 0;
181 struct bpf_filter filter;
183 print_array(tcp, addr, len, &filter, sizeof(filter),
184 umoven_or_printaddr, print_bpf_filter, &insns);
188 #include "seccomp_fprog.h"
191 print_seccomp_filter(struct tcb *tcp, unsigned long addr)
193 struct seccomp_fprog fprog;
195 if (fetch_seccomp_fprog(tcp, addr, &fprog)) {
196 tprintf("{len=%hu, filter=", fprog.len);
197 print_seccomp_fprog(tcp, fprog.filter, fprog.len);
203 decode_seccomp_set_mode_strict(unsigned int flags, unsigned long addr)
205 tprintf("%u, ", flags);
211 unsigned int op = tcp->u_arg[0];
213 printxval(seccomp_ops, op, "SECCOMP_SET_MODE_???");
216 if (op == SECCOMP_SET_MODE_FILTER) {
217 printflags(seccomp_filter_flags, tcp->u_arg[1],
218 "SECCOMP_FILTER_FLAG_???");
220 print_seccomp_filter(tcp, tcp->u_arg[2]);
222 decode_seccomp_set_mode_strict(tcp->u_arg[1],