linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] tuntap: forbid calling TUNSETIFF when detached
@ 2013-01-11  8:50 Jason Wang
  2013-01-11  8:50 ` [PATCH 2/2] tuntap: fix leaking reference count Jason Wang
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Jason Wang @ 2013-01-11  8:50 UTC (permalink / raw)
  To: davem, mst, netdev, linux-kernel; +Cc: Jason Wang, Stefan Hajnoczi

Michael points out that even after Stefan's fix the TUNSETIFF is still allowed
to create a new tap device. This because we only check tfile->tun but the
tfile->detached were introduced. Fix this by failing early in tun_set_iff() if
the file is detached. After this fix, there's no need to do the check again in
tun_set_iff(), so this patch removes it.

Cc: Michael S. Tsirkin <mst@redhat.com>
Cc: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/net/tun.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 99b58d8..9a46d70 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -491,8 +491,6 @@ static int tun_attach(struct tun_struct *tun, struct file *file)
 	err = -EINVAL;
 	if (rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held()))
 		goto out;
-	if (tfile->detached && tun != tfile->detached)
-		goto out;
 
 	err = -EBUSY;
 	if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
@@ -1546,6 +1544,9 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 	struct net_device *dev;
 	int err;
 
+	if (rcu_dereference_protected(tfile->detached, lockdep_rtnl_is_held()))
+		return -EINVAL;
+
 	dev = __dev_get_by_name(net, ifr->ifr_name);
 	if (dev) {
 		if (ifr->ifr_flags & IFF_TUN_EXCL)
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/2] tuntap: fix leaking reference count
  2013-01-11  8:50 [PATCH 1/2] tuntap: forbid calling TUNSETIFF when detached Jason Wang
@ 2013-01-11  8:50 ` Jason Wang
  2013-01-11 14:42 ` [PATCH 1/2] tuntap: forbid calling TUNSETIFF when detached Stefan Hajnoczi
  2013-01-11 14:55 ` Eric Dumazet
  2 siblings, 0 replies; 6+ messages in thread
From: Jason Wang @ 2013-01-11  8:50 UTC (permalink / raw)
  To: davem, mst, netdev, linux-kernel; +Cc: Jason Wang

Reference count leaking of both module and sock were found:

- When a detached file were closed, its sock refcnt from device were not
  released, solving this by add the sock_put().
- The module were hold or drop unconditionally in TUNSETPERSIST, which means we
  if we set the persist flag for N times, we need unset it for another N
  times. Solving this by only hold or drop an reference when there's a flag
  change and also drop the reference count when the persist device is deleted.

Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/net/tun.c |   12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 9a46d70..d690dc8 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -429,8 +429,10 @@ static void __tun_detach(struct tun_file *tfile, bool clean)
 		/* Drop read queue */
 		skb_queue_purge(&tfile->sk.sk_receive_queue);
 		tun_set_real_num_queues(tun);
-	} else if (tfile->detached && clean)
+	} else if (tfile->detached && clean) {
 		tun = tun_enable_queue(tfile);
+		sock_put(&tfile->sk);
+	}
 
 	if (clean) {
 		if (tun && tun->numqueues == 0 && tun->numdisabled == 0 &&
@@ -481,6 +483,9 @@ static void tun_detach_all(struct net_device *dev)
 		sock_put(&tfile->sk);
 	}
 	BUG_ON(tun->numdisabled != 0);
+
+	if (tun->flags & TUN_PERSIST)
+		module_put(THIS_MODULE);
 }
 
 static int tun_attach(struct tun_struct *tun, struct file *file)
@@ -1881,10 +1886,11 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 		/* Disable/Enable persist mode. Keep an extra reference to the
 		 * module to prevent the module being unprobed.
 		 */
-		if (arg) {
+		if (arg && !(tun->flags & TUN_PERSIST)) {
 			tun->flags |= TUN_PERSIST;
 			__module_get(THIS_MODULE);
-		} else {
+		}
+		if (!arg && (tun->flags & TUN_PERSIST)) {
 			tun->flags &= ~TUN_PERSIST;
 			module_put(THIS_MODULE);
 		}
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] tuntap: forbid calling TUNSETIFF when detached
  2013-01-11  8:50 [PATCH 1/2] tuntap: forbid calling TUNSETIFF when detached Jason Wang
  2013-01-11  8:50 ` [PATCH 2/2] tuntap: fix leaking reference count Jason Wang
@ 2013-01-11 14:42 ` Stefan Hajnoczi
  2013-01-12  2:31   ` Jason Wang
  2013-01-11 14:55 ` Eric Dumazet
  2 siblings, 1 reply; 6+ messages in thread
