Coverage Report

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