From: Bjorn Helgaas <helgaas@kernel.org>
To: Dan Carpenter <dan.carpenter@linaro.org>
Cc: Ma Ke <make24@iscas.ac.cn>, Bjorn Helgaas <bhelgaas@google.com>,
linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] PCI: Fix double free in pci_register_host_bridge()
Date: Fri, 7 Mar 2025 13:50:33 -0600 [thread overview]
Message-ID: <20250307195033.GA424012@bhelgaas> (raw)
In-Reply-To: <db806a6c-a91b-4e5a-a84b-6b7e01bdac85@stanley.mountain>
On Fri, Mar 07, 2025 at 11:46:41AM +0300, Dan Carpenter wrote:
> Calling put_device(&bus->dev) will call release_pcibus_dev() which will
> free the bus. It leads to a use after free when we dereference "bus" in
> the cleanup code and the kfree(bus) is a double free.
>
> Fixes: b80b4d4972e6 ("PCI: Fix reference leak in pci_register_host_bridge()")
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
Squashed into the b80b4d4972e6 commit on pci/enumeration for v6.15,
thanks!
> ---
> drivers/pci/probe.c | 13 ++++++++-----
> 1 file changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
> index 819d23ce3565..c13f2c957002 100644
> --- a/drivers/pci/probe.c
> +++ b/drivers/pci/probe.c
> @@ -957,6 +957,7 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)
> resource_size_t offset, next_offset;
> LIST_HEAD(resources);
> struct resource *res, *next_res;
> + bool bus_registered = false;
> char addr[64], *fmt;
> const char *name;
> int err;
> @@ -1020,10 +1021,9 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)
> name = dev_name(&bus->dev);
>
> err = device_register(&bus->dev);
> - if (err) {
> - put_device(&bus->dev);
> + bus_registered = true;
> + if (err)
> goto unregister;
> - }
>
> pcibios_add_bus(bus);
>
> @@ -1110,12 +1110,15 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)
> unregister:
> put_device(&bridge->dev);
> device_del(&bridge->dev);
> -
> free:
> #ifdef CONFIG_PCI_DOMAINS_GENERIC
> pci_bus_release_domain_nr(parent, bus->domain_nr);
> #endif
> - kfree(bus);
> + if (bus_registered)
> + put_device(&bus->dev);
> + else
> + kfree(bus);
> +
> return err;
> }
>
> --
> 2.47.2
>
prev parent reply other threads:[~2025-03-07 19:50 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <cover.1741336994.git.dan.carpenter@linaro.org>
2025-03-07 8:46 ` [PATCH 1/2] PCI: remove stray put_device() in pci_register_host_bridge() Dan Carpenter
2025-03-07 19:50 ` Bjorn Helgaas
2025-03-07 8:46 ` [PATCH 2/2] PCI: Fix double free " Dan Carpenter
2025-03-07 19:50 ` Bjorn Helgaas [this message]
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=20250307195033.GA424012@bhelgaas \
--to=helgaas@kernel.org \
--cc=bhelgaas@google.com \
--cc=dan.carpenter@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=make24@iscas.ac.cn \
/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