bpf: Pin the start cgroup in cgroup_iter_seq_init()
authorHou Tao <houtao1@huawei.com>
Mon, 21 Nov 2022 07:34:38 +0000 (15:34 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 31 Dec 2022 12:32:13 +0000 (13:32 +0100)
commit3751ba4aec0b460a94aaa692013b9dd1a0cb14f7
tree2a617fb279ea971cc3b0f6bf4c6d5e9fbb7e890b
parentb7c906977301cbfcead885ac74a1879f3c1e4a34
bpf: Pin the start cgroup in cgroup_iter_seq_init()

[ Upstream commit 1a5160d4d8fe63ba4964cfff4a85831b6af75f2d ]

bpf_iter_attach_cgroup() has already acquired an extra reference for the
start cgroup, but the reference may be released if the iterator link fd
is closed after the creation of iterator fd, and it may lead to
user-after-free problem when reading the iterator fd.

An alternative fix is pinning iterator link when opening iterator,
but it will make iterator link being still visible after the close of
iterator link fd and the behavior is different with other link types, so
just fixing it by acquiring another reference for the start cgroup.

Fixes: d4ccaf58a847 ("bpf: Introduce cgroup iter")
Signed-off-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20221121073440.1828292-2-houtao@huaweicloud.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
kernel/bpf/cgroup_iter.c