Coverage Report

Created: 2026-06-15 06:41

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
52.3k
{
16
52.3k
  if (MODE_IS_BIG_ENDIAN(ud->mode))
17
30.6k
    return (((uint16_t)code[0] << 8) | code[1]);
18
21.6k
  else
19
21.6k
    return (((uint16_t)code[1] << 8) | code[0]);
20
52.3k
}
21
22
static uint32_t read_u32(cs_struct *ud, const uint8_t *code)
23
17.7k
{
24
17.7k
  if (MODE_IS_BIG_ENDIAN(ud->mode))
25
10.4k
    return ((uint32_t)read_u16(ud, code) << 16) | read_u16(ud, code + 2);
26
7.34k
  else
27
7.34k
    return ((uint32_t)read_u16(ud, code + 2) << 16) | read_u16(ud, code);
28
17.7k
}
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
30.0k
{
33
30.0k
  bpf_internal *bpf;
34
35
30.0k
  if (code_len < 8)
36
532
    return NULL;
37
29.5k
  bpf = cs_mem_malloc(sizeof(bpf_internal));
38
29.5k
  if (bpf == NULL)
39
0
    return NULL;
40
  /* default value */
41
29.5k
  bpf->insn_size = 8;
42
29.5k
  return bpf;
43
29.5k
}
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
11.3k
{
49
11.3k
  bpf_internal *bpf;
50
51
11.3k
  bpf = alloc_bpf_internal(code_len);
52
11.3k
  if (bpf == NULL)
53
179
    return NULL;
54
55
11.1k
  bpf->op = read_u16(ud, code);
56
11.1k
  bpf->jt = code[2];
57
11.1k
  bpf->jf = code[3];
58
11.1k
  bpf->k = read_u32(ud, code + 4);
59
11.1k
  return bpf;
60
11.3k
}
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
18.6k
{
66
18.6k
  bpf_internal *bpf;
67
68
18.6k
  bpf = alloc_bpf_internal(code_len);
69
18.6k
  if (bpf == NULL)
70
353
    return NULL;
71
72
18.3k
  bpf->op = (uint16_t)code[0];
73
18.3k
  bpf->dst = code[1] & 0xf;
74
18.3k
  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
18.3k
  if (bpf->op == (BPF_CLASS_LD | BPF_SIZE_DW | BPF_MODE_IMM)) {
79
546
    if (code_len < 16) {
80
4
      cs_mem_free(bpf);
81
4
      return NULL;
82
4
    }
83
542
    bpf->k = read_u32(ud, code + 4) | (((uint64_t)read_u32(ud, code + 12)) << 32);
84
542
    bpf->insn_size = 16;
85
542
  }
86
17.7k
  else {
87
17.7k
    bpf->offset = read_u16(ud, code + 2);
88
17.7k
    bpf->k = read_u32(ud, code + 4);
89
17.7k
  }
90
18.3k
  return bpf;
91
18.3k
}
92
93
7.28k
#define CHECK_READABLE_REG(ud, reg) do { \
94
7.28k
    if (! ((reg) >= BPF_REG_R0 && (reg) <= BPF_REG_R10)) \
95
7.28k
      return false; \
96
7.28k
  } while (0)
97
98
5.44k
#define CHECK_WRITABLE_REG(ud, reg) do { \
99
5.44k
    if (! ((reg) >= BPF_REG_R0 && (reg) < BPF_REG_R10)) \
100
5.44k
      return false; \
101
5.44k
  } while (0)
102
103
7.28k
#define CHECK_READABLE_AND_PUSH(ud, MI, r) do { \
104
7.28k
    CHECK_READABLE_REG(ud, r + BPF_REG_R0); \
105
7.28k
    MCOperand_CreateReg0(MI, r + BPF_REG_R0); \
106
7.21k
  } while (0)
