Re: tun netns BUG()

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Eric W. Biederman <ebiederm@...>, David S. Miller <davem@...>
Cc: <dwmw2@...>, <netdev@...>, <johannes@...>
Date: Friday, July 3, 2009 - 5:03 am

On Fri, Jul 03, 2009 at 04:35:01PM +0800, Herbert Xu wrote:

tun: Fix device unregister race

It is currently possible for an asynchronous device unregister
to cause the same tun device to be unregistered twice.  This
is because the unregister in tun_chr_close only checks whether
__tun_get(tfile) != NULL.  This however has nothing to do with
whether the device has already been unregistered.  All it tells
you is whether __tun_detach has been called.

This patch fixes this by using the most obvious thing to test
whether the device has been unregistered.

It also moves __tun_detach outside of rtnl_unlock since nothing
that it does requires that lock.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 11a0ba4..b393536 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -1324,20 +1324,22 @@ static int tun_chr_close(struct inode *inode, struct file *file)
 	struct tun_file *tfile = file->private_data;
 	struct tun_struct *tun;
 
-
-	rtnl_lock();
 	tun = __tun_get(tfile);
 	if (tun) {
-		DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
+		struct net_device *dev = tun->dev;
+
+		DBG(KERN_INFO "%s: tun_chr_close\n", dev->name);
 
 		__tun_detach(tun);
 
 		/* If desireable, unregister the netdevice. */
-		if (!(tun->flags & TUN_PERSIST))
-			unregister_netdevice(tun->dev);
-
+		if (!(tun->flags & TUN_PERSIST)) {
+			rtnl_lock();
+			if (dev->reg_state == NETREG_REGISTERED)
+				unregister_netdevice(dev);
+			rtnl_unlock();
+		}
 	}
-	rtnl_unlock();
 
 	tun = tfile->tun;
 	if (tun)

Cheers,
-- 
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
tun netns BUG(), David Woodhouse, (Thu Jun 4, 3:05 pm)
Re: tun netns BUG(), Eric W. Biederman, (Thu Jun 4, 9:19 pm)
Re: tun netns BUG(), David Woodhouse, (Fri Jun 5, 4:42 am)
Re: tun netns BUG(), Eric W. Biederman, (Fri Jun 5, 7:20 pm)
Re: tun netns BUG(), David Woodhouse, (Fri Jun 5, 5:17 am)
Re: tun netns BUG(), Eric W. Biederman, (Fri Jun 5, 7:24 pm)
Re: tun netns BUG(), Herbert Xu, (Fri Jul 3, 4:35 am)
Re: tun netns BUG(), Herbert Xu, (Fri Jul 3, 5:03 am)
Re: tun netns BUG(), Eric W. Biederman, (Fri Jul 3, 10:52 am)
Re: tun netns BUG(), Herbert Xu, (Fri Jul 3, 11:25 am)
Re: tun netns BUG(), David Miller, (Sun Jul 5, 10:01 pm)
Re: tun netns BUG(), David Woodhouse, (Sat Jun 6, 3:06 am)
Re: tun netns BUG(), David Miller, (Mon Jun 8, 3:44 am)