bpf: Fix a potential use-after-free in bpf_link_free()
authorCong Wang <cong.wang@bytedance.com>
Sun, 2 Jun 2024 18:27:03 +0000 (11:27 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Mon, 3 Jun 2024 16:16:19 +0000 (18:16 +0200)
commit2884dc7d08d98a89d8d65121524bb7533183a63a
tree2ba0a318481cbdc9dd240af512fc8563635c3354
parent2317dc2c22cc353b699c7d1db47b2fe91f54055c
bpf: Fix a potential use-after-free in bpf_link_free()

After commit 1a80dbcb2dba, bpf_link can be freed by
link->ops->dealloc_deferred, but the code still tests and uses
link->ops->dealloc afterward, which leads to a use-after-free as
reported by syzbot. Actually, one of them should be sufficient, so
just call one of them instead of both. Also add a WARN_ON() in case
of any problematic implementation.

Fixes: 1a80dbcb2dba ("bpf: support deferring bpf_link dealloc to after RCU grace period")
Reported-by: syzbot+1989ee16d94720836244@syzkaller.appspotmail.com
Signed-off-by: Cong Wang <cong.wang@bytedance.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/bpf/20240602182703.207276-1-xiyou.wangcong@gmail.com
kernel/bpf/syscall.c