[v2,04/57] ns: Fix wrong opened net ns file

Submitted by Kirill Tkhai on March 28, 2017, 3:34 p.m.

Details

Message ID 149071527110.12770.16132844612421181242.stgit@localhost.localdomain
State New
Series "Nested pid namespaces support"
Headers show

Commit Message

Kirill Tkhai March 28, 2017, 3:34 p.m.
Since net ns is assigned after prepare_fds() and,
in common case, at the moment of open_ns_fd() call
task points to a net ns, which differs to its target
net ns, we can't get the ns from a task. So, get it
from fdstore. Also, support userns ns fds.

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 criu/namespaces.c |   21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/criu/namespaces.c b/criu/namespaces.c
index 20e67092..4ecd6e36 100644
--- a/criu/namespaces.c
+++ b/criu/namespaces.c
@@ -567,8 +567,22 @@  static int open_ns_fd(struct file_desc *d, int *new_fd)
 	struct ns_file_info *nfi = container_of(d, struct ns_file_info, d);
 	struct pstree_item *item, *t;
 	struct ns_desc *nd = NULL;
+	struct ns_id *ns;
+	int nsfd_id, fd;
 	char path[64];
-	int fd;
+
+	for (ns = ns_ids; ns != NULL; ns = ns->next) {
+		if (ns->id != nfi->nfe->ns_id)
+			continue;
+		if (ns->nd == &user_ns_desc && (root_ns_mask & CLONE_NEWUSER))
+			nsfd_id = ns->user.nsfd_id;
+		else if (ns->nd == &net_ns_desc && (root_ns_mask & CLONE_NEWNET))
+			nsfd_id = ns->net.nsfd_id;
+		else
+			break;
+		fd = fdstore_get(nsfd_id);
+		goto check_open;
+	}
 
 	/*
 	 * Find out who can open us.
@@ -586,6 +600,10 @@  static int open_ns_fd(struct file_desc *d, int *new_fd)
 			item = t;
 			nd = &net_ns_desc;
 			break;
+		} else if (ids->user_ns_id == nfi->nfe->ns_id) {
+			item = t;
+			nd = &user_ns_desc;
+			break;
 		} else if (ids->ipc_ns_id == nfi->nfe->ns_id) {
 			item = t;
 			nd = &ipc_ns_desc;
@@ -619,6 +637,7 @@  static int open_ns_fd(struct file_desc *d, int *new_fd)
 	path[sizeof(path) - 1] = '\0';
 
 	fd = open(path, nfi->nfe->flags);
+check_open:
 	if (fd < 0) {
 		pr_perror("Can't open file %s on restore", path);
 		return fd;