From: Bjorn Helgaas <helgaas@kernel.org>
To: Niklas Schnelle <schnelle@linux.ibm.com>
Cc: Gerd Bayer <gbayer@linux.ibm.com>,
Gerald Schaefer <gerald.schaefer@linux.ibm.com>,
Heiko Carstens <hca@linux.ibm.com>,
Vasily Gorbik <gor@linux.ibm.com>,
Alexander Gordeev <agordeev@linux.ibm.com>,
Christian Borntraeger <borntraeger@linux.ibm.com>,
Sven Schnelle <svens@linux.ibm.com>,
Bjorn Helgaas <bhelgaas@google.com>,
Pierre Morel <pmorel@linux.ibm.com>,
Matthew Rosato <mjrosato@linux.ibm.com>,
linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-pci@vger.kernel.org
Subject: Re: [PATCH RESEND] PCI: s390: Fix use-after-free of PCI bus resources with s390 per-function hotplug
Date: Wed, 22 Feb 2023 16:02:20 -0600 [thread overview]
Message-ID: <20230222220220.GA3804275@bhelgaas> (raw)
In-Reply-To: <1a621a2b836d81d12b6f265f47d93b827e0a82df.camel@linux.ibm.com>
On Mon, Feb 20, 2023 at 01:53:34PM +0100, Niklas Schnelle wrote:
> On Fri, 2023-02-17 at 17:15 -0600, Bjorn Helgaas wrote:
> > On Tue, Feb 14, 2023 at 10:49:10AM +0100, Niklas Schnelle wrote:
> > > ...
> > What happens when zpci_bus_release() calls
> > pci_free_resource_list() on &zbus->resources? It looks like that
> > ultimately calls kfree(), which is OK for the
> > zpci_setup_bus_resources() stuff, but what about the
> > zbus->bus_resource that was not kalloc'ed?
>
> As far as I can see pci_free_resource_list() only calls kfree() on the
> entry not on entry->res. The resources set up in
> zpci_setup_bus_resources() are freed in zpci_cleanup_bus_resources()
> explicitly.
So I guess the zbus->resources are allocated in zpci_bus_scan_device()
where zpci_setup_bus_resources() adds a zbus resource for every
zpci_dev BAR, and freed in zpci_bus_release() when the last zpci_dev
is unregistered.
Does that mean that if you add device A, add device B, and remove A,
the zbus retains A's resources even though A is gone? What if you
then add device C whose resources partially overlap A's?
> > > static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
> > > {
> > > + struct resource *res;
> > > int i;
> > >
> > > + pci_lock_rescan_remove();
> >
> > What exactly is this protecting? This doesn't seem like quite the
> > right place since we're not adding/removing a pci_dev here. Is this
> > to protect the bus->resources list in pci_bus_remove_resource()?
>
> Yes I did not find a lock that is specifically for bus->resources but
> it seemed to me that changes to resources would only affect things
> running under the rescan/remove lock.
Yeah, OK.
> > > for (i = 0; i < PCI_STD_NUM_BARS; i++) {
> > > - if (!zdev->bars[i].size || !zdev->bars[i].res)
> > > + res = zdev->bars[i].res;
> > > + if (!res)
> > > continue;
> > >
> > > + release_resource(res);
> > > + pci_bus_remove_resource(zdev->zbus->bus, res);
> > > zpci_free_iomap(zdev, zdev->bars[i].map_idx);
> > > - release_resource(zdev->bars[i].res);
> > > - kfree(zdev->bars[i].res);
> > > + zdev->bars[i].res = NULL;
> > > + kfree(res);
> > > }
> > > zdev->has_resources = 0;
> > > + pci_unlock_rescan_remove();
next prev parent reply other threads:[~2023-02-22 22:02 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-14 9:49 [PATCH RESEND] PCI: s390: Fix use-after-free of PCI bus resources with s390 per-function hotplug Niklas Schnelle
2023-02-17 23:15 ` Bjorn Helgaas
2023-02-20 12:53 ` Niklas Schnelle
2023-02-22 16:54 ` Niklas Schnelle
2023-02-23 19:53 ` Bjorn Helgaas
2023-02-24 4:19 ` Lukas Wunner
2023-02-28 9:08 ` Niklas Schnelle
2023-03-08 18:38 ` Bjorn Helgaas
2023-02-22 22:02 ` Bjorn Helgaas [this message]
2023-02-23 11:22 ` Niklas Schnelle
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=20230222220220.GA3804275@bhelgaas \
--to=helgaas@kernel.org \
--cc=agordeev@linux.ibm.com \
--cc=bhelgaas@google.com \
--cc=borntraeger@linux.ibm.com \
--cc=gbayer@linux.ibm.com \
--cc=gerald.schaefer@linux.ibm.com \
--cc=gor@linux.ibm.com \
--cc=hca@linux.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=mjrosato@linux.ibm.com \
--cc=pmorel@linux.ibm.com \
--cc=schnelle@linux.ibm.com \
--cc=svens@linux.ibm.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;
as well as URLs for NNTP newsgroup(s).