From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796388013; b=LjzclVRY0TW9Gku9TaHo3Akk4sHxOtO9GmGQz8IP+wA/yZudc1mcE4kxVTQlfa0EDTGP bvidQuTniOPW5EuWs6pifnGhU46hVQoooyklS4blau7Rcur4Tcm+A7hWwo4C0Vf/5HGYd SNLGviYCJ2cNW83cSQN0gfwpURHik3R6VzBEj0vPocgB8PmD0wfeWupI+fUoPCFxYMGy9 5wCj7Ekbwo0FqBp0ghVwuM2ddslbefUJdK22FsvOVPLEm30ai0zn1PA2CiduibLEGXugw dj9+Z7z2cpSxxgDFfO22ZDDGEMRKzs8jQBsZVzQn+/1tNRTglo0WfnKgclEfFooyz6g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388013; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=Kjp4m0M8DkMxNovq7pQnzoxtyg3K6SUai0ZnF6Rr6JY=; b=UgWbxNXvaJJ+5FopEK4hlCgIVXu5gN8/Vta+lrW5vf3J7Yk4zYrXSWwTfaA4wj9MWFx4 eIY9kEzElLrLLA5YUjynfu/sCtY7rr7eS4IERXb89k56t4fUKyVpUQhvb7BeGl+eb1NmR 77iuzVsb5zQBaRk5KbHbC9uXvz/gN7UW65tyULuUT371Lr+OZsqHEpsLvX/MOL4aWMHTG K307OgbEUyKTsYykURqnYJX3xEGIr4RPQrTjz6vSnkETKFSFhdEjZz58sWrw9oLfb6l0l v5Chw6zHetX5AxWMst6P7m4Ry4E+RLpvqqmnEjt3DjqsZh0dzNHawKm/JMYgUiI7iwA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=XAYh3JU9; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFBCD606A32C; Fri, 4 Dec 2026 12:40:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=XAYh3JU9 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 303B01F000C2; Fri, 4 Dec 2026 12:40:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796388013; bh=Kjp4m0M8DkMxNovq7pQnzoxtyg3K6SUai0ZnF6Rr6JY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=XAYh3JU9KqS0JZlVSNzcJc+gSd8hN63n8Zyknsm088rnMJJTfAR5x/mzUUYKujgfo 4aIThZqI8/s+e4HXd3tZPEtbsUtuAvYAmtjqesS1xBaKDO6Jvftl7PbmvqKcBxuMWq z3yl7ltGjaXpJvNCL28C5sKs6NEXNgn1KyIzEVBM= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Josef Bacik , Alexei Starovoitov , Sasha Levin Subject: [PATCH 6.1 0411/1059] bpf: Look at all 64 bits of a BPF_JSET mask on 32-bit Date: Fri, 4 Dec 2026 18:59:02 +0900 Message-ID: <20261204100654.986253246@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Josef Bacik [ Upstream commit ee4f44e0e3c8125eebe9ac967b6984bc9253d489 ] In the branch of a BPF_JSET which is taken, the verifier can learn something about the register only if the mask has a single bit: then this bit is set. regs_refine_cond_op() tests this with is_power_of_2(val) and then ORs val into var_off. val is a u64, but is_power_of_2() takes an unsigned long. On a 32-bit kernel it only sees the lower half of the mask. For if r1 & 0xffffffff80000000 goto l which is a BPF_JSET | BPF_K with an immediate of 0x80000000, the lower half is a power of two. So the verifier takes all 33 bits for set at l, while the branch is taken if any of them is. A mask of 0x100000001 from a register does the same for bit 0. The log of an i386 kernel, for r6 = bpf_get_prandom_u32() << 31: 3: (45) if r6 & 0x80000000 goto pc+2 from 3 to 6: R6=0xffffffff80000000 R10=fp0 6: (77) r6 >>= 63 ; R6=1 When the program runs, r6 >> 63 is 0. >>From then on the verifier works with known bits which the register does not have at run time. It accepts, for example, an offset into a map value which it derives from these bits and believes to be constant, and the program can access memory outside of the map value. Count the bits of the whole value. Only 32-bit kernels are affected. On a 64-bit kernel unsigned long holds the whole mask, and this patch changes nothing. Fixes: 960ea056561a ("bpf: verifier: teach the verifier to reason about the BPF_JSET instruction") Signed-off-by: Josef Bacik Signed-off-by: Alexei Starovoitov Link: https://patch.msgid.link/20261017163745.3341787-8-alexei.starovoitov@gmail.com [ sashal: Replaced is_power_of_2(val) with hweight64(val) == 1 in reg_set_min_max(), since regs_refine_cond_op() doesn't exist in this tree. ] 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 dee55d5db7da7..49577f7cf6262 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -10048,7 +10048,7 @@ static void reg_set_min_max(struct bpf_reg_state *true_reg, tnum_const(val32)); } else { false_64off = tnum_and(false_64off, tnum_const(~val)); - if (is_power_of_2(val)) + if (hweight64(val) == 1) true_64off = tnum_or(true_64off, tnum_const(val)); } -- 2.53.0