From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mta1.migadu.com (out-15.mta1.migadu.com [95.215.58.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A4BF47AF5C for ; Tue, 1 Sep 2026 10:51:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.15 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788259880; cv=none; b=SyTFtwOPklNidxWsNiiL5z9GviQakGOF35pxwY8ld5fTcFri4a04WYvWztTDtFzhtRMTrzzZJdiHQfZZXPJFa5SOlD5GgFoGYXF7OSTxv27xmFhstAzgvBRvvyxz3fJjhJ3YOgnf/d5Y23eSwqYY180yZ1ryH/uHfFk9zPdPLEE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788259880; c=relaxed/simple; bh=edHUuQXAD8zD3HIZyMJ5TXVcUbJG2wFP7sF8oZ3FG3k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oZBOqrYgnNIn3fdLxWFOd5BrdVv8G3BBZUgMbSnO/6XU+s1FBQ7asISZG3BYvVGMpQwh3el3g4EX5Z7VRlku/DL04rlpECzHIbre7vNDpgJg+Dx28PID1+cxv9yKyuLdU/ziiEkI1DiJo2V0uF4hpraCKe1OUzzDBfpH3sV9z3g= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=WLgHcUdn; arc=none smtp.client-ip=95.215.58.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="WLgHcUdn" X-Envelope-To: linux-kselftest@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=edHUuQXAD8zD3HIZyMJ5TXVcUbJG2wFP7sF8oZ3FG3k=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1788259874; v=1; x=1788864674; b=WLgHcUdn64zDH6jeEEcgMteCSzphLgYQeZ3fnW7bYSiYRAn6xg4DkwbdukdlpAqrL8MX/Gpx I1WPML+a5lPoBv+lEBtSh5nMT52DomBDYGwHsL522TbncaSkNkVbCYY23jLvLKIloGYyN4HeZtd +uDMue9J78/yJh6/CNyOmBBw= X-Envelope-To: linux-kselftest@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 59ba207f978f01a8; Tue, 01 Sep 2026 10:51:14 +0000 X-Mizu-Trace-ID: 59ba207f978f01a8 X-Migadu-Flow: FLOW_OUT From: Jiayuan Chen To: bpf@vger.kernel.org Cc: Jiayuan Chen , Ihor Solodrai , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Shuah Khan , Mykyta Yatsenko , Alan Maguire , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf v4 3/5] bpf: Fix NULL-ptr-deref in btf_var_show() Date: Tue, 1 Sep 2026 18:47:37 +0800 Message-ID: <20260901104924.346187-4-jiayuan.chen@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260901104924.346187-1-jiayuan.chen@linux.dev> References: <20260901104924.346187-1-jiayuan.chen@linux.dev> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 7835fe201384..7c634d639b7f 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -2925,7 +2925,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.43.0