From 38ef5bb7b4a7e8b191f4087c140a07a0779fa903 Mon Sep 17 00:00:00 2001 From: Andrea Claudi Date: Mon, 19 Apr 2021 15:37:25 +0200 Subject: [PATCH] ip: netns: fix missing netns close on some error paths In functions netns_pids() and netns_identify_pid(), the netns file is not closed on some error paths. Fix this using a conditional close and a single return point on both functions. Fixes: 44b563269ea1 ("ip-nexthop: support flush by id") Signed-off-by: Andrea Claudi Signed-off-by: Stephen Hemminger Conflict: The function reconstructs Reference: https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit?id=38ef5bb7b4a7e8b191f4087c140a07a0779fa903 --- ip/ipnetns.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/ip/ipnetns.c b/ip/ipnetns.c index cf9a471..4b88810 100644 --- a/ip/ipnetns.c +++ b/ip/ipnetns.c @@ -627,18 +627,18 @@ static int netns_pids(int argc, char **argv) { const char *name; char net_path[PATH_MAX]; - int netns; + int netns = -1, ret = -1; struct stat netst; DIR *dir; struct dirent *entry; if (argc < 1) { fprintf(stderr, "No netns name specified\n"); - return -1; + goto out; } if (argc > 1) { fprintf(stderr, "extra arguments specified\n"); - return -1; + goto out; } name = argv[0]; @@ -647,17 +647,17 @@ static int netns_pids(int argc, char **argv) if (netns < 0) { fprintf(stderr, "Cannot open network namespace: %s\n", strerror(errno)); - return -1; + goto out; } if (fstat(netns, &netst) < 0) { fprintf(stderr, "Stat of netns failed: %s\n", strerror(errno)); - return -1; + goto out; } dir = opendir(get_proc_string()); if (!dir) { fprintf(stderr, "Open of %s failed: %s\n", get_proc_string(), strerror(errno)); - return -1; + goto out; } while ((entry = readdir(dir))) { char pid_net_path[PATH_MAX]; @@ -673,15 +673,18 @@ static int netns_pids(int argc, char **argv) printf("%s\n", entry->d_name); } } + ret = 0; closedir(dir); - return 0; - +out: + if (netns >= 0) + close(netns); + return ret; } int netns_identify_pid(const char *pidstr, char *name, int len) { char net_path[PATH_MAX]; - int netns; + int netns = -1, ret = -1; struct stat netst; DIR *dir; struct dirent *entry; @@ -693,22 +696,24 @@ int netns_identify_pid(const char *pidstr, char *name, int len) if (netns < 0) { fprintf(stderr, "Cannot open network namespace: %s\n", strerror(errno)); - return -1; + goto out; } if (fstat(netns, &netst) < 0) { fprintf(stderr, "Stat of netns failed: %s\n", strerror(errno)); - return -1; + goto out; } dir = opendir(NETNS_RUN_DIR); if (!dir) { /* Succeed treat a missing directory as an empty directory */ - if (errno == ENOENT) - return 0; + if (errno == ENOENT) { + ret = 0; + goto out; + } fprintf(stderr, "Failed to open directory %s:%s\n", NETNS_RUN_DIR, strerror(errno)); - return -1; + goto out; } while ((entry = readdir(dir))) { @@ -731,9 +736,12 @@ int netns_identify_pid(const char *pidstr, char *name, int len) strlcpy(name, entry->d_name, len); } } + ret = 0; closedir(dir); - return 0; - +out: + if (netns >= 0) + close(netns); + return ret; } static int netns_identify(int argc, char **argv) -- 2.23.0