Debian Codegraph @

tools/scmp_bpf_disasm.c

raw
1 /**
2 * BPF Disassembler
3 *
4 * Copyright (c) 2012 Red Hat <[email protected]>
5 * Author: Paul Moore <[email protected]>
6 */
7
8 /*
9 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of version 2.1 of the GNU Lesser General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This library is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, see <http://www.gnu.org/licenses>.
20 */
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <linux/audit.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34
35 #include "bpf.h"
36 #include "util.h"
37
38 #define _OP_FMT "%-3s"
39
40 /**
41 * Print the usage information to stderr and exit
42 * @param program the name of the current program being invoked
43 *
44 * Print the usage information and exit with EINVAL.
45 *
46 */
47 static void exit_usage(const char *program)
48 {
49 fprintf(stderr, "usage: %s -a <arch> [-d] [-h]\n", program);
50 exit(EINVAL);
51 }
52
53 /**
54 * Decode the BPF operand
55 * @param bpf the BPF instruction
56 *
57 * Decode the BPF operand and print it to stdout.
58 *
59 */
60 static const char *bpf_decode_op(const bpf_instr_raw *bpf)
61 {
62 switch (bpf->code) {
63 case BPF_LD+BPF_W+BPF_IMM:
64 case BPF_LD+BPF_W+BPF_ABS:
65 case BPF_LD+BPF_W+BPF_IND:
66 case BPF_LD+BPF_W+BPF_MEM:
67 case BPF_LD+BPF_W+BPF_LEN:
68 case BPF_LD+BPF_W+BPF_MSH:
69 return "ld";
70 case BPF_LD+BPF_H+BPF_IMM:
71 case BPF_LD+BPF_H+BPF_ABS:
72 case BPF_LD+BPF_H+BPF_IND:
73 case BPF_LD+BPF_H+BPF_MEM:
74 case BPF_LD+BPF_H+BPF_LEN:
75 case BPF_LD+BPF_H+BPF_MSH:
76 return "ldh";
77 case BPF_LD+BPF_B+BPF_IMM:
78 case BPF_LD+BPF_B+BPF_ABS:
79 case BPF_LD+BPF_B+BPF_IND:
80 case BPF_LD+BPF_B+BPF_MEM:
81 case BPF_LD+BPF_B+BPF_LEN:
82 case BPF_LD+BPF_B+BPF_MSH:
83 return "ldb";
84 case BPF_LDX+BPF_W+BPF_IMM:
85 case BPF_LDX+BPF_W+BPF_ABS:
86 case BPF_LDX+BPF_W+BPF_IND:
87 case BPF_LDX+BPF_W+BPF_MEM:
88 case BPF_LDX+BPF_W+BPF_LEN:
89 case BPF_LDX+BPF_W+BPF_MSH:
90 case BPF_LDX+BPF_H+BPF_IMM:
91 case BPF_LDX+BPF_H+BPF_ABS:
92 case BPF_LDX+BPF_H+BPF_IND:
93 case BPF_LDX+BPF_H+BPF_MEM:
94 case BPF_LDX+BPF_H+BPF_LEN:
95 case BPF_LDX+BPF_H+BPF_MSH:
96 case BPF_LDX+BPF_B+BPF_IMM:
97 case BPF_LDX+BPF_B+BPF_ABS:
98 case BPF_LDX+BPF_B+BPF_IND:
99 case BPF_LDX+BPF_B+BPF_MEM:
100 case BPF_LDX+BPF_B+BPF_LEN:
101 case BPF_LDX+BPF_B+BPF_MSH:
102 return "ldx";
103 case BPF_ST:
104 return "st";
105 case BPF_STX:
106 return "stx";
107 case BPF_ALU+BPF_ADD+BPF_K:
108 case BPF_ALU+BPF_ADD+BPF_X:
109 return "add";
110 case BPF_ALU+BPF_SUB+BPF_K:
111 case BPF_ALU+BPF_SUB+BPF_X:
112 return "sub";
113 case BPF_ALU+BPF_MUL+BPF_K:
114 case BPF_ALU+BPF_MUL+BPF_X:
115 return "mul";
116 case BPF_ALU+BPF_DIV+BPF_K:
117 case BPF_ALU+BPF_DIV+BPF_X:
118 return "div";
119 case BPF_ALU+BPF_OR+BPF_K:
120 case BPF_ALU+BPF_OR+BPF_X:
121 return "or";
122 case BPF_ALU+BPF_AND+BPF_K:
123 case BPF_ALU+BPF_AND+BPF_X:
124 return "and";
125 case BPF_ALU+BPF_LSH+BPF_K:
126 case BPF_ALU+BPF_LSH+BPF_X:
127 return "lsh";
128 case BPF_ALU+BPF_RSH+BPF_K:
129 case BPF_ALU+BPF_RSH+BPF_X:
130 return "rsh";
131 case BPF_ALU+BPF_NEG+BPF_K:
132 case BPF_ALU+BPF_NEG+BPF_X:
133 return "neg";
134 case BPF_ALU+BPF_MOD+BPF_K:
135 case BPF_ALU+BPF_MOD+BPF_X:
136 return "mod";
137 case BPF_ALU+BPF_XOR+BPF_K:
138 case BPF_ALU+BPF_XOR+BPF_X:
139 return "xor";
140 case BPF_JMP+BPF_JA+BPF_K:
141 case BPF_JMP+BPF_JA+BPF_X:
142 return "jmp";
143 case BPF_JMP+BPF_JEQ+BPF_K:
144 case BPF_JMP+BPF_JEQ+BPF_X:
145 return "jeq";
146 case BPF_JMP+BPF_JGT+BPF_K:
147 case BPF_JMP+BPF_JGT+BPF_X:
148 return "jgt";
149 case BPF_JMP+BPF_JGE+BPF_K:
150 case BPF_JMP+BPF_JGE+BPF_X:
151 return "jge";
152 case BPF_JMP+BPF_JSET+BPF_K:
153 case BPF_JMP+BPF_JSET+BPF_X:
154 return "jset";
155 case BPF_RET+BPF_K:
156 case BPF_RET+BPF_X:
157 case BPF_RET+BPF_A:
158 return "ret";
159 case BPF_MISC+BPF_TAX:
160 return "tax";
161 case BPF_MISC+BPF_TXA:
162 return "txa";
163 }
164 return "???";
165 }
166
167 /**
168 * Decode a RET action
169 * @param k the return action
170 *
171 * Decode the action and print it to stdout.
172 *
173 */
174 static void bpf_decode_action(uint32_t k)
175 {
176 uint32_t act = k & SECCOMP_RET_ACTION_FULL;
177 uint32_t data = k & SECCOMP_RET_DATA;
178
179 switch (act) {
180 case SECCOMP_RET_KILL_PROCESS:
181 printf("KILL_PROCESS");
182 break;
183 case SECCOMP_RET_KILL_THREAD:
184 printf("KILL");
185 break;
186 case SECCOMP_RET_TRAP:
187 printf("TRAP");
188 break;
189 case SECCOMP_RET_ERRNO:
190 printf("ERRNO(%u)", data);
191 break;
192 case SECCOMP_RET_TRACE:
193 printf("TRACE(%u)", data);
194 break;
195 case SECCOMP_RET_LOG:
196 printf("LOG");
197 break;
198 case SECCOMP_RET_ALLOW:
199 printf("ALLOW");
200 break;
201 default:
202 printf("0x%.8x", k);
203 }
204 }
205
206 /**
207 * Decode the BPF arguments (JT, JF, and K)
208 * @param bpf the BPF instruction
209 * @param line the current line number
210 *
211 * Decode the BPF arguments (JT, JF, and K) and print the relevant information
212 * to stdout based on the operand.
213 *
214 */
215 static void bpf_decode_args(const bpf_instr_raw *bpf, unsigned int line)
216 {
217 switch (BPF_CLASS(bpf->code)) {
218 case BPF_LD:
219 case BPF_LDX:
220 switch (BPF_MODE(bpf->code)) {
221 case BPF_ABS:
222 printf("$data[%u]", bpf->k);
223 break;
224 case BPF_MEM:
225 printf("$temp[%u]", bpf->k);
226 break;
227 case BPF_IMM:
228 printf("%u", bpf->k);
229 break;
230 case BPF_IND:
231 printf("$data[X + %u]", bpf->k);
232 break;
233 case BPF_LEN:
234 printf("len($data)");
235 break;
236 case BPF_MSH:
237 printf("4 * $data[%u] & 0x0f", bpf->k);
238 break;
239 }
240 break;
241 case BPF_ST:
242 case BPF_STX:
243 printf("$temp[%u]", bpf->k);
244 break;
245 case BPF_ALU:
246 if (BPF_SRC(bpf->code) == BPF_K) {
247 switch (BPF_OP(bpf->code)) {
248 case BPF_OR:
249 case BPF_AND:
250 printf("0x%.8x", bpf->k);
251 break;
252 default:
253 printf("%u", bpf->k);
254 }
255 } else
256 printf("%u", bpf->k);
257 break;
258 case BPF_JMP:
259 if (BPF_OP(bpf->code) == BPF_JA) {
260 printf("%.4u", (line + 1) + bpf->k);
261 } else {
262 printf("%-4u true:%.4u false:%.4u",
263 bpf->k,
264 (line + 1) + bpf->jt,
265 (line + 1) + bpf->jf);
266 }
267 break;
268 case BPF_RET:
269 if (BPF_RVAL(bpf->code) == BPF_A) {
270 /* XXX - accumulator? */
271 printf("$acc");
272 } else if (BPF_SRC(bpf->code) == BPF_K) {
273 bpf_decode_action(bpf->k);
274 } else if (BPF_SRC(bpf->code) == BPF_X) {
275 /* XXX - any idea? */
276 printf("???");
277 }
278 break;
279 case BPF_MISC:
280 break;
281 default:
282 printf("???");
283 }
284 }
285
286 /**
287 * Perform a simple decoding of the BPF program
288 * @param file the BPF program
289 *
290 * Read the BPF program and display the instructions. Returns zero on success,
291 * non-zero values on failure.
292 *
293 */
294 static int bpf_decode(FILE *file)
295 {
296 unsigned int line = 0;
297 bpf_instr_raw bpf;
298
299 /* header */
300 printf(" line OP JT JF K\n");
301 printf("=================================\n");
302
303 while (fread(&bpf, sizeof(bpf), 1, file)) {
304 /* convert the bpf statement */
305 bpf.code = ttoh16(arch, bpf.code);
306 bpf.k = ttoh32(arch, bpf.k);
307
308 /* display a hex dump */
309 printf(" %.4u: 0x%.2x 0x%.2x 0x%.2x 0x%.8x",
310 line, bpf.code, bpf.jt, bpf.jf, bpf.k);
311
312 /* display the assembler statements */
313 printf(" ");
314 printf(_OP_FMT, bpf_decode_op(&bpf));
315 printf(" ");
316 bpf_decode_args(&bpf, line);
317 printf("\n");
318
319 line++;
320 }
321
322 if (ferror(file))
323 return errno;
324 return 0;
325 }
326
327 /**
328 * Decode the BPF arguments (JT, JF, and K)
329 * @param bpf the BPF instruction
330 * @param line the current line number
331 *
332 * Decode the BPF arguments (JT, JF, and K) and print the relevant information
333 * to stdout based on the operand.
334 *
335 */
336 static void bpf_dot_decode_args(const bpf_instr_raw *bpf, unsigned int line)
337 {
338 const char *op = bpf_decode_op(bpf);
339
340 printf("\tline%d[label=\"%s", line, op);
341 switch (BPF_CLASS(bpf->code)) {
342 case BPF_LD:
343 case BPF_LDX:
344 switch (BPF_MODE(bpf->code)) {
345 case BPF_ABS:
346 printf(" $data[%u]\",shape=parallelogram]\n", bpf->k);
347 break;
348 case BPF_MEM:
349 printf(" $temp[%u]\",shape=parallelogram]\n", bpf->k);
350 break;
351 case BPF_IMM:
352 printf(" %u\",shape=parallelogram]\n", bpf->k);
353 break;
354 case BPF_IND:
355 printf(" $data[X + %u]\",shape=parallelogram]\n", bpf->k);
356 break;
357 case BPF_LEN:
358 printf(" len($data)\",shape=parallelogram]\n");
359 break;
360 case BPF_MSH:
361 printf(" 4 * $data[%u] & 0x0f\",shape=parallelogram]\n", bpf->k);
362 break;
363 }
364 break;
365 case BPF_ST:
366 case BPF_STX:
367 printf(" $temp[%u]\",shape=parallelogram]\n",
368 bpf->k);
369 break;
370 case BPF_ALU:
371 if (BPF_SRC(bpf->code) == BPF_K) {
372 switch (BPF_OP(bpf->code)) {
373 case BPF_OR:
374 case BPF_AND:
375 printf(" 0x%.8x\",shape=rectangle]\n", bpf->k);
376 break;
377 default:
378 printf(" %u\",shape=rectangle]\n", bpf->k);
379 }
380 } else
381 printf(" %u\",shape=rectangle]\n", bpf->k);
382 break;
383 case BPF_JMP:
384 if (BPF_OP(bpf->code) == BPF_JA) {
385 printf("\",shape=hexagon]\n");
386 printf("\tline%d -> line%d\n",
387 line, (line + 1) + bpf->k);
388 } else {
389 printf(" %-4u", bpf->k);
390 /* Heuristic: if k > 256, also emit hex version */
391 if (bpf->k > 256)
392 printf("\\n(0x%.8x)", bpf->k);
393 printf("\",shape=diamond]\n");
394 printf("\tline%d -> line%d [label=\"true\"]\n",
395 line, (line + 1) + bpf->jt);
396 printf("\tline%d -> line%d [label=\"false\"]\n",
397 line, (line + 1) + bpf->jf);
398 }
399 break;
400 case BPF_RET:
401 if (BPF_RVAL(bpf->code) == BPF_A) {
402 /* XXX - accumulator? */
403 printf(" $acc\", shape=\"box\", style=rounded]\n");
404 } else if (BPF_SRC(bpf->code) == BPF_K) {
405 printf(" ");
406 bpf_decode_action(bpf->k);
407 printf("\", shape=\"box\", style=rounded]\n");
408 } else if (BPF_SRC(bpf->code) == BPF_X) {
409 /* XXX - any idea? */
410 printf(" ???\", shape=\"box\", style=rounded]\n");
411 }
412 break;
413 case BPF_MISC:
414 printf("\"]\n");
415 break;
416 default:
417 printf(" ???\"]\n");
418 }
419 }
420
421 /**
422 * Perform a simple decoding of the BPF program to a dot graph
423 * @param file the BPF program
424 *
425 * Read the BPF program and display the instructions. Returns zero on success,
426 * non-zero values on failure.
427 *
428 */
429 static int bpf_dot_decode(FILE *file)
430 {
431 unsigned int line = 0;
432 bpf_instr_raw bpf;
433 int prev_class = 0;
434
435 /* header */
436 printf("digraph {\n");
437 printf("\tstart[shape=\"box\", style=rounded];\n");
438
439 while (fread(&bpf, sizeof(bpf), 1, file)) {
440 /* convert the bpf statement */
441 bpf.code = ttoh16(arch, bpf.code);
442 bpf.k = ttoh32(arch, bpf.k);
443
444 /* display the statement */
445 bpf_dot_decode_args(&bpf, line);
446
447 /* if previous line wasn't RET/JMP, link it to this line */
448 if (line == 0)
449 printf("\tstart -> line%d\n", line);
450 else if ((prev_class != BPF_JMP) && (prev_class != BPF_RET))
451 printf("\tline%d -> line%d\n", line - 1, line);
452 prev_class = BPF_CLASS(bpf.code);
453
454 line++;
455 }
456 printf("}\n");
457
458 if (ferror(file))
459 return errno;
460 return 0;
461 }
462
463 /**
464 * main
465 */
466 int main(int argc, char *argv[])
467 {
468 int rc;
469 int opt;
470 bool dot_out = false;
471 FILE *file;
472
473 /* parse the command line */
474 while ((opt = getopt(argc, argv, "a:dh")) > 0) {
475 switch (opt) {
476 case 'a':
477 if (strcmp(optarg, "x86") == 0)
478 arch = AUDIT_ARCH_I386;
479 else if (strcmp(optarg, "x86_64") == 0)
480 arch = AUDIT_ARCH_X86_64;
481 else if (strcmp(optarg, "x32") == 0)
482 arch = AUDIT_ARCH_X86_64;
483 else if (strcmp(optarg, "arm") == 0)
484 arch = AUDIT_ARCH_ARM;
485 else if (strcmp(optarg, "aarch64") == 0)
486 arch = AUDIT_ARCH_AARCH64;
487 else if (strcmp(optarg, "loongarch64") == 0)
488 arch = AUDIT_ARCH_LOONGARCH64;
489 else if (strcmp(optarg, "mips") == 0)
490 arch = AUDIT_ARCH_MIPS;
491 else if (strcmp(optarg, "mipsel") == 0)
492 arch = AUDIT_ARCH_MIPSEL;
493 else if (strcmp(optarg, "mips64") == 0)
494 arch = AUDIT_ARCH_MIPS64;
495 else if (strcmp(optarg, "mipsel64") == 0)
496 arch = AUDIT_ARCH_MIPSEL64;
497 else if (strcmp(optarg, "mips64n32") == 0)
498 arch = AUDIT_ARCH_MIPS64N32;
499 else if (strcmp(optarg, "mipsel64n32") == 0)
500 arch = AUDIT_ARCH_MIPSEL64N32;
501 else if (strcmp(optarg, "ppc64") == 0)
502 arch = AUDIT_ARCH_PPC64;
503 else if (strcmp(optarg, "ppc64le") == 0)
504 arch = AUDIT_ARCH_PPC64LE;
505 else if (strcmp(optarg, "ppc") == 0)
506 arch = AUDIT_ARCH_PPC;
507 else if (strcmp(optarg, "s390") == 0)
508 arch = AUDIT_ARCH_S390;
509 else if (strcmp(optarg, "s390x") == 0)
510 arch = AUDIT_ARCH_S390X;
511 else if (strcmp(optarg, "riscv64") == 0)
512 arch = AUDIT_ARCH_RISCV64;
513 else
514 exit_usage(argv[0]);
515 break;
516 case 'd':
517 dot_out = true;
518 break;
519 default:
520 /* usage information */
521 exit_usage(argv[0]);
522 }
523 }
524
525 if ((optind > 1) && (optind < argc)) {
526 int opt_file = optind - 1 ;
527 file = fopen(argv[opt_file], "r");
528 if (file == NULL) {
529 fprintf(stderr, "error: unable to open \"%s\" (%s)\n",
530 argv[opt_file], strerror(errno));
531 return errno;
532 }
533 } else
534 file = stdin;
535
536 if (dot_out)
537 rc = bpf_dot_decode(file);
538 else
539 rc = bpf_decode(file);
540 fclose(file);
541
542 return rc;
543 }