]> granicus.if.org Git - strace/blob - bpf_filter.c
Update copyright headers
[strace] / bpf_filter.c
1 /*
2  * Decoder of classic BPF programs.
3  *
4  * Copyright (c) 2015-2017 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2017-2018 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
18  *
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.
29  */
30
31 #include "defs.h"
32
33 #include "bpf_filter.h"
34 #include "bpf_fprog.h"
35
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"
45
46 static void
47 print_bpf_filter_code(const uint16_t code)
48 {
49         uint16_t i = code & ~BPF_CLASS(code);
50
51         printxval(bpf_class, BPF_CLASS(code), "BPF_???");
52         switch (BPF_CLASS(code)) {
53         case BPF_LD:
54         case BPF_LDX:
55                 tprints("|");
56                 printxval(bpf_size, BPF_SIZE(code), "BPF_???");
57                 tprints("|");
58                 printxval(bpf_mode, BPF_MODE(code), "BPF_???");
59                 break;
60         case BPF_ST:
61         case BPF_STX:
62                 if (i) {
63                         tprintf("|%#x", i);
64                         tprints_comment("BPF_???");
65                 }
66                 break;
67         case BPF_ALU:
68                 tprints("|");
69                 printxval(bpf_src, BPF_SRC(code), "BPF_???");
70                 tprints("|");
71                 printxval(bpf_op_alu, BPF_OP(code), "BPF_???");
72                 break;
73         case BPF_JMP:
74                 tprints("|");
75                 printxval(bpf_src, BPF_SRC(code), "BPF_???");
76                 tprints("|");
77                 printxval(bpf_op_jmp, BPF_OP(code), "BPF_???");
78                 break;
79         case BPF_RET:
80                 tprints("|");
81                 printxval(bpf_rval, BPF_RVAL(code), "BPF_???");
82                 i &= ~BPF_RVAL(code);
83                 if (i) {
84                         tprintf("|%#x", i);
85                         tprints_comment("BPF_???");
86                 }
87                 break;
88         case BPF_MISC:
89                 tprints("|");
90                 printxval(bpf_miscop, BPF_MISCOP(code), "BPF_???");
91                 i &= ~BPF_MISCOP(code);
92                 if (i) {
93                         tprintf("|%#x", i);
94                         tprints_comment("BPF_???");
95                 }
96                 break;
97         }
98 }
99
100 static void
101 print_bpf_filter_stmt(const struct bpf_filter_block *const filter,
102                       const print_bpf_filter_fn print_k)
103 {
104         tprints("BPF_STMT(");
105         print_bpf_filter_code(filter->code);
106         tprints(", ");
107         if (!print_k || !print_k(filter))
108                 tprintf("%#x", filter->k);
109         tprints(")");
110 }
111
112 static void
113 print_bpf_filter_jump(const struct bpf_filter_block *const filter)
114 {
115         tprints("BPF_JUMP(");
116         print_bpf_filter_code(filter->code);
117         tprintf(", %#x, %#x, %#x)", filter->k, filter->jt, filter->jf);
118 }
119
120 struct bpf_filter_block_data {
121         const print_bpf_filter_fn fn;
122         unsigned int count;
123 };
124
125 static bool
126 print_bpf_filter_block(struct tcb *const tcp, void *const elem_buf,
127                        const size_t elem_size, void *const data)
128 {
129         const struct bpf_filter_block *const filter = elem_buf;
130         struct bpf_filter_block_data *const fbd = data;
131
132         if (fbd->count++ >= BPF_MAXINSNS) {
133                 tprints("...");
134                 return false;
135         }
136
137         if (filter->jt || filter->jf)
138                 print_bpf_filter_jump(filter);
139         else
140                 print_bpf_filter_stmt(filter, fbd->fn);
141
142         return true;
143 }
144
145 void
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)
148 {
149         if (abbrev(tcp)) {
150                 printaddr(addr);
151         } else {
152                 struct bpf_filter_block_data fbd = { .fn = print_k };
153                 struct bpf_filter_block filter;
154
155                 print_array(tcp, addr, len, &filter, sizeof(filter),
156                             umoven_or_printaddr, print_bpf_filter_block, &fbd);
157         }
158 }
159
160 void
161 decode_bpf_fprog(struct tcb *const tcp, const kernel_ulong_t addr,
162                  const print_bpf_filter_fn print_k)
163 {
164         struct bpf_fprog fprog;
165
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);
169                 tprints("}");
170         }
171 }