From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8F9D2C3A5A1 for ; Thu, 22 Aug 2019 12:55:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6C4D02089E for ; Thu, 22 Aug 2019 12:55:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388758AbfHVMzm (ORCPT ); Thu, 22 Aug 2019 08:55:42 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:4759 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1732412AbfHVMzm (ORCPT ); Thu, 22 Aug 2019 08:55:42 -0400 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id C20AE92E8AD1A8BE20CA; Thu, 22 Aug 2019 20:55:38 +0800 (CST) Received: from [127.0.0.1] (10.133.205.80) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.439.0; Thu, 22 Aug 2019 20:55:33 +0800 Subject: Re: [PATCH v3] tun: fix use-after-free when register netdev failed To: Jason Wang , David Miller References: <1566221479-16094-1-git-send-email-yangyingliang@huawei.com> <20190819.182522.414877916903078544.davem@davemloft.net> <5D5E3133.2070108@huawei.com> CC: , , , From: Yang Yingliang Message-ID: <5D5E90C3.50306@huawei.com> Date: Thu, 22 Aug 2019 20:55:31 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 In-Reply-To: <5D5E3133.2070108@huawei.com> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.133.205.80] X-CFilter-Loop: Reflected Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On 2019/8/22 14:07, Yang Yingliang wrote: > > > On 2019/8/22 10:13, Jason Wang wrote: >> >> On 2019/8/20 上午10:28, Jason Wang wrote: >>> >>> On 2019/8/20 上午9:25, David Miller wrote: >>>> From: Yang Yingliang >>>> Date: Mon, 19 Aug 2019 21:31:19 +0800 >>>> >>>>> Call tun_attach() after register_netdevice() to make sure tfile->tun >>>>> is not published until the netdevice is registered. So the read/write >>>>> thread can not use the tun pointer that may freed by free_netdev(). >>>>> (The tun and dev pointer are allocated by alloc_netdev_mqs(), they >>>>> can >>>>> be freed by netdev_freemem().) >>>> register_netdevice() must always be the last operation in the order of >>>> network device setup. >>>> >>>> At the point register_netdevice() is called, the device is visible >>>> globally >>>> and therefore all of it's software state must be fully initialized and >>>> ready for us. >>>> >>>> You're going to have to find another solution to these problems. >>> >>> >>> The device is loosely coupled with sockets/queues. Each side is >>> allowed to be go away without caring the other side. So in this >>> case, there's a small window that network stack think the device has >>> one queue but actually not, the code can then safely drop them. >>> Maybe it's ok here with some comments? >>> >>> Or if not, we can try to hold the device before tun_attach and drop >>> it after register_netdevice(). >> >> >> Hi Yang: >> >> I think maybe we can try to hold refcnt instead of playing real num >> queues here. Do you want to post a V4? > I think the refcnt can prevent freeing the memory in this case. > When register_netdevice() failed, free_netdev() will be called directly, > dev->pcpu_refcnt and dev are freed without checking refcnt of dev. How about using patch-v1 that using a flag to check whether the device registered successfully. > >> >> Thanks >> >> >>> >>> Thanks >>> >> >> . >> > > > > . >