Commit bda944e1 authored by Kirill Tkhai's avatar Kirill Tkhai Committed by Andrei Vagin

mnt: Put root fd to fdstore

mntns_get_root_fd() may be called by a task from
!root_user_ns, and it fails if so.

Put root fd to fdstore to allow use it every task.

v3: New
Signed-off-by: 's avatarKirill Tkhai <ktkhai@virtuozzo.com>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent 0f7e6928
......@@ -105,7 +105,7 @@ struct ns_id {
struct mount_info *mntinfo_list;
struct mount_info *mntinfo_tree;
int ns_fd;
int root_fd;
int root_fd_id;
} mnt;
struct {
......
......@@ -27,6 +27,7 @@
#include "files-reg.h"
#include "external.h"
#include "clone-noasan.h"
#include "fdstore.h"
#include "images/mnt.pb-c.h"
......@@ -3007,7 +3008,6 @@ void fini_restore_mntns(void)
if (nsid->nd != &mnt_ns_desc)
continue;
close_safe(&nsid->mnt.ns_fd);
close_safe(&nsid->mnt.root_fd);
nsid->ns_populated = true;
}
}
......@@ -3212,7 +3212,7 @@ void cleanup_mnt_ns(void)
int prepare_mnt_ns(void)
{
int ret = -1, rst = -1;
int ret = -1, rst = -1, fd;
struct ns_id ns = { .type = NS_CRIU, .ns_pid = PROC_SELF, .nd = &mnt_ns_desc };
struct ns_id *nsid;
......@@ -3299,10 +3299,17 @@ int prepare_mnt_ns(void)
if (cr_pivot_root(path))
goto err;
/* root_fd is used to restore file mappings */
nsid->mnt.root_fd = open_proc(PROC_SELF, "root");
if (nsid->mnt.root_fd < 0)
/* root fd is used to restore file mappings */
fd = open_proc(PROC_SELF, "root");
if (fd < 0)
goto err;
nsid->mnt.root_fd_id = fdstore_add(fd);
if (nsid->mnt.root_fd_id < 0) {
pr_err("Can't add root fd\n");
close(fd);
goto err;
}
close(fd);
/* And return back to regain the access to the roots yard */
if (setns(rst, CLONE_NEWNS)) {
......@@ -3417,7 +3424,7 @@ int mntns_get_root_fd(struct ns_id *mntns)
if (!mntns->ns_populated) {
int fd;
fd = open_proc(vpid(root_item), "fd/%d", mntns->mnt.root_fd);
fd = fdstore_get(mntns->mnt.root_fd_id);
if (fd < 0)
return -1;
......
......@@ -28,6 +28,7 @@
#include "pstree.h"
#include "external.h"
#include "crtools.h"
#include "fdstore.h"
#include "protobuf.h"
#include "images/sk-unix.pb-c.h"
......@@ -1036,6 +1037,7 @@ static void revert_unix_sk_cwd(int *prev_cwd_fd, int *root_fd)
static int prep_unix_sk_cwd(struct unix_sk_info *ui, int *prev_cwd_fd, int *prev_root_fd)
{
static struct ns_id *root = NULL;
int fd;
*prev_cwd_fd = open(".", O_RDONLY);
if (*prev_cwd_fd < 0) {
......@@ -1052,10 +1054,17 @@ static int prep_unix_sk_cwd(struct unix_sk_info *ui, int *prev_cwd_fd, int *prev
goto err;
}
if (fchdir(root->mnt.root_fd)) {
fd = fdstore_get(root->mnt.root_fd_id);
if (fd < 0) {
pr_err("Can't get root fd\n");
goto err;
}
if (fchdir(fd)) {
pr_perror("Unable to change current working dir");
close(fd);
goto err;
}
close(fd);
if (chroot(".")) {
pr_perror("Unable to change root directory");
goto err;
......
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