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=1795027240; b=uGa0FsP1a0R8diP8uUkPw61flPG5u1WXGxDyHxL7+PFGTaIiRj95kE8usKJjvbF/KB2t FeM4xsV1dAb3X8Yb3bwz9cvptWt5J05AXTI0jJT3PuNr8YxW5kE0p5cBPAkXnCNuLbomA BkDgsDfmQr9Zx+PGRT0ysuizhJnp9NV7v/c1VuMkwW2S+FDVNmDgyRsHTmeS/PMoF2/sG jfh6/k59Qn8ueWdRrO4w5cDf2YxHfhb2EtfHSb2iZJlsgC5yhT6hJQl+4Q4xXfMAPdA0E 9uWWdaT5z5rnUx9eO62CjpqVx7yZPjRyvodvzGwCskNnqUzyqWr6q8rZMs7zwaTLdmQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795027240; 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=L1ZrK0UMO0Bdu6glWsJqjhC4HolrOInf4HyHWfT/pXE=; b=VRwZ0s+DlALIHDnOKicJFRIbD/Nl67COcRC9Vtf/pmgn6eKGUZGjvUdMzP4cSOwkchFj PRs3pHK17yoo8OlM/GP10UAUfwhKN+dG401KxmJkj1wnrZrlfI9emhoRvNuMJAYKoyOb1 s7Rj1hTh3e1h7WEIuwBxtPuqjvSa9dfJlhoO8t+zXLmimvdFVjfknuwz8zcajFSylDfHn Xm4oUMMdvLSzCDdKOMwk/UAywUpptY93rknur8R5ZhzLBeKejJMbhzWygnb8x3xF+IWC8 +T/gt6VawD2cLAlFpvcFBgYb24jq0cncsjXsQU6OiQwfzKMyuj+3KQ5ZsRyi8WbY/bA== 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=S9mcZRf3; 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 0FE8C606A775; Wed, 18 Nov 2026 18:40:39 +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=S9mcZRf3 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D5E001F000C7; Wed, 18 Nov 2026 18:40:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795027239; bh=L1ZrK0UMO0Bdu6glWsJqjhC4HolrOInf4HyHWfT/pXE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=S9mcZRf3F0CzKEljz8fWN6YihSgFBW8J/TJDrJo+sJDnFENzE8fl161AQWCopIulC FPyx3OChj1Q0HGwG6JBzPdGrgWmfUQaXKXMm2PVzHIFF3qymjcpf7HnjCyw7PdbPZ2 BDWJvEsol46DuLi/x2XAZapjCSKOjEnA8jXeEipI= 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.18 1718/2151] bpf: Look at all 64 bits of a BPF_JSET mask on 32-bit Date: Wed, 18 Nov 2026 15:57:33 +0100 Message-ID: <20261118150224.415431332@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118150121.603863898@linuxfoundation.org> References: <20261118150121.603863898@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.18-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 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 1ce3bfc12b304..f747197b36a68 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -16857,7 +16857,7 @@ static void regs_refine_cond_op(struct bpf_reg_state *reg1, struct bpf_reg_state * this restriction. I.e., !(r1 & 0x3) means neither bit 0 nor * bit 1 is set, which we can readily use in adjustments. */ - if (!is_power_of_2(val)) + if (hweight64(val) != 1) break; if (is_jmp32) { t = tnum_or(tnum_subreg(reg1->var_off), tnum_const(val)); -- 2.53.0