Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp768778rwd; Thu, 18 May 2023 03:48:56 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4JwmCiCoXGHNa/bX+KJa4dGsj8YP8ARz912f5ZUsdR1PMtgPeaJNVl4kDVigRz2XFxNEsm X-Received: by 2002:a05:6a20:7f8c:b0:109:38b4:a21b with SMTP id d12-20020a056a207f8c00b0010938b4a21bmr1829065pzj.51.1684406936569; Thu, 18 May 2023 03:48:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684406936; cv=none; d=google.com; s=arc-20160816; b=Z9GahKDS7FdTK52zd4jAN44sWT/IjbCoRVVUuyfmxnhfw9WkT8D5+WaiLZKfXruouQ p98osUp+6hYRpDuOle0+0L4mOHR1FykcdX+NIkCXruI7TJOofQcrqpCzI7z+4avlgk0/ qlTw3w11WaEUXWmy878JR1WYhG+Ee515hDP0fDRs3jwtxg/pCDVSpoLIWk0kEdY+9JR+ 0mPHhfJekfFKBB4i6Mxy1ujiHtGBo1yPdAq+vujeRGOg1lJiAWIK+ko1kmO/ynuA6MwY rvcZub/XeA3RXBsGhWv5AgtTTMsxe0POSr8aYEyx2DjR17dAb15AUEqNgoSQuVgkiEUo f+Fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=RYD7sC8H/RB37FQhgyM0o4cJU2ufEP5S9BccMYPUa/Y=; b=j6reCqb3E/XQ8WmGrXGU4ATcEiBc6QS2xP37WJIXg4CFW2jYXpAq24QGjPi0Wn7DgV NSx2Kw11vOtEXKrsPQsh+ruJfLiNZKuFTR3wUYPCAAOK5BAI+XC+3WpKTA3NGTbIIYQ+ Ay+TH9ePfh6OpLpgZp3ZSKok0+YOSG+5/t4AC6kAMpdL8wYMbQj4eTRYcHa60m9MhY3K ugXgkj4jynStyGZb7eYInjwp4XjNDxdbomYROpDqs9pBm9PQO62Hc69xFeizb0gHa5+B 7/U1AM1Ne0drikMkVdziDUwLCamwG6E0QWQE/r1lYEX88MeProoZpzhlDAlTI13IGeoO zRTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="UiNQg/eY"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i8-20020a639d08000000b00518706a93f1si1097919pgd.361.2023.05.18.03.48.42; Thu, 18 May 2023 03:48:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="UiNQg/eY"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230318AbjERKZn (ORCPT + 99 others); Thu, 18 May 2023 06:25:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230230AbjERKZk (ORCPT ); Thu, 18 May 2023 06:25:40 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE96C1BCA for ; Thu, 18 May 2023 03:25:39 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 49CF7612D3 for ; Thu, 18 May 2023 10:25:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E3F10C4339B; Thu, 18 May 2023 10:25:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1684405538; bh=horsSNtaATRHh7WRspe0LOv/cMkQnBJ+gC4dIJ6oeec=; h=From:To:Cc:Subject:Date:From; b=UiNQg/eYb4x6iZ1LdMXXHsaZn1Hr98TWOyCGSu9FBy5Ds0nuEkQfNm6ik1cJeiNNe l7maUw/oEBkmnXL/pYk+c20+krnLrWuFhLUccIpf+cZQi9NknqACLbF48gcTSEi8XA ZFm/9h7p54dfnkUFW9AhVQjJaglr3UEuukO/C8D/thR1BCU/nNnnnHyalO77axp1Kh qBpQQi2sjzYaTylk1l+nfsn9LgtSUFTFXwfzDmbaQSwMSHRU4c6Ik+3drjWr/GXaYb /jOpcxP5WoZEXEEBnSfXNPTWHgI4fZU60kh9/NOMZQyCCE8OQhc0k2+F/7NjCcVptu U/l9sIALbzAwg== From: Will Deacon To: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, Will Deacon , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Krzesimir Nowak , Andrey Ignatov , Yonghong Song Subject: [PATCH v2] bpf: Fix mask generation for 32-bit narrow loads of 64-bit fields Date: Thu, 18 May 2023 11:25:28 +0100 Message-Id: <20230518102528.1341-1-will@kernel.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A narrow load from a 64-bit context field results in a 64-bit load followed potentially by a 64-bit right-shift and then a bitwise AND operation to extract the relevant data. In the case of a 32-bit access, an immediate mask of 0xffffffff is used to construct a 64-bit BPP_AND operation which then sign-extends the mask value and effectively acts as a glorified no-op. For example: 0: 61 10 00 00 00 00 00 00 r0 = *(u32 *)(r1 + 0) results in the following code generation for a 64-bit field: ldr x7, [x7] // 64-bit load mov x10, #0xffffffffffffffff and x7, x7, x10 Fix the mask generation so that narrow loads always perform a 32-bit AND operation: ldr x7, [x7] // 64-bit load mov w10, #0xffffffff and w7, w7, w10 Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: John Fastabend Cc: Krzesimir Nowak Cc: Andrey Ignatov Acked-by: Yonghong Song Fixes: 31fd85816dbe ("bpf: permits narrower load from bpf program context fields") Signed-off-by: Will Deacon --- v2: Improve commit message and add Acked-by. 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 fbcf5a4e2fcd..5871aa78d01a 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -17033,7 +17033,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env) insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, insn->dst_reg, shift); - insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, + insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, (1ULL << size * 8) - 1); } } -- 2.40.1.698.g37aff9b760-goog