Class: SeccompTools::Emulator

Inherits:
Object
  • Object
show all
Defined in:
lib/seccomp-tools/emulator.rb

Overview

For emulating seccomp.

Instance Method Summary collapse

Constructor Details

#initialize(instructions, sys_nr: nil, args: [], instruction_pointer: nil, arch: nil) ⇒ Emulator

Instantiate a SeccompTools::Emulator object.

All parameters except instructions are optional. A warning is shown when uninitialized data is accessed.

Parameters:

  • instructions (Array<Instruction::Base>)
  • sys_nr (Integer) (defaults to: nil)

    Syscall number.

  • args (Array<Integer>) (defaults to: [])

    Syscall arguments

  • instruction_pointer (Integer) (defaults to: nil)

    Program counter address when this syscall invoked.

  • arch (Symbol?) (defaults to: nil)

    System architecture is used when this parameter is not provided.

    See Util.supported_archs for list of supported architectures.



22
23
24
25
26
27
28
# File 'lib/seccomp-tools/emulator.rb', line 22

def initialize(instructions, sys_nr: nil, args: [], instruction_pointer: nil, arch: nil)
  @instructions = instructions
  @sys_nr = sys_nr
  @args = args
  @ip = instruction_pointer
  @arch = audit(arch || Util.system_arch)
end

Instance Method Details

#run{Symbol, Integer => Integer}

Run emulation!

Returns:

  • ({Symbol, Integer => Integer})


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/seccomp-tools/emulator.rb', line 32

def run
  @values = { pc: 0, a: 0, x: 0 }
  loop do
    break if @values[:ret] # break when returned

    yield(@values) if block_given?
    inst = @instructions[pc]
    op, *args = inst.symbolize
    case op
    when :ret then ret(args.first) # ret
    when :ld then ld(args[0], args[1]) # ld/ldx
    when :st then st(args[0], args[1]) # st/stx
    when :jmp then jmp(args[0]) # directly jmp
    when :cmp then cmp(*args[0, 4]) # jmp with comparison
    when :alu then alu(args[0], args[1]) # alu
    when :misc then misc(args[0]) # misc: txa/tax
    end
    set(:pc, get(:pc) + 1) if %i[ld st alu misc].include?(op)
  end
  @values
end