]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
lxc_init: fix cgroup parsing
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 14 Dec 2017 22:00:04 +0000 (23:00 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Sun, 17 Dec 2017 15:56:48 +0000 (16:56 +0100)
coverity: #1426132
coverity: #1426133

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/Makefile.am
src/lxc/lxc_init.c

index f5a6b42d86b534727b84da112190a903a25a233e..9cc5aff05dfd9294fa4a656519f2a52a0e95f404 100644 (file)
@@ -289,7 +289,7 @@ endif
 if HAVE_STATIC_LIBCAP
 sbin_PROGRAMS += init.lxc.static
 
-init_lxc_static_SOURCES = lxc_init.c error.c log.c initutils.c caps.c
+init_lxc_static_SOURCES = lxc_init.c error.c log.c initutils.c caps.c parse.c
 
 if !HAVE_GETLINE
 if HAVE_FGETLN
index 267f281e45e1c7b10f8a764ac98d06f7b101e2c0..aa77d44746d64f0aecefb931fa3a8c5f43db1d4d 100644 (file)
@@ -41,6 +41,7 @@
 #include "initutils.h"
 #include "log.h"
 #include "utils.h"
+#include "parse.h"
 #include "version.h"
 
 /* option keys for long only options */
@@ -96,46 +97,64 @@ static struct arguments my_args = {
 static void prevent_forking(void)
 {
        FILE *f;
-       char name[PATH_MAX], path[PATH_MAX];
-       int ret;
+       int fd = -1;
+       size_t len = 0;
+       char *line = NULL;
+       char path[MAXPATHLEN];
 
        f = fopen("/proc/self/cgroup", "r");
-       if (!f) {
-               SYSERROR("opening /proc/self/cgroup");
+       if (!f)
                return;
-       }
 
-       while (!feof(f)) {
-               int fd;
+       while (getline(&line, &len, f) != -1) {
+               int ret;
+               char *p, *p2;
 
-               if (2 != fscanf(f, "%*d:%[^:]:%s", name, path)) {
-                       ERROR("didn't scan the right number of things");
-                       goto out;
-               }
+               p = strchr(line, ':');
+               if (!p)
+                       continue;
+               p++;
+               p2 = strchr(p, ':');
+               if (!p2)
+                       continue;
+               *p2 = '\0';
 
-               if (strcmp(name, "pids"))
+               /* This is a cgroup v2 entry. Skip it. */
+               if ((p2 - p) == 0)
                        continue;
 
-               ret = snprintf(name, sizeof(name), "/sys/fs/cgroup/pids/%s/pids.max", path);
-               if (ret < 0 || ret >= sizeof(path)) {
-                       ERROR("failed snprintf");
-                       goto out;
+               if (strcmp(p, "pids") != 0)
+                       continue;
+               p2++;
+
+               p2 += lxc_char_left_gc(p2, strlen(p2));
+               p2[lxc_char_right_gc(p2, strlen(p2))] = '\0';
+
+               ret = snprintf(path, sizeof(path),
+                              "/sys/fs/cgroup/pids/%s/pids.max", p2);
+               if (ret < 0 || (size_t)ret >= sizeof(path)) {
+                       ERROR("Failed to create string");
+                       goto on_error;
                }
 
-               fd = open(name, O_WRONLY);
+               fd = open(path, O_WRONLY);
                if (fd < 0) {
-                       SYSERROR("open");
-                       goto out;
+                       SYSERROR("Failed to open \"%s\"", path);
+                       goto on_error;
                }
 
                if (write(fd, "1", 1) != 1)
-                       SYSERROR("write");
+                       SYSERROR("Failed to write to \"%s\"", path);
 
                close(fd);
+               fd = -1;
                break;
        }
 
-out:
+on_error:
+       if (fd >= 0)
+               close(fd);
+       free(line);
        fclose(f);
 }
 
@@ -146,7 +165,7 @@ static void kill_children(pid_t pid)
        int ret;
 
        ret = snprintf(path, sizeof(path), "/proc/%d/task/%d/children", pid, pid);
-       if (ret < 0 || ret >= sizeof(path)) {
+       if (ret < 0 || (size_t)ret >= sizeof(path)) {
                ERROR("failed snprintf");
                return;
        }
@@ -403,8 +422,6 @@ out:
        exit(ret);
 }
 
-
-
 static void print_usage(const struct option longopts[])
 
 {