From nobody Sat Sep 26 19:33:47 2026 Received: from mta1.migadu.com (out-12.mta1.migadu.com [95.215.58.12]) (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 3D3EB33DEF7 for ; Mon, 31 Aug 2026 11:03:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788174237; cv=none; b=C4ng899vrlAUMrFbRaK2DRVEBN9ElOw/qiY/LKv+KXj9Qdb+7BEHTAAYpladYNaaTKljwOlbIXIzlp2ZHRvpHxuDz5WAMqSRm/bnxibD4DvhDkrs2BD935DWpYthAtTgsOgDUXJyEmH8FRpbw3Qs3P925RPKHPax5rH686o4PK8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788174237; c=relaxed/simple; bh=ChU7rLrz5Ly5f6u8c+cGt26nIjegz2kM1bSEHnszRUA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oycDnOjVFbpD3MEh/6tEHPAnW7I5C5zqQT5x9KC6uZleIk9gwhpL1IxmgkUY/FL0PbYh6YbEHFpUhw1GSgxVdj8ex6q/e0Bxn55wXx9pcQOE5OZi6rBgOK4hSdDFshJ8hxjWsXG/BZOttDhXKM0qmUk6isce1bYRNf7SG7fmklk= 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=jmwFNBib; arc=none smtp.client-ip=95.215.58.12 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="jmwFNBib" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=ChU7rLrz5Ly5f6u8c+cGt26nIjegz2kM1bSEHnszRUA=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1788174234; v=1; x=1788779034; b=jmwFNBibWAk20ARxYY5i/Yk8K8RAIQPX/aRb+6F9DgC4ddC+HGw6D85H9hQIgS5KZcTaQ/BH PCPU62Nghsr/aTvAKVTTZqMSrd++WNzlnxDVUo+0BfvITOSDc1q3YsK2MqAsXBXuq/I4CcN2Iwu t2EvpIrr7GJyTg2uKyHqFRCw= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 27ca0f8a3d15d337; Mon, 31 Aug 2026 11:03:53 +0000 X-Mizu-Trace-ID: 27ca0f8a3d15d337 X-Migadu-Flow: FLOW_OUT From: Jiayuan Chen To: bpf@vger.kernel.org Cc: Jiayuan Chen , syzbot+37b56485bbbf90ad8489@syzkaller.appspotmail.com, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Shuah Khan , Mykyta Yatsenko , Alan Maguire , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf v3 1/5] bpf: Reject key-less BTF for hash maps Date: Mon, 31 Aug 2026 19:01:06 +0800 Message-ID: <20260831110314.150870-2-jiayuan.chen@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260831110314.150870-1-jiayuan.chen@linux.dev> References: <20260831110314.150870-1-jiayuan.chen@linux.dev> 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 Content-Type: text/plain; charset="utf-8" map_check_btf() allows a key-less BTF (btf_key_type_id =3D=3D 0) only for maps that have a ->map_check_btf callback, and leaves the actual decision to that callback. Hash maps used to have no ->map_check_btf, so a key-less BTF was rejected outright. That changed when htab and rhtab gained a ->map_check_btf to register a dtor - htab in commit 1df97a7453ee ("bpf: Register dtor for freeing special fields") and rhtab in commit 6905f8601298 ("bpf: Allow special fields in resizable hashtab"). Neither looks at the key, so a key-less hash map now passes map_check_btf() and gets created. Reading it back through bpffs feeds the key type_id 0 into btf_type_seq_show(); btf_type_by_id() returns the void type, kind_ops[BTF_KIND_UNKN] is NULL, and btf_type_show() dereferences it: KASAN: null-ptr-deref in range [0x0000000000000028-0x000000000000002f] RIP: 0010:btf_type_show+0x223/0x2e0 kernel/bpf/btf.c:8232 Call Trace: btf_type_seq_show_flags+0xca/0x120 kernel/bpf/btf.c:8250 htab_map_seq_show_elem+0x12e/0x350 kernel/bpf/hashtab.c:1669 map_seq_show+0x13d/0x1e0 kernel/bpf/inode.c:293 seq_read_iter+0x93f/0x1270 fs/seq_file.c:196 seq_read+0x344/0x4d0 fs/seq_file.c:163 vfs_read+0x1e4/0xb40 fs/read_write.c:572 __x64_sys_pread64+0x1eb/0x250 fs/read_write.c:769 do_syscall_64+0x123/0x790 arch/x86/entry/syscall_64.c:84 entry_SYSCALL_64_after_hwframe+0x77/0x7f Reject a key-less BTF in htab_map_check_btf() and rhtab_map_check_btf(), restoring the previous behavior. Fixes: 1df97a7453ee ("bpf: Register dtor for freeing special fields") Fixes: 6905f8601298 ("bpf: Allow special fields in resizable hashtab") Reported-by: syzbot+37b56485bbbf90ad8489@syzkaller.appspotmail.com Closes: https://lore.kernel.org/all/6a8f4e88.27659fcc.2ceef7.0008.GAE@googl= e.com/T/ Signed-off-by: Jiayuan Chen Acked-by: Ihor Solodrai --- kernel/bpf/hashtab.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index d40cb5dd446c..43123424183c 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -530,6 +530,10 @@ static int htab_map_check_btf(struct bpf_map *map, con= st struct btf *btf, { struct bpf_htab *htab =3D container_of(map, struct bpf_htab, map); =20 + /* Hash maps have no use for a key-less BTF. */ + if (btf_type_is_void(key_type)) + return -EINVAL; + if (htab_is_prealloc(htab)) return 0; /* @@ -3110,6 +3114,10 @@ static int rhtab_map_check_btf(struct bpf_map *map, = const struct btf *btf, { struct bpf_rhtab *rhtab =3D container_of(map, struct bpf_rhtab, map); =20 + /* Hash maps have no use for a key-less BTF. */ + if (btf_type_is_void(key_type)) + return -EINVAL; + return bpf_ma_set_dtor(map, &rhtab->ma, rhtab_mem_dtor); } =20 --=20 2.43.0 From nobody Sat Sep 26 19:33:47 2026 Received: from mta0.migadu.com (out-237.mta0.migadu.com [91.218.175.237]) (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 0C4B4342509 for ; Mon, 31 Aug 2026 11:04:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.237 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788174265; cv=none; b=GY+p1PiHJeaTB08OB1X+t8Lt9U82ec+SDXZwVWHOvjqX/ma8GVk2WMa6WcXEvTvBgOfL1Mnr4LcWqiDRLQuplK8ZKpKexUxxN3DtcmtRTE3w3fge53EWydIKo+WrgP2/wvWNqxcdGWDcLBnkd+NNsqRas91ehVtgyRNlWj1e360= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788174265; c=relaxed/simple; bh=MZqBEMfNU2/DRjrc3AMVWf0ULkLuJij9FfiG6ryc5V8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WUInMoUw1ulP2y8PQ1QaOr5KZCa94u4f5F7zO0BtY68xBrCvnVQm0azIcOQe1qX5b2eklvNFvjBrZ9L2lDDI1P+15U+ZuT5JeJyuSvpeDnANWDVR30KmHBADv8OphBKFP1HcgeVkgEq7Y6C9r4RacsFz9VxugPzStxMAT198e48= 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=UXfvt/k2; arc=none smtp.client-ip=91.218.175.237 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="UXfvt/k2" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=MZqBEMfNU2/DRjrc3AMVWf0ULkLuJij9FfiG6ryc5V8=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1788174261; v=1; x=1788779061; b=UXfvt/k2ZbhuKuP5rfveqerf8sd6weitkYAHzkW/m701o5KKAjmw3MBxcxMe2CtkRdMuDjH5 GzZS8CWhJTCrQ8xq4jfOd8kj9/PckC8zxewBtcIHfBNG6Xq0ZlDbLKmXNIIxqqQ3j/BNH31/YcW SPIdyHL/DFA19b7qOyaCMEXo= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id b7e16e44ec0b80f5; Mon, 31 Aug 2026 11:04:21 +0000 X-Mizu-Trace-ID: b7e16e44ec0b80f5 X-Migadu-Flow: FLOW_OUT From: Jiayuan Chen To: bpf@vger.kernel.org Cc: Jiayuan Chen , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Shuah Khan , Mykyta Yatsenko , Alan Maguire , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf v3 2/5] bpf: Fix NULL-ptr-deref when showing a void BTF type Date: Mon, 31 Aug 2026 19:01:07 +0800 Message-ID: <20260831110314.150870-3-jiayuan.chen@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260831110314.150870-1-jiayuan.chen@linux.dev> References: <20260831110314.150870-1-jiayuan.chen@linux.dev> 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 Content-Type: text/plain; charset="utf-8" btf_modifier_show() resolves the modifier and then calls btf_type_ops(t)->show() unconditionally. For the void type (type_id 0, BTF_KIND_UNKN) kind_ops[] has no entry, so ->show is NULL. The map dump path cannot reach a void type (a map key/value must have a size and void has none), but bpf_snprintf_btf() takes a type_id straight from the BPF program, and a "const void" (a modifier that resolves to void, present in the vmlinux BTF) NULL-derefs there: KASAN: null-ptr-deref in range [0x0000000000000028-0x000000000000002f] RIP: 0010:btf_modifier_show (kernel/bpf/btf.c:2914) Call Trace: btf_type_show (kernel/bpf/btf.c:8251) btf_type_snprintf_show (kernel/bpf/btf.c:8321) 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:94) entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121) void has no size, so there is nothing to render - not even a byte length to fall back to dumping as raw hex. btf_df_show() is already the ->show for the other kinds that carry no value to print - FWD, FUNC, FUNC_PROTO, FLOAT and DECL_TAG - and emits an "" placeholder; void belongs to the same group and only lacks a show op because it has no kind_ops[] entry at all. Fall back to btf_df_show() in btf_modifier_show() when the resolved type has no show op, so a void type prints that placeholder instead of crashing; bpf_snprintf_btf() then returns the length as usual. 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 91b8ce77f699..6e267c4c4e1f 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -2911,7 +2911,15 @@ static void btf_modifier_show(const struct btf *btf, else t =3D btf_type_skip_modifiers(btf, type_id, NULL); =20 - btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show); + /* + * A modifier can resolve to the void type (e.g. "const void"), which + * has no show op (kind_ops[BTF_KIND_UNKN] is NULL). Print a placeholder + * instead of dereferencing NULL. + */ + if (!btf_type_ops(t)) + btf_df_show(btf, t, type_id, data, bits_offset, show); + else + btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show); } =20 static void btf_var_show(const struct btf *btf, const struct btf_type *t, --=20 2.43.0 From nobody Sat Sep 26 19:33:47 2026 Received: from mta0.migadu.com (out-249.mta0.migadu.com [91.218.175.249]) (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 5527F345CCE for ; Mon, 31 Aug 2026 11:04:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.249 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788174296; cv=none; b=PHYhV/LEPS8IBibFsPayZAF7hb85bGXcpREtchsAsPPWIn2lXaP9GsBDPr30Zawz2B1kusGIS9DPaFhMD4y8ZFdJBgNNupQ0u3ot9XJBrWVgRwgP5YG+DnpV0OXVsyAyPTDQYfymCo6Vw4erBy8a04UHcEWzmtMRMycr23KGdKM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788174296; c=relaxed/simple; bh=7IVZKYnERBZ3SOHrfGkdcEFGIt3uXESKgR5MqtCSsfI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZNtjGCBYDQhy4x2n1QFwCPC6Hzt21MVRTXHeK4+Fu4W+B0Xo0JCcAP20S+f1iFGjYcLSriX7/Di4y3sceTPnYc0hdhq2rRByN0NtARf4U6nTchLMcrLh/MpFJqT78fQby0n2QVorD/7tnYyZYyUhaWHpXtZUOB0Iyl0SdbSDrZ0= 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=B7Vnotce; arc=none smtp.client-ip=91.218.175.249 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="B7Vnotce" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=7IVZKYnERBZ3SOHrfGkdcEFGIt3uXESKgR5MqtCSsfI=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1788174293; v=1; x=1788779093; b=B7VnotceCQHqKt+GDUC7TI5Bj9LwMQGJ+wIaoI6f4DEwMKVz7Mwnn8w/CUYxGvK1V+4JuO/G ZXu8xcHhLjRAHMK/DpnvUCk2PU/9a3aBkaYQln457SILsT8qiwjZIT9KWI7v+nUa8osHEcFtE9+ ufqPVnRxb1jJ2clLRjFfu3PM= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 46169a63f5267a32; Mon, 31 Aug 2026 11:04:53 +0000 X-Mizu-Trace-ID: 46169a63f5267a32 X-Migadu-Flow: FLOW_OUT From: Jiayuan Chen To: bpf@vger.kernel.org Cc: Jiayuan Chen , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Shuah Khan , Mykyta Yatsenko , Alan Maguire , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf v3 3/5] bpf: Fix NULL-ptr-deref in btf_var_show() Date: Mon, 31 Aug 2026 19:01:08 +0800 Message-ID: <20260831110314.150870-4-jiayuan.chen@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260831110314.150870-1-jiayuan.chen@linux.dev> References: <20260831110314.150870-1-jiayuan.chen@linux.dev> 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 Content-Type: text/plain; charset="utf-8" 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 6e267c4c4e1f..3cf35ee49c10 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -2926,7 +2926,15 @@ static void btf_var_show(const struct btf *btf, cons= t struct btf_type *t, u32 type_id, void *data, u8 bits_offset, struct btf_show *show) { - t =3D 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 =3D btf_type_id_resolve(btf, &type_id); + else + t =3D btf_type_skip_modifiers(btf, t->type, &type_id); =20 btf_type_ops(t)->show(btf, t, type_id, data, bits_offset, show); } --=20 2.43.0 From nobody Sat Sep 26 19:33:47 2026 Received: from mta0.migadu.com (out-4.mta0.migadu.com [91.218.175.4]) (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 BD15C34CFC2 for ; Mon, 31 Aug 2026 11:05:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788174324; cv=none; b=lIqe//uNmaC+IQNplMzg7mIapjKg5A9aciWWC++znNt+2kQbsz6Koes4JP2nTXYUwIETeQ0Y1rDJi+5O8RKC6nHV+XRJqAwOOXo2oCn8SYi7Az0U2TiKDlQEcbupvPpslPNrQHXY8lgA1amYVPEPPMOOMZyr8mbH+/CEWtbVZQ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788174324; c=relaxed/simple; bh=8WHtN6ourocR0E48NCxNPrEIJOFHpFqlFquB2qhCgpA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Dzqn1YMpqghnvPBWVT+XcvOq62zZnK0O3U2be28arLjvrSemaLsVnHQlb+ta7tHhrJfFdMVaCZGFvm2NX8Vz59YBlLIALpYMmezKxWc/2IpB7fjfTNu+uEkavcWx1lRrm5vqQPGrZlFxGg3a7siX64c5jGRxr5nJAsRqcmSTFgM= 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=mRV5w/Sq; arc=none smtp.client-ip=91.218.175.4 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="mRV5w/Sq" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=8WHtN6ourocR0E48NCxNPrEIJOFHpFqlFquB2qhCgpA=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1788174320; v=1; x=1788779120; b=mRV5w/SqUAWJ2quLPiAcMmP0XSiUMu7x1cbKU0KXKv9Y+XF6CC9QLaAhJRNAz4+7zK0pYBZY 7x0dOeXYA0OC9r2bpSstmlrZXREGT7xnPSGMVzF7ZfkVH/jIic3yqv7ZomWKQqo1FxFtBLuaOce euavRX1TxVLFwXYLfs16CAH8= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 97a19a524f8c428b; Mon, 31 Aug 2026 11:05:20 +0000 X-Mizu-Trace-ID: 97a19a524f8c428b X-Migadu-Flow: FLOW_OUT From: Jiayuan Chen To: bpf@vger.kernel.org Cc: Jiayuan Chen , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Shuah Khan , Mykyta Yatsenko , Alan Maguire , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf v3 4/5] selftests/bpf: Add test for key-less BTF hash map Date: Mon, 31 Aug 2026 19:01:09 +0800 Message-ID: <20260831110314.150870-5-jiayuan.chen@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260831110314.150870-1-jiayuan.chen@linux.dev> References: <20260831110314.150870-1-jiayuan.chen@linux.dev> 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 Content-Type: text/plain; charset="utf-8" Create a hash and an rhash map with btf_key_type_id =3D=3D 0 and expect bpf_map_create() to fail with -EINVAL; a positive control with a real key type confirms the rejection is about the key-less BTF. On an unfixed kernel the map is created, and the test pins and reads it back to walk the bpffs dump path, which reproduces the btf_type_show() NULL-deref - so running it on an unfixed kernel oopses the reading task (and panics it under panic_on_oops). Signed-off-by: Jiayuan Chen --- .../bpf/prog_tests/btf_map_keyless.c | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_map_keyless.c diff --git a/tools/testing/selftests/bpf/prog_tests/btf_map_keyless.c b/too= ls/testing/selftests/bpf/prog_tests/btf_map_keyless.c new file mode 100644 index 000000000000..a7d037f57a7e --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/btf_map_keyless.c @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +/* + * A hash map with a key-less BTF (btf_key_type_id =3D=3D 0) used to NULL-= deref in + * btf_type_show() when dumped via bpffs. + * A fixed kernel rejects such a map at creation; on an unfixed kernel the + * pin-and-read below deliberately walks that bpffs dump path, so it doubl= es + * as a reproducer: it oopses an unfixed kernel (and panics it under + * panic_on_oops). + */ +static void check_keyless(int map_type, __u32 map_flags, int btf_fd, int v= al_id) +{ + LIBBPF_OPTS(bpf_map_create_opts, opts); + const char *path =3D "/sys/fs/bpf/keyless_map"; + __u32 key =3D 1, val =3D 0x41424344; + char buf[256]; + int map_fd; + FILE *f; + + opts.map_flags =3D map_flags; + opts.btf_fd =3D btf_fd; + opts.btf_value_type_id =3D val_id; + + /* + * Positive control: the same map with a real key type must be accepted, + * so the -EINVAL below is about the key-less BTF and not some unrelated + * rejection (e.g. an unknown map type). + */ + opts.btf_key_type_id =3D val_id; + map_fd =3D bpf_map_create(map_type, "keyed_map", 4, 4, 8, &opts); + if (!ASSERT_GE(map_fd, 0, "keyed create is accepted")) + return; + close(map_fd); + + /* A key-less BTF must be rejected. */ + opts.btf_key_type_id =3D 0; + map_fd =3D bpf_map_create(map_type, "keyless_map", 4, 4, 8, &opts); + + if (map_fd >=3D 0) { + /* Unfixed kernel: reproduce the oops via the bpffs dump path. */ + (void)bpf_map_update_elem(map_fd, &key, &val, 0); + if (bpf_obj_pin(map_fd, path) =3D=3D 0) { + f =3D fopen(path, "r"); + if (f) { + while (fgets(buf, sizeof(buf), f)) + ; + fclose(f); + } + unlink(path); + } + close(map_fd); + } + + ASSERT_EQ(map_fd, -EINVAL, "key-less create is rejected"); +} + +void test_btf_map_keyless(void) +{ + int btf_fd, val_id; + struct btf *btf; + + btf =3D btf__new_empty(); + if (!ASSERT_OK_PTR(btf, "btf__new_empty")) + return; + + val_id =3D btf__add_int(btf, "int", 4, BTF_INT_SIGNED); + if (!ASSERT_GT(val_id, 0, "btf__add_int")) + goto out; + + if (!ASSERT_OK(btf__load_into_kernel(btf), "btf__load_into_kernel")) + goto out; + btf_fd =3D btf__fd(btf); + + if (test__start_subtest("hash")) + check_keyless(BPF_MAP_TYPE_HASH, 0, btf_fd, val_id); + if (test__start_subtest("rhash")) + check_keyless(BPF_MAP_TYPE_RHASH, BPF_F_NO_PREALLOC, btf_fd, val_id); +out: + btf__free(btf); +} --=20 2.43.0 From nobody Sat Sep 26 19:33:47 2026 Received: from mta0.migadu.com (out-17.mta0.migadu.com [91.218.175.17]) (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 D8AF33B14CE for ; Mon, 31 Aug 2026 11:05:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788174355; cv=none; b=Mk3bhyJTXDBhGe3897armtaIZwXLH7IzV4LtFvB8dskAiPAn/VZQm7CnM520esYvFt9xTcXgCp3lNlu0YC7G7/V425vjWSb+z1/AFSNxPFloEk4rO+2PLrHx4We/9ZuWwJ4bngC8CkOw7O/natUdSNnYhY9Pic4tPwvEDX/EojQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788174355; c=relaxed/simple; bh=4uP82EAtBfHYzE0BX91YpcIeTVIQbLCyiWnv6c3JKd4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CUJa7fLyo0xlMoIp+PMujam3dFO123xhh9B4uIzWmOPgYOV5pug65RZRu+2++iSEYgB0+odutW8IhTevnu0YNmzBdo2HraGV04w7Rnfst0X8FpCphcH/OXijvNwI9YvEFXIBmedtaiJGAIwT/oc/qBBWFcsrzjeHchA4ltcT050= 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=R0qUIyoH; arc=none smtp.client-ip=91.218.175.17 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="R0qUIyoH" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=4uP82EAtBfHYzE0BX91YpcIeTVIQbLCyiWnv6c3JKd4=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1788174351; v=1; x=1788779151; b=R0qUIyoHsrQiJ4n/VzBz+3eOXEIRQfCfnMhGMtP+IaJf/baxmhAyzhcGdt09ZpRby/owFVls UYM2iLpX5QobRBvyWObeDsjbbfQxcuSNc3HvZcLVxpec2rQxPP3NHYKfJrBjMD2We/nXAzJcBPW FL59dwjBjapE5KsZtm12MX68= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id f2098870f5880476; Mon, 31 Aug 2026 11:05:51 +0000 X-Mizu-Trace-ID: f2098870f5880476 X-Migadu-Flow: FLOW_OUT From: Jiayuan Chen To: bpf@vger.kernel.org Cc: Jiayuan Chen , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Shuah Khan , Mykyta Yatsenko , Alan Maguire , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf v3 5/5] selftests/bpf: Add test for showing a void BTF type Date: Mon, 31 Aug 2026 19:01:10 +0800 Message-ID: <20260831110314.150870-6-jiayuan.chen@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260831110314.150870-1-jiayuan.chen@linux.dev> References: <20260831110314.150870-1-jiayuan.chen@linux.dev> 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 Content-Type: text/plain; charset="utf-8" Call bpf_snprintf_btf() with type_ids from the vmlinux BTF that used to NULL-deref in the BTF show path: a "const void" (checked to render the "" placeholder) and a BTF_KIND_VAR (checked to render without crashing). On an unfixed kernel each oopses the task (and panics it under panic_on_oops), so the test doubles as a reproducer. Signed-off-by: Jiayuan Chen --- .../selftests/bpf/prog_tests/btf_show_void.c | 81 +++++++++++++++++++ .../selftests/bpf/progs/btf_show_void.c | 24 ++++++ 2 files changed, 105 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_show_void.c create mode 100644 tools/testing/selftests/bpf/progs/btf_show_void.c diff --git a/tools/testing/selftests/bpf/prog_tests/btf_show_void.c b/tools= /testing/selftests/bpf/prog_tests/btf_show_void.c new file mode 100644 index 000000000000..d73232dc2a04 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/btf_show_void.c @@ -0,0 +1,81 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include "btf_show_void.skel.h" + +/* + * bpf_snprintf_btf() renders a type_id taken straight from the vmlinux BT= F. + * Two such type_ids used to NULL-deref in the BTF show path: + * - a "const void" (a modifier resolving to void) in btf_modifier_show() + * - a BTF_KIND_VAR in btf_var_show() (base BTF has no resolved_ids) + * A fixed kernel renders both without crashing. + */ +static long run(struct btf_show_void *skel, __u32 type_id) +{ + LIBBPF_OPTS(bpf_test_run_opts, topts); + char ctx[8] =3D {}; + + skel->bss->type_id =3D type_id; + topts.ctx_in =3D ctx; + topts.ctx_size_in =3D sizeof(ctx); + if (!ASSERT_OK(bpf_prog_test_run_opts(bpf_program__fd(skel->progs.dump_ty= pe), + &topts), "test_run")) + return -1; + return skel->bss->ret; +} + +void test_btf_show_void(void) +{ + const struct btf_type *t; + struct btf_show_void *skel; + int i, n, cv =3D 0, var =3D 0; + struct btf *btf; + + btf =3D btf__parse("/sys/kernel/btf/vmlinux", NULL); + if (!btf) { + test__skip(); + return; + } + + skel =3D btf_show_void__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open_and_load")) + goto out_btf; + + n =3D btf__type_cnt(btf); + for (i =3D 1; i < n && !(cv && var); i++) { + t =3D btf__type_by_id(btf, i); + if (!cv && btf_kind(t) =3D=3D BTF_KIND_CONST && t->type =3D=3D 0) + cv =3D i; + /* Pick a VAR small enough to render from the program's buffer. */ + if (!var && btf_kind(t) =3D=3D BTF_KIND_VAR) { + long sz =3D btf__resolve_size(btf, t->type); + + if (sz > 0 && sz <=3D (long)sizeof(skel->bss->obj)) + var =3D i; + } + } + + /* "const void" renders the "" placeholder. */ + if (test__start_subtest("const_void")) { + if (cv) { + ASSERT_EQ(run(skel, cv), + sizeof("") - 1, "ret"); + ASSERT_STREQ(skel->bss->out, "", + "placeholder"); + } else { + test__skip(); + } + } + + /* A BTF_KIND_VAR must resolve and render without error. */ + if (test__start_subtest("var")) { + if (var) + ASSERT_GT(run(skel, var), 0, "ret"); + else + test__skip(); + } + + btf_show_void__destroy(skel); +out_btf: + btf__free(btf); +} diff --git a/tools/testing/selftests/bpf/progs/btf_show_void.c b/tools/test= ing/selftests/bpf/progs/btf_show_void.c new file mode 100644 index 000000000000..44af80fbbb80 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/btf_show_void.c @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "btf_ptr.h" +#include + +__u32 type_id; +/* A buffer we own to render the selected type from, kept in bounds. */ +char obj[256]; +char out[64]; +long ret; + +SEC("raw_tp/sys_enter") +int dump_type(void *ctx) +{ + struct btf_ptr ptr =3D { + .ptr =3D obj, + .type_id =3D type_id, + .flags =3D 0, + }; + + ret =3D bpf_snprintf_btf(out, sizeof(out), &ptr, sizeof(ptr), 0); + return 0; +} + +char _license[] SEC("license") =3D "GPL"; --=20 2.43.0