Skip to content

Commit

Permalink
bpf: fix 64-bit divide
Browse files Browse the repository at this point in the history
ALU64_DIV instruction should be dividing 64-bit by 64-bit,
whereas do_div() does 64-bit by 32-bit divide.
x64 and arm64 JITs correctly implement 64 by 64 unsigned divide.
llvm BPF backend emits code assuming that ALU64_DIV does 64 by 64.

Fixes: 89aa075 ("net: sock: allow eBPF programs to be attached to sockets")
Reported-by: Michael Holzheu <[email protected]>
Acked-by: Daniel Borkmann <[email protected]>
Signed-off-by: Alexei Starovoitov <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
Alexei Starovoitov authored and davem330 committed Apr 28, 2015
1 parent 8e046d6 commit 876a7ae
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions kernel/bpf/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
ALU64_MOD_X:
if (unlikely(SRC == 0))
return 0;
tmp = DST;
DST = do_div(tmp, SRC);
div64_u64_rem(DST, SRC, &tmp);
DST = tmp;
CONT;
ALU_MOD_X:
if (unlikely(SRC == 0))
Expand All @@ -367,8 +367,8 @@ static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
DST = do_div(tmp, (u32) SRC);
CONT;
ALU64_MOD_K:
tmp = DST;
DST = do_div(tmp, IMM);
div64_u64_rem(DST, IMM, &tmp);
DST = tmp;
CONT;
ALU_MOD_K:
tmp = (u32) DST;
Expand All @@ -377,7 +377,7 @@ static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
ALU64_DIV_X:
if (unlikely(SRC == 0))
return 0;
do_div(DST, SRC);
DST = div64_u64(DST, SRC);
CONT;
ALU_DIV_X:
if (unlikely(SRC == 0))
Expand All @@ -387,7 +387,7 @@ static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
DST = (u32) tmp;
CONT;
ALU64_DIV_K:
do_div(DST, IMM);
DST = div64_u64(DST, IMM);
CONT;
ALU_DIV_K:
tmp = (u32) DST;
Expand Down

0 comments on commit 876a7ae

Please sign in to comment.