Skip to content

Commit

Permalink
Don't raise error on long direct jump (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
david942j authored Dec 25, 2023
1 parent 4f35cf8 commit bfaed9e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
26 changes: 15 additions & 11 deletions lib/seccomp-tools/asm/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,11 @@ def resolve_symbols(statements)

jt = resolve_symbol(idx, statement.data[1])
jf = resolve_symbol(idx, statement.data[2])
statement.data[1] = jt
statement.data[2] = jf
statement.data[1] = [jt, statement.data[1]]
statement.data[2] = [jf, statement.data[2]]
end
end

# The farthest distance of a relative jump in BPF.
JUMP_DISTANCE_MAX = 255

# @param [Integer] index
# @param [SeccompTools::Asm::Token, :next] sym
def resolve_symbol(index, sym)
Expand All @@ -89,7 +86,7 @@ def resolve_symbol(index, sym)

if @symbols[str].nil?
# special case - goto <n> can be considered as $+1+<n>
return str.to_i if str == str.to_i.to_s && str.to_i <= JUMP_DISTANCE_MAX
return str.to_i if str == str.to_i.to_s

raise SeccompTools::UndefinedLabelError,
@scanner.format_error(sym, "Cannot find label '#{str}'")
Expand All @@ -100,10 +97,6 @@ def resolve_symbol(index, sym)
raise SeccompTools::BackwardJumpError,
@scanner.format_error(sym, "Does not support backward jumping to '#{str}'")
end
if dis > JUMP_DISTANCE_MAX
raise SeccompTools::LongJumpError,
@scanner.format_error(sym, "Does not support jumping farther than #{JUMP_DISTANCE_MAX}, got: #{dis}")
end
end
end

Expand Down Expand Up @@ -176,10 +169,21 @@ def emit_ret(val)
emit(:ret, src, k: val.to_i)
end

def emit_cmp(cmp, jt, jf)
# The farthest distance of a relative jump in BPF.
JUMP_DISTANCE_MAX = 255

def emit_cmp(cmp, jt_sym, jf_sym)
jt = jt_sym[0]
jf = jf_sym[0]
jop, jt, jf = convert_jmp_op(cmp, jt, jf)
return emit(:jmp, :none, 0, jt: 0, jf: 0, k: jt) if jop == :ja || jt == jf

[jt_sym, jf_sym].each do |dis, sym|
if dis > JUMP_DISTANCE_MAX
raise SeccompTools::LongJumpError,
@scanner.format_error(sym, "Does not support jumping farther than #{JUMP_DISTANCE_MAX}, got: #{dis}")
end
end
val = cmp[1]
src = val.x? ? :x : :k
k = val.x? ? 0 : val.to_i
Expand Down
11 changes: 11 additions & 0 deletions spec/asm/compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,17 @@
return ALLOW
EOS
end

it 'supports long direct jump' do
compiler = described_class.new(<<-EOS, nil, :amd64)
A = args[0]
goto end
#{"A = 0\n" * 260}
end: return ALLOW
EOS

expect { compiler.compile! }.not_to raise_error
end
end

describe 'label' do
Expand Down

0 comments on commit bfaed9e

Please sign in to comment.