Skip to content

Commit

Permalink
tun: don't parse buffers that have not been filled with data
Browse files Browse the repository at this point in the history
read_ns_sys_file() can return an error, but we are trying to parse a
buffer before checking a return code.

CID 417395 (#3 of 3): String not null terminated (STRING_NULL)
2. string_null: Passing unterminated string buf to strtol, which expects
   a null-terminated string.

Signed-off-by: Andrei Vagin <avagin@gmail.com>
  • Loading branch information
avagin committed Oct 8, 2023
1 parent 24e2492 commit 4e5247a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
7 changes: 5 additions & 2 deletions criu/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,18 @@ int read_ns_sys_file(char *path, char *buf, int len)
}

rlen = read(fd, buf, len);
if (rlen == -1)
pr_perror("Can't read ns' %s", path);
close(fd);

if (rlen == len) {
buf[0] = '\0';
pr_err("Too small buffer to read ns sys file %s\n", path);
return -1;
}

if (rlen > 0)
buf[rlen - 1] = '\0';
if (rlen >= 0)
buf[rlen] = '\0';

return rlen;
}
Expand Down
15 changes: 7 additions & 8 deletions criu/tun.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,27 +455,26 @@ int dump_tun_link(NetDeviceEntry *nde, struct cr_imgset *fds, struct nlattr **in
TunLinkEntry tle = TUN_LINK_ENTRY__INIT;
char spath[64];
char buf[64];
int ret = 0;
struct tun_link *tl;

sprintf(spath, "class/net/%s/tun_flags", nde->name);
ret |= read_ns_sys_file(spath, buf, sizeof(buf));
if (read_ns_sys_file(spath, buf, sizeof(buf)) < 0)
return -1;
tle.flags = strtol(buf, NULL, 0);

sprintf(spath, "class/net/%s/owner", nde->name);
ret |= read_ns_sys_file(spath, buf, sizeof(buf));
if (read_ns_sys_file(spath, buf, sizeof(buf)) < 0)
return -1;
tle.owner = strtol(buf, NULL, 10);

sprintf(spath, "class/net/%s/group", nde->name);
ret |= read_ns_sys_file(spath, buf, sizeof(buf));
if (read_ns_sys_file(spath, buf, sizeof(buf)) < 0)
return -1;
tle.group = strtol(buf, NULL, 10);

if (ret < 0)
return ret;

tl = get_tun_link_fd(nde->name, nde->peer_nsid, tle.flags);
if (!tl)
return ret;
return -1;

tle.vnethdr = tl->dmp.vnethdr;
tle.sndbuf = tl->dmp.sndbuf;
Expand Down

0 comments on commit 4e5247a

Please sign in to comment.