1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
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 
__func_get_name(const struct bpf_insn_cbs * cbs,const struct bpf_insn * insn,char * buff,size_t len)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 
__func_imm_name(const struct bpf_insn_cbs * cbs,const struct bpf_insn * insn,u64 full_imm,char * buff,size_t len)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 
func_id_name(int id)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 
print_bpf_end_insn(bpf_insn_print_t verbose,void * private_data,const struct bpf_insn * insn)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 
print_bpf_insn(const struct bpf_insn_cbs * cbs,const struct bpf_insn * insn,bool allow_ptr_leaks)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, "(%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 {
218 			verbose(cbs->private_data, "BUG_st_%02x\n", insn->code);
219 		}
220 	} else if (class == BPF_LDX) {
221 		if (BPF_MODE(insn->code) != BPF_MEM) {
222 			verbose(cbs->private_data, "BUG_ldx_%02x\n", insn->code);
223 			return;
224 		}
225 		verbose(cbs->private_data, "(%02x) r%d = *(%s *)(r%d %+d)\n",
226 			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) {
231 			verbose(cbs->private_data, "(%02x) r0 = *(%s *)skb[%d]\n",
232 				insn->code,
233 				bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
234 				insn->imm);
235 		} else if (BPF_MODE(insn->code) == BPF_IND) {
236 			verbose(cbs->private_data, "(%02x) r0 = *(%s *)skb[r%d + %d]\n",
237 				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;
246 			bool is_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD ||
247 				      insn->src_reg == BPF_PSEUDO_MAP_VALUE;
248 			char tmp[64];
249 
250 			if (is_ptr && !allow_ptr_leaks)
251 				imm = 0;
252 
253 			verbose(cbs->private_data, "(%02x) r%d = %s\n",
254 				insn->code, insn->dst_reg,
255 				__func_imm_name(cbs, insn, imm,
256 						tmp, sizeof(tmp)));
257 		} else {
258 			verbose(cbs->private_data, "BUG_ld_%02x\n", insn->code);
259 			return;
260 		}
261 	} else if (class == BPF_JMP32 || class == BPF_JMP) {
262 		u8 opcode = BPF_OP(insn->code);
263 
264 		if (opcode == BPF_CALL) {
265 			char tmp[64];
266 
267 			if (insn->src_reg == BPF_PSEUDO_CALL) {
268 				verbose(cbs->private_data, "(%02x) call pc%s\n",
269 					insn->code,
270 					__func_get_name(cbs, insn,
271 							tmp, sizeof(tmp)));
272 			} else {
273 				strcpy(tmp, "unknown");
274 				verbose(cbs->private_data, "(%02x) call %s#%d\n", insn->code,
275 					__func_get_name(cbs, insn,
276 							tmp, sizeof(tmp)),
277 					insn->imm);
278 			}
279 		} else if (insn->code == (BPF_JMP | BPF_JA)) {
280 			verbose(cbs->private_data, "(%02x) goto pc%+d\n",
281 				insn->code, insn->off);
282 		} else if (insn->code == (BPF_JMP | BPF_EXIT)) {
283 			verbose(cbs->private_data, "(%02x) exit\n", insn->code);
284 		} else if (BPF_SRC(insn->code) == BPF_X) {
285 			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,
289 				bpf_jmp_string[BPF_OP(insn->code) >> 4],
290 				class == BPF_JMP32 ? 'w' : 'r',
291 				insn->src_reg, insn->off);
292 		} else {
293 			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,
297 				bpf_jmp_string[BPF_OP(insn->code) >> 4],
298 				insn->imm, insn->off);
299 		}
300 	} else {
301 		verbose(cbs->private_data, "(%02x) %s\n",
302 			insn->code, bpf_class_string[class]);
303 	}
304 }
305