Skip to content

Commit cb58aa8

Browse files
kolyshkinxemul
authored andcommitted
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 <[email protected]> Signed-off-by: Kir Kolyshkin <[email protected]> Signed-off-by: Pavel Emelyanov <[email protected]>
1 parent c44683c commit cb58aa8

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

criu/pstree.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -887,17 +887,17 @@ int prepare_pstree(void)
887887
int ret;
888888
pid_t pid_max = 0, kpid_max = 0;
889889
int fd;
890-
char buf[20];
890+
char buf[21];
891891

892892
fd = open_proc(PROC_GEN, PID_MAX_PATH);
893-
if (fd != 1) {
894-
ret = read(fd, buf, sizeof(buf));
893+
if (fd >= 0) {
894+
ret = read(fd, buf, sizeof(buf) - 1);
895+
close(fd);
895896
if (ret > 0) {
896897
buf[ret] = 0;
897898
kpid_max = strtoul(buf, NULL, 10);
898899
pr_debug("kernel pid_max=%d\n", kpid_max);
899900
}
900-
close (fd);
901901
}
902902

903903
ret = read_pstree_image(&pid_max);

0 commit comments

Comments
 (0)