Coverage Report

Created: 2023-12-08 06:05

/src/capstonenext/arch/BPF/BPFDisassembler.c
Line
Count
Source (jump to first uncovered line)
1
/* Capstone Disassembly Engine */
2
/* BPF Backend by david942j <david942j@gmail.com>, 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
43.1k
{
16
43.1k
  if (MODE_IS_BIG_ENDIAN(ud->mode))
17
18.1k
    return (((uint16_t)code[0] << 8) | code[1]);
18
24.9k
  else
19
24.9k
    return (((uint16_t)code[1] << 8) | code[0]);
20
43.1k
}
21
22
static uint32_t read_u32(cs_struct *ud, const uint8_t *code)
23
14.6k
{
24
14.6k
  if (MODE_IS_BIG_ENDIAN(ud->mode))
25
6.26k
    return ((uint32_t)read_u16(ud, code) << 16) | read_u16(ud, code + 2);
26
8.41k
  else
27
8.41k
    return ((uint32_t)read_u16(ud, code + 2) << 16) | read_u16(ud, code);
28
14.6k
}
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
14.4k
{
33
14.4k
  bpf_internal *bpf;
34
35
14.4k
  if (code_len < 8)
36
269
    return NULL;
37
14.2k
  bpf = cs_mem_malloc(sizeof(bpf_internal));
38
14.2k
  if (bpf == NULL)
39
0
    return NULL;
40
  /* default value */
41
14.2k
  bpf->insn_size = 8;
42
14.2k
  return bpf;
43
14.2k
}
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
6.15k
{
49
6.15k
  bpf_internal *bpf;
50
51
6.15k
  bpf = alloc_bpf_internal(code_len);
52
6.15k
  if (bpf == NULL)
53
96
    return NULL;
54
55
6.06k
  bpf->op = read_u16(ud, code);
56
6.06k
  bpf->jt = code[2];
57
6.06k
  bpf->jf = code[3];
58
6.06k
  bpf->k = read_u32(ud, code + 4);
59
6.06k
  return bpf;
60
6.15k
}
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
8.32k
{
66
8.32k
  bpf_internal *bpf;
67
68
8.32k
  bpf = alloc_bpf_internal(code_len);
69
8.32k
  if (bpf == NULL)
70
173
    return NULL;
71
72
8.15k
  bpf->op = (uint16_t)code[0];
73
8.15k
  bpf->dst = code[1] & 0xf;
74
8.15k
  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
8.15k
  if (bpf->op == (BPF_CLASS_LD | BPF_SIZE_DW | BPF_MODE_IMM)) {
79
462
    if (code_len < 16) {
80
2
      cs_mem_free(bpf);
81
2
      return NULL;
82
2
    }
83
460
    bpf->k = read_u32(ud, code + 4) | (((uint64_t)read_u32(ud, code + 12)) << 32);
84
460
    bpf->insn_size = 16;
85
460
  }
86
7.69k
  else {
87
7.69k
    bpf->offset = read_u16(ud, code + 2);
88
7.69k
    bpf->k = read_u32(ud, code + 4);
89
7.69k
  }
90
8.15k
  return bpf;
91
8.15k
}
92
93
5.17k
#define CHECK_READABLE_REG(ud, reg) do { \
94
5.17k
    if (! ((reg) >= BPF_REG_R0 && (reg) <= BPF_REG_R10)) \
95
5.17k
      return false; \
96
5.17k
  } while (0)
97
98
3.52k
#define CHECK_WRITABLE_REG(ud, reg) do { \
99
3.52k
    if (! ((reg) >= BPF_REG_R0 && (reg) < BPF_REG_R10)) \
100
3.52k
      return false; \
101
3.52k
  } while (0)
102
103
5.17k
#define CHECK_READABLE_AND_PUSH(ud, MI, r) do { \
104
5.17k
    CHECK_READABLE_REG(ud, r + BPF_REG_R0); \
