From cb58aa84d6bfb3e6e0d1db202942ec136192b6ec Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 4 Aug 2016 04:04:00 +0300 Subject: [PATCH] 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 Signed-off-by: Kir Kolyshkin Signed-off-by: Pavel Emelyanov --- criu/pstree.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/criu/pstree.c b/criu/pstree.c index 20a7cd059..d23b1f18c 100644 --- a/criu/pstree.c +++ b/criu/pstree.c @@ -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);