Commit 539da5f2 authored by Hou Tao's avatar Hou Tao Committed by Wen Zhiwei
Browse files

bpf: Check validity of link->type in bpf_link_show_fdinfo()

stable inclusion
from stable-v6.6.62
commit d5092b0a1aaf35d77ebd8d33384d7930bec5cb5d
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/IB5BUT

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=d5092b0a1aaf35d77ebd8d33384d7930bec5cb5d



--------------------------------

[ Upstream commit 8421d4c8762bd022cb491f2f0f7019ef51b4f0a7 ]

If a newly-added link type doesn't invoke BPF_LINK_TYPE(), accessing
bpf_link_type_strs[link->type] may result in an out-of-bounds access.

To spot such missed invocations early in the future, checking the
validity of link->type in bpf_link_show_fdinfo() and emitting a warning
when such invocations are missed.

Signed-off-by: default avatarHou Tao <houtao1@huawei.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20241024013558.1135167-3-houtao@huaweicloud.com


Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
Signed-off-by: default avatarWen Zhiwei <wenzhiwei@kylinos.cn>
parent 577b34f9
Loading
Loading
Loading
Loading
+9 −5
Original line number Diff line number Diff line
@@ -2970,13 +2970,17 @@ static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
{
	const struct bpf_link *link = filp->private_data;
	const struct bpf_prog *prog = link->prog;
	enum bpf_link_type type = link->type;
	char prog_tag[sizeof(prog->tag) * 2 + 1] = { };

	seq_printf(m,
		   "link_type:\t%s\n"
		   "link_id:\t%u\n",
		   bpf_link_type_strs[link->type],
		   link->id);
	if (type < ARRAY_SIZE(bpf_link_type_strs) && bpf_link_type_strs[type]) {
		seq_printf(m, "link_type:\t%s\n", bpf_link_type_strs[type]);
	} else {
		WARN_ONCE(1, "missing BPF_LINK_TYPE(...) for link type %u\n", type);
		seq_printf(m, "link_type:\t<%u>\n", type);
	}
	seq_printf(m, "link_id:\t%u\n", link->id);

	if (prog) {
		bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
		seq_printf(m,