Commit 71dca295 authored by Ihor Solodrai's avatar Ihor Solodrai Committed by Alexei Starovoitov
Browse files

selftests/bpf: Fix resource leaks caused by missing cleanups



ASAN reported a number of resource leaks:
  - Add missing  *__destroy(skel) calls
  - Replace bpf_link__detach() with bpf_link__destroy() where appropriate
  - cgrp_local_storage: Add bpf_link__destroy() when bpf_iter_create fails
  - dynptr: Add missing bpf_object__close()

Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
Signed-off-by: default avatarIhor Solodrai <ihor.solodrai@linux.dev>
Link: https://lore.kernel.org/r/20260223190736.649171-16-ihor.solodrai@linux.dev


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 68b8bea1
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -202,7 +202,7 @@ static void test_cgroup_iter_sleepable(int cgroup_fd, __u64 cgroup_id)

	iter_fd = bpf_iter_create(bpf_link__fd(link));
	if (!ASSERT_GE(iter_fd, 0, "iter_create"))
		goto out;
		goto out_link;

	/* trigger the program run */
	(void)read(iter_fd, buf, sizeof(buf));
@@ -210,6 +210,8 @@ static void test_cgroup_iter_sleepable(int cgroup_fd, __u64 cgroup_id)
	ASSERT_EQ(skel->bss->cgroup_id, cgroup_id, "cgroup_id");

	close(iter_fd);
out_link:
	bpf_link__destroy(link);
out:
	cgrp_ls_sleepable__destroy(skel);
}
+4 −1
Original line number Diff line number Diff line
@@ -137,11 +137,14 @@ static void verify_success(const char *prog_name, enum test_setup_type setup_typ
		);

		link = bpf_program__attach(prog);
		if (!ASSERT_OK_PTR(link, "bpf_program__attach"))
		if (!ASSERT_OK_PTR(link, "bpf_program__attach")) {
			bpf_object__close(obj);
			goto cleanup;
		}

		err = bpf_prog_test_run_opts(aux_prog_fd, &topts);
		bpf_link__destroy(link);
		bpf_object__close(obj);

		if (!ASSERT_OK(err, "test_run"))
			goto cleanup;
+14 −14
Original line number Diff line number Diff line
@@ -204,7 +204,7 @@ static void test_skmsg_helpers_with_link(enum bpf_map_type map_type)
	/* Fail since bpf_link for the same prog type has been created. */
	link2 = bpf_program__attach_sockmap(prog_clone, map);
	if (!ASSERT_ERR_PTR(link2, "bpf_program__attach_sockmap")) {
		bpf_link__detach(link2);
		bpf_link__destroy(link2);
		goto out;
	}

@@ -230,7 +230,7 @@ static void test_skmsg_helpers_with_link(enum bpf_map_type map_type)
	if (!ASSERT_OK(err, "bpf_link_update"))
		goto out;
out:
	bpf_link__detach(link);
	bpf_link__destroy(link);
	test_skmsg_load_helpers__destroy(skel);
}

@@ -417,7 +417,7 @@ static void test_sockmap_skb_verdict_attach_with_link(void)
	if (!ASSERT_OK_PTR(link, "bpf_program__attach_sockmap"))
		goto out;

	bpf_link__detach(link);
	bpf_link__destroy(link);

	err = bpf_prog_attach(bpf_program__fd(prog), map, BPF_SK_SKB_STREAM_VERDICT, 0);
	if (!ASSERT_OK(err, "bpf_prog_attach"))
@@ -426,7 +426,7 @@ static void test_sockmap_skb_verdict_attach_with_link(void)
	/* Fail since attaching with the same prog/map has been done. */
	link = bpf_program__attach_sockmap(prog, map);
	if (!ASSERT_ERR_PTR(link, "bpf_program__attach_sockmap"))
		bpf_link__detach(link);
		bpf_link__destroy(link);

	err = bpf_prog_detach2(bpf_program__fd(prog), map, BPF_SK_SKB_STREAM_VERDICT);
	if (!ASSERT_OK(err, "bpf_prog_detach2"))
@@ -747,13 +747,13 @@ static void test_sockmap_skb_verdict_peek_with_link(void)
	test_sockmap_skb_verdict_peek_helper(map);
	ASSERT_EQ(pass->bss->clone_called, 1, "clone_called");
out:
	bpf_link__detach(link);
	bpf_link__destroy(link);
	test_sockmap_pass_prog__destroy(pass);
}

static void test_sockmap_unconnected_unix(void)
{
	int err, map, stream = 0, dgram = 0, zero = 0;
	int err, map, stream = -1, dgram = -1, zero = 0;
	struct test_sockmap_pass_prog *skel;

	skel = test_sockmap_pass_prog__open_and_load();
@@ -764,22 +764,22 @@ static void test_sockmap_unconnected_unix(void)

	stream = xsocket(AF_UNIX, SOCK_STREAM, 0);
	if (stream < 0)
		return;
		goto out;

	dgram = xsocket(AF_UNIX, SOCK_DGRAM, 0);
	if (dgram < 0) {
		close(stream);
		return;
	}
	if (dgram < 0)
		goto out;

	err = bpf_map_update_elem(map, &zero, &stream, BPF_ANY);
	ASSERT_ERR(err, "bpf_map_update_elem(stream)");
	if (!ASSERT_ERR(err, "bpf_map_update_elem(stream)"))
		goto out;

	err = bpf_map_update_elem(map, &zero, &dgram, BPF_ANY);
	ASSERT_OK(err, "bpf_map_update_elem(dgram)");

out:
	close(stream);
	close(dgram);
	test_sockmap_pass_prog__destroy(skel);
}

static void test_sockmap_many_socket(void)
@@ -1027,7 +1027,7 @@ static void test_sockmap_skb_verdict_vsock_poll(void)
	if (xrecv_nonblock(conn, &buf, 1, 0) != 1)
		FAIL("xrecv_nonblock");
detach:
	bpf_link__detach(link);
	bpf_link__destroy(link);
close:
	xclose(conn);
	xclose(peer);
+1 −1
Original line number Diff line number Diff line
@@ -899,7 +899,7 @@ static void test_msg_redir_to_listening_with_link(struct test_sockmap_listen *sk

	redir_to_listening(family, sotype, sock_map, verdict_map, REDIR_EGRESS);

	bpf_link__detach(link);
	bpf_link__destroy(link);
}

static void redir_partial(int family, int sotype, int sock_map, int parser_map)
+1 −3
Original line number Diff line number Diff line
@@ -54,9 +54,7 @@ static void test_private_stack_fail(void)
	}

	err = struct_ops_private_stack_fail__load(skel);
	if (!ASSERT_ERR(err, "struct_ops_private_stack_fail__load"))
		goto cleanup;
	return;
	ASSERT_ERR(err, "struct_ops_private_stack_fail__load");

cleanup:
	struct_ops_private_stack_fail__destroy(skel);
Loading