All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jason Wang <jasowang@redhat.com>
To: Cong Wang <xiyou.wangcong@gmail.com>
Cc: "weiyongjun (A)" <weiyongjun1@huawei.com>,
	yuehaibing <yuehaibing@huawei.com>,
	David Miller <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jesper Dangaard Brouer <brouer@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Li,Rongqing" <lirongqing@baidu.com>,
	nicolas dichtel <nicolas.dichtel@6wind.com>,
	Chas Williams <3chas3@gmail.com>,
	wangli39@baidu.com, LKML <linux-kernel@vger.kernel.org>,
	Linux Kernel Network Developers <netdev@vger.kernel.org>,
	Peter Xu <peterx@redhat.com>
Subject: Re: [PATCH] tun: Fix use-after-free in tun_net_xmit
Date: Sun, 5 May 2019 17:09:36 +0800	[thread overview]
Message-ID: <d32f77c0-ce75-b96f-f158-1891966dc83c@redhat.com> (raw)
In-Reply-To: <CAM_iQpXNp4h-ZAf4S+OH_1kVE_qk_eb+r6=ZUsK1t2=3aQOOtw@mail.gmail.com>


On 2019/4/30 上午12:38, Cong Wang wrote:
> On Sun, Apr 28, 2019 at 7:23 PM Jason Wang <jasowang@redhat.com> 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?
>
> Sure, this is how RCU is supposed to work.
>
>>> 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().
> Why tun->numqueues needs RCU? It is an integer, and reading a stale
> value is _perfectly_ fine.


I meant we don't want e.g tun_net_xmit() to see the stale value after 
synchronize_net() in __tun_detach(), since it has various other steps 
with the assumption that no tfile dereference from data path. E.g one 
example is XDP rxq information un-registering which looks racy in the 
case of XDP_TX.


>
> If you actually meant to say tun->tfiles[] itself, no, it is a fixed-size array,
> it doesn't shrink or grow, so we don't need RCU for it. This is also why
> a stale tun->numqueues is fine, as long as it never goes out-of-bound.


We do kind of shrinking or growing through tun->numqueues. That's why we 
check against it in various places. But, of course this is buggy.


>
>
>>> 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 believe SOCK_RCU_FREE is the fix for the root cause, not just a
> cover-up.
>
>
>> 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):
> I agree that synchronize_net() can be removed. However I don't
> understand your untested patch at all, it looks like to fix a completely
> different problem rather than this use-after-free.


As has been mentioned, the problem of current code is that we still 
leave pointers  to freed tfile in tfiles[] array in __tun_detach() and 
the check with tun->numqueues seems racy. So the patch just NULL out the 
detached tfile pointers and make sure no it can not be dereferenced from 
tfile after synchronize_net() by dereferencing tfile instead of checking 
tun->numqueues .


Thanks

>
> Thanks.

  parent reply	other threads:[~2019-05-05  9:09 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-22  3:57 BUG: KASAN: use-after-free Read in tun_net_xmit YueHaibing
2019-04-22  9:41 ` Jason Wang
2019-04-22 12:37   ` YueHaibing
2019-04-23  6:00   ` Cong Wang
2019-04-23  6:42     ` Jason Wang
2019-04-23 16:41       ` Cong Wang
2019-04-24  9:11         ` Jason Wang
2019-04-24 12:25           ` YueHaibing
2019-04-25 16:04             ` YueHaibing
2019-04-26  7:37               ` Jason Wang
2019-04-28  3:05                 ` [PATCH] tun: Fix use-after-free " 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
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 [this message]
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=d32f77c0-ce75-b96f-f158-1891966dc83c@redhat.com \
    --to=jasowang@redhat.com \
    --cc=3chas3@gmail.com \
    --cc=brouer@redhat.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.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 \
    --cc=yuehaibing@huawei.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.