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/trap_registry.h"
10 #include "sandbox/linux/seccomp-bpf/codegen.h"
11 #include "sandbox/linux/seccomp-bpf/linux_seccomp.h"
16 void DumpBPF::PrintProgram(const CodeGen::Program
& program
) {
17 for (CodeGen::Program::const_iterator iter
= program
.begin();
18 iter
!= program
.end();
20 int ip
= (int)(iter
- program
.begin());
21 fprintf(stderr
, "%3d) ", ip
);
22 switch (BPF_CLASS(iter
->code
)) {
24 if (iter
->code
== BPF_LD
+ BPF_W
+ BPF_ABS
) {
25 fprintf(stderr
, "LOAD %d // ", (int)iter
->k
);
26 if (iter
->k
== offsetof(struct arch_seccomp_data
, nr
)) {
27 fprintf(stderr
, "System call number\n");
28 } else if (iter
->k
== offsetof(struct arch_seccomp_data
, arch
)) {
29 fprintf(stderr
, "Architecture\n");
31 offsetof(struct arch_seccomp_data
, instruction_pointer
)) {
32 fprintf(stderr
, "Instruction pointer (LSB)\n");
34 offsetof(struct arch_seccomp_data
, instruction_pointer
) +
36 fprintf(stderr
, "Instruction pointer (MSB)\n");
37 } else if (iter
->k
>= offsetof(struct arch_seccomp_data
, args
) &&
38 iter
->k
< offsetof(struct arch_seccomp_data
, args
) + 48 &&
39 (iter
->k
- offsetof(struct arch_seccomp_data
, args
)) % 4 ==
43 "Argument %d (%cSB)\n",
44 (int)(iter
->k
- offsetof(struct arch_seccomp_data
, args
)) / 8,
45 (iter
->k
- offsetof(struct arch_seccomp_data
, args
)) % 8 ? 'M'
48 fprintf(stderr
, "???\n");
51 fprintf(stderr
, "LOAD ???\n");
55 if (BPF_OP(iter
->code
) == BPF_JA
) {
56 fprintf(stderr
, "JMP %d\n", ip
+ iter
->k
+ 1);
58 fprintf(stderr
, "if A %s 0x%x; then JMP %d else JMP %d\n",
59 BPF_OP(iter
->code
) == BPF_JSET
? "&" :
60 BPF_OP(iter
->code
) == BPF_JEQ
? "==" :
61 BPF_OP(iter
->code
) == BPF_JGE
? ">=" :
62 BPF_OP(iter
->code
) == BPF_JGT
? ">" : "???",
64 ip
+ iter
->jt
+ 1, ip
+ iter
->jf
+ 1);
68 fprintf(stderr
, "RET 0x%x // ", iter
->k
);
69 if ((iter
->k
& SECCOMP_RET_ACTION
) == SECCOMP_RET_TRAP
) {
70 fprintf(stderr
, "Trap #%d\n", iter
->k
& SECCOMP_RET_DATA
);
71 } else if ((iter
->k
& SECCOMP_RET_ACTION
) == SECCOMP_RET_ERRNO
) {
72 fprintf(stderr
, "errno = %d\n", iter
->k
& SECCOMP_RET_DATA
);
73 } else if ((iter
->k
& SECCOMP_RET_ACTION
) == SECCOMP_RET_TRACE
) {
74 fprintf(stderr
, "Trace #%d\n", iter
->k
& SECCOMP_RET_DATA
);
75 } else if (iter
->k
== SECCOMP_RET_ALLOW
) {
76 fprintf(stderr
, "Allowed\n");
78 fprintf(stderr
, "???\n");
82 fprintf(stderr
, BPF_OP(iter
->code
) == BPF_NEG
83 ? "A := -A\n" : "A := A %s 0x%x\n",
84 BPF_OP(iter
->code
) == BPF_ADD
? "+" :
85 BPF_OP(iter
->code
) == BPF_SUB
? "-" :
86 BPF_OP(iter
->code
) == BPF_MUL
? "*" :
87 BPF_OP(iter
->code
) == BPF_DIV
? "/" :
88 BPF_OP(iter
->code
) == BPF_MOD
? "%" :
89 BPF_OP(iter
->code
) == BPF_OR
? "|" :
90 BPF_OP(iter
->code
) == BPF_XOR
? "^" :
91 BPF_OP(iter
->code
) == BPF_AND
? "&" :
92 BPF_OP(iter
->code
) == BPF_LSH
? "<<" :
93 BPF_OP(iter
->code
) == BPF_RSH
? ">>" : "???",
97 fprintf(stderr
, "???\n");
104 } // namespace bpf_dsl
105 } // namespace sandbox