Coverage Report

Created: 2025-12-05 06:11

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