107
108
5.44k
#define CHECK_WRITABLE_AND_PUSH(ud, MI, r) do { \
109
5.44k
    CHECK_WRITABLE_REG(ud, r + BPF_REG_R0); \
110
5.44k
    MCOperand_CreateReg0(MI, r + BPF_REG_R0); \
111
5.43k
  } while (0)
112
113
static bool decodeLoad(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
114
8.78k
{
115
8.78k
  if (!EBPF_MODE(ud)) {
116
    /*
117
     *  +-----+-----------+--------------------+
118
     *  | ldb |    [k]    |       [x+k]        |
119
     *  | ldh |    [k]    |       [x+k]        |
120
     *  +-----+-----------+--------------------+
121
     */
122
4.65k
    if (BPF_SIZE(bpf->op) == BPF_SIZE_DW)
123
10
      return false;
124
4.64k
    if (BPF_SIZE(bpf->op) == BPF_SIZE_B || BPF_SIZE(bpf->op) == BPF_SIZE_H) {
125
      /* no ldx */
126
1.35k
      if (BPF_CLASS(bpf->op) != BPF_CLASS_LD)
127
9
        return false;
128
      /* can only be BPF_ABS and BPF_IND */
129
1.34k
      if (BPF_MODE(bpf->op) == BPF_MODE_ABS) {
130
788
        MCOperand_CreateImm0(MI, bpf->k);
131
788
        return true;
132
788
      }
133
556
      else if (BPF_MODE(bpf->op) == BPF_MODE_IND) {
134
553
        MCOperand_CreateReg0(MI, BPF_REG_X);
135
553
        MCOperand_CreateImm0(MI, bpf->k);
136
553
        return true;
137
553
      }
138
3
      return false;
139
1.34k
    }
140
    /*
141
     *  +-----+----+------+------+-----+-------+
142
     *  | ld  | #k | #len | M[k] | [k] | [x+k] |
143
     *  +-----+----+------+------+-----+-------+
144
     *  | ldx | #k | #len | M[k] | 4*([k]&0xf) |
145
     *  +-----+----+------+------+-------------+
146
     */
147
3.28k
    switch (BPF_MODE(bpf->op)) {
148
1.58k
    default:
149
1.58k
      break;
150
1.58k
    case BPF_MODE_IMM:
151
989
      MCOperand_CreateImm0(MI, bpf->k);
152
989
      return true;
153
435
    case BPF_MODE_LEN:
154
435
      return true;
155
282
    case BPF_MODE_MEM:
156
282
      MCOperand_CreateImm0(MI, bpf->k);
157
282
      return true;
158
3.28k
    }
159
1.58k
    if (BPF_CLASS(bpf->op) == BPF_CLASS_LD) {
160
1.18k
      if (BPF_MODE(bpf->op) == BPF_MODE_ABS) {
161
770
        MCOperand_CreateImm0(MI, bpf->k);
162
770
        return true;
163
770
      }
164
413
      else if (BPF_MODE(bpf->op) == BPF_MODE_IND) {
165
411
        MCOperand_CreateReg0(MI, BPF_REG_X);
166
411
        MCOperand_CreateImm0(MI, bpf->k);
167
411
        return true;
168
411
      }
169
1.18k
    }
170
400
    else { /* LDX */
171
400
      if (BPF_MODE(bpf->op) == BPF_MODE_MSH) {
172
397
        MCOperand_CreateImm0(MI, bpf->k);
173
397
        return true;
174
397
      }
175
400
    }
176
5
    return false;
177
1.58k
  }
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
4.13k
  if (BPF_CLASS(bpf->op) == BPF_CLASS_LD) {
187
3.41k
    switch (BPF_MODE(bpf->op)) {
188
573
    case BPF_MODE_IMM:
189
573
      if (bpf->op != (BPF_CLASS_LD | BPF_SIZE_DW | BPF_MODE_IMM))
190
31
        return false;
191
542
      CHECK_WRITABLE_AND_PUSH(ud, MI, bpf->dst);
192
540
      MCOperand_CreateImm0(MI, bpf->k);
193
540
      return true;
194
1.86k
    case BPF_MODE_ABS:
195
1.86k
      MCOperand_CreateImm0(MI, bpf->k);
196
1.86k
      return true;
197
975
    case BPF_MODE_IND:
198
975
      CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
199
973
      MCOperand_CreateImm0(MI, bpf->k);
200
973
      return true;
201
3.41k
    }
202
10
    return false;
203
204
3.41k
  }
205
  /* LDX */
206
713
  if (BPF_MODE(bpf->op) == BPF_MODE_MEM) {
207
698
    CHECK_WRITABLE_AND_PUSH(ud, MI, bpf->dst);
208
695
    CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
209
693
    MCOperand_CreateImm0(MI, bpf->offset);
210
693
    return true;
211
695
  }
212
15
  return false;
213
713
}
214
215
static bool decodeStore(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
216
2.80k
{
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
2.80k
  if (!EBPF_MODE(ud)) {
224
    /* can only store to M[] */
225
643
    if (bpf->op != (BPF_CLASS(bpf->op) | BPF_MODE_MEM | BPF_SIZE_W))
226
17
      return false;
227
626
    MCOperand_CreateImm0(MI, bpf->k);
228
626
    return true;
229
643
  }
230
231
  /* eBPF */
232
233
2.16k
  if (BPF_MODE(bpf->op) == BPF_MODE_XADD) {
234
391
    if (BPF_CLASS(bpf->op) != BPF_CLASS_STX)
235
4
      return false;
236
387
    if (BPF_SIZE(bpf->op) != BPF_SIZE_W && BPF_SIZE(bpf->op) != BPF_SIZE_DW)
237
3
      return false;
238
    /* xadd [dst + off], src */
239
384
    CHECK_READABLE_AND_PUSH(ud, MI, bpf->dst);
240
380
    MCOperand_CreateImm0(MI, bpf->offset);
241
380
    CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
242
377
    return true;
243
380
  }
244
245
1.77k
  if (BPF_MODE(bpf->op) != BPF_MODE_MEM)
246
21
    return false;
247
248
  /* st [dst + off], src */
249
1.75k
  CHECK_READABLE_AND_PUSH(ud, MI, bpf->dst);
250
1.74k
  MCOperand_CreateImm0(MI, bpf->offset);
251
1.74k
  if (BPF_CLASS(bpf->op) == BPF_CLASS_ST)
252
998
    MCOperand_CreateImm0(MI, bpf->k);
253
751
  else
254
751
    CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
255
1.74k
  return true;
256
1.74k
}
257
258
static bool decodeALU(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
259
5.93k
{
260
  /* Set MI->Operands */
261
262
  /* cBPF */
263
5.93k
  if (!EBPF_MODE(ud)) {
264
1.68k
    if (BPF_OP(bpf->op) > BPF_ALU_XOR)
265
1
      return false;
266
    /* cBPF's NEG has no operands */
267
1.68k
    if (BPF_OP(bpf->op) == BPF_ALU_NEG)
268
261
      return true;
269
1.41k
    if (BPF_SRC(bpf->op) == BPF_SRC_K)
270
602
      MCOperand_CreateImm0(MI, bpf->k);
271
817
    else /* BPF_SRC_X */
272
817
      MCOperand_CreateReg0(MI, BPF_REG_X);
273
1.41k
    return true;
274
1.68k
  }
275
276
  /* eBPF */
277
278
4.25k
  if (BPF_OP(bpf->op) > BPF_ALU_END)
279
7
    return false;
280
  /* ALU64 class doesn't have ENDian */
281
  /* ENDian's imm must be one of 16, 32, 64 */
282
4.24k
  if (BPF_OP(bpf->op) == BPF_ALU_END) {
283
746
    if (BPF_CLASS(bpf->op) == BPF_CLASS_ALU64)
284
1
      return false;
285
745
    if (bpf->k != 16 && bpf->k != 32 && bpf->k != 64)
286
41
      return false;
287
745
  }
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
4.20k
  CHECK_WRITABLE_AND_PUSH(ud, MI, bpf->dst);
296
297
  /* special cases */
298
4.19k
  if (BPF_OP(bpf->op) == BPF_ALU_NEG)
299
294
    return true;
300
3.90k
  if (BPF_OP(bpf->op) == BPF_ALU_END) {
301
    /* bpf->k must be one of 16, 32, 64 */
302
703
    MCInst_setOpcode(MI, MCInst_getOpcode(MI) | ((uint32_t)bpf->k << 4));
303
703
    return true;
304
703
  }
305
306
  /* normal cases */
307
3.19k
  if (BPF_SRC(bpf->op) == BPF_SRC_K) {
308
2.93k
    MCOperand_CreateImm0(MI, bpf->k);
309
2.93k
  }
310
261
  else { /* BPF_SRC_X */
311
261
    CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
312
261
  }
313
3.19k
  return true;
314
3.19k
}
315
316
static bool decodeJump(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
317
3.57k
{
318
  /* cBPF and eBPF are very different in class jump */
319
3.57k
  if (!EBPF_MODE(ud)) {
320
962
    if (BPF_OP(bpf->op) > BPF_JUMP_JSET)
321
2
      return false;
322
323
    /* ja is a special case of jumps */
324
960
    if (BPF_OP(bpf->op) == BPF_JUMP_JA) {
325
217
      MCOperand_CreateImm0(MI, bpf->k);
326
217
      return true;
327
217
    }
328
329
743
    if (BPF_SRC(bpf->op) == BPF_SRC_K)
330
317
      MCOperand_CreateImm0(MI, bpf->k);
331
426
    else /* BPF_SRC_X */
332
426
      MCOperand_CreateReg0(MI, BPF_REG_X);
333
743
    MCOperand_CreateImm0(MI, bpf->jt);
334
743
    MCOperand_CreateImm0(MI, bpf->jf);
335
743
  }
336
2.61k
  else {
337
2.61k
    if (BPF_OP(bpf->op) > BPF_JUMP_JSLE)
338
1
      return false;
339
340
    /* No operands for exit */
341
2.61k
    if (BPF_OP(bpf->op) == BPF_JUMP_EXIT)
342
262
      return bpf->op == (BPF_CLASS_JMP | BPF_JUMP_EXIT);
343
2.34k
    if (BPF_OP(bpf->op) == BPF_JUMP_CALL) {
344
593
      if (bpf->op == (BPF_CLASS_JMP | BPF_JUMP_CALL)) {
345
264
        MCOperand_CreateImm0(MI, bpf->k);
346
264
        return true;
347
264
      }
348
329
      if (bpf->op == (BPF_CLASS_JMP | BPF_JUMP_CALL | BPF_SRC_X)) {
349
329
        CHECK_READABLE_AND_PUSH(ud, MI, bpf->k);
350
288
        return true;
351
329
      }
352
0
      return false;
353
329
    }
354
355
    /* ja is a special case of jumps */
356
1.75k
    if (BPF_OP(bpf->op) == BPF_JUMP_JA) {
357
307
      if (BPF_SRC(bpf->op) != BPF_SRC_K)
358
1
        return false;
359
306
      MCOperand_CreateImm0(MI, bpf->offset);
360
306
      return true;
361
307
    }
362
363
    /* <j>  dst, src, +off */
364
1.44k
    CHECK_READABLE_AND_PUSH(ud, MI, bpf->dst);
365
1.44k
    if (BPF_SRC(bpf->op) == BPF_SRC_K)
366
1.13k
      MCOperand_CreateImm0(MI, bpf->k);
367
307
    else
368
307
      CHECK_READABLE_AND_PUSH(ud, MI, bpf->src);
369
1.44k
    MCOperand_CreateImm0(MI, bpf->offset);
370
1.44k
  }
371
2.18k
  return true;
372
3.57k
}
373
374
static bool decodeReturn(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
375
1.16k
{
376
  /* Here only handles the BPF_RET class in cBPF */
377
1.16k
  switch (BPF_RVAL(bpf->op)) {
378
449
  case BPF_SRC_K:
379
449
    MCOperand_CreateImm0(MI, bpf->k);
380
449
    return true;
381
289
  case BPF_SRC_X:
382
289
    MCOperand_CreateReg0(MI, BPF_REG_X);
383
289
    return true;
384
423
  case BPF_SRC_A:
385
423
    MCOperand_CreateReg0(MI, BPF_REG_A);
386
423
    return true;
387
1.16k
  }
388
4
  return false;
389
1.16k
}
390
391
static bool decodeMISC(cs_struct *ud, MCInst *MI, bpf_internal *bpf)
392
708
{
393
708
  uint16_t op = bpf->op ^ BPF_CLASS_MISC;
394
708
  return op == BPF_MISCOP_TAX || op == BPF_MISCOP_TXA;
395
708
}
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
17.2k
{
402
17.2k
  cs_detail *detail;
403
404
17.2k
  detail = MI->flat_insn->detail;
405
  // initialize detail
406
17.2k
  if (detail) {
407
17.2k
    memset(detail, 0, offsetof(cs_detail, bpf) + sizeof(cs_bpf));
408
17.2k
  }
409
410
17.2k
  MCInst_clear(MI);
411
17.2k
  MCInst_setOpcode(MI, bpf->op);
412
413
17.2k
  switch (BPF_CLASS(bpf->op)) {
414
0
  default: /* should never happen */
415
0
    return false;
416
3.74k
  case BPF_CLASS_LD:
417
4.63k
  case BPF_CLASS_LDX:
418
4.63k
    return decodeLoad(ud, MI, bpf);
419
860
  case BPF_CLASS_ST:
420
1.80k
  case BPF_CLASS_STX:
421
1.80k
    return decodeStore(ud, MI, bpf);
422
3.64k
  case BPF_CLASS_ALU:
423
3.64k
    return decodeALU(ud, MI, bpf);
424
3.57k
  case BPF_CLASS_JMP:
425
3.57k
    return decodeJump(ud, MI, bpf);
426
680
  case BPF_CLASS_RET:
427
    /* eBPF doesn't have this class */
428
680
    if (EBPF_MODE(ud))
429
5
      return false;
430
675
    return decodeReturn(ud, MI, bpf);
431
2.93k
  case BPF_CLASS_MISC:
432
  /* case BPF_CLASS_ALU64: */
433
2.93k
    if (EBPF_MODE(ud))
434
2.28k
      return decodeALU(ud, MI, bpf);
435
647
    else
436
647
      return decodeMISC(ud, MI, bpf);
437
17.2k
  }
438
17.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
17.5k
{
443
17.5k
  cs_struct *cs;
444
17.5k
  bpf_internal *bpf;
445
446
17.5k
  cs = (cs_struct*)ud;
447
17.5k
  if (EBPF_MODE(cs))
448
10.4k
    bpf = fetch_ebpf(cs, code, code_len);
449
7.11k
  else
450
7.11k
    bpf = fetch_cbpf(cs, code, code_len);
451
17.5k
  if (bpf == NULL)
452
266
    return false;
453
17.2k
  if (!getInstruction(cs, instr, bpf)) {
454
216
    cs_mem_free(bpf);
455
216
    return false;
456
216
  }
457
458
17.0k
  *size = bpf->insn_size;
459
17.0k
  cs_mem_free(bpf);
460
461
  return true;
462
17.2k
}
463
464
#endif