Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Fix a fd leak in error paths in open_netns

As Martin mentioned in review comment, there is an existing bug that
orig_netns_fd will be leaked in the later "goto fail;" case after
open("/proc/self/ns/net") in open_netns() in network_helpers.c. This
patch adds "close(token->orig_netns_fd);" before "free(token);" to
fix it.

Fixes: a30338840fa5 ("selftests/bpf: Move open_netns() and close_netns() into network_helpers.c")
Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
Link: https://lore.kernel.org/r/a104040b47c3c34c67f3f125cdfdde244a870d3c.1713868264.git.tanggeliang@kylinos.cn
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

authored by

Geliang Tang and committed by
Martin KaFai Lau
151f7442 55d30cc9

+2
+2
tools/testing/selftests/bpf/network_helpers.c
··· 478 478 479 479 return token; 480 480 fail: 481 + if (token->orig_netns_fd != -1) 482 + close(token->orig_netns_fd); 481 483 free(token); 482 484 return NULL; 483 485 }