From: Jiri Olsa <olsajiri@gmail.com>
To: Hou Tao <houtao@huaweicloud.com>
Cc: bpf@vger.kernel.org, Martin KaFai Lau <martin.lau@linux.dev>,
Alexei Starovoitov <alexei.starovoitov@gmail.com>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Hao Luo <haoluo@google.com>,
Yonghong Song <yonghong.song@linux.dev>,
Daniel Borkmann <daniel@iogearbox.net>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@fomichev.me>,
John Fastabend <john.fastabend@gmail.com>,
Yafang Shao <laoar.shao@gmail.com>,
houtao1@huawei.com, xukuohai@huawei.com
Subject: Re: [PATCH bpf v2 2/7] bpf: Add assertion for the size of bpf_link_type_strs[]
Date: Mon, 21 Oct 2024 10:18:39 +0200 [thread overview]
Message-ID: <ZxYOX9_sIrSKGFB2@krava> (raw)
In-Reply-To: <20241021014004.1647816-3-houtao@huaweicloud.com>
On Mon, Oct 21, 2024 at 09:39:59AM +0800, Hou Tao wrote:
> From: Hou Tao <houtao1@huawei.com>
>
> If a corresponding link type doesn't invoke BPF_LINK_TYPE(), accessing
> bpf_link_type_strs[link->type] may result in out-of-bound access.
>
> To prevent such missed invocations in the future, the following static
> assertion seems feasible:
>
> BUILD_BUG_ON(ARRAY_SIZE(bpf_link_type_strs) != __MAX_BPF_LINK_TYPE)
>
> However, this doesn't work well. The reason is that the invocation of
> BPF_LINK_TYPE() for one link type is optional due to its CONFIG_XXX
> dependency and the elements in bpf_link_type_strs[] will be sparse. For
> example, if CONFIG_NET is disabled, the size of bpf_link_type_strs will
> be BPF_LINK_TYPE_UPROBE_MULTI + 1.
>
> Therefore, in addition to the static assertion, remove all CONFIG_XXX
> conditions for the invocation of BPF_LINK_TYPE(). If these CONFIG_XXX
> conditions become necessary later, the fix may need to be revised (e.g.,
> to check the validity of link_type in bpf_link_show_fdinfo()).
>
> Signed-off-by: Hou Tao <houtao1@huawei.com>
> ---
> include/linux/bpf_types.h | 6 ------
> kernel/bpf/syscall.c | 2 ++
> 2 files changed, 2 insertions(+), 6 deletions(-)
>
> diff --git a/include/linux/bpf_types.h b/include/linux/bpf_types.h
> index fa78f49d4a9a..6b7eabe9a115 100644
> --- a/include/linux/bpf_types.h
> +++ b/include/linux/bpf_types.h
> @@ -136,21 +136,15 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_ARENA, arena_map_ops)
>
> BPF_LINK_TYPE(BPF_LINK_TYPE_RAW_TRACEPOINT, raw_tracepoint)
> BPF_LINK_TYPE(BPF_LINK_TYPE_TRACING, tracing)
> -#ifdef CONFIG_CGROUP_BPF
> BPF_LINK_TYPE(BPF_LINK_TYPE_CGROUP, cgroup)
> -#endif
> BPF_LINK_TYPE(BPF_LINK_TYPE_ITER, iter)
> -#ifdef CONFIG_NET
> BPF_LINK_TYPE(BPF_LINK_TYPE_NETNS, netns)
> BPF_LINK_TYPE(BPF_LINK_TYPE_XDP, xdp)
> BPF_LINK_TYPE(BPF_LINK_TYPE_NETFILTER, netfilter)
> BPF_LINK_TYPE(BPF_LINK_TYPE_TCX, tcx)
> BPF_LINK_TYPE(BPF_LINK_TYPE_NETKIT, netkit)
> BPF_LINK_TYPE(BPF_LINK_TYPE_SOCKMAP, sockmap)
> -#endif
> -#ifdef CONFIG_PERF_EVENTS
> BPF_LINK_TYPE(BPF_LINK_TYPE_PERF_EVENT, perf)
> -#endif
> BPF_LINK_TYPE(BPF_LINK_TYPE_KPROBE_MULTI, kprobe_multi)
> BPF_LINK_TYPE(BPF_LINK_TYPE_STRUCT_OPS, struct_ops)
> BPF_LINK_TYPE(BPF_LINK_TYPE_UPROBE_MULTI, uprobe_multi)
> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 8cfa7183d2ef..9f335c379b05 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -3071,6 +3071,8 @@ static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
> const struct bpf_prog *prog = link->prog;
> char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
>
> + BUILD_BUG_ON(ARRAY_SIZE(bpf_link_type_strs) != __MAX_BPF_LINK_TYPE);
I wonder it'd be simpler to just kill BPF_LINK_TYPE completely
and add link names directly to bpf_link_type_strs array..
it seems it's the only purpose of the BPF_LINK_TYPE macro
jirka
next prev parent reply other threads:[~2024-10-21 8:18 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-21 1:39 [PATCH bpf v2 0/7] Misc fixes for bpf Hou Tao
2024-10-21 1:39 ` [PATCH bpf v2 1/7] bpf: Add the missing BPF_LINK_TYPE invocation for sockmap Hou Tao
2024-10-21 1:39 ` [PATCH bpf v2 2/7] bpf: Add assertion for the size of bpf_link_type_strs[] Hou Tao
2024-10-21 8:18 ` Jiri Olsa [this message]
2024-10-21 23:02 ` Andrii Nakryiko
2024-10-22 7:35 ` Hou Tao
2024-10-22 17:40 ` Andrii Nakryiko
2024-10-22 20:26 ` Alexei Starovoitov
2024-10-22 20:41 ` Andrii Nakryiko
2024-10-21 1:40 ` [PATCH bpf v2 3/7] bpf: Preserve param->string when parsing mount options Hou Tao
2024-10-21 9:09 ` Jiri Olsa
2024-10-21 1:40 ` [PATCH bpf v2 4/7] bpf: Free dynamically allocated bits in bpf_iter_bits_destroy() Hou Tao
2024-10-21 2:45 ` Hou Tao
2024-10-21 23:07 ` Andrii Nakryiko
2024-10-22 7:25 ` Hou Tao
2024-10-21 1:40 ` [PATCH bpf v2 5/7] bpf: Check the validity of nr_words in bpf_iter_bits_new() Hou Tao
2024-10-21 9:51 ` Jiri Olsa
2024-10-21 23:09 ` Andrii Nakryiko
2024-10-23 3:17 ` Yafang Shao
2024-10-23 8:29 ` Hou Tao
2024-10-23 9:25 ` Yafang Shao
2024-10-23 9:34 ` Yafang Shao
2024-10-21 1:40 ` [PATCH bpf v2 6/7] bpf: Use __u64 to save the bits in bits iterator Hou Tao
2024-10-23 3:10 ` Yafang Shao
2024-10-23 8:09 ` Hou Tao
2024-10-21 1:40 ` [PATCH bpf v2 7/7] selftests/bpf: Test multiplication overflow of nr_bits in bits_iter Hou Tao
2024-10-21 23:11 ` [PATCH bpf v2 0/7] Misc fixes for bpf Andrii Nakryiko
2024-10-22 7:37 ` Hou Tao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZxYOX9_sIrSKGFB2@krava \
--to=olsajiri@gmail.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=houtao1@huawei.com \
--cc=houtao@huaweicloud.com \
--cc=john.fastabend@gmail.com \
--cc=kpsingh@kernel.org \
--cc=laoar.shao@gmail.com \
--cc=martin.lau@linux.dev \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=xukuohai@huawei.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox