[CRIU] [PATCH] util: Do not leak dup-ed file descriptor

Pavel Emelyanov xemul at virtuozzo.com
Mon Dec 19 02:14:04 PST 2016


When going daemon criu services ask cr_daemon() to keep
some descriptors open and the routine dup2-s them. The
problem is that the original descriptor is left where it
was and is leaked.

Signed-off-by: Pavel Emelyanov <xemul at virtuozzo.com>
---
 criu/util.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/criu/util.c b/criu/util.c
index 1288680..2534a70 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -713,8 +713,15 @@ int cr_daemon(int nochdir, int noclose, int *keep_fd, int close_fd)
 		if (close_fd != -1)
 			close(close_fd);
 
-		if (*keep_fd != -1)
-			*keep_fd = dup2(*keep_fd, 3);
+		if ((*keep_fd != -1) && (*keep_fd != 3)) {
+			fd = dup2(*keep_fd, 3);
+			if (fd < 0) {
+				pr_perror("Dup2 failed");
+				return -1;
+			}
+			close(*keep_fd);
+			*keep_fd = fd;
+		}
 
 		fd = open("/dev/null", O_RDWR);
 		if (fd < 0) {
-- 
2.5.0


More information about the CRIU mailing list