* [PATCH] arm64: bpf: fix mod-by-zero case
@ 2015-11-05 4:43 Zi Shen Lim
2015-11-06 17:00 ` Catalin Marinas
0 siblings, 1 reply; 2+ messages in thread
From: Zi Shen Lim @ 2015-11-05 4:43 UTC (permalink / raw)
To: Catalin Marinas, Alexei Starovoitov
Cc: Zi Shen Lim, Yang Shi, Xi Wang, linux-arm-kernel, linux-kernel, stable
Turns out in the case of modulo by zero in a BPF program:
A = A % X; (X == 0)
the expected behavior is to terminate with return value 0.
The bug in JIT is exposed by a new test case [1].
[1] https://lkml.org/lkml/2015/11/4/499
Signed-off-by: Zi Shen Lim <zlim.lnx@gmail.com>
Reported-by: Yang Shi <yang.shi@linaro.org>
Reported-by: Xi Wang <xi.wang@gmail.com>
CC: Alexei Starovoitov <ast@plumgrid.com>
CC: Catalin Marinas <catalin.marinas@arm.com>
Fixes: e54bcde3d69d ("arm64: eBPF JIT compiler")
Cc: <stable@vger.kernel.org> # 3.18+
---
This patch applies on top of "arm64: bpf: fix fiv-by-zero case" [2].
[2] https://lkml.org/lkml/2015/11/4/25
arch/arm64/net/bpf_jit_comp.c | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index 9ae6f23..6217f80 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -269,6 +269,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
break;
case BPF_ALU | BPF_DIV | BPF_X:
case BPF_ALU64 | BPF_DIV | BPF_X:
+ case BPF_ALU | BPF_MOD | BPF_X:
+ case BPF_ALU64 | BPF_MOD | BPF_X:
{
const u8 r0 = bpf2a64[BPF_REG_0];
@@ -281,16 +283,19 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
check_imm26(jmp_offset);
emit(A64_B(jmp_offset), ctx);
/* else */
- emit(A64_UDIV(is64, dst, dst, src), ctx);
+ switch (BPF_OP(code)) {
+ case BPF_DIV:
+ emit(A64_UDIV(is64, dst, dst, src), ctx);
+ break;
+ case BPF_MOD:
+ ctx->tmp_used = 1;
+ emit(A64_UDIV(is64, tmp, dst, src), ctx);
+ emit(A64_MUL(is64, tmp, tmp, src), ctx);
+ emit(A64_SUB(is64, dst, dst, tmp), ctx);
+ break;
+ }
break;
}
- case BPF_ALU | BPF_MOD | BPF_X:
- case BPF_ALU64 | BPF_MOD | BPF_X:
- ctx->tmp_used = 1;
- emit(A64_UDIV(is64, tmp, dst, src), ctx);
- emit(A64_MUL(is64, tmp, tmp, src), ctx);
- emit(A64_SUB(is64, dst, dst, tmp), ctx);
- break;
case BPF_ALU | BPF_LSH | BPF_X:
case BPF_ALU64 | BPF_LSH | BPF_X:
emit(A64_LSLV(is64, dst, dst, src), ctx);
--
1.9.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] arm64: bpf: fix mod-by-zero case
2015-11-05 4:43 [PATCH] arm64: bpf: fix mod-by-zero case Zi Shen Lim
@ 2015-11-06 17:00 ` Catalin Marinas
0 siblings, 0 replies; 2+ messages in thread
From: Catalin Marinas @ 2015-11-06 17:00 UTC (permalink / raw)
To: Zi Shen Lim
Cc: Alexei Starovoitov, Yang Shi, linux-kernel, stable, Xi Wang,
linux-arm-kernel
On Wed, Nov 04, 2015 at 08:43:59PM -0800, Zi Shen Lim wrote:
> Turns out in the case of modulo by zero in a BPF program:
> A = A % X; (X == 0)
> the expected behavior is to terminate with return value 0.
>
> The bug in JIT is exposed by a new test case [1].
>
> [1] https://lkml.org/lkml/2015/11/4/499
>
> Signed-off-by: Zi Shen Lim <zlim.lnx@gmail.com>
> Reported-by: Yang Shi <yang.shi@linaro.org>
> Reported-by: Xi Wang <xi.wang@gmail.com>
> CC: Alexei Starovoitov <ast@plumgrid.com>
> CC: Catalin Marinas <catalin.marinas@arm.com>
> Fixes: e54bcde3d69d ("arm64: eBPF JIT compiler")
> Cc: <stable@vger.kernel.org> # 3.18+
> ---
> This patch applies on top of "arm64: bpf: fix fiv-by-zero case" [2].
Both patches applied. Thanks.
--
Catalin
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2015-11-06 17:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-05 4:43 [PATCH] arm64: bpf: fix mod-by-zero case Zi Shen Lim
2015-11-06 17:00 ` Catalin Marinas
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).