From patchwork Thu Nov 5 04:43:59 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Shen Lim X-Patchwork-Id: 7557241 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 09CA7BEEA4 for ; Thu, 5 Nov 2015 04:46:21 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 251392085C for ; Thu, 5 Nov 2015 04:46:20 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 298DB2085B for ; Thu, 5 Nov 2015 04:46:19 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZuCPV-0007M5-W4; Thu, 05 Nov 2015 04:44:30 +0000 Received: from mail-pa0-x235.google.com ([2607:f8b0:400e:c03::235]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZuCPR-0007HF-Qd for linux-arm-kernel@lists.infradead.org; Thu, 05 Nov 2015 04:44:26 +0000 Received: by pabfh17 with SMTP id fh17so74890024pab.0 for ; Wed, 04 Nov 2015 20:44:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=D3+tM6y2Y4VMBp0T1r3KL6OkhIvbZ3PyE5xi0nB63Jw=; b=WUVOSC69Vj/CWsBq3MOm/VQJ4SP8xQOPZky/eWS1yRhu1qqUrVnEesBVBW70XHhcrY 6MmpX8PiV59o1lsDeJB9peUBtPRFijR0mRcVARmwH1BgQGXVpPYP5Btkk+nOHkWFSrBl 3FCxzYAnr17C1dLnuamMBGZvnjo5V4akXUyBp29KEnVmEV+9aKlAITIEjeESm10CENfQ xd4Uyv49brE5NaIZzxL/QpTpJXcSutJyrH2JEryPo9wat9Rp3dmWAZi7CHMqFoeuosdj i8+cSfX8USb5zo3w8q3DuirNHWlvMQ28q4vQzI8F28sB/fwLDSa9GNE7g6xhZIxLo2Jn ii2w== X-Received: by 10.68.78.66 with SMTP id z2mr6781245pbw.144.1446698644679; Wed, 04 Nov 2015 20:44:04 -0800 (PST) Received: from localhost.localdomain (c-73-223-118-172.hsd1.ca.comcast.net. [73.223.118.172]) by smtp.gmail.com with ESMTPSA id zk3sm5041447pbb.41.2015.11.04.20.44.03 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 04 Nov 2015 20:44:04 -0800 (PST) From: Zi Shen Lim To: Catalin Marinas , Alexei Starovoitov Subject: [PATCH] arm64: bpf: fix mod-by-zero case Date: Wed, 4 Nov 2015 20:43:59 -0800 Message-Id: <1446698639-12362-1-git-send-email-zlim.lnx@gmail.com> X-Mailer: git-send-email 1.9.1 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20151104_204425_900537_2D357B1B X-CRM114-Status: GOOD ( 11.69 ) X-Spam-Score: -2.7 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Yang Shi , Zi Shen Lim , linux-kernel@vger.kernel.org, stable@vger.kernel.org, Xi Wang , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Reported-by: Yang Shi Reported-by: Xi Wang CC: Alexei Starovoitov CC: Catalin Marinas Fixes: e54bcde3d69d ("arm64: eBPF JIT compiler") Cc: # 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);