blob: 7b4afb7d96db18306006f51b7805b1150df0789c [file] [log] [blame]
Daniel Borkmann49ca6152021-08-24 09:39:31 +02001// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -07002/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 * Copyright (c) 2016 Facebook
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -07004 */
5
6#include <linux/bpf.h>
7
8#include "disasm.h"
9
10#define __BPF_FUNC_STR_FN(x) [BPF_FUNC_ ## x] = __stringify(bpf_ ## x)
11static const char * const func_id_str[] = {
12 __BPF_FUNC_MAPPER(__BPF_FUNC_STR_FN)
13};
14#undef __BPF_FUNC_STR_FN
15
Daniel Borkmann7105e822017-12-20 13:42:57 +010016static const char *__func_get_name(const struct bpf_insn_cbs *cbs,
17 const struct bpf_insn *insn,
18 char *buff, size_t len)
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -070019{
20 BUILD_BUG_ON(ARRAY_SIZE(func_id_str) != __BPF_FUNC_MAX_ID);
21
Martin KaFai Laue6ac2452021-03-24 18:51:42 -070022 if (!insn->src_reg &&
Daniel Borkmann7105e822017-12-20 13:42:57 +010023 insn->imm >= 0 && insn->imm < __BPF_FUNC_MAX_ID &&
24 func_id_str[insn->imm])
25 return func_id_str[insn->imm];
26
Martin KaFai Laue6ac2452021-03-24 18:51:42 -070027 if (cbs && cbs->cb_call) {
28 const char *res;
29
30 res = cbs->cb_call(cbs->private_data, insn);
31 if (res)
32 return res;
33 }
Daniel Borkmann7105e822017-12-20 13:42:57 +010034
35 if (insn->src_reg == BPF_PSEUDO_CALL)
36 snprintf(buff, len, "%+d", insn->imm);
Martin KaFai Laue6ac2452021-03-24 18:51:42 -070037 else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL)
38 snprintf(buff, len, "kernel-function");
Daniel Borkmann7105e822017-12-20 13:42:57 +010039
40 return buff;
41}
42
43static const char *__func_imm_name(const struct bpf_insn_cbs *cbs,
44 const struct bpf_insn *insn,
45 u64 full_imm, char *buff, size_t len)
46{
47 if (cbs && cbs->cb_imm)
48 return cbs->cb_imm(cbs->private_data, insn, full_imm);
49
50 snprintf(buff, len, "0x%llx", (unsigned long long)full_imm);
51 return buff;
52}
53
54const char *func_id_name(int id)
55{
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -070056 if (id >= 0 && id < __BPF_FUNC_MAX_ID && func_id_str[id])
57 return func_id_str[id];
58 else
59 return "unknown";
60}
61
62const char *const bpf_class_string[8] = {
63 [BPF_LD] = "ld",
64 [BPF_LDX] = "ldx",
65 [BPF_ST] = "st",
66 [BPF_STX] = "stx",
67 [BPF_ALU] = "alu",
68 [BPF_JMP] = "jmp",
Jiong Wang56cbd822019-01-26 12:26:02 -050069 [BPF_JMP32] = "jmp32",
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -070070 [BPF_ALU64] = "alu64",
71};
72
73const char *const bpf_alu_string[16] = {
74 [BPF_ADD >> 4] = "+=",
75 [BPF_SUB >> 4] = "-=",
76 [BPF_MUL >> 4] = "*=",
77 [BPF_DIV >> 4] = "/=",
78 [BPF_OR >> 4] = "|=",
79 [BPF_AND >> 4] = "&=",
80 [BPF_LSH >> 4] = "<<=",
81 [BPF_RSH >> 4] = ">>=",
82 [BPF_NEG >> 4] = "neg",
83 [BPF_MOD >> 4] = "%=",
84 [BPF_XOR >> 4] = "^=",
85 [BPF_MOV >> 4] = "=",
86 [BPF_ARSH >> 4] = "s>>=",
87 [BPF_END >> 4] = "endian",
88};
89
Brendan Jackman981f94c2021-01-14 18:17:49 +000090static const char *const bpf_atomic_alu_string[16] = {
91 [BPF_ADD >> 4] = "add",
92 [BPF_AND >> 4] = "and",
93 [BPF_OR >> 4] = "or",
Xu Kuohaid6fe1cf2021-03-25 13:41:41 +000094 [BPF_XOR >> 4] = "xor",
Brendan Jackman981f94c2021-01-14 18:17:49 +000095};
96
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -070097static const char *const bpf_ldst_string[] = {
98 [BPF_W >> 3] = "u32",
99 [BPF_H >> 3] = "u16",
100 [BPF_B >> 3] = "u8",
101 [BPF_DW >> 3] = "u64",
102};
103
104static const char *const bpf_jmp_string[16] = {
105 [BPF_JA >> 4] = "jmp",
106 [BPF_JEQ >> 4] = "==",
107 [BPF_JGT >> 4] = ">",
108 [BPF_JLT >> 4] = "<",
109 [BPF_JGE >> 4] = ">=",
110 [BPF_JLE >> 4] = "<=",
111 [BPF_JSET >> 4] = "&",
112 [BPF_JNE >> 4] = "!=",
113 [BPF_JSGT >> 4] = "s>",
114 [BPF_JSLT >> 4] = "s<",
115 [BPF_JSGE >> 4] = "s>=",
116 [BPF_JSLE >> 4] = "s<=",
117 [BPF_CALL >> 4] = "call",
118 [BPF_EXIT >> 4] = "exit",
119};
120
Daniel Borkmann7105e822017-12-20 13:42:57 +0100121static void print_bpf_end_insn(bpf_insn_print_t verbose,
Jiri Olsaabe08842018-03-23 11:41:28 +0100122 void *private_data,
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700123 const struct bpf_insn *insn)
124{
Jiri Olsaabe08842018-03-23 11:41:28 +0100125 verbose(private_data, "(%02x) r%d = %s%d r%d\n",
126 insn->code, insn->dst_reg,
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700127 BPF_SRC(insn->code) == BPF_TO_BE ? "be" : "le",
128 insn->imm, insn->dst_reg);
129}
130
Daniel Borkmann7105e822017-12-20 13:42:57 +0100131void print_bpf_insn(const struct bpf_insn_cbs *cbs,
Daniel Borkmann7105e822017-12-20 13:42:57 +0100132 const struct bpf_insn *insn,
133 bool allow_ptr_leaks)
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700134{
Daniel Borkmann7105e822017-12-20 13:42:57 +0100135 const bpf_insn_print_t verbose = cbs->cb_print;
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700136 u8 class = BPF_CLASS(insn->code);
137
138 if (class == BPF_ALU || class == BPF_ALU64) {
139 if (BPF_OP(insn->code) == BPF_END) {
140 if (class == BPF_ALU64)
Jiri Olsaabe08842018-03-23 11:41:28 +0100141 verbose(cbs->private_data, "BUG_alu64_%02x\n", insn->code);
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700142 else
Jiri Olsaabe08842018-03-23 11:41:28 +0100143 print_bpf_end_insn(verbose, cbs->private_data, insn);
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700144 } else if (BPF_OP(insn->code) == BPF_NEG) {
Jiong Wang56cbd822019-01-26 12:26:02 -0500145 verbose(cbs->private_data, "(%02x) %c%d = -%c%d\n",
146 insn->code, class == BPF_ALU ? 'w' : 'r',
147 insn->dst_reg, class == BPF_ALU ? 'w' : 'r',
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700148 insn->dst_reg);
149 } else if (BPF_SRC(insn->code) == BPF_X) {
Jiong Wang56cbd822019-01-26 12:26:02 -0500150 verbose(cbs->private_data, "(%02x) %c%d %s %c%d\n",
151 insn->code, class == BPF_ALU ? 'w' : 'r',
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700152 insn->dst_reg,
153 bpf_alu_string[BPF_OP(insn->code) >> 4],
Jiong Wang56cbd822019-01-26 12:26:02 -0500154 class == BPF_ALU ? 'w' : 'r',
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700155 insn->src_reg);
156 } else {
Jiong Wang56cbd822019-01-26 12:26:02 -0500157 verbose(cbs->private_data, "(%02x) %c%d %s %d\n",
158 insn->code, class == BPF_ALU ? 'w' : 'r',
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700159 insn->dst_reg,
160 bpf_alu_string[BPF_OP(insn->code) >> 4],
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700161 insn->imm);
162 }
163 } else if (class == BPF_STX) {
164 if (BPF_MODE(insn->code) == BPF_MEM)
Jiri Olsaabe08842018-03-23 11:41:28 +0100165 verbose(cbs->private_data, "(%02x) *(%s *)(r%d %+d) = r%d\n",
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700166 insn->code,
167 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
168 insn->dst_reg,
169 insn->off, insn->src_reg);
Brendan Jackman91c960b2021-01-14 18:17:44 +0000170 else if (BPF_MODE(insn->code) == BPF_ATOMIC &&
Menglong Dong60e578e2021-01-26 18:25:07 -0800171 (insn->imm == BPF_ADD || insn->imm == BPF_AND ||
Brendan Jackman981f94c2021-01-14 18:17:49 +0000172 insn->imm == BPF_OR || insn->imm == BPF_XOR)) {
173 verbose(cbs->private_data, "(%02x) lock *(%s *)(r%d %+d) %s r%d\n",
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700174 insn->code,
175 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
176 insn->dst_reg, insn->off,
Brendan Jackman981f94c2021-01-14 18:17:49 +0000177 bpf_alu_string[BPF_OP(insn->imm) >> 4],
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700178 insn->src_reg);
Brendan Jackman5ca419f2021-01-14 18:17:46 +0000179 } else if (BPF_MODE(insn->code) == BPF_ATOMIC &&
Brendan Jackman981f94c2021-01-14 18:17:49 +0000180 (insn->imm == (BPF_ADD | BPF_FETCH) ||
181 insn->imm == (BPF_AND | BPF_FETCH) ||
182 insn->imm == (BPF_OR | BPF_FETCH) ||
183 insn->imm == (BPF_XOR | BPF_FETCH))) {
184 verbose(cbs->private_data, "(%02x) r%d = atomic%s_fetch_%s((%s *)(r%d %+d), r%d)\n",
Brendan Jackman5ca419f2021-01-14 18:17:46 +0000185 insn->code, insn->src_reg,
186 BPF_SIZE(insn->code) == BPF_DW ? "64" : "",
Brendan Jackman981f94c2021-01-14 18:17:49 +0000187 bpf_atomic_alu_string[BPF_OP(insn->imm) >> 4],
Brendan Jackman5ca419f2021-01-14 18:17:46 +0000188 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
189 insn->dst_reg, insn->off, insn->src_reg);
Brendan Jackman5ffa2552021-01-14 18:17:47 +0000190 } else if (BPF_MODE(insn->code) == BPF_ATOMIC &&
191 insn->imm == BPF_CMPXCHG) {
192 verbose(cbs->private_data, "(%02x) r0 = atomic%s_cmpxchg((%s *)(r%d %+d), r0, r%d)\n",
193 insn->code,
194 BPF_SIZE(insn->code) == BPF_DW ? "64" : "",
195 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
196 insn->dst_reg, insn->off,
197 insn->src_reg);
198 } else if (BPF_MODE(insn->code) == BPF_ATOMIC &&
199 insn->imm == BPF_XCHG) {
200 verbose(cbs->private_data, "(%02x) r%d = atomic%s_xchg((%s *)(r%d %+d), r%d)\n",
201 insn->code, insn->src_reg,
202 BPF_SIZE(insn->code) == BPF_DW ? "64" : "",
203 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
204 insn->dst_reg, insn->off, insn->src_reg);
Brendan Jackman91c960b2021-01-14 18:17:44 +0000205 } else {
Jiri Olsaabe08842018-03-23 11:41:28 +0100206 verbose(cbs->private_data, "BUG_%02x\n", insn->code);
Brendan Jackman91c960b2021-01-14 18:17:44 +0000207 }
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700208 } else if (class == BPF_ST) {
Daniel Borkmannf5e81d12021-07-13 08:18:31 +0000209 if (BPF_MODE(insn->code) == BPF_MEM) {
210 verbose(cbs->private_data, "(%02x) *(%s *)(r%d %+d) = %d\n",
211 insn->code,
212 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
213 insn->dst_reg,
214 insn->off, insn->imm);
215 } else if (BPF_MODE(insn->code) == 0xc0 /* BPF_NOSPEC, no UAPI */) {
216 verbose(cbs->private_data, "(%02x) nospec\n", insn->code);
217 } else {
Jiri Olsaabe08842018-03-23 11:41:28 +0100218 verbose(cbs->private_data, "BUG_st_%02x\n", insn->code);
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700219 }
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700220 } else if (class == BPF_LDX) {
221 if (BPF_MODE(insn->code) != BPF_MEM) {
Jiri Olsaabe08842018-03-23 11:41:28 +0100222 verbose(cbs->private_data, "BUG_ldx_%02x\n", insn->code);
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700223 return;
224 }
Jiri Olsaabe08842018-03-23 11:41:28 +0100225 verbose(cbs->private_data, "(%02x) r%d = *(%s *)(r%d %+d)\n",
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700226 insn->code, insn->dst_reg,
227 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
228 insn->src_reg, insn->off);
229 } else if (class == BPF_LD) {
230 if (BPF_MODE(insn->code) == BPF_ABS) {
Jiri Olsaabe08842018-03-23 11:41:28 +0100231 verbose(cbs->private_data, "(%02x) r0 = *(%s *)skb[%d]\n",
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700232 insn->code,
233 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
234 insn->imm);
235 } else if (BPF_MODE(insn->code) == BPF_IND) {
Jiri Olsaabe08842018-03-23 11:41:28 +0100236 verbose(cbs->private_data, "(%02x) r0 = *(%s *)skb[r%d + %d]\n",
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700237 insn->code,
238 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
239 insn->src_reg, insn->imm);
240 } else if (BPF_MODE(insn->code) == BPF_IMM &&
241 BPF_SIZE(insn->code) == BPF_DW) {
242 /* At this point, we already made sure that the second
243 * part of the ldimm64 insn is accessible.
244 */
245 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +0200246 bool is_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD ||
247 insn->src_reg == BPF_PSEUDO_MAP_VALUE;
Daniel Borkmann7105e822017-12-20 13:42:57 +0100248 char tmp[64];
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700249
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +0200250 if (is_ptr && !allow_ptr_leaks)
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700251 imm = 0;
252
Jiri Olsaabe08842018-03-23 11:41:28 +0100253 verbose(cbs->private_data, "(%02x) r%d = %s\n",
Daniel Borkmann7105e822017-12-20 13:42:57 +0100254 insn->code, insn->dst_reg,
255 __func_imm_name(cbs, insn, imm,
256 tmp, sizeof(tmp)));
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700257 } else {
Jiri Olsaabe08842018-03-23 11:41:28 +0100258 verbose(cbs->private_data, "BUG_ld_%02x\n", insn->code);
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700259 return;
260 }
Jiong Wang56cbd822019-01-26 12:26:02 -0500261 } else if (class == BPF_JMP32 || class == BPF_JMP) {
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700262 u8 opcode = BPF_OP(insn->code);
263
264 if (opcode == BPF_CALL) {
Daniel Borkmann7105e822017-12-20 13:42:57 +0100265 char tmp[64];
266
267 if (insn->src_reg == BPF_PSEUDO_CALL) {
Jiri Olsaabe08842018-03-23 11:41:28 +0100268 verbose(cbs->private_data, "(%02x) call pc%s\n",
Daniel Borkmann7105e822017-12-20 13:42:57 +0100269 insn->code,
270 __func_get_name(cbs, insn,
271 tmp, sizeof(tmp)));
272 } else {
273 strcpy(tmp, "unknown");
Jiri Olsaabe08842018-03-23 11:41:28 +0100274 verbose(cbs->private_data, "(%02x) call %s#%d\n", insn->code,
Daniel Borkmann7105e822017-12-20 13:42:57 +0100275 __func_get_name(cbs, insn,
276 tmp, sizeof(tmp)),
277 insn->imm);
278 }
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700279 } else if (insn->code == (BPF_JMP | BPF_JA)) {
Jiri Olsaabe08842018-03-23 11:41:28 +0100280 verbose(cbs->private_data, "(%02x) goto pc%+d\n",
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700281 insn->code, insn->off);
282 } else if (insn->code == (BPF_JMP | BPF_EXIT)) {
Jiri Olsaabe08842018-03-23 11:41:28 +0100283 verbose(cbs->private_data, "(%02x) exit\n", insn->code);
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700284 } else if (BPF_SRC(insn->code) == BPF_X) {
Jiong Wang56cbd822019-01-26 12:26:02 -0500285 verbose(cbs->private_data,
286 "(%02x) if %c%d %s %c%d goto pc%+d\n",
287 insn->code, class == BPF_JMP32 ? 'w' : 'r',
288 insn->dst_reg,
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700289 bpf_jmp_string[BPF_OP(insn->code) >> 4],
Jiong Wang56cbd822019-01-26 12:26:02 -0500290 class == BPF_JMP32 ? 'w' : 'r',
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700291 insn->src_reg, insn->off);
292 } else {
Jiong Wang56cbd822019-01-26 12:26:02 -0500293 verbose(cbs->private_data,
294 "(%02x) if %c%d %s 0x%x goto pc%+d\n",
295 insn->code, class == BPF_JMP32 ? 'w' : 'r',
296 insn->dst_reg,
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700297 bpf_jmp_string[BPF_OP(insn->code) >> 4],
298 insn->imm, insn->off);
299 }
300 } else {
Jiri Olsaabe08842018-03-23 11:41:28 +0100301 verbose(cbs->private_data, "(%02x) %s\n",
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -0700302 insn->code, bpf_class_string[class]);
303 }
304}