[2/3] tun: Set net ns before opening master

Submitted by Cyrill Gorcunov on Sept. 21, 2018, 8:41 a.m.

Details

Message ID 20180921084145.18740-3-gorcunov@gmail.com
State Accepted
Series "tun: Fixes for netns sake"
Headers show

Commit Message

Cyrill Gorcunov Sept. 21, 2018, 8:41 a.m.
Opening tun device is tricky: the net device
is allocated from current net namespace, thus
we should set it first, otherwise all tuns
which live in nested net namespace get moved
to the toplevel one.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
---
 criu/tun.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/tun.c b/criu/tun.c
index 2e189c69c8ac..e9c18c0abbe2 100644
--- a/criu/tun.c
+++ b/criu/tun.c
@@ -369,13 +369,14 @@  static int tunfile_open(struct file_desc *d, int *new_fd)
 	struct tun_link *tl;
 
 	ti = container_of(d, struct tunfile_info, d);
-	fd = open_reg_by_id(ti->tfe->id);
-	if (fd < 0)
-		return -1;
 
 	ns_id = ti->tfe->ns_id;
 	if (set_netns(ns_id))
-		goto err;
+		return -1;
+
+	fd = open_reg_by_id(ti->tfe->id);
+	if (fd < 0)
+		return -1;
 
 	if (!ti->tfe->netdev)
 		/* just-opened tun file */

Comments

Dmitry Safonov Sept. 21, 2018, 1:22 p.m.
2018-09-21 9:41 GMT+01:00 Cyrill Gorcunov <gorcunov@gmail.com>:
> Opening tun device is tricky: the net device
> is allocated from current net namespace, thus
> we should set it first, otherwise all tuns
> which live in nested net namespace get moved
> to the toplevel one.
>
> Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>

Sorry about non-fun question, but is it possible to create
a test for that?

Thanks,
             Dmitry
Cyrill Gorcunov Sept. 21, 2018, 1:26 p.m.
On Fri, Sep 21, 2018 at 02:22:50PM +0100, Dmitry Safonov wrote:
> 2018-09-21 9:41 GMT+01:00 Cyrill Gorcunov <gorcunov@gmail.com>:
> > Opening tun device is tricky: the net device
> > is allocated from current net namespace, thus
> > we should set it first, otherwise all tuns
> > which live in nested net namespace get moved
> > to the toplevel one.
> >
> > Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
> 
> Sorry about non-fun question, but is it possible to create
> a test for that?

Yes, I was about to make so once my patches for
tests are applied.
Andrey Vagin Sept. 24, 2018, 9:33 p.m.
On Fri, Sep 21, 2018 at 04:26:25PM +0300, Cyrill Gorcunov wrote:
> On Fri, Sep 21, 2018 at 02:22:50PM +0100, Dmitry Safonov wrote:
> > 2018-09-21 9:41 GMT+01:00 Cyrill Gorcunov <gorcunov@gmail.com>:
> > > Opening tun device is tricky: the net device
> > > is allocated from current net namespace, thus
> > > we should set it first, otherwise all tuns
> > > which live in nested net namespace get moved
> > > to the toplevel one.
> > >
> > > Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
> > 
> > Sorry about non-fun question, but is it possible to create
> > a test for that?
> 
> Yes, I was about to make so once my patches for
> tests are applied.

They have been applied. We are waiting for a test;)
Cyrill Gorcunov Sept. 24, 2018, 10:03 p.m.
On Mon, Sep 24, 2018 at 09:33:20PM +0000, Andrey Vagin wrote:
> > 
> > Yes, I was about to make so once my patches for
> > tests are applied.
> 
> They have been applied. We are waiting for a test;)

Will send tomorrow. We need not only test ns but also
name clashing.