From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE, SPF_PASS,T_DKIMWL_WL_HIGH,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1EECDC28CC3 for ; Sat, 1 Jun 2019 13:36:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EDF4D27590 for ; Sat, 1 Jun 2019 13:36:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1559396167; bh=AMadrBeWN9rTHWin6Mm2I89LRg6/dmoiCD4W0ctfC8A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=w9GswfRtBJDA14RsHvM51a+VJgmSe7PYz/h+wvX//3iQYaQeBRk5p7ySqaPf7kJJT Q/2RUX4Ad+Zw1bFwQ2OQn89s/bT51INctwPErd8Y38xYyLkL9YAmYPhosCxVm4OPd6 xaMH1G1d4m3zoY4A4wBplNmpIzcYmTMY6NUcPjJ8= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728892AbfFANXI (ORCPT ); Sat, 1 Jun 2019 09:23:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:52810 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728293AbfFANXH (ORCPT ); Sat, 1 Jun 2019 09:23:07 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 93421264BE; Sat, 1 Jun 2019 13:23:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1559395386; bh=AMadrBeWN9rTHWin6Mm2I89LRg6/dmoiCD4W0ctfC8A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d+SRToAr/0fmBjHdleQ3n/bSSX2O4LqjDQjS1mDN6+EiXROQVZAiPbiytdV/Yt2eO Y08hZhkzHRCXcMiYwkVsXT7BfTooeFEqwoyRrbQx/niCzXw/aphWTd2u1KsPLzlpaC YJE5rdfFV7i7o7PAwwM9OlVXlSDF/IdkvsZk5twY= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Krzesimir Nowak , Alban Crequy , =?UTF-8?q?Iago=20L=C3=B3pez=20Galeiras?= , Yonghong Song , Daniel Borkmann , Sasha Levin , netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH AUTOSEL 4.19 028/141] bpf: fix undefined behavior in narrow load handling Date: Sat, 1 Jun 2019 09:20:04 -0400 Message-Id: <20190601132158.25821-28-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190601132158.25821-1-sashal@kernel.org> References: <20190601132158.25821-1-sashal@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Krzesimir Nowak [ Upstream commit e2f7fc0ac6957cabff4cecf6c721979b571af208 ] Commit 31fd85816dbe ("bpf: permits narrower load from bpf program context fields") made the verifier add AND instructions to clear the unwanted bits with a mask when doing a narrow load. The mask is computed with (1 << size * 8) - 1 where "size" is the size of the narrow load. When doing a 4 byte load of a an 8 byte field the verifier shifts the literal 1 by 32 places to the left. This results in an overflow of a signed integer, which is an undefined behavior. Typically, the computed mask was zero, so the result of the narrow load ended up being zero too. Cast the literal to long long to avoid overflows. Note that narrow load of the 4 byte fields does not have the undefined behavior, because the load size can only be either 1 or 2 bytes, so shifting 1 by 8 or 16 places will not overflow it. And reading 4 bytes would not be a narrow load of a 4 bytes field. Fixes: 31fd85816dbe ("bpf: permits narrower load from bpf program context fields") Reviewed-by: Alban Crequy Reviewed-by: Iago López Galeiras Signed-off-by: Krzesimir Nowak Cc: Yonghong Song Signed-off-by: Daniel Borkmann Signed-off-by: Sasha Levin --- kernel/bpf/verifier.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index acc2305ad8957..d3580a68dbefa 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5743,7 +5743,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env) insn->dst_reg, shift); insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, - (1 << size * 8) - 1); + (1ULL << size * 8) - 1); } } -- 2.20.1