Skip to content

Commit 5e8d7dc

Browse files
committed
tun: don't parse buffers that have not been filled with data
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 <[email protected]>
1 parent 4c9d23d commit 5e8d7dc

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

criu/net.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,18 @@ int read_ns_sys_file(char *path, char *buf, int len)
111111
}
112112

113113
rlen = read(fd, buf, len);
114+
if (rlen == -1)
115+
pr_perror("Can't read ns' %s", path);
114116
close(fd);
115117

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

121-
if (rlen > 0)
122-
buf[rlen - 1] = '\0';
124+
if (rlen >= 0)
125+
buf[rlen] = '\0';
123126

124127
return rlen;
125128
}

criu/tun.c

+7-8
Original file line numberDiff line numberDiff line change
@@ -455,27 +455,26 @@ int dump_tun_link(NetDeviceEntry *nde, struct cr_imgset *fds, struct nlattr **in
455455
TunLinkEntry tle = TUN_LINK_ENTRY__INIT;
456456
char spath[64];
457457
char buf[64];
458-
int ret = 0;
459458
struct tun_link *tl;
460459

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

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

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

473-
if (ret < 0)
474-
return ret;
475-
476475
tl = get_tun_link_fd(nde->name, nde->peer_nsid, tle.flags);
477476
if (!tl)
478-
return ret;
477+
return -1;
479478

480479
tle.vnethdr = tl->dmp.vnethdr;
481480
tle.sndbuf = tl->dmp.sndbuf;

0 commit comments

Comments
 (0)