Skip to content

Commit

Permalink
lxc_init: fix cgroup parsing
Browse files Browse the repository at this point in the history
coverity: #1426132
coverity: #1426133

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Dec 14, 2017
1 parent f4bdebf commit 1933b53
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/lxc/Makefile.am
Expand Up @@ -304,7 +304,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
Expand Down
66 changes: 37 additions & 29 deletions src/lxc/lxc_init.c
Expand Up @@ -40,6 +40,7 @@
#include "error.h"
#include "initutils.h"
#include "log.h"
#include "parse.h"
#include "version.h"

/* option keys for long only options */
Expand Down Expand Up @@ -98,55 +99,64 @@ static struct arguments my_args = {
static void prevent_forking(void)
{
FILE *f;
char name[MAXPATHLEN], path[MAXPATHLEN];
int ret;
int fd = -1;
size_t len = 0;
char *line = NULL;
char path[MAXPATHLEN];

f = fopen("/proc/self/cgroup", "r");
if (!f) {
SYSERROR("Failed to open \"/proc/self/cgroup\"");
if (!f)
return;
}

while (!feof(f)) {
int fd, i;
while (getline(&line, &len, f) != -1) {
int ret;
char *p, *p2;

if (1 != fscanf(f, "%*d:%" QUOTEVAL(MAXPATHLEN) "s", name)) {
ERROR("Failed to parse \"/proc/self/cgroup\"");
goto out;
}
path[0] = 0;
p = strchr(line, ':');
if (!p)
continue;
p++;
p2 = strchr(p, ':');
if (!p2)
continue;
*p2 = '\0';

for (i = 0; i < sizeof(name); i++) {
if (name[i] == ':') {
name[i] = 0;
strncpy(path, name + i + 1, sizeof(path));
break;
}
}
/* This is a cgroup v2 entry. Skip it. */
if ((p2 - p) == 0)
continue;

if (strcmp(name, "pids"))
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(name, sizeof(name), "/sys/fs/cgroup/pids/%s/pids.max", path);
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 out;
goto on_error;
}

fd = open(name, O_WRONLY);
fd = open(path, O_WRONLY);
if (fd < 0) {
SYSERROR("Failed to open \"%s\"", name);
goto out;
SYSERROR("Failed to open \"%s\"", path);
goto on_error;
}

if (write(fd, "1", 1) != 1)
SYSERROR("Failed to write to \"%s\"", name);
SYSERROR("Failed to write to \"%s\"", path);

close(fd);
fd = -1;
break;
}

out:
on_error:
if (fd >= 0)
close(fd);
free(line);
fclose(f);
}

Expand Down Expand Up @@ -419,8 +429,6 @@ int main(int argc, char *argv[])
exit(ret);
}



static void print_usage(const struct option longopts[])

{
Expand Down

0 comments on commit 1933b53

Please sign in to comment.