[CRIU] [PATCH 2/2] remap: Sanitize ghost file path printing

Pavel Emelyanov xemul at parallels.com
Wed Sep 23 05:39:48 PDT 2015


First -- avoid two memory copies by printing ns root directly, and
second -- remove extra argument from create_ghost, the mnt_id value
we need there can be found on the ghost_file object.

Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
 files-reg.c     | 26 ++++++++++++++------------
 include/mount.h |  2 +-
 mount.c         | 19 +++++++++----------
 3 files changed, 24 insertions(+), 23 deletions(-)

diff --git a/files-reg.c b/files-reg.c
index 1b41969..bed1e79 100644
--- a/files-reg.c
+++ b/files-reg.c
@@ -66,12 +66,20 @@ struct link_remap_rlb {
 };
 static LIST_HEAD(link_remaps);
 
-static int create_ghost(struct ghost_file *gf, GhostFileEntry *gfe, char *root, struct cr_img *img)
+static int create_ghost(struct ghost_file *gf, GhostFileEntry *gfe, struct cr_img *img)
 {
-	int gfd, ghost_flags, ret = -1;
+	int gfd, ghost_flags, ret;
 	char path[PATH_MAX];
 
-	snprintf(path, sizeof(path), "%s/%s", root, gf->remap.rpath);
+	ret = rst_get_mnt_root(gf->remap.rmnt_id, path, sizeof(path));
+	if (ret < 0) {
+		pr_err("The %d mount is not found for ghost\n", gf->remap.rmnt_id);
+		goto err;
+	}
+
+	snprintf(path + ret, sizeof(path) - ret, "%s", gf->remap.rpath);
+	ret = -1;
+
 	if (S_ISFIFO(gfe->mode)) {
 		if (mknod(path, gfe->mode, 0)) {
 			pr_perror("Can't create node for ghost file");
@@ -127,7 +135,6 @@ static int open_remap_ghost(struct reg_file_info *rfi,
 	struct ghost_file *gf;
 	GhostFileEntry *gfe = NULL;
 	struct cr_img *img;
-	char *root;
 
 	list_for_each_entry(gf, &ghost_files, list)
 		if (gf->id == rfe->remap_id)
@@ -141,17 +148,11 @@ static int open_remap_ghost(struct reg_file_info *rfi,
 
 	pr_info("Opening ghost file %#x for %s\n", rfe->remap_id, rfi->path);
 
-	root = rst_get_mnt_root(rfi->rfe->mnt_id);
-	if (root == NULL) {
-		pr_err("The %d mount is not found\n", rfi->rfe->mnt_id);
-		return -1;
-	}
-
 	gf = shmalloc(sizeof(*gf));
 	if (!gf)
 		return -1;
+
 	gf->remap.rpath = xmalloc(PATH_MAX);
-	gf->remap.rmnt_id = rfi->rfe->mnt_id;
 	if (!gf->remap.rpath)
 		goto err;
 
@@ -169,13 +170,14 @@ static int open_remap_ghost(struct reg_file_info *rfi,
 	 */
 	gf->dev = gfe->dev;
 	gf->ino = gfe->ino;
+	gf->remap.rmnt_id = rfi->rfe->mnt_id;
 
 	if (S_ISDIR(gfe->mode))
 		strncpy(gf->remap.rpath, rfi->path, PATH_MAX);
 	else
 		snprintf(gf->remap.rpath, PATH_MAX, "%s.cr.%x.ghost", rfi->path, rfe->remap_id);
 
-	if (create_ghost(gf, gfe, root, img))
+	if (create_ghost(gf, gfe, img))
 		goto close_ifd;
 
 	ghost_file_entry__free_unpacked(gfe, NULL);
diff --git a/include/mount.h b/include/mount.h
index 76a8e24..3d17c1f 100644
--- a/include/mount.h
+++ b/include/mount.h
@@ -115,7 +115,7 @@ extern bool phys_stat_dev_match(dev_t st_dev, dev_t phys_dev,
 extern int restore_task_mnt_ns(struct pstree_item *current);
 extern int fini_mnt_ns(void);
 
-extern char *rst_get_mnt_root(int mnt_id);
+extern int rst_get_mnt_root(int mnt_id, char *path, int plen);
 extern int ext_mount_add(char *key, char *val);
 
 #endif /* __CR_MOUNT_H__ */
diff --git a/mount.c b/mount.c
index 0bff365..4def1cb 100644
--- a/mount.c
+++ b/mount.c
@@ -2553,25 +2553,24 @@ static int read_mnt_ns_img(void)
 	return 0;
 }
 
-char *rst_get_mnt_root(int mnt_id)
+int rst_get_mnt_root(int mnt_id, char *path, int plen)
 {
 	struct mount_info *m;
-	static char path[PATH_MAX] = "/";
 
-	if (!(root_ns_mask & CLONE_NEWNS))
-		return path;
-
-	if (mnt_id == -1)
-		return path;
+	if (!(root_ns_mask & CLONE_NEWNS) || mnt_id == -1)
+		goto rroot;
 
 	m = lookup_mnt_id(mnt_id);
 	if (m == NULL)
-		return NULL;
+		return -1;
 
 	if (m->nsid->type == NS_OTHER)
-		print_ns_root(m->nsid, path, sizeof(path));
+		return print_ns_root(m->nsid, path, plen);
 
-	return path;
+rroot:
+	path[0] = '/';
+	path[1] = '\0';
+	return 1;
 }
 
 static int do_restore_task_mnt_ns(struct ns_id *nsid, struct pstree_item *current)
-- 
1.9.3




More information about the CRIU mailing list