diff mbox series

bpf: fix divides by zero

Message ID 1515807206.3606.4.camel@gmail.com
State Changes Requested, archived
Delegated to: BPF Maintainers
Headers show
Series bpf: fix divides by zero | expand

Commit Message

Eric Dumazet Jan. 13, 2018, 1:33 a.m. UTC
From: Eric Dumazet <edumazet@google.com>

Divides by zero are not nice, lets avoid them if possible.

Also do_div() seems not needed when dealing with 32bit operands,
but this seems a minor detail.

Fixes: bd4cf0ed331a ("net: filter: rework/optimize internal BPF interpreter's instruction set")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reported-by: syzbot <syzkaller@googlegroups.com>
---
 kernel/bpf/core.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

 		DST = do_div(tmp, (u32) SRC);
@@ -975,7 +975,7 @@ static unsigned int ___bpf_prog_run(u64 *regs,
const struct bpf_insn *insn,
 		DST = div64_u64(DST, SRC);
 		CONT;
 	ALU_DIV_X:
-		if (unlikely(SRC == 0))
+		if (unlikely((u32)SRC == 0))
 			return 0;
 		tmp = (u32) DST;
 		do_div(tmp, (u32) SRC);

Comments

Eric Dumazet Jan. 13, 2018, 1:41 a.m. UTC | #1
On Fri, 2018-01-12 at 17:33 -0800, Eric Dumazet wrote:
> From: Eric Dumazet <edumazet@google.com>
> 

Sorry for the mangled patch. Will send V2
Alexei Starovoitov Jan. 13, 2018, 1:42 a.m. UTC | #2
On Fri, Jan 12, 2018 at 05:33:26PM -0800, Eric Dumazet wrote:
> From: Eric Dumazet <edumazet@google.com>
> 
> Divides by zero are not nice, lets avoid them if possible.
> 
> Also do_div() seems not needed when dealing with 32bit operands,
> but this seems a minor detail.
> 
> Fixes: bd4cf0ed331a ("net: filter: rework/optimize internal BPF interpreter's instruction set")
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> Reported-by: syzbot <syzkaller@googlegroups.com>
> ---
>  kernel/bpf/core.c |    4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
> index
> 51ec2dda7f08c6c90af084589bb6d80662c77d12..7949e8b8f94e9cc196e0449214493
> ccce61b0903 100644
> --- a/kernel/bpf/core.c
> +++ b/kernel/bpf/core.c
> @@ -956,7 +956,7 @@ static unsigned int ___bpf_prog_run(u64 *regs,
> const struct bpf_insn *insn,
>  		DST = tmp;
>  		CONT;
>  	ALU_MOD_X:
> -		if (unlikely(SRC == 0))
> +		if (unlikely((u32)SRC == 0))

wow.
Acked-by: Alexei Starovoitov <ast@kernel.org>
we likely need to fix all JITs as well.
At least x64, arm64, sparc have the same bug.

Long term it's probably better to move all such checks out of JITs
and interpreter into the verifier and patch div/mod with
additional 'if src == 0'. This way we can do any type of
error reporting and/or aborting execution.
diff mbox series

Patch

diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index
51ec2dda7f08c6c90af084589bb6d80662c77d12..7949e8b8f94e9cc196e0449214493
ccce61b0903 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -956,7 +956,7 @@  static unsigned int ___bpf_prog_run(u64 *regs,
const struct bpf_insn *insn,
 		DST = tmp;
 		CONT;
 	ALU_MOD_X:
-		if (unlikely(SRC == 0))
+		if (unlikely((u32)SRC == 0))
 			return 0;
 		tmp = (u32) DST;