mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: YueHaibing <yuehaibing@huawei.com>
To: Jason Wang <jasowang@redhat.com>, Cong Wang <xiyou.wangcong@gmail.com>
Cc: "weiyongjun (A)" <weiyongjun1@huawei.com>, <davem@davemloft.net>,
	<edumazet@google.com>, <brouer@redhat.com>, <mst@redhat.com>,
	<lirongqing@baidu.com>,
	nicolas dichtel <nicolas.dichtel@6wind.com>, <3chas3@gmail.com>,
	<wangli39@baidu.com>, <linux-kernel@vger.kernel.org>,
	<netdev@vger.kernel.org>, Peter Xu <peterx@redhat.com>
Subject: Re: [PATCH] tun: Fix use-after-free in tun_net_xmit
Date: Mon, 29 Apr 2019 21:07:07 +0800	[thread overview]
Message-ID: <27477e2a-ddfa-4e80-7f8c-18e4f5ff4990@huawei.com> (raw)
In-Reply-To: <528517144.24310809.1556504619719.JavaMail.zimbra@redhat.com>

On 2019/4/29 10:23, Jason Wang wrote:
> 
> On 2019/4/29 上午1:59, Cong Wang wrote:
>> On Sun, Apr 28, 2019 at 12:51 AM Jason Wang <jasowang@redhat.com> wrote:
>>>> tun_net_xmit() doesn't have the chance to
>>>> access the change because it holding the rcu_read_lock().
>>>
>>>
>>> The problem is the following codes:
>>>
>>>
>>>          --tun->numqueues;
>>>
>>>          ...
>>>
>>>          synchronize_net();
>>>
>>> We need make sure the decrement of tun->numqueues be visible to readers
>>> after synchronize_net(). And in tun_net_xmit():
>>
>> It doesn't matter at all. Readers are okay to read it even they still use the
>> stale tun->numqueues, as long as the tfile is not freed readers can read
>> whatever they want...
> 
> This is only true if we set SOCK_RCU_FREE, isn't it?
> 
>>
>> The decrement of tun->numqueues is just how we unpublish the old
>> tfile, it is still valid for readers to read it _after_ unpublish, we only need
>> to worry about free, not about unpublish. This is the whole spirit of RCU.
>>
> 
> The point is we don't convert tun->numqueues to RCU but use
> synchronize_net().
> 
>> You need to rethink about my SOCK_RCU_FREE patch.
> 
> The code is wrote before SOCK_RCU_FREE is introduced and assume no
> de-reference from device after synchronize_net(). It doesn't harm to
> figure out the root cause which may give us more confidence to the fix
> (e.g like SOCK_RCU_FREE).
> 
> I don't object to fix with SOCK_RCU_FREE, but then we should remove
> the redundant synchronize_net(). But I still prefer to synchronize
> everything explicitly like (completely untested):
> 
>>From df91f77d35a6aa7943b6f2a7d4b329990896a0fe Mon Sep 17 00:00:00 2001
> From: Jason Wang <jasowang@redhat.com>
> Date: Mon, 29 Apr 2019 10:21:06 +0800
> Subject: [PATCH] tuntap: synchronize through tfiles instead of numqueues
> 
> Signed-off-by: Jason Wang <jasowang@redhat.com>
> ---
>  drivers/net/tun.c | 11 +++++------
>  1 file changed, 5 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> index 80bff1b4ec17..03715f605fb5 100644
> --- a/drivers/net/tun.c
> +++ b/drivers/net/tun.c
> @@ -698,6 +698,7 @@ static void __tun_detach(struct tun_file *tfile, bool clean)
>  
>  		rcu_assign_pointer(tun->tfiles[index],
>  				   tun->tfiles[tun->numqueues - 1]);
> +		rcu_assign_pointer(tun->tfiles[tun->numqueues], NULL);
>  		ntfile = rtnl_dereference(tun->tfiles[index]);

move here to avoid NULL pointer dereference,  it works for me

		rcu_assign_pointer(tun->tfiles[tun->numqueues -1 ], NULL);

>  		ntfile->queue_index = index;
>  
> @@ -1082,7 +1083,7 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
>  	tfile = rcu_dereference(tun->tfiles[txq]);
>  
>  	/* Drop packet if interface is not attached */
> -	if (txq >= tun->numqueues)
> +	if (!tfile)
>  		goto drop;
>  
>  	if (!rcu_dereference(tun->steering_prog))
> @@ -1305,15 +1306,13 @@ static int tun_xdp_xmit(struct net_device *dev, int n,
>  
>  	rcu_read_lock();
>  
> -	numqueues = READ_ONCE(tun->numqueues);
> -	if (!numqueues) {
> +	tfile = rcu_dereference(tun->tfiles[smp_processor_id() %
> +					    tun->numqueues]);
> +	if (!tfile) {
>  		rcu_read_unlock();
>  		return -ENXIO; /* Caller will free/return all frames */
>  	}
>  
> -	tfile = rcu_dereference(tun->tfiles[smp_processor_id() %
> -					    numqueues]);
> -
>  	spin_lock(&tfile->tx_ring.producer_lock);
>  	for (i = 0; i < n; i++) {
>  		struct xdp_frame *xdp = frames[i];
> 


  parent reply	other threads:[~2019-04-29 13:07 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <71250616-36c1-0d96-8fac-4aaaae6a28d4@redhat.com>
2019-04-28  3:05 ` Yue Haibing
2019-04-28  3:24   ` Jason Wang
2019-04-28  4:26     ` Jason Wang
2019-04-28  5:40       ` weiyongjun (A)
2019-04-28  7:51         ` Jason Wang
2019-04-28 17:59           ` Cong Wang
2019-04-29  2:23             ` Jason Wang
2019-04-29  3:53               ` weiyongjun (A)
2019-04-29 13:07               ` YueHaibing [this message]
2019-04-29 16:38               ` Cong Wang
2019-04-30  2:44                 ` YueHaibing
2019-04-30 23:33                   ` Cong Wang
2019-05-05  9:09                 ` Jason Wang
2019-04-28 17:49   ` Cong Wang
2019-04-29 14:55   ` Michael S. Tsirkin
2019-04-29 16:58     ` Cong Wang
2019-04-30  5:11       ` weiyongjun (A)
2019-04-30 23:42         ` Cong Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=27477e2a-ddfa-4e80-7f8c-18e4f5ff4990@huawei.com \
    --to=yuehaibing@huawei.com \
    --cc=3chas3@gmail.com \
    --cc=brouer@redhat.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=jasowang@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lirongqing@baidu.com \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=nicolas.dichtel@6wind.com \
    --cc=peterx@redhat.com \
    --cc=wangli39@baidu.com \
    --cc=weiyongjun1@huawei.com \
    --cc=xiyou.wangcong@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®