Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp7149513rwr; Tue, 2 May 2023 10:06:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6GPGNUk49tGO1OXvT7G75xar/REYEP8wOD8x6MnEJMdjHzrZmJmOP1USy2tYtiWno2Balt X-Received: by 2002:a05:6830:360a:b0:6a5:de6d:358d with SMTP id bg10-20020a056830360a00b006a5de6d358dmr7550037otb.7.1683047185595; Tue, 02 May 2023 10:06:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683047185; cv=none; d=google.com; s=arc-20160816; b=HMKAO6ju+JODL1YLvYd2srYoRL7yGL24mjx1zvJ04D+ZatYvou3r2DcURXRGe/R57D hJdaQaBvtHg9/agTC0JsChqUOgBRfC+3PxbPsz1aO0l/9ud9ooI+jxRwqBoO/9GFLoGR BAFFWZwaFuqPsYw0JFECJfFg6ZbX1XRLFgqe1IZbUZFBKMwKc2M2INIOMN3x2QoZc7ii U4G9/qsdY/b6054QRQJghMaPsuHlOKyxNsVHvSW1jh/eXmE7zjbw9mJQe1XjS9zPMoLP f1ZdC6TDaTYsoojMpNVEG8yamNMlNTVZIDgqLg22Wb4VIZVxmzpKQULWXLJYYB8y0dh/ waAQ== 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=mQbQb5VYKY9skIlc4lBRhU/pyb/R6gD1UoWyaMmpK8A=; b=GFdonyPJ0sU1q3o9tqGqrlHtElxXaiKlIuhx9NGyL/LGOnKKGmlo2EsAU0MCzBkfaY s1UlWYA0nOSvCmTUjhqYmAfjeWlZ8jTsbWMEmF/HgTAzV82JFFWk+oazCHCFUBSTswUh Bj7Yyrl8mEEHZOQm+D8+fQ09u5NfwbLgeB/hLMipqGW7gCD0EBp/VPgnzP+EjptZCTW8 87Pb1OXb6iHNi0N/IbkJAaeIh2m5ThFrQGIAXprf68gW7ePdRHtMhv25AX1pIAUNQjj1 WpIlgP6fkZ3Wh7hoTGwU50g69AGFI4tIRPZ/BcV/PCJ+lt6md3bNc8QwRGXdn5SBMtgy yKPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r7SH3CQF; 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 t6-20020a056830082600b006a2efaa22bfsi22463093ots.214.2023.05.02.10.06.12; Tue, 02 May 2023 10:06:25 -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=r7SH3CQF; 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 S234068AbjEBQ6D (ORCPT + 99 others); Tue, 2 May 2023 12:58:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233651AbjEBQ6C (ORCPT ); Tue, 2 May 2023 12:58:02 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD6C695; Tue, 2 May 2023 09:58:00 -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 5796562032; Tue, 2 May 2023 16:58:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DEFFDC433EF; Tue, 2 May 2023 16:57:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683046679; bh=U8AxR3Op0UshB19LCMR4iPqap4hs4SAkHoCkhdDAPSM=; h=From:To:Cc:Subject:Date:From; b=r7SH3CQFycqwpVAg10P3TcRQpv+yhxW0OxmeLWnHll3SxA1Hb0QA8KyRdL0jU8cBX QxeJTqDtiN9K0+f1R/BWXQoWNTwXnoaXyGmfSY3AwcuuEmm9WLLjikNj6aH4vf5YMx TTKuWZcoCn9vFWVMFl1+2PJA2bwcPEqe/iGkzW3yWeCy8bLKa2M7ypwwBLcJcipLmk 0H/l3jRdTL5uMS2vLgp5OQI4TK/RxSfSrUN2U8hoDNrjYbCS80uFgPvrq2fNV89EnU eMP2yELOkL2TMcJGH65eZs21sXqij10HAIgcMditVraKc+sw4NxHa/QRewm6jML7d6 o3K3gFsekzHOw== 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 , Yonghong Song , Andrey Ignatov Subject: [PATCH] bpf: Fix mask generation for 32-bit narrow loads of 64-bit fields Date: Tue, 2 May 2023 17:57:54 +0100 Message-Id: <20230502165754.16728-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=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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. Fix the mask generation so that narrow loads always perform a 32-bit AND operation. Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: John Fastabend Cc: Krzesimir Nowak Cc: Yonghong Song Cc: Andrey Ignatov Fixes: 31fd85816dbe ("bpf: permits narrower load from bpf program context fields") Signed-off-by: Will Deacon --- I spotted this while playing around with the JIT on arm64. I can't figure out why 31fd85816dbe special-cases 8-byte ctx fields in the first place, so I fear I may be missing something... 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.495.gc816e09b53d-goog