Skip to content

Commit

Permalink
selftests/bpf: Fix a fd leak in error paths in open_netns
Browse files Browse the repository at this point in the history
[ Upstream commit 151f7442436658ee84076681d8f52e987fe147ea ]

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: a303388 ("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>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
Geliang Tang authored and gregkh committed Jun 12, 2024
1 parent e19681a commit 51929a8
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tools/testing/selftests/bpf/network_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ struct nstoken *open_netns(const char *name)

return token;
fail:
if (token->orig_netns_fd != -1)
close(token->orig_netns_fd);
free(token);
return NULL;
}
Expand Down

0 comments on commit 51929a8

Please sign in to comment.