Class: SeccompTools::CLI::Emu

Inherits:
Base
  • Object
show all
Defined in:
lib/seccomp-tools/cli/emu.rb

Overview

Handle 'emu' command.

Constant Summary collapse

SUMMARY =

Summary of this command.

'Emulate seccomp rules.'
USAGE =

Usage of this command.

"emu - #{SUMMARY}\n\nUsage: seccomp-tools emu [options] BPF_FILE [sys_nr [arg0 [arg1 ... arg5]]]".freeze

Instance Attribute Summary

Attributes inherited from Base

#argv, #option

Instance Method Summary collapse

Constructor Details

#initializeEmu

Instantiate an SeccompTools::CLI::Emu object, defaulting to verbose output.

Takes the same arguments as Base#initialize.



23
24
25
26
# File 'lib/seccomp-tools/cli/emu.rb', line 23

def initialize(*)
  super
  option[:verbose] = 1
end

Instance Method Details

#handlevoid

This method returns an undefined value.

Emulates the filter against the given syscall and shows the resulting action.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/seccomp-tools/cli/emu.rb', line 49

def handle
  return unless super

  option[:ifile] = argv.shift
  return CLI.show(parser.help) if option[:ifile].nil?

  raw = input
  insts = SeccompTools::Disasm.to_bpf(raw, option[:arch]).map(&:inst)
  sys, *args = argv
  sys = evaluate_sys_nr(sys) if sys
  args.map! { |v| Integer(v) }
  trace = Set.new
  res = SeccompTools::Emulator.new(
    insts,
    sys_nr: sys,
    args:,
    instruction_pointer: option[:instruction_pointer] && Integer(option[:instruction_pointer]),
    arch: option[:arch]
  ).run

  if option[:verbose] >= 1
    disasm = SeccompTools::Disasm.disasm(raw, arch: option[:arch]).lines
    output_emulate_path(disasm, trace, res)
  end
  output do
    ret_type = Const::BPF::ACTION.invert[res[:ret] & Const::BPF::SECCOMP_RET_ACTION_FULL]
    errno = ret_type == :ERRNO ? "(#{res[:ret] & Const::BPF::SECCOMP_RET_DATA})" : ''
    format("return %s%s at line %04d\n", ret_type, errno, res[:pc])
  end
end

#parserOptionParser

Define option parser.

Returns:

  • (OptionParser)

    The parser of this command's options.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/seccomp-tools/cli/emu.rb', line 31

def parser
  @parser ||= OptionParser.new do |opt|
    opt.banner = usage

    option_arch(opt)

    opt.on('-q', '--[no-]quiet', 'Run quietly, only show emulation result.') do |v|
      option[:verbose] = 0 if v
    end

    opt.on('-i', '--ip=VAL', Integer, 'Set instruction pointer.') do |val|
      option[:instruction_pointer] = val
    end
  end
end