selftests/bpf: Test the release of map btf
authorHou Tao <houtao1@huawei.com>
Sat, 16 Dec 2023 03:55:10 +0000 (11:55 +0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Mon, 18 Dec 2023 17:15:49 +0000 (18:15 +0100)
commite58aac1a9a179fa9dab3025ef955cdb548c439f2
treeb65884e81cb7e69ee23755fde1b1c9a34ac2582e
parent0c970ed2f87c058fe3ddeb4d7d8f64f72cf41d7a
selftests/bpf: Test the release of map btf

When there is bpf_list_head or bpf_rb_root field in map value, the free
of map btf and the free of map value may run concurrently and there may
be use-after-free problem, so add two test cases to demonstrate it. And
the use-after-free problem can been easily reproduced by using bpf_next
tree and a KASAN-enabled kernel.

The first test case tests the racing between the free of map btf and the
free of array map. It constructs the racing by releasing the array map in
the end after other ref-counter of map btf has been released. To delay
the free of array map and make it be invoked after btf_free_rcu() is
invoked, it stresses system_unbound_wq by closing multiple percpu array
maps before it closes the array map.

The second case tests the racing between the free of map btf and the
free of inner map. Beside using the similar method as the first one
does, it uses bpf_map_delete_elem() to delete the inner map and to defer
the release of inner map after one RCU grace period.

The reason for using two skeletons is to prevent the release of outer
map and inner map in map_in_map_btf.c interfering the release of bpf
map in normal_map_btf.c.

Signed-off-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/bpf/20231216035510.4030605-1-houtao@huaweicloud.com
tools/testing/selftests/bpf/prog_tests/map_btf.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/map_in_map_btf.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/normal_map_btf.c [new file with mode: 0644]