Skip to content

Commit 8421d4c

Browse files
Hou Taoanakryiko
authored andcommitted
bpf: Check validity of link->type in bpf_link_show_fdinfo()
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 <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent c2f8030 commit 8421d4c

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

kernel/bpf/syscall.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,13 +3069,17 @@ static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
30693069
{
30703070
const struct bpf_link *link = filp->private_data;
30713071
const struct bpf_prog *prog = link->prog;
3072+
enum bpf_link_type type = link->type;
30723073
char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
30733074

3074-
seq_printf(m,
3075-
"link_type:\t%s\n"
3076-
"link_id:\t%u\n",
3077-
bpf_link_type_strs[link->type],
3078-
link->id);
3075+
if (type < ARRAY_SIZE(bpf_link_type_strs) && bpf_link_type_strs[type]) {
3076+
seq_printf(m, "link_type:\t%s\n", bpf_link_type_strs[type]);
3077+
} else {
3078+
WARN_ONCE(1, "missing BPF_LINK_TYPE(...) for link type %u\n", type);
3079+
seq_printf(m, "link_type:\t<%u>\n", type);
3080+
}
3081+
seq_printf(m, "link_id:\t%u\n", link->id);
3082+
30793083
if (prog) {
30803084
bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
30813085
seq_printf(m,

0 commit comments

Comments
 (0)