Debian Codegraph @

arch/BPF/BPFDisassembler.c

raw symbols SCIP
1 /* Capstone Disassembly Engine */
2 /* BPF Backend by david942j <[email protected]>, 2019 */
3
4 #ifdef CAPSTONE_HAS_BPF
5
6 #include <string.h>
7 #include <stddef.h> // offsetof macro
8
9 #include "BPFConstants.h"
10 #include "BPFDisassembler.h"
11 #include "BPFMapping.h"
12 #include "../../cs_priv.h"
13
14 static uint16_t read_u16(cs_struct *ud, const uint8_t *code)
15 {
16 if (MODE_IS_BIG_ENDIAN(ud->mode))
17 return (((uint16_t)code[0] << 8) | code[1]);
18 else
19 return (((uint16_t)code[1] << 8) | code[0]);
20 }
21
22 static uint32_t read_u32(cs_struct *ud, const uint8_t *code)
23 {
24 if (MODE_IS_BIG_ENDIAN(ud->mode))
25 return ((uint32_t)read_u16(ud, code) << 16) | read_u16(ud, code + 2);
26 else
27 return ((uint32_t)read_u16(ud, code + 2) << 16) | read_u16(ud, code);
28 }
29
30 ///< Malloc bpf_internal, also checks if code_len is large enough.
31 static bpf_internal *alloc_bpf_internal(size_t code_len)
32 {
33 bpf_internal *bpf;
34
35 if (code_len < 8)
36 return NULL;
37 bpf = cs_mem_malloc(sizeof(bpf_internal));
38 if (bpf == NULL)
39 return NULL;
40 /* default value */
41 bpf->insn_size = 8;
42 return bpf;
43 }
44
45 ///< Fetch a cBPF structure from code
46 static bpf_internal* fetch_cbpf(cs_struct *ud, const uint8_t *code,
47 size_t code_len)
48 {
49 bpf_internal *bpf;
50
51 bpf = alloc_bpf_internal(code_len);
52 if (bpf == NULL)
53 return NULL;
54
55 bpf->op = read_u16(ud, code);
56 bpf->jt = code[2];
57 bpf->jf = code[3];
58 bpf->k = read_u32(ud, code + 4);
59 return bpf;
60 }
61
62 ///< Fetch an eBPF structure from code
63 static bpf_internal* fetch_ebpf(cs_struct *ud, const uint8_t *code,
64 size_t code_len)
65 {
66 bpf_internal *bpf;
67
68 bpf = alloc_bpf_internal(code_len);
69 if (bpf == NULL)
70 return NULL;
71
72 bpf->op = (uint16_t)code[0];
73 bpf->dst = code[1] & 0xf;
74 bpf->src = (code[1] & 0xf0) >> 4;
75
76 // eBPF has one 16-byte instruction: BPF_LD | BPF_DW | BPF_IMM,
77 // in this case imm is combined with the next block's imm.
78 if (bpf->op == (BPF_CLASS_LD | BPF_SIZE_DW | BPF_MODE_IMM)) {
79 if (code_len < 16) {
80 cs_mem_free(bpf);
81 return NULL;
82 }
83 bpf->k = read_u32(ud, code + 4) | (((uint64_t)read_u32(ud, code + 12)) << 32);
84 bpf->insn_size = 16;
85 }
86 else {
87 bpf->offset = read_u16(ud, code + 2);
88 bpf->k = read_u32(ud, code + 4);
89 }
90 return bpf;
91 }
92
93 #define CHECK_READABLE_REG(ud, reg) do { \
94 if (! ((reg) >= BPF_REG_R0 && (reg) <= BPF_REG_R10)) \
95 return false; \
96 } while (0)
97
98 #define CHECK_WRITABLE_REG(ud, reg) do { \
99 if (! ((reg) >= BPF_REG_R0 && (reg) < BPF_REG_R10)) \
100 return false; \
101 } while (0)
102
103 #define CHECK_READABLE_AND_PUSH(ud, MI, r) do { \
104 CHECK_READABLE_REG(ud, r + BPF_REG_R0); \
105 MCOperand_CreateReg0(MI, r + BPF_REG_R0); \
106 } while (0)
107
108 #define CHECK_WRITABLE_AND_PUSH(ud, MI, r) do { \
109 CHECK_WRITABLE_REG(ud, r + BPF_REG_R0); \
110 MCOperand_CreateReg0(MI, r + BPF_REG_R0); \
111 } while (0)
112
113 static bool decodeLoad(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
114 {
115 if (!EBPF_MODE(ud)) {
116 /*
117 * +-----+-----------+--------------------+
118 * | ldb | [k] | [x+k] |
119 * | ldh | [k] | [x+k] |
120 * +-----+-----------+--------------------+
121 */
122 if (BPF_SIZE(bpf->op) == BPF_SIZE_DW)
123 return false;
124 if (BPF_SIZE(bpf->op) == BPF_SIZE_B || BPF_SIZE(bpf->op) == BPF_SIZE_H) {
125 /* no ldx */
126 if (BPF_CLASS(bpf->op) != BPF_CLASS_LD)
127 return false;
128 /* can only be BPF_ABS and BPF_IND */
129 if (BPF_MODE(bpf->op) == BPF_MODE_ABS) {
130 MCOperand_CreateImm0(MI, bpf->k);
131 return true;
132 }
133 else if (BPF_MODE(bpf->op) == BPF_MODE_IND) {
134 MCOperand_CreateReg0(MI, BPF_REG_X);
135 MCOperand_CreateImm0(MI, bpf->k);
136 return true;
137 }
138 return false;
139 }
140 /*
141 * +-----+----+------+------+-----+-------+
142 * | ld | #k | #len | M[k] | [k] | [x+k] |
143 * +-----+----+------+------+-----+-------+
144 * | ldx | #k | #len | M[k] | 4*([k]&0xf) |
145 * +-----+----+------+------+-------------+
146 */
147 switch (BPF_MODE(bpf->op)) {
148 default:
149 break;
150 case BPF_MODE_IMM:
151 MCOperand_CreateImm0(MI, bpf->k);
152 return true;
153 case BPF_MODE_LEN:
154 return true;
155 case BPF_MODE_MEM:
156 MCOperand_CreateImm0(MI, bpf->k);
157 return true;
158 }
159 if (BPF_CLASS(bpf->op) == BPF_CLASS_LD) {
160 if (BPF_MODE(bpf->op) == BPF_MODE_ABS) {
161 MCOperand_CreateImm0(MI, bpf->k);
162 return true;
163 }
164 else if (BPF_MODE(bpf->op) == BPF_MODE_IND) {
165 MCOperand_CreateReg0(MI, BPF_REG_X);
166 MCOperand_CreateImm0(MI, bpf->k);
167 return true;
168 }
169 }
170 else { /* LDX */
171 if (BPF_MODE(bpf->op) == BPF_MODE_MSH) {
172 MCOperand_CreateImm0(MI, bpf->k);
173 return true;
174 }
175 }
176 return false;
177 }
178
179 /* eBPF mode */
180 /*
181 * - IMM: lddw dst, imm64
182 * - ABS: ld{w,h,b,dw} [k]
183 * - IND: ld{w,h,b,dw} [src+k]
184 * - MEM: ldx{w,h,b,dw} dst, [src+off]
185 */
186 if (BPF_CLASS(bpf->op) == BPF_CLASS_LD) {
187 switch (BPF_MODE(bpf->op)) {
188 case BPF_MODE_IMM:
189 if (bpf->op != (BPF_CLASS_LD | BPF_SIZE_DW | BPF_MODE_IMM))
190 return false;
191 CHECK_WRITABLE_AND_PUSH(ud, MI, bpf->dst);
192 MCOperand_CreateImm0(MI, bpf->k);
193 return true;
194 case BPF_MODE_ABS:
195 MCOperand_CreateImm0(MI, bpf->k);
196 return true;
197 case BPF_MODE_IND:
198 CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
199 MCOperand_CreateImm0(MI, bpf->k);
200 return true;
201 }
202 return false;
203
204 }
205 /* LDX */
206 if (BPF_MODE(bpf->op) == BPF_MODE_MEM) {
207 CHECK_WRITABLE_AND_PUSH(ud, MI, bpf->dst);
208 CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
209 MCOperand_CreateImm0(MI, bpf->offset);
210 return true;
211 }
212 return false;
213 }
214
215 static bool decodeStore(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
216 {
217 /* in cBPF, only BPF_ST* | BPF_MEM | BPF_W is valid
218 * while in eBPF:
219 * - BPF_STX | BPF_XADD | BPF_{W,DW}
220 * - BPF_ST* | BPF_MEM | BPF_{W,H,B,DW}
221 * are valid
222 */
223 if (!EBPF_MODE(ud)) {
224 /* can only store to M[] */
225 if (bpf->op != (BPF_CLASS(bpf->op) | BPF_MODE_MEM | BPF_SIZE_W))
226 return false;
227 MCOperand_CreateImm0(MI, bpf->k);
228 return true;
229 }
230
231 /* eBPF */
232
233 if (BPF_MODE(bpf->op) == BPF_MODE_XADD) {
234 if (BPF_CLASS(bpf->op) != BPF_CLASS_STX)
235 return false;
236 if (BPF_SIZE(bpf->op) != BPF_SIZE_W && BPF_SIZE(bpf->op) != BPF_SIZE_DW)
237 return false;
238 /* xadd [dst + off], src */
239 CHECK_READABLE_AND_PUSH(ud, MI, bpf->dst);
240 MCOperand_CreateImm0(MI, bpf->offset);
241 CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
242 return true;
243 }
244
245 if (BPF_MODE(bpf->op) != BPF_MODE_MEM)
246 return false;
247
248 /* st [dst + off], src */
249 CHECK_READABLE_AND_PUSH(ud, MI, bpf->dst);
250 MCOperand_CreateImm0(MI, bpf->offset);
251 if (BPF_CLASS(bpf->op) == BPF_CLASS_ST)
252 MCOperand_CreateImm0(MI, bpf->k);
253 else
254 CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
255 return true;
256 }
257
258 static bool decodeALU(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
259 {
260 /* Set MI->Operands */
261
262 /* cBPF */
263 if (!EBPF_MODE(ud)) {
264 if (BPF_OP(bpf->op) > BPF_ALU_XOR)
265 return false;
266 /* cBPF's NEG has no operands */
267 if (BPF_OP(bpf->op) == BPF_ALU_NEG)
268 return true;
269 if (BPF_SRC(bpf->op) == BPF_SRC_K)
270 MCOperand_CreateImm0(MI, bpf->k);
271 else /* BPF_SRC_X */
272 MCOperand_CreateReg0(MI, BPF_REG_X);
273 return true;
274 }
275
276 /* eBPF */
277
278 if (BPF_OP(bpf->op) > BPF_ALU_END)
279 return false;
280 /* ALU64 class doesn't have ENDian */
281 /* ENDian's imm must be one of 16, 32, 64 */
282 if (BPF_OP(bpf->op) == BPF_ALU_END) {
283 if (BPF_CLASS(bpf->op) == BPF_CLASS_ALU64)
284 return false;
285 if (bpf->k != 16 && bpf->k != 32 && bpf->k != 64)
286 return false;
287 }
288
289 /* - op dst, imm
290 * - op dst, src
291 * - neg dst
292 * - le<imm> dst
293 */
294 /* every ALU instructions have dst op */
295 CHECK_WRITABLE_AND_PUSH(ud, MI, bpf->dst);
296
297 /* special cases */
298 if (BPF_OP(bpf->op) == BPF_ALU_NEG)
299 return true;
300 if (BPF_OP(bpf->op) == BPF_ALU_END) {
301 /* bpf->k must be one of 16, 32, 64 */
302 MCInst_setOpcode(MI, MCInst_getOpcode(MI) | ((uint32_t)bpf->k << 4));
303 return true;
304 }
305
306 /* normal cases */
307 if (BPF_SRC(bpf->op) == BPF_SRC_K) {
308 MCOperand_CreateImm0(MI, bpf->k);
309 }
310 else { /* BPF_SRC_X */
311 CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
312 }
313 return true;
314 }
315
316 static bool decodeJump(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
317 {
318 /* cBPF and eBPF are very different in class jump */
319 if (!EBPF_MODE(ud)) {
320 if (BPF_OP(bpf->op) > BPF_JUMP_JSET)
321 return false;
322
323 /* ja is a special case of jumps */
324 if (BPF_OP(bpf->op) == BPF_JUMP_JA) {
325 MCOperand_CreateImm0(MI, bpf->k);
326 return true;
327 }
328
329 if (BPF_SRC(bpf->op) == BPF_SRC_K)
330 MCOperand_CreateImm0(MI, bpf->k);
331 else /* BPF_SRC_X */
332 MCOperand_CreateReg0(MI, BPF_REG_X);
333 MCOperand_CreateImm0(MI, bpf->jt);
334 MCOperand_CreateImm0(MI, bpf->jf);
335 }
336 else {
337 if (BPF_OP(bpf->op) > BPF_JUMP_JSLE)
338 return false;
339
340 /* No operands for exit */
341 if (BPF_OP(bpf->op) == BPF_JUMP_EXIT)
342 return bpf->op == (BPF_CLASS_JMP | BPF_JUMP_EXIT);
343 if (BPF_OP(bpf->op) == BPF_JUMP_CALL) {
344 if (bpf->op == (BPF_CLASS_JMP | BPF_JUMP_CALL)) {
345 MCOperand_CreateImm0(MI, bpf->k);
346 return true;
347 }
348 if (bpf->op == (BPF_CLASS_JMP | BPF_JUMP_CALL | BPF_SRC_X)) {
349 CHECK_READABLE_AND_PUSH(ud, MI, bpf->k);
350 return true;
351 }
352 return false;
353 }
354
355 /* ja is a special case of jumps */
356 if (BPF_OP(bpf->op) == BPF_JUMP_JA) {
357 if (BPF_SRC(bpf->op) != BPF_SRC_K)
358 return false;
359 MCOperand_CreateImm0(MI, bpf->offset);
360 return true;
361 }
362
363 /* <j> dst, src, +off */
364 CHECK_READABLE_AND_PUSH(ud, MI, bpf->dst);
365 if (BPF_SRC(bpf->op) == BPF_SRC_K)
366 MCOperand_CreateImm0(MI, bpf->k);
367 else
368 CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
369 MCOperand_CreateImm0(MI, bpf->offset);
370 }
371 return true;
372 }
373
374 static bool decodeReturn(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
375 {
376 /* Here only handles the BPF_RET class in cBPF */
377 switch (BPF_RVAL(bpf->op)) {
378 case BPF_SRC_K:
379 MCOperand_CreateImm0(MI, bpf->k);
380 return true;
381 case BPF_SRC_X:
382 MCOperand_CreateReg0(MI, BPF_REG_X);
383 return true;
384 case BPF_SRC_A:
385 MCOperand_CreateReg0(MI, BPF_REG_A);
386 return true;
387 }
388 return false;
389 }
390
391 static bool decodeMISC(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
392 {
393 uint16_t op = bpf->op ^ BPF_CLASS_MISC;
394 return op == BPF_MISCOP_TAX || op == BPF_MISCOP_TXA;
395 }
396
397 ///< 1. Check if the instruction is valid
398 ///< 2. Set MI->opcode
399 ///< 3. Set MI->Operands
400 static bool getInstruction(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
401 {
402 cs_detail *detail;
403
404 detail = MI->flat_insn->detail;
405 // initialize detail
406 if (detail) {
407 memset(detail, 0, offsetof(cs_detail, bpf) + sizeof(cs_bpf));
408 }
409
410 MCInst_clear(MI);
411 MCInst_setOpcode(MI, bpf->op);
412
413 switch (BPF_CLASS(bpf->op)) {
414 default: /* should never happen */
415 return false;
416 case BPF_CLASS_LD:
417 case BPF_CLASS_LDX:
418 return decodeLoad(ud, MI, bpf);
419 case BPF_CLASS_ST:
420 case BPF_CLASS_STX:
421 return decodeStore(ud, MI, bpf);
422 case BPF_CLASS_ALU:
423 return decodeALU(ud, MI, bpf);
424 case BPF_CLASS_JMP:
425 return decodeJump(ud, MI, bpf);
426 case BPF_CLASS_RET:
427 /* eBPF doesn't have this class */
428 if (EBPF_MODE(ud))
429 return false;
430 return decodeReturn(ud, MI, bpf);
431 case BPF_CLASS_MISC:
432 /* case BPF_CLASS_ALU64: */
433 if (EBPF_MODE(ud))
434 return decodeALU(ud, MI, bpf);
435 else
436 return decodeMISC(ud, MI, bpf);
437 }
438 }
439
440 bool BPF_getInstruction(csh ud, const uint8_t *code, size_t code_len,
441 MCInst *instr, uint16_t *size, uint64_t address, void *info)
442 {
443 cs_struct *cs;
444 bpf_internal *bpf;
445
446 cs = (cs_struct*)ud;
447 if (EBPF_MODE(cs))
448 bpf = fetch_ebpf(cs, code, code_len);
449 else
450 bpf = fetch_cbpf(cs, code, code_len);
451 if (bpf == NULL)
452 return false;
453 if (!getInstruction(cs, instr, bpf)) {
454 cs_mem_free(bpf);
455 return false;
456 }
457
458 *size = bpf->insn_size;
459 cs_mem_free(bpf);
460
461 return true;
462 }
463
464 #endif