Commit 152222a6 authored by Pavel Emelyanov's avatar Pavel Emelyanov

remap: Sanitize ghost file path printing

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: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 6cf77f67
...@@ -66,12 +66,20 @@ struct link_remap_rlb { ...@@ -66,12 +66,20 @@ struct link_remap_rlb {
}; };
static LIST_HEAD(link_remaps); 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]; 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 (S_ISFIFO(gfe->mode)) {
if (mknod(path, gfe->mode, 0)) { if (mknod(path, gfe->mode, 0)) {
pr_perror("Can't create node for ghost file"); pr_perror("Can't create node for ghost file");
...@@ -127,7 +135,6 @@ static int open_remap_ghost(struct reg_file_info *rfi, ...@@ -127,7 +135,6 @@ static int open_remap_ghost(struct reg_file_info *rfi,
struct ghost_file *gf; struct ghost_file *gf;
GhostFileEntry *gfe = NULL; GhostFileEntry *gfe = NULL;
struct cr_img *img; struct cr_img *img;
char *root;
list_for_each_entry(gf, &ghost_files, list) list_for_each_entry(gf, &ghost_files, list)
if (gf->id == rfe->remap_id) if (gf->id == rfe->remap_id)
...@@ -141,17 +148,11 @@ static int open_remap_ghost(struct reg_file_info *rfi, ...@@ -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); 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)); gf = shmalloc(sizeof(*gf));
if (!gf) if (!gf)
return -1; return -1;
gf->remap.rpath = xmalloc(PATH_MAX); gf->remap.rpath = xmalloc(PATH_MAX);
gf->remap.rmnt_id = rfi->rfe->mnt_id;
if (!gf->remap.rpath) if (!gf->remap.rpath)
goto err; goto err;
...@@ -169,13 +170,14 @@ static int open_remap_ghost(struct reg_file_info *rfi, ...@@ -169,13 +170,14 @@ static int open_remap_ghost(struct reg_file_info *rfi,
*/ */
gf->dev = gfe->dev; gf->dev = gfe->dev;
gf->ino = gfe->ino; gf->ino = gfe->ino;
gf->remap.rmnt_id = rfi->rfe->mnt_id;
if (S_ISDIR(gfe->mode)) if (S_ISDIR(gfe->mode))
strncpy(gf->remap.rpath, rfi->path, PATH_MAX); strncpy(gf->remap.rpath, rfi->path, PATH_MAX);
else else
snprintf(gf->remap.rpath, PATH_MAX, "%s.cr.%x.ghost", rfi->path, rfe->remap_id); 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; goto close_ifd;
ghost_file_entry__free_unpacked(gfe, NULL); ghost_file_entry__free_unpacked(gfe, NULL);
......
...@@ -115,7 +115,7 @@ extern bool phys_stat_dev_match(dev_t st_dev, dev_t phys_dev, ...@@ -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 restore_task_mnt_ns(struct pstree_item *current);
extern int depopulate_roots_yard(void); extern int depopulate_roots_yard(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); extern int ext_mount_add(char *key, char *val);
extern int mntns_maybe_create_roots(void); extern int mntns_maybe_create_roots(void);
extern void cleanup_mnt_ns(void); extern void cleanup_mnt_ns(void);
......
...@@ -2526,25 +2526,24 @@ static int read_mnt_ns_img(void) ...@@ -2526,25 +2526,24 @@ static int read_mnt_ns_img(void)
return 0; 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; struct mount_info *m;
static char path[PATH_MAX] = "/";
if (!(root_ns_mask & CLONE_NEWNS)) if (!(root_ns_mask & CLONE_NEWNS) || mnt_id == -1)
return path; goto rroot;
if (mnt_id == -1)
return path;
m = lookup_mnt_id(mnt_id); m = lookup_mnt_id(mnt_id);
if (m == NULL) if (m == NULL)
return NULL; return -1;
if (m->nsid->type == NS_OTHER) 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;
} }
int mntns_maybe_create_roots(void) int mntns_maybe_create_roots(void)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment