Re: [PATCH bpf v2 11/15] selftests/bpf: Free bpf_object in test_sysctl
From: Eduard Zingerman
Date: Thu Feb 19 2026 - 19:09:16 EST
On Tue, 2026-02-17 at 16:30 -0800, Ihor Solodrai wrote:
> ASAN reported a resource leak due to the bpf_object not being tracked
> in test_sysctl. Add obj field to struct sysctl_test to properly clean
> up bpf_object if a program was loaded from a file.
>
> Signed-off-by: Ihor Solodrai <ihor.solodrai@xxxxxxxxx>
> ---
Acked-by: Eduard Zingerman <eddyz87@xxxxxxxxx>
> @@ -1471,14 +1472,16 @@ static int load_sysctl_prog_file(struct sysctl_test *test)
> return -1;
> }
>
> + test->obj = obj;
> return prog_fd;
> }
>
> static int load_sysctl_prog(struct sysctl_test *test, const char *sysctl_path)
> {
> - return test->prog_file
> - ? load_sysctl_prog_file(test)
> - : load_sysctl_prog_insns(test, sysctl_path);
> + if (test->prog_file)
> + return load_sysctl_prog_file(test);
> + test->obj = NULL;
Nit: is this necessary? The way `tests` is defined ->obj will be NULL anyway.
Looks a bit asymmetrical.
> + return load_sysctl_prog_insns(test, sysctl_path);
> }
>
> static int access_sysctl(const char *sysctl_path,
> @@ -1573,7 +1576,10 @@ static int run_test_case(int cgfd, struct sysctl_test *test)
> /* Detaching w/o checking return code: best effort attempt. */
> if (progfd != -1)
> bpf_prog_detach(cgfd, atype);
> - close(progfd);
> + if (test->obj)
> + bpf_object__close(test->obj);
> + else if (progfd != -1)
> + close(progfd);
Nit: close(-1) works fine, as well as bpf_object__close(NULL),
so there is no need for 'if' statements here.
> printf("[%s]\n", err ? "FAIL" : "PASS");
> return err;
> }