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=1796387569; b=yDFJ5/3B0jL0rEXib09139tJfGMUTJwGZHl1rcDKmK8ZGbOq9o839dXbcNXm6hAtFKvl yyU0jt/OG1eyBjpYbWqgqqTekQ97ac5aQDAo02XNCKGjFlBFo09UvECJ6ihqNh9lDpMvW EAc9f1C/g9uf3G1bU8R8CV8Fr/m/IkA713J1IQo0aMoSy7tL+yTyarfTru1piraEJlxKA oxTQQULqogaisnuz5R4VhssWHBvhnhUMef//GMfA19JxSoirNx544q36+RHw3XeBwIlQD 9kPTRflvDbrKwTqDHUlU46Il1qMedWMhZBFQC+fGZyrykVH3MdSX8mrJbgjOHYzTecQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387569; 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=jWx5n3EYHkvusUkbk5fkhBCi7I71SoJIOVCGIMgNlyY=; b=N1vj8fkXcbfbmwvBMDrG9jecjYJHPr4n9gIBNtXyjqCeJD4h9lAbsXxrIKgmUTnpymef otd21osik0PZPij4E2i9vzsZsLCq5dwXFnlH0wi6PklUs487DgRvmveP/ZFyANANyO9VI FcIfJVSZncFLAJ6/perPdfK/e7gY0D8iA/cgY0bwCUVnp0iWlpNsWSAjGgaozYAUegGHl GVCcKTC3T9JQgBoUHkj49oUlTQVaSnDkAhSBX0Bx72VQEdoWCFCKkxr77q8W3tNaZz3m4 Jg2SgIw266zNswmo+XxvEkF3VBKk3gGEaRwfy0ZZ9DjoW3LTYBXeQLaVDM/VwSj1t8g== 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=Mx03OWYd; 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 3A237606A334; Fri, 4 Dec 2026 12:32:49 +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=Mx03OWYd Received: by smtp.kernel.org (Postfix) with ESMTPSA id A74031F000C2; Fri, 4 Dec 2026 12:32:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387569; bh=jWx5n3EYHkvusUkbk5fkhBCi7I71SoJIOVCGIMgNlyY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Mx03OWYd/2B82kZKWj9Jk+F4RQESjotj+2H6/h+6z5E1j9K2KEkzK0khHsMr8NR+B CwkD7p7WF4NEqCSJWDnlDZxujWf6DHk5WHKW22LSzPupuHvI0dGSj1TuemDrRhpZ1U Z0XhbM+JsD+q9px7sBLrmBZ3iA6LP1zHVgLQIo2Y= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Hao Sun , Kumar Kartikeya Dwivedi , Sasha Levin Subject: [PATCH 6.1 0299/1059] bpf: Fix uninit read for non-fetch atomics on partially spilled slots Date: Fri, 4 Dec 2026 18:57:10 +0900 Message-ID: <20261204100652.019692954@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: Hao Sun [ Upstream commit 5a84b4424db8ae07f0ac682ab031c37a91dce50b ] check_stack_read_fixed_off() skips partial spill checks for non-fetch atomics; the following prog can be loaded: 0: (b7) r1 = 1 ; R1=1 1: (63) *(u32 *)(r10 -8) = r1 ; R1=1 R10=fp0 fp-8=????1 2: (db) lock *(u64 *)(r10 -8) += r1 ; R1=1 R10=fp0 fp-8=mmmmmmmm 3: (79) r0 = *(u64 *)(r10 -8) ; R0=scalar() R10=fp0 fp-8=mmmmmmmm 4: (77) r0 >>= 32 ; R0=scalar(smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff)) 5: (95) exit When test run: retval=4294967295 Note fp-8 is ????1 at #1, yet it becomes fp-8=mmmmmmmm after the non-fetching atomic add at #2; hence the high 32 bits are leaked. Fix by applying the partial load check; after the patch, the prog is rejected: Verification failed: Memory Safety: Uninitialized stack read Reason: This rejected read uses 8 bytes at stack offset -8, but byte 4 in that range is uninitialized on this path. Programs loaded with CAP_PERFMON can be allowed to read uninitialized stack bytes, but this program is being rejected without that allowance. At: ... 0 | (b7) r1 = 1 1 | (63) *(u32 *)(r10 -8) = r1 >>> 2 | (db) lock *(u64 *)(r10 -8) += r1 3 | (79) r0 = *(u64 *)(r10 -8) 4 | (77) r0 >>= 32 This affects CAP_BPF only. Fixes: 354e8f1970f8 ("bpf: Support <8-byte scalar spill and refill") Signed-off-by: Hao Sun Link: https://lore.kernel.org/bpf/20260924131342.934290-1-sunhao.th@gmail.com Signed-off-by: Kumar Kartikeya Dwivedi [ sashal: Moved the early dst_regno < 0 return to the start of the aligned full-size fill branch and after the per-byte validity loop in the partial-fill branch, since this tree lacks upstream's narrow-fill structure. ] Signed-off-by: Sasha Levin --- kernel/bpf/verifier.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index dfc6475a9203e..ba4ae75512294 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -3541,10 +3541,11 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env, } mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); - if (dst_regno < 0) - return 0; if (!(off % BPF_REG_SIZE) && size == spill_size) { + if (dst_regno < 0) + return 0; + /* The earlier check_reg_arg() has decided the * subreg_def for this insn. Save it first. */ @@ -3565,6 +3566,9 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env, off, i, size); return -EACCES; } + if (dst_regno < 0) + return 0; + mark_reg_unknown(env, state->regs, dst_regno); } state->regs[dst_regno].live |= REG_LIVE_WRITTEN; -- 2.53.0