From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 23B5A39022A; Wed, 30 Sep 2026 16:21:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790785265; cv=none; b=boXFfYrFDdD6HK4GXoDF1YuKRT088slQdLe2c9JaTWRIamaNYlROVBo4twaP0fJkrMHwxa21DHU0rMIn78Rfv/9D0h3+vRzk8GoekRpYsLz3z/BwnvCRG2+Is21rU3hZ8hCu+sjxwjjOH5ACz4RnwwhHHX7+9EQE1S77PbWxues= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790785265; c=relaxed/simple; bh=vnoUtmL/2SLbQ9ilFmq8VRxDhweZR1mOT64sVeCtY3E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=vCKujFTXK2TBpf40CX5JJ0G5d+hCyvvnaz17t/WerTQ7FHcwS12peYY+FAEWhpaZG/u9CQFCIm5i2bEnVu5rMOfs01CsLLbthThYACrwtx9URSp8SThrgLmXqPQ6vW9gtw518/7x/m+2v8IYiH2PDFH0xzBKwNyx4o+r/99s6U8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=OnwlRvnZ; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="OnwlRvnZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6BE1A1F00898; Wed, 30 Sep 2026 16:20:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1790785260; bh=JpSXxMp2HZc+1zJDoJQMRcbNkj8mKsZ94Tj01OIU3jE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=OnwlRvnZkUHdVYMzbs6+zxmQewjzcTxiMZvp2jaKkD58Olrmou1LU4jPFrg8yIJmt iKf9uc21DSMoiUb7urFUWw77gTsvaUH3Hv6Rzspu3b2CA3vY8V5Oiek/j59ggUhTQu l+GMp8PcoZUmtxk8Jr7rDKARYnG9o7KadwnVqOcw= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Jiayuan Chen , Ihor Solodrai , Alexei Starovoitov , Sasha Levin Subject: [PATCH 6.1 401/982] bpf: Fix NULL-ptr-deref in btf_var_show() Date: Wed, 30 Sep 2026 17:18:57 +0200 Message-ID: <20260930152425.424111723@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260930152416.775402466@linuxfoundation.org> References: <20260930152416.775402466@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jiayuan Chen [ Upstream commit 5403a383f52fc0905703b488f7c3db4b2447dc58 ] btf_var_show() calls btf_type_id_resolve() unconditionally, which dereferences btf->resolved_ids. That is NULL for a base BTF - e.g. the vmlinux BTF that bpf_snprintf_btf() renders against - since base BTF is not resolved during parsing. btf_modifier_show() guards this with 'if (btf->resolved_ids)', but btf_var_show() does not. A BPF program that passes the type_id of a BTF_KIND_VAR from the vmlinux BTF to bpf_snprintf_btf() thus NULL-derefs: KASAN: probably user-memory-access in range [0x46638-0x4663f] RIP: 0010:btf_var_show (kernel/bpf/btf.c:2929) Call Trace: btf_type_show (kernel/bpf/btf.c:8259) btf_type_snprintf_show (kernel/bpf/btf.c:8329) bpf_snprintf_btf (kernel/trace/bpf_trace.c:1047) bpf_prog_test_run_raw_tp (net/bpf/test_run.c:829) __sys_bpf (kernel/bpf/syscall.c:4804) do_syscall_64 (arch/x86/entry/syscall_64.c:84) entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121) Resolve the var's type directly with btf_type_skip_modifiers() when resolved_ids is NULL, mirroring btf_modifier_show(). Fixes: c4d0bfb45068 ("bpf: Add bpf_snprintf_btf helper") Signed-off-by: Jiayuan Chen Acked-by: Ihor Solodrai Link: https://lore.kernel.org/r/20260901104924.346187-4-jiayuan.chen@linux.dev Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- kernel/bpf/btf.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index da85fc671023c..cf6883e7b158f 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -2682,7 +2682,15 @@ static void btf_var_show(const struct btf *btf, const struct btf_type *t, u32 type_id, void *data, u8 bits_offset, struct btf_show *show) { - t = btf_type_id_resolve(btf, &type_id); + /* + * btf_type_id_resolve() dereferences btf->resolved_ids, which is NULL + * for a base BTF (e.g. the vmlinux BTF that bpf_snprintf_btf() uses). + * Resolve the var's type directly in that case. + */ + if (btf->resolved_ids) + t = btf_type_id_resolve(btf, &type_id); + else + t = btf_type_skip_modifiers(btf, t->type, &type_id); btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show); } -- 2.53.0