Coverage Report

Created: 2025-10-10 06:20

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