selftests/bpf: Fix freplace_link segfault in tailcalls prog test
authorTengda Wu <wutengda@huaweicloud.com>
Wed, 22 Jan 2025 02:28:38 +0000 (10:28 +0800)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 3 Feb 2025 11:33:51 +0000 (03:33 -0800)
commita63a631c9b5cb25a1c17dd2cb18c63df91e978b1
tree9e5906fa173a2f9a11e338fc7310a00cc09dbf8c
parent2014c95afecee3e76ca4a56956a936e23283f05b
selftests/bpf: Fix freplace_link segfault in tailcalls prog test

There are two bpf_link__destroy(freplace_link) calls in
test_tailcall_bpf2bpf_freplace(). After the first bpf_link__destroy()
is called, if the following bpf_map_{update,delete}_elem() throws an
exception, it will jump to the "out" label and call bpf_link__destroy()
again, causing double free and eventually leading to a segfault.

Fix it by directly resetting freplace_link to NULL after the first
bpf_link__destroy() call.

Fixes: 021611d33e78 ("selftests/bpf: Add test to verify tailcall and freplace restrictions")
Signed-off-by: Tengda Wu <wutengda@huaweicloud.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Reviewed-by: Leon Hwang <leon.hwang@linux.dev>
Link: https://lore.kernel.org/bpf/20250122022838.1079157-1-wutengda@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/tailcalls.c