]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
lib/fs: Fix single return points for get_cgroup2_*
authorAndrea Claudi <aclaudi@redhat.com>
Mon, 22 Feb 2021 18:14:32 +0000 (19:14 +0100)
committerStephen Hemminger <stephen@networkplumber.org>
Tue, 23 Feb 2021 02:20:44 +0000 (18:20 -0800)
Functions get_cgroup2_id() and get_cgroup2_path() may call close() with
a negative argument.
Avoid that making the calls conditional on the file descriptors.

get_cgroup2_path() may also return NULL leaking a file descriptor.
Ensure this does not happen using a single return point.

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>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
lib/fs.c

index 2ae506ecc501b53da18dfc66734472eaacf3fbd2..ee0b130b6c33cbb488adb5e3b19be57f72737ab6 100644 (file)
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -157,7 +157,8 @@ __u64 get_cgroup2_id(const char *path)
        memcpy(cg_id.bytes, fhp->f_handle, sizeof(__u64));
 
 out:
-       close(mnt_fd);
+       if (mnt_fd >= 0)
+               close(mnt_fd);
        free(mnt);
 
        return cg_id.id;
@@ -179,16 +180,16 @@ char *get_cgroup2_path(__u64 id, bool full)
        char *path = NULL;
        char fd_path[64];
        int link_len;
-       char *mnt;
+       char *mnt = NULL;
 
        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) {
@@ -225,8 +226,10 @@ char *get_cgroup2_path(__u64 id, bool full)
                        "Failed to allocate memory for cgroup2 path\n");
 
 out:
-       close(fd);
-       close(mnt_fd);
+       if (fd >= 0)
+               close(fd);
+       if (mnt_fd >= 0)
+               close(mnt_fd);
        free(mnt);
 
        return path;