Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memfd: make memfd_open() skip fd attributes not needed for vma mapping #2244

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion criu/files-reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -2508,7 +2508,8 @@ static int open_filemap(int pid, struct vma_area *vma)
*/
ret = dup(plugin_fd);
} else if (vma->e->status & VMA_AREA_MEMFD) {
ret = memfd_open(vma->vmfd, &flags);
if (!inherited_fd(vma->vmfd, &ret))
osctobe marked this conversation as resolved.
Show resolved Hide resolved
ret = memfd_open(vma->vmfd, &flags);
} else {
ret = open_path(vma->vmfd, do_open_reg_noseek_flags, &flags);
}
Expand Down
46 changes: 23 additions & 23 deletions criu/memfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,11 @@ int memfd_open(struct file_desc *d, u32 *fdflags)
mfi = container_of(d, struct memfd_info, d);
mfe = mfi->mfe;

if (inherited_fd(d, &fd))
return fd;

pr_info("Restoring memfd id=%d\n", mfe->id);

fd = memfd_open_inode(mfi->inode);
if (fd < 0)
goto err;
return -1;

/* Reopen the fd with original permissions */
flags = fdflags ? *fdflags : mfe->flags;
Expand All @@ -348,40 +345,43 @@ int memfd_open(struct file_desc *d, u32 *fdflags)
* important though.
*/
_fd = __open_proc(PROC_SELF, 0, flags, "fd/%d", fd);
if (_fd < 0) {
if (_fd < 0)
pr_perror("Can't reopen memfd id=%d", mfe->id);
goto err;
}

close(fd);
fd = _fd;
return _fd;
}

static int memfd_open_fe_fd(struct file_desc *d, int *new_fd)
{
MemfdFileEntry *mfe;
int fd;

if (inherited_fd(d, new_fd))
return 0;

fd = memfd_open(d, NULL);
if (fd < 0)
return -1;

mfe = container_of(d, struct memfd_info, d)->mfe;

if (restore_fown(fd, mfe->fown) < 0)
goto err;

if (lseek(fd, mfe->pos, SEEK_SET) < 0) {
pr_perror("Can't restore file position of memfd id=%d", mfe->id);
pr_perror("Can't restore file position of %d for memfd id=%d", fd, mfe->id);
goto err;
osctobe marked this conversation as resolved.
Show resolved Hide resolved
}

return fd;
*new_fd = fd;
return 0;

err:
if (fd >= 0)
close(fd);
close(fd);
return -1;
}

static int memfd_open_fe_fd(struct file_desc *fd, int *new_fd)
{
int tmp;

tmp = memfd_open(fd, NULL);
if (tmp < 0)
return -1;
*new_fd = tmp;
return 0;
}

static char *memfd_d_name(struct file_desc *d, char *buf, size_t s)
{
MemfdInodeEntry *mie = NULL;
Expand Down
Loading