From: Stefan Hajnoczi @ 2013-01-11 14:42 UTC (permalink / raw)
  To: Jason Wang; +Cc: davem, mst, netdev, linux-kernel

On Fri, Jan 11, 2013 at 04:50:41PM +0800, Jason Wang wrote:
> @@ -1546,6 +1544,9 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
>  	struct net_device *dev;
>  	int err;
>  
> +	if (rcu_dereference_protected(tfile->detached, lockdep_rtnl_is_held()))
> +		return -EINVAL;
> +

How come none of the other tfile->detached users call rcu_*()?

Stefan

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] tuntap: forbid calling TUNSETIFF when detached
  2013-01-11  8:50 [PATCH 1/2] tuntap: forbid calling TUNSETIFF when detached Jason Wang
  2013-01-11  8:50 ` [PATCH 2/2] tuntap: fix leaking reference count Jason Wang
  2013-01-11 14:42 ` [PATCH 1/2] tuntap: forbid calling TUNSETIFF when detached Stefan Hajnoczi
@ 2013-01-11 14:55 ` Eric Dumazet
  2013-01-12  2:31   ` Jason Wang
  2 siblings, 1 reply; 6+ messages in thread
From: Eric Dumazet @ 2013-01-11 14:55 UTC (permalink / raw)
  To: Jason Wang; +Cc: davem, mst, netdev, linux-kernel, Stefan Hajnoczi

On Fri, 2013-01-11 at 16:50 +0800, Jason Wang wrote:

>  
> +	if (rcu_dereference_protected(tfile->detached, lockdep_rtnl_is_held()))
> +		return -EINVAL;
> +

Thats an open coded rtnl_dereference()

(please cleanup the whole tun.c file)




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] tuntap: forbid calling TUNSETIFF when detached
  2013-01-11 14:42 ` [PATCH 1/2] tuntap: forbid calling TUNSETIFF when detached Stefan Hajnoczi
@ 2013-01-12  2:31   ` Jason Wang
  0 siblings, 0 replies; 6+ messages in thread
From: Jason Wang @ 2013-01-12  2:31 UTC (permalink / raw)
  To: Stefan Hajnoczi; +Cc: davem, mst, netdev, linux-kernel

On 01/11/2013 10:42 PM, Stefan Hajnoczi wrote:
> On Fri, Jan 11, 2013 at 04:50:41PM +0800, Jason Wang wrote:
>> @@ -1546,6 +1544,9 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
>>  	struct net_device *dev;
>>  	int err;
>>  
>> +	if (rcu_dereference_protected(tfile->detached, lockdep_rtnl_is_held()))
>> +		return -EINVAL;
>> +
> How come none of the other tfile->detached users call rcu_*()?

Right, no need to use rcu, the access were protected by rtnl lock.

Will repost.
> Stefan
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] tuntap: forbid calling TUNSETIFF when detached
  2013-01-11 14:55 ` Eric Dumazet
@ 2013-01-12  2:31   ` Jason Wang
  0 siblings, 0 replies; 6+ messages in thread
From: Jason Wang @ 2013-01-12  2:31 UTC (permalink / raw)
  To: Eric Dumazet; +Cc: davem, mst, netdev, linux-kernel, Stefan Hajnoczi

On 01/11/2013 10:55 PM, Eric Dumazet wrote:
> On Fri, 2013-01-11 at 16:50 +0800, Jason Wang wrote:
>
>>  
>> +	if (rcu_dereference_protected(tfile->detached, lockdep_rtnl_is_held()))
>> +		return -EINVAL;
>> +
> Thats an open coded rtnl_dereference()
>
> (please cleanup the whole tun.c file)
>

Sure.
>
> --
> 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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-01-12  2:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-11  8:50 [PATCH 1/2] tuntap: forbid calling TUNSETIFF when detached Jason Wang
2013-01-11  8:50 ` [PATCH 2/2] tuntap: fix leaking reference count Jason Wang
2013-01-11 14:42 ` [PATCH 1/2] tuntap: forbid calling TUNSETIFF when detached Stefan Hajnoczi
2013-01-12  2:31   ` Jason Wang
2013-01-11 14:55 ` Eric Dumazet
2013-01-12  2:31   ` Jason Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).