From nobody Wed Oct 8 02:03:27 2025 Received: from mail-gw02.astralinux.ru (mail-gw02.astralinux.ru [93.188.205.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D5832D8783; Thu, 3 Jul 2025 09:51:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=93.188.205.243 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751536269; cv=none; b=n5ICruOrgXf1V4DPU7zz+2KZqljJX/CFTxjqqOY6pA9MGJBvJ6tDO5Z3fkgo/Qe4XXM1+7MBptkJIDjMA3y5jDecE9biRoQTMMqDlepSVprIbnDCUt7Do0R3zvAkUAmwu23clxDdaJC0Wd32zsWAnO54t7vvYVLzw7cR5Ex94O0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751536269; c=relaxed/simple; bh=4tHBMGYPbtDNX4c95d5DdGLxYFokfo+G5JDtcZw6FJo=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Zmhn9mD+es1Hvp6YIB26Cc74HU7g+5JgNBrjIL5bN8k5uQYyYEVl+3BxHvn7R6Dc4vnhgqfbqC96up5U5pX7o7crGOomkejsxSiCyqhrcxnz+59igHwFytU44iUjEpGTzPw/o3XU05WPSNYJFQR5vnPJtlIACNUrK0r8hme8jGc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=astralinux.ru; spf=pass smtp.mailfrom=astralinux.ru; dkim=pass (2048-bit key) header.d=astralinux.ru header.i=@astralinux.ru header.b=bKpX686F; arc=none smtp.client-ip=93.188.205.243 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=astralinux.ru Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=astralinux.ru Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=astralinux.ru header.i=@astralinux.ru header.b="bKpX686F" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=astralinux.ru; s=mail; t=1751536257; bh=4tHBMGYPbtDNX4c95d5DdGLxYFokfo+G5JDtcZw6FJo=; h=From:To:Cc:Subject:Date:From; b=bKpX686FSJ3YnzUeaoRuSP6PVjMzoNp6eAS6SV3zMEcQ6Pu4s4uNXhpg42TDGRcO3 hQMaHqp9tX5amfMosYwNLBBMAuDuOkv64PCo/iseCfVNP3kIb3DtDeSpwDnm/26p1q bmi4qMTAeQKWPbsfwE13MaOB6obdIkyH/wNU6XQ4h63pizZWF8IJjBtY9M6hgTWEeJ UBtDXmrqkbC0U+Mnvm+jOhP9FFHbP7DfBBTzFywDC+sCe14e8jMp0HI8OreXaxSuLE kmn32TVb/QwK8gwWg+Ch2Tcvy2w/E5fkTo+LJRkL9vqwD02ZmyIsjXS+1uFzhF+co9 yPsExjAtOii3A== Received: from gca-msk-a-srv-ksmg01.astralinux.ru (localhost [127.0.0.1]) by mail-gw02.astralinux.ru (Postfix) with ESMTP id 8DD651F96E; Thu, 3 Jul 2025 12:50:57 +0300 (MSK) Received: from new-mail.astralinux.ru (unknown [10.177.185.197]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail-gw02.astralinux.ru (Postfix) with ESMTPS; Thu, 3 Jul 2025 12:50:54 +0300 (MSK) Received: from localhost.localdomain (unknown [10.190.6.76]) by new-mail.astralinux.ru (Postfix) with ESMTPA id 4bXsSs5ybgztQTX; Thu, 3 Jul 2025 12:50:21 +0300 (MSK) From: Anastasia Belova To: stable@vger.kernel.org, Greg Kroah-Hartman Cc: Anastasia Belova , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, lvc-project@linuxtesting.org, Kumar Kartikeya Dwivedi , Sasha Levin Subject: [PATCH 5.10/5.15] bpf: Do mark_chain_precision for ARG_CONST_ALLOC_SIZE_OR_ZERO Date: Thu, 3 Jul 2025 12:50:12 +0300 Message-ID: <20250703095013.148069-1-abelova@astralinux.ru> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-KSMG-AntiPhishing: NotDetected, bases: 2025/07/03 08:54:00 X-KSMG-AntiSpam-Auth: dkim=none X-KSMG-AntiSpam-Envelope-From: abelova@astralinux.ru X-KSMG-AntiSpam-Info: LuaCore: 63 0.3.63 9cc2b4b18bf16653fda093d2c494e542ac094a39, {Tracking_uf_ne_domains}, {Tracking_from_domain_doesnt_match_to}, new-mail.astralinux.ru:7.1.1;d41d8cd98f00b204e9800998ecf8427e.com:7.1.1;lore.kernel.org:7.1.1;astralinux.ru:7.1.1;127.0.0.199:7.1.2, FromAlignment: s X-KSMG-AntiSpam-Interceptor-Info: scan successful X-KSMG-AntiSpam-Lua-Profiles: 194515 [Jul 03 2025] X-KSMG-AntiSpam-Method: none X-KSMG-AntiSpam-Rate: 0 X-KSMG-AntiSpam-Status: not_detected X-KSMG-AntiSpam-Version: 6.1.1.11 X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.1.0.7854, bases: 2025/07/03 05:31:00 #27614197 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: NotDetected, bases: 2025/07/03 08:54:00 X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 1 Content-Type: text/plain; charset="utf-8" From: Kumar Kartikeya Dwivedi [ Upstream commit 2fc31465c5373b5ca4edf2e5238558cb62902311 ] Precision markers need to be propagated whenever we have an ARG_CONST_* style argument, as the verifier cannot consider imprecise scalars to be equivalent for the purposes of states_equal check when such arguments refine the return value (in this case, set mem_size for PTR_TO_MEM). The resultant mem_size for the R0 is derived from the constant value, and if the verifier incorrectly prunes states considering them equivalent where such arguments exist (by seeing that both registers have reg->precise as false in regsafe), we can end up with invalid programs passing the verifier which can do access beyond what should have been the correct mem_size in that explored state. To show a concrete example of the problem: 0000000000000000 : 0: r2 =3D *(u32 *)(r1 + 80) 1: r1 =3D *(u32 *)(r1 + 76) 2: r3 =3D r1 3: r3 +=3D 4 4: if r3 > r2 goto +18 5: w2 =3D 0 6: *(u32 *)(r1 + 0) =3D r2 7: r1 =3D *(u32 *)(r1 + 0) 8: r2 =3D 1 9: if w1 =3D=3D 0 goto +1 10: r2 =3D -1 0000000000000058 : 11: r1 =3D 0 ll 13: r3 =3D 0 14: call bpf_ringbuf_reserve 15: if r0 =3D=3D 0 goto +7 16: r1 =3D r0 17: r1 +=3D 16777215 18: w2 =3D 0 19: *(u8 *)(r1 + 0) =3D r2 20: r1 =3D r0 21: r2 =3D 0 22: call bpf_ringbuf_submit 00000000000000b8 : 23: w0 =3D 0 24: exit For the first case, the single line execution's exploration will prune the search at insn 14 for the branch insn 9's second leg as it will be verified first using r2 =3D -1 (UINT_MAX), while as w1 at insn 9 will always be 0 so at runtime we don't get error for being greater than UINT_MAX/4 from bpf_ringbuf_reserve. The verifier during regsafe just sees reg->precise as false for both r2 registers in both states, hence considers them equal for purposes of states_equal. If we propagated precise markers using the backtracking support, we would use the precise marking to then ensure that old r2 (UINT_MAX) was within the new r2 (1) and this would never be true, so the verification would rightfully fail. The end result is that the out of bounds access at instruction 19 would be permitted without this fix. Note that reg->precise is always set to true when user does not have CAP_BPF (or when subprog count is greater than 1 (i.e. use of any static or global functions)), hence this is only a problem when precision marks need to be explicitly propagated (i.e. privileged users with CAP_BPF). A simplified test case has been included in the next patch to prevent future regressions. Fixes: 457f44363a88 ("bpf: Implement BPF ring buffer and verifier support f= or it") Signed-off-by: Kumar Kartikeya Dwivedi Link: https://lore.kernel.org/r/20220823185300.406-2-memxor@gmail.com Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin Signed-off-by: Anastasia Belova --- Backport fix for CVE-2022-49961 kernel/bpf/verifier.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 7049a85a78ab..fbfdfec46199 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5518,6 +5518,9 @@ static int check_func_arg(struct bpf_verifier_env *en= v, u32 arg, return -EACCES; } meta->mem_size =3D reg->var_off.value; + err =3D mark_chain_precision(env, regno); + if (err) + return err; } else if (arg_type_is_int_ptr(arg_type)) { int size =3D int_ptr_type_to_size(arg_type); =20 --=20 2.43.0