1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sandbox/linux/bpf_dsl/dump_bpf.h"
9 #include "sandbox/linux/bpf_dsl/codegen.h"
10 #include "sandbox/linux/bpf_dsl/trap_registry.h"
11 #include "sandbox/linux/system_headers/linux_filter.h"
12 #include "sandbox/linux/system_headers/linux_seccomp.h"
17 void DumpBPF::PrintProgram(const CodeGen::Program
& program
) {
18 for (CodeGen::Program::const_iterator iter
= program
.begin();
19 iter
!= program
.end();
21 int ip
= (int)(iter
- program
.begin());
22 fprintf(stderr
, "%3d) ", ip
);
23 switch (BPF_CLASS(iter
->code
)) {
25 if (iter
->code
== BPF_LD
+ BPF_W
+ BPF_ABS
) {
26 fprintf(stderr
, "LOAD %d // ", (int)iter
->k
);
27 if (iter
->k
== offsetof(struct arch_seccomp_data
, nr
)) {
28 fprintf(stderr
, "System call number\n");
29 } else if (iter
->k
== offsetof(struct arch_seccomp_data
, arch
)) {
30 fprintf(stderr
, "Architecture\n");
32 offsetof(struct arch_seccomp_data
, instruction_pointer
)) {
33 fprintf(stderr
, "Instruction pointer (LSB)\n");
35 offsetof(struct arch_seccomp_data
, instruction_pointer
) +
37 fprintf(stderr
, "Instruction pointer (MSB)\n");
38 } else if (iter
->k
>= offsetof(struct arch_seccomp_data
, args
) &&
39 iter
->k
< offsetof(struct arch_seccomp_data
, args
) + 48 &&
40 (iter
->k
- offsetof(struct arch_seccomp_data
, args
)) % 4 ==
44 "Argument %d (%cSB)\n",
45 (int)(iter
->k
- offsetof(struct arch_seccomp_data
, args
)) / 8,
46 (iter
->k
- offsetof(struct arch_seccomp_data
, args
)) % 8 ? 'M'
49 fprintf(stderr
, "???\n");
52 fprintf(stderr
, "LOAD ???\n");
56 if (BPF_OP(iter
->code
) == BPF_JA
) {
57 fprintf(stderr
, "JMP %d\n", ip
+ iter
->k
+ 1);
59 fprintf(stderr
, "if A %s 0x%x; then JMP %d else JMP %d\n",
60 BPF_OP(iter
->code
) == BPF_JSET
? "&" :
61 BPF_OP(iter
->code
) == BPF_JEQ
? "==" :
62 BPF_OP(iter
->code
) == BPF_JGE
? ">=" :
63 BPF_OP(iter
->code
) == BPF_JGT
? ">" : "???",
65 ip
+ iter
->jt
+ 1, ip
+ iter
->jf
+ 1);
69 fprintf(stderr
, "RET 0x%x // ", iter
->k
);
70 if ((iter
->k
& SECCOMP_RET_ACTION
) == SECCOMP_RET_TRAP
) {
71 fprintf(stderr
, "Trap #%d\n", iter
->k
& SECCOMP_RET_DATA
);
72 } else if ((iter
->k
& SECCOMP_RET_ACTION
) == SECCOMP_RET_ERRNO
) {
73 fprintf(stderr
, "errno = %d\n", iter
->k
& SECCOMP_RET_DATA
);
74 } else if ((iter
->k
& SECCOMP_RET_ACTION
) == SECCOMP_RET_TRACE
) {
75 fprintf(stderr
, "Trace #%d\n", iter
->k
& SECCOMP_RET_DATA
);
76 } else if (iter
->k
== SECCOMP_RET_ALLOW
) {
77 fprintf(stderr
, "Allowed\n");
79 fprintf(stderr
, "???\n");
83 if (BPF_OP(iter
->code
) == BPF_NEG
) {
84 fprintf(stderr
, "A := -A\n");
86 fprintf(stderr
, "A := A %s 0x%x\n",
87 BPF_OP(iter
->code
) == BPF_ADD
? "+" :
88 BPF_OP(iter
->code
) == BPF_SUB
? "-" :
89 BPF_OP(iter
->code
) == BPF_MUL
? "*" :
90 BPF_OP(iter
->code
) == BPF_DIV
? "/" :
91 BPF_OP(iter
->code
) == BPF_MOD
? "%" :
92 BPF_OP(iter
->code
) == BPF_OR
? "|" :
93 BPF_OP(iter
->code
) == BPF_XOR
? "^" :
94 BPF_OP(iter
->code
) == BPF_AND
? "&" :
95 BPF_OP(iter
->code
) == BPF_LSH
? "<<" :
96 BPF_OP(iter
->code
) == BPF_RSH
? ">>" : "???",
101 fprintf(stderr
, "???\n");
108 } // namespace bpf_dsl
109 } // namespace sandbox