public inbox for linux-pci@vger.kernel.org
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: "Wassenberg, Dennis" <Dennis.Wassenberg@secunet.com>
Cc: "linux-pci@vger.kernel.org" <linux-pci@vger.kernel.org>,
	 "kbusch@kernel.org" <kbusch@kernel.org>,
	 "mika.westerberg@linux.intel.com"
	<mika.westerberg@linux.intel.com>,
	 "mpearson-lenovo@squebb.ca" <mpearson-lenovo@squebb.ca>,
	 "Jonathan.Cameron@huawei.com" <Jonathan.Cameron@huawei.com>,
	 "minipli@grsecurity.net" <minipli@grsecurity.net>,
	 "lukas@wunner.de" <lukas@wunner.de>
Subject: Re: UAF during boot on MTL based devices with attached dock
Date: Tue, 24 Sep 2024 13:51:51 +0300 (EEST)	[thread overview]
Message-ID: <68de3ca4-a624-8b02-8f6d-889deb61495d@linux.intel.com> (raw)
In-Reply-To: <c394a3f07bfb7240a2c32fa6d467ea1a03547881.camel@secunet.com>

On Mon, 23 Sep 2024, Wassenberg, Dennis wrote:

> Hi together,
> 
> we did some further analysis on this:
> 
> Because we are working on kernel 6.8.12, I will use some logs from this kernel version, just for demonstration. The
> initial report was based on 6.11.
> 
> After we tried a KASAN build (dmesg-ramoops-kasan) it looks like it is exactly the same pciehp flow which leads to the
> UAF.
> Both going through pciehp_ist -> pciehp_disable_slot -> pciehp_unconfigure_device -> pci_remove_bus_device -> ...
> This means there are two consecutive interrupts, running on CPU 12 and both will execute the same flow.
> At the latest the pci_lock_rescan_remove should be taken in pciehp_unconfigure_device to prevent accessing the pci/bus
> structures in parallel.
> 
> I had a look if there are shared data structures accessed in this code path:
> For me the access to "*parent = ctrl->pcie->port->subordinate;" looks fishy in pciehp_unconfigure_device. The parent ptr
> will be obtained before getting the lock (pci_lock_rescan_remove). Now, if there are two concurrent/consecutive flows
> come into this function, both will get the pointer to the parent bridge/subordinate. One thread will enter the lock and
> the other one is waiting until the lock is gone. The thread which enters the lock at first will completely remove the
> bridge and the subordinate: pciehp_unconfigure_device -> pci_stop_and_remove_bus_device -> pci_remove_bus_device ->
> pci_destroy_dev: This will destroy the pci_dev and the subordinate is a part the this structure as well. Now everything
> is gone below this pci_bus (childs included). In pci_remove_bus_device there is a loop which iterates over all child
> devices and call pci_remove_bus_device again. This means even the child bridges of the current bridge will be deleted.
> In the end: everything is gone below the bridge which is regarded here at first.

Doesn't that end up removing portdrv/hotplug too so pciehp_remove() does 
release ctrl? I'm not sure if ctrl can be safely accessed even if the 
lock is taken first?

-- 
 i.

