Linux PCI subsystem development
 help / color / mirror / Atom feed
From: Bjorn Helgaas <helgaas@kernel.org>
To: Ding Hui <dinghui@sangfor.com.cn>
Cc: Jay Fang <f.fangjian@huawei.com>,
	bhelgaas@google.com, linux-pci@vger.kernel.org,
	jonathan.cameron@huawei.com, prime.zeng@hisilicon.com
Subject: Re: [PATCH] PCI/ASPM: Update ASPM sysfs on MFD function removal to avoid use-after-free
Date: Wed, 31 Jul 2024 16:46:06 -0500	[thread overview]
Message-ID: <20240731214606.GA83038@bhelgaas> (raw)
In-Reply-To: <1580700a-95e6-45cc-a461-68fa1cc1b50c@sangfor.com.cn>

On Tue, Jul 30, 2024 at 05:57:43PM +0800, Ding Hui wrote:
> On 2024/7/30 9:16, Jay Fang wrote:
> >  From 'commit 456d8aa37d0f ("PCI/ASPM: Disable ASPM on MFD function removal
> > to avoid use-after-free")' we know that PCIe spec r6.0, sec 7.5.3.7,
> > recommends that software program the same ASPM Control(pcie_link_state)
> > value in all functions of multi-function devices, and free the
> > pcie_link_state when any child function is removed.
> > 
> > However, ASPM Control sysfs is still visible to other children even if it
> > has been removed by any child function, and careless use it will
> > trigger use-after-free error, e.g.:
> > 
> >    # lspci -tv
> >      -[0000:16]---00.0-[17]--+-00.0  Device 19e5:0222
> >                              \-00.1  Device 19e5:0222
> >    # echo 1 > /sys/bus/pci/devices/0000:17:00.0/remove       // pcie_link_state will be released
> >    # echo 1 > /sys/bus/pci/devices/0000:17:00.1/link/l1_aspm // will trigger error
> > 
> >    Unable to handle kernel NULL pointer dereference at virtual address 0000000000000030
> >    Call trace:
> >     aspm_attr_store_common.constprop.0+0x10c/0x154
> >     l1_aspm_store+0x24/0x30
> >     dev_attr_store+0x20/0x34
> >     sysfs_kf_write+0x4c/0x5c
> > 
> > We can solve this problem by updating the ASPM Control sysfs of all
> > children immediately after ASPM Control have been freed.
> > 
> > Fixes: 456d8aa37d0f ("PCI/ASPM: Disable ASPM on MFD function removal to avoid use-after-free")
> > Signed-off-by: Jay Fang <f.fangjian@huawei.com>
> > ---
> >   drivers/pci/pcie/aspm.c | 2 ++
> >   1 file changed, 2 insertions(+)
> > 
> > diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c
> > index cee2365e54b8..eee9e6739924 100644
> > --- a/drivers/pci/pcie/aspm.c
> > +++ b/drivers/pci/pcie/aspm.c
> > @@ -1262,6 +1262,8 @@ void pcie_aspm_exit_link_state(struct pci_dev *pdev)
> >   		pcie_config_aspm_path(parent_link);
> >   	}
> > +	pcie_aspm_update_sysfs_visibility(parent);
> > +
> 
> To be more rigorous, is there still a race window in
> aspm_attr_{show,store}_common or clkpm_{show,store} before updating
> the visibility, we can get an old or NULL pointer by
> pcie_aspm_get_link()?

Yeah, I think we still have a problem even with this patch.

For one thing, aspm_attr_store_common() captures the pointer from
pcie_aspm_get_link() before the critical section, so by the time it
*uses* the pointer, pcie_aspm_exit_link_state() may have freed the
link state.

And there are several other callers of pcie_aspm_get_link() that
either call it before a critical section or don't have a critical
section at all.

I think it may be a mistake to alloc/free the link state separately
from the pci_dev itself.

> >   	mutex_unlock(&aspm_lock);
> >   	up_read(&pci_bus_sem);
> >   }
> 
> -- 
> Thanks,
> - Ding Hui
> 

  reply	other threads:[~2024-07-31 21:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-30  1:16 [PATCH] PCI/ASPM: Update ASPM sysfs on MFD function removal to avoid use-after-free Jay Fang
2024-07-30  9:57 ` Ding Hui
2024-07-31 21:46   ` Bjorn Helgaas [this message]
2024-08-01 12:05     ` Jay Fang
2024-08-01 17:11       ` Bjorn Helgaas
2024-08-06 16:38         ` Ilpo Järvinen
2024-08-06 18:00           ` Bjorn Helgaas

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=20240731214606.GA83038@bhelgaas \
    --to=helgaas@kernel.org \
    --cc=bhelgaas@google.com \
    --cc=dinghui@sangfor.com.cn \
    --cc=f.fangjian@huawei.com \
    --cc=jonathan.cameron@huawei.com \
    --cc=linux-pci@vger.kernel.org \
    --cc=prime.zeng@hisilicon.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