From: Andrea Claudi <aclaudi@redhat.com>
To: netdev@vger.kernel.org
Cc: stephen@networkplumber.org, dsahern@gmail.com
Subject: [PATCH iproute2 2/2] lib/fs: Fix single return points for get_cgroup2_*
Date: Fri, 18 Dec 2020 20:09:23 +0100 [thread overview]
Message-ID: <9b07b59c4c422b29d6c8297f7f7ec0f2dcc7fb3f.1608315719.git.aclaudi@redhat.com> (raw)
In-Reply-To: <cover.1608315719.git.aclaudi@redhat.com>
Functions get_cgroup2_id() and get_cgroup2_path() uncorrectly performs
cleanup on the single return point. Both of them may get to use close()
with a negative argument, if open() fails.
Fix this adding proper labels and gotos to make sure we clean up only
resources we are effectively used before.
Fixes: d5e6ee0dac64 ("ss: introduce cgroup2 cache and helper functions")
Fixes: 8f1cd119b377 ("lib: fix checking of returned file handle size for cgroup")
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
---
lib/fs.c | 27 +++++++++++++++++----------
1 file changed, 17 insertions(+), 10 deletions(-)
diff --git a/lib/fs.c b/lib/fs.c
index 2ae506ec..77414e99 100644
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -139,27 +139,31 @@ __u64 get_cgroup2_id(const char *path)
mnt_fd = open(mnt, O_RDONLY);
if (mnt_fd < 0) {
fprintf(stderr, "Failed to open cgroup2 mount\n");
- goto out;
+ goto out_clean_mnt;
}
fhp->handle_bytes = sizeof(__u64);
if (name_to_handle_at(mnt_fd, path, fhp, &mnt_id, 0) < 0) {
fprintf(stderr, "Failed to get cgroup2 ID: %s\n",
strerror(errno));
- goto out;
+ goto out_clean_all;
}
}
if (fhp->handle_bytes != sizeof(__u64)) {
fprintf(stderr, "Invalid size of cgroup2 ID\n");
- goto out;
+ if (mnt_fd < 0)
+ goto out;
+ goto out_clean_all;
}
memcpy(cg_id.bytes, fhp->f_handle, sizeof(__u64));
-out:
+out_clean_all:
close(mnt_fd);
+out_clean_mnt:
free(mnt);
+out:
return cg_id.id;
}
@@ -183,17 +187,17 @@ char *get_cgroup2_path(__u64 id, bool full)
if (!id) {
fprintf(stderr, "Invalid cgroup2 ID\n");
- return NULL;
+ goto out;
}
mnt = find_cgroup2_mount(false);
if (!mnt)
- return NULL;
+ goto out;
mnt_fd = open(mnt, O_RDONLY);
if (mnt_fd < 0) {
fprintf(stderr, "Failed to open cgroup2 mount\n");
- goto out;
+ goto out_clean_mnt;
}
fhp->handle_bytes = sizeof(__u64);
@@ -203,7 +207,7 @@ char *get_cgroup2_path(__u64 id, bool full)
fd = open_by_handle_at(mnt_fd, fhp, 0);
if (fd < 0) {
fprintf(stderr, "Failed to open cgroup2 by ID\n");
- goto out;
+ goto out_clean_mntfd;
}
snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d", fd);
@@ -212,7 +216,7 @@ char *get_cgroup2_path(__u64 id, bool full)
fprintf(stderr,
"Failed to read value of symbolic link %s\n",
fd_path);
- goto out;
+ goto out_clean_all;
}
link_buf[link_len] = '\0';
@@ -224,11 +228,14 @@ char *get_cgroup2_path(__u64 id, bool full)
fprintf(stderr,
"Failed to allocate memory for cgroup2 path\n");
-out:
+out_clean_all:
close(fd);
+out_clean_mntfd:
close(mnt_fd);
+out_clean_mnt:
free(mnt);
+out:
return path;
}
--
2.29.2
next prev parent reply other threads:[~2020-12-18 19:16 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-18 19:09 [PATCH iproute2 0/2] Some fixes to lib/fs.c Andrea Claudi
2020-12-18 19:09 ` [PATCH iproute2 1/2] lib/fs: avoid double call to mkdir on make_path() Andrea Claudi
2020-12-18 22:59 ` Phil Sutter
2020-12-18 19:09 ` Andrea Claudi [this message]
2020-12-18 23:08 ` [PATCH iproute2 2/2] lib/fs: Fix single return points for get_cgroup2_* Phil Sutter
2020-12-18 23:29 ` Andrea Claudi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9b07b59c4c422b29d6c8297f7f7ec0f2dcc7fb3f.1608315719.git.aclaudi@redhat.com \
--to=aclaudi@redhat.com \
--cc=dsahern@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=stephen@networkplumber.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).