diff --git a/criu/net.c b/criu/net.c index b4230007cd9..38dafcaeb69 100644 --- a/criu/net.c +++ b/criu/net.c @@ -2035,7 +2035,10 @@ static int prepare_net_ns_second_stage(struct ns_id *ns) ns->net.nsfd_id = fdstore_add(fd); if (ns->net.nsfd_id < 0) ret = -1; - close(fd); + + /* fd for the root is closed in prepare_net_namespaces() */ + if (ns->type != NS_ROOT) + close(fd); } ns->ns_populated = true; @@ -2094,7 +2097,7 @@ static int create_net_ns(void *arg) int prepare_net_namespaces() { struct ns_id *nsid; - int ret = -1; + int ret = -1, rst = -1; if (!(root_ns_mask & CLONE_NEWNET)) return 0; @@ -2137,6 +2140,9 @@ int prepare_net_namespaces() if (nsid->nd != &net_ns_desc) continue; + if (nsid->type == NS_ROOT) + rst = nsid->net.ns_fd; + if (switch_ns_by_fd(nsid->net.ns_fd, &net_ns_desc, NULL)) goto err; @@ -2146,11 +2152,20 @@ int prepare_net_namespaces() close_safe(&nsid->net.nlsk); } + if (rst < 0) { + pr_err("Unable to find the root net namespace"); + goto err; + } + + if (switch_ns_by_fd(rst, &net_ns_desc, NULL)) + goto err; + close_service_fd(NS_FD_OFF); ret = 0; err: if (ret) pr_err("Can't create net_ns\n"); + close_safe(&rst); return ret; }