[CRIU] [PATCH 3/5] restore_fs(): don't leak fds on error

Kir Kolyshkin kir at openvz.org
Tue May 5 15:31:10 PDT 2015


First, if we can't open dd_cwd, we leak opened dd_root.

Second, if chroot(dd_root) fails, we leak opened dd_cwd.

Instead of adding more close() statements in place, let's
change the code to close both fds on exit path.

Reported by Coverity, CID 51639, CID 51631.

Signed-off-by: Kir Kolyshkin <kir at openvz.org>
---
 files.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/files.c b/files.c
index bf39567..1a60e41 100644
--- a/files.c
+++ b/files.c
@@ -1077,7 +1077,7 @@ static int fchroot(int fd)
 
 int restore_fs(struct pstree_item *me)
 {
-	int dd_root, dd_cwd, ret, err = -1;
+	int dd_root = -1, dd_cwd = -1, ret, err = -1;
 	struct rst_info *ri = rsti(me);
 
 	/*
@@ -1104,14 +1104,12 @@ int restore_fs(struct pstree_item *me)
 	 */
 
 	ret = fchroot(dd_root);
-	close(dd_root);
 	if (ret < 0) {
 		pr_perror("Can't change root");
 		goto out;
 	}
 
 	ret = fchdir(dd_cwd);
-	close(dd_cwd);
 	if (ret < 0) {
 		pr_perror("Can't change cwd");
 		goto out;
@@ -1124,6 +1122,11 @@ int restore_fs(struct pstree_item *me)
 
 	err = 0;
 out:
+	if (dd_cwd >= 0)
+		close(dd_cwd);
+	if (dd_root >= 0)
+		close(dd_root);
+
 	return err;
 }
 
-- 
1.9.3



More information about the CRIU mailing list