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=1796385124; b=5jQWd0lnmqgQGwv9SJfJQ4JxE5hsw7Zl8PuR/8yMnGypOwlmMd8Q4PPv4j4LPLBtV9YS lTxI++BFjJEB1s/Omi72YJ8m/7SPt8hDkdv064QoJVCu/ddZ3v79LCj5ENpzviSWBlF1s BqojEqsARyxINDmLFYpWrzR3bbSKMKYFSyUuOpMG5yRkk+M7yagTuZEuvY682X5DuaL/v Sxuayp6tUrHvVJnSqlZaUAjKqivk8fhyBczIj55ilgzYclsOCMHY796eRMGpor+RhMOi6 k36F9QSLCrXvJQ0vwBe/UcvqhGXteCO826ZKuFyvDZbA8KH1xsMlBAw34MmRwfq8Rew== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796385124; 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=O0hkTCgEMV5dBNhmswNPoNmNRM6tlU7iiZrSmbQD/Sw=; b=bLnslX1dP5XLik0sjCiTj4HfKGyyng5tIgKdRgW4OpeyLAEMgJwSi+mD3uexAcd+2YBv 37rDyAsvBT3/Z7eCHwyl63Q974j0ISRpnLDad2Gf8Oz1d4+j0io0+pGgbmQ9Y0+KmrzM6 QM/BPrWI8vCubn50Y7kzVg8pd0vazDW/EcOMrb60vgVYHU6nIVyW2qjq7kgufAOTqt8DY uDZFMY4rU9fQ/ZeMQNLdDZ50KkScVvT6OMHpP4JWyYAVjcQcL3Pa0Sk0CAbv+/Y3lFP38 eTiS0zG3kYvdqINdrgAMORFVvs+WPTHtIzd9+fZIFnyd0/ALdcBbeompdsymMt0+vkw== 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=PgC2qFHn; 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 96D6C606A322; Fri, 4 Dec 2026 11:52:04 +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=PgC2qFHn Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0EC6D1F000C2; Fri, 4 Dec 2026 11:52:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796385124; bh=O0hkTCgEMV5dBNhmswNPoNmNRM6tlU7iiZrSmbQD/Sw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=PgC2qFHneqRqRaawUVy9KhOvgXBIA1E6lcxqorRhxAeE5T4UI1B/HwpkpJ11MlIS9 gRBJNLHue5r1Q/2cJtF9japUb77oyrkLdxL3ur4xMifV5JaiC11Nm0j5NxS4p/qQfC /Wcz4UCeUxyzBwdM6YE8qwHFslyW98nTaPARJem0= 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.6 0429/1167] bpf: Look at all 64 bits of a BPF_JSET mask on 32-bit Date: Fri, 4 Dec 2026 18:57:36 +0900 Message-ID: <20261204100713.192801958@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@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.6-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: Retargeted the change to reg_set_min_max() since regs_refine_cond_op() doesn't exist here, using hweight64(uval) == 1 in the BPF_JSET branch. ] 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 687be68c728a9..771d0c5a46ac4 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -14406,7 +14406,7 @@ static void reg_set_min_max(struct bpf_reg_state *true_reg, tnum_const(uval32)); } else { false_64off = tnum_and(false_64off, tnum_const(~uval)); - if (is_power_of_2(uval)) + if (hweight64(uval) == 1) true_64off = tnum_or(true_64off, tnum_const(uval)); } -- 2.53.0