Coverage Report

Created: 2026-03-13 06:50

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