105
5.17k
    MCOperand_CreateReg0(MI, r + BPF_REG_R0); \
106
5.11k
  } while (0)
107
108
3.52k
#define CHECK_WRITABLE_AND_PUSH(ud, MI, r) do { \
109
3.52k
    CHECK_WRITABLE_REG(ud, r + BPF_REG_R0); \
110
3.52k
    MCOperand_CreateReg0(MI, r + BPF_REG_R0); \
111
3.51k
  } while (0)
112
113
static bool decodeLoad(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
114
4.35k
{
115
4.35k
  if (!EBPF_MODE(ud)) {
116
    /*
117
     *  +-----+-----------+--------------------+
118
     *  | ldb |    [k]    |       [x+k]        |
119
     *  | ldh |    [k]    |       [x+k]        |
120
     *  +-----+-----------+--------------------+
121
     */
122
2.42k
    if (BPF_SIZE(bpf->op) == BPF_SIZE_DW)
123
8
      return false;
124
2.41k
    if (BPF_SIZE(bpf->op) == BPF_SIZE_B || BPF_SIZE(bpf->op) == BPF_SIZE_H) {
125
      /* no ldx */
126
606
      if (BPF_CLASS(bpf->op) != BPF_CLASS_LD)
127
9
        return false;
128
      /* can only be BPF_ABS and BPF_IND */
129
597
      if (BPF_MODE(bpf->op) == BPF_MODE_ABS) {
130
338
        MCOperand_CreateImm0(MI, bpf->k);
131
338
        return true;
132
338
      }
133
259
      else if (BPF_MODE(bpf->op) == BPF_MODE_IND) {
134
256
        MCOperand_CreateReg0(MI, BPF_REG_X);
135
256
        MCOperand_CreateImm0(MI, bpf->k);
136
256
        return true;
137
256
      }
138
3
      return false;
139
597
    }
140
    /*
141
     *  +-----+----+------+------+-----+-------+
142
     *  | ld  | #k | #len | M[k] | [k] | [x+k] |
143
     *  +-----+----+------+------+-----+-------+
144
     *  | ldx | #k | #len | M[k] | 4*([k]&0xf) |
145
     *  +-----+----+------+------+-------------+
146
     */
147
1.81k
    switch (BPF_MODE(bpf->op)) {
148
807
    default:
149
807
      break;
150
807
    case BPF_MODE_IMM:
151
482
      MCOperand_CreateImm0(MI, bpf->k);
152
482
      return true;
153
166
    case BPF_MODE_LEN:
154
166
      return true;
155
357
    case BPF_MODE_MEM:
156
357
      MCOperand_CreateImm0(MI, bpf->k);
157
357
      return true;
158
1.81k
    }
159
807
    if (BPF_CLASS(bpf->op) == BPF_CLASS_LD) {
160
480
      if (BPF_MODE(bpf->op) == BPF_MODE_ABS) {
161
249
        MCOperand_CreateImm0(MI, bpf->k);
162
249
        return true;
163
249
      }
164
231
      else if (BPF_MODE(bpf->op) == BPF_MODE_IND) {
165
229
        MCOperand_CreateReg0(MI, BPF_REG_X);
166
229
        MCOperand_CreateImm0(MI, bpf->k);
167
229
        return true;
168
229
      }
169
480
    }
170
327
    else { /* LDX */
171
327
      if (BPF_MODE(bpf->op) == BPF_MODE_MSH) {
172
324
        MCOperand_CreateImm0(MI, bpf->k);
173
324
        return true;
174
324
      }
175
327
    }
176
5
    return false;
177
807
  }
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
1.92k
  if (BPF_CLASS(bpf->op) == BPF_CLASS_LD) {
187
1.48k
    switch (BPF_MODE(bpf->op)) {
188
475
    case BPF_MODE_IMM:
189
475
      if (bpf->op != (BPF_CLASS_LD | BPF_SIZE_DW | BPF_MODE_IMM))
190
15
        return false;
191
460
      CHECK_WRITABLE_AND_PUSH(ud, MI, bpf->dst);
192
459
      MCOperand_CreateImm0(MI, bpf->k);
193
459
      return true;
194
446
    case BPF_MODE_ABS:
195
446
      MCOperand_CreateImm0(MI, bpf->k);
196
446
      return true;
197
560
    case BPF_MODE_IND:
198
560
      CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
199
558
      MCOperand_CreateImm0(MI, bpf->k);
200
558
      return true;
201
1.48k
    }
202
8
    return false;
203
204
1.48k
  }
205
  /* LDX */
206
435
  if (BPF_MODE(bpf->op) == BPF_MODE_MEM) {
207
430
    CHECK_WRITABLE_AND_PUSH(ud, MI, bpf->dst);
208
429
    CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
209
428
    MCOperand_CreateImm0(MI, bpf->offset);
210
428
    return true;
211
429
  }
212
5
  return false;
213
435
}
214
215
static bool decodeStore(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
216
1.64k
{
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
1.64k
  if (!EBPF_MODE(ud)) {
224
    /* can only store to M[] */
225
42
    if (bpf->op != (BPF_CLASS(bpf->op) | BPF_MODE_MEM | BPF_SIZE_W))
226
13
      return false;
227
29
    MCOperand_CreateImm0(MI, bpf->k);
228
29
    return true;
229
42
  }
230
231
  /* eBPF */
232
233
1.60k
  if (BPF_MODE(bpf->op) == BPF_MODE_XADD) {
234
37
    if (BPF_CLASS(bpf->op) != BPF_CLASS_STX)
235
1
      return false;
236
36
    if (BPF_SIZE(bpf->op) != BPF_SIZE_W && BPF_SIZE(bpf->op) != BPF_SIZE_DW)
237
1
      return false;
238
    /* xadd [dst + off], src */
239
35
    CHECK_READABLE_AND_PUSH(ud, MI, bpf->dst);
240
33
    MCOperand_CreateImm0(MI, bpf->offset);
241
33
    CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
242
32
    return true;
243
33
  }
244
245
1.56k
  if (BPF_MODE(bpf->op) != BPF_MODE_MEM)
246
10
    return false;
247
248
  /* st [dst + off], src */
249
1.55k
  CHECK_READABLE_AND_PUSH(ud, MI, bpf->dst);
250
1.55k
  MCOperand_CreateImm0(MI, bpf->offset);
251
1.55k
  if (BPF_CLASS(bpf->op) == BPF_CLASS_ST)
252
506
    MCOperand_CreateImm0(MI, bpf->k);
253
1.04k
  else
254
1.04k
    CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
255
1.55k
  return true;
256
1.55k
}
257
258
static bool decodeALU(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
259
4.23k
{
260
  /* Set MI->Operands */
261
262
  /* cBPF */
263
4.23k
  if (!EBPF_MODE(ud)) {
264
1.55k
    if (BPF_OP(bpf->op) > BPF_ALU_XOR)
265
1
      return false;
266
    /* cBPF's NEG has no operands */
267
1.55k
    if (BPF_OP(bpf->op) == BPF_ALU_NEG)
268
339
      return true;
269
1.21k
    if (BPF_SRC(bpf->op) == BPF_SRC_K)
270
585
      MCOperand_CreateImm0(MI, bpf->k);
271
626
    else /* BPF_SRC_X */
272
626
      MCOperand_CreateReg0(MI, BPF_REG_X);
273
1.21k
    return true;
274
1.55k
  }
275
276
  /* eBPF */
277
278
2.68k
  if (BPF_OP(bpf->op) > BPF_ALU_END)
279
4
    return false;
280
  /* ALU64 class doesn't have ENDian */
281
  /* ENDian's imm must be one of 16, 32, 64 */
282
2.67k
  if (BPF_OP(bpf->op) == BPF_ALU_END) {
283
118
    if (BPF_CLASS(bpf->op) == BPF_CLASS_ALU64)
284
2
      return false;
285
116
    if (bpf->k != 16 && bpf->k != 32 && bpf->k != 64)
286
36
      return false;
287
116
  }
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
2.63k
  CHECK_WRITABLE_AND_PUSH(ud, MI, bpf->dst);
296
297
  /* special cases */
298
2.63k
  if (BPF_OP(bpf->op) == BPF_ALU_NEG)
299
276
    return true;
300
2.35k
  if (BPF_OP(bpf->op) == BPF_ALU_END) {
301
    /* bpf->k must be one of 16, 32, 64 */
302
80
    MCInst_setOpcode(MI, MCInst_getOpcode(MI) | ((uint32_t)bpf->k << 4));
303
80
    return true;
304
80
  }
305
306
  /* normal cases */
307
2.27k
  if (BPF_SRC(bpf->op) == BPF_SRC_K) {
308
2.09k
    MCOperand_CreateImm0(MI, bpf->k);
309
2.09k
  }
310
184
  else { /* BPF_SRC_X */
311
184
    CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
312
184
  }
313
2.27k
  return true;
314
2.27k
}
315
316
static bool decodeJump(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
317
2.84k
{
318
  /* cBPF and eBPF are very different in class jump */
319
2.84k
  if (!EBPF_MODE(ud)) {
320
906
    if (BPF_OP(bpf->op) > BPF_JUMP_JSET)
321
6
      return false;
322
323
    /* ja is a special case of jumps */
324
900
    if (BPF_OP(bpf->op) == BPF_JUMP_JA) {
325
271
      MCOperand_CreateImm0(MI, bpf->k);
326
271
      return true;
327
271
    }
328
329
629
    if (BPF_SRC(bpf->op) == BPF_SRC_K)
330
437
      MCOperand_CreateImm0(MI, bpf->k);
331
192
    else /* BPF_SRC_X */
332
192
      MCOperand_CreateReg0(MI, BPF_REG_X);
333
629
    MCOperand_CreateImm0(MI, bpf->jt);
334
629
    MCOperand_CreateImm0(MI, bpf->jf);
335
629
  }
336
1.93k
  else {
337
1.93k
    if (BPF_OP(bpf->op) > BPF_JUMP_JSLE)
338
2
      return false;
339
340
    /* No operands for exit */
341
1.93k
    if (BPF_OP(bpf->op) == BPF_JUMP_EXIT)
342
279
      return bpf->op == (BPF_CLASS_JMP | BPF_JUMP_EXIT);
343
1.65k
    if (BPF_OP(bpf->op) == BPF_JUMP_CALL) {
344
384
      if (bpf->op == (BPF_CLASS_JMP | BPF_JUMP_CALL)) {
345
341
        MCOperand_CreateImm0(MI, bpf->k);
346
341
        return true;
347
341
      }
348
43
      if (bpf->op == (BPF_CLASS_JMP | BPF_JUMP_CALL | BPF_SRC_X)) {
349
43
        CHECK_READABLE_AND_PUSH(ud, MI, bpf->k);
350
3
        return true;
351
43
      }
352
0
      return false;
353
43
    }
354
355
    /* ja is a special case of jumps */
356
1.27k
    if (BPF_OP(bpf->op) == BPF_JUMP_JA) {
357
70
      if (BPF_SRC(bpf->op) != BPF_SRC_K)
358
1
        return false;
359
69
      MCOperand_CreateImm0(MI, bpf->offset);
360
69
      return true;
361
70
    }
362
363
    /* <j>  dst, src, +off */
364
1.20k
    CHECK_READABLE_AND_PUSH(ud, MI, bpf->dst);
365
1.19k
    if (BPF_SRC(bpf->op) == BPF_SRC_K)
366
1.11k
      MCOperand_CreateImm0(MI, bpf->k);
367
85
    else
368
85
      CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
369
1.19k
    MCOperand_CreateImm0(MI, bpf->offset);
370
1.19k
  }
371
1.82k
  return true;
372
2.84k
}
373
374
static bool decodeReturn(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
375
1.08k
{
376
  /* Here only handles the BPF_RET class in cBPF */
377
1.08k
  switch (BPF_RVAL(bpf->op)) {
378
321
  case BPF_SRC_K:
379
321
    MCOperand_CreateImm0(MI, bpf->k);
380
321
    return true;
381
534
  case BPF_SRC_X:
382
534
    MCOperand_CreateReg0(MI, BPF_REG_X);
383
534
    return true;
384
227
  case BPF_SRC_A:
385
227
    MCOperand_CreateReg0(MI, BPF_REG_A);
386
227
    return true;
387
1.08k
  }
388
1
  return false;
389
1.08k
}
390
391
static bool decodeMISC(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
392
54
{
393
54
  uint16_t op = bpf->op ^ BPF_CLASS_MISC;
394
54
  return op == BPF_MISCOP_TAX || op == BPF_MISCOP_TXA;
395
54
}
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
14.2k
{
402
14.2k
  cs_detail *detail;
403
404
14.2k
  detail = MI->flat_insn->detail;
405
  // initialize detail
406
14.2k
  if (detail) {
407
14.2k
    memset(detail, 0, offsetof(cs_detail, bpf) + sizeof(cs_bpf));
408
14.2k
  }
409
410
14.2k
  MCInst_clear(MI);
411
14.2k
  MCInst_setOpcode(MI, bpf->op);
412
413
14.2k
  switch (BPF_CLASS(bpf->op)) {
414
0
  default: /* should never happen */
415
0
    return false;
416
3.13k
  case BPF_CLASS_LD:
417
4.35k
  case BPF_CLASS_LDX:
418
4.35k
    return decodeLoad(ud, MI, bpf);
419
540
  case BPF_CLASS_ST:
420
1.64k
  case BPF_CLASS_STX:
421
1.64k
    return decodeStore(ud, MI, bpf);
422
2.45k
  case BPF_CLASS_ALU:
423
2.45k
    return decodeALU(ud, MI, bpf);
424
2.84k
  case BPF_CLASS_JMP:
425
2.84k
    return decodeJump(ud, MI, bpf);
426
1.09k
  case BPF_CLASS_RET:
427
    /* eBPF doesn't have this class */
428
1.09k
    if (EBPF_MODE(ud))
429
11
      return false;
430
1.08k
    return decodeReturn(ud, MI, bpf);
431
1.83k
  case BPF_CLASS_MISC:
432
  /* case BPF_CLASS_ALU64: */
433
1.83k
    if (EBPF_MODE(ud))
434
1.77k
      return decodeALU(ud, MI, bpf);
435
54
    else
436
54
      return decodeMISC(ud, MI, bpf);
437
14.2k
  }
438
14.2k
}
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
14.4k
{
443
14.4k
  cs_struct *cs;
444
14.4k
  bpf_internal *bpf;
445
446
14.4k
  cs = (cs_struct*)ud;
447
14.4k
  if (EBPF_MODE(cs))
448
8.32k
    bpf = fetch_ebpf(cs, code, code_len);
449
6.15k
  else
450
6.15k
    bpf = fetch_cbpf(cs, code, code_len);
451
14.4k
  if (bpf == NULL)
452
271
    return false;
453
14.2k
  if (!getInstruction(cs, instr, bpf)) {
454
228
    cs_mem_free(bpf);
455
228
    return false;
456
228
  }
457
458
13.9k
  *size = bpf->insn_size;
459
13.9k
  cs_mem_free(bpf);
460
461
13.9k
  return true;
462
14.2k
}
463
464
#endif