Class: SeccompTools::BPF
- Inherits:
-
Object
- Object
- SeccompTools::BPF
- Defined in:
- lib/seccomp-tools/bpf.rb
Overview
One BPF instruction, i.e. a struct sock_filter.
Beyond the four fields of the C struct, a BPF also carries the architecture it belongs to and its line number, which together allow it to be disassembled into readable assembly.
Instance Attribute Summary collapse
-
#arch ⇒ Symbol
readonly
Architecture.
-
#code ⇒ Integer
readonly
BPF code.
-
#jf ⇒ Integer
readonly
BPF JF.
-
#jt ⇒ Integer
readonly
BPF JT.
-
#k ⇒ Integer
readonly
BPF K.
-
#line ⇒ Integer
readonly
Line number.
-
#states ⇒ Set<SeccompTools::Symbolic::State>
Possible states before this instruction.
Instance Method Summary collapse
-
#asm ⇒ String
Convert to raw bytes.
-
#branch(state) {|pc, st| ... } ⇒ void
Yields every branch that may be taken after executing this instruction.
-
#command ⇒ Symbol?
Command according to
code. -
#decompile ⇒ String
Decompile.
-
#disasm(**options) ⇒ String
Pretty display the disassemble result.
-
#initialize(raw, arch, line) ⇒ BPF
constructor
Instantiate a BPF object.
-
#inst ⇒ SeccompTools::Instruction::Base
Corresponding instruction object.
-
#show_arg_infer? ⇒ Boolean
Whether the syscall argument names need to be inferred.
-
#show_code? ⇒ Boolean
Whether the raw
code,jt,jf,kfields need to be dumped.
Constructor Details
#initialize(raw, arch, line) ⇒ BPF
Instantiate a SeccompTools::BPF object.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/seccomp-tools/bpf.rb', line 38 def initialize(raw, arch, line) if raw.is_a?(String) io = ::StringIO.new(raw) endian = Const::Endian::ENDIAN[arch] @code = io.read(2).unpack1("S#{endian}") @jt = io.read(1).ord @jf = io.read(1).ord @k = io.read(4).unpack1("L#{endian}") else @code = raw[:code] @jt = raw[:jt] @jf = raw[:jf] @k = raw[:k] end @arch = arch @line = line @states = Set.new @disasm_setting = { code: true, arg_infer: true } end |
Instance Attribute Details
#arch ⇒ Symbol (readonly)
Returns Architecture.
26 27 28 |
# File 'lib/seccomp-tools/bpf.rb', line 26 def arch @arch end |
#code ⇒ Integer (readonly)
Returns BPF code.
18 19 20 |
# File 'lib/seccomp-tools/bpf.rb', line 18 def code @code end |
#jf ⇒ Integer (readonly)
Returns BPF JF.
22 23 24 |
# File 'lib/seccomp-tools/bpf.rb', line 22 def jf @jf end |
#jt ⇒ Integer (readonly)
Returns BPF JT.
20 21 22 |
# File 'lib/seccomp-tools/bpf.rb', line 20 def jt @jt end |
#k ⇒ Integer (readonly)
Returns BPF K.
24 25 26 |
# File 'lib/seccomp-tools/bpf.rb', line 24 def k @k end |
#line ⇒ Integer (readonly)
Returns Line number.
16 17 18 |
# File 'lib/seccomp-tools/bpf.rb', line 16 def line @line end |
#states ⇒ Set<SeccompTools::Symbolic::State>
Returns Possible states before this instruction.
28 29 30 |
# File 'lib/seccomp-tools/bpf.rb', line 28 def states @states end |
Instance Method Details
#asm ⇒ String
Convert to raw bytes.
94 95 96 97 |
# File 'lib/seccomp-tools/bpf.rb', line 94 def asm endian = Const::Endian::ENDIAN[arch] [code, jt, jf, k].pack("S#{endian}CCL#{endian}") end |
#branch(state) {|pc, st| ... } ⇒ void
This method returns an undefined value.
Yields every branch that may be taken after executing this instruction.
121 122 123 |
# File 'lib/seccomp-tools/bpf.rb', line 121 def branch(state, &) inst.branch(state).each(&) end |
#command ⇒ Symbol?
Command according to code.
102 103 104 |
# File 'lib/seccomp-tools/bpf.rb', line 102 def command Const::BPF::COMMAND.invert[code & 7] end |
#decompile ⇒ String
Decompile.
109 110 111 |
# File 'lib/seccomp-tools/bpf.rb', line 109 def decompile inst.decompile end |
#disasm(**options) ⇒ String
Pretty display the disassemble result.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/seccomp-tools/bpf.rb', line 68 def disasm(**) @disasm_setting.merge!() if show_code? format(' %04d: 0x%02x 0x%02x 0x%02x 0x%08x %s', line, code, jt, jf, k, decompile) else format('%04d: %s', line, decompile) end end |
#inst ⇒ SeccompTools::Instruction::Base
Corresponding instruction object.
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/seccomp-tools/bpf.rb', line 127 def inst @inst ||= case command when :alu then SeccompTools::Instruction::ALU when :jmp then SeccompTools::Instruction::JMP when :ld then SeccompTools::Instruction::LD when :ldx then SeccompTools::Instruction::LDX when :misc then SeccompTools::Instruction::MISC when :ret then SeccompTools::Instruction::RET when :st then SeccompTools::Instruction::ST when :stx then SeccompTools::Instruction::STX end.new(self) end |
#show_arg_infer? ⇒ Boolean
Whether the syscall argument names need to be inferred.
87 88 89 |
# File 'lib/seccomp-tools/bpf.rb', line 87 def show_arg_infer? @disasm_setting[:arg_infer] end |
#show_code? ⇒ Boolean
Whether the raw code, jt, jf, k fields need to be dumped.
81 82 83 |
# File 'lib/seccomp-tools/bpf.rb', line 81 def show_code? @disasm_setting[:code] end |