]> git.baikalelectronics.ru Git - kernel.git/commit
bpf: fix x64 JIT code generation for jmp to 1st insn
authorAlexei Starovoitov <ast@kernel.org>
Wed, 31 Jul 2019 01:38:26 +0000 (18:38 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 1 Aug 2019 20:12:09 +0000 (13:12 -0700)
commitb71ebcb3a5e598dc950afc3fae07351f0c1223eb
tree38919cff1c1ef870322d43d8f05e15f048affb85
parent8b50b52522acfcf967865b83196159a685b6d920
bpf: fix x64 JIT code generation for jmp to 1st insn

Introduction of bounded loops exposed old bug in x64 JIT.
JIT maintains the array of offsets to the end of all instructions to
compute jmp offsets.
addrs[0] - offset of the end of the 1st insn (that includes prologue).
addrs[1] - offset of the end of the 2nd insn.
JIT didn't keep the offset of the beginning of the 1st insn,
since classic BPF didn't have backward jumps and valid extended BPF
couldn't have a branch to 1st insn, because it didn't allow loops.
With bounded loops it's possible to construct a valid program that
jumps backwards to the 1st insn.
Fix JIT by computing:
addrs[0] - offset of the end of prologue == start of the 1st insn.
addrs[1] - offset of the end of 1st insn.

v1->v2:
- Yonghong noticed a bug in jit linfo.
  Fix it by passing 'addrs + 1' to bpf_prog_fill_jited_linfo(),
  since it expects insn_to_jit_off array to be offsets to last byte.

Reported-by: syzbot+35101610ff3e83119b1b@syzkaller.appspotmail.com
Fixes: f07a3d2cf663 ("bpf: introduce bounded loops")
Fixes: ebcb62a514ad ("net: filter: Just In Time compiler for x86-64")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Song Liu <songliubraving@fb.com>
arch/x86/net/bpf_jit_comp.c