diff --git a/lib/seccomp-tools/asm/compiler.rb b/lib/seccomp-tools/asm/compiler.rb index 5235cf4..8527511 100644 --- a/lib/seccomp-tools/asm/compiler.rb +++ b/lib/seccomp-tools/asm/compiler.rb @@ -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) @@ -89,7 +86,7 @@ def resolve_symbol(index, sym) if @symbols[str].nil? # special case - goto can be considered as $+1+ - 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}'") @@ -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 @@ -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 diff --git a/spec/asm/compiler_spec.rb b/spec/asm/compiler_spec.rb index aac5c03..5dd4dfe 100644 --- a/spec/asm/compiler_spec.rb +++ b/spec/asm/compiler_spec.rb @@ -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