Commit cb58aa84 authored by Kir Kolyshkin's avatar Kir Kolyshkin Committed by Pavel Emelyanov

prepare_pstree: fixup reading kernel pid_max

Two fixes (reported by coverity) and a minor nitpick:

1. Fix checking error from open_proc().

2. Fix buffer overflow. MAX_ULONG can be 20 characters long, so
ret = read() can return 20 and buf[ret] = 0 will overrun the buf.
Make a buf one character longer (an extra byte for \0) and pass
sizeof(buf) - 1 to read to fix it.

3. Call close() right after read().

This is a fixup to commit e68bded.

Reported by Coverity, CID 168505, 168504.

Cc: Laurent Dufour <ldufour@linux.vnet.ibm.com>
Signed-off-by: 's avatarKir Kolyshkin <kir@openvz.org>
Signed-off-by: 's avatarPavel Emelyanov <xemul@virtuozzo.com>
parent c44683c1
......@@ -887,17 +887,17 @@ int prepare_pstree(void)
int ret;
pid_t pid_max = 0, kpid_max = 0;
int fd;
char buf[20];
char buf[21];
fd = open_proc(PROC_GEN, PID_MAX_PATH);
if (fd != 1) {
ret = read(fd, buf, sizeof(buf));
if (fd >= 0) {
ret = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (ret > 0) {
buf[ret] = 0;
kpid_max = strtoul(buf, NULL, 10);
pr_debug("kernel pid_max=%d\n", kpid_max);
}
close (fd);
}
ret = read_pstree_image(&pid_max);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment