xref: /openbmc/linux/kernel/bpf/disasm.c (revision 276e552e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3  * Copyright (c) 2016 Facebook
4  */
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)
11 static const char * const func_id_str[] = {
12 	__BPF_FUNC_MAPPER(__BPF_FUNC_STR_FN)
13 };
14 #undef __BPF_FUNC_STR_FN
15 
16 static const char *__func_get_name(const struct bpf_insn_cbs *cbs,
17 				   const struct bpf_insn *insn,
18 				   char *buff, size_t len)
19 {
20 	BUILD_BUG_ON(ARRAY_SIZE(func_id_str) != __BPF_FUNC_MAX_ID);
21 
22 	if (!insn->src_reg &&
23 	    insn->imm >= 0 && insn->imm < __BPF_FUNC_MAX_ID &&
24 	    func_id_str[insn->imm])
25 		return func_id_str[insn->imm];
26 
27 	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 	}
34 
35 	if (insn->src_reg == BPF_PSEUDO_CALL)
36 		snprintf(buff, len, "%+d", insn->imm);
37 	else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL)
38 		snprintf(buff, len, "kernel-function");
39 
40 	return buff;
41 }
42 
43 static 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 
54 const char *func_id_name(int id)
55 {
56 	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 
62 const 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",
69 	[BPF_JMP32] = "jmp32",
70 	[BPF_ALU64] = "alu64",
71 };
72 
73 const 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 
90 static const char *const bpf_atomic_alu_string[16] = {
91 	[BPF_ADD >> 4]  = "add",
92 	[BPF_AND >> 4]  = "and",
93 	[BPF_OR >> 4]  = "or",
94 	[BPF_XOR >> 4]  = "xor",
95 };
96 
97 static 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 
104 static 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 
121 static void print_bpf_end_insn(bpf_insn_print_t verbose,
122 			       void *private_data,
123 			       const struct bpf_insn *insn)
124 {
125 	verbose(private_data, "(%02x) r%d = %s%d r%d\n",
126 		insn->code, insn->dst_reg,
127 		BPF_SRC(insn->code) == BPF_TO_BE ? "be" : "le",
128 		insn->imm, insn->dst_reg);
129 }
130 
131 void print_bpf_insn(const struct bpf_insn_cbs *cbs,
132 		    const struct bpf_insn *insn,
133 		    bool allow_ptr_leaks)
134 {
135 	const bpf_insn_print_t verbose = cbs->cb_print;
136 	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)
141 				verbose(cbs->private_data, "BUG_alu64_%02x\n", insn->code);
142 			else
143 				print_bpf_end_insn(verbose, cbs->private_data, insn);
144 		} else if (BPF_OP(insn->code) == BPF_NEG) {
145 			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',
148 				insn->dst_reg);
149 		} else if (BPF_SRC(insn->code) == BPF_X) {
150 			verbose(cbs->private_data, "(%02x) %c%d %s %c%d\n",
151 				insn->code, class == BPF_ALU ? 'w' : 'r',
152 				insn->dst_reg,
153 				bpf_alu_string[BPF_OP(insn->code) >> 4],
154 				class == BPF_ALU ? 'w' : 'r',
155 				insn->src_reg);
156 		} else {
157 			verbose(cbs->private_data, "(%02x) %c%d %s %d\n",
158 				insn->code, class == BPF_ALU ? 'w' : 'r',
159 				insn->dst_reg,
160 				bpf_alu_string[BPF_OP(insn->code) >> 4],
161 				insn->imm);
162 		}
163 	} else if (class == BPF_STX) {
164 		if (BPF_MODE(insn->code) == BPF_MEM)
165 			verbose(cbs->private_data, "(%02x) *(%s *)(r%d %+d) = r%d\n",
166 				insn->code,
167 				bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
168 				insn->dst_reg,
169 				insn->off, insn->src_reg);
170 		else if (BPF_MODE(insn->code) == BPF_ATOMIC &&
171 			 (insn->imm == BPF_ADD || insn->imm == BPF_AND ||
172 			  insn->imm == BPF_OR || insn->imm == BPF_XOR)) {
173 			verbose(cbs->private_data, "(%02x) lock *(%s *)(r%d %+d) %s r%d\n",
174 				insn->code,
175 				bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
176 				insn->dst_reg, insn->off,
177 				bpf_alu_string[BPF_OP(insn->imm) >> 4],
178 				insn->src_reg);
179 		} else if (BPF_MODE(insn->code) == BPF_ATOMIC &&
180 			   (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",
185 				insn->code, insn->src_reg,
186 				BPF_SIZE(insn->code) == BPF_DW ? "64" : "",
187 				bpf_atomic_alu_string[BPF_OP(insn->imm) >> 4],
188 				bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
189 				insn->dst_reg, insn->off, insn->src_reg);
190 		} 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);
205 		} else {
206 			verbose(cbs->private_data, "BUG_%02x\n", insn->code);
207 		}
208 	} else if (class == BPF_ST) {
209 		if (BPF_MODE(insn->code) != BPF_MEM) {
210 			verbose(cbs->private_data, "BUG_st_%02x\n", insn->code);
211 			return;
212 		}
213 		verbose(cbs->private_data, "(%02x) *(%s *)(r%d %+d) = %d\n",
214 			insn->code,
215 			bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
216 			insn->dst_reg,
217 			insn->off, insn->imm);
218 	} else if (class == BPF_LDX) {
219 		if (BPF_MODE(insn->code) != BPF_MEM) {
220 			verbose(cbs->private_data, "BUG_ldx_%02x\n", insn->code);
221 			return;
222 		}
223 		verbose(cbs->private_data, "(%02x) r%d = *(%s *)(r%d %+d)\n",
224 			insn->code, insn->dst_reg,
225 			bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
226 			insn->src_reg, insn->off);
227 	} else if (class == BPF_LD) {
228 		if (BPF_MODE(insn->code) == BPF_ABS) {
229 			verbose(cbs->private_data, "(%02x) r0 = *(%s *)skb[%d]\n",
230 				insn->code,
231 				bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
232 				insn->imm);
233 		} else if (BPF_MODE(insn->code) == BPF_IND) {
234 			verbose(cbs->private_data, "(%02x) r0 = *(%s *)skb[r%d + %d]\n",
235 				insn->code,
236 				bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
237 				insn->src_reg, insn->imm);
238 		} else if (BPF_MODE(insn->code) == BPF_IMM &&
239 			   BPF_SIZE(insn->code) == BPF_DW) {
240 			/* At this point, we already made sure that the second
241 			 * part of the ldimm64 insn is accessible.
242 			 */
243 			u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
244 			bool is_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD ||
245 				      insn->src_reg == BPF_PSEUDO_MAP_VALUE;
246 			char tmp[64];
247 
248 			if (is_ptr && !allow_ptr_leaks)
249 				imm = 0;
250 
251 			verbose(cbs->private_data, "(%02x) r%d = %s\n",
252 				insn->code, insn->dst_reg,
253 				__func_imm_name(cbs, insn, imm,
254 						tmp, sizeof(tmp)));
255 		} else {
256 			verbose(cbs->private_data, "BUG_ld_%02x\n", insn->code);
257 			return;
258 		}
259 	} else if (class == BPF_JMP32 || class == BPF_JMP) {
260 		u8 opcode = BPF_OP(insn->code);
261 
262 		if (opcode == BPF_CALL) {
263 			char tmp[64];
264 
265 			if (insn->src_reg == BPF_PSEUDO_CALL) {
266 				verbose(cbs->private_data, "(%02x) call pc%s\n",
267 					insn->code,
268 					__func_get_name(cbs, insn,
269 							tmp, sizeof(tmp)));
270 			} else {
271 				strcpy(tmp, "unknown");
272 				verbose(cbs->private_data, "(%02x) call %s#%d\n", insn->code,
273 					__func_get_name(cbs, insn,
274 							tmp, sizeof(tmp)),
275 					insn->imm);
276 			}
277 		} else if (insn->code == (BPF_JMP | BPF_JA)) {
278 			verbose(cbs->private_data, "(%02x) goto pc%+d\n",
279 				insn->code, insn->off);
280 		} else if (insn->code == (BPF_JMP | BPF_EXIT)) {
281 			verbose(cbs->private_data, "(%02x) exit\n", insn->code);
282 		} else if (BPF_SRC(insn->code) == BPF_X) {
283 			verbose(cbs->private_data,
284 				"(%02x) if %c%d %s %c%d goto pc%+d\n",
285 				insn->code, class == BPF_JMP32 ? 'w' : 'r',
286 				insn->dst_reg,
287 				bpf_jmp_string[BPF_OP(insn->code) >> 4],
288 				class == BPF_JMP32 ? 'w' : 'r',
289 				insn->src_reg, insn->off);
290 		} else {
291 			verbose(cbs->private_data,
292 				"(%02x) if %c%d %s 0x%x goto pc%+d\n",
293 				insn->code, class == BPF_JMP32 ? 'w' : 'r',
294 				insn->dst_reg,
295 				bpf_jmp_string[BPF_OP(insn->code) >> 4],
296 				insn->imm, insn->off);
297 		}
298 	} else {
299 		verbose(cbs->private_data, "(%02x) %s\n",
300 			insn->code, bpf_class_string[class]);
301 	}
302 }
303