2 * Decoder of classic BPF programs.
4 * Copyright (c) 2015-2017 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2017-2018 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.
33 #include "bpf_filter.h"
34 #include "bpf_fprog.h"
36 #include <linux/filter.h>
37 #include "xlat/bpf_class.h"
38 #include "xlat/bpf_miscop.h"
39 #include "xlat/bpf_mode.h"
40 #include "xlat/bpf_op_alu.h"
41 #include "xlat/bpf_op_jmp.h"
42 #include "xlat/bpf_rval.h"
43 #include "xlat/bpf_size.h"
44 #include "xlat/bpf_src.h"
47 print_bpf_filter_code(const uint16_t code)
49 uint16_t i = code & ~BPF_CLASS(code);
51 printxval(bpf_class, BPF_CLASS(code), "BPF_???");
52 switch (BPF_CLASS(code)) {
56 printxval(bpf_size, BPF_SIZE(code), "BPF_???");
58 printxval(bpf_mode, BPF_MODE(code), "BPF_???");
64 tprints_comment("BPF_???");
69 printxval(bpf_src, BPF_SRC(code), "BPF_???");
71 printxval(bpf_op_alu, BPF_OP(code), "BPF_???");
75 printxval(bpf_src, BPF_SRC(code), "BPF_???");
77 printxval(bpf_op_jmp, BPF_OP(code), "BPF_???");
81 printxval(bpf_rval, BPF_RVAL(code), "BPF_???");
85 tprints_comment("BPF_???");
90 printxval(bpf_miscop, BPF_MISCOP(code), "BPF_???");
91 i &= ~BPF_MISCOP(code);
94 tprints_comment("BPF_???");
101 print_bpf_filter_stmt(const struct bpf_filter_block *const filter,
102 const print_bpf_filter_fn print_k)
104 tprints("BPF_STMT(");
105 print_bpf_filter_code(filter->code);
107 if (!print_k || !print_k(filter))
108 tprintf("%#x", filter->k);
113 print_bpf_filter_jump(const struct bpf_filter_block *const filter)
115 tprints("BPF_JUMP(");
116 print_bpf_filter_code(filter->code);
117 tprintf(", %#x, %#x, %#x)", filter->k, filter->jt, filter->jf);
120 struct bpf_filter_block_data {
121 const print_bpf_filter_fn fn;
126 print_bpf_filter_block(struct tcb *const tcp, void *const elem_buf,
127 const size_t elem_size, void *const data)
129 const struct bpf_filter_block *const filter = elem_buf;
130 struct bpf_filter_block_data *const fbd = data;
132 if (fbd->count++ >= BPF_MAXINSNS) {
137 if (filter->jt || filter->jf)
138 print_bpf_filter_jump(filter);
140 print_bpf_filter_stmt(filter, fbd->fn);
146 print_bpf_fprog(struct tcb *const tcp, const kernel_ulong_t addr,
147 const unsigned short len, const print_bpf_filter_fn print_k)
152 struct bpf_filter_block_data fbd = { .fn = print_k };
153 struct bpf_filter_block filter;
155 print_array(tcp, addr, len, &filter, sizeof(filter),
156 umoven_or_printaddr, print_bpf_filter_block, &fbd);
161 decode_bpf_fprog(struct tcb *const tcp, const kernel_ulong_t addr,
162 const print_bpf_filter_fn print_k)
164 struct bpf_fprog fprog;
166 if (fetch_bpf_fprog(tcp, addr, &fprog)) {
167 tprintf("{len=%hu, filter=", fprog.len);
168 print_bpf_fprog(tcp, fprog.filter, fprog.len, print_k);