Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp2434038pxu; Mon, 14 Dec 2020 02:08:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJxxSuGFtQ1uKAWiuAyGzN0HWXo7ybxo1Ix9CLIJRuoj63gt7IVcD7hFKMuQA9dSp2wiIttJ X-Received: by 2002:a17:906:4881:: with SMTP id v1mr1206901ejq.465.1607940532783; Mon, 14 Dec 2020 02:08:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607940532; cv=none; d=google.com; s=arc-20160816; b=M+3QsPFKTcBrZb4P/FjWrSFhtr0FkPNhpW+SyKnhny+6zjiwadMYJu9C9jmJjjzB+S UOpq2is+fMo856j34SuxNPAgQbnRuSs7Jb9d4j0wqAcT5sNruwSR+LhgdsT19ewj/Fd1 9nWGmIKlZF62S+KinJzYhvZy85F01tWkVt3yZCceF101HEkc2HpEHbaCQGOU+LvLUE9r zkMj9dtshWSWzbpxFxIUWXyknM7I5sIzupIuTAbCaEf5m1hYhBQeIFcztzv5da2KdQ9J HvzUapUrA8j8zz5bNYx8tFXBbEVArCg5kfbOMUrjEUeO+ZcdlZG8JFL+c03EUZfHFbOJ xKiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=kfuqMU39BtkSpKizPwncmxly1PZ+6i3WhRUwCLmfT0Y=; b=LyOWErk8/CkSQrVglT+nnQ/01PKXt2Cidhf/SO5e+l8xIqNeqYPmMrutT23Sg2yqIU 2EPXgrHl6e5HmXd1YLQ8UTELIUnIT+++ZT2wYlu6P6VB/Xi6xAbASmfzqOwxKvdw1Kkf U7ZGoRitvK8kXxuF8ucbjMz478OSM160opr+eKpKLei1oXQZYsUm0st9qN5if1z/mhHA ctWw4ORF/gc26gN8DnS69liiJ6b7BjxomgZPo5jGSvhbJWVgAqkl+/+94t3vRSfgm7FS Sa2swQ+KDNJVzQcIoxz3dlx1b4QfqyNmeUzWbWj1lIuPK6dIwaPB97v54A6L+kBHNwbv u9yw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o10si9042147ejg.80.2020.12.14.02.08.30; Mon, 14 Dec 2020 02:08:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2407097AbgLNHZ1 (ORCPT + 99 others); Mon, 14 Dec 2020 02:25:27 -0500 Received: from szxga06-in.huawei.com ([45.249.212.32]:9435 "EHLO szxga06-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726970AbgLNHZ1 (ORCPT ); Mon, 14 Dec 2020 02:25:27 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.60]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4CvXvN2j1szhsDs; Mon, 14 Dec 2020 15:24:16 +0800 (CST) Received: from [10.174.177.149] (10.174.177.149) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.498.0; Mon, 14 Dec 2020 15:24:42 +0800 Subject: Re: [PATCH] PCI: fix use-after-free in pci_register_host_bridge To: Rob Herring CC: Bjorn Helgaas , , References: <20201120074848.31418-1-miaoqinglang@huawei.com> <20201211154652.GA313883@robh.at.kernel.org> From: Qinglang Miao Message-ID: Date: Mon, 14 Dec 2020 15:24:41 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <20201211154652.GA313883@robh.at.kernel.org> Content-Type: text/plain; charset="gbk"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.177.149] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ?? 2020/12/11 23:46, Rob Herring ะด??: > On Fri, Nov 20, 2020 at 03:48:48PM +0800, Qinglang Miao wrote: >> When put_device(&bridge->dev) being called, kfree(bridge) is inside >> of release function, so the following device_del would cause a >> use-after-free bug. >> >> Fixes: 37d6a0a6f470 ("PCI: Add pci_register_host_bridge() interface") > > That commit did have some problems, but this patch doesn't apply to that > commit. See commits 1b54ae8327a4 and 9885440b16b8. > >> Reported-by: Hulk Robot >> Signed-off-by: Qinglang Miao >> --- >> drivers/pci/probe.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c >> index 4289030b0..82292e87e 100644 >> --- a/drivers/pci/probe.c >> +++ b/drivers/pci/probe.c >> @@ -991,8 +991,8 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge) >> return 0; >> >> unregister: >> - put_device(&bridge->dev); >> device_del(&bridge->dev); >> + put_device(&bridge->dev); > > I don't think this is right. > > Let's look at pci_register_host_bridge() with only the relevant > sections: > > static int pci_register_host_bridge(struct pci_host_bridge *bridge) > { > ... > > err = device_add(&bridge->dev); > if (err) { > put_device(&bridge->dev); > goto free; > } > bus->bridge = get_device(&bridge->dev); > > ... > if (err) > goto unregister; > ... > > return 0; > > unregister: > put_device(&bridge->dev); > device_del(&bridge->dev); > > free: > kfree(bus); > return err; > } > > The documentation for device_add says this: > * Rule of thumb is: if device_add() succeeds, you should call > * device_del() when you want to get rid of it. If device_add() has > * *not* succeeded, use *only* put_device() to drop the reference > * count. > > The put_device at the end is to balance the get_device after device_add. > It will *only* decrement the use count. Then we call device_del as the > documentation says. > > Rob > . Hi, Rob Your words make sence to me: the code is *logicly* correct here and won't raise a use-after-free bug. I do hold a misunderstanding of this one, sorry for that ~ But I still think this patch should be reconsidered: The kdoc of device_unregister explicitly mentions the possibility that other refs might continue to exist after device_unregister was called, and *del_device* is first part of it. By the way, 'del_device() called before put_device()' is everywhere in kernel code, like device_unregister(), pci_destroy_dev() or switchtec_pci_remove() In fact, I can't find another place in kernel code looks like: put_device(x); device_del(x); So I guess put_device() ought to be the last time we touch the object (I don't find evidence strong enough in kdoc to prove this) and putting put_device after device_del is a more natural logic. Qinglang . >