]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
bpf: Check validity of link->type in bpf_link_show_fdinfo()
authorHou Tao <houtao1@huawei.com>
Thu, 24 Oct 2024 01:35:58 +0000 (09:35 +0800)
committerAndrii Nakryiko <andrii@kernel.org>
Thu, 24 Oct 2024 17:17:12 +0000 (10:17 -0700)
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: Hou Tao <houtao1@huawei.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20241024013558.1135167-3-houtao@huaweicloud.com
kernel/bpf/syscall.c

index 67179529080b3d5898ad36aba0146b201e1846fb..c5aa127ed4cc0149443222970a7ad67e2d86667c 100644 (file)
@@ -3069,13 +3069,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,