Coverage Report

Created: 2026-07-16 06:55

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