> After this the thread leaves the lock with pci_unlock_rescan_remove in pciehp_unconfigure_device. Now the second
> thread/ISR will enter the lock. If the second thread belongs to a child bridge of the bridge which was already removed,
> it will run into an UAF. This is because the parent bridge destroys all child bridges as well, but the second thread
> gets the subordinate pointer before accessing the lock. This means it could be possible  hat the second thread uses the
> already destroyed subordinate pointer which makes the subordinate invalid. Accessing the pci_bus structure via this
> subordinate will definitely run into an UAF.
> 
> In addition we looked closer at pci_stop_and_remove_bus_device_locked() and noticed that while pci_stop_bus_device() is
> also walking the ->devices list in reverse order, pci_remove_bus_device() isn't. Maybe it should, to ensure a consistent
> order of destruction?
>
> We addressed both with the following patch: v2-0001-PCI-pcihp-fix-subordinate-access-in-pciehp_unconf.patch
> 
> Whats your thoughts about this?
> 
> 
> After applying this patch on top of 6.8.12 the initial UAF is gone (the one shown in dmesg-ramoops-kasan), but a
> different UAF comes up (dmesg-ramoops-kasan-v2). This new one is more similar to the one which I reported initially on
> Kernel 6.11. I think even though the UAF in dmesg-ramoops-kasan is not easy to reproduce on vanilla 6.11, because an
> other one will happen, it is a valid fix which should be applied anyway because the code in 6.11 and 6.8.18 doesn't
> differ in this area.
> I attached a KASAN log as well where both patches are integrated: (v2-0001-PCI-pcihp-fix-subordinate-access-in-
> pciehp_unconf.patch + PCI: Don't access freed bus in pci_slot_release() from Ilpo (dmesg-ramoops-kasan-v2+patch_ilpo).
> 
> 
> In addition I am currently trying to reproduce this on vanilla 6.11 with activated KASAN but I was not lucky enough to
> catch this until yet (without KASAN it is easy to reproduce for me).
> 
> Thank you & best regards,
> Dennis
> 
> 
> 
> On Thu, 2024-09-19 at 10:06 +0200, Dennis Wassenberg wrote:
> > Hi together,
> > 
> > we are facing into issues which seems to be PCI related and asking for your estimations.
> > 
> > Background:
> > We want to boot up an Intel MeteorLake based system (e.g. Lenovo ThinkPad X13 Gen5) with the Lenovo Thunderbolt 4
> > universal dock attached during boot. On some devices it is nearly 100% reproducible that the boot will fail. Other
> > systems will never show this issue (e.g. older devices based on RaptorLake or AlderLake platform).
> > 
> > We did some debugging on this and came to the conclusion that there is a use-after-free in pci_slot_release.
> > The Thunderbolt 4 Dock will expose a PCI hierarchy at first and shortly after that, due to the device is inaccessible,
> > it will release the additional buses/ports. This seems to end up in a race where pci_slot_release accesses &slot->bus
> > which as already freed:
> > 
> > 0000:00 [root bus]
> >       -> 0000:00:07.0 [bridge to 20-49]
> >                      -> 0000:20:00.0 [bridge to 21-49]
> >                                     -> 0000:21:00.0 [bridge to 22]
> >                                        0000:21:01.0 [bridge to 23-2e]
> >                                        0000:21:02.0 [bridge to 2f-3a]
> >                                        0000:21:03.0 [bridge to 3b-48]
> >                                        0000:21:04.0 [bridge to 49]
> >          0000:00:07.2 [bridge to 50-79]
> > 
> > 
> > We are currently running on kernel 6.8.12. Because this kernel is out of support I tried it on 6.11. This kernel shows
> > exactly the same issue. I attached two log files:
> > dmesg-ramoops-0: Based on kernel 6.11 with added kernel command line option "slab_debug" in order to force a kernel
> > Oops
> > while accessing freed memory.
> > dmesg-ramoops-0-pci_dbg: This it like dmesg-ramoops-0 with additional kernel command line option '"dyndbg=file
> > drivers/pci/* +p" ignore_loglevel' in order to give you more insight whats happening on the pci bus.
> > 
> > I would appreciate any kind of help on this.
> > 
> > Thank you & best regards,
> > Dennis
> > 
> > 
> 
> 

  reply	other threads:[~2024-09-24 10:51 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-19  8:06 UAF during boot on MTL based devices with attached dock Wassenberg, Dennis
2024-09-21  9:08 ` Ilpo Järvinen
2024-09-23  8:38   ` Wassenberg, Dennis
2024-09-23  4:41 ` mika.westerberg
2024-09-23  8:43   ` Wassenberg, Dennis
2024-09-23 11:17     ` mika.westerberg
2024-09-23 13:42       ` Wassenberg, Dennis
2024-09-23 12:23 ` Wassenberg, Dennis
2024-09-24 10:51   ` Ilpo Järvinen [this message]
2024-09-25 15:38     ` Wassenberg, Dennis
2024-09-26 13:58       ` Ilpo Järvinen
2024-10-07 16:34         ` Wassenberg, Dennis
2024-10-03 13:46       ` Lukas Wunner
2024-10-04  7:45         ` Lukas Wunner
2024-10-07 16:49           ` Wassenberg, Dennis
2024-10-08 13:58             ` Lukas Wunner
2024-10-08 16:37               ` mika.westerberg
2024-10-08 18:23                 ` Lukas Wunner
2024-10-09  4:44                   ` mika.westerberg
2024-10-09 11:47                     ` Lukas Wunner
2024-10-09 12:55                       ` mika.westerberg
2024-10-09  6:26               ` Wassenberg, Dennis
2024-10-07 16:20         ` Wassenberg, Dennis
2024-09-24  8:54 ` Lukas Wunner

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=68de3ca4-a624-8b02-8f6d-889deb61495d@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=Dennis.Wassenberg@secunet.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=kbusch@kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=lukas@wunner.de \
    --cc=mika.westerberg@linux.intel.com \
    --cc=minipli@grsecurity.net \
    --cc=mpearson-lenovo@squebb.ca \
    /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