[CRIU] [PATCH] prepare_pstree: fixup reading kernel pid_max

Kir Kolyshkin kir at openvz.org
Wed Aug 3 18:04:01 PDT 2016


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 at linux.vnet.ibm.com>
Signed-off-by: Kir Kolyshkin <kir at openvz.org>
---
 criu/pstree.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/criu/pstree.c b/criu/pstree.c
index af89dbb..afc64dc 100644
--- a/criu/pstree.c
+++ b/criu/pstree.c
@@ -987,17 +987,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);
-- 
2.7.4



More information about the CRIU mailing list