]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
cgget: Fix segfault when no flags provided 40/head
authorTom Hromatka <tom.hromatka@oracle.com>
Wed, 14 Apr 2021 16:02:44 +0000 (10:02 -0600)
committerTom Hromatka <tom.hromatka@oracle.com>
Mon, 19 Apr 2021 14:17:33 +0000 (08:17 -0600)
Fix a segmentation fault when cgget is invoked in the
following form:
cgget CroupName

parse_opt_args() was erroneously expecting a previously
populated struct cgroup in this case, and we were indexing
out of bounds of the cg_list.

Fixes: 1b7d606fa12a ("cgget: Major refactor")
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
src/tools/cgget.c

index 08c2286ca098018e8911cf9387c5d9f9704f1dae..769ef9270006d411e65f260474f2aaff5601f9f6 100644 (file)
@@ -336,9 +336,25 @@ static int parse_opt_args(int argc, char *argv[], struct cgroup **cg_list[],
        }
 
        while (argv[optind] != NULL) {
-               cg = (*cg_list)[(*cg_list_len) - 1];
+               if ((*cg_list_len) > 0)
+                       cg = (*cg_list)[(*cg_list_len) - 1];
+               else
+                       cg = NULL;
+
+               if ((*cg_list_len) == 0) {
+                       /* The user didn't provide a '-r' or '-g' flag.
+                        * The parse_a_flag() function can be reused here
+                        * because we both have the same use case - gather
+                        * all the data about this particular cgroup.
+                        */
+                       ret = parse_a_flag(cg_list, cg_list_len);
+                       if (ret)
+                               goto out;
 
-               if (strlen(cg->name) == 0) {
+                       strncpy((*cg_list)[(*cg_list_len) - 1]->name,
+                               argv[optind],
+                               sizeof((*cg_list)[(*cg_list_len) - 1]->name) - 1);
+               } else if (cg != NULL && strlen(cg->name) == 0) {
                        /* this cgroup was created based upon control/value
                         * pairs or with a -g <controller> option.  we'll
                         * populate it with the parameter